xref: /openbmc/linux/fs/namei.c (revision 5a18fff2)
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 {
501a7472babSAl Viro 	if (nd->flags & LOOKUP_RCU) {
502a7472babSAl Viro 		if (unlikely(nameidata_dentry_drop_rcu(nd, dentry))) {
503a7472babSAl Viro 			nd->flags &= ~LOOKUP_RCU;
504a7472babSAl Viro 			nd->root.mnt = NULL;
505a7472babSAl Viro 			rcu_read_unlock();
506a7472babSAl Viro 			br_read_unlock(vfsmount_lock);
507a7472babSAl Viro 			return -ECHILD;
508a7472babSAl Viro 		}
509a7472babSAl Viro 	}
51031e6b01fSNick Piggin 	return 0;
51131e6b01fSNick Piggin }
51231e6b01fSNick Piggin 
51331e6b01fSNick Piggin /**
51431e6b01fSNick Piggin  * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk
51531e6b01fSNick Piggin  * @nd: nameidata pathwalk data to drop
51639191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
51731e6b01fSNick Piggin  *
51831e6b01fSNick Piggin  * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk.
51931e6b01fSNick Piggin  * nd->path should be the final element of the lookup, so nd->root is discarded.
52031e6b01fSNick Piggin  * Must be called from rcu-walk context.
52131e6b01fSNick Piggin  */
52231e6b01fSNick Piggin static int nameidata_drop_rcu_last(struct nameidata *nd)
52331e6b01fSNick Piggin {
52431e6b01fSNick Piggin 	struct dentry *dentry = nd->path.dentry;
52531e6b01fSNick Piggin 
52631e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
52731e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
52831e6b01fSNick Piggin 	nd->root.mnt = NULL;
52931e6b01fSNick Piggin 	spin_lock(&dentry->d_lock);
53031e6b01fSNick Piggin 	if (!__d_rcu_to_refcount(dentry, nd->seq))
53131e6b01fSNick Piggin 		goto err_unlock;
53231e6b01fSNick Piggin 	BUG_ON(nd->inode != dentry->d_inode);
53331e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
53431e6b01fSNick Piggin 
53531e6b01fSNick Piggin 	mntget(nd->path.mnt);
53631e6b01fSNick Piggin 
53731e6b01fSNick Piggin 	rcu_read_unlock();
53831e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
53931e6b01fSNick Piggin 
54031e6b01fSNick Piggin 	return 0;
54131e6b01fSNick Piggin 
54231e6b01fSNick Piggin err_unlock:
54331e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
54431e6b01fSNick Piggin 	rcu_read_unlock();
54531e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
54631e6b01fSNick Piggin 	return -ECHILD;
54731e6b01fSNick Piggin }
54831e6b01fSNick Piggin 
54931e6b01fSNick Piggin /**
550834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
551834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
552834f2a4aSTrond Myklebust  */
553834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
554834f2a4aSTrond Myklebust {
5552dab5974SLinus Torvalds 	struct file *file = nd->intent.open.file;
5562dab5974SLinus Torvalds 
5572dab5974SLinus Torvalds 	if (file && !IS_ERR(file)) {
5582dab5974SLinus Torvalds 		if (file->f_path.dentry == NULL)
5592dab5974SLinus Torvalds 			put_filp(file);
560834f2a4aSTrond Myklebust 		else
5612dab5974SLinus Torvalds 			fput(file);
5622dab5974SLinus Torvalds 	}
563834f2a4aSTrond Myklebust }
564834f2a4aSTrond Myklebust 
565f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
56634286d66SNick Piggin {
567f60aef7eSAl Viro 	return dentry->d_op->d_revalidate(dentry, nd);
56834286d66SNick Piggin }
56934286d66SNick Piggin 
570f5e1c1c1SAl Viro static struct dentry *
571bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd)
572bcdc5e01SIan Kent {
573f5e1c1c1SAl Viro 	int status = d_revalidate(dentry, nd);
574bcdc5e01SIan Kent 	if (unlikely(status <= 0)) {
575bcdc5e01SIan Kent 		/*
576bcdc5e01SIan Kent 		 * The dentry failed validation.
577bcdc5e01SIan Kent 		 * If d_revalidate returned 0 attempt to invalidate
578bcdc5e01SIan Kent 		 * the dentry otherwise d_revalidate is asking us
579bcdc5e01SIan Kent 		 * to return a fail status.
580bcdc5e01SIan Kent 		 */
58134286d66SNick Piggin 		if (status < 0) {
58234286d66SNick Piggin 			dput(dentry);
58334286d66SNick Piggin 			dentry = ERR_PTR(status);
584f5e1c1c1SAl Viro 		} else if (!d_invalidate(dentry)) {
585bcdc5e01SIan Kent 			dput(dentry);
586bcdc5e01SIan Kent 			dentry = NULL;
587bcdc5e01SIan Kent 		}
588bcdc5e01SIan Kent 	}
589f5e1c1c1SAl Viro 	return dentry;
590f5e1c1c1SAl Viro }
591f5e1c1c1SAl Viro 
5921da177e4SLinus Torvalds /*
59316c2cd71SAl Viro  * handle_reval_path - force revalidation of a dentry
59439159de2SJeff Layton  *
59539159de2SJeff Layton  * In some situations the path walking code will trust dentries without
59639159de2SJeff Layton  * revalidating them. This causes problems for filesystems that depend on
59739159de2SJeff Layton  * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
59839159de2SJeff Layton  * (which indicates that it's possible for the dentry to go stale), force
59939159de2SJeff Layton  * a d_revalidate call before proceeding.
60039159de2SJeff Layton  *
60139159de2SJeff Layton  * Returns 0 if the revalidation was successful. If the revalidation fails,
60239159de2SJeff Layton  * either return the error returned by d_revalidate or -ESTALE if the
60339159de2SJeff Layton  * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
60439159de2SJeff Layton  * invalidate the dentry. It's up to the caller to handle putting references
60539159de2SJeff Layton  * to the path if necessary.
60639159de2SJeff Layton  */
60716c2cd71SAl Viro static inline int handle_reval_path(struct nameidata *nd)
60839159de2SJeff Layton {
60916c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
61039159de2SJeff Layton 	int status;
61139159de2SJeff Layton 
61216c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
61339159de2SJeff Layton 		return 0;
61439159de2SJeff Layton 
61516c2cd71SAl Viro 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
61616c2cd71SAl Viro 		return 0;
61716c2cd71SAl Viro 
61816c2cd71SAl Viro 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
61916c2cd71SAl Viro 		return 0;
62016c2cd71SAl Viro 
62116c2cd71SAl Viro 	/* Note: we do not d_invalidate() */
62234286d66SNick Piggin 	status = d_revalidate(dentry, nd);
62339159de2SJeff Layton 	if (status > 0)
62439159de2SJeff Layton 		return 0;
62539159de2SJeff Layton 
62616c2cd71SAl Viro 	if (!status)
62739159de2SJeff Layton 		status = -ESTALE;
62816c2cd71SAl Viro 
62939159de2SJeff Layton 	return status;
63039159de2SJeff Layton }
63139159de2SJeff Layton 
63239159de2SJeff Layton /*
633b75b5086SAl Viro  * Short-cut version of permission(), for calling on directories
634b75b5086SAl Viro  * during pathname resolution.  Combines parts of permission()
635b75b5086SAl Viro  * and generic_permission(), and tests ONLY for MAY_EXEC permission.
6361da177e4SLinus Torvalds  *
6371da177e4SLinus Torvalds  * If appropriate, check DAC only.  If not appropriate, or
638b75b5086SAl Viro  * short-cut DAC fails, then call ->permission() to do more
6391da177e4SLinus Torvalds  * complete permission check.
6401da177e4SLinus Torvalds  */
641b74c79e9SNick Piggin static inline int exec_permission(struct inode *inode, unsigned int flags)
6421da177e4SLinus Torvalds {
6435909ccaaSLinus Torvalds 	int ret;
6441da177e4SLinus Torvalds 
645cb9179eaSLinus Torvalds 	if (inode->i_op->permission) {
646b74c79e9SNick Piggin 		ret = inode->i_op->permission(inode, MAY_EXEC, flags);
647b74c79e9SNick Piggin 	} else {
648b74c79e9SNick Piggin 		ret = acl_permission_check(inode, MAY_EXEC, flags,
649b74c79e9SNick Piggin 				inode->i_op->check_acl);
650cb9179eaSLinus Torvalds 	}
651b74c79e9SNick Piggin 	if (likely(!ret))
6521da177e4SLinus Torvalds 		goto ok;
653b74c79e9SNick Piggin 	if (ret == -ECHILD)
65431e6b01fSNick Piggin 		return ret;
6551da177e4SLinus Torvalds 
656f1ac9f6bSLinus Torvalds 	if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH))
6571da177e4SLinus Torvalds 		goto ok;
6581da177e4SLinus Torvalds 
6595909ccaaSLinus Torvalds 	return ret;
6601da177e4SLinus Torvalds ok:
661b74c79e9SNick Piggin 	return security_inode_exec_permission(inode, flags);
6621da177e4SLinus Torvalds }
6631da177e4SLinus Torvalds 
6642a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
6652a737871SAl Viro {
666f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
667f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
6682a737871SAl Viro }
6692a737871SAl Viro 
6706de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
6716de88d72SAl Viro 
67231e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
67331e6b01fSNick Piggin {
67431e6b01fSNick Piggin 	if (!nd->root.mnt) {
67531e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
676c28cc364SNick Piggin 		unsigned seq;
677c28cc364SNick Piggin 
678c28cc364SNick Piggin 		do {
679c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
68031e6b01fSNick Piggin 			nd->root = fs->root;
681c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
68231e6b01fSNick Piggin 	}
68331e6b01fSNick Piggin }
68431e6b01fSNick Piggin 
685f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
6861da177e4SLinus Torvalds {
68731e6b01fSNick Piggin 	int ret;
68831e6b01fSNick Piggin 
6891da177e4SLinus Torvalds 	if (IS_ERR(link))
6901da177e4SLinus Torvalds 		goto fail;
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	if (*link == '/') {
6932a737871SAl Viro 		set_root(nd);
6941d957f9bSJan Blunck 		path_put(&nd->path);
6952a737871SAl Viro 		nd->path = nd->root;
6962a737871SAl Viro 		path_get(&nd->root);
69716c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
6981da177e4SLinus Torvalds 	}
69931e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
700b4091d5fSChristoph Hellwig 
70131e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
70231e6b01fSNick Piggin 	return ret;
7031da177e4SLinus Torvalds fail:
7041d957f9bSJan Blunck 	path_put(&nd->path);
7051da177e4SLinus Torvalds 	return PTR_ERR(link);
7061da177e4SLinus Torvalds }
7071da177e4SLinus Torvalds 
7081d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
709051d3812SIan Kent {
710051d3812SIan Kent 	dput(path->dentry);
7114ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
712051d3812SIan Kent 		mntput(path->mnt);
713051d3812SIan Kent }
714051d3812SIan Kent 
7157b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
7167b9337aaSNick Piggin 					struct nameidata *nd)
717051d3812SIan Kent {
71831e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
7194ac91378SJan Blunck 		dput(nd->path.dentry);
72031e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
7214ac91378SJan Blunck 			mntput(nd->path.mnt);
7229a229683SHuang Shijie 	}
72331e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
7244ac91378SJan Blunck 	nd->path.dentry = path->dentry;
725051d3812SIan Kent }
726051d3812SIan Kent 
727def4af30SAl Viro static __always_inline int
7287b9337aaSNick Piggin __do_follow_link(const struct path *link, struct nameidata *nd, void **p)
7291da177e4SLinus Torvalds {
7301da177e4SLinus Torvalds 	int error;
7317b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
7321da177e4SLinus Torvalds 
733844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
734844a3917SAl Viro 
7357b9337aaSNick Piggin 	touch_atime(link->mnt, dentry);
7361da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
737cd4e91d3SAl Viro 
73887556ef1SDavid Howells 	if (link->mnt == nd->path.mnt)
7397b9337aaSNick Piggin 		mntget(link->mnt);
74031e6b01fSNick Piggin 
74136f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
74236f3b4f6SAl Viro 	if (error) {
74336f3b4f6SAl Viro 		*p = ERR_PTR(error); /* no ->put_link(), please */
74436f3b4f6SAl Viro 		path_put(&nd->path);
74536f3b4f6SAl Viro 		return error;
74636f3b4f6SAl Viro 	}
74736f3b4f6SAl Viro 
74886acdca1SAl Viro 	nd->last_type = LAST_BIND;
749def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
750def4af30SAl Viro 	error = PTR_ERR(*p);
751def4af30SAl Viro 	if (!IS_ERR(*p)) {
7521da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
753cc314eefSLinus Torvalds 		error = 0;
7541da177e4SLinus Torvalds 		if (s)
7551da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
75616c2cd71SAl Viro 		else if (nd->last_type == LAST_BIND)
75716c2cd71SAl Viro 			nd->flags |= LOOKUP_JUMPED;
7581da177e4SLinus Torvalds 	}
7591da177e4SLinus Torvalds 	return error;
7601da177e4SLinus Torvalds }
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds /*
7631da177e4SLinus Torvalds  * This limits recursive symlink follows to 8, while
7641da177e4SLinus Torvalds  * limiting consecutive symlinks to 40.
7651da177e4SLinus Torvalds  *
7661da177e4SLinus Torvalds  * Without that kind of total limit, nasty chains of consecutive
7671da177e4SLinus Torvalds  * symlinks can cause almost arbitrarily long lookups.
7681da177e4SLinus Torvalds  */
7693abb17e8SLinus Torvalds static inline int do_follow_link(struct inode *inode, struct path *path, struct nameidata *nd)
7701da177e4SLinus Torvalds {
771def4af30SAl Viro 	void *cookie;
7721da177e4SLinus Torvalds 	int err = -ELOOP;
773844a3917SAl Viro 
774844a3917SAl Viro 	/* We drop rcu-walk here */
775844a3917SAl Viro 	if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry))
776844a3917SAl Viro 		return -ECHILD;
7773abb17e8SLinus Torvalds 	BUG_ON(inode != path->dentry->d_inode);
778844a3917SAl Viro 
7791da177e4SLinus Torvalds 	if (current->link_count >= MAX_NESTED_LINKS)
7801da177e4SLinus Torvalds 		goto loop;
7811da177e4SLinus Torvalds 	if (current->total_link_count >= 40)
7821da177e4SLinus Torvalds 		goto loop;
7831da177e4SLinus Torvalds 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
7841da177e4SLinus Torvalds 	cond_resched();
7851da177e4SLinus Torvalds 	current->link_count++;
7861da177e4SLinus Torvalds 	current->total_link_count++;
7871da177e4SLinus Torvalds 	nd->depth++;
788def4af30SAl Viro 	err = __do_follow_link(path, nd, &cookie);
789def4af30SAl Viro 	if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link)
790def4af30SAl Viro 		path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie);
791258fa999SAl Viro 	path_put(path);
7921da177e4SLinus Torvalds 	current->link_count--;
7931da177e4SLinus Torvalds 	nd->depth--;
7941da177e4SLinus Torvalds 	return err;
7951da177e4SLinus Torvalds loop:
7961d957f9bSJan Blunck 	path_put_conditional(path, nd);
7971d957f9bSJan Blunck 	path_put(&nd->path);
7981da177e4SLinus Torvalds 	return err;
7991da177e4SLinus Torvalds }
8001da177e4SLinus Torvalds 
80131e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
80231e6b01fSNick Piggin {
80331e6b01fSNick Piggin 	struct vfsmount *parent;
80431e6b01fSNick Piggin 	struct dentry *mountpoint;
80531e6b01fSNick Piggin 
80631e6b01fSNick Piggin 	parent = path->mnt->mnt_parent;
80731e6b01fSNick Piggin 	if (parent == path->mnt)
80831e6b01fSNick Piggin 		return 0;
80931e6b01fSNick Piggin 	mountpoint = path->mnt->mnt_mountpoint;
81031e6b01fSNick Piggin 	path->dentry = mountpoint;
81131e6b01fSNick Piggin 	path->mnt = parent;
81231e6b01fSNick Piggin 	return 1;
81331e6b01fSNick Piggin }
81431e6b01fSNick Piggin 
815bab77ebfSAl Viro int follow_up(struct path *path)
8161da177e4SLinus Torvalds {
8171da177e4SLinus Torvalds 	struct vfsmount *parent;
8181da177e4SLinus Torvalds 	struct dentry *mountpoint;
81999b7db7bSNick Piggin 
82099b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
821bab77ebfSAl Viro 	parent = path->mnt->mnt_parent;
822bab77ebfSAl Viro 	if (parent == path->mnt) {
82399b7db7bSNick Piggin 		br_read_unlock(vfsmount_lock);
8241da177e4SLinus Torvalds 		return 0;
8251da177e4SLinus Torvalds 	}
8261da177e4SLinus Torvalds 	mntget(parent);
827bab77ebfSAl Viro 	mountpoint = dget(path->mnt->mnt_mountpoint);
82899b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
829bab77ebfSAl Viro 	dput(path->dentry);
830bab77ebfSAl Viro 	path->dentry = mountpoint;
831bab77ebfSAl Viro 	mntput(path->mnt);
832bab77ebfSAl Viro 	path->mnt = parent;
8331da177e4SLinus Torvalds 	return 1;
8341da177e4SLinus Torvalds }
8351da177e4SLinus Torvalds 
836b5c84bf6SNick Piggin /*
8379875cf80SDavid Howells  * Perform an automount
8389875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
8399875cf80SDavid Howells  *   were called with.
8401da177e4SLinus Torvalds  */
8419875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
8429875cf80SDavid Howells 			    bool *need_mntput)
84331e6b01fSNick Piggin {
8449875cf80SDavid Howells 	struct vfsmount *mnt;
845ea5b778aSDavid Howells 	int err;
8469875cf80SDavid Howells 
8479875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
8489875cf80SDavid Howells 		return -EREMOTE;
8499875cf80SDavid Howells 
8506f45b656SDavid Howells 	/* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
8516f45b656SDavid Howells 	 * and this is the terminal part of the path.
8526f45b656SDavid Howells 	 */
8536f45b656SDavid Howells 	if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
8546f45b656SDavid Howells 		return -EISDIR; /* we actually want to stop here */
8556f45b656SDavid Howells 
8569875cf80SDavid Howells 	/* We want to mount if someone is trying to open/create a file of any
8579875cf80SDavid Howells 	 * type under the mountpoint, wants to traverse through the mountpoint
8589875cf80SDavid Howells 	 * or wants to open the mounted directory.
8599875cf80SDavid Howells 	 *
8609875cf80SDavid Howells 	 * We don't want to mount if someone's just doing a stat and they've
8619875cf80SDavid Howells 	 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
8629875cf80SDavid Howells 	 * appended a '/' to the name.
8639875cf80SDavid Howells 	 */
8649875cf80SDavid Howells 	if (!(flags & LOOKUP_FOLLOW) &&
8659875cf80SDavid Howells 	    !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
8669875cf80SDavid Howells 		       LOOKUP_OPEN | LOOKUP_CREATE)))
8679875cf80SDavid Howells 		return -EISDIR;
8689875cf80SDavid Howells 
8699875cf80SDavid Howells 	current->total_link_count++;
8709875cf80SDavid Howells 	if (current->total_link_count >= 40)
8719875cf80SDavid Howells 		return -ELOOP;
8729875cf80SDavid Howells 
8739875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
8749875cf80SDavid Howells 	if (IS_ERR(mnt)) {
8759875cf80SDavid Howells 		/*
8769875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
8779875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
8789875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
8799875cf80SDavid Howells 		 *
8809875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
8819875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
8829875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
8839875cf80SDavid Howells 		 */
8849875cf80SDavid Howells 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
8859875cf80SDavid Howells 			return -EREMOTE;
8869875cf80SDavid Howells 		return PTR_ERR(mnt);
88731e6b01fSNick Piggin 	}
888ea5b778aSDavid Howells 
8899875cf80SDavid Howells 	if (!mnt) /* mount collision */
8909875cf80SDavid Howells 		return 0;
8919875cf80SDavid Howells 
89219a167afSAl Viro 	err = finish_automount(mnt, path);
893ea5b778aSDavid Howells 
894ea5b778aSDavid Howells 	switch (err) {
895ea5b778aSDavid Howells 	case -EBUSY:
896ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
89719a167afSAl Viro 		return 0;
898ea5b778aSDavid Howells 	case 0:
899463ffb2eSAl Viro 		dput(path->dentry);
9009875cf80SDavid Howells 		if (*need_mntput)
9019875cf80SDavid Howells 			mntput(path->mnt);
9029875cf80SDavid Howells 		path->mnt = mnt;
9039875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
9049875cf80SDavid Howells 		*need_mntput = true;
9059875cf80SDavid Howells 		return 0;
90619a167afSAl Viro 	default:
90719a167afSAl Viro 		return err;
9089875cf80SDavid Howells 	}
90919a167afSAl Viro 
910ea5b778aSDavid Howells }
9119875cf80SDavid Howells 
9129875cf80SDavid Howells /*
9139875cf80SDavid Howells  * Handle a dentry that is managed in some way.
914cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
9159875cf80SDavid Howells  * - Flagged as mountpoint
9169875cf80SDavid Howells  * - Flagged as automount point
9179875cf80SDavid Howells  *
9189875cf80SDavid Howells  * This may only be called in refwalk mode.
9199875cf80SDavid Howells  *
9209875cf80SDavid Howells  * Serialization is taken care of in namespace.c
9219875cf80SDavid Howells  */
9229875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
9239875cf80SDavid Howells {
9249875cf80SDavid Howells 	unsigned managed;
9259875cf80SDavid Howells 	bool need_mntput = false;
9269875cf80SDavid Howells 	int ret;
9279875cf80SDavid Howells 
9289875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
9299875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
9309875cf80SDavid Howells 	 * the components of that value change under us */
9319875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
9329875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
9339875cf80SDavid Howells 	       unlikely(managed != 0)) {
934cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
935cc53ce53SDavid Howells 		 * being held. */
936cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
937cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
938cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
939ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(path->dentry,
940ab90911fSDavid Howells 							   false, false);
941cc53ce53SDavid Howells 			if (ret < 0)
942cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
943cc53ce53SDavid Howells 		}
944cc53ce53SDavid Howells 
9459875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
9469875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
9479875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
9489875cf80SDavid Howells 			if (mounted) {
9499875cf80SDavid Howells 				dput(path->dentry);
9509875cf80SDavid Howells 				if (need_mntput)
951463ffb2eSAl Viro 					mntput(path->mnt);
952463ffb2eSAl Viro 				path->mnt = mounted;
953463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
9549875cf80SDavid Howells 				need_mntput = true;
9559875cf80SDavid Howells 				continue;
956463ffb2eSAl Viro 			}
957463ffb2eSAl Viro 
9589875cf80SDavid Howells 			/* Something is mounted on this dentry in another
9599875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
9609875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
9619875cf80SDavid Howells 			 * vfsmount_lock */
9621da177e4SLinus Torvalds 		}
9639875cf80SDavid Howells 
9649875cf80SDavid Howells 		/* Handle an automount point */
9659875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
9669875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
9679875cf80SDavid Howells 			if (ret < 0)
9689875cf80SDavid Howells 				return ret == -EISDIR ? 0 : ret;
9699875cf80SDavid Howells 			continue;
9709875cf80SDavid Howells 		}
9719875cf80SDavid Howells 
9729875cf80SDavid Howells 		/* We didn't change the current path point */
9739875cf80SDavid Howells 		break;
9749875cf80SDavid Howells 	}
9759875cf80SDavid Howells 	return 0;
9761da177e4SLinus Torvalds }
9771da177e4SLinus Torvalds 
978cc53ce53SDavid Howells int follow_down_one(struct path *path)
9791da177e4SLinus Torvalds {
9801da177e4SLinus Torvalds 	struct vfsmount *mounted;
9811da177e4SLinus Torvalds 
9821c755af4SAl Viro 	mounted = lookup_mnt(path);
9831da177e4SLinus Torvalds 	if (mounted) {
9849393bd07SAl Viro 		dput(path->dentry);
9859393bd07SAl Viro 		mntput(path->mnt);
9869393bd07SAl Viro 		path->mnt = mounted;
9879393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
9881da177e4SLinus Torvalds 		return 1;
9891da177e4SLinus Torvalds 	}
9901da177e4SLinus Torvalds 	return 0;
9911da177e4SLinus Torvalds }
9921da177e4SLinus Torvalds 
9939875cf80SDavid Howells /*
9949875cf80SDavid Howells  * Skip to top of mountpoint pile in rcuwalk mode.  We abort the rcu-walk if we
995cc53ce53SDavid Howells  * meet a managed dentry and we're not walking to "..".  True is returned to
9969875cf80SDavid Howells  * continue, false to abort.
9979875cf80SDavid Howells  */
9989875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
9999875cf80SDavid Howells 			       struct inode **inode, bool reverse_transit)
10009875cf80SDavid Howells {
10019875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
10029875cf80SDavid Howells 		struct vfsmount *mounted;
1003ab90911fSDavid Howells 		if (unlikely(path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) &&
1004ab90911fSDavid Howells 		    !reverse_transit &&
1005ab90911fSDavid Howells 		    path->dentry->d_op->d_manage(path->dentry, false, true) < 0)
1006ab90911fSDavid Howells 			return false;
10079875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
10089875cf80SDavid Howells 		if (!mounted)
10099875cf80SDavid Howells 			break;
10109875cf80SDavid Howells 		path->mnt = mounted;
10119875cf80SDavid Howells 		path->dentry = mounted->mnt_root;
10129875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
10139875cf80SDavid Howells 		*inode = path->dentry->d_inode;
10149875cf80SDavid Howells 	}
10159875cf80SDavid Howells 
10169875cf80SDavid Howells 	if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
10179875cf80SDavid Howells 		return reverse_transit;
10189875cf80SDavid Howells 	return true;
10199875cf80SDavid Howells }
10209875cf80SDavid Howells 
102131e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
102231e6b01fSNick Piggin {
102331e6b01fSNick Piggin 	struct inode *inode = nd->inode;
102431e6b01fSNick Piggin 
102531e6b01fSNick Piggin 	set_root_rcu(nd);
102631e6b01fSNick Piggin 
102731e6b01fSNick Piggin 	while (1) {
102831e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
102931e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
103031e6b01fSNick Piggin 			break;
103131e6b01fSNick Piggin 		}
103231e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
103331e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
103431e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
103531e6b01fSNick Piggin 			unsigned seq;
103631e6b01fSNick Piggin 
103731e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
103831e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
1039ef7562d5SAl Viro 				goto failed;
104031e6b01fSNick Piggin 			inode = parent->d_inode;
104131e6b01fSNick Piggin 			nd->path.dentry = parent;
104231e6b01fSNick Piggin 			nd->seq = seq;
104331e6b01fSNick Piggin 			break;
104431e6b01fSNick Piggin 		}
104531e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
104631e6b01fSNick Piggin 			break;
104731e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
104831e6b01fSNick Piggin 		inode = nd->path.dentry->d_inode;
104931e6b01fSNick Piggin 	}
10509875cf80SDavid Howells 	__follow_mount_rcu(nd, &nd->path, &inode, true);
105131e6b01fSNick Piggin 	nd->inode = inode;
105231e6b01fSNick Piggin 	return 0;
1053ef7562d5SAl Viro 
1054ef7562d5SAl Viro failed:
1055ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
1056ef7562d5SAl Viro 	nd->root.mnt = NULL;
1057ef7562d5SAl Viro 	rcu_read_unlock();
1058ef7562d5SAl Viro 	br_read_unlock(vfsmount_lock);
1059ef7562d5SAl Viro 	return -ECHILD;
106031e6b01fSNick Piggin }
106131e6b01fSNick Piggin 
10629875cf80SDavid Howells /*
1063cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
1064cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
1065cc53ce53SDavid Howells  * caller is permitted to proceed or not.
1066cc53ce53SDavid Howells  *
1067cc53ce53SDavid Howells  * Care must be taken as namespace_sem may be held (indicated by mounting_here
1068cc53ce53SDavid Howells  * being true).
1069cc53ce53SDavid Howells  */
1070cc53ce53SDavid Howells int follow_down(struct path *path, bool mounting_here)
1071cc53ce53SDavid Howells {
1072cc53ce53SDavid Howells 	unsigned managed;
1073cc53ce53SDavid Howells 	int ret;
1074cc53ce53SDavid Howells 
1075cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
1076cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1077cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1078cc53ce53SDavid Howells 		 * being held.
1079cc53ce53SDavid Howells 		 *
1080cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
1081cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
1082cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
1083cc53ce53SDavid Howells 		 * superstructure.
1084cc53ce53SDavid Howells 		 *
1085cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
1086cc53ce53SDavid Howells 		 */
1087cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
1088cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1089cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
1090ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
1091ab90911fSDavid Howells 				path->dentry, mounting_here, false);
1092cc53ce53SDavid Howells 			if (ret < 0)
1093cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
1094cc53ce53SDavid Howells 		}
1095cc53ce53SDavid Howells 
1096cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
1097cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
1098cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
1099cc53ce53SDavid Howells 			if (!mounted)
1100cc53ce53SDavid Howells 				break;
1101cc53ce53SDavid Howells 			dput(path->dentry);
1102cc53ce53SDavid Howells 			mntput(path->mnt);
1103cc53ce53SDavid Howells 			path->mnt = mounted;
1104cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1105cc53ce53SDavid Howells 			continue;
1106cc53ce53SDavid Howells 		}
1107cc53ce53SDavid Howells 
1108cc53ce53SDavid Howells 		/* Don't handle automount points here */
1109cc53ce53SDavid Howells 		break;
1110cc53ce53SDavid Howells 	}
1111cc53ce53SDavid Howells 	return 0;
1112cc53ce53SDavid Howells }
1113cc53ce53SDavid Howells 
1114cc53ce53SDavid Howells /*
11159875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
11169875cf80SDavid Howells  */
11179875cf80SDavid Howells static void follow_mount(struct path *path)
11189875cf80SDavid Howells {
11199875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
11209875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
11219875cf80SDavid Howells 		if (!mounted)
11229875cf80SDavid Howells 			break;
11239875cf80SDavid Howells 		dput(path->dentry);
11249875cf80SDavid Howells 		mntput(path->mnt);
11259875cf80SDavid Howells 		path->mnt = mounted;
11269875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
11279875cf80SDavid Howells 	}
11289875cf80SDavid Howells }
11299875cf80SDavid Howells 
113031e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
11311da177e4SLinus Torvalds {
11322a737871SAl Viro 	set_root(nd);
1133e518ddb7SAndreas Mohr 
11341da177e4SLinus Torvalds 	while(1) {
11354ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
11361da177e4SLinus Torvalds 
11372a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
11382a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
11391da177e4SLinus Torvalds 			break;
11401da177e4SLinus Torvalds 		}
11414ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
11423088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
11433088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
11441da177e4SLinus Torvalds 			dput(old);
11451da177e4SLinus Torvalds 			break;
11461da177e4SLinus Torvalds 		}
11473088dd70SAl Viro 		if (!follow_up(&nd->path))
11481da177e4SLinus Torvalds 			break;
11491da177e4SLinus Torvalds 	}
115079ed0226SAl Viro 	follow_mount(&nd->path);
115131e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
11521da177e4SLinus Torvalds }
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds /*
1155baa03890SNick Piggin  * Allocate a dentry with name and parent, and perform a parent
1156baa03890SNick Piggin  * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1157baa03890SNick Piggin  * on error. parent->d_inode->i_mutex must be held. d_lookup must
1158baa03890SNick Piggin  * have verified that no child exists while under i_mutex.
1159baa03890SNick Piggin  */
1160baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1161baa03890SNick Piggin 				struct qstr *name, struct nameidata *nd)
1162baa03890SNick Piggin {
1163baa03890SNick Piggin 	struct inode *inode = parent->d_inode;
1164baa03890SNick Piggin 	struct dentry *dentry;
1165baa03890SNick Piggin 	struct dentry *old;
1166baa03890SNick Piggin 
1167baa03890SNick Piggin 	/* Don't create child dentry for a dead directory. */
1168baa03890SNick Piggin 	if (unlikely(IS_DEADDIR(inode)))
1169baa03890SNick Piggin 		return ERR_PTR(-ENOENT);
1170baa03890SNick Piggin 
1171baa03890SNick Piggin 	dentry = d_alloc(parent, name);
1172baa03890SNick Piggin 	if (unlikely(!dentry))
1173baa03890SNick Piggin 		return ERR_PTR(-ENOMEM);
1174baa03890SNick Piggin 
1175baa03890SNick Piggin 	old = inode->i_op->lookup(inode, dentry, nd);
1176baa03890SNick Piggin 	if (unlikely(old)) {
1177baa03890SNick Piggin 		dput(dentry);
1178baa03890SNick Piggin 		dentry = old;
1179baa03890SNick Piggin 	}
1180baa03890SNick Piggin 	return dentry;
1181baa03890SNick Piggin }
1182baa03890SNick Piggin 
1183baa03890SNick Piggin /*
11841da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
11851da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
11861da177e4SLinus Torvalds  *  It _is_ time-critical.
11871da177e4SLinus Torvalds  */
11881da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
118931e6b01fSNick Piggin 			struct path *path, struct inode **inode)
11901da177e4SLinus Torvalds {
11914ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
119231e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
11935a18fff2SAl Viro 	int need_reval = 1;
11945a18fff2SAl Viro 	int status = 1;
11959875cf80SDavid Howells 	int err;
11969875cf80SDavid Howells 
11973cac260aSAl Viro 	/*
1198b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1199b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1200b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1201b04f784eSNick Piggin 	 */
120231e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
120331e6b01fSNick Piggin 		unsigned seq;
120431e6b01fSNick Piggin 		*inode = nd->inode;
120531e6b01fSNick Piggin 		dentry = __d_lookup_rcu(parent, name, &seq, inode);
12065a18fff2SAl Viro 		if (!dentry)
12075a18fff2SAl Viro 			goto unlazy;
12085a18fff2SAl Viro 
120931e6b01fSNick Piggin 		/* Memory barrier in read_seqcount_begin of child is enough */
121031e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
121131e6b01fSNick Piggin 			return -ECHILD;
121231e6b01fSNick Piggin 		nd->seq = seq;
12135a18fff2SAl Viro 
121424643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
12155a18fff2SAl Viro 			status = d_revalidate(dentry, nd);
12165a18fff2SAl Viro 			if (unlikely(status <= 0)) {
12175a18fff2SAl Viro 				if (status != -ECHILD)
12185a18fff2SAl Viro 					need_reval = 0;
12195a18fff2SAl Viro 				goto unlazy;
12205a18fff2SAl Viro 			}
122124643087SAl Viro 		}
122231e6b01fSNick Piggin 		path->mnt = mnt;
122331e6b01fSNick Piggin 		path->dentry = dentry;
12249875cf80SDavid Howells 		if (likely(__follow_mount_rcu(nd, path, inode, false)))
12259875cf80SDavid Howells 			return 0;
12265a18fff2SAl Viro unlazy:
12275a18fff2SAl Viro 		if (dentry) {
12285a18fff2SAl Viro 			if (nameidata_dentry_drop_rcu(nd, dentry))
12295a18fff2SAl Viro 				return -ECHILD;
12305a18fff2SAl Viro 		} else {
12319875cf80SDavid Howells 			if (nameidata_drop_rcu(nd))
12329875cf80SDavid Howells 				return -ECHILD;
12339875cf80SDavid Howells 		}
12345a18fff2SAl Viro 	} else {
123531e6b01fSNick Piggin 		dentry = __d_lookup(parent, name);
123624643087SAl Viro 	}
12375a18fff2SAl Viro 
12385a18fff2SAl Viro retry:
12395a18fff2SAl Viro 	if (unlikely(!dentry)) {
12405a18fff2SAl Viro 		struct inode *dir = parent->d_inode;
12415a18fff2SAl Viro 		BUG_ON(nd->inode != dir);
12425a18fff2SAl Viro 
12435a18fff2SAl Viro 		mutex_lock(&dir->i_mutex);
12445a18fff2SAl Viro 		dentry = d_lookup(parent, name);
12455a18fff2SAl Viro 		if (likely(!dentry)) {
12465a18fff2SAl Viro 			dentry = d_alloc_and_lookup(parent, name, nd);
12475a18fff2SAl Viro 			if (IS_ERR(dentry)) {
12485a18fff2SAl Viro 				mutex_unlock(&dir->i_mutex);
12495a18fff2SAl Viro 				return PTR_ERR(dentry);
12505a18fff2SAl Viro 			}
12515a18fff2SAl Viro 			/* known good */
12525a18fff2SAl Viro 			need_reval = 0;
12535a18fff2SAl Viro 			status = 1;
12545a18fff2SAl Viro 		}
12555a18fff2SAl Viro 		mutex_unlock(&dir->i_mutex);
12565a18fff2SAl Viro 	}
12575a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
12585a18fff2SAl Viro 		status = d_revalidate(dentry, nd);
12595a18fff2SAl Viro 	if (unlikely(status <= 0)) {
12605a18fff2SAl Viro 		if (status < 0) {
12615a18fff2SAl Viro 			dput(dentry);
12625a18fff2SAl Viro 			return status;
12635a18fff2SAl Viro 		}
12645a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
12655a18fff2SAl Viro 			dput(dentry);
12665a18fff2SAl Viro 			dentry = NULL;
12675a18fff2SAl Viro 			need_reval = 1;
12685a18fff2SAl Viro 			goto retry;
12695a18fff2SAl Viro 		}
12705a18fff2SAl Viro 	}
12715a18fff2SAl Viro 
12721da177e4SLinus Torvalds 	path->mnt = mnt;
12731da177e4SLinus Torvalds 	path->dentry = dentry;
12749875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
127589312214SIan Kent 	if (unlikely(err < 0)) {
127689312214SIan Kent 		path_put_conditional(path, nd);
12779875cf80SDavid Howells 		return err;
127889312214SIan Kent 	}
127931e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
12801da177e4SLinus Torvalds 	return 0;
12811da177e4SLinus Torvalds }
12821da177e4SLinus Torvalds 
128352094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
128452094c8aSAl Viro {
128552094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
128652094c8aSAl Viro 		int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
128752094c8aSAl Viro 		if (err != -ECHILD)
128852094c8aSAl Viro 			return err;
128952094c8aSAl Viro 		if (nameidata_drop_rcu(nd))
129052094c8aSAl Viro 			return -ECHILD;
129152094c8aSAl Viro 	}
129252094c8aSAl Viro 	return exec_permission(nd->inode, 0);
129352094c8aSAl Viro }
129452094c8aSAl Viro 
12959856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
12969856fa1bSAl Viro {
12979856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
12989856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
12999856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
13009856fa1bSAl Viro 				return -ECHILD;
13019856fa1bSAl Viro 		} else
13029856fa1bSAl Viro 			follow_dotdot(nd);
13039856fa1bSAl Viro 	}
13049856fa1bSAl Viro 	return 0;
13059856fa1bSAl Viro }
13069856fa1bSAl Viro 
1307951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1308951361f9SAl Viro {
1309951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1310951361f9SAl Viro 		path_put(&nd->path);
1311951361f9SAl Viro 	} else {
1312951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
1313951361f9SAl Viro 		nd->root.mnt = NULL;
1314951361f9SAl Viro 		rcu_read_unlock();
1315951361f9SAl Viro 		br_read_unlock(vfsmount_lock);
1316951361f9SAl Viro 	}
1317951361f9SAl Viro }
1318951361f9SAl Viro 
13191da177e4SLinus Torvalds /*
13201da177e4SLinus Torvalds  * Name resolution.
1321ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1322ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
13231da177e4SLinus Torvalds  *
1324ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1325ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
13261da177e4SLinus Torvalds  */
13276de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
13281da177e4SLinus Torvalds {
13291da177e4SLinus Torvalds 	struct path next;
13301da177e4SLinus Torvalds 	int err;
13311da177e4SLinus Torvalds 	unsigned int lookup_flags = nd->flags;
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds 	while (*name=='/')
13341da177e4SLinus Torvalds 		name++;
13351da177e4SLinus Torvalds 	if (!*name)
1336086e183aSAl Viro 		return 0;
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds 	if (nd->depth)
1339f55eab82STrond Myklebust 		lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
13401da177e4SLinus Torvalds 
13411da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
13421da177e4SLinus Torvalds 	for(;;) {
134331e6b01fSNick Piggin 		struct inode *inode;
13441da177e4SLinus Torvalds 		unsigned long hash;
13451da177e4SLinus Torvalds 		struct qstr this;
13461da177e4SLinus Torvalds 		unsigned int c;
1347fe479a58SAl Viro 		int type;
13481da177e4SLinus Torvalds 
1349cdce5d6bSTrond Myklebust 		nd->flags |= LOOKUP_CONTINUE;
135052094c8aSAl Viro 
135152094c8aSAl Viro 		err = may_lookup(nd);
13521da177e4SLinus Torvalds  		if (err)
13531da177e4SLinus Torvalds 			break;
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 		this.name = name;
13561da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds 		hash = init_name_hash();
13591da177e4SLinus Torvalds 		do {
13601da177e4SLinus Torvalds 			name++;
13611da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
13621da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
13631da177e4SLinus Torvalds 		} while (c && (c != '/'));
13641da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
13651da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
13661da177e4SLinus Torvalds 
1367fe479a58SAl Viro 		type = LAST_NORM;
1368fe479a58SAl Viro 		if (this.name[0] == '.') switch (this.len) {
1369fe479a58SAl Viro 			case 2:
137016c2cd71SAl Viro 				if (this.name[1] == '.') {
1371fe479a58SAl Viro 					type = LAST_DOTDOT;
137216c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
137316c2cd71SAl Viro 				}
1374fe479a58SAl Viro 				break;
1375fe479a58SAl Viro 			case 1:
1376fe479a58SAl Viro 				type = LAST_DOT;
1377fe479a58SAl Viro 		}
13785a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
13795a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
138016c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
13815a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
13825a202bcdSAl Viro 				err = parent->d_op->d_hash(parent, nd->inode,
13835a202bcdSAl Viro 							   &this);
13845a202bcdSAl Viro 				if (err < 0)
13855a202bcdSAl Viro 					break;
13865a202bcdSAl Viro 			}
13875a202bcdSAl Viro 		}
1388fe479a58SAl Viro 
13891da177e4SLinus Torvalds 		/* remove trailing slashes? */
13901da177e4SLinus Torvalds 		if (!c)
13911da177e4SLinus Torvalds 			goto last_component;
13921da177e4SLinus Torvalds 		while (*++name == '/');
13931da177e4SLinus Torvalds 		if (!*name)
13941da177e4SLinus Torvalds 			goto last_with_slashes;
13951da177e4SLinus Torvalds 
13961da177e4SLinus Torvalds 		/*
13971da177e4SLinus Torvalds 		 * "." and ".." are special - ".." especially so because it has
13981da177e4SLinus Torvalds 		 * to be able to know about the current root directory and
13991da177e4SLinus Torvalds 		 * parent relationships.
14001da177e4SLinus Torvalds 		 */
1401fe479a58SAl Viro 		if (unlikely(type != LAST_NORM)) {
1402ef7562d5SAl Viro 			if (handle_dots(nd, type))
1403ef7562d5SAl Viro 				return -ECHILD;
14041da177e4SLinus Torvalds 			continue;
14051da177e4SLinus Torvalds 		}
1406fe479a58SAl Viro 
14071da177e4SLinus Torvalds 		/* This does the actual lookups.. */
140831e6b01fSNick Piggin 		err = do_lookup(nd, &this, &next, &inode);
14091da177e4SLinus Torvalds 		if (err)
14101da177e4SLinus Torvalds 			break;
14111da177e4SLinus Torvalds 
14127bc055d1SAl Viro 		if (inode && inode->i_op->follow_link) {
14133abb17e8SLinus Torvalds 			err = do_follow_link(inode, &next, nd);
14141da177e4SLinus Torvalds 			if (err)
1415a7472babSAl Viro 				return err;
141631e6b01fSNick Piggin 			nd->inode = nd->path.dentry->d_inode;
141731e6b01fSNick Piggin 		} else {
141809dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
141931e6b01fSNick Piggin 			nd->inode = inode;
142031e6b01fSNick Piggin 		}
14217bc055d1SAl Viro 		err = -ENOENT;
14227bc055d1SAl Viro 		if (!nd->inode)
14237bc055d1SAl Viro 			break;
14241da177e4SLinus Torvalds 		err = -ENOTDIR;
142531e6b01fSNick Piggin 		if (!nd->inode->i_op->lookup)
14261da177e4SLinus Torvalds 			break;
14271da177e4SLinus Torvalds 		continue;
14281da177e4SLinus Torvalds 		/* here ends the main loop */
14291da177e4SLinus Torvalds 
14301da177e4SLinus Torvalds last_with_slashes:
14311da177e4SLinus Torvalds 		lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
14321da177e4SLinus Torvalds last_component:
1433f55eab82STrond Myklebust 		/* Clear LOOKUP_CONTINUE iff it was previously unset */
1434f55eab82STrond Myklebust 		nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
14351da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_PARENT)
14361da177e4SLinus Torvalds 			goto lookup_parent;
1437ef7562d5SAl Viro 		if (unlikely(type != LAST_NORM))
1438ef7562d5SAl Viro 			return handle_dots(nd, type);
143931e6b01fSNick Piggin 		err = do_lookup(nd, &this, &next, &inode);
14401da177e4SLinus Torvalds 		if (err)
14411da177e4SLinus Torvalds 			break;
1442db372915SDavid Howells 		if (inode && unlikely(inode->i_op->follow_link) &&
1443db372915SDavid Howells 		    (lookup_flags & LOOKUP_FOLLOW)) {
14443abb17e8SLinus Torvalds 			err = do_follow_link(inode, &next, nd);
14451da177e4SLinus Torvalds 			if (err)
1446a7472babSAl Viro 				return err;
144731e6b01fSNick Piggin 			nd->inode = nd->path.dentry->d_inode;
144831e6b01fSNick Piggin 		} else {
144909dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
145031e6b01fSNick Piggin 			nd->inode = inode;
145131e6b01fSNick Piggin 		}
14521da177e4SLinus Torvalds 		err = -ENOENT;
145331e6b01fSNick Piggin 		if (!nd->inode)
14541da177e4SLinus Torvalds 			break;
14551da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_DIRECTORY) {
14561da177e4SLinus Torvalds 			err = -ENOTDIR;
145731e6b01fSNick Piggin 			if (!nd->inode->i_op->lookup)
14581da177e4SLinus Torvalds 				break;
14591da177e4SLinus Torvalds 		}
1460086e183aSAl Viro 		return 0;
14611da177e4SLinus Torvalds lookup_parent:
14621da177e4SLinus Torvalds 		nd->last = this;
1463fe479a58SAl Viro 		nd->last_type = type;
14641da177e4SLinus Torvalds 		return 0;
14651da177e4SLinus Torvalds 	}
1466951361f9SAl Viro 	terminate_walk(nd);
14671da177e4SLinus Torvalds 	return err;
14681da177e4SLinus Torvalds }
14691da177e4SLinus Torvalds 
147070e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
147170e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
147231e6b01fSNick Piggin {
147331e6b01fSNick Piggin 	int retval = 0;
147431e6b01fSNick Piggin 	int fput_needed;
147531e6b01fSNick Piggin 	struct file *file;
147631e6b01fSNick Piggin 
147731e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
147816c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
147931e6b01fSNick Piggin 	nd->depth = 0;
148031e6b01fSNick Piggin 	nd->root.mnt = NULL;
148131e6b01fSNick Piggin 
148231e6b01fSNick Piggin 	if (*name=='/') {
1483e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
148431e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
148531e6b01fSNick Piggin 			rcu_read_lock();
1486e41f7d4eSAl Viro 			set_root_rcu(nd);
1487e41f7d4eSAl Viro 		} else {
1488e41f7d4eSAl Viro 			set_root(nd);
1489e41f7d4eSAl Viro 			path_get(&nd->root);
1490e41f7d4eSAl Viro 		}
149131e6b01fSNick Piggin 		nd->path = nd->root;
149231e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1493e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
149431e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1495c28cc364SNick Piggin 			unsigned seq;
149631e6b01fSNick Piggin 
149731e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
149831e6b01fSNick Piggin 			rcu_read_lock();
149931e6b01fSNick Piggin 
1500c28cc364SNick Piggin 			do {
1501c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
150231e6b01fSNick Piggin 				nd->path = fs->pwd;
1503c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1504c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1505e41f7d4eSAl Viro 		} else {
1506e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1507e41f7d4eSAl Viro 		}
150831e6b01fSNick Piggin 	} else {
150931e6b01fSNick Piggin 		struct dentry *dentry;
151031e6b01fSNick Piggin 
151131e6b01fSNick Piggin 		file = fget_light(dfd, &fput_needed);
151231e6b01fSNick Piggin 		retval = -EBADF;
151331e6b01fSNick Piggin 		if (!file)
151431e6b01fSNick Piggin 			goto out_fail;
151531e6b01fSNick Piggin 
151631e6b01fSNick Piggin 		dentry = file->f_path.dentry;
151731e6b01fSNick Piggin 
151831e6b01fSNick Piggin 		retval = -ENOTDIR;
151931e6b01fSNick Piggin 		if (!S_ISDIR(dentry->d_inode->i_mode))
152031e6b01fSNick Piggin 			goto fput_fail;
152131e6b01fSNick Piggin 
152231e6b01fSNick Piggin 		retval = file_permission(file, MAY_EXEC);
152331e6b01fSNick Piggin 		if (retval)
152431e6b01fSNick Piggin 			goto fput_fail;
152531e6b01fSNick Piggin 
152631e6b01fSNick Piggin 		nd->path = file->f_path;
1527e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
152831e6b01fSNick Piggin 			if (fput_needed)
152970e9b357SAl Viro 				*fp = file;
1530c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
153131e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
153231e6b01fSNick Piggin 			rcu_read_lock();
15335590ff0dSUlrich Drepper 		} else {
15345dd784d0SJan Blunck 			path_get(&file->f_path);
15355590ff0dSUlrich Drepper 			fput_light(file, fput_needed);
15361da177e4SLinus Torvalds 		}
1537e41f7d4eSAl Viro 	}
1538e41f7d4eSAl Viro 
153931e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
15409b4a9b14SAl Viro 	return 0;
15412dfdd266SJosef 'Jeff' Sipek 
15429b4a9b14SAl Viro fput_fail:
15439b4a9b14SAl Viro 	fput_light(file, fput_needed);
15449b4a9b14SAl Viro out_fail:
15459b4a9b14SAl Viro 	return retval;
15469b4a9b14SAl Viro }
15479b4a9b14SAl Viro 
15489b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1549ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
15509b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
15519b4a9b14SAl Viro {
155270e9b357SAl Viro 	struct file *base = NULL;
155331e6b01fSNick Piggin 	int retval;
155431e6b01fSNick Piggin 
155531e6b01fSNick Piggin 	/*
155631e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
155731e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
155831e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
155931e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
156031e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
156131e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
156231e6b01fSNick Piggin 	 * analogue, foo_rcu().
156331e6b01fSNick Piggin 	 *
156431e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
156531e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
156631e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
156731e6b01fSNick Piggin 	 * be able to complete).
156831e6b01fSNick Piggin 	 */
156970e9b357SAl Viro 	retval = path_init(dfd, name, flags, nd, &base);
1570ee0827cdSAl Viro 
157131e6b01fSNick Piggin 	if (unlikely(retval))
157231e6b01fSNick Piggin 		return retval;
1573ee0827cdSAl Viro 
1574ee0827cdSAl Viro 	current->total_link_count = 0;
1575ee0827cdSAl Viro 	retval = link_path_walk(name, nd);
1576ee0827cdSAl Viro 
1577ee0827cdSAl Viro 	if (nd->flags & LOOKUP_RCU) {
15784455ca62SAl Viro 		/* went all way through without dropping RCU */
15794455ca62SAl Viro 		BUG_ON(retval);
1580086e183aSAl Viro 		if (nameidata_drop_rcu_last(nd))
1581086e183aSAl Viro 			retval = -ECHILD;
1582086e183aSAl Viro 	}
158331e6b01fSNick Piggin 
158416c2cd71SAl Viro 	if (!retval)
158516c2cd71SAl Viro 		retval = handle_reval_path(nd);
158616c2cd71SAl Viro 
158770e9b357SAl Viro 	if (base)
158870e9b357SAl Viro 		fput(base);
1589ee0827cdSAl Viro 
159031e6b01fSNick Piggin 	if (nd->root.mnt) {
159131e6b01fSNick Piggin 		path_put(&nd->root);
159231e6b01fSNick Piggin 		nd->root.mnt = NULL;
159331e6b01fSNick Piggin 	}
1594ee0827cdSAl Viro 	return retval;
159531e6b01fSNick Piggin }
159631e6b01fSNick Piggin 
1597ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1598ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1599ee0827cdSAl Viro {
1600ee0827cdSAl Viro 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1601ee0827cdSAl Viro 	if (unlikely(retval == -ECHILD))
1602ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags, nd);
1603ee0827cdSAl Viro 	if (unlikely(retval == -ESTALE))
1604ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1605ee0827cdSAl Viro 
160631e6b01fSNick Piggin 	if (likely(!retval)) {
160731e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
160831e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
160931e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
161031e6b01fSNick Piggin 		}
161131e6b01fSNick Piggin 	}
1612170aa3d0SUlrich Drepper 	return retval;
16131da177e4SLinus Torvalds }
16141da177e4SLinus Torvalds 
1615c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd)
16165590ff0dSUlrich Drepper {
1617c9c6cac0SAl Viro 	return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
16185590ff0dSUlrich Drepper }
16195590ff0dSUlrich Drepper 
1620d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1621d1811465SAl Viro {
1622d1811465SAl Viro 	struct nameidata nd;
1623d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1624d1811465SAl Viro 	if (!res)
1625d1811465SAl Viro 		*path = nd.path;
1626d1811465SAl Viro 	return res;
1627d1811465SAl Viro }
1628d1811465SAl Viro 
162916f18200SJosef 'Jeff' Sipek /**
163016f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
163116f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
163216f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
163316f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
163416f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
163516f18200SJosef 'Jeff' Sipek  * @nd: pointer to nameidata
163616f18200SJosef 'Jeff' Sipek  */
163716f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
163816f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
163916f18200SJosef 'Jeff' Sipek 		    struct nameidata *nd)
164016f18200SJosef 'Jeff' Sipek {
1641ee0827cdSAl Viro 	int result;
164216f18200SJosef 'Jeff' Sipek 
164316f18200SJosef 'Jeff' Sipek 	/* same as do_path_lookup */
164416f18200SJosef 'Jeff' Sipek 	nd->last_type = LAST_ROOT;
164516c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
164616f18200SJosef 'Jeff' Sipek 	nd->depth = 0;
164716f18200SJosef 'Jeff' Sipek 
1648c8e7f449SJan Blunck 	nd->path.dentry = dentry;
1649c8e7f449SJan Blunck 	nd->path.mnt = mnt;
1650c8e7f449SJan Blunck 	path_get(&nd->path);
16515b857119SAl Viro 	nd->root = nd->path;
16525b857119SAl Viro 	path_get(&nd->root);
165331e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
165416f18200SJosef 'Jeff' Sipek 
1655ee0827cdSAl Viro 	current->total_link_count = 0;
1656ee0827cdSAl Viro 
1657ee0827cdSAl Viro 	result = link_path_walk(name, nd);
165816c2cd71SAl Viro 	if (!result)
165916c2cd71SAl Viro 		result = handle_reval_path(nd);
1660ee0827cdSAl Viro 	if (result == -ESTALE) {
1661ee0827cdSAl Viro 		/* nd->path had been dropped */
1662ee0827cdSAl Viro 		current->total_link_count = 0;
1663ee0827cdSAl Viro 		nd->path.dentry = dentry;
1664ee0827cdSAl Viro 		nd->path.mnt = mnt;
1665ee0827cdSAl Viro 		nd->inode = dentry->d_inode;
1666ee0827cdSAl Viro 		path_get(&nd->path);
166716c2cd71SAl Viro 		nd->flags = flags | LOOKUP_JUMPED | LOOKUP_REVAL;
166816c2cd71SAl Viro 
1669ee0827cdSAl Viro 		result = link_path_walk(name, nd);
167016c2cd71SAl Viro 		if (!result)
167116c2cd71SAl Viro 			result = handle_reval_path(nd);
1672ee0827cdSAl Viro 	}
1673ee0827cdSAl Viro 	if (unlikely(!result && !audit_dummy_context() && nd->path.dentry &&
167431e6b01fSNick Piggin 				nd->inode))
16754ac91378SJan Blunck 		audit_inode(name, nd->path.dentry);
167616f18200SJosef 'Jeff' Sipek 
16772a737871SAl Viro 	path_put(&nd->root);
16782a737871SAl Viro 	nd->root.mnt = NULL;
167916f18200SJosef 'Jeff' Sipek 
1680ee0827cdSAl Viro 	return result;
168116f18200SJosef 'Jeff' Sipek }
168216f18200SJosef 'Jeff' Sipek 
1683eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1684eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
16851da177e4SLinus Torvalds {
168681fca444SChristoph Hellwig 	struct inode *inode = base->d_inode;
16871da177e4SLinus Torvalds 	struct dentry *dentry;
16881da177e4SLinus Torvalds 	int err;
16891da177e4SLinus Torvalds 
1690b74c79e9SNick Piggin 	err = exec_permission(inode, 0);
169181fca444SChristoph Hellwig 	if (err)
169281fca444SChristoph Hellwig 		return ERR_PTR(err);
16931da177e4SLinus Torvalds 
16941da177e4SLinus Torvalds 	/*
1695b04f784eSNick Piggin 	 * Don't bother with __d_lookup: callers are for creat as
1696b04f784eSNick Piggin 	 * well as unlink, so a lot of the time it would cost
1697b04f784eSNick Piggin 	 * a double lookup.
16986e6b1bd1SAl Viro 	 */
16996e6b1bd1SAl Viro 	dentry = d_lookup(base, name);
17006e6b1bd1SAl Viro 
1701fb045adbSNick Piggin 	if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
17026e6b1bd1SAl Viro 		dentry = do_revalidate(dentry, nd);
17036e6b1bd1SAl Viro 
17041da177e4SLinus Torvalds 	if (!dentry)
1705baa03890SNick Piggin 		dentry = d_alloc_and_lookup(base, name, nd);
17065a202bcdSAl Viro 
17071da177e4SLinus Torvalds 	return dentry;
17081da177e4SLinus Torvalds }
17091da177e4SLinus Torvalds 
1710057f6c01SJames Morris /*
1711057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1712057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1713057f6c01SJames Morris  * SMP-safe.
1714057f6c01SJames Morris  */
1715a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
17161da177e4SLinus Torvalds {
17174ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
17181da177e4SLinus Torvalds }
17191da177e4SLinus Torvalds 
1720eead1911SChristoph Hellwig /**
1721a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1722eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1723eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1724eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1725eead1911SChristoph Hellwig  *
1726a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1727a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1728eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1729eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1730eead1911SChristoph Hellwig  */
1731057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1732057f6c01SJames Morris {
1733057f6c01SJames Morris 	struct qstr this;
17346a96ba54SAl Viro 	unsigned long hash;
17356a96ba54SAl Viro 	unsigned int c;
1736057f6c01SJames Morris 
17372f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
17382f9092e1SDavid Woodhouse 
17396a96ba54SAl Viro 	this.name = name;
17406a96ba54SAl Viro 	this.len = len;
17416a96ba54SAl Viro 	if (!len)
17426a96ba54SAl Viro 		return ERR_PTR(-EACCES);
17436a96ba54SAl Viro 
17446a96ba54SAl Viro 	hash = init_name_hash();
17456a96ba54SAl Viro 	while (len--) {
17466a96ba54SAl Viro 		c = *(const unsigned char *)name++;
17476a96ba54SAl Viro 		if (c == '/' || c == '\0')
17486a96ba54SAl Viro 			return ERR_PTR(-EACCES);
17496a96ba54SAl Viro 		hash = partial_name_hash(c, hash);
17506a96ba54SAl Viro 	}
17516a96ba54SAl Viro 	this.hash = end_name_hash(hash);
17525a202bcdSAl Viro 	/*
17535a202bcdSAl Viro 	 * See if the low-level filesystem might want
17545a202bcdSAl Viro 	 * to use its own hash..
17555a202bcdSAl Viro 	 */
17565a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
17575a202bcdSAl Viro 		int err = base->d_op->d_hash(base, base->d_inode, &this);
17585a202bcdSAl Viro 		if (err < 0)
17595a202bcdSAl Viro 			return ERR_PTR(err);
17605a202bcdSAl Viro 	}
1761eead1911SChristoph Hellwig 
176249705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1763057f6c01SJames Morris }
1764057f6c01SJames Morris 
17652d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags,
17662d8f3038SAl Viro 		 struct path *path)
17671da177e4SLinus Torvalds {
17682d8f3038SAl Viro 	struct nameidata nd;
17691da177e4SLinus Torvalds 	char *tmp = getname(name);
17701da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
17711da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
17722d8f3038SAl Viro 
17732d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
17742d8f3038SAl Viro 
17752d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
17761da177e4SLinus Torvalds 		putname(tmp);
17772d8f3038SAl Viro 		if (!err)
17782d8f3038SAl Viro 			*path = nd.path;
17791da177e4SLinus Torvalds 	}
17801da177e4SLinus Torvalds 	return err;
17811da177e4SLinus Torvalds }
17821da177e4SLinus Torvalds 
17832ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
17842ad94ae6SAl Viro 			struct nameidata *nd, char **name)
17852ad94ae6SAl Viro {
17862ad94ae6SAl Viro 	char *s = getname(path);
17872ad94ae6SAl Viro 	int error;
17882ad94ae6SAl Viro 
17892ad94ae6SAl Viro 	if (IS_ERR(s))
17902ad94ae6SAl Viro 		return PTR_ERR(s);
17912ad94ae6SAl Viro 
17922ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
17932ad94ae6SAl Viro 	if (error)
17942ad94ae6SAl Viro 		putname(s);
17952ad94ae6SAl Viro 	else
17962ad94ae6SAl Viro 		*name = s;
17972ad94ae6SAl Viro 
17982ad94ae6SAl Viro 	return error;
17992ad94ae6SAl Viro }
18002ad94ae6SAl Viro 
18011da177e4SLinus Torvalds /*
18021da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
18031da177e4SLinus Torvalds  * minimal.
18041da177e4SLinus Torvalds  */
18051da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
18061da177e4SLinus Torvalds {
1807da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1808da9592edSDavid Howells 
18091da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
18101da177e4SLinus Torvalds 		return 0;
1811da9592edSDavid Howells 	if (inode->i_uid == fsuid)
18121da177e4SLinus Torvalds 		return 0;
1813da9592edSDavid Howells 	if (dir->i_uid == fsuid)
18141da177e4SLinus Torvalds 		return 0;
18151da177e4SLinus Torvalds 	return !capable(CAP_FOWNER);
18161da177e4SLinus Torvalds }
18171da177e4SLinus Torvalds 
18181da177e4SLinus Torvalds /*
18191da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
18201da177e4SLinus Torvalds  *  whether the type of victim is right.
18211da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
18221da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
18231da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
18241da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
18251da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
18261da177e4SLinus Torvalds  *	a. be owner of dir, or
18271da177e4SLinus Torvalds  *	b. be owner of victim, or
18281da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
18291da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
18301da177e4SLinus Torvalds  *     links pointing to it.
18311da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
18321da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
18331da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
18341da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
18351da177e4SLinus Torvalds  *     nfs_async_unlink().
18361da177e4SLinus Torvalds  */
1837858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
18381da177e4SLinus Torvalds {
18391da177e4SLinus Torvalds 	int error;
18401da177e4SLinus Torvalds 
18411da177e4SLinus Torvalds 	if (!victim->d_inode)
18421da177e4SLinus Torvalds 		return -ENOENT;
18431da177e4SLinus Torvalds 
18441da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
1845cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
18461da177e4SLinus Torvalds 
1847f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
18481da177e4SLinus Torvalds 	if (error)
18491da177e4SLinus Torvalds 		return error;
18501da177e4SLinus Torvalds 	if (IS_APPEND(dir))
18511da177e4SLinus Torvalds 		return -EPERM;
18521da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1853f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
18541da177e4SLinus Torvalds 		return -EPERM;
18551da177e4SLinus Torvalds 	if (isdir) {
18561da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
18571da177e4SLinus Torvalds 			return -ENOTDIR;
18581da177e4SLinus Torvalds 		if (IS_ROOT(victim))
18591da177e4SLinus Torvalds 			return -EBUSY;
18601da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
18611da177e4SLinus Torvalds 		return -EISDIR;
18621da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18631da177e4SLinus Torvalds 		return -ENOENT;
18641da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
18651da177e4SLinus Torvalds 		return -EBUSY;
18661da177e4SLinus Torvalds 	return 0;
18671da177e4SLinus Torvalds }
18681da177e4SLinus Torvalds 
18691da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
18701da177e4SLinus Torvalds  *  dir.
18711da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
18721da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
18731da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
18741da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
18751da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
18761da177e4SLinus Torvalds  */
1877a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
18781da177e4SLinus Torvalds {
18791da177e4SLinus Torvalds 	if (child->d_inode)
18801da177e4SLinus Torvalds 		return -EEXIST;
18811da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18821da177e4SLinus Torvalds 		return -ENOENT;
1883f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
18841da177e4SLinus Torvalds }
18851da177e4SLinus Torvalds 
18861da177e4SLinus Torvalds /*
18871da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
18881da177e4SLinus Torvalds  */
18891da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
18901da177e4SLinus Torvalds {
18911da177e4SLinus Torvalds 	struct dentry *p;
18921da177e4SLinus Torvalds 
18931da177e4SLinus Torvalds 	if (p1 == p2) {
1894f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
18951da177e4SLinus Torvalds 		return NULL;
18961da177e4SLinus Torvalds 	}
18971da177e4SLinus Torvalds 
1898a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
18991da177e4SLinus Torvalds 
1900e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1901e2761a11SOGAWA Hirofumi 	if (p) {
1902f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1903f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
19041da177e4SLinus Torvalds 		return p;
19051da177e4SLinus Torvalds 	}
19061da177e4SLinus Torvalds 
1907e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1908e2761a11SOGAWA Hirofumi 	if (p) {
1909f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1910f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
19111da177e4SLinus Torvalds 		return p;
19121da177e4SLinus Torvalds 	}
19131da177e4SLinus Torvalds 
1914f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1915f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
19161da177e4SLinus Torvalds 	return NULL;
19171da177e4SLinus Torvalds }
19181da177e4SLinus Torvalds 
19191da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
19201da177e4SLinus Torvalds {
19211b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
19221da177e4SLinus Torvalds 	if (p1 != p2) {
19231b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1924a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
19251da177e4SLinus Torvalds 	}
19261da177e4SLinus Torvalds }
19271da177e4SLinus Torvalds 
19281da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
19291da177e4SLinus Torvalds 		struct nameidata *nd)
19301da177e4SLinus Torvalds {
1931a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
19321da177e4SLinus Torvalds 
19331da177e4SLinus Torvalds 	if (error)
19341da177e4SLinus Torvalds 		return error;
19351da177e4SLinus Torvalds 
1936acfa4380SAl Viro 	if (!dir->i_op->create)
19371da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
19381da177e4SLinus Torvalds 	mode &= S_IALLUGO;
19391da177e4SLinus Torvalds 	mode |= S_IFREG;
19401da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
19411da177e4SLinus Torvalds 	if (error)
19421da177e4SLinus Torvalds 		return error;
19431da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
1944a74574aaSStephen Smalley 	if (!error)
1945f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
19461da177e4SLinus Torvalds 	return error;
19471da177e4SLinus Torvalds }
19481da177e4SLinus Torvalds 
19493fb64190SChristoph Hellwig int may_open(struct path *path, int acc_mode, int flag)
19501da177e4SLinus Torvalds {
19513fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
19521da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
19531da177e4SLinus Torvalds 	int error;
19541da177e4SLinus Torvalds 
19551da177e4SLinus Torvalds 	if (!inode)
19561da177e4SLinus Torvalds 		return -ENOENT;
19571da177e4SLinus Torvalds 
1958c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
1959c8fe8f30SChristoph Hellwig 	case S_IFLNK:
19601da177e4SLinus Torvalds 		return -ELOOP;
1961c8fe8f30SChristoph Hellwig 	case S_IFDIR:
1962c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
19631da177e4SLinus Torvalds 			return -EISDIR;
1964c8fe8f30SChristoph Hellwig 		break;
1965c8fe8f30SChristoph Hellwig 	case S_IFBLK:
1966c8fe8f30SChristoph Hellwig 	case S_IFCHR:
19673fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
19681da177e4SLinus Torvalds 			return -EACCES;
1969c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
1970c8fe8f30SChristoph Hellwig 	case S_IFIFO:
1971c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
19721da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
1973c8fe8f30SChristoph Hellwig 		break;
19744a3fd211SDave Hansen 	}
1975b41572e9SDave Hansen 
19763fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
1977b41572e9SDave Hansen 	if (error)
1978b41572e9SDave Hansen 		return error;
19796146f0d5SMimi Zohar 
19801da177e4SLinus Torvalds 	/*
19811da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
19821da177e4SLinus Torvalds 	 */
19831da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
19848737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
19857715b521SAl Viro 			return -EPERM;
19861da177e4SLinus Torvalds 		if (flag & O_TRUNC)
19877715b521SAl Viro 			return -EPERM;
19881da177e4SLinus Torvalds 	}
19891da177e4SLinus Torvalds 
19901da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
19917715b521SAl Viro 	if (flag & O_NOATIME && !is_owner_or_cap(inode))
19927715b521SAl Viro 		return -EPERM;
19931da177e4SLinus Torvalds 
19941da177e4SLinus Torvalds 	/*
19951da177e4SLinus Torvalds 	 * Ensure there are no outstanding leases on the file.
19961da177e4SLinus Torvalds 	 */
1997b65a9cfcSAl Viro 	return break_lease(inode, flag);
19987715b521SAl Viro }
19997715b521SAl Viro 
2000e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
20017715b521SAl Viro {
2002e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
20037715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
20047715b521SAl Viro 	int error = get_write_access(inode);
20051da177e4SLinus Torvalds 	if (error)
20067715b521SAl Viro 		return error;
20071da177e4SLinus Torvalds 	/*
20081da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
20091da177e4SLinus Torvalds 	 */
20101da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2011be6d3e56SKentaro Takeda 	if (!error)
2012ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
20131da177e4SLinus Torvalds 	if (!error) {
20147715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2015d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2016e1181ee6SJeff Layton 				    filp);
20171da177e4SLinus Torvalds 	}
20181da177e4SLinus Torvalds 	put_write_access(inode);
2019acd0c935SMimi Zohar 	return error;
20201da177e4SLinus Torvalds }
20211da177e4SLinus Torvalds 
2022d57999e1SDave Hansen /*
2023d57999e1SDave Hansen  * Note that while the flag value (low two bits) for sys_open means:
2024d57999e1SDave Hansen  *	00 - read-only
2025d57999e1SDave Hansen  *	01 - write-only
2026d57999e1SDave Hansen  *	10 - read-write
2027d57999e1SDave Hansen  *	11 - special
2028d57999e1SDave Hansen  * it is changed into
2029d57999e1SDave Hansen  *	00 - no permissions needed
2030d57999e1SDave Hansen  *	01 - read-permission
2031d57999e1SDave Hansen  *	10 - write-permission
2032d57999e1SDave Hansen  *	11 - read-write
2033d57999e1SDave Hansen  * for the internal routines (ie open_namei()/follow_link() etc)
2034d57999e1SDave Hansen  * This is more logical, and also allows the 00 "no perm needed"
2035d57999e1SDave Hansen  * to be used for symlinks (where the permissions are checked
2036d57999e1SDave Hansen  * later).
2037d57999e1SDave Hansen  *
2038d57999e1SDave Hansen */
2039d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2040d57999e1SDave Hansen {
2041d57999e1SDave Hansen 	if ((flag+1) & O_ACCMODE)
2042d57999e1SDave Hansen 		flag++;
2043d57999e1SDave Hansen 	return flag;
2044d57999e1SDave Hansen }
2045d57999e1SDave Hansen 
204631e6b01fSNick Piggin /*
2047fe2d35ffSAl Viro  * Handle the last step of open()
204831e6b01fSNick Piggin  */
2049fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
2050c3e380b0SAl Viro 			    const struct open_flags *op, const char *pathname)
2051fb1cc555SAl Viro {
2052a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
20536c0d46c4SAl Viro 	struct dentry *dentry;
2054ca344a89SAl Viro 	int open_flag = op->open_flag;
20556c0d46c4SAl Viro 	int will_truncate = open_flag & O_TRUNC;
2056ca344a89SAl Viro 	int want_write = 0;
2057ca344a89SAl Viro 	int skip_perm = 0;
2058fb1cc555SAl Viro 	struct file *filp;
2059fe2d35ffSAl Viro 	struct inode *inode;
206016c2cd71SAl Viro 	int error;
2061fb1cc555SAl Viro 
2062c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2063c3e380b0SAl Viro 	nd->flags |= op->intent;
2064c3e380b0SAl Viro 
20651f36f774SAl Viro 	switch (nd->last_type) {
20661f36f774SAl Viro 	case LAST_DOTDOT:
2067176306f5SNeil Brown 	case LAST_DOT:
2068fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2069fe2d35ffSAl Viro 		if (error)
2070fe2d35ffSAl Viro 			return ERR_PTR(error);
20711f36f774SAl Viro 		/* fallthrough */
20721f36f774SAl Viro 	case LAST_ROOT:
2073fe2d35ffSAl Viro 		if (nd->flags & LOOKUP_RCU) {
2074fe2d35ffSAl Viro 			if (nameidata_drop_rcu_last(nd))
2075fe2d35ffSAl Viro 				return ERR_PTR(-ECHILD);
2076fe2d35ffSAl Viro 		}
207716c2cd71SAl Viro 		error = handle_reval_path(nd);
207816c2cd71SAl Viro 		if (error)
207916c2cd71SAl Viro 			goto exit;
2080fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2081ca344a89SAl Viro 		if (open_flag & O_CREAT) {
208216c2cd71SAl Viro 			error = -EISDIR;
20831f36f774SAl Viro 			goto exit;
2084fe2d35ffSAl Viro 		}
2085fe2d35ffSAl Viro 		goto ok;
20861f36f774SAl Viro 	case LAST_BIND:
2087fe2d35ffSAl Viro 		/* can't be RCU mode here */
208816c2cd71SAl Viro 		error = handle_reval_path(nd);
208916c2cd71SAl Viro 		if (error)
209016c2cd71SAl Viro 			goto exit;
20911f36f774SAl Viro 		audit_inode(pathname, dir);
20921f36f774SAl Viro 		goto ok;
20931f36f774SAl Viro 	}
2094a2c36b45SAl Viro 
2095ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2096fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2097fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2098fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2099fe2d35ffSAl Viro 		error = do_lookup(nd, &nd->last, path, &inode);
2100fe2d35ffSAl Viro 		if (error) {
2101fe2d35ffSAl Viro 			terminate_walk(nd);
2102fe2d35ffSAl Viro 			return ERR_PTR(error);
2103fe2d35ffSAl Viro 		}
2104fe2d35ffSAl Viro 		if (!inode) {
2105fe2d35ffSAl Viro 			path_to_nameidata(path, nd);
2106fe2d35ffSAl Viro 			terminate_walk(nd);
2107fe2d35ffSAl Viro 			return ERR_PTR(-ENOENT);
2108fe2d35ffSAl Viro 		}
2109fe2d35ffSAl Viro 		if (unlikely(inode->i_op->follow_link)) {
2110fe2d35ffSAl Viro 			/* We drop rcu-walk here */
2111fe2d35ffSAl Viro 			if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry))
2112fe2d35ffSAl Viro 				return ERR_PTR(-ECHILD);
2113fe2d35ffSAl Viro 			return NULL;
2114fe2d35ffSAl Viro 		}
2115fe2d35ffSAl Viro 		path_to_nameidata(path, nd);
2116fe2d35ffSAl Viro 		nd->inode = inode;
2117fe2d35ffSAl Viro 		/* sayonara */
2118fe2d35ffSAl Viro 		if (nd->flags & LOOKUP_RCU) {
2119fe2d35ffSAl Viro 			if (nameidata_drop_rcu_last(nd))
2120fe2d35ffSAl Viro 				return ERR_PTR(-ECHILD);
2121fe2d35ffSAl Viro 		}
2122fe2d35ffSAl Viro 
2123fe2d35ffSAl Viro 		error = -ENOTDIR;
2124fe2d35ffSAl Viro 		if (nd->flags & LOOKUP_DIRECTORY) {
2125fe2d35ffSAl Viro 			if (!inode->i_op->lookup)
2126fe2d35ffSAl Viro 				goto exit;
2127fe2d35ffSAl Viro 		}
2128fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2129fe2d35ffSAl Viro 		goto ok;
2130fe2d35ffSAl Viro 	}
2131fe2d35ffSAl Viro 
2132fe2d35ffSAl Viro 	/* create side of things */
2133fe2d35ffSAl Viro 
2134fe2d35ffSAl Viro 	if (nd->flags & LOOKUP_RCU) {
2135fe2d35ffSAl Viro 		if (nameidata_drop_rcu_last(nd))
2136fe2d35ffSAl Viro 			return ERR_PTR(-ECHILD);
2137fe2d35ffSAl Viro 	}
2138fe2d35ffSAl Viro 
2139fe2d35ffSAl Viro 	audit_inode(pathname, dir);
214016c2cd71SAl Viro 	error = -EISDIR;
21411f36f774SAl Viro 	/* trailing slashes? */
214231e6b01fSNick Piggin 	if (nd->last.name[nd->last.len])
21431f36f774SAl Viro 		goto exit;
21441f36f774SAl Viro 
2145a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
2146a1e28038SAl Viro 
21476c0d46c4SAl Viro 	dentry = lookup_hash(nd);
21486c0d46c4SAl Viro 	error = PTR_ERR(dentry);
21496c0d46c4SAl Viro 	if (IS_ERR(dentry)) {
2150fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
2151fb1cc555SAl Viro 		goto exit;
2152fb1cc555SAl Viro 	}
2153fb1cc555SAl Viro 
21546c0d46c4SAl Viro 	path->dentry = dentry;
21556c0d46c4SAl Viro 	path->mnt = nd->path.mnt;
21566c0d46c4SAl Viro 
2157fb1cc555SAl Viro 	/* Negative dentry, just create the file */
21586c0d46c4SAl Viro 	if (!dentry->d_inode) {
21596c0d46c4SAl Viro 		int mode = op->mode;
21606c0d46c4SAl Viro 		if (!IS_POSIXACL(dir->d_inode))
21616c0d46c4SAl Viro 			mode &= ~current_umask();
2162fb1cc555SAl Viro 		/*
2163fb1cc555SAl Viro 		 * This write is needed to ensure that a
21646c0d46c4SAl Viro 		 * rw->ro transition does not occur between
2165fb1cc555SAl Viro 		 * the time when the file is created and when
2166fb1cc555SAl Viro 		 * a permanent write count is taken through
2167fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
2168fb1cc555SAl Viro 		 */
2169fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
2170fb1cc555SAl Viro 		if (error)
2171fb1cc555SAl Viro 			goto exit_mutex_unlock;
2172ca344a89SAl Viro 		want_write = 1;
21739b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2174ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
21756c0d46c4SAl Viro 		will_truncate = 0;
2176ca344a89SAl Viro 		skip_perm = 1;
21776c0d46c4SAl Viro 		error = security_path_mknod(&nd->path, dentry, mode, 0);
21786c0d46c4SAl Viro 		if (error)
21796c0d46c4SAl Viro 			goto exit_mutex_unlock;
21806c0d46c4SAl Viro 		error = vfs_create(dir->d_inode, dentry, mode, nd);
21816c0d46c4SAl Viro 		if (error)
21826c0d46c4SAl Viro 			goto exit_mutex_unlock;
21836c0d46c4SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
21846c0d46c4SAl Viro 		dput(nd->path.dentry);
21856c0d46c4SAl Viro 		nd->path.dentry = dentry;
2186ca344a89SAl Viro 		goto common;
2187fb1cc555SAl Viro 	}
2188fb1cc555SAl Viro 
2189fb1cc555SAl Viro 	/*
2190fb1cc555SAl Viro 	 * It already exists.
2191fb1cc555SAl Viro 	 */
2192fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2193fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2194fb1cc555SAl Viro 
2195fb1cc555SAl Viro 	error = -EEXIST;
2196ca344a89SAl Viro 	if (open_flag & O_EXCL)
2197fb1cc555SAl Viro 		goto exit_dput;
2198fb1cc555SAl Viro 
21999875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
22009875cf80SDavid Howells 	if (error < 0)
2201fb1cc555SAl Viro 		goto exit_dput;
2202fb1cc555SAl Viro 
2203fb1cc555SAl Viro 	error = -ENOENT;
2204fb1cc555SAl Viro 	if (!path->dentry->d_inode)
2205fb1cc555SAl Viro 		goto exit_dput;
22069e67f361SAl Viro 
22079e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
2208fb1cc555SAl Viro 		return NULL;
2209fb1cc555SAl Viro 
2210fb1cc555SAl Viro 	path_to_nameidata(path, nd);
221131e6b01fSNick Piggin 	nd->inode = path->dentry->d_inode;
2212fb1cc555SAl Viro 	error = -EISDIR;
221331e6b01fSNick Piggin 	if (S_ISDIR(nd->inode->i_mode))
2214fb1cc555SAl Viro 		goto exit;
221567ee3ad2SAl Viro ok:
22166c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
22176c0d46c4SAl Viro 		will_truncate = 0;
22186c0d46c4SAl Viro 
22190f9d1a10SAl Viro 	if (will_truncate) {
22200f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
22210f9d1a10SAl Viro 		if (error)
22220f9d1a10SAl Viro 			goto exit;
2223ca344a89SAl Viro 		want_write = 1;
22240f9d1a10SAl Viro 	}
2225ca344a89SAl Viro common:
2226ca344a89SAl Viro 	error = may_open(&nd->path, skip_perm ? 0 : op->acc_mode, open_flag);
2227ca344a89SAl Viro 	if (error)
22280f9d1a10SAl Viro 		goto exit;
22290f9d1a10SAl Viro 	filp = nameidata_to_filp(nd);
22300f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
22310f9d1a10SAl Viro 		error = ima_file_check(filp, op->acc_mode);
22320f9d1a10SAl Viro 		if (error) {
22330f9d1a10SAl Viro 			fput(filp);
22340f9d1a10SAl Viro 			filp = ERR_PTR(error);
22350f9d1a10SAl Viro 		}
22360f9d1a10SAl Viro 	}
22370f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
22380f9d1a10SAl Viro 		if (will_truncate) {
22390f9d1a10SAl Viro 			error = handle_truncate(filp);
22400f9d1a10SAl Viro 			if (error) {
22410f9d1a10SAl Viro 				fput(filp);
22420f9d1a10SAl Viro 				filp = ERR_PTR(error);
22430f9d1a10SAl Viro 			}
22440f9d1a10SAl Viro 		}
22450f9d1a10SAl Viro 	}
2246ca344a89SAl Viro out:
2247ca344a89SAl Viro 	if (want_write)
22480f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
22490f9d1a10SAl Viro 	path_put(&nd->path);
2250fb1cc555SAl Viro 	return filp;
2251fb1cc555SAl Viro 
2252fb1cc555SAl Viro exit_mutex_unlock:
2253fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2254fb1cc555SAl Viro exit_dput:
2255fb1cc555SAl Viro 	path_put_conditional(path, nd);
2256fb1cc555SAl Viro exit:
2257ca344a89SAl Viro 	filp = ERR_PTR(error);
2258ca344a89SAl Viro 	goto out;
2259fb1cc555SAl Viro }
2260fb1cc555SAl Viro 
226113aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname,
226247c805dcSAl Viro 		const struct open_flags *op, int flags)
22631da177e4SLinus Torvalds {
2264fe2d35ffSAl Viro 	struct file *base = NULL;
22654a3fd211SDave Hansen 	struct file *filp;
2266a70e65dfSChristoph Hellwig 	struct nameidata nd;
22679850c056SAl Viro 	struct path path;
22681da177e4SLinus Torvalds 	int count = 0;
226913aab428SAl Viro 	int error;
227031e6b01fSNick Piggin 
227131e6b01fSNick Piggin 	filp = get_empty_filp();
227231e6b01fSNick Piggin 	if (!filp)
227331e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
227431e6b01fSNick Piggin 
227547c805dcSAl Viro 	filp->f_flags = op->open_flag;
227631e6b01fSNick Piggin 	nd.intent.open.file = filp;
227747c805dcSAl Viro 	nd.intent.open.flags = open_to_namei_flags(op->open_flag);
227847c805dcSAl Viro 	nd.intent.open.create_mode = op->mode;
227931e6b01fSNick Piggin 
2280fe2d35ffSAl Viro 	error = path_init(dfd, pathname, flags | LOOKUP_PARENT, &nd, &base);
228131e6b01fSNick Piggin 	if (unlikely(error))
228213aab428SAl Viro 		goto out_filp;
228331e6b01fSNick Piggin 
2284fe2d35ffSAl Viro 	current->total_link_count = 0;
2285fe2d35ffSAl Viro 	error = link_path_walk(pathname, &nd);
228631e6b01fSNick Piggin 	if (unlikely(error))
228731e6b01fSNick Piggin 		goto out_filp;
22881da177e4SLinus Torvalds 
228947c805dcSAl Viro 	filp = do_last(&nd, &path, op, pathname);
2290806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
22917b9337aaSNick Piggin 		struct path link = path;
22927b9337aaSNick Piggin 		struct inode *linki = link.dentry->d_inode;
2293def4af30SAl Viro 		void *cookie;
229440b39136SAl Viro 		if (!(nd.flags & LOOKUP_FOLLOW) || count++ == 32) {
229540b39136SAl Viro 			path_put_conditional(&path, &nd);
229640b39136SAl Viro 			path_put(&nd.path);
229740b39136SAl Viro 			filp = ERR_PTR(-ELOOP);
229840b39136SAl Viro 			break;
229940b39136SAl Viro 		}
2300806b681cSAl Viro 		/*
2301806b681cSAl Viro 		 * This is subtle. Instead of calling do_follow_link() we do
2302806b681cSAl Viro 		 * the thing by hands. The reason is that this way we have zero
2303806b681cSAl Viro 		 * link_count and path_walk() (called from ->follow_link)
2304806b681cSAl Viro 		 * honoring LOOKUP_PARENT.  After that we have the parent and
2305806b681cSAl Viro 		 * last component, i.e. we are in the same situation as after
2306806b681cSAl Viro 		 * the first path_walk().  Well, almost - if the last component
2307806b681cSAl Viro 		 * is normal we get its copy stored in nd->last.name and we will
2308806b681cSAl Viro 		 * have to putname() it when we are done. Procfs-like symlinks
2309806b681cSAl Viro 		 * just set LAST_BIND.
2310806b681cSAl Viro 		 */
2311806b681cSAl Viro 		nd.flags |= LOOKUP_PARENT;
2312c3e380b0SAl Viro 		nd.flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
23137b9337aaSNick Piggin 		error = __do_follow_link(&link, &nd, &cookie);
2314c3e380b0SAl Viro 		if (unlikely(error))
2315f1afe9efSAl Viro 			filp = ERR_PTR(error);
2316c3e380b0SAl Viro 		else
231747c805dcSAl Viro 			filp = do_last(&nd, &path, op, pathname);
2318f1afe9efSAl Viro 		if (!IS_ERR(cookie) && linki->i_op->put_link)
23197b9337aaSNick Piggin 			linki->i_op->put_link(link.dentry, &nd, cookie);
23207b9337aaSNick Piggin 		path_put(&link);
2321806b681cSAl Viro 	}
232210fa8e62SAl Viro out:
23232a737871SAl Viro 	if (nd.root.mnt)
23242a737871SAl Viro 		path_put(&nd.root);
2325fe2d35ffSAl Viro 	if (base)
2326fe2d35ffSAl Viro 		fput(base);
23272dab5974SLinus Torvalds 	release_open_intent(&nd);
232810fa8e62SAl Viro 	return filp;
23291da177e4SLinus Torvalds 
233031e6b01fSNick Piggin out_filp:
233110fa8e62SAl Viro 	filp = ERR_PTR(error);
233210fa8e62SAl Viro 	goto out;
2333de459215SKirill Korotaev }
23341da177e4SLinus Torvalds 
233513aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname,
233613aab428SAl Viro 		const struct open_flags *op, int flags)
233713aab428SAl Viro {
233813aab428SAl Viro 	struct file *filp;
233913aab428SAl Viro 
234013aab428SAl Viro 	filp = path_openat(dfd, pathname, op, flags | LOOKUP_RCU);
234113aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
234213aab428SAl Viro 		filp = path_openat(dfd, pathname, op, flags);
234313aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
234413aab428SAl Viro 		filp = path_openat(dfd, pathname, op, flags | LOOKUP_REVAL);
234513aab428SAl Viro 	return filp;
234613aab428SAl Viro }
234713aab428SAl Viro 
23481da177e4SLinus Torvalds /**
23491da177e4SLinus Torvalds  * lookup_create - lookup a dentry, creating it if it doesn't exist
23501da177e4SLinus Torvalds  * @nd: nameidata info
23511da177e4SLinus Torvalds  * @is_dir: directory flag
23521da177e4SLinus Torvalds  *
23531da177e4SLinus Torvalds  * Simple function to lookup and return a dentry and create it
23541da177e4SLinus Torvalds  * if it doesn't exist.  Is SMP-safe.
2355c663e5d8SChristoph Hellwig  *
23564ac91378SJan Blunck  * Returns with nd->path.dentry->d_inode->i_mutex locked.
23571da177e4SLinus Torvalds  */
23581da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir)
23591da177e4SLinus Torvalds {
2360c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
23611da177e4SLinus Torvalds 
23624ac91378SJan Blunck 	mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2363c663e5d8SChristoph Hellwig 	/*
2364c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2365c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2366c663e5d8SChristoph Hellwig 	 */
23671da177e4SLinus Torvalds 	if (nd->last_type != LAST_NORM)
23681da177e4SLinus Torvalds 		goto fail;
23691da177e4SLinus Torvalds 	nd->flags &= ~LOOKUP_PARENT;
23703516586aSAl Viro 	nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2371a634904aSASANO Masahiro 	nd->intent.open.flags = O_EXCL;
2372c663e5d8SChristoph Hellwig 
2373c663e5d8SChristoph Hellwig 	/*
2374c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2375c663e5d8SChristoph Hellwig 	 */
237649705b77SChristoph Hellwig 	dentry = lookup_hash(nd);
23771da177e4SLinus Torvalds 	if (IS_ERR(dentry))
23781da177e4SLinus Torvalds 		goto fail;
2379c663e5d8SChristoph Hellwig 
2380e9baf6e5SAl Viro 	if (dentry->d_inode)
2381e9baf6e5SAl Viro 		goto eexist;
2382c663e5d8SChristoph Hellwig 	/*
2383c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2384c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2385c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2386c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2387c663e5d8SChristoph Hellwig 	 */
2388e9baf6e5SAl Viro 	if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
23891da177e4SLinus Torvalds 		dput(dentry);
23901da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2391e9baf6e5SAl Viro 	}
2392e9baf6e5SAl Viro 	return dentry;
2393e9baf6e5SAl Viro eexist:
2394e9baf6e5SAl Viro 	dput(dentry);
2395e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
23961da177e4SLinus Torvalds fail:
23971da177e4SLinus Torvalds 	return dentry;
23981da177e4SLinus Torvalds }
2399f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create);
24001da177e4SLinus Torvalds 
24011da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
24021da177e4SLinus Torvalds {
2403a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
24041da177e4SLinus Torvalds 
24051da177e4SLinus Torvalds 	if (error)
24061da177e4SLinus Torvalds 		return error;
24071da177e4SLinus Torvalds 
24081da177e4SLinus Torvalds 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
24091da177e4SLinus Torvalds 		return -EPERM;
24101da177e4SLinus Torvalds 
2411acfa4380SAl Viro 	if (!dir->i_op->mknod)
24121da177e4SLinus Torvalds 		return -EPERM;
24131da177e4SLinus Torvalds 
241408ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
241508ce5f16SSerge E. Hallyn 	if (error)
241608ce5f16SSerge E. Hallyn 		return error;
241708ce5f16SSerge E. Hallyn 
24181da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
24191da177e4SLinus Torvalds 	if (error)
24201da177e4SLinus Torvalds 		return error;
24211da177e4SLinus Torvalds 
24221da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2423a74574aaSStephen Smalley 	if (!error)
2424f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
24251da177e4SLinus Torvalds 	return error;
24261da177e4SLinus Torvalds }
24271da177e4SLinus Torvalds 
2428463c3197SDave Hansen static int may_mknod(mode_t mode)
2429463c3197SDave Hansen {
2430463c3197SDave Hansen 	switch (mode & S_IFMT) {
2431463c3197SDave Hansen 	case S_IFREG:
2432463c3197SDave Hansen 	case S_IFCHR:
2433463c3197SDave Hansen 	case S_IFBLK:
2434463c3197SDave Hansen 	case S_IFIFO:
2435463c3197SDave Hansen 	case S_IFSOCK:
2436463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2437463c3197SDave Hansen 		return 0;
2438463c3197SDave Hansen 	case S_IFDIR:
2439463c3197SDave Hansen 		return -EPERM;
2440463c3197SDave Hansen 	default:
2441463c3197SDave Hansen 		return -EINVAL;
2442463c3197SDave Hansen 	}
2443463c3197SDave Hansen }
2444463c3197SDave Hansen 
24452e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
24462e4d0924SHeiko Carstens 		unsigned, dev)
24471da177e4SLinus Torvalds {
24482ad94ae6SAl Viro 	int error;
24491da177e4SLinus Torvalds 	char *tmp;
24501da177e4SLinus Torvalds 	struct dentry *dentry;
24511da177e4SLinus Torvalds 	struct nameidata nd;
24521da177e4SLinus Torvalds 
24531da177e4SLinus Torvalds 	if (S_ISDIR(mode))
24541da177e4SLinus Torvalds 		return -EPERM;
24551da177e4SLinus Torvalds 
24562ad94ae6SAl Viro 	error = user_path_parent(dfd, filename, &nd, &tmp);
24571da177e4SLinus Torvalds 	if (error)
24582ad94ae6SAl Viro 		return error;
24592ad94ae6SAl Viro 
24601da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
2461463c3197SDave Hansen 	if (IS_ERR(dentry)) {
24621da177e4SLinus Torvalds 		error = PTR_ERR(dentry);
2463463c3197SDave Hansen 		goto out_unlock;
2464463c3197SDave Hansen 	}
24654ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2466ce3b0f8dSAl Viro 		mode &= ~current_umask();
2467463c3197SDave Hansen 	error = may_mknod(mode);
2468463c3197SDave Hansen 	if (error)
2469463c3197SDave Hansen 		goto out_dput;
2470463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2471463c3197SDave Hansen 	if (error)
2472463c3197SDave Hansen 		goto out_dput;
2473be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd.path, dentry, mode, dev);
2474be6d3e56SKentaro Takeda 	if (error)
2475be6d3e56SKentaro Takeda 		goto out_drop_write;
24761da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
24771da177e4SLinus Torvalds 		case 0: case S_IFREG:
24784ac91378SJan Blunck 			error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
24791da177e4SLinus Torvalds 			break;
24801da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
24814ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
24821da177e4SLinus Torvalds 					new_decode_dev(dev));
24831da177e4SLinus Torvalds 			break;
24841da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
24854ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
24861da177e4SLinus Torvalds 			break;
24871da177e4SLinus Torvalds 	}
2488be6d3e56SKentaro Takeda out_drop_write:
2489463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2490463c3197SDave Hansen out_dput:
24911da177e4SLinus Torvalds 	dput(dentry);
2492463c3197SDave Hansen out_unlock:
24934ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
24941d957f9bSJan Blunck 	path_put(&nd.path);
24951da177e4SLinus Torvalds 	putname(tmp);
24961da177e4SLinus Torvalds 
24971da177e4SLinus Torvalds 	return error;
24981da177e4SLinus Torvalds }
24991da177e4SLinus Torvalds 
25003480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
25015590ff0dSUlrich Drepper {
25025590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
25035590ff0dSUlrich Drepper }
25045590ff0dSUlrich Drepper 
25051da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
25061da177e4SLinus Torvalds {
2507a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
25081da177e4SLinus Torvalds 
25091da177e4SLinus Torvalds 	if (error)
25101da177e4SLinus Torvalds 		return error;
25111da177e4SLinus Torvalds 
2512acfa4380SAl Viro 	if (!dir->i_op->mkdir)
25131da177e4SLinus Torvalds 		return -EPERM;
25141da177e4SLinus Torvalds 
25151da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
25161da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
25171da177e4SLinus Torvalds 	if (error)
25181da177e4SLinus Torvalds 		return error;
25191da177e4SLinus Torvalds 
25201da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2521a74574aaSStephen Smalley 	if (!error)
2522f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
25231da177e4SLinus Torvalds 	return error;
25241da177e4SLinus Torvalds }
25251da177e4SLinus Torvalds 
25262e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
25271da177e4SLinus Torvalds {
25281da177e4SLinus Torvalds 	int error = 0;
25291da177e4SLinus Torvalds 	char * tmp;
25306902d925SDave Hansen 	struct dentry *dentry;
25316902d925SDave Hansen 	struct nameidata nd;
25321da177e4SLinus Torvalds 
25332ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &tmp);
25342ad94ae6SAl Viro 	if (error)
25356902d925SDave Hansen 		goto out_err;
25361da177e4SLinus Torvalds 
25371da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 1);
25381da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
25396902d925SDave Hansen 	if (IS_ERR(dentry))
25406902d925SDave Hansen 		goto out_unlock;
25416902d925SDave Hansen 
25424ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2543ce3b0f8dSAl Viro 		mode &= ~current_umask();
2544463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2545463c3197SDave Hansen 	if (error)
2546463c3197SDave Hansen 		goto out_dput;
2547be6d3e56SKentaro Takeda 	error = security_path_mkdir(&nd.path, dentry, mode);
2548be6d3e56SKentaro Takeda 	if (error)
2549be6d3e56SKentaro Takeda 		goto out_drop_write;
25504ac91378SJan Blunck 	error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2551be6d3e56SKentaro Takeda out_drop_write:
2552463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2553463c3197SDave Hansen out_dput:
25541da177e4SLinus Torvalds 	dput(dentry);
25556902d925SDave Hansen out_unlock:
25564ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
25571d957f9bSJan Blunck 	path_put(&nd.path);
25581da177e4SLinus Torvalds 	putname(tmp);
25596902d925SDave Hansen out_err:
25601da177e4SLinus Torvalds 	return error;
25611da177e4SLinus Torvalds }
25621da177e4SLinus Torvalds 
25633cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
25645590ff0dSUlrich Drepper {
25655590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
25665590ff0dSUlrich Drepper }
25675590ff0dSUlrich Drepper 
25681da177e4SLinus Torvalds /*
25691da177e4SLinus Torvalds  * We try to drop the dentry early: we should have
25701da177e4SLinus Torvalds  * a usage count of 2 if we're the only user of this
25711da177e4SLinus Torvalds  * dentry, and if that is true (possibly after pruning
25721da177e4SLinus Torvalds  * the dcache), then we drop the dentry now.
25731da177e4SLinus Torvalds  *
25741da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
25751da177e4SLinus Torvalds  * do a
25761da177e4SLinus Torvalds  *
25771da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
25781da177e4SLinus Torvalds  *		return -EBUSY;
25791da177e4SLinus Torvalds  *
25801da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
25811da177e4SLinus Torvalds  * that is still in use by something else..
25821da177e4SLinus Torvalds  */
25831da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
25841da177e4SLinus Torvalds {
25851da177e4SLinus Torvalds 	dget(dentry);
25861da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
25871da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
2588b7ab39f6SNick Piggin 	if (dentry->d_count == 2)
25891da177e4SLinus Torvalds 		__d_drop(dentry);
25901da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
25911da177e4SLinus Torvalds }
25921da177e4SLinus Torvalds 
25931da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
25941da177e4SLinus Torvalds {
25951da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
25961da177e4SLinus Torvalds 
25971da177e4SLinus Torvalds 	if (error)
25981da177e4SLinus Torvalds 		return error;
25991da177e4SLinus Torvalds 
2600acfa4380SAl Viro 	if (!dir->i_op->rmdir)
26011da177e4SLinus Torvalds 		return -EPERM;
26021da177e4SLinus Torvalds 
26031b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
26041da177e4SLinus Torvalds 	dentry_unhash(dentry);
26051da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
26061da177e4SLinus Torvalds 		error = -EBUSY;
26071da177e4SLinus Torvalds 	else {
26081da177e4SLinus Torvalds 		error = security_inode_rmdir(dir, dentry);
26091da177e4SLinus Torvalds 		if (!error) {
26101da177e4SLinus Torvalds 			error = dir->i_op->rmdir(dir, dentry);
2611d83c49f3SAl Viro 			if (!error) {
26121da177e4SLinus Torvalds 				dentry->d_inode->i_flags |= S_DEAD;
2613d83c49f3SAl Viro 				dont_mount(dentry);
2614d83c49f3SAl Viro 			}
26151da177e4SLinus Torvalds 		}
26161da177e4SLinus Torvalds 	}
26171b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
26181da177e4SLinus Torvalds 	if (!error) {
26191da177e4SLinus Torvalds 		d_delete(dentry);
26201da177e4SLinus Torvalds 	}
26211da177e4SLinus Torvalds 	dput(dentry);
26221da177e4SLinus Torvalds 
26231da177e4SLinus Torvalds 	return error;
26241da177e4SLinus Torvalds }
26251da177e4SLinus Torvalds 
26265590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
26271da177e4SLinus Torvalds {
26281da177e4SLinus Torvalds 	int error = 0;
26291da177e4SLinus Torvalds 	char * name;
26301da177e4SLinus Torvalds 	struct dentry *dentry;
26311da177e4SLinus Torvalds 	struct nameidata nd;
26321da177e4SLinus Torvalds 
26332ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
26341da177e4SLinus Torvalds 	if (error)
26352ad94ae6SAl Viro 		return error;
26361da177e4SLinus Torvalds 
26371da177e4SLinus Torvalds 	switch(nd.last_type) {
26381da177e4SLinus Torvalds 	case LAST_DOTDOT:
26391da177e4SLinus Torvalds 		error = -ENOTEMPTY;
26401da177e4SLinus Torvalds 		goto exit1;
26411da177e4SLinus Torvalds 	case LAST_DOT:
26421da177e4SLinus Torvalds 		error = -EINVAL;
26431da177e4SLinus Torvalds 		goto exit1;
26441da177e4SLinus Torvalds 	case LAST_ROOT:
26451da177e4SLinus Torvalds 		error = -EBUSY;
26461da177e4SLinus Torvalds 		goto exit1;
26471da177e4SLinus Torvalds 	}
26480612d9fbSOGAWA Hirofumi 
26490612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
26500612d9fbSOGAWA Hirofumi 
26514ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
265249705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
26531da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
26546902d925SDave Hansen 	if (IS_ERR(dentry))
26556902d925SDave Hansen 		goto exit2;
26560622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
26570622753bSDave Hansen 	if (error)
26580622753bSDave Hansen 		goto exit3;
2659be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2660be6d3e56SKentaro Takeda 	if (error)
2661be6d3e56SKentaro Takeda 		goto exit4;
26624ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2663be6d3e56SKentaro Takeda exit4:
26640622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
26650622753bSDave Hansen exit3:
26661da177e4SLinus Torvalds 	dput(dentry);
26676902d925SDave Hansen exit2:
26684ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
26691da177e4SLinus Torvalds exit1:
26701d957f9bSJan Blunck 	path_put(&nd.path);
26711da177e4SLinus Torvalds 	putname(name);
26721da177e4SLinus Torvalds 	return error;
26731da177e4SLinus Torvalds }
26741da177e4SLinus Torvalds 
26753cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
26765590ff0dSUlrich Drepper {
26775590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
26785590ff0dSUlrich Drepper }
26795590ff0dSUlrich Drepper 
26801da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
26811da177e4SLinus Torvalds {
26821da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
26831da177e4SLinus Torvalds 
26841da177e4SLinus Torvalds 	if (error)
26851da177e4SLinus Torvalds 		return error;
26861da177e4SLinus Torvalds 
2687acfa4380SAl Viro 	if (!dir->i_op->unlink)
26881da177e4SLinus Torvalds 		return -EPERM;
26891da177e4SLinus Torvalds 
26901b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
26911da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
26921da177e4SLinus Torvalds 		error = -EBUSY;
26931da177e4SLinus Torvalds 	else {
26941da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2695bec1052eSAl Viro 		if (!error) {
26961da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2697bec1052eSAl Viro 			if (!error)
2698d83c49f3SAl Viro 				dont_mount(dentry);
2699bec1052eSAl Viro 		}
27001da177e4SLinus Torvalds 	}
27011b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
27021da177e4SLinus Torvalds 
27031da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
27041da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2705ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
27061da177e4SLinus Torvalds 		d_delete(dentry);
27071da177e4SLinus Torvalds 	}
27080eeca283SRobert Love 
27091da177e4SLinus Torvalds 	return error;
27101da177e4SLinus Torvalds }
27111da177e4SLinus Torvalds 
27121da177e4SLinus Torvalds /*
27131da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
27141b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
27151da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
27161da177e4SLinus Torvalds  * while waiting on the I/O.
27171da177e4SLinus Torvalds  */
27185590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
27191da177e4SLinus Torvalds {
27202ad94ae6SAl Viro 	int error;
27211da177e4SLinus Torvalds 	char *name;
27221da177e4SLinus Torvalds 	struct dentry *dentry;
27231da177e4SLinus Torvalds 	struct nameidata nd;
27241da177e4SLinus Torvalds 	struct inode *inode = NULL;
27251da177e4SLinus Torvalds 
27262ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
27271da177e4SLinus Torvalds 	if (error)
27282ad94ae6SAl Viro 		return error;
27292ad94ae6SAl Viro 
27301da177e4SLinus Torvalds 	error = -EISDIR;
27311da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
27321da177e4SLinus Torvalds 		goto exit1;
27330612d9fbSOGAWA Hirofumi 
27340612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
27350612d9fbSOGAWA Hirofumi 
27364ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
273749705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
27381da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
27391da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
27401da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
27411da177e4SLinus Torvalds 		if (nd.last.name[nd.last.len])
27421da177e4SLinus Torvalds 			goto slashes;
27431da177e4SLinus Torvalds 		inode = dentry->d_inode;
27441da177e4SLinus Torvalds 		if (inode)
27457de9c6eeSAl Viro 			ihold(inode);
27460622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
27470622753bSDave Hansen 		if (error)
27480622753bSDave Hansen 			goto exit2;
2749be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2750be6d3e56SKentaro Takeda 		if (error)
2751be6d3e56SKentaro Takeda 			goto exit3;
27524ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2753be6d3e56SKentaro Takeda exit3:
27540622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
27551da177e4SLinus Torvalds 	exit2:
27561da177e4SLinus Torvalds 		dput(dentry);
27571da177e4SLinus Torvalds 	}
27584ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
27591da177e4SLinus Torvalds 	if (inode)
27601da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
27611da177e4SLinus Torvalds exit1:
27621d957f9bSJan Blunck 	path_put(&nd.path);
27631da177e4SLinus Torvalds 	putname(name);
27641da177e4SLinus Torvalds 	return error;
27651da177e4SLinus Torvalds 
27661da177e4SLinus Torvalds slashes:
27671da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
27681da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
27691da177e4SLinus Torvalds 	goto exit2;
27701da177e4SLinus Torvalds }
27711da177e4SLinus Torvalds 
27722e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
27735590ff0dSUlrich Drepper {
27745590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
27755590ff0dSUlrich Drepper 		return -EINVAL;
27765590ff0dSUlrich Drepper 
27775590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
27785590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
27795590ff0dSUlrich Drepper 
27805590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
27815590ff0dSUlrich Drepper }
27825590ff0dSUlrich Drepper 
27833480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
27845590ff0dSUlrich Drepper {
27855590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
27865590ff0dSUlrich Drepper }
27875590ff0dSUlrich Drepper 
2788db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
27891da177e4SLinus Torvalds {
2790a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
27911da177e4SLinus Torvalds 
27921da177e4SLinus Torvalds 	if (error)
27931da177e4SLinus Torvalds 		return error;
27941da177e4SLinus Torvalds 
2795acfa4380SAl Viro 	if (!dir->i_op->symlink)
27961da177e4SLinus Torvalds 		return -EPERM;
27971da177e4SLinus Torvalds 
27981da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
27991da177e4SLinus Torvalds 	if (error)
28001da177e4SLinus Torvalds 		return error;
28011da177e4SLinus Torvalds 
28021da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2803a74574aaSStephen Smalley 	if (!error)
2804f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
28051da177e4SLinus Torvalds 	return error;
28061da177e4SLinus Torvalds }
28071da177e4SLinus Torvalds 
28082e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
28092e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
28101da177e4SLinus Torvalds {
28112ad94ae6SAl Viro 	int error;
28121da177e4SLinus Torvalds 	char *from;
28131da177e4SLinus Torvalds 	char *to;
28146902d925SDave Hansen 	struct dentry *dentry;
28156902d925SDave Hansen 	struct nameidata nd;
28161da177e4SLinus Torvalds 
28171da177e4SLinus Torvalds 	from = getname(oldname);
28181da177e4SLinus Torvalds 	if (IS_ERR(from))
28191da177e4SLinus Torvalds 		return PTR_ERR(from);
28202ad94ae6SAl Viro 
28212ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
28222ad94ae6SAl Viro 	if (error)
28236902d925SDave Hansen 		goto out_putname;
28241da177e4SLinus Torvalds 
28251da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
28261da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
28276902d925SDave Hansen 	if (IS_ERR(dentry))
28286902d925SDave Hansen 		goto out_unlock;
28296902d925SDave Hansen 
283075c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
283175c3f29dSDave Hansen 	if (error)
283275c3f29dSDave Hansen 		goto out_dput;
2833be6d3e56SKentaro Takeda 	error = security_path_symlink(&nd.path, dentry, from);
2834be6d3e56SKentaro Takeda 	if (error)
2835be6d3e56SKentaro Takeda 		goto out_drop_write;
2836db2e747bSMiklos Szeredi 	error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2837be6d3e56SKentaro Takeda out_drop_write:
283875c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
283975c3f29dSDave Hansen out_dput:
28401da177e4SLinus Torvalds 	dput(dentry);
28416902d925SDave Hansen out_unlock:
28424ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
28431d957f9bSJan Blunck 	path_put(&nd.path);
28441da177e4SLinus Torvalds 	putname(to);
28456902d925SDave Hansen out_putname:
28461da177e4SLinus Torvalds 	putname(from);
28471da177e4SLinus Torvalds 	return error;
28481da177e4SLinus Torvalds }
28491da177e4SLinus Torvalds 
28503480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
28515590ff0dSUlrich Drepper {
28525590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
28535590ff0dSUlrich Drepper }
28545590ff0dSUlrich Drepper 
28551da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
28561da177e4SLinus Torvalds {
28571da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
28581da177e4SLinus Torvalds 	int error;
28591da177e4SLinus Torvalds 
28601da177e4SLinus Torvalds 	if (!inode)
28611da177e4SLinus Torvalds 		return -ENOENT;
28621da177e4SLinus Torvalds 
2863a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
28641da177e4SLinus Torvalds 	if (error)
28651da177e4SLinus Torvalds 		return error;
28661da177e4SLinus Torvalds 
28671da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
28681da177e4SLinus Torvalds 		return -EXDEV;
28691da177e4SLinus Torvalds 
28701da177e4SLinus Torvalds 	/*
28711da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
28721da177e4SLinus Torvalds 	 */
28731da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
28741da177e4SLinus Torvalds 		return -EPERM;
2875acfa4380SAl Viro 	if (!dir->i_op->link)
28761da177e4SLinus Torvalds 		return -EPERM;
28777e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
28781da177e4SLinus Torvalds 		return -EPERM;
28791da177e4SLinus Torvalds 
28801da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
28811da177e4SLinus Torvalds 	if (error)
28821da177e4SLinus Torvalds 		return error;
28831da177e4SLinus Torvalds 
28847e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
28851da177e4SLinus Torvalds 	error = dir->i_op->link(old_dentry, dir, new_dentry);
28867e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
2887e31e14ecSStephen Smalley 	if (!error)
28887e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
28891da177e4SLinus Torvalds 	return error;
28901da177e4SLinus Torvalds }
28911da177e4SLinus Torvalds 
28921da177e4SLinus Torvalds /*
28931da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
28941da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
28951da177e4SLinus Torvalds  * newname.  --KAB
28961da177e4SLinus Torvalds  *
28971da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
28981da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
28991da177e4SLinus Torvalds  * and other special files.  --ADM
29001da177e4SLinus Torvalds  */
29012e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
29022e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
29031da177e4SLinus Torvalds {
29041da177e4SLinus Torvalds 	struct dentry *new_dentry;
29052d8f3038SAl Viro 	struct nameidata nd;
29062d8f3038SAl Viro 	struct path old_path;
29071da177e4SLinus Torvalds 	int error;
29081da177e4SLinus Torvalds 	char *to;
29091da177e4SLinus Torvalds 
291045c9b11aSUlrich Drepper 	if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
2911c04030e1SUlrich Drepper 		return -EINVAL;
2912c04030e1SUlrich Drepper 
29132d8f3038SAl Viro 	error = user_path_at(olddfd, oldname,
291445c9b11aSUlrich Drepper 			     flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
29152d8f3038SAl Viro 			     &old_path);
29161da177e4SLinus Torvalds 	if (error)
29172ad94ae6SAl Viro 		return error;
29182ad94ae6SAl Viro 
29192ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
29201da177e4SLinus Torvalds 	if (error)
29211da177e4SLinus Torvalds 		goto out;
29221da177e4SLinus Torvalds 	error = -EXDEV;
29232d8f3038SAl Viro 	if (old_path.mnt != nd.path.mnt)
29241da177e4SLinus Torvalds 		goto out_release;
29251da177e4SLinus Torvalds 	new_dentry = lookup_create(&nd, 0);
29261da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
29276902d925SDave Hansen 	if (IS_ERR(new_dentry))
29286902d925SDave Hansen 		goto out_unlock;
292975c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
293075c3f29dSDave Hansen 	if (error)
293175c3f29dSDave Hansen 		goto out_dput;
2932be6d3e56SKentaro Takeda 	error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2933be6d3e56SKentaro Takeda 	if (error)
2934be6d3e56SKentaro Takeda 		goto out_drop_write;
29352d8f3038SAl Viro 	error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
2936be6d3e56SKentaro Takeda out_drop_write:
293775c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
293875c3f29dSDave Hansen out_dput:
29391da177e4SLinus Torvalds 	dput(new_dentry);
29406902d925SDave Hansen out_unlock:
29414ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
29421da177e4SLinus Torvalds out_release:
29431d957f9bSJan Blunck 	path_put(&nd.path);
29442ad94ae6SAl Viro 	putname(to);
29451da177e4SLinus Torvalds out:
29462d8f3038SAl Viro 	path_put(&old_path);
29471da177e4SLinus Torvalds 
29481da177e4SLinus Torvalds 	return error;
29491da177e4SLinus Torvalds }
29501da177e4SLinus Torvalds 
29513480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
29525590ff0dSUlrich Drepper {
2953c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
29545590ff0dSUlrich Drepper }
29555590ff0dSUlrich Drepper 
29561da177e4SLinus Torvalds /*
29571da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
29581da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
29591da177e4SLinus Torvalds  * Problems:
29601da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
29611da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
29621da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
2963a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
29641da177e4SLinus Torvalds  *	   story.
29651da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
29661b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
29671da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
29681da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
2969a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
29701da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
29711da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
29721da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
2973a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
29741da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
29751da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
29761da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
29771da177e4SLinus Torvalds  *	d) some filesystems don't support opened-but-unlinked directories,
29781da177e4SLinus Torvalds  *	   either because of layout or because they are not ready to deal with
29791da177e4SLinus Torvalds  *	   all cases correctly. The latter will be fixed (taking this sort of
29801da177e4SLinus Torvalds  *	   stuff into VFS), but the former is not going away. Solution: the same
29811da177e4SLinus Torvalds  *	   trick as in rmdir().
29821da177e4SLinus Torvalds  *	e) conversion from fhandle to dentry may come in the wrong moment - when
29831b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
29841da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2985c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
29861da177e4SLinus Torvalds  *	   locking].
29871da177e4SLinus Torvalds  */
298875c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
29891da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
29901da177e4SLinus Torvalds {
29911da177e4SLinus Torvalds 	int error = 0;
29921da177e4SLinus Torvalds 	struct inode *target;
29931da177e4SLinus Torvalds 
29941da177e4SLinus Torvalds 	/*
29951da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
29961da177e4SLinus Torvalds 	 * we'll need to flip '..'.
29971da177e4SLinus Torvalds 	 */
29981da177e4SLinus Torvalds 	if (new_dir != old_dir) {
2999f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
30001da177e4SLinus Torvalds 		if (error)
30011da177e4SLinus Torvalds 			return error;
30021da177e4SLinus Torvalds 	}
30031da177e4SLinus Torvalds 
30041da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30051da177e4SLinus Torvalds 	if (error)
30061da177e4SLinus Torvalds 		return error;
30071da177e4SLinus Torvalds 
30081da177e4SLinus Torvalds 	target = new_dentry->d_inode;
3009d83c49f3SAl Viro 	if (target)
30101b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
30111da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
30121da177e4SLinus Torvalds 		error = -EBUSY;
3013d83c49f3SAl Viro 	else {
3014d83c49f3SAl Viro 		if (target)
3015d83c49f3SAl Viro 			dentry_unhash(new_dentry);
30161da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3017d83c49f3SAl Viro 	}
30181da177e4SLinus Torvalds 	if (target) {
3019d83c49f3SAl Viro 		if (!error) {
30201da177e4SLinus Torvalds 			target->i_flags |= S_DEAD;
3021d83c49f3SAl Viro 			dont_mount(new_dentry);
3022d83c49f3SAl Viro 		}
30231b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
30241da177e4SLinus Torvalds 		if (d_unhashed(new_dentry))
30251da177e4SLinus Torvalds 			d_rehash(new_dentry);
30261da177e4SLinus Torvalds 		dput(new_dentry);
30271da177e4SLinus Torvalds 	}
3028e31e14ecSStephen Smalley 	if (!error)
3029349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30301da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
30311da177e4SLinus Torvalds 	return error;
30321da177e4SLinus Torvalds }
30331da177e4SLinus Torvalds 
303475c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
30351da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
30361da177e4SLinus Torvalds {
30371da177e4SLinus Torvalds 	struct inode *target;
30381da177e4SLinus Torvalds 	int error;
30391da177e4SLinus Torvalds 
30401da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30411da177e4SLinus Torvalds 	if (error)
30421da177e4SLinus Torvalds 		return error;
30431da177e4SLinus Torvalds 
30441da177e4SLinus Torvalds 	dget(new_dentry);
30451da177e4SLinus Torvalds 	target = new_dentry->d_inode;
30461da177e4SLinus Torvalds 	if (target)
30471b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
30481da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
30491da177e4SLinus Torvalds 		error = -EBUSY;
30501da177e4SLinus Torvalds 	else
30511da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
30521da177e4SLinus Torvalds 	if (!error) {
3053bec1052eSAl Viro 		if (target)
3054d83c49f3SAl Viro 			dont_mount(new_dentry);
3055349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30561da177e4SLinus Torvalds 			d_move(old_dentry, new_dentry);
30571da177e4SLinus Torvalds 	}
30581da177e4SLinus Torvalds 	if (target)
30591b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
30601da177e4SLinus Torvalds 	dput(new_dentry);
30611da177e4SLinus Torvalds 	return error;
30621da177e4SLinus Torvalds }
30631da177e4SLinus Torvalds 
30641da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
30651da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
30661da177e4SLinus Torvalds {
30671da177e4SLinus Torvalds 	int error;
30681da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
306959b0df21SEric Paris 	const unsigned char *old_name;
30701da177e4SLinus Torvalds 
30711da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
30721da177e4SLinus Torvalds  		return 0;
30731da177e4SLinus Torvalds 
30741da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
30751da177e4SLinus Torvalds 	if (error)
30761da177e4SLinus Torvalds 		return error;
30771da177e4SLinus Torvalds 
30781da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3079a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
30801da177e4SLinus Torvalds 	else
30811da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
30821da177e4SLinus Torvalds 	if (error)
30831da177e4SLinus Torvalds 		return error;
30841da177e4SLinus Torvalds 
3085acfa4380SAl Viro 	if (!old_dir->i_op->rename)
30861da177e4SLinus Torvalds 		return -EPERM;
30871da177e4SLinus Torvalds 
30880eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
30890eeca283SRobert Love 
30901da177e4SLinus Torvalds 	if (is_dir)
30911da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
30921da177e4SLinus Torvalds 	else
30931da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3094123df294SAl Viro 	if (!error)
3095123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
30965a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
30970eeca283SRobert Love 	fsnotify_oldname_free(old_name);
30980eeca283SRobert Love 
30991da177e4SLinus Torvalds 	return error;
31001da177e4SLinus Torvalds }
31011da177e4SLinus Torvalds 
31022e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
31032e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
31041da177e4SLinus Torvalds {
31051da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
31061da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
31071da177e4SLinus Torvalds 	struct dentry *trap;
31081da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
31092ad94ae6SAl Viro 	char *from;
31102ad94ae6SAl Viro 	char *to;
31112ad94ae6SAl Viro 	int error;
31121da177e4SLinus Torvalds 
31132ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
31141da177e4SLinus Torvalds 	if (error)
31151da177e4SLinus Torvalds 		goto exit;
31161da177e4SLinus Torvalds 
31172ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
31181da177e4SLinus Torvalds 	if (error)
31191da177e4SLinus Torvalds 		goto exit1;
31201da177e4SLinus Torvalds 
31211da177e4SLinus Torvalds 	error = -EXDEV;
31224ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
31231da177e4SLinus Torvalds 		goto exit2;
31241da177e4SLinus Torvalds 
31254ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
31261da177e4SLinus Torvalds 	error = -EBUSY;
31271da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
31281da177e4SLinus Torvalds 		goto exit2;
31291da177e4SLinus Torvalds 
31304ac91378SJan Blunck 	new_dir = newnd.path.dentry;
31311da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
31321da177e4SLinus Torvalds 		goto exit2;
31331da177e4SLinus Torvalds 
31340612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
31350612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
31364e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
31370612d9fbSOGAWA Hirofumi 
31381da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
31391da177e4SLinus Torvalds 
314049705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
31411da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
31421da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
31431da177e4SLinus Torvalds 		goto exit3;
31441da177e4SLinus Torvalds 	/* source must exist */
31451da177e4SLinus Torvalds 	error = -ENOENT;
31461da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
31471da177e4SLinus Torvalds 		goto exit4;
31481da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
31491da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
31501da177e4SLinus Torvalds 		error = -ENOTDIR;
31511da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
31521da177e4SLinus Torvalds 			goto exit4;
31531da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
31541da177e4SLinus Torvalds 			goto exit4;
31551da177e4SLinus Torvalds 	}
31561da177e4SLinus Torvalds 	/* source should not be ancestor of target */
31571da177e4SLinus Torvalds 	error = -EINVAL;
31581da177e4SLinus Torvalds 	if (old_dentry == trap)
31591da177e4SLinus Torvalds 		goto exit4;
316049705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
31611da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
31621da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
31631da177e4SLinus Torvalds 		goto exit4;
31641da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
31651da177e4SLinus Torvalds 	error = -ENOTEMPTY;
31661da177e4SLinus Torvalds 	if (new_dentry == trap)
31671da177e4SLinus Torvalds 		goto exit5;
31681da177e4SLinus Torvalds 
31699079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
31709079b1ebSDave Hansen 	if (error)
31719079b1ebSDave Hansen 		goto exit5;
3172be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3173be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3174be6d3e56SKentaro Takeda 	if (error)
3175be6d3e56SKentaro Takeda 		goto exit6;
31761da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
31771da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3178be6d3e56SKentaro Takeda exit6:
31799079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
31801da177e4SLinus Torvalds exit5:
31811da177e4SLinus Torvalds 	dput(new_dentry);
31821da177e4SLinus Torvalds exit4:
31831da177e4SLinus Torvalds 	dput(old_dentry);
31841da177e4SLinus Torvalds exit3:
31851da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
31861da177e4SLinus Torvalds exit2:
31871d957f9bSJan Blunck 	path_put(&newnd.path);
31882ad94ae6SAl Viro 	putname(to);
31891da177e4SLinus Torvalds exit1:
31901d957f9bSJan Blunck 	path_put(&oldnd.path);
31911da177e4SLinus Torvalds 	putname(from);
31922ad94ae6SAl Viro exit:
31931da177e4SLinus Torvalds 	return error;
31941da177e4SLinus Torvalds }
31951da177e4SLinus Torvalds 
3196a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
31975590ff0dSUlrich Drepper {
31985590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
31995590ff0dSUlrich Drepper }
32005590ff0dSUlrich Drepper 
32011da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
32021da177e4SLinus Torvalds {
32031da177e4SLinus Torvalds 	int len;
32041da177e4SLinus Torvalds 
32051da177e4SLinus Torvalds 	len = PTR_ERR(link);
32061da177e4SLinus Torvalds 	if (IS_ERR(link))
32071da177e4SLinus Torvalds 		goto out;
32081da177e4SLinus Torvalds 
32091da177e4SLinus Torvalds 	len = strlen(link);
32101da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
32111da177e4SLinus Torvalds 		len = buflen;
32121da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
32131da177e4SLinus Torvalds 		len = -EFAULT;
32141da177e4SLinus Torvalds out:
32151da177e4SLinus Torvalds 	return len;
32161da177e4SLinus Torvalds }
32171da177e4SLinus Torvalds 
32181da177e4SLinus Torvalds /*
32191da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
32201da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
32211da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
32221da177e4SLinus Torvalds  */
32231da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
32241da177e4SLinus Torvalds {
32251da177e4SLinus Torvalds 	struct nameidata nd;
3226cc314eefSLinus Torvalds 	void *cookie;
3227694a1764SMarcin Slusarz 	int res;
3228cc314eefSLinus Torvalds 
32291da177e4SLinus Torvalds 	nd.depth = 0;
3230cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3231694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3232694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3233694a1764SMarcin Slusarz 
3234694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
32351da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3236cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3237694a1764SMarcin Slusarz 	return res;
32381da177e4SLinus Torvalds }
32391da177e4SLinus Torvalds 
32401da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
32411da177e4SLinus Torvalds {
32421da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
32431da177e4SLinus Torvalds }
32441da177e4SLinus Torvalds 
32451da177e4SLinus Torvalds /* get the link contents into pagecache */
32461da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
32471da177e4SLinus Torvalds {
3248ebd09abbSDuane Griffin 	char *kaddr;
32491da177e4SLinus Torvalds 	struct page *page;
32501da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3251090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
32521da177e4SLinus Torvalds 	if (IS_ERR(page))
32536fe6900eSNick Piggin 		return (char*)page;
32541da177e4SLinus Torvalds 	*ppage = page;
3255ebd09abbSDuane Griffin 	kaddr = kmap(page);
3256ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3257ebd09abbSDuane Griffin 	return kaddr;
32581da177e4SLinus Torvalds }
32591da177e4SLinus Torvalds 
32601da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
32611da177e4SLinus Torvalds {
32621da177e4SLinus Torvalds 	struct page *page = NULL;
32631da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
32641da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
32651da177e4SLinus Torvalds 	if (page) {
32661da177e4SLinus Torvalds 		kunmap(page);
32671da177e4SLinus Torvalds 		page_cache_release(page);
32681da177e4SLinus Torvalds 	}
32691da177e4SLinus Torvalds 	return res;
32701da177e4SLinus Torvalds }
32711da177e4SLinus Torvalds 
3272cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
32731da177e4SLinus Torvalds {
3274cc314eefSLinus Torvalds 	struct page *page = NULL;
32751da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3276cc314eefSLinus Torvalds 	return page;
32771da177e4SLinus Torvalds }
32781da177e4SLinus Torvalds 
3279cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
32801da177e4SLinus Torvalds {
3281cc314eefSLinus Torvalds 	struct page *page = cookie;
3282cc314eefSLinus Torvalds 
3283cc314eefSLinus Torvalds 	if (page) {
32841da177e4SLinus Torvalds 		kunmap(page);
32851da177e4SLinus Torvalds 		page_cache_release(page);
32861da177e4SLinus Torvalds 	}
32871da177e4SLinus Torvalds }
32881da177e4SLinus Torvalds 
328954566b2cSNick Piggin /*
329054566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
329154566b2cSNick Piggin  */
329254566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
32931da177e4SLinus Torvalds {
32941da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
32950adb25d2SKirill Korotaev 	struct page *page;
3296afddba49SNick Piggin 	void *fsdata;
3297beb497abSDmitriy Monakhov 	int err;
32981da177e4SLinus Torvalds 	char *kaddr;
329954566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
330054566b2cSNick Piggin 	if (nofs)
330154566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
33021da177e4SLinus Torvalds 
33037e53cac4SNeilBrown retry:
3304afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
330554566b2cSNick Piggin 				flags, &page, &fsdata);
33061da177e4SLinus Torvalds 	if (err)
3307afddba49SNick Piggin 		goto fail;
3308afddba49SNick Piggin 
33091da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
33101da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
33111da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
3312afddba49SNick Piggin 
3313afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3314afddba49SNick Piggin 							page, fsdata);
33151da177e4SLinus Torvalds 	if (err < 0)
33161da177e4SLinus Torvalds 		goto fail;
3317afddba49SNick Piggin 	if (err < len-1)
3318afddba49SNick Piggin 		goto retry;
3319afddba49SNick Piggin 
33201da177e4SLinus Torvalds 	mark_inode_dirty(inode);
33211da177e4SLinus Torvalds 	return 0;
33221da177e4SLinus Torvalds fail:
33231da177e4SLinus Torvalds 	return err;
33241da177e4SLinus Torvalds }
33251da177e4SLinus Torvalds 
33260adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
33270adb25d2SKirill Korotaev {
33280adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
332954566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
33300adb25d2SKirill Korotaev }
33310adb25d2SKirill Korotaev 
333292e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
33331da177e4SLinus Torvalds 	.readlink	= generic_readlink,
33341da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
33351da177e4SLinus Torvalds 	.put_link	= page_put_link,
33361da177e4SLinus Torvalds };
33371da177e4SLinus Torvalds 
33382d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
3339cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
33401da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
33411da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
33421da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
33431da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
33441da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
33451da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
33461da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
33471da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
33481da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
33490adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
33501da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
33511da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
3352c9c6cac0SAl Viro EXPORT_SYMBOL(kern_path_parent);
3353d1811465SAl Viro EXPORT_SYMBOL(kern_path);
335416f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3355f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
33568c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission);
33571da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
33581da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
33591da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
33601da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
33611da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
33621da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
33631da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
33641da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
33651da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
33661da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
33671da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
33681da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
33691da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
33701da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3371