xref: /openbmc/linux/fs/namei.c (revision 19660af7)
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
7325985edcSLucas De Marchi  * the name is a symlink pointing to a non-existent 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 
139f52e0c11SAl Viro static char *getname_flags(const char __user * filename, int flags)
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) {
150f52e0c11SAl Viro 			if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
1511da177e4SLinus Torvalds 				__putname(tmp);
1521da177e4SLinus Torvalds 				result = ERR_PTR(retval);
1531da177e4SLinus Torvalds 			}
1541da177e4SLinus Torvalds 		}
155f52e0c11SAl Viro 	}
1561da177e4SLinus Torvalds 	audit_getname(result);
1571da177e4SLinus Torvalds 	return result;
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
160f52e0c11SAl Viro char *getname(const char __user * filename)
161f52e0c11SAl Viro {
162f52e0c11SAl Viro 	return getname_flags(filename, 0);
163f52e0c11SAl Viro }
164f52e0c11SAl Viro 
1651da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
1661da177e4SLinus Torvalds void putname(const char *name)
1671da177e4SLinus Torvalds {
1685ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
1691da177e4SLinus Torvalds 		audit_putname(name);
1701da177e4SLinus Torvalds 	else
1711da177e4SLinus Torvalds 		__putname(name);
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds EXPORT_SYMBOL(putname);
1741da177e4SLinus Torvalds #endif
1751da177e4SLinus Torvalds 
1765909ccaaSLinus Torvalds /*
1775909ccaaSLinus Torvalds  * This does basic POSIX ACL permission checking
1785909ccaaSLinus Torvalds  */
179b74c79e9SNick Piggin static int acl_permission_check(struct inode *inode, int mask, unsigned int flags,
180b74c79e9SNick Piggin 		int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
1815909ccaaSLinus Torvalds {
1825909ccaaSLinus Torvalds 	umode_t			mode = inode->i_mode;
1835909ccaaSLinus Torvalds 
1845909ccaaSLinus Torvalds 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
1855909ccaaSLinus Torvalds 
186e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
187e795b717SSerge E. Hallyn 		goto other_perms;
188e795b717SSerge E. Hallyn 
1895909ccaaSLinus Torvalds 	if (current_fsuid() == inode->i_uid)
1905909ccaaSLinus Torvalds 		mode >>= 6;
1915909ccaaSLinus Torvalds 	else {
1925909ccaaSLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
193b74c79e9SNick Piggin 			int error = check_acl(inode, mask, flags);
1945909ccaaSLinus Torvalds 			if (error != -EAGAIN)
1955909ccaaSLinus Torvalds 				return error;
1965909ccaaSLinus Torvalds 		}
1975909ccaaSLinus Torvalds 
1985909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
1995909ccaaSLinus Torvalds 			mode >>= 3;
2005909ccaaSLinus Torvalds 	}
2015909ccaaSLinus Torvalds 
202e795b717SSerge E. Hallyn other_perms:
2035909ccaaSLinus Torvalds 	/*
2045909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
2055909ccaaSLinus Torvalds 	 */
2065909ccaaSLinus Torvalds 	if ((mask & ~mode) == 0)
2075909ccaaSLinus Torvalds 		return 0;
2085909ccaaSLinus Torvalds 	return -EACCES;
2095909ccaaSLinus Torvalds }
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds /**
2121da177e4SLinus Torvalds  * generic_permission -  check for access rights on a Posix-like filesystem
2131da177e4SLinus Torvalds  * @inode:	inode to check access rights for
2141da177e4SLinus Torvalds  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
2151da177e4SLinus Torvalds  * @check_acl:	optional callback to check for Posix ACLs
21639191628SRandy Dunlap  * @flags:	IPERM_FLAG_ flags.
2171da177e4SLinus Torvalds  *
2181da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
2191da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
2201da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
221b74c79e9SNick Piggin  * are used for other things.
222b74c79e9SNick Piggin  *
223b74c79e9SNick Piggin  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
224b74c79e9SNick Piggin  * request cannot be satisfied (eg. requires blocking or too much complexity).
225b74c79e9SNick Piggin  * It would then be called again in ref-walk mode.
2261da177e4SLinus Torvalds  */
227b74c79e9SNick Piggin int generic_permission(struct inode *inode, int mask, unsigned int flags,
228b74c79e9SNick Piggin 	int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
2291da177e4SLinus Torvalds {
2305909ccaaSLinus Torvalds 	int ret;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	/*
2335909ccaaSLinus Torvalds 	 * Do the basic POSIX ACL permission checks.
2341da177e4SLinus Torvalds 	 */
235b74c79e9SNick Piggin 	ret = acl_permission_check(inode, mask, flags, check_acl);
2365909ccaaSLinus Torvalds 	if (ret != -EACCES)
2375909ccaaSLinus Torvalds 		return ret;
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	/*
2401da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
2411da177e4SLinus Torvalds 	 * Executable DACs are overridable if at least one exec bit is set.
2421da177e4SLinus Torvalds 	 */
243f696a365SMiklos Szeredi 	if (!(mask & MAY_EXEC) || execute_ok(inode))
244e795b717SSerge E. Hallyn 		if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
2451da177e4SLinus Torvalds 			return 0;
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	/*
2481da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
2491da177e4SLinus Torvalds 	 */
2507ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
2511da177e4SLinus Torvalds 	if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
252e795b717SSerge E. Hallyn 		if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
2531da177e4SLinus Torvalds 			return 0;
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	return -EACCES;
2561da177e4SLinus Torvalds }
2571da177e4SLinus Torvalds 
258cb23beb5SChristoph Hellwig /**
259cb23beb5SChristoph Hellwig  * inode_permission  -  check for access rights to a given inode
260cb23beb5SChristoph Hellwig  * @inode:	inode to check permission on
261cb23beb5SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
262cb23beb5SChristoph Hellwig  *
263cb23beb5SChristoph Hellwig  * Used to check for read/write/execute permissions on an inode.
264cb23beb5SChristoph Hellwig  * We use "fsuid" for this, letting us set arbitrary permissions
265cb23beb5SChristoph Hellwig  * for filesystem access without changing the "normal" uids which
266cb23beb5SChristoph Hellwig  * are used for other things.
267cb23beb5SChristoph Hellwig  */
268f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask)
2691da177e4SLinus Torvalds {
270e6305c43SAl Viro 	int retval;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	if (mask & MAY_WRITE) {
27322590e41SMiklos Szeredi 		umode_t mode = inode->i_mode;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 		/*
2761da177e4SLinus Torvalds 		 * Nobody gets write access to a read-only fs.
2771da177e4SLinus Torvalds 		 */
2781da177e4SLinus Torvalds 		if (IS_RDONLY(inode) &&
2791da177e4SLinus Torvalds 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
2801da177e4SLinus Torvalds 			return -EROFS;
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 		/*
2831da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
2841da177e4SLinus Torvalds 		 */
2851da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
2861da177e4SLinus Torvalds 			return -EACCES;
2871da177e4SLinus Torvalds 	}
2881da177e4SLinus Torvalds 
289acfa4380SAl Viro 	if (inode->i_op->permission)
290b74c79e9SNick Piggin 		retval = inode->i_op->permission(inode, mask, 0);
291f696a365SMiklos Szeredi 	else
292b74c79e9SNick Piggin 		retval = generic_permission(inode, mask, 0,
293b74c79e9SNick Piggin 				inode->i_op->check_acl);
294f696a365SMiklos Szeredi 
2951da177e4SLinus Torvalds 	if (retval)
2961da177e4SLinus Torvalds 		return retval;
2971da177e4SLinus Torvalds 
29808ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
29908ce5f16SSerge E. Hallyn 	if (retval)
30008ce5f16SSerge E. Hallyn 		return retval;
30108ce5f16SSerge E. Hallyn 
302d09ca739SEric Paris 	return security_inode_permission(inode, mask);
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds 
305e4543eddSChristoph Hellwig /**
3068c744fb8SChristoph Hellwig  * file_permission  -  check for additional access rights to a given file
3078c744fb8SChristoph Hellwig  * @file:	file to check access rights for
3088c744fb8SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
3098c744fb8SChristoph Hellwig  *
3108c744fb8SChristoph Hellwig  * Used to check for read/write/execute permissions on an already opened
3118c744fb8SChristoph Hellwig  * file.
3128c744fb8SChristoph Hellwig  *
3138c744fb8SChristoph Hellwig  * Note:
3148c744fb8SChristoph Hellwig  *	Do not use this function in new code.  All access checks should
315cb23beb5SChristoph Hellwig  *	be done using inode_permission().
3168c744fb8SChristoph Hellwig  */
3178c744fb8SChristoph Hellwig int file_permission(struct file *file, int mask)
3188c744fb8SChristoph Hellwig {
319f419a2e3SAl Viro 	return inode_permission(file->f_path.dentry->d_inode, mask);
3208c744fb8SChristoph Hellwig }
3218c744fb8SChristoph Hellwig 
3221da177e4SLinus Torvalds /*
3231da177e4SLinus Torvalds  * get_write_access() gets write permission for a file.
3241da177e4SLinus Torvalds  * put_write_access() releases this write permission.
3251da177e4SLinus Torvalds  * This is used for regular files.
3261da177e4SLinus Torvalds  * We cannot support write (and maybe mmap read-write shared) accesses and
3271da177e4SLinus Torvalds  * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
3281da177e4SLinus Torvalds  * can have the following values:
3291da177e4SLinus Torvalds  * 0: no writers, no VM_DENYWRITE mappings
3301da177e4SLinus Torvalds  * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
3311da177e4SLinus Torvalds  * > 0: (i_writecount) users are writing to the file.
3321da177e4SLinus Torvalds  *
3331da177e4SLinus Torvalds  * Normally we operate on that counter with atomic_{inc,dec} and it's safe
3341da177e4SLinus Torvalds  * except for the cases where we don't hold i_writecount yet. Then we need to
3351da177e4SLinus Torvalds  * use {get,deny}_write_access() - these functions check the sign and refuse
3361da177e4SLinus Torvalds  * to do the change if sign is wrong. Exclusion between them is provided by
3371da177e4SLinus Torvalds  * the inode->i_lock spinlock.
3381da177e4SLinus Torvalds  */
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds int get_write_access(struct inode * inode)
3411da177e4SLinus Torvalds {
3421da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
3431da177e4SLinus Torvalds 	if (atomic_read(&inode->i_writecount) < 0) {
3441da177e4SLinus Torvalds 		spin_unlock(&inode->i_lock);
3451da177e4SLinus Torvalds 		return -ETXTBSY;
3461da177e4SLinus Torvalds 	}
3471da177e4SLinus Torvalds 	atomic_inc(&inode->i_writecount);
3481da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	return 0;
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds int deny_write_access(struct file * file)
3541da177e4SLinus Torvalds {
3550f7fc9e4SJosef "Jeff" Sipek 	struct inode *inode = file->f_path.dentry->d_inode;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
3581da177e4SLinus Torvalds 	if (atomic_read(&inode->i_writecount) > 0) {
3591da177e4SLinus Torvalds 		spin_unlock(&inode->i_lock);
3601da177e4SLinus Torvalds 		return -ETXTBSY;
3611da177e4SLinus Torvalds 	}
3621da177e4SLinus Torvalds 	atomic_dec(&inode->i_writecount);
3631da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	return 0;
3661da177e4SLinus Torvalds }
3671da177e4SLinus Torvalds 
3681d957f9bSJan Blunck /**
3695dd784d0SJan Blunck  * path_get - get a reference to a path
3705dd784d0SJan Blunck  * @path: path to get the reference to
3715dd784d0SJan Blunck  *
3725dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
3735dd784d0SJan Blunck  */
3745dd784d0SJan Blunck void path_get(struct path *path)
3755dd784d0SJan Blunck {
3765dd784d0SJan Blunck 	mntget(path->mnt);
3775dd784d0SJan Blunck 	dget(path->dentry);
3785dd784d0SJan Blunck }
3795dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
3805dd784d0SJan Blunck 
3815dd784d0SJan Blunck /**
3821d957f9bSJan Blunck  * path_put - put a reference to a path
3831d957f9bSJan Blunck  * @path: path to put the reference to
3841d957f9bSJan Blunck  *
3851d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
3861d957f9bSJan Blunck  */
3871d957f9bSJan Blunck void path_put(struct path *path)
3881da177e4SLinus Torvalds {
3891d957f9bSJan Blunck 	dput(path->dentry);
3901d957f9bSJan Blunck 	mntput(path->mnt);
3911da177e4SLinus Torvalds }
3921d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
3931da177e4SLinus Torvalds 
39419660af7SAl Viro /*
39531e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
39619660af7SAl Viro  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
39719660af7SAl Viro  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
39819660af7SAl Viro  * normal reference counts on dentries and vfsmounts to transition to rcu-walk
39919660af7SAl Viro  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
40019660af7SAl Viro  * got stuck, so ref-walk may continue from there. If this is not successful
40119660af7SAl Viro  * (eg. a seqcount has changed), then failure is returned and it's up to caller
40219660af7SAl Viro  * to restart the path walk from the beginning in ref-walk mode.
40331e6b01fSNick Piggin  */
40431e6b01fSNick Piggin 
40531e6b01fSNick Piggin /**
40619660af7SAl Viro  * unlazy_walk - try to switch to ref-walk mode.
40719660af7SAl Viro  * @nd: nameidata pathwalk data
40819660af7SAl Viro  * @dentry: child of nd->path.dentry or NULL
40939191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
41031e6b01fSNick Piggin  *
41119660af7SAl Viro  * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
41219660af7SAl Viro  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
41319660af7SAl Viro  * @nd or NULL.  Must be called from rcu-walk context.
41431e6b01fSNick Piggin  */
41519660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
41631e6b01fSNick Piggin {
41731e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
41831e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
4195b6ca027SAl Viro 	int want_root = 0;
42031e6b01fSNick Piggin 
42131e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
4225b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
4235b6ca027SAl Viro 		want_root = 1;
42431e6b01fSNick Piggin 		spin_lock(&fs->lock);
42531e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
42631e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
42731e6b01fSNick Piggin 			goto err_root;
42831e6b01fSNick Piggin 	}
42931e6b01fSNick Piggin 	spin_lock(&parent->d_lock);
43019660af7SAl Viro 	if (!dentry) {
43119660af7SAl Viro 		if (!__d_rcu_to_refcount(parent, nd->seq))
43219660af7SAl Viro 			goto err_parent;
43319660af7SAl Viro 		BUG_ON(nd->inode != parent->d_inode);
43419660af7SAl Viro 	} else {
43531e6b01fSNick Piggin 		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
43631e6b01fSNick Piggin 		if (!__d_rcu_to_refcount(dentry, nd->seq))
43719660af7SAl Viro 			goto err_child;
43831e6b01fSNick Piggin 		/*
43919660af7SAl Viro 		 * If the sequence check on the child dentry passed, then
44019660af7SAl Viro 		 * the child has not been removed from its parent. This
44119660af7SAl Viro 		 * means the parent dentry must be valid and able to take
44219660af7SAl Viro 		 * a reference at this point.
44331e6b01fSNick Piggin 		 */
44431e6b01fSNick Piggin 		BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
44531e6b01fSNick Piggin 		BUG_ON(!parent->d_count);
44631e6b01fSNick Piggin 		parent->d_count++;
44731e6b01fSNick Piggin 		spin_unlock(&dentry->d_lock);
44819660af7SAl Viro 	}
44931e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
4505b6ca027SAl Viro 	if (want_root) {
45131e6b01fSNick Piggin 		path_get(&nd->root);
45231e6b01fSNick Piggin 		spin_unlock(&fs->lock);
45331e6b01fSNick Piggin 	}
45431e6b01fSNick Piggin 	mntget(nd->path.mnt);
45531e6b01fSNick Piggin 
45631e6b01fSNick Piggin 	rcu_read_unlock();
45731e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
45831e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
45931e6b01fSNick Piggin 	return 0;
46019660af7SAl Viro 
46119660af7SAl Viro err_child:
46231e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
46319660af7SAl Viro err_parent:
46431e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
46531e6b01fSNick Piggin err_root:
4665b6ca027SAl Viro 	if (want_root)
46731e6b01fSNick Piggin 		spin_unlock(&fs->lock);
46831e6b01fSNick Piggin 	return -ECHILD;
46931e6b01fSNick Piggin }
47031e6b01fSNick Piggin 
47131e6b01fSNick Piggin /**
47231e6b01fSNick Piggin  * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk
47331e6b01fSNick Piggin  * @nd: nameidata pathwalk data to drop
47439191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
47531e6b01fSNick Piggin  *
47631e6b01fSNick Piggin  * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk.
47731e6b01fSNick Piggin  * nd->path should be the final element of the lookup, so nd->root is discarded.
47831e6b01fSNick Piggin  * Must be called from rcu-walk context.
47931e6b01fSNick Piggin  */
48031e6b01fSNick Piggin static int nameidata_drop_rcu_last(struct nameidata *nd)
48131e6b01fSNick Piggin {
48231e6b01fSNick Piggin 	struct dentry *dentry = nd->path.dentry;
48331e6b01fSNick Piggin 
48431e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
48531e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
4865b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
48731e6b01fSNick Piggin 		nd->root.mnt = NULL;
48831e6b01fSNick Piggin 	spin_lock(&dentry->d_lock);
48931e6b01fSNick Piggin 	if (!__d_rcu_to_refcount(dentry, nd->seq))
49031e6b01fSNick Piggin 		goto err_unlock;
49131e6b01fSNick Piggin 	BUG_ON(nd->inode != dentry->d_inode);
49231e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
49331e6b01fSNick Piggin 
49431e6b01fSNick Piggin 	mntget(nd->path.mnt);
49531e6b01fSNick Piggin 
49631e6b01fSNick Piggin 	rcu_read_unlock();
49731e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
49831e6b01fSNick Piggin 
49931e6b01fSNick Piggin 	return 0;
50031e6b01fSNick Piggin 
50131e6b01fSNick Piggin err_unlock:
50231e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
50331e6b01fSNick Piggin 	rcu_read_unlock();
50431e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
50531e6b01fSNick Piggin 	return -ECHILD;
50631e6b01fSNick Piggin }
50731e6b01fSNick Piggin 
50831e6b01fSNick Piggin /**
509834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
510834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
511834f2a4aSTrond Myklebust  */
512834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
513834f2a4aSTrond Myklebust {
5142dab5974SLinus Torvalds 	struct file *file = nd->intent.open.file;
5152dab5974SLinus Torvalds 
5162dab5974SLinus Torvalds 	if (file && !IS_ERR(file)) {
5172dab5974SLinus Torvalds 		if (file->f_path.dentry == NULL)
5182dab5974SLinus Torvalds 			put_filp(file);
519834f2a4aSTrond Myklebust 		else
5202dab5974SLinus Torvalds 			fput(file);
5212dab5974SLinus Torvalds 	}
522834f2a4aSTrond Myklebust }
523834f2a4aSTrond Myklebust 
524f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
52534286d66SNick Piggin {
526f60aef7eSAl Viro 	return dentry->d_op->d_revalidate(dentry, nd);
52734286d66SNick Piggin }
52834286d66SNick Piggin 
529f5e1c1c1SAl Viro static struct dentry *
530bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd)
531bcdc5e01SIan Kent {
532f5e1c1c1SAl Viro 	int status = d_revalidate(dentry, nd);
533bcdc5e01SIan Kent 	if (unlikely(status <= 0)) {
534bcdc5e01SIan Kent 		/*
535bcdc5e01SIan Kent 		 * The dentry failed validation.
536bcdc5e01SIan Kent 		 * If d_revalidate returned 0 attempt to invalidate
537bcdc5e01SIan Kent 		 * the dentry otherwise d_revalidate is asking us
538bcdc5e01SIan Kent 		 * to return a fail status.
539bcdc5e01SIan Kent 		 */
54034286d66SNick Piggin 		if (status < 0) {
54134286d66SNick Piggin 			dput(dentry);
54234286d66SNick Piggin 			dentry = ERR_PTR(status);
543f5e1c1c1SAl Viro 		} else if (!d_invalidate(dentry)) {
544bcdc5e01SIan Kent 			dput(dentry);
545bcdc5e01SIan Kent 			dentry = NULL;
546bcdc5e01SIan Kent 		}
547bcdc5e01SIan Kent 	}
548f5e1c1c1SAl Viro 	return dentry;
549f5e1c1c1SAl Viro }
550f5e1c1c1SAl Viro 
5511da177e4SLinus Torvalds /*
55216c2cd71SAl Viro  * handle_reval_path - force revalidation of a dentry
55339159de2SJeff Layton  *
55439159de2SJeff Layton  * In some situations the path walking code will trust dentries without
55539159de2SJeff Layton  * revalidating them. This causes problems for filesystems that depend on
55639159de2SJeff Layton  * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
55739159de2SJeff Layton  * (which indicates that it's possible for the dentry to go stale), force
55839159de2SJeff Layton  * a d_revalidate call before proceeding.
55939159de2SJeff Layton  *
56039159de2SJeff Layton  * Returns 0 if the revalidation was successful. If the revalidation fails,
56139159de2SJeff Layton  * either return the error returned by d_revalidate or -ESTALE if the
56239159de2SJeff Layton  * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
56339159de2SJeff Layton  * invalidate the dentry. It's up to the caller to handle putting references
56439159de2SJeff Layton  * to the path if necessary.
56539159de2SJeff Layton  */
56616c2cd71SAl Viro static inline int handle_reval_path(struct nameidata *nd)
56739159de2SJeff Layton {
56816c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
56939159de2SJeff Layton 	int status;
57039159de2SJeff Layton 
57116c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
57239159de2SJeff Layton 		return 0;
57339159de2SJeff Layton 
57416c2cd71SAl Viro 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
57516c2cd71SAl Viro 		return 0;
57616c2cd71SAl Viro 
57716c2cd71SAl Viro 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
57816c2cd71SAl Viro 		return 0;
57916c2cd71SAl Viro 
58016c2cd71SAl Viro 	/* Note: we do not d_invalidate() */
58134286d66SNick Piggin 	status = d_revalidate(dentry, nd);
58239159de2SJeff Layton 	if (status > 0)
58339159de2SJeff Layton 		return 0;
58439159de2SJeff Layton 
58516c2cd71SAl Viro 	if (!status)
58639159de2SJeff Layton 		status = -ESTALE;
58716c2cd71SAl Viro 
58839159de2SJeff Layton 	return status;
58939159de2SJeff Layton }
59039159de2SJeff Layton 
59139159de2SJeff Layton /*
592b75b5086SAl Viro  * Short-cut version of permission(), for calling on directories
593b75b5086SAl Viro  * during pathname resolution.  Combines parts of permission()
594b75b5086SAl Viro  * and generic_permission(), and tests ONLY for MAY_EXEC permission.
5951da177e4SLinus Torvalds  *
5961da177e4SLinus Torvalds  * If appropriate, check DAC only.  If not appropriate, or
597b75b5086SAl Viro  * short-cut DAC fails, then call ->permission() to do more
5981da177e4SLinus Torvalds  * complete permission check.
5991da177e4SLinus Torvalds  */
600b74c79e9SNick Piggin static inline int exec_permission(struct inode *inode, unsigned int flags)
6011da177e4SLinus Torvalds {
6025909ccaaSLinus Torvalds 	int ret;
603e795b717SSerge E. Hallyn 	struct user_namespace *ns = inode_userns(inode);
6041da177e4SLinus Torvalds 
605cb9179eaSLinus Torvalds 	if (inode->i_op->permission) {
606b74c79e9SNick Piggin 		ret = inode->i_op->permission(inode, MAY_EXEC, flags);
607b74c79e9SNick Piggin 	} else {
608b74c79e9SNick Piggin 		ret = acl_permission_check(inode, MAY_EXEC, flags,
609b74c79e9SNick Piggin 				inode->i_op->check_acl);
610cb9179eaSLinus Torvalds 	}
611b74c79e9SNick Piggin 	if (likely(!ret))
6121da177e4SLinus Torvalds 		goto ok;
613b74c79e9SNick Piggin 	if (ret == -ECHILD)
61431e6b01fSNick Piggin 		return ret;
6151da177e4SLinus Torvalds 
616e795b717SSerge E. Hallyn 	if (ns_capable(ns, CAP_DAC_OVERRIDE) ||
617e795b717SSerge E. Hallyn 			ns_capable(ns, CAP_DAC_READ_SEARCH))
6181da177e4SLinus Torvalds 		goto ok;
6191da177e4SLinus Torvalds 
6205909ccaaSLinus Torvalds 	return ret;
6211da177e4SLinus Torvalds ok:
622b74c79e9SNick Piggin 	return security_inode_exec_permission(inode, flags);
6231da177e4SLinus Torvalds }
6241da177e4SLinus Torvalds 
6252a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
6262a737871SAl Viro {
627f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
628f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
6292a737871SAl Viro }
6302a737871SAl Viro 
6316de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
6326de88d72SAl Viro 
63331e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
63431e6b01fSNick Piggin {
63531e6b01fSNick Piggin 	if (!nd->root.mnt) {
63631e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
637c28cc364SNick Piggin 		unsigned seq;
638c28cc364SNick Piggin 
639c28cc364SNick Piggin 		do {
640c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
64131e6b01fSNick Piggin 			nd->root = fs->root;
642c1530019STim Chen 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
643c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
64431e6b01fSNick Piggin 	}
64531e6b01fSNick Piggin }
64631e6b01fSNick Piggin 
647f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
6481da177e4SLinus Torvalds {
64931e6b01fSNick Piggin 	int ret;
65031e6b01fSNick Piggin 
6511da177e4SLinus Torvalds 	if (IS_ERR(link))
6521da177e4SLinus Torvalds 		goto fail;
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 	if (*link == '/') {
6552a737871SAl Viro 		set_root(nd);
6561d957f9bSJan Blunck 		path_put(&nd->path);
6572a737871SAl Viro 		nd->path = nd->root;
6582a737871SAl Viro 		path_get(&nd->root);
65916c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
6601da177e4SLinus Torvalds 	}
66131e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
662b4091d5fSChristoph Hellwig 
66331e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
66431e6b01fSNick Piggin 	return ret;
6651da177e4SLinus Torvalds fail:
6661d957f9bSJan Blunck 	path_put(&nd->path);
6671da177e4SLinus Torvalds 	return PTR_ERR(link);
6681da177e4SLinus Torvalds }
6691da177e4SLinus Torvalds 
6701d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
671051d3812SIan Kent {
672051d3812SIan Kent 	dput(path->dentry);
6734ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
674051d3812SIan Kent 		mntput(path->mnt);
675051d3812SIan Kent }
676051d3812SIan Kent 
6777b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
6787b9337aaSNick Piggin 					struct nameidata *nd)
679051d3812SIan Kent {
68031e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
6814ac91378SJan Blunck 		dput(nd->path.dentry);
68231e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
6834ac91378SJan Blunck 			mntput(nd->path.mnt);
6849a229683SHuang Shijie 	}
68531e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
6864ac91378SJan Blunck 	nd->path.dentry = path->dentry;
687051d3812SIan Kent }
688051d3812SIan Kent 
689574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
690574197e0SAl Viro {
691574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
692574197e0SAl Viro 	if (!IS_ERR(cookie) && inode->i_op->put_link)
693574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
694574197e0SAl Viro 	path_put(link);
695574197e0SAl Viro }
696574197e0SAl Viro 
697def4af30SAl Viro static __always_inline int
698574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
6991da177e4SLinus Torvalds {
7001da177e4SLinus Torvalds 	int error;
7017b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
7021da177e4SLinus Torvalds 
703844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
704844a3917SAl Viro 
7050e794589SAl Viro 	if (link->mnt == nd->path.mnt)
7060e794589SAl Viro 		mntget(link->mnt);
7070e794589SAl Viro 
708574197e0SAl Viro 	if (unlikely(current->total_link_count >= 40)) {
709574197e0SAl Viro 		*p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
710574197e0SAl Viro 		path_put(&nd->path);
711574197e0SAl Viro 		return -ELOOP;
712574197e0SAl Viro 	}
713574197e0SAl Viro 	cond_resched();
714574197e0SAl Viro 	current->total_link_count++;
715574197e0SAl Viro 
7167b9337aaSNick Piggin 	touch_atime(link->mnt, dentry);
7171da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
718cd4e91d3SAl Viro 
71936f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
72036f3b4f6SAl Viro 	if (error) {
72136f3b4f6SAl Viro 		*p = ERR_PTR(error); /* no ->put_link(), please */
72236f3b4f6SAl Viro 		path_put(&nd->path);
72336f3b4f6SAl Viro 		return error;
72436f3b4f6SAl Viro 	}
72536f3b4f6SAl Viro 
72686acdca1SAl Viro 	nd->last_type = LAST_BIND;
727def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
728def4af30SAl Viro 	error = PTR_ERR(*p);
729def4af30SAl Viro 	if (!IS_ERR(*p)) {
7301da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
731cc314eefSLinus Torvalds 		error = 0;
7321da177e4SLinus Torvalds 		if (s)
7331da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
734bcda7652SAl Viro 		else if (nd->last_type == LAST_BIND) {
73516c2cd71SAl Viro 			nd->flags |= LOOKUP_JUMPED;
736b21041d0SAl Viro 			nd->inode = nd->path.dentry->d_inode;
737b21041d0SAl Viro 			if (nd->inode->i_op->follow_link) {
738bcda7652SAl Viro 				/* stepped on a _really_ weird one */
739bcda7652SAl Viro 				path_put(&nd->path);
740bcda7652SAl Viro 				error = -ELOOP;
741bcda7652SAl Viro 			}
742bcda7652SAl Viro 		}
7431da177e4SLinus Torvalds 	}
7441da177e4SLinus Torvalds 	return error;
7451da177e4SLinus Torvalds }
7461da177e4SLinus Torvalds 
74731e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
74831e6b01fSNick Piggin {
74931e6b01fSNick Piggin 	struct vfsmount *parent;
75031e6b01fSNick Piggin 	struct dentry *mountpoint;
75131e6b01fSNick Piggin 
75231e6b01fSNick Piggin 	parent = path->mnt->mnt_parent;
75331e6b01fSNick Piggin 	if (parent == path->mnt)
75431e6b01fSNick Piggin 		return 0;
75531e6b01fSNick Piggin 	mountpoint = path->mnt->mnt_mountpoint;
75631e6b01fSNick Piggin 	path->dentry = mountpoint;
75731e6b01fSNick Piggin 	path->mnt = parent;
75831e6b01fSNick Piggin 	return 1;
75931e6b01fSNick Piggin }
76031e6b01fSNick Piggin 
761bab77ebfSAl Viro int follow_up(struct path *path)
7621da177e4SLinus Torvalds {
7631da177e4SLinus Torvalds 	struct vfsmount *parent;
7641da177e4SLinus Torvalds 	struct dentry *mountpoint;
76599b7db7bSNick Piggin 
76699b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
767bab77ebfSAl Viro 	parent = path->mnt->mnt_parent;
768bab77ebfSAl Viro 	if (parent == path->mnt) {
76999b7db7bSNick Piggin 		br_read_unlock(vfsmount_lock);
7701da177e4SLinus Torvalds 		return 0;
7711da177e4SLinus Torvalds 	}
7721da177e4SLinus Torvalds 	mntget(parent);
773bab77ebfSAl Viro 	mountpoint = dget(path->mnt->mnt_mountpoint);
77499b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
775bab77ebfSAl Viro 	dput(path->dentry);
776bab77ebfSAl Viro 	path->dentry = mountpoint;
777bab77ebfSAl Viro 	mntput(path->mnt);
778bab77ebfSAl Viro 	path->mnt = parent;
7791da177e4SLinus Torvalds 	return 1;
7801da177e4SLinus Torvalds }
7811da177e4SLinus Torvalds 
782b5c84bf6SNick Piggin /*
7839875cf80SDavid Howells  * Perform an automount
7849875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
7859875cf80SDavid Howells  *   were called with.
7861da177e4SLinus Torvalds  */
7879875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
7889875cf80SDavid Howells 			    bool *need_mntput)
78931e6b01fSNick Piggin {
7909875cf80SDavid Howells 	struct vfsmount *mnt;
791ea5b778aSDavid Howells 	int err;
7929875cf80SDavid Howells 
7939875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
7949875cf80SDavid Howells 		return -EREMOTE;
7959875cf80SDavid Howells 
7966f45b656SDavid Howells 	/* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
7976f45b656SDavid Howells 	 * and this is the terminal part of the path.
7986f45b656SDavid Howells 	 */
7996f45b656SDavid Howells 	if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
8006f45b656SDavid Howells 		return -EISDIR; /* we actually want to stop here */
8016f45b656SDavid Howells 
8029875cf80SDavid Howells 	/* We want to mount if someone is trying to open/create a file of any
8039875cf80SDavid Howells 	 * type under the mountpoint, wants to traverse through the mountpoint
8049875cf80SDavid Howells 	 * or wants to open the mounted directory.
8059875cf80SDavid Howells 	 *
8069875cf80SDavid Howells 	 * We don't want to mount if someone's just doing a stat and they've
8079875cf80SDavid Howells 	 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
8089875cf80SDavid Howells 	 * appended a '/' to the name.
8099875cf80SDavid Howells 	 */
8109875cf80SDavid Howells 	if (!(flags & LOOKUP_FOLLOW) &&
8119875cf80SDavid Howells 	    !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
8129875cf80SDavid Howells 		       LOOKUP_OPEN | LOOKUP_CREATE)))
8139875cf80SDavid Howells 		return -EISDIR;
8149875cf80SDavid Howells 
8159875cf80SDavid Howells 	current->total_link_count++;
8169875cf80SDavid Howells 	if (current->total_link_count >= 40)
8179875cf80SDavid Howells 		return -ELOOP;
8189875cf80SDavid Howells 
8199875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
8209875cf80SDavid Howells 	if (IS_ERR(mnt)) {
8219875cf80SDavid Howells 		/*
8229875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
8239875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
8249875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
8259875cf80SDavid Howells 		 *
8269875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
8279875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
8289875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
8299875cf80SDavid Howells 		 */
8309875cf80SDavid Howells 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
8319875cf80SDavid Howells 			return -EREMOTE;
8329875cf80SDavid Howells 		return PTR_ERR(mnt);
83331e6b01fSNick Piggin 	}
834ea5b778aSDavid Howells 
8359875cf80SDavid Howells 	if (!mnt) /* mount collision */
8369875cf80SDavid Howells 		return 0;
8379875cf80SDavid Howells 
83819a167afSAl Viro 	err = finish_automount(mnt, path);
839ea5b778aSDavid Howells 
840ea5b778aSDavid Howells 	switch (err) {
841ea5b778aSDavid Howells 	case -EBUSY:
842ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
84319a167afSAl Viro 		return 0;
844ea5b778aSDavid Howells 	case 0:
845463ffb2eSAl Viro 		dput(path->dentry);
8469875cf80SDavid Howells 		if (*need_mntput)
8479875cf80SDavid Howells 			mntput(path->mnt);
8489875cf80SDavid Howells 		path->mnt = mnt;
8499875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
8509875cf80SDavid Howells 		*need_mntput = true;
8519875cf80SDavid Howells 		return 0;
85219a167afSAl Viro 	default:
85319a167afSAl Viro 		return err;
8549875cf80SDavid Howells 	}
85519a167afSAl Viro 
856ea5b778aSDavid Howells }
8579875cf80SDavid Howells 
8589875cf80SDavid Howells /*
8599875cf80SDavid Howells  * Handle a dentry that is managed in some way.
860cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
8619875cf80SDavid Howells  * - Flagged as mountpoint
8629875cf80SDavid Howells  * - Flagged as automount point
8639875cf80SDavid Howells  *
8649875cf80SDavid Howells  * This may only be called in refwalk mode.
8659875cf80SDavid Howells  *
8669875cf80SDavid Howells  * Serialization is taken care of in namespace.c
8679875cf80SDavid Howells  */
8689875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
8699875cf80SDavid Howells {
8709875cf80SDavid Howells 	unsigned managed;
8719875cf80SDavid Howells 	bool need_mntput = false;
8729875cf80SDavid Howells 	int ret;
8739875cf80SDavid Howells 
8749875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
8759875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
8769875cf80SDavid Howells 	 * the components of that value change under us */
8779875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
8789875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
8799875cf80SDavid Howells 	       unlikely(managed != 0)) {
880cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
881cc53ce53SDavid Howells 		 * being held. */
882cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
883cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
884cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
8851aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
886cc53ce53SDavid Howells 			if (ret < 0)
887cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
888cc53ce53SDavid Howells 		}
889cc53ce53SDavid Howells 
8909875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
8919875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
8929875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
8939875cf80SDavid Howells 			if (mounted) {
8949875cf80SDavid Howells 				dput(path->dentry);
8959875cf80SDavid Howells 				if (need_mntput)
896463ffb2eSAl Viro 					mntput(path->mnt);
897463ffb2eSAl Viro 				path->mnt = mounted;
898463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
8999875cf80SDavid Howells 				need_mntput = true;
9009875cf80SDavid Howells 				continue;
901463ffb2eSAl Viro 			}
902463ffb2eSAl Viro 
9039875cf80SDavid Howells 			/* Something is mounted on this dentry in another
9049875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
9059875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
9069875cf80SDavid Howells 			 * vfsmount_lock */
9071da177e4SLinus Torvalds 		}
9089875cf80SDavid Howells 
9099875cf80SDavid Howells 		/* Handle an automount point */
9109875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
9119875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
9129875cf80SDavid Howells 			if (ret < 0)
9139875cf80SDavid Howells 				return ret == -EISDIR ? 0 : ret;
9149875cf80SDavid Howells 			continue;
9159875cf80SDavid Howells 		}
9169875cf80SDavid Howells 
9179875cf80SDavid Howells 		/* We didn't change the current path point */
9189875cf80SDavid Howells 		break;
9199875cf80SDavid Howells 	}
9209875cf80SDavid Howells 	return 0;
9211da177e4SLinus Torvalds }
9221da177e4SLinus Torvalds 
923cc53ce53SDavid Howells int follow_down_one(struct path *path)
9241da177e4SLinus Torvalds {
9251da177e4SLinus Torvalds 	struct vfsmount *mounted;
9261da177e4SLinus Torvalds 
9271c755af4SAl Viro 	mounted = lookup_mnt(path);
9281da177e4SLinus Torvalds 	if (mounted) {
9299393bd07SAl Viro 		dput(path->dentry);
9309393bd07SAl Viro 		mntput(path->mnt);
9319393bd07SAl Viro 		path->mnt = mounted;
9329393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
9331da177e4SLinus Torvalds 		return 1;
9341da177e4SLinus Torvalds 	}
9351da177e4SLinus Torvalds 	return 0;
9361da177e4SLinus Torvalds }
9371da177e4SLinus Torvalds 
93862a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
93962a7375eSIan Kent {
94062a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
94162a7375eSIan Kent 		dentry->d_op->d_manage(dentry, true) < 0);
94262a7375eSIan Kent }
94362a7375eSIan Kent 
9449875cf80SDavid Howells /*
9459875cf80SDavid Howells  * Skip to top of mountpoint pile in rcuwalk mode.  We abort the rcu-walk if we
946cc53ce53SDavid Howells  * meet a managed dentry and we're not walking to "..".  True is returned to
9479875cf80SDavid Howells  * continue, false to abort.
9489875cf80SDavid Howells  */
9499875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
9509875cf80SDavid Howells 			       struct inode **inode, bool reverse_transit)
9519875cf80SDavid Howells {
95262a7375eSIan Kent 	for (;;) {
9539875cf80SDavid Howells 		struct vfsmount *mounted;
95462a7375eSIan Kent 		/*
95562a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
95662a7375eSIan Kent 		 * that wants to block transit.
95762a7375eSIan Kent 		 */
95862a7375eSIan Kent 		*inode = path->dentry->d_inode;
95962a7375eSIan Kent 		if (!reverse_transit &&
96062a7375eSIan Kent 		     unlikely(managed_dentry_might_block(path->dentry)))
961ab90911fSDavid Howells 			return false;
96262a7375eSIan Kent 
96362a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
96462a7375eSIan Kent 			break;
96562a7375eSIan Kent 
9669875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
9679875cf80SDavid Howells 		if (!mounted)
9689875cf80SDavid Howells 			break;
9699875cf80SDavid Howells 		path->mnt = mounted;
9709875cf80SDavid Howells 		path->dentry = mounted->mnt_root;
9719875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
9729875cf80SDavid Howells 	}
9739875cf80SDavid Howells 
9749875cf80SDavid Howells 	if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
9759875cf80SDavid Howells 		return reverse_transit;
9769875cf80SDavid Howells 	return true;
9779875cf80SDavid Howells }
9789875cf80SDavid Howells 
97931e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
98031e6b01fSNick Piggin {
98131e6b01fSNick Piggin 	struct inode *inode = nd->inode;
98231e6b01fSNick Piggin 
98331e6b01fSNick Piggin 	set_root_rcu(nd);
98431e6b01fSNick Piggin 
98531e6b01fSNick Piggin 	while (1) {
98631e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
98731e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
98831e6b01fSNick Piggin 			break;
98931e6b01fSNick Piggin 		}
99031e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
99131e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
99231e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
99331e6b01fSNick Piggin 			unsigned seq;
99431e6b01fSNick Piggin 
99531e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
99631e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
997ef7562d5SAl Viro 				goto failed;
99831e6b01fSNick Piggin 			inode = parent->d_inode;
99931e6b01fSNick Piggin 			nd->path.dentry = parent;
100031e6b01fSNick Piggin 			nd->seq = seq;
100131e6b01fSNick Piggin 			break;
100231e6b01fSNick Piggin 		}
100331e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
100431e6b01fSNick Piggin 			break;
100531e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
100631e6b01fSNick Piggin 		inode = nd->path.dentry->d_inode;
100731e6b01fSNick Piggin 	}
10089875cf80SDavid Howells 	__follow_mount_rcu(nd, &nd->path, &inode, true);
100931e6b01fSNick Piggin 	nd->inode = inode;
101031e6b01fSNick Piggin 	return 0;
1011ef7562d5SAl Viro 
1012ef7562d5SAl Viro failed:
1013ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
10145b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
1015ef7562d5SAl Viro 		nd->root.mnt = NULL;
1016ef7562d5SAl Viro 	rcu_read_unlock();
1017ef7562d5SAl Viro 	br_read_unlock(vfsmount_lock);
1018ef7562d5SAl Viro 	return -ECHILD;
101931e6b01fSNick Piggin }
102031e6b01fSNick Piggin 
10219875cf80SDavid Howells /*
1022cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
1023cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
1024cc53ce53SDavid Howells  * caller is permitted to proceed or not.
1025cc53ce53SDavid Howells  *
1026cc53ce53SDavid Howells  * Care must be taken as namespace_sem may be held (indicated by mounting_here
1027cc53ce53SDavid Howells  * being true).
1028cc53ce53SDavid Howells  */
10297cc90cc3SAl Viro int follow_down(struct path *path)
1030cc53ce53SDavid Howells {
1031cc53ce53SDavid Howells 	unsigned managed;
1032cc53ce53SDavid Howells 	int ret;
1033cc53ce53SDavid Howells 
1034cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
1035cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1036cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1037cc53ce53SDavid Howells 		 * being held.
1038cc53ce53SDavid Howells 		 *
1039cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
1040cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
1041cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
1042cc53ce53SDavid Howells 		 * superstructure.
1043cc53ce53SDavid Howells 		 *
1044cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
1045cc53ce53SDavid Howells 		 */
1046cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
1047cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1048cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
1049ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
10501aed3e42SAl Viro 				path->dentry, false);
1051cc53ce53SDavid Howells 			if (ret < 0)
1052cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
1053cc53ce53SDavid Howells 		}
1054cc53ce53SDavid Howells 
1055cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
1056cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
1057cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
1058cc53ce53SDavid Howells 			if (!mounted)
1059cc53ce53SDavid Howells 				break;
1060cc53ce53SDavid Howells 			dput(path->dentry);
1061cc53ce53SDavid Howells 			mntput(path->mnt);
1062cc53ce53SDavid Howells 			path->mnt = mounted;
1063cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1064cc53ce53SDavid Howells 			continue;
1065cc53ce53SDavid Howells 		}
1066cc53ce53SDavid Howells 
1067cc53ce53SDavid Howells 		/* Don't handle automount points here */
1068cc53ce53SDavid Howells 		break;
1069cc53ce53SDavid Howells 	}
1070cc53ce53SDavid Howells 	return 0;
1071cc53ce53SDavid Howells }
1072cc53ce53SDavid Howells 
1073cc53ce53SDavid Howells /*
10749875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
10759875cf80SDavid Howells  */
10769875cf80SDavid Howells static void follow_mount(struct path *path)
10779875cf80SDavid Howells {
10789875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
10799875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
10809875cf80SDavid Howells 		if (!mounted)
10819875cf80SDavid Howells 			break;
10829875cf80SDavid Howells 		dput(path->dentry);
10839875cf80SDavid Howells 		mntput(path->mnt);
10849875cf80SDavid Howells 		path->mnt = mounted;
10859875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
10869875cf80SDavid Howells 	}
10879875cf80SDavid Howells }
10889875cf80SDavid Howells 
108931e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
10901da177e4SLinus Torvalds {
10912a737871SAl Viro 	set_root(nd);
1092e518ddb7SAndreas Mohr 
10931da177e4SLinus Torvalds 	while(1) {
10944ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
10951da177e4SLinus Torvalds 
10962a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
10972a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
10981da177e4SLinus Torvalds 			break;
10991da177e4SLinus Torvalds 		}
11004ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
11013088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
11023088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
11031da177e4SLinus Torvalds 			dput(old);
11041da177e4SLinus Torvalds 			break;
11051da177e4SLinus Torvalds 		}
11063088dd70SAl Viro 		if (!follow_up(&nd->path))
11071da177e4SLinus Torvalds 			break;
11081da177e4SLinus Torvalds 	}
110979ed0226SAl Viro 	follow_mount(&nd->path);
111031e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
11111da177e4SLinus Torvalds }
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds /*
1114baa03890SNick Piggin  * Allocate a dentry with name and parent, and perform a parent
1115baa03890SNick Piggin  * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1116baa03890SNick Piggin  * on error. parent->d_inode->i_mutex must be held. d_lookup must
1117baa03890SNick Piggin  * have verified that no child exists while under i_mutex.
1118baa03890SNick Piggin  */
1119baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1120baa03890SNick Piggin 				struct qstr *name, struct nameidata *nd)
1121baa03890SNick Piggin {
1122baa03890SNick Piggin 	struct inode *inode = parent->d_inode;
1123baa03890SNick Piggin 	struct dentry *dentry;
1124baa03890SNick Piggin 	struct dentry *old;
1125baa03890SNick Piggin 
1126baa03890SNick Piggin 	/* Don't create child dentry for a dead directory. */
1127baa03890SNick Piggin 	if (unlikely(IS_DEADDIR(inode)))
1128baa03890SNick Piggin 		return ERR_PTR(-ENOENT);
1129baa03890SNick Piggin 
1130baa03890SNick Piggin 	dentry = d_alloc(parent, name);
1131baa03890SNick Piggin 	if (unlikely(!dentry))
1132baa03890SNick Piggin 		return ERR_PTR(-ENOMEM);
1133baa03890SNick Piggin 
1134baa03890SNick Piggin 	old = inode->i_op->lookup(inode, dentry, nd);
1135baa03890SNick Piggin 	if (unlikely(old)) {
1136baa03890SNick Piggin 		dput(dentry);
1137baa03890SNick Piggin 		dentry = old;
1138baa03890SNick Piggin 	}
1139baa03890SNick Piggin 	return dentry;
1140baa03890SNick Piggin }
1141baa03890SNick Piggin 
1142baa03890SNick Piggin /*
11431da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
11441da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
11451da177e4SLinus Torvalds  *  It _is_ time-critical.
11461da177e4SLinus Torvalds  */
11471da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
114831e6b01fSNick Piggin 			struct path *path, struct inode **inode)
11491da177e4SLinus Torvalds {
11504ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
115131e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
11525a18fff2SAl Viro 	int need_reval = 1;
11535a18fff2SAl Viro 	int status = 1;
11549875cf80SDavid Howells 	int err;
11559875cf80SDavid Howells 
11563cac260aSAl Viro 	/*
1157b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1158b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1159b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1160b04f784eSNick Piggin 	 */
116131e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
116231e6b01fSNick Piggin 		unsigned seq;
116331e6b01fSNick Piggin 		*inode = nd->inode;
116431e6b01fSNick Piggin 		dentry = __d_lookup_rcu(parent, name, &seq, inode);
11655a18fff2SAl Viro 		if (!dentry)
11665a18fff2SAl Viro 			goto unlazy;
11675a18fff2SAl Viro 
116831e6b01fSNick Piggin 		/* Memory barrier in read_seqcount_begin of child is enough */
116931e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
117031e6b01fSNick Piggin 			return -ECHILD;
117131e6b01fSNick Piggin 		nd->seq = seq;
11725a18fff2SAl Viro 
117324643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
11745a18fff2SAl Viro 			status = d_revalidate(dentry, nd);
11755a18fff2SAl Viro 			if (unlikely(status <= 0)) {
11765a18fff2SAl Viro 				if (status != -ECHILD)
11775a18fff2SAl Viro 					need_reval = 0;
11785a18fff2SAl Viro 				goto unlazy;
11795a18fff2SAl Viro 			}
118024643087SAl Viro 		}
118131e6b01fSNick Piggin 		path->mnt = mnt;
118231e6b01fSNick Piggin 		path->dentry = dentry;
11839875cf80SDavid Howells 		if (likely(__follow_mount_rcu(nd, path, inode, false)))
11849875cf80SDavid Howells 			return 0;
11855a18fff2SAl Viro unlazy:
118619660af7SAl Viro 		if (unlazy_walk(nd, dentry))
11875a18fff2SAl Viro 			return -ECHILD;
11885a18fff2SAl Viro 	} else {
118931e6b01fSNick Piggin 		dentry = __d_lookup(parent, name);
119024643087SAl Viro 	}
11915a18fff2SAl Viro 
11925a18fff2SAl Viro retry:
11935a18fff2SAl Viro 	if (unlikely(!dentry)) {
11945a18fff2SAl Viro 		struct inode *dir = parent->d_inode;
11955a18fff2SAl Viro 		BUG_ON(nd->inode != dir);
11965a18fff2SAl Viro 
11975a18fff2SAl Viro 		mutex_lock(&dir->i_mutex);
11985a18fff2SAl Viro 		dentry = d_lookup(parent, name);
11995a18fff2SAl Viro 		if (likely(!dentry)) {
12005a18fff2SAl Viro 			dentry = d_alloc_and_lookup(parent, name, nd);
12015a18fff2SAl Viro 			if (IS_ERR(dentry)) {
12025a18fff2SAl Viro 				mutex_unlock(&dir->i_mutex);
12035a18fff2SAl Viro 				return PTR_ERR(dentry);
12045a18fff2SAl Viro 			}
12055a18fff2SAl Viro 			/* known good */
12065a18fff2SAl Viro 			need_reval = 0;
12075a18fff2SAl Viro 			status = 1;
12085a18fff2SAl Viro 		}
12095a18fff2SAl Viro 		mutex_unlock(&dir->i_mutex);
12105a18fff2SAl Viro 	}
12115a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
12125a18fff2SAl Viro 		status = d_revalidate(dentry, nd);
12135a18fff2SAl Viro 	if (unlikely(status <= 0)) {
12145a18fff2SAl Viro 		if (status < 0) {
12155a18fff2SAl Viro 			dput(dentry);
12165a18fff2SAl Viro 			return status;
12175a18fff2SAl Viro 		}
12185a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
12195a18fff2SAl Viro 			dput(dentry);
12205a18fff2SAl Viro 			dentry = NULL;
12215a18fff2SAl Viro 			need_reval = 1;
12225a18fff2SAl Viro 			goto retry;
12235a18fff2SAl Viro 		}
12245a18fff2SAl Viro 	}
12255a18fff2SAl Viro 
12261da177e4SLinus Torvalds 	path->mnt = mnt;
12271da177e4SLinus Torvalds 	path->dentry = dentry;
12289875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
122989312214SIan Kent 	if (unlikely(err < 0)) {
123089312214SIan Kent 		path_put_conditional(path, nd);
12319875cf80SDavid Howells 		return err;
123289312214SIan Kent 	}
123331e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
12341da177e4SLinus Torvalds 	return 0;
12351da177e4SLinus Torvalds }
12361da177e4SLinus Torvalds 
123752094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
123852094c8aSAl Viro {
123952094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
124052094c8aSAl Viro 		int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
124152094c8aSAl Viro 		if (err != -ECHILD)
124252094c8aSAl Viro 			return err;
124319660af7SAl Viro 		if (unlazy_walk(nd, NULL))
124452094c8aSAl Viro 			return -ECHILD;
124552094c8aSAl Viro 	}
124652094c8aSAl Viro 	return exec_permission(nd->inode, 0);
124752094c8aSAl Viro }
124852094c8aSAl Viro 
12499856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
12509856fa1bSAl Viro {
12519856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
12529856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
12539856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
12549856fa1bSAl Viro 				return -ECHILD;
12559856fa1bSAl Viro 		} else
12569856fa1bSAl Viro 			follow_dotdot(nd);
12579856fa1bSAl Viro 	}
12589856fa1bSAl Viro 	return 0;
12599856fa1bSAl Viro }
12609856fa1bSAl Viro 
1261951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1262951361f9SAl Viro {
1263951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1264951361f9SAl Viro 		path_put(&nd->path);
1265951361f9SAl Viro 	} else {
1266951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
12675b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1268951361f9SAl Viro 			nd->root.mnt = NULL;
1269951361f9SAl Viro 		rcu_read_unlock();
1270951361f9SAl Viro 		br_read_unlock(vfsmount_lock);
1271951361f9SAl Viro 	}
1272951361f9SAl Viro }
1273951361f9SAl Viro 
1274ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
1275ce57dfc1SAl Viro 		struct qstr *name, int type, int follow)
1276ce57dfc1SAl Viro {
1277ce57dfc1SAl Viro 	struct inode *inode;
1278ce57dfc1SAl Viro 	int err;
1279ce57dfc1SAl Viro 	/*
1280ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1281ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1282ce57dfc1SAl Viro 	 * parent relationships.
1283ce57dfc1SAl Viro 	 */
1284ce57dfc1SAl Viro 	if (unlikely(type != LAST_NORM))
1285ce57dfc1SAl Viro 		return handle_dots(nd, type);
1286ce57dfc1SAl Viro 	err = do_lookup(nd, name, path, &inode);
1287ce57dfc1SAl Viro 	if (unlikely(err)) {
1288ce57dfc1SAl Viro 		terminate_walk(nd);
1289ce57dfc1SAl Viro 		return err;
1290ce57dfc1SAl Viro 	}
1291ce57dfc1SAl Viro 	if (!inode) {
1292ce57dfc1SAl Viro 		path_to_nameidata(path, nd);
1293ce57dfc1SAl Viro 		terminate_walk(nd);
1294ce57dfc1SAl Viro 		return -ENOENT;
1295ce57dfc1SAl Viro 	}
1296ce57dfc1SAl Viro 	if (unlikely(inode->i_op->follow_link) && follow) {
129719660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
129819660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
129919660af7SAl Viro 				terminate_walk(nd);
1300ce57dfc1SAl Viro 				return -ECHILD;
130119660af7SAl Viro 			}
130219660af7SAl Viro 		}
1303ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1304ce57dfc1SAl Viro 		return 1;
1305ce57dfc1SAl Viro 	}
1306ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1307ce57dfc1SAl Viro 	nd->inode = inode;
1308ce57dfc1SAl Viro 	return 0;
1309ce57dfc1SAl Viro }
1310ce57dfc1SAl Viro 
13111da177e4SLinus Torvalds /*
1312b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1313b356379aSAl Viro  * limiting consecutive symlinks to 40.
1314b356379aSAl Viro  *
1315b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1316b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1317b356379aSAl Viro  */
1318b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1319b356379aSAl Viro {
1320b356379aSAl Viro 	int res;
1321b356379aSAl Viro 
1322b356379aSAl Viro 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1323b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1324b356379aSAl Viro 		path_put_conditional(path, nd);
1325b356379aSAl Viro 		path_put(&nd->path);
1326b356379aSAl Viro 		return -ELOOP;
1327b356379aSAl Viro 	}
1328b356379aSAl Viro 
1329b356379aSAl Viro 	nd->depth++;
1330b356379aSAl Viro 	current->link_count++;
1331b356379aSAl Viro 
1332b356379aSAl Viro 	do {
1333b356379aSAl Viro 		struct path link = *path;
1334b356379aSAl Viro 		void *cookie;
1335574197e0SAl Viro 
1336574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
1337b356379aSAl Viro 		if (!res)
1338b356379aSAl Viro 			res = walk_component(nd, path, &nd->last,
1339b356379aSAl Viro 					     nd->last_type, LOOKUP_FOLLOW);
1340574197e0SAl Viro 		put_link(nd, &link, cookie);
1341b356379aSAl Viro 	} while (res > 0);
1342b356379aSAl Viro 
1343b356379aSAl Viro 	current->link_count--;
1344b356379aSAl Viro 	nd->depth--;
1345b356379aSAl Viro 	return res;
1346b356379aSAl Viro }
1347b356379aSAl Viro 
1348b356379aSAl Viro /*
13491da177e4SLinus Torvalds  * Name resolution.
1350ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1351ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
13521da177e4SLinus Torvalds  *
1353ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1354ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
13551da177e4SLinus Torvalds  */
13566de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
13571da177e4SLinus Torvalds {
13581da177e4SLinus Torvalds 	struct path next;
13591da177e4SLinus Torvalds 	int err;
13601da177e4SLinus Torvalds 	unsigned int lookup_flags = nd->flags;
13611da177e4SLinus Torvalds 
13621da177e4SLinus Torvalds 	while (*name=='/')
13631da177e4SLinus Torvalds 		name++;
13641da177e4SLinus Torvalds 	if (!*name)
1365086e183aSAl Viro 		return 0;
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
13681da177e4SLinus Torvalds 	for(;;) {
13691da177e4SLinus Torvalds 		unsigned long hash;
13701da177e4SLinus Torvalds 		struct qstr this;
13711da177e4SLinus Torvalds 		unsigned int c;
1372fe479a58SAl Viro 		int type;
13731da177e4SLinus Torvalds 
1374cdce5d6bSTrond Myklebust 		nd->flags |= LOOKUP_CONTINUE;
137552094c8aSAl Viro 
137652094c8aSAl Viro 		err = may_lookup(nd);
13771da177e4SLinus Torvalds  		if (err)
13781da177e4SLinus Torvalds 			break;
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 		this.name = name;
13811da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds 		hash = init_name_hash();
13841da177e4SLinus Torvalds 		do {
13851da177e4SLinus Torvalds 			name++;
13861da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
13871da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
13881da177e4SLinus Torvalds 		} while (c && (c != '/'));
13891da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
13901da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
13911da177e4SLinus Torvalds 
1392fe479a58SAl Viro 		type = LAST_NORM;
1393fe479a58SAl Viro 		if (this.name[0] == '.') switch (this.len) {
1394fe479a58SAl Viro 			case 2:
139516c2cd71SAl Viro 				if (this.name[1] == '.') {
1396fe479a58SAl Viro 					type = LAST_DOTDOT;
139716c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
139816c2cd71SAl Viro 				}
1399fe479a58SAl Viro 				break;
1400fe479a58SAl Viro 			case 1:
1401fe479a58SAl Viro 				type = LAST_DOT;
1402fe479a58SAl Viro 		}
14035a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
14045a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
140516c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
14065a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
14075a202bcdSAl Viro 				err = parent->d_op->d_hash(parent, nd->inode,
14085a202bcdSAl Viro 							   &this);
14095a202bcdSAl Viro 				if (err < 0)
14105a202bcdSAl Viro 					break;
14115a202bcdSAl Viro 			}
14125a202bcdSAl Viro 		}
1413fe479a58SAl Viro 
14141da177e4SLinus Torvalds 		/* remove trailing slashes? */
14151da177e4SLinus Torvalds 		if (!c)
14161da177e4SLinus Torvalds 			goto last_component;
14171da177e4SLinus Torvalds 		while (*++name == '/');
14181da177e4SLinus Torvalds 		if (!*name)
1419b356379aSAl Viro 			goto last_component;
14201da177e4SLinus Torvalds 
1421ce57dfc1SAl Viro 		err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1422ce57dfc1SAl Viro 		if (err < 0)
1423ce57dfc1SAl Viro 			return err;
1424fe479a58SAl Viro 
1425ce57dfc1SAl Viro 		if (err) {
1426b356379aSAl Viro 			err = nested_symlink(&next, nd);
14271da177e4SLinus Torvalds 			if (err)
1428a7472babSAl Viro 				return err;
142931e6b01fSNick Piggin 		}
14301da177e4SLinus Torvalds 		err = -ENOTDIR;
143131e6b01fSNick Piggin 		if (!nd->inode->i_op->lookup)
14321da177e4SLinus Torvalds 			break;
14331da177e4SLinus Torvalds 		continue;
14341da177e4SLinus Torvalds 		/* here ends the main loop */
14351da177e4SLinus Torvalds 
14361da177e4SLinus Torvalds last_component:
1437f55eab82STrond Myklebust 		/* Clear LOOKUP_CONTINUE iff it was previously unset */
1438f55eab82STrond Myklebust 		nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
1439ce57dfc1SAl Viro 		nd->last = this;
1440ce57dfc1SAl Viro 		nd->last_type = type;
1441ce57dfc1SAl Viro 		return 0;
1442ce57dfc1SAl Viro 	}
1443951361f9SAl Viro 	terminate_walk(nd);
14441da177e4SLinus Torvalds 	return err;
14451da177e4SLinus Torvalds }
14461da177e4SLinus Torvalds 
144770e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
144870e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
144931e6b01fSNick Piggin {
145031e6b01fSNick Piggin 	int retval = 0;
145131e6b01fSNick Piggin 	int fput_needed;
145231e6b01fSNick Piggin 	struct file *file;
145331e6b01fSNick Piggin 
145431e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
145516c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
145631e6b01fSNick Piggin 	nd->depth = 0;
14575b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
14585b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
145973d049a4SAl Viro 		if (*name) {
14605b6ca027SAl Viro 			if (!inode->i_op->lookup)
14615b6ca027SAl Viro 				return -ENOTDIR;
14625b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
14635b6ca027SAl Viro 			if (retval)
14645b6ca027SAl Viro 				return retval;
146573d049a4SAl Viro 		}
14665b6ca027SAl Viro 		nd->path = nd->root;
14675b6ca027SAl Viro 		nd->inode = inode;
14685b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
14695b6ca027SAl Viro 			br_read_lock(vfsmount_lock);
14705b6ca027SAl Viro 			rcu_read_lock();
14715b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
14725b6ca027SAl Viro 		} else {
14735b6ca027SAl Viro 			path_get(&nd->path);
14745b6ca027SAl Viro 		}
14755b6ca027SAl Viro 		return 0;
14765b6ca027SAl Viro 	}
14775b6ca027SAl Viro 
147831e6b01fSNick Piggin 	nd->root.mnt = NULL;
147931e6b01fSNick Piggin 
148031e6b01fSNick Piggin 	if (*name=='/') {
1481e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
148231e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
148331e6b01fSNick Piggin 			rcu_read_lock();
1484e41f7d4eSAl Viro 			set_root_rcu(nd);
1485e41f7d4eSAl Viro 		} else {
1486e41f7d4eSAl Viro 			set_root(nd);
1487e41f7d4eSAl Viro 			path_get(&nd->root);
1488e41f7d4eSAl Viro 		}
148931e6b01fSNick Piggin 		nd->path = nd->root;
149031e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1491e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
149231e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1493c28cc364SNick Piggin 			unsigned seq;
149431e6b01fSNick Piggin 
149531e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
149631e6b01fSNick Piggin 			rcu_read_lock();
149731e6b01fSNick Piggin 
1498c28cc364SNick Piggin 			do {
1499c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
150031e6b01fSNick Piggin 				nd->path = fs->pwd;
1501c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1502c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1503e41f7d4eSAl Viro 		} else {
1504e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1505e41f7d4eSAl Viro 		}
150631e6b01fSNick Piggin 	} else {
150731e6b01fSNick Piggin 		struct dentry *dentry;
150831e6b01fSNick Piggin 
15091abf0c71SAl Viro 		file = fget_raw_light(dfd, &fput_needed);
151031e6b01fSNick Piggin 		retval = -EBADF;
151131e6b01fSNick Piggin 		if (!file)
151231e6b01fSNick Piggin 			goto out_fail;
151331e6b01fSNick Piggin 
151431e6b01fSNick Piggin 		dentry = file->f_path.dentry;
151531e6b01fSNick Piggin 
1516f52e0c11SAl Viro 		if (*name) {
151731e6b01fSNick Piggin 			retval = -ENOTDIR;
151831e6b01fSNick Piggin 			if (!S_ISDIR(dentry->d_inode->i_mode))
151931e6b01fSNick Piggin 				goto fput_fail;
152031e6b01fSNick Piggin 
152131e6b01fSNick Piggin 			retval = file_permission(file, MAY_EXEC);
152231e6b01fSNick Piggin 			if (retval)
152331e6b01fSNick Piggin 				goto fput_fail;
1524f52e0c11SAl Viro 		}
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 
1548bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1549bd92d7feSAl Viro {
1550bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1551bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1552bd92d7feSAl Viro 
1553bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
1554bd92d7feSAl Viro 	return walk_component(nd, path, &nd->last, nd->last_type,
1555bd92d7feSAl Viro 					nd->flags & LOOKUP_FOLLOW);
1556bd92d7feSAl Viro }
1557bd92d7feSAl Viro 
15589b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1559ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
15609b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
15619b4a9b14SAl Viro {
156270e9b357SAl Viro 	struct file *base = NULL;
1563bd92d7feSAl Viro 	struct path path;
1564bd92d7feSAl Viro 	int err;
156531e6b01fSNick Piggin 
156631e6b01fSNick Piggin 	/*
156731e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
156831e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
156931e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
157031e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
157131e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
157231e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
157331e6b01fSNick Piggin 	 * analogue, foo_rcu().
157431e6b01fSNick Piggin 	 *
157531e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
157631e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
157731e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
157831e6b01fSNick Piggin 	 * be able to complete).
157931e6b01fSNick Piggin 	 */
1580bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1581ee0827cdSAl Viro 
1582bd92d7feSAl Viro 	if (unlikely(err))
1583bd92d7feSAl Viro 		return err;
1584ee0827cdSAl Viro 
1585ee0827cdSAl Viro 	current->total_link_count = 0;
1586bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1587bd92d7feSAl Viro 
1588bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1589bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1590bd92d7feSAl Viro 		while (err > 0) {
1591bd92d7feSAl Viro 			void *cookie;
1592bd92d7feSAl Viro 			struct path link = path;
1593bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1594574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
1595bd92d7feSAl Viro 			if (!err)
1596bd92d7feSAl Viro 				err = lookup_last(nd, &path);
1597574197e0SAl Viro 			put_link(nd, &link, cookie);
1598bd92d7feSAl Viro 		}
1599bd92d7feSAl Viro 	}
1600ee0827cdSAl Viro 
1601ee0827cdSAl Viro 	if (nd->flags & LOOKUP_RCU) {
16024455ca62SAl Viro 		/* went all way through without dropping RCU */
1603bd92d7feSAl Viro 		BUG_ON(err);
1604086e183aSAl Viro 		if (nameidata_drop_rcu_last(nd))
1605bd92d7feSAl Viro 			err = -ECHILD;
1606086e183aSAl Viro 	}
160731e6b01fSNick Piggin 
1608bd23a539SAl Viro 	if (!err) {
1609bd92d7feSAl Viro 		err = handle_reval_path(nd);
1610bd23a539SAl Viro 		if (err)
1611bd23a539SAl Viro 			path_put(&nd->path);
1612bd23a539SAl Viro 	}
1613bd92d7feSAl Viro 
1614bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
1615bd92d7feSAl Viro 		if (!nd->inode->i_op->lookup) {
1616bd92d7feSAl Viro 			path_put(&nd->path);
1617bd23a539SAl Viro 			err = -ENOTDIR;
1618bd92d7feSAl Viro 		}
1619bd92d7feSAl Viro 	}
162016c2cd71SAl Viro 
162170e9b357SAl Viro 	if (base)
162270e9b357SAl Viro 		fput(base);
1623ee0827cdSAl Viro 
16245b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
162531e6b01fSNick Piggin 		path_put(&nd->root);
162631e6b01fSNick Piggin 		nd->root.mnt = NULL;
162731e6b01fSNick Piggin 	}
1628bd92d7feSAl Viro 	return err;
162931e6b01fSNick Piggin }
163031e6b01fSNick Piggin 
1631ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1632ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1633ee0827cdSAl Viro {
1634ee0827cdSAl Viro 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1635ee0827cdSAl Viro 	if (unlikely(retval == -ECHILD))
1636ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags, nd);
1637ee0827cdSAl Viro 	if (unlikely(retval == -ESTALE))
1638ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1639ee0827cdSAl Viro 
164031e6b01fSNick Piggin 	if (likely(!retval)) {
164131e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
164231e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
164331e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
164431e6b01fSNick Piggin 		}
164531e6b01fSNick Piggin 	}
1646170aa3d0SUlrich Drepper 	return retval;
16471da177e4SLinus Torvalds }
16481da177e4SLinus Torvalds 
1649c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd)
16505590ff0dSUlrich Drepper {
1651c9c6cac0SAl Viro 	return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
16525590ff0dSUlrich Drepper }
16535590ff0dSUlrich Drepper 
1654d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1655d1811465SAl Viro {
1656d1811465SAl Viro 	struct nameidata nd;
1657d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1658d1811465SAl Viro 	if (!res)
1659d1811465SAl Viro 		*path = nd.path;
1660d1811465SAl Viro 	return res;
1661d1811465SAl Viro }
1662d1811465SAl Viro 
166316f18200SJosef 'Jeff' Sipek /**
166416f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
166516f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
166616f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
166716f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
166816f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
166916f18200SJosef 'Jeff' Sipek  * @nd: pointer to nameidata
167016f18200SJosef 'Jeff' Sipek  */
167116f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
167216f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
167316f18200SJosef 'Jeff' Sipek 		    struct nameidata *nd)
167416f18200SJosef 'Jeff' Sipek {
16755b6ca027SAl Viro 	nd->root.dentry = dentry;
16765b6ca027SAl Viro 	nd->root.mnt = mnt;
16775b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
16785b6ca027SAl Viro 	return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
167916f18200SJosef 'Jeff' Sipek }
168016f18200SJosef 'Jeff' Sipek 
1681eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1682eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
16831da177e4SLinus Torvalds {
168481fca444SChristoph Hellwig 	struct inode *inode = base->d_inode;
16851da177e4SLinus Torvalds 	struct dentry *dentry;
16861da177e4SLinus Torvalds 	int err;
16871da177e4SLinus Torvalds 
1688b74c79e9SNick Piggin 	err = exec_permission(inode, 0);
168981fca444SChristoph Hellwig 	if (err)
169081fca444SChristoph Hellwig 		return ERR_PTR(err);
16911da177e4SLinus Torvalds 
16921da177e4SLinus Torvalds 	/*
1693b04f784eSNick Piggin 	 * Don't bother with __d_lookup: callers are for creat as
1694b04f784eSNick Piggin 	 * well as unlink, so a lot of the time it would cost
1695b04f784eSNick Piggin 	 * a double lookup.
16966e6b1bd1SAl Viro 	 */
16976e6b1bd1SAl Viro 	dentry = d_lookup(base, name);
16986e6b1bd1SAl Viro 
1699fb045adbSNick Piggin 	if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
17006e6b1bd1SAl Viro 		dentry = do_revalidate(dentry, nd);
17016e6b1bd1SAl Viro 
17021da177e4SLinus Torvalds 	if (!dentry)
1703baa03890SNick Piggin 		dentry = d_alloc_and_lookup(base, name, nd);
17045a202bcdSAl Viro 
17051da177e4SLinus Torvalds 	return dentry;
17061da177e4SLinus Torvalds }
17071da177e4SLinus Torvalds 
1708057f6c01SJames Morris /*
1709057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1710057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1711057f6c01SJames Morris  * SMP-safe.
1712057f6c01SJames Morris  */
1713a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
17141da177e4SLinus Torvalds {
17154ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
17161da177e4SLinus Torvalds }
17171da177e4SLinus Torvalds 
1718eead1911SChristoph Hellwig /**
1719a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1720eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1721eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1722eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1723eead1911SChristoph Hellwig  *
1724a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1725a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1726eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1727eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1728eead1911SChristoph Hellwig  */
1729057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1730057f6c01SJames Morris {
1731057f6c01SJames Morris 	struct qstr this;
17326a96ba54SAl Viro 	unsigned long hash;
17336a96ba54SAl Viro 	unsigned int c;
1734057f6c01SJames Morris 
17352f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
17362f9092e1SDavid Woodhouse 
17376a96ba54SAl Viro 	this.name = name;
17386a96ba54SAl Viro 	this.len = len;
17396a96ba54SAl Viro 	if (!len)
17406a96ba54SAl Viro 		return ERR_PTR(-EACCES);
17416a96ba54SAl Viro 
17426a96ba54SAl Viro 	hash = init_name_hash();
17436a96ba54SAl Viro 	while (len--) {
17446a96ba54SAl Viro 		c = *(const unsigned char *)name++;
17456a96ba54SAl Viro 		if (c == '/' || c == '\0')
17466a96ba54SAl Viro 			return ERR_PTR(-EACCES);
17476a96ba54SAl Viro 		hash = partial_name_hash(c, hash);
17486a96ba54SAl Viro 	}
17496a96ba54SAl Viro 	this.hash = end_name_hash(hash);
17505a202bcdSAl Viro 	/*
17515a202bcdSAl Viro 	 * See if the low-level filesystem might want
17525a202bcdSAl Viro 	 * to use its own hash..
17535a202bcdSAl Viro 	 */
17545a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
17555a202bcdSAl Viro 		int err = base->d_op->d_hash(base, base->d_inode, &this);
17565a202bcdSAl Viro 		if (err < 0)
17575a202bcdSAl Viro 			return ERR_PTR(err);
17585a202bcdSAl Viro 	}
1759eead1911SChristoph Hellwig 
176049705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1761057f6c01SJames Morris }
1762057f6c01SJames Morris 
17632d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags,
17642d8f3038SAl Viro 		 struct path *path)
17651da177e4SLinus Torvalds {
17662d8f3038SAl Viro 	struct nameidata nd;
1767f52e0c11SAl Viro 	char *tmp = getname_flags(name, flags);
17681da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
17691da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
17702d8f3038SAl Viro 
17712d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
17722d8f3038SAl Viro 
17732d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
17741da177e4SLinus Torvalds 		putname(tmp);
17752d8f3038SAl Viro 		if (!err)
17762d8f3038SAl Viro 			*path = nd.path;
17771da177e4SLinus Torvalds 	}
17781da177e4SLinus Torvalds 	return err;
17791da177e4SLinus Torvalds }
17801da177e4SLinus Torvalds 
17812ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
17822ad94ae6SAl Viro 			struct nameidata *nd, char **name)
17832ad94ae6SAl Viro {
17842ad94ae6SAl Viro 	char *s = getname(path);
17852ad94ae6SAl Viro 	int error;
17862ad94ae6SAl Viro 
17872ad94ae6SAl Viro 	if (IS_ERR(s))
17882ad94ae6SAl Viro 		return PTR_ERR(s);
17892ad94ae6SAl Viro 
17902ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
17912ad94ae6SAl Viro 	if (error)
17922ad94ae6SAl Viro 		putname(s);
17932ad94ae6SAl Viro 	else
17942ad94ae6SAl Viro 		*name = s;
17952ad94ae6SAl Viro 
17962ad94ae6SAl Viro 	return error;
17972ad94ae6SAl Viro }
17982ad94ae6SAl Viro 
17991da177e4SLinus Torvalds /*
18001da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
18011da177e4SLinus Torvalds  * minimal.
18021da177e4SLinus Torvalds  */
18031da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
18041da177e4SLinus Torvalds {
1805da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1806da9592edSDavid Howells 
18071da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
18081da177e4SLinus Torvalds 		return 0;
1809e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
1810e795b717SSerge E. Hallyn 		goto other_userns;
1811da9592edSDavid Howells 	if (inode->i_uid == fsuid)
18121da177e4SLinus Torvalds 		return 0;
1813da9592edSDavid Howells 	if (dir->i_uid == fsuid)
18141da177e4SLinus Torvalds 		return 0;
1815e795b717SSerge E. Hallyn 
1816e795b717SSerge E. Hallyn other_userns:
1817e795b717SSerge E. Hallyn 	return !ns_capable(inode_userns(inode), CAP_FOWNER);
18181da177e4SLinus Torvalds }
18191da177e4SLinus Torvalds 
18201da177e4SLinus Torvalds /*
18211da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
18221da177e4SLinus Torvalds  *  whether the type of victim is right.
18231da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
18241da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
18251da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
18261da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
18271da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
18281da177e4SLinus Torvalds  *	a. be owner of dir, or
18291da177e4SLinus Torvalds  *	b. be owner of victim, or
18301da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
18311da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
18321da177e4SLinus Torvalds  *     links pointing to it.
18331da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
18341da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
18351da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
18361da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
18371da177e4SLinus Torvalds  *     nfs_async_unlink().
18381da177e4SLinus Torvalds  */
1839858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
18401da177e4SLinus Torvalds {
18411da177e4SLinus Torvalds 	int error;
18421da177e4SLinus Torvalds 
18431da177e4SLinus Torvalds 	if (!victim->d_inode)
18441da177e4SLinus Torvalds 		return -ENOENT;
18451da177e4SLinus Torvalds 
18461da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
1847cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
18481da177e4SLinus Torvalds 
1849f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
18501da177e4SLinus Torvalds 	if (error)
18511da177e4SLinus Torvalds 		return error;
18521da177e4SLinus Torvalds 	if (IS_APPEND(dir))
18531da177e4SLinus Torvalds 		return -EPERM;
18541da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1855f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
18561da177e4SLinus Torvalds 		return -EPERM;
18571da177e4SLinus Torvalds 	if (isdir) {
18581da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
18591da177e4SLinus Torvalds 			return -ENOTDIR;
18601da177e4SLinus Torvalds 		if (IS_ROOT(victim))
18611da177e4SLinus Torvalds 			return -EBUSY;
18621da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
18631da177e4SLinus Torvalds 		return -EISDIR;
18641da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18651da177e4SLinus Torvalds 		return -ENOENT;
18661da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
18671da177e4SLinus Torvalds 		return -EBUSY;
18681da177e4SLinus Torvalds 	return 0;
18691da177e4SLinus Torvalds }
18701da177e4SLinus Torvalds 
18711da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
18721da177e4SLinus Torvalds  *  dir.
18731da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
18741da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
18751da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
18761da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
18771da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
18781da177e4SLinus Torvalds  */
1879a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
18801da177e4SLinus Torvalds {
18811da177e4SLinus Torvalds 	if (child->d_inode)
18821da177e4SLinus Torvalds 		return -EEXIST;
18831da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18841da177e4SLinus Torvalds 		return -ENOENT;
1885f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
18861da177e4SLinus Torvalds }
18871da177e4SLinus Torvalds 
18881da177e4SLinus Torvalds /*
18891da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
18901da177e4SLinus Torvalds  */
18911da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
18921da177e4SLinus Torvalds {
18931da177e4SLinus Torvalds 	struct dentry *p;
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds 	if (p1 == p2) {
1896f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
18971da177e4SLinus Torvalds 		return NULL;
18981da177e4SLinus Torvalds 	}
18991da177e4SLinus Torvalds 
1900a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
19011da177e4SLinus Torvalds 
1902e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1903e2761a11SOGAWA Hirofumi 	if (p) {
1904f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1905f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
19061da177e4SLinus Torvalds 		return p;
19071da177e4SLinus Torvalds 	}
19081da177e4SLinus Torvalds 
1909e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1910e2761a11SOGAWA Hirofumi 	if (p) {
1911f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1912f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
19131da177e4SLinus Torvalds 		return p;
19141da177e4SLinus Torvalds 	}
19151da177e4SLinus Torvalds 
1916f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1917f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
19181da177e4SLinus Torvalds 	return NULL;
19191da177e4SLinus Torvalds }
19201da177e4SLinus Torvalds 
19211da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
19221da177e4SLinus Torvalds {
19231b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
19241da177e4SLinus Torvalds 	if (p1 != p2) {
19251b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1926a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
19271da177e4SLinus Torvalds 	}
19281da177e4SLinus Torvalds }
19291da177e4SLinus Torvalds 
19301da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
19311da177e4SLinus Torvalds 		struct nameidata *nd)
19321da177e4SLinus Torvalds {
1933a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds 	if (error)
19361da177e4SLinus Torvalds 		return error;
19371da177e4SLinus Torvalds 
1938acfa4380SAl Viro 	if (!dir->i_op->create)
19391da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
19401da177e4SLinus Torvalds 	mode &= S_IALLUGO;
19411da177e4SLinus Torvalds 	mode |= S_IFREG;
19421da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
19431da177e4SLinus Torvalds 	if (error)
19441da177e4SLinus Torvalds 		return error;
19451da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
1946a74574aaSStephen Smalley 	if (!error)
1947f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
19481da177e4SLinus Torvalds 	return error;
19491da177e4SLinus Torvalds }
19501da177e4SLinus Torvalds 
195173d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
19521da177e4SLinus Torvalds {
19533fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
19541da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
19551da177e4SLinus Torvalds 	int error;
19561da177e4SLinus Torvalds 
1957bcda7652SAl Viro 	/* O_PATH? */
1958bcda7652SAl Viro 	if (!acc_mode)
1959bcda7652SAl Viro 		return 0;
1960bcda7652SAl Viro 
19611da177e4SLinus Torvalds 	if (!inode)
19621da177e4SLinus Torvalds 		return -ENOENT;
19631da177e4SLinus Torvalds 
1964c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
1965c8fe8f30SChristoph Hellwig 	case S_IFLNK:
19661da177e4SLinus Torvalds 		return -ELOOP;
1967c8fe8f30SChristoph Hellwig 	case S_IFDIR:
1968c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
19691da177e4SLinus Torvalds 			return -EISDIR;
1970c8fe8f30SChristoph Hellwig 		break;
1971c8fe8f30SChristoph Hellwig 	case S_IFBLK:
1972c8fe8f30SChristoph Hellwig 	case S_IFCHR:
19733fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
19741da177e4SLinus Torvalds 			return -EACCES;
1975c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
1976c8fe8f30SChristoph Hellwig 	case S_IFIFO:
1977c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
19781da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
1979c8fe8f30SChristoph Hellwig 		break;
19804a3fd211SDave Hansen 	}
1981b41572e9SDave Hansen 
19823fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
1983b41572e9SDave Hansen 	if (error)
1984b41572e9SDave Hansen 		return error;
19856146f0d5SMimi Zohar 
19861da177e4SLinus Torvalds 	/*
19871da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
19881da177e4SLinus Torvalds 	 */
19891da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
19908737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
19917715b521SAl Viro 			return -EPERM;
19921da177e4SLinus Torvalds 		if (flag & O_TRUNC)
19937715b521SAl Viro 			return -EPERM;
19941da177e4SLinus Torvalds 	}
19951da177e4SLinus Torvalds 
19961da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
19972e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
19987715b521SAl Viro 		return -EPERM;
19991da177e4SLinus Torvalds 
20001da177e4SLinus Torvalds 	/*
20011da177e4SLinus Torvalds 	 * Ensure there are no outstanding leases on the file.
20021da177e4SLinus Torvalds 	 */
2003b65a9cfcSAl Viro 	return break_lease(inode, flag);
20047715b521SAl Viro }
20057715b521SAl Viro 
2006e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
20077715b521SAl Viro {
2008e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
20097715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
20107715b521SAl Viro 	int error = get_write_access(inode);
20111da177e4SLinus Torvalds 	if (error)
20127715b521SAl Viro 		return error;
20131da177e4SLinus Torvalds 	/*
20141da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
20151da177e4SLinus Torvalds 	 */
20161da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2017be6d3e56SKentaro Takeda 	if (!error)
2018ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
20191da177e4SLinus Torvalds 	if (!error) {
20207715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2021d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2022e1181ee6SJeff Layton 				    filp);
20231da177e4SLinus Torvalds 	}
20241da177e4SLinus Torvalds 	put_write_access(inode);
2025acd0c935SMimi Zohar 	return error;
20261da177e4SLinus Torvalds }
20271da177e4SLinus Torvalds 
2028d57999e1SDave Hansen /*
2029d57999e1SDave Hansen  * Note that while the flag value (low two bits) for sys_open means:
2030d57999e1SDave Hansen  *	00 - read-only
2031d57999e1SDave Hansen  *	01 - write-only
2032d57999e1SDave Hansen  *	10 - read-write
2033d57999e1SDave Hansen  *	11 - special
2034d57999e1SDave Hansen  * it is changed into
2035d57999e1SDave Hansen  *	00 - no permissions needed
2036d57999e1SDave Hansen  *	01 - read-permission
2037d57999e1SDave Hansen  *	10 - write-permission
2038d57999e1SDave Hansen  *	11 - read-write
2039d57999e1SDave Hansen  * for the internal routines (ie open_namei()/follow_link() etc)
2040d57999e1SDave Hansen  * This is more logical, and also allows the 00 "no perm needed"
2041d57999e1SDave Hansen  * to be used for symlinks (where the permissions are checked
2042d57999e1SDave Hansen  * later).
2043d57999e1SDave Hansen  *
2044d57999e1SDave Hansen */
2045d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2046d57999e1SDave Hansen {
2047d57999e1SDave Hansen 	if ((flag+1) & O_ACCMODE)
2048d57999e1SDave Hansen 		flag++;
2049d57999e1SDave Hansen 	return flag;
2050d57999e1SDave Hansen }
2051d57999e1SDave Hansen 
205231e6b01fSNick Piggin /*
2053fe2d35ffSAl Viro  * Handle the last step of open()
205431e6b01fSNick Piggin  */
2055fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
2056c3e380b0SAl Viro 			    const struct open_flags *op, const char *pathname)
2057fb1cc555SAl Viro {
2058a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
20596c0d46c4SAl Viro 	struct dentry *dentry;
2060ca344a89SAl Viro 	int open_flag = op->open_flag;
20616c0d46c4SAl Viro 	int will_truncate = open_flag & O_TRUNC;
2062ca344a89SAl Viro 	int want_write = 0;
2063bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2064fb1cc555SAl Viro 	struct file *filp;
206516c2cd71SAl Viro 	int error;
2066fb1cc555SAl Viro 
2067c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2068c3e380b0SAl Viro 	nd->flags |= op->intent;
2069c3e380b0SAl Viro 
20701f36f774SAl Viro 	switch (nd->last_type) {
20711f36f774SAl Viro 	case LAST_DOTDOT:
2072176306f5SNeil Brown 	case LAST_DOT:
2073fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2074fe2d35ffSAl Viro 		if (error)
2075fe2d35ffSAl Viro 			return ERR_PTR(error);
20761f36f774SAl Viro 		/* fallthrough */
20771f36f774SAl Viro 	case LAST_ROOT:
2078fe2d35ffSAl Viro 		if (nd->flags & LOOKUP_RCU) {
2079fe2d35ffSAl Viro 			if (nameidata_drop_rcu_last(nd))
2080fe2d35ffSAl Viro 				return ERR_PTR(-ECHILD);
2081fe2d35ffSAl Viro 		}
208216c2cd71SAl Viro 		error = handle_reval_path(nd);
208316c2cd71SAl Viro 		if (error)
208416c2cd71SAl Viro 			goto exit;
2085fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2086ca344a89SAl Viro 		if (open_flag & O_CREAT) {
208716c2cd71SAl Viro 			error = -EISDIR;
20881f36f774SAl Viro 			goto exit;
2089fe2d35ffSAl Viro 		}
2090fe2d35ffSAl Viro 		goto ok;
20911f36f774SAl Viro 	case LAST_BIND:
2092fe2d35ffSAl Viro 		/* can't be RCU mode here */
209316c2cd71SAl Viro 		error = handle_reval_path(nd);
209416c2cd71SAl Viro 		if (error)
209516c2cd71SAl Viro 			goto exit;
20961f36f774SAl Viro 		audit_inode(pathname, dir);
20971f36f774SAl Viro 		goto ok;
20981f36f774SAl Viro 	}
2099a2c36b45SAl Viro 
2100ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2101bcda7652SAl Viro 		int symlink_ok = 0;
2102fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2103fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2104bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2105bcda7652SAl Viro 			symlink_ok = 1;
2106fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2107ce57dfc1SAl Viro 		error = walk_component(nd, path, &nd->last, LAST_NORM,
2108ce57dfc1SAl Viro 					!symlink_ok);
2109ce57dfc1SAl Viro 		if (error < 0)
2110fe2d35ffSAl Viro 			return ERR_PTR(error);
2111ce57dfc1SAl Viro 		if (error) /* symlink */
2112fe2d35ffSAl Viro 			return NULL;
2113fe2d35ffSAl Viro 		/* sayonara */
2114fe2d35ffSAl Viro 		if (nd->flags & LOOKUP_RCU) {
2115fe2d35ffSAl Viro 			if (nameidata_drop_rcu_last(nd))
2116fe2d35ffSAl Viro 				return ERR_PTR(-ECHILD);
2117fe2d35ffSAl Viro 		}
2118fe2d35ffSAl Viro 
2119fe2d35ffSAl Viro 		error = -ENOTDIR;
2120fe2d35ffSAl Viro 		if (nd->flags & LOOKUP_DIRECTORY) {
2121ce57dfc1SAl Viro 			if (!nd->inode->i_op->lookup)
2122fe2d35ffSAl Viro 				goto exit;
2123fe2d35ffSAl Viro 		}
2124fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2125fe2d35ffSAl Viro 		goto ok;
2126fe2d35ffSAl Viro 	}
2127fe2d35ffSAl Viro 
2128fe2d35ffSAl Viro 	/* create side of things */
2129fe2d35ffSAl Viro 
2130fe2d35ffSAl Viro 	if (nd->flags & LOOKUP_RCU) {
2131fe2d35ffSAl Viro 		if (nameidata_drop_rcu_last(nd))
2132fe2d35ffSAl Viro 			return ERR_PTR(-ECHILD);
2133fe2d35ffSAl Viro 	}
2134fe2d35ffSAl Viro 
2135fe2d35ffSAl Viro 	audit_inode(pathname, dir);
213616c2cd71SAl Viro 	error = -EISDIR;
21371f36f774SAl Viro 	/* trailing slashes? */
213831e6b01fSNick Piggin 	if (nd->last.name[nd->last.len])
21391f36f774SAl Viro 		goto exit;
21401f36f774SAl Viro 
2141a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
2142a1e28038SAl Viro 
21436c0d46c4SAl Viro 	dentry = lookup_hash(nd);
21446c0d46c4SAl Viro 	error = PTR_ERR(dentry);
21456c0d46c4SAl Viro 	if (IS_ERR(dentry)) {
2146fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
2147fb1cc555SAl Viro 		goto exit;
2148fb1cc555SAl Viro 	}
2149fb1cc555SAl Viro 
21506c0d46c4SAl Viro 	path->dentry = dentry;
21516c0d46c4SAl Viro 	path->mnt = nd->path.mnt;
21526c0d46c4SAl Viro 
2153fb1cc555SAl Viro 	/* Negative dentry, just create the file */
21546c0d46c4SAl Viro 	if (!dentry->d_inode) {
21556c0d46c4SAl Viro 		int mode = op->mode;
21566c0d46c4SAl Viro 		if (!IS_POSIXACL(dir->d_inode))
21576c0d46c4SAl Viro 			mode &= ~current_umask();
2158fb1cc555SAl Viro 		/*
2159fb1cc555SAl Viro 		 * This write is needed to ensure that a
21606c0d46c4SAl Viro 		 * rw->ro transition does not occur between
2161fb1cc555SAl Viro 		 * the time when the file is created and when
2162fb1cc555SAl Viro 		 * a permanent write count is taken through
2163fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
2164fb1cc555SAl Viro 		 */
2165fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
2166fb1cc555SAl Viro 		if (error)
2167fb1cc555SAl Viro 			goto exit_mutex_unlock;
2168ca344a89SAl Viro 		want_write = 1;
21699b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2170ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
21716c0d46c4SAl Viro 		will_truncate = 0;
2172bcda7652SAl Viro 		acc_mode = MAY_OPEN;
21736c0d46c4SAl Viro 		error = security_path_mknod(&nd->path, dentry, mode, 0);
21746c0d46c4SAl Viro 		if (error)
21756c0d46c4SAl Viro 			goto exit_mutex_unlock;
21766c0d46c4SAl Viro 		error = vfs_create(dir->d_inode, dentry, mode, nd);
21776c0d46c4SAl Viro 		if (error)
21786c0d46c4SAl Viro 			goto exit_mutex_unlock;
21796c0d46c4SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
21806c0d46c4SAl Viro 		dput(nd->path.dentry);
21816c0d46c4SAl Viro 		nd->path.dentry = dentry;
2182ca344a89SAl Viro 		goto common;
2183fb1cc555SAl Viro 	}
2184fb1cc555SAl Viro 
2185fb1cc555SAl Viro 	/*
2186fb1cc555SAl Viro 	 * It already exists.
2187fb1cc555SAl Viro 	 */
2188fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2189fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2190fb1cc555SAl Viro 
2191fb1cc555SAl Viro 	error = -EEXIST;
2192ca344a89SAl Viro 	if (open_flag & O_EXCL)
2193fb1cc555SAl Viro 		goto exit_dput;
2194fb1cc555SAl Viro 
21959875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
21969875cf80SDavid Howells 	if (error < 0)
2197fb1cc555SAl Viro 		goto exit_dput;
2198fb1cc555SAl Viro 
2199fb1cc555SAl Viro 	error = -ENOENT;
2200fb1cc555SAl Viro 	if (!path->dentry->d_inode)
2201fb1cc555SAl Viro 		goto exit_dput;
22029e67f361SAl Viro 
22039e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
2204fb1cc555SAl Viro 		return NULL;
2205fb1cc555SAl Viro 
2206fb1cc555SAl Viro 	path_to_nameidata(path, nd);
220731e6b01fSNick Piggin 	nd->inode = path->dentry->d_inode;
2208fb1cc555SAl Viro 	error = -EISDIR;
220931e6b01fSNick Piggin 	if (S_ISDIR(nd->inode->i_mode))
2210fb1cc555SAl Viro 		goto exit;
221167ee3ad2SAl Viro ok:
22126c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
22136c0d46c4SAl Viro 		will_truncate = 0;
22146c0d46c4SAl Viro 
22150f9d1a10SAl Viro 	if (will_truncate) {
22160f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
22170f9d1a10SAl Viro 		if (error)
22180f9d1a10SAl Viro 			goto exit;
2219ca344a89SAl Viro 		want_write = 1;
22200f9d1a10SAl Viro 	}
2221ca344a89SAl Viro common:
2222bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2223ca344a89SAl Viro 	if (error)
22240f9d1a10SAl Viro 		goto exit;
22250f9d1a10SAl Viro 	filp = nameidata_to_filp(nd);
22260f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
22270f9d1a10SAl Viro 		error = ima_file_check(filp, op->acc_mode);
22280f9d1a10SAl Viro 		if (error) {
22290f9d1a10SAl Viro 			fput(filp);
22300f9d1a10SAl Viro 			filp = ERR_PTR(error);
22310f9d1a10SAl Viro 		}
22320f9d1a10SAl Viro 	}
22330f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
22340f9d1a10SAl Viro 		if (will_truncate) {
22350f9d1a10SAl Viro 			error = handle_truncate(filp);
22360f9d1a10SAl Viro 			if (error) {
22370f9d1a10SAl Viro 				fput(filp);
22380f9d1a10SAl Viro 				filp = ERR_PTR(error);
22390f9d1a10SAl Viro 			}
22400f9d1a10SAl Viro 		}
22410f9d1a10SAl Viro 	}
2242ca344a89SAl Viro out:
2243ca344a89SAl Viro 	if (want_write)
22440f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
22450f9d1a10SAl Viro 	path_put(&nd->path);
2246fb1cc555SAl Viro 	return filp;
2247fb1cc555SAl Viro 
2248fb1cc555SAl Viro exit_mutex_unlock:
2249fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2250fb1cc555SAl Viro exit_dput:
2251fb1cc555SAl Viro 	path_put_conditional(path, nd);
2252fb1cc555SAl Viro exit:
2253ca344a89SAl Viro 	filp = ERR_PTR(error);
2254ca344a89SAl Viro 	goto out;
2255fb1cc555SAl Viro }
2256fb1cc555SAl Viro 
225713aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname,
225873d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
22591da177e4SLinus Torvalds {
2260fe2d35ffSAl Viro 	struct file *base = NULL;
22614a3fd211SDave Hansen 	struct file *filp;
22629850c056SAl Viro 	struct path path;
226313aab428SAl Viro 	int error;
226431e6b01fSNick Piggin 
226531e6b01fSNick Piggin 	filp = get_empty_filp();
226631e6b01fSNick Piggin 	if (!filp)
226731e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
226831e6b01fSNick Piggin 
226947c805dcSAl Viro 	filp->f_flags = op->open_flag;
227073d049a4SAl Viro 	nd->intent.open.file = filp;
227173d049a4SAl Viro 	nd->intent.open.flags = open_to_namei_flags(op->open_flag);
227273d049a4SAl Viro 	nd->intent.open.create_mode = op->mode;
227331e6b01fSNick Piggin 
227473d049a4SAl Viro 	error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
227531e6b01fSNick Piggin 	if (unlikely(error))
227613aab428SAl Viro 		goto out_filp;
227731e6b01fSNick Piggin 
2278fe2d35ffSAl Viro 	current->total_link_count = 0;
227973d049a4SAl Viro 	error = link_path_walk(pathname, nd);
228031e6b01fSNick Piggin 	if (unlikely(error))
228131e6b01fSNick Piggin 		goto out_filp;
22821da177e4SLinus Torvalds 
228373d049a4SAl Viro 	filp = do_last(nd, &path, op, pathname);
2284806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
22857b9337aaSNick Piggin 		struct path link = path;
2286def4af30SAl Viro 		void *cookie;
2287574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
228873d049a4SAl Viro 			path_put_conditional(&path, nd);
228973d049a4SAl Viro 			path_put(&nd->path);
229040b39136SAl Viro 			filp = ERR_PTR(-ELOOP);
229140b39136SAl Viro 			break;
229240b39136SAl Viro 		}
229373d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
229473d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2295574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
2296c3e380b0SAl Viro 		if (unlikely(error))
2297f1afe9efSAl Viro 			filp = ERR_PTR(error);
2298c3e380b0SAl Viro 		else
229973d049a4SAl Viro 			filp = do_last(nd, &path, op, pathname);
2300574197e0SAl Viro 		put_link(nd, &link, cookie);
2301806b681cSAl Viro 	}
230210fa8e62SAl Viro out:
230373d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
230473d049a4SAl Viro 		path_put(&nd->root);
2305fe2d35ffSAl Viro 	if (base)
2306fe2d35ffSAl Viro 		fput(base);
230773d049a4SAl Viro 	release_open_intent(nd);
230810fa8e62SAl Viro 	return filp;
23091da177e4SLinus Torvalds 
231031e6b01fSNick Piggin out_filp:
231110fa8e62SAl Viro 	filp = ERR_PTR(error);
231210fa8e62SAl Viro 	goto out;
2313de459215SKirill Korotaev }
23141da177e4SLinus Torvalds 
231513aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname,
231613aab428SAl Viro 		const struct open_flags *op, int flags)
231713aab428SAl Viro {
231873d049a4SAl Viro 	struct nameidata nd;
231913aab428SAl Viro 	struct file *filp;
232013aab428SAl Viro 
232173d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
232213aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
232373d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
232413aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
232573d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
232613aab428SAl Viro 	return filp;
232713aab428SAl Viro }
232813aab428SAl Viro 
232973d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
233073d049a4SAl Viro 		const char *name, const struct open_flags *op, int flags)
233173d049a4SAl Viro {
233273d049a4SAl Viro 	struct nameidata nd;
233373d049a4SAl Viro 	struct file *file;
233473d049a4SAl Viro 
233573d049a4SAl Viro 	nd.root.mnt = mnt;
233673d049a4SAl Viro 	nd.root.dentry = dentry;
233773d049a4SAl Viro 
233873d049a4SAl Viro 	flags |= LOOKUP_ROOT;
233973d049a4SAl Viro 
2340bcda7652SAl Viro 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
234173d049a4SAl Viro 		return ERR_PTR(-ELOOP);
234273d049a4SAl Viro 
234373d049a4SAl Viro 	file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
234473d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
234573d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags);
234673d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
234773d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
234873d049a4SAl Viro 	return file;
234973d049a4SAl Viro }
235073d049a4SAl Viro 
23511da177e4SLinus Torvalds /**
23521da177e4SLinus Torvalds  * lookup_create - lookup a dentry, creating it if it doesn't exist
23531da177e4SLinus Torvalds  * @nd: nameidata info
23541da177e4SLinus Torvalds  * @is_dir: directory flag
23551da177e4SLinus Torvalds  *
23561da177e4SLinus Torvalds  * Simple function to lookup and return a dentry and create it
23571da177e4SLinus Torvalds  * if it doesn't exist.  Is SMP-safe.
2358c663e5d8SChristoph Hellwig  *
23594ac91378SJan Blunck  * Returns with nd->path.dentry->d_inode->i_mutex locked.
23601da177e4SLinus Torvalds  */
23611da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir)
23621da177e4SLinus Torvalds {
2363c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
23641da177e4SLinus Torvalds 
23654ac91378SJan Blunck 	mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2366c663e5d8SChristoph Hellwig 	/*
2367c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2368c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2369c663e5d8SChristoph Hellwig 	 */
23701da177e4SLinus Torvalds 	if (nd->last_type != LAST_NORM)
23711da177e4SLinus Torvalds 		goto fail;
23721da177e4SLinus Torvalds 	nd->flags &= ~LOOKUP_PARENT;
23733516586aSAl Viro 	nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2374a634904aSASANO Masahiro 	nd->intent.open.flags = O_EXCL;
2375c663e5d8SChristoph Hellwig 
2376c663e5d8SChristoph Hellwig 	/*
2377c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2378c663e5d8SChristoph Hellwig 	 */
237949705b77SChristoph Hellwig 	dentry = lookup_hash(nd);
23801da177e4SLinus Torvalds 	if (IS_ERR(dentry))
23811da177e4SLinus Torvalds 		goto fail;
2382c663e5d8SChristoph Hellwig 
2383e9baf6e5SAl Viro 	if (dentry->d_inode)
2384e9baf6e5SAl Viro 		goto eexist;
2385c663e5d8SChristoph Hellwig 	/*
2386c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2387c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2388c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2389c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2390c663e5d8SChristoph Hellwig 	 */
2391e9baf6e5SAl Viro 	if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
23921da177e4SLinus Torvalds 		dput(dentry);
23931da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2394e9baf6e5SAl Viro 	}
2395e9baf6e5SAl Viro 	return dentry;
2396e9baf6e5SAl Viro eexist:
2397e9baf6e5SAl Viro 	dput(dentry);
2398e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
23991da177e4SLinus Torvalds fail:
24001da177e4SLinus Torvalds 	return dentry;
24011da177e4SLinus Torvalds }
2402f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create);
24031da177e4SLinus Torvalds 
24041da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
24051da177e4SLinus Torvalds {
2406a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
24071da177e4SLinus Torvalds 
24081da177e4SLinus Torvalds 	if (error)
24091da177e4SLinus Torvalds 		return error;
24101da177e4SLinus Torvalds 
2411e795b717SSerge E. Hallyn 	if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2412e795b717SSerge E. Hallyn 	    !ns_capable(inode_userns(dir), CAP_MKNOD))
24131da177e4SLinus Torvalds 		return -EPERM;
24141da177e4SLinus Torvalds 
2415acfa4380SAl Viro 	if (!dir->i_op->mknod)
24161da177e4SLinus Torvalds 		return -EPERM;
24171da177e4SLinus Torvalds 
241808ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
241908ce5f16SSerge E. Hallyn 	if (error)
242008ce5f16SSerge E. Hallyn 		return error;
242108ce5f16SSerge E. Hallyn 
24221da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
24231da177e4SLinus Torvalds 	if (error)
24241da177e4SLinus Torvalds 		return error;
24251da177e4SLinus Torvalds 
24261da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2427a74574aaSStephen Smalley 	if (!error)
2428f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
24291da177e4SLinus Torvalds 	return error;
24301da177e4SLinus Torvalds }
24311da177e4SLinus Torvalds 
2432463c3197SDave Hansen static int may_mknod(mode_t mode)
2433463c3197SDave Hansen {
2434463c3197SDave Hansen 	switch (mode & S_IFMT) {
2435463c3197SDave Hansen 	case S_IFREG:
2436463c3197SDave Hansen 	case S_IFCHR:
2437463c3197SDave Hansen 	case S_IFBLK:
2438463c3197SDave Hansen 	case S_IFIFO:
2439463c3197SDave Hansen 	case S_IFSOCK:
2440463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2441463c3197SDave Hansen 		return 0;
2442463c3197SDave Hansen 	case S_IFDIR:
2443463c3197SDave Hansen 		return -EPERM;
2444463c3197SDave Hansen 	default:
2445463c3197SDave Hansen 		return -EINVAL;
2446463c3197SDave Hansen 	}
2447463c3197SDave Hansen }
2448463c3197SDave Hansen 
24492e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
24502e4d0924SHeiko Carstens 		unsigned, dev)
24511da177e4SLinus Torvalds {
24522ad94ae6SAl Viro 	int error;
24531da177e4SLinus Torvalds 	char *tmp;
24541da177e4SLinus Torvalds 	struct dentry *dentry;
24551da177e4SLinus Torvalds 	struct nameidata nd;
24561da177e4SLinus Torvalds 
24571da177e4SLinus Torvalds 	if (S_ISDIR(mode))
24581da177e4SLinus Torvalds 		return -EPERM;
24591da177e4SLinus Torvalds 
24602ad94ae6SAl Viro 	error = user_path_parent(dfd, filename, &nd, &tmp);
24611da177e4SLinus Torvalds 	if (error)
24622ad94ae6SAl Viro 		return error;
24632ad94ae6SAl Viro 
24641da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
2465463c3197SDave Hansen 	if (IS_ERR(dentry)) {
24661da177e4SLinus Torvalds 		error = PTR_ERR(dentry);
2467463c3197SDave Hansen 		goto out_unlock;
2468463c3197SDave Hansen 	}
24694ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2470ce3b0f8dSAl Viro 		mode &= ~current_umask();
2471463c3197SDave Hansen 	error = may_mknod(mode);
2472463c3197SDave Hansen 	if (error)
2473463c3197SDave Hansen 		goto out_dput;
2474463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2475463c3197SDave Hansen 	if (error)
2476463c3197SDave Hansen 		goto out_dput;
2477be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd.path, dentry, mode, dev);
2478be6d3e56SKentaro Takeda 	if (error)
2479be6d3e56SKentaro Takeda 		goto out_drop_write;
24801da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
24811da177e4SLinus Torvalds 		case 0: case S_IFREG:
24824ac91378SJan Blunck 			error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
24831da177e4SLinus Torvalds 			break;
24841da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
24854ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
24861da177e4SLinus Torvalds 					new_decode_dev(dev));
24871da177e4SLinus Torvalds 			break;
24881da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
24894ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
24901da177e4SLinus Torvalds 			break;
24911da177e4SLinus Torvalds 	}
2492be6d3e56SKentaro Takeda out_drop_write:
2493463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2494463c3197SDave Hansen out_dput:
24951da177e4SLinus Torvalds 	dput(dentry);
2496463c3197SDave Hansen out_unlock:
24974ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
24981d957f9bSJan Blunck 	path_put(&nd.path);
24991da177e4SLinus Torvalds 	putname(tmp);
25001da177e4SLinus Torvalds 
25011da177e4SLinus Torvalds 	return error;
25021da177e4SLinus Torvalds }
25031da177e4SLinus Torvalds 
25043480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
25055590ff0dSUlrich Drepper {
25065590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
25075590ff0dSUlrich Drepper }
25085590ff0dSUlrich Drepper 
25091da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
25101da177e4SLinus Torvalds {
2511a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
25121da177e4SLinus Torvalds 
25131da177e4SLinus Torvalds 	if (error)
25141da177e4SLinus Torvalds 		return error;
25151da177e4SLinus Torvalds 
2516acfa4380SAl Viro 	if (!dir->i_op->mkdir)
25171da177e4SLinus Torvalds 		return -EPERM;
25181da177e4SLinus Torvalds 
25191da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
25201da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
25211da177e4SLinus Torvalds 	if (error)
25221da177e4SLinus Torvalds 		return error;
25231da177e4SLinus Torvalds 
25241da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2525a74574aaSStephen Smalley 	if (!error)
2526f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
25271da177e4SLinus Torvalds 	return error;
25281da177e4SLinus Torvalds }
25291da177e4SLinus Torvalds 
25302e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
25311da177e4SLinus Torvalds {
25321da177e4SLinus Torvalds 	int error = 0;
25331da177e4SLinus Torvalds 	char * tmp;
25346902d925SDave Hansen 	struct dentry *dentry;
25356902d925SDave Hansen 	struct nameidata nd;
25361da177e4SLinus Torvalds 
25372ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &tmp);
25382ad94ae6SAl Viro 	if (error)
25396902d925SDave Hansen 		goto out_err;
25401da177e4SLinus Torvalds 
25411da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 1);
25421da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
25436902d925SDave Hansen 	if (IS_ERR(dentry))
25446902d925SDave Hansen 		goto out_unlock;
25456902d925SDave Hansen 
25464ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2547ce3b0f8dSAl Viro 		mode &= ~current_umask();
2548463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2549463c3197SDave Hansen 	if (error)
2550463c3197SDave Hansen 		goto out_dput;
2551be6d3e56SKentaro Takeda 	error = security_path_mkdir(&nd.path, dentry, mode);
2552be6d3e56SKentaro Takeda 	if (error)
2553be6d3e56SKentaro Takeda 		goto out_drop_write;
25544ac91378SJan Blunck 	error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2555be6d3e56SKentaro Takeda out_drop_write:
2556463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2557463c3197SDave Hansen out_dput:
25581da177e4SLinus Torvalds 	dput(dentry);
25596902d925SDave Hansen out_unlock:
25604ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
25611d957f9bSJan Blunck 	path_put(&nd.path);
25621da177e4SLinus Torvalds 	putname(tmp);
25636902d925SDave Hansen out_err:
25641da177e4SLinus Torvalds 	return error;
25651da177e4SLinus Torvalds }
25661da177e4SLinus Torvalds 
25673cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
25685590ff0dSUlrich Drepper {
25695590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
25705590ff0dSUlrich Drepper }
25715590ff0dSUlrich Drepper 
25721da177e4SLinus Torvalds /*
25731da177e4SLinus Torvalds  * We try to drop the dentry early: we should have
25741da177e4SLinus Torvalds  * a usage count of 2 if we're the only user of this
25751da177e4SLinus Torvalds  * dentry, and if that is true (possibly after pruning
25761da177e4SLinus Torvalds  * the dcache), then we drop the dentry now.
25771da177e4SLinus Torvalds  *
25781da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
25791da177e4SLinus Torvalds  * do a
25801da177e4SLinus Torvalds  *
25811da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
25821da177e4SLinus Torvalds  *		return -EBUSY;
25831da177e4SLinus Torvalds  *
25841da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
25851da177e4SLinus Torvalds  * that is still in use by something else..
25861da177e4SLinus Torvalds  */
25871da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
25881da177e4SLinus Torvalds {
25891da177e4SLinus Torvalds 	dget(dentry);
25901da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
25911da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
2592b7ab39f6SNick Piggin 	if (dentry->d_count == 2)
25931da177e4SLinus Torvalds 		__d_drop(dentry);
25941da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
25951da177e4SLinus Torvalds }
25961da177e4SLinus Torvalds 
25971da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
25981da177e4SLinus Torvalds {
25991da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
26001da177e4SLinus Torvalds 
26011da177e4SLinus Torvalds 	if (error)
26021da177e4SLinus Torvalds 		return error;
26031da177e4SLinus Torvalds 
2604acfa4380SAl Viro 	if (!dir->i_op->rmdir)
26051da177e4SLinus Torvalds 		return -EPERM;
26061da177e4SLinus Torvalds 
26071b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
26081da177e4SLinus Torvalds 	dentry_unhash(dentry);
26091da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
26101da177e4SLinus Torvalds 		error = -EBUSY;
26111da177e4SLinus Torvalds 	else {
26121da177e4SLinus Torvalds 		error = security_inode_rmdir(dir, dentry);
26131da177e4SLinus Torvalds 		if (!error) {
26141da177e4SLinus Torvalds 			error = dir->i_op->rmdir(dir, dentry);
2615d83c49f3SAl Viro 			if (!error) {
26161da177e4SLinus Torvalds 				dentry->d_inode->i_flags |= S_DEAD;
2617d83c49f3SAl Viro 				dont_mount(dentry);
2618d83c49f3SAl Viro 			}
26191da177e4SLinus Torvalds 		}
26201da177e4SLinus Torvalds 	}
26211b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
26221da177e4SLinus Torvalds 	if (!error) {
26231da177e4SLinus Torvalds 		d_delete(dentry);
26241da177e4SLinus Torvalds 	}
26251da177e4SLinus Torvalds 	dput(dentry);
26261da177e4SLinus Torvalds 
26271da177e4SLinus Torvalds 	return error;
26281da177e4SLinus Torvalds }
26291da177e4SLinus Torvalds 
26305590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
26311da177e4SLinus Torvalds {
26321da177e4SLinus Torvalds 	int error = 0;
26331da177e4SLinus Torvalds 	char * name;
26341da177e4SLinus Torvalds 	struct dentry *dentry;
26351da177e4SLinus Torvalds 	struct nameidata nd;
26361da177e4SLinus Torvalds 
26372ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
26381da177e4SLinus Torvalds 	if (error)
26392ad94ae6SAl Viro 		return error;
26401da177e4SLinus Torvalds 
26411da177e4SLinus Torvalds 	switch(nd.last_type) {
26421da177e4SLinus Torvalds 	case LAST_DOTDOT:
26431da177e4SLinus Torvalds 		error = -ENOTEMPTY;
26441da177e4SLinus Torvalds 		goto exit1;
26451da177e4SLinus Torvalds 	case LAST_DOT:
26461da177e4SLinus Torvalds 		error = -EINVAL;
26471da177e4SLinus Torvalds 		goto exit1;
26481da177e4SLinus Torvalds 	case LAST_ROOT:
26491da177e4SLinus Torvalds 		error = -EBUSY;
26501da177e4SLinus Torvalds 		goto exit1;
26511da177e4SLinus Torvalds 	}
26520612d9fbSOGAWA Hirofumi 
26530612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
26540612d9fbSOGAWA Hirofumi 
26554ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
265649705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
26571da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
26586902d925SDave Hansen 	if (IS_ERR(dentry))
26596902d925SDave Hansen 		goto exit2;
26600622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
26610622753bSDave Hansen 	if (error)
26620622753bSDave Hansen 		goto exit3;
2663be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2664be6d3e56SKentaro Takeda 	if (error)
2665be6d3e56SKentaro Takeda 		goto exit4;
26664ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2667be6d3e56SKentaro Takeda exit4:
26680622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
26690622753bSDave Hansen exit3:
26701da177e4SLinus Torvalds 	dput(dentry);
26716902d925SDave Hansen exit2:
26724ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
26731da177e4SLinus Torvalds exit1:
26741d957f9bSJan Blunck 	path_put(&nd.path);
26751da177e4SLinus Torvalds 	putname(name);
26761da177e4SLinus Torvalds 	return error;
26771da177e4SLinus Torvalds }
26781da177e4SLinus Torvalds 
26793cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
26805590ff0dSUlrich Drepper {
26815590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
26825590ff0dSUlrich Drepper }
26835590ff0dSUlrich Drepper 
26841da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
26851da177e4SLinus Torvalds {
26861da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
26871da177e4SLinus Torvalds 
26881da177e4SLinus Torvalds 	if (error)
26891da177e4SLinus Torvalds 		return error;
26901da177e4SLinus Torvalds 
2691acfa4380SAl Viro 	if (!dir->i_op->unlink)
26921da177e4SLinus Torvalds 		return -EPERM;
26931da177e4SLinus Torvalds 
26941b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
26951da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
26961da177e4SLinus Torvalds 		error = -EBUSY;
26971da177e4SLinus Torvalds 	else {
26981da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2699bec1052eSAl Viro 		if (!error) {
27001da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2701bec1052eSAl Viro 			if (!error)
2702d83c49f3SAl Viro 				dont_mount(dentry);
2703bec1052eSAl Viro 		}
27041da177e4SLinus Torvalds 	}
27051b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
27061da177e4SLinus Torvalds 
27071da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
27081da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2709ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
27101da177e4SLinus Torvalds 		d_delete(dentry);
27111da177e4SLinus Torvalds 	}
27120eeca283SRobert Love 
27131da177e4SLinus Torvalds 	return error;
27141da177e4SLinus Torvalds }
27151da177e4SLinus Torvalds 
27161da177e4SLinus Torvalds /*
27171da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
27181b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
27191da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
27201da177e4SLinus Torvalds  * while waiting on the I/O.
27211da177e4SLinus Torvalds  */
27225590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
27231da177e4SLinus Torvalds {
27242ad94ae6SAl Viro 	int error;
27251da177e4SLinus Torvalds 	char *name;
27261da177e4SLinus Torvalds 	struct dentry *dentry;
27271da177e4SLinus Torvalds 	struct nameidata nd;
27281da177e4SLinus Torvalds 	struct inode *inode = NULL;
27291da177e4SLinus Torvalds 
27302ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
27311da177e4SLinus Torvalds 	if (error)
27322ad94ae6SAl Viro 		return error;
27332ad94ae6SAl Viro 
27341da177e4SLinus Torvalds 	error = -EISDIR;
27351da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
27361da177e4SLinus Torvalds 		goto exit1;
27370612d9fbSOGAWA Hirofumi 
27380612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
27390612d9fbSOGAWA Hirofumi 
27404ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
274149705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
27421da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
27431da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
27441da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
27451da177e4SLinus Torvalds 		if (nd.last.name[nd.last.len])
27461da177e4SLinus Torvalds 			goto slashes;
27471da177e4SLinus Torvalds 		inode = dentry->d_inode;
27481da177e4SLinus Torvalds 		if (inode)
27497de9c6eeSAl Viro 			ihold(inode);
27500622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
27510622753bSDave Hansen 		if (error)
27520622753bSDave Hansen 			goto exit2;
2753be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2754be6d3e56SKentaro Takeda 		if (error)
2755be6d3e56SKentaro Takeda 			goto exit3;
27564ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2757be6d3e56SKentaro Takeda exit3:
27580622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
27591da177e4SLinus Torvalds 	exit2:
27601da177e4SLinus Torvalds 		dput(dentry);
27611da177e4SLinus Torvalds 	}
27624ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
27631da177e4SLinus Torvalds 	if (inode)
27641da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
27651da177e4SLinus Torvalds exit1:
27661d957f9bSJan Blunck 	path_put(&nd.path);
27671da177e4SLinus Torvalds 	putname(name);
27681da177e4SLinus Torvalds 	return error;
27691da177e4SLinus Torvalds 
27701da177e4SLinus Torvalds slashes:
27711da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
27721da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
27731da177e4SLinus Torvalds 	goto exit2;
27741da177e4SLinus Torvalds }
27751da177e4SLinus Torvalds 
27762e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
27775590ff0dSUlrich Drepper {
27785590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
27795590ff0dSUlrich Drepper 		return -EINVAL;
27805590ff0dSUlrich Drepper 
27815590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
27825590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
27835590ff0dSUlrich Drepper 
27845590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
27855590ff0dSUlrich Drepper }
27865590ff0dSUlrich Drepper 
27873480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
27885590ff0dSUlrich Drepper {
27895590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
27905590ff0dSUlrich Drepper }
27915590ff0dSUlrich Drepper 
2792db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
27931da177e4SLinus Torvalds {
2794a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
27951da177e4SLinus Torvalds 
27961da177e4SLinus Torvalds 	if (error)
27971da177e4SLinus Torvalds 		return error;
27981da177e4SLinus Torvalds 
2799acfa4380SAl Viro 	if (!dir->i_op->symlink)
28001da177e4SLinus Torvalds 		return -EPERM;
28011da177e4SLinus Torvalds 
28021da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
28031da177e4SLinus Torvalds 	if (error)
28041da177e4SLinus Torvalds 		return error;
28051da177e4SLinus Torvalds 
28061da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2807a74574aaSStephen Smalley 	if (!error)
2808f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
28091da177e4SLinus Torvalds 	return error;
28101da177e4SLinus Torvalds }
28111da177e4SLinus Torvalds 
28122e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
28132e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
28141da177e4SLinus Torvalds {
28152ad94ae6SAl Viro 	int error;
28161da177e4SLinus Torvalds 	char *from;
28171da177e4SLinus Torvalds 	char *to;
28186902d925SDave Hansen 	struct dentry *dentry;
28196902d925SDave Hansen 	struct nameidata nd;
28201da177e4SLinus Torvalds 
28211da177e4SLinus Torvalds 	from = getname(oldname);
28221da177e4SLinus Torvalds 	if (IS_ERR(from))
28231da177e4SLinus Torvalds 		return PTR_ERR(from);
28242ad94ae6SAl Viro 
28252ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
28262ad94ae6SAl Viro 	if (error)
28276902d925SDave Hansen 		goto out_putname;
28281da177e4SLinus Torvalds 
28291da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
28301da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
28316902d925SDave Hansen 	if (IS_ERR(dentry))
28326902d925SDave Hansen 		goto out_unlock;
28336902d925SDave Hansen 
283475c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
283575c3f29dSDave Hansen 	if (error)
283675c3f29dSDave Hansen 		goto out_dput;
2837be6d3e56SKentaro Takeda 	error = security_path_symlink(&nd.path, dentry, from);
2838be6d3e56SKentaro Takeda 	if (error)
2839be6d3e56SKentaro Takeda 		goto out_drop_write;
2840db2e747bSMiklos Szeredi 	error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2841be6d3e56SKentaro Takeda out_drop_write:
284275c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
284375c3f29dSDave Hansen out_dput:
28441da177e4SLinus Torvalds 	dput(dentry);
28456902d925SDave Hansen out_unlock:
28464ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
28471d957f9bSJan Blunck 	path_put(&nd.path);
28481da177e4SLinus Torvalds 	putname(to);
28496902d925SDave Hansen out_putname:
28501da177e4SLinus Torvalds 	putname(from);
28511da177e4SLinus Torvalds 	return error;
28521da177e4SLinus Torvalds }
28531da177e4SLinus Torvalds 
28543480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
28555590ff0dSUlrich Drepper {
28565590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
28575590ff0dSUlrich Drepper }
28585590ff0dSUlrich Drepper 
28591da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
28601da177e4SLinus Torvalds {
28611da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
28621da177e4SLinus Torvalds 	int error;
28631da177e4SLinus Torvalds 
28641da177e4SLinus Torvalds 	if (!inode)
28651da177e4SLinus Torvalds 		return -ENOENT;
28661da177e4SLinus Torvalds 
2867a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
28681da177e4SLinus Torvalds 	if (error)
28691da177e4SLinus Torvalds 		return error;
28701da177e4SLinus Torvalds 
28711da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
28721da177e4SLinus Torvalds 		return -EXDEV;
28731da177e4SLinus Torvalds 
28741da177e4SLinus Torvalds 	/*
28751da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
28761da177e4SLinus Torvalds 	 */
28771da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
28781da177e4SLinus Torvalds 		return -EPERM;
2879acfa4380SAl Viro 	if (!dir->i_op->link)
28801da177e4SLinus Torvalds 		return -EPERM;
28817e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
28821da177e4SLinus Torvalds 		return -EPERM;
28831da177e4SLinus Torvalds 
28841da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
28851da177e4SLinus Torvalds 	if (error)
28861da177e4SLinus Torvalds 		return error;
28871da177e4SLinus Torvalds 
28887e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
2889aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
2890aae8a97dSAneesh Kumar K.V 	if (inode->i_nlink == 0)
2891aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
2892aae8a97dSAneesh Kumar K.V 	else
28931da177e4SLinus Torvalds 		error = dir->i_op->link(old_dentry, dir, new_dentry);
28947e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
2895e31e14ecSStephen Smalley 	if (!error)
28967e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
28971da177e4SLinus Torvalds 	return error;
28981da177e4SLinus Torvalds }
28991da177e4SLinus Torvalds 
29001da177e4SLinus Torvalds /*
29011da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
29021da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
29031da177e4SLinus Torvalds  * newname.  --KAB
29041da177e4SLinus Torvalds  *
29051da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
29061da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
29071da177e4SLinus Torvalds  * and other special files.  --ADM
29081da177e4SLinus Torvalds  */
29092e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
29102e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
29111da177e4SLinus Torvalds {
29121da177e4SLinus Torvalds 	struct dentry *new_dentry;
29132d8f3038SAl Viro 	struct nameidata nd;
29142d8f3038SAl Viro 	struct path old_path;
291511a7b371SAneesh Kumar K.V 	int how = 0;
29161da177e4SLinus Torvalds 	int error;
29171da177e4SLinus Torvalds 	char *to;
29181da177e4SLinus Torvalds 
291911a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
2920c04030e1SUlrich Drepper 		return -EINVAL;
292111a7b371SAneesh Kumar K.V 	/*
292211a7b371SAneesh Kumar K.V 	 * To use null names we require CAP_DAC_READ_SEARCH
292311a7b371SAneesh Kumar K.V 	 * This ensures that not everyone will be able to create
292411a7b371SAneesh Kumar K.V 	 * handlink using the passed filedescriptor.
292511a7b371SAneesh Kumar K.V 	 */
292611a7b371SAneesh Kumar K.V 	if (flags & AT_EMPTY_PATH) {
292711a7b371SAneesh Kumar K.V 		if (!capable(CAP_DAC_READ_SEARCH))
292811a7b371SAneesh Kumar K.V 			return -ENOENT;
292911a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
293011a7b371SAneesh Kumar K.V 	}
2931c04030e1SUlrich Drepper 
293211a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
293311a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
293411a7b371SAneesh Kumar K.V 
293511a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
29361da177e4SLinus Torvalds 	if (error)
29372ad94ae6SAl Viro 		return error;
29382ad94ae6SAl Viro 
29392ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
29401da177e4SLinus Torvalds 	if (error)
29411da177e4SLinus Torvalds 		goto out;
29421da177e4SLinus Torvalds 	error = -EXDEV;
29432d8f3038SAl Viro 	if (old_path.mnt != nd.path.mnt)
29441da177e4SLinus Torvalds 		goto out_release;
29451da177e4SLinus Torvalds 	new_dentry = lookup_create(&nd, 0);
29461da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
29476902d925SDave Hansen 	if (IS_ERR(new_dentry))
29486902d925SDave Hansen 		goto out_unlock;
294975c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
295075c3f29dSDave Hansen 	if (error)
295175c3f29dSDave Hansen 		goto out_dput;
2952be6d3e56SKentaro Takeda 	error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2953be6d3e56SKentaro Takeda 	if (error)
2954be6d3e56SKentaro Takeda 		goto out_drop_write;
29552d8f3038SAl Viro 	error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
2956be6d3e56SKentaro Takeda out_drop_write:
295775c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
295875c3f29dSDave Hansen out_dput:
29591da177e4SLinus Torvalds 	dput(new_dentry);
29606902d925SDave Hansen out_unlock:
29614ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
29621da177e4SLinus Torvalds out_release:
29631d957f9bSJan Blunck 	path_put(&nd.path);
29642ad94ae6SAl Viro 	putname(to);
29651da177e4SLinus Torvalds out:
29662d8f3038SAl Viro 	path_put(&old_path);
29671da177e4SLinus Torvalds 
29681da177e4SLinus Torvalds 	return error;
29691da177e4SLinus Torvalds }
29701da177e4SLinus Torvalds 
29713480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
29725590ff0dSUlrich Drepper {
2973c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
29745590ff0dSUlrich Drepper }
29755590ff0dSUlrich Drepper 
29761da177e4SLinus Torvalds /*
29771da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
29781da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
29791da177e4SLinus Torvalds  * Problems:
29801da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
29811da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
29821da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
2983a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
29841da177e4SLinus Torvalds  *	   story.
29851da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
29861b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
29871da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
29881da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
2989a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
29901da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
29911da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
29921da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
2993a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
29941da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
29951da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
29961da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
29971da177e4SLinus Torvalds  *	d) some filesystems don't support opened-but-unlinked directories,
29981da177e4SLinus Torvalds  *	   either because of layout or because they are not ready to deal with
29991da177e4SLinus Torvalds  *	   all cases correctly. The latter will be fixed (taking this sort of
30001da177e4SLinus Torvalds  *	   stuff into VFS), but the former is not going away. Solution: the same
30011da177e4SLinus Torvalds  *	   trick as in rmdir().
30021da177e4SLinus Torvalds  *	e) conversion from fhandle to dentry may come in the wrong moment - when
30031b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
30041da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3005c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
30061da177e4SLinus Torvalds  *	   locking].
30071da177e4SLinus Torvalds  */
300875c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
30091da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
30101da177e4SLinus Torvalds {
30111da177e4SLinus Torvalds 	int error = 0;
30121da177e4SLinus Torvalds 	struct inode *target;
30131da177e4SLinus Torvalds 
30141da177e4SLinus Torvalds 	/*
30151da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
30161da177e4SLinus Torvalds 	 * we'll need to flip '..'.
30171da177e4SLinus Torvalds 	 */
30181da177e4SLinus Torvalds 	if (new_dir != old_dir) {
3019f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
30201da177e4SLinus Torvalds 		if (error)
30211da177e4SLinus Torvalds 			return error;
30221da177e4SLinus Torvalds 	}
30231da177e4SLinus Torvalds 
30241da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30251da177e4SLinus Torvalds 	if (error)
30261da177e4SLinus Torvalds 		return error;
30271da177e4SLinus Torvalds 
30281da177e4SLinus Torvalds 	target = new_dentry->d_inode;
3029d83c49f3SAl Viro 	if (target)
30301b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
30311da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
30321da177e4SLinus Torvalds 		error = -EBUSY;
3033d83c49f3SAl Viro 	else {
3034d83c49f3SAl Viro 		if (target)
3035d83c49f3SAl Viro 			dentry_unhash(new_dentry);
30361da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3037d83c49f3SAl Viro 	}
30381da177e4SLinus Torvalds 	if (target) {
3039d83c49f3SAl Viro 		if (!error) {
30401da177e4SLinus Torvalds 			target->i_flags |= S_DEAD;
3041d83c49f3SAl Viro 			dont_mount(new_dentry);
3042d83c49f3SAl Viro 		}
30431b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
30441da177e4SLinus Torvalds 		if (d_unhashed(new_dentry))
30451da177e4SLinus Torvalds 			d_rehash(new_dentry);
30461da177e4SLinus Torvalds 		dput(new_dentry);
30471da177e4SLinus Torvalds 	}
3048e31e14ecSStephen Smalley 	if (!error)
3049349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30501da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
30511da177e4SLinus Torvalds 	return error;
30521da177e4SLinus Torvalds }
30531da177e4SLinus Torvalds 
305475c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
30551da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
30561da177e4SLinus Torvalds {
30571da177e4SLinus Torvalds 	struct inode *target;
30581da177e4SLinus Torvalds 	int error;
30591da177e4SLinus Torvalds 
30601da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30611da177e4SLinus Torvalds 	if (error)
30621da177e4SLinus Torvalds 		return error;
30631da177e4SLinus Torvalds 
30641da177e4SLinus Torvalds 	dget(new_dentry);
30651da177e4SLinus Torvalds 	target = new_dentry->d_inode;
30661da177e4SLinus Torvalds 	if (target)
30671b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
30681da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
30691da177e4SLinus Torvalds 		error = -EBUSY;
30701da177e4SLinus Torvalds 	else
30711da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
30721da177e4SLinus Torvalds 	if (!error) {
3073bec1052eSAl Viro 		if (target)
3074d83c49f3SAl Viro 			dont_mount(new_dentry);
3075349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30761da177e4SLinus Torvalds 			d_move(old_dentry, new_dentry);
30771da177e4SLinus Torvalds 	}
30781da177e4SLinus Torvalds 	if (target)
30791b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
30801da177e4SLinus Torvalds 	dput(new_dentry);
30811da177e4SLinus Torvalds 	return error;
30821da177e4SLinus Torvalds }
30831da177e4SLinus Torvalds 
30841da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
30851da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
30861da177e4SLinus Torvalds {
30871da177e4SLinus Torvalds 	int error;
30881da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
308959b0df21SEric Paris 	const unsigned char *old_name;
30901da177e4SLinus Torvalds 
30911da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
30921da177e4SLinus Torvalds  		return 0;
30931da177e4SLinus Torvalds 
30941da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
30951da177e4SLinus Torvalds 	if (error)
30961da177e4SLinus Torvalds 		return error;
30971da177e4SLinus Torvalds 
30981da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3099a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
31001da177e4SLinus Torvalds 	else
31011da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
31021da177e4SLinus Torvalds 	if (error)
31031da177e4SLinus Torvalds 		return error;
31041da177e4SLinus Torvalds 
3105acfa4380SAl Viro 	if (!old_dir->i_op->rename)
31061da177e4SLinus Torvalds 		return -EPERM;
31071da177e4SLinus Torvalds 
31080eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
31090eeca283SRobert Love 
31101da177e4SLinus Torvalds 	if (is_dir)
31111da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
31121da177e4SLinus Torvalds 	else
31131da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3114123df294SAl Viro 	if (!error)
3115123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
31165a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
31170eeca283SRobert Love 	fsnotify_oldname_free(old_name);
31180eeca283SRobert Love 
31191da177e4SLinus Torvalds 	return error;
31201da177e4SLinus Torvalds }
31211da177e4SLinus Torvalds 
31222e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
31232e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
31241da177e4SLinus Torvalds {
31251da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
31261da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
31271da177e4SLinus Torvalds 	struct dentry *trap;
31281da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
31292ad94ae6SAl Viro 	char *from;
31302ad94ae6SAl Viro 	char *to;
31312ad94ae6SAl Viro 	int error;
31321da177e4SLinus Torvalds 
31332ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
31341da177e4SLinus Torvalds 	if (error)
31351da177e4SLinus Torvalds 		goto exit;
31361da177e4SLinus Torvalds 
31372ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
31381da177e4SLinus Torvalds 	if (error)
31391da177e4SLinus Torvalds 		goto exit1;
31401da177e4SLinus Torvalds 
31411da177e4SLinus Torvalds 	error = -EXDEV;
31424ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
31431da177e4SLinus Torvalds 		goto exit2;
31441da177e4SLinus Torvalds 
31454ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
31461da177e4SLinus Torvalds 	error = -EBUSY;
31471da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
31481da177e4SLinus Torvalds 		goto exit2;
31491da177e4SLinus Torvalds 
31504ac91378SJan Blunck 	new_dir = newnd.path.dentry;
31511da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
31521da177e4SLinus Torvalds 		goto exit2;
31531da177e4SLinus Torvalds 
31540612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
31550612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
31564e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
31570612d9fbSOGAWA Hirofumi 
31581da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
31591da177e4SLinus Torvalds 
316049705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
31611da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
31621da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
31631da177e4SLinus Torvalds 		goto exit3;
31641da177e4SLinus Torvalds 	/* source must exist */
31651da177e4SLinus Torvalds 	error = -ENOENT;
31661da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
31671da177e4SLinus Torvalds 		goto exit4;
31681da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
31691da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
31701da177e4SLinus Torvalds 		error = -ENOTDIR;
31711da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
31721da177e4SLinus Torvalds 			goto exit4;
31731da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
31741da177e4SLinus Torvalds 			goto exit4;
31751da177e4SLinus Torvalds 	}
31761da177e4SLinus Torvalds 	/* source should not be ancestor of target */
31771da177e4SLinus Torvalds 	error = -EINVAL;
31781da177e4SLinus Torvalds 	if (old_dentry == trap)
31791da177e4SLinus Torvalds 		goto exit4;
318049705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
31811da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
31821da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
31831da177e4SLinus Torvalds 		goto exit4;
31841da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
31851da177e4SLinus Torvalds 	error = -ENOTEMPTY;
31861da177e4SLinus Torvalds 	if (new_dentry == trap)
31871da177e4SLinus Torvalds 		goto exit5;
31881da177e4SLinus Torvalds 
31899079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
31909079b1ebSDave Hansen 	if (error)
31919079b1ebSDave Hansen 		goto exit5;
3192be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3193be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3194be6d3e56SKentaro Takeda 	if (error)
3195be6d3e56SKentaro Takeda 		goto exit6;
31961da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
31971da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3198be6d3e56SKentaro Takeda exit6:
31999079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
32001da177e4SLinus Torvalds exit5:
32011da177e4SLinus Torvalds 	dput(new_dentry);
32021da177e4SLinus Torvalds exit4:
32031da177e4SLinus Torvalds 	dput(old_dentry);
32041da177e4SLinus Torvalds exit3:
32051da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
32061da177e4SLinus Torvalds exit2:
32071d957f9bSJan Blunck 	path_put(&newnd.path);
32082ad94ae6SAl Viro 	putname(to);
32091da177e4SLinus Torvalds exit1:
32101d957f9bSJan Blunck 	path_put(&oldnd.path);
32111da177e4SLinus Torvalds 	putname(from);
32122ad94ae6SAl Viro exit:
32131da177e4SLinus Torvalds 	return error;
32141da177e4SLinus Torvalds }
32151da177e4SLinus Torvalds 
3216a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
32175590ff0dSUlrich Drepper {
32185590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
32195590ff0dSUlrich Drepper }
32205590ff0dSUlrich Drepper 
32211da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
32221da177e4SLinus Torvalds {
32231da177e4SLinus Torvalds 	int len;
32241da177e4SLinus Torvalds 
32251da177e4SLinus Torvalds 	len = PTR_ERR(link);
32261da177e4SLinus Torvalds 	if (IS_ERR(link))
32271da177e4SLinus Torvalds 		goto out;
32281da177e4SLinus Torvalds 
32291da177e4SLinus Torvalds 	len = strlen(link);
32301da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
32311da177e4SLinus Torvalds 		len = buflen;
32321da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
32331da177e4SLinus Torvalds 		len = -EFAULT;
32341da177e4SLinus Torvalds out:
32351da177e4SLinus Torvalds 	return len;
32361da177e4SLinus Torvalds }
32371da177e4SLinus Torvalds 
32381da177e4SLinus Torvalds /*
32391da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
32401da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
32411da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
32421da177e4SLinus Torvalds  */
32431da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
32441da177e4SLinus Torvalds {
32451da177e4SLinus Torvalds 	struct nameidata nd;
3246cc314eefSLinus Torvalds 	void *cookie;
3247694a1764SMarcin Slusarz 	int res;
3248cc314eefSLinus Torvalds 
32491da177e4SLinus Torvalds 	nd.depth = 0;
3250cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3251694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3252694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3253694a1764SMarcin Slusarz 
3254694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
32551da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3256cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3257694a1764SMarcin Slusarz 	return res;
32581da177e4SLinus Torvalds }
32591da177e4SLinus Torvalds 
32601da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
32611da177e4SLinus Torvalds {
32621da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
32631da177e4SLinus Torvalds }
32641da177e4SLinus Torvalds 
32651da177e4SLinus Torvalds /* get the link contents into pagecache */
32661da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
32671da177e4SLinus Torvalds {
3268ebd09abbSDuane Griffin 	char *kaddr;
32691da177e4SLinus Torvalds 	struct page *page;
32701da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3271090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
32721da177e4SLinus Torvalds 	if (IS_ERR(page))
32736fe6900eSNick Piggin 		return (char*)page;
32741da177e4SLinus Torvalds 	*ppage = page;
3275ebd09abbSDuane Griffin 	kaddr = kmap(page);
3276ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3277ebd09abbSDuane Griffin 	return kaddr;
32781da177e4SLinus Torvalds }
32791da177e4SLinus Torvalds 
32801da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
32811da177e4SLinus Torvalds {
32821da177e4SLinus Torvalds 	struct page *page = NULL;
32831da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
32841da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
32851da177e4SLinus Torvalds 	if (page) {
32861da177e4SLinus Torvalds 		kunmap(page);
32871da177e4SLinus Torvalds 		page_cache_release(page);
32881da177e4SLinus Torvalds 	}
32891da177e4SLinus Torvalds 	return res;
32901da177e4SLinus Torvalds }
32911da177e4SLinus Torvalds 
3292cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
32931da177e4SLinus Torvalds {
3294cc314eefSLinus Torvalds 	struct page *page = NULL;
32951da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3296cc314eefSLinus Torvalds 	return page;
32971da177e4SLinus Torvalds }
32981da177e4SLinus Torvalds 
3299cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
33001da177e4SLinus Torvalds {
3301cc314eefSLinus Torvalds 	struct page *page = cookie;
3302cc314eefSLinus Torvalds 
3303cc314eefSLinus Torvalds 	if (page) {
33041da177e4SLinus Torvalds 		kunmap(page);
33051da177e4SLinus Torvalds 		page_cache_release(page);
33061da177e4SLinus Torvalds 	}
33071da177e4SLinus Torvalds }
33081da177e4SLinus Torvalds 
330954566b2cSNick Piggin /*
331054566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
331154566b2cSNick Piggin  */
331254566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
33131da177e4SLinus Torvalds {
33141da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
33150adb25d2SKirill Korotaev 	struct page *page;
3316afddba49SNick Piggin 	void *fsdata;
3317beb497abSDmitriy Monakhov 	int err;
33181da177e4SLinus Torvalds 	char *kaddr;
331954566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
332054566b2cSNick Piggin 	if (nofs)
332154566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
33221da177e4SLinus Torvalds 
33237e53cac4SNeilBrown retry:
3324afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
332554566b2cSNick Piggin 				flags, &page, &fsdata);
33261da177e4SLinus Torvalds 	if (err)
3327afddba49SNick Piggin 		goto fail;
3328afddba49SNick Piggin 
33291da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
33301da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
33311da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
3332afddba49SNick Piggin 
3333afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3334afddba49SNick Piggin 							page, fsdata);
33351da177e4SLinus Torvalds 	if (err < 0)
33361da177e4SLinus Torvalds 		goto fail;
3337afddba49SNick Piggin 	if (err < len-1)
3338afddba49SNick Piggin 		goto retry;
3339afddba49SNick Piggin 
33401da177e4SLinus Torvalds 	mark_inode_dirty(inode);
33411da177e4SLinus Torvalds 	return 0;
33421da177e4SLinus Torvalds fail:
33431da177e4SLinus Torvalds 	return err;
33441da177e4SLinus Torvalds }
33451da177e4SLinus Torvalds 
33460adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
33470adb25d2SKirill Korotaev {
33480adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
334954566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
33500adb25d2SKirill Korotaev }
33510adb25d2SKirill Korotaev 
335292e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
33531da177e4SLinus Torvalds 	.readlink	= generic_readlink,
33541da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
33551da177e4SLinus Torvalds 	.put_link	= page_put_link,
33561da177e4SLinus Torvalds };
33571da177e4SLinus Torvalds 
33582d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
3359cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
33601da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
33611da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
33621da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
33631da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
33641da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
33651da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
33661da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
33671da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
33681da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
33690adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
33701da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
33711da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
3372c9c6cac0SAl Viro EXPORT_SYMBOL(kern_path_parent);
3373d1811465SAl Viro EXPORT_SYMBOL(kern_path);
337416f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3375f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
33768c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission);
33771da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
33781da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
33791da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
33801da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
33811da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
33821da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
33831da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
33841da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
33851da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
33861da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
33871da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
33881da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
33891da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
33901da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3391