xref: /openbmc/linux/fs/namei.c (revision 4455ca62)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/namei.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  * Some corrections by tytso.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
121da177e4SLinus Torvalds  * lookup logic.
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/init.h>
181da177e4SLinus Torvalds #include <linux/module.h>
191da177e4SLinus Torvalds #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/fs.h>
211da177e4SLinus Torvalds #include <linux/namei.h>
221da177e4SLinus Torvalds #include <linux/pagemap.h>
230eeca283SRobert Love #include <linux/fsnotify.h>
241da177e4SLinus Torvalds #include <linux/personality.h>
251da177e4SLinus Torvalds #include <linux/security.h>
266146f0d5SMimi Zohar #include <linux/ima.h>
271da177e4SLinus Torvalds #include <linux/syscalls.h>
281da177e4SLinus Torvalds #include <linux/mount.h>
291da177e4SLinus Torvalds #include <linux/audit.h>
3016f7e0feSRandy Dunlap #include <linux/capability.h>
31834f2a4aSTrond Myklebust #include <linux/file.h>
325590ff0dSUlrich Drepper #include <linux/fcntl.h>
3308ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h>
345ad4e53bSAl Viro #include <linux/fs_struct.h>
351da177e4SLinus Torvalds #include <asm/uaccess.h>
361da177e4SLinus Torvalds 
37e81e3f4dSEric Paris #include "internal.h"
38e81e3f4dSEric Paris 
391da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer]
401da177e4SLinus Torvalds  * Fundamental changes in the pathname lookup mechanisms (namei)
411da177e4SLinus Torvalds  * were necessary because of omirr.  The reason is that omirr needs
421da177e4SLinus Torvalds  * to know the _real_ pathname, not the user-supplied one, in case
431da177e4SLinus Torvalds  * of symlinks (and also when transname replacements occur).
441da177e4SLinus Torvalds  *
451da177e4SLinus Torvalds  * The new code replaces the old recursive symlink resolution with
461da177e4SLinus Torvalds  * an iterative one (in case of non-nested symlink chains).  It does
471da177e4SLinus Torvalds  * this with calls to <fs>_follow_link().
481da177e4SLinus Torvalds  * As a side effect, dir_namei(), _namei() and follow_link() are now
491da177e4SLinus Torvalds  * replaced with a single function lookup_dentry() that can handle all
501da177e4SLinus Torvalds  * the special cases of the former code.
511da177e4SLinus Torvalds  *
521da177e4SLinus Torvalds  * With the new dcache, the pathname is stored at each inode, at least as
531da177e4SLinus Torvalds  * long as the refcount of the inode is positive.  As a side effect, the
541da177e4SLinus Torvalds  * size of the dcache depends on the inode cache and thus is dynamic.
551da177e4SLinus Torvalds  *
561da177e4SLinus Torvalds  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
571da177e4SLinus Torvalds  * resolution to correspond with current state of the code.
581da177e4SLinus Torvalds  *
591da177e4SLinus Torvalds  * Note that the symlink resolution is not *completely* iterative.
601da177e4SLinus Torvalds  * There is still a significant amount of tail- and mid- recursion in
611da177e4SLinus Torvalds  * the algorithm.  Also, note that <fs>_readlink() is not used in
621da177e4SLinus Torvalds  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
631da177e4SLinus Torvalds  * may return different results than <fs>_follow_link().  Many virtual
641da177e4SLinus Torvalds  * filesystems (including /proc) exhibit this behavior.
651da177e4SLinus Torvalds  */
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
681da177e4SLinus Torvalds  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
691da177e4SLinus Torvalds  * and the name already exists in form of a symlink, try to create the new
701da177e4SLinus Torvalds  * name indicated by the symlink. The old code always complained that the
711da177e4SLinus Torvalds  * name already exists, due to not following the symlink even if its target
721da177e4SLinus Torvalds  * is nonexistent.  The new semantics affects also mknod() and link() when
731da177e4SLinus Torvalds  * the name is a symlink pointing to a non-existant name.
741da177e4SLinus Torvalds  *
751da177e4SLinus Torvalds  * I don't know which semantics is the right one, since I have no access
761da177e4SLinus Torvalds  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
771da177e4SLinus Torvalds  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
781da177e4SLinus Torvalds  * "old" one. Personally, I think the new semantics is much more logical.
791da177e4SLinus Torvalds  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
801da177e4SLinus Torvalds  * file does succeed in both HP-UX and SunOs, but not in Solaris
811da177e4SLinus Torvalds  * and in the old Linux semantics.
821da177e4SLinus Torvalds  */
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
851da177e4SLinus Torvalds  * semantics.  See the comments in "open_namei" and "do_link" below.
861da177e4SLinus Torvalds  *
871da177e4SLinus Torvalds  * [10-Sep-98 Alan Modra] Another symlink change.
881da177e4SLinus Torvalds  */
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
911da177e4SLinus Torvalds  *	inside the path - always follow.
921da177e4SLinus Torvalds  *	in the last component in creation/removal/renaming - never follow.
931da177e4SLinus Torvalds  *	if LOOKUP_FOLLOW passed - follow.
941da177e4SLinus Torvalds  *	if the pathname has trailing slashes - follow.
951da177e4SLinus Torvalds  *	otherwise - don't follow.
961da177e4SLinus Torvalds  * (applied in that order).
971da177e4SLinus Torvalds  *
981da177e4SLinus Torvalds  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
991da177e4SLinus Torvalds  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
1001da177e4SLinus Torvalds  * During the 2.4 we need to fix the userland stuff depending on it -
1011da177e4SLinus Torvalds  * hopefully we will be able to get rid of that wart in 2.5. So far only
1021da177e4SLinus Torvalds  * XEmacs seems to be relying on it...
1031da177e4SLinus Torvalds  */
1041da177e4SLinus Torvalds /*
1051da177e4SLinus Torvalds  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
106a11f3a05SArjan van de Ven  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
1071da177e4SLinus Torvalds  * any extra contention...
1081da177e4SLinus Torvalds  */
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional
1111da177e4SLinus Torvalds  * checking and hopefully speeding things up, we copy filenames to the
1121da177e4SLinus Torvalds  * kernel data space before using them..
1131da177e4SLinus Torvalds  *
1141da177e4SLinus Torvalds  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
1151da177e4SLinus Torvalds  * PATH_MAX includes the nul terminator --RR.
1161da177e4SLinus Torvalds  */
117858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page)
1181da177e4SLinus Torvalds {
1191da177e4SLinus Torvalds 	int retval;
1201da177e4SLinus Torvalds 	unsigned long len = PATH_MAX;
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	if (!segment_eq(get_fs(), KERNEL_DS)) {
1231da177e4SLinus Torvalds 		if ((unsigned long) filename >= TASK_SIZE)
1241da177e4SLinus Torvalds 			return -EFAULT;
1251da177e4SLinus Torvalds 		if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
1261da177e4SLinus Torvalds 			len = TASK_SIZE - (unsigned long) filename;
1271da177e4SLinus Torvalds 	}
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	retval = strncpy_from_user(page, filename, len);
1301da177e4SLinus Torvalds 	if (retval > 0) {
1311da177e4SLinus Torvalds 		if (retval < len)
1321da177e4SLinus Torvalds 			return 0;
1331da177e4SLinus Torvalds 		return -ENAMETOOLONG;
1341da177e4SLinus Torvalds 	} else if (!retval)
1351da177e4SLinus Torvalds 		retval = -ENOENT;
1361da177e4SLinus Torvalds 	return retval;
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds char * getname(const char __user * filename)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	char *tmp, *result;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	result = ERR_PTR(-ENOMEM);
1441da177e4SLinus Torvalds 	tmp = __getname();
1451da177e4SLinus Torvalds 	if (tmp)  {
1461da177e4SLinus Torvalds 		int retval = do_getname(filename, tmp);
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 		result = tmp;
1491da177e4SLinus Torvalds 		if (retval < 0) {
1501da177e4SLinus Torvalds 			__putname(tmp);
1511da177e4SLinus Torvalds 			result = ERR_PTR(retval);
1521da177e4SLinus Torvalds 		}
1531da177e4SLinus Torvalds 	}
1541da177e4SLinus Torvalds 	audit_getname(result);
1551da177e4SLinus Torvalds 	return result;
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
1591da177e4SLinus Torvalds void putname(const char *name)
1601da177e4SLinus Torvalds {
1615ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
1621da177e4SLinus Torvalds 		audit_putname(name);
1631da177e4SLinus Torvalds 	else
1641da177e4SLinus Torvalds 		__putname(name);
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds EXPORT_SYMBOL(putname);
1671da177e4SLinus Torvalds #endif
1681da177e4SLinus Torvalds 
1695909ccaaSLinus Torvalds /*
1705909ccaaSLinus Torvalds  * This does basic POSIX ACL permission checking
1715909ccaaSLinus Torvalds  */
172b74c79e9SNick Piggin static int acl_permission_check(struct inode *inode, int mask, unsigned int flags,
173b74c79e9SNick Piggin 		int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
1745909ccaaSLinus Torvalds {
1755909ccaaSLinus Torvalds 	umode_t			mode = inode->i_mode;
1765909ccaaSLinus Torvalds 
1775909ccaaSLinus Torvalds 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
1785909ccaaSLinus Torvalds 
1795909ccaaSLinus Torvalds 	if (current_fsuid() == inode->i_uid)
1805909ccaaSLinus Torvalds 		mode >>= 6;
1815909ccaaSLinus Torvalds 	else {
1825909ccaaSLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
183b74c79e9SNick Piggin 			int error = check_acl(inode, mask, flags);
1845909ccaaSLinus Torvalds 			if (error != -EAGAIN)
1855909ccaaSLinus Torvalds 				return error;
1865909ccaaSLinus Torvalds 		}
1875909ccaaSLinus Torvalds 
1885909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
1895909ccaaSLinus Torvalds 			mode >>= 3;
1905909ccaaSLinus Torvalds 	}
1915909ccaaSLinus Torvalds 
1925909ccaaSLinus Torvalds 	/*
1935909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
1945909ccaaSLinus Torvalds 	 */
1955909ccaaSLinus Torvalds 	if ((mask & ~mode) == 0)
1965909ccaaSLinus Torvalds 		return 0;
1975909ccaaSLinus Torvalds 	return -EACCES;
1985909ccaaSLinus Torvalds }
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds /**
2011da177e4SLinus Torvalds  * generic_permission -  check for access rights on a Posix-like filesystem
2021da177e4SLinus Torvalds  * @inode:	inode to check access rights for
2031da177e4SLinus Torvalds  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
2041da177e4SLinus Torvalds  * @check_acl:	optional callback to check for Posix ACLs
20539191628SRandy Dunlap  * @flags:	IPERM_FLAG_ flags.
2061da177e4SLinus Torvalds  *
2071da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
2081da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
2091da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
210b74c79e9SNick Piggin  * are used for other things.
211b74c79e9SNick Piggin  *
212b74c79e9SNick Piggin  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
213b74c79e9SNick Piggin  * request cannot be satisfied (eg. requires blocking or too much complexity).
214b74c79e9SNick Piggin  * It would then be called again in ref-walk mode.
2151da177e4SLinus Torvalds  */
216b74c79e9SNick Piggin int generic_permission(struct inode *inode, int mask, unsigned int flags,
217b74c79e9SNick Piggin 	int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
2181da177e4SLinus Torvalds {
2195909ccaaSLinus Torvalds 	int ret;
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	/*
2225909ccaaSLinus Torvalds 	 * Do the basic POSIX ACL permission checks.
2231da177e4SLinus Torvalds 	 */
224b74c79e9SNick Piggin 	ret = acl_permission_check(inode, mask, flags, check_acl);
2255909ccaaSLinus Torvalds 	if (ret != -EACCES)
2265909ccaaSLinus Torvalds 		return ret;
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	/*
2291da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
2301da177e4SLinus Torvalds 	 * Executable DACs are overridable if at least one exec bit is set.
2311da177e4SLinus Torvalds 	 */
232f696a365SMiklos Szeredi 	if (!(mask & MAY_EXEC) || execute_ok(inode))
2331da177e4SLinus Torvalds 		if (capable(CAP_DAC_OVERRIDE))
2341da177e4SLinus Torvalds 			return 0;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	/*
2371da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
2381da177e4SLinus Torvalds 	 */
2397ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
2401da177e4SLinus Torvalds 	if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
2411da177e4SLinus Torvalds 		if (capable(CAP_DAC_READ_SEARCH))
2421da177e4SLinus Torvalds 			return 0;
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	return -EACCES;
2451da177e4SLinus Torvalds }
2461da177e4SLinus Torvalds 
247cb23beb5SChristoph Hellwig /**
248cb23beb5SChristoph Hellwig  * inode_permission  -  check for access rights to a given inode
249cb23beb5SChristoph Hellwig  * @inode:	inode to check permission on
250cb23beb5SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
251cb23beb5SChristoph Hellwig  *
252cb23beb5SChristoph Hellwig  * Used to check for read/write/execute permissions on an inode.
253cb23beb5SChristoph Hellwig  * We use "fsuid" for this, letting us set arbitrary permissions
254cb23beb5SChristoph Hellwig  * for filesystem access without changing the "normal" uids which
255cb23beb5SChristoph Hellwig  * are used for other things.
256cb23beb5SChristoph Hellwig  */
257f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask)
2581da177e4SLinus Torvalds {
259e6305c43SAl Viro 	int retval;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	if (mask & MAY_WRITE) {
26222590e41SMiklos Szeredi 		umode_t mode = inode->i_mode;
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 		/*
2651da177e4SLinus Torvalds 		 * Nobody gets write access to a read-only fs.
2661da177e4SLinus Torvalds 		 */
2671da177e4SLinus Torvalds 		if (IS_RDONLY(inode) &&
2681da177e4SLinus Torvalds 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
2691da177e4SLinus Torvalds 			return -EROFS;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 		/*
2721da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
2731da177e4SLinus Torvalds 		 */
2741da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
2751da177e4SLinus Torvalds 			return -EACCES;
2761da177e4SLinus Torvalds 	}
2771da177e4SLinus Torvalds 
278acfa4380SAl Viro 	if (inode->i_op->permission)
279b74c79e9SNick Piggin 		retval = inode->i_op->permission(inode, mask, 0);
280f696a365SMiklos Szeredi 	else
281b74c79e9SNick Piggin 		retval = generic_permission(inode, mask, 0,
282b74c79e9SNick Piggin 				inode->i_op->check_acl);
283f696a365SMiklos Szeredi 
2841da177e4SLinus Torvalds 	if (retval)
2851da177e4SLinus Torvalds 		return retval;
2861da177e4SLinus Torvalds 
28708ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
28808ce5f16SSerge E. Hallyn 	if (retval)
28908ce5f16SSerge E. Hallyn 		return retval;
29008ce5f16SSerge E. Hallyn 
291d09ca739SEric Paris 	return security_inode_permission(inode, mask);
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
294e4543eddSChristoph Hellwig /**
2958c744fb8SChristoph Hellwig  * file_permission  -  check for additional access rights to a given file
2968c744fb8SChristoph Hellwig  * @file:	file to check access rights for
2978c744fb8SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
2988c744fb8SChristoph Hellwig  *
2998c744fb8SChristoph Hellwig  * Used to check for read/write/execute permissions on an already opened
3008c744fb8SChristoph Hellwig  * file.
3018c744fb8SChristoph Hellwig  *
3028c744fb8SChristoph Hellwig  * Note:
3038c744fb8SChristoph Hellwig  *	Do not use this function in new code.  All access checks should
304cb23beb5SChristoph Hellwig  *	be done using inode_permission().
3058c744fb8SChristoph Hellwig  */
3068c744fb8SChristoph Hellwig int file_permission(struct file *file, int mask)
3078c744fb8SChristoph Hellwig {
308f419a2e3SAl Viro 	return inode_permission(file->f_path.dentry->d_inode, mask);
3098c744fb8SChristoph Hellwig }
3108c744fb8SChristoph Hellwig 
3111da177e4SLinus Torvalds /*
3121da177e4SLinus Torvalds  * get_write_access() gets write permission for a file.
3131da177e4SLinus Torvalds  * put_write_access() releases this write permission.
3141da177e4SLinus Torvalds  * This is used for regular files.
3151da177e4SLinus Torvalds  * We cannot support write (and maybe mmap read-write shared) accesses and
3161da177e4SLinus Torvalds  * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
3171da177e4SLinus Torvalds  * can have the following values:
3181da177e4SLinus Torvalds  * 0: no writers, no VM_DENYWRITE mappings
3191da177e4SLinus Torvalds  * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
3201da177e4SLinus Torvalds  * > 0: (i_writecount) users are writing to the file.
3211da177e4SLinus Torvalds  *
3221da177e4SLinus Torvalds  * Normally we operate on that counter with atomic_{inc,dec} and it's safe
3231da177e4SLinus Torvalds  * except for the cases where we don't hold i_writecount yet. Then we need to
3241da177e4SLinus Torvalds  * use {get,deny}_write_access() - these functions check the sign and refuse
3251da177e4SLinus Torvalds  * to do the change if sign is wrong. Exclusion between them is provided by
3261da177e4SLinus Torvalds  * the inode->i_lock spinlock.
3271da177e4SLinus Torvalds  */
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds int get_write_access(struct inode * inode)
3301da177e4SLinus Torvalds {
3311da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
3321da177e4SLinus Torvalds 	if (atomic_read(&inode->i_writecount) < 0) {
3331da177e4SLinus Torvalds 		spin_unlock(&inode->i_lock);
3341da177e4SLinus Torvalds 		return -ETXTBSY;
3351da177e4SLinus Torvalds 	}
3361da177e4SLinus Torvalds 	atomic_inc(&inode->i_writecount);
3371da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	return 0;
3401da177e4SLinus Torvalds }
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds int deny_write_access(struct file * file)
3431da177e4SLinus Torvalds {
3440f7fc9e4SJosef "Jeff" Sipek 	struct inode *inode = file->f_path.dentry->d_inode;
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
3471da177e4SLinus Torvalds 	if (atomic_read(&inode->i_writecount) > 0) {
3481da177e4SLinus Torvalds 		spin_unlock(&inode->i_lock);
3491da177e4SLinus Torvalds 		return -ETXTBSY;
3501da177e4SLinus Torvalds 	}
3511da177e4SLinus Torvalds 	atomic_dec(&inode->i_writecount);
3521da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 	return 0;
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
3571d957f9bSJan Blunck /**
3585dd784d0SJan Blunck  * path_get - get a reference to a path
3595dd784d0SJan Blunck  * @path: path to get the reference to
3605dd784d0SJan Blunck  *
3615dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
3625dd784d0SJan Blunck  */
3635dd784d0SJan Blunck void path_get(struct path *path)
3645dd784d0SJan Blunck {
3655dd784d0SJan Blunck 	mntget(path->mnt);
3665dd784d0SJan Blunck 	dget(path->dentry);
3675dd784d0SJan Blunck }
3685dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
3695dd784d0SJan Blunck 
3705dd784d0SJan Blunck /**
3711d957f9bSJan Blunck  * path_put - put a reference to a path
3721d957f9bSJan Blunck  * @path: path to put the reference to
3731d957f9bSJan Blunck  *
3741d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
3751d957f9bSJan Blunck  */
3761d957f9bSJan Blunck void path_put(struct path *path)
3771da177e4SLinus Torvalds {
3781d957f9bSJan Blunck 	dput(path->dentry);
3791d957f9bSJan Blunck 	mntput(path->mnt);
3801da177e4SLinus Torvalds }
3811d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
3821da177e4SLinus Torvalds 
383834f2a4aSTrond Myklebust /**
38431e6b01fSNick Piggin  * nameidata_drop_rcu - drop this nameidata out of rcu-walk
38531e6b01fSNick Piggin  * @nd: nameidata pathwalk data to drop
38639191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
38731e6b01fSNick Piggin  *
38831e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
38931e6b01fSNick Piggin  * Documentation/filesystems/path-lookup.txt). __drop_rcu* functions attempt
39031e6b01fSNick Piggin  * to drop out of rcu-walk mode and take normal reference counts on dentries
39131e6b01fSNick Piggin  * and vfsmounts to transition to rcu-walk mode. __drop_rcu* functions take
39231e6b01fSNick Piggin  * refcounts at the last known good point before rcu-walk got stuck, so
39331e6b01fSNick Piggin  * ref-walk may continue from there. If this is not successful (eg. a seqcount
39431e6b01fSNick Piggin  * has changed), then failure is returned and path walk restarts from the
39531e6b01fSNick Piggin  * beginning in ref-walk mode.
39631e6b01fSNick Piggin  *
39731e6b01fSNick Piggin  * nameidata_drop_rcu attempts to drop the current nd->path and nd->root into
39831e6b01fSNick Piggin  * ref-walk. Must be called from rcu-walk context.
39931e6b01fSNick Piggin  */
40031e6b01fSNick Piggin static int nameidata_drop_rcu(struct nameidata *nd)
40131e6b01fSNick Piggin {
40231e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
40331e6b01fSNick Piggin 	struct dentry *dentry = nd->path.dentry;
40431e6b01fSNick Piggin 
40531e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
40631e6b01fSNick Piggin 	if (nd->root.mnt) {
40731e6b01fSNick Piggin 		spin_lock(&fs->lock);
40831e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
40931e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
41031e6b01fSNick Piggin 			goto err_root;
41131e6b01fSNick Piggin 	}
41231e6b01fSNick Piggin 	spin_lock(&dentry->d_lock);
41331e6b01fSNick Piggin 	if (!__d_rcu_to_refcount(dentry, nd->seq))
41431e6b01fSNick Piggin 		goto err;
41531e6b01fSNick Piggin 	BUG_ON(nd->inode != dentry->d_inode);
41631e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
41731e6b01fSNick Piggin 	if (nd->root.mnt) {
41831e6b01fSNick Piggin 		path_get(&nd->root);
41931e6b01fSNick Piggin 		spin_unlock(&fs->lock);
42031e6b01fSNick Piggin 	}
42131e6b01fSNick Piggin 	mntget(nd->path.mnt);
42231e6b01fSNick Piggin 
42331e6b01fSNick Piggin 	rcu_read_unlock();
42431e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
42531e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
42631e6b01fSNick Piggin 	return 0;
42731e6b01fSNick Piggin err:
42831e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
42931e6b01fSNick Piggin err_root:
43031e6b01fSNick Piggin 	if (nd->root.mnt)
43131e6b01fSNick Piggin 		spin_unlock(&fs->lock);
43231e6b01fSNick Piggin 	return -ECHILD;
43331e6b01fSNick Piggin }
43431e6b01fSNick Piggin 
43531e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing.  */
43631e6b01fSNick Piggin static inline int nameidata_drop_rcu_maybe(struct nameidata *nd)
43731e6b01fSNick Piggin {
43831e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU)
43931e6b01fSNick Piggin 		return nameidata_drop_rcu(nd);
44031e6b01fSNick Piggin 	return 0;
44131e6b01fSNick Piggin }
44231e6b01fSNick Piggin 
44331e6b01fSNick Piggin /**
44431e6b01fSNick Piggin  * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk
44531e6b01fSNick Piggin  * @nd: nameidata pathwalk data to drop
44631e6b01fSNick Piggin  * @dentry: dentry to drop
44739191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
44831e6b01fSNick Piggin  *
44931e6b01fSNick Piggin  * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root,
45031e6b01fSNick Piggin  * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on
45131e6b01fSNick Piggin  * @nd. Must be called from rcu-walk context.
45231e6b01fSNick Piggin  */
45331e6b01fSNick Piggin static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry)
45431e6b01fSNick Piggin {
45531e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
45631e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
45731e6b01fSNick Piggin 
45831e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
45931e6b01fSNick Piggin 	if (nd->root.mnt) {
46031e6b01fSNick Piggin 		spin_lock(&fs->lock);
46131e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
46231e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
46331e6b01fSNick Piggin 			goto err_root;
46431e6b01fSNick Piggin 	}
46531e6b01fSNick Piggin 	spin_lock(&parent->d_lock);
46631e6b01fSNick Piggin 	spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
46731e6b01fSNick Piggin 	if (!__d_rcu_to_refcount(dentry, nd->seq))
46831e6b01fSNick Piggin 		goto err;
46931e6b01fSNick Piggin 	/*
47031e6b01fSNick Piggin 	 * If the sequence check on the child dentry passed, then the child has
47131e6b01fSNick Piggin 	 * not been removed from its parent. This means the parent dentry must
47231e6b01fSNick Piggin 	 * be valid and able to take a reference at this point.
47331e6b01fSNick Piggin 	 */
47431e6b01fSNick Piggin 	BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
47531e6b01fSNick Piggin 	BUG_ON(!parent->d_count);
47631e6b01fSNick Piggin 	parent->d_count++;
47731e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
47831e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
47931e6b01fSNick Piggin 	if (nd->root.mnt) {
48031e6b01fSNick Piggin 		path_get(&nd->root);
48131e6b01fSNick Piggin 		spin_unlock(&fs->lock);
48231e6b01fSNick Piggin 	}
48331e6b01fSNick Piggin 	mntget(nd->path.mnt);
48431e6b01fSNick Piggin 
48531e6b01fSNick Piggin 	rcu_read_unlock();
48631e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
48731e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
48831e6b01fSNick Piggin 	return 0;
48931e6b01fSNick Piggin err:
49031e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
49131e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
49231e6b01fSNick Piggin err_root:
49331e6b01fSNick Piggin 	if (nd->root.mnt)
49431e6b01fSNick Piggin 		spin_unlock(&fs->lock);
49531e6b01fSNick Piggin 	return -ECHILD;
49631e6b01fSNick Piggin }
49731e6b01fSNick Piggin 
49831e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing.  */
49931e6b01fSNick Piggin static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry)
50031e6b01fSNick Piggin {
50131e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU)
50231e6b01fSNick Piggin 		return nameidata_dentry_drop_rcu(nd, dentry);
50331e6b01fSNick Piggin 	return 0;
50431e6b01fSNick Piggin }
50531e6b01fSNick Piggin 
50631e6b01fSNick Piggin /**
50731e6b01fSNick Piggin  * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk
50831e6b01fSNick Piggin  * @nd: nameidata pathwalk data to drop
50939191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
51031e6b01fSNick Piggin  *
51131e6b01fSNick Piggin  * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk.
51231e6b01fSNick Piggin  * nd->path should be the final element of the lookup, so nd->root is discarded.
51331e6b01fSNick Piggin  * Must be called from rcu-walk context.
51431e6b01fSNick Piggin  */
51531e6b01fSNick Piggin static int nameidata_drop_rcu_last(struct nameidata *nd)
51631e6b01fSNick Piggin {
51731e6b01fSNick Piggin 	struct dentry *dentry = nd->path.dentry;
51831e6b01fSNick Piggin 
51931e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
52031e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
52131e6b01fSNick Piggin 	nd->root.mnt = NULL;
52231e6b01fSNick Piggin 	spin_lock(&dentry->d_lock);
52331e6b01fSNick Piggin 	if (!__d_rcu_to_refcount(dentry, nd->seq))
52431e6b01fSNick Piggin 		goto err_unlock;
52531e6b01fSNick Piggin 	BUG_ON(nd->inode != dentry->d_inode);
52631e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
52731e6b01fSNick Piggin 
52831e6b01fSNick Piggin 	mntget(nd->path.mnt);
52931e6b01fSNick Piggin 
53031e6b01fSNick Piggin 	rcu_read_unlock();
53131e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
53231e6b01fSNick Piggin 
53331e6b01fSNick Piggin 	return 0;
53431e6b01fSNick Piggin 
53531e6b01fSNick Piggin err_unlock:
53631e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
53731e6b01fSNick Piggin 	rcu_read_unlock();
53831e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
53931e6b01fSNick Piggin 	return -ECHILD;
54031e6b01fSNick Piggin }
54131e6b01fSNick Piggin 
54231e6b01fSNick Piggin /**
543834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
544834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
545834f2a4aSTrond Myklebust  */
546834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
547834f2a4aSTrond Myklebust {
5482dab5974SLinus Torvalds 	struct file *file = nd->intent.open.file;
5492dab5974SLinus Torvalds 
5502dab5974SLinus Torvalds 	if (file && !IS_ERR(file)) {
5512dab5974SLinus Torvalds 		if (file->f_path.dentry == NULL)
5522dab5974SLinus Torvalds 			put_filp(file);
553834f2a4aSTrond Myklebust 		else
5542dab5974SLinus Torvalds 			fput(file);
5552dab5974SLinus Torvalds 	}
556834f2a4aSTrond Myklebust }
557834f2a4aSTrond Myklebust 
558f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
55934286d66SNick Piggin {
560f60aef7eSAl Viro 	return dentry->d_op->d_revalidate(dentry, nd);
56134286d66SNick Piggin }
56234286d66SNick Piggin 
563f5e1c1c1SAl Viro static struct dentry *
564bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd)
565bcdc5e01SIan Kent {
566f5e1c1c1SAl Viro 	int status = d_revalidate(dentry, nd);
567bcdc5e01SIan Kent 	if (unlikely(status <= 0)) {
568bcdc5e01SIan Kent 		/*
569bcdc5e01SIan Kent 		 * The dentry failed validation.
570bcdc5e01SIan Kent 		 * If d_revalidate returned 0 attempt to invalidate
571bcdc5e01SIan Kent 		 * the dentry otherwise d_revalidate is asking us
572bcdc5e01SIan Kent 		 * to return a fail status.
573bcdc5e01SIan Kent 		 */
57434286d66SNick Piggin 		if (status < 0) {
57534286d66SNick Piggin 			dput(dentry);
57634286d66SNick Piggin 			dentry = ERR_PTR(status);
577f5e1c1c1SAl Viro 		} else if (!d_invalidate(dentry)) {
578bcdc5e01SIan Kent 			dput(dentry);
579bcdc5e01SIan Kent 			dentry = NULL;
580bcdc5e01SIan Kent 		}
581bcdc5e01SIan Kent 	}
582f5e1c1c1SAl Viro 	return dentry;
583f5e1c1c1SAl Viro }
584f5e1c1c1SAl Viro 
585f5e1c1c1SAl Viro static inline struct dentry *
586f5e1c1c1SAl Viro do_revalidate_rcu(struct dentry *dentry, struct nameidata *nd)
587f5e1c1c1SAl Viro {
588f60aef7eSAl Viro 	int status = d_revalidate(dentry, nd);
589f5e1c1c1SAl Viro 	if (likely(status > 0))
590f5e1c1c1SAl Viro 		return dentry;
591f5e1c1c1SAl Viro 	if (status == -ECHILD) {
592f5e1c1c1SAl Viro 		if (nameidata_dentry_drop_rcu(nd, dentry))
593f5e1c1c1SAl Viro 			return ERR_PTR(-ECHILD);
594f5e1c1c1SAl Viro 		return do_revalidate(dentry, nd);
595f5e1c1c1SAl Viro 	}
596f5e1c1c1SAl Viro 	if (status < 0)
597f5e1c1c1SAl Viro 		return ERR_PTR(status);
598f5e1c1c1SAl Viro 	/* Don't d_invalidate in rcu-walk mode */
599f5e1c1c1SAl Viro 	if (nameidata_dentry_drop_rcu(nd, dentry))
600f5e1c1c1SAl Viro 		return ERR_PTR(-ECHILD);
601f5e1c1c1SAl Viro 	if (!d_invalidate(dentry)) {
602f5e1c1c1SAl Viro 		dput(dentry);
603f5e1c1c1SAl Viro 		dentry = NULL;
604bcdc5e01SIan Kent 	}
605bcdc5e01SIan Kent 	return dentry;
606bcdc5e01SIan Kent }
607bcdc5e01SIan Kent 
6081da177e4SLinus Torvalds /*
60916c2cd71SAl Viro  * handle_reval_path - force revalidation of a dentry
61039159de2SJeff Layton  *
61139159de2SJeff Layton  * In some situations the path walking code will trust dentries without
61239159de2SJeff Layton  * revalidating them. This causes problems for filesystems that depend on
61339159de2SJeff Layton  * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
61439159de2SJeff Layton  * (which indicates that it's possible for the dentry to go stale), force
61539159de2SJeff Layton  * a d_revalidate call before proceeding.
61639159de2SJeff Layton  *
61739159de2SJeff Layton  * Returns 0 if the revalidation was successful. If the revalidation fails,
61839159de2SJeff Layton  * either return the error returned by d_revalidate or -ESTALE if the
61939159de2SJeff Layton  * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
62039159de2SJeff Layton  * invalidate the dentry. It's up to the caller to handle putting references
62139159de2SJeff Layton  * to the path if necessary.
62239159de2SJeff Layton  */
62316c2cd71SAl Viro static inline int handle_reval_path(struct nameidata *nd)
62439159de2SJeff Layton {
62516c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
62639159de2SJeff Layton 	int status;
62739159de2SJeff Layton 
62816c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
62939159de2SJeff Layton 		return 0;
63039159de2SJeff Layton 
63116c2cd71SAl Viro 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
63216c2cd71SAl Viro 		return 0;
63316c2cd71SAl Viro 
63416c2cd71SAl Viro 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
63516c2cd71SAl Viro 		return 0;
63616c2cd71SAl Viro 
63716c2cd71SAl Viro 	/* Note: we do not d_invalidate() */
63834286d66SNick Piggin 	status = d_revalidate(dentry, nd);
63939159de2SJeff Layton 	if (status > 0)
64039159de2SJeff Layton 		return 0;
64139159de2SJeff Layton 
64216c2cd71SAl Viro 	if (!status)
64339159de2SJeff Layton 		status = -ESTALE;
64416c2cd71SAl Viro 
64539159de2SJeff Layton 	return status;
64639159de2SJeff Layton }
64739159de2SJeff Layton 
64839159de2SJeff Layton /*
649b75b5086SAl Viro  * Short-cut version of permission(), for calling on directories
650b75b5086SAl Viro  * during pathname resolution.  Combines parts of permission()
651b75b5086SAl Viro  * and generic_permission(), and tests ONLY for MAY_EXEC permission.
6521da177e4SLinus Torvalds  *
6531da177e4SLinus Torvalds  * If appropriate, check DAC only.  If not appropriate, or
654b75b5086SAl Viro  * short-cut DAC fails, then call ->permission() to do more
6551da177e4SLinus Torvalds  * complete permission check.
6561da177e4SLinus Torvalds  */
657b74c79e9SNick Piggin static inline int exec_permission(struct inode *inode, unsigned int flags)
6581da177e4SLinus Torvalds {
6595909ccaaSLinus Torvalds 	int ret;
6601da177e4SLinus Torvalds 
661cb9179eaSLinus Torvalds 	if (inode->i_op->permission) {
662b74c79e9SNick Piggin 		ret = inode->i_op->permission(inode, MAY_EXEC, flags);
663b74c79e9SNick Piggin 	} else {
664b74c79e9SNick Piggin 		ret = acl_permission_check(inode, MAY_EXEC, flags,
665b74c79e9SNick Piggin 				inode->i_op->check_acl);
666cb9179eaSLinus Torvalds 	}
667b74c79e9SNick Piggin 	if (likely(!ret))
6681da177e4SLinus Torvalds 		goto ok;
669b74c79e9SNick Piggin 	if (ret == -ECHILD)
67031e6b01fSNick Piggin 		return ret;
6711da177e4SLinus Torvalds 
672f1ac9f6bSLinus Torvalds 	if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH))
6731da177e4SLinus Torvalds 		goto ok;
6741da177e4SLinus Torvalds 
6755909ccaaSLinus Torvalds 	return ret;
6761da177e4SLinus Torvalds ok:
677b74c79e9SNick Piggin 	return security_inode_exec_permission(inode, flags);
6781da177e4SLinus Torvalds }
6791da177e4SLinus Torvalds 
6802a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
6812a737871SAl Viro {
682f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
683f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
6842a737871SAl Viro }
6852a737871SAl Viro 
6866de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
6876de88d72SAl Viro 
68831e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
68931e6b01fSNick Piggin {
69031e6b01fSNick Piggin 	if (!nd->root.mnt) {
69131e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
692c28cc364SNick Piggin 		unsigned seq;
693c28cc364SNick Piggin 
694c28cc364SNick Piggin 		do {
695c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
69631e6b01fSNick Piggin 			nd->root = fs->root;
697c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
69831e6b01fSNick Piggin 	}
69931e6b01fSNick Piggin }
70031e6b01fSNick Piggin 
701f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
7021da177e4SLinus Torvalds {
70331e6b01fSNick Piggin 	int ret;
70431e6b01fSNick Piggin 
7051da177e4SLinus Torvalds 	if (IS_ERR(link))
7061da177e4SLinus Torvalds 		goto fail;
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 	if (*link == '/') {
7092a737871SAl Viro 		set_root(nd);
7101d957f9bSJan Blunck 		path_put(&nd->path);
7112a737871SAl Viro 		nd->path = nd->root;
7122a737871SAl Viro 		path_get(&nd->root);
71316c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
7141da177e4SLinus Torvalds 	}
71531e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
716b4091d5fSChristoph Hellwig 
71731e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
71831e6b01fSNick Piggin 	return ret;
7191da177e4SLinus Torvalds fail:
7201d957f9bSJan Blunck 	path_put(&nd->path);
7211da177e4SLinus Torvalds 	return PTR_ERR(link);
7221da177e4SLinus Torvalds }
7231da177e4SLinus Torvalds 
7241d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
725051d3812SIan Kent {
726051d3812SIan Kent 	dput(path->dentry);
7274ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
728051d3812SIan Kent 		mntput(path->mnt);
729051d3812SIan Kent }
730051d3812SIan Kent 
7317b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
7327b9337aaSNick Piggin 					struct nameidata *nd)
733051d3812SIan Kent {
73431e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
7354ac91378SJan Blunck 		dput(nd->path.dentry);
73631e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
7374ac91378SJan Blunck 			mntput(nd->path.mnt);
7389a229683SHuang Shijie 	}
73931e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
7404ac91378SJan Blunck 	nd->path.dentry = path->dentry;
741051d3812SIan Kent }
742051d3812SIan Kent 
743def4af30SAl Viro static __always_inline int
7447b9337aaSNick Piggin __do_follow_link(const struct path *link, struct nameidata *nd, void **p)
7451da177e4SLinus Torvalds {
7461da177e4SLinus Torvalds 	int error;
7477b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
7481da177e4SLinus Torvalds 
749844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
750844a3917SAl Viro 
7517b9337aaSNick Piggin 	touch_atime(link->mnt, dentry);
7521da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
753cd4e91d3SAl Viro 
75487556ef1SDavid Howells 	if (link->mnt == nd->path.mnt)
7557b9337aaSNick Piggin 		mntget(link->mnt);
75631e6b01fSNick Piggin 
75736f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
75836f3b4f6SAl Viro 	if (error) {
75936f3b4f6SAl Viro 		*p = ERR_PTR(error); /* no ->put_link(), please */
76036f3b4f6SAl Viro 		path_put(&nd->path);
76136f3b4f6SAl Viro 		return error;
76236f3b4f6SAl Viro 	}
76336f3b4f6SAl Viro 
76486acdca1SAl Viro 	nd->last_type = LAST_BIND;
765def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
766def4af30SAl Viro 	error = PTR_ERR(*p);
767def4af30SAl Viro 	if (!IS_ERR(*p)) {
7681da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
769cc314eefSLinus Torvalds 		error = 0;
7701da177e4SLinus Torvalds 		if (s)
7711da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
77216c2cd71SAl Viro 		else if (nd->last_type == LAST_BIND)
77316c2cd71SAl Viro 			nd->flags |= LOOKUP_JUMPED;
7741da177e4SLinus Torvalds 	}
7751da177e4SLinus Torvalds 	return error;
7761da177e4SLinus Torvalds }
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds /*
7791da177e4SLinus Torvalds  * This limits recursive symlink follows to 8, while
7801da177e4SLinus Torvalds  * limiting consecutive symlinks to 40.
7811da177e4SLinus Torvalds  *
7821da177e4SLinus Torvalds  * Without that kind of total limit, nasty chains of consecutive
7831da177e4SLinus Torvalds  * symlinks can cause almost arbitrarily long lookups.
7841da177e4SLinus Torvalds  */
7853abb17e8SLinus Torvalds static inline int do_follow_link(struct inode *inode, struct path *path, struct nameidata *nd)
7861da177e4SLinus Torvalds {
787def4af30SAl Viro 	void *cookie;
7881da177e4SLinus Torvalds 	int err = -ELOOP;
789844a3917SAl Viro 
790844a3917SAl Viro 	/* We drop rcu-walk here */
791844a3917SAl Viro 	if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry))
792844a3917SAl Viro 		return -ECHILD;
7933abb17e8SLinus Torvalds 	BUG_ON(inode != path->dentry->d_inode);
794844a3917SAl Viro 
7951da177e4SLinus Torvalds 	if (current->link_count >= MAX_NESTED_LINKS)
7961da177e4SLinus Torvalds 		goto loop;
7971da177e4SLinus Torvalds 	if (current->total_link_count >= 40)
7981da177e4SLinus Torvalds 		goto loop;
7991da177e4SLinus Torvalds 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
8001da177e4SLinus Torvalds 	cond_resched();
8011da177e4SLinus Torvalds 	current->link_count++;
8021da177e4SLinus Torvalds 	current->total_link_count++;
8031da177e4SLinus Torvalds 	nd->depth++;
804def4af30SAl Viro 	err = __do_follow_link(path, nd, &cookie);
805def4af30SAl Viro 	if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link)
806def4af30SAl Viro 		path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie);
807258fa999SAl Viro 	path_put(path);
8081da177e4SLinus Torvalds 	current->link_count--;
8091da177e4SLinus Torvalds 	nd->depth--;
8101da177e4SLinus Torvalds 	return err;
8111da177e4SLinus Torvalds loop:
8121d957f9bSJan Blunck 	path_put_conditional(path, nd);
8131d957f9bSJan Blunck 	path_put(&nd->path);
8141da177e4SLinus Torvalds 	return err;
8151da177e4SLinus Torvalds }
8161da177e4SLinus Torvalds 
81731e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
81831e6b01fSNick Piggin {
81931e6b01fSNick Piggin 	struct vfsmount *parent;
82031e6b01fSNick Piggin 	struct dentry *mountpoint;
82131e6b01fSNick Piggin 
82231e6b01fSNick Piggin 	parent = path->mnt->mnt_parent;
82331e6b01fSNick Piggin 	if (parent == path->mnt)
82431e6b01fSNick Piggin 		return 0;
82531e6b01fSNick Piggin 	mountpoint = path->mnt->mnt_mountpoint;
82631e6b01fSNick Piggin 	path->dentry = mountpoint;
82731e6b01fSNick Piggin 	path->mnt = parent;
82831e6b01fSNick Piggin 	return 1;
82931e6b01fSNick Piggin }
83031e6b01fSNick Piggin 
831bab77ebfSAl Viro int follow_up(struct path *path)
8321da177e4SLinus Torvalds {
8331da177e4SLinus Torvalds 	struct vfsmount *parent;
8341da177e4SLinus Torvalds 	struct dentry *mountpoint;
83599b7db7bSNick Piggin 
83699b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
837bab77ebfSAl Viro 	parent = path->mnt->mnt_parent;
838bab77ebfSAl Viro 	if (parent == path->mnt) {
83999b7db7bSNick Piggin 		br_read_unlock(vfsmount_lock);
8401da177e4SLinus Torvalds 		return 0;
8411da177e4SLinus Torvalds 	}
8421da177e4SLinus Torvalds 	mntget(parent);
843bab77ebfSAl Viro 	mountpoint = dget(path->mnt->mnt_mountpoint);
84499b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
845bab77ebfSAl Viro 	dput(path->dentry);
846bab77ebfSAl Viro 	path->dentry = mountpoint;
847bab77ebfSAl Viro 	mntput(path->mnt);
848bab77ebfSAl Viro 	path->mnt = parent;
8491da177e4SLinus Torvalds 	return 1;
8501da177e4SLinus Torvalds }
8511da177e4SLinus Torvalds 
852b5c84bf6SNick Piggin /*
8539875cf80SDavid Howells  * Perform an automount
8549875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
8559875cf80SDavid Howells  *   were called with.
8561da177e4SLinus Torvalds  */
8579875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
8589875cf80SDavid Howells 			    bool *need_mntput)
85931e6b01fSNick Piggin {
8609875cf80SDavid Howells 	struct vfsmount *mnt;
861ea5b778aSDavid Howells 	int err;
8629875cf80SDavid Howells 
8639875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
8649875cf80SDavid Howells 		return -EREMOTE;
8659875cf80SDavid Howells 
8666f45b656SDavid Howells 	/* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
8676f45b656SDavid Howells 	 * and this is the terminal part of the path.
8686f45b656SDavid Howells 	 */
8696f45b656SDavid Howells 	if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
8706f45b656SDavid Howells 		return -EISDIR; /* we actually want to stop here */
8716f45b656SDavid Howells 
8729875cf80SDavid Howells 	/* We want to mount if someone is trying to open/create a file of any
8739875cf80SDavid Howells 	 * type under the mountpoint, wants to traverse through the mountpoint
8749875cf80SDavid Howells 	 * or wants to open the mounted directory.
8759875cf80SDavid Howells 	 *
8769875cf80SDavid Howells 	 * We don't want to mount if someone's just doing a stat and they've
8779875cf80SDavid Howells 	 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
8789875cf80SDavid Howells 	 * appended a '/' to the name.
8799875cf80SDavid Howells 	 */
8809875cf80SDavid Howells 	if (!(flags & LOOKUP_FOLLOW) &&
8819875cf80SDavid Howells 	    !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
8829875cf80SDavid Howells 		       LOOKUP_OPEN | LOOKUP_CREATE)))
8839875cf80SDavid Howells 		return -EISDIR;
8849875cf80SDavid Howells 
8859875cf80SDavid Howells 	current->total_link_count++;
8869875cf80SDavid Howells 	if (current->total_link_count >= 40)
8879875cf80SDavid Howells 		return -ELOOP;
8889875cf80SDavid Howells 
8899875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
8909875cf80SDavid Howells 	if (IS_ERR(mnt)) {
8919875cf80SDavid Howells 		/*
8929875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
8939875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
8949875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
8959875cf80SDavid Howells 		 *
8969875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
8979875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
8989875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
8999875cf80SDavid Howells 		 */
9009875cf80SDavid Howells 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
9019875cf80SDavid Howells 			return -EREMOTE;
9029875cf80SDavid Howells 		return PTR_ERR(mnt);
90331e6b01fSNick Piggin 	}
904ea5b778aSDavid Howells 
9059875cf80SDavid Howells 	if (!mnt) /* mount collision */
9069875cf80SDavid Howells 		return 0;
9079875cf80SDavid Howells 
90819a167afSAl Viro 	err = finish_automount(mnt, path);
909ea5b778aSDavid Howells 
910ea5b778aSDavid Howells 	switch (err) {
911ea5b778aSDavid Howells 	case -EBUSY:
912ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
91319a167afSAl Viro 		return 0;
914ea5b778aSDavid Howells 	case 0:
915463ffb2eSAl Viro 		dput(path->dentry);
9169875cf80SDavid Howells 		if (*need_mntput)
9179875cf80SDavid Howells 			mntput(path->mnt);
9189875cf80SDavid Howells 		path->mnt = mnt;
9199875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
9209875cf80SDavid Howells 		*need_mntput = true;
9219875cf80SDavid Howells 		return 0;
92219a167afSAl Viro 	default:
92319a167afSAl Viro 		return err;
9249875cf80SDavid Howells 	}
92519a167afSAl Viro 
926ea5b778aSDavid Howells }
9279875cf80SDavid Howells 
9289875cf80SDavid Howells /*
9299875cf80SDavid Howells  * Handle a dentry that is managed in some way.
930cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
9319875cf80SDavid Howells  * - Flagged as mountpoint
9329875cf80SDavid Howells  * - Flagged as automount point
9339875cf80SDavid Howells  *
9349875cf80SDavid Howells  * This may only be called in refwalk mode.
9359875cf80SDavid Howells  *
9369875cf80SDavid Howells  * Serialization is taken care of in namespace.c
9379875cf80SDavid Howells  */
9389875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
9399875cf80SDavid Howells {
9409875cf80SDavid Howells 	unsigned managed;
9419875cf80SDavid Howells 	bool need_mntput = false;
9429875cf80SDavid Howells 	int ret;
9439875cf80SDavid Howells 
9449875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
9459875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
9469875cf80SDavid Howells 	 * the components of that value change under us */
9479875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
9489875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
9499875cf80SDavid Howells 	       unlikely(managed != 0)) {
950cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
951cc53ce53SDavid Howells 		 * being held. */
952cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
953cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
954cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
955ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(path->dentry,
956ab90911fSDavid Howells 							   false, false);
957cc53ce53SDavid Howells 			if (ret < 0)
958cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
959cc53ce53SDavid Howells 		}
960cc53ce53SDavid Howells 
9619875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
9629875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
9639875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
9649875cf80SDavid Howells 			if (mounted) {
9659875cf80SDavid Howells 				dput(path->dentry);
9669875cf80SDavid Howells 				if (need_mntput)
967463ffb2eSAl Viro 					mntput(path->mnt);
968463ffb2eSAl Viro 				path->mnt = mounted;
969463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
9709875cf80SDavid Howells 				need_mntput = true;
9719875cf80SDavid Howells 				continue;
972463ffb2eSAl Viro 			}
973463ffb2eSAl Viro 
9749875cf80SDavid Howells 			/* Something is mounted on this dentry in another
9759875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
9769875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
9779875cf80SDavid Howells 			 * vfsmount_lock */
9781da177e4SLinus Torvalds 		}
9799875cf80SDavid Howells 
9809875cf80SDavid Howells 		/* Handle an automount point */
9819875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
9829875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
9839875cf80SDavid Howells 			if (ret < 0)
9849875cf80SDavid Howells 				return ret == -EISDIR ? 0 : ret;
9859875cf80SDavid Howells 			continue;
9869875cf80SDavid Howells 		}
9879875cf80SDavid Howells 
9889875cf80SDavid Howells 		/* We didn't change the current path point */
9899875cf80SDavid Howells 		break;
9909875cf80SDavid Howells 	}
9919875cf80SDavid Howells 	return 0;
9921da177e4SLinus Torvalds }
9931da177e4SLinus Torvalds 
994cc53ce53SDavid Howells int follow_down_one(struct path *path)
9951da177e4SLinus Torvalds {
9961da177e4SLinus Torvalds 	struct vfsmount *mounted;
9971da177e4SLinus Torvalds 
9981c755af4SAl Viro 	mounted = lookup_mnt(path);
9991da177e4SLinus Torvalds 	if (mounted) {
10009393bd07SAl Viro 		dput(path->dentry);
10019393bd07SAl Viro 		mntput(path->mnt);
10029393bd07SAl Viro 		path->mnt = mounted;
10039393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
10041da177e4SLinus Torvalds 		return 1;
10051da177e4SLinus Torvalds 	}
10061da177e4SLinus Torvalds 	return 0;
10071da177e4SLinus Torvalds }
10081da177e4SLinus Torvalds 
10099875cf80SDavid Howells /*
10109875cf80SDavid Howells  * Skip to top of mountpoint pile in rcuwalk mode.  We abort the rcu-walk if we
1011cc53ce53SDavid Howells  * meet a managed dentry and we're not walking to "..".  True is returned to
10129875cf80SDavid Howells  * continue, false to abort.
10139875cf80SDavid Howells  */
10149875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
10159875cf80SDavid Howells 			       struct inode **inode, bool reverse_transit)
10169875cf80SDavid Howells {
10179875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
10189875cf80SDavid Howells 		struct vfsmount *mounted;
1019ab90911fSDavid Howells 		if (unlikely(path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) &&
1020ab90911fSDavid Howells 		    !reverse_transit &&
1021ab90911fSDavid Howells 		    path->dentry->d_op->d_manage(path->dentry, false, true) < 0)
1022ab90911fSDavid Howells 			return false;
10239875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
10249875cf80SDavid Howells 		if (!mounted)
10259875cf80SDavid Howells 			break;
10269875cf80SDavid Howells 		path->mnt = mounted;
10279875cf80SDavid Howells 		path->dentry = mounted->mnt_root;
10289875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
10299875cf80SDavid Howells 		*inode = path->dentry->d_inode;
10309875cf80SDavid Howells 	}
10319875cf80SDavid Howells 
10329875cf80SDavid Howells 	if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
10339875cf80SDavid Howells 		return reverse_transit;
10349875cf80SDavid Howells 	return true;
10359875cf80SDavid Howells }
10369875cf80SDavid Howells 
103731e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
103831e6b01fSNick Piggin {
103931e6b01fSNick Piggin 	struct inode *inode = nd->inode;
104031e6b01fSNick Piggin 
104131e6b01fSNick Piggin 	set_root_rcu(nd);
104231e6b01fSNick Piggin 
104331e6b01fSNick Piggin 	while (1) {
104431e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
104531e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
104631e6b01fSNick Piggin 			break;
104731e6b01fSNick Piggin 		}
104831e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
104931e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
105031e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
105131e6b01fSNick Piggin 			unsigned seq;
105231e6b01fSNick Piggin 
105331e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
105431e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
105531e6b01fSNick Piggin 				return -ECHILD;
105631e6b01fSNick Piggin 			inode = parent->d_inode;
105731e6b01fSNick Piggin 			nd->path.dentry = parent;
105831e6b01fSNick Piggin 			nd->seq = seq;
105931e6b01fSNick Piggin 			break;
106031e6b01fSNick Piggin 		}
106131e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
106231e6b01fSNick Piggin 			break;
106331e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
106431e6b01fSNick Piggin 		inode = nd->path.dentry->d_inode;
106531e6b01fSNick Piggin 	}
10669875cf80SDavid Howells 	__follow_mount_rcu(nd, &nd->path, &inode, true);
106731e6b01fSNick Piggin 	nd->inode = inode;
106831e6b01fSNick Piggin 
106931e6b01fSNick Piggin 	return 0;
107031e6b01fSNick Piggin }
107131e6b01fSNick Piggin 
10729875cf80SDavid Howells /*
1073cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
1074cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
1075cc53ce53SDavid Howells  * caller is permitted to proceed or not.
1076cc53ce53SDavid Howells  *
1077cc53ce53SDavid Howells  * Care must be taken as namespace_sem may be held (indicated by mounting_here
1078cc53ce53SDavid Howells  * being true).
1079cc53ce53SDavid Howells  */
1080cc53ce53SDavid Howells int follow_down(struct path *path, bool mounting_here)
1081cc53ce53SDavid Howells {
1082cc53ce53SDavid Howells 	unsigned managed;
1083cc53ce53SDavid Howells 	int ret;
1084cc53ce53SDavid Howells 
1085cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
1086cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1087cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1088cc53ce53SDavid Howells 		 * being held.
1089cc53ce53SDavid Howells 		 *
1090cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
1091cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
1092cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
1093cc53ce53SDavid Howells 		 * superstructure.
1094cc53ce53SDavid Howells 		 *
1095cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
1096cc53ce53SDavid Howells 		 */
1097cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
1098cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1099cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
1100ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
1101ab90911fSDavid Howells 				path->dentry, mounting_here, false);
1102cc53ce53SDavid Howells 			if (ret < 0)
1103cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
1104cc53ce53SDavid Howells 		}
1105cc53ce53SDavid Howells 
1106cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
1107cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
1108cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
1109cc53ce53SDavid Howells 			if (!mounted)
1110cc53ce53SDavid Howells 				break;
1111cc53ce53SDavid Howells 			dput(path->dentry);
1112cc53ce53SDavid Howells 			mntput(path->mnt);
1113cc53ce53SDavid Howells 			path->mnt = mounted;
1114cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1115cc53ce53SDavid Howells 			continue;
1116cc53ce53SDavid Howells 		}
1117cc53ce53SDavid Howells 
1118cc53ce53SDavid Howells 		/* Don't handle automount points here */
1119cc53ce53SDavid Howells 		break;
1120cc53ce53SDavid Howells 	}
1121cc53ce53SDavid Howells 	return 0;
1122cc53ce53SDavid Howells }
1123cc53ce53SDavid Howells 
1124cc53ce53SDavid Howells /*
11259875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
11269875cf80SDavid Howells  */
11279875cf80SDavid Howells static void follow_mount(struct path *path)
11289875cf80SDavid Howells {
11299875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
11309875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
11319875cf80SDavid Howells 		if (!mounted)
11329875cf80SDavid Howells 			break;
11339875cf80SDavid Howells 		dput(path->dentry);
11349875cf80SDavid Howells 		mntput(path->mnt);
11359875cf80SDavid Howells 		path->mnt = mounted;
11369875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
11379875cf80SDavid Howells 	}
11389875cf80SDavid Howells }
11399875cf80SDavid Howells 
114031e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
11411da177e4SLinus Torvalds {
11422a737871SAl Viro 	set_root(nd);
1143e518ddb7SAndreas Mohr 
11441da177e4SLinus Torvalds 	while(1) {
11454ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
11461da177e4SLinus Torvalds 
11472a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
11482a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
11491da177e4SLinus Torvalds 			break;
11501da177e4SLinus Torvalds 		}
11514ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
11523088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
11533088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
11541da177e4SLinus Torvalds 			dput(old);
11551da177e4SLinus Torvalds 			break;
11561da177e4SLinus Torvalds 		}
11573088dd70SAl Viro 		if (!follow_up(&nd->path))
11581da177e4SLinus Torvalds 			break;
11591da177e4SLinus Torvalds 	}
116079ed0226SAl Viro 	follow_mount(&nd->path);
116131e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
11621da177e4SLinus Torvalds }
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds /*
1165baa03890SNick Piggin  * Allocate a dentry with name and parent, and perform a parent
1166baa03890SNick Piggin  * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1167baa03890SNick Piggin  * on error. parent->d_inode->i_mutex must be held. d_lookup must
1168baa03890SNick Piggin  * have verified that no child exists while under i_mutex.
1169baa03890SNick Piggin  */
1170baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1171baa03890SNick Piggin 				struct qstr *name, struct nameidata *nd)
1172baa03890SNick Piggin {
1173baa03890SNick Piggin 	struct inode *inode = parent->d_inode;
1174baa03890SNick Piggin 	struct dentry *dentry;
1175baa03890SNick Piggin 	struct dentry *old;
1176baa03890SNick Piggin 
1177baa03890SNick Piggin 	/* Don't create child dentry for a dead directory. */
1178baa03890SNick Piggin 	if (unlikely(IS_DEADDIR(inode)))
1179baa03890SNick Piggin 		return ERR_PTR(-ENOENT);
1180baa03890SNick Piggin 
1181baa03890SNick Piggin 	dentry = d_alloc(parent, name);
1182baa03890SNick Piggin 	if (unlikely(!dentry))
1183baa03890SNick Piggin 		return ERR_PTR(-ENOMEM);
1184baa03890SNick Piggin 
1185baa03890SNick Piggin 	old = inode->i_op->lookup(inode, dentry, nd);
1186baa03890SNick Piggin 	if (unlikely(old)) {
1187baa03890SNick Piggin 		dput(dentry);
1188baa03890SNick Piggin 		dentry = old;
1189baa03890SNick Piggin 	}
1190baa03890SNick Piggin 	return dentry;
1191baa03890SNick Piggin }
1192baa03890SNick Piggin 
1193baa03890SNick Piggin /*
11941da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
11951da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
11961da177e4SLinus Torvalds  *  It _is_ time-critical.
11971da177e4SLinus Torvalds  */
11981da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
119931e6b01fSNick Piggin 			struct path *path, struct inode **inode)
12001da177e4SLinus Torvalds {
12014ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
120231e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
12036e6b1bd1SAl Viro 	struct inode *dir;
12049875cf80SDavid Howells 	int err;
12059875cf80SDavid Howells 
12063cac260aSAl Viro 	/*
12073cac260aSAl Viro 	 * See if the low-level filesystem might want
12083cac260aSAl Viro 	 * to use its own hash..
12093cac260aSAl Viro 	 */
1210fb045adbSNick Piggin 	if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
12119875cf80SDavid Howells 		err = parent->d_op->d_hash(parent, nd->inode, name);
12123cac260aSAl Viro 		if (err < 0)
12133cac260aSAl Viro 			return err;
12143cac260aSAl Viro 	}
12151da177e4SLinus Torvalds 
1216b04f784eSNick Piggin 	/*
1217b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1218b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1219b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1220b04f784eSNick Piggin 	 */
122131e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
122231e6b01fSNick Piggin 		unsigned seq;
122331e6b01fSNick Piggin 
122431e6b01fSNick Piggin 		*inode = nd->inode;
122531e6b01fSNick Piggin 		dentry = __d_lookup_rcu(parent, name, &seq, inode);
122631e6b01fSNick Piggin 		if (!dentry) {
122731e6b01fSNick Piggin 			if (nameidata_drop_rcu(nd))
122831e6b01fSNick Piggin 				return -ECHILD;
122931e6b01fSNick Piggin 			goto need_lookup;
123031e6b01fSNick Piggin 		}
123131e6b01fSNick Piggin 		/* Memory barrier in read_seqcount_begin of child is enough */
123231e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
123331e6b01fSNick Piggin 			return -ECHILD;
123431e6b01fSNick Piggin 
123531e6b01fSNick Piggin 		nd->seq = seq;
123624643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1237f5e1c1c1SAl Viro 			dentry = do_revalidate_rcu(dentry, nd);
123824643087SAl Viro 			if (!dentry)
123924643087SAl Viro 				goto need_lookup;
124024643087SAl Viro 			if (IS_ERR(dentry))
124124643087SAl Viro 				goto fail;
124224643087SAl Viro 			if (!(nd->flags & LOOKUP_RCU))
124324643087SAl Viro 				goto done;
124424643087SAl Viro 		}
124531e6b01fSNick Piggin 		path->mnt = mnt;
124631e6b01fSNick Piggin 		path->dentry = dentry;
12479875cf80SDavid Howells 		if (likely(__follow_mount_rcu(nd, path, inode, false)))
12489875cf80SDavid Howells 			return 0;
12499875cf80SDavid Howells 		if (nameidata_drop_rcu(nd))
12509875cf80SDavid Howells 			return -ECHILD;
12519875cf80SDavid Howells 		/* fallthru */
12529875cf80SDavid Howells 	}
125331e6b01fSNick Piggin 	dentry = __d_lookup(parent, name);
12541da177e4SLinus Torvalds 	if (!dentry)
12551da177e4SLinus Torvalds 		goto need_lookup;
12562e2e88eaSNick Piggin found:
125724643087SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
125824643087SAl Viro 		dentry = do_revalidate(dentry, nd);
125924643087SAl Viro 		if (!dentry)
126024643087SAl Viro 			goto need_lookup;
126124643087SAl Viro 		if (IS_ERR(dentry))
126224643087SAl Viro 			goto fail;
126324643087SAl Viro 	}
12641da177e4SLinus Torvalds done:
12651da177e4SLinus Torvalds 	path->mnt = mnt;
12661da177e4SLinus Torvalds 	path->dentry = dentry;
12679875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
126889312214SIan Kent 	if (unlikely(err < 0)) {
126989312214SIan Kent 		path_put_conditional(path, nd);
12709875cf80SDavid Howells 		return err;
127189312214SIan Kent 	}
127231e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
12731da177e4SLinus Torvalds 	return 0;
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds need_lookup:
12766e6b1bd1SAl Viro 	dir = parent->d_inode;
127731e6b01fSNick Piggin 	BUG_ON(nd->inode != dir);
12786e6b1bd1SAl Viro 
12796e6b1bd1SAl Viro 	mutex_lock(&dir->i_mutex);
12806e6b1bd1SAl Viro 	/*
12816e6b1bd1SAl Viro 	 * First re-do the cached lookup just in case it was created
1282b04f784eSNick Piggin 	 * while we waited for the directory semaphore, or the first
1283b04f784eSNick Piggin 	 * lookup failed due to an unrelated rename.
12846e6b1bd1SAl Viro 	 *
1285b04f784eSNick Piggin 	 * This could use version numbering or similar to avoid unnecessary
1286b04f784eSNick Piggin 	 * cache lookups, but then we'd have to do the first lookup in the
1287b04f784eSNick Piggin 	 * non-racy way. However in the common case here, everything should
1288b04f784eSNick Piggin 	 * be hot in cache, so would it be a big win?
12896e6b1bd1SAl Viro 	 */
12906e6b1bd1SAl Viro 	dentry = d_lookup(parent, name);
1291baa03890SNick Piggin 	if (likely(!dentry)) {
1292baa03890SNick Piggin 		dentry = d_alloc_and_lookup(parent, name, nd);
12936e6b1bd1SAl Viro 		mutex_unlock(&dir->i_mutex);
12946e6b1bd1SAl Viro 		if (IS_ERR(dentry))
12956e6b1bd1SAl Viro 			goto fail;
12966e6b1bd1SAl Viro 		goto done;
12976e6b1bd1SAl Viro 	}
12986e6b1bd1SAl Viro 	/*
12996e6b1bd1SAl Viro 	 * Uhhuh! Nasty case: the cache was re-populated while
13006e6b1bd1SAl Viro 	 * we waited on the semaphore. Need to revalidate.
13016e6b1bd1SAl Viro 	 */
13026e6b1bd1SAl Viro 	mutex_unlock(&dir->i_mutex);
13032e2e88eaSNick Piggin 	goto found;
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds fail:
13061da177e4SLinus Torvalds 	return PTR_ERR(dentry);
13071da177e4SLinus Torvalds }
13081da177e4SLinus Torvalds 
130952094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
131052094c8aSAl Viro {
131152094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
131252094c8aSAl Viro 		int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
131352094c8aSAl Viro 		if (err != -ECHILD)
131452094c8aSAl Viro 			return err;
131552094c8aSAl Viro 		if (nameidata_drop_rcu(nd))
131652094c8aSAl Viro 			return -ECHILD;
131752094c8aSAl Viro 	}
131852094c8aSAl Viro 	return exec_permission(nd->inode, 0);
131952094c8aSAl Viro }
132052094c8aSAl Viro 
13219856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
13229856fa1bSAl Viro {
13239856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
13249856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
13259856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
13269856fa1bSAl Viro 				return -ECHILD;
13279856fa1bSAl Viro 		} else
13289856fa1bSAl Viro 			follow_dotdot(nd);
13299856fa1bSAl Viro 	}
13309856fa1bSAl Viro 	return 0;
13319856fa1bSAl Viro }
13329856fa1bSAl Viro 
13331da177e4SLinus Torvalds /*
13341da177e4SLinus Torvalds  * Name resolution.
1335ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1336ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
13371da177e4SLinus Torvalds  *
1338ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1339ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
13401da177e4SLinus Torvalds  */
13416de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
13421da177e4SLinus Torvalds {
13431da177e4SLinus Torvalds 	struct path next;
13441da177e4SLinus Torvalds 	int err;
13451da177e4SLinus Torvalds 	unsigned int lookup_flags = nd->flags;
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds 	while (*name=='/')
13481da177e4SLinus Torvalds 		name++;
13491da177e4SLinus Torvalds 	if (!*name)
1350086e183aSAl Viro 		return 0;
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds 	if (nd->depth)
1353f55eab82STrond Myklebust 		lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
13561da177e4SLinus Torvalds 	for(;;) {
135731e6b01fSNick Piggin 		struct inode *inode;
13581da177e4SLinus Torvalds 		unsigned long hash;
13591da177e4SLinus Torvalds 		struct qstr this;
13601da177e4SLinus Torvalds 		unsigned int c;
1361fe479a58SAl Viro 		int type;
13621da177e4SLinus Torvalds 
1363cdce5d6bSTrond Myklebust 		nd->flags |= LOOKUP_CONTINUE;
136452094c8aSAl Viro 
136552094c8aSAl Viro 		err = may_lookup(nd);
13661da177e4SLinus Torvalds  		if (err)
13671da177e4SLinus Torvalds 			break;
13681da177e4SLinus Torvalds 
13691da177e4SLinus Torvalds 		this.name = name;
13701da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
13711da177e4SLinus Torvalds 
13721da177e4SLinus Torvalds 		hash = init_name_hash();
13731da177e4SLinus Torvalds 		do {
13741da177e4SLinus Torvalds 			name++;
13751da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
13761da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
13771da177e4SLinus Torvalds 		} while (c && (c != '/'));
13781da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
13791da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
13801da177e4SLinus Torvalds 
1381fe479a58SAl Viro 		type = LAST_NORM;
1382fe479a58SAl Viro 		if (this.name[0] == '.') switch (this.len) {
1383fe479a58SAl Viro 			case 2:
138416c2cd71SAl Viro 				if (this.name[1] == '.') {
1385fe479a58SAl Viro 					type = LAST_DOTDOT;
138616c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
138716c2cd71SAl Viro 				}
1388fe479a58SAl Viro 				break;
1389fe479a58SAl Viro 			case 1:
1390fe479a58SAl Viro 				type = LAST_DOT;
1391fe479a58SAl Viro 		}
139216c2cd71SAl Viro 		if (likely(type == LAST_NORM))
139316c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
1394fe479a58SAl Viro 
13951da177e4SLinus Torvalds 		/* remove trailing slashes? */
13961da177e4SLinus Torvalds 		if (!c)
13971da177e4SLinus Torvalds 			goto last_component;
13981da177e4SLinus Torvalds 		while (*++name == '/');
13991da177e4SLinus Torvalds 		if (!*name)
14001da177e4SLinus Torvalds 			goto last_with_slashes;
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds 		/*
14031da177e4SLinus Torvalds 		 * "." and ".." are special - ".." especially so because it has
14041da177e4SLinus Torvalds 		 * to be able to know about the current root directory and
14051da177e4SLinus Torvalds 		 * parent relationships.
14061da177e4SLinus Torvalds 		 */
1407fe479a58SAl Viro 		if (unlikely(type != LAST_NORM)) {
14084455ca62SAl Viro 			err = handle_dots(nd, type);
14094455ca62SAl Viro 			if (err)
14104455ca62SAl Viro 				goto return_err;
14111da177e4SLinus Torvalds 			continue;
14121da177e4SLinus Torvalds 		}
1413fe479a58SAl Viro 
14141da177e4SLinus Torvalds 		/* This does the actual lookups.. */
141531e6b01fSNick Piggin 		err = do_lookup(nd, &this, &next, &inode);
14161da177e4SLinus Torvalds 		if (err)
14171da177e4SLinus Torvalds 			break;
14181da177e4SLinus Torvalds 
14197bc055d1SAl Viro 		if (inode && inode->i_op->follow_link) {
14203abb17e8SLinus Torvalds 			err = do_follow_link(inode, &next, nd);
14211da177e4SLinus Torvalds 			if (err)
14221da177e4SLinus Torvalds 				goto return_err;
142331e6b01fSNick Piggin 			nd->inode = nd->path.dentry->d_inode;
142431e6b01fSNick Piggin 		} else {
142509dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
142631e6b01fSNick Piggin 			nd->inode = inode;
142731e6b01fSNick Piggin 		}
14287bc055d1SAl Viro 		err = -ENOENT;
14297bc055d1SAl Viro 		if (!nd->inode)
14307bc055d1SAl Viro 			break;
14311da177e4SLinus Torvalds 		err = -ENOTDIR;
143231e6b01fSNick Piggin 		if (!nd->inode->i_op->lookup)
14331da177e4SLinus Torvalds 			break;
14341da177e4SLinus Torvalds 		continue;
14351da177e4SLinus Torvalds 		/* here ends the main loop */
14361da177e4SLinus Torvalds 
14371da177e4SLinus Torvalds last_with_slashes:
14381da177e4SLinus Torvalds 		lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
14391da177e4SLinus Torvalds last_component:
1440f55eab82STrond Myklebust 		/* Clear LOOKUP_CONTINUE iff it was previously unset */
1441f55eab82STrond Myklebust 		nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
14421da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_PARENT)
14431da177e4SLinus Torvalds 			goto lookup_parent;
1444fe479a58SAl Viro 		if (unlikely(type != LAST_NORM)) {
14454455ca62SAl Viro 			err = handle_dots(nd, type);
14464455ca62SAl Viro 			if (err)
14474455ca62SAl Viro 				goto return_err;
1448086e183aSAl Viro 			return 0;
14491da177e4SLinus Torvalds 		}
145031e6b01fSNick Piggin 		err = do_lookup(nd, &this, &next, &inode);
14511da177e4SLinus Torvalds 		if (err)
14521da177e4SLinus Torvalds 			break;
1453db372915SDavid Howells 		if (inode && unlikely(inode->i_op->follow_link) &&
1454db372915SDavid Howells 		    (lookup_flags & LOOKUP_FOLLOW)) {
14553abb17e8SLinus Torvalds 			err = do_follow_link(inode, &next, nd);
14561da177e4SLinus Torvalds 			if (err)
14571da177e4SLinus Torvalds 				goto return_err;
145831e6b01fSNick Piggin 			nd->inode = nd->path.dentry->d_inode;
145931e6b01fSNick Piggin 		} else {
146009dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
146131e6b01fSNick Piggin 			nd->inode = inode;
146231e6b01fSNick Piggin 		}
14631da177e4SLinus Torvalds 		err = -ENOENT;
146431e6b01fSNick Piggin 		if (!nd->inode)
14651da177e4SLinus Torvalds 			break;
14661da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_DIRECTORY) {
14671da177e4SLinus Torvalds 			err = -ENOTDIR;
146831e6b01fSNick Piggin 			if (!nd->inode->i_op->lookup)
14691da177e4SLinus Torvalds 				break;
14701da177e4SLinus Torvalds 		}
1471086e183aSAl Viro 		return 0;
14721da177e4SLinus Torvalds lookup_parent:
14731da177e4SLinus Torvalds 		nd->last = this;
1474fe479a58SAl Viro 		nd->last_type = type;
14751da177e4SLinus Torvalds 		return 0;
14761da177e4SLinus Torvalds 	}
147731e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU))
14781d957f9bSJan Blunck 		path_put(&nd->path);
14791da177e4SLinus Torvalds return_err:
14804455ca62SAl Viro 	if (nd->flags & LOOKUP_RCU) {
14814455ca62SAl Viro 		nd->flags &= ~LOOKUP_RCU;
14824455ca62SAl Viro 		nd->root.mnt = NULL;
14834455ca62SAl Viro 		rcu_read_unlock();
14844455ca62SAl Viro 		br_read_unlock(vfsmount_lock);
14854455ca62SAl Viro 	}
14861da177e4SLinus Torvalds 	return err;
14871da177e4SLinus Torvalds }
14881da177e4SLinus Torvalds 
1489e41f7d4eSAl Viro static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
149031e6b01fSNick Piggin {
149131e6b01fSNick Piggin 	int retval = 0;
149231e6b01fSNick Piggin 	int fput_needed;
149331e6b01fSNick Piggin 	struct file *file;
149431e6b01fSNick Piggin 
149531e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
149616c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
149731e6b01fSNick Piggin 	nd->depth = 0;
149831e6b01fSNick Piggin 	nd->root.mnt = NULL;
149931e6b01fSNick Piggin 	nd->file = NULL;
150031e6b01fSNick Piggin 
150131e6b01fSNick Piggin 	if (*name=='/') {
1502e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
150331e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
150431e6b01fSNick Piggin 			rcu_read_lock();
1505e41f7d4eSAl Viro 			set_root_rcu(nd);
1506e41f7d4eSAl Viro 		} else {
1507e41f7d4eSAl Viro 			set_root(nd);
1508e41f7d4eSAl Viro 			path_get(&nd->root);
1509e41f7d4eSAl Viro 		}
151031e6b01fSNick Piggin 		nd->path = nd->root;
151131e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1512e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
151331e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1514c28cc364SNick Piggin 			unsigned seq;
151531e6b01fSNick Piggin 
151631e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
151731e6b01fSNick Piggin 			rcu_read_lock();
151831e6b01fSNick Piggin 
1519c28cc364SNick Piggin 			do {
1520c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
152131e6b01fSNick Piggin 				nd->path = fs->pwd;
1522c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1523c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1524e41f7d4eSAl Viro 		} else {
1525e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1526e41f7d4eSAl Viro 		}
152731e6b01fSNick Piggin 	} else {
152831e6b01fSNick Piggin 		struct dentry *dentry;
152931e6b01fSNick Piggin 
153031e6b01fSNick Piggin 		file = fget_light(dfd, &fput_needed);
153131e6b01fSNick Piggin 		retval = -EBADF;
153231e6b01fSNick Piggin 		if (!file)
153331e6b01fSNick Piggin 			goto out_fail;
153431e6b01fSNick Piggin 
153531e6b01fSNick Piggin 		dentry = file->f_path.dentry;
153631e6b01fSNick Piggin 
153731e6b01fSNick Piggin 		retval = -ENOTDIR;
153831e6b01fSNick Piggin 		if (!S_ISDIR(dentry->d_inode->i_mode))
153931e6b01fSNick Piggin 			goto fput_fail;
154031e6b01fSNick Piggin 
154131e6b01fSNick Piggin 		retval = file_permission(file, MAY_EXEC);
154231e6b01fSNick Piggin 		if (retval)
154331e6b01fSNick Piggin 			goto fput_fail;
154431e6b01fSNick Piggin 
154531e6b01fSNick Piggin 		nd->path = file->f_path;
1546e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
154731e6b01fSNick Piggin 			if (fput_needed)
154831e6b01fSNick Piggin 				nd->file = file;
1549c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
155031e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
155131e6b01fSNick Piggin 			rcu_read_lock();
15525590ff0dSUlrich Drepper 		} else {
15535dd784d0SJan Blunck 			path_get(&file->f_path);
15545590ff0dSUlrich Drepper 			fput_light(file, fput_needed);
15551da177e4SLinus Torvalds 		}
1556e41f7d4eSAl Viro 	}
1557e41f7d4eSAl Viro 
155831e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
15599b4a9b14SAl Viro 	return 0;
15602dfdd266SJosef 'Jeff' Sipek 
15619b4a9b14SAl Viro fput_fail:
15629b4a9b14SAl Viro 	fput_light(file, fput_needed);
15639b4a9b14SAl Viro out_fail:
15649b4a9b14SAl Viro 	return retval;
15659b4a9b14SAl Viro }
15669b4a9b14SAl Viro 
15679b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1568ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
15699b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
15709b4a9b14SAl Viro {
157131e6b01fSNick Piggin 	int retval;
157231e6b01fSNick Piggin 
157331e6b01fSNick Piggin 	/*
157431e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
157531e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
157631e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
157731e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
157831e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
157931e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
158031e6b01fSNick Piggin 	 * analogue, foo_rcu().
158131e6b01fSNick Piggin 	 *
158231e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
158331e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
158431e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
158531e6b01fSNick Piggin 	 * be able to complete).
158631e6b01fSNick Piggin 	 */
1587ee0827cdSAl Viro 	retval = path_init(dfd, name, flags, nd);
1588ee0827cdSAl Viro 
158931e6b01fSNick Piggin 	if (unlikely(retval))
159031e6b01fSNick Piggin 		return retval;
1591ee0827cdSAl Viro 
1592ee0827cdSAl Viro 	current->total_link_count = 0;
1593ee0827cdSAl Viro 	retval = link_path_walk(name, nd);
1594ee0827cdSAl Viro 
1595ee0827cdSAl Viro 	if (nd->flags & LOOKUP_RCU) {
15964455ca62SAl Viro 		/* went all way through without dropping RCU */
15974455ca62SAl Viro 		BUG_ON(retval);
1598086e183aSAl Viro 		if (nameidata_drop_rcu_last(nd))
1599086e183aSAl Viro 			retval = -ECHILD;
1600086e183aSAl Viro 	}
160131e6b01fSNick Piggin 
160216c2cd71SAl Viro 	if (!retval)
160316c2cd71SAl Viro 		retval = handle_reval_path(nd);
160416c2cd71SAl Viro 
1605ee0827cdSAl Viro 	if (nd->file) {
1606ee0827cdSAl Viro 		fput(nd->file);
1607ee0827cdSAl Viro 		nd->file = NULL;
1608ee0827cdSAl Viro 	}
1609ee0827cdSAl Viro 
161031e6b01fSNick Piggin 	if (nd->root.mnt) {
161131e6b01fSNick Piggin 		path_put(&nd->root);
161231e6b01fSNick Piggin 		nd->root.mnt = NULL;
161331e6b01fSNick Piggin 	}
1614ee0827cdSAl Viro 	return retval;
161531e6b01fSNick Piggin }
161631e6b01fSNick Piggin 
1617ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1618ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1619ee0827cdSAl Viro {
1620ee0827cdSAl Viro 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1621ee0827cdSAl Viro 	if (unlikely(retval == -ECHILD))
1622ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags, nd);
1623ee0827cdSAl Viro 	if (unlikely(retval == -ESTALE))
1624ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1625ee0827cdSAl Viro 
162631e6b01fSNick Piggin 	if (likely(!retval)) {
162731e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
162831e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
162931e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
163031e6b01fSNick Piggin 		}
163131e6b01fSNick Piggin 	}
1632170aa3d0SUlrich Drepper 	return retval;
16331da177e4SLinus Torvalds }
16341da177e4SLinus Torvalds 
1635c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd)
16365590ff0dSUlrich Drepper {
1637c9c6cac0SAl Viro 	return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
16385590ff0dSUlrich Drepper }
16395590ff0dSUlrich Drepper 
1640d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1641d1811465SAl Viro {
1642d1811465SAl Viro 	struct nameidata nd;
1643d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1644d1811465SAl Viro 	if (!res)
1645d1811465SAl Viro 		*path = nd.path;
1646d1811465SAl Viro 	return res;
1647d1811465SAl Viro }
1648d1811465SAl Viro 
164916f18200SJosef 'Jeff' Sipek /**
165016f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
165116f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
165216f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
165316f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
165416f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
165516f18200SJosef 'Jeff' Sipek  * @nd: pointer to nameidata
165616f18200SJosef 'Jeff' Sipek  */
165716f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
165816f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
165916f18200SJosef 'Jeff' Sipek 		    struct nameidata *nd)
166016f18200SJosef 'Jeff' Sipek {
1661ee0827cdSAl Viro 	int result;
166216f18200SJosef 'Jeff' Sipek 
166316f18200SJosef 'Jeff' Sipek 	/* same as do_path_lookup */
166416f18200SJosef 'Jeff' Sipek 	nd->last_type = LAST_ROOT;
166516c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
166616f18200SJosef 'Jeff' Sipek 	nd->depth = 0;
166716f18200SJosef 'Jeff' Sipek 
1668c8e7f449SJan Blunck 	nd->path.dentry = dentry;
1669c8e7f449SJan Blunck 	nd->path.mnt = mnt;
1670c8e7f449SJan Blunck 	path_get(&nd->path);
16715b857119SAl Viro 	nd->root = nd->path;
16725b857119SAl Viro 	path_get(&nd->root);
167331e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
167416f18200SJosef 'Jeff' Sipek 
1675ee0827cdSAl Viro 	current->total_link_count = 0;
1676ee0827cdSAl Viro 
1677ee0827cdSAl Viro 	result = link_path_walk(name, nd);
167816c2cd71SAl Viro 	if (!result)
167916c2cd71SAl Viro 		result = handle_reval_path(nd);
1680ee0827cdSAl Viro 	if (result == -ESTALE) {
1681ee0827cdSAl Viro 		/* nd->path had been dropped */
1682ee0827cdSAl Viro 		current->total_link_count = 0;
1683ee0827cdSAl Viro 		nd->path.dentry = dentry;
1684ee0827cdSAl Viro 		nd->path.mnt = mnt;
1685ee0827cdSAl Viro 		nd->inode = dentry->d_inode;
1686ee0827cdSAl Viro 		path_get(&nd->path);
168716c2cd71SAl Viro 		nd->flags = flags | LOOKUP_JUMPED | LOOKUP_REVAL;
168816c2cd71SAl Viro 
1689ee0827cdSAl Viro 		result = link_path_walk(name, nd);
169016c2cd71SAl Viro 		if (!result)
169116c2cd71SAl Viro 			result = handle_reval_path(nd);
1692ee0827cdSAl Viro 	}
1693ee0827cdSAl Viro 	if (unlikely(!result && !audit_dummy_context() && nd->path.dentry &&
169431e6b01fSNick Piggin 				nd->inode))
16954ac91378SJan Blunck 		audit_inode(name, nd->path.dentry);
169616f18200SJosef 'Jeff' Sipek 
16972a737871SAl Viro 	path_put(&nd->root);
16982a737871SAl Viro 	nd->root.mnt = NULL;
169916f18200SJosef 'Jeff' Sipek 
1700ee0827cdSAl Viro 	return result;
170116f18200SJosef 'Jeff' Sipek }
170216f18200SJosef 'Jeff' Sipek 
1703eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1704eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
17051da177e4SLinus Torvalds {
170681fca444SChristoph Hellwig 	struct inode *inode = base->d_inode;
17071da177e4SLinus Torvalds 	struct dentry *dentry;
17081da177e4SLinus Torvalds 	int err;
17091da177e4SLinus Torvalds 
1710b74c79e9SNick Piggin 	err = exec_permission(inode, 0);
171181fca444SChristoph Hellwig 	if (err)
171281fca444SChristoph Hellwig 		return ERR_PTR(err);
17131da177e4SLinus Torvalds 
17141da177e4SLinus Torvalds 	/*
17151da177e4SLinus Torvalds 	 * See if the low-level filesystem might want
17161da177e4SLinus Torvalds 	 * to use its own hash..
17171da177e4SLinus Torvalds 	 */
1718fb045adbSNick Piggin 	if (base->d_flags & DCACHE_OP_HASH) {
1719b1e6a015SNick Piggin 		err = base->d_op->d_hash(base, inode, name);
17201da177e4SLinus Torvalds 		dentry = ERR_PTR(err);
17211da177e4SLinus Torvalds 		if (err < 0)
17221da177e4SLinus Torvalds 			goto out;
17231da177e4SLinus Torvalds 	}
17241da177e4SLinus Torvalds 
1725b04f784eSNick Piggin 	/*
1726b04f784eSNick Piggin 	 * Don't bother with __d_lookup: callers are for creat as
1727b04f784eSNick Piggin 	 * well as unlink, so a lot of the time it would cost
1728b04f784eSNick Piggin 	 * a double lookup.
17296e6b1bd1SAl Viro 	 */
17306e6b1bd1SAl Viro 	dentry = d_lookup(base, name);
17316e6b1bd1SAl Viro 
1732fb045adbSNick Piggin 	if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
17336e6b1bd1SAl Viro 		dentry = do_revalidate(dentry, nd);
17346e6b1bd1SAl Viro 
17351da177e4SLinus Torvalds 	if (!dentry)
1736baa03890SNick Piggin 		dentry = d_alloc_and_lookup(base, name, nd);
17371da177e4SLinus Torvalds out:
17381da177e4SLinus Torvalds 	return dentry;
17391da177e4SLinus Torvalds }
17401da177e4SLinus Torvalds 
1741057f6c01SJames Morris /*
1742057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1743057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1744057f6c01SJames Morris  * SMP-safe.
1745057f6c01SJames Morris  */
1746a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
17471da177e4SLinus Torvalds {
17484ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
17491da177e4SLinus Torvalds }
17501da177e4SLinus Torvalds 
1751eead1911SChristoph Hellwig static int __lookup_one_len(const char *name, struct qstr *this,
1752eead1911SChristoph Hellwig 		struct dentry *base, int len)
17531da177e4SLinus Torvalds {
17541da177e4SLinus Torvalds 	unsigned long hash;
17551da177e4SLinus Torvalds 	unsigned int c;
17561da177e4SLinus Torvalds 
1757057f6c01SJames Morris 	this->name = name;
1758057f6c01SJames Morris 	this->len = len;
17591da177e4SLinus Torvalds 	if (!len)
1760057f6c01SJames Morris 		return -EACCES;
17611da177e4SLinus Torvalds 
17621da177e4SLinus Torvalds 	hash = init_name_hash();
17631da177e4SLinus Torvalds 	while (len--) {
17641da177e4SLinus Torvalds 		c = *(const unsigned char *)name++;
17651da177e4SLinus Torvalds 		if (c == '/' || c == '\0')
1766057f6c01SJames Morris 			return -EACCES;
17671da177e4SLinus Torvalds 		hash = partial_name_hash(c, hash);
17681da177e4SLinus Torvalds 	}
1769057f6c01SJames Morris 	this->hash = end_name_hash(hash);
1770057f6c01SJames Morris 	return 0;
1771057f6c01SJames Morris }
17721da177e4SLinus Torvalds 
1773eead1911SChristoph Hellwig /**
1774a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1775eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1776eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1777eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1778eead1911SChristoph Hellwig  *
1779a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1780a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1781eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1782eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1783eead1911SChristoph Hellwig  */
1784057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1785057f6c01SJames Morris {
1786057f6c01SJames Morris 	int err;
1787057f6c01SJames Morris 	struct qstr this;
1788057f6c01SJames Morris 
17892f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
17902f9092e1SDavid Woodhouse 
1791057f6c01SJames Morris 	err = __lookup_one_len(name, &this, base, len);
1792057f6c01SJames Morris 	if (err)
1793057f6c01SJames Morris 		return ERR_PTR(err);
1794eead1911SChristoph Hellwig 
179549705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1796057f6c01SJames Morris }
1797057f6c01SJames Morris 
17982d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags,
17992d8f3038SAl Viro 		 struct path *path)
18001da177e4SLinus Torvalds {
18012d8f3038SAl Viro 	struct nameidata nd;
18021da177e4SLinus Torvalds 	char *tmp = getname(name);
18031da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
18041da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
18052d8f3038SAl Viro 
18062d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
18072d8f3038SAl Viro 
18082d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
18091da177e4SLinus Torvalds 		putname(tmp);
18102d8f3038SAl Viro 		if (!err)
18112d8f3038SAl Viro 			*path = nd.path;
18121da177e4SLinus Torvalds 	}
18131da177e4SLinus Torvalds 	return err;
18141da177e4SLinus Torvalds }
18151da177e4SLinus Torvalds 
18162ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
18172ad94ae6SAl Viro 			struct nameidata *nd, char **name)
18182ad94ae6SAl Viro {
18192ad94ae6SAl Viro 	char *s = getname(path);
18202ad94ae6SAl Viro 	int error;
18212ad94ae6SAl Viro 
18222ad94ae6SAl Viro 	if (IS_ERR(s))
18232ad94ae6SAl Viro 		return PTR_ERR(s);
18242ad94ae6SAl Viro 
18252ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
18262ad94ae6SAl Viro 	if (error)
18272ad94ae6SAl Viro 		putname(s);
18282ad94ae6SAl Viro 	else
18292ad94ae6SAl Viro 		*name = s;
18302ad94ae6SAl Viro 
18312ad94ae6SAl Viro 	return error;
18322ad94ae6SAl Viro }
18332ad94ae6SAl Viro 
18341da177e4SLinus Torvalds /*
18351da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
18361da177e4SLinus Torvalds  * minimal.
18371da177e4SLinus Torvalds  */
18381da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
18391da177e4SLinus Torvalds {
1840da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1841da9592edSDavid Howells 
18421da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
18431da177e4SLinus Torvalds 		return 0;
1844da9592edSDavid Howells 	if (inode->i_uid == fsuid)
18451da177e4SLinus Torvalds 		return 0;
1846da9592edSDavid Howells 	if (dir->i_uid == fsuid)
18471da177e4SLinus Torvalds 		return 0;
18481da177e4SLinus Torvalds 	return !capable(CAP_FOWNER);
18491da177e4SLinus Torvalds }
18501da177e4SLinus Torvalds 
18511da177e4SLinus Torvalds /*
18521da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
18531da177e4SLinus Torvalds  *  whether the type of victim is right.
18541da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
18551da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
18561da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
18571da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
18581da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
18591da177e4SLinus Torvalds  *	a. be owner of dir, or
18601da177e4SLinus Torvalds  *	b. be owner of victim, or
18611da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
18621da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
18631da177e4SLinus Torvalds  *     links pointing to it.
18641da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
18651da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
18661da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
18671da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
18681da177e4SLinus Torvalds  *     nfs_async_unlink().
18691da177e4SLinus Torvalds  */
1870858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
18711da177e4SLinus Torvalds {
18721da177e4SLinus Torvalds 	int error;
18731da177e4SLinus Torvalds 
18741da177e4SLinus Torvalds 	if (!victim->d_inode)
18751da177e4SLinus Torvalds 		return -ENOENT;
18761da177e4SLinus Torvalds 
18771da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
1878cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
18791da177e4SLinus Torvalds 
1880f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
18811da177e4SLinus Torvalds 	if (error)
18821da177e4SLinus Torvalds 		return error;
18831da177e4SLinus Torvalds 	if (IS_APPEND(dir))
18841da177e4SLinus Torvalds 		return -EPERM;
18851da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1886f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
18871da177e4SLinus Torvalds 		return -EPERM;
18881da177e4SLinus Torvalds 	if (isdir) {
18891da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
18901da177e4SLinus Torvalds 			return -ENOTDIR;
18911da177e4SLinus Torvalds 		if (IS_ROOT(victim))
18921da177e4SLinus Torvalds 			return -EBUSY;
18931da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
18941da177e4SLinus Torvalds 		return -EISDIR;
18951da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18961da177e4SLinus Torvalds 		return -ENOENT;
18971da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
18981da177e4SLinus Torvalds 		return -EBUSY;
18991da177e4SLinus Torvalds 	return 0;
19001da177e4SLinus Torvalds }
19011da177e4SLinus Torvalds 
19021da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
19031da177e4SLinus Torvalds  *  dir.
19041da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
19051da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
19061da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
19071da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
19081da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
19091da177e4SLinus Torvalds  */
1910a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
19111da177e4SLinus Torvalds {
19121da177e4SLinus Torvalds 	if (child->d_inode)
19131da177e4SLinus Torvalds 		return -EEXIST;
19141da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
19151da177e4SLinus Torvalds 		return -ENOENT;
1916f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
19171da177e4SLinus Torvalds }
19181da177e4SLinus Torvalds 
19191da177e4SLinus Torvalds /*
19201da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
19211da177e4SLinus Torvalds  */
19221da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
19231da177e4SLinus Torvalds {
19241da177e4SLinus Torvalds 	struct dentry *p;
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 	if (p1 == p2) {
1927f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
19281da177e4SLinus Torvalds 		return NULL;
19291da177e4SLinus Torvalds 	}
19301da177e4SLinus Torvalds 
1931a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
19321da177e4SLinus Torvalds 
1933e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1934e2761a11SOGAWA Hirofumi 	if (p) {
1935f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1936f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
19371da177e4SLinus Torvalds 		return p;
19381da177e4SLinus Torvalds 	}
19391da177e4SLinus Torvalds 
1940e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1941e2761a11SOGAWA Hirofumi 	if (p) {
1942f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1943f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
19441da177e4SLinus Torvalds 		return p;
19451da177e4SLinus Torvalds 	}
19461da177e4SLinus Torvalds 
1947f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1948f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
19491da177e4SLinus Torvalds 	return NULL;
19501da177e4SLinus Torvalds }
19511da177e4SLinus Torvalds 
19521da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
19531da177e4SLinus Torvalds {
19541b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
19551da177e4SLinus Torvalds 	if (p1 != p2) {
19561b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1957a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
19581da177e4SLinus Torvalds 	}
19591da177e4SLinus Torvalds }
19601da177e4SLinus Torvalds 
19611da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
19621da177e4SLinus Torvalds 		struct nameidata *nd)
19631da177e4SLinus Torvalds {
1964a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
19651da177e4SLinus Torvalds 
19661da177e4SLinus Torvalds 	if (error)
19671da177e4SLinus Torvalds 		return error;
19681da177e4SLinus Torvalds 
1969acfa4380SAl Viro 	if (!dir->i_op->create)
19701da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
19711da177e4SLinus Torvalds 	mode &= S_IALLUGO;
19721da177e4SLinus Torvalds 	mode |= S_IFREG;
19731da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
19741da177e4SLinus Torvalds 	if (error)
19751da177e4SLinus Torvalds 		return error;
19761da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
1977a74574aaSStephen Smalley 	if (!error)
1978f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
19791da177e4SLinus Torvalds 	return error;
19801da177e4SLinus Torvalds }
19811da177e4SLinus Torvalds 
19823fb64190SChristoph Hellwig int may_open(struct path *path, int acc_mode, int flag)
19831da177e4SLinus Torvalds {
19843fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
19851da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
19861da177e4SLinus Torvalds 	int error;
19871da177e4SLinus Torvalds 
19881da177e4SLinus Torvalds 	if (!inode)
19891da177e4SLinus Torvalds 		return -ENOENT;
19901da177e4SLinus Torvalds 
1991c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
1992c8fe8f30SChristoph Hellwig 	case S_IFLNK:
19931da177e4SLinus Torvalds 		return -ELOOP;
1994c8fe8f30SChristoph Hellwig 	case S_IFDIR:
1995c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
19961da177e4SLinus Torvalds 			return -EISDIR;
1997c8fe8f30SChristoph Hellwig 		break;
1998c8fe8f30SChristoph Hellwig 	case S_IFBLK:
1999c8fe8f30SChristoph Hellwig 	case S_IFCHR:
20003fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
20011da177e4SLinus Torvalds 			return -EACCES;
2002c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
2003c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2004c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
20051da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2006c8fe8f30SChristoph Hellwig 		break;
20074a3fd211SDave Hansen 	}
2008b41572e9SDave Hansen 
20093fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
2010b41572e9SDave Hansen 	if (error)
2011b41572e9SDave Hansen 		return error;
20126146f0d5SMimi Zohar 
20131da177e4SLinus Torvalds 	/*
20141da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
20151da177e4SLinus Torvalds 	 */
20161da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
20178737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
20187715b521SAl Viro 			return -EPERM;
20191da177e4SLinus Torvalds 		if (flag & O_TRUNC)
20207715b521SAl Viro 			return -EPERM;
20211da177e4SLinus Torvalds 	}
20221da177e4SLinus Torvalds 
20231da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
20247715b521SAl Viro 	if (flag & O_NOATIME && !is_owner_or_cap(inode))
20257715b521SAl Viro 		return -EPERM;
20261da177e4SLinus Torvalds 
20271da177e4SLinus Torvalds 	/*
20281da177e4SLinus Torvalds 	 * Ensure there are no outstanding leases on the file.
20291da177e4SLinus Torvalds 	 */
2030b65a9cfcSAl Viro 	return break_lease(inode, flag);
20317715b521SAl Viro }
20327715b521SAl Viro 
2033e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
20347715b521SAl Viro {
2035e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
20367715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
20377715b521SAl Viro 	int error = get_write_access(inode);
20381da177e4SLinus Torvalds 	if (error)
20397715b521SAl Viro 		return error;
20401da177e4SLinus Torvalds 	/*
20411da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
20421da177e4SLinus Torvalds 	 */
20431da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2044be6d3e56SKentaro Takeda 	if (!error)
2045ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
20461da177e4SLinus Torvalds 	if (!error) {
20477715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2048d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2049e1181ee6SJeff Layton 				    filp);
20501da177e4SLinus Torvalds 	}
20511da177e4SLinus Torvalds 	put_write_access(inode);
2052acd0c935SMimi Zohar 	return error;
20531da177e4SLinus Torvalds }
20541da177e4SLinus Torvalds 
2055d57999e1SDave Hansen /*
2056d57999e1SDave Hansen  * Be careful about ever adding any more callers of this
2057d57999e1SDave Hansen  * function.  Its flags must be in the namei format, not
2058d57999e1SDave Hansen  * what get passed to sys_open().
2059d57999e1SDave Hansen  */
2060d57999e1SDave Hansen static int __open_namei_create(struct nameidata *nd, struct path *path,
20618737c930SAl Viro 				int open_flag, int mode)
2062aab520e2SDave Hansen {
2063aab520e2SDave Hansen 	int error;
20644ac91378SJan Blunck 	struct dentry *dir = nd->path.dentry;
2065aab520e2SDave Hansen 
2066aab520e2SDave Hansen 	if (!IS_POSIXACL(dir->d_inode))
2067ce3b0f8dSAl Viro 		mode &= ~current_umask();
2068be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd->path, path->dentry, mode, 0);
2069be6d3e56SKentaro Takeda 	if (error)
2070be6d3e56SKentaro Takeda 		goto out_unlock;
2071aab520e2SDave Hansen 	error = vfs_create(dir->d_inode, path->dentry, mode, nd);
2072be6d3e56SKentaro Takeda out_unlock:
2073aab520e2SDave Hansen 	mutex_unlock(&dir->d_inode->i_mutex);
20744ac91378SJan Blunck 	dput(nd->path.dentry);
20754ac91378SJan Blunck 	nd->path.dentry = path->dentry;
207631e6b01fSNick Piggin 
2077aab520e2SDave Hansen 	if (error)
2078aab520e2SDave Hansen 		return error;
2079aab520e2SDave Hansen 	/* Don't check for write permission, don't truncate */
20808737c930SAl Viro 	return may_open(&nd->path, 0, open_flag & ~O_TRUNC);
2081aab520e2SDave Hansen }
2082aab520e2SDave Hansen 
20831da177e4SLinus Torvalds /*
2084d57999e1SDave Hansen  * Note that while the flag value (low two bits) for sys_open means:
2085d57999e1SDave Hansen  *	00 - read-only
2086d57999e1SDave Hansen  *	01 - write-only
2087d57999e1SDave Hansen  *	10 - read-write
2088d57999e1SDave Hansen  *	11 - special
2089d57999e1SDave Hansen  * it is changed into
2090d57999e1SDave Hansen  *	00 - no permissions needed
2091d57999e1SDave Hansen  *	01 - read-permission
2092d57999e1SDave Hansen  *	10 - write-permission
2093d57999e1SDave Hansen  *	11 - read-write
2094d57999e1SDave Hansen  * for the internal routines (ie open_namei()/follow_link() etc)
2095d57999e1SDave Hansen  * This is more logical, and also allows the 00 "no perm needed"
2096d57999e1SDave Hansen  * to be used for symlinks (where the permissions are checked
2097d57999e1SDave Hansen  * later).
2098d57999e1SDave Hansen  *
2099d57999e1SDave Hansen */
2100d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2101d57999e1SDave Hansen {
2102d57999e1SDave Hansen 	if ((flag+1) & O_ACCMODE)
2103d57999e1SDave Hansen 		flag++;
2104d57999e1SDave Hansen 	return flag;
2105d57999e1SDave Hansen }
2106d57999e1SDave Hansen 
21077715b521SAl Viro static int open_will_truncate(int flag, struct inode *inode)
21084a3fd211SDave Hansen {
2109d57999e1SDave Hansen 	/*
21104a3fd211SDave Hansen 	 * We'll never write to the fs underlying
21114a3fd211SDave Hansen 	 * a device file.
21124a3fd211SDave Hansen 	 */
21134a3fd211SDave Hansen 	if (special_file(inode->i_mode))
21144a3fd211SDave Hansen 		return 0;
21154a3fd211SDave Hansen 	return (flag & O_TRUNC);
21164a3fd211SDave Hansen }
21174a3fd211SDave Hansen 
2118648fa861SAl Viro static struct file *finish_open(struct nameidata *nd,
21199a66179eSAl Viro 				int open_flag, int acc_mode)
2120648fa861SAl Viro {
2121648fa861SAl Viro 	struct file *filp;
2122648fa861SAl Viro 	int will_truncate;
2123648fa861SAl Viro 	int error;
2124648fa861SAl Viro 
21259a66179eSAl Viro 	will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode);
2126648fa861SAl Viro 	if (will_truncate) {
2127648fa861SAl Viro 		error = mnt_want_write(nd->path.mnt);
2128648fa861SAl Viro 		if (error)
2129648fa861SAl Viro 			goto exit;
2130648fa861SAl Viro 	}
2131648fa861SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2132648fa861SAl Viro 	if (error) {
2133648fa861SAl Viro 		if (will_truncate)
2134648fa861SAl Viro 			mnt_drop_write(nd->path.mnt);
2135648fa861SAl Viro 		goto exit;
2136648fa861SAl Viro 	}
2137648fa861SAl Viro 	filp = nameidata_to_filp(nd);
2138648fa861SAl Viro 	if (!IS_ERR(filp)) {
2139648fa861SAl Viro 		error = ima_file_check(filp, acc_mode);
2140648fa861SAl Viro 		if (error) {
2141648fa861SAl Viro 			fput(filp);
2142648fa861SAl Viro 			filp = ERR_PTR(error);
2143648fa861SAl Viro 		}
2144648fa861SAl Viro 	}
2145648fa861SAl Viro 	if (!IS_ERR(filp)) {
2146648fa861SAl Viro 		if (will_truncate) {
2147e1181ee6SJeff Layton 			error = handle_truncate(filp);
2148648fa861SAl Viro 			if (error) {
2149648fa861SAl Viro 				fput(filp);
2150648fa861SAl Viro 				filp = ERR_PTR(error);
2151648fa861SAl Viro 			}
2152648fa861SAl Viro 		}
2153648fa861SAl Viro 	}
2154648fa861SAl Viro 	/*
2155648fa861SAl Viro 	 * It is now safe to drop the mnt write
2156648fa861SAl Viro 	 * because the filp has had a write taken
2157648fa861SAl Viro 	 * on its behalf.
2158648fa861SAl Viro 	 */
2159648fa861SAl Viro 	if (will_truncate)
2160648fa861SAl Viro 		mnt_drop_write(nd->path.mnt);
2161d893f1bcSAl Viro 	path_put(&nd->path);
2162648fa861SAl Viro 	return filp;
2163648fa861SAl Viro 
2164648fa861SAl Viro exit:
2165648fa861SAl Viro 	path_put(&nd->path);
2166648fa861SAl Viro 	return ERR_PTR(error);
2167648fa861SAl Viro }
2168648fa861SAl Viro 
216931e6b01fSNick Piggin /*
217031e6b01fSNick Piggin  * Handle O_CREAT case for do_filp_open
217131e6b01fSNick Piggin  */
2172fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
2173c3e380b0SAl Viro 			    const struct open_flags *op, const char *pathname)
2174fb1cc555SAl Viro {
2175a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
2176fb1cc555SAl Viro 	struct file *filp;
217716c2cd71SAl Viro 	int error;
2178fb1cc555SAl Viro 
2179c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2180c3e380b0SAl Viro 	nd->flags |= op->intent;
2181c3e380b0SAl Viro 
21821f36f774SAl Viro 	switch (nd->last_type) {
21831f36f774SAl Viro 	case LAST_DOTDOT:
21841f36f774SAl Viro 		follow_dotdot(nd);
21851f36f774SAl Viro 		dir = nd->path.dentry;
2186176306f5SNeil Brown 	case LAST_DOT:
21871f36f774SAl Viro 		/* fallthrough */
21881f36f774SAl Viro 	case LAST_ROOT:
218916c2cd71SAl Viro 		error = handle_reval_path(nd);
219016c2cd71SAl Viro 		if (error)
219116c2cd71SAl Viro 			goto exit;
219216c2cd71SAl Viro 		error = -EISDIR;
21931f36f774SAl Viro 		goto exit;
21941f36f774SAl Viro 	case LAST_BIND:
219516c2cd71SAl Viro 		error = handle_reval_path(nd);
219616c2cd71SAl Viro 		if (error)
219716c2cd71SAl Viro 			goto exit;
21981f36f774SAl Viro 		audit_inode(pathname, dir);
21991f36f774SAl Viro 		goto ok;
22001f36f774SAl Viro 	}
2201a2c36b45SAl Viro 
220216c2cd71SAl Viro 	error = -EISDIR;
22031f36f774SAl Viro 	/* trailing slashes? */
220431e6b01fSNick Piggin 	if (nd->last.name[nd->last.len])
22051f36f774SAl Viro 		goto exit;
22061f36f774SAl Viro 
2207a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
2208a1e28038SAl Viro 
2209a1e28038SAl Viro 	path->dentry = lookup_hash(nd);
2210a1e28038SAl Viro 	path->mnt = nd->path.mnt;
2211a1e28038SAl Viro 
2212fb1cc555SAl Viro 	error = PTR_ERR(path->dentry);
2213fb1cc555SAl Viro 	if (IS_ERR(path->dentry)) {
2214fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
2215fb1cc555SAl Viro 		goto exit;
2216fb1cc555SAl Viro 	}
2217fb1cc555SAl Viro 
2218fb1cc555SAl Viro 	if (IS_ERR(nd->intent.open.file)) {
2219fb1cc555SAl Viro 		error = PTR_ERR(nd->intent.open.file);
2220fb1cc555SAl Viro 		goto exit_mutex_unlock;
2221fb1cc555SAl Viro 	}
2222fb1cc555SAl Viro 
2223fb1cc555SAl Viro 	/* Negative dentry, just create the file */
2224fb1cc555SAl Viro 	if (!path->dentry->d_inode) {
2225fb1cc555SAl Viro 		/*
2226fb1cc555SAl Viro 		 * This write is needed to ensure that a
2227fb1cc555SAl Viro 		 * ro->rw transition does not occur between
2228fb1cc555SAl Viro 		 * the time when the file is created and when
2229fb1cc555SAl Viro 		 * a permanent write count is taken through
2230fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
2231fb1cc555SAl Viro 		 */
2232fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
2233fb1cc555SAl Viro 		if (error)
2234fb1cc555SAl Viro 			goto exit_mutex_unlock;
2235c3e380b0SAl Viro 		error = __open_namei_create(nd, path, op->open_flag, op->mode);
2236fb1cc555SAl Viro 		if (error) {
2237fb1cc555SAl Viro 			mnt_drop_write(nd->path.mnt);
2238fb1cc555SAl Viro 			goto exit;
2239fb1cc555SAl Viro 		}
2240fb1cc555SAl Viro 		filp = nameidata_to_filp(nd);
2241fb1cc555SAl Viro 		mnt_drop_write(nd->path.mnt);
2242d893f1bcSAl Viro 		path_put(&nd->path);
2243fb1cc555SAl Viro 		if (!IS_ERR(filp)) {
2244c3e380b0SAl Viro 			error = ima_file_check(filp, op->acc_mode);
2245fb1cc555SAl Viro 			if (error) {
2246fb1cc555SAl Viro 				fput(filp);
2247fb1cc555SAl Viro 				filp = ERR_PTR(error);
2248fb1cc555SAl Viro 			}
2249fb1cc555SAl Viro 		}
2250fb1cc555SAl Viro 		return filp;
2251fb1cc555SAl Viro 	}
2252fb1cc555SAl Viro 
2253fb1cc555SAl Viro 	/*
2254fb1cc555SAl Viro 	 * It already exists.
2255fb1cc555SAl Viro 	 */
2256fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2257fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2258fb1cc555SAl Viro 
2259fb1cc555SAl Viro 	error = -EEXIST;
2260c3e380b0SAl Viro 	if (op->open_flag & O_EXCL)
2261fb1cc555SAl Viro 		goto exit_dput;
2262fb1cc555SAl Viro 
22639875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
22649875cf80SDavid Howells 	if (error < 0)
2265fb1cc555SAl Viro 		goto exit_dput;
2266fb1cc555SAl Viro 
2267fb1cc555SAl Viro 	error = -ENOENT;
2268fb1cc555SAl Viro 	if (!path->dentry->d_inode)
2269fb1cc555SAl Viro 		goto exit_dput;
22709e67f361SAl Viro 
22719e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
2272fb1cc555SAl Viro 		return NULL;
2273fb1cc555SAl Viro 
2274fb1cc555SAl Viro 	path_to_nameidata(path, nd);
227531e6b01fSNick Piggin 	nd->inode = path->dentry->d_inode;
2276fb1cc555SAl Viro 	error = -EISDIR;
227731e6b01fSNick Piggin 	if (S_ISDIR(nd->inode->i_mode))
2278fb1cc555SAl Viro 		goto exit;
227967ee3ad2SAl Viro ok:
2280c3e380b0SAl Viro 	filp = finish_open(nd, op->open_flag, op->acc_mode);
2281fb1cc555SAl Viro 	return filp;
2282fb1cc555SAl Viro 
2283fb1cc555SAl Viro exit_mutex_unlock:
2284fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2285fb1cc555SAl Viro exit_dput:
2286fb1cc555SAl Viro 	path_put_conditional(path, nd);
2287fb1cc555SAl Viro exit:
2288fb1cc555SAl Viro 	path_put(&nd->path);
2289fb1cc555SAl Viro 	return ERR_PTR(error);
2290fb1cc555SAl Viro }
2291fb1cc555SAl Viro 
229213aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname,
229347c805dcSAl Viro 		const struct open_flags *op, int flags)
22941da177e4SLinus Torvalds {
22954a3fd211SDave Hansen 	struct file *filp;
2296a70e65dfSChristoph Hellwig 	struct nameidata nd;
22979850c056SAl Viro 	struct path path;
22981da177e4SLinus Torvalds 	int count = 0;
229913aab428SAl Viro 	int error;
230031e6b01fSNick Piggin 
230131e6b01fSNick Piggin 	filp = get_empty_filp();
230231e6b01fSNick Piggin 	if (!filp)
230331e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
230431e6b01fSNick Piggin 
230547c805dcSAl Viro 	filp->f_flags = op->open_flag;
230631e6b01fSNick Piggin 	nd.intent.open.file = filp;
230747c805dcSAl Viro 	nd.intent.open.flags = open_to_namei_flags(op->open_flag);
230847c805dcSAl Viro 	nd.intent.open.create_mode = op->mode;
230931e6b01fSNick Piggin 
231047c805dcSAl Viro 	if (op->open_flag & O_CREAT)
231131e6b01fSNick Piggin 		goto creat;
231231e6b01fSNick Piggin 
231331e6b01fSNick Piggin 	/* !O_CREAT, simple open */
231413aab428SAl Viro 	error = path_lookupat(dfd, pathname, flags | op->intent, &nd);
231531e6b01fSNick Piggin 	if (unlikely(error))
231613aab428SAl Viro 		goto out_filp;
231731e6b01fSNick Piggin 	error = -ELOOP;
231831e6b01fSNick Piggin 	if (!(nd.flags & LOOKUP_FOLLOW)) {
231931e6b01fSNick Piggin 		if (nd.inode->i_op->follow_link)
232013aab428SAl Viro 			goto out_path;
232131e6b01fSNick Piggin 	}
232231e6b01fSNick Piggin 	error = -ENOTDIR;
232331e6b01fSNick Piggin 	if (nd.flags & LOOKUP_DIRECTORY) {
232431e6b01fSNick Piggin 		if (!nd.inode->i_op->lookup)
232513aab428SAl Viro 			goto out_path;
232631e6b01fSNick Piggin 	}
232731e6b01fSNick Piggin 	audit_inode(pathname, nd.path.dentry);
232847c805dcSAl Viro 	filp = finish_open(&nd, op->open_flag, op->acc_mode);
23292dab5974SLinus Torvalds 	release_open_intent(&nd);
233031e6b01fSNick Piggin 	return filp;
233131e6b01fSNick Piggin 
233231e6b01fSNick Piggin creat:
233331e6b01fSNick Piggin 	/* OK, have to create the file. Find the parent. */
2334c3e380b0SAl Viro 	error = path_lookupat(dfd, pathname, LOOKUP_PARENT | flags, &nd);
233531e6b01fSNick Piggin 	if (unlikely(error))
233631e6b01fSNick Piggin 		goto out_filp;
233731e6b01fSNick Piggin 	if (unlikely(!audit_dummy_context()))
23389b4a9b14SAl Viro 		audit_inode(pathname, nd.path.dentry);
23391da177e4SLinus Torvalds 
23401da177e4SLinus Torvalds 	/*
2341a2c36b45SAl Viro 	 * We have the parent and last component.
23421da177e4SLinus Torvalds 	 */
234347c805dcSAl Viro 	filp = do_last(&nd, &path, op, pathname);
2344806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
23457b9337aaSNick Piggin 		struct path link = path;
23467b9337aaSNick Piggin 		struct inode *linki = link.dentry->d_inode;
2347def4af30SAl Viro 		void *cookie;
2348806b681cSAl Viro 		error = -ELOOP;
2349db372915SDavid Howells 		if (!(nd.flags & LOOKUP_FOLLOW))
23501f36f774SAl Viro 			goto exit_dput;
23511f36f774SAl Viro 		if (count++ == 32)
2352806b681cSAl Viro 			goto exit_dput;
2353806b681cSAl Viro 		/*
2354806b681cSAl Viro 		 * This is subtle. Instead of calling do_follow_link() we do
2355806b681cSAl Viro 		 * the thing by hands. The reason is that this way we have zero
2356806b681cSAl Viro 		 * link_count and path_walk() (called from ->follow_link)
2357806b681cSAl Viro 		 * honoring LOOKUP_PARENT.  After that we have the parent and
2358806b681cSAl Viro 		 * last component, i.e. we are in the same situation as after
2359806b681cSAl Viro 		 * the first path_walk().  Well, almost - if the last component
2360806b681cSAl Viro 		 * is normal we get its copy stored in nd->last.name and we will
2361806b681cSAl Viro 		 * have to putname() it when we are done. Procfs-like symlinks
2362806b681cSAl Viro 		 * just set LAST_BIND.
2363806b681cSAl Viro 		 */
2364806b681cSAl Viro 		nd.flags |= LOOKUP_PARENT;
2365c3e380b0SAl Viro 		nd.flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
23667b9337aaSNick Piggin 		error = __do_follow_link(&link, &nd, &cookie);
2367c3e380b0SAl Viro 		if (unlikely(error))
2368f1afe9efSAl Viro 			filp = ERR_PTR(error);
2369c3e380b0SAl Viro 		else
237047c805dcSAl Viro 			filp = do_last(&nd, &path, op, pathname);
2371f1afe9efSAl Viro 		if (!IS_ERR(cookie) && linki->i_op->put_link)
23727b9337aaSNick Piggin 			linki->i_op->put_link(link.dentry, &nd, cookie);
23737b9337aaSNick Piggin 		path_put(&link);
2374806b681cSAl Viro 	}
237510fa8e62SAl Viro out:
23762a737871SAl Viro 	if (nd.root.mnt)
23772a737871SAl Viro 		path_put(&nd.root);
23782dab5974SLinus Torvalds 	release_open_intent(&nd);
237910fa8e62SAl Viro 	return filp;
23801da177e4SLinus Torvalds 
2381806b681cSAl Viro exit_dput:
2382806b681cSAl Viro 	path_put_conditional(&path, &nd);
238331e6b01fSNick Piggin out_path:
238431e6b01fSNick Piggin 	path_put(&nd.path);
238531e6b01fSNick Piggin out_filp:
238610fa8e62SAl Viro 	filp = ERR_PTR(error);
238710fa8e62SAl Viro 	goto out;
2388de459215SKirill Korotaev }
23891da177e4SLinus Torvalds 
239013aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname,
239113aab428SAl Viro 		const struct open_flags *op, int flags)
239213aab428SAl Viro {
239313aab428SAl Viro 	struct file *filp;
239413aab428SAl Viro 
239513aab428SAl Viro 	filp = path_openat(dfd, pathname, op, flags | LOOKUP_RCU);
239613aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
239713aab428SAl Viro 		filp = path_openat(dfd, pathname, op, flags);
239813aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
239913aab428SAl Viro 		filp = path_openat(dfd, pathname, op, flags | LOOKUP_REVAL);
240013aab428SAl Viro 	return filp;
240113aab428SAl Viro }
240213aab428SAl Viro 
24031da177e4SLinus Torvalds /**
24041da177e4SLinus Torvalds  * lookup_create - lookup a dentry, creating it if it doesn't exist
24051da177e4SLinus Torvalds  * @nd: nameidata info
24061da177e4SLinus Torvalds  * @is_dir: directory flag
24071da177e4SLinus Torvalds  *
24081da177e4SLinus Torvalds  * Simple function to lookup and return a dentry and create it
24091da177e4SLinus Torvalds  * if it doesn't exist.  Is SMP-safe.
2410c663e5d8SChristoph Hellwig  *
24114ac91378SJan Blunck  * Returns with nd->path.dentry->d_inode->i_mutex locked.
24121da177e4SLinus Torvalds  */
24131da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir)
24141da177e4SLinus Torvalds {
2415c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
24161da177e4SLinus Torvalds 
24174ac91378SJan Blunck 	mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2418c663e5d8SChristoph Hellwig 	/*
2419c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2420c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2421c663e5d8SChristoph Hellwig 	 */
24221da177e4SLinus Torvalds 	if (nd->last_type != LAST_NORM)
24231da177e4SLinus Torvalds 		goto fail;
24241da177e4SLinus Torvalds 	nd->flags &= ~LOOKUP_PARENT;
24253516586aSAl Viro 	nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2426a634904aSASANO Masahiro 	nd->intent.open.flags = O_EXCL;
2427c663e5d8SChristoph Hellwig 
2428c663e5d8SChristoph Hellwig 	/*
2429c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2430c663e5d8SChristoph Hellwig 	 */
243149705b77SChristoph Hellwig 	dentry = lookup_hash(nd);
24321da177e4SLinus Torvalds 	if (IS_ERR(dentry))
24331da177e4SLinus Torvalds 		goto fail;
2434c663e5d8SChristoph Hellwig 
2435e9baf6e5SAl Viro 	if (dentry->d_inode)
2436e9baf6e5SAl Viro 		goto eexist;
2437c663e5d8SChristoph Hellwig 	/*
2438c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2439c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2440c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2441c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2442c663e5d8SChristoph Hellwig 	 */
2443e9baf6e5SAl Viro 	if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
24441da177e4SLinus Torvalds 		dput(dentry);
24451da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2446e9baf6e5SAl Viro 	}
2447e9baf6e5SAl Viro 	return dentry;
2448e9baf6e5SAl Viro eexist:
2449e9baf6e5SAl Viro 	dput(dentry);
2450e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
24511da177e4SLinus Torvalds fail:
24521da177e4SLinus Torvalds 	return dentry;
24531da177e4SLinus Torvalds }
2454f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create);
24551da177e4SLinus Torvalds 
24561da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
24571da177e4SLinus Torvalds {
2458a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
24591da177e4SLinus Torvalds 
24601da177e4SLinus Torvalds 	if (error)
24611da177e4SLinus Torvalds 		return error;
24621da177e4SLinus Torvalds 
24631da177e4SLinus Torvalds 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
24641da177e4SLinus Torvalds 		return -EPERM;
24651da177e4SLinus Torvalds 
2466acfa4380SAl Viro 	if (!dir->i_op->mknod)
24671da177e4SLinus Torvalds 		return -EPERM;
24681da177e4SLinus Torvalds 
246908ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
247008ce5f16SSerge E. Hallyn 	if (error)
247108ce5f16SSerge E. Hallyn 		return error;
247208ce5f16SSerge E. Hallyn 
24731da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
24741da177e4SLinus Torvalds 	if (error)
24751da177e4SLinus Torvalds 		return error;
24761da177e4SLinus Torvalds 
24771da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2478a74574aaSStephen Smalley 	if (!error)
2479f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
24801da177e4SLinus Torvalds 	return error;
24811da177e4SLinus Torvalds }
24821da177e4SLinus Torvalds 
2483463c3197SDave Hansen static int may_mknod(mode_t mode)
2484463c3197SDave Hansen {
2485463c3197SDave Hansen 	switch (mode & S_IFMT) {
2486463c3197SDave Hansen 	case S_IFREG:
2487463c3197SDave Hansen 	case S_IFCHR:
2488463c3197SDave Hansen 	case S_IFBLK:
2489463c3197SDave Hansen 	case S_IFIFO:
2490463c3197SDave Hansen 	case S_IFSOCK:
2491463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2492463c3197SDave Hansen 		return 0;
2493463c3197SDave Hansen 	case S_IFDIR:
2494463c3197SDave Hansen 		return -EPERM;
2495463c3197SDave Hansen 	default:
2496463c3197SDave Hansen 		return -EINVAL;
2497463c3197SDave Hansen 	}
2498463c3197SDave Hansen }
2499463c3197SDave Hansen 
25002e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
25012e4d0924SHeiko Carstens 		unsigned, dev)
25021da177e4SLinus Torvalds {
25032ad94ae6SAl Viro 	int error;
25041da177e4SLinus Torvalds 	char *tmp;
25051da177e4SLinus Torvalds 	struct dentry *dentry;
25061da177e4SLinus Torvalds 	struct nameidata nd;
25071da177e4SLinus Torvalds 
25081da177e4SLinus Torvalds 	if (S_ISDIR(mode))
25091da177e4SLinus Torvalds 		return -EPERM;
25101da177e4SLinus Torvalds 
25112ad94ae6SAl Viro 	error = user_path_parent(dfd, filename, &nd, &tmp);
25121da177e4SLinus Torvalds 	if (error)
25132ad94ae6SAl Viro 		return error;
25142ad94ae6SAl Viro 
25151da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
2516463c3197SDave Hansen 	if (IS_ERR(dentry)) {
25171da177e4SLinus Torvalds 		error = PTR_ERR(dentry);
2518463c3197SDave Hansen 		goto out_unlock;
2519463c3197SDave Hansen 	}
25204ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2521ce3b0f8dSAl Viro 		mode &= ~current_umask();
2522463c3197SDave Hansen 	error = may_mknod(mode);
2523463c3197SDave Hansen 	if (error)
2524463c3197SDave Hansen 		goto out_dput;
2525463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2526463c3197SDave Hansen 	if (error)
2527463c3197SDave Hansen 		goto out_dput;
2528be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd.path, dentry, mode, dev);
2529be6d3e56SKentaro Takeda 	if (error)
2530be6d3e56SKentaro Takeda 		goto out_drop_write;
25311da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
25321da177e4SLinus Torvalds 		case 0: case S_IFREG:
25334ac91378SJan Blunck 			error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
25341da177e4SLinus Torvalds 			break;
25351da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
25364ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
25371da177e4SLinus Torvalds 					new_decode_dev(dev));
25381da177e4SLinus Torvalds 			break;
25391da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
25404ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
25411da177e4SLinus Torvalds 			break;
25421da177e4SLinus Torvalds 	}
2543be6d3e56SKentaro Takeda out_drop_write:
2544463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2545463c3197SDave Hansen out_dput:
25461da177e4SLinus Torvalds 	dput(dentry);
2547463c3197SDave Hansen out_unlock:
25484ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
25491d957f9bSJan Blunck 	path_put(&nd.path);
25501da177e4SLinus Torvalds 	putname(tmp);
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds 	return error;
25531da177e4SLinus Torvalds }
25541da177e4SLinus Torvalds 
25553480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
25565590ff0dSUlrich Drepper {
25575590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
25585590ff0dSUlrich Drepper }
25595590ff0dSUlrich Drepper 
25601da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
25611da177e4SLinus Torvalds {
2562a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
25631da177e4SLinus Torvalds 
25641da177e4SLinus Torvalds 	if (error)
25651da177e4SLinus Torvalds 		return error;
25661da177e4SLinus Torvalds 
2567acfa4380SAl Viro 	if (!dir->i_op->mkdir)
25681da177e4SLinus Torvalds 		return -EPERM;
25691da177e4SLinus Torvalds 
25701da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
25711da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
25721da177e4SLinus Torvalds 	if (error)
25731da177e4SLinus Torvalds 		return error;
25741da177e4SLinus Torvalds 
25751da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2576a74574aaSStephen Smalley 	if (!error)
2577f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
25781da177e4SLinus Torvalds 	return error;
25791da177e4SLinus Torvalds }
25801da177e4SLinus Torvalds 
25812e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
25821da177e4SLinus Torvalds {
25831da177e4SLinus Torvalds 	int error = 0;
25841da177e4SLinus Torvalds 	char * tmp;
25856902d925SDave Hansen 	struct dentry *dentry;
25866902d925SDave Hansen 	struct nameidata nd;
25871da177e4SLinus Torvalds 
25882ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &tmp);
25892ad94ae6SAl Viro 	if (error)
25906902d925SDave Hansen 		goto out_err;
25911da177e4SLinus Torvalds 
25921da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 1);
25931da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
25946902d925SDave Hansen 	if (IS_ERR(dentry))
25956902d925SDave Hansen 		goto out_unlock;
25966902d925SDave Hansen 
25974ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2598ce3b0f8dSAl Viro 		mode &= ~current_umask();
2599463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2600463c3197SDave Hansen 	if (error)
2601463c3197SDave Hansen 		goto out_dput;
2602be6d3e56SKentaro Takeda 	error = security_path_mkdir(&nd.path, dentry, mode);
2603be6d3e56SKentaro Takeda 	if (error)
2604be6d3e56SKentaro Takeda 		goto out_drop_write;
26054ac91378SJan Blunck 	error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2606be6d3e56SKentaro Takeda out_drop_write:
2607463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2608463c3197SDave Hansen out_dput:
26091da177e4SLinus Torvalds 	dput(dentry);
26106902d925SDave Hansen out_unlock:
26114ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
26121d957f9bSJan Blunck 	path_put(&nd.path);
26131da177e4SLinus Torvalds 	putname(tmp);
26146902d925SDave Hansen out_err:
26151da177e4SLinus Torvalds 	return error;
26161da177e4SLinus Torvalds }
26171da177e4SLinus Torvalds 
26183cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
26195590ff0dSUlrich Drepper {
26205590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
26215590ff0dSUlrich Drepper }
26225590ff0dSUlrich Drepper 
26231da177e4SLinus Torvalds /*
26241da177e4SLinus Torvalds  * We try to drop the dentry early: we should have
26251da177e4SLinus Torvalds  * a usage count of 2 if we're the only user of this
26261da177e4SLinus Torvalds  * dentry, and if that is true (possibly after pruning
26271da177e4SLinus Torvalds  * the dcache), then we drop the dentry now.
26281da177e4SLinus Torvalds  *
26291da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
26301da177e4SLinus Torvalds  * do a
26311da177e4SLinus Torvalds  *
26321da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
26331da177e4SLinus Torvalds  *		return -EBUSY;
26341da177e4SLinus Torvalds  *
26351da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
26361da177e4SLinus Torvalds  * that is still in use by something else..
26371da177e4SLinus Torvalds  */
26381da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
26391da177e4SLinus Torvalds {
26401da177e4SLinus Torvalds 	dget(dentry);
26411da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
26421da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
2643b7ab39f6SNick Piggin 	if (dentry->d_count == 2)
26441da177e4SLinus Torvalds 		__d_drop(dentry);
26451da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
26461da177e4SLinus Torvalds }
26471da177e4SLinus Torvalds 
26481da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
26491da177e4SLinus Torvalds {
26501da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
26511da177e4SLinus Torvalds 
26521da177e4SLinus Torvalds 	if (error)
26531da177e4SLinus Torvalds 		return error;
26541da177e4SLinus Torvalds 
2655acfa4380SAl Viro 	if (!dir->i_op->rmdir)
26561da177e4SLinus Torvalds 		return -EPERM;
26571da177e4SLinus Torvalds 
26581b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
26591da177e4SLinus Torvalds 	dentry_unhash(dentry);
26601da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
26611da177e4SLinus Torvalds 		error = -EBUSY;
26621da177e4SLinus Torvalds 	else {
26631da177e4SLinus Torvalds 		error = security_inode_rmdir(dir, dentry);
26641da177e4SLinus Torvalds 		if (!error) {
26651da177e4SLinus Torvalds 			error = dir->i_op->rmdir(dir, dentry);
2666d83c49f3SAl Viro 			if (!error) {
26671da177e4SLinus Torvalds 				dentry->d_inode->i_flags |= S_DEAD;
2668d83c49f3SAl Viro 				dont_mount(dentry);
2669d83c49f3SAl Viro 			}
26701da177e4SLinus Torvalds 		}
26711da177e4SLinus Torvalds 	}
26721b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
26731da177e4SLinus Torvalds 	if (!error) {
26741da177e4SLinus Torvalds 		d_delete(dentry);
26751da177e4SLinus Torvalds 	}
26761da177e4SLinus Torvalds 	dput(dentry);
26771da177e4SLinus Torvalds 
26781da177e4SLinus Torvalds 	return error;
26791da177e4SLinus Torvalds }
26801da177e4SLinus Torvalds 
26815590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
26821da177e4SLinus Torvalds {
26831da177e4SLinus Torvalds 	int error = 0;
26841da177e4SLinus Torvalds 	char * name;
26851da177e4SLinus Torvalds 	struct dentry *dentry;
26861da177e4SLinus Torvalds 	struct nameidata nd;
26871da177e4SLinus Torvalds 
26882ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
26891da177e4SLinus Torvalds 	if (error)
26902ad94ae6SAl Viro 		return error;
26911da177e4SLinus Torvalds 
26921da177e4SLinus Torvalds 	switch(nd.last_type) {
26931da177e4SLinus Torvalds 	case LAST_DOTDOT:
26941da177e4SLinus Torvalds 		error = -ENOTEMPTY;
26951da177e4SLinus Torvalds 		goto exit1;
26961da177e4SLinus Torvalds 	case LAST_DOT:
26971da177e4SLinus Torvalds 		error = -EINVAL;
26981da177e4SLinus Torvalds 		goto exit1;
26991da177e4SLinus Torvalds 	case LAST_ROOT:
27001da177e4SLinus Torvalds 		error = -EBUSY;
27011da177e4SLinus Torvalds 		goto exit1;
27021da177e4SLinus Torvalds 	}
27030612d9fbSOGAWA Hirofumi 
27040612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
27050612d9fbSOGAWA Hirofumi 
27064ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
270749705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
27081da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
27096902d925SDave Hansen 	if (IS_ERR(dentry))
27106902d925SDave Hansen 		goto exit2;
27110622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
27120622753bSDave Hansen 	if (error)
27130622753bSDave Hansen 		goto exit3;
2714be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2715be6d3e56SKentaro Takeda 	if (error)
2716be6d3e56SKentaro Takeda 		goto exit4;
27174ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2718be6d3e56SKentaro Takeda exit4:
27190622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
27200622753bSDave Hansen exit3:
27211da177e4SLinus Torvalds 	dput(dentry);
27226902d925SDave Hansen exit2:
27234ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
27241da177e4SLinus Torvalds exit1:
27251d957f9bSJan Blunck 	path_put(&nd.path);
27261da177e4SLinus Torvalds 	putname(name);
27271da177e4SLinus Torvalds 	return error;
27281da177e4SLinus Torvalds }
27291da177e4SLinus Torvalds 
27303cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
27315590ff0dSUlrich Drepper {
27325590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
27335590ff0dSUlrich Drepper }
27345590ff0dSUlrich Drepper 
27351da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
27361da177e4SLinus Torvalds {
27371da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
27381da177e4SLinus Torvalds 
27391da177e4SLinus Torvalds 	if (error)
27401da177e4SLinus Torvalds 		return error;
27411da177e4SLinus Torvalds 
2742acfa4380SAl Viro 	if (!dir->i_op->unlink)
27431da177e4SLinus Torvalds 		return -EPERM;
27441da177e4SLinus Torvalds 
27451b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
27461da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
27471da177e4SLinus Torvalds 		error = -EBUSY;
27481da177e4SLinus Torvalds 	else {
27491da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2750bec1052eSAl Viro 		if (!error) {
27511da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2752bec1052eSAl Viro 			if (!error)
2753d83c49f3SAl Viro 				dont_mount(dentry);
2754bec1052eSAl Viro 		}
27551da177e4SLinus Torvalds 	}
27561b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
27571da177e4SLinus Torvalds 
27581da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
27591da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2760ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
27611da177e4SLinus Torvalds 		d_delete(dentry);
27621da177e4SLinus Torvalds 	}
27630eeca283SRobert Love 
27641da177e4SLinus Torvalds 	return error;
27651da177e4SLinus Torvalds }
27661da177e4SLinus Torvalds 
27671da177e4SLinus Torvalds /*
27681da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
27691b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
27701da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
27711da177e4SLinus Torvalds  * while waiting on the I/O.
27721da177e4SLinus Torvalds  */
27735590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
27741da177e4SLinus Torvalds {
27752ad94ae6SAl Viro 	int error;
27761da177e4SLinus Torvalds 	char *name;
27771da177e4SLinus Torvalds 	struct dentry *dentry;
27781da177e4SLinus Torvalds 	struct nameidata nd;
27791da177e4SLinus Torvalds 	struct inode *inode = NULL;
27801da177e4SLinus Torvalds 
27812ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
27821da177e4SLinus Torvalds 	if (error)
27832ad94ae6SAl Viro 		return error;
27842ad94ae6SAl Viro 
27851da177e4SLinus Torvalds 	error = -EISDIR;
27861da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
27871da177e4SLinus Torvalds 		goto exit1;
27880612d9fbSOGAWA Hirofumi 
27890612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
27900612d9fbSOGAWA Hirofumi 
27914ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
279249705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
27931da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
27941da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
27951da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
27961da177e4SLinus Torvalds 		if (nd.last.name[nd.last.len])
27971da177e4SLinus Torvalds 			goto slashes;
27981da177e4SLinus Torvalds 		inode = dentry->d_inode;
27991da177e4SLinus Torvalds 		if (inode)
28007de9c6eeSAl Viro 			ihold(inode);
28010622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
28020622753bSDave Hansen 		if (error)
28030622753bSDave Hansen 			goto exit2;
2804be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2805be6d3e56SKentaro Takeda 		if (error)
2806be6d3e56SKentaro Takeda 			goto exit3;
28074ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2808be6d3e56SKentaro Takeda exit3:
28090622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
28101da177e4SLinus Torvalds 	exit2:
28111da177e4SLinus Torvalds 		dput(dentry);
28121da177e4SLinus Torvalds 	}
28134ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
28141da177e4SLinus Torvalds 	if (inode)
28151da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
28161da177e4SLinus Torvalds exit1:
28171d957f9bSJan Blunck 	path_put(&nd.path);
28181da177e4SLinus Torvalds 	putname(name);
28191da177e4SLinus Torvalds 	return error;
28201da177e4SLinus Torvalds 
28211da177e4SLinus Torvalds slashes:
28221da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
28231da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
28241da177e4SLinus Torvalds 	goto exit2;
28251da177e4SLinus Torvalds }
28261da177e4SLinus Torvalds 
28272e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
28285590ff0dSUlrich Drepper {
28295590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
28305590ff0dSUlrich Drepper 		return -EINVAL;
28315590ff0dSUlrich Drepper 
28325590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
28335590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
28345590ff0dSUlrich Drepper 
28355590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
28365590ff0dSUlrich Drepper }
28375590ff0dSUlrich Drepper 
28383480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
28395590ff0dSUlrich Drepper {
28405590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
28415590ff0dSUlrich Drepper }
28425590ff0dSUlrich Drepper 
2843db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
28441da177e4SLinus Torvalds {
2845a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
28461da177e4SLinus Torvalds 
28471da177e4SLinus Torvalds 	if (error)
28481da177e4SLinus Torvalds 		return error;
28491da177e4SLinus Torvalds 
2850acfa4380SAl Viro 	if (!dir->i_op->symlink)
28511da177e4SLinus Torvalds 		return -EPERM;
28521da177e4SLinus Torvalds 
28531da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
28541da177e4SLinus Torvalds 	if (error)
28551da177e4SLinus Torvalds 		return error;
28561da177e4SLinus Torvalds 
28571da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2858a74574aaSStephen Smalley 	if (!error)
2859f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
28601da177e4SLinus Torvalds 	return error;
28611da177e4SLinus Torvalds }
28621da177e4SLinus Torvalds 
28632e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
28642e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
28651da177e4SLinus Torvalds {
28662ad94ae6SAl Viro 	int error;
28671da177e4SLinus Torvalds 	char *from;
28681da177e4SLinus Torvalds 	char *to;
28696902d925SDave Hansen 	struct dentry *dentry;
28706902d925SDave Hansen 	struct nameidata nd;
28711da177e4SLinus Torvalds 
28721da177e4SLinus Torvalds 	from = getname(oldname);
28731da177e4SLinus Torvalds 	if (IS_ERR(from))
28741da177e4SLinus Torvalds 		return PTR_ERR(from);
28752ad94ae6SAl Viro 
28762ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
28772ad94ae6SAl Viro 	if (error)
28786902d925SDave Hansen 		goto out_putname;
28791da177e4SLinus Torvalds 
28801da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
28811da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
28826902d925SDave Hansen 	if (IS_ERR(dentry))
28836902d925SDave Hansen 		goto out_unlock;
28846902d925SDave Hansen 
288575c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
288675c3f29dSDave Hansen 	if (error)
288775c3f29dSDave Hansen 		goto out_dput;
2888be6d3e56SKentaro Takeda 	error = security_path_symlink(&nd.path, dentry, from);
2889be6d3e56SKentaro Takeda 	if (error)
2890be6d3e56SKentaro Takeda 		goto out_drop_write;
2891db2e747bSMiklos Szeredi 	error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2892be6d3e56SKentaro Takeda out_drop_write:
289375c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
289475c3f29dSDave Hansen out_dput:
28951da177e4SLinus Torvalds 	dput(dentry);
28966902d925SDave Hansen out_unlock:
28974ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
28981d957f9bSJan Blunck 	path_put(&nd.path);
28991da177e4SLinus Torvalds 	putname(to);
29006902d925SDave Hansen out_putname:
29011da177e4SLinus Torvalds 	putname(from);
29021da177e4SLinus Torvalds 	return error;
29031da177e4SLinus Torvalds }
29041da177e4SLinus Torvalds 
29053480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
29065590ff0dSUlrich Drepper {
29075590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
29085590ff0dSUlrich Drepper }
29095590ff0dSUlrich Drepper 
29101da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
29111da177e4SLinus Torvalds {
29121da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
29131da177e4SLinus Torvalds 	int error;
29141da177e4SLinus Torvalds 
29151da177e4SLinus Torvalds 	if (!inode)
29161da177e4SLinus Torvalds 		return -ENOENT;
29171da177e4SLinus Torvalds 
2918a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
29191da177e4SLinus Torvalds 	if (error)
29201da177e4SLinus Torvalds 		return error;
29211da177e4SLinus Torvalds 
29221da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
29231da177e4SLinus Torvalds 		return -EXDEV;
29241da177e4SLinus Torvalds 
29251da177e4SLinus Torvalds 	/*
29261da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
29271da177e4SLinus Torvalds 	 */
29281da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
29291da177e4SLinus Torvalds 		return -EPERM;
2930acfa4380SAl Viro 	if (!dir->i_op->link)
29311da177e4SLinus Torvalds 		return -EPERM;
29327e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
29331da177e4SLinus Torvalds 		return -EPERM;
29341da177e4SLinus Torvalds 
29351da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
29361da177e4SLinus Torvalds 	if (error)
29371da177e4SLinus Torvalds 		return error;
29381da177e4SLinus Torvalds 
29397e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
29401da177e4SLinus Torvalds 	error = dir->i_op->link(old_dentry, dir, new_dentry);
29417e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
2942e31e14ecSStephen Smalley 	if (!error)
29437e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
29441da177e4SLinus Torvalds 	return error;
29451da177e4SLinus Torvalds }
29461da177e4SLinus Torvalds 
29471da177e4SLinus Torvalds /*
29481da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
29491da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
29501da177e4SLinus Torvalds  * newname.  --KAB
29511da177e4SLinus Torvalds  *
29521da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
29531da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
29541da177e4SLinus Torvalds  * and other special files.  --ADM
29551da177e4SLinus Torvalds  */
29562e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
29572e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
29581da177e4SLinus Torvalds {
29591da177e4SLinus Torvalds 	struct dentry *new_dentry;
29602d8f3038SAl Viro 	struct nameidata nd;
29612d8f3038SAl Viro 	struct path old_path;
29621da177e4SLinus Torvalds 	int error;
29631da177e4SLinus Torvalds 	char *to;
29641da177e4SLinus Torvalds 
296545c9b11aSUlrich Drepper 	if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
2966c04030e1SUlrich Drepper 		return -EINVAL;
2967c04030e1SUlrich Drepper 
29682d8f3038SAl Viro 	error = user_path_at(olddfd, oldname,
296945c9b11aSUlrich Drepper 			     flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
29702d8f3038SAl Viro 			     &old_path);
29711da177e4SLinus Torvalds 	if (error)
29722ad94ae6SAl Viro 		return error;
29732ad94ae6SAl Viro 
29742ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
29751da177e4SLinus Torvalds 	if (error)
29761da177e4SLinus Torvalds 		goto out;
29771da177e4SLinus Torvalds 	error = -EXDEV;
29782d8f3038SAl Viro 	if (old_path.mnt != nd.path.mnt)
29791da177e4SLinus Torvalds 		goto out_release;
29801da177e4SLinus Torvalds 	new_dentry = lookup_create(&nd, 0);
29811da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
29826902d925SDave Hansen 	if (IS_ERR(new_dentry))
29836902d925SDave Hansen 		goto out_unlock;
298475c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
298575c3f29dSDave Hansen 	if (error)
298675c3f29dSDave Hansen 		goto out_dput;
2987be6d3e56SKentaro Takeda 	error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2988be6d3e56SKentaro Takeda 	if (error)
2989be6d3e56SKentaro Takeda 		goto out_drop_write;
29902d8f3038SAl Viro 	error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
2991be6d3e56SKentaro Takeda out_drop_write:
299275c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
299375c3f29dSDave Hansen out_dput:
29941da177e4SLinus Torvalds 	dput(new_dentry);
29956902d925SDave Hansen out_unlock:
29964ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
29971da177e4SLinus Torvalds out_release:
29981d957f9bSJan Blunck 	path_put(&nd.path);
29992ad94ae6SAl Viro 	putname(to);
30001da177e4SLinus Torvalds out:
30012d8f3038SAl Viro 	path_put(&old_path);
30021da177e4SLinus Torvalds 
30031da177e4SLinus Torvalds 	return error;
30041da177e4SLinus Torvalds }
30051da177e4SLinus Torvalds 
30063480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
30075590ff0dSUlrich Drepper {
3008c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
30095590ff0dSUlrich Drepper }
30105590ff0dSUlrich Drepper 
30111da177e4SLinus Torvalds /*
30121da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
30131da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
30141da177e4SLinus Torvalds  * Problems:
30151da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
30161da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
30171da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
3018a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
30191da177e4SLinus Torvalds  *	   story.
30201da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
30211b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
30221da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
30231da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
3024a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
30251da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
30261da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
30271da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
3028a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
30291da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
30301da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
30311da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
30321da177e4SLinus Torvalds  *	d) some filesystems don't support opened-but-unlinked directories,
30331da177e4SLinus Torvalds  *	   either because of layout or because they are not ready to deal with
30341da177e4SLinus Torvalds  *	   all cases correctly. The latter will be fixed (taking this sort of
30351da177e4SLinus Torvalds  *	   stuff into VFS), but the former is not going away. Solution: the same
30361da177e4SLinus Torvalds  *	   trick as in rmdir().
30371da177e4SLinus Torvalds  *	e) conversion from fhandle to dentry may come in the wrong moment - when
30381b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
30391da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3040c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
30411da177e4SLinus Torvalds  *	   locking].
30421da177e4SLinus Torvalds  */
304375c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
30441da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
30451da177e4SLinus Torvalds {
30461da177e4SLinus Torvalds 	int error = 0;
30471da177e4SLinus Torvalds 	struct inode *target;
30481da177e4SLinus Torvalds 
30491da177e4SLinus Torvalds 	/*
30501da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
30511da177e4SLinus Torvalds 	 * we'll need to flip '..'.
30521da177e4SLinus Torvalds 	 */
30531da177e4SLinus Torvalds 	if (new_dir != old_dir) {
3054f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
30551da177e4SLinus Torvalds 		if (error)
30561da177e4SLinus Torvalds 			return error;
30571da177e4SLinus Torvalds 	}
30581da177e4SLinus Torvalds 
30591da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30601da177e4SLinus Torvalds 	if (error)
30611da177e4SLinus Torvalds 		return error;
30621da177e4SLinus Torvalds 
30631da177e4SLinus Torvalds 	target = new_dentry->d_inode;
3064d83c49f3SAl Viro 	if (target)
30651b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
30661da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
30671da177e4SLinus Torvalds 		error = -EBUSY;
3068d83c49f3SAl Viro 	else {
3069d83c49f3SAl Viro 		if (target)
3070d83c49f3SAl Viro 			dentry_unhash(new_dentry);
30711da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3072d83c49f3SAl Viro 	}
30731da177e4SLinus Torvalds 	if (target) {
3074d83c49f3SAl Viro 		if (!error) {
30751da177e4SLinus Torvalds 			target->i_flags |= S_DEAD;
3076d83c49f3SAl Viro 			dont_mount(new_dentry);
3077d83c49f3SAl Viro 		}
30781b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
30791da177e4SLinus Torvalds 		if (d_unhashed(new_dentry))
30801da177e4SLinus Torvalds 			d_rehash(new_dentry);
30811da177e4SLinus Torvalds 		dput(new_dentry);
30821da177e4SLinus Torvalds 	}
3083e31e14ecSStephen Smalley 	if (!error)
3084349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30851da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
30861da177e4SLinus Torvalds 	return error;
30871da177e4SLinus Torvalds }
30881da177e4SLinus Torvalds 
308975c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
30901da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
30911da177e4SLinus Torvalds {
30921da177e4SLinus Torvalds 	struct inode *target;
30931da177e4SLinus Torvalds 	int error;
30941da177e4SLinus Torvalds 
30951da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30961da177e4SLinus Torvalds 	if (error)
30971da177e4SLinus Torvalds 		return error;
30981da177e4SLinus Torvalds 
30991da177e4SLinus Torvalds 	dget(new_dentry);
31001da177e4SLinus Torvalds 	target = new_dentry->d_inode;
31011da177e4SLinus Torvalds 	if (target)
31021b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
31031da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
31041da177e4SLinus Torvalds 		error = -EBUSY;
31051da177e4SLinus Torvalds 	else
31061da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
31071da177e4SLinus Torvalds 	if (!error) {
3108bec1052eSAl Viro 		if (target)
3109d83c49f3SAl Viro 			dont_mount(new_dentry);
3110349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
31111da177e4SLinus Torvalds 			d_move(old_dentry, new_dentry);
31121da177e4SLinus Torvalds 	}
31131da177e4SLinus Torvalds 	if (target)
31141b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
31151da177e4SLinus Torvalds 	dput(new_dentry);
31161da177e4SLinus Torvalds 	return error;
31171da177e4SLinus Torvalds }
31181da177e4SLinus Torvalds 
31191da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
31201da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
31211da177e4SLinus Torvalds {
31221da177e4SLinus Torvalds 	int error;
31231da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
312459b0df21SEric Paris 	const unsigned char *old_name;
31251da177e4SLinus Torvalds 
31261da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
31271da177e4SLinus Torvalds  		return 0;
31281da177e4SLinus Torvalds 
31291da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
31301da177e4SLinus Torvalds 	if (error)
31311da177e4SLinus Torvalds 		return error;
31321da177e4SLinus Torvalds 
31331da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3134a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
31351da177e4SLinus Torvalds 	else
31361da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
31371da177e4SLinus Torvalds 	if (error)
31381da177e4SLinus Torvalds 		return error;
31391da177e4SLinus Torvalds 
3140acfa4380SAl Viro 	if (!old_dir->i_op->rename)
31411da177e4SLinus Torvalds 		return -EPERM;
31421da177e4SLinus Torvalds 
31430eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
31440eeca283SRobert Love 
31451da177e4SLinus Torvalds 	if (is_dir)
31461da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
31471da177e4SLinus Torvalds 	else
31481da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3149123df294SAl Viro 	if (!error)
3150123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
31515a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
31520eeca283SRobert Love 	fsnotify_oldname_free(old_name);
31530eeca283SRobert Love 
31541da177e4SLinus Torvalds 	return error;
31551da177e4SLinus Torvalds }
31561da177e4SLinus Torvalds 
31572e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
31582e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
31591da177e4SLinus Torvalds {
31601da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
31611da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
31621da177e4SLinus Torvalds 	struct dentry *trap;
31631da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
31642ad94ae6SAl Viro 	char *from;
31652ad94ae6SAl Viro 	char *to;
31662ad94ae6SAl Viro 	int error;
31671da177e4SLinus Torvalds 
31682ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
31691da177e4SLinus Torvalds 	if (error)
31701da177e4SLinus Torvalds 		goto exit;
31711da177e4SLinus Torvalds 
31722ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
31731da177e4SLinus Torvalds 	if (error)
31741da177e4SLinus Torvalds 		goto exit1;
31751da177e4SLinus Torvalds 
31761da177e4SLinus Torvalds 	error = -EXDEV;
31774ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
31781da177e4SLinus Torvalds 		goto exit2;
31791da177e4SLinus Torvalds 
31804ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
31811da177e4SLinus Torvalds 	error = -EBUSY;
31821da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
31831da177e4SLinus Torvalds 		goto exit2;
31841da177e4SLinus Torvalds 
31854ac91378SJan Blunck 	new_dir = newnd.path.dentry;
31861da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
31871da177e4SLinus Torvalds 		goto exit2;
31881da177e4SLinus Torvalds 
31890612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
31900612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
31914e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
31920612d9fbSOGAWA Hirofumi 
31931da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
31941da177e4SLinus Torvalds 
319549705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
31961da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
31971da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
31981da177e4SLinus Torvalds 		goto exit3;
31991da177e4SLinus Torvalds 	/* source must exist */
32001da177e4SLinus Torvalds 	error = -ENOENT;
32011da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
32021da177e4SLinus Torvalds 		goto exit4;
32031da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
32041da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
32051da177e4SLinus Torvalds 		error = -ENOTDIR;
32061da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
32071da177e4SLinus Torvalds 			goto exit4;
32081da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
32091da177e4SLinus Torvalds 			goto exit4;
32101da177e4SLinus Torvalds 	}
32111da177e4SLinus Torvalds 	/* source should not be ancestor of target */
32121da177e4SLinus Torvalds 	error = -EINVAL;
32131da177e4SLinus Torvalds 	if (old_dentry == trap)
32141da177e4SLinus Torvalds 		goto exit4;
321549705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
32161da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
32171da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
32181da177e4SLinus Torvalds 		goto exit4;
32191da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
32201da177e4SLinus Torvalds 	error = -ENOTEMPTY;
32211da177e4SLinus Torvalds 	if (new_dentry == trap)
32221da177e4SLinus Torvalds 		goto exit5;
32231da177e4SLinus Torvalds 
32249079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
32259079b1ebSDave Hansen 	if (error)
32269079b1ebSDave Hansen 		goto exit5;
3227be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3228be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3229be6d3e56SKentaro Takeda 	if (error)
3230be6d3e56SKentaro Takeda 		goto exit6;
32311da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
32321da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3233be6d3e56SKentaro Takeda exit6:
32349079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
32351da177e4SLinus Torvalds exit5:
32361da177e4SLinus Torvalds 	dput(new_dentry);
32371da177e4SLinus Torvalds exit4:
32381da177e4SLinus Torvalds 	dput(old_dentry);
32391da177e4SLinus Torvalds exit3:
32401da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
32411da177e4SLinus Torvalds exit2:
32421d957f9bSJan Blunck 	path_put(&newnd.path);
32432ad94ae6SAl Viro 	putname(to);
32441da177e4SLinus Torvalds exit1:
32451d957f9bSJan Blunck 	path_put(&oldnd.path);
32461da177e4SLinus Torvalds 	putname(from);
32472ad94ae6SAl Viro exit:
32481da177e4SLinus Torvalds 	return error;
32491da177e4SLinus Torvalds }
32501da177e4SLinus Torvalds 
3251a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
32525590ff0dSUlrich Drepper {
32535590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
32545590ff0dSUlrich Drepper }
32555590ff0dSUlrich Drepper 
32561da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
32571da177e4SLinus Torvalds {
32581da177e4SLinus Torvalds 	int len;
32591da177e4SLinus Torvalds 
32601da177e4SLinus Torvalds 	len = PTR_ERR(link);
32611da177e4SLinus Torvalds 	if (IS_ERR(link))
32621da177e4SLinus Torvalds 		goto out;
32631da177e4SLinus Torvalds 
32641da177e4SLinus Torvalds 	len = strlen(link);
32651da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
32661da177e4SLinus Torvalds 		len = buflen;
32671da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
32681da177e4SLinus Torvalds 		len = -EFAULT;
32691da177e4SLinus Torvalds out:
32701da177e4SLinus Torvalds 	return len;
32711da177e4SLinus Torvalds }
32721da177e4SLinus Torvalds 
32731da177e4SLinus Torvalds /*
32741da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
32751da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
32761da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
32771da177e4SLinus Torvalds  */
32781da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
32791da177e4SLinus Torvalds {
32801da177e4SLinus Torvalds 	struct nameidata nd;
3281cc314eefSLinus Torvalds 	void *cookie;
3282694a1764SMarcin Slusarz 	int res;
3283cc314eefSLinus Torvalds 
32841da177e4SLinus Torvalds 	nd.depth = 0;
3285cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3286694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3287694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3288694a1764SMarcin Slusarz 
3289694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
32901da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3291cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3292694a1764SMarcin Slusarz 	return res;
32931da177e4SLinus Torvalds }
32941da177e4SLinus Torvalds 
32951da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
32961da177e4SLinus Torvalds {
32971da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
32981da177e4SLinus Torvalds }
32991da177e4SLinus Torvalds 
33001da177e4SLinus Torvalds /* get the link contents into pagecache */
33011da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
33021da177e4SLinus Torvalds {
3303ebd09abbSDuane Griffin 	char *kaddr;
33041da177e4SLinus Torvalds 	struct page *page;
33051da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3306090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
33071da177e4SLinus Torvalds 	if (IS_ERR(page))
33086fe6900eSNick Piggin 		return (char*)page;
33091da177e4SLinus Torvalds 	*ppage = page;
3310ebd09abbSDuane Griffin 	kaddr = kmap(page);
3311ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3312ebd09abbSDuane Griffin 	return kaddr;
33131da177e4SLinus Torvalds }
33141da177e4SLinus Torvalds 
33151da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
33161da177e4SLinus Torvalds {
33171da177e4SLinus Torvalds 	struct page *page = NULL;
33181da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
33191da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
33201da177e4SLinus Torvalds 	if (page) {
33211da177e4SLinus Torvalds 		kunmap(page);
33221da177e4SLinus Torvalds 		page_cache_release(page);
33231da177e4SLinus Torvalds 	}
33241da177e4SLinus Torvalds 	return res;
33251da177e4SLinus Torvalds }
33261da177e4SLinus Torvalds 
3327cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
33281da177e4SLinus Torvalds {
3329cc314eefSLinus Torvalds 	struct page *page = NULL;
33301da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3331cc314eefSLinus Torvalds 	return page;
33321da177e4SLinus Torvalds }
33331da177e4SLinus Torvalds 
3334cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
33351da177e4SLinus Torvalds {
3336cc314eefSLinus Torvalds 	struct page *page = cookie;
3337cc314eefSLinus Torvalds 
3338cc314eefSLinus Torvalds 	if (page) {
33391da177e4SLinus Torvalds 		kunmap(page);
33401da177e4SLinus Torvalds 		page_cache_release(page);
33411da177e4SLinus Torvalds 	}
33421da177e4SLinus Torvalds }
33431da177e4SLinus Torvalds 
334454566b2cSNick Piggin /*
334554566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
334654566b2cSNick Piggin  */
334754566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
33481da177e4SLinus Torvalds {
33491da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
33500adb25d2SKirill Korotaev 	struct page *page;
3351afddba49SNick Piggin 	void *fsdata;
3352beb497abSDmitriy Monakhov 	int err;
33531da177e4SLinus Torvalds 	char *kaddr;
335454566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
335554566b2cSNick Piggin 	if (nofs)
335654566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
33571da177e4SLinus Torvalds 
33587e53cac4SNeilBrown retry:
3359afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
336054566b2cSNick Piggin 				flags, &page, &fsdata);
33611da177e4SLinus Torvalds 	if (err)
3362afddba49SNick Piggin 		goto fail;
3363afddba49SNick Piggin 
33641da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
33651da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
33661da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
3367afddba49SNick Piggin 
3368afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3369afddba49SNick Piggin 							page, fsdata);
33701da177e4SLinus Torvalds 	if (err < 0)
33711da177e4SLinus Torvalds 		goto fail;
3372afddba49SNick Piggin 	if (err < len-1)
3373afddba49SNick Piggin 		goto retry;
3374afddba49SNick Piggin 
33751da177e4SLinus Torvalds 	mark_inode_dirty(inode);
33761da177e4SLinus Torvalds 	return 0;
33771da177e4SLinus Torvalds fail:
33781da177e4SLinus Torvalds 	return err;
33791da177e4SLinus Torvalds }
33801da177e4SLinus Torvalds 
33810adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
33820adb25d2SKirill Korotaev {
33830adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
338454566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
33850adb25d2SKirill Korotaev }
33860adb25d2SKirill Korotaev 
338792e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
33881da177e4SLinus Torvalds 	.readlink	= generic_readlink,
33891da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
33901da177e4SLinus Torvalds 	.put_link	= page_put_link,
33911da177e4SLinus Torvalds };
33921da177e4SLinus Torvalds 
33932d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
3394cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
33951da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
33961da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
33971da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
33981da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
33991da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
34001da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
34011da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
34021da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
34031da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
34040adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
34051da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
34061da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
3407c9c6cac0SAl Viro EXPORT_SYMBOL(kern_path_parent);
3408d1811465SAl Viro EXPORT_SYMBOL(kern_path);
340916f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3410f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
34118c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission);
34121da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
34131da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
34141da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
34151da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
34161da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
34171da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
34181da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
34191da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
34201da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
34211da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
34221da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
34231da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
34241da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
34251da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3426