xref: /openbmc/linux/fs/namei.c (revision 9875cf80)
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 /**
371b3e19d92SNick Piggin  * path_get_long - get a long reference to a path
372b3e19d92SNick Piggin  * @path: path to get the reference to
373b3e19d92SNick Piggin  *
374b3e19d92SNick Piggin  * Given a path increment the reference count to the dentry and the vfsmount.
375b3e19d92SNick Piggin  */
376b3e19d92SNick Piggin void path_get_long(struct path *path)
377b3e19d92SNick Piggin {
378b3e19d92SNick Piggin 	mntget_long(path->mnt);
379b3e19d92SNick Piggin 	dget(path->dentry);
380b3e19d92SNick Piggin }
381b3e19d92SNick Piggin 
382b3e19d92SNick Piggin /**
3831d957f9bSJan Blunck  * path_put - put a reference to a path
3841d957f9bSJan Blunck  * @path: path to put the reference to
3851d957f9bSJan Blunck  *
3861d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
3871d957f9bSJan Blunck  */
3881d957f9bSJan Blunck void path_put(struct path *path)
3891da177e4SLinus Torvalds {
3901d957f9bSJan Blunck 	dput(path->dentry);
3911d957f9bSJan Blunck 	mntput(path->mnt);
3921da177e4SLinus Torvalds }
3931d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
3941da177e4SLinus Torvalds 
395834f2a4aSTrond Myklebust /**
396b3e19d92SNick Piggin  * path_put_long - put a long reference to a path
397b3e19d92SNick Piggin  * @path: path to put the reference to
398b3e19d92SNick Piggin  *
399b3e19d92SNick Piggin  * Given a path decrement the reference count to the dentry and the vfsmount.
400b3e19d92SNick Piggin  */
401b3e19d92SNick Piggin void path_put_long(struct path *path)
402b3e19d92SNick Piggin {
403b3e19d92SNick Piggin 	dput(path->dentry);
404b3e19d92SNick Piggin 	mntput_long(path->mnt);
405b3e19d92SNick Piggin }
406b3e19d92SNick Piggin 
407b3e19d92SNick Piggin /**
40831e6b01fSNick Piggin  * nameidata_drop_rcu - drop this nameidata out of rcu-walk
40931e6b01fSNick Piggin  * @nd: nameidata pathwalk data to drop
41039191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
41131e6b01fSNick Piggin  *
41231e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
41331e6b01fSNick Piggin  * Documentation/filesystems/path-lookup.txt). __drop_rcu* functions attempt
41431e6b01fSNick Piggin  * to drop out of rcu-walk mode and take normal reference counts on dentries
41531e6b01fSNick Piggin  * and vfsmounts to transition to rcu-walk mode. __drop_rcu* functions take
41631e6b01fSNick Piggin  * refcounts at the last known good point before rcu-walk got stuck, so
41731e6b01fSNick Piggin  * ref-walk may continue from there. If this is not successful (eg. a seqcount
41831e6b01fSNick Piggin  * has changed), then failure is returned and path walk restarts from the
41931e6b01fSNick Piggin  * beginning in ref-walk mode.
42031e6b01fSNick Piggin  *
42131e6b01fSNick Piggin  * nameidata_drop_rcu attempts to drop the current nd->path and nd->root into
42231e6b01fSNick Piggin  * ref-walk. Must be called from rcu-walk context.
42331e6b01fSNick Piggin  */
42431e6b01fSNick Piggin static int nameidata_drop_rcu(struct nameidata *nd)
42531e6b01fSNick Piggin {
42631e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
42731e6b01fSNick Piggin 	struct dentry *dentry = nd->path.dentry;
42831e6b01fSNick Piggin 
42931e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
43031e6b01fSNick Piggin 	if (nd->root.mnt) {
43131e6b01fSNick Piggin 		spin_lock(&fs->lock);
43231e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
43331e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
43431e6b01fSNick Piggin 			goto err_root;
43531e6b01fSNick Piggin 	}
43631e6b01fSNick Piggin 	spin_lock(&dentry->d_lock);
43731e6b01fSNick Piggin 	if (!__d_rcu_to_refcount(dentry, nd->seq))
43831e6b01fSNick Piggin 		goto err;
43931e6b01fSNick Piggin 	BUG_ON(nd->inode != dentry->d_inode);
44031e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
44131e6b01fSNick Piggin 	if (nd->root.mnt) {
44231e6b01fSNick Piggin 		path_get(&nd->root);
44331e6b01fSNick Piggin 		spin_unlock(&fs->lock);
44431e6b01fSNick Piggin 	}
44531e6b01fSNick Piggin 	mntget(nd->path.mnt);
44631e6b01fSNick Piggin 
44731e6b01fSNick Piggin 	rcu_read_unlock();
44831e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
44931e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
45031e6b01fSNick Piggin 	return 0;
45131e6b01fSNick Piggin err:
45231e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
45331e6b01fSNick Piggin err_root:
45431e6b01fSNick Piggin 	if (nd->root.mnt)
45531e6b01fSNick Piggin 		spin_unlock(&fs->lock);
45631e6b01fSNick Piggin 	return -ECHILD;
45731e6b01fSNick Piggin }
45831e6b01fSNick Piggin 
45931e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing.  */
46031e6b01fSNick Piggin static inline int nameidata_drop_rcu_maybe(struct nameidata *nd)
46131e6b01fSNick Piggin {
46231e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU)
46331e6b01fSNick Piggin 		return nameidata_drop_rcu(nd);
46431e6b01fSNick Piggin 	return 0;
46531e6b01fSNick Piggin }
46631e6b01fSNick Piggin 
46731e6b01fSNick Piggin /**
46831e6b01fSNick Piggin  * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk
46931e6b01fSNick Piggin  * @nd: nameidata pathwalk data to drop
47031e6b01fSNick Piggin  * @dentry: dentry to drop
47139191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
47231e6b01fSNick Piggin  *
47331e6b01fSNick Piggin  * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root,
47431e6b01fSNick Piggin  * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on
47531e6b01fSNick Piggin  * @nd. Must be called from rcu-walk context.
47631e6b01fSNick Piggin  */
47731e6b01fSNick Piggin static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry)
47831e6b01fSNick Piggin {
47931e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
48031e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
48131e6b01fSNick Piggin 
48290dbb77bSNick Piggin 	/*
48390dbb77bSNick Piggin 	 * It can be possible to revalidate the dentry that we started
48490dbb77bSNick Piggin 	 * the path walk with. force_reval_path may also revalidate the
48590dbb77bSNick Piggin 	 * dentry already committed to the nameidata.
48690dbb77bSNick Piggin 	 */
48790dbb77bSNick Piggin 	if (unlikely(parent == dentry))
48890dbb77bSNick Piggin 		return nameidata_drop_rcu(nd);
48990dbb77bSNick Piggin 
49031e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
49131e6b01fSNick Piggin 	if (nd->root.mnt) {
49231e6b01fSNick Piggin 		spin_lock(&fs->lock);
49331e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
49431e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
49531e6b01fSNick Piggin 			goto err_root;
49631e6b01fSNick Piggin 	}
49731e6b01fSNick Piggin 	spin_lock(&parent->d_lock);
49831e6b01fSNick Piggin 	spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
49931e6b01fSNick Piggin 	if (!__d_rcu_to_refcount(dentry, nd->seq))
50031e6b01fSNick Piggin 		goto err;
50131e6b01fSNick Piggin 	/*
50231e6b01fSNick Piggin 	 * If the sequence check on the child dentry passed, then the child has
50331e6b01fSNick Piggin 	 * not been removed from its parent. This means the parent dentry must
50431e6b01fSNick Piggin 	 * be valid and able to take a reference at this point.
50531e6b01fSNick Piggin 	 */
50631e6b01fSNick Piggin 	BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
50731e6b01fSNick Piggin 	BUG_ON(!parent->d_count);
50831e6b01fSNick Piggin 	parent->d_count++;
50931e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
51031e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
51131e6b01fSNick Piggin 	if (nd->root.mnt) {
51231e6b01fSNick Piggin 		path_get(&nd->root);
51331e6b01fSNick Piggin 		spin_unlock(&fs->lock);
51431e6b01fSNick Piggin 	}
51531e6b01fSNick Piggin 	mntget(nd->path.mnt);
51631e6b01fSNick Piggin 
51731e6b01fSNick Piggin 	rcu_read_unlock();
51831e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
51931e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
52031e6b01fSNick Piggin 	return 0;
52131e6b01fSNick Piggin err:
52231e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
52331e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
52431e6b01fSNick Piggin err_root:
52531e6b01fSNick Piggin 	if (nd->root.mnt)
52631e6b01fSNick Piggin 		spin_unlock(&fs->lock);
52731e6b01fSNick Piggin 	return -ECHILD;
52831e6b01fSNick Piggin }
52931e6b01fSNick Piggin 
53031e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing.  */
53131e6b01fSNick Piggin static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry)
53231e6b01fSNick Piggin {
53331e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU)
53431e6b01fSNick Piggin 		return nameidata_dentry_drop_rcu(nd, dentry);
53531e6b01fSNick Piggin 	return 0;
53631e6b01fSNick Piggin }
53731e6b01fSNick Piggin 
53831e6b01fSNick Piggin /**
53931e6b01fSNick Piggin  * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk
54031e6b01fSNick Piggin  * @nd: nameidata pathwalk data to drop
54139191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
54231e6b01fSNick Piggin  *
54331e6b01fSNick Piggin  * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk.
54431e6b01fSNick Piggin  * nd->path should be the final element of the lookup, so nd->root is discarded.
54531e6b01fSNick Piggin  * Must be called from rcu-walk context.
54631e6b01fSNick Piggin  */
54731e6b01fSNick Piggin static int nameidata_drop_rcu_last(struct nameidata *nd)
54831e6b01fSNick Piggin {
54931e6b01fSNick Piggin 	struct dentry *dentry = nd->path.dentry;
55031e6b01fSNick Piggin 
55131e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
55231e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
55331e6b01fSNick Piggin 	nd->root.mnt = NULL;
55431e6b01fSNick Piggin 	spin_lock(&dentry->d_lock);
55531e6b01fSNick Piggin 	if (!__d_rcu_to_refcount(dentry, nd->seq))
55631e6b01fSNick Piggin 		goto err_unlock;
55731e6b01fSNick Piggin 	BUG_ON(nd->inode != dentry->d_inode);
55831e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
55931e6b01fSNick Piggin 
56031e6b01fSNick Piggin 	mntget(nd->path.mnt);
56131e6b01fSNick Piggin 
56231e6b01fSNick Piggin 	rcu_read_unlock();
56331e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
56431e6b01fSNick Piggin 
56531e6b01fSNick Piggin 	return 0;
56631e6b01fSNick Piggin 
56731e6b01fSNick Piggin err_unlock:
56831e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
56931e6b01fSNick Piggin 	rcu_read_unlock();
57031e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
57131e6b01fSNick Piggin 	return -ECHILD;
57231e6b01fSNick Piggin }
57331e6b01fSNick Piggin 
57431e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing.  */
57531e6b01fSNick Piggin static inline int nameidata_drop_rcu_last_maybe(struct nameidata *nd)
57631e6b01fSNick Piggin {
57731e6b01fSNick Piggin 	if (likely(nd->flags & LOOKUP_RCU))
57831e6b01fSNick Piggin 		return nameidata_drop_rcu_last(nd);
57931e6b01fSNick Piggin 	return 0;
58031e6b01fSNick Piggin }
58131e6b01fSNick Piggin 
58231e6b01fSNick Piggin /**
583834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
584834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
585834f2a4aSTrond Myklebust  */
586834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
587834f2a4aSTrond Myklebust {
5880f7fc9e4SJosef "Jeff" Sipek 	if (nd->intent.open.file->f_path.dentry == NULL)
589834f2a4aSTrond Myklebust 		put_filp(nd->intent.open.file);
590834f2a4aSTrond Myklebust 	else
591834f2a4aSTrond Myklebust 		fput(nd->intent.open.file);
592834f2a4aSTrond Myklebust }
593834f2a4aSTrond Myklebust 
594bb20c18dSNick Piggin /*
595bb20c18dSNick Piggin  * Call d_revalidate and handle filesystems that request rcu-walk
596bb20c18dSNick Piggin  * to be dropped. This may be called and return in rcu-walk mode,
597bb20c18dSNick Piggin  * regardless of success or error. If -ECHILD is returned, the caller
598bb20c18dSNick Piggin  * must return -ECHILD back up the path walk stack so path walk may
599bb20c18dSNick Piggin  * be restarted in ref-walk mode.
600bb20c18dSNick Piggin  */
60134286d66SNick Piggin static int d_revalidate(struct dentry *dentry, struct nameidata *nd)
60234286d66SNick Piggin {
60334286d66SNick Piggin 	int status;
60434286d66SNick Piggin 
60534286d66SNick Piggin 	status = dentry->d_op->d_revalidate(dentry, nd);
60634286d66SNick Piggin 	if (status == -ECHILD) {
60734286d66SNick Piggin 		if (nameidata_dentry_drop_rcu(nd, dentry))
60834286d66SNick Piggin 			return status;
60934286d66SNick Piggin 		status = dentry->d_op->d_revalidate(dentry, nd);
61034286d66SNick Piggin 	}
61134286d66SNick Piggin 
61234286d66SNick Piggin 	return status;
61334286d66SNick Piggin }
61434286d66SNick Piggin 
615bcdc5e01SIan Kent static inline struct dentry *
616bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd)
617bcdc5e01SIan Kent {
61834286d66SNick Piggin 	int status;
61934286d66SNick Piggin 
62034286d66SNick Piggin 	status = d_revalidate(dentry, nd);
621bcdc5e01SIan Kent 	if (unlikely(status <= 0)) {
622bcdc5e01SIan Kent 		/*
623bcdc5e01SIan Kent 		 * The dentry failed validation.
624bcdc5e01SIan Kent 		 * If d_revalidate returned 0 attempt to invalidate
625bcdc5e01SIan Kent 		 * the dentry otherwise d_revalidate is asking us
626bcdc5e01SIan Kent 		 * to return a fail status.
627bcdc5e01SIan Kent 		 */
62834286d66SNick Piggin 		if (status < 0) {
62934286d66SNick Piggin 			/* If we're in rcu-walk, we don't have a ref */
63034286d66SNick Piggin 			if (!(nd->flags & LOOKUP_RCU))
63134286d66SNick Piggin 				dput(dentry);
63234286d66SNick Piggin 			dentry = ERR_PTR(status);
63334286d66SNick Piggin 
63434286d66SNick Piggin 		} else {
63534286d66SNick Piggin 			/* Don't d_invalidate in rcu-walk mode */
63634286d66SNick Piggin 			if (nameidata_dentry_drop_rcu_maybe(nd, dentry))
63734286d66SNick Piggin 				return ERR_PTR(-ECHILD);
638bcdc5e01SIan Kent 			if (!d_invalidate(dentry)) {
639bcdc5e01SIan Kent 				dput(dentry);
640bcdc5e01SIan Kent 				dentry = NULL;
641bcdc5e01SIan Kent 			}
642bcdc5e01SIan Kent 		}
643bcdc5e01SIan Kent 	}
644bcdc5e01SIan Kent 	return dentry;
645bcdc5e01SIan Kent }
646bcdc5e01SIan Kent 
647fb045adbSNick Piggin static inline int need_reval_dot(struct dentry *dentry)
648fb045adbSNick Piggin {
649fb045adbSNick Piggin 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
650fb045adbSNick Piggin 		return 0;
651fb045adbSNick Piggin 
652fb045adbSNick Piggin 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
653fb045adbSNick Piggin 		return 0;
654fb045adbSNick Piggin 
655fb045adbSNick Piggin 	return 1;
656fb045adbSNick Piggin }
657fb045adbSNick Piggin 
6581da177e4SLinus Torvalds /*
65939159de2SJeff Layton  * force_reval_path - force revalidation of a dentry
66039159de2SJeff Layton  *
66139159de2SJeff Layton  * In some situations the path walking code will trust dentries without
66239159de2SJeff Layton  * revalidating them. This causes problems for filesystems that depend on
66339159de2SJeff Layton  * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
66439159de2SJeff Layton  * (which indicates that it's possible for the dentry to go stale), force
66539159de2SJeff Layton  * a d_revalidate call before proceeding.
66639159de2SJeff Layton  *
66739159de2SJeff Layton  * Returns 0 if the revalidation was successful. If the revalidation fails,
66839159de2SJeff Layton  * either return the error returned by d_revalidate or -ESTALE if the
66939159de2SJeff Layton  * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
67039159de2SJeff Layton  * invalidate the dentry. It's up to the caller to handle putting references
67139159de2SJeff Layton  * to the path if necessary.
67239159de2SJeff Layton  */
67339159de2SJeff Layton static int
67439159de2SJeff Layton force_reval_path(struct path *path, struct nameidata *nd)
67539159de2SJeff Layton {
67639159de2SJeff Layton 	int status;
67739159de2SJeff Layton 	struct dentry *dentry = path->dentry;
67839159de2SJeff Layton 
67939159de2SJeff Layton 	/*
68039159de2SJeff Layton 	 * only check on filesystems where it's possible for the dentry to
681fb045adbSNick Piggin 	 * become stale.
68239159de2SJeff Layton 	 */
683fb045adbSNick Piggin 	if (!need_reval_dot(dentry))
68439159de2SJeff Layton 		return 0;
68539159de2SJeff Layton 
68634286d66SNick Piggin 	status = d_revalidate(dentry, nd);
68739159de2SJeff Layton 	if (status > 0)
68839159de2SJeff Layton 		return 0;
68939159de2SJeff Layton 
69039159de2SJeff Layton 	if (!status) {
691bb20c18dSNick Piggin 		/* Don't d_invalidate in rcu-walk mode */
692bb20c18dSNick Piggin 		if (nameidata_drop_rcu(nd))
693bb20c18dSNick Piggin 			return -ECHILD;
69439159de2SJeff Layton 		d_invalidate(dentry);
69539159de2SJeff Layton 		status = -ESTALE;
69639159de2SJeff Layton 	}
69739159de2SJeff Layton 	return status;
69839159de2SJeff Layton }
69939159de2SJeff Layton 
70039159de2SJeff Layton /*
701b75b5086SAl Viro  * Short-cut version of permission(), for calling on directories
702b75b5086SAl Viro  * during pathname resolution.  Combines parts of permission()
703b75b5086SAl Viro  * and generic_permission(), and tests ONLY for MAY_EXEC permission.
7041da177e4SLinus Torvalds  *
7051da177e4SLinus Torvalds  * If appropriate, check DAC only.  If not appropriate, or
706b75b5086SAl Viro  * short-cut DAC fails, then call ->permission() to do more
7071da177e4SLinus Torvalds  * complete permission check.
7081da177e4SLinus Torvalds  */
709b74c79e9SNick Piggin static inline int exec_permission(struct inode *inode, unsigned int flags)
7101da177e4SLinus Torvalds {
7115909ccaaSLinus Torvalds 	int ret;
7121da177e4SLinus Torvalds 
713cb9179eaSLinus Torvalds 	if (inode->i_op->permission) {
714b74c79e9SNick Piggin 		ret = inode->i_op->permission(inode, MAY_EXEC, flags);
715b74c79e9SNick Piggin 	} else {
716b74c79e9SNick Piggin 		ret = acl_permission_check(inode, MAY_EXEC, flags,
717b74c79e9SNick Piggin 				inode->i_op->check_acl);
718cb9179eaSLinus Torvalds 	}
719b74c79e9SNick Piggin 	if (likely(!ret))
7201da177e4SLinus Torvalds 		goto ok;
721b74c79e9SNick Piggin 	if (ret == -ECHILD)
72231e6b01fSNick Piggin 		return ret;
7231da177e4SLinus Torvalds 
724f1ac9f6bSLinus Torvalds 	if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH))
7251da177e4SLinus Torvalds 		goto ok;
7261da177e4SLinus Torvalds 
7275909ccaaSLinus Torvalds 	return ret;
7281da177e4SLinus Torvalds ok:
729b74c79e9SNick Piggin 	return security_inode_exec_permission(inode, flags);
7301da177e4SLinus Torvalds }
7311da177e4SLinus Torvalds 
7322a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
7332a737871SAl Viro {
734f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
735f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
7362a737871SAl Viro }
7372a737871SAl Viro 
7386de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
7396de88d72SAl Viro 
74031e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
74131e6b01fSNick Piggin {
74231e6b01fSNick Piggin 	if (!nd->root.mnt) {
74331e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
744c28cc364SNick Piggin 		unsigned seq;
745c28cc364SNick Piggin 
746c28cc364SNick Piggin 		do {
747c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
74831e6b01fSNick Piggin 			nd->root = fs->root;
749c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
75031e6b01fSNick Piggin 	}
75131e6b01fSNick Piggin }
75231e6b01fSNick Piggin 
753f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
7541da177e4SLinus Torvalds {
75531e6b01fSNick Piggin 	int ret;
75631e6b01fSNick Piggin 
7571da177e4SLinus Torvalds 	if (IS_ERR(link))
7581da177e4SLinus Torvalds 		goto fail;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	if (*link == '/') {
7612a737871SAl Viro 		set_root(nd);
7621d957f9bSJan Blunck 		path_put(&nd->path);
7632a737871SAl Viro 		nd->path = nd->root;
7642a737871SAl Viro 		path_get(&nd->root);
7651da177e4SLinus Torvalds 	}
76631e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
767b4091d5fSChristoph Hellwig 
76831e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
76931e6b01fSNick Piggin 	return ret;
7701da177e4SLinus Torvalds fail:
7711d957f9bSJan Blunck 	path_put(&nd->path);
7721da177e4SLinus Torvalds 	return PTR_ERR(link);
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
7751d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
776051d3812SIan Kent {
777051d3812SIan Kent 	dput(path->dentry);
7784ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
779051d3812SIan Kent 		mntput(path->mnt);
780051d3812SIan Kent }
781051d3812SIan Kent 
7827b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
7837b9337aaSNick Piggin 					struct nameidata *nd)
784051d3812SIan Kent {
78531e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
7864ac91378SJan Blunck 		dput(nd->path.dentry);
78731e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
7884ac91378SJan Blunck 			mntput(nd->path.mnt);
7899a229683SHuang Shijie 	}
79031e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
7914ac91378SJan Blunck 	nd->path.dentry = path->dentry;
792051d3812SIan Kent }
793051d3812SIan Kent 
794def4af30SAl Viro static __always_inline int
7957b9337aaSNick Piggin __do_follow_link(const struct path *link, struct nameidata *nd, void **p)
7961da177e4SLinus Torvalds {
7971da177e4SLinus Torvalds 	int error;
7987b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
7991da177e4SLinus Torvalds 
8007b9337aaSNick Piggin 	touch_atime(link->mnt, dentry);
8011da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
802cd4e91d3SAl Viro 
8037b9337aaSNick Piggin 	if (link->mnt != nd->path.mnt) {
8047b9337aaSNick Piggin 		path_to_nameidata(link, nd);
80531e6b01fSNick Piggin 		nd->inode = nd->path.dentry->d_inode;
806051d3812SIan Kent 		dget(dentry);
807051d3812SIan Kent 	}
8087b9337aaSNick Piggin 	mntget(link->mnt);
80931e6b01fSNick Piggin 
81086acdca1SAl Viro 	nd->last_type = LAST_BIND;
811def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
812def4af30SAl Viro 	error = PTR_ERR(*p);
813def4af30SAl Viro 	if (!IS_ERR(*p)) {
8141da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
815cc314eefSLinus Torvalds 		error = 0;
8161da177e4SLinus Torvalds 		if (s)
8171da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
81839159de2SJeff Layton 		else if (nd->last_type == LAST_BIND) {
81939159de2SJeff Layton 			error = force_reval_path(&nd->path, nd);
82039159de2SJeff Layton 			if (error)
82139159de2SJeff Layton 				path_put(&nd->path);
82239159de2SJeff Layton 		}
8231da177e4SLinus Torvalds 	}
8241da177e4SLinus Torvalds 	return error;
8251da177e4SLinus Torvalds }
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds /*
8281da177e4SLinus Torvalds  * This limits recursive symlink follows to 8, while
8291da177e4SLinus Torvalds  * limiting consecutive symlinks to 40.
8301da177e4SLinus Torvalds  *
8311da177e4SLinus Torvalds  * Without that kind of total limit, nasty chains of consecutive
8321da177e4SLinus Torvalds  * symlinks can cause almost arbitrarily long lookups.
8331da177e4SLinus Torvalds  */
83490ebe565SAl Viro static inline int do_follow_link(struct path *path, struct nameidata *nd)
8351da177e4SLinus Torvalds {
836def4af30SAl Viro 	void *cookie;
8371da177e4SLinus Torvalds 	int err = -ELOOP;
8381da177e4SLinus Torvalds 	if (current->link_count >= MAX_NESTED_LINKS)
8391da177e4SLinus Torvalds 		goto loop;
8401da177e4SLinus Torvalds 	if (current->total_link_count >= 40)
8411da177e4SLinus Torvalds 		goto loop;
8421da177e4SLinus Torvalds 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
8431da177e4SLinus Torvalds 	cond_resched();
84490ebe565SAl Viro 	err = security_inode_follow_link(path->dentry, nd);
8451da177e4SLinus Torvalds 	if (err)
8461da177e4SLinus Torvalds 		goto loop;
8471da177e4SLinus Torvalds 	current->link_count++;
8481da177e4SLinus Torvalds 	current->total_link_count++;
8491da177e4SLinus Torvalds 	nd->depth++;
850def4af30SAl Viro 	err = __do_follow_link(path, nd, &cookie);
851def4af30SAl Viro 	if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link)
852def4af30SAl Viro 		path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie);
853258fa999SAl Viro 	path_put(path);
8541da177e4SLinus Torvalds 	current->link_count--;
8551da177e4SLinus Torvalds 	nd->depth--;
8561da177e4SLinus Torvalds 	return err;
8571da177e4SLinus Torvalds loop:
8581d957f9bSJan Blunck 	path_put_conditional(path, nd);
8591d957f9bSJan Blunck 	path_put(&nd->path);
8601da177e4SLinus Torvalds 	return err;
8611da177e4SLinus Torvalds }
8621da177e4SLinus Torvalds 
86331e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
86431e6b01fSNick Piggin {
86531e6b01fSNick Piggin 	struct vfsmount *parent;
86631e6b01fSNick Piggin 	struct dentry *mountpoint;
86731e6b01fSNick Piggin 
86831e6b01fSNick Piggin 	parent = path->mnt->mnt_parent;
86931e6b01fSNick Piggin 	if (parent == path->mnt)
87031e6b01fSNick Piggin 		return 0;
87131e6b01fSNick Piggin 	mountpoint = path->mnt->mnt_mountpoint;
87231e6b01fSNick Piggin 	path->dentry = mountpoint;
87331e6b01fSNick Piggin 	path->mnt = parent;
87431e6b01fSNick Piggin 	return 1;
87531e6b01fSNick Piggin }
87631e6b01fSNick Piggin 
877bab77ebfSAl Viro int follow_up(struct path *path)
8781da177e4SLinus Torvalds {
8791da177e4SLinus Torvalds 	struct vfsmount *parent;
8801da177e4SLinus Torvalds 	struct dentry *mountpoint;
88199b7db7bSNick Piggin 
88299b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
883bab77ebfSAl Viro 	parent = path->mnt->mnt_parent;
884bab77ebfSAl Viro 	if (parent == path->mnt) {
88599b7db7bSNick Piggin 		br_read_unlock(vfsmount_lock);
8861da177e4SLinus Torvalds 		return 0;
8871da177e4SLinus Torvalds 	}
8881da177e4SLinus Torvalds 	mntget(parent);
889bab77ebfSAl Viro 	mountpoint = dget(path->mnt->mnt_mountpoint);
89099b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
891bab77ebfSAl Viro 	dput(path->dentry);
892bab77ebfSAl Viro 	path->dentry = mountpoint;
893bab77ebfSAl Viro 	mntput(path->mnt);
894bab77ebfSAl Viro 	path->mnt = parent;
8951da177e4SLinus Torvalds 	return 1;
8961da177e4SLinus Torvalds }
8971da177e4SLinus Torvalds 
898b5c84bf6SNick Piggin /*
8999875cf80SDavid Howells  * Perform an automount
9009875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
9019875cf80SDavid Howells  *   were called with.
9021da177e4SLinus Torvalds  */
9039875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
9049875cf80SDavid Howells 			    bool *need_mntput)
90531e6b01fSNick Piggin {
9069875cf80SDavid Howells 	struct vfsmount *mnt;
9079875cf80SDavid Howells 
9089875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
9099875cf80SDavid Howells 		return -EREMOTE;
9109875cf80SDavid Howells 
9119875cf80SDavid Howells 	/* We want to mount if someone is trying to open/create a file of any
9129875cf80SDavid Howells 	 * type under the mountpoint, wants to traverse through the mountpoint
9139875cf80SDavid Howells 	 * or wants to open the mounted directory.
9149875cf80SDavid Howells 	 *
9159875cf80SDavid Howells 	 * We don't want to mount if someone's just doing a stat and they've
9169875cf80SDavid Howells 	 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
9179875cf80SDavid Howells 	 * appended a '/' to the name.
9189875cf80SDavid Howells 	 */
9199875cf80SDavid Howells 	if (!(flags & LOOKUP_FOLLOW) &&
9209875cf80SDavid Howells 	    !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
9219875cf80SDavid Howells 		       LOOKUP_OPEN | LOOKUP_CREATE)))
9229875cf80SDavid Howells 		return -EISDIR;
9239875cf80SDavid Howells 
9249875cf80SDavid Howells 	current->total_link_count++;
9259875cf80SDavid Howells 	if (current->total_link_count >= 40)
9269875cf80SDavid Howells 		return -ELOOP;
9279875cf80SDavid Howells 
9289875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
9299875cf80SDavid Howells 	if (IS_ERR(mnt)) {
9309875cf80SDavid Howells 		/*
9319875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
9329875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
9339875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
9349875cf80SDavid Howells 		 *
9359875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
9369875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
9379875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
9389875cf80SDavid Howells 		 */
9399875cf80SDavid Howells 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
9409875cf80SDavid Howells 			return -EREMOTE;
9419875cf80SDavid Howells 		return PTR_ERR(mnt);
94231e6b01fSNick Piggin 	}
9439875cf80SDavid Howells 	if (!mnt) /* mount collision */
9449875cf80SDavid Howells 		return 0;
9459875cf80SDavid Howells 
9469875cf80SDavid Howells 	if (mnt->mnt_sb == path->mnt->mnt_sb &&
9479875cf80SDavid Howells 	    mnt->mnt_root == path->dentry) {
9489875cf80SDavid Howells 		mntput(mnt);
9499875cf80SDavid Howells 		return -ELOOP;
95031e6b01fSNick Piggin 	}
95131e6b01fSNick Piggin 
952463ffb2eSAl Viro 	dput(path->dentry);
9539875cf80SDavid Howells 	if (*need_mntput)
9549875cf80SDavid Howells 		mntput(path->mnt);
9559875cf80SDavid Howells 	path->mnt = mnt;
9569875cf80SDavid Howells 	path->dentry = dget(mnt->mnt_root);
9579875cf80SDavid Howells 	*need_mntput = true;
9589875cf80SDavid Howells 	return 0;
9599875cf80SDavid Howells }
9609875cf80SDavid Howells 
9619875cf80SDavid Howells /*
9629875cf80SDavid Howells  * Handle a dentry that is managed in some way.
9639875cf80SDavid Howells  * - Flagged as mountpoint
9649875cf80SDavid Howells  * - Flagged as automount point
9659875cf80SDavid Howells  *
9669875cf80SDavid Howells  * This may only be called in refwalk mode.
9679875cf80SDavid Howells  *
9689875cf80SDavid Howells  * Serialization is taken care of in namespace.c
9699875cf80SDavid Howells  */
9709875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
9719875cf80SDavid Howells {
9729875cf80SDavid Howells 	unsigned managed;
9739875cf80SDavid Howells 	bool need_mntput = false;
9749875cf80SDavid Howells 	int ret;
9759875cf80SDavid Howells 
9769875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
9779875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
9789875cf80SDavid Howells 	 * the components of that value change under us */
9799875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
9809875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
9819875cf80SDavid Howells 	       unlikely(managed != 0)) {
9829875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
9839875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
9849875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
9859875cf80SDavid Howells 			if (mounted) {
9869875cf80SDavid Howells 				dput(path->dentry);
9879875cf80SDavid Howells 				if (need_mntput)
988463ffb2eSAl Viro 					mntput(path->mnt);
989463ffb2eSAl Viro 				path->mnt = mounted;
990463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
9919875cf80SDavid Howells 				need_mntput = true;
9929875cf80SDavid Howells 				continue;
993463ffb2eSAl Viro 			}
994463ffb2eSAl Viro 
9959875cf80SDavid Howells 			/* Something is mounted on this dentry in another
9969875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
9979875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
9989875cf80SDavid Howells 			 * vfsmount_lock */
9991da177e4SLinus Torvalds 		}
10009875cf80SDavid Howells 
10019875cf80SDavid Howells 		/* Handle an automount point */
10029875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
10039875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
10049875cf80SDavid Howells 			if (ret < 0)
10059875cf80SDavid Howells 				return ret == -EISDIR ? 0 : ret;
10069875cf80SDavid Howells 			continue;
10079875cf80SDavid Howells 		}
10089875cf80SDavid Howells 
10099875cf80SDavid Howells 		/* We didn't change the current path point */
10109875cf80SDavid Howells 		break;
10119875cf80SDavid Howells 	}
10129875cf80SDavid Howells 	return 0;
10131da177e4SLinus Torvalds }
10141da177e4SLinus Torvalds 
10159393bd07SAl Viro int follow_down(struct path *path)
10161da177e4SLinus Torvalds {
10171da177e4SLinus Torvalds 	struct vfsmount *mounted;
10181da177e4SLinus Torvalds 
10191c755af4SAl Viro 	mounted = lookup_mnt(path);
10201da177e4SLinus Torvalds 	if (mounted) {
10219393bd07SAl Viro 		dput(path->dentry);
10229393bd07SAl Viro 		mntput(path->mnt);
10239393bd07SAl Viro 		path->mnt = mounted;
10249393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
10251da177e4SLinus Torvalds 		return 1;
10261da177e4SLinus Torvalds 	}
10271da177e4SLinus Torvalds 	return 0;
10281da177e4SLinus Torvalds }
10291da177e4SLinus Torvalds 
10309875cf80SDavid Howells /*
10319875cf80SDavid Howells  * Skip to top of mountpoint pile in rcuwalk mode.  We abort the rcu-walk if we
10329875cf80SDavid Howells  * meet an automount point and we're not walking to "..".  True is returned to
10339875cf80SDavid Howells  * continue, false to abort.
10349875cf80SDavid Howells  */
10359875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
10369875cf80SDavid Howells 			       struct inode **inode, bool reverse_transit)
10379875cf80SDavid Howells {
10389875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
10399875cf80SDavid Howells 		struct vfsmount *mounted;
10409875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
10419875cf80SDavid Howells 		if (!mounted)
10429875cf80SDavid Howells 			break;
10439875cf80SDavid Howells 		path->mnt = mounted;
10449875cf80SDavid Howells 		path->dentry = mounted->mnt_root;
10459875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
10469875cf80SDavid Howells 		*inode = path->dentry->d_inode;
10479875cf80SDavid Howells 	}
10489875cf80SDavid Howells 
10499875cf80SDavid Howells 	if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
10509875cf80SDavid Howells 		return reverse_transit;
10519875cf80SDavid Howells 	return true;
10529875cf80SDavid Howells }
10539875cf80SDavid Howells 
105431e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
105531e6b01fSNick Piggin {
105631e6b01fSNick Piggin 	struct inode *inode = nd->inode;
105731e6b01fSNick Piggin 
105831e6b01fSNick Piggin 	set_root_rcu(nd);
105931e6b01fSNick Piggin 
106031e6b01fSNick Piggin 	while (1) {
106131e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
106231e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
106331e6b01fSNick Piggin 			break;
106431e6b01fSNick Piggin 		}
106531e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
106631e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
106731e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
106831e6b01fSNick Piggin 			unsigned seq;
106931e6b01fSNick Piggin 
107031e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
107131e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
107231e6b01fSNick Piggin 				return -ECHILD;
107331e6b01fSNick Piggin 			inode = parent->d_inode;
107431e6b01fSNick Piggin 			nd->path.dentry = parent;
107531e6b01fSNick Piggin 			nd->seq = seq;
107631e6b01fSNick Piggin 			break;
107731e6b01fSNick Piggin 		}
107831e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
107931e6b01fSNick Piggin 			break;
108031e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
108131e6b01fSNick Piggin 		inode = nd->path.dentry->d_inode;
108231e6b01fSNick Piggin 	}
10839875cf80SDavid Howells 	__follow_mount_rcu(nd, &nd->path, &inode, true);
108431e6b01fSNick Piggin 	nd->inode = inode;
108531e6b01fSNick Piggin 
108631e6b01fSNick Piggin 	return 0;
108731e6b01fSNick Piggin }
108831e6b01fSNick Piggin 
10899875cf80SDavid Howells /*
10909875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
10919875cf80SDavid Howells  */
10929875cf80SDavid Howells static void follow_mount(struct path *path)
10939875cf80SDavid Howells {
10949875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
10959875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
10969875cf80SDavid Howells 		if (!mounted)
10979875cf80SDavid Howells 			break;
10989875cf80SDavid Howells 		dput(path->dentry);
10999875cf80SDavid Howells 		mntput(path->mnt);
11009875cf80SDavid Howells 		path->mnt = mounted;
11019875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
11029875cf80SDavid Howells 	}
11039875cf80SDavid Howells }
11049875cf80SDavid Howells 
110531e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
11061da177e4SLinus Torvalds {
11072a737871SAl Viro 	set_root(nd);
1108e518ddb7SAndreas Mohr 
11091da177e4SLinus Torvalds 	while(1) {
11104ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
11111da177e4SLinus Torvalds 
11122a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
11132a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
11141da177e4SLinus Torvalds 			break;
11151da177e4SLinus Torvalds 		}
11164ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
11173088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
11183088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
11191da177e4SLinus Torvalds 			dput(old);
11201da177e4SLinus Torvalds 			break;
11211da177e4SLinus Torvalds 		}
11223088dd70SAl Viro 		if (!follow_up(&nd->path))
11231da177e4SLinus Torvalds 			break;
11241da177e4SLinus Torvalds 	}
112579ed0226SAl Viro 	follow_mount(&nd->path);
112631e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
11271da177e4SLinus Torvalds }
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds /*
1130baa03890SNick Piggin  * Allocate a dentry with name and parent, and perform a parent
1131baa03890SNick Piggin  * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1132baa03890SNick Piggin  * on error. parent->d_inode->i_mutex must be held. d_lookup must
1133baa03890SNick Piggin  * have verified that no child exists while under i_mutex.
1134baa03890SNick Piggin  */
1135baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1136baa03890SNick Piggin 				struct qstr *name, struct nameidata *nd)
1137baa03890SNick Piggin {
1138baa03890SNick Piggin 	struct inode *inode = parent->d_inode;
1139baa03890SNick Piggin 	struct dentry *dentry;
1140baa03890SNick Piggin 	struct dentry *old;
1141baa03890SNick Piggin 
1142baa03890SNick Piggin 	/* Don't create child dentry for a dead directory. */
1143baa03890SNick Piggin 	if (unlikely(IS_DEADDIR(inode)))
1144baa03890SNick Piggin 		return ERR_PTR(-ENOENT);
1145baa03890SNick Piggin 
1146baa03890SNick Piggin 	dentry = d_alloc(parent, name);
1147baa03890SNick Piggin 	if (unlikely(!dentry))
1148baa03890SNick Piggin 		return ERR_PTR(-ENOMEM);
1149baa03890SNick Piggin 
1150baa03890SNick Piggin 	old = inode->i_op->lookup(inode, dentry, nd);
1151baa03890SNick Piggin 	if (unlikely(old)) {
1152baa03890SNick Piggin 		dput(dentry);
1153baa03890SNick Piggin 		dentry = old;
1154baa03890SNick Piggin 	}
1155baa03890SNick Piggin 	return dentry;
1156baa03890SNick Piggin }
1157baa03890SNick Piggin 
1158baa03890SNick Piggin /*
11591da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
11601da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
11611da177e4SLinus Torvalds  *  It _is_ time-critical.
11621da177e4SLinus Torvalds  */
11631da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
116431e6b01fSNick Piggin 			struct path *path, struct inode **inode)
11651da177e4SLinus Torvalds {
11664ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
116731e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
11686e6b1bd1SAl Viro 	struct inode *dir;
11699875cf80SDavid Howells 	int err;
11709875cf80SDavid Howells 
11713cac260aSAl Viro 	/*
11723cac260aSAl Viro 	 * See if the low-level filesystem might want
11733cac260aSAl Viro 	 * to use its own hash..
11743cac260aSAl Viro 	 */
1175fb045adbSNick Piggin 	if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
11769875cf80SDavid Howells 		err = parent->d_op->d_hash(parent, nd->inode, name);
11773cac260aSAl Viro 		if (err < 0)
11783cac260aSAl Viro 			return err;
11793cac260aSAl Viro 	}
11801da177e4SLinus Torvalds 
1181b04f784eSNick Piggin 	/*
1182b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1183b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1184b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1185b04f784eSNick Piggin 	 */
118631e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
118731e6b01fSNick Piggin 		unsigned seq;
118831e6b01fSNick Piggin 
118931e6b01fSNick Piggin 		*inode = nd->inode;
119031e6b01fSNick Piggin 		dentry = __d_lookup_rcu(parent, name, &seq, inode);
119131e6b01fSNick Piggin 		if (!dentry) {
119231e6b01fSNick Piggin 			if (nameidata_drop_rcu(nd))
119331e6b01fSNick Piggin 				return -ECHILD;
119431e6b01fSNick Piggin 			goto need_lookup;
119531e6b01fSNick Piggin 		}
119631e6b01fSNick Piggin 		/* Memory barrier in read_seqcount_begin of child is enough */
119731e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
119831e6b01fSNick Piggin 			return -ECHILD;
119931e6b01fSNick Piggin 
120031e6b01fSNick Piggin 		nd->seq = seq;
120134286d66SNick Piggin 		if (dentry->d_flags & DCACHE_OP_REVALIDATE)
120231e6b01fSNick Piggin 			goto need_revalidate;
12031a8edf40SAl Viro done2:
120431e6b01fSNick Piggin 		path->mnt = mnt;
120531e6b01fSNick Piggin 		path->dentry = dentry;
12069875cf80SDavid Howells 		if (likely(__follow_mount_rcu(nd, path, inode, false)))
12079875cf80SDavid Howells 			return 0;
12089875cf80SDavid Howells 		if (nameidata_drop_rcu(nd))
12099875cf80SDavid Howells 			return -ECHILD;
12109875cf80SDavid Howells 		/* fallthru */
12119875cf80SDavid Howells 	}
121231e6b01fSNick Piggin 	dentry = __d_lookup(parent, name);
12131da177e4SLinus Torvalds 	if (!dentry)
12141da177e4SLinus Torvalds 		goto need_lookup;
12152e2e88eaSNick Piggin found:
1216fb045adbSNick Piggin 	if (dentry->d_flags & DCACHE_OP_REVALIDATE)
12171da177e4SLinus Torvalds 		goto need_revalidate;
12181da177e4SLinus Torvalds done:
12191da177e4SLinus Torvalds 	path->mnt = mnt;
12201da177e4SLinus Torvalds 	path->dentry = dentry;
12219875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
12229875cf80SDavid Howells 	if (unlikely(err < 0))
12239875cf80SDavid Howells 		return err;
122431e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
12251da177e4SLinus Torvalds 	return 0;
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds need_lookup:
12286e6b1bd1SAl Viro 	dir = parent->d_inode;
122931e6b01fSNick Piggin 	BUG_ON(nd->inode != dir);
12306e6b1bd1SAl Viro 
12316e6b1bd1SAl Viro 	mutex_lock(&dir->i_mutex);
12326e6b1bd1SAl Viro 	/*
12336e6b1bd1SAl Viro 	 * First re-do the cached lookup just in case it was created
1234b04f784eSNick Piggin 	 * while we waited for the directory semaphore, or the first
1235b04f784eSNick Piggin 	 * lookup failed due to an unrelated rename.
12366e6b1bd1SAl Viro 	 *
1237b04f784eSNick Piggin 	 * This could use version numbering or similar to avoid unnecessary
1238b04f784eSNick Piggin 	 * cache lookups, but then we'd have to do the first lookup in the
1239b04f784eSNick Piggin 	 * non-racy way. However in the common case here, everything should
1240b04f784eSNick Piggin 	 * be hot in cache, so would it be a big win?
12416e6b1bd1SAl Viro 	 */
12426e6b1bd1SAl Viro 	dentry = d_lookup(parent, name);
1243baa03890SNick Piggin 	if (likely(!dentry)) {
1244baa03890SNick Piggin 		dentry = d_alloc_and_lookup(parent, name, nd);
12456e6b1bd1SAl Viro 		mutex_unlock(&dir->i_mutex);
12466e6b1bd1SAl Viro 		if (IS_ERR(dentry))
12476e6b1bd1SAl Viro 			goto fail;
12486e6b1bd1SAl Viro 		goto done;
12496e6b1bd1SAl Viro 	}
12506e6b1bd1SAl Viro 	/*
12516e6b1bd1SAl Viro 	 * Uhhuh! Nasty case: the cache was re-populated while
12526e6b1bd1SAl Viro 	 * we waited on the semaphore. Need to revalidate.
12536e6b1bd1SAl Viro 	 */
12546e6b1bd1SAl Viro 	mutex_unlock(&dir->i_mutex);
12552e2e88eaSNick Piggin 	goto found;
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds need_revalidate:
1258bcdc5e01SIan Kent 	dentry = do_revalidate(dentry, nd);
1259bcdc5e01SIan Kent 	if (!dentry)
12601da177e4SLinus Torvalds 		goto need_lookup;
1261bcdc5e01SIan Kent 	if (IS_ERR(dentry))
1262bcdc5e01SIan Kent 		goto fail;
12631a8edf40SAl Viro 	if (nd->flags & LOOKUP_RCU)
12641a8edf40SAl Viro 		goto done2;
1265bcdc5e01SIan Kent 	goto done;
12661da177e4SLinus Torvalds 
12671da177e4SLinus Torvalds fail:
12681da177e4SLinus Torvalds 	return PTR_ERR(dentry);
12691da177e4SLinus Torvalds }
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds /*
1272ac278a9cSAl Viro  * This is a temporary kludge to deal with "automount" symlinks; proper
1273ac278a9cSAl Viro  * solution is to trigger them on follow_mount(), so that do_lookup()
1274ac278a9cSAl Viro  * would DTRT.  To be killed before 2.6.34-final.
1275ac278a9cSAl Viro  */
1276ac278a9cSAl Viro static inline int follow_on_final(struct inode *inode, unsigned lookup_flags)
1277ac278a9cSAl Viro {
1278ac278a9cSAl Viro 	return inode && unlikely(inode->i_op->follow_link) &&
1279ac278a9cSAl Viro 		((lookup_flags & LOOKUP_FOLLOW) || S_ISDIR(inode->i_mode));
1280ac278a9cSAl Viro }
1281ac278a9cSAl Viro 
1282ac278a9cSAl Viro /*
12831da177e4SLinus Torvalds  * Name resolution.
1284ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1285ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
12861da177e4SLinus Torvalds  *
1287ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1288ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
12891da177e4SLinus Torvalds  */
12906de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
12911da177e4SLinus Torvalds {
12921da177e4SLinus Torvalds 	struct path next;
12931da177e4SLinus Torvalds 	int err;
12941da177e4SLinus Torvalds 	unsigned int lookup_flags = nd->flags;
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 	while (*name=='/')
12971da177e4SLinus Torvalds 		name++;
12981da177e4SLinus Torvalds 	if (!*name)
12991da177e4SLinus Torvalds 		goto return_reval;
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds 	if (nd->depth)
1302f55eab82STrond Myklebust 		lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
13051da177e4SLinus Torvalds 	for(;;) {
130631e6b01fSNick Piggin 		struct inode *inode;
13071da177e4SLinus Torvalds 		unsigned long hash;
13081da177e4SLinus Torvalds 		struct qstr this;
13091da177e4SLinus Torvalds 		unsigned int c;
13101da177e4SLinus Torvalds 
1311cdce5d6bSTrond Myklebust 		nd->flags |= LOOKUP_CONTINUE;
131231e6b01fSNick Piggin 		if (nd->flags & LOOKUP_RCU) {
1313b74c79e9SNick Piggin 			err = exec_permission(nd->inode, IPERM_FLAG_RCU);
131431e6b01fSNick Piggin 			if (err == -ECHILD) {
131531e6b01fSNick Piggin 				if (nameidata_drop_rcu(nd))
131631e6b01fSNick Piggin 					return -ECHILD;
131731e6b01fSNick Piggin 				goto exec_again;
131831e6b01fSNick Piggin 			}
131931e6b01fSNick Piggin 		} else {
132031e6b01fSNick Piggin exec_again:
1321b74c79e9SNick Piggin 			err = exec_permission(nd->inode, 0);
132231e6b01fSNick Piggin 		}
13231da177e4SLinus Torvalds  		if (err)
13241da177e4SLinus Torvalds 			break;
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds 		this.name = name;
13271da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 		hash = init_name_hash();
13301da177e4SLinus Torvalds 		do {
13311da177e4SLinus Torvalds 			name++;
13321da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
13331da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
13341da177e4SLinus Torvalds 		} while (c && (c != '/'));
13351da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
13361da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 		/* remove trailing slashes? */
13391da177e4SLinus Torvalds 		if (!c)
13401da177e4SLinus Torvalds 			goto last_component;
13411da177e4SLinus Torvalds 		while (*++name == '/');
13421da177e4SLinus Torvalds 		if (!*name)
13431da177e4SLinus Torvalds 			goto last_with_slashes;
13441da177e4SLinus Torvalds 
13451da177e4SLinus Torvalds 		/*
13461da177e4SLinus Torvalds 		 * "." and ".." are special - ".." especially so because it has
13471da177e4SLinus Torvalds 		 * to be able to know about the current root directory and
13481da177e4SLinus Torvalds 		 * parent relationships.
13491da177e4SLinus Torvalds 		 */
13501da177e4SLinus Torvalds 		if (this.name[0] == '.') switch (this.len) {
13511da177e4SLinus Torvalds 			default:
13521da177e4SLinus Torvalds 				break;
13531da177e4SLinus Torvalds 			case 2:
13541da177e4SLinus Torvalds 				if (this.name[1] != '.')
13551da177e4SLinus Torvalds 					break;
135631e6b01fSNick Piggin 				if (nd->flags & LOOKUP_RCU) {
135731e6b01fSNick Piggin 					if (follow_dotdot_rcu(nd))
135831e6b01fSNick Piggin 						return -ECHILD;
135931e6b01fSNick Piggin 				} else
136058c465ebSAl Viro 					follow_dotdot(nd);
13611da177e4SLinus Torvalds 				/* fallthrough */
13621da177e4SLinus Torvalds 			case 1:
13631da177e4SLinus Torvalds 				continue;
13641da177e4SLinus Torvalds 		}
13651da177e4SLinus Torvalds 		/* This does the actual lookups.. */
136631e6b01fSNick Piggin 		err = do_lookup(nd, &this, &next, &inode);
13671da177e4SLinus Torvalds 		if (err)
13681da177e4SLinus Torvalds 			break;
13691da177e4SLinus Torvalds 		err = -ENOENT;
13701da177e4SLinus Torvalds 		if (!inode)
13711da177e4SLinus Torvalds 			goto out_dput;
13721da177e4SLinus Torvalds 
13731da177e4SLinus Torvalds 		if (inode->i_op->follow_link) {
137431e6b01fSNick Piggin 			/* We commonly drop rcu-walk here */
137531e6b01fSNick Piggin 			if (nameidata_dentry_drop_rcu_maybe(nd, next.dentry))
137631e6b01fSNick Piggin 				return -ECHILD;
137731e6b01fSNick Piggin 			BUG_ON(inode != next.dentry->d_inode);
137890ebe565SAl Viro 			err = do_follow_link(&next, nd);
13791da177e4SLinus Torvalds 			if (err)
13801da177e4SLinus Torvalds 				goto return_err;
138131e6b01fSNick Piggin 			nd->inode = nd->path.dentry->d_inode;
13821da177e4SLinus Torvalds 			err = -ENOENT;
138331e6b01fSNick Piggin 			if (!nd->inode)
13841da177e4SLinus Torvalds 				break;
138531e6b01fSNick Piggin 		} else {
138609dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
138731e6b01fSNick Piggin 			nd->inode = inode;
138831e6b01fSNick Piggin 		}
13891da177e4SLinus Torvalds 		err = -ENOTDIR;
139031e6b01fSNick Piggin 		if (!nd->inode->i_op->lookup)
13911da177e4SLinus Torvalds 			break;
13921da177e4SLinus Torvalds 		continue;
13931da177e4SLinus Torvalds 		/* here ends the main loop */
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds last_with_slashes:
13961da177e4SLinus Torvalds 		lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
13971da177e4SLinus Torvalds last_component:
1398f55eab82STrond Myklebust 		/* Clear LOOKUP_CONTINUE iff it was previously unset */
1399f55eab82STrond Myklebust 		nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
14001da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_PARENT)
14011da177e4SLinus Torvalds 			goto lookup_parent;
14021da177e4SLinus Torvalds 		if (this.name[0] == '.') switch (this.len) {
14031da177e4SLinus Torvalds 			default:
14041da177e4SLinus Torvalds 				break;
14051da177e4SLinus Torvalds 			case 2:
14061da177e4SLinus Torvalds 				if (this.name[1] != '.')
14071da177e4SLinus Torvalds 					break;
140831e6b01fSNick Piggin 				if (nd->flags & LOOKUP_RCU) {
140931e6b01fSNick Piggin 					if (follow_dotdot_rcu(nd))
141031e6b01fSNick Piggin 						return -ECHILD;
141131e6b01fSNick Piggin 				} else
141258c465ebSAl Viro 					follow_dotdot(nd);
14131da177e4SLinus Torvalds 				/* fallthrough */
14141da177e4SLinus Torvalds 			case 1:
14151da177e4SLinus Torvalds 				goto return_reval;
14161da177e4SLinus Torvalds 		}
141731e6b01fSNick Piggin 		err = do_lookup(nd, &this, &next, &inode);
14181da177e4SLinus Torvalds 		if (err)
14191da177e4SLinus Torvalds 			break;
1420ac278a9cSAl Viro 		if (follow_on_final(inode, lookup_flags)) {
142131e6b01fSNick Piggin 			if (nameidata_dentry_drop_rcu_maybe(nd, next.dentry))
142231e6b01fSNick Piggin 				return -ECHILD;
142331e6b01fSNick Piggin 			BUG_ON(inode != next.dentry->d_inode);
142490ebe565SAl Viro 			err = do_follow_link(&next, nd);
14251da177e4SLinus Torvalds 			if (err)
14261da177e4SLinus Torvalds 				goto return_err;
142731e6b01fSNick Piggin 			nd->inode = nd->path.dentry->d_inode;
142831e6b01fSNick Piggin 		} else {
142909dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
143031e6b01fSNick Piggin 			nd->inode = inode;
143131e6b01fSNick Piggin 		}
14321da177e4SLinus Torvalds 		err = -ENOENT;
143331e6b01fSNick Piggin 		if (!nd->inode)
14341da177e4SLinus Torvalds 			break;
14351da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_DIRECTORY) {
14361da177e4SLinus Torvalds 			err = -ENOTDIR;
143731e6b01fSNick Piggin 			if (!nd->inode->i_op->lookup)
14381da177e4SLinus Torvalds 				break;
14391da177e4SLinus Torvalds 		}
14401da177e4SLinus Torvalds 		goto return_base;
14411da177e4SLinus Torvalds lookup_parent:
14421da177e4SLinus Torvalds 		nd->last = this;
14431da177e4SLinus Torvalds 		nd->last_type = LAST_NORM;
14441da177e4SLinus Torvalds 		if (this.name[0] != '.')
14451da177e4SLinus Torvalds 			goto return_base;
14461da177e4SLinus Torvalds 		if (this.len == 1)
14471da177e4SLinus Torvalds 			nd->last_type = LAST_DOT;
14481da177e4SLinus Torvalds 		else if (this.len == 2 && this.name[1] == '.')
14491da177e4SLinus Torvalds 			nd->last_type = LAST_DOTDOT;
14501da177e4SLinus Torvalds 		else
14511da177e4SLinus Torvalds 			goto return_base;
14521da177e4SLinus Torvalds return_reval:
14531da177e4SLinus Torvalds 		/*
14541da177e4SLinus Torvalds 		 * We bypassed the ordinary revalidation routines.
14551da177e4SLinus Torvalds 		 * We may need to check the cached dentry for staleness.
14561da177e4SLinus Torvalds 		 */
1457fb045adbSNick Piggin 		if (need_reval_dot(nd->path.dentry)) {
14581da177e4SLinus Torvalds 			/* Note: we do not d_invalidate() */
145934286d66SNick Piggin 			err = d_revalidate(nd->path.dentry, nd);
146034286d66SNick Piggin 			if (!err)
146134286d66SNick Piggin 				err = -ESTALE;
146234286d66SNick Piggin 			if (err < 0)
14631da177e4SLinus Torvalds 				break;
14641da177e4SLinus Torvalds 		}
14651da177e4SLinus Torvalds return_base:
146631e6b01fSNick Piggin 		if (nameidata_drop_rcu_last_maybe(nd))
146731e6b01fSNick Piggin 			return -ECHILD;
14681da177e4SLinus Torvalds 		return 0;
14691da177e4SLinus Torvalds out_dput:
147031e6b01fSNick Piggin 		if (!(nd->flags & LOOKUP_RCU))
14711d957f9bSJan Blunck 			path_put_conditional(&next, nd);
14721da177e4SLinus Torvalds 		break;
14731da177e4SLinus Torvalds 	}
147431e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU))
14751d957f9bSJan Blunck 		path_put(&nd->path);
14761da177e4SLinus Torvalds return_err:
14771da177e4SLinus Torvalds 	return err;
14781da177e4SLinus Torvalds }
14791da177e4SLinus Torvalds 
148031e6b01fSNick Piggin static inline int path_walk_rcu(const char *name, struct nameidata *nd)
148131e6b01fSNick Piggin {
148231e6b01fSNick Piggin 	current->total_link_count = 0;
148331e6b01fSNick Piggin 
148431e6b01fSNick Piggin 	return link_path_walk(name, nd);
148531e6b01fSNick Piggin }
148631e6b01fSNick Piggin 
148731e6b01fSNick Piggin static inline int path_walk_simple(const char *name, struct nameidata *nd)
148831e6b01fSNick Piggin {
148931e6b01fSNick Piggin 	current->total_link_count = 0;
149031e6b01fSNick Piggin 
149131e6b01fSNick Piggin 	return link_path_walk(name, nd);
149231e6b01fSNick Piggin }
149331e6b01fSNick Piggin 
1494fc9b52cdSHarvey Harrison static int path_walk(const char *name, struct nameidata *nd)
14951da177e4SLinus Torvalds {
14966de88d72SAl Viro 	struct path save = nd->path;
14976de88d72SAl Viro 	int result;
14986de88d72SAl Viro 
14991da177e4SLinus Torvalds 	current->total_link_count = 0;
15006de88d72SAl Viro 
15016de88d72SAl Viro 	/* make sure the stuff we saved doesn't go away */
15026de88d72SAl Viro 	path_get(&save);
15036de88d72SAl Viro 
15046de88d72SAl Viro 	result = link_path_walk(name, nd);
15056de88d72SAl Viro 	if (result == -ESTALE) {
15066de88d72SAl Viro 		/* nd->path had been dropped */
15076de88d72SAl Viro 		current->total_link_count = 0;
15086de88d72SAl Viro 		nd->path = save;
15096de88d72SAl Viro 		path_get(&nd->path);
15106de88d72SAl Viro 		nd->flags |= LOOKUP_REVAL;
15116de88d72SAl Viro 		result = link_path_walk(name, nd);
15126de88d72SAl Viro 	}
15136de88d72SAl Viro 
15146de88d72SAl Viro 	path_put(&save);
15156de88d72SAl Viro 
15166de88d72SAl Viro 	return result;
15171da177e4SLinus Torvalds }
15181da177e4SLinus Torvalds 
151931e6b01fSNick Piggin static void path_finish_rcu(struct nameidata *nd)
152031e6b01fSNick Piggin {
152131e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
152231e6b01fSNick Piggin 		/* RCU dangling. Cancel it. */
152331e6b01fSNick Piggin 		nd->flags &= ~LOOKUP_RCU;
152431e6b01fSNick Piggin 		nd->root.mnt = NULL;
152531e6b01fSNick Piggin 		rcu_read_unlock();
152631e6b01fSNick Piggin 		br_read_unlock(vfsmount_lock);
152731e6b01fSNick Piggin 	}
152831e6b01fSNick Piggin 	if (nd->file)
152931e6b01fSNick Piggin 		fput(nd->file);
153031e6b01fSNick Piggin }
153131e6b01fSNick Piggin 
153231e6b01fSNick Piggin static int path_init_rcu(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
153331e6b01fSNick Piggin {
153431e6b01fSNick Piggin 	int retval = 0;
153531e6b01fSNick Piggin 	int fput_needed;
153631e6b01fSNick Piggin 	struct file *file;
153731e6b01fSNick Piggin 
153831e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
153931e6b01fSNick Piggin 	nd->flags = flags | LOOKUP_RCU;
154031e6b01fSNick Piggin 	nd->depth = 0;
154131e6b01fSNick Piggin 	nd->root.mnt = NULL;
154231e6b01fSNick Piggin 	nd->file = NULL;
154331e6b01fSNick Piggin 
154431e6b01fSNick Piggin 	if (*name=='/') {
154531e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
1546c28cc364SNick Piggin 		unsigned seq;
154731e6b01fSNick Piggin 
154831e6b01fSNick Piggin 		br_read_lock(vfsmount_lock);
154931e6b01fSNick Piggin 		rcu_read_lock();
155031e6b01fSNick Piggin 
1551c28cc364SNick Piggin 		do {
1552c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
155331e6b01fSNick Piggin 			nd->root = fs->root;
155431e6b01fSNick Piggin 			nd->path = nd->root;
1555c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1556c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
155731e6b01fSNick Piggin 
155831e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
155931e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
1560c28cc364SNick Piggin 		unsigned seq;
156131e6b01fSNick Piggin 
156231e6b01fSNick Piggin 		br_read_lock(vfsmount_lock);
156331e6b01fSNick Piggin 		rcu_read_lock();
156431e6b01fSNick Piggin 
1565c28cc364SNick Piggin 		do {
1566c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
156731e6b01fSNick Piggin 			nd->path = fs->pwd;
1568c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1569c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
1570c28cc364SNick Piggin 
157131e6b01fSNick Piggin 	} else {
157231e6b01fSNick Piggin 		struct dentry *dentry;
157331e6b01fSNick Piggin 
157431e6b01fSNick Piggin 		file = fget_light(dfd, &fput_needed);
157531e6b01fSNick Piggin 		retval = -EBADF;
157631e6b01fSNick Piggin 		if (!file)
157731e6b01fSNick Piggin 			goto out_fail;
157831e6b01fSNick Piggin 
157931e6b01fSNick Piggin 		dentry = file->f_path.dentry;
158031e6b01fSNick Piggin 
158131e6b01fSNick Piggin 		retval = -ENOTDIR;
158231e6b01fSNick Piggin 		if (!S_ISDIR(dentry->d_inode->i_mode))
158331e6b01fSNick Piggin 			goto fput_fail;
158431e6b01fSNick Piggin 
158531e6b01fSNick Piggin 		retval = file_permission(file, MAY_EXEC);
158631e6b01fSNick Piggin 		if (retval)
158731e6b01fSNick Piggin 			goto fput_fail;
158831e6b01fSNick Piggin 
158931e6b01fSNick Piggin 		nd->path = file->f_path;
159031e6b01fSNick Piggin 		if (fput_needed)
159131e6b01fSNick Piggin 			nd->file = file;
159231e6b01fSNick Piggin 
1593c28cc364SNick Piggin 		nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
159431e6b01fSNick Piggin 		br_read_lock(vfsmount_lock);
159531e6b01fSNick Piggin 		rcu_read_lock();
159631e6b01fSNick Piggin 	}
159731e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
159831e6b01fSNick Piggin 	return 0;
159931e6b01fSNick Piggin 
160031e6b01fSNick Piggin fput_fail:
160131e6b01fSNick Piggin 	fput_light(file, fput_needed);
160231e6b01fSNick Piggin out_fail:
160331e6b01fSNick Piggin 	return retval;
160431e6b01fSNick Piggin }
160531e6b01fSNick Piggin 
16069b4a9b14SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
16071da177e4SLinus Torvalds {
1608ea3834d9SPrasanna Meda 	int retval = 0;
1609170aa3d0SUlrich Drepper 	int fput_needed;
1610170aa3d0SUlrich Drepper 	struct file *file;
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
16131da177e4SLinus Torvalds 	nd->flags = flags;
16141da177e4SLinus Torvalds 	nd->depth = 0;
16152a737871SAl Viro 	nd->root.mnt = NULL;
16161da177e4SLinus Torvalds 
16171da177e4SLinus Torvalds 	if (*name=='/') {
16182a737871SAl Viro 		set_root(nd);
16192a737871SAl Viro 		nd->path = nd->root;
16202a737871SAl Viro 		path_get(&nd->root);
16215590ff0dSUlrich Drepper 	} else if (dfd == AT_FDCWD) {
1622f7ad3c6bSMiklos Szeredi 		get_fs_pwd(current->fs, &nd->path);
16235590ff0dSUlrich Drepper 	} else {
16245590ff0dSUlrich Drepper 		struct dentry *dentry;
16255590ff0dSUlrich Drepper 
16265590ff0dSUlrich Drepper 		file = fget_light(dfd, &fput_needed);
16275590ff0dSUlrich Drepper 		retval = -EBADF;
1628170aa3d0SUlrich Drepper 		if (!file)
16296d09bb62STrond Myklebust 			goto out_fail;
16305590ff0dSUlrich Drepper 
16310f7fc9e4SJosef "Jeff" Sipek 		dentry = file->f_path.dentry;
16325590ff0dSUlrich Drepper 
16335590ff0dSUlrich Drepper 		retval = -ENOTDIR;
1634170aa3d0SUlrich Drepper 		if (!S_ISDIR(dentry->d_inode->i_mode))
16356d09bb62STrond Myklebust 			goto fput_fail;
16365590ff0dSUlrich Drepper 
16375590ff0dSUlrich Drepper 		retval = file_permission(file, MAY_EXEC);
1638170aa3d0SUlrich Drepper 		if (retval)
16396d09bb62STrond Myklebust 			goto fput_fail;
16405590ff0dSUlrich Drepper 
16415dd784d0SJan Blunck 		nd->path = file->f_path;
16425dd784d0SJan Blunck 		path_get(&file->f_path);
16435590ff0dSUlrich Drepper 
16445590ff0dSUlrich Drepper 		fput_light(file, fput_needed);
16451da177e4SLinus Torvalds 	}
164631e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
16479b4a9b14SAl Viro 	return 0;
16482dfdd266SJosef 'Jeff' Sipek 
16499b4a9b14SAl Viro fput_fail:
16509b4a9b14SAl Viro 	fput_light(file, fput_needed);
16519b4a9b14SAl Viro out_fail:
16529b4a9b14SAl Viro 	return retval;
16539b4a9b14SAl Viro }
16549b4a9b14SAl Viro 
16559b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
16569b4a9b14SAl Viro static int do_path_lookup(int dfd, const char *name,
16579b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
16589b4a9b14SAl Viro {
165931e6b01fSNick Piggin 	int retval;
166031e6b01fSNick Piggin 
166131e6b01fSNick Piggin 	/*
166231e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
166331e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
166431e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
166531e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
166631e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
166731e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
166831e6b01fSNick Piggin 	 * analogue, foo_rcu().
166931e6b01fSNick Piggin 	 *
167031e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
167131e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
167231e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
167331e6b01fSNick Piggin 	 * be able to complete).
167431e6b01fSNick Piggin 	 */
167531e6b01fSNick Piggin 	retval = path_init_rcu(dfd, name, flags, nd);
167631e6b01fSNick Piggin 	if (unlikely(retval))
167731e6b01fSNick Piggin 		return retval;
167831e6b01fSNick Piggin 	retval = path_walk_rcu(name, nd);
167931e6b01fSNick Piggin 	path_finish_rcu(nd);
16802a737871SAl Viro 	if (nd->root.mnt) {
16812a737871SAl Viro 		path_put(&nd->root);
16822a737871SAl Viro 		nd->root.mnt = NULL;
16832a737871SAl Viro 	}
168431e6b01fSNick Piggin 
168531e6b01fSNick Piggin 	if (unlikely(retval == -ECHILD || retval == -ESTALE)) {
168631e6b01fSNick Piggin 		/* slower, locked walk */
168731e6b01fSNick Piggin 		if (retval == -ESTALE)
168831e6b01fSNick Piggin 			flags |= LOOKUP_REVAL;
168931e6b01fSNick Piggin 		retval = path_init(dfd, name, flags, nd);
169031e6b01fSNick Piggin 		if (unlikely(retval))
169131e6b01fSNick Piggin 			return retval;
169231e6b01fSNick Piggin 		retval = path_walk(name, nd);
169331e6b01fSNick Piggin 		if (nd->root.mnt) {
169431e6b01fSNick Piggin 			path_put(&nd->root);
169531e6b01fSNick Piggin 			nd->root.mnt = NULL;
169631e6b01fSNick Piggin 		}
169731e6b01fSNick Piggin 	}
169831e6b01fSNick Piggin 
169931e6b01fSNick Piggin 	if (likely(!retval)) {
170031e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
170131e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
170231e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
170331e6b01fSNick Piggin 		}
170431e6b01fSNick Piggin 	}
170531e6b01fSNick Piggin 
1706170aa3d0SUlrich Drepper 	return retval;
17071da177e4SLinus Torvalds }
17081da177e4SLinus Torvalds 
1709fc9b52cdSHarvey Harrison int path_lookup(const char *name, unsigned int flags,
17105590ff0dSUlrich Drepper 			struct nameidata *nd)
17115590ff0dSUlrich Drepper {
17125590ff0dSUlrich Drepper 	return do_path_lookup(AT_FDCWD, name, flags, nd);
17135590ff0dSUlrich Drepper }
17145590ff0dSUlrich Drepper 
1715d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1716d1811465SAl Viro {
1717d1811465SAl Viro 	struct nameidata nd;
1718d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1719d1811465SAl Viro 	if (!res)
1720d1811465SAl Viro 		*path = nd.path;
1721d1811465SAl Viro 	return res;
1722d1811465SAl Viro }
1723d1811465SAl Viro 
172416f18200SJosef 'Jeff' Sipek /**
172516f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
172616f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
172716f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
172816f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
172916f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
173016f18200SJosef 'Jeff' Sipek  * @nd: pointer to nameidata
173116f18200SJosef 'Jeff' Sipek  */
173216f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
173316f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
173416f18200SJosef 'Jeff' Sipek 		    struct nameidata *nd)
173516f18200SJosef 'Jeff' Sipek {
173616f18200SJosef 'Jeff' Sipek 	int retval;
173716f18200SJosef 'Jeff' Sipek 
173816f18200SJosef 'Jeff' Sipek 	/* same as do_path_lookup */
173916f18200SJosef 'Jeff' Sipek 	nd->last_type = LAST_ROOT;
174016f18200SJosef 'Jeff' Sipek 	nd->flags = flags;
174116f18200SJosef 'Jeff' Sipek 	nd->depth = 0;
174216f18200SJosef 'Jeff' Sipek 
1743c8e7f449SJan Blunck 	nd->path.dentry = dentry;
1744c8e7f449SJan Blunck 	nd->path.mnt = mnt;
1745c8e7f449SJan Blunck 	path_get(&nd->path);
17465b857119SAl Viro 	nd->root = nd->path;
17475b857119SAl Viro 	path_get(&nd->root);
174831e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
174916f18200SJosef 'Jeff' Sipek 
175016f18200SJosef 'Jeff' Sipek 	retval = path_walk(name, nd);
17514ac91378SJan Blunck 	if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
175231e6b01fSNick Piggin 				nd->inode))
17534ac91378SJan Blunck 		audit_inode(name, nd->path.dentry);
175416f18200SJosef 'Jeff' Sipek 
17552a737871SAl Viro 	path_put(&nd->root);
17562a737871SAl Viro 	nd->root.mnt = NULL;
175716f18200SJosef 'Jeff' Sipek 
17582a737871SAl Viro 	return retval;
175916f18200SJosef 'Jeff' Sipek }
176016f18200SJosef 'Jeff' Sipek 
1761eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1762eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
17631da177e4SLinus Torvalds {
176481fca444SChristoph Hellwig 	struct inode *inode = base->d_inode;
17651da177e4SLinus Torvalds 	struct dentry *dentry;
17661da177e4SLinus Torvalds 	int err;
17671da177e4SLinus Torvalds 
1768b74c79e9SNick Piggin 	err = exec_permission(inode, 0);
176981fca444SChristoph Hellwig 	if (err)
177081fca444SChristoph Hellwig 		return ERR_PTR(err);
17711da177e4SLinus Torvalds 
17721da177e4SLinus Torvalds 	/*
17731da177e4SLinus Torvalds 	 * See if the low-level filesystem might want
17741da177e4SLinus Torvalds 	 * to use its own hash..
17751da177e4SLinus Torvalds 	 */
1776fb045adbSNick Piggin 	if (base->d_flags & DCACHE_OP_HASH) {
1777b1e6a015SNick Piggin 		err = base->d_op->d_hash(base, inode, name);
17781da177e4SLinus Torvalds 		dentry = ERR_PTR(err);
17791da177e4SLinus Torvalds 		if (err < 0)
17801da177e4SLinus Torvalds 			goto out;
17811da177e4SLinus Torvalds 	}
17821da177e4SLinus Torvalds 
1783b04f784eSNick Piggin 	/*
1784b04f784eSNick Piggin 	 * Don't bother with __d_lookup: callers are for creat as
1785b04f784eSNick Piggin 	 * well as unlink, so a lot of the time it would cost
1786b04f784eSNick Piggin 	 * a double lookup.
17876e6b1bd1SAl Viro 	 */
17886e6b1bd1SAl Viro 	dentry = d_lookup(base, name);
17896e6b1bd1SAl Viro 
1790fb045adbSNick Piggin 	if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
17916e6b1bd1SAl Viro 		dentry = do_revalidate(dentry, nd);
17926e6b1bd1SAl Viro 
17931da177e4SLinus Torvalds 	if (!dentry)
1794baa03890SNick Piggin 		dentry = d_alloc_and_lookup(base, name, nd);
17951da177e4SLinus Torvalds out:
17961da177e4SLinus Torvalds 	return dentry;
17971da177e4SLinus Torvalds }
17981da177e4SLinus Torvalds 
1799057f6c01SJames Morris /*
1800057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1801057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1802057f6c01SJames Morris  * SMP-safe.
1803057f6c01SJames Morris  */
1804a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
18051da177e4SLinus Torvalds {
18064ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
18071da177e4SLinus Torvalds }
18081da177e4SLinus Torvalds 
1809eead1911SChristoph Hellwig static int __lookup_one_len(const char *name, struct qstr *this,
1810eead1911SChristoph Hellwig 		struct dentry *base, int len)
18111da177e4SLinus Torvalds {
18121da177e4SLinus Torvalds 	unsigned long hash;
18131da177e4SLinus Torvalds 	unsigned int c;
18141da177e4SLinus Torvalds 
1815057f6c01SJames Morris 	this->name = name;
1816057f6c01SJames Morris 	this->len = len;
18171da177e4SLinus Torvalds 	if (!len)
1818057f6c01SJames Morris 		return -EACCES;
18191da177e4SLinus Torvalds 
18201da177e4SLinus Torvalds 	hash = init_name_hash();
18211da177e4SLinus Torvalds 	while (len--) {
18221da177e4SLinus Torvalds 		c = *(const unsigned char *)name++;
18231da177e4SLinus Torvalds 		if (c == '/' || c == '\0')
1824057f6c01SJames Morris 			return -EACCES;
18251da177e4SLinus Torvalds 		hash = partial_name_hash(c, hash);
18261da177e4SLinus Torvalds 	}
1827057f6c01SJames Morris 	this->hash = end_name_hash(hash);
1828057f6c01SJames Morris 	return 0;
1829057f6c01SJames Morris }
18301da177e4SLinus Torvalds 
1831eead1911SChristoph Hellwig /**
1832a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1833eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1834eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1835eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1836eead1911SChristoph Hellwig  *
1837a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1838a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1839eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1840eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1841eead1911SChristoph Hellwig  */
1842057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1843057f6c01SJames Morris {
1844057f6c01SJames Morris 	int err;
1845057f6c01SJames Morris 	struct qstr this;
1846057f6c01SJames Morris 
18472f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
18482f9092e1SDavid Woodhouse 
1849057f6c01SJames Morris 	err = __lookup_one_len(name, &this, base, len);
1850057f6c01SJames Morris 	if (err)
1851057f6c01SJames Morris 		return ERR_PTR(err);
1852eead1911SChristoph Hellwig 
185349705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1854057f6c01SJames Morris }
1855057f6c01SJames Morris 
18562d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags,
18572d8f3038SAl Viro 		 struct path *path)
18581da177e4SLinus Torvalds {
18592d8f3038SAl Viro 	struct nameidata nd;
18601da177e4SLinus Torvalds 	char *tmp = getname(name);
18611da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
18621da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
18632d8f3038SAl Viro 
18642d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
18652d8f3038SAl Viro 
18662d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
18671da177e4SLinus Torvalds 		putname(tmp);
18682d8f3038SAl Viro 		if (!err)
18692d8f3038SAl Viro 			*path = nd.path;
18701da177e4SLinus Torvalds 	}
18711da177e4SLinus Torvalds 	return err;
18721da177e4SLinus Torvalds }
18731da177e4SLinus Torvalds 
18742ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
18752ad94ae6SAl Viro 			struct nameidata *nd, char **name)
18762ad94ae6SAl Viro {
18772ad94ae6SAl Viro 	char *s = getname(path);
18782ad94ae6SAl Viro 	int error;
18792ad94ae6SAl Viro 
18802ad94ae6SAl Viro 	if (IS_ERR(s))
18812ad94ae6SAl Viro 		return PTR_ERR(s);
18822ad94ae6SAl Viro 
18832ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
18842ad94ae6SAl Viro 	if (error)
18852ad94ae6SAl Viro 		putname(s);
18862ad94ae6SAl Viro 	else
18872ad94ae6SAl Viro 		*name = s;
18882ad94ae6SAl Viro 
18892ad94ae6SAl Viro 	return error;
18902ad94ae6SAl Viro }
18912ad94ae6SAl Viro 
18921da177e4SLinus Torvalds /*
18931da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
18941da177e4SLinus Torvalds  * minimal.
18951da177e4SLinus Torvalds  */
18961da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
18971da177e4SLinus Torvalds {
1898da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1899da9592edSDavid Howells 
19001da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
19011da177e4SLinus Torvalds 		return 0;
1902da9592edSDavid Howells 	if (inode->i_uid == fsuid)
19031da177e4SLinus Torvalds 		return 0;
1904da9592edSDavid Howells 	if (dir->i_uid == fsuid)
19051da177e4SLinus Torvalds 		return 0;
19061da177e4SLinus Torvalds 	return !capable(CAP_FOWNER);
19071da177e4SLinus Torvalds }
19081da177e4SLinus Torvalds 
19091da177e4SLinus Torvalds /*
19101da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
19111da177e4SLinus Torvalds  *  whether the type of victim is right.
19121da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
19131da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
19141da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
19151da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
19161da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
19171da177e4SLinus Torvalds  *	a. be owner of dir, or
19181da177e4SLinus Torvalds  *	b. be owner of victim, or
19191da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
19201da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
19211da177e4SLinus Torvalds  *     links pointing to it.
19221da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
19231da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
19241da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
19251da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
19261da177e4SLinus Torvalds  *     nfs_async_unlink().
19271da177e4SLinus Torvalds  */
1928858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
19291da177e4SLinus Torvalds {
19301da177e4SLinus Torvalds 	int error;
19311da177e4SLinus Torvalds 
19321da177e4SLinus Torvalds 	if (!victim->d_inode)
19331da177e4SLinus Torvalds 		return -ENOENT;
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
1936cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
19371da177e4SLinus Torvalds 
1938f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
19391da177e4SLinus Torvalds 	if (error)
19401da177e4SLinus Torvalds 		return error;
19411da177e4SLinus Torvalds 	if (IS_APPEND(dir))
19421da177e4SLinus Torvalds 		return -EPERM;
19431da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1944f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
19451da177e4SLinus Torvalds 		return -EPERM;
19461da177e4SLinus Torvalds 	if (isdir) {
19471da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
19481da177e4SLinus Torvalds 			return -ENOTDIR;
19491da177e4SLinus Torvalds 		if (IS_ROOT(victim))
19501da177e4SLinus Torvalds 			return -EBUSY;
19511da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
19521da177e4SLinus Torvalds 		return -EISDIR;
19531da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
19541da177e4SLinus Torvalds 		return -ENOENT;
19551da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
19561da177e4SLinus Torvalds 		return -EBUSY;
19571da177e4SLinus Torvalds 	return 0;
19581da177e4SLinus Torvalds }
19591da177e4SLinus Torvalds 
19601da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
19611da177e4SLinus Torvalds  *  dir.
19621da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
19631da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
19641da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
19651da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
19661da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
19671da177e4SLinus Torvalds  */
1968a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
19691da177e4SLinus Torvalds {
19701da177e4SLinus Torvalds 	if (child->d_inode)
19711da177e4SLinus Torvalds 		return -EEXIST;
19721da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
19731da177e4SLinus Torvalds 		return -ENOENT;
1974f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
19751da177e4SLinus Torvalds }
19761da177e4SLinus Torvalds 
19771da177e4SLinus Torvalds /*
19781da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
19791da177e4SLinus Torvalds  */
19801da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
19811da177e4SLinus Torvalds {
19821da177e4SLinus Torvalds 	struct dentry *p;
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds 	if (p1 == p2) {
1985f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
19861da177e4SLinus Torvalds 		return NULL;
19871da177e4SLinus Torvalds 	}
19881da177e4SLinus Torvalds 
1989a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
19901da177e4SLinus Torvalds 
1991e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1992e2761a11SOGAWA Hirofumi 	if (p) {
1993f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1994f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
19951da177e4SLinus Torvalds 		return p;
19961da177e4SLinus Torvalds 	}
19971da177e4SLinus Torvalds 
1998e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1999e2761a11SOGAWA Hirofumi 	if (p) {
2000f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2001f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
20021da177e4SLinus Torvalds 		return p;
20031da177e4SLinus Torvalds 	}
20041da177e4SLinus Torvalds 
2005f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2006f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
20071da177e4SLinus Torvalds 	return NULL;
20081da177e4SLinus Torvalds }
20091da177e4SLinus Torvalds 
20101da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
20111da177e4SLinus Torvalds {
20121b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
20131da177e4SLinus Torvalds 	if (p1 != p2) {
20141b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
2015a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
20161da177e4SLinus Torvalds 	}
20171da177e4SLinus Torvalds }
20181da177e4SLinus Torvalds 
20191da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
20201da177e4SLinus Torvalds 		struct nameidata *nd)
20211da177e4SLinus Torvalds {
2022a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
20231da177e4SLinus Torvalds 
20241da177e4SLinus Torvalds 	if (error)
20251da177e4SLinus Torvalds 		return error;
20261da177e4SLinus Torvalds 
2027acfa4380SAl Viro 	if (!dir->i_op->create)
20281da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
20291da177e4SLinus Torvalds 	mode &= S_IALLUGO;
20301da177e4SLinus Torvalds 	mode |= S_IFREG;
20311da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
20321da177e4SLinus Torvalds 	if (error)
20331da177e4SLinus Torvalds 		return error;
20341da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
2035a74574aaSStephen Smalley 	if (!error)
2036f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
20371da177e4SLinus Torvalds 	return error;
20381da177e4SLinus Torvalds }
20391da177e4SLinus Torvalds 
20403fb64190SChristoph Hellwig int may_open(struct path *path, int acc_mode, int flag)
20411da177e4SLinus Torvalds {
20423fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
20431da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
20441da177e4SLinus Torvalds 	int error;
20451da177e4SLinus Torvalds 
20461da177e4SLinus Torvalds 	if (!inode)
20471da177e4SLinus Torvalds 		return -ENOENT;
20481da177e4SLinus Torvalds 
2049c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2050c8fe8f30SChristoph Hellwig 	case S_IFLNK:
20511da177e4SLinus Torvalds 		return -ELOOP;
2052c8fe8f30SChristoph Hellwig 	case S_IFDIR:
2053c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
20541da177e4SLinus Torvalds 			return -EISDIR;
2055c8fe8f30SChristoph Hellwig 		break;
2056c8fe8f30SChristoph Hellwig 	case S_IFBLK:
2057c8fe8f30SChristoph Hellwig 	case S_IFCHR:
20583fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
20591da177e4SLinus Torvalds 			return -EACCES;
2060c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
2061c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2062c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
20631da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2064c8fe8f30SChristoph Hellwig 		break;
20654a3fd211SDave Hansen 	}
2066b41572e9SDave Hansen 
20673fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
2068b41572e9SDave Hansen 	if (error)
2069b41572e9SDave Hansen 		return error;
20706146f0d5SMimi Zohar 
20711da177e4SLinus Torvalds 	/*
20721da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
20731da177e4SLinus Torvalds 	 */
20741da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
20758737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
20767715b521SAl Viro 			return -EPERM;
20771da177e4SLinus Torvalds 		if (flag & O_TRUNC)
20787715b521SAl Viro 			return -EPERM;
20791da177e4SLinus Torvalds 	}
20801da177e4SLinus Torvalds 
20811da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
20827715b521SAl Viro 	if (flag & O_NOATIME && !is_owner_or_cap(inode))
20837715b521SAl Viro 		return -EPERM;
20841da177e4SLinus Torvalds 
20851da177e4SLinus Torvalds 	/*
20861da177e4SLinus Torvalds 	 * Ensure there are no outstanding leases on the file.
20871da177e4SLinus Torvalds 	 */
2088b65a9cfcSAl Viro 	return break_lease(inode, flag);
20897715b521SAl Viro }
20907715b521SAl Viro 
2091e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
20927715b521SAl Viro {
2093e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
20947715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
20957715b521SAl Viro 	int error = get_write_access(inode);
20961da177e4SLinus Torvalds 	if (error)
20977715b521SAl Viro 		return error;
20981da177e4SLinus Torvalds 	/*
20991da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
21001da177e4SLinus Torvalds 	 */
21011da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2102be6d3e56SKentaro Takeda 	if (!error)
2103ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
21041da177e4SLinus Torvalds 	if (!error) {
21057715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2106d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2107e1181ee6SJeff Layton 				    filp);
21081da177e4SLinus Torvalds 	}
21091da177e4SLinus Torvalds 	put_write_access(inode);
2110acd0c935SMimi Zohar 	return error;
21111da177e4SLinus Torvalds }
21121da177e4SLinus Torvalds 
2113d57999e1SDave Hansen /*
2114d57999e1SDave Hansen  * Be careful about ever adding any more callers of this
2115d57999e1SDave Hansen  * function.  Its flags must be in the namei format, not
2116d57999e1SDave Hansen  * what get passed to sys_open().
2117d57999e1SDave Hansen  */
2118d57999e1SDave Hansen static int __open_namei_create(struct nameidata *nd, struct path *path,
21198737c930SAl Viro 				int open_flag, int mode)
2120aab520e2SDave Hansen {
2121aab520e2SDave Hansen 	int error;
21224ac91378SJan Blunck 	struct dentry *dir = nd->path.dentry;
2123aab520e2SDave Hansen 
2124aab520e2SDave Hansen 	if (!IS_POSIXACL(dir->d_inode))
2125ce3b0f8dSAl Viro 		mode &= ~current_umask();
2126be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd->path, path->dentry, mode, 0);
2127be6d3e56SKentaro Takeda 	if (error)
2128be6d3e56SKentaro Takeda 		goto out_unlock;
2129aab520e2SDave Hansen 	error = vfs_create(dir->d_inode, path->dentry, mode, nd);
2130be6d3e56SKentaro Takeda out_unlock:
2131aab520e2SDave Hansen 	mutex_unlock(&dir->d_inode->i_mutex);
21324ac91378SJan Blunck 	dput(nd->path.dentry);
21334ac91378SJan Blunck 	nd->path.dentry = path->dentry;
213431e6b01fSNick Piggin 
2135aab520e2SDave Hansen 	if (error)
2136aab520e2SDave Hansen 		return error;
2137aab520e2SDave Hansen 	/* Don't check for write permission, don't truncate */
21388737c930SAl Viro 	return may_open(&nd->path, 0, open_flag & ~O_TRUNC);
2139aab520e2SDave Hansen }
2140aab520e2SDave Hansen 
21411da177e4SLinus Torvalds /*
2142d57999e1SDave Hansen  * Note that while the flag value (low two bits) for sys_open means:
2143d57999e1SDave Hansen  *	00 - read-only
2144d57999e1SDave Hansen  *	01 - write-only
2145d57999e1SDave Hansen  *	10 - read-write
2146d57999e1SDave Hansen  *	11 - special
2147d57999e1SDave Hansen  * it is changed into
2148d57999e1SDave Hansen  *	00 - no permissions needed
2149d57999e1SDave Hansen  *	01 - read-permission
2150d57999e1SDave Hansen  *	10 - write-permission
2151d57999e1SDave Hansen  *	11 - read-write
2152d57999e1SDave Hansen  * for the internal routines (ie open_namei()/follow_link() etc)
2153d57999e1SDave Hansen  * This is more logical, and also allows the 00 "no perm needed"
2154d57999e1SDave Hansen  * to be used for symlinks (where the permissions are checked
2155d57999e1SDave Hansen  * later).
2156d57999e1SDave Hansen  *
2157d57999e1SDave Hansen */
2158d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2159d57999e1SDave Hansen {
2160d57999e1SDave Hansen 	if ((flag+1) & O_ACCMODE)
2161d57999e1SDave Hansen 		flag++;
2162d57999e1SDave Hansen 	return flag;
2163d57999e1SDave Hansen }
2164d57999e1SDave Hansen 
21657715b521SAl Viro static int open_will_truncate(int flag, struct inode *inode)
21664a3fd211SDave Hansen {
2167d57999e1SDave Hansen 	/*
21684a3fd211SDave Hansen 	 * We'll never write to the fs underlying
21694a3fd211SDave Hansen 	 * a device file.
21704a3fd211SDave Hansen 	 */
21714a3fd211SDave Hansen 	if (special_file(inode->i_mode))
21724a3fd211SDave Hansen 		return 0;
21734a3fd211SDave Hansen 	return (flag & O_TRUNC);
21744a3fd211SDave Hansen }
21754a3fd211SDave Hansen 
2176648fa861SAl Viro static struct file *finish_open(struct nameidata *nd,
21779a66179eSAl Viro 				int open_flag, int acc_mode)
2178648fa861SAl Viro {
2179648fa861SAl Viro 	struct file *filp;
2180648fa861SAl Viro 	int will_truncate;
2181648fa861SAl Viro 	int error;
2182648fa861SAl Viro 
21839a66179eSAl Viro 	will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode);
2184648fa861SAl Viro 	if (will_truncate) {
2185648fa861SAl Viro 		error = mnt_want_write(nd->path.mnt);
2186648fa861SAl Viro 		if (error)
2187648fa861SAl Viro 			goto exit;
2188648fa861SAl Viro 	}
2189648fa861SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2190648fa861SAl Viro 	if (error) {
2191648fa861SAl Viro 		if (will_truncate)
2192648fa861SAl Viro 			mnt_drop_write(nd->path.mnt);
2193648fa861SAl Viro 		goto exit;
2194648fa861SAl Viro 	}
2195648fa861SAl Viro 	filp = nameidata_to_filp(nd);
2196648fa861SAl Viro 	if (!IS_ERR(filp)) {
2197648fa861SAl Viro 		error = ima_file_check(filp, acc_mode);
2198648fa861SAl Viro 		if (error) {
2199648fa861SAl Viro 			fput(filp);
2200648fa861SAl Viro 			filp = ERR_PTR(error);
2201648fa861SAl Viro 		}
2202648fa861SAl Viro 	}
2203648fa861SAl Viro 	if (!IS_ERR(filp)) {
2204648fa861SAl Viro 		if (will_truncate) {
2205e1181ee6SJeff Layton 			error = handle_truncate(filp);
2206648fa861SAl Viro 			if (error) {
2207648fa861SAl Viro 				fput(filp);
2208648fa861SAl Viro 				filp = ERR_PTR(error);
2209648fa861SAl Viro 			}
2210648fa861SAl Viro 		}
2211648fa861SAl Viro 	}
2212648fa861SAl Viro 	/*
2213648fa861SAl Viro 	 * It is now safe to drop the mnt write
2214648fa861SAl Viro 	 * because the filp has had a write taken
2215648fa861SAl Viro 	 * on its behalf.
2216648fa861SAl Viro 	 */
2217648fa861SAl Viro 	if (will_truncate)
2218648fa861SAl Viro 		mnt_drop_write(nd->path.mnt);
2219d893f1bcSAl Viro 	path_put(&nd->path);
2220648fa861SAl Viro 	return filp;
2221648fa861SAl Viro 
2222648fa861SAl Viro exit:
2223648fa861SAl Viro 	if (!IS_ERR(nd->intent.open.file))
2224648fa861SAl Viro 		release_open_intent(nd);
2225648fa861SAl Viro 	path_put(&nd->path);
2226648fa861SAl Viro 	return ERR_PTR(error);
2227648fa861SAl Viro }
2228648fa861SAl Viro 
222931e6b01fSNick Piggin /*
223031e6b01fSNick Piggin  * Handle O_CREAT case for do_filp_open
223131e6b01fSNick Piggin  */
2232fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
22335b369df8SAl Viro 			    int open_flag, int acc_mode,
22343e297b61SAl Viro 			    int mode, const char *pathname)
2235fb1cc555SAl Viro {
2236a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
2237fb1cc555SAl Viro 	struct file *filp;
22381f36f774SAl Viro 	int error = -EISDIR;
2239fb1cc555SAl Viro 
22401f36f774SAl Viro 	switch (nd->last_type) {
22411f36f774SAl Viro 	case LAST_DOTDOT:
22421f36f774SAl Viro 		follow_dotdot(nd);
22431f36f774SAl Viro 		dir = nd->path.dentry;
2244176306f5SNeil Brown 	case LAST_DOT:
2245fb045adbSNick Piggin 		if (need_reval_dot(dir)) {
2246f20877d9SJ. R. Okajima 			int status = d_revalidate(nd->path.dentry, nd);
2247f20877d9SJ. R. Okajima 			if (!status)
2248f20877d9SJ. R. Okajima 				status = -ESTALE;
2249f20877d9SJ. R. Okajima 			if (status < 0) {
2250f20877d9SJ. R. Okajima 				error = status;
2251a2c36b45SAl Viro 				goto exit;
22521f36f774SAl Viro 			}
2253f20877d9SJ. R. Okajima 		}
22541f36f774SAl Viro 		/* fallthrough */
22551f36f774SAl Viro 	case LAST_ROOT:
22561f36f774SAl Viro 		goto exit;
22571f36f774SAl Viro 	case LAST_BIND:
22581f36f774SAl Viro 		audit_inode(pathname, dir);
22591f36f774SAl Viro 		goto ok;
22601f36f774SAl Viro 	}
2261a2c36b45SAl Viro 
22621f36f774SAl Viro 	/* trailing slashes? */
226331e6b01fSNick Piggin 	if (nd->last.name[nd->last.len])
22641f36f774SAl Viro 		goto exit;
22651f36f774SAl Viro 
2266a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
2267a1e28038SAl Viro 
2268a1e28038SAl Viro 	path->dentry = lookup_hash(nd);
2269a1e28038SAl Viro 	path->mnt = nd->path.mnt;
2270a1e28038SAl Viro 
2271fb1cc555SAl Viro 	error = PTR_ERR(path->dentry);
2272fb1cc555SAl Viro 	if (IS_ERR(path->dentry)) {
2273fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
2274fb1cc555SAl Viro 		goto exit;
2275fb1cc555SAl Viro 	}
2276fb1cc555SAl Viro 
2277fb1cc555SAl Viro 	if (IS_ERR(nd->intent.open.file)) {
2278fb1cc555SAl Viro 		error = PTR_ERR(nd->intent.open.file);
2279fb1cc555SAl Viro 		goto exit_mutex_unlock;
2280fb1cc555SAl Viro 	}
2281fb1cc555SAl Viro 
2282fb1cc555SAl Viro 	/* Negative dentry, just create the file */
2283fb1cc555SAl Viro 	if (!path->dentry->d_inode) {
2284fb1cc555SAl Viro 		/*
2285fb1cc555SAl Viro 		 * This write is needed to ensure that a
2286fb1cc555SAl Viro 		 * ro->rw transition does not occur between
2287fb1cc555SAl Viro 		 * the time when the file is created and when
2288fb1cc555SAl Viro 		 * a permanent write count is taken through
2289fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
2290fb1cc555SAl Viro 		 */
2291fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
2292fb1cc555SAl Viro 		if (error)
2293fb1cc555SAl Viro 			goto exit_mutex_unlock;
2294fb1cc555SAl Viro 		error = __open_namei_create(nd, path, open_flag, mode);
2295fb1cc555SAl Viro 		if (error) {
2296fb1cc555SAl Viro 			mnt_drop_write(nd->path.mnt);
2297fb1cc555SAl Viro 			goto exit;
2298fb1cc555SAl Viro 		}
2299fb1cc555SAl Viro 		filp = nameidata_to_filp(nd);
2300fb1cc555SAl Viro 		mnt_drop_write(nd->path.mnt);
2301d893f1bcSAl Viro 		path_put(&nd->path);
2302fb1cc555SAl Viro 		if (!IS_ERR(filp)) {
2303fb1cc555SAl Viro 			error = ima_file_check(filp, acc_mode);
2304fb1cc555SAl Viro 			if (error) {
2305fb1cc555SAl Viro 				fput(filp);
2306fb1cc555SAl Viro 				filp = ERR_PTR(error);
2307fb1cc555SAl Viro 			}
2308fb1cc555SAl Viro 		}
2309fb1cc555SAl Viro 		return filp;
2310fb1cc555SAl Viro 	}
2311fb1cc555SAl Viro 
2312fb1cc555SAl Viro 	/*
2313fb1cc555SAl Viro 	 * It already exists.
2314fb1cc555SAl Viro 	 */
2315fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2316fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2317fb1cc555SAl Viro 
2318fb1cc555SAl Viro 	error = -EEXIST;
23195b369df8SAl Viro 	if (open_flag & O_EXCL)
2320fb1cc555SAl Viro 		goto exit_dput;
2321fb1cc555SAl Viro 
23229875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
23239875cf80SDavid Howells 	if (error < 0)
2324fb1cc555SAl Viro 		goto exit_dput;
2325fb1cc555SAl Viro 
2326fb1cc555SAl Viro 	error = -ENOENT;
2327fb1cc555SAl Viro 	if (!path->dentry->d_inode)
2328fb1cc555SAl Viro 		goto exit_dput;
23299e67f361SAl Viro 
23309e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
2331fb1cc555SAl Viro 		return NULL;
2332fb1cc555SAl Viro 
2333fb1cc555SAl Viro 	path_to_nameidata(path, nd);
233431e6b01fSNick Piggin 	nd->inode = path->dentry->d_inode;
2335fb1cc555SAl Viro 	error = -EISDIR;
233631e6b01fSNick Piggin 	if (S_ISDIR(nd->inode->i_mode))
2337fb1cc555SAl Viro 		goto exit;
233867ee3ad2SAl Viro ok:
23399a66179eSAl Viro 	filp = finish_open(nd, open_flag, acc_mode);
2340fb1cc555SAl Viro 	return filp;
2341fb1cc555SAl Viro 
2342fb1cc555SAl Viro exit_mutex_unlock:
2343fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2344fb1cc555SAl Viro exit_dput:
2345fb1cc555SAl Viro 	path_put_conditional(path, nd);
2346fb1cc555SAl Viro exit:
2347fb1cc555SAl Viro 	if (!IS_ERR(nd->intent.open.file))
2348fb1cc555SAl Viro 		release_open_intent(nd);
2349fb1cc555SAl Viro 	path_put(&nd->path);
2350fb1cc555SAl Viro 	return ERR_PTR(error);
2351fb1cc555SAl Viro }
2352fb1cc555SAl Viro 
23534a3fd211SDave Hansen /*
23544a3fd211SDave Hansen  * Note that the low bits of the passed in "open_flag"
23554a3fd211SDave Hansen  * are not the same as in the local variable "flag". See
23564a3fd211SDave Hansen  * open_to_namei_flags() for more details.
23571da177e4SLinus Torvalds  */
2358a70e65dfSChristoph Hellwig struct file *do_filp_open(int dfd, const char *pathname,
23596e8341a1SAl Viro 		int open_flag, int mode, int acc_mode)
23601da177e4SLinus Torvalds {
23614a3fd211SDave Hansen 	struct file *filp;
2362a70e65dfSChristoph Hellwig 	struct nameidata nd;
23636e8341a1SAl Viro 	int error;
23649850c056SAl Viro 	struct path path;
23651da177e4SLinus Torvalds 	int count = 0;
2366d57999e1SDave Hansen 	int flag = open_to_namei_flags(open_flag);
236731e6b01fSNick Piggin 	int flags;
23681f36f774SAl Viro 
23691f36f774SAl Viro 	if (!(open_flag & O_CREAT))
23701f36f774SAl Viro 		mode = 0;
23711da177e4SLinus Torvalds 
2372b1085ba8SLino Sanfilippo 	/* Must never be set by userspace */
2373b1085ba8SLino Sanfilippo 	open_flag &= ~FMODE_NONOTIFY;
2374b1085ba8SLino Sanfilippo 
23756b2f3d1fSChristoph Hellwig 	/*
23766b2f3d1fSChristoph Hellwig 	 * O_SYNC is implemented as __O_SYNC|O_DSYNC.  As many places only
23776b2f3d1fSChristoph Hellwig 	 * check for O_DSYNC if the need any syncing at all we enforce it's
23786b2f3d1fSChristoph Hellwig 	 * always set instead of having to deal with possibly weird behaviour
23796b2f3d1fSChristoph Hellwig 	 * for malicious applications setting only __O_SYNC.
23806b2f3d1fSChristoph Hellwig 	 */
23816b2f3d1fSChristoph Hellwig 	if (open_flag & __O_SYNC)
23826b2f3d1fSChristoph Hellwig 		open_flag |= O_DSYNC;
23836b2f3d1fSChristoph Hellwig 
23846e8341a1SAl Viro 	if (!acc_mode)
23856d125529SAl Viro 		acc_mode = MAY_OPEN | ACC_MODE(open_flag);
23861da177e4SLinus Torvalds 
2387834f2a4aSTrond Myklebust 	/* O_TRUNC implies we need access checks for write permissions */
23884296e2cbSAl Viro 	if (open_flag & O_TRUNC)
2389834f2a4aSTrond Myklebust 		acc_mode |= MAY_WRITE;
2390834f2a4aSTrond Myklebust 
23911da177e4SLinus Torvalds 	/* Allow the LSM permission hook to distinguish append
23921da177e4SLinus Torvalds 	   access from general write access. */
23934296e2cbSAl Viro 	if (open_flag & O_APPEND)
23941da177e4SLinus Torvalds 		acc_mode |= MAY_APPEND;
23951da177e4SLinus Torvalds 
239631e6b01fSNick Piggin 	flags = LOOKUP_OPEN;
239731e6b01fSNick Piggin 	if (open_flag & O_CREAT) {
239831e6b01fSNick Piggin 		flags |= LOOKUP_CREATE;
239931e6b01fSNick Piggin 		if (open_flag & O_EXCL)
240031e6b01fSNick Piggin 			flags |= LOOKUP_EXCL;
2401654f562cSJ. R. Okajima 	}
240231e6b01fSNick Piggin 	if (open_flag & O_DIRECTORY)
240331e6b01fSNick Piggin 		flags |= LOOKUP_DIRECTORY;
240431e6b01fSNick Piggin 	if (!(open_flag & O_NOFOLLOW))
240531e6b01fSNick Piggin 		flags |= LOOKUP_FOLLOW;
240631e6b01fSNick Piggin 
240731e6b01fSNick Piggin 	filp = get_empty_filp();
240831e6b01fSNick Piggin 	if (!filp)
240931e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
241031e6b01fSNick Piggin 
241131e6b01fSNick Piggin 	filp->f_flags = open_flag;
241231e6b01fSNick Piggin 	nd.intent.open.file = filp;
241331e6b01fSNick Piggin 	nd.intent.open.flags = flag;
241431e6b01fSNick Piggin 	nd.intent.open.create_mode = mode;
241531e6b01fSNick Piggin 
241631e6b01fSNick Piggin 	if (open_flag & O_CREAT)
241731e6b01fSNick Piggin 		goto creat;
241831e6b01fSNick Piggin 
241931e6b01fSNick Piggin 	/* !O_CREAT, simple open */
242031e6b01fSNick Piggin 	error = do_path_lookup(dfd, pathname, flags, &nd);
242131e6b01fSNick Piggin 	if (unlikely(error))
242231e6b01fSNick Piggin 		goto out_filp;
242331e6b01fSNick Piggin 	error = -ELOOP;
242431e6b01fSNick Piggin 	if (!(nd.flags & LOOKUP_FOLLOW)) {
242531e6b01fSNick Piggin 		if (nd.inode->i_op->follow_link)
242631e6b01fSNick Piggin 			goto out_path;
242731e6b01fSNick Piggin 	}
242831e6b01fSNick Piggin 	error = -ENOTDIR;
242931e6b01fSNick Piggin 	if (nd.flags & LOOKUP_DIRECTORY) {
243031e6b01fSNick Piggin 		if (!nd.inode->i_op->lookup)
243131e6b01fSNick Piggin 			goto out_path;
243231e6b01fSNick Piggin 	}
243331e6b01fSNick Piggin 	audit_inode(pathname, nd.path.dentry);
243431e6b01fSNick Piggin 	filp = finish_open(&nd, open_flag, acc_mode);
243531e6b01fSNick Piggin 	return filp;
243631e6b01fSNick Piggin 
243731e6b01fSNick Piggin creat:
243831e6b01fSNick Piggin 	/* OK, have to create the file. Find the parent. */
243931e6b01fSNick Piggin 	error = path_init_rcu(dfd, pathname,
244031e6b01fSNick Piggin 			LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd);
244131e6b01fSNick Piggin 	if (error)
244231e6b01fSNick Piggin 		goto out_filp;
244331e6b01fSNick Piggin 	error = path_walk_rcu(pathname, &nd);
244431e6b01fSNick Piggin 	path_finish_rcu(&nd);
244531e6b01fSNick Piggin 	if (unlikely(error == -ECHILD || error == -ESTALE)) {
244631e6b01fSNick Piggin 		/* slower, locked walk */
244731e6b01fSNick Piggin 		if (error == -ESTALE) {
244831e6b01fSNick Piggin reval:
244931e6b01fSNick Piggin 			flags |= LOOKUP_REVAL;
245031e6b01fSNick Piggin 		}
245131e6b01fSNick Piggin 		error = path_init(dfd, pathname,
245231e6b01fSNick Piggin 				LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd);
245331e6b01fSNick Piggin 		if (error)
245431e6b01fSNick Piggin 			goto out_filp;
245531e6b01fSNick Piggin 
245631e6b01fSNick Piggin 		error = path_walk_simple(pathname, &nd);
245731e6b01fSNick Piggin 	}
245831e6b01fSNick Piggin 	if (unlikely(error))
245931e6b01fSNick Piggin 		goto out_filp;
246031e6b01fSNick Piggin 	if (unlikely(!audit_dummy_context()))
24619b4a9b14SAl Viro 		audit_inode(pathname, nd.path.dentry);
24621da177e4SLinus Torvalds 
24631da177e4SLinus Torvalds 	/*
2464a2c36b45SAl Viro 	 * We have the parent and last component.
24651da177e4SLinus Torvalds 	 */
246631e6b01fSNick Piggin 	nd.flags = flags;
24673e297b61SAl Viro 	filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
2468806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
24697b9337aaSNick Piggin 		struct path link = path;
24707b9337aaSNick Piggin 		struct inode *linki = link.dentry->d_inode;
2471def4af30SAl Viro 		void *cookie;
2472806b681cSAl Viro 		error = -ELOOP;
24731f36f774SAl Viro 		/* S_ISDIR part is a temporary automount kludge */
24747b9337aaSNick Piggin 		if (!(nd.flags & LOOKUP_FOLLOW) && !S_ISDIR(linki->i_mode))
24751f36f774SAl Viro 			goto exit_dput;
24761f36f774SAl Viro 		if (count++ == 32)
2477806b681cSAl Viro 			goto exit_dput;
2478806b681cSAl Viro 		/*
2479806b681cSAl Viro 		 * This is subtle. Instead of calling do_follow_link() we do
2480806b681cSAl Viro 		 * the thing by hands. The reason is that this way we have zero
2481806b681cSAl Viro 		 * link_count and path_walk() (called from ->follow_link)
2482806b681cSAl Viro 		 * honoring LOOKUP_PARENT.  After that we have the parent and
2483806b681cSAl Viro 		 * last component, i.e. we are in the same situation as after
2484806b681cSAl Viro 		 * the first path_walk().  Well, almost - if the last component
2485806b681cSAl Viro 		 * is normal we get its copy stored in nd->last.name and we will
2486806b681cSAl Viro 		 * have to putname() it when we are done. Procfs-like symlinks
2487806b681cSAl Viro 		 * just set LAST_BIND.
2488806b681cSAl Viro 		 */
2489806b681cSAl Viro 		nd.flags |= LOOKUP_PARENT;
24907b9337aaSNick Piggin 		error = security_inode_follow_link(link.dentry, &nd);
2491806b681cSAl Viro 		if (error)
2492806b681cSAl Viro 			goto exit_dput;
24937b9337aaSNick Piggin 		error = __do_follow_link(&link, &nd, &cookie);
2494def4af30SAl Viro 		if (unlikely(error)) {
24957b9337aaSNick Piggin 			if (!IS_ERR(cookie) && linki->i_op->put_link)
24967b9337aaSNick Piggin 				linki->i_op->put_link(link.dentry, &nd, cookie);
2497806b681cSAl Viro 			/* nd.path had been dropped */
24987b9337aaSNick Piggin 			nd.path = link;
249931e6b01fSNick Piggin 			goto out_path;
2500806b681cSAl Viro 		}
2501806b681cSAl Viro 		nd.flags &= ~LOOKUP_PARENT;
25023e297b61SAl Viro 		filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
25037b9337aaSNick Piggin 		if (linki->i_op->put_link)
25047b9337aaSNick Piggin 			linki->i_op->put_link(link.dentry, &nd, cookie);
25057b9337aaSNick Piggin 		path_put(&link);
2506806b681cSAl Viro 	}
250710fa8e62SAl Viro out:
25082a737871SAl Viro 	if (nd.root.mnt)
25092a737871SAl Viro 		path_put(&nd.root);
251031e6b01fSNick Piggin 	if (filp == ERR_PTR(-ESTALE) && !(flags & LOOKUP_REVAL))
251110fa8e62SAl Viro 		goto reval;
251210fa8e62SAl Viro 	return filp;
25131da177e4SLinus Torvalds 
2514806b681cSAl Viro exit_dput:
2515806b681cSAl Viro 	path_put_conditional(&path, &nd);
251631e6b01fSNick Piggin out_path:
251731e6b01fSNick Piggin 	path_put(&nd.path);
251831e6b01fSNick Piggin out_filp:
2519806b681cSAl Viro 	if (!IS_ERR(nd.intent.open.file))
2520a70e65dfSChristoph Hellwig 		release_open_intent(&nd);
252110fa8e62SAl Viro 	filp = ERR_PTR(error);
252210fa8e62SAl Viro 	goto out;
2523de459215SKirill Korotaev }
25241da177e4SLinus Torvalds 
25251da177e4SLinus Torvalds /**
2526a70e65dfSChristoph Hellwig  * filp_open - open file and return file pointer
2527a70e65dfSChristoph Hellwig  *
2528a70e65dfSChristoph Hellwig  * @filename:	path to open
2529a70e65dfSChristoph Hellwig  * @flags:	open flags as per the open(2) second argument
2530a70e65dfSChristoph Hellwig  * @mode:	mode for the new file if O_CREAT is set, else ignored
2531a70e65dfSChristoph Hellwig  *
2532a70e65dfSChristoph Hellwig  * This is the helper to open a file from kernelspace if you really
2533a70e65dfSChristoph Hellwig  * have to.  But in generally you should not do this, so please move
2534a70e65dfSChristoph Hellwig  * along, nothing to see here..
2535a70e65dfSChristoph Hellwig  */
2536a70e65dfSChristoph Hellwig struct file *filp_open(const char *filename, int flags, int mode)
2537a70e65dfSChristoph Hellwig {
25386e8341a1SAl Viro 	return do_filp_open(AT_FDCWD, filename, flags, mode, 0);
2539a70e65dfSChristoph Hellwig }
2540a70e65dfSChristoph Hellwig EXPORT_SYMBOL(filp_open);
2541a70e65dfSChristoph Hellwig 
2542a70e65dfSChristoph Hellwig /**
25431da177e4SLinus Torvalds  * lookup_create - lookup a dentry, creating it if it doesn't exist
25441da177e4SLinus Torvalds  * @nd: nameidata info
25451da177e4SLinus Torvalds  * @is_dir: directory flag
25461da177e4SLinus Torvalds  *
25471da177e4SLinus Torvalds  * Simple function to lookup and return a dentry and create it
25481da177e4SLinus Torvalds  * if it doesn't exist.  Is SMP-safe.
2549c663e5d8SChristoph Hellwig  *
25504ac91378SJan Blunck  * Returns with nd->path.dentry->d_inode->i_mutex locked.
25511da177e4SLinus Torvalds  */
25521da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir)
25531da177e4SLinus Torvalds {
2554c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
25551da177e4SLinus Torvalds 
25564ac91378SJan Blunck 	mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2557c663e5d8SChristoph Hellwig 	/*
2558c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2559c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2560c663e5d8SChristoph Hellwig 	 */
25611da177e4SLinus Torvalds 	if (nd->last_type != LAST_NORM)
25621da177e4SLinus Torvalds 		goto fail;
25631da177e4SLinus Torvalds 	nd->flags &= ~LOOKUP_PARENT;
25643516586aSAl Viro 	nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2565a634904aSASANO Masahiro 	nd->intent.open.flags = O_EXCL;
2566c663e5d8SChristoph Hellwig 
2567c663e5d8SChristoph Hellwig 	/*
2568c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2569c663e5d8SChristoph Hellwig 	 */
257049705b77SChristoph Hellwig 	dentry = lookup_hash(nd);
25711da177e4SLinus Torvalds 	if (IS_ERR(dentry))
25721da177e4SLinus Torvalds 		goto fail;
2573c663e5d8SChristoph Hellwig 
2574e9baf6e5SAl Viro 	if (dentry->d_inode)
2575e9baf6e5SAl Viro 		goto eexist;
2576c663e5d8SChristoph Hellwig 	/*
2577c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2578c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2579c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2580c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2581c663e5d8SChristoph Hellwig 	 */
2582e9baf6e5SAl Viro 	if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
25831da177e4SLinus Torvalds 		dput(dentry);
25841da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2585e9baf6e5SAl Viro 	}
2586e9baf6e5SAl Viro 	return dentry;
2587e9baf6e5SAl Viro eexist:
2588e9baf6e5SAl Viro 	dput(dentry);
2589e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
25901da177e4SLinus Torvalds fail:
25911da177e4SLinus Torvalds 	return dentry;
25921da177e4SLinus Torvalds }
2593f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create);
25941da177e4SLinus Torvalds 
25951da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
25961da177e4SLinus Torvalds {
2597a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
25981da177e4SLinus Torvalds 
25991da177e4SLinus Torvalds 	if (error)
26001da177e4SLinus Torvalds 		return error;
26011da177e4SLinus Torvalds 
26021da177e4SLinus Torvalds 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
26031da177e4SLinus Torvalds 		return -EPERM;
26041da177e4SLinus Torvalds 
2605acfa4380SAl Viro 	if (!dir->i_op->mknod)
26061da177e4SLinus Torvalds 		return -EPERM;
26071da177e4SLinus Torvalds 
260808ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
260908ce5f16SSerge E. Hallyn 	if (error)
261008ce5f16SSerge E. Hallyn 		return error;
261108ce5f16SSerge E. Hallyn 
26121da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
26131da177e4SLinus Torvalds 	if (error)
26141da177e4SLinus Torvalds 		return error;
26151da177e4SLinus Torvalds 
26161da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2617a74574aaSStephen Smalley 	if (!error)
2618f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
26191da177e4SLinus Torvalds 	return error;
26201da177e4SLinus Torvalds }
26211da177e4SLinus Torvalds 
2622463c3197SDave Hansen static int may_mknod(mode_t mode)
2623463c3197SDave Hansen {
2624463c3197SDave Hansen 	switch (mode & S_IFMT) {
2625463c3197SDave Hansen 	case S_IFREG:
2626463c3197SDave Hansen 	case S_IFCHR:
2627463c3197SDave Hansen 	case S_IFBLK:
2628463c3197SDave Hansen 	case S_IFIFO:
2629463c3197SDave Hansen 	case S_IFSOCK:
2630463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2631463c3197SDave Hansen 		return 0;
2632463c3197SDave Hansen 	case S_IFDIR:
2633463c3197SDave Hansen 		return -EPERM;
2634463c3197SDave Hansen 	default:
2635463c3197SDave Hansen 		return -EINVAL;
2636463c3197SDave Hansen 	}
2637463c3197SDave Hansen }
2638463c3197SDave Hansen 
26392e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
26402e4d0924SHeiko Carstens 		unsigned, dev)
26411da177e4SLinus Torvalds {
26422ad94ae6SAl Viro 	int error;
26431da177e4SLinus Torvalds 	char *tmp;
26441da177e4SLinus Torvalds 	struct dentry *dentry;
26451da177e4SLinus Torvalds 	struct nameidata nd;
26461da177e4SLinus Torvalds 
26471da177e4SLinus Torvalds 	if (S_ISDIR(mode))
26481da177e4SLinus Torvalds 		return -EPERM;
26491da177e4SLinus Torvalds 
26502ad94ae6SAl Viro 	error = user_path_parent(dfd, filename, &nd, &tmp);
26511da177e4SLinus Torvalds 	if (error)
26522ad94ae6SAl Viro 		return error;
26532ad94ae6SAl Viro 
26541da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
2655463c3197SDave Hansen 	if (IS_ERR(dentry)) {
26561da177e4SLinus Torvalds 		error = PTR_ERR(dentry);
2657463c3197SDave Hansen 		goto out_unlock;
2658463c3197SDave Hansen 	}
26594ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2660ce3b0f8dSAl Viro 		mode &= ~current_umask();
2661463c3197SDave Hansen 	error = may_mknod(mode);
2662463c3197SDave Hansen 	if (error)
2663463c3197SDave Hansen 		goto out_dput;
2664463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2665463c3197SDave Hansen 	if (error)
2666463c3197SDave Hansen 		goto out_dput;
2667be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd.path, dentry, mode, dev);
2668be6d3e56SKentaro Takeda 	if (error)
2669be6d3e56SKentaro Takeda 		goto out_drop_write;
26701da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
26711da177e4SLinus Torvalds 		case 0: case S_IFREG:
26724ac91378SJan Blunck 			error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
26731da177e4SLinus Torvalds 			break;
26741da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
26754ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
26761da177e4SLinus Torvalds 					new_decode_dev(dev));
26771da177e4SLinus Torvalds 			break;
26781da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
26794ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
26801da177e4SLinus Torvalds 			break;
26811da177e4SLinus Torvalds 	}
2682be6d3e56SKentaro Takeda out_drop_write:
2683463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2684463c3197SDave Hansen out_dput:
26851da177e4SLinus Torvalds 	dput(dentry);
2686463c3197SDave Hansen out_unlock:
26874ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
26881d957f9bSJan Blunck 	path_put(&nd.path);
26891da177e4SLinus Torvalds 	putname(tmp);
26901da177e4SLinus Torvalds 
26911da177e4SLinus Torvalds 	return error;
26921da177e4SLinus Torvalds }
26931da177e4SLinus Torvalds 
26943480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
26955590ff0dSUlrich Drepper {
26965590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
26975590ff0dSUlrich Drepper }
26985590ff0dSUlrich Drepper 
26991da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
27001da177e4SLinus Torvalds {
2701a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
27021da177e4SLinus Torvalds 
27031da177e4SLinus Torvalds 	if (error)
27041da177e4SLinus Torvalds 		return error;
27051da177e4SLinus Torvalds 
2706acfa4380SAl Viro 	if (!dir->i_op->mkdir)
27071da177e4SLinus Torvalds 		return -EPERM;
27081da177e4SLinus Torvalds 
27091da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
27101da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
27111da177e4SLinus Torvalds 	if (error)
27121da177e4SLinus Torvalds 		return error;
27131da177e4SLinus Torvalds 
27141da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2715a74574aaSStephen Smalley 	if (!error)
2716f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
27171da177e4SLinus Torvalds 	return error;
27181da177e4SLinus Torvalds }
27191da177e4SLinus Torvalds 
27202e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
27211da177e4SLinus Torvalds {
27221da177e4SLinus Torvalds 	int error = 0;
27231da177e4SLinus Torvalds 	char * tmp;
27246902d925SDave Hansen 	struct dentry *dentry;
27256902d925SDave Hansen 	struct nameidata nd;
27261da177e4SLinus Torvalds 
27272ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &tmp);
27282ad94ae6SAl Viro 	if (error)
27296902d925SDave Hansen 		goto out_err;
27301da177e4SLinus Torvalds 
27311da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 1);
27321da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
27336902d925SDave Hansen 	if (IS_ERR(dentry))
27346902d925SDave Hansen 		goto out_unlock;
27356902d925SDave Hansen 
27364ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2737ce3b0f8dSAl Viro 		mode &= ~current_umask();
2738463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2739463c3197SDave Hansen 	if (error)
2740463c3197SDave Hansen 		goto out_dput;
2741be6d3e56SKentaro Takeda 	error = security_path_mkdir(&nd.path, dentry, mode);
2742be6d3e56SKentaro Takeda 	if (error)
2743be6d3e56SKentaro Takeda 		goto out_drop_write;
27444ac91378SJan Blunck 	error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2745be6d3e56SKentaro Takeda out_drop_write:
2746463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2747463c3197SDave Hansen out_dput:
27481da177e4SLinus Torvalds 	dput(dentry);
27496902d925SDave Hansen out_unlock:
27504ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
27511d957f9bSJan Blunck 	path_put(&nd.path);
27521da177e4SLinus Torvalds 	putname(tmp);
27536902d925SDave Hansen out_err:
27541da177e4SLinus Torvalds 	return error;
27551da177e4SLinus Torvalds }
27561da177e4SLinus Torvalds 
27573cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
27585590ff0dSUlrich Drepper {
27595590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
27605590ff0dSUlrich Drepper }
27615590ff0dSUlrich Drepper 
27621da177e4SLinus Torvalds /*
27631da177e4SLinus Torvalds  * We try to drop the dentry early: we should have
27641da177e4SLinus Torvalds  * a usage count of 2 if we're the only user of this
27651da177e4SLinus Torvalds  * dentry, and if that is true (possibly after pruning
27661da177e4SLinus Torvalds  * the dcache), then we drop the dentry now.
27671da177e4SLinus Torvalds  *
27681da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
27691da177e4SLinus Torvalds  * do a
27701da177e4SLinus Torvalds  *
27711da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
27721da177e4SLinus Torvalds  *		return -EBUSY;
27731da177e4SLinus Torvalds  *
27741da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
27751da177e4SLinus Torvalds  * that is still in use by something else..
27761da177e4SLinus Torvalds  */
27771da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
27781da177e4SLinus Torvalds {
27791da177e4SLinus Torvalds 	dget(dentry);
27801da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
27811da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
2782b7ab39f6SNick Piggin 	if (dentry->d_count == 2)
27831da177e4SLinus Torvalds 		__d_drop(dentry);
27841da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
27851da177e4SLinus Torvalds }
27861da177e4SLinus Torvalds 
27871da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
27881da177e4SLinus Torvalds {
27891da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
27901da177e4SLinus Torvalds 
27911da177e4SLinus Torvalds 	if (error)
27921da177e4SLinus Torvalds 		return error;
27931da177e4SLinus Torvalds 
2794acfa4380SAl Viro 	if (!dir->i_op->rmdir)
27951da177e4SLinus Torvalds 		return -EPERM;
27961da177e4SLinus Torvalds 
27971b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
27981da177e4SLinus Torvalds 	dentry_unhash(dentry);
27991da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
28001da177e4SLinus Torvalds 		error = -EBUSY;
28011da177e4SLinus Torvalds 	else {
28021da177e4SLinus Torvalds 		error = security_inode_rmdir(dir, dentry);
28031da177e4SLinus Torvalds 		if (!error) {
28041da177e4SLinus Torvalds 			error = dir->i_op->rmdir(dir, dentry);
2805d83c49f3SAl Viro 			if (!error) {
28061da177e4SLinus Torvalds 				dentry->d_inode->i_flags |= S_DEAD;
2807d83c49f3SAl Viro 				dont_mount(dentry);
2808d83c49f3SAl Viro 			}
28091da177e4SLinus Torvalds 		}
28101da177e4SLinus Torvalds 	}
28111b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
28121da177e4SLinus Torvalds 	if (!error) {
28131da177e4SLinus Torvalds 		d_delete(dentry);
28141da177e4SLinus Torvalds 	}
28151da177e4SLinus Torvalds 	dput(dentry);
28161da177e4SLinus Torvalds 
28171da177e4SLinus Torvalds 	return error;
28181da177e4SLinus Torvalds }
28191da177e4SLinus Torvalds 
28205590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
28211da177e4SLinus Torvalds {
28221da177e4SLinus Torvalds 	int error = 0;
28231da177e4SLinus Torvalds 	char * name;
28241da177e4SLinus Torvalds 	struct dentry *dentry;
28251da177e4SLinus Torvalds 	struct nameidata nd;
28261da177e4SLinus Torvalds 
28272ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
28281da177e4SLinus Torvalds 	if (error)
28292ad94ae6SAl Viro 		return error;
28301da177e4SLinus Torvalds 
28311da177e4SLinus Torvalds 	switch(nd.last_type) {
28321da177e4SLinus Torvalds 	case LAST_DOTDOT:
28331da177e4SLinus Torvalds 		error = -ENOTEMPTY;
28341da177e4SLinus Torvalds 		goto exit1;
28351da177e4SLinus Torvalds 	case LAST_DOT:
28361da177e4SLinus Torvalds 		error = -EINVAL;
28371da177e4SLinus Torvalds 		goto exit1;
28381da177e4SLinus Torvalds 	case LAST_ROOT:
28391da177e4SLinus Torvalds 		error = -EBUSY;
28401da177e4SLinus Torvalds 		goto exit1;
28411da177e4SLinus Torvalds 	}
28420612d9fbSOGAWA Hirofumi 
28430612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
28440612d9fbSOGAWA Hirofumi 
28454ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
284649705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
28471da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
28486902d925SDave Hansen 	if (IS_ERR(dentry))
28496902d925SDave Hansen 		goto exit2;
28500622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
28510622753bSDave Hansen 	if (error)
28520622753bSDave Hansen 		goto exit3;
2853be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2854be6d3e56SKentaro Takeda 	if (error)
2855be6d3e56SKentaro Takeda 		goto exit4;
28564ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2857be6d3e56SKentaro Takeda exit4:
28580622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
28590622753bSDave Hansen exit3:
28601da177e4SLinus Torvalds 	dput(dentry);
28616902d925SDave Hansen exit2:
28624ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
28631da177e4SLinus Torvalds exit1:
28641d957f9bSJan Blunck 	path_put(&nd.path);
28651da177e4SLinus Torvalds 	putname(name);
28661da177e4SLinus Torvalds 	return error;
28671da177e4SLinus Torvalds }
28681da177e4SLinus Torvalds 
28693cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
28705590ff0dSUlrich Drepper {
28715590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
28725590ff0dSUlrich Drepper }
28735590ff0dSUlrich Drepper 
28741da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
28751da177e4SLinus Torvalds {
28761da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
28771da177e4SLinus Torvalds 
28781da177e4SLinus Torvalds 	if (error)
28791da177e4SLinus Torvalds 		return error;
28801da177e4SLinus Torvalds 
2881acfa4380SAl Viro 	if (!dir->i_op->unlink)
28821da177e4SLinus Torvalds 		return -EPERM;
28831da177e4SLinus Torvalds 
28841b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
28851da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
28861da177e4SLinus Torvalds 		error = -EBUSY;
28871da177e4SLinus Torvalds 	else {
28881da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2889bec1052eSAl Viro 		if (!error) {
28901da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2891bec1052eSAl Viro 			if (!error)
2892d83c49f3SAl Viro 				dont_mount(dentry);
2893bec1052eSAl Viro 		}
28941da177e4SLinus Torvalds 	}
28951b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
28961da177e4SLinus Torvalds 
28971da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
28981da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2899ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
29001da177e4SLinus Torvalds 		d_delete(dentry);
29011da177e4SLinus Torvalds 	}
29020eeca283SRobert Love 
29031da177e4SLinus Torvalds 	return error;
29041da177e4SLinus Torvalds }
29051da177e4SLinus Torvalds 
29061da177e4SLinus Torvalds /*
29071da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
29081b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
29091da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
29101da177e4SLinus Torvalds  * while waiting on the I/O.
29111da177e4SLinus Torvalds  */
29125590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
29131da177e4SLinus Torvalds {
29142ad94ae6SAl Viro 	int error;
29151da177e4SLinus Torvalds 	char *name;
29161da177e4SLinus Torvalds 	struct dentry *dentry;
29171da177e4SLinus Torvalds 	struct nameidata nd;
29181da177e4SLinus Torvalds 	struct inode *inode = NULL;
29191da177e4SLinus Torvalds 
29202ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
29211da177e4SLinus Torvalds 	if (error)
29222ad94ae6SAl Viro 		return error;
29232ad94ae6SAl Viro 
29241da177e4SLinus Torvalds 	error = -EISDIR;
29251da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
29261da177e4SLinus Torvalds 		goto exit1;
29270612d9fbSOGAWA Hirofumi 
29280612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
29290612d9fbSOGAWA Hirofumi 
29304ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
293149705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
29321da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
29331da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
29341da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
29351da177e4SLinus Torvalds 		if (nd.last.name[nd.last.len])
29361da177e4SLinus Torvalds 			goto slashes;
29371da177e4SLinus Torvalds 		inode = dentry->d_inode;
29381da177e4SLinus Torvalds 		if (inode)
29397de9c6eeSAl Viro 			ihold(inode);
29400622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
29410622753bSDave Hansen 		if (error)
29420622753bSDave Hansen 			goto exit2;
2943be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2944be6d3e56SKentaro Takeda 		if (error)
2945be6d3e56SKentaro Takeda 			goto exit3;
29464ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2947be6d3e56SKentaro Takeda exit3:
29480622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
29491da177e4SLinus Torvalds 	exit2:
29501da177e4SLinus Torvalds 		dput(dentry);
29511da177e4SLinus Torvalds 	}
29524ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
29531da177e4SLinus Torvalds 	if (inode)
29541da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
29551da177e4SLinus Torvalds exit1:
29561d957f9bSJan Blunck 	path_put(&nd.path);
29571da177e4SLinus Torvalds 	putname(name);
29581da177e4SLinus Torvalds 	return error;
29591da177e4SLinus Torvalds 
29601da177e4SLinus Torvalds slashes:
29611da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
29621da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
29631da177e4SLinus Torvalds 	goto exit2;
29641da177e4SLinus Torvalds }
29651da177e4SLinus Torvalds 
29662e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
29675590ff0dSUlrich Drepper {
29685590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
29695590ff0dSUlrich Drepper 		return -EINVAL;
29705590ff0dSUlrich Drepper 
29715590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
29725590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
29735590ff0dSUlrich Drepper 
29745590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
29755590ff0dSUlrich Drepper }
29765590ff0dSUlrich Drepper 
29773480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
29785590ff0dSUlrich Drepper {
29795590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
29805590ff0dSUlrich Drepper }
29815590ff0dSUlrich Drepper 
2982db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
29831da177e4SLinus Torvalds {
2984a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
29851da177e4SLinus Torvalds 
29861da177e4SLinus Torvalds 	if (error)
29871da177e4SLinus Torvalds 		return error;
29881da177e4SLinus Torvalds 
2989acfa4380SAl Viro 	if (!dir->i_op->symlink)
29901da177e4SLinus Torvalds 		return -EPERM;
29911da177e4SLinus Torvalds 
29921da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
29931da177e4SLinus Torvalds 	if (error)
29941da177e4SLinus Torvalds 		return error;
29951da177e4SLinus Torvalds 
29961da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2997a74574aaSStephen Smalley 	if (!error)
2998f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
29991da177e4SLinus Torvalds 	return error;
30001da177e4SLinus Torvalds }
30011da177e4SLinus Torvalds 
30022e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
30032e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
30041da177e4SLinus Torvalds {
30052ad94ae6SAl Viro 	int error;
30061da177e4SLinus Torvalds 	char *from;
30071da177e4SLinus Torvalds 	char *to;
30086902d925SDave Hansen 	struct dentry *dentry;
30096902d925SDave Hansen 	struct nameidata nd;
30101da177e4SLinus Torvalds 
30111da177e4SLinus Torvalds 	from = getname(oldname);
30121da177e4SLinus Torvalds 	if (IS_ERR(from))
30131da177e4SLinus Torvalds 		return PTR_ERR(from);
30142ad94ae6SAl Viro 
30152ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
30162ad94ae6SAl Viro 	if (error)
30176902d925SDave Hansen 		goto out_putname;
30181da177e4SLinus Torvalds 
30191da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
30201da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
30216902d925SDave Hansen 	if (IS_ERR(dentry))
30226902d925SDave Hansen 		goto out_unlock;
30236902d925SDave Hansen 
302475c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
302575c3f29dSDave Hansen 	if (error)
302675c3f29dSDave Hansen 		goto out_dput;
3027be6d3e56SKentaro Takeda 	error = security_path_symlink(&nd.path, dentry, from);
3028be6d3e56SKentaro Takeda 	if (error)
3029be6d3e56SKentaro Takeda 		goto out_drop_write;
3030db2e747bSMiklos Szeredi 	error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
3031be6d3e56SKentaro Takeda out_drop_write:
303275c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
303375c3f29dSDave Hansen out_dput:
30341da177e4SLinus Torvalds 	dput(dentry);
30356902d925SDave Hansen out_unlock:
30364ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
30371d957f9bSJan Blunck 	path_put(&nd.path);
30381da177e4SLinus Torvalds 	putname(to);
30396902d925SDave Hansen out_putname:
30401da177e4SLinus Torvalds 	putname(from);
30411da177e4SLinus Torvalds 	return error;
30421da177e4SLinus Torvalds }
30431da177e4SLinus Torvalds 
30443480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
30455590ff0dSUlrich Drepper {
30465590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
30475590ff0dSUlrich Drepper }
30485590ff0dSUlrich Drepper 
30491da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
30501da177e4SLinus Torvalds {
30511da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
30521da177e4SLinus Torvalds 	int error;
30531da177e4SLinus Torvalds 
30541da177e4SLinus Torvalds 	if (!inode)
30551da177e4SLinus Torvalds 		return -ENOENT;
30561da177e4SLinus Torvalds 
3057a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
30581da177e4SLinus Torvalds 	if (error)
30591da177e4SLinus Torvalds 		return error;
30601da177e4SLinus Torvalds 
30611da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
30621da177e4SLinus Torvalds 		return -EXDEV;
30631da177e4SLinus Torvalds 
30641da177e4SLinus Torvalds 	/*
30651da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
30661da177e4SLinus Torvalds 	 */
30671da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
30681da177e4SLinus Torvalds 		return -EPERM;
3069acfa4380SAl Viro 	if (!dir->i_op->link)
30701da177e4SLinus Torvalds 		return -EPERM;
30717e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
30721da177e4SLinus Torvalds 		return -EPERM;
30731da177e4SLinus Torvalds 
30741da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
30751da177e4SLinus Torvalds 	if (error)
30761da177e4SLinus Torvalds 		return error;
30771da177e4SLinus Torvalds 
30787e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
30791da177e4SLinus Torvalds 	error = dir->i_op->link(old_dentry, dir, new_dentry);
30807e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
3081e31e14ecSStephen Smalley 	if (!error)
30827e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
30831da177e4SLinus Torvalds 	return error;
30841da177e4SLinus Torvalds }
30851da177e4SLinus Torvalds 
30861da177e4SLinus Torvalds /*
30871da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
30881da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
30891da177e4SLinus Torvalds  * newname.  --KAB
30901da177e4SLinus Torvalds  *
30911da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
30921da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
30931da177e4SLinus Torvalds  * and other special files.  --ADM
30941da177e4SLinus Torvalds  */
30952e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
30962e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
30971da177e4SLinus Torvalds {
30981da177e4SLinus Torvalds 	struct dentry *new_dentry;
30992d8f3038SAl Viro 	struct nameidata nd;
31002d8f3038SAl Viro 	struct path old_path;
31011da177e4SLinus Torvalds 	int error;
31021da177e4SLinus Torvalds 	char *to;
31031da177e4SLinus Torvalds 
310445c9b11aSUlrich Drepper 	if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
3105c04030e1SUlrich Drepper 		return -EINVAL;
3106c04030e1SUlrich Drepper 
31072d8f3038SAl Viro 	error = user_path_at(olddfd, oldname,
310845c9b11aSUlrich Drepper 			     flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
31092d8f3038SAl Viro 			     &old_path);
31101da177e4SLinus Torvalds 	if (error)
31112ad94ae6SAl Viro 		return error;
31122ad94ae6SAl Viro 
31132ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
31141da177e4SLinus Torvalds 	if (error)
31151da177e4SLinus Torvalds 		goto out;
31161da177e4SLinus Torvalds 	error = -EXDEV;
31172d8f3038SAl Viro 	if (old_path.mnt != nd.path.mnt)
31181da177e4SLinus Torvalds 		goto out_release;
31191da177e4SLinus Torvalds 	new_dentry = lookup_create(&nd, 0);
31201da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
31216902d925SDave Hansen 	if (IS_ERR(new_dentry))
31226902d925SDave Hansen 		goto out_unlock;
312375c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
312475c3f29dSDave Hansen 	if (error)
312575c3f29dSDave Hansen 		goto out_dput;
3126be6d3e56SKentaro Takeda 	error = security_path_link(old_path.dentry, &nd.path, new_dentry);
3127be6d3e56SKentaro Takeda 	if (error)
3128be6d3e56SKentaro Takeda 		goto out_drop_write;
31292d8f3038SAl Viro 	error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
3130be6d3e56SKentaro Takeda out_drop_write:
313175c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
313275c3f29dSDave Hansen out_dput:
31331da177e4SLinus Torvalds 	dput(new_dentry);
31346902d925SDave Hansen out_unlock:
31354ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
31361da177e4SLinus Torvalds out_release:
31371d957f9bSJan Blunck 	path_put(&nd.path);
31382ad94ae6SAl Viro 	putname(to);
31391da177e4SLinus Torvalds out:
31402d8f3038SAl Viro 	path_put(&old_path);
31411da177e4SLinus Torvalds 
31421da177e4SLinus Torvalds 	return error;
31431da177e4SLinus Torvalds }
31441da177e4SLinus Torvalds 
31453480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
31465590ff0dSUlrich Drepper {
3147c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
31485590ff0dSUlrich Drepper }
31495590ff0dSUlrich Drepper 
31501da177e4SLinus Torvalds /*
31511da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
31521da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
31531da177e4SLinus Torvalds  * Problems:
31541da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
31551da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
31561da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
3157a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
31581da177e4SLinus Torvalds  *	   story.
31591da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
31601b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
31611da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
31621da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
3163a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
31641da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
31651da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
31661da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
3167a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
31681da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
31691da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
31701da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
31711da177e4SLinus Torvalds  *	d) some filesystems don't support opened-but-unlinked directories,
31721da177e4SLinus Torvalds  *	   either because of layout or because they are not ready to deal with
31731da177e4SLinus Torvalds  *	   all cases correctly. The latter will be fixed (taking this sort of
31741da177e4SLinus Torvalds  *	   stuff into VFS), but the former is not going away. Solution: the same
31751da177e4SLinus Torvalds  *	   trick as in rmdir().
31761da177e4SLinus Torvalds  *	e) conversion from fhandle to dentry may come in the wrong moment - when
31771b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
31781da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3179c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
31801da177e4SLinus Torvalds  *	   locking].
31811da177e4SLinus Torvalds  */
318275c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
31831da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
31841da177e4SLinus Torvalds {
31851da177e4SLinus Torvalds 	int error = 0;
31861da177e4SLinus Torvalds 	struct inode *target;
31871da177e4SLinus Torvalds 
31881da177e4SLinus Torvalds 	/*
31891da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
31901da177e4SLinus Torvalds 	 * we'll need to flip '..'.
31911da177e4SLinus Torvalds 	 */
31921da177e4SLinus Torvalds 	if (new_dir != old_dir) {
3193f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
31941da177e4SLinus Torvalds 		if (error)
31951da177e4SLinus Torvalds 			return error;
31961da177e4SLinus Torvalds 	}
31971da177e4SLinus Torvalds 
31981da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
31991da177e4SLinus Torvalds 	if (error)
32001da177e4SLinus Torvalds 		return error;
32011da177e4SLinus Torvalds 
32021da177e4SLinus Torvalds 	target = new_dentry->d_inode;
3203d83c49f3SAl Viro 	if (target)
32041b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
32051da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
32061da177e4SLinus Torvalds 		error = -EBUSY;
3207d83c49f3SAl Viro 	else {
3208d83c49f3SAl Viro 		if (target)
3209d83c49f3SAl Viro 			dentry_unhash(new_dentry);
32101da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3211d83c49f3SAl Viro 	}
32121da177e4SLinus Torvalds 	if (target) {
3213d83c49f3SAl Viro 		if (!error) {
32141da177e4SLinus Torvalds 			target->i_flags |= S_DEAD;
3215d83c49f3SAl Viro 			dont_mount(new_dentry);
3216d83c49f3SAl Viro 		}
32171b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
32181da177e4SLinus Torvalds 		if (d_unhashed(new_dentry))
32191da177e4SLinus Torvalds 			d_rehash(new_dentry);
32201da177e4SLinus Torvalds 		dput(new_dentry);
32211da177e4SLinus Torvalds 	}
3222e31e14ecSStephen Smalley 	if (!error)
3223349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
32241da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
32251da177e4SLinus Torvalds 	return error;
32261da177e4SLinus Torvalds }
32271da177e4SLinus Torvalds 
322875c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
32291da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
32301da177e4SLinus Torvalds {
32311da177e4SLinus Torvalds 	struct inode *target;
32321da177e4SLinus Torvalds 	int error;
32331da177e4SLinus Torvalds 
32341da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
32351da177e4SLinus Torvalds 	if (error)
32361da177e4SLinus Torvalds 		return error;
32371da177e4SLinus Torvalds 
32381da177e4SLinus Torvalds 	dget(new_dentry);
32391da177e4SLinus Torvalds 	target = new_dentry->d_inode;
32401da177e4SLinus Torvalds 	if (target)
32411b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
32421da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
32431da177e4SLinus Torvalds 		error = -EBUSY;
32441da177e4SLinus Torvalds 	else
32451da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
32461da177e4SLinus Torvalds 	if (!error) {
3247bec1052eSAl Viro 		if (target)
3248d83c49f3SAl Viro 			dont_mount(new_dentry);
3249349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
32501da177e4SLinus Torvalds 			d_move(old_dentry, new_dentry);
32511da177e4SLinus Torvalds 	}
32521da177e4SLinus Torvalds 	if (target)
32531b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
32541da177e4SLinus Torvalds 	dput(new_dentry);
32551da177e4SLinus Torvalds 	return error;
32561da177e4SLinus Torvalds }
32571da177e4SLinus Torvalds 
32581da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
32591da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
32601da177e4SLinus Torvalds {
32611da177e4SLinus Torvalds 	int error;
32621da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
326359b0df21SEric Paris 	const unsigned char *old_name;
32641da177e4SLinus Torvalds 
32651da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
32661da177e4SLinus Torvalds  		return 0;
32671da177e4SLinus Torvalds 
32681da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
32691da177e4SLinus Torvalds 	if (error)
32701da177e4SLinus Torvalds 		return error;
32711da177e4SLinus Torvalds 
32721da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3273a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
32741da177e4SLinus Torvalds 	else
32751da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
32761da177e4SLinus Torvalds 	if (error)
32771da177e4SLinus Torvalds 		return error;
32781da177e4SLinus Torvalds 
3279acfa4380SAl Viro 	if (!old_dir->i_op->rename)
32801da177e4SLinus Torvalds 		return -EPERM;
32811da177e4SLinus Torvalds 
32820eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
32830eeca283SRobert Love 
32841da177e4SLinus Torvalds 	if (is_dir)
32851da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
32861da177e4SLinus Torvalds 	else
32871da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3288123df294SAl Viro 	if (!error)
3289123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
32905a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
32910eeca283SRobert Love 	fsnotify_oldname_free(old_name);
32920eeca283SRobert Love 
32931da177e4SLinus Torvalds 	return error;
32941da177e4SLinus Torvalds }
32951da177e4SLinus Torvalds 
32962e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
32972e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
32981da177e4SLinus Torvalds {
32991da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
33001da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
33011da177e4SLinus Torvalds 	struct dentry *trap;
33021da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
33032ad94ae6SAl Viro 	char *from;
33042ad94ae6SAl Viro 	char *to;
33052ad94ae6SAl Viro 	int error;
33061da177e4SLinus Torvalds 
33072ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
33081da177e4SLinus Torvalds 	if (error)
33091da177e4SLinus Torvalds 		goto exit;
33101da177e4SLinus Torvalds 
33112ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
33121da177e4SLinus Torvalds 	if (error)
33131da177e4SLinus Torvalds 		goto exit1;
33141da177e4SLinus Torvalds 
33151da177e4SLinus Torvalds 	error = -EXDEV;
33164ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
33171da177e4SLinus Torvalds 		goto exit2;
33181da177e4SLinus Torvalds 
33194ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
33201da177e4SLinus Torvalds 	error = -EBUSY;
33211da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
33221da177e4SLinus Torvalds 		goto exit2;
33231da177e4SLinus Torvalds 
33244ac91378SJan Blunck 	new_dir = newnd.path.dentry;
33251da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
33261da177e4SLinus Torvalds 		goto exit2;
33271da177e4SLinus Torvalds 
33280612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
33290612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
33304e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
33310612d9fbSOGAWA Hirofumi 
33321da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
33331da177e4SLinus Torvalds 
333449705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
33351da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
33361da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
33371da177e4SLinus Torvalds 		goto exit3;
33381da177e4SLinus Torvalds 	/* source must exist */
33391da177e4SLinus Torvalds 	error = -ENOENT;
33401da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
33411da177e4SLinus Torvalds 		goto exit4;
33421da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
33431da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
33441da177e4SLinus Torvalds 		error = -ENOTDIR;
33451da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
33461da177e4SLinus Torvalds 			goto exit4;
33471da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
33481da177e4SLinus Torvalds 			goto exit4;
33491da177e4SLinus Torvalds 	}
33501da177e4SLinus Torvalds 	/* source should not be ancestor of target */
33511da177e4SLinus Torvalds 	error = -EINVAL;
33521da177e4SLinus Torvalds 	if (old_dentry == trap)
33531da177e4SLinus Torvalds 		goto exit4;
335449705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
33551da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
33561da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
33571da177e4SLinus Torvalds 		goto exit4;
33581da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
33591da177e4SLinus Torvalds 	error = -ENOTEMPTY;
33601da177e4SLinus Torvalds 	if (new_dentry == trap)
33611da177e4SLinus Torvalds 		goto exit5;
33621da177e4SLinus Torvalds 
33639079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
33649079b1ebSDave Hansen 	if (error)
33659079b1ebSDave Hansen 		goto exit5;
3366be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3367be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3368be6d3e56SKentaro Takeda 	if (error)
3369be6d3e56SKentaro Takeda 		goto exit6;
33701da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
33711da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3372be6d3e56SKentaro Takeda exit6:
33739079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
33741da177e4SLinus Torvalds exit5:
33751da177e4SLinus Torvalds 	dput(new_dentry);
33761da177e4SLinus Torvalds exit4:
33771da177e4SLinus Torvalds 	dput(old_dentry);
33781da177e4SLinus Torvalds exit3:
33791da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
33801da177e4SLinus Torvalds exit2:
33811d957f9bSJan Blunck 	path_put(&newnd.path);
33822ad94ae6SAl Viro 	putname(to);
33831da177e4SLinus Torvalds exit1:
33841d957f9bSJan Blunck 	path_put(&oldnd.path);
33851da177e4SLinus Torvalds 	putname(from);
33862ad94ae6SAl Viro exit:
33871da177e4SLinus Torvalds 	return error;
33881da177e4SLinus Torvalds }
33891da177e4SLinus Torvalds 
3390a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
33915590ff0dSUlrich Drepper {
33925590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
33935590ff0dSUlrich Drepper }
33945590ff0dSUlrich Drepper 
33951da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
33961da177e4SLinus Torvalds {
33971da177e4SLinus Torvalds 	int len;
33981da177e4SLinus Torvalds 
33991da177e4SLinus Torvalds 	len = PTR_ERR(link);
34001da177e4SLinus Torvalds 	if (IS_ERR(link))
34011da177e4SLinus Torvalds 		goto out;
34021da177e4SLinus Torvalds 
34031da177e4SLinus Torvalds 	len = strlen(link);
34041da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
34051da177e4SLinus Torvalds 		len = buflen;
34061da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
34071da177e4SLinus Torvalds 		len = -EFAULT;
34081da177e4SLinus Torvalds out:
34091da177e4SLinus Torvalds 	return len;
34101da177e4SLinus Torvalds }
34111da177e4SLinus Torvalds 
34121da177e4SLinus Torvalds /*
34131da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
34141da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
34151da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
34161da177e4SLinus Torvalds  */
34171da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
34181da177e4SLinus Torvalds {
34191da177e4SLinus Torvalds 	struct nameidata nd;
3420cc314eefSLinus Torvalds 	void *cookie;
3421694a1764SMarcin Slusarz 	int res;
3422cc314eefSLinus Torvalds 
34231da177e4SLinus Torvalds 	nd.depth = 0;
3424cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3425694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3426694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3427694a1764SMarcin Slusarz 
3428694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
34291da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3430cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3431694a1764SMarcin Slusarz 	return res;
34321da177e4SLinus Torvalds }
34331da177e4SLinus Torvalds 
34341da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
34351da177e4SLinus Torvalds {
34361da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
34371da177e4SLinus Torvalds }
34381da177e4SLinus Torvalds 
34391da177e4SLinus Torvalds /* get the link contents into pagecache */
34401da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
34411da177e4SLinus Torvalds {
3442ebd09abbSDuane Griffin 	char *kaddr;
34431da177e4SLinus Torvalds 	struct page *page;
34441da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3445090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
34461da177e4SLinus Torvalds 	if (IS_ERR(page))
34476fe6900eSNick Piggin 		return (char*)page;
34481da177e4SLinus Torvalds 	*ppage = page;
3449ebd09abbSDuane Griffin 	kaddr = kmap(page);
3450ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3451ebd09abbSDuane Griffin 	return kaddr;
34521da177e4SLinus Torvalds }
34531da177e4SLinus Torvalds 
34541da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
34551da177e4SLinus Torvalds {
34561da177e4SLinus Torvalds 	struct page *page = NULL;
34571da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
34581da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
34591da177e4SLinus Torvalds 	if (page) {
34601da177e4SLinus Torvalds 		kunmap(page);
34611da177e4SLinus Torvalds 		page_cache_release(page);
34621da177e4SLinus Torvalds 	}
34631da177e4SLinus Torvalds 	return res;
34641da177e4SLinus Torvalds }
34651da177e4SLinus Torvalds 
3466cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
34671da177e4SLinus Torvalds {
3468cc314eefSLinus Torvalds 	struct page *page = NULL;
34691da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3470cc314eefSLinus Torvalds 	return page;
34711da177e4SLinus Torvalds }
34721da177e4SLinus Torvalds 
3473cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
34741da177e4SLinus Torvalds {
3475cc314eefSLinus Torvalds 	struct page *page = cookie;
3476cc314eefSLinus Torvalds 
3477cc314eefSLinus Torvalds 	if (page) {
34781da177e4SLinus Torvalds 		kunmap(page);
34791da177e4SLinus Torvalds 		page_cache_release(page);
34801da177e4SLinus Torvalds 	}
34811da177e4SLinus Torvalds }
34821da177e4SLinus Torvalds 
348354566b2cSNick Piggin /*
348454566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
348554566b2cSNick Piggin  */
348654566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
34871da177e4SLinus Torvalds {
34881da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
34890adb25d2SKirill Korotaev 	struct page *page;
3490afddba49SNick Piggin 	void *fsdata;
3491beb497abSDmitriy Monakhov 	int err;
34921da177e4SLinus Torvalds 	char *kaddr;
349354566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
349454566b2cSNick Piggin 	if (nofs)
349554566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
34961da177e4SLinus Torvalds 
34977e53cac4SNeilBrown retry:
3498afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
349954566b2cSNick Piggin 				flags, &page, &fsdata);
35001da177e4SLinus Torvalds 	if (err)
3501afddba49SNick Piggin 		goto fail;
3502afddba49SNick Piggin 
35031da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
35041da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
35051da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
3506afddba49SNick Piggin 
3507afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3508afddba49SNick Piggin 							page, fsdata);
35091da177e4SLinus Torvalds 	if (err < 0)
35101da177e4SLinus Torvalds 		goto fail;
3511afddba49SNick Piggin 	if (err < len-1)
3512afddba49SNick Piggin 		goto retry;
3513afddba49SNick Piggin 
35141da177e4SLinus Torvalds 	mark_inode_dirty(inode);
35151da177e4SLinus Torvalds 	return 0;
35161da177e4SLinus Torvalds fail:
35171da177e4SLinus Torvalds 	return err;
35181da177e4SLinus Torvalds }
35191da177e4SLinus Torvalds 
35200adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
35210adb25d2SKirill Korotaev {
35220adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
352354566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
35240adb25d2SKirill Korotaev }
35250adb25d2SKirill Korotaev 
352692e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
35271da177e4SLinus Torvalds 	.readlink	= generic_readlink,
35281da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
35291da177e4SLinus Torvalds 	.put_link	= page_put_link,
35301da177e4SLinus Torvalds };
35311da177e4SLinus Torvalds 
35322d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
35331da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
35341da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
35351da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
35361da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
35371da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
35381da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
35391da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
35401da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
35411da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
35420adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
35431da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
35441da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
35451da177e4SLinus Torvalds EXPORT_SYMBOL(path_lookup);
3546d1811465SAl Viro EXPORT_SYMBOL(kern_path);
354716f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3548f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
35498c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission);
35501da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
35511da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
35521da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
35531da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
35541da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
35551da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
35561da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
35571da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
35581da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
35591da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
35601da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
35611da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
35621da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
35631da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3564