xref: /openbmc/linux/fs/namei.c (revision 90dbb77b)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/namei.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  * Some corrections by tytso.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
121da177e4SLinus Torvalds  * lookup logic.
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/init.h>
181da177e4SLinus Torvalds #include <linux/module.h>
191da177e4SLinus Torvalds #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/fs.h>
211da177e4SLinus Torvalds #include <linux/namei.h>
221da177e4SLinus Torvalds #include <linux/pagemap.h>
230eeca283SRobert Love #include <linux/fsnotify.h>
241da177e4SLinus Torvalds #include <linux/personality.h>
251da177e4SLinus Torvalds #include <linux/security.h>
266146f0d5SMimi Zohar #include <linux/ima.h>
271da177e4SLinus Torvalds #include <linux/syscalls.h>
281da177e4SLinus Torvalds #include <linux/mount.h>
291da177e4SLinus Torvalds #include <linux/audit.h>
3016f7e0feSRandy Dunlap #include <linux/capability.h>
31834f2a4aSTrond Myklebust #include <linux/file.h>
325590ff0dSUlrich Drepper #include <linux/fcntl.h>
3308ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h>
345ad4e53bSAl Viro #include <linux/fs_struct.h>
351da177e4SLinus Torvalds #include <asm/uaccess.h>
361da177e4SLinus Torvalds 
37e81e3f4dSEric Paris #include "internal.h"
38e81e3f4dSEric Paris 
391da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer]
401da177e4SLinus Torvalds  * Fundamental changes in the pathname lookup mechanisms (namei)
411da177e4SLinus Torvalds  * were necessary because of omirr.  The reason is that omirr needs
421da177e4SLinus Torvalds  * to know the _real_ pathname, not the user-supplied one, in case
431da177e4SLinus Torvalds  * of symlinks (and also when transname replacements occur).
441da177e4SLinus Torvalds  *
451da177e4SLinus Torvalds  * The new code replaces the old recursive symlink resolution with
461da177e4SLinus Torvalds  * an iterative one (in case of non-nested symlink chains).  It does
471da177e4SLinus Torvalds  * this with calls to <fs>_follow_link().
481da177e4SLinus Torvalds  * As a side effect, dir_namei(), _namei() and follow_link() are now
491da177e4SLinus Torvalds  * replaced with a single function lookup_dentry() that can handle all
501da177e4SLinus Torvalds  * the special cases of the former code.
511da177e4SLinus Torvalds  *
521da177e4SLinus Torvalds  * With the new dcache, the pathname is stored at each inode, at least as
531da177e4SLinus Torvalds  * long as the refcount of the inode is positive.  As a side effect, the
541da177e4SLinus Torvalds  * size of the dcache depends on the inode cache and thus is dynamic.
551da177e4SLinus Torvalds  *
561da177e4SLinus Torvalds  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
571da177e4SLinus Torvalds  * resolution to correspond with current state of the code.
581da177e4SLinus Torvalds  *
591da177e4SLinus Torvalds  * Note that the symlink resolution is not *completely* iterative.
601da177e4SLinus Torvalds  * There is still a significant amount of tail- and mid- recursion in
611da177e4SLinus Torvalds  * the algorithm.  Also, note that <fs>_readlink() is not used in
621da177e4SLinus Torvalds  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
631da177e4SLinus Torvalds  * may return different results than <fs>_follow_link().  Many virtual
641da177e4SLinus Torvalds  * filesystems (including /proc) exhibit this behavior.
651da177e4SLinus Torvalds  */
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
681da177e4SLinus Torvalds  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
691da177e4SLinus Torvalds  * and the name already exists in form of a symlink, try to create the new
701da177e4SLinus Torvalds  * name indicated by the symlink. The old code always complained that the
711da177e4SLinus Torvalds  * name already exists, due to not following the symlink even if its target
721da177e4SLinus Torvalds  * is nonexistent.  The new semantics affects also mknod() and link() when
731da177e4SLinus Torvalds  * the name is a symlink pointing to a non-existant name.
741da177e4SLinus Torvalds  *
751da177e4SLinus Torvalds  * I don't know which semantics is the right one, since I have no access
761da177e4SLinus Torvalds  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
771da177e4SLinus Torvalds  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
781da177e4SLinus Torvalds  * "old" one. Personally, I think the new semantics is much more logical.
791da177e4SLinus Torvalds  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
801da177e4SLinus Torvalds  * file does succeed in both HP-UX and SunOs, but not in Solaris
811da177e4SLinus Torvalds  * and in the old Linux semantics.
821da177e4SLinus Torvalds  */
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
851da177e4SLinus Torvalds  * semantics.  See the comments in "open_namei" and "do_link" below.
861da177e4SLinus Torvalds  *
871da177e4SLinus Torvalds  * [10-Sep-98 Alan Modra] Another symlink change.
881da177e4SLinus Torvalds  */
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
911da177e4SLinus Torvalds  *	inside the path - always follow.
921da177e4SLinus Torvalds  *	in the last component in creation/removal/renaming - never follow.
931da177e4SLinus Torvalds  *	if LOOKUP_FOLLOW passed - follow.
941da177e4SLinus Torvalds  *	if the pathname has trailing slashes - follow.
951da177e4SLinus Torvalds  *	otherwise - don't follow.
961da177e4SLinus Torvalds  * (applied in that order).
971da177e4SLinus Torvalds  *
981da177e4SLinus Torvalds  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
991da177e4SLinus Torvalds  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
1001da177e4SLinus Torvalds  * During the 2.4 we need to fix the userland stuff depending on it -
1011da177e4SLinus Torvalds  * hopefully we will be able to get rid of that wart in 2.5. So far only
1021da177e4SLinus Torvalds  * XEmacs seems to be relying on it...
1031da177e4SLinus Torvalds  */
1041da177e4SLinus Torvalds /*
1051da177e4SLinus Torvalds  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
106a11f3a05SArjan van de Ven  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
1071da177e4SLinus Torvalds  * any extra contention...
1081da177e4SLinus Torvalds  */
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional
1111da177e4SLinus Torvalds  * checking and hopefully speeding things up, we copy filenames to the
1121da177e4SLinus Torvalds  * kernel data space before using them..
1131da177e4SLinus Torvalds  *
1141da177e4SLinus Torvalds  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
1151da177e4SLinus Torvalds  * PATH_MAX includes the nul terminator --RR.
1161da177e4SLinus Torvalds  */
117858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page)
1181da177e4SLinus Torvalds {
1191da177e4SLinus Torvalds 	int retval;
1201da177e4SLinus Torvalds 	unsigned long len = PATH_MAX;
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	if (!segment_eq(get_fs(), KERNEL_DS)) {
1231da177e4SLinus Torvalds 		if ((unsigned long) filename >= TASK_SIZE)
1241da177e4SLinus Torvalds 			return -EFAULT;
1251da177e4SLinus Torvalds 		if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
1261da177e4SLinus Torvalds 			len = TASK_SIZE - (unsigned long) filename;
1271da177e4SLinus Torvalds 	}
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	retval = strncpy_from_user(page, filename, len);
1301da177e4SLinus Torvalds 	if (retval > 0) {
1311da177e4SLinus Torvalds 		if (retval < len)
1321da177e4SLinus Torvalds 			return 0;
1331da177e4SLinus Torvalds 		return -ENAMETOOLONG;
1341da177e4SLinus Torvalds 	} else if (!retval)
1351da177e4SLinus Torvalds 		retval = -ENOENT;
1361da177e4SLinus Torvalds 	return retval;
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds char * getname(const char __user * filename)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	char *tmp, *result;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	result = ERR_PTR(-ENOMEM);
1441da177e4SLinus Torvalds 	tmp = __getname();
1451da177e4SLinus Torvalds 	if (tmp)  {
1461da177e4SLinus Torvalds 		int retval = do_getname(filename, tmp);
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 		result = tmp;
1491da177e4SLinus Torvalds 		if (retval < 0) {
1501da177e4SLinus Torvalds 			__putname(tmp);
1511da177e4SLinus Torvalds 			result = ERR_PTR(retval);
1521da177e4SLinus Torvalds 		}
1531da177e4SLinus Torvalds 	}
1541da177e4SLinus Torvalds 	audit_getname(result);
1551da177e4SLinus Torvalds 	return result;
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
1591da177e4SLinus Torvalds void putname(const char *name)
1601da177e4SLinus Torvalds {
1615ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
1621da177e4SLinus Torvalds 		audit_putname(name);
1631da177e4SLinus Torvalds 	else
1641da177e4SLinus Torvalds 		__putname(name);
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds EXPORT_SYMBOL(putname);
1671da177e4SLinus Torvalds #endif
1681da177e4SLinus Torvalds 
1695909ccaaSLinus Torvalds /*
1705909ccaaSLinus Torvalds  * This does basic POSIX ACL permission checking
1715909ccaaSLinus Torvalds  */
172b74c79e9SNick Piggin static int acl_permission_check(struct inode *inode, int mask, unsigned int flags,
173b74c79e9SNick Piggin 		int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
1745909ccaaSLinus Torvalds {
1755909ccaaSLinus Torvalds 	umode_t			mode = inode->i_mode;
1765909ccaaSLinus Torvalds 
1775909ccaaSLinus Torvalds 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
1785909ccaaSLinus Torvalds 
1795909ccaaSLinus Torvalds 	if (current_fsuid() == inode->i_uid)
1805909ccaaSLinus Torvalds 		mode >>= 6;
1815909ccaaSLinus Torvalds 	else {
1825909ccaaSLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
183b74c79e9SNick Piggin 			int error = check_acl(inode, mask, flags);
1845909ccaaSLinus Torvalds 			if (error != -EAGAIN)
1855909ccaaSLinus Torvalds 				return error;
1865909ccaaSLinus Torvalds 		}
1875909ccaaSLinus Torvalds 
1885909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
1895909ccaaSLinus Torvalds 			mode >>= 3;
1905909ccaaSLinus Torvalds 	}
1915909ccaaSLinus Torvalds 
1925909ccaaSLinus Torvalds 	/*
1935909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
1945909ccaaSLinus Torvalds 	 */
1955909ccaaSLinus Torvalds 	if ((mask & ~mode) == 0)
1965909ccaaSLinus Torvalds 		return 0;
1975909ccaaSLinus Torvalds 	return -EACCES;
1985909ccaaSLinus Torvalds }
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds /**
2011da177e4SLinus Torvalds  * generic_permission -  check for access rights on a Posix-like filesystem
2021da177e4SLinus Torvalds  * @inode:	inode to check access rights for
2031da177e4SLinus Torvalds  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
2041da177e4SLinus Torvalds  * @check_acl:	optional callback to check for Posix ACLs
205b74c79e9SNick Piggin  * @flags	IPERM_FLAG_ flags.
2061da177e4SLinus Torvalds  *
2071da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
2081da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
2091da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
210b74c79e9SNick Piggin  * are used for other things.
211b74c79e9SNick Piggin  *
212b74c79e9SNick Piggin  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
213b74c79e9SNick Piggin  * request cannot be satisfied (eg. requires blocking or too much complexity).
214b74c79e9SNick Piggin  * It would then be called again in ref-walk mode.
2151da177e4SLinus Torvalds  */
216b74c79e9SNick Piggin int generic_permission(struct inode *inode, int mask, unsigned int flags,
217b74c79e9SNick Piggin 	int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
2181da177e4SLinus Torvalds {
2195909ccaaSLinus Torvalds 	int ret;
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	/*
2225909ccaaSLinus Torvalds 	 * Do the basic POSIX ACL permission checks.
2231da177e4SLinus Torvalds 	 */
224b74c79e9SNick Piggin 	ret = acl_permission_check(inode, mask, flags, check_acl);
2255909ccaaSLinus Torvalds 	if (ret != -EACCES)
2265909ccaaSLinus Torvalds 		return ret;
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	/*
2291da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
2301da177e4SLinus Torvalds 	 * Executable DACs are overridable if at least one exec bit is set.
2311da177e4SLinus Torvalds 	 */
232f696a365SMiklos Szeredi 	if (!(mask & MAY_EXEC) || execute_ok(inode))
2331da177e4SLinus Torvalds 		if (capable(CAP_DAC_OVERRIDE))
2341da177e4SLinus Torvalds 			return 0;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	/*
2371da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
2381da177e4SLinus Torvalds 	 */
2397ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
2401da177e4SLinus Torvalds 	if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
2411da177e4SLinus Torvalds 		if (capable(CAP_DAC_READ_SEARCH))
2421da177e4SLinus Torvalds 			return 0;
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	return -EACCES;
2451da177e4SLinus Torvalds }
2461da177e4SLinus Torvalds 
247cb23beb5SChristoph Hellwig /**
248cb23beb5SChristoph Hellwig  * inode_permission  -  check for access rights to a given inode
249cb23beb5SChristoph Hellwig  * @inode:	inode to check permission on
250cb23beb5SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
251cb23beb5SChristoph Hellwig  *
252cb23beb5SChristoph Hellwig  * Used to check for read/write/execute permissions on an inode.
253cb23beb5SChristoph Hellwig  * We use "fsuid" for this, letting us set arbitrary permissions
254cb23beb5SChristoph Hellwig  * for filesystem access without changing the "normal" uids which
255cb23beb5SChristoph Hellwig  * are used for other things.
256cb23beb5SChristoph Hellwig  */
257f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask)
2581da177e4SLinus Torvalds {
259e6305c43SAl Viro 	int retval;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	if (mask & MAY_WRITE) {
26222590e41SMiklos Szeredi 		umode_t mode = inode->i_mode;
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 		/*
2651da177e4SLinus Torvalds 		 * Nobody gets write access to a read-only fs.
2661da177e4SLinus Torvalds 		 */
2671da177e4SLinus Torvalds 		if (IS_RDONLY(inode) &&
2681da177e4SLinus Torvalds 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
2691da177e4SLinus Torvalds 			return -EROFS;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 		/*
2721da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
2731da177e4SLinus Torvalds 		 */
2741da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
2751da177e4SLinus Torvalds 			return -EACCES;
2761da177e4SLinus Torvalds 	}
2771da177e4SLinus Torvalds 
278acfa4380SAl Viro 	if (inode->i_op->permission)
279b74c79e9SNick Piggin 		retval = inode->i_op->permission(inode, mask, 0);
280f696a365SMiklos Szeredi 	else
281b74c79e9SNick Piggin 		retval = generic_permission(inode, mask, 0,
282b74c79e9SNick Piggin 				inode->i_op->check_acl);
283f696a365SMiklos Szeredi 
2841da177e4SLinus Torvalds 	if (retval)
2851da177e4SLinus Torvalds 		return retval;
2861da177e4SLinus Torvalds 
28708ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
28808ce5f16SSerge E. Hallyn 	if (retval)
28908ce5f16SSerge E. Hallyn 		return retval;
29008ce5f16SSerge E. Hallyn 
291d09ca739SEric Paris 	return security_inode_permission(inode, mask);
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
294e4543eddSChristoph Hellwig /**
2958c744fb8SChristoph Hellwig  * file_permission  -  check for additional access rights to a given file
2968c744fb8SChristoph Hellwig  * @file:	file to check access rights for
2978c744fb8SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
2988c744fb8SChristoph Hellwig  *
2998c744fb8SChristoph Hellwig  * Used to check for read/write/execute permissions on an already opened
3008c744fb8SChristoph Hellwig  * file.
3018c744fb8SChristoph Hellwig  *
3028c744fb8SChristoph Hellwig  * Note:
3038c744fb8SChristoph Hellwig  *	Do not use this function in new code.  All access checks should
304cb23beb5SChristoph Hellwig  *	be done using inode_permission().
3058c744fb8SChristoph Hellwig  */
3068c744fb8SChristoph Hellwig int file_permission(struct file *file, int mask)
3078c744fb8SChristoph Hellwig {
308f419a2e3SAl Viro 	return inode_permission(file->f_path.dentry->d_inode, mask);
3098c744fb8SChristoph Hellwig }
3108c744fb8SChristoph Hellwig 
3111da177e4SLinus Torvalds /*
3121da177e4SLinus Torvalds  * get_write_access() gets write permission for a file.
3131da177e4SLinus Torvalds  * put_write_access() releases this write permission.
3141da177e4SLinus Torvalds  * This is used for regular files.
3151da177e4SLinus Torvalds  * We cannot support write (and maybe mmap read-write shared) accesses and
3161da177e4SLinus Torvalds  * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
3171da177e4SLinus Torvalds  * can have the following values:
3181da177e4SLinus Torvalds  * 0: no writers, no VM_DENYWRITE mappings
3191da177e4SLinus Torvalds  * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
3201da177e4SLinus Torvalds  * > 0: (i_writecount) users are writing to the file.
3211da177e4SLinus Torvalds  *
3221da177e4SLinus Torvalds  * Normally we operate on that counter with atomic_{inc,dec} and it's safe
3231da177e4SLinus Torvalds  * except for the cases where we don't hold i_writecount yet. Then we need to
3241da177e4SLinus Torvalds  * use {get,deny}_write_access() - these functions check the sign and refuse
3251da177e4SLinus Torvalds  * to do the change if sign is wrong. Exclusion between them is provided by
3261da177e4SLinus Torvalds  * the inode->i_lock spinlock.
3271da177e4SLinus Torvalds  */
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds int get_write_access(struct inode * inode)
3301da177e4SLinus Torvalds {
3311da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
3321da177e4SLinus Torvalds 	if (atomic_read(&inode->i_writecount) < 0) {
3331da177e4SLinus Torvalds 		spin_unlock(&inode->i_lock);
3341da177e4SLinus Torvalds 		return -ETXTBSY;
3351da177e4SLinus Torvalds 	}
3361da177e4SLinus Torvalds 	atomic_inc(&inode->i_writecount);
3371da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	return 0;
3401da177e4SLinus Torvalds }
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds int deny_write_access(struct file * file)
3431da177e4SLinus Torvalds {
3440f7fc9e4SJosef "Jeff" Sipek 	struct inode *inode = file->f_path.dentry->d_inode;
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
3471da177e4SLinus Torvalds 	if (atomic_read(&inode->i_writecount) > 0) {
3481da177e4SLinus Torvalds 		spin_unlock(&inode->i_lock);
3491da177e4SLinus Torvalds 		return -ETXTBSY;
3501da177e4SLinus Torvalds 	}
3511da177e4SLinus Torvalds 	atomic_dec(&inode->i_writecount);
3521da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 	return 0;
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
3571d957f9bSJan Blunck /**
3585dd784d0SJan Blunck  * path_get - get a reference to a path
3595dd784d0SJan Blunck  * @path: path to get the reference to
3605dd784d0SJan Blunck  *
3615dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
3625dd784d0SJan Blunck  */
3635dd784d0SJan Blunck void path_get(struct path *path)
3645dd784d0SJan Blunck {
3655dd784d0SJan Blunck 	mntget(path->mnt);
3665dd784d0SJan Blunck 	dget(path->dentry);
3675dd784d0SJan Blunck }
3685dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
3695dd784d0SJan Blunck 
3705dd784d0SJan Blunck /**
371b3e19d92SNick Piggin  * path_get_long - get a long reference to a path
372b3e19d92SNick Piggin  * @path: path to get the reference to
373b3e19d92SNick Piggin  *
374b3e19d92SNick Piggin  * Given a path increment the reference count to the dentry and the vfsmount.
375b3e19d92SNick Piggin  */
376b3e19d92SNick Piggin void path_get_long(struct path *path)
377b3e19d92SNick Piggin {
378b3e19d92SNick Piggin 	mntget_long(path->mnt);
379b3e19d92SNick Piggin 	dget(path->dentry);
380b3e19d92SNick Piggin }
381b3e19d92SNick Piggin 
382b3e19d92SNick Piggin /**
3831d957f9bSJan Blunck  * path_put - put a reference to a path
3841d957f9bSJan Blunck  * @path: path to put the reference to
3851d957f9bSJan Blunck  *
3861d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
3871d957f9bSJan Blunck  */
3881d957f9bSJan Blunck void path_put(struct path *path)
3891da177e4SLinus Torvalds {
3901d957f9bSJan Blunck 	dput(path->dentry);
3911d957f9bSJan Blunck 	mntput(path->mnt);
3921da177e4SLinus Torvalds }
3931d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
3941da177e4SLinus Torvalds 
395834f2a4aSTrond Myklebust /**
396b3e19d92SNick Piggin  * path_put_long - put a long reference to a path
397b3e19d92SNick Piggin  * @path: path to put the reference to
398b3e19d92SNick Piggin  *
399b3e19d92SNick Piggin  * Given a path decrement the reference count to the dentry and the vfsmount.
400b3e19d92SNick Piggin  */
401b3e19d92SNick Piggin void path_put_long(struct path *path)
402b3e19d92SNick Piggin {
403b3e19d92SNick Piggin 	dput(path->dentry);
404b3e19d92SNick Piggin 	mntput_long(path->mnt);
405b3e19d92SNick Piggin }
406b3e19d92SNick Piggin 
407b3e19d92SNick Piggin /**
40831e6b01fSNick Piggin  * nameidata_drop_rcu - drop this nameidata out of rcu-walk
40931e6b01fSNick Piggin  * @nd: nameidata pathwalk data to drop
41031e6b01fSNick Piggin  * @Returns: 0 on success, -ECHLID on failure
41131e6b01fSNick Piggin  *
41231e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
41331e6b01fSNick Piggin  * Documentation/filesystems/path-lookup.txt). __drop_rcu* functions attempt
41431e6b01fSNick Piggin  * to drop out of rcu-walk mode and take normal reference counts on dentries
41531e6b01fSNick Piggin  * and vfsmounts to transition to rcu-walk mode. __drop_rcu* functions take
41631e6b01fSNick Piggin  * refcounts at the last known good point before rcu-walk got stuck, so
41731e6b01fSNick Piggin  * ref-walk may continue from there. If this is not successful (eg. a seqcount
41831e6b01fSNick Piggin  * has changed), then failure is returned and path walk restarts from the
41931e6b01fSNick Piggin  * beginning in ref-walk mode.
42031e6b01fSNick Piggin  *
42131e6b01fSNick Piggin  * nameidata_drop_rcu attempts to drop the current nd->path and nd->root into
42231e6b01fSNick Piggin  * ref-walk. Must be called from rcu-walk context.
42331e6b01fSNick Piggin  */
42431e6b01fSNick Piggin static int nameidata_drop_rcu(struct nameidata *nd)
42531e6b01fSNick Piggin {
42631e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
42731e6b01fSNick Piggin 	struct dentry *dentry = nd->path.dentry;
42831e6b01fSNick Piggin 
42931e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
43031e6b01fSNick Piggin 	if (nd->root.mnt) {
43131e6b01fSNick Piggin 		spin_lock(&fs->lock);
43231e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
43331e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
43431e6b01fSNick Piggin 			goto err_root;
43531e6b01fSNick Piggin 	}
43631e6b01fSNick Piggin 	spin_lock(&dentry->d_lock);
43731e6b01fSNick Piggin 	if (!__d_rcu_to_refcount(dentry, nd->seq))
43831e6b01fSNick Piggin 		goto err;
43931e6b01fSNick Piggin 	BUG_ON(nd->inode != dentry->d_inode);
44031e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
44131e6b01fSNick Piggin 	if (nd->root.mnt) {
44231e6b01fSNick Piggin 		path_get(&nd->root);
44331e6b01fSNick Piggin 		spin_unlock(&fs->lock);
44431e6b01fSNick Piggin 	}
44531e6b01fSNick Piggin 	mntget(nd->path.mnt);
44631e6b01fSNick Piggin 
44731e6b01fSNick Piggin 	rcu_read_unlock();
44831e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
44931e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
45031e6b01fSNick Piggin 	return 0;
45131e6b01fSNick Piggin err:
45231e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
45331e6b01fSNick Piggin err_root:
45431e6b01fSNick Piggin 	if (nd->root.mnt)
45531e6b01fSNick Piggin 		spin_unlock(&fs->lock);
45631e6b01fSNick Piggin 	return -ECHILD;
45731e6b01fSNick Piggin }
45831e6b01fSNick Piggin 
45931e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing.  */
46031e6b01fSNick Piggin static inline int nameidata_drop_rcu_maybe(struct nameidata *nd)
46131e6b01fSNick Piggin {
46231e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU)
46331e6b01fSNick Piggin 		return nameidata_drop_rcu(nd);
46431e6b01fSNick Piggin 	return 0;
46531e6b01fSNick Piggin }
46631e6b01fSNick Piggin 
46731e6b01fSNick Piggin /**
46831e6b01fSNick Piggin  * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk
46931e6b01fSNick Piggin  * @nd: nameidata pathwalk data to drop
47031e6b01fSNick Piggin  * @dentry: dentry to drop
47131e6b01fSNick Piggin  * @Returns: 0 on success, -ECHLID on failure
47231e6b01fSNick Piggin  *
47331e6b01fSNick Piggin  * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root,
47431e6b01fSNick Piggin  * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on
47531e6b01fSNick Piggin  * @nd. Must be called from rcu-walk context.
47631e6b01fSNick Piggin  */
47731e6b01fSNick Piggin static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry)
47831e6b01fSNick Piggin {
47931e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
48031e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
48131e6b01fSNick Piggin 
48290dbb77bSNick Piggin 	/*
48390dbb77bSNick Piggin 	 * It can be possible to revalidate the dentry that we started
48490dbb77bSNick Piggin 	 * the path walk with. force_reval_path may also revalidate the
48590dbb77bSNick Piggin 	 * dentry already committed to the nameidata.
48690dbb77bSNick Piggin 	 */
48790dbb77bSNick Piggin 	if (unlikely(parent == dentry))
48890dbb77bSNick Piggin 		return nameidata_drop_rcu(nd);
48990dbb77bSNick Piggin 
49031e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
49131e6b01fSNick Piggin 	if (nd->root.mnt) {
49231e6b01fSNick Piggin 		spin_lock(&fs->lock);
49331e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
49431e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
49531e6b01fSNick Piggin 			goto err_root;
49631e6b01fSNick Piggin 	}
49731e6b01fSNick Piggin 	spin_lock(&parent->d_lock);
49831e6b01fSNick Piggin 	spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
49931e6b01fSNick Piggin 	if (!__d_rcu_to_refcount(dentry, nd->seq))
50031e6b01fSNick Piggin 		goto err;
50131e6b01fSNick Piggin 	/*
50231e6b01fSNick Piggin 	 * If the sequence check on the child dentry passed, then the child has
50331e6b01fSNick Piggin 	 * not been removed from its parent. This means the parent dentry must
50431e6b01fSNick Piggin 	 * be valid and able to take a reference at this point.
50531e6b01fSNick Piggin 	 */
50631e6b01fSNick Piggin 	BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
50731e6b01fSNick Piggin 	BUG_ON(!parent->d_count);
50831e6b01fSNick Piggin 	parent->d_count++;
50931e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
51031e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
51131e6b01fSNick Piggin 	if (nd->root.mnt) {
51231e6b01fSNick Piggin 		path_get(&nd->root);
51331e6b01fSNick Piggin 		spin_unlock(&fs->lock);
51431e6b01fSNick Piggin 	}
51531e6b01fSNick Piggin 	mntget(nd->path.mnt);
51631e6b01fSNick Piggin 
51731e6b01fSNick Piggin 	rcu_read_unlock();
51831e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
51931e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
52031e6b01fSNick Piggin 	return 0;
52131e6b01fSNick Piggin err:
52231e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
52331e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
52431e6b01fSNick Piggin err_root:
52531e6b01fSNick Piggin 	if (nd->root.mnt)
52631e6b01fSNick Piggin 		spin_unlock(&fs->lock);
52731e6b01fSNick Piggin 	return -ECHILD;
52831e6b01fSNick Piggin }
52931e6b01fSNick Piggin 
53031e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing.  */
53131e6b01fSNick Piggin static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry)
53231e6b01fSNick Piggin {
53331e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU)
53431e6b01fSNick Piggin 		return nameidata_dentry_drop_rcu(nd, dentry);
53531e6b01fSNick Piggin 	return 0;
53631e6b01fSNick Piggin }
53731e6b01fSNick Piggin 
53831e6b01fSNick Piggin /**
53931e6b01fSNick Piggin  * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk
54031e6b01fSNick Piggin  * @nd: nameidata pathwalk data to drop
54131e6b01fSNick Piggin  * @Returns: 0 on success, -ECHLID on failure
54231e6b01fSNick Piggin  *
54331e6b01fSNick Piggin  * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk.
54431e6b01fSNick Piggin  * nd->path should be the final element of the lookup, so nd->root is discarded.
54531e6b01fSNick Piggin  * Must be called from rcu-walk context.
54631e6b01fSNick Piggin  */
54731e6b01fSNick Piggin static int nameidata_drop_rcu_last(struct nameidata *nd)
54831e6b01fSNick Piggin {
54931e6b01fSNick Piggin 	struct dentry *dentry = nd->path.dentry;
55031e6b01fSNick Piggin 
55131e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
55231e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
55331e6b01fSNick Piggin 	nd->root.mnt = NULL;
55431e6b01fSNick Piggin 	spin_lock(&dentry->d_lock);
55531e6b01fSNick Piggin 	if (!__d_rcu_to_refcount(dentry, nd->seq))
55631e6b01fSNick Piggin 		goto err_unlock;
55731e6b01fSNick Piggin 	BUG_ON(nd->inode != dentry->d_inode);
55831e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
55931e6b01fSNick Piggin 
56031e6b01fSNick Piggin 	mntget(nd->path.mnt);
56131e6b01fSNick Piggin 
56231e6b01fSNick Piggin 	rcu_read_unlock();
56331e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
56431e6b01fSNick Piggin 
56531e6b01fSNick Piggin 	return 0;
56631e6b01fSNick Piggin 
56731e6b01fSNick Piggin err_unlock:
56831e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
56931e6b01fSNick Piggin 	rcu_read_unlock();
57031e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
57131e6b01fSNick Piggin 	return -ECHILD;
57231e6b01fSNick Piggin }
57331e6b01fSNick Piggin 
57431e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing.  */
57531e6b01fSNick Piggin static inline int nameidata_drop_rcu_last_maybe(struct nameidata *nd)
57631e6b01fSNick Piggin {
57731e6b01fSNick Piggin 	if (likely(nd->flags & LOOKUP_RCU))
57831e6b01fSNick Piggin 		return nameidata_drop_rcu_last(nd);
57931e6b01fSNick Piggin 	return 0;
58031e6b01fSNick Piggin }
58131e6b01fSNick Piggin 
58231e6b01fSNick Piggin /**
583834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
584834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
585834f2a4aSTrond Myklebust  */
586834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
587834f2a4aSTrond Myklebust {
5880f7fc9e4SJosef "Jeff" Sipek 	if (nd->intent.open.file->f_path.dentry == NULL)
589834f2a4aSTrond Myklebust 		put_filp(nd->intent.open.file);
590834f2a4aSTrond Myklebust 	else
591834f2a4aSTrond Myklebust 		fput(nd->intent.open.file);
592834f2a4aSTrond Myklebust }
593834f2a4aSTrond Myklebust 
594bb20c18dSNick Piggin /*
595bb20c18dSNick Piggin  * Call d_revalidate and handle filesystems that request rcu-walk
596bb20c18dSNick Piggin  * to be dropped. This may be called and return in rcu-walk mode,
597bb20c18dSNick Piggin  * regardless of success or error. If -ECHILD is returned, the caller
598bb20c18dSNick Piggin  * must return -ECHILD back up the path walk stack so path walk may
599bb20c18dSNick Piggin  * be restarted in ref-walk mode.
600bb20c18dSNick Piggin  */
60134286d66SNick Piggin static int d_revalidate(struct dentry *dentry, struct nameidata *nd)
60234286d66SNick Piggin {
60334286d66SNick Piggin 	int status;
60434286d66SNick Piggin 
60534286d66SNick Piggin 	status = dentry->d_op->d_revalidate(dentry, nd);
60634286d66SNick Piggin 	if (status == -ECHILD) {
60734286d66SNick Piggin 		if (nameidata_dentry_drop_rcu(nd, dentry))
60834286d66SNick Piggin 			return status;
60934286d66SNick Piggin 		status = dentry->d_op->d_revalidate(dentry, nd);
61034286d66SNick Piggin 	}
61134286d66SNick Piggin 
61234286d66SNick Piggin 	return status;
61334286d66SNick Piggin }
61434286d66SNick Piggin 
615bcdc5e01SIan Kent static inline struct dentry *
616bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd)
617bcdc5e01SIan Kent {
61834286d66SNick Piggin 	int status;
61934286d66SNick Piggin 
62034286d66SNick Piggin 	status = d_revalidate(dentry, nd);
621bcdc5e01SIan Kent 	if (unlikely(status <= 0)) {
622bcdc5e01SIan Kent 		/*
623bcdc5e01SIan Kent 		 * The dentry failed validation.
624bcdc5e01SIan Kent 		 * If d_revalidate returned 0 attempt to invalidate
625bcdc5e01SIan Kent 		 * the dentry otherwise d_revalidate is asking us
626bcdc5e01SIan Kent 		 * to return a fail status.
627bcdc5e01SIan Kent 		 */
62834286d66SNick Piggin 		if (status < 0) {
62934286d66SNick Piggin 			/* If we're in rcu-walk, we don't have a ref */
63034286d66SNick Piggin 			if (!(nd->flags & LOOKUP_RCU))
63134286d66SNick Piggin 				dput(dentry);
63234286d66SNick Piggin 			dentry = ERR_PTR(status);
63334286d66SNick Piggin 
63434286d66SNick Piggin 		} else {
63534286d66SNick Piggin 			/* Don't d_invalidate in rcu-walk mode */
63634286d66SNick Piggin 			if (nameidata_dentry_drop_rcu_maybe(nd, dentry))
63734286d66SNick Piggin 				return ERR_PTR(-ECHILD);
638bcdc5e01SIan Kent 			if (!d_invalidate(dentry)) {
639bcdc5e01SIan Kent 				dput(dentry);
640bcdc5e01SIan Kent 				dentry = NULL;
641bcdc5e01SIan Kent 			}
642bcdc5e01SIan Kent 		}
643bcdc5e01SIan Kent 	}
644bcdc5e01SIan Kent 	return dentry;
645bcdc5e01SIan Kent }
646bcdc5e01SIan Kent 
647fb045adbSNick Piggin static inline int need_reval_dot(struct dentry *dentry)
648fb045adbSNick Piggin {
649fb045adbSNick Piggin 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
650fb045adbSNick Piggin 		return 0;
651fb045adbSNick Piggin 
652fb045adbSNick Piggin 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
653fb045adbSNick Piggin 		return 0;
654fb045adbSNick Piggin 
655fb045adbSNick Piggin 	return 1;
656fb045adbSNick Piggin }
657fb045adbSNick Piggin 
6581da177e4SLinus Torvalds /*
65939159de2SJeff Layton  * force_reval_path - force revalidation of a dentry
66039159de2SJeff Layton  *
66139159de2SJeff Layton  * In some situations the path walking code will trust dentries without
66239159de2SJeff Layton  * revalidating them. This causes problems for filesystems that depend on
66339159de2SJeff Layton  * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
66439159de2SJeff Layton  * (which indicates that it's possible for the dentry to go stale), force
66539159de2SJeff Layton  * a d_revalidate call before proceeding.
66639159de2SJeff Layton  *
66739159de2SJeff Layton  * Returns 0 if the revalidation was successful. If the revalidation fails,
66839159de2SJeff Layton  * either return the error returned by d_revalidate or -ESTALE if the
66939159de2SJeff Layton  * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
67039159de2SJeff Layton  * invalidate the dentry. It's up to the caller to handle putting references
67139159de2SJeff Layton  * to the path if necessary.
67239159de2SJeff Layton  */
67339159de2SJeff Layton static int
67439159de2SJeff Layton force_reval_path(struct path *path, struct nameidata *nd)
67539159de2SJeff Layton {
67639159de2SJeff Layton 	int status;
67739159de2SJeff Layton 	struct dentry *dentry = path->dentry;
67839159de2SJeff Layton 
67939159de2SJeff Layton 	/*
68039159de2SJeff Layton 	 * only check on filesystems where it's possible for the dentry to
681fb045adbSNick Piggin 	 * become stale.
68239159de2SJeff Layton 	 */
683fb045adbSNick Piggin 	if (!need_reval_dot(dentry))
68439159de2SJeff Layton 		return 0;
68539159de2SJeff Layton 
68634286d66SNick Piggin 	status = d_revalidate(dentry, nd);
68739159de2SJeff Layton 	if (status > 0)
68839159de2SJeff Layton 		return 0;
68939159de2SJeff Layton 
69039159de2SJeff Layton 	if (!status) {
691bb20c18dSNick Piggin 		/* Don't d_invalidate in rcu-walk mode */
692bb20c18dSNick Piggin 		if (nameidata_drop_rcu(nd))
693bb20c18dSNick Piggin 			return -ECHILD;
69439159de2SJeff Layton 		d_invalidate(dentry);
69539159de2SJeff Layton 		status = -ESTALE;
69639159de2SJeff Layton 	}
69739159de2SJeff Layton 	return status;
69839159de2SJeff Layton }
69939159de2SJeff Layton 
70039159de2SJeff Layton /*
701b75b5086SAl Viro  * Short-cut version of permission(), for calling on directories
702b75b5086SAl Viro  * during pathname resolution.  Combines parts of permission()
703b75b5086SAl Viro  * and generic_permission(), and tests ONLY for MAY_EXEC permission.
7041da177e4SLinus Torvalds  *
7051da177e4SLinus Torvalds  * If appropriate, check DAC only.  If not appropriate, or
706b75b5086SAl Viro  * short-cut DAC fails, then call ->permission() to do more
7071da177e4SLinus Torvalds  * complete permission check.
7081da177e4SLinus Torvalds  */
709b74c79e9SNick Piggin static inline int exec_permission(struct inode *inode, unsigned int flags)
7101da177e4SLinus Torvalds {
7115909ccaaSLinus Torvalds 	int ret;
7121da177e4SLinus Torvalds 
713cb9179eaSLinus Torvalds 	if (inode->i_op->permission) {
714b74c79e9SNick Piggin 		ret = inode->i_op->permission(inode, MAY_EXEC, flags);
715b74c79e9SNick Piggin 	} else {
716b74c79e9SNick Piggin 		ret = acl_permission_check(inode, MAY_EXEC, flags,
717b74c79e9SNick Piggin 				inode->i_op->check_acl);
718cb9179eaSLinus Torvalds 	}
719b74c79e9SNick Piggin 	if (likely(!ret))
7201da177e4SLinus Torvalds 		goto ok;
721b74c79e9SNick Piggin 	if (ret == -ECHILD)
72231e6b01fSNick Piggin 		return ret;
7231da177e4SLinus Torvalds 
724f1ac9f6bSLinus Torvalds 	if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH))
7251da177e4SLinus Torvalds 		goto ok;
7261da177e4SLinus Torvalds 
7275909ccaaSLinus Torvalds 	return ret;
7281da177e4SLinus Torvalds ok:
729b74c79e9SNick Piggin 	return security_inode_exec_permission(inode, flags);
7301da177e4SLinus Torvalds }
7311da177e4SLinus Torvalds 
7322a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
7332a737871SAl Viro {
734f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
735f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
7362a737871SAl Viro }
7372a737871SAl Viro 
7386de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
7396de88d72SAl Viro 
74031e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
74131e6b01fSNick Piggin {
74231e6b01fSNick Piggin 	if (!nd->root.mnt) {
74331e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
744c28cc364SNick Piggin 		unsigned seq;
745c28cc364SNick Piggin 
746c28cc364SNick Piggin 		do {
747c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
74831e6b01fSNick Piggin 			nd->root = fs->root;
749c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
75031e6b01fSNick Piggin 	}
75131e6b01fSNick Piggin }
75231e6b01fSNick Piggin 
753f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
7541da177e4SLinus Torvalds {
75531e6b01fSNick Piggin 	int ret;
75631e6b01fSNick Piggin 
7571da177e4SLinus Torvalds 	if (IS_ERR(link))
7581da177e4SLinus Torvalds 		goto fail;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	if (*link == '/') {
7612a737871SAl Viro 		set_root(nd);
7621d957f9bSJan Blunck 		path_put(&nd->path);
7632a737871SAl Viro 		nd->path = nd->root;
7642a737871SAl Viro 		path_get(&nd->root);
7651da177e4SLinus Torvalds 	}
76631e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
767b4091d5fSChristoph Hellwig 
76831e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
76931e6b01fSNick Piggin 	return ret;
7701da177e4SLinus Torvalds fail:
7711d957f9bSJan Blunck 	path_put(&nd->path);
7721da177e4SLinus Torvalds 	return PTR_ERR(link);
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
7751d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
776051d3812SIan Kent {
777051d3812SIan Kent 	dput(path->dentry);
7784ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
779051d3812SIan Kent 		mntput(path->mnt);
780051d3812SIan Kent }
781051d3812SIan Kent 
782051d3812SIan Kent static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
783051d3812SIan Kent {
78431e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
7854ac91378SJan Blunck 		dput(nd->path.dentry);
78631e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
7874ac91378SJan Blunck 			mntput(nd->path.mnt);
7889a229683SHuang Shijie 	}
78931e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
7904ac91378SJan Blunck 	nd->path.dentry = path->dentry;
791051d3812SIan Kent }
792051d3812SIan Kent 
793def4af30SAl Viro static __always_inline int
794def4af30SAl Viro __do_follow_link(struct path *path, struct nameidata *nd, void **p)
7951da177e4SLinus Torvalds {
7961da177e4SLinus Torvalds 	int error;
797cd4e91d3SAl Viro 	struct dentry *dentry = path->dentry;
7981da177e4SLinus Torvalds 
799d671a1cbSAl Viro 	touch_atime(path->mnt, dentry);
8001da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
801cd4e91d3SAl Viro 
8024ac91378SJan Blunck 	if (path->mnt != nd->path.mnt) {
803051d3812SIan Kent 		path_to_nameidata(path, nd);
80431e6b01fSNick Piggin 		nd->inode = nd->path.dentry->d_inode;
805051d3812SIan Kent 		dget(dentry);
806051d3812SIan Kent 	}
807cd4e91d3SAl Viro 	mntget(path->mnt);
80831e6b01fSNick Piggin 
80986acdca1SAl Viro 	nd->last_type = LAST_BIND;
810def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
811def4af30SAl Viro 	error = PTR_ERR(*p);
812def4af30SAl Viro 	if (!IS_ERR(*p)) {
8131da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
814cc314eefSLinus Torvalds 		error = 0;
8151da177e4SLinus Torvalds 		if (s)
8161da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
81739159de2SJeff Layton 		else if (nd->last_type == LAST_BIND) {
81839159de2SJeff Layton 			error = force_reval_path(&nd->path, nd);
81939159de2SJeff Layton 			if (error)
82039159de2SJeff Layton 				path_put(&nd->path);
82139159de2SJeff Layton 		}
8221da177e4SLinus Torvalds 	}
8231da177e4SLinus Torvalds 	return error;
8241da177e4SLinus Torvalds }
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds /*
8271da177e4SLinus Torvalds  * This limits recursive symlink follows to 8, while
8281da177e4SLinus Torvalds  * limiting consecutive symlinks to 40.
8291da177e4SLinus Torvalds  *
8301da177e4SLinus Torvalds  * Without that kind of total limit, nasty chains of consecutive
8311da177e4SLinus Torvalds  * symlinks can cause almost arbitrarily long lookups.
8321da177e4SLinus Torvalds  */
83390ebe565SAl Viro static inline int do_follow_link(struct path *path, struct nameidata *nd)
8341da177e4SLinus Torvalds {
835def4af30SAl Viro 	void *cookie;
8361da177e4SLinus Torvalds 	int err = -ELOOP;
8371da177e4SLinus Torvalds 	if (current->link_count >= MAX_NESTED_LINKS)
8381da177e4SLinus Torvalds 		goto loop;
8391da177e4SLinus Torvalds 	if (current->total_link_count >= 40)
8401da177e4SLinus Torvalds 		goto loop;
8411da177e4SLinus Torvalds 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
8421da177e4SLinus Torvalds 	cond_resched();
84390ebe565SAl Viro 	err = security_inode_follow_link(path->dentry, nd);
8441da177e4SLinus Torvalds 	if (err)
8451da177e4SLinus Torvalds 		goto loop;
8461da177e4SLinus Torvalds 	current->link_count++;
8471da177e4SLinus Torvalds 	current->total_link_count++;
8481da177e4SLinus Torvalds 	nd->depth++;
849def4af30SAl Viro 	err = __do_follow_link(path, nd, &cookie);
850def4af30SAl Viro 	if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link)
851def4af30SAl Viro 		path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie);
852258fa999SAl Viro 	path_put(path);
8531da177e4SLinus Torvalds 	current->link_count--;
8541da177e4SLinus Torvalds 	nd->depth--;
8551da177e4SLinus Torvalds 	return err;
8561da177e4SLinus Torvalds loop:
8571d957f9bSJan Blunck 	path_put_conditional(path, nd);
8581d957f9bSJan Blunck 	path_put(&nd->path);
8591da177e4SLinus Torvalds 	return err;
8601da177e4SLinus Torvalds }
8611da177e4SLinus Torvalds 
86231e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
86331e6b01fSNick Piggin {
86431e6b01fSNick Piggin 	struct vfsmount *parent;
86531e6b01fSNick Piggin 	struct dentry *mountpoint;
86631e6b01fSNick Piggin 
86731e6b01fSNick Piggin 	parent = path->mnt->mnt_parent;
86831e6b01fSNick Piggin 	if (parent == path->mnt)
86931e6b01fSNick Piggin 		return 0;
87031e6b01fSNick Piggin 	mountpoint = path->mnt->mnt_mountpoint;
87131e6b01fSNick Piggin 	path->dentry = mountpoint;
87231e6b01fSNick Piggin 	path->mnt = parent;
87331e6b01fSNick Piggin 	return 1;
87431e6b01fSNick Piggin }
87531e6b01fSNick Piggin 
876bab77ebfSAl Viro int follow_up(struct path *path)
8771da177e4SLinus Torvalds {
8781da177e4SLinus Torvalds 	struct vfsmount *parent;
8791da177e4SLinus Torvalds 	struct dentry *mountpoint;
88099b7db7bSNick Piggin 
88199b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
882bab77ebfSAl Viro 	parent = path->mnt->mnt_parent;
883bab77ebfSAl Viro 	if (parent == path->mnt) {
88499b7db7bSNick Piggin 		br_read_unlock(vfsmount_lock);
8851da177e4SLinus Torvalds 		return 0;
8861da177e4SLinus Torvalds 	}
8871da177e4SLinus Torvalds 	mntget(parent);
888bab77ebfSAl Viro 	mountpoint = dget(path->mnt->mnt_mountpoint);
88999b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
890bab77ebfSAl Viro 	dput(path->dentry);
891bab77ebfSAl Viro 	path->dentry = mountpoint;
892bab77ebfSAl Viro 	mntput(path->mnt);
893bab77ebfSAl Viro 	path->mnt = parent;
8941da177e4SLinus Torvalds 	return 1;
8951da177e4SLinus Torvalds }
8961da177e4SLinus Torvalds 
897b5c84bf6SNick Piggin /*
898b5c84bf6SNick Piggin  * serialization is taken care of in namespace.c
8991da177e4SLinus Torvalds  */
90031e6b01fSNick Piggin static void __follow_mount_rcu(struct nameidata *nd, struct path *path,
90131e6b01fSNick Piggin 				struct inode **inode)
90231e6b01fSNick Piggin {
90331e6b01fSNick Piggin 	while (d_mountpoint(path->dentry)) {
90431e6b01fSNick Piggin 		struct vfsmount *mounted;
90531e6b01fSNick Piggin 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
90631e6b01fSNick Piggin 		if (!mounted)
90731e6b01fSNick Piggin 			return;
90831e6b01fSNick Piggin 		path->mnt = mounted;
90931e6b01fSNick Piggin 		path->dentry = mounted->mnt_root;
91031e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
91131e6b01fSNick Piggin 		*inode = path->dentry->d_inode;
91231e6b01fSNick Piggin 	}
91331e6b01fSNick Piggin }
91431e6b01fSNick Piggin 
915463ffb2eSAl Viro static int __follow_mount(struct path *path)
916463ffb2eSAl Viro {
917463ffb2eSAl Viro 	int res = 0;
918463ffb2eSAl Viro 	while (d_mountpoint(path->dentry)) {
9191c755af4SAl Viro 		struct vfsmount *mounted = lookup_mnt(path);
920463ffb2eSAl Viro 		if (!mounted)
921463ffb2eSAl Viro 			break;
922463ffb2eSAl Viro 		dput(path->dentry);
923463ffb2eSAl Viro 		if (res)
924463ffb2eSAl Viro 			mntput(path->mnt);
925463ffb2eSAl Viro 		path->mnt = mounted;
926463ffb2eSAl Viro 		path->dentry = dget(mounted->mnt_root);
927463ffb2eSAl Viro 		res = 1;
928463ffb2eSAl Viro 	}
929463ffb2eSAl Viro 	return res;
930463ffb2eSAl Viro }
931463ffb2eSAl Viro 
93279ed0226SAl Viro static void follow_mount(struct path *path)
9331da177e4SLinus Torvalds {
93479ed0226SAl Viro 	while (d_mountpoint(path->dentry)) {
9351c755af4SAl Viro 		struct vfsmount *mounted = lookup_mnt(path);
9361da177e4SLinus Torvalds 		if (!mounted)
9371da177e4SLinus Torvalds 			break;
93879ed0226SAl Viro 		dput(path->dentry);
93979ed0226SAl Viro 		mntput(path->mnt);
94079ed0226SAl Viro 		path->mnt = mounted;
94179ed0226SAl Viro 		path->dentry = dget(mounted->mnt_root);
9421da177e4SLinus Torvalds 	}
9431da177e4SLinus Torvalds }
9441da177e4SLinus Torvalds 
9459393bd07SAl Viro int follow_down(struct path *path)
9461da177e4SLinus Torvalds {
9471da177e4SLinus Torvalds 	struct vfsmount *mounted;
9481da177e4SLinus Torvalds 
9491c755af4SAl Viro 	mounted = lookup_mnt(path);
9501da177e4SLinus Torvalds 	if (mounted) {
9519393bd07SAl Viro 		dput(path->dentry);
9529393bd07SAl Viro 		mntput(path->mnt);
9539393bd07SAl Viro 		path->mnt = mounted;
9549393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
9551da177e4SLinus Torvalds 		return 1;
9561da177e4SLinus Torvalds 	}
9571da177e4SLinus Torvalds 	return 0;
9581da177e4SLinus Torvalds }
9591da177e4SLinus Torvalds 
96031e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
96131e6b01fSNick Piggin {
96231e6b01fSNick Piggin 	struct inode *inode = nd->inode;
96331e6b01fSNick Piggin 
96431e6b01fSNick Piggin 	set_root_rcu(nd);
96531e6b01fSNick Piggin 
96631e6b01fSNick Piggin 	while(1) {
96731e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
96831e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
96931e6b01fSNick Piggin 			break;
97031e6b01fSNick Piggin 		}
97131e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
97231e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
97331e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
97431e6b01fSNick Piggin 			unsigned seq;
97531e6b01fSNick Piggin 
97631e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
97731e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
97831e6b01fSNick Piggin 				return -ECHILD;
97931e6b01fSNick Piggin 			inode = parent->d_inode;
98031e6b01fSNick Piggin 			nd->path.dentry = parent;
98131e6b01fSNick Piggin 			nd->seq = seq;
98231e6b01fSNick Piggin 			break;
98331e6b01fSNick Piggin 		}
98431e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
98531e6b01fSNick Piggin 			break;
98631e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
98731e6b01fSNick Piggin 		inode = nd->path.dentry->d_inode;
98831e6b01fSNick Piggin 	}
98931e6b01fSNick Piggin 	__follow_mount_rcu(nd, &nd->path, &inode);
99031e6b01fSNick Piggin 	nd->inode = inode;
99131e6b01fSNick Piggin 
99231e6b01fSNick Piggin 	return 0;
99331e6b01fSNick Piggin }
99431e6b01fSNick Piggin 
99531e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
9961da177e4SLinus Torvalds {
9972a737871SAl Viro 	set_root(nd);
998e518ddb7SAndreas Mohr 
9991da177e4SLinus Torvalds 	while(1) {
10004ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
10011da177e4SLinus Torvalds 
10022a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
10032a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
10041da177e4SLinus Torvalds 			break;
10051da177e4SLinus Torvalds 		}
10064ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
10073088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
10083088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
10091da177e4SLinus Torvalds 			dput(old);
10101da177e4SLinus Torvalds 			break;
10111da177e4SLinus Torvalds 		}
10123088dd70SAl Viro 		if (!follow_up(&nd->path))
10131da177e4SLinus Torvalds 			break;
10141da177e4SLinus Torvalds 	}
101579ed0226SAl Viro 	follow_mount(&nd->path);
101631e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
10171da177e4SLinus Torvalds }
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds /*
1020baa03890SNick Piggin  * Allocate a dentry with name and parent, and perform a parent
1021baa03890SNick Piggin  * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1022baa03890SNick Piggin  * on error. parent->d_inode->i_mutex must be held. d_lookup must
1023baa03890SNick Piggin  * have verified that no child exists while under i_mutex.
1024baa03890SNick Piggin  */
1025baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1026baa03890SNick Piggin 				struct qstr *name, struct nameidata *nd)
1027baa03890SNick Piggin {
1028baa03890SNick Piggin 	struct inode *inode = parent->d_inode;
1029baa03890SNick Piggin 	struct dentry *dentry;
1030baa03890SNick Piggin 	struct dentry *old;
1031baa03890SNick Piggin 
1032baa03890SNick Piggin 	/* Don't create child dentry for a dead directory. */
1033baa03890SNick Piggin 	if (unlikely(IS_DEADDIR(inode)))
1034baa03890SNick Piggin 		return ERR_PTR(-ENOENT);
1035baa03890SNick Piggin 
1036baa03890SNick Piggin 	dentry = d_alloc(parent, name);
1037baa03890SNick Piggin 	if (unlikely(!dentry))
1038baa03890SNick Piggin 		return ERR_PTR(-ENOMEM);
1039baa03890SNick Piggin 
1040baa03890SNick Piggin 	old = inode->i_op->lookup(inode, dentry, nd);
1041baa03890SNick Piggin 	if (unlikely(old)) {
1042baa03890SNick Piggin 		dput(dentry);
1043baa03890SNick Piggin 		dentry = old;
1044baa03890SNick Piggin 	}
1045baa03890SNick Piggin 	return dentry;
1046baa03890SNick Piggin }
1047baa03890SNick Piggin 
1048baa03890SNick Piggin /*
10491da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
10501da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
10511da177e4SLinus Torvalds  *  It _is_ time-critical.
10521da177e4SLinus Torvalds  */
10531da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
105431e6b01fSNick Piggin 			struct path *path, struct inode **inode)
10551da177e4SLinus Torvalds {
10564ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
105731e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
10586e6b1bd1SAl Viro 	struct inode *dir;
10593cac260aSAl Viro 	/*
10603cac260aSAl Viro 	 * See if the low-level filesystem might want
10613cac260aSAl Viro 	 * to use its own hash..
10623cac260aSAl Viro 	 */
1063fb045adbSNick Piggin 	if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
106431e6b01fSNick Piggin 		int err = parent->d_op->d_hash(parent, nd->inode, name);
10653cac260aSAl Viro 		if (err < 0)
10663cac260aSAl Viro 			return err;
10673cac260aSAl Viro 	}
10681da177e4SLinus Torvalds 
1069b04f784eSNick Piggin 	/*
1070b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1071b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1072b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1073b04f784eSNick Piggin 	 */
107431e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
107531e6b01fSNick Piggin 		unsigned seq;
107631e6b01fSNick Piggin 
107731e6b01fSNick Piggin 		*inode = nd->inode;
107831e6b01fSNick Piggin 		dentry = __d_lookup_rcu(parent, name, &seq, inode);
107931e6b01fSNick Piggin 		if (!dentry) {
108031e6b01fSNick Piggin 			if (nameidata_drop_rcu(nd))
108131e6b01fSNick Piggin 				return -ECHILD;
108231e6b01fSNick Piggin 			goto need_lookup;
108331e6b01fSNick Piggin 		}
108431e6b01fSNick Piggin 		/* Memory barrier in read_seqcount_begin of child is enough */
108531e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
108631e6b01fSNick Piggin 			return -ECHILD;
108731e6b01fSNick Piggin 
108831e6b01fSNick Piggin 		nd->seq = seq;
108934286d66SNick Piggin 		if (dentry->d_flags & DCACHE_OP_REVALIDATE)
109031e6b01fSNick Piggin 			goto need_revalidate;
109131e6b01fSNick Piggin 		path->mnt = mnt;
109231e6b01fSNick Piggin 		path->dentry = dentry;
109331e6b01fSNick Piggin 		__follow_mount_rcu(nd, path, inode);
109431e6b01fSNick Piggin 	} else {
109531e6b01fSNick Piggin 		dentry = __d_lookup(parent, name);
10961da177e4SLinus Torvalds 		if (!dentry)
10971da177e4SLinus Torvalds 			goto need_lookup;
10982e2e88eaSNick Piggin found:
1099fb045adbSNick Piggin 		if (dentry->d_flags & DCACHE_OP_REVALIDATE)
11001da177e4SLinus Torvalds 			goto need_revalidate;
11011da177e4SLinus Torvalds done:
11021da177e4SLinus Torvalds 		path->mnt = mnt;
11031da177e4SLinus Torvalds 		path->dentry = dentry;
1104634ee701SAl Viro 		__follow_mount(path);
110531e6b01fSNick Piggin 		*inode = path->dentry->d_inode;
110631e6b01fSNick Piggin 	}
11071da177e4SLinus Torvalds 	return 0;
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds need_lookup:
11106e6b1bd1SAl Viro 	dir = parent->d_inode;
111131e6b01fSNick Piggin 	BUG_ON(nd->inode != dir);
11126e6b1bd1SAl Viro 
11136e6b1bd1SAl Viro 	mutex_lock(&dir->i_mutex);
11146e6b1bd1SAl Viro 	/*
11156e6b1bd1SAl Viro 	 * First re-do the cached lookup just in case it was created
1116b04f784eSNick Piggin 	 * while we waited for the directory semaphore, or the first
1117b04f784eSNick Piggin 	 * lookup failed due to an unrelated rename.
11186e6b1bd1SAl Viro 	 *
1119b04f784eSNick Piggin 	 * This could use version numbering or similar to avoid unnecessary
1120b04f784eSNick Piggin 	 * cache lookups, but then we'd have to do the first lookup in the
1121b04f784eSNick Piggin 	 * non-racy way. However in the common case here, everything should
1122b04f784eSNick Piggin 	 * be hot in cache, so would it be a big win?
11236e6b1bd1SAl Viro 	 */
11246e6b1bd1SAl Viro 	dentry = d_lookup(parent, name);
1125baa03890SNick Piggin 	if (likely(!dentry)) {
1126baa03890SNick Piggin 		dentry = d_alloc_and_lookup(parent, name, nd);
11276e6b1bd1SAl Viro 		mutex_unlock(&dir->i_mutex);
11286e6b1bd1SAl Viro 		if (IS_ERR(dentry))
11296e6b1bd1SAl Viro 			goto fail;
11306e6b1bd1SAl Viro 		goto done;
11316e6b1bd1SAl Viro 	}
11326e6b1bd1SAl Viro 	/*
11336e6b1bd1SAl Viro 	 * Uhhuh! Nasty case: the cache was re-populated while
11346e6b1bd1SAl Viro 	 * we waited on the semaphore. Need to revalidate.
11356e6b1bd1SAl Viro 	 */
11366e6b1bd1SAl Viro 	mutex_unlock(&dir->i_mutex);
11372e2e88eaSNick Piggin 	goto found;
11381da177e4SLinus Torvalds 
11391da177e4SLinus Torvalds need_revalidate:
1140bcdc5e01SIan Kent 	dentry = do_revalidate(dentry, nd);
1141bcdc5e01SIan Kent 	if (!dentry)
11421da177e4SLinus Torvalds 		goto need_lookup;
1143bcdc5e01SIan Kent 	if (IS_ERR(dentry))
1144bcdc5e01SIan Kent 		goto fail;
1145bcdc5e01SIan Kent 	goto done;
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds fail:
11481da177e4SLinus Torvalds 	return PTR_ERR(dentry);
11491da177e4SLinus Torvalds }
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds /*
1152ac278a9cSAl Viro  * This is a temporary kludge to deal with "automount" symlinks; proper
1153ac278a9cSAl Viro  * solution is to trigger them on follow_mount(), so that do_lookup()
1154ac278a9cSAl Viro  * would DTRT.  To be killed before 2.6.34-final.
1155ac278a9cSAl Viro  */
1156ac278a9cSAl Viro static inline int follow_on_final(struct inode *inode, unsigned lookup_flags)
1157ac278a9cSAl Viro {
1158ac278a9cSAl Viro 	return inode && unlikely(inode->i_op->follow_link) &&
1159ac278a9cSAl Viro 		((lookup_flags & LOOKUP_FOLLOW) || S_ISDIR(inode->i_mode));
1160ac278a9cSAl Viro }
1161ac278a9cSAl Viro 
1162ac278a9cSAl Viro /*
11631da177e4SLinus Torvalds  * Name resolution.
1164ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1165ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
11661da177e4SLinus Torvalds  *
1167ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1168ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
11691da177e4SLinus Torvalds  */
11706de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
11711da177e4SLinus Torvalds {
11721da177e4SLinus Torvalds 	struct path next;
11731da177e4SLinus Torvalds 	int err;
11741da177e4SLinus Torvalds 	unsigned int lookup_flags = nd->flags;
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 	while (*name=='/')
11771da177e4SLinus Torvalds 		name++;
11781da177e4SLinus Torvalds 	if (!*name)
11791da177e4SLinus Torvalds 		goto return_reval;
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 	if (nd->depth)
1182f55eab82STrond Myklebust 		lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
11831da177e4SLinus Torvalds 
11841da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
11851da177e4SLinus Torvalds 	for(;;) {
118631e6b01fSNick Piggin 		struct inode *inode;
11871da177e4SLinus Torvalds 		unsigned long hash;
11881da177e4SLinus Torvalds 		struct qstr this;
11891da177e4SLinus Torvalds 		unsigned int c;
11901da177e4SLinus Torvalds 
1191cdce5d6bSTrond Myklebust 		nd->flags |= LOOKUP_CONTINUE;
119231e6b01fSNick Piggin 		if (nd->flags & LOOKUP_RCU) {
1193b74c79e9SNick Piggin 			err = exec_permission(nd->inode, IPERM_FLAG_RCU);
119431e6b01fSNick Piggin 			if (err == -ECHILD) {
119531e6b01fSNick Piggin 				if (nameidata_drop_rcu(nd))
119631e6b01fSNick Piggin 					return -ECHILD;
119731e6b01fSNick Piggin 				goto exec_again;
119831e6b01fSNick Piggin 			}
119931e6b01fSNick Piggin 		} else {
120031e6b01fSNick Piggin exec_again:
1201b74c79e9SNick Piggin 			err = exec_permission(nd->inode, 0);
120231e6b01fSNick Piggin 		}
12031da177e4SLinus Torvalds  		if (err)
12041da177e4SLinus Torvalds 			break;
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds 		this.name = name;
12071da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 		hash = init_name_hash();
12101da177e4SLinus Torvalds 		do {
12111da177e4SLinus Torvalds 			name++;
12121da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
12131da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
12141da177e4SLinus Torvalds 		} while (c && (c != '/'));
12151da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
12161da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 		/* remove trailing slashes? */
12191da177e4SLinus Torvalds 		if (!c)
12201da177e4SLinus Torvalds 			goto last_component;
12211da177e4SLinus Torvalds 		while (*++name == '/');
12221da177e4SLinus Torvalds 		if (!*name)
12231da177e4SLinus Torvalds 			goto last_with_slashes;
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds 		/*
12261da177e4SLinus Torvalds 		 * "." and ".." are special - ".." especially so because it has
12271da177e4SLinus Torvalds 		 * to be able to know about the current root directory and
12281da177e4SLinus Torvalds 		 * parent relationships.
12291da177e4SLinus Torvalds 		 */
12301da177e4SLinus Torvalds 		if (this.name[0] == '.') switch (this.len) {
12311da177e4SLinus Torvalds 			default:
12321da177e4SLinus Torvalds 				break;
12331da177e4SLinus Torvalds 			case 2:
12341da177e4SLinus Torvalds 				if (this.name[1] != '.')
12351da177e4SLinus Torvalds 					break;
123631e6b01fSNick Piggin 				if (nd->flags & LOOKUP_RCU) {
123731e6b01fSNick Piggin 					if (follow_dotdot_rcu(nd))
123831e6b01fSNick Piggin 						return -ECHILD;
123931e6b01fSNick Piggin 				} else
124058c465ebSAl Viro 					follow_dotdot(nd);
12411da177e4SLinus Torvalds 				/* fallthrough */
12421da177e4SLinus Torvalds 			case 1:
12431da177e4SLinus Torvalds 				continue;
12441da177e4SLinus Torvalds 		}
12451da177e4SLinus Torvalds 		/* This does the actual lookups.. */
124631e6b01fSNick Piggin 		err = do_lookup(nd, &this, &next, &inode);
12471da177e4SLinus Torvalds 		if (err)
12481da177e4SLinus Torvalds 			break;
12491da177e4SLinus Torvalds 		err = -ENOENT;
12501da177e4SLinus Torvalds 		if (!inode)
12511da177e4SLinus Torvalds 			goto out_dput;
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds 		if (inode->i_op->follow_link) {
125431e6b01fSNick Piggin 			/* We commonly drop rcu-walk here */
125531e6b01fSNick Piggin 			if (nameidata_dentry_drop_rcu_maybe(nd, next.dentry))
125631e6b01fSNick Piggin 				return -ECHILD;
125731e6b01fSNick Piggin 			BUG_ON(inode != next.dentry->d_inode);
125890ebe565SAl Viro 			err = do_follow_link(&next, nd);
12591da177e4SLinus Torvalds 			if (err)
12601da177e4SLinus Torvalds 				goto return_err;
126131e6b01fSNick Piggin 			nd->inode = nd->path.dentry->d_inode;
12621da177e4SLinus Torvalds 			err = -ENOENT;
126331e6b01fSNick Piggin 			if (!nd->inode)
12641da177e4SLinus Torvalds 				break;
126531e6b01fSNick Piggin 		} else {
126609dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
126731e6b01fSNick Piggin 			nd->inode = inode;
126831e6b01fSNick Piggin 		}
12691da177e4SLinus Torvalds 		err = -ENOTDIR;
127031e6b01fSNick Piggin 		if (!nd->inode->i_op->lookup)
12711da177e4SLinus Torvalds 			break;
12721da177e4SLinus Torvalds 		continue;
12731da177e4SLinus Torvalds 		/* here ends the main loop */
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds last_with_slashes:
12761da177e4SLinus Torvalds 		lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
12771da177e4SLinus Torvalds last_component:
1278f55eab82STrond Myklebust 		/* Clear LOOKUP_CONTINUE iff it was previously unset */
1279f55eab82STrond Myklebust 		nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
12801da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_PARENT)
12811da177e4SLinus Torvalds 			goto lookup_parent;
12821da177e4SLinus Torvalds 		if (this.name[0] == '.') switch (this.len) {
12831da177e4SLinus Torvalds 			default:
12841da177e4SLinus Torvalds 				break;
12851da177e4SLinus Torvalds 			case 2:
12861da177e4SLinus Torvalds 				if (this.name[1] != '.')
12871da177e4SLinus Torvalds 					break;
128831e6b01fSNick Piggin 				if (nd->flags & LOOKUP_RCU) {
128931e6b01fSNick Piggin 					if (follow_dotdot_rcu(nd))
129031e6b01fSNick Piggin 						return -ECHILD;
129131e6b01fSNick Piggin 				} else
129258c465ebSAl Viro 					follow_dotdot(nd);
12931da177e4SLinus Torvalds 				/* fallthrough */
12941da177e4SLinus Torvalds 			case 1:
12951da177e4SLinus Torvalds 				goto return_reval;
12961da177e4SLinus Torvalds 		}
129731e6b01fSNick Piggin 		err = do_lookup(nd, &this, &next, &inode);
12981da177e4SLinus Torvalds 		if (err)
12991da177e4SLinus Torvalds 			break;
1300ac278a9cSAl Viro 		if (follow_on_final(inode, lookup_flags)) {
130131e6b01fSNick Piggin 			if (nameidata_dentry_drop_rcu_maybe(nd, next.dentry))
130231e6b01fSNick Piggin 				return -ECHILD;
130331e6b01fSNick Piggin 			BUG_ON(inode != next.dentry->d_inode);
130490ebe565SAl Viro 			err = do_follow_link(&next, nd);
13051da177e4SLinus Torvalds 			if (err)
13061da177e4SLinus Torvalds 				goto return_err;
130731e6b01fSNick Piggin 			nd->inode = nd->path.dentry->d_inode;
130831e6b01fSNick Piggin 		} else {
130909dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
131031e6b01fSNick Piggin 			nd->inode = inode;
131131e6b01fSNick Piggin 		}
13121da177e4SLinus Torvalds 		err = -ENOENT;
131331e6b01fSNick Piggin 		if (!nd->inode)
13141da177e4SLinus Torvalds 			break;
13151da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_DIRECTORY) {
13161da177e4SLinus Torvalds 			err = -ENOTDIR;
131731e6b01fSNick Piggin 			if (!nd->inode->i_op->lookup)
13181da177e4SLinus Torvalds 				break;
13191da177e4SLinus Torvalds 		}
13201da177e4SLinus Torvalds 		goto return_base;
13211da177e4SLinus Torvalds lookup_parent:
13221da177e4SLinus Torvalds 		nd->last = this;
13231da177e4SLinus Torvalds 		nd->last_type = LAST_NORM;
13241da177e4SLinus Torvalds 		if (this.name[0] != '.')
13251da177e4SLinus Torvalds 			goto return_base;
13261da177e4SLinus Torvalds 		if (this.len == 1)
13271da177e4SLinus Torvalds 			nd->last_type = LAST_DOT;
13281da177e4SLinus Torvalds 		else if (this.len == 2 && this.name[1] == '.')
13291da177e4SLinus Torvalds 			nd->last_type = LAST_DOTDOT;
13301da177e4SLinus Torvalds 		else
13311da177e4SLinus Torvalds 			goto return_base;
13321da177e4SLinus Torvalds return_reval:
13331da177e4SLinus Torvalds 		/*
13341da177e4SLinus Torvalds 		 * We bypassed the ordinary revalidation routines.
13351da177e4SLinus Torvalds 		 * We may need to check the cached dentry for staleness.
13361da177e4SLinus Torvalds 		 */
1337fb045adbSNick Piggin 		if (need_reval_dot(nd->path.dentry)) {
13381da177e4SLinus Torvalds 			/* Note: we do not d_invalidate() */
133934286d66SNick Piggin 			err = d_revalidate(nd->path.dentry, nd);
134034286d66SNick Piggin 			if (!err)
134134286d66SNick Piggin 				err = -ESTALE;
134234286d66SNick Piggin 			if (err < 0)
13431da177e4SLinus Torvalds 				break;
13441da177e4SLinus Torvalds 		}
13451da177e4SLinus Torvalds return_base:
134631e6b01fSNick Piggin 		if (nameidata_drop_rcu_last_maybe(nd))
134731e6b01fSNick Piggin 			return -ECHILD;
13481da177e4SLinus Torvalds 		return 0;
13491da177e4SLinus Torvalds out_dput:
135031e6b01fSNick Piggin 		if (!(nd->flags & LOOKUP_RCU))
13511d957f9bSJan Blunck 			path_put_conditional(&next, nd);
13521da177e4SLinus Torvalds 		break;
13531da177e4SLinus Torvalds 	}
135431e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU))
13551d957f9bSJan Blunck 		path_put(&nd->path);
13561da177e4SLinus Torvalds return_err:
13571da177e4SLinus Torvalds 	return err;
13581da177e4SLinus Torvalds }
13591da177e4SLinus Torvalds 
136031e6b01fSNick Piggin static inline int path_walk_rcu(const char *name, struct nameidata *nd)
136131e6b01fSNick Piggin {
136231e6b01fSNick Piggin 	current->total_link_count = 0;
136331e6b01fSNick Piggin 
136431e6b01fSNick Piggin 	return link_path_walk(name, nd);
136531e6b01fSNick Piggin }
136631e6b01fSNick Piggin 
136731e6b01fSNick Piggin static inline int path_walk_simple(const char *name, struct nameidata *nd)
136831e6b01fSNick Piggin {
136931e6b01fSNick Piggin 	current->total_link_count = 0;
137031e6b01fSNick Piggin 
137131e6b01fSNick Piggin 	return link_path_walk(name, nd);
137231e6b01fSNick Piggin }
137331e6b01fSNick Piggin 
1374fc9b52cdSHarvey Harrison static int path_walk(const char *name, struct nameidata *nd)
13751da177e4SLinus Torvalds {
13766de88d72SAl Viro 	struct path save = nd->path;
13776de88d72SAl Viro 	int result;
13786de88d72SAl Viro 
13791da177e4SLinus Torvalds 	current->total_link_count = 0;
13806de88d72SAl Viro 
13816de88d72SAl Viro 	/* make sure the stuff we saved doesn't go away */
13826de88d72SAl Viro 	path_get(&save);
13836de88d72SAl Viro 
13846de88d72SAl Viro 	result = link_path_walk(name, nd);
13856de88d72SAl Viro 	if (result == -ESTALE) {
13866de88d72SAl Viro 		/* nd->path had been dropped */
13876de88d72SAl Viro 		current->total_link_count = 0;
13886de88d72SAl Viro 		nd->path = save;
13896de88d72SAl Viro 		path_get(&nd->path);
13906de88d72SAl Viro 		nd->flags |= LOOKUP_REVAL;
13916de88d72SAl Viro 		result = link_path_walk(name, nd);
13926de88d72SAl Viro 	}
13936de88d72SAl Viro 
13946de88d72SAl Viro 	path_put(&save);
13956de88d72SAl Viro 
13966de88d72SAl Viro 	return result;
13971da177e4SLinus Torvalds }
13981da177e4SLinus Torvalds 
139931e6b01fSNick Piggin static void path_finish_rcu(struct nameidata *nd)
140031e6b01fSNick Piggin {
140131e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
140231e6b01fSNick Piggin 		/* RCU dangling. Cancel it. */
140331e6b01fSNick Piggin 		nd->flags &= ~LOOKUP_RCU;
140431e6b01fSNick Piggin 		nd->root.mnt = NULL;
140531e6b01fSNick Piggin 		rcu_read_unlock();
140631e6b01fSNick Piggin 		br_read_unlock(vfsmount_lock);
140731e6b01fSNick Piggin 	}
140831e6b01fSNick Piggin 	if (nd->file)
140931e6b01fSNick Piggin 		fput(nd->file);
141031e6b01fSNick Piggin }
141131e6b01fSNick Piggin 
141231e6b01fSNick Piggin static int path_init_rcu(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
141331e6b01fSNick Piggin {
141431e6b01fSNick Piggin 	int retval = 0;
141531e6b01fSNick Piggin 	int fput_needed;
141631e6b01fSNick Piggin 	struct file *file;
141731e6b01fSNick Piggin 
141831e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
141931e6b01fSNick Piggin 	nd->flags = flags | LOOKUP_RCU;
142031e6b01fSNick Piggin 	nd->depth = 0;
142131e6b01fSNick Piggin 	nd->root.mnt = NULL;
142231e6b01fSNick Piggin 	nd->file = NULL;
142331e6b01fSNick Piggin 
142431e6b01fSNick Piggin 	if (*name=='/') {
142531e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
1426c28cc364SNick Piggin 		unsigned seq;
142731e6b01fSNick Piggin 
142831e6b01fSNick Piggin 		br_read_lock(vfsmount_lock);
142931e6b01fSNick Piggin 		rcu_read_lock();
143031e6b01fSNick Piggin 
1431c28cc364SNick Piggin 		do {
1432c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
143331e6b01fSNick Piggin 			nd->root = fs->root;
143431e6b01fSNick Piggin 			nd->path = nd->root;
1435c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1436c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
143731e6b01fSNick Piggin 
143831e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
143931e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
1440c28cc364SNick Piggin 		unsigned seq;
144131e6b01fSNick Piggin 
144231e6b01fSNick Piggin 		br_read_lock(vfsmount_lock);
144331e6b01fSNick Piggin 		rcu_read_lock();
144431e6b01fSNick Piggin 
1445c28cc364SNick Piggin 		do {
1446c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
144731e6b01fSNick Piggin 			nd->path = fs->pwd;
1448c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1449c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
1450c28cc364SNick Piggin 
145131e6b01fSNick Piggin 	} else {
145231e6b01fSNick Piggin 		struct dentry *dentry;
145331e6b01fSNick Piggin 
145431e6b01fSNick Piggin 		file = fget_light(dfd, &fput_needed);
145531e6b01fSNick Piggin 		retval = -EBADF;
145631e6b01fSNick Piggin 		if (!file)
145731e6b01fSNick Piggin 			goto out_fail;
145831e6b01fSNick Piggin 
145931e6b01fSNick Piggin 		dentry = file->f_path.dentry;
146031e6b01fSNick Piggin 
146131e6b01fSNick Piggin 		retval = -ENOTDIR;
146231e6b01fSNick Piggin 		if (!S_ISDIR(dentry->d_inode->i_mode))
146331e6b01fSNick Piggin 			goto fput_fail;
146431e6b01fSNick Piggin 
146531e6b01fSNick Piggin 		retval = file_permission(file, MAY_EXEC);
146631e6b01fSNick Piggin 		if (retval)
146731e6b01fSNick Piggin 			goto fput_fail;
146831e6b01fSNick Piggin 
146931e6b01fSNick Piggin 		nd->path = file->f_path;
147031e6b01fSNick Piggin 		if (fput_needed)
147131e6b01fSNick Piggin 			nd->file = file;
147231e6b01fSNick Piggin 
1473c28cc364SNick Piggin 		nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
147431e6b01fSNick Piggin 		br_read_lock(vfsmount_lock);
147531e6b01fSNick Piggin 		rcu_read_lock();
147631e6b01fSNick Piggin 	}
147731e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
147831e6b01fSNick Piggin 	return 0;
147931e6b01fSNick Piggin 
148031e6b01fSNick Piggin fput_fail:
148131e6b01fSNick Piggin 	fput_light(file, fput_needed);
148231e6b01fSNick Piggin out_fail:
148331e6b01fSNick Piggin 	return retval;
148431e6b01fSNick Piggin }
148531e6b01fSNick Piggin 
14869b4a9b14SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
14871da177e4SLinus Torvalds {
1488ea3834d9SPrasanna Meda 	int retval = 0;
1489170aa3d0SUlrich Drepper 	int fput_needed;
1490170aa3d0SUlrich Drepper 	struct file *file;
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
14931da177e4SLinus Torvalds 	nd->flags = flags;
14941da177e4SLinus Torvalds 	nd->depth = 0;
14952a737871SAl Viro 	nd->root.mnt = NULL;
14961da177e4SLinus Torvalds 
14971da177e4SLinus Torvalds 	if (*name=='/') {
14982a737871SAl Viro 		set_root(nd);
14992a737871SAl Viro 		nd->path = nd->root;
15002a737871SAl Viro 		path_get(&nd->root);
15015590ff0dSUlrich Drepper 	} else if (dfd == AT_FDCWD) {
1502f7ad3c6bSMiklos Szeredi 		get_fs_pwd(current->fs, &nd->path);
15035590ff0dSUlrich Drepper 	} else {
15045590ff0dSUlrich Drepper 		struct dentry *dentry;
15055590ff0dSUlrich Drepper 
15065590ff0dSUlrich Drepper 		file = fget_light(dfd, &fput_needed);
15075590ff0dSUlrich Drepper 		retval = -EBADF;
1508170aa3d0SUlrich Drepper 		if (!file)
15096d09bb62STrond Myklebust 			goto out_fail;
15105590ff0dSUlrich Drepper 
15110f7fc9e4SJosef "Jeff" Sipek 		dentry = file->f_path.dentry;
15125590ff0dSUlrich Drepper 
15135590ff0dSUlrich Drepper 		retval = -ENOTDIR;
1514170aa3d0SUlrich Drepper 		if (!S_ISDIR(dentry->d_inode->i_mode))
15156d09bb62STrond Myklebust 			goto fput_fail;
15165590ff0dSUlrich Drepper 
15175590ff0dSUlrich Drepper 		retval = file_permission(file, MAY_EXEC);
1518170aa3d0SUlrich Drepper 		if (retval)
15196d09bb62STrond Myklebust 			goto fput_fail;
15205590ff0dSUlrich Drepper 
15215dd784d0SJan Blunck 		nd->path = file->f_path;
15225dd784d0SJan Blunck 		path_get(&file->f_path);
15235590ff0dSUlrich Drepper 
15245590ff0dSUlrich Drepper 		fput_light(file, fput_needed);
15251da177e4SLinus Torvalds 	}
152631e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
15279b4a9b14SAl Viro 	return 0;
15282dfdd266SJosef 'Jeff' Sipek 
15299b4a9b14SAl Viro fput_fail:
15309b4a9b14SAl Viro 	fput_light(file, fput_needed);
15319b4a9b14SAl Viro out_fail:
15329b4a9b14SAl Viro 	return retval;
15339b4a9b14SAl Viro }
15349b4a9b14SAl Viro 
15359b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
15369b4a9b14SAl Viro static int do_path_lookup(int dfd, const char *name,
15379b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
15389b4a9b14SAl Viro {
153931e6b01fSNick Piggin 	int retval;
154031e6b01fSNick Piggin 
154131e6b01fSNick Piggin 	/*
154231e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
154331e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
154431e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
154531e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
154631e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
154731e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
154831e6b01fSNick Piggin 	 * analogue, foo_rcu().
154931e6b01fSNick Piggin 	 *
155031e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
155131e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
155231e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
155331e6b01fSNick Piggin 	 * be able to complete).
155431e6b01fSNick Piggin 	 */
155531e6b01fSNick Piggin 	retval = path_init_rcu(dfd, name, flags, nd);
155631e6b01fSNick Piggin 	if (unlikely(retval))
155731e6b01fSNick Piggin 		return retval;
155831e6b01fSNick Piggin 	retval = path_walk_rcu(name, nd);
155931e6b01fSNick Piggin 	path_finish_rcu(nd);
15602a737871SAl Viro 	if (nd->root.mnt) {
15612a737871SAl Viro 		path_put(&nd->root);
15622a737871SAl Viro 		nd->root.mnt = NULL;
15632a737871SAl Viro 	}
156431e6b01fSNick Piggin 
156531e6b01fSNick Piggin 	if (unlikely(retval == -ECHILD || retval == -ESTALE)) {
156631e6b01fSNick Piggin 		/* slower, locked walk */
156731e6b01fSNick Piggin 		if (retval == -ESTALE)
156831e6b01fSNick Piggin 			flags |= LOOKUP_REVAL;
156931e6b01fSNick Piggin 		retval = path_init(dfd, name, flags, nd);
157031e6b01fSNick Piggin 		if (unlikely(retval))
157131e6b01fSNick Piggin 			return retval;
157231e6b01fSNick Piggin 		retval = path_walk(name, nd);
157331e6b01fSNick Piggin 		if (nd->root.mnt) {
157431e6b01fSNick Piggin 			path_put(&nd->root);
157531e6b01fSNick Piggin 			nd->root.mnt = NULL;
157631e6b01fSNick Piggin 		}
157731e6b01fSNick Piggin 	}
157831e6b01fSNick Piggin 
157931e6b01fSNick Piggin 	if (likely(!retval)) {
158031e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
158131e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
158231e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
158331e6b01fSNick Piggin 		}
158431e6b01fSNick Piggin 	}
158531e6b01fSNick Piggin 
1586170aa3d0SUlrich Drepper 	return retval;
15871da177e4SLinus Torvalds }
15881da177e4SLinus Torvalds 
1589fc9b52cdSHarvey Harrison int path_lookup(const char *name, unsigned int flags,
15905590ff0dSUlrich Drepper 			struct nameidata *nd)
15915590ff0dSUlrich Drepper {
15925590ff0dSUlrich Drepper 	return do_path_lookup(AT_FDCWD, name, flags, nd);
15935590ff0dSUlrich Drepper }
15945590ff0dSUlrich Drepper 
1595d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1596d1811465SAl Viro {
1597d1811465SAl Viro 	struct nameidata nd;
1598d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1599d1811465SAl Viro 	if (!res)
1600d1811465SAl Viro 		*path = nd.path;
1601d1811465SAl Viro 	return res;
1602d1811465SAl Viro }
1603d1811465SAl Viro 
160416f18200SJosef 'Jeff' Sipek /**
160516f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
160616f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
160716f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
160816f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
160916f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
161016f18200SJosef 'Jeff' Sipek  * @nd: pointer to nameidata
161116f18200SJosef 'Jeff' Sipek  */
161216f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
161316f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
161416f18200SJosef 'Jeff' Sipek 		    struct nameidata *nd)
161516f18200SJosef 'Jeff' Sipek {
161616f18200SJosef 'Jeff' Sipek 	int retval;
161716f18200SJosef 'Jeff' Sipek 
161816f18200SJosef 'Jeff' Sipek 	/* same as do_path_lookup */
161916f18200SJosef 'Jeff' Sipek 	nd->last_type = LAST_ROOT;
162016f18200SJosef 'Jeff' Sipek 	nd->flags = flags;
162116f18200SJosef 'Jeff' Sipek 	nd->depth = 0;
162216f18200SJosef 'Jeff' Sipek 
1623c8e7f449SJan Blunck 	nd->path.dentry = dentry;
1624c8e7f449SJan Blunck 	nd->path.mnt = mnt;
1625c8e7f449SJan Blunck 	path_get(&nd->path);
16265b857119SAl Viro 	nd->root = nd->path;
16275b857119SAl Viro 	path_get(&nd->root);
162831e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
162916f18200SJosef 'Jeff' Sipek 
163016f18200SJosef 'Jeff' Sipek 	retval = path_walk(name, nd);
16314ac91378SJan Blunck 	if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
163231e6b01fSNick Piggin 				nd->inode))
16334ac91378SJan Blunck 		audit_inode(name, nd->path.dentry);
163416f18200SJosef 'Jeff' Sipek 
16352a737871SAl Viro 	path_put(&nd->root);
16362a737871SAl Viro 	nd->root.mnt = NULL;
163716f18200SJosef 'Jeff' Sipek 
16382a737871SAl Viro 	return retval;
163916f18200SJosef 'Jeff' Sipek }
164016f18200SJosef 'Jeff' Sipek 
1641eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1642eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
16431da177e4SLinus Torvalds {
164481fca444SChristoph Hellwig 	struct inode *inode = base->d_inode;
16451da177e4SLinus Torvalds 	struct dentry *dentry;
16461da177e4SLinus Torvalds 	int err;
16471da177e4SLinus Torvalds 
1648b74c79e9SNick Piggin 	err = exec_permission(inode, 0);
164981fca444SChristoph Hellwig 	if (err)
165081fca444SChristoph Hellwig 		return ERR_PTR(err);
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds 	/*
16531da177e4SLinus Torvalds 	 * See if the low-level filesystem might want
16541da177e4SLinus Torvalds 	 * to use its own hash..
16551da177e4SLinus Torvalds 	 */
1656fb045adbSNick Piggin 	if (base->d_flags & DCACHE_OP_HASH) {
1657b1e6a015SNick Piggin 		err = base->d_op->d_hash(base, inode, name);
16581da177e4SLinus Torvalds 		dentry = ERR_PTR(err);
16591da177e4SLinus Torvalds 		if (err < 0)
16601da177e4SLinus Torvalds 			goto out;
16611da177e4SLinus Torvalds 	}
16621da177e4SLinus Torvalds 
1663b04f784eSNick Piggin 	/*
1664b04f784eSNick Piggin 	 * Don't bother with __d_lookup: callers are for creat as
1665b04f784eSNick Piggin 	 * well as unlink, so a lot of the time it would cost
1666b04f784eSNick Piggin 	 * a double lookup.
16676e6b1bd1SAl Viro 	 */
16686e6b1bd1SAl Viro 	dentry = d_lookup(base, name);
16696e6b1bd1SAl Viro 
1670fb045adbSNick Piggin 	if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
16716e6b1bd1SAl Viro 		dentry = do_revalidate(dentry, nd);
16726e6b1bd1SAl Viro 
16731da177e4SLinus Torvalds 	if (!dentry)
1674baa03890SNick Piggin 		dentry = d_alloc_and_lookup(base, name, nd);
16751da177e4SLinus Torvalds out:
16761da177e4SLinus Torvalds 	return dentry;
16771da177e4SLinus Torvalds }
16781da177e4SLinus Torvalds 
1679057f6c01SJames Morris /*
1680057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1681057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1682057f6c01SJames Morris  * SMP-safe.
1683057f6c01SJames Morris  */
1684a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
16851da177e4SLinus Torvalds {
16864ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
16871da177e4SLinus Torvalds }
16881da177e4SLinus Torvalds 
1689eead1911SChristoph Hellwig static int __lookup_one_len(const char *name, struct qstr *this,
1690eead1911SChristoph Hellwig 		struct dentry *base, int len)
16911da177e4SLinus Torvalds {
16921da177e4SLinus Torvalds 	unsigned long hash;
16931da177e4SLinus Torvalds 	unsigned int c;
16941da177e4SLinus Torvalds 
1695057f6c01SJames Morris 	this->name = name;
1696057f6c01SJames Morris 	this->len = len;
16971da177e4SLinus Torvalds 	if (!len)
1698057f6c01SJames Morris 		return -EACCES;
16991da177e4SLinus Torvalds 
17001da177e4SLinus Torvalds 	hash = init_name_hash();
17011da177e4SLinus Torvalds 	while (len--) {
17021da177e4SLinus Torvalds 		c = *(const unsigned char *)name++;
17031da177e4SLinus Torvalds 		if (c == '/' || c == '\0')
1704057f6c01SJames Morris 			return -EACCES;
17051da177e4SLinus Torvalds 		hash = partial_name_hash(c, hash);
17061da177e4SLinus Torvalds 	}
1707057f6c01SJames Morris 	this->hash = end_name_hash(hash);
1708057f6c01SJames Morris 	return 0;
1709057f6c01SJames Morris }
17101da177e4SLinus Torvalds 
1711eead1911SChristoph Hellwig /**
1712a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1713eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1714eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1715eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1716eead1911SChristoph Hellwig  *
1717a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1718a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1719eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1720eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1721eead1911SChristoph Hellwig  */
1722057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1723057f6c01SJames Morris {
1724057f6c01SJames Morris 	int err;
1725057f6c01SJames Morris 	struct qstr this;
1726057f6c01SJames Morris 
17272f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
17282f9092e1SDavid Woodhouse 
1729057f6c01SJames Morris 	err = __lookup_one_len(name, &this, base, len);
1730057f6c01SJames Morris 	if (err)
1731057f6c01SJames Morris 		return ERR_PTR(err);
1732eead1911SChristoph Hellwig 
173349705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1734057f6c01SJames Morris }
1735057f6c01SJames Morris 
17362d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags,
17372d8f3038SAl Viro 		 struct path *path)
17381da177e4SLinus Torvalds {
17392d8f3038SAl Viro 	struct nameidata nd;
17401da177e4SLinus Torvalds 	char *tmp = getname(name);
17411da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
17421da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
17432d8f3038SAl Viro 
17442d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
17452d8f3038SAl Viro 
17462d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
17471da177e4SLinus Torvalds 		putname(tmp);
17482d8f3038SAl Viro 		if (!err)
17492d8f3038SAl Viro 			*path = nd.path;
17501da177e4SLinus Torvalds 	}
17511da177e4SLinus Torvalds 	return err;
17521da177e4SLinus Torvalds }
17531da177e4SLinus Torvalds 
17542ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
17552ad94ae6SAl Viro 			struct nameidata *nd, char **name)
17562ad94ae6SAl Viro {
17572ad94ae6SAl Viro 	char *s = getname(path);
17582ad94ae6SAl Viro 	int error;
17592ad94ae6SAl Viro 
17602ad94ae6SAl Viro 	if (IS_ERR(s))
17612ad94ae6SAl Viro 		return PTR_ERR(s);
17622ad94ae6SAl Viro 
17632ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
17642ad94ae6SAl Viro 	if (error)
17652ad94ae6SAl Viro 		putname(s);
17662ad94ae6SAl Viro 	else
17672ad94ae6SAl Viro 		*name = s;
17682ad94ae6SAl Viro 
17692ad94ae6SAl Viro 	return error;
17702ad94ae6SAl Viro }
17712ad94ae6SAl Viro 
17721da177e4SLinus Torvalds /*
17731da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
17741da177e4SLinus Torvalds  * minimal.
17751da177e4SLinus Torvalds  */
17761da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
17771da177e4SLinus Torvalds {
1778da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1779da9592edSDavid Howells 
17801da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
17811da177e4SLinus Torvalds 		return 0;
1782da9592edSDavid Howells 	if (inode->i_uid == fsuid)
17831da177e4SLinus Torvalds 		return 0;
1784da9592edSDavid Howells 	if (dir->i_uid == fsuid)
17851da177e4SLinus Torvalds 		return 0;
17861da177e4SLinus Torvalds 	return !capable(CAP_FOWNER);
17871da177e4SLinus Torvalds }
17881da177e4SLinus Torvalds 
17891da177e4SLinus Torvalds /*
17901da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
17911da177e4SLinus Torvalds  *  whether the type of victim is right.
17921da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
17931da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
17941da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
17951da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
17961da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
17971da177e4SLinus Torvalds  *	a. be owner of dir, or
17981da177e4SLinus Torvalds  *	b. be owner of victim, or
17991da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
18001da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
18011da177e4SLinus Torvalds  *     links pointing to it.
18021da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
18031da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
18041da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
18051da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
18061da177e4SLinus Torvalds  *     nfs_async_unlink().
18071da177e4SLinus Torvalds  */
1808858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
18091da177e4SLinus Torvalds {
18101da177e4SLinus Torvalds 	int error;
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds 	if (!victim->d_inode)
18131da177e4SLinus Torvalds 		return -ENOENT;
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
1816cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
18171da177e4SLinus Torvalds 
1818f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
18191da177e4SLinus Torvalds 	if (error)
18201da177e4SLinus Torvalds 		return error;
18211da177e4SLinus Torvalds 	if (IS_APPEND(dir))
18221da177e4SLinus Torvalds 		return -EPERM;
18231da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1824f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
18251da177e4SLinus Torvalds 		return -EPERM;
18261da177e4SLinus Torvalds 	if (isdir) {
18271da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
18281da177e4SLinus Torvalds 			return -ENOTDIR;
18291da177e4SLinus Torvalds 		if (IS_ROOT(victim))
18301da177e4SLinus Torvalds 			return -EBUSY;
18311da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
18321da177e4SLinus Torvalds 		return -EISDIR;
18331da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18341da177e4SLinus Torvalds 		return -ENOENT;
18351da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
18361da177e4SLinus Torvalds 		return -EBUSY;
18371da177e4SLinus Torvalds 	return 0;
18381da177e4SLinus Torvalds }
18391da177e4SLinus Torvalds 
18401da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
18411da177e4SLinus Torvalds  *  dir.
18421da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
18431da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
18441da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
18451da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
18461da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
18471da177e4SLinus Torvalds  */
1848a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
18491da177e4SLinus Torvalds {
18501da177e4SLinus Torvalds 	if (child->d_inode)
18511da177e4SLinus Torvalds 		return -EEXIST;
18521da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18531da177e4SLinus Torvalds 		return -ENOENT;
1854f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
18551da177e4SLinus Torvalds }
18561da177e4SLinus Torvalds 
18571da177e4SLinus Torvalds /*
18581da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
18591da177e4SLinus Torvalds  */
18601da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
18611da177e4SLinus Torvalds {
18621da177e4SLinus Torvalds 	struct dentry *p;
18631da177e4SLinus Torvalds 
18641da177e4SLinus Torvalds 	if (p1 == p2) {
1865f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
18661da177e4SLinus Torvalds 		return NULL;
18671da177e4SLinus Torvalds 	}
18681da177e4SLinus Torvalds 
1869a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
18701da177e4SLinus Torvalds 
1871e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1872e2761a11SOGAWA Hirofumi 	if (p) {
1873f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1874f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
18751da177e4SLinus Torvalds 		return p;
18761da177e4SLinus Torvalds 	}
18771da177e4SLinus Torvalds 
1878e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1879e2761a11SOGAWA Hirofumi 	if (p) {
1880f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1881f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
18821da177e4SLinus Torvalds 		return p;
18831da177e4SLinus Torvalds 	}
18841da177e4SLinus Torvalds 
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 NULL;
18881da177e4SLinus Torvalds }
18891da177e4SLinus Torvalds 
18901da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
18911da177e4SLinus Torvalds {
18921b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
18931da177e4SLinus Torvalds 	if (p1 != p2) {
18941b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1895a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
18961da177e4SLinus Torvalds 	}
18971da177e4SLinus Torvalds }
18981da177e4SLinus Torvalds 
18991da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
19001da177e4SLinus Torvalds 		struct nameidata *nd)
19011da177e4SLinus Torvalds {
1902a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
19031da177e4SLinus Torvalds 
19041da177e4SLinus Torvalds 	if (error)
19051da177e4SLinus Torvalds 		return error;
19061da177e4SLinus Torvalds 
1907acfa4380SAl Viro 	if (!dir->i_op->create)
19081da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
19091da177e4SLinus Torvalds 	mode &= S_IALLUGO;
19101da177e4SLinus Torvalds 	mode |= S_IFREG;
19111da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
19121da177e4SLinus Torvalds 	if (error)
19131da177e4SLinus Torvalds 		return error;
19141da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
1915a74574aaSStephen Smalley 	if (!error)
1916f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
19171da177e4SLinus Torvalds 	return error;
19181da177e4SLinus Torvalds }
19191da177e4SLinus Torvalds 
19203fb64190SChristoph Hellwig int may_open(struct path *path, int acc_mode, int flag)
19211da177e4SLinus Torvalds {
19223fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
19231da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
19241da177e4SLinus Torvalds 	int error;
19251da177e4SLinus Torvalds 
19261da177e4SLinus Torvalds 	if (!inode)
19271da177e4SLinus Torvalds 		return -ENOENT;
19281da177e4SLinus Torvalds 
1929c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
1930c8fe8f30SChristoph Hellwig 	case S_IFLNK:
19311da177e4SLinus Torvalds 		return -ELOOP;
1932c8fe8f30SChristoph Hellwig 	case S_IFDIR:
1933c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
19341da177e4SLinus Torvalds 			return -EISDIR;
1935c8fe8f30SChristoph Hellwig 		break;
1936c8fe8f30SChristoph Hellwig 	case S_IFBLK:
1937c8fe8f30SChristoph Hellwig 	case S_IFCHR:
19383fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
19391da177e4SLinus Torvalds 			return -EACCES;
1940c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
1941c8fe8f30SChristoph Hellwig 	case S_IFIFO:
1942c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
19431da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
1944c8fe8f30SChristoph Hellwig 		break;
19454a3fd211SDave Hansen 	}
1946b41572e9SDave Hansen 
19473fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
1948b41572e9SDave Hansen 	if (error)
1949b41572e9SDave Hansen 		return error;
19506146f0d5SMimi Zohar 
19511da177e4SLinus Torvalds 	/*
19521da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
19531da177e4SLinus Torvalds 	 */
19541da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
19558737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
19567715b521SAl Viro 			return -EPERM;
19571da177e4SLinus Torvalds 		if (flag & O_TRUNC)
19587715b521SAl Viro 			return -EPERM;
19591da177e4SLinus Torvalds 	}
19601da177e4SLinus Torvalds 
19611da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
19627715b521SAl Viro 	if (flag & O_NOATIME && !is_owner_or_cap(inode))
19637715b521SAl Viro 		return -EPERM;
19641da177e4SLinus Torvalds 
19651da177e4SLinus Torvalds 	/*
19661da177e4SLinus Torvalds 	 * Ensure there are no outstanding leases on the file.
19671da177e4SLinus Torvalds 	 */
1968b65a9cfcSAl Viro 	return break_lease(inode, flag);
19697715b521SAl Viro }
19707715b521SAl Viro 
19717715b521SAl Viro static int handle_truncate(struct path *path)
19727715b521SAl Viro {
19737715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
19747715b521SAl Viro 	int error = get_write_access(inode);
19751da177e4SLinus Torvalds 	if (error)
19767715b521SAl Viro 		return error;
19771da177e4SLinus Torvalds 	/*
19781da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
19791da177e4SLinus Torvalds 	 */
19801da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
1981be6d3e56SKentaro Takeda 	if (!error)
1982ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
19831da177e4SLinus Torvalds 	if (!error) {
19847715b521SAl Viro 		error = do_truncate(path->dentry, 0,
1985d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
1986d139d7ffSMiklos Szeredi 				    NULL);
19871da177e4SLinus Torvalds 	}
19881da177e4SLinus Torvalds 	put_write_access(inode);
1989acd0c935SMimi Zohar 	return error;
19901da177e4SLinus Torvalds }
19911da177e4SLinus Torvalds 
1992d57999e1SDave Hansen /*
1993d57999e1SDave Hansen  * Be careful about ever adding any more callers of this
1994d57999e1SDave Hansen  * function.  Its flags must be in the namei format, not
1995d57999e1SDave Hansen  * what get passed to sys_open().
1996d57999e1SDave Hansen  */
1997d57999e1SDave Hansen static int __open_namei_create(struct nameidata *nd, struct path *path,
19988737c930SAl Viro 				int open_flag, int mode)
1999aab520e2SDave Hansen {
2000aab520e2SDave Hansen 	int error;
20014ac91378SJan Blunck 	struct dentry *dir = nd->path.dentry;
2002aab520e2SDave Hansen 
2003aab520e2SDave Hansen 	if (!IS_POSIXACL(dir->d_inode))
2004ce3b0f8dSAl Viro 		mode &= ~current_umask();
2005be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd->path, path->dentry, mode, 0);
2006be6d3e56SKentaro Takeda 	if (error)
2007be6d3e56SKentaro Takeda 		goto out_unlock;
2008aab520e2SDave Hansen 	error = vfs_create(dir->d_inode, path->dentry, mode, nd);
2009be6d3e56SKentaro Takeda out_unlock:
2010aab520e2SDave Hansen 	mutex_unlock(&dir->d_inode->i_mutex);
20114ac91378SJan Blunck 	dput(nd->path.dentry);
20124ac91378SJan Blunck 	nd->path.dentry = path->dentry;
201331e6b01fSNick Piggin 
2014aab520e2SDave Hansen 	if (error)
2015aab520e2SDave Hansen 		return error;
2016aab520e2SDave Hansen 	/* Don't check for write permission, don't truncate */
20178737c930SAl Viro 	return may_open(&nd->path, 0, open_flag & ~O_TRUNC);
2018aab520e2SDave Hansen }
2019aab520e2SDave Hansen 
20201da177e4SLinus Torvalds /*
2021d57999e1SDave Hansen  * Note that while the flag value (low two bits) for sys_open means:
2022d57999e1SDave Hansen  *	00 - read-only
2023d57999e1SDave Hansen  *	01 - write-only
2024d57999e1SDave Hansen  *	10 - read-write
2025d57999e1SDave Hansen  *	11 - special
2026d57999e1SDave Hansen  * it is changed into
2027d57999e1SDave Hansen  *	00 - no permissions needed
2028d57999e1SDave Hansen  *	01 - read-permission
2029d57999e1SDave Hansen  *	10 - write-permission
2030d57999e1SDave Hansen  *	11 - read-write
2031d57999e1SDave Hansen  * for the internal routines (ie open_namei()/follow_link() etc)
2032d57999e1SDave Hansen  * This is more logical, and also allows the 00 "no perm needed"
2033d57999e1SDave Hansen  * to be used for symlinks (where the permissions are checked
2034d57999e1SDave Hansen  * later).
2035d57999e1SDave Hansen  *
2036d57999e1SDave Hansen */
2037d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2038d57999e1SDave Hansen {
2039d57999e1SDave Hansen 	if ((flag+1) & O_ACCMODE)
2040d57999e1SDave Hansen 		flag++;
2041d57999e1SDave Hansen 	return flag;
2042d57999e1SDave Hansen }
2043d57999e1SDave Hansen 
20447715b521SAl Viro static int open_will_truncate(int flag, struct inode *inode)
20454a3fd211SDave Hansen {
2046d57999e1SDave Hansen 	/*
20474a3fd211SDave Hansen 	 * We'll never write to the fs underlying
20484a3fd211SDave Hansen 	 * a device file.
20494a3fd211SDave Hansen 	 */
20504a3fd211SDave Hansen 	if (special_file(inode->i_mode))
20514a3fd211SDave Hansen 		return 0;
20524a3fd211SDave Hansen 	return (flag & O_TRUNC);
20534a3fd211SDave Hansen }
20544a3fd211SDave Hansen 
2055648fa861SAl Viro static struct file *finish_open(struct nameidata *nd,
20569a66179eSAl Viro 				int open_flag, int acc_mode)
2057648fa861SAl Viro {
2058648fa861SAl Viro 	struct file *filp;
2059648fa861SAl Viro 	int will_truncate;
2060648fa861SAl Viro 	int error;
2061648fa861SAl Viro 
20629a66179eSAl Viro 	will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode);
2063648fa861SAl Viro 	if (will_truncate) {
2064648fa861SAl Viro 		error = mnt_want_write(nd->path.mnt);
2065648fa861SAl Viro 		if (error)
2066648fa861SAl Viro 			goto exit;
2067648fa861SAl Viro 	}
2068648fa861SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2069648fa861SAl Viro 	if (error) {
2070648fa861SAl Viro 		if (will_truncate)
2071648fa861SAl Viro 			mnt_drop_write(nd->path.mnt);
2072648fa861SAl Viro 		goto exit;
2073648fa861SAl Viro 	}
2074648fa861SAl Viro 	filp = nameidata_to_filp(nd);
2075648fa861SAl Viro 	if (!IS_ERR(filp)) {
2076648fa861SAl Viro 		error = ima_file_check(filp, acc_mode);
2077648fa861SAl Viro 		if (error) {
2078648fa861SAl Viro 			fput(filp);
2079648fa861SAl Viro 			filp = ERR_PTR(error);
2080648fa861SAl Viro 		}
2081648fa861SAl Viro 	}
2082648fa861SAl Viro 	if (!IS_ERR(filp)) {
2083648fa861SAl Viro 		if (will_truncate) {
2084648fa861SAl Viro 			error = handle_truncate(&nd->path);
2085648fa861SAl Viro 			if (error) {
2086648fa861SAl Viro 				fput(filp);
2087648fa861SAl Viro 				filp = ERR_PTR(error);
2088648fa861SAl Viro 			}
2089648fa861SAl Viro 		}
2090648fa861SAl Viro 	}
2091648fa861SAl Viro 	/*
2092648fa861SAl Viro 	 * It is now safe to drop the mnt write
2093648fa861SAl Viro 	 * because the filp has had a write taken
2094648fa861SAl Viro 	 * on its behalf.
2095648fa861SAl Viro 	 */
2096648fa861SAl Viro 	if (will_truncate)
2097648fa861SAl Viro 		mnt_drop_write(nd->path.mnt);
2098d893f1bcSAl Viro 	path_put(&nd->path);
2099648fa861SAl Viro 	return filp;
2100648fa861SAl Viro 
2101648fa861SAl Viro exit:
2102648fa861SAl Viro 	if (!IS_ERR(nd->intent.open.file))
2103648fa861SAl Viro 		release_open_intent(nd);
2104648fa861SAl Viro 	path_put(&nd->path);
2105648fa861SAl Viro 	return ERR_PTR(error);
2106648fa861SAl Viro }
2107648fa861SAl Viro 
210831e6b01fSNick Piggin /*
210931e6b01fSNick Piggin  * Handle O_CREAT case for do_filp_open
211031e6b01fSNick Piggin  */
2111fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
21125b369df8SAl Viro 			    int open_flag, int acc_mode,
21133e297b61SAl Viro 			    int mode, const char *pathname)
2114fb1cc555SAl Viro {
2115a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
2116fb1cc555SAl Viro 	struct file *filp;
21171f36f774SAl Viro 	int error = -EISDIR;
2118fb1cc555SAl Viro 
21191f36f774SAl Viro 	switch (nd->last_type) {
21201f36f774SAl Viro 	case LAST_DOTDOT:
21211f36f774SAl Viro 		follow_dotdot(nd);
21221f36f774SAl Viro 		dir = nd->path.dentry;
2123176306f5SNeil Brown 	case LAST_DOT:
2124fb045adbSNick Piggin 		if (need_reval_dot(dir)) {
212534286d66SNick Piggin 			error = d_revalidate(nd->path.dentry, nd);
212634286d66SNick Piggin 			if (!error)
21271f36f774SAl Viro 				error = -ESTALE;
212834286d66SNick Piggin 			if (error < 0)
2129a2c36b45SAl Viro 				goto exit;
21301f36f774SAl Viro 		}
21311f36f774SAl Viro 		/* fallthrough */
21321f36f774SAl Viro 	case LAST_ROOT:
21331f36f774SAl Viro 		goto exit;
21341f36f774SAl Viro 	case LAST_BIND:
21351f36f774SAl Viro 		audit_inode(pathname, dir);
21361f36f774SAl Viro 		goto ok;
21371f36f774SAl Viro 	}
2138a2c36b45SAl Viro 
21391f36f774SAl Viro 	/* trailing slashes? */
214031e6b01fSNick Piggin 	if (nd->last.name[nd->last.len])
21411f36f774SAl Viro 		goto exit;
21421f36f774SAl Viro 
2143a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
2144a1e28038SAl Viro 
2145a1e28038SAl Viro 	path->dentry = lookup_hash(nd);
2146a1e28038SAl Viro 	path->mnt = nd->path.mnt;
2147a1e28038SAl Viro 
2148fb1cc555SAl Viro 	error = PTR_ERR(path->dentry);
2149fb1cc555SAl Viro 	if (IS_ERR(path->dentry)) {
2150fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
2151fb1cc555SAl Viro 		goto exit;
2152fb1cc555SAl Viro 	}
2153fb1cc555SAl Viro 
2154fb1cc555SAl Viro 	if (IS_ERR(nd->intent.open.file)) {
2155fb1cc555SAl Viro 		error = PTR_ERR(nd->intent.open.file);
2156fb1cc555SAl Viro 		goto exit_mutex_unlock;
2157fb1cc555SAl Viro 	}
2158fb1cc555SAl Viro 
2159fb1cc555SAl Viro 	/* Negative dentry, just create the file */
2160fb1cc555SAl Viro 	if (!path->dentry->d_inode) {
2161fb1cc555SAl Viro 		/*
2162fb1cc555SAl Viro 		 * This write is needed to ensure that a
2163fb1cc555SAl Viro 		 * ro->rw transition does not occur between
2164fb1cc555SAl Viro 		 * the time when the file is created and when
2165fb1cc555SAl Viro 		 * a permanent write count is taken through
2166fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
2167fb1cc555SAl Viro 		 */
2168fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
2169fb1cc555SAl Viro 		if (error)
2170fb1cc555SAl Viro 			goto exit_mutex_unlock;
2171fb1cc555SAl Viro 		error = __open_namei_create(nd, path, open_flag, mode);
2172fb1cc555SAl Viro 		if (error) {
2173fb1cc555SAl Viro 			mnt_drop_write(nd->path.mnt);
2174fb1cc555SAl Viro 			goto exit;
2175fb1cc555SAl Viro 		}
2176fb1cc555SAl Viro 		filp = nameidata_to_filp(nd);
2177fb1cc555SAl Viro 		mnt_drop_write(nd->path.mnt);
2178d893f1bcSAl Viro 		path_put(&nd->path);
2179fb1cc555SAl Viro 		if (!IS_ERR(filp)) {
2180fb1cc555SAl Viro 			error = ima_file_check(filp, acc_mode);
2181fb1cc555SAl Viro 			if (error) {
2182fb1cc555SAl Viro 				fput(filp);
2183fb1cc555SAl Viro 				filp = ERR_PTR(error);
2184fb1cc555SAl Viro 			}
2185fb1cc555SAl Viro 		}
2186fb1cc555SAl Viro 		return filp;
2187fb1cc555SAl Viro 	}
2188fb1cc555SAl Viro 
2189fb1cc555SAl Viro 	/*
2190fb1cc555SAl Viro 	 * It already exists.
2191fb1cc555SAl Viro 	 */
2192fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2193fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2194fb1cc555SAl Viro 
2195fb1cc555SAl Viro 	error = -EEXIST;
21965b369df8SAl Viro 	if (open_flag & O_EXCL)
2197fb1cc555SAl Viro 		goto exit_dput;
2198fb1cc555SAl Viro 
2199fb1cc555SAl Viro 	if (__follow_mount(path)) {
2200fb1cc555SAl Viro 		error = -ELOOP;
22015b369df8SAl Viro 		if (open_flag & O_NOFOLLOW)
2202fb1cc555SAl Viro 			goto exit_dput;
2203fb1cc555SAl Viro 	}
2204fb1cc555SAl Viro 
2205fb1cc555SAl Viro 	error = -ENOENT;
2206fb1cc555SAl Viro 	if (!path->dentry->d_inode)
2207fb1cc555SAl Viro 		goto exit_dput;
22089e67f361SAl Viro 
22099e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
2210fb1cc555SAl Viro 		return NULL;
2211fb1cc555SAl Viro 
2212fb1cc555SAl Viro 	path_to_nameidata(path, nd);
221331e6b01fSNick Piggin 	nd->inode = path->dentry->d_inode;
2214fb1cc555SAl Viro 	error = -EISDIR;
221531e6b01fSNick Piggin 	if (S_ISDIR(nd->inode->i_mode))
2216fb1cc555SAl Viro 		goto exit;
221767ee3ad2SAl Viro ok:
22189a66179eSAl Viro 	filp = finish_open(nd, open_flag, acc_mode);
2219fb1cc555SAl Viro 	return filp;
2220fb1cc555SAl Viro 
2221fb1cc555SAl Viro exit_mutex_unlock:
2222fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2223fb1cc555SAl Viro exit_dput:
2224fb1cc555SAl Viro 	path_put_conditional(path, nd);
2225fb1cc555SAl Viro exit:
2226fb1cc555SAl Viro 	if (!IS_ERR(nd->intent.open.file))
2227fb1cc555SAl Viro 		release_open_intent(nd);
2228fb1cc555SAl Viro 	path_put(&nd->path);
2229fb1cc555SAl Viro 	return ERR_PTR(error);
2230fb1cc555SAl Viro }
2231fb1cc555SAl Viro 
22324a3fd211SDave Hansen /*
22334a3fd211SDave Hansen  * Note that the low bits of the passed in "open_flag"
22344a3fd211SDave Hansen  * are not the same as in the local variable "flag". See
22354a3fd211SDave Hansen  * open_to_namei_flags() for more details.
22361da177e4SLinus Torvalds  */
2237a70e65dfSChristoph Hellwig struct file *do_filp_open(int dfd, const char *pathname,
22386e8341a1SAl Viro 		int open_flag, int mode, int acc_mode)
22391da177e4SLinus Torvalds {
22404a3fd211SDave Hansen 	struct file *filp;
2241a70e65dfSChristoph Hellwig 	struct nameidata nd;
22426e8341a1SAl Viro 	int error;
22439850c056SAl Viro 	struct path path;
22441da177e4SLinus Torvalds 	int count = 0;
2245d57999e1SDave Hansen 	int flag = open_to_namei_flags(open_flag);
224631e6b01fSNick Piggin 	int flags;
22471f36f774SAl Viro 
22481f36f774SAl Viro 	if (!(open_flag & O_CREAT))
22491f36f774SAl Viro 		mode = 0;
22501da177e4SLinus Torvalds 
2251b1085ba8SLino Sanfilippo 	/* Must never be set by userspace */
2252b1085ba8SLino Sanfilippo 	open_flag &= ~FMODE_NONOTIFY;
2253b1085ba8SLino Sanfilippo 
22546b2f3d1fSChristoph Hellwig 	/*
22556b2f3d1fSChristoph Hellwig 	 * O_SYNC is implemented as __O_SYNC|O_DSYNC.  As many places only
22566b2f3d1fSChristoph Hellwig 	 * check for O_DSYNC if the need any syncing at all we enforce it's
22576b2f3d1fSChristoph Hellwig 	 * always set instead of having to deal with possibly weird behaviour
22586b2f3d1fSChristoph Hellwig 	 * for malicious applications setting only __O_SYNC.
22596b2f3d1fSChristoph Hellwig 	 */
22606b2f3d1fSChristoph Hellwig 	if (open_flag & __O_SYNC)
22616b2f3d1fSChristoph Hellwig 		open_flag |= O_DSYNC;
22626b2f3d1fSChristoph Hellwig 
22636e8341a1SAl Viro 	if (!acc_mode)
22646d125529SAl Viro 		acc_mode = MAY_OPEN | ACC_MODE(open_flag);
22651da177e4SLinus Torvalds 
2266834f2a4aSTrond Myklebust 	/* O_TRUNC implies we need access checks for write permissions */
22674296e2cbSAl Viro 	if (open_flag & O_TRUNC)
2268834f2a4aSTrond Myklebust 		acc_mode |= MAY_WRITE;
2269834f2a4aSTrond Myklebust 
22701da177e4SLinus Torvalds 	/* Allow the LSM permission hook to distinguish append
22711da177e4SLinus Torvalds 	   access from general write access. */
22724296e2cbSAl Viro 	if (open_flag & O_APPEND)
22731da177e4SLinus Torvalds 		acc_mode |= MAY_APPEND;
22741da177e4SLinus Torvalds 
227531e6b01fSNick Piggin 	flags = LOOKUP_OPEN;
227631e6b01fSNick Piggin 	if (open_flag & O_CREAT) {
227731e6b01fSNick Piggin 		flags |= LOOKUP_CREATE;
227831e6b01fSNick Piggin 		if (open_flag & O_EXCL)
227931e6b01fSNick Piggin 			flags |= LOOKUP_EXCL;
2280654f562cSJ. R. Okajima 	}
228131e6b01fSNick Piggin 	if (open_flag & O_DIRECTORY)
228231e6b01fSNick Piggin 		flags |= LOOKUP_DIRECTORY;
228331e6b01fSNick Piggin 	if (!(open_flag & O_NOFOLLOW))
228431e6b01fSNick Piggin 		flags |= LOOKUP_FOLLOW;
228531e6b01fSNick Piggin 
228631e6b01fSNick Piggin 	filp = get_empty_filp();
228731e6b01fSNick Piggin 	if (!filp)
228831e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
228931e6b01fSNick Piggin 
229031e6b01fSNick Piggin 	filp->f_flags = open_flag;
229131e6b01fSNick Piggin 	nd.intent.open.file = filp;
229231e6b01fSNick Piggin 	nd.intent.open.flags = flag;
229331e6b01fSNick Piggin 	nd.intent.open.create_mode = mode;
229431e6b01fSNick Piggin 
229531e6b01fSNick Piggin 	if (open_flag & O_CREAT)
229631e6b01fSNick Piggin 		goto creat;
229731e6b01fSNick Piggin 
229831e6b01fSNick Piggin 	/* !O_CREAT, simple open */
229931e6b01fSNick Piggin 	error = do_path_lookup(dfd, pathname, flags, &nd);
230031e6b01fSNick Piggin 	if (unlikely(error))
230131e6b01fSNick Piggin 		goto out_filp;
230231e6b01fSNick Piggin 	error = -ELOOP;
230331e6b01fSNick Piggin 	if (!(nd.flags & LOOKUP_FOLLOW)) {
230431e6b01fSNick Piggin 		if (nd.inode->i_op->follow_link)
230531e6b01fSNick Piggin 			goto out_path;
230631e6b01fSNick Piggin 	}
230731e6b01fSNick Piggin 	error = -ENOTDIR;
230831e6b01fSNick Piggin 	if (nd.flags & LOOKUP_DIRECTORY) {
230931e6b01fSNick Piggin 		if (!nd.inode->i_op->lookup)
231031e6b01fSNick Piggin 			goto out_path;
231131e6b01fSNick Piggin 	}
231231e6b01fSNick Piggin 	audit_inode(pathname, nd.path.dentry);
231331e6b01fSNick Piggin 	filp = finish_open(&nd, open_flag, acc_mode);
231431e6b01fSNick Piggin 	return filp;
231531e6b01fSNick Piggin 
231631e6b01fSNick Piggin creat:
231731e6b01fSNick Piggin 	/* OK, have to create the file. Find the parent. */
231831e6b01fSNick Piggin 	error = path_init_rcu(dfd, pathname,
231931e6b01fSNick Piggin 			LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd);
232031e6b01fSNick Piggin 	if (error)
232131e6b01fSNick Piggin 		goto out_filp;
232231e6b01fSNick Piggin 	error = path_walk_rcu(pathname, &nd);
232331e6b01fSNick Piggin 	path_finish_rcu(&nd);
232431e6b01fSNick Piggin 	if (unlikely(error == -ECHILD || error == -ESTALE)) {
232531e6b01fSNick Piggin 		/* slower, locked walk */
232631e6b01fSNick Piggin 		if (error == -ESTALE) {
232731e6b01fSNick Piggin reval:
232831e6b01fSNick Piggin 			flags |= LOOKUP_REVAL;
232931e6b01fSNick Piggin 		}
233031e6b01fSNick Piggin 		error = path_init(dfd, pathname,
233131e6b01fSNick Piggin 				LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd);
233231e6b01fSNick Piggin 		if (error)
233331e6b01fSNick Piggin 			goto out_filp;
233431e6b01fSNick Piggin 
233531e6b01fSNick Piggin 		error = path_walk_simple(pathname, &nd);
233631e6b01fSNick Piggin 	}
233731e6b01fSNick Piggin 	if (unlikely(error))
233831e6b01fSNick Piggin 		goto out_filp;
233931e6b01fSNick Piggin 	if (unlikely(!audit_dummy_context()))
23409b4a9b14SAl Viro 		audit_inode(pathname, nd.path.dentry);
23411da177e4SLinus Torvalds 
23421da177e4SLinus Torvalds 	/*
2343a2c36b45SAl Viro 	 * We have the parent and last component.
23441da177e4SLinus Torvalds 	 */
234531e6b01fSNick Piggin 	nd.flags = flags;
23463e297b61SAl Viro 	filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
2347806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
2348def4af30SAl Viro 		struct path holder;
2349def4af30SAl Viro 		void *cookie;
2350806b681cSAl Viro 		error = -ELOOP;
23511f36f774SAl Viro 		/* S_ISDIR part is a temporary automount kludge */
235231e6b01fSNick Piggin 		if (!(nd.flags & LOOKUP_FOLLOW) && !S_ISDIR(nd.inode->i_mode))
23531f36f774SAl Viro 			goto exit_dput;
23541f36f774SAl Viro 		if (count++ == 32)
2355806b681cSAl Viro 			goto exit_dput;
2356806b681cSAl Viro 		/*
2357806b681cSAl Viro 		 * This is subtle. Instead of calling do_follow_link() we do
2358806b681cSAl Viro 		 * the thing by hands. The reason is that this way we have zero
2359806b681cSAl Viro 		 * link_count and path_walk() (called from ->follow_link)
2360806b681cSAl Viro 		 * honoring LOOKUP_PARENT.  After that we have the parent and
2361806b681cSAl Viro 		 * last component, i.e. we are in the same situation as after
2362806b681cSAl Viro 		 * the first path_walk().  Well, almost - if the last component
2363806b681cSAl Viro 		 * is normal we get its copy stored in nd->last.name and we will
2364806b681cSAl Viro 		 * have to putname() it when we are done. Procfs-like symlinks
2365806b681cSAl Viro 		 * just set LAST_BIND.
2366806b681cSAl Viro 		 */
2367806b681cSAl Viro 		nd.flags |= LOOKUP_PARENT;
2368806b681cSAl Viro 		error = security_inode_follow_link(path.dentry, &nd);
2369806b681cSAl Viro 		if (error)
2370806b681cSAl Viro 			goto exit_dput;
2371def4af30SAl Viro 		error = __do_follow_link(&path, &nd, &cookie);
2372def4af30SAl Viro 		if (unlikely(error)) {
237331e6b01fSNick Piggin 			if (!IS_ERR(cookie) && nd.inode->i_op->put_link)
237431e6b01fSNick Piggin 				nd.inode->i_op->put_link(path.dentry, &nd, cookie);
2375806b681cSAl Viro 			/* nd.path had been dropped */
237631e6b01fSNick Piggin 			nd.path = path;
237731e6b01fSNick Piggin 			goto out_path;
2378806b681cSAl Viro 		}
2379def4af30SAl Viro 		holder = path;
2380806b681cSAl Viro 		nd.flags &= ~LOOKUP_PARENT;
23813e297b61SAl Viro 		filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
238231e6b01fSNick Piggin 		if (nd.inode->i_op->put_link)
238331e6b01fSNick Piggin 			nd.inode->i_op->put_link(holder.dentry, &nd, cookie);
2384def4af30SAl Viro 		path_put(&holder);
2385806b681cSAl Viro 	}
238610fa8e62SAl Viro out:
23872a737871SAl Viro 	if (nd.root.mnt)
23882a737871SAl Viro 		path_put(&nd.root);
238931e6b01fSNick Piggin 	if (filp == ERR_PTR(-ESTALE) && !(flags & LOOKUP_REVAL))
239010fa8e62SAl Viro 		goto reval;
239110fa8e62SAl Viro 	return filp;
23921da177e4SLinus Torvalds 
2393806b681cSAl Viro exit_dput:
2394806b681cSAl Viro 	path_put_conditional(&path, &nd);
239531e6b01fSNick Piggin out_path:
239631e6b01fSNick Piggin 	path_put(&nd.path);
239731e6b01fSNick Piggin out_filp:
2398806b681cSAl Viro 	if (!IS_ERR(nd.intent.open.file))
2399a70e65dfSChristoph Hellwig 		release_open_intent(&nd);
240010fa8e62SAl Viro 	filp = ERR_PTR(error);
240110fa8e62SAl Viro 	goto out;
2402de459215SKirill Korotaev }
24031da177e4SLinus Torvalds 
24041da177e4SLinus Torvalds /**
2405a70e65dfSChristoph Hellwig  * filp_open - open file and return file pointer
2406a70e65dfSChristoph Hellwig  *
2407a70e65dfSChristoph Hellwig  * @filename:	path to open
2408a70e65dfSChristoph Hellwig  * @flags:	open flags as per the open(2) second argument
2409a70e65dfSChristoph Hellwig  * @mode:	mode for the new file if O_CREAT is set, else ignored
2410a70e65dfSChristoph Hellwig  *
2411a70e65dfSChristoph Hellwig  * This is the helper to open a file from kernelspace if you really
2412a70e65dfSChristoph Hellwig  * have to.  But in generally you should not do this, so please move
2413a70e65dfSChristoph Hellwig  * along, nothing to see here..
2414a70e65dfSChristoph Hellwig  */
2415a70e65dfSChristoph Hellwig struct file *filp_open(const char *filename, int flags, int mode)
2416a70e65dfSChristoph Hellwig {
24176e8341a1SAl Viro 	return do_filp_open(AT_FDCWD, filename, flags, mode, 0);
2418a70e65dfSChristoph Hellwig }
2419a70e65dfSChristoph Hellwig EXPORT_SYMBOL(filp_open);
2420a70e65dfSChristoph Hellwig 
2421a70e65dfSChristoph Hellwig /**
24221da177e4SLinus Torvalds  * lookup_create - lookup a dentry, creating it if it doesn't exist
24231da177e4SLinus Torvalds  * @nd: nameidata info
24241da177e4SLinus Torvalds  * @is_dir: directory flag
24251da177e4SLinus Torvalds  *
24261da177e4SLinus Torvalds  * Simple function to lookup and return a dentry and create it
24271da177e4SLinus Torvalds  * if it doesn't exist.  Is SMP-safe.
2428c663e5d8SChristoph Hellwig  *
24294ac91378SJan Blunck  * Returns with nd->path.dentry->d_inode->i_mutex locked.
24301da177e4SLinus Torvalds  */
24311da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir)
24321da177e4SLinus Torvalds {
2433c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
24341da177e4SLinus Torvalds 
24354ac91378SJan Blunck 	mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2436c663e5d8SChristoph Hellwig 	/*
2437c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2438c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2439c663e5d8SChristoph Hellwig 	 */
24401da177e4SLinus Torvalds 	if (nd->last_type != LAST_NORM)
24411da177e4SLinus Torvalds 		goto fail;
24421da177e4SLinus Torvalds 	nd->flags &= ~LOOKUP_PARENT;
24433516586aSAl Viro 	nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2444a634904aSASANO Masahiro 	nd->intent.open.flags = O_EXCL;
2445c663e5d8SChristoph Hellwig 
2446c663e5d8SChristoph Hellwig 	/*
2447c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2448c663e5d8SChristoph Hellwig 	 */
244949705b77SChristoph Hellwig 	dentry = lookup_hash(nd);
24501da177e4SLinus Torvalds 	if (IS_ERR(dentry))
24511da177e4SLinus Torvalds 		goto fail;
2452c663e5d8SChristoph Hellwig 
2453e9baf6e5SAl Viro 	if (dentry->d_inode)
2454e9baf6e5SAl Viro 		goto eexist;
2455c663e5d8SChristoph Hellwig 	/*
2456c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2457c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2458c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2459c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2460c663e5d8SChristoph Hellwig 	 */
2461e9baf6e5SAl Viro 	if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
24621da177e4SLinus Torvalds 		dput(dentry);
24631da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2464e9baf6e5SAl Viro 	}
2465e9baf6e5SAl Viro 	return dentry;
2466e9baf6e5SAl Viro eexist:
2467e9baf6e5SAl Viro 	dput(dentry);
2468e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
24691da177e4SLinus Torvalds fail:
24701da177e4SLinus Torvalds 	return dentry;
24711da177e4SLinus Torvalds }
2472f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create);
24731da177e4SLinus Torvalds 
24741da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
24751da177e4SLinus Torvalds {
2476a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
24771da177e4SLinus Torvalds 
24781da177e4SLinus Torvalds 	if (error)
24791da177e4SLinus Torvalds 		return error;
24801da177e4SLinus Torvalds 
24811da177e4SLinus Torvalds 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
24821da177e4SLinus Torvalds 		return -EPERM;
24831da177e4SLinus Torvalds 
2484acfa4380SAl Viro 	if (!dir->i_op->mknod)
24851da177e4SLinus Torvalds 		return -EPERM;
24861da177e4SLinus Torvalds 
248708ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
248808ce5f16SSerge E. Hallyn 	if (error)
248908ce5f16SSerge E. Hallyn 		return error;
249008ce5f16SSerge E. Hallyn 
24911da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
24921da177e4SLinus Torvalds 	if (error)
24931da177e4SLinus Torvalds 		return error;
24941da177e4SLinus Torvalds 
24951da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2496a74574aaSStephen Smalley 	if (!error)
2497f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
24981da177e4SLinus Torvalds 	return error;
24991da177e4SLinus Torvalds }
25001da177e4SLinus Torvalds 
2501463c3197SDave Hansen static int may_mknod(mode_t mode)
2502463c3197SDave Hansen {
2503463c3197SDave Hansen 	switch (mode & S_IFMT) {
2504463c3197SDave Hansen 	case S_IFREG:
2505463c3197SDave Hansen 	case S_IFCHR:
2506463c3197SDave Hansen 	case S_IFBLK:
2507463c3197SDave Hansen 	case S_IFIFO:
2508463c3197SDave Hansen 	case S_IFSOCK:
2509463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2510463c3197SDave Hansen 		return 0;
2511463c3197SDave Hansen 	case S_IFDIR:
2512463c3197SDave Hansen 		return -EPERM;
2513463c3197SDave Hansen 	default:
2514463c3197SDave Hansen 		return -EINVAL;
2515463c3197SDave Hansen 	}
2516463c3197SDave Hansen }
2517463c3197SDave Hansen 
25182e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
25192e4d0924SHeiko Carstens 		unsigned, dev)
25201da177e4SLinus Torvalds {
25212ad94ae6SAl Viro 	int error;
25221da177e4SLinus Torvalds 	char *tmp;
25231da177e4SLinus Torvalds 	struct dentry *dentry;
25241da177e4SLinus Torvalds 	struct nameidata nd;
25251da177e4SLinus Torvalds 
25261da177e4SLinus Torvalds 	if (S_ISDIR(mode))
25271da177e4SLinus Torvalds 		return -EPERM;
25281da177e4SLinus Torvalds 
25292ad94ae6SAl Viro 	error = user_path_parent(dfd, filename, &nd, &tmp);
25301da177e4SLinus Torvalds 	if (error)
25312ad94ae6SAl Viro 		return error;
25322ad94ae6SAl Viro 
25331da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
2534463c3197SDave Hansen 	if (IS_ERR(dentry)) {
25351da177e4SLinus Torvalds 		error = PTR_ERR(dentry);
2536463c3197SDave Hansen 		goto out_unlock;
2537463c3197SDave Hansen 	}
25384ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2539ce3b0f8dSAl Viro 		mode &= ~current_umask();
2540463c3197SDave Hansen 	error = may_mknod(mode);
2541463c3197SDave Hansen 	if (error)
2542463c3197SDave Hansen 		goto out_dput;
2543463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2544463c3197SDave Hansen 	if (error)
2545463c3197SDave Hansen 		goto out_dput;
2546be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd.path, dentry, mode, dev);
2547be6d3e56SKentaro Takeda 	if (error)
2548be6d3e56SKentaro Takeda 		goto out_drop_write;
25491da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
25501da177e4SLinus Torvalds 		case 0: case S_IFREG:
25514ac91378SJan Blunck 			error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
25521da177e4SLinus Torvalds 			break;
25531da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
25544ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
25551da177e4SLinus Torvalds 					new_decode_dev(dev));
25561da177e4SLinus Torvalds 			break;
25571da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
25584ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
25591da177e4SLinus Torvalds 			break;
25601da177e4SLinus Torvalds 	}
2561be6d3e56SKentaro Takeda out_drop_write:
2562463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2563463c3197SDave Hansen out_dput:
25641da177e4SLinus Torvalds 	dput(dentry);
2565463c3197SDave Hansen out_unlock:
25664ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
25671d957f9bSJan Blunck 	path_put(&nd.path);
25681da177e4SLinus Torvalds 	putname(tmp);
25691da177e4SLinus Torvalds 
25701da177e4SLinus Torvalds 	return error;
25711da177e4SLinus Torvalds }
25721da177e4SLinus Torvalds 
25733480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
25745590ff0dSUlrich Drepper {
25755590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
25765590ff0dSUlrich Drepper }
25775590ff0dSUlrich Drepper 
25781da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
25791da177e4SLinus Torvalds {
2580a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
25811da177e4SLinus Torvalds 
25821da177e4SLinus Torvalds 	if (error)
25831da177e4SLinus Torvalds 		return error;
25841da177e4SLinus Torvalds 
2585acfa4380SAl Viro 	if (!dir->i_op->mkdir)
25861da177e4SLinus Torvalds 		return -EPERM;
25871da177e4SLinus Torvalds 
25881da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
25891da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
25901da177e4SLinus Torvalds 	if (error)
25911da177e4SLinus Torvalds 		return error;
25921da177e4SLinus Torvalds 
25931da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2594a74574aaSStephen Smalley 	if (!error)
2595f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
25961da177e4SLinus Torvalds 	return error;
25971da177e4SLinus Torvalds }
25981da177e4SLinus Torvalds 
25992e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
26001da177e4SLinus Torvalds {
26011da177e4SLinus Torvalds 	int error = 0;
26021da177e4SLinus Torvalds 	char * tmp;
26036902d925SDave Hansen 	struct dentry *dentry;
26046902d925SDave Hansen 	struct nameidata nd;
26051da177e4SLinus Torvalds 
26062ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &tmp);
26072ad94ae6SAl Viro 	if (error)
26086902d925SDave Hansen 		goto out_err;
26091da177e4SLinus Torvalds 
26101da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 1);
26111da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
26126902d925SDave Hansen 	if (IS_ERR(dentry))
26136902d925SDave Hansen 		goto out_unlock;
26146902d925SDave Hansen 
26154ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2616ce3b0f8dSAl Viro 		mode &= ~current_umask();
2617463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2618463c3197SDave Hansen 	if (error)
2619463c3197SDave Hansen 		goto out_dput;
2620be6d3e56SKentaro Takeda 	error = security_path_mkdir(&nd.path, dentry, mode);
2621be6d3e56SKentaro Takeda 	if (error)
2622be6d3e56SKentaro Takeda 		goto out_drop_write;
26234ac91378SJan Blunck 	error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2624be6d3e56SKentaro Takeda out_drop_write:
2625463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2626463c3197SDave Hansen out_dput:
26271da177e4SLinus Torvalds 	dput(dentry);
26286902d925SDave Hansen out_unlock:
26294ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
26301d957f9bSJan Blunck 	path_put(&nd.path);
26311da177e4SLinus Torvalds 	putname(tmp);
26326902d925SDave Hansen out_err:
26331da177e4SLinus Torvalds 	return error;
26341da177e4SLinus Torvalds }
26351da177e4SLinus Torvalds 
26363cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
26375590ff0dSUlrich Drepper {
26385590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
26395590ff0dSUlrich Drepper }
26405590ff0dSUlrich Drepper 
26411da177e4SLinus Torvalds /*
26421da177e4SLinus Torvalds  * We try to drop the dentry early: we should have
26431da177e4SLinus Torvalds  * a usage count of 2 if we're the only user of this
26441da177e4SLinus Torvalds  * dentry, and if that is true (possibly after pruning
26451da177e4SLinus Torvalds  * the dcache), then we drop the dentry now.
26461da177e4SLinus Torvalds  *
26471da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
26481da177e4SLinus Torvalds  * do a
26491da177e4SLinus Torvalds  *
26501da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
26511da177e4SLinus Torvalds  *		return -EBUSY;
26521da177e4SLinus Torvalds  *
26531da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
26541da177e4SLinus Torvalds  * that is still in use by something else..
26551da177e4SLinus Torvalds  */
26561da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
26571da177e4SLinus Torvalds {
26581da177e4SLinus Torvalds 	dget(dentry);
26591da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
26601da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
2661b7ab39f6SNick Piggin 	if (dentry->d_count == 2)
26621da177e4SLinus Torvalds 		__d_drop(dentry);
26631da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
26641da177e4SLinus Torvalds }
26651da177e4SLinus Torvalds 
26661da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
26671da177e4SLinus Torvalds {
26681da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
26691da177e4SLinus Torvalds 
26701da177e4SLinus Torvalds 	if (error)
26711da177e4SLinus Torvalds 		return error;
26721da177e4SLinus Torvalds 
2673acfa4380SAl Viro 	if (!dir->i_op->rmdir)
26741da177e4SLinus Torvalds 		return -EPERM;
26751da177e4SLinus Torvalds 
26761b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
26771da177e4SLinus Torvalds 	dentry_unhash(dentry);
26781da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
26791da177e4SLinus Torvalds 		error = -EBUSY;
26801da177e4SLinus Torvalds 	else {
26811da177e4SLinus Torvalds 		error = security_inode_rmdir(dir, dentry);
26821da177e4SLinus Torvalds 		if (!error) {
26831da177e4SLinus Torvalds 			error = dir->i_op->rmdir(dir, dentry);
2684d83c49f3SAl Viro 			if (!error) {
26851da177e4SLinus Torvalds 				dentry->d_inode->i_flags |= S_DEAD;
2686d83c49f3SAl Viro 				dont_mount(dentry);
2687d83c49f3SAl Viro 			}
26881da177e4SLinus Torvalds 		}
26891da177e4SLinus Torvalds 	}
26901b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
26911da177e4SLinus Torvalds 	if (!error) {
26921da177e4SLinus Torvalds 		d_delete(dentry);
26931da177e4SLinus Torvalds 	}
26941da177e4SLinus Torvalds 	dput(dentry);
26951da177e4SLinus Torvalds 
26961da177e4SLinus Torvalds 	return error;
26971da177e4SLinus Torvalds }
26981da177e4SLinus Torvalds 
26995590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
27001da177e4SLinus Torvalds {
27011da177e4SLinus Torvalds 	int error = 0;
27021da177e4SLinus Torvalds 	char * name;
27031da177e4SLinus Torvalds 	struct dentry *dentry;
27041da177e4SLinus Torvalds 	struct nameidata nd;
27051da177e4SLinus Torvalds 
27062ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
27071da177e4SLinus Torvalds 	if (error)
27082ad94ae6SAl Viro 		return error;
27091da177e4SLinus Torvalds 
27101da177e4SLinus Torvalds 	switch(nd.last_type) {
27111da177e4SLinus Torvalds 	case LAST_DOTDOT:
27121da177e4SLinus Torvalds 		error = -ENOTEMPTY;
27131da177e4SLinus Torvalds 		goto exit1;
27141da177e4SLinus Torvalds 	case LAST_DOT:
27151da177e4SLinus Torvalds 		error = -EINVAL;
27161da177e4SLinus Torvalds 		goto exit1;
27171da177e4SLinus Torvalds 	case LAST_ROOT:
27181da177e4SLinus Torvalds 		error = -EBUSY;
27191da177e4SLinus Torvalds 		goto exit1;
27201da177e4SLinus Torvalds 	}
27210612d9fbSOGAWA Hirofumi 
27220612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
27230612d9fbSOGAWA Hirofumi 
27244ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
272549705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
27261da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
27276902d925SDave Hansen 	if (IS_ERR(dentry))
27286902d925SDave Hansen 		goto exit2;
27290622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
27300622753bSDave Hansen 	if (error)
27310622753bSDave Hansen 		goto exit3;
2732be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2733be6d3e56SKentaro Takeda 	if (error)
2734be6d3e56SKentaro Takeda 		goto exit4;
27354ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2736be6d3e56SKentaro Takeda exit4:
27370622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
27380622753bSDave Hansen exit3:
27391da177e4SLinus Torvalds 	dput(dentry);
27406902d925SDave Hansen exit2:
27414ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
27421da177e4SLinus Torvalds exit1:
27431d957f9bSJan Blunck 	path_put(&nd.path);
27441da177e4SLinus Torvalds 	putname(name);
27451da177e4SLinus Torvalds 	return error;
27461da177e4SLinus Torvalds }
27471da177e4SLinus Torvalds 
27483cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
27495590ff0dSUlrich Drepper {
27505590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
27515590ff0dSUlrich Drepper }
27525590ff0dSUlrich Drepper 
27531da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
27541da177e4SLinus Torvalds {
27551da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
27561da177e4SLinus Torvalds 
27571da177e4SLinus Torvalds 	if (error)
27581da177e4SLinus Torvalds 		return error;
27591da177e4SLinus Torvalds 
2760acfa4380SAl Viro 	if (!dir->i_op->unlink)
27611da177e4SLinus Torvalds 		return -EPERM;
27621da177e4SLinus Torvalds 
27631b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
27641da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
27651da177e4SLinus Torvalds 		error = -EBUSY;
27661da177e4SLinus Torvalds 	else {
27671da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2768bec1052eSAl Viro 		if (!error) {
27691da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2770bec1052eSAl Viro 			if (!error)
2771d83c49f3SAl Viro 				dont_mount(dentry);
2772bec1052eSAl Viro 		}
27731da177e4SLinus Torvalds 	}
27741b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
27751da177e4SLinus Torvalds 
27761da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
27771da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2778ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
27791da177e4SLinus Torvalds 		d_delete(dentry);
27801da177e4SLinus Torvalds 	}
27810eeca283SRobert Love 
27821da177e4SLinus Torvalds 	return error;
27831da177e4SLinus Torvalds }
27841da177e4SLinus Torvalds 
27851da177e4SLinus Torvalds /*
27861da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
27871b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
27881da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
27891da177e4SLinus Torvalds  * while waiting on the I/O.
27901da177e4SLinus Torvalds  */
27915590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
27921da177e4SLinus Torvalds {
27932ad94ae6SAl Viro 	int error;
27941da177e4SLinus Torvalds 	char *name;
27951da177e4SLinus Torvalds 	struct dentry *dentry;
27961da177e4SLinus Torvalds 	struct nameidata nd;
27971da177e4SLinus Torvalds 	struct inode *inode = NULL;
27981da177e4SLinus Torvalds 
27992ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
28001da177e4SLinus Torvalds 	if (error)
28012ad94ae6SAl Viro 		return error;
28022ad94ae6SAl Viro 
28031da177e4SLinus Torvalds 	error = -EISDIR;
28041da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
28051da177e4SLinus Torvalds 		goto exit1;
28060612d9fbSOGAWA Hirofumi 
28070612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
28080612d9fbSOGAWA Hirofumi 
28094ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
281049705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
28111da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
28121da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
28131da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
28141da177e4SLinus Torvalds 		if (nd.last.name[nd.last.len])
28151da177e4SLinus Torvalds 			goto slashes;
28161da177e4SLinus Torvalds 		inode = dentry->d_inode;
28171da177e4SLinus Torvalds 		if (inode)
28187de9c6eeSAl Viro 			ihold(inode);
28190622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
28200622753bSDave Hansen 		if (error)
28210622753bSDave Hansen 			goto exit2;
2822be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2823be6d3e56SKentaro Takeda 		if (error)
2824be6d3e56SKentaro Takeda 			goto exit3;
28254ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2826be6d3e56SKentaro Takeda exit3:
28270622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
28281da177e4SLinus Torvalds 	exit2:
28291da177e4SLinus Torvalds 		dput(dentry);
28301da177e4SLinus Torvalds 	}
28314ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
28321da177e4SLinus Torvalds 	if (inode)
28331da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
28341da177e4SLinus Torvalds exit1:
28351d957f9bSJan Blunck 	path_put(&nd.path);
28361da177e4SLinus Torvalds 	putname(name);
28371da177e4SLinus Torvalds 	return error;
28381da177e4SLinus Torvalds 
28391da177e4SLinus Torvalds slashes:
28401da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
28411da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
28421da177e4SLinus Torvalds 	goto exit2;
28431da177e4SLinus Torvalds }
28441da177e4SLinus Torvalds 
28452e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
28465590ff0dSUlrich Drepper {
28475590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
28485590ff0dSUlrich Drepper 		return -EINVAL;
28495590ff0dSUlrich Drepper 
28505590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
28515590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
28525590ff0dSUlrich Drepper 
28535590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
28545590ff0dSUlrich Drepper }
28555590ff0dSUlrich Drepper 
28563480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
28575590ff0dSUlrich Drepper {
28585590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
28595590ff0dSUlrich Drepper }
28605590ff0dSUlrich Drepper 
2861db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
28621da177e4SLinus Torvalds {
2863a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
28641da177e4SLinus Torvalds 
28651da177e4SLinus Torvalds 	if (error)
28661da177e4SLinus Torvalds 		return error;
28671da177e4SLinus Torvalds 
2868acfa4380SAl Viro 	if (!dir->i_op->symlink)
28691da177e4SLinus Torvalds 		return -EPERM;
28701da177e4SLinus Torvalds 
28711da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
28721da177e4SLinus Torvalds 	if (error)
28731da177e4SLinus Torvalds 		return error;
28741da177e4SLinus Torvalds 
28751da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2876a74574aaSStephen Smalley 	if (!error)
2877f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
28781da177e4SLinus Torvalds 	return error;
28791da177e4SLinus Torvalds }
28801da177e4SLinus Torvalds 
28812e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
28822e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
28831da177e4SLinus Torvalds {
28842ad94ae6SAl Viro 	int error;
28851da177e4SLinus Torvalds 	char *from;
28861da177e4SLinus Torvalds 	char *to;
28876902d925SDave Hansen 	struct dentry *dentry;
28886902d925SDave Hansen 	struct nameidata nd;
28891da177e4SLinus Torvalds 
28901da177e4SLinus Torvalds 	from = getname(oldname);
28911da177e4SLinus Torvalds 	if (IS_ERR(from))
28921da177e4SLinus Torvalds 		return PTR_ERR(from);
28932ad94ae6SAl Viro 
28942ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
28952ad94ae6SAl Viro 	if (error)
28966902d925SDave Hansen 		goto out_putname;
28971da177e4SLinus Torvalds 
28981da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
28991da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
29006902d925SDave Hansen 	if (IS_ERR(dentry))
29016902d925SDave Hansen 		goto out_unlock;
29026902d925SDave Hansen 
290375c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
290475c3f29dSDave Hansen 	if (error)
290575c3f29dSDave Hansen 		goto out_dput;
2906be6d3e56SKentaro Takeda 	error = security_path_symlink(&nd.path, dentry, from);
2907be6d3e56SKentaro Takeda 	if (error)
2908be6d3e56SKentaro Takeda 		goto out_drop_write;
2909db2e747bSMiklos Szeredi 	error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2910be6d3e56SKentaro Takeda out_drop_write:
291175c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
291275c3f29dSDave Hansen out_dput:
29131da177e4SLinus Torvalds 	dput(dentry);
29146902d925SDave Hansen out_unlock:
29154ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
29161d957f9bSJan Blunck 	path_put(&nd.path);
29171da177e4SLinus Torvalds 	putname(to);
29186902d925SDave Hansen out_putname:
29191da177e4SLinus Torvalds 	putname(from);
29201da177e4SLinus Torvalds 	return error;
29211da177e4SLinus Torvalds }
29221da177e4SLinus Torvalds 
29233480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
29245590ff0dSUlrich Drepper {
29255590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
29265590ff0dSUlrich Drepper }
29275590ff0dSUlrich Drepper 
29281da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
29291da177e4SLinus Torvalds {
29301da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
29311da177e4SLinus Torvalds 	int error;
29321da177e4SLinus Torvalds 
29331da177e4SLinus Torvalds 	if (!inode)
29341da177e4SLinus Torvalds 		return -ENOENT;
29351da177e4SLinus Torvalds 
2936a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
29371da177e4SLinus Torvalds 	if (error)
29381da177e4SLinus Torvalds 		return error;
29391da177e4SLinus Torvalds 
29401da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
29411da177e4SLinus Torvalds 		return -EXDEV;
29421da177e4SLinus Torvalds 
29431da177e4SLinus Torvalds 	/*
29441da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
29451da177e4SLinus Torvalds 	 */
29461da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
29471da177e4SLinus Torvalds 		return -EPERM;
2948acfa4380SAl Viro 	if (!dir->i_op->link)
29491da177e4SLinus Torvalds 		return -EPERM;
29507e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
29511da177e4SLinus Torvalds 		return -EPERM;
29521da177e4SLinus Torvalds 
29531da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
29541da177e4SLinus Torvalds 	if (error)
29551da177e4SLinus Torvalds 		return error;
29561da177e4SLinus Torvalds 
29577e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
29581da177e4SLinus Torvalds 	error = dir->i_op->link(old_dentry, dir, new_dentry);
29597e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
2960e31e14ecSStephen Smalley 	if (!error)
29617e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
29621da177e4SLinus Torvalds 	return error;
29631da177e4SLinus Torvalds }
29641da177e4SLinus Torvalds 
29651da177e4SLinus Torvalds /*
29661da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
29671da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
29681da177e4SLinus Torvalds  * newname.  --KAB
29691da177e4SLinus Torvalds  *
29701da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
29711da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
29721da177e4SLinus Torvalds  * and other special files.  --ADM
29731da177e4SLinus Torvalds  */
29742e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
29752e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
29761da177e4SLinus Torvalds {
29771da177e4SLinus Torvalds 	struct dentry *new_dentry;
29782d8f3038SAl Viro 	struct nameidata nd;
29792d8f3038SAl Viro 	struct path old_path;
29801da177e4SLinus Torvalds 	int error;
29811da177e4SLinus Torvalds 	char *to;
29821da177e4SLinus Torvalds 
298345c9b11aSUlrich Drepper 	if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
2984c04030e1SUlrich Drepper 		return -EINVAL;
2985c04030e1SUlrich Drepper 
29862d8f3038SAl Viro 	error = user_path_at(olddfd, oldname,
298745c9b11aSUlrich Drepper 			     flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
29882d8f3038SAl Viro 			     &old_path);
29891da177e4SLinus Torvalds 	if (error)
29902ad94ae6SAl Viro 		return error;
29912ad94ae6SAl Viro 
29922ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
29931da177e4SLinus Torvalds 	if (error)
29941da177e4SLinus Torvalds 		goto out;
29951da177e4SLinus Torvalds 	error = -EXDEV;
29962d8f3038SAl Viro 	if (old_path.mnt != nd.path.mnt)
29971da177e4SLinus Torvalds 		goto out_release;
29981da177e4SLinus Torvalds 	new_dentry = lookup_create(&nd, 0);
29991da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
30006902d925SDave Hansen 	if (IS_ERR(new_dentry))
30016902d925SDave Hansen 		goto out_unlock;
300275c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
300375c3f29dSDave Hansen 	if (error)
300475c3f29dSDave Hansen 		goto out_dput;
3005be6d3e56SKentaro Takeda 	error = security_path_link(old_path.dentry, &nd.path, new_dentry);
3006be6d3e56SKentaro Takeda 	if (error)
3007be6d3e56SKentaro Takeda 		goto out_drop_write;
30082d8f3038SAl Viro 	error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
3009be6d3e56SKentaro Takeda out_drop_write:
301075c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
301175c3f29dSDave Hansen out_dput:
30121da177e4SLinus Torvalds 	dput(new_dentry);
30136902d925SDave Hansen out_unlock:
30144ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
30151da177e4SLinus Torvalds out_release:
30161d957f9bSJan Blunck 	path_put(&nd.path);
30172ad94ae6SAl Viro 	putname(to);
30181da177e4SLinus Torvalds out:
30192d8f3038SAl Viro 	path_put(&old_path);
30201da177e4SLinus Torvalds 
30211da177e4SLinus Torvalds 	return error;
30221da177e4SLinus Torvalds }
30231da177e4SLinus Torvalds 
30243480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
30255590ff0dSUlrich Drepper {
3026c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
30275590ff0dSUlrich Drepper }
30285590ff0dSUlrich Drepper 
30291da177e4SLinus Torvalds /*
30301da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
30311da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
30321da177e4SLinus Torvalds  * Problems:
30331da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
30341da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
30351da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
3036a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
30371da177e4SLinus Torvalds  *	   story.
30381da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
30391b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
30401da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
30411da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
3042a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
30431da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
30441da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
30451da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
3046a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
30471da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
30481da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
30491da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
30501da177e4SLinus Torvalds  *	d) some filesystems don't support opened-but-unlinked directories,
30511da177e4SLinus Torvalds  *	   either because of layout or because they are not ready to deal with
30521da177e4SLinus Torvalds  *	   all cases correctly. The latter will be fixed (taking this sort of
30531da177e4SLinus Torvalds  *	   stuff into VFS), but the former is not going away. Solution: the same
30541da177e4SLinus Torvalds  *	   trick as in rmdir().
30551da177e4SLinus Torvalds  *	e) conversion from fhandle to dentry may come in the wrong moment - when
30561b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
30571da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3058c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
30591da177e4SLinus Torvalds  *	   locking].
30601da177e4SLinus Torvalds  */
306175c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
30621da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
30631da177e4SLinus Torvalds {
30641da177e4SLinus Torvalds 	int error = 0;
30651da177e4SLinus Torvalds 	struct inode *target;
30661da177e4SLinus Torvalds 
30671da177e4SLinus Torvalds 	/*
30681da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
30691da177e4SLinus Torvalds 	 * we'll need to flip '..'.
30701da177e4SLinus Torvalds 	 */
30711da177e4SLinus Torvalds 	if (new_dir != old_dir) {
3072f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
30731da177e4SLinus Torvalds 		if (error)
30741da177e4SLinus Torvalds 			return error;
30751da177e4SLinus Torvalds 	}
30761da177e4SLinus Torvalds 
30771da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30781da177e4SLinus Torvalds 	if (error)
30791da177e4SLinus Torvalds 		return error;
30801da177e4SLinus Torvalds 
30811da177e4SLinus Torvalds 	target = new_dentry->d_inode;
3082d83c49f3SAl Viro 	if (target)
30831b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
30841da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
30851da177e4SLinus Torvalds 		error = -EBUSY;
3086d83c49f3SAl Viro 	else {
3087d83c49f3SAl Viro 		if (target)
3088d83c49f3SAl Viro 			dentry_unhash(new_dentry);
30891da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3090d83c49f3SAl Viro 	}
30911da177e4SLinus Torvalds 	if (target) {
3092d83c49f3SAl Viro 		if (!error) {
30931da177e4SLinus Torvalds 			target->i_flags |= S_DEAD;
3094d83c49f3SAl Viro 			dont_mount(new_dentry);
3095d83c49f3SAl Viro 		}
30961b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
30971da177e4SLinus Torvalds 		if (d_unhashed(new_dentry))
30981da177e4SLinus Torvalds 			d_rehash(new_dentry);
30991da177e4SLinus Torvalds 		dput(new_dentry);
31001da177e4SLinus Torvalds 	}
3101e31e14ecSStephen Smalley 	if (!error)
3102349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
31031da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
31041da177e4SLinus Torvalds 	return error;
31051da177e4SLinus Torvalds }
31061da177e4SLinus Torvalds 
310775c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
31081da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
31091da177e4SLinus Torvalds {
31101da177e4SLinus Torvalds 	struct inode *target;
31111da177e4SLinus Torvalds 	int error;
31121da177e4SLinus Torvalds 
31131da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
31141da177e4SLinus Torvalds 	if (error)
31151da177e4SLinus Torvalds 		return error;
31161da177e4SLinus Torvalds 
31171da177e4SLinus Torvalds 	dget(new_dentry);
31181da177e4SLinus Torvalds 	target = new_dentry->d_inode;
31191da177e4SLinus Torvalds 	if (target)
31201b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
31211da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
31221da177e4SLinus Torvalds 		error = -EBUSY;
31231da177e4SLinus Torvalds 	else
31241da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
31251da177e4SLinus Torvalds 	if (!error) {
3126bec1052eSAl Viro 		if (target)
3127d83c49f3SAl Viro 			dont_mount(new_dentry);
3128349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
31291da177e4SLinus Torvalds 			d_move(old_dentry, new_dentry);
31301da177e4SLinus Torvalds 	}
31311da177e4SLinus Torvalds 	if (target)
31321b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
31331da177e4SLinus Torvalds 	dput(new_dentry);
31341da177e4SLinus Torvalds 	return error;
31351da177e4SLinus Torvalds }
31361da177e4SLinus Torvalds 
31371da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
31381da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
31391da177e4SLinus Torvalds {
31401da177e4SLinus Torvalds 	int error;
31411da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
314259b0df21SEric Paris 	const unsigned char *old_name;
31431da177e4SLinus Torvalds 
31441da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
31451da177e4SLinus Torvalds  		return 0;
31461da177e4SLinus Torvalds 
31471da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
31481da177e4SLinus Torvalds 	if (error)
31491da177e4SLinus Torvalds 		return error;
31501da177e4SLinus Torvalds 
31511da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3152a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
31531da177e4SLinus Torvalds 	else
31541da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
31551da177e4SLinus Torvalds 	if (error)
31561da177e4SLinus Torvalds 		return error;
31571da177e4SLinus Torvalds 
3158acfa4380SAl Viro 	if (!old_dir->i_op->rename)
31591da177e4SLinus Torvalds 		return -EPERM;
31601da177e4SLinus Torvalds 
31610eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
31620eeca283SRobert Love 
31631da177e4SLinus Torvalds 	if (is_dir)
31641da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
31651da177e4SLinus Torvalds 	else
31661da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3167123df294SAl Viro 	if (!error)
3168123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
31695a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
31700eeca283SRobert Love 	fsnotify_oldname_free(old_name);
31710eeca283SRobert Love 
31721da177e4SLinus Torvalds 	return error;
31731da177e4SLinus Torvalds }
31741da177e4SLinus Torvalds 
31752e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
31762e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
31771da177e4SLinus Torvalds {
31781da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
31791da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
31801da177e4SLinus Torvalds 	struct dentry *trap;
31811da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
31822ad94ae6SAl Viro 	char *from;
31832ad94ae6SAl Viro 	char *to;
31842ad94ae6SAl Viro 	int error;
31851da177e4SLinus Torvalds 
31862ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
31871da177e4SLinus Torvalds 	if (error)
31881da177e4SLinus Torvalds 		goto exit;
31891da177e4SLinus Torvalds 
31902ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
31911da177e4SLinus Torvalds 	if (error)
31921da177e4SLinus Torvalds 		goto exit1;
31931da177e4SLinus Torvalds 
31941da177e4SLinus Torvalds 	error = -EXDEV;
31954ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
31961da177e4SLinus Torvalds 		goto exit2;
31971da177e4SLinus Torvalds 
31984ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
31991da177e4SLinus Torvalds 	error = -EBUSY;
32001da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
32011da177e4SLinus Torvalds 		goto exit2;
32021da177e4SLinus Torvalds 
32034ac91378SJan Blunck 	new_dir = newnd.path.dentry;
32041da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
32051da177e4SLinus Torvalds 		goto exit2;
32061da177e4SLinus Torvalds 
32070612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
32080612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
32094e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
32100612d9fbSOGAWA Hirofumi 
32111da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
32121da177e4SLinus Torvalds 
321349705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
32141da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
32151da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
32161da177e4SLinus Torvalds 		goto exit3;
32171da177e4SLinus Torvalds 	/* source must exist */
32181da177e4SLinus Torvalds 	error = -ENOENT;
32191da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
32201da177e4SLinus Torvalds 		goto exit4;
32211da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
32221da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
32231da177e4SLinus Torvalds 		error = -ENOTDIR;
32241da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
32251da177e4SLinus Torvalds 			goto exit4;
32261da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
32271da177e4SLinus Torvalds 			goto exit4;
32281da177e4SLinus Torvalds 	}
32291da177e4SLinus Torvalds 	/* source should not be ancestor of target */
32301da177e4SLinus Torvalds 	error = -EINVAL;
32311da177e4SLinus Torvalds 	if (old_dentry == trap)
32321da177e4SLinus Torvalds 		goto exit4;
323349705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
32341da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
32351da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
32361da177e4SLinus Torvalds 		goto exit4;
32371da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
32381da177e4SLinus Torvalds 	error = -ENOTEMPTY;
32391da177e4SLinus Torvalds 	if (new_dentry == trap)
32401da177e4SLinus Torvalds 		goto exit5;
32411da177e4SLinus Torvalds 
32429079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
32439079b1ebSDave Hansen 	if (error)
32449079b1ebSDave Hansen 		goto exit5;
3245be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3246be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3247be6d3e56SKentaro Takeda 	if (error)
3248be6d3e56SKentaro Takeda 		goto exit6;
32491da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
32501da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3251be6d3e56SKentaro Takeda exit6:
32529079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
32531da177e4SLinus Torvalds exit5:
32541da177e4SLinus Torvalds 	dput(new_dentry);
32551da177e4SLinus Torvalds exit4:
32561da177e4SLinus Torvalds 	dput(old_dentry);
32571da177e4SLinus Torvalds exit3:
32581da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
32591da177e4SLinus Torvalds exit2:
32601d957f9bSJan Blunck 	path_put(&newnd.path);
32612ad94ae6SAl Viro 	putname(to);
32621da177e4SLinus Torvalds exit1:
32631d957f9bSJan Blunck 	path_put(&oldnd.path);
32641da177e4SLinus Torvalds 	putname(from);
32652ad94ae6SAl Viro exit:
32661da177e4SLinus Torvalds 	return error;
32671da177e4SLinus Torvalds }
32681da177e4SLinus Torvalds 
3269a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
32705590ff0dSUlrich Drepper {
32715590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
32725590ff0dSUlrich Drepper }
32735590ff0dSUlrich Drepper 
32741da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
32751da177e4SLinus Torvalds {
32761da177e4SLinus Torvalds 	int len;
32771da177e4SLinus Torvalds 
32781da177e4SLinus Torvalds 	len = PTR_ERR(link);
32791da177e4SLinus Torvalds 	if (IS_ERR(link))
32801da177e4SLinus Torvalds 		goto out;
32811da177e4SLinus Torvalds 
32821da177e4SLinus Torvalds 	len = strlen(link);
32831da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
32841da177e4SLinus Torvalds 		len = buflen;
32851da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
32861da177e4SLinus Torvalds 		len = -EFAULT;
32871da177e4SLinus Torvalds out:
32881da177e4SLinus Torvalds 	return len;
32891da177e4SLinus Torvalds }
32901da177e4SLinus Torvalds 
32911da177e4SLinus Torvalds /*
32921da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
32931da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
32941da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
32951da177e4SLinus Torvalds  */
32961da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
32971da177e4SLinus Torvalds {
32981da177e4SLinus Torvalds 	struct nameidata nd;
3299cc314eefSLinus Torvalds 	void *cookie;
3300694a1764SMarcin Slusarz 	int res;
3301cc314eefSLinus Torvalds 
33021da177e4SLinus Torvalds 	nd.depth = 0;
3303cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3304694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3305694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3306694a1764SMarcin Slusarz 
3307694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
33081da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3309cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3310694a1764SMarcin Slusarz 	return res;
33111da177e4SLinus Torvalds }
33121da177e4SLinus Torvalds 
33131da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
33141da177e4SLinus Torvalds {
33151da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
33161da177e4SLinus Torvalds }
33171da177e4SLinus Torvalds 
33181da177e4SLinus Torvalds /* get the link contents into pagecache */
33191da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
33201da177e4SLinus Torvalds {
3321ebd09abbSDuane Griffin 	char *kaddr;
33221da177e4SLinus Torvalds 	struct page *page;
33231da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3324090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
33251da177e4SLinus Torvalds 	if (IS_ERR(page))
33266fe6900eSNick Piggin 		return (char*)page;
33271da177e4SLinus Torvalds 	*ppage = page;
3328ebd09abbSDuane Griffin 	kaddr = kmap(page);
3329ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3330ebd09abbSDuane Griffin 	return kaddr;
33311da177e4SLinus Torvalds }
33321da177e4SLinus Torvalds 
33331da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
33341da177e4SLinus Torvalds {
33351da177e4SLinus Torvalds 	struct page *page = NULL;
33361da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
33371da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
33381da177e4SLinus Torvalds 	if (page) {
33391da177e4SLinus Torvalds 		kunmap(page);
33401da177e4SLinus Torvalds 		page_cache_release(page);
33411da177e4SLinus Torvalds 	}
33421da177e4SLinus Torvalds 	return res;
33431da177e4SLinus Torvalds }
33441da177e4SLinus Torvalds 
3345cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
33461da177e4SLinus Torvalds {
3347cc314eefSLinus Torvalds 	struct page *page = NULL;
33481da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3349cc314eefSLinus Torvalds 	return page;
33501da177e4SLinus Torvalds }
33511da177e4SLinus Torvalds 
3352cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
33531da177e4SLinus Torvalds {
3354cc314eefSLinus Torvalds 	struct page *page = cookie;
3355cc314eefSLinus Torvalds 
3356cc314eefSLinus Torvalds 	if (page) {
33571da177e4SLinus Torvalds 		kunmap(page);
33581da177e4SLinus Torvalds 		page_cache_release(page);
33591da177e4SLinus Torvalds 	}
33601da177e4SLinus Torvalds }
33611da177e4SLinus Torvalds 
336254566b2cSNick Piggin /*
336354566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
336454566b2cSNick Piggin  */
336554566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
33661da177e4SLinus Torvalds {
33671da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
33680adb25d2SKirill Korotaev 	struct page *page;
3369afddba49SNick Piggin 	void *fsdata;
3370beb497abSDmitriy Monakhov 	int err;
33711da177e4SLinus Torvalds 	char *kaddr;
337254566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
337354566b2cSNick Piggin 	if (nofs)
337454566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
33751da177e4SLinus Torvalds 
33767e53cac4SNeilBrown retry:
3377afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
337854566b2cSNick Piggin 				flags, &page, &fsdata);
33791da177e4SLinus Torvalds 	if (err)
3380afddba49SNick Piggin 		goto fail;
3381afddba49SNick Piggin 
33821da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
33831da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
33841da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
3385afddba49SNick Piggin 
3386afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3387afddba49SNick Piggin 							page, fsdata);
33881da177e4SLinus Torvalds 	if (err < 0)
33891da177e4SLinus Torvalds 		goto fail;
3390afddba49SNick Piggin 	if (err < len-1)
3391afddba49SNick Piggin 		goto retry;
3392afddba49SNick Piggin 
33931da177e4SLinus Torvalds 	mark_inode_dirty(inode);
33941da177e4SLinus Torvalds 	return 0;
33951da177e4SLinus Torvalds fail:
33961da177e4SLinus Torvalds 	return err;
33971da177e4SLinus Torvalds }
33981da177e4SLinus Torvalds 
33990adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
34000adb25d2SKirill Korotaev {
34010adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
340254566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
34030adb25d2SKirill Korotaev }
34040adb25d2SKirill Korotaev 
340592e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
34061da177e4SLinus Torvalds 	.readlink	= generic_readlink,
34071da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
34081da177e4SLinus Torvalds 	.put_link	= page_put_link,
34091da177e4SLinus Torvalds };
34101da177e4SLinus Torvalds 
34112d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
34121da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
34131da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
34141da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
34151da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
34161da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
34171da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
34181da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
34191da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
34201da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
34210adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
34221da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
34231da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
34241da177e4SLinus Torvalds EXPORT_SYMBOL(path_lookup);
3425d1811465SAl Viro EXPORT_SYMBOL(kern_path);
342616f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3427f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
34288c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission);
34291da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
34301da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
34311da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
34321da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
34331da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
34341da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
34351da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
34361da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
34371da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
34381da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
34391da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
34401da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
34411da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
34421da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3443