xref: /openbmc/linux/fs/namei.c (revision 50338b88)
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 {
18226cf46beSLinus Torvalds 	unsigned int 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 /**
472834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
473834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
474834f2a4aSTrond Myklebust  */
475834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
476834f2a4aSTrond Myklebust {
4772dab5974SLinus Torvalds 	struct file *file = nd->intent.open.file;
4782dab5974SLinus Torvalds 
4792dab5974SLinus Torvalds 	if (file && !IS_ERR(file)) {
4802dab5974SLinus Torvalds 		if (file->f_path.dentry == NULL)
4812dab5974SLinus Torvalds 			put_filp(file);
482834f2a4aSTrond Myklebust 		else
4832dab5974SLinus Torvalds 			fput(file);
4842dab5974SLinus Torvalds 	}
485834f2a4aSTrond Myklebust }
486834f2a4aSTrond Myklebust 
487f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
48834286d66SNick Piggin {
489f60aef7eSAl Viro 	return dentry->d_op->d_revalidate(dentry, nd);
49034286d66SNick Piggin }
49134286d66SNick Piggin 
492f5e1c1c1SAl Viro static struct dentry *
493bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd)
494bcdc5e01SIan Kent {
495f5e1c1c1SAl Viro 	int status = d_revalidate(dentry, nd);
496bcdc5e01SIan Kent 	if (unlikely(status <= 0)) {
497bcdc5e01SIan Kent 		/*
498bcdc5e01SIan Kent 		 * The dentry failed validation.
499bcdc5e01SIan Kent 		 * If d_revalidate returned 0 attempt to invalidate
500bcdc5e01SIan Kent 		 * the dentry otherwise d_revalidate is asking us
501bcdc5e01SIan Kent 		 * to return a fail status.
502bcdc5e01SIan Kent 		 */
50334286d66SNick Piggin 		if (status < 0) {
50434286d66SNick Piggin 			dput(dentry);
50534286d66SNick Piggin 			dentry = ERR_PTR(status);
506f5e1c1c1SAl Viro 		} else if (!d_invalidate(dentry)) {
507bcdc5e01SIan Kent 			dput(dentry);
508bcdc5e01SIan Kent 			dentry = NULL;
509bcdc5e01SIan Kent 		}
510bcdc5e01SIan Kent 	}
511f5e1c1c1SAl Viro 	return dentry;
512f5e1c1c1SAl Viro }
513f5e1c1c1SAl Viro 
5149f1fafeeSAl Viro /**
5159f1fafeeSAl Viro  * complete_walk - successful completion of path walk
5169f1fafeeSAl Viro  * @nd:  pointer nameidata
51739159de2SJeff Layton  *
5189f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
5199f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
5209f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
5219f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
5229f1fafeeSAl Viro  * need to drop nd->path.
52339159de2SJeff Layton  */
5249f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
52539159de2SJeff Layton {
52616c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
52739159de2SJeff Layton 	int status;
52839159de2SJeff Layton 
5299f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
5309f1fafeeSAl Viro 		nd->flags &= ~LOOKUP_RCU;
5319f1fafeeSAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
5329f1fafeeSAl Viro 			nd->root.mnt = NULL;
5339f1fafeeSAl Viro 		spin_lock(&dentry->d_lock);
5349f1fafeeSAl Viro 		if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
5359f1fafeeSAl Viro 			spin_unlock(&dentry->d_lock);
5369f1fafeeSAl Viro 			rcu_read_unlock();
5379f1fafeeSAl Viro 			br_read_unlock(vfsmount_lock);
5389f1fafeeSAl Viro 			return -ECHILD;
5399f1fafeeSAl Viro 		}
5409f1fafeeSAl Viro 		BUG_ON(nd->inode != dentry->d_inode);
5419f1fafeeSAl Viro 		spin_unlock(&dentry->d_lock);
5429f1fafeeSAl Viro 		mntget(nd->path.mnt);
5439f1fafeeSAl Viro 		rcu_read_unlock();
5449f1fafeeSAl Viro 		br_read_unlock(vfsmount_lock);
5459f1fafeeSAl Viro 	}
5469f1fafeeSAl Viro 
54716c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
54839159de2SJeff Layton 		return 0;
54939159de2SJeff Layton 
55016c2cd71SAl Viro 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
55116c2cd71SAl Viro 		return 0;
55216c2cd71SAl Viro 
55316c2cd71SAl Viro 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
55416c2cd71SAl Viro 		return 0;
55516c2cd71SAl Viro 
55616c2cd71SAl Viro 	/* Note: we do not d_invalidate() */
55734286d66SNick Piggin 	status = d_revalidate(dentry, nd);
55839159de2SJeff Layton 	if (status > 0)
55939159de2SJeff Layton 		return 0;
56039159de2SJeff Layton 
56116c2cd71SAl Viro 	if (!status)
56239159de2SJeff Layton 		status = -ESTALE;
56316c2cd71SAl Viro 
5649f1fafeeSAl Viro 	path_put(&nd->path);
56539159de2SJeff Layton 	return status;
56639159de2SJeff Layton }
56739159de2SJeff Layton 
56839159de2SJeff Layton /*
569b75b5086SAl Viro  * Short-cut version of permission(), for calling on directories
570b75b5086SAl Viro  * during pathname resolution.  Combines parts of permission()
571b75b5086SAl Viro  * and generic_permission(), and tests ONLY for MAY_EXEC permission.
5721da177e4SLinus Torvalds  *
5731da177e4SLinus Torvalds  * If appropriate, check DAC only.  If not appropriate, or
574b75b5086SAl Viro  * short-cut DAC fails, then call ->permission() to do more
5751da177e4SLinus Torvalds  * complete permission check.
5761da177e4SLinus Torvalds  */
577b74c79e9SNick Piggin static inline int exec_permission(struct inode *inode, unsigned int flags)
5781da177e4SLinus Torvalds {
5795909ccaaSLinus Torvalds 	int ret;
580e795b717SSerge E. Hallyn 	struct user_namespace *ns = inode_userns(inode);
5811da177e4SLinus Torvalds 
582cb9179eaSLinus Torvalds 	if (inode->i_op->permission) {
583b74c79e9SNick Piggin 		ret = inode->i_op->permission(inode, MAY_EXEC, flags);
584b74c79e9SNick Piggin 	} else {
585b74c79e9SNick Piggin 		ret = acl_permission_check(inode, MAY_EXEC, flags,
586b74c79e9SNick Piggin 				inode->i_op->check_acl);
587cb9179eaSLinus Torvalds 	}
588b74c79e9SNick Piggin 	if (likely(!ret))
5891da177e4SLinus Torvalds 		goto ok;
590b74c79e9SNick Piggin 	if (ret == -ECHILD)
59131e6b01fSNick Piggin 		return ret;
5921da177e4SLinus Torvalds 
593e795b717SSerge E. Hallyn 	if (ns_capable(ns, CAP_DAC_OVERRIDE) ||
594e795b717SSerge E. Hallyn 			ns_capable(ns, CAP_DAC_READ_SEARCH))
5951da177e4SLinus Torvalds 		goto ok;
5961da177e4SLinus Torvalds 
5975909ccaaSLinus Torvalds 	return ret;
5981da177e4SLinus Torvalds ok:
599b74c79e9SNick Piggin 	return security_inode_exec_permission(inode, flags);
6001da177e4SLinus Torvalds }
6011da177e4SLinus Torvalds 
6022a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
6032a737871SAl Viro {
604f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
605f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
6062a737871SAl Viro }
6072a737871SAl Viro 
6086de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
6096de88d72SAl Viro 
61031e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
61131e6b01fSNick Piggin {
61231e6b01fSNick Piggin 	if (!nd->root.mnt) {
61331e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
614c28cc364SNick Piggin 		unsigned seq;
615c28cc364SNick Piggin 
616c28cc364SNick Piggin 		do {
617c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
61831e6b01fSNick Piggin 			nd->root = fs->root;
619c1530019STim Chen 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
620c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
62131e6b01fSNick Piggin 	}
62231e6b01fSNick Piggin }
62331e6b01fSNick Piggin 
624f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
6251da177e4SLinus Torvalds {
62631e6b01fSNick Piggin 	int ret;
62731e6b01fSNick Piggin 
6281da177e4SLinus Torvalds 	if (IS_ERR(link))
6291da177e4SLinus Torvalds 		goto fail;
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	if (*link == '/') {
6322a737871SAl Viro 		set_root(nd);
6331d957f9bSJan Blunck 		path_put(&nd->path);
6342a737871SAl Viro 		nd->path = nd->root;
6352a737871SAl Viro 		path_get(&nd->root);
63616c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
6371da177e4SLinus Torvalds 	}
63831e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
639b4091d5fSChristoph Hellwig 
64031e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
64131e6b01fSNick Piggin 	return ret;
6421da177e4SLinus Torvalds fail:
6431d957f9bSJan Blunck 	path_put(&nd->path);
6441da177e4SLinus Torvalds 	return PTR_ERR(link);
6451da177e4SLinus Torvalds }
6461da177e4SLinus Torvalds 
6471d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
648051d3812SIan Kent {
649051d3812SIan Kent 	dput(path->dentry);
6504ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
651051d3812SIan Kent 		mntput(path->mnt);
652051d3812SIan Kent }
653051d3812SIan Kent 
6547b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
6557b9337aaSNick Piggin 					struct nameidata *nd)
656051d3812SIan Kent {
65731e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
6584ac91378SJan Blunck 		dput(nd->path.dentry);
65931e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
6604ac91378SJan Blunck 			mntput(nd->path.mnt);
6619a229683SHuang Shijie 	}
66231e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
6634ac91378SJan Blunck 	nd->path.dentry = path->dentry;
664051d3812SIan Kent }
665051d3812SIan Kent 
666574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
667574197e0SAl Viro {
668574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
669574197e0SAl Viro 	if (!IS_ERR(cookie) && inode->i_op->put_link)
670574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
671574197e0SAl Viro 	path_put(link);
672574197e0SAl Viro }
673574197e0SAl Viro 
674def4af30SAl Viro static __always_inline int
675574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
6761da177e4SLinus Torvalds {
6771da177e4SLinus Torvalds 	int error;
6787b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
6791da177e4SLinus Torvalds 
680844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
681844a3917SAl Viro 
6820e794589SAl Viro 	if (link->mnt == nd->path.mnt)
6830e794589SAl Viro 		mntget(link->mnt);
6840e794589SAl Viro 
685574197e0SAl Viro 	if (unlikely(current->total_link_count >= 40)) {
686574197e0SAl Viro 		*p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
687574197e0SAl Viro 		path_put(&nd->path);
688574197e0SAl Viro 		return -ELOOP;
689574197e0SAl Viro 	}
690574197e0SAl Viro 	cond_resched();
691574197e0SAl Viro 	current->total_link_count++;
692574197e0SAl Viro 
6937b9337aaSNick Piggin 	touch_atime(link->mnt, dentry);
6941da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
695cd4e91d3SAl Viro 
69636f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
69736f3b4f6SAl Viro 	if (error) {
69836f3b4f6SAl Viro 		*p = ERR_PTR(error); /* no ->put_link(), please */
69936f3b4f6SAl Viro 		path_put(&nd->path);
70036f3b4f6SAl Viro 		return error;
70136f3b4f6SAl Viro 	}
70236f3b4f6SAl Viro 
70386acdca1SAl Viro 	nd->last_type = LAST_BIND;
704def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
705def4af30SAl Viro 	error = PTR_ERR(*p);
706def4af30SAl Viro 	if (!IS_ERR(*p)) {
7071da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
708cc314eefSLinus Torvalds 		error = 0;
7091da177e4SLinus Torvalds 		if (s)
7101da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
711bcda7652SAl Viro 		else if (nd->last_type == LAST_BIND) {
71216c2cd71SAl Viro 			nd->flags |= LOOKUP_JUMPED;
713b21041d0SAl Viro 			nd->inode = nd->path.dentry->d_inode;
714b21041d0SAl Viro 			if (nd->inode->i_op->follow_link) {
715bcda7652SAl Viro 				/* stepped on a _really_ weird one */
716bcda7652SAl Viro 				path_put(&nd->path);
717bcda7652SAl Viro 				error = -ELOOP;
718bcda7652SAl Viro 			}
719bcda7652SAl Viro 		}
7201da177e4SLinus Torvalds 	}
7211da177e4SLinus Torvalds 	return error;
7221da177e4SLinus Torvalds }
7231da177e4SLinus Torvalds 
72431e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
72531e6b01fSNick Piggin {
72631e6b01fSNick Piggin 	struct vfsmount *parent;
72731e6b01fSNick Piggin 	struct dentry *mountpoint;
72831e6b01fSNick Piggin 
72931e6b01fSNick Piggin 	parent = path->mnt->mnt_parent;
73031e6b01fSNick Piggin 	if (parent == path->mnt)
73131e6b01fSNick Piggin 		return 0;
73231e6b01fSNick Piggin 	mountpoint = path->mnt->mnt_mountpoint;
73331e6b01fSNick Piggin 	path->dentry = mountpoint;
73431e6b01fSNick Piggin 	path->mnt = parent;
73531e6b01fSNick Piggin 	return 1;
73631e6b01fSNick Piggin }
73731e6b01fSNick Piggin 
738bab77ebfSAl Viro int follow_up(struct path *path)
7391da177e4SLinus Torvalds {
7401da177e4SLinus Torvalds 	struct vfsmount *parent;
7411da177e4SLinus Torvalds 	struct dentry *mountpoint;
74299b7db7bSNick Piggin 
74399b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
744bab77ebfSAl Viro 	parent = path->mnt->mnt_parent;
745bab77ebfSAl Viro 	if (parent == path->mnt) {
74699b7db7bSNick Piggin 		br_read_unlock(vfsmount_lock);
7471da177e4SLinus Torvalds 		return 0;
7481da177e4SLinus Torvalds 	}
7491da177e4SLinus Torvalds 	mntget(parent);
750bab77ebfSAl Viro 	mountpoint = dget(path->mnt->mnt_mountpoint);
75199b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
752bab77ebfSAl Viro 	dput(path->dentry);
753bab77ebfSAl Viro 	path->dentry = mountpoint;
754bab77ebfSAl Viro 	mntput(path->mnt);
755bab77ebfSAl Viro 	path->mnt = parent;
7561da177e4SLinus Torvalds 	return 1;
7571da177e4SLinus Torvalds }
7581da177e4SLinus Torvalds 
759b5c84bf6SNick Piggin /*
7609875cf80SDavid Howells  * Perform an automount
7619875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
7629875cf80SDavid Howells  *   were called with.
7631da177e4SLinus Torvalds  */
7649875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
7659875cf80SDavid Howells 			    bool *need_mntput)
76631e6b01fSNick Piggin {
7679875cf80SDavid Howells 	struct vfsmount *mnt;
768ea5b778aSDavid Howells 	int err;
7699875cf80SDavid Howells 
7709875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
7719875cf80SDavid Howells 		return -EREMOTE;
7729875cf80SDavid Howells 
7736f45b656SDavid Howells 	/* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
7746f45b656SDavid Howells 	 * and this is the terminal part of the path.
7756f45b656SDavid Howells 	 */
7766f45b656SDavid Howells 	if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
7776f45b656SDavid Howells 		return -EISDIR; /* we actually want to stop here */
7786f45b656SDavid Howells 
7799875cf80SDavid Howells 	/* We want to mount if someone is trying to open/create a file of any
7809875cf80SDavid Howells 	 * type under the mountpoint, wants to traverse through the mountpoint
7819875cf80SDavid Howells 	 * or wants to open the mounted directory.
7829875cf80SDavid Howells 	 *
7839875cf80SDavid Howells 	 * We don't want to mount if someone's just doing a stat and they've
7849875cf80SDavid Howells 	 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
7859875cf80SDavid Howells 	 * appended a '/' to the name.
7869875cf80SDavid Howells 	 */
7879875cf80SDavid Howells 	if (!(flags & LOOKUP_FOLLOW) &&
7889875cf80SDavid Howells 	    !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
7899875cf80SDavid Howells 		       LOOKUP_OPEN | LOOKUP_CREATE)))
7909875cf80SDavid Howells 		return -EISDIR;
7919875cf80SDavid Howells 
7929875cf80SDavid Howells 	current->total_link_count++;
7939875cf80SDavid Howells 	if (current->total_link_count >= 40)
7949875cf80SDavid Howells 		return -ELOOP;
7959875cf80SDavid Howells 
7969875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
7979875cf80SDavid Howells 	if (IS_ERR(mnt)) {
7989875cf80SDavid Howells 		/*
7999875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
8009875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
8019875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
8029875cf80SDavid Howells 		 *
8039875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
8049875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
8059875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
8069875cf80SDavid Howells 		 */
8079875cf80SDavid Howells 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
8089875cf80SDavid Howells 			return -EREMOTE;
8099875cf80SDavid Howells 		return PTR_ERR(mnt);
81031e6b01fSNick Piggin 	}
811ea5b778aSDavid Howells 
8129875cf80SDavid Howells 	if (!mnt) /* mount collision */
8139875cf80SDavid Howells 		return 0;
8149875cf80SDavid Howells 
81519a167afSAl Viro 	err = finish_automount(mnt, path);
816ea5b778aSDavid Howells 
817ea5b778aSDavid Howells 	switch (err) {
818ea5b778aSDavid Howells 	case -EBUSY:
819ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
82019a167afSAl Viro 		return 0;
821ea5b778aSDavid Howells 	case 0:
822463ffb2eSAl Viro 		dput(path->dentry);
8239875cf80SDavid Howells 		if (*need_mntput)
8249875cf80SDavid Howells 			mntput(path->mnt);
8259875cf80SDavid Howells 		path->mnt = mnt;
8269875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
8279875cf80SDavid Howells 		*need_mntput = true;
8289875cf80SDavid Howells 		return 0;
82919a167afSAl Viro 	default:
83019a167afSAl Viro 		return err;
8319875cf80SDavid Howells 	}
83219a167afSAl Viro 
833ea5b778aSDavid Howells }
8349875cf80SDavid Howells 
8359875cf80SDavid Howells /*
8369875cf80SDavid Howells  * Handle a dentry that is managed in some way.
837cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
8389875cf80SDavid Howells  * - Flagged as mountpoint
8399875cf80SDavid Howells  * - Flagged as automount point
8409875cf80SDavid Howells  *
8419875cf80SDavid Howells  * This may only be called in refwalk mode.
8429875cf80SDavid Howells  *
8439875cf80SDavid Howells  * Serialization is taken care of in namespace.c
8449875cf80SDavid Howells  */
8459875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
8469875cf80SDavid Howells {
8479875cf80SDavid Howells 	unsigned managed;
8489875cf80SDavid Howells 	bool need_mntput = false;
8499875cf80SDavid Howells 	int ret;
8509875cf80SDavid Howells 
8519875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
8529875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
8539875cf80SDavid Howells 	 * the components of that value change under us */
8549875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
8559875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
8569875cf80SDavid Howells 	       unlikely(managed != 0)) {
857cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
858cc53ce53SDavid Howells 		 * being held. */
859cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
860cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
861cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
8621aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
863cc53ce53SDavid Howells 			if (ret < 0)
864cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
865cc53ce53SDavid Howells 		}
866cc53ce53SDavid Howells 
8679875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
8689875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
8699875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
8709875cf80SDavid Howells 			if (mounted) {
8719875cf80SDavid Howells 				dput(path->dentry);
8729875cf80SDavid Howells 				if (need_mntput)
873463ffb2eSAl Viro 					mntput(path->mnt);
874463ffb2eSAl Viro 				path->mnt = mounted;
875463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
8769875cf80SDavid Howells 				need_mntput = true;
8779875cf80SDavid Howells 				continue;
878463ffb2eSAl Viro 			}
879463ffb2eSAl Viro 
8809875cf80SDavid Howells 			/* Something is mounted on this dentry in another
8819875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
8829875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
8839875cf80SDavid Howells 			 * vfsmount_lock */
8841da177e4SLinus Torvalds 		}
8859875cf80SDavid Howells 
8869875cf80SDavid Howells 		/* Handle an automount point */
8879875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
8889875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
8899875cf80SDavid Howells 			if (ret < 0)
8909875cf80SDavid Howells 				return ret == -EISDIR ? 0 : ret;
8919875cf80SDavid Howells 			continue;
8929875cf80SDavid Howells 		}
8939875cf80SDavid Howells 
8949875cf80SDavid Howells 		/* We didn't change the current path point */
8959875cf80SDavid Howells 		break;
8969875cf80SDavid Howells 	}
8979875cf80SDavid Howells 	return 0;
8981da177e4SLinus Torvalds }
8991da177e4SLinus Torvalds 
900cc53ce53SDavid Howells int follow_down_one(struct path *path)
9011da177e4SLinus Torvalds {
9021da177e4SLinus Torvalds 	struct vfsmount *mounted;
9031da177e4SLinus Torvalds 
9041c755af4SAl Viro 	mounted = lookup_mnt(path);
9051da177e4SLinus Torvalds 	if (mounted) {
9069393bd07SAl Viro 		dput(path->dentry);
9079393bd07SAl Viro 		mntput(path->mnt);
9089393bd07SAl Viro 		path->mnt = mounted;
9099393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
9101da177e4SLinus Torvalds 		return 1;
9111da177e4SLinus Torvalds 	}
9121da177e4SLinus Torvalds 	return 0;
9131da177e4SLinus Torvalds }
9141da177e4SLinus Torvalds 
91562a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
91662a7375eSIan Kent {
91762a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
91862a7375eSIan Kent 		dentry->d_op->d_manage(dentry, true) < 0);
91962a7375eSIan Kent }
92062a7375eSIan Kent 
9219875cf80SDavid Howells /*
922287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
923287548e4SAl Viro  * we meet a managed dentry that would need blocking.
9249875cf80SDavid Howells  */
9259875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
926287548e4SAl Viro 			       struct inode **inode)
9279875cf80SDavid Howells {
92862a7375eSIan Kent 	for (;;) {
9299875cf80SDavid Howells 		struct vfsmount *mounted;
93062a7375eSIan Kent 		/*
93162a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
93262a7375eSIan Kent 		 * that wants to block transit.
93362a7375eSIan Kent 		 */
93462a7375eSIan Kent 		*inode = path->dentry->d_inode;
935287548e4SAl Viro 		if (unlikely(managed_dentry_might_block(path->dentry)))
936ab90911fSDavid Howells 			return false;
93762a7375eSIan Kent 
93862a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
93962a7375eSIan Kent 			break;
94062a7375eSIan Kent 
9419875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
9429875cf80SDavid Howells 		if (!mounted)
9439875cf80SDavid Howells 			break;
9449875cf80SDavid Howells 		path->mnt = mounted;
9459875cf80SDavid Howells 		path->dentry = mounted->mnt_root;
9469875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
9479875cf80SDavid Howells 	}
9489875cf80SDavid Howells 	return true;
9499875cf80SDavid Howells }
9509875cf80SDavid Howells 
951dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd)
952287548e4SAl Viro {
953dea39376SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
954287548e4SAl Viro 		struct vfsmount *mounted;
955dea39376SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
956287548e4SAl Viro 		if (!mounted)
957287548e4SAl Viro 			break;
958dea39376SAl Viro 		nd->path.mnt = mounted;
959dea39376SAl Viro 		nd->path.dentry = mounted->mnt_root;
960dea39376SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
961287548e4SAl Viro 	}
962287548e4SAl Viro }
963287548e4SAl Viro 
96431e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
96531e6b01fSNick Piggin {
96631e6b01fSNick Piggin 	set_root_rcu(nd);
96731e6b01fSNick Piggin 
96831e6b01fSNick Piggin 	while (1) {
96931e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
97031e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
97131e6b01fSNick Piggin 			break;
97231e6b01fSNick Piggin 		}
97331e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
97431e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
97531e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
97631e6b01fSNick Piggin 			unsigned seq;
97731e6b01fSNick Piggin 
97831e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
97931e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
980ef7562d5SAl Viro 				goto failed;
98131e6b01fSNick Piggin 			nd->path.dentry = parent;
98231e6b01fSNick Piggin 			nd->seq = seq;
98331e6b01fSNick Piggin 			break;
98431e6b01fSNick Piggin 		}
98531e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
98631e6b01fSNick Piggin 			break;
98731e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
98831e6b01fSNick Piggin 	}
989dea39376SAl Viro 	follow_mount_rcu(nd);
990dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
99131e6b01fSNick Piggin 	return 0;
992ef7562d5SAl Viro 
993ef7562d5SAl Viro failed:
994ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
9955b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
996ef7562d5SAl Viro 		nd->root.mnt = NULL;
997ef7562d5SAl Viro 	rcu_read_unlock();
998ef7562d5SAl Viro 	br_read_unlock(vfsmount_lock);
999ef7562d5SAl Viro 	return -ECHILD;
100031e6b01fSNick Piggin }
100131e6b01fSNick Piggin 
10029875cf80SDavid Howells /*
1003cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
1004cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
1005cc53ce53SDavid Howells  * caller is permitted to proceed or not.
1006cc53ce53SDavid Howells  *
1007cc53ce53SDavid Howells  * Care must be taken as namespace_sem may be held (indicated by mounting_here
1008cc53ce53SDavid Howells  * being true).
1009cc53ce53SDavid Howells  */
10107cc90cc3SAl Viro int follow_down(struct path *path)
1011cc53ce53SDavid Howells {
1012cc53ce53SDavid Howells 	unsigned managed;
1013cc53ce53SDavid Howells 	int ret;
1014cc53ce53SDavid Howells 
1015cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
1016cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1017cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1018cc53ce53SDavid Howells 		 * being held.
1019cc53ce53SDavid Howells 		 *
1020cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
1021cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
1022cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
1023cc53ce53SDavid Howells 		 * superstructure.
1024cc53ce53SDavid Howells 		 *
1025cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
1026cc53ce53SDavid Howells 		 */
1027cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
1028cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1029cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
1030ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
10311aed3e42SAl Viro 				path->dentry, false);
1032cc53ce53SDavid Howells 			if (ret < 0)
1033cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
1034cc53ce53SDavid Howells 		}
1035cc53ce53SDavid Howells 
1036cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
1037cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
1038cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
1039cc53ce53SDavid Howells 			if (!mounted)
1040cc53ce53SDavid Howells 				break;
1041cc53ce53SDavid Howells 			dput(path->dentry);
1042cc53ce53SDavid Howells 			mntput(path->mnt);
1043cc53ce53SDavid Howells 			path->mnt = mounted;
1044cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1045cc53ce53SDavid Howells 			continue;
1046cc53ce53SDavid Howells 		}
1047cc53ce53SDavid Howells 
1048cc53ce53SDavid Howells 		/* Don't handle automount points here */
1049cc53ce53SDavid Howells 		break;
1050cc53ce53SDavid Howells 	}
1051cc53ce53SDavid Howells 	return 0;
1052cc53ce53SDavid Howells }
1053cc53ce53SDavid Howells 
1054cc53ce53SDavid Howells /*
10559875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
10569875cf80SDavid Howells  */
10579875cf80SDavid Howells static void follow_mount(struct path *path)
10589875cf80SDavid Howells {
10599875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
10609875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
10619875cf80SDavid Howells 		if (!mounted)
10629875cf80SDavid Howells 			break;
10639875cf80SDavid Howells 		dput(path->dentry);
10649875cf80SDavid Howells 		mntput(path->mnt);
10659875cf80SDavid Howells 		path->mnt = mounted;
10669875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
10679875cf80SDavid Howells 	}
10689875cf80SDavid Howells }
10699875cf80SDavid Howells 
107031e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
10711da177e4SLinus Torvalds {
10722a737871SAl Viro 	set_root(nd);
1073e518ddb7SAndreas Mohr 
10741da177e4SLinus Torvalds 	while(1) {
10754ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
10761da177e4SLinus Torvalds 
10772a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
10782a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
10791da177e4SLinus Torvalds 			break;
10801da177e4SLinus Torvalds 		}
10814ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
10823088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
10833088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
10841da177e4SLinus Torvalds 			dput(old);
10851da177e4SLinus Torvalds 			break;
10861da177e4SLinus Torvalds 		}
10873088dd70SAl Viro 		if (!follow_up(&nd->path))
10881da177e4SLinus Torvalds 			break;
10891da177e4SLinus Torvalds 	}
109079ed0226SAl Viro 	follow_mount(&nd->path);
109131e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
10921da177e4SLinus Torvalds }
10931da177e4SLinus Torvalds 
10941da177e4SLinus Torvalds /*
1095baa03890SNick Piggin  * Allocate a dentry with name and parent, and perform a parent
1096baa03890SNick Piggin  * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1097baa03890SNick Piggin  * on error. parent->d_inode->i_mutex must be held. d_lookup must
1098baa03890SNick Piggin  * have verified that no child exists while under i_mutex.
1099baa03890SNick Piggin  */
1100baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1101baa03890SNick Piggin 				struct qstr *name, struct nameidata *nd)
1102baa03890SNick Piggin {
1103baa03890SNick Piggin 	struct inode *inode = parent->d_inode;
1104baa03890SNick Piggin 	struct dentry *dentry;
1105baa03890SNick Piggin 	struct dentry *old;
1106baa03890SNick Piggin 
1107baa03890SNick Piggin 	/* Don't create child dentry for a dead directory. */
1108baa03890SNick Piggin 	if (unlikely(IS_DEADDIR(inode)))
1109baa03890SNick Piggin 		return ERR_PTR(-ENOENT);
1110baa03890SNick Piggin 
1111baa03890SNick Piggin 	dentry = d_alloc(parent, name);
1112baa03890SNick Piggin 	if (unlikely(!dentry))
1113baa03890SNick Piggin 		return ERR_PTR(-ENOMEM);
1114baa03890SNick Piggin 
1115baa03890SNick Piggin 	old = inode->i_op->lookup(inode, dentry, nd);
1116baa03890SNick Piggin 	if (unlikely(old)) {
1117baa03890SNick Piggin 		dput(dentry);
1118baa03890SNick Piggin 		dentry = old;
1119baa03890SNick Piggin 	}
1120baa03890SNick Piggin 	return dentry;
1121baa03890SNick Piggin }
1122baa03890SNick Piggin 
1123baa03890SNick Piggin /*
11241da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
11251da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
11261da177e4SLinus Torvalds  *  It _is_ time-critical.
11271da177e4SLinus Torvalds  */
11281da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
112931e6b01fSNick Piggin 			struct path *path, struct inode **inode)
11301da177e4SLinus Torvalds {
11314ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
113231e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
11335a18fff2SAl Viro 	int need_reval = 1;
11345a18fff2SAl Viro 	int status = 1;
11359875cf80SDavid Howells 	int err;
11369875cf80SDavid Howells 
11373cac260aSAl Viro 	/*
1138b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1139b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1140b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1141b04f784eSNick Piggin 	 */
114231e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
114331e6b01fSNick Piggin 		unsigned seq;
114431e6b01fSNick Piggin 		*inode = nd->inode;
114531e6b01fSNick Piggin 		dentry = __d_lookup_rcu(parent, name, &seq, inode);
11465a18fff2SAl Viro 		if (!dentry)
11475a18fff2SAl Viro 			goto unlazy;
11485a18fff2SAl Viro 
114931e6b01fSNick Piggin 		/* Memory barrier in read_seqcount_begin of child is enough */
115031e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
115131e6b01fSNick Piggin 			return -ECHILD;
115231e6b01fSNick Piggin 		nd->seq = seq;
11535a18fff2SAl Viro 
115424643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
11555a18fff2SAl Viro 			status = d_revalidate(dentry, nd);
11565a18fff2SAl Viro 			if (unlikely(status <= 0)) {
11575a18fff2SAl Viro 				if (status != -ECHILD)
11585a18fff2SAl Viro 					need_reval = 0;
11595a18fff2SAl Viro 				goto unlazy;
11605a18fff2SAl Viro 			}
116124643087SAl Viro 		}
116231e6b01fSNick Piggin 		path->mnt = mnt;
116331e6b01fSNick Piggin 		path->dentry = dentry;
1164d6e9bd25SAl Viro 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1165d6e9bd25SAl Viro 			goto unlazy;
1166d6e9bd25SAl Viro 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1167d6e9bd25SAl Viro 			goto unlazy;
11689875cf80SDavid Howells 		return 0;
11695a18fff2SAl Viro unlazy:
117019660af7SAl Viro 		if (unlazy_walk(nd, dentry))
11715a18fff2SAl Viro 			return -ECHILD;
11725a18fff2SAl Viro 	} else {
117331e6b01fSNick Piggin 		dentry = __d_lookup(parent, name);
117424643087SAl Viro 	}
11755a18fff2SAl Viro 
11765a18fff2SAl Viro retry:
11775a18fff2SAl Viro 	if (unlikely(!dentry)) {
11785a18fff2SAl Viro 		struct inode *dir = parent->d_inode;
11795a18fff2SAl Viro 		BUG_ON(nd->inode != dir);
11805a18fff2SAl Viro 
11815a18fff2SAl Viro 		mutex_lock(&dir->i_mutex);
11825a18fff2SAl Viro 		dentry = d_lookup(parent, name);
11835a18fff2SAl Viro 		if (likely(!dentry)) {
11845a18fff2SAl Viro 			dentry = d_alloc_and_lookup(parent, name, nd);
11855a18fff2SAl Viro 			if (IS_ERR(dentry)) {
11865a18fff2SAl Viro 				mutex_unlock(&dir->i_mutex);
11875a18fff2SAl Viro 				return PTR_ERR(dentry);
11885a18fff2SAl Viro 			}
11895a18fff2SAl Viro 			/* known good */
11905a18fff2SAl Viro 			need_reval = 0;
11915a18fff2SAl Viro 			status = 1;
11925a18fff2SAl Viro 		}
11935a18fff2SAl Viro 		mutex_unlock(&dir->i_mutex);
11945a18fff2SAl Viro 	}
11955a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
11965a18fff2SAl Viro 		status = d_revalidate(dentry, nd);
11975a18fff2SAl Viro 	if (unlikely(status <= 0)) {
11985a18fff2SAl Viro 		if (status < 0) {
11995a18fff2SAl Viro 			dput(dentry);
12005a18fff2SAl Viro 			return status;
12015a18fff2SAl Viro 		}
12025a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
12035a18fff2SAl Viro 			dput(dentry);
12045a18fff2SAl Viro 			dentry = NULL;
12055a18fff2SAl Viro 			need_reval = 1;
12065a18fff2SAl Viro 			goto retry;
12075a18fff2SAl Viro 		}
12085a18fff2SAl Viro 	}
12095a18fff2SAl Viro 
12101da177e4SLinus Torvalds 	path->mnt = mnt;
12111da177e4SLinus Torvalds 	path->dentry = dentry;
12129875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
121389312214SIan Kent 	if (unlikely(err < 0)) {
121489312214SIan Kent 		path_put_conditional(path, nd);
12159875cf80SDavid Howells 		return err;
121689312214SIan Kent 	}
121731e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
12181da177e4SLinus Torvalds 	return 0;
12191da177e4SLinus Torvalds }
12201da177e4SLinus Torvalds 
122152094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
122252094c8aSAl Viro {
122352094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
122452094c8aSAl Viro 		int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
122552094c8aSAl Viro 		if (err != -ECHILD)
122652094c8aSAl Viro 			return err;
122719660af7SAl Viro 		if (unlazy_walk(nd, NULL))
122852094c8aSAl Viro 			return -ECHILD;
122952094c8aSAl Viro 	}
123052094c8aSAl Viro 	return exec_permission(nd->inode, 0);
123152094c8aSAl Viro }
123252094c8aSAl Viro 
12339856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
12349856fa1bSAl Viro {
12359856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
12369856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
12379856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
12389856fa1bSAl Viro 				return -ECHILD;
12399856fa1bSAl Viro 		} else
12409856fa1bSAl Viro 			follow_dotdot(nd);
12419856fa1bSAl Viro 	}
12429856fa1bSAl Viro 	return 0;
12439856fa1bSAl Viro }
12449856fa1bSAl Viro 
1245951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1246951361f9SAl Viro {
1247951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1248951361f9SAl Viro 		path_put(&nd->path);
1249951361f9SAl Viro 	} else {
1250951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
12515b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1252951361f9SAl Viro 			nd->root.mnt = NULL;
1253951361f9SAl Viro 		rcu_read_unlock();
1254951361f9SAl Viro 		br_read_unlock(vfsmount_lock);
1255951361f9SAl Viro 	}
1256951361f9SAl Viro }
1257951361f9SAl Viro 
1258ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
1259ce57dfc1SAl Viro 		struct qstr *name, int type, int follow)
1260ce57dfc1SAl Viro {
1261ce57dfc1SAl Viro 	struct inode *inode;
1262ce57dfc1SAl Viro 	int err;
1263ce57dfc1SAl Viro 	/*
1264ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1265ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1266ce57dfc1SAl Viro 	 * parent relationships.
1267ce57dfc1SAl Viro 	 */
1268ce57dfc1SAl Viro 	if (unlikely(type != LAST_NORM))
1269ce57dfc1SAl Viro 		return handle_dots(nd, type);
1270ce57dfc1SAl Viro 	err = do_lookup(nd, name, path, &inode);
1271ce57dfc1SAl Viro 	if (unlikely(err)) {
1272ce57dfc1SAl Viro 		terminate_walk(nd);
1273ce57dfc1SAl Viro 		return err;
1274ce57dfc1SAl Viro 	}
1275ce57dfc1SAl Viro 	if (!inode) {
1276ce57dfc1SAl Viro 		path_to_nameidata(path, nd);
1277ce57dfc1SAl Viro 		terminate_walk(nd);
1278ce57dfc1SAl Viro 		return -ENOENT;
1279ce57dfc1SAl Viro 	}
1280ce57dfc1SAl Viro 	if (unlikely(inode->i_op->follow_link) && follow) {
128119660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
128219660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
128319660af7SAl Viro 				terminate_walk(nd);
1284ce57dfc1SAl Viro 				return -ECHILD;
128519660af7SAl Viro 			}
128619660af7SAl Viro 		}
1287ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1288ce57dfc1SAl Viro 		return 1;
1289ce57dfc1SAl Viro 	}
1290ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1291ce57dfc1SAl Viro 	nd->inode = inode;
1292ce57dfc1SAl Viro 	return 0;
1293ce57dfc1SAl Viro }
1294ce57dfc1SAl Viro 
12951da177e4SLinus Torvalds /*
1296b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1297b356379aSAl Viro  * limiting consecutive symlinks to 40.
1298b356379aSAl Viro  *
1299b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1300b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1301b356379aSAl Viro  */
1302b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1303b356379aSAl Viro {
1304b356379aSAl Viro 	int res;
1305b356379aSAl Viro 
1306b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1307b356379aSAl Viro 		path_put_conditional(path, nd);
1308b356379aSAl Viro 		path_put(&nd->path);
1309b356379aSAl Viro 		return -ELOOP;
1310b356379aSAl Viro 	}
13111a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1312b356379aSAl Viro 
1313b356379aSAl Viro 	nd->depth++;
1314b356379aSAl Viro 	current->link_count++;
1315b356379aSAl Viro 
1316b356379aSAl Viro 	do {
1317b356379aSAl Viro 		struct path link = *path;
1318b356379aSAl Viro 		void *cookie;
1319574197e0SAl Viro 
1320574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
1321b356379aSAl Viro 		if (!res)
1322b356379aSAl Viro 			res = walk_component(nd, path, &nd->last,
1323b356379aSAl Viro 					     nd->last_type, LOOKUP_FOLLOW);
1324574197e0SAl Viro 		put_link(nd, &link, cookie);
1325b356379aSAl Viro 	} while (res > 0);
1326b356379aSAl Viro 
1327b356379aSAl Viro 	current->link_count--;
1328b356379aSAl Viro 	nd->depth--;
1329b356379aSAl Viro 	return res;
1330b356379aSAl Viro }
1331b356379aSAl Viro 
1332b356379aSAl Viro /*
13331da177e4SLinus Torvalds  * Name resolution.
1334ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1335ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
13361da177e4SLinus Torvalds  *
1337ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1338ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
13391da177e4SLinus Torvalds  */
13406de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
13411da177e4SLinus Torvalds {
13421da177e4SLinus Torvalds 	struct path next;
13431da177e4SLinus Torvalds 	int err;
13441da177e4SLinus Torvalds 	unsigned int lookup_flags = nd->flags;
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 	while (*name=='/')
13471da177e4SLinus Torvalds 		name++;
13481da177e4SLinus Torvalds 	if (!*name)
1349086e183aSAl Viro 		return 0;
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
13521da177e4SLinus Torvalds 	for(;;) {
13531da177e4SLinus Torvalds 		unsigned long hash;
13541da177e4SLinus Torvalds 		struct qstr this;
13551da177e4SLinus Torvalds 		unsigned int c;
1356fe479a58SAl Viro 		int type;
13571da177e4SLinus Torvalds 
1358cdce5d6bSTrond Myklebust 		nd->flags |= LOOKUP_CONTINUE;
135952094c8aSAl Viro 
136052094c8aSAl Viro 		err = may_lookup(nd);
13611da177e4SLinus Torvalds  		if (err)
13621da177e4SLinus Torvalds 			break;
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 		this.name = name;
13651da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
13661da177e4SLinus Torvalds 
13671da177e4SLinus Torvalds 		hash = init_name_hash();
13681da177e4SLinus Torvalds 		do {
13691da177e4SLinus Torvalds 			name++;
13701da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
13711da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
13721da177e4SLinus Torvalds 		} while (c && (c != '/'));
13731da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
13741da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
13751da177e4SLinus Torvalds 
1376fe479a58SAl Viro 		type = LAST_NORM;
1377fe479a58SAl Viro 		if (this.name[0] == '.') switch (this.len) {
1378fe479a58SAl Viro 			case 2:
137916c2cd71SAl Viro 				if (this.name[1] == '.') {
1380fe479a58SAl Viro 					type = LAST_DOTDOT;
138116c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
138216c2cd71SAl Viro 				}
1383fe479a58SAl Viro 				break;
1384fe479a58SAl Viro 			case 1:
1385fe479a58SAl Viro 				type = LAST_DOT;
1386fe479a58SAl Viro 		}
13875a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
13885a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
138916c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
13905a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
13915a202bcdSAl Viro 				err = parent->d_op->d_hash(parent, nd->inode,
13925a202bcdSAl Viro 							   &this);
13935a202bcdSAl Viro 				if (err < 0)
13945a202bcdSAl Viro 					break;
13955a202bcdSAl Viro 			}
13965a202bcdSAl Viro 		}
1397fe479a58SAl Viro 
13981da177e4SLinus Torvalds 		/* remove trailing slashes? */
13991da177e4SLinus Torvalds 		if (!c)
14001da177e4SLinus Torvalds 			goto last_component;
14011da177e4SLinus Torvalds 		while (*++name == '/');
14021da177e4SLinus Torvalds 		if (!*name)
1403b356379aSAl Viro 			goto last_component;
14041da177e4SLinus Torvalds 
1405ce57dfc1SAl Viro 		err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1406ce57dfc1SAl Viro 		if (err < 0)
1407ce57dfc1SAl Viro 			return err;
1408fe479a58SAl Viro 
1409ce57dfc1SAl Viro 		if (err) {
1410b356379aSAl Viro 			err = nested_symlink(&next, nd);
14111da177e4SLinus Torvalds 			if (err)
1412a7472babSAl Viro 				return err;
141331e6b01fSNick Piggin 		}
14141da177e4SLinus Torvalds 		err = -ENOTDIR;
141531e6b01fSNick Piggin 		if (!nd->inode->i_op->lookup)
14161da177e4SLinus Torvalds 			break;
14171da177e4SLinus Torvalds 		continue;
14181da177e4SLinus Torvalds 		/* here ends the main loop */
14191da177e4SLinus Torvalds 
14201da177e4SLinus Torvalds last_component:
1421f55eab82STrond Myklebust 		/* Clear LOOKUP_CONTINUE iff it was previously unset */
1422f55eab82STrond Myklebust 		nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
1423ce57dfc1SAl Viro 		nd->last = this;
1424ce57dfc1SAl Viro 		nd->last_type = type;
1425ce57dfc1SAl Viro 		return 0;
1426ce57dfc1SAl Viro 	}
1427951361f9SAl Viro 	terminate_walk(nd);
14281da177e4SLinus Torvalds 	return err;
14291da177e4SLinus Torvalds }
14301da177e4SLinus Torvalds 
143170e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
143270e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
143331e6b01fSNick Piggin {
143431e6b01fSNick Piggin 	int retval = 0;
143531e6b01fSNick Piggin 	int fput_needed;
143631e6b01fSNick Piggin 	struct file *file;
143731e6b01fSNick Piggin 
143831e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
143916c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
144031e6b01fSNick Piggin 	nd->depth = 0;
14415b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
14425b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
144373d049a4SAl Viro 		if (*name) {
14445b6ca027SAl Viro 			if (!inode->i_op->lookup)
14455b6ca027SAl Viro 				return -ENOTDIR;
14465b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
14475b6ca027SAl Viro 			if (retval)
14485b6ca027SAl Viro 				return retval;
144973d049a4SAl Viro 		}
14505b6ca027SAl Viro 		nd->path = nd->root;
14515b6ca027SAl Viro 		nd->inode = inode;
14525b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
14535b6ca027SAl Viro 			br_read_lock(vfsmount_lock);
14545b6ca027SAl Viro 			rcu_read_lock();
14555b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
14565b6ca027SAl Viro 		} else {
14575b6ca027SAl Viro 			path_get(&nd->path);
14585b6ca027SAl Viro 		}
14595b6ca027SAl Viro 		return 0;
14605b6ca027SAl Viro 	}
14615b6ca027SAl Viro 
146231e6b01fSNick Piggin 	nd->root.mnt = NULL;
146331e6b01fSNick Piggin 
146431e6b01fSNick Piggin 	if (*name=='/') {
1465e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
146631e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
146731e6b01fSNick Piggin 			rcu_read_lock();
1468e41f7d4eSAl Viro 			set_root_rcu(nd);
1469e41f7d4eSAl Viro 		} else {
1470e41f7d4eSAl Viro 			set_root(nd);
1471e41f7d4eSAl Viro 			path_get(&nd->root);
1472e41f7d4eSAl Viro 		}
147331e6b01fSNick Piggin 		nd->path = nd->root;
147431e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1475e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
147631e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1477c28cc364SNick Piggin 			unsigned seq;
147831e6b01fSNick Piggin 
147931e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
148031e6b01fSNick Piggin 			rcu_read_lock();
148131e6b01fSNick Piggin 
1482c28cc364SNick Piggin 			do {
1483c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
148431e6b01fSNick Piggin 				nd->path = fs->pwd;
1485c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1486c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1487e41f7d4eSAl Viro 		} else {
1488e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1489e41f7d4eSAl Viro 		}
149031e6b01fSNick Piggin 	} else {
149131e6b01fSNick Piggin 		struct dentry *dentry;
149231e6b01fSNick Piggin 
14931abf0c71SAl Viro 		file = fget_raw_light(dfd, &fput_needed);
149431e6b01fSNick Piggin 		retval = -EBADF;
149531e6b01fSNick Piggin 		if (!file)
149631e6b01fSNick Piggin 			goto out_fail;
149731e6b01fSNick Piggin 
149831e6b01fSNick Piggin 		dentry = file->f_path.dentry;
149931e6b01fSNick Piggin 
1500f52e0c11SAl Viro 		if (*name) {
150131e6b01fSNick Piggin 			retval = -ENOTDIR;
150231e6b01fSNick Piggin 			if (!S_ISDIR(dentry->d_inode->i_mode))
150331e6b01fSNick Piggin 				goto fput_fail;
150431e6b01fSNick Piggin 
150531e6b01fSNick Piggin 			retval = file_permission(file, MAY_EXEC);
150631e6b01fSNick Piggin 			if (retval)
150731e6b01fSNick Piggin 				goto fput_fail;
1508f52e0c11SAl Viro 		}
150931e6b01fSNick Piggin 
151031e6b01fSNick Piggin 		nd->path = file->f_path;
1511e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
151231e6b01fSNick Piggin 			if (fput_needed)
151370e9b357SAl Viro 				*fp = file;
1514c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
151531e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
151631e6b01fSNick Piggin 			rcu_read_lock();
15175590ff0dSUlrich Drepper 		} else {
15185dd784d0SJan Blunck 			path_get(&file->f_path);
15195590ff0dSUlrich Drepper 			fput_light(file, fput_needed);
15201da177e4SLinus Torvalds 		}
1521e41f7d4eSAl Viro 	}
1522e41f7d4eSAl Viro 
152331e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
15249b4a9b14SAl Viro 	return 0;
15252dfdd266SJosef 'Jeff' Sipek 
15269b4a9b14SAl Viro fput_fail:
15279b4a9b14SAl Viro 	fput_light(file, fput_needed);
15289b4a9b14SAl Viro out_fail:
15299b4a9b14SAl Viro 	return retval;
15309b4a9b14SAl Viro }
15319b4a9b14SAl Viro 
1532bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1533bd92d7feSAl Viro {
1534bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1535bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1536bd92d7feSAl Viro 
1537bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
1538bd92d7feSAl Viro 	return walk_component(nd, path, &nd->last, nd->last_type,
1539bd92d7feSAl Viro 					nd->flags & LOOKUP_FOLLOW);
1540bd92d7feSAl Viro }
1541bd92d7feSAl Viro 
15429b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1543ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
15449b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
15459b4a9b14SAl Viro {
154670e9b357SAl Viro 	struct file *base = NULL;
1547bd92d7feSAl Viro 	struct path path;
1548bd92d7feSAl Viro 	int err;
154931e6b01fSNick Piggin 
155031e6b01fSNick Piggin 	/*
155131e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
155231e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
155331e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
155431e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
155531e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
155631e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
155731e6b01fSNick Piggin 	 * analogue, foo_rcu().
155831e6b01fSNick Piggin 	 *
155931e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
156031e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
156131e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
156231e6b01fSNick Piggin 	 * be able to complete).
156331e6b01fSNick Piggin 	 */
1564bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1565ee0827cdSAl Viro 
1566bd92d7feSAl Viro 	if (unlikely(err))
1567bd92d7feSAl Viro 		return err;
1568ee0827cdSAl Viro 
1569ee0827cdSAl Viro 	current->total_link_count = 0;
1570bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1571bd92d7feSAl Viro 
1572bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1573bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1574bd92d7feSAl Viro 		while (err > 0) {
1575bd92d7feSAl Viro 			void *cookie;
1576bd92d7feSAl Viro 			struct path link = path;
1577bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1578574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
1579bd92d7feSAl Viro 			if (!err)
1580bd92d7feSAl Viro 				err = lookup_last(nd, &path);
1581574197e0SAl Viro 			put_link(nd, &link, cookie);
1582bd92d7feSAl Viro 		}
1583bd92d7feSAl Viro 	}
1584ee0827cdSAl Viro 
15859f1fafeeSAl Viro 	if (!err)
15869f1fafeeSAl Viro 		err = complete_walk(nd);
1587bd92d7feSAl Viro 
1588bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
1589bd92d7feSAl Viro 		if (!nd->inode->i_op->lookup) {
1590bd92d7feSAl Viro 			path_put(&nd->path);
1591bd23a539SAl Viro 			err = -ENOTDIR;
1592bd92d7feSAl Viro 		}
1593bd92d7feSAl Viro 	}
159416c2cd71SAl Viro 
159570e9b357SAl Viro 	if (base)
159670e9b357SAl Viro 		fput(base);
1597ee0827cdSAl Viro 
15985b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
159931e6b01fSNick Piggin 		path_put(&nd->root);
160031e6b01fSNick Piggin 		nd->root.mnt = NULL;
160131e6b01fSNick Piggin 	}
1602bd92d7feSAl Viro 	return err;
160331e6b01fSNick Piggin }
160431e6b01fSNick Piggin 
1605ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1606ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1607ee0827cdSAl Viro {
1608ee0827cdSAl Viro 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1609ee0827cdSAl Viro 	if (unlikely(retval == -ECHILD))
1610ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags, nd);
1611ee0827cdSAl Viro 	if (unlikely(retval == -ESTALE))
1612ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1613ee0827cdSAl Viro 
161431e6b01fSNick Piggin 	if (likely(!retval)) {
161531e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
161631e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
161731e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
161831e6b01fSNick Piggin 		}
161931e6b01fSNick Piggin 	}
1620170aa3d0SUlrich Drepper 	return retval;
16211da177e4SLinus Torvalds }
16221da177e4SLinus Torvalds 
1623c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd)
16245590ff0dSUlrich Drepper {
1625c9c6cac0SAl Viro 	return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
16265590ff0dSUlrich Drepper }
16275590ff0dSUlrich Drepper 
1628d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1629d1811465SAl Viro {
1630d1811465SAl Viro 	struct nameidata nd;
1631d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1632d1811465SAl Viro 	if (!res)
1633d1811465SAl Viro 		*path = nd.path;
1634d1811465SAl Viro 	return res;
1635d1811465SAl Viro }
1636d1811465SAl Viro 
163716f18200SJosef 'Jeff' Sipek /**
163816f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
163916f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
164016f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
164116f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
164216f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
164316f18200SJosef 'Jeff' Sipek  * @nd: pointer to nameidata
164416f18200SJosef 'Jeff' Sipek  */
164516f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
164616f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
164716f18200SJosef 'Jeff' Sipek 		    struct nameidata *nd)
164816f18200SJosef 'Jeff' Sipek {
16495b6ca027SAl Viro 	nd->root.dentry = dentry;
16505b6ca027SAl Viro 	nd->root.mnt = mnt;
16515b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
16525b6ca027SAl Viro 	return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
165316f18200SJosef 'Jeff' Sipek }
165416f18200SJosef 'Jeff' Sipek 
1655eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1656eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
16571da177e4SLinus Torvalds {
165881fca444SChristoph Hellwig 	struct inode *inode = base->d_inode;
16591da177e4SLinus Torvalds 	struct dentry *dentry;
16601da177e4SLinus Torvalds 	int err;
16611da177e4SLinus Torvalds 
1662b74c79e9SNick Piggin 	err = exec_permission(inode, 0);
166381fca444SChristoph Hellwig 	if (err)
166481fca444SChristoph Hellwig 		return ERR_PTR(err);
16651da177e4SLinus Torvalds 
16661da177e4SLinus Torvalds 	/*
1667b04f784eSNick Piggin 	 * Don't bother with __d_lookup: callers are for creat as
1668b04f784eSNick Piggin 	 * well as unlink, so a lot of the time it would cost
1669b04f784eSNick Piggin 	 * a double lookup.
16706e6b1bd1SAl Viro 	 */
16716e6b1bd1SAl Viro 	dentry = d_lookup(base, name);
16726e6b1bd1SAl Viro 
1673fb045adbSNick Piggin 	if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
16746e6b1bd1SAl Viro 		dentry = do_revalidate(dentry, nd);
16756e6b1bd1SAl Viro 
16761da177e4SLinus Torvalds 	if (!dentry)
1677baa03890SNick Piggin 		dentry = d_alloc_and_lookup(base, name, nd);
16785a202bcdSAl Viro 
16791da177e4SLinus Torvalds 	return dentry;
16801da177e4SLinus Torvalds }
16811da177e4SLinus Torvalds 
1682057f6c01SJames Morris /*
1683057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1684057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1685057f6c01SJames Morris  * SMP-safe.
1686057f6c01SJames Morris  */
1687a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
16881da177e4SLinus Torvalds {
16894ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
16901da177e4SLinus Torvalds }
16911da177e4SLinus Torvalds 
1692eead1911SChristoph Hellwig /**
1693a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1694eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1695eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1696eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1697eead1911SChristoph Hellwig  *
1698a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1699a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1700eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1701eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1702eead1911SChristoph Hellwig  */
1703057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1704057f6c01SJames Morris {
1705057f6c01SJames Morris 	struct qstr this;
17066a96ba54SAl Viro 	unsigned long hash;
17076a96ba54SAl Viro 	unsigned int c;
1708057f6c01SJames Morris 
17092f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
17102f9092e1SDavid Woodhouse 
17116a96ba54SAl Viro 	this.name = name;
17126a96ba54SAl Viro 	this.len = len;
17136a96ba54SAl Viro 	if (!len)
17146a96ba54SAl Viro 		return ERR_PTR(-EACCES);
17156a96ba54SAl Viro 
17166a96ba54SAl Viro 	hash = init_name_hash();
17176a96ba54SAl Viro 	while (len--) {
17186a96ba54SAl Viro 		c = *(const unsigned char *)name++;
17196a96ba54SAl Viro 		if (c == '/' || c == '\0')
17206a96ba54SAl Viro 			return ERR_PTR(-EACCES);
17216a96ba54SAl Viro 		hash = partial_name_hash(c, hash);
17226a96ba54SAl Viro 	}
17236a96ba54SAl Viro 	this.hash = end_name_hash(hash);
17245a202bcdSAl Viro 	/*
17255a202bcdSAl Viro 	 * See if the low-level filesystem might want
17265a202bcdSAl Viro 	 * to use its own hash..
17275a202bcdSAl Viro 	 */
17285a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
17295a202bcdSAl Viro 		int err = base->d_op->d_hash(base, base->d_inode, &this);
17305a202bcdSAl Viro 		if (err < 0)
17315a202bcdSAl Viro 			return ERR_PTR(err);
17325a202bcdSAl Viro 	}
1733eead1911SChristoph Hellwig 
173449705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1735057f6c01SJames Morris }
1736057f6c01SJames Morris 
17372d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags,
17382d8f3038SAl Viro 		 struct path *path)
17391da177e4SLinus Torvalds {
17402d8f3038SAl Viro 	struct nameidata nd;
1741f52e0c11SAl Viro 	char *tmp = getname_flags(name, flags);
17421da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
17431da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
17442d8f3038SAl Viro 
17452d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
17462d8f3038SAl Viro 
17472d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
17481da177e4SLinus Torvalds 		putname(tmp);
17492d8f3038SAl Viro 		if (!err)
17502d8f3038SAl Viro 			*path = nd.path;
17511da177e4SLinus Torvalds 	}
17521da177e4SLinus Torvalds 	return err;
17531da177e4SLinus Torvalds }
17541da177e4SLinus Torvalds 
17552ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
17562ad94ae6SAl Viro 			struct nameidata *nd, char **name)
17572ad94ae6SAl Viro {
17582ad94ae6SAl Viro 	char *s = getname(path);
17592ad94ae6SAl Viro 	int error;
17602ad94ae6SAl Viro 
17612ad94ae6SAl Viro 	if (IS_ERR(s))
17622ad94ae6SAl Viro 		return PTR_ERR(s);
17632ad94ae6SAl Viro 
17642ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
17652ad94ae6SAl Viro 	if (error)
17662ad94ae6SAl Viro 		putname(s);
17672ad94ae6SAl Viro 	else
17682ad94ae6SAl Viro 		*name = s;
17692ad94ae6SAl Viro 
17702ad94ae6SAl Viro 	return error;
17712ad94ae6SAl Viro }
17722ad94ae6SAl Viro 
17731da177e4SLinus Torvalds /*
17741da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
17751da177e4SLinus Torvalds  * minimal.
17761da177e4SLinus Torvalds  */
17771da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
17781da177e4SLinus Torvalds {
1779da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1780da9592edSDavid Howells 
17811da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
17821da177e4SLinus Torvalds 		return 0;
1783e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
1784e795b717SSerge E. Hallyn 		goto other_userns;
1785da9592edSDavid Howells 	if (inode->i_uid == fsuid)
17861da177e4SLinus Torvalds 		return 0;
1787da9592edSDavid Howells 	if (dir->i_uid == fsuid)
17881da177e4SLinus Torvalds 		return 0;
1789e795b717SSerge E. Hallyn 
1790e795b717SSerge E. Hallyn other_userns:
1791e795b717SSerge E. Hallyn 	return !ns_capable(inode_userns(inode), CAP_FOWNER);
17921da177e4SLinus Torvalds }
17931da177e4SLinus Torvalds 
17941da177e4SLinus Torvalds /*
17951da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
17961da177e4SLinus Torvalds  *  whether the type of victim is right.
17971da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
17981da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
17991da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
18001da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
18011da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
18021da177e4SLinus Torvalds  *	a. be owner of dir, or
18031da177e4SLinus Torvalds  *	b. be owner of victim, or
18041da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
18051da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
18061da177e4SLinus Torvalds  *     links pointing to it.
18071da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
18081da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
18091da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
18101da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
18111da177e4SLinus Torvalds  *     nfs_async_unlink().
18121da177e4SLinus Torvalds  */
1813858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
18141da177e4SLinus Torvalds {
18151da177e4SLinus Torvalds 	int error;
18161da177e4SLinus Torvalds 
18171da177e4SLinus Torvalds 	if (!victim->d_inode)
18181da177e4SLinus Torvalds 		return -ENOENT;
18191da177e4SLinus Torvalds 
18201da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
1821cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
18221da177e4SLinus Torvalds 
1823f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
18241da177e4SLinus Torvalds 	if (error)
18251da177e4SLinus Torvalds 		return error;
18261da177e4SLinus Torvalds 	if (IS_APPEND(dir))
18271da177e4SLinus Torvalds 		return -EPERM;
18281da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1829f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
18301da177e4SLinus Torvalds 		return -EPERM;
18311da177e4SLinus Torvalds 	if (isdir) {
18321da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
18331da177e4SLinus Torvalds 			return -ENOTDIR;
18341da177e4SLinus Torvalds 		if (IS_ROOT(victim))
18351da177e4SLinus Torvalds 			return -EBUSY;
18361da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
18371da177e4SLinus Torvalds 		return -EISDIR;
18381da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18391da177e4SLinus Torvalds 		return -ENOENT;
18401da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
18411da177e4SLinus Torvalds 		return -EBUSY;
18421da177e4SLinus Torvalds 	return 0;
18431da177e4SLinus Torvalds }
18441da177e4SLinus Torvalds 
18451da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
18461da177e4SLinus Torvalds  *  dir.
18471da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
18481da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
18491da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
18501da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
18511da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
18521da177e4SLinus Torvalds  */
1853a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
18541da177e4SLinus Torvalds {
18551da177e4SLinus Torvalds 	if (child->d_inode)
18561da177e4SLinus Torvalds 		return -EEXIST;
18571da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18581da177e4SLinus Torvalds 		return -ENOENT;
1859f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
18601da177e4SLinus Torvalds }
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds /*
18631da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
18641da177e4SLinus Torvalds  */
18651da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
18661da177e4SLinus Torvalds {
18671da177e4SLinus Torvalds 	struct dentry *p;
18681da177e4SLinus Torvalds 
18691da177e4SLinus Torvalds 	if (p1 == p2) {
1870f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
18711da177e4SLinus Torvalds 		return NULL;
18721da177e4SLinus Torvalds 	}
18731da177e4SLinus Torvalds 
1874a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
18751da177e4SLinus Torvalds 
1876e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1877e2761a11SOGAWA Hirofumi 	if (p) {
1878f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1879f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
18801da177e4SLinus Torvalds 		return p;
18811da177e4SLinus Torvalds 	}
18821da177e4SLinus Torvalds 
1883e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1884e2761a11SOGAWA Hirofumi 	if (p) {
1885f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1886f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
18871da177e4SLinus Torvalds 		return p;
18881da177e4SLinus Torvalds 	}
18891da177e4SLinus Torvalds 
1890f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1891f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
18921da177e4SLinus Torvalds 	return NULL;
18931da177e4SLinus Torvalds }
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
18961da177e4SLinus Torvalds {
18971b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
18981da177e4SLinus Torvalds 	if (p1 != p2) {
18991b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1900a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
19011da177e4SLinus Torvalds 	}
19021da177e4SLinus Torvalds }
19031da177e4SLinus Torvalds 
19041da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
19051da177e4SLinus Torvalds 		struct nameidata *nd)
19061da177e4SLinus Torvalds {
1907a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
19081da177e4SLinus Torvalds 
19091da177e4SLinus Torvalds 	if (error)
19101da177e4SLinus Torvalds 		return error;
19111da177e4SLinus Torvalds 
1912acfa4380SAl Viro 	if (!dir->i_op->create)
19131da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
19141da177e4SLinus Torvalds 	mode &= S_IALLUGO;
19151da177e4SLinus Torvalds 	mode |= S_IFREG;
19161da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
19171da177e4SLinus Torvalds 	if (error)
19181da177e4SLinus Torvalds 		return error;
19191da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
1920a74574aaSStephen Smalley 	if (!error)
1921f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
19221da177e4SLinus Torvalds 	return error;
19231da177e4SLinus Torvalds }
19241da177e4SLinus Torvalds 
192573d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
19261da177e4SLinus Torvalds {
19273fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
19281da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
19291da177e4SLinus Torvalds 	int error;
19301da177e4SLinus Torvalds 
1931bcda7652SAl Viro 	/* O_PATH? */
1932bcda7652SAl Viro 	if (!acc_mode)
1933bcda7652SAl Viro 		return 0;
1934bcda7652SAl Viro 
19351da177e4SLinus Torvalds 	if (!inode)
19361da177e4SLinus Torvalds 		return -ENOENT;
19371da177e4SLinus Torvalds 
1938c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
1939c8fe8f30SChristoph Hellwig 	case S_IFLNK:
19401da177e4SLinus Torvalds 		return -ELOOP;
1941c8fe8f30SChristoph Hellwig 	case S_IFDIR:
1942c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
19431da177e4SLinus Torvalds 			return -EISDIR;
1944c8fe8f30SChristoph Hellwig 		break;
1945c8fe8f30SChristoph Hellwig 	case S_IFBLK:
1946c8fe8f30SChristoph Hellwig 	case S_IFCHR:
19473fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
19481da177e4SLinus Torvalds 			return -EACCES;
1949c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
1950c8fe8f30SChristoph Hellwig 	case S_IFIFO:
1951c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
19521da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
1953c8fe8f30SChristoph Hellwig 		break;
19544a3fd211SDave Hansen 	}
1955b41572e9SDave Hansen 
19563fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
1957b41572e9SDave Hansen 	if (error)
1958b41572e9SDave Hansen 		return error;
19596146f0d5SMimi Zohar 
19601da177e4SLinus Torvalds 	/*
19611da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
19621da177e4SLinus Torvalds 	 */
19631da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
19648737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
19657715b521SAl Viro 			return -EPERM;
19661da177e4SLinus Torvalds 		if (flag & O_TRUNC)
19677715b521SAl Viro 			return -EPERM;
19681da177e4SLinus Torvalds 	}
19691da177e4SLinus Torvalds 
19701da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
19712e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
19727715b521SAl Viro 		return -EPERM;
19731da177e4SLinus Torvalds 
19741da177e4SLinus Torvalds 	/*
19751da177e4SLinus Torvalds 	 * Ensure there are no outstanding leases on the file.
19761da177e4SLinus Torvalds 	 */
1977b65a9cfcSAl Viro 	return break_lease(inode, flag);
19787715b521SAl Viro }
19797715b521SAl Viro 
1980e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
19817715b521SAl Viro {
1982e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
19837715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
19847715b521SAl Viro 	int error = get_write_access(inode);
19851da177e4SLinus Torvalds 	if (error)
19867715b521SAl Viro 		return error;
19871da177e4SLinus Torvalds 	/*
19881da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
19891da177e4SLinus Torvalds 	 */
19901da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
1991be6d3e56SKentaro Takeda 	if (!error)
1992ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
19931da177e4SLinus Torvalds 	if (!error) {
19947715b521SAl Viro 		error = do_truncate(path->dentry, 0,
1995d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
1996e1181ee6SJeff Layton 				    filp);
19971da177e4SLinus Torvalds 	}
19981da177e4SLinus Torvalds 	put_write_access(inode);
1999acd0c935SMimi Zohar 	return error;
20001da177e4SLinus Torvalds }
20011da177e4SLinus Torvalds 
2002d57999e1SDave Hansen /*
2003d57999e1SDave Hansen  * Note that while the flag value (low two bits) for sys_open means:
2004d57999e1SDave Hansen  *	00 - read-only
2005d57999e1SDave Hansen  *	01 - write-only
2006d57999e1SDave Hansen  *	10 - read-write
2007d57999e1SDave Hansen  *	11 - special
2008d57999e1SDave Hansen  * it is changed into
2009d57999e1SDave Hansen  *	00 - no permissions needed
2010d57999e1SDave Hansen  *	01 - read-permission
2011d57999e1SDave Hansen  *	10 - write-permission
2012d57999e1SDave Hansen  *	11 - read-write
2013d57999e1SDave Hansen  * for the internal routines (ie open_namei()/follow_link() etc)
2014d57999e1SDave Hansen  * This is more logical, and also allows the 00 "no perm needed"
2015d57999e1SDave Hansen  * to be used for symlinks (where the permissions are checked
2016d57999e1SDave Hansen  * later).
2017d57999e1SDave Hansen  *
2018d57999e1SDave Hansen */
2019d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2020d57999e1SDave Hansen {
2021d57999e1SDave Hansen 	if ((flag+1) & O_ACCMODE)
2022d57999e1SDave Hansen 		flag++;
2023d57999e1SDave Hansen 	return flag;
2024d57999e1SDave Hansen }
2025d57999e1SDave Hansen 
202631e6b01fSNick Piggin /*
2027fe2d35ffSAl Viro  * Handle the last step of open()
202831e6b01fSNick Piggin  */
2029fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
2030c3e380b0SAl Viro 			    const struct open_flags *op, const char *pathname)
2031fb1cc555SAl Viro {
2032a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
20336c0d46c4SAl Viro 	struct dentry *dentry;
2034ca344a89SAl Viro 	int open_flag = op->open_flag;
20356c0d46c4SAl Viro 	int will_truncate = open_flag & O_TRUNC;
2036ca344a89SAl Viro 	int want_write = 0;
2037bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2038fb1cc555SAl Viro 	struct file *filp;
203916c2cd71SAl Viro 	int error;
2040fb1cc555SAl Viro 
2041c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2042c3e380b0SAl Viro 	nd->flags |= op->intent;
2043c3e380b0SAl Viro 
20441f36f774SAl Viro 	switch (nd->last_type) {
20451f36f774SAl Viro 	case LAST_DOTDOT:
2046176306f5SNeil Brown 	case LAST_DOT:
2047fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2048fe2d35ffSAl Viro 		if (error)
2049fe2d35ffSAl Viro 			return ERR_PTR(error);
20501f36f774SAl Viro 		/* fallthrough */
20511f36f774SAl Viro 	case LAST_ROOT:
20529f1fafeeSAl Viro 		error = complete_walk(nd);
205316c2cd71SAl Viro 		if (error)
20549f1fafeeSAl Viro 			return ERR_PTR(error);
2055fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2056ca344a89SAl Viro 		if (open_flag & O_CREAT) {
205716c2cd71SAl Viro 			error = -EISDIR;
20581f36f774SAl Viro 			goto exit;
2059fe2d35ffSAl Viro 		}
2060fe2d35ffSAl Viro 		goto ok;
20611f36f774SAl Viro 	case LAST_BIND:
20629f1fafeeSAl Viro 		error = complete_walk(nd);
206316c2cd71SAl Viro 		if (error)
20649f1fafeeSAl Viro 			return ERR_PTR(error);
20651f36f774SAl Viro 		audit_inode(pathname, dir);
20661f36f774SAl Viro 		goto ok;
20671f36f774SAl Viro 	}
2068a2c36b45SAl Viro 
2069ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2070bcda7652SAl Viro 		int symlink_ok = 0;
2071fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2072fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2073bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2074bcda7652SAl Viro 			symlink_ok = 1;
2075fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2076ce57dfc1SAl Viro 		error = walk_component(nd, path, &nd->last, LAST_NORM,
2077ce57dfc1SAl Viro 					!symlink_ok);
2078ce57dfc1SAl Viro 		if (error < 0)
2079fe2d35ffSAl Viro 			return ERR_PTR(error);
2080ce57dfc1SAl Viro 		if (error) /* symlink */
2081fe2d35ffSAl Viro 			return NULL;
2082fe2d35ffSAl Viro 		/* sayonara */
20839f1fafeeSAl Viro 		error = complete_walk(nd);
20849f1fafeeSAl Viro 		if (error)
2085fe2d35ffSAl Viro 			return ERR_PTR(-ECHILD);
2086fe2d35ffSAl Viro 
2087fe2d35ffSAl Viro 		error = -ENOTDIR;
2088fe2d35ffSAl Viro 		if (nd->flags & LOOKUP_DIRECTORY) {
2089ce57dfc1SAl Viro 			if (!nd->inode->i_op->lookup)
2090fe2d35ffSAl Viro 				goto exit;
2091fe2d35ffSAl Viro 		}
2092fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2093fe2d35ffSAl Viro 		goto ok;
2094fe2d35ffSAl Viro 	}
2095fe2d35ffSAl Viro 
2096fe2d35ffSAl Viro 	/* create side of things */
20979f1fafeeSAl Viro 	error = complete_walk(nd);
20989f1fafeeSAl Viro 	if (error)
20999f1fafeeSAl Viro 		return ERR_PTR(error);
2100fe2d35ffSAl Viro 
2101fe2d35ffSAl Viro 	audit_inode(pathname, dir);
210216c2cd71SAl Viro 	error = -EISDIR;
21031f36f774SAl Viro 	/* trailing slashes? */
210431e6b01fSNick Piggin 	if (nd->last.name[nd->last.len])
21051f36f774SAl Viro 		goto exit;
21061f36f774SAl Viro 
2107a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
2108a1e28038SAl Viro 
21096c0d46c4SAl Viro 	dentry = lookup_hash(nd);
21106c0d46c4SAl Viro 	error = PTR_ERR(dentry);
21116c0d46c4SAl Viro 	if (IS_ERR(dentry)) {
2112fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
2113fb1cc555SAl Viro 		goto exit;
2114fb1cc555SAl Viro 	}
2115fb1cc555SAl Viro 
21166c0d46c4SAl Viro 	path->dentry = dentry;
21176c0d46c4SAl Viro 	path->mnt = nd->path.mnt;
21186c0d46c4SAl Viro 
2119fb1cc555SAl Viro 	/* Negative dentry, just create the file */
21206c0d46c4SAl Viro 	if (!dentry->d_inode) {
21216c0d46c4SAl Viro 		int mode = op->mode;
21226c0d46c4SAl Viro 		if (!IS_POSIXACL(dir->d_inode))
21236c0d46c4SAl Viro 			mode &= ~current_umask();
2124fb1cc555SAl Viro 		/*
2125fb1cc555SAl Viro 		 * This write is needed to ensure that a
21266c0d46c4SAl Viro 		 * rw->ro transition does not occur between
2127fb1cc555SAl Viro 		 * the time when the file is created and when
2128fb1cc555SAl Viro 		 * a permanent write count is taken through
2129fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
2130fb1cc555SAl Viro 		 */
2131fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
2132fb1cc555SAl Viro 		if (error)
2133fb1cc555SAl Viro 			goto exit_mutex_unlock;
2134ca344a89SAl Viro 		want_write = 1;
21359b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2136ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
21376c0d46c4SAl Viro 		will_truncate = 0;
2138bcda7652SAl Viro 		acc_mode = MAY_OPEN;
21396c0d46c4SAl Viro 		error = security_path_mknod(&nd->path, dentry, mode, 0);
21406c0d46c4SAl Viro 		if (error)
21416c0d46c4SAl Viro 			goto exit_mutex_unlock;
21426c0d46c4SAl Viro 		error = vfs_create(dir->d_inode, dentry, mode, nd);
21436c0d46c4SAl Viro 		if (error)
21446c0d46c4SAl Viro 			goto exit_mutex_unlock;
21456c0d46c4SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
21466c0d46c4SAl Viro 		dput(nd->path.dentry);
21476c0d46c4SAl Viro 		nd->path.dentry = dentry;
2148ca344a89SAl Viro 		goto common;
2149fb1cc555SAl Viro 	}
2150fb1cc555SAl Viro 
2151fb1cc555SAl Viro 	/*
2152fb1cc555SAl Viro 	 * It already exists.
2153fb1cc555SAl Viro 	 */
2154fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2155fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2156fb1cc555SAl Viro 
2157fb1cc555SAl Viro 	error = -EEXIST;
2158ca344a89SAl Viro 	if (open_flag & O_EXCL)
2159fb1cc555SAl Viro 		goto exit_dput;
2160fb1cc555SAl Viro 
21619875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
21629875cf80SDavid Howells 	if (error < 0)
2163fb1cc555SAl Viro 		goto exit_dput;
2164fb1cc555SAl Viro 
2165fb1cc555SAl Viro 	error = -ENOENT;
2166fb1cc555SAl Viro 	if (!path->dentry->d_inode)
2167fb1cc555SAl Viro 		goto exit_dput;
21689e67f361SAl Viro 
21699e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
2170fb1cc555SAl Viro 		return NULL;
2171fb1cc555SAl Viro 
2172fb1cc555SAl Viro 	path_to_nameidata(path, nd);
217331e6b01fSNick Piggin 	nd->inode = path->dentry->d_inode;
2174fb1cc555SAl Viro 	error = -EISDIR;
217531e6b01fSNick Piggin 	if (S_ISDIR(nd->inode->i_mode))
2176fb1cc555SAl Viro 		goto exit;
217767ee3ad2SAl Viro ok:
21786c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
21796c0d46c4SAl Viro 		will_truncate = 0;
21806c0d46c4SAl Viro 
21810f9d1a10SAl Viro 	if (will_truncate) {
21820f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
21830f9d1a10SAl Viro 		if (error)
21840f9d1a10SAl Viro 			goto exit;
2185ca344a89SAl Viro 		want_write = 1;
21860f9d1a10SAl Viro 	}
2187ca344a89SAl Viro common:
2188bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2189ca344a89SAl Viro 	if (error)
21900f9d1a10SAl Viro 		goto exit;
21910f9d1a10SAl Viro 	filp = nameidata_to_filp(nd);
21920f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
21930f9d1a10SAl Viro 		error = ima_file_check(filp, op->acc_mode);
21940f9d1a10SAl Viro 		if (error) {
21950f9d1a10SAl Viro 			fput(filp);
21960f9d1a10SAl Viro 			filp = ERR_PTR(error);
21970f9d1a10SAl Viro 		}
21980f9d1a10SAl Viro 	}
21990f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
22000f9d1a10SAl Viro 		if (will_truncate) {
22010f9d1a10SAl Viro 			error = handle_truncate(filp);
22020f9d1a10SAl Viro 			if (error) {
22030f9d1a10SAl Viro 				fput(filp);
22040f9d1a10SAl Viro 				filp = ERR_PTR(error);
22050f9d1a10SAl Viro 			}
22060f9d1a10SAl Viro 		}
22070f9d1a10SAl Viro 	}
2208ca344a89SAl Viro out:
2209ca344a89SAl Viro 	if (want_write)
22100f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
22110f9d1a10SAl Viro 	path_put(&nd->path);
2212fb1cc555SAl Viro 	return filp;
2213fb1cc555SAl Viro 
2214fb1cc555SAl Viro exit_mutex_unlock:
2215fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2216fb1cc555SAl Viro exit_dput:
2217fb1cc555SAl Viro 	path_put_conditional(path, nd);
2218fb1cc555SAl Viro exit:
2219ca344a89SAl Viro 	filp = ERR_PTR(error);
2220ca344a89SAl Viro 	goto out;
2221fb1cc555SAl Viro }
2222fb1cc555SAl Viro 
222313aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname,
222473d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
22251da177e4SLinus Torvalds {
2226fe2d35ffSAl Viro 	struct file *base = NULL;
22274a3fd211SDave Hansen 	struct file *filp;
22289850c056SAl Viro 	struct path path;
222913aab428SAl Viro 	int error;
223031e6b01fSNick Piggin 
223131e6b01fSNick Piggin 	filp = get_empty_filp();
223231e6b01fSNick Piggin 	if (!filp)
223331e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
223431e6b01fSNick Piggin 
223547c805dcSAl Viro 	filp->f_flags = op->open_flag;
223673d049a4SAl Viro 	nd->intent.open.file = filp;
223773d049a4SAl Viro 	nd->intent.open.flags = open_to_namei_flags(op->open_flag);
223873d049a4SAl Viro 	nd->intent.open.create_mode = op->mode;
223931e6b01fSNick Piggin 
224073d049a4SAl Viro 	error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
224131e6b01fSNick Piggin 	if (unlikely(error))
224213aab428SAl Viro 		goto out_filp;
224331e6b01fSNick Piggin 
2244fe2d35ffSAl Viro 	current->total_link_count = 0;
224573d049a4SAl Viro 	error = link_path_walk(pathname, nd);
224631e6b01fSNick Piggin 	if (unlikely(error))
224731e6b01fSNick Piggin 		goto out_filp;
22481da177e4SLinus Torvalds 
224973d049a4SAl Viro 	filp = do_last(nd, &path, op, pathname);
2250806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
22517b9337aaSNick Piggin 		struct path link = path;
2252def4af30SAl Viro 		void *cookie;
2253574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
225473d049a4SAl Viro 			path_put_conditional(&path, nd);
225573d049a4SAl Viro 			path_put(&nd->path);
225640b39136SAl Viro 			filp = ERR_PTR(-ELOOP);
225740b39136SAl Viro 			break;
225840b39136SAl Viro 		}
225973d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
226073d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2261574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
2262c3e380b0SAl Viro 		if (unlikely(error))
2263f1afe9efSAl Viro 			filp = ERR_PTR(error);
2264c3e380b0SAl Viro 		else
226573d049a4SAl Viro 			filp = do_last(nd, &path, op, pathname);
2266574197e0SAl Viro 		put_link(nd, &link, cookie);
2267806b681cSAl Viro 	}
226810fa8e62SAl Viro out:
226973d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
227073d049a4SAl Viro 		path_put(&nd->root);
2271fe2d35ffSAl Viro 	if (base)
2272fe2d35ffSAl Viro 		fput(base);
227373d049a4SAl Viro 	release_open_intent(nd);
227410fa8e62SAl Viro 	return filp;
22751da177e4SLinus Torvalds 
227631e6b01fSNick Piggin out_filp:
227710fa8e62SAl Viro 	filp = ERR_PTR(error);
227810fa8e62SAl Viro 	goto out;
2279de459215SKirill Korotaev }
22801da177e4SLinus Torvalds 
228113aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname,
228213aab428SAl Viro 		const struct open_flags *op, int flags)
228313aab428SAl Viro {
228473d049a4SAl Viro 	struct nameidata nd;
228513aab428SAl Viro 	struct file *filp;
228613aab428SAl Viro 
228773d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
228813aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
228973d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
229013aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
229173d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
229213aab428SAl Viro 	return filp;
229313aab428SAl Viro }
229413aab428SAl Viro 
229573d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
229673d049a4SAl Viro 		const char *name, const struct open_flags *op, int flags)
229773d049a4SAl Viro {
229873d049a4SAl Viro 	struct nameidata nd;
229973d049a4SAl Viro 	struct file *file;
230073d049a4SAl Viro 
230173d049a4SAl Viro 	nd.root.mnt = mnt;
230273d049a4SAl Viro 	nd.root.dentry = dentry;
230373d049a4SAl Viro 
230473d049a4SAl Viro 	flags |= LOOKUP_ROOT;
230573d049a4SAl Viro 
2306bcda7652SAl Viro 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
230773d049a4SAl Viro 		return ERR_PTR(-ELOOP);
230873d049a4SAl Viro 
230973d049a4SAl Viro 	file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
231073d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
231173d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags);
231273d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
231373d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
231473d049a4SAl Viro 	return file;
231573d049a4SAl Viro }
231673d049a4SAl Viro 
23171da177e4SLinus Torvalds /**
23181da177e4SLinus Torvalds  * lookup_create - lookup a dentry, creating it if it doesn't exist
23191da177e4SLinus Torvalds  * @nd: nameidata info
23201da177e4SLinus Torvalds  * @is_dir: directory flag
23211da177e4SLinus Torvalds  *
23221da177e4SLinus Torvalds  * Simple function to lookup and return a dentry and create it
23231da177e4SLinus Torvalds  * if it doesn't exist.  Is SMP-safe.
2324c663e5d8SChristoph Hellwig  *
23254ac91378SJan Blunck  * Returns with nd->path.dentry->d_inode->i_mutex locked.
23261da177e4SLinus Torvalds  */
23271da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir)
23281da177e4SLinus Torvalds {
2329c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
23301da177e4SLinus Torvalds 
23314ac91378SJan Blunck 	mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2332c663e5d8SChristoph Hellwig 	/*
2333c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2334c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2335c663e5d8SChristoph Hellwig 	 */
23361da177e4SLinus Torvalds 	if (nd->last_type != LAST_NORM)
23371da177e4SLinus Torvalds 		goto fail;
23381da177e4SLinus Torvalds 	nd->flags &= ~LOOKUP_PARENT;
23393516586aSAl Viro 	nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2340a634904aSASANO Masahiro 	nd->intent.open.flags = O_EXCL;
2341c663e5d8SChristoph Hellwig 
2342c663e5d8SChristoph Hellwig 	/*
2343c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2344c663e5d8SChristoph Hellwig 	 */
234549705b77SChristoph Hellwig 	dentry = lookup_hash(nd);
23461da177e4SLinus Torvalds 	if (IS_ERR(dentry))
23471da177e4SLinus Torvalds 		goto fail;
2348c663e5d8SChristoph Hellwig 
2349e9baf6e5SAl Viro 	if (dentry->d_inode)
2350e9baf6e5SAl Viro 		goto eexist;
2351c663e5d8SChristoph Hellwig 	/*
2352c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2353c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2354c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2355c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2356c663e5d8SChristoph Hellwig 	 */
2357e9baf6e5SAl Viro 	if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
23581da177e4SLinus Torvalds 		dput(dentry);
23591da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2360e9baf6e5SAl Viro 	}
2361e9baf6e5SAl Viro 	return dentry;
2362e9baf6e5SAl Viro eexist:
2363e9baf6e5SAl Viro 	dput(dentry);
2364e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
23651da177e4SLinus Torvalds fail:
23661da177e4SLinus Torvalds 	return dentry;
23671da177e4SLinus Torvalds }
2368f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create);
23691da177e4SLinus Torvalds 
23701da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
23711da177e4SLinus Torvalds {
2372a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
23731da177e4SLinus Torvalds 
23741da177e4SLinus Torvalds 	if (error)
23751da177e4SLinus Torvalds 		return error;
23761da177e4SLinus Torvalds 
2377e795b717SSerge E. Hallyn 	if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2378e795b717SSerge E. Hallyn 	    !ns_capable(inode_userns(dir), CAP_MKNOD))
23791da177e4SLinus Torvalds 		return -EPERM;
23801da177e4SLinus Torvalds 
2381acfa4380SAl Viro 	if (!dir->i_op->mknod)
23821da177e4SLinus Torvalds 		return -EPERM;
23831da177e4SLinus Torvalds 
238408ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
238508ce5f16SSerge E. Hallyn 	if (error)
238608ce5f16SSerge E. Hallyn 		return error;
238708ce5f16SSerge E. Hallyn 
23881da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
23891da177e4SLinus Torvalds 	if (error)
23901da177e4SLinus Torvalds 		return error;
23911da177e4SLinus Torvalds 
23921da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2393a74574aaSStephen Smalley 	if (!error)
2394f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
23951da177e4SLinus Torvalds 	return error;
23961da177e4SLinus Torvalds }
23971da177e4SLinus Torvalds 
2398463c3197SDave Hansen static int may_mknod(mode_t mode)
2399463c3197SDave Hansen {
2400463c3197SDave Hansen 	switch (mode & S_IFMT) {
2401463c3197SDave Hansen 	case S_IFREG:
2402463c3197SDave Hansen 	case S_IFCHR:
2403463c3197SDave Hansen 	case S_IFBLK:
2404463c3197SDave Hansen 	case S_IFIFO:
2405463c3197SDave Hansen 	case S_IFSOCK:
2406463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2407463c3197SDave Hansen 		return 0;
2408463c3197SDave Hansen 	case S_IFDIR:
2409463c3197SDave Hansen 		return -EPERM;
2410463c3197SDave Hansen 	default:
2411463c3197SDave Hansen 		return -EINVAL;
2412463c3197SDave Hansen 	}
2413463c3197SDave Hansen }
2414463c3197SDave Hansen 
24152e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
24162e4d0924SHeiko Carstens 		unsigned, dev)
24171da177e4SLinus Torvalds {
24182ad94ae6SAl Viro 	int error;
24191da177e4SLinus Torvalds 	char *tmp;
24201da177e4SLinus Torvalds 	struct dentry *dentry;
24211da177e4SLinus Torvalds 	struct nameidata nd;
24221da177e4SLinus Torvalds 
24231da177e4SLinus Torvalds 	if (S_ISDIR(mode))
24241da177e4SLinus Torvalds 		return -EPERM;
24251da177e4SLinus Torvalds 
24262ad94ae6SAl Viro 	error = user_path_parent(dfd, filename, &nd, &tmp);
24271da177e4SLinus Torvalds 	if (error)
24282ad94ae6SAl Viro 		return error;
24292ad94ae6SAl Viro 
24301da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
2431463c3197SDave Hansen 	if (IS_ERR(dentry)) {
24321da177e4SLinus Torvalds 		error = PTR_ERR(dentry);
2433463c3197SDave Hansen 		goto out_unlock;
2434463c3197SDave Hansen 	}
24354ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2436ce3b0f8dSAl Viro 		mode &= ~current_umask();
2437463c3197SDave Hansen 	error = may_mknod(mode);
2438463c3197SDave Hansen 	if (error)
2439463c3197SDave Hansen 		goto out_dput;
2440463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2441463c3197SDave Hansen 	if (error)
2442463c3197SDave Hansen 		goto out_dput;
2443be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd.path, dentry, mode, dev);
2444be6d3e56SKentaro Takeda 	if (error)
2445be6d3e56SKentaro Takeda 		goto out_drop_write;
24461da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
24471da177e4SLinus Torvalds 		case 0: case S_IFREG:
24484ac91378SJan Blunck 			error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
24491da177e4SLinus Torvalds 			break;
24501da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
24514ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
24521da177e4SLinus Torvalds 					new_decode_dev(dev));
24531da177e4SLinus Torvalds 			break;
24541da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
24554ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
24561da177e4SLinus Torvalds 			break;
24571da177e4SLinus Torvalds 	}
2458be6d3e56SKentaro Takeda out_drop_write:
2459463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2460463c3197SDave Hansen out_dput:
24611da177e4SLinus Torvalds 	dput(dentry);
2462463c3197SDave Hansen out_unlock:
24634ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
24641d957f9bSJan Blunck 	path_put(&nd.path);
24651da177e4SLinus Torvalds 	putname(tmp);
24661da177e4SLinus Torvalds 
24671da177e4SLinus Torvalds 	return error;
24681da177e4SLinus Torvalds }
24691da177e4SLinus Torvalds 
24703480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
24715590ff0dSUlrich Drepper {
24725590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
24735590ff0dSUlrich Drepper }
24745590ff0dSUlrich Drepper 
24751da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
24761da177e4SLinus Torvalds {
2477a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
24781da177e4SLinus Torvalds 
24791da177e4SLinus Torvalds 	if (error)
24801da177e4SLinus Torvalds 		return error;
24811da177e4SLinus Torvalds 
2482acfa4380SAl Viro 	if (!dir->i_op->mkdir)
24831da177e4SLinus Torvalds 		return -EPERM;
24841da177e4SLinus Torvalds 
24851da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
24861da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
24871da177e4SLinus Torvalds 	if (error)
24881da177e4SLinus Torvalds 		return error;
24891da177e4SLinus Torvalds 
24901da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2491a74574aaSStephen Smalley 	if (!error)
2492f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
24931da177e4SLinus Torvalds 	return error;
24941da177e4SLinus Torvalds }
24951da177e4SLinus Torvalds 
24962e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
24971da177e4SLinus Torvalds {
24981da177e4SLinus Torvalds 	int error = 0;
24991da177e4SLinus Torvalds 	char * tmp;
25006902d925SDave Hansen 	struct dentry *dentry;
25016902d925SDave Hansen 	struct nameidata nd;
25021da177e4SLinus Torvalds 
25032ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &tmp);
25042ad94ae6SAl Viro 	if (error)
25056902d925SDave Hansen 		goto out_err;
25061da177e4SLinus Torvalds 
25071da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 1);
25081da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
25096902d925SDave Hansen 	if (IS_ERR(dentry))
25106902d925SDave Hansen 		goto out_unlock;
25116902d925SDave Hansen 
25124ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2513ce3b0f8dSAl Viro 		mode &= ~current_umask();
2514463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2515463c3197SDave Hansen 	if (error)
2516463c3197SDave Hansen 		goto out_dput;
2517be6d3e56SKentaro Takeda 	error = security_path_mkdir(&nd.path, dentry, mode);
2518be6d3e56SKentaro Takeda 	if (error)
2519be6d3e56SKentaro Takeda 		goto out_drop_write;
25204ac91378SJan Blunck 	error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2521be6d3e56SKentaro Takeda out_drop_write:
2522463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2523463c3197SDave Hansen out_dput:
25241da177e4SLinus Torvalds 	dput(dentry);
25256902d925SDave Hansen out_unlock:
25264ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
25271d957f9bSJan Blunck 	path_put(&nd.path);
25281da177e4SLinus Torvalds 	putname(tmp);
25296902d925SDave Hansen out_err:
25301da177e4SLinus Torvalds 	return error;
25311da177e4SLinus Torvalds }
25321da177e4SLinus Torvalds 
25333cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
25345590ff0dSUlrich Drepper {
25355590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
25365590ff0dSUlrich Drepper }
25375590ff0dSUlrich Drepper 
25381da177e4SLinus Torvalds /*
2539a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
2540a71905f0SSage Weil  * should have a usage count of 2 if we're the only user of this
2541a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
2542a71905f0SSage Weil  * then we drop the dentry now.
25431da177e4SLinus Torvalds  *
25441da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
25451da177e4SLinus Torvalds  * do a
25461da177e4SLinus Torvalds  *
25471da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
25481da177e4SLinus Torvalds  *		return -EBUSY;
25491da177e4SLinus Torvalds  *
25501da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
25511da177e4SLinus Torvalds  * that is still in use by something else..
25521da177e4SLinus Torvalds  */
25531da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
25541da177e4SLinus Torvalds {
25551da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
25561da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
255764252c75SSage Weil 	if (dentry->d_count == 1)
25581da177e4SLinus Torvalds 		__d_drop(dentry);
25591da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
25601da177e4SLinus Torvalds }
25611da177e4SLinus Torvalds 
25621da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
25631da177e4SLinus Torvalds {
25641da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
25651da177e4SLinus Torvalds 
25661da177e4SLinus Torvalds 	if (error)
25671da177e4SLinus Torvalds 		return error;
25681da177e4SLinus Torvalds 
2569acfa4380SAl Viro 	if (!dir->i_op->rmdir)
25701da177e4SLinus Torvalds 		return -EPERM;
25711da177e4SLinus Torvalds 
25721b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
2573912dbc15SSage Weil 
25741da177e4SLinus Torvalds 	error = -EBUSY;
2575912dbc15SSage Weil 	if (d_mountpoint(dentry))
2576912dbc15SSage Weil 		goto out;
2577912dbc15SSage Weil 
25781da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
2579912dbc15SSage Weil 	if (error)
2580912dbc15SSage Weil 		goto out;
2581912dbc15SSage Weil 
25823cebde24SSage Weil 	shrink_dcache_parent(dentry);
25831da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
2584912dbc15SSage Weil 	if (error)
2585912dbc15SSage Weil 		goto out;
2586912dbc15SSage Weil 
25871da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
2588d83c49f3SAl Viro 	dont_mount(dentry);
25891da177e4SLinus Torvalds 
2590912dbc15SSage Weil out:
2591912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
2592912dbc15SSage Weil 	if (!error)
2593912dbc15SSage Weil 		d_delete(dentry);
25941da177e4SLinus Torvalds 	return error;
25951da177e4SLinus Torvalds }
25961da177e4SLinus Torvalds 
25975590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
25981da177e4SLinus Torvalds {
25991da177e4SLinus Torvalds 	int error = 0;
26001da177e4SLinus Torvalds 	char * name;
26011da177e4SLinus Torvalds 	struct dentry *dentry;
26021da177e4SLinus Torvalds 	struct nameidata nd;
26031da177e4SLinus Torvalds 
26042ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
26051da177e4SLinus Torvalds 	if (error)
26062ad94ae6SAl Viro 		return error;
26071da177e4SLinus Torvalds 
26081da177e4SLinus Torvalds 	switch(nd.last_type) {
26091da177e4SLinus Torvalds 	case LAST_DOTDOT:
26101da177e4SLinus Torvalds 		error = -ENOTEMPTY;
26111da177e4SLinus Torvalds 		goto exit1;
26121da177e4SLinus Torvalds 	case LAST_DOT:
26131da177e4SLinus Torvalds 		error = -EINVAL;
26141da177e4SLinus Torvalds 		goto exit1;
26151da177e4SLinus Torvalds 	case LAST_ROOT:
26161da177e4SLinus Torvalds 		error = -EBUSY;
26171da177e4SLinus Torvalds 		goto exit1;
26181da177e4SLinus Torvalds 	}
26190612d9fbSOGAWA Hirofumi 
26200612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
26210612d9fbSOGAWA Hirofumi 
26224ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
262349705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
26241da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
26256902d925SDave Hansen 	if (IS_ERR(dentry))
26266902d925SDave Hansen 		goto exit2;
2627e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
2628e6bc45d6STheodore Ts'o 		error = -ENOENT;
2629e6bc45d6STheodore Ts'o 		goto exit3;
2630e6bc45d6STheodore Ts'o 	}
26310622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
26320622753bSDave Hansen 	if (error)
26330622753bSDave Hansen 		goto exit3;
2634be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2635be6d3e56SKentaro Takeda 	if (error)
2636be6d3e56SKentaro Takeda 		goto exit4;
26374ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2638be6d3e56SKentaro Takeda exit4:
26390622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
26400622753bSDave Hansen exit3:
26411da177e4SLinus Torvalds 	dput(dentry);
26426902d925SDave Hansen exit2:
26434ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
26441da177e4SLinus Torvalds exit1:
26451d957f9bSJan Blunck 	path_put(&nd.path);
26461da177e4SLinus Torvalds 	putname(name);
26471da177e4SLinus Torvalds 	return error;
26481da177e4SLinus Torvalds }
26491da177e4SLinus Torvalds 
26503cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
26515590ff0dSUlrich Drepper {
26525590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
26535590ff0dSUlrich Drepper }
26545590ff0dSUlrich Drepper 
26551da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
26561da177e4SLinus Torvalds {
26571da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
26581da177e4SLinus Torvalds 
26591da177e4SLinus Torvalds 	if (error)
26601da177e4SLinus Torvalds 		return error;
26611da177e4SLinus Torvalds 
2662acfa4380SAl Viro 	if (!dir->i_op->unlink)
26631da177e4SLinus Torvalds 		return -EPERM;
26641da177e4SLinus Torvalds 
26651b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
26661da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
26671da177e4SLinus Torvalds 		error = -EBUSY;
26681da177e4SLinus Torvalds 	else {
26691da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2670bec1052eSAl Viro 		if (!error) {
26711da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2672bec1052eSAl Viro 			if (!error)
2673d83c49f3SAl Viro 				dont_mount(dentry);
2674bec1052eSAl Viro 		}
26751da177e4SLinus Torvalds 	}
26761b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
26771da177e4SLinus Torvalds 
26781da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
26791da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2680ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
26811da177e4SLinus Torvalds 		d_delete(dentry);
26821da177e4SLinus Torvalds 	}
26830eeca283SRobert Love 
26841da177e4SLinus Torvalds 	return error;
26851da177e4SLinus Torvalds }
26861da177e4SLinus Torvalds 
26871da177e4SLinus Torvalds /*
26881da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
26891b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
26901da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
26911da177e4SLinus Torvalds  * while waiting on the I/O.
26921da177e4SLinus Torvalds  */
26935590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
26941da177e4SLinus Torvalds {
26952ad94ae6SAl Viro 	int error;
26961da177e4SLinus Torvalds 	char *name;
26971da177e4SLinus Torvalds 	struct dentry *dentry;
26981da177e4SLinus Torvalds 	struct nameidata nd;
26991da177e4SLinus Torvalds 	struct inode *inode = NULL;
27001da177e4SLinus Torvalds 
27012ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
27021da177e4SLinus Torvalds 	if (error)
27032ad94ae6SAl Viro 		return error;
27042ad94ae6SAl Viro 
27051da177e4SLinus Torvalds 	error = -EISDIR;
27061da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
27071da177e4SLinus Torvalds 		goto exit1;
27080612d9fbSOGAWA Hirofumi 
27090612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
27100612d9fbSOGAWA Hirofumi 
27114ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
271249705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
27131da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
27141da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
27151da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
271650338b88STörök Edwin 		if (nd.last.name[nd.last.len])
271750338b88STörök Edwin 			goto slashes;
27181da177e4SLinus Torvalds 		inode = dentry->d_inode;
271950338b88STörök Edwin 		if (!inode)
2720e6bc45d6STheodore Ts'o 			goto slashes;
27217de9c6eeSAl Viro 		ihold(inode);
27220622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
27230622753bSDave Hansen 		if (error)
27240622753bSDave Hansen 			goto exit2;
2725be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2726be6d3e56SKentaro Takeda 		if (error)
2727be6d3e56SKentaro Takeda 			goto exit3;
27284ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2729be6d3e56SKentaro Takeda exit3:
27300622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
27311da177e4SLinus Torvalds 	exit2:
27321da177e4SLinus Torvalds 		dput(dentry);
27331da177e4SLinus Torvalds 	}
27344ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
27351da177e4SLinus Torvalds 	if (inode)
27361da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
27371da177e4SLinus Torvalds exit1:
27381d957f9bSJan Blunck 	path_put(&nd.path);
27391da177e4SLinus Torvalds 	putname(name);
27401da177e4SLinus Torvalds 	return error;
27411da177e4SLinus Torvalds 
27421da177e4SLinus Torvalds slashes:
27431da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
27441da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
27451da177e4SLinus Torvalds 	goto exit2;
27461da177e4SLinus Torvalds }
27471da177e4SLinus Torvalds 
27482e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
27495590ff0dSUlrich Drepper {
27505590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
27515590ff0dSUlrich Drepper 		return -EINVAL;
27525590ff0dSUlrich Drepper 
27535590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
27545590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
27555590ff0dSUlrich Drepper 
27565590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
27575590ff0dSUlrich Drepper }
27585590ff0dSUlrich Drepper 
27593480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
27605590ff0dSUlrich Drepper {
27615590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
27625590ff0dSUlrich Drepper }
27635590ff0dSUlrich Drepper 
2764db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
27651da177e4SLinus Torvalds {
2766a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
27671da177e4SLinus Torvalds 
27681da177e4SLinus Torvalds 	if (error)
27691da177e4SLinus Torvalds 		return error;
27701da177e4SLinus Torvalds 
2771acfa4380SAl Viro 	if (!dir->i_op->symlink)
27721da177e4SLinus Torvalds 		return -EPERM;
27731da177e4SLinus Torvalds 
27741da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
27751da177e4SLinus Torvalds 	if (error)
27761da177e4SLinus Torvalds 		return error;
27771da177e4SLinus Torvalds 
27781da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2779a74574aaSStephen Smalley 	if (!error)
2780f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
27811da177e4SLinus Torvalds 	return error;
27821da177e4SLinus Torvalds }
27831da177e4SLinus Torvalds 
27842e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
27852e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
27861da177e4SLinus Torvalds {
27872ad94ae6SAl Viro 	int error;
27881da177e4SLinus Torvalds 	char *from;
27891da177e4SLinus Torvalds 	char *to;
27906902d925SDave Hansen 	struct dentry *dentry;
27916902d925SDave Hansen 	struct nameidata nd;
27921da177e4SLinus Torvalds 
27931da177e4SLinus Torvalds 	from = getname(oldname);
27941da177e4SLinus Torvalds 	if (IS_ERR(from))
27951da177e4SLinus Torvalds 		return PTR_ERR(from);
27962ad94ae6SAl Viro 
27972ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
27982ad94ae6SAl Viro 	if (error)
27996902d925SDave Hansen 		goto out_putname;
28001da177e4SLinus Torvalds 
28011da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
28021da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
28036902d925SDave Hansen 	if (IS_ERR(dentry))
28046902d925SDave Hansen 		goto out_unlock;
28056902d925SDave Hansen 
280675c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
280775c3f29dSDave Hansen 	if (error)
280875c3f29dSDave Hansen 		goto out_dput;
2809be6d3e56SKentaro Takeda 	error = security_path_symlink(&nd.path, dentry, from);
2810be6d3e56SKentaro Takeda 	if (error)
2811be6d3e56SKentaro Takeda 		goto out_drop_write;
2812db2e747bSMiklos Szeredi 	error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2813be6d3e56SKentaro Takeda out_drop_write:
281475c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
281575c3f29dSDave Hansen out_dput:
28161da177e4SLinus Torvalds 	dput(dentry);
28176902d925SDave Hansen out_unlock:
28184ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
28191d957f9bSJan Blunck 	path_put(&nd.path);
28201da177e4SLinus Torvalds 	putname(to);
28216902d925SDave Hansen out_putname:
28221da177e4SLinus Torvalds 	putname(from);
28231da177e4SLinus Torvalds 	return error;
28241da177e4SLinus Torvalds }
28251da177e4SLinus Torvalds 
28263480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
28275590ff0dSUlrich Drepper {
28285590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
28295590ff0dSUlrich Drepper }
28305590ff0dSUlrich Drepper 
28311da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
28321da177e4SLinus Torvalds {
28331da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
28341da177e4SLinus Torvalds 	int error;
28351da177e4SLinus Torvalds 
28361da177e4SLinus Torvalds 	if (!inode)
28371da177e4SLinus Torvalds 		return -ENOENT;
28381da177e4SLinus Torvalds 
2839a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
28401da177e4SLinus Torvalds 	if (error)
28411da177e4SLinus Torvalds 		return error;
28421da177e4SLinus Torvalds 
28431da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
28441da177e4SLinus Torvalds 		return -EXDEV;
28451da177e4SLinus Torvalds 
28461da177e4SLinus Torvalds 	/*
28471da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
28481da177e4SLinus Torvalds 	 */
28491da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
28501da177e4SLinus Torvalds 		return -EPERM;
2851acfa4380SAl Viro 	if (!dir->i_op->link)
28521da177e4SLinus Torvalds 		return -EPERM;
28537e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
28541da177e4SLinus Torvalds 		return -EPERM;
28551da177e4SLinus Torvalds 
28561da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
28571da177e4SLinus Torvalds 	if (error)
28581da177e4SLinus Torvalds 		return error;
28591da177e4SLinus Torvalds 
28607e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
2861aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
2862aae8a97dSAneesh Kumar K.V 	if (inode->i_nlink == 0)
2863aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
2864aae8a97dSAneesh Kumar K.V 	else
28651da177e4SLinus Torvalds 		error = dir->i_op->link(old_dentry, dir, new_dentry);
28667e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
2867e31e14ecSStephen Smalley 	if (!error)
28687e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
28691da177e4SLinus Torvalds 	return error;
28701da177e4SLinus Torvalds }
28711da177e4SLinus Torvalds 
28721da177e4SLinus Torvalds /*
28731da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
28741da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
28751da177e4SLinus Torvalds  * newname.  --KAB
28761da177e4SLinus Torvalds  *
28771da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
28781da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
28791da177e4SLinus Torvalds  * and other special files.  --ADM
28801da177e4SLinus Torvalds  */
28812e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
28822e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
28831da177e4SLinus Torvalds {
28841da177e4SLinus Torvalds 	struct dentry *new_dentry;
28852d8f3038SAl Viro 	struct nameidata nd;
28862d8f3038SAl Viro 	struct path old_path;
288711a7b371SAneesh Kumar K.V 	int how = 0;
28881da177e4SLinus Torvalds 	int error;
28891da177e4SLinus Torvalds 	char *to;
28901da177e4SLinus Torvalds 
289111a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
2892c04030e1SUlrich Drepper 		return -EINVAL;
289311a7b371SAneesh Kumar K.V 	/*
289411a7b371SAneesh Kumar K.V 	 * To use null names we require CAP_DAC_READ_SEARCH
289511a7b371SAneesh Kumar K.V 	 * This ensures that not everyone will be able to create
289611a7b371SAneesh Kumar K.V 	 * handlink using the passed filedescriptor.
289711a7b371SAneesh Kumar K.V 	 */
289811a7b371SAneesh Kumar K.V 	if (flags & AT_EMPTY_PATH) {
289911a7b371SAneesh Kumar K.V 		if (!capable(CAP_DAC_READ_SEARCH))
290011a7b371SAneesh Kumar K.V 			return -ENOENT;
290111a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
290211a7b371SAneesh Kumar K.V 	}
2903c04030e1SUlrich Drepper 
290411a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
290511a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
290611a7b371SAneesh Kumar K.V 
290711a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
29081da177e4SLinus Torvalds 	if (error)
29092ad94ae6SAl Viro 		return error;
29102ad94ae6SAl Viro 
29112ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
29121da177e4SLinus Torvalds 	if (error)
29131da177e4SLinus Torvalds 		goto out;
29141da177e4SLinus Torvalds 	error = -EXDEV;
29152d8f3038SAl Viro 	if (old_path.mnt != nd.path.mnt)
29161da177e4SLinus Torvalds 		goto out_release;
29171da177e4SLinus Torvalds 	new_dentry = lookup_create(&nd, 0);
29181da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
29196902d925SDave Hansen 	if (IS_ERR(new_dentry))
29206902d925SDave Hansen 		goto out_unlock;
292175c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
292275c3f29dSDave Hansen 	if (error)
292375c3f29dSDave Hansen 		goto out_dput;
2924be6d3e56SKentaro Takeda 	error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2925be6d3e56SKentaro Takeda 	if (error)
2926be6d3e56SKentaro Takeda 		goto out_drop_write;
29272d8f3038SAl Viro 	error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
2928be6d3e56SKentaro Takeda out_drop_write:
292975c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
293075c3f29dSDave Hansen out_dput:
29311da177e4SLinus Torvalds 	dput(new_dentry);
29326902d925SDave Hansen out_unlock:
29334ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
29341da177e4SLinus Torvalds out_release:
29351d957f9bSJan Blunck 	path_put(&nd.path);
29362ad94ae6SAl Viro 	putname(to);
29371da177e4SLinus Torvalds out:
29382d8f3038SAl Viro 	path_put(&old_path);
29391da177e4SLinus Torvalds 
29401da177e4SLinus Torvalds 	return error;
29411da177e4SLinus Torvalds }
29421da177e4SLinus Torvalds 
29433480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
29445590ff0dSUlrich Drepper {
2945c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
29465590ff0dSUlrich Drepper }
29475590ff0dSUlrich Drepper 
29481da177e4SLinus Torvalds /*
29491da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
29501da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
29511da177e4SLinus Torvalds  * Problems:
29521da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
29531da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
29541da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
2955a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
29561da177e4SLinus Torvalds  *	   story.
29571da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
29581b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
29591da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
29601da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
2961a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
29621da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
29631da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
29641da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
2965a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
29661da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
29671da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
29681da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
2969e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
29701b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
29711da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2972c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
29731da177e4SLinus Torvalds  *	   locking].
29741da177e4SLinus Torvalds  */
297575c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
29761da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
29771da177e4SLinus Torvalds {
29781da177e4SLinus Torvalds 	int error = 0;
29799055cba7SSage Weil 	struct inode *target = new_dentry->d_inode;
29801da177e4SLinus Torvalds 
29811da177e4SLinus Torvalds 	/*
29821da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
29831da177e4SLinus Torvalds 	 * we'll need to flip '..'.
29841da177e4SLinus Torvalds 	 */
29851da177e4SLinus Torvalds 	if (new_dir != old_dir) {
2986f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
29871da177e4SLinus Torvalds 		if (error)
29881da177e4SLinus Torvalds 			return error;
29891da177e4SLinus Torvalds 	}
29901da177e4SLinus Torvalds 
29911da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
29921da177e4SLinus Torvalds 	if (error)
29931da177e4SLinus Torvalds 		return error;
29941da177e4SLinus Torvalds 
2995d83c49f3SAl Viro 	if (target)
29961b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
29979055cba7SSage Weil 
29981da177e4SLinus Torvalds 	error = -EBUSY;
29999055cba7SSage Weil 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
30009055cba7SSage Weil 		goto out;
30019055cba7SSage Weil 
30023cebde24SSage Weil 	if (target)
30033cebde24SSage Weil 		shrink_dcache_parent(new_dentry);
30041da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
30059055cba7SSage Weil 	if (error)
30069055cba7SSage Weil 		goto out;
30079055cba7SSage Weil 
30081da177e4SLinus Torvalds 	if (target) {
30091da177e4SLinus Torvalds 		target->i_flags |= S_DEAD;
3010d83c49f3SAl Viro 		dont_mount(new_dentry);
3011d83c49f3SAl Viro 	}
30129055cba7SSage Weil out:
30139055cba7SSage Weil 	if (target)
30141b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
3015e31e14ecSStephen Smalley 	if (!error)
3016349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30171da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
30181da177e4SLinus Torvalds 	return error;
30191da177e4SLinus Torvalds }
30201da177e4SLinus Torvalds 
302175c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
30221da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
30231da177e4SLinus Torvalds {
302451892bbbSSage Weil 	struct inode *target = new_dentry->d_inode;
30251da177e4SLinus Torvalds 	int error;
30261da177e4SLinus Torvalds 
30271da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30281da177e4SLinus Torvalds 	if (error)
30291da177e4SLinus Torvalds 		return error;
30301da177e4SLinus Torvalds 
30311da177e4SLinus Torvalds 	dget(new_dentry);
30321da177e4SLinus Torvalds 	if (target)
30331b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
303451892bbbSSage Weil 
30351da177e4SLinus Torvalds 	error = -EBUSY;
303651892bbbSSage Weil 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
303751892bbbSSage Weil 		goto out;
303851892bbbSSage Weil 
30391da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
304051892bbbSSage Weil 	if (error)
304151892bbbSSage Weil 		goto out;
304251892bbbSSage Weil 
3043bec1052eSAl Viro 	if (target)
3044d83c49f3SAl Viro 		dont_mount(new_dentry);
3045349457ccSMark Fasheh 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30461da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
304751892bbbSSage Weil out:
30481da177e4SLinus Torvalds 	if (target)
30491b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
30501da177e4SLinus Torvalds 	dput(new_dentry);
30511da177e4SLinus Torvalds 	return error;
30521da177e4SLinus Torvalds }
30531da177e4SLinus Torvalds 
30541da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
30551da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
30561da177e4SLinus Torvalds {
30571da177e4SLinus Torvalds 	int error;
30581da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
305959b0df21SEric Paris 	const unsigned char *old_name;
30601da177e4SLinus Torvalds 
30611da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
30621da177e4SLinus Torvalds  		return 0;
30631da177e4SLinus Torvalds 
30641da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
30651da177e4SLinus Torvalds 	if (error)
30661da177e4SLinus Torvalds 		return error;
30671da177e4SLinus Torvalds 
30681da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3069a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
30701da177e4SLinus Torvalds 	else
30711da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
30721da177e4SLinus Torvalds 	if (error)
30731da177e4SLinus Torvalds 		return error;
30741da177e4SLinus Torvalds 
3075acfa4380SAl Viro 	if (!old_dir->i_op->rename)
30761da177e4SLinus Torvalds 		return -EPERM;
30771da177e4SLinus Torvalds 
30780eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
30790eeca283SRobert Love 
30801da177e4SLinus Torvalds 	if (is_dir)
30811da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
30821da177e4SLinus Torvalds 	else
30831da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3084123df294SAl Viro 	if (!error)
3085123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
30865a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
30870eeca283SRobert Love 	fsnotify_oldname_free(old_name);
30880eeca283SRobert Love 
30891da177e4SLinus Torvalds 	return error;
30901da177e4SLinus Torvalds }
30911da177e4SLinus Torvalds 
30922e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
30932e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
30941da177e4SLinus Torvalds {
30951da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
30961da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
30971da177e4SLinus Torvalds 	struct dentry *trap;
30981da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
30992ad94ae6SAl Viro 	char *from;
31002ad94ae6SAl Viro 	char *to;
31012ad94ae6SAl Viro 	int error;
31021da177e4SLinus Torvalds 
31032ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
31041da177e4SLinus Torvalds 	if (error)
31051da177e4SLinus Torvalds 		goto exit;
31061da177e4SLinus Torvalds 
31072ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
31081da177e4SLinus Torvalds 	if (error)
31091da177e4SLinus Torvalds 		goto exit1;
31101da177e4SLinus Torvalds 
31111da177e4SLinus Torvalds 	error = -EXDEV;
31124ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
31131da177e4SLinus Torvalds 		goto exit2;
31141da177e4SLinus Torvalds 
31154ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
31161da177e4SLinus Torvalds 	error = -EBUSY;
31171da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
31181da177e4SLinus Torvalds 		goto exit2;
31191da177e4SLinus Torvalds 
31204ac91378SJan Blunck 	new_dir = newnd.path.dentry;
31211da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
31221da177e4SLinus Torvalds 		goto exit2;
31231da177e4SLinus Torvalds 
31240612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
31250612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
31264e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
31270612d9fbSOGAWA Hirofumi 
31281da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
31291da177e4SLinus Torvalds 
313049705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
31311da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
31321da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
31331da177e4SLinus Torvalds 		goto exit3;
31341da177e4SLinus Torvalds 	/* source must exist */
31351da177e4SLinus Torvalds 	error = -ENOENT;
31361da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
31371da177e4SLinus Torvalds 		goto exit4;
31381da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
31391da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
31401da177e4SLinus Torvalds 		error = -ENOTDIR;
31411da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
31421da177e4SLinus Torvalds 			goto exit4;
31431da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
31441da177e4SLinus Torvalds 			goto exit4;
31451da177e4SLinus Torvalds 	}
31461da177e4SLinus Torvalds 	/* source should not be ancestor of target */
31471da177e4SLinus Torvalds 	error = -EINVAL;
31481da177e4SLinus Torvalds 	if (old_dentry == trap)
31491da177e4SLinus Torvalds 		goto exit4;
315049705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
31511da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
31521da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
31531da177e4SLinus Torvalds 		goto exit4;
31541da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
31551da177e4SLinus Torvalds 	error = -ENOTEMPTY;
31561da177e4SLinus Torvalds 	if (new_dentry == trap)
31571da177e4SLinus Torvalds 		goto exit5;
31581da177e4SLinus Torvalds 
31599079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
31609079b1ebSDave Hansen 	if (error)
31619079b1ebSDave Hansen 		goto exit5;
3162be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3163be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3164be6d3e56SKentaro Takeda 	if (error)
3165be6d3e56SKentaro Takeda 		goto exit6;
31661da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
31671da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3168be6d3e56SKentaro Takeda exit6:
31699079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
31701da177e4SLinus Torvalds exit5:
31711da177e4SLinus Torvalds 	dput(new_dentry);
31721da177e4SLinus Torvalds exit4:
31731da177e4SLinus Torvalds 	dput(old_dentry);
31741da177e4SLinus Torvalds exit3:
31751da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
31761da177e4SLinus Torvalds exit2:
31771d957f9bSJan Blunck 	path_put(&newnd.path);
31782ad94ae6SAl Viro 	putname(to);
31791da177e4SLinus Torvalds exit1:
31801d957f9bSJan Blunck 	path_put(&oldnd.path);
31811da177e4SLinus Torvalds 	putname(from);
31822ad94ae6SAl Viro exit:
31831da177e4SLinus Torvalds 	return error;
31841da177e4SLinus Torvalds }
31851da177e4SLinus Torvalds 
3186a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
31875590ff0dSUlrich Drepper {
31885590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
31895590ff0dSUlrich Drepper }
31905590ff0dSUlrich Drepper 
31911da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
31921da177e4SLinus Torvalds {
31931da177e4SLinus Torvalds 	int len;
31941da177e4SLinus Torvalds 
31951da177e4SLinus Torvalds 	len = PTR_ERR(link);
31961da177e4SLinus Torvalds 	if (IS_ERR(link))
31971da177e4SLinus Torvalds 		goto out;
31981da177e4SLinus Torvalds 
31991da177e4SLinus Torvalds 	len = strlen(link);
32001da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
32011da177e4SLinus Torvalds 		len = buflen;
32021da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
32031da177e4SLinus Torvalds 		len = -EFAULT;
32041da177e4SLinus Torvalds out:
32051da177e4SLinus Torvalds 	return len;
32061da177e4SLinus Torvalds }
32071da177e4SLinus Torvalds 
32081da177e4SLinus Torvalds /*
32091da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
32101da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
32111da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
32121da177e4SLinus Torvalds  */
32131da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
32141da177e4SLinus Torvalds {
32151da177e4SLinus Torvalds 	struct nameidata nd;
3216cc314eefSLinus Torvalds 	void *cookie;
3217694a1764SMarcin Slusarz 	int res;
3218cc314eefSLinus Torvalds 
32191da177e4SLinus Torvalds 	nd.depth = 0;
3220cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3221694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3222694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3223694a1764SMarcin Slusarz 
3224694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
32251da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3226cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3227694a1764SMarcin Slusarz 	return res;
32281da177e4SLinus Torvalds }
32291da177e4SLinus Torvalds 
32301da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
32311da177e4SLinus Torvalds {
32321da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
32331da177e4SLinus Torvalds }
32341da177e4SLinus Torvalds 
32351da177e4SLinus Torvalds /* get the link contents into pagecache */
32361da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
32371da177e4SLinus Torvalds {
3238ebd09abbSDuane Griffin 	char *kaddr;
32391da177e4SLinus Torvalds 	struct page *page;
32401da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3241090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
32421da177e4SLinus Torvalds 	if (IS_ERR(page))
32436fe6900eSNick Piggin 		return (char*)page;
32441da177e4SLinus Torvalds 	*ppage = page;
3245ebd09abbSDuane Griffin 	kaddr = kmap(page);
3246ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3247ebd09abbSDuane Griffin 	return kaddr;
32481da177e4SLinus Torvalds }
32491da177e4SLinus Torvalds 
32501da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
32511da177e4SLinus Torvalds {
32521da177e4SLinus Torvalds 	struct page *page = NULL;
32531da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
32541da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
32551da177e4SLinus Torvalds 	if (page) {
32561da177e4SLinus Torvalds 		kunmap(page);
32571da177e4SLinus Torvalds 		page_cache_release(page);
32581da177e4SLinus Torvalds 	}
32591da177e4SLinus Torvalds 	return res;
32601da177e4SLinus Torvalds }
32611da177e4SLinus Torvalds 
3262cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
32631da177e4SLinus Torvalds {
3264cc314eefSLinus Torvalds 	struct page *page = NULL;
32651da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3266cc314eefSLinus Torvalds 	return page;
32671da177e4SLinus Torvalds }
32681da177e4SLinus Torvalds 
3269cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
32701da177e4SLinus Torvalds {
3271cc314eefSLinus Torvalds 	struct page *page = cookie;
3272cc314eefSLinus Torvalds 
3273cc314eefSLinus Torvalds 	if (page) {
32741da177e4SLinus Torvalds 		kunmap(page);
32751da177e4SLinus Torvalds 		page_cache_release(page);
32761da177e4SLinus Torvalds 	}
32771da177e4SLinus Torvalds }
32781da177e4SLinus Torvalds 
327954566b2cSNick Piggin /*
328054566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
328154566b2cSNick Piggin  */
328254566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
32831da177e4SLinus Torvalds {
32841da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
32850adb25d2SKirill Korotaev 	struct page *page;
3286afddba49SNick Piggin 	void *fsdata;
3287beb497abSDmitriy Monakhov 	int err;
32881da177e4SLinus Torvalds 	char *kaddr;
328954566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
329054566b2cSNick Piggin 	if (nofs)
329154566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
32921da177e4SLinus Torvalds 
32937e53cac4SNeilBrown retry:
3294afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
329554566b2cSNick Piggin 				flags, &page, &fsdata);
32961da177e4SLinus Torvalds 	if (err)
3297afddba49SNick Piggin 		goto fail;
3298afddba49SNick Piggin 
32991da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
33001da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
33011da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
3302afddba49SNick Piggin 
3303afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3304afddba49SNick Piggin 							page, fsdata);
33051da177e4SLinus Torvalds 	if (err < 0)
33061da177e4SLinus Torvalds 		goto fail;
3307afddba49SNick Piggin 	if (err < len-1)
3308afddba49SNick Piggin 		goto retry;
3309afddba49SNick Piggin 
33101da177e4SLinus Torvalds 	mark_inode_dirty(inode);
33111da177e4SLinus Torvalds 	return 0;
33121da177e4SLinus Torvalds fail:
33131da177e4SLinus Torvalds 	return err;
33141da177e4SLinus Torvalds }
33151da177e4SLinus Torvalds 
33160adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
33170adb25d2SKirill Korotaev {
33180adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
331954566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
33200adb25d2SKirill Korotaev }
33210adb25d2SKirill Korotaev 
332292e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
33231da177e4SLinus Torvalds 	.readlink	= generic_readlink,
33241da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
33251da177e4SLinus Torvalds 	.put_link	= page_put_link,
33261da177e4SLinus Torvalds };
33271da177e4SLinus Torvalds 
33282d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
3329cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
33301da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
33311da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
33321da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
33331da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
33341da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
33351da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
33361da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
33371da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
33381da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
33390adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
33401da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
33411da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
3342c9c6cac0SAl Viro EXPORT_SYMBOL(kern_path_parent);
3343d1811465SAl Viro EXPORT_SYMBOL(kern_path);
334416f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3345f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
33468c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission);
33471da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
33481da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
33491da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
33501da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
33511da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
33521da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
33531da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
33541da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
33551da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
33561da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
33571da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
33581da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
33591da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
33601da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3361