xref: /openbmc/linux/fs/namei.c (revision b74c79e9)
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 /**
3711d957f9bSJan Blunck  * path_put - put a reference to a path
3721d957f9bSJan Blunck  * @path: path to put the reference to
3731d957f9bSJan Blunck  *
3741d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
3751d957f9bSJan Blunck  */
3761d957f9bSJan Blunck void path_put(struct path *path)
3771da177e4SLinus Torvalds {
3781d957f9bSJan Blunck 	dput(path->dentry);
3791d957f9bSJan Blunck 	mntput(path->mnt);
3801da177e4SLinus Torvalds }
3811d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
3821da177e4SLinus Torvalds 
383834f2a4aSTrond Myklebust /**
38431e6b01fSNick Piggin  * nameidata_drop_rcu - drop this nameidata out of rcu-walk
38531e6b01fSNick Piggin  * @nd: nameidata pathwalk data to drop
38631e6b01fSNick Piggin  * @Returns: 0 on success, -ECHLID on failure
38731e6b01fSNick Piggin  *
38831e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
38931e6b01fSNick Piggin  * Documentation/filesystems/path-lookup.txt). __drop_rcu* functions attempt
39031e6b01fSNick Piggin  * to drop out of rcu-walk mode and take normal reference counts on dentries
39131e6b01fSNick Piggin  * and vfsmounts to transition to rcu-walk mode. __drop_rcu* functions take
39231e6b01fSNick Piggin  * refcounts at the last known good point before rcu-walk got stuck, so
39331e6b01fSNick Piggin  * ref-walk may continue from there. If this is not successful (eg. a seqcount
39431e6b01fSNick Piggin  * has changed), then failure is returned and path walk restarts from the
39531e6b01fSNick Piggin  * beginning in ref-walk mode.
39631e6b01fSNick Piggin  *
39731e6b01fSNick Piggin  * nameidata_drop_rcu attempts to drop the current nd->path and nd->root into
39831e6b01fSNick Piggin  * ref-walk. Must be called from rcu-walk context.
39931e6b01fSNick Piggin  */
40031e6b01fSNick Piggin static int nameidata_drop_rcu(struct nameidata *nd)
40131e6b01fSNick Piggin {
40231e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
40331e6b01fSNick Piggin 	struct dentry *dentry = nd->path.dentry;
40431e6b01fSNick Piggin 
40531e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
40631e6b01fSNick Piggin 	if (nd->root.mnt) {
40731e6b01fSNick Piggin 		spin_lock(&fs->lock);
40831e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
40931e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
41031e6b01fSNick Piggin 			goto err_root;
41131e6b01fSNick Piggin 	}
41231e6b01fSNick Piggin 	spin_lock(&dentry->d_lock);
41331e6b01fSNick Piggin 	if (!__d_rcu_to_refcount(dentry, nd->seq))
41431e6b01fSNick Piggin 		goto err;
41531e6b01fSNick Piggin 	BUG_ON(nd->inode != dentry->d_inode);
41631e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
41731e6b01fSNick Piggin 	if (nd->root.mnt) {
41831e6b01fSNick Piggin 		path_get(&nd->root);
41931e6b01fSNick Piggin 		spin_unlock(&fs->lock);
42031e6b01fSNick Piggin 	}
42131e6b01fSNick Piggin 	mntget(nd->path.mnt);
42231e6b01fSNick Piggin 
42331e6b01fSNick Piggin 	rcu_read_unlock();
42431e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
42531e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
42631e6b01fSNick Piggin 	return 0;
42731e6b01fSNick Piggin err:
42831e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
42931e6b01fSNick Piggin err_root:
43031e6b01fSNick Piggin 	if (nd->root.mnt)
43131e6b01fSNick Piggin 		spin_unlock(&fs->lock);
43231e6b01fSNick Piggin 	return -ECHILD;
43331e6b01fSNick Piggin }
43431e6b01fSNick Piggin 
43531e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing.  */
43631e6b01fSNick Piggin static inline int nameidata_drop_rcu_maybe(struct nameidata *nd)
43731e6b01fSNick Piggin {
43831e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU)
43931e6b01fSNick Piggin 		return nameidata_drop_rcu(nd);
44031e6b01fSNick Piggin 	return 0;
44131e6b01fSNick Piggin }
44231e6b01fSNick Piggin 
44331e6b01fSNick Piggin /**
44431e6b01fSNick Piggin  * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk
44531e6b01fSNick Piggin  * @nd: nameidata pathwalk data to drop
44631e6b01fSNick Piggin  * @dentry: dentry to drop
44731e6b01fSNick Piggin  * @Returns: 0 on success, -ECHLID on failure
44831e6b01fSNick Piggin  *
44931e6b01fSNick Piggin  * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root,
45031e6b01fSNick Piggin  * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on
45131e6b01fSNick Piggin  * @nd. Must be called from rcu-walk context.
45231e6b01fSNick Piggin  */
45331e6b01fSNick Piggin static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry)
45431e6b01fSNick Piggin {
45531e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
45631e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
45731e6b01fSNick Piggin 
45831e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
45931e6b01fSNick Piggin 	if (nd->root.mnt) {
46031e6b01fSNick Piggin 		spin_lock(&fs->lock);
46131e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
46231e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
46331e6b01fSNick Piggin 			goto err_root;
46431e6b01fSNick Piggin 	}
46531e6b01fSNick Piggin 	spin_lock(&parent->d_lock);
46631e6b01fSNick Piggin 	spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
46731e6b01fSNick Piggin 	if (!__d_rcu_to_refcount(dentry, nd->seq))
46831e6b01fSNick Piggin 		goto err;
46931e6b01fSNick Piggin 	/*
47031e6b01fSNick Piggin 	 * If the sequence check on the child dentry passed, then the child has
47131e6b01fSNick Piggin 	 * not been removed from its parent. This means the parent dentry must
47231e6b01fSNick Piggin 	 * be valid and able to take a reference at this point.
47331e6b01fSNick Piggin 	 */
47431e6b01fSNick Piggin 	BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
47531e6b01fSNick Piggin 	BUG_ON(!parent->d_count);
47631e6b01fSNick Piggin 	parent->d_count++;
47731e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
47831e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
47931e6b01fSNick Piggin 	if (nd->root.mnt) {
48031e6b01fSNick Piggin 		path_get(&nd->root);
48131e6b01fSNick Piggin 		spin_unlock(&fs->lock);
48231e6b01fSNick Piggin 	}
48331e6b01fSNick Piggin 	mntget(nd->path.mnt);
48431e6b01fSNick Piggin 
48531e6b01fSNick Piggin 	rcu_read_unlock();
48631e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
48731e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
48831e6b01fSNick Piggin 	return 0;
48931e6b01fSNick Piggin err:
49031e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
49131e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
49231e6b01fSNick Piggin err_root:
49331e6b01fSNick Piggin 	if (nd->root.mnt)
49431e6b01fSNick Piggin 		spin_unlock(&fs->lock);
49531e6b01fSNick Piggin 	return -ECHILD;
49631e6b01fSNick Piggin }
49731e6b01fSNick Piggin 
49831e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing.  */
49931e6b01fSNick Piggin static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry)
50031e6b01fSNick Piggin {
50131e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU)
50231e6b01fSNick Piggin 		return nameidata_dentry_drop_rcu(nd, dentry);
50331e6b01fSNick Piggin 	return 0;
50431e6b01fSNick Piggin }
50531e6b01fSNick Piggin 
50631e6b01fSNick Piggin /**
50731e6b01fSNick Piggin  * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk
50831e6b01fSNick Piggin  * @nd: nameidata pathwalk data to drop
50931e6b01fSNick Piggin  * @Returns: 0 on success, -ECHLID on failure
51031e6b01fSNick Piggin  *
51131e6b01fSNick Piggin  * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk.
51231e6b01fSNick Piggin  * nd->path should be the final element of the lookup, so nd->root is discarded.
51331e6b01fSNick Piggin  * Must be called from rcu-walk context.
51431e6b01fSNick Piggin  */
51531e6b01fSNick Piggin static int nameidata_drop_rcu_last(struct nameidata *nd)
51631e6b01fSNick Piggin {
51731e6b01fSNick Piggin 	struct dentry *dentry = nd->path.dentry;
51831e6b01fSNick Piggin 
51931e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
52031e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
52131e6b01fSNick Piggin 	nd->root.mnt = NULL;
52231e6b01fSNick Piggin 	spin_lock(&dentry->d_lock);
52331e6b01fSNick Piggin 	if (!__d_rcu_to_refcount(dentry, nd->seq))
52431e6b01fSNick Piggin 		goto err_unlock;
52531e6b01fSNick Piggin 	BUG_ON(nd->inode != dentry->d_inode);
52631e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
52731e6b01fSNick Piggin 
52831e6b01fSNick Piggin 	mntget(nd->path.mnt);
52931e6b01fSNick Piggin 
53031e6b01fSNick Piggin 	rcu_read_unlock();
53131e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
53231e6b01fSNick Piggin 
53331e6b01fSNick Piggin 	return 0;
53431e6b01fSNick Piggin 
53531e6b01fSNick Piggin err_unlock:
53631e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
53731e6b01fSNick Piggin 	rcu_read_unlock();
53831e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
53931e6b01fSNick Piggin 	return -ECHILD;
54031e6b01fSNick Piggin }
54131e6b01fSNick Piggin 
54231e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing.  */
54331e6b01fSNick Piggin static inline int nameidata_drop_rcu_last_maybe(struct nameidata *nd)
54431e6b01fSNick Piggin {
54531e6b01fSNick Piggin 	if (likely(nd->flags & LOOKUP_RCU))
54631e6b01fSNick Piggin 		return nameidata_drop_rcu_last(nd);
54731e6b01fSNick Piggin 	return 0;
54831e6b01fSNick Piggin }
54931e6b01fSNick Piggin 
55031e6b01fSNick Piggin /**
551834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
552834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
553834f2a4aSTrond Myklebust  */
554834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
555834f2a4aSTrond Myklebust {
5560f7fc9e4SJosef "Jeff" Sipek 	if (nd->intent.open.file->f_path.dentry == NULL)
557834f2a4aSTrond Myklebust 		put_filp(nd->intent.open.file);
558834f2a4aSTrond Myklebust 	else
559834f2a4aSTrond Myklebust 		fput(nd->intent.open.file);
560834f2a4aSTrond Myklebust }
561834f2a4aSTrond Myklebust 
56234286d66SNick Piggin static int d_revalidate(struct dentry *dentry, struct nameidata *nd)
56334286d66SNick Piggin {
56434286d66SNick Piggin 	int status;
56534286d66SNick Piggin 
56634286d66SNick Piggin 	status = dentry->d_op->d_revalidate(dentry, nd);
56734286d66SNick Piggin 	if (status == -ECHILD) {
56834286d66SNick Piggin 		if (nameidata_dentry_drop_rcu(nd, dentry))
56934286d66SNick Piggin 			return status;
57034286d66SNick Piggin 		status = dentry->d_op->d_revalidate(dentry, nd);
57134286d66SNick Piggin 	}
57234286d66SNick Piggin 
57334286d66SNick Piggin 	return status;
57434286d66SNick Piggin }
57534286d66SNick Piggin 
576bcdc5e01SIan Kent static inline struct dentry *
577bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd)
578bcdc5e01SIan Kent {
57934286d66SNick Piggin 	int status;
58034286d66SNick Piggin 
58134286d66SNick Piggin 	status = d_revalidate(dentry, nd);
582bcdc5e01SIan Kent 	if (unlikely(status <= 0)) {
583bcdc5e01SIan Kent 		/*
584bcdc5e01SIan Kent 		 * The dentry failed validation.
585bcdc5e01SIan Kent 		 * If d_revalidate returned 0 attempt to invalidate
586bcdc5e01SIan Kent 		 * the dentry otherwise d_revalidate is asking us
587bcdc5e01SIan Kent 		 * to return a fail status.
588bcdc5e01SIan Kent 		 */
58934286d66SNick Piggin 		if (status < 0) {
59034286d66SNick Piggin 			/* If we're in rcu-walk, we don't have a ref */
59134286d66SNick Piggin 			if (!(nd->flags & LOOKUP_RCU))
59234286d66SNick Piggin 				dput(dentry);
59334286d66SNick Piggin 			dentry = ERR_PTR(status);
59434286d66SNick Piggin 
59534286d66SNick Piggin 		} else {
59634286d66SNick Piggin 			/* Don't d_invalidate in rcu-walk mode */
59734286d66SNick Piggin 			if (nameidata_dentry_drop_rcu_maybe(nd, dentry))
59834286d66SNick Piggin 				return ERR_PTR(-ECHILD);
599bcdc5e01SIan Kent 			if (!d_invalidate(dentry)) {
600bcdc5e01SIan Kent 				dput(dentry);
601bcdc5e01SIan Kent 				dentry = NULL;
602bcdc5e01SIan Kent 			}
603bcdc5e01SIan Kent 		}
604bcdc5e01SIan Kent 	}
605bcdc5e01SIan Kent 	return dentry;
606bcdc5e01SIan Kent }
607bcdc5e01SIan Kent 
608fb045adbSNick Piggin static inline int need_reval_dot(struct dentry *dentry)
609fb045adbSNick Piggin {
610fb045adbSNick Piggin 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
611fb045adbSNick Piggin 		return 0;
612fb045adbSNick Piggin 
613fb045adbSNick Piggin 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
614fb045adbSNick Piggin 		return 0;
615fb045adbSNick Piggin 
616fb045adbSNick Piggin 	return 1;
617fb045adbSNick Piggin }
618fb045adbSNick Piggin 
6191da177e4SLinus Torvalds /*
62039159de2SJeff Layton  * force_reval_path - force revalidation of a dentry
62139159de2SJeff Layton  *
62239159de2SJeff Layton  * In some situations the path walking code will trust dentries without
62339159de2SJeff Layton  * revalidating them. This causes problems for filesystems that depend on
62439159de2SJeff Layton  * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
62539159de2SJeff Layton  * (which indicates that it's possible for the dentry to go stale), force
62639159de2SJeff Layton  * a d_revalidate call before proceeding.
62739159de2SJeff Layton  *
62839159de2SJeff Layton  * Returns 0 if the revalidation was successful. If the revalidation fails,
62939159de2SJeff Layton  * either return the error returned by d_revalidate or -ESTALE if the
63039159de2SJeff Layton  * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
63139159de2SJeff Layton  * invalidate the dentry. It's up to the caller to handle putting references
63239159de2SJeff Layton  * to the path if necessary.
63339159de2SJeff Layton  */
63439159de2SJeff Layton static int
63539159de2SJeff Layton force_reval_path(struct path *path, struct nameidata *nd)
63639159de2SJeff Layton {
63739159de2SJeff Layton 	int status;
63839159de2SJeff Layton 	struct dentry *dentry = path->dentry;
63939159de2SJeff Layton 
64039159de2SJeff Layton 	/*
64139159de2SJeff Layton 	 * only check on filesystems where it's possible for the dentry to
642fb045adbSNick Piggin 	 * become stale.
64339159de2SJeff Layton 	 */
644fb045adbSNick Piggin 	if (!need_reval_dot(dentry))
64539159de2SJeff Layton 		return 0;
64639159de2SJeff Layton 
64734286d66SNick Piggin 	status = d_revalidate(dentry, nd);
64839159de2SJeff Layton 	if (status > 0)
64939159de2SJeff Layton 		return 0;
65039159de2SJeff Layton 
65139159de2SJeff Layton 	if (!status) {
65239159de2SJeff Layton 		d_invalidate(dentry);
65339159de2SJeff Layton 		status = -ESTALE;
65439159de2SJeff Layton 	}
65539159de2SJeff Layton 	return status;
65639159de2SJeff Layton }
65739159de2SJeff Layton 
65839159de2SJeff Layton /*
659b75b5086SAl Viro  * Short-cut version of permission(), for calling on directories
660b75b5086SAl Viro  * during pathname resolution.  Combines parts of permission()
661b75b5086SAl Viro  * and generic_permission(), and tests ONLY for MAY_EXEC permission.
6621da177e4SLinus Torvalds  *
6631da177e4SLinus Torvalds  * If appropriate, check DAC only.  If not appropriate, or
664b75b5086SAl Viro  * short-cut DAC fails, then call ->permission() to do more
6651da177e4SLinus Torvalds  * complete permission check.
6661da177e4SLinus Torvalds  */
667b74c79e9SNick Piggin static inline int exec_permission(struct inode *inode, unsigned int flags)
6681da177e4SLinus Torvalds {
6695909ccaaSLinus Torvalds 	int ret;
6701da177e4SLinus Torvalds 
671cb9179eaSLinus Torvalds 	if (inode->i_op->permission) {
672b74c79e9SNick Piggin 		ret = inode->i_op->permission(inode, MAY_EXEC, flags);
673b74c79e9SNick Piggin 	} else {
674b74c79e9SNick Piggin 		ret = acl_permission_check(inode, MAY_EXEC, flags,
675b74c79e9SNick Piggin 				inode->i_op->check_acl);
676cb9179eaSLinus Torvalds 	}
677b74c79e9SNick Piggin 	if (likely(!ret))
6781da177e4SLinus Torvalds 		goto ok;
679b74c79e9SNick Piggin 	if (ret == -ECHILD)
68031e6b01fSNick Piggin 		return ret;
6811da177e4SLinus Torvalds 
682f1ac9f6bSLinus Torvalds 	if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH))
6831da177e4SLinus Torvalds 		goto ok;
6841da177e4SLinus Torvalds 
6855909ccaaSLinus Torvalds 	return ret;
6861da177e4SLinus Torvalds ok:
687b74c79e9SNick Piggin 	return security_inode_exec_permission(inode, flags);
6881da177e4SLinus Torvalds }
6891da177e4SLinus Torvalds 
6902a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
6912a737871SAl Viro {
692f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
693f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
6942a737871SAl Viro }
6952a737871SAl Viro 
6966de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
6976de88d72SAl Viro 
69831e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
69931e6b01fSNick Piggin {
70031e6b01fSNick Piggin 	if (!nd->root.mnt) {
70131e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
702c28cc364SNick Piggin 		unsigned seq;
703c28cc364SNick Piggin 
704c28cc364SNick Piggin 		do {
705c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
70631e6b01fSNick Piggin 			nd->root = fs->root;
707c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
70831e6b01fSNick Piggin 	}
70931e6b01fSNick Piggin }
71031e6b01fSNick Piggin 
711f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
7121da177e4SLinus Torvalds {
71331e6b01fSNick Piggin 	int ret;
71431e6b01fSNick Piggin 
7151da177e4SLinus Torvalds 	if (IS_ERR(link))
7161da177e4SLinus Torvalds 		goto fail;
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	if (*link == '/') {
7192a737871SAl Viro 		set_root(nd);
7201d957f9bSJan Blunck 		path_put(&nd->path);
7212a737871SAl Viro 		nd->path = nd->root;
7222a737871SAl Viro 		path_get(&nd->root);
7231da177e4SLinus Torvalds 	}
72431e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
725b4091d5fSChristoph Hellwig 
72631e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
72731e6b01fSNick Piggin 	return ret;
7281da177e4SLinus Torvalds fail:
7291d957f9bSJan Blunck 	path_put(&nd->path);
7301da177e4SLinus Torvalds 	return PTR_ERR(link);
7311da177e4SLinus Torvalds }
7321da177e4SLinus Torvalds 
7331d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
734051d3812SIan Kent {
735051d3812SIan Kent 	dput(path->dentry);
7364ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
737051d3812SIan Kent 		mntput(path->mnt);
738051d3812SIan Kent }
739051d3812SIan Kent 
740051d3812SIan Kent static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
741051d3812SIan Kent {
74231e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
7434ac91378SJan Blunck 		dput(nd->path.dentry);
74431e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
7454ac91378SJan Blunck 			mntput(nd->path.mnt);
7469a229683SHuang Shijie 	}
74731e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
7484ac91378SJan Blunck 	nd->path.dentry = path->dentry;
749051d3812SIan Kent }
750051d3812SIan Kent 
751def4af30SAl Viro static __always_inline int
752def4af30SAl Viro __do_follow_link(struct path *path, struct nameidata *nd, void **p)
7531da177e4SLinus Torvalds {
7541da177e4SLinus Torvalds 	int error;
755cd4e91d3SAl Viro 	struct dentry *dentry = path->dentry;
7561da177e4SLinus Torvalds 
757d671a1cbSAl Viro 	touch_atime(path->mnt, dentry);
7581da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
759cd4e91d3SAl Viro 
7604ac91378SJan Blunck 	if (path->mnt != nd->path.mnt) {
761051d3812SIan Kent 		path_to_nameidata(path, nd);
76231e6b01fSNick Piggin 		nd->inode = nd->path.dentry->d_inode;
763051d3812SIan Kent 		dget(dentry);
764051d3812SIan Kent 	}
765cd4e91d3SAl Viro 	mntget(path->mnt);
76631e6b01fSNick Piggin 
76786acdca1SAl Viro 	nd->last_type = LAST_BIND;
768def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
769def4af30SAl Viro 	error = PTR_ERR(*p);
770def4af30SAl Viro 	if (!IS_ERR(*p)) {
7711da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
772cc314eefSLinus Torvalds 		error = 0;
7731da177e4SLinus Torvalds 		if (s)
7741da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
77539159de2SJeff Layton 		else if (nd->last_type == LAST_BIND) {
77639159de2SJeff Layton 			error = force_reval_path(&nd->path, nd);
77739159de2SJeff Layton 			if (error)
77839159de2SJeff Layton 				path_put(&nd->path);
77939159de2SJeff Layton 		}
7801da177e4SLinus Torvalds 	}
7811da177e4SLinus Torvalds 	return error;
7821da177e4SLinus Torvalds }
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds /*
7851da177e4SLinus Torvalds  * This limits recursive symlink follows to 8, while
7861da177e4SLinus Torvalds  * limiting consecutive symlinks to 40.
7871da177e4SLinus Torvalds  *
7881da177e4SLinus Torvalds  * Without that kind of total limit, nasty chains of consecutive
7891da177e4SLinus Torvalds  * symlinks can cause almost arbitrarily long lookups.
7901da177e4SLinus Torvalds  */
79190ebe565SAl Viro static inline int do_follow_link(struct path *path, struct nameidata *nd)
7921da177e4SLinus Torvalds {
793def4af30SAl Viro 	void *cookie;
7941da177e4SLinus Torvalds 	int err = -ELOOP;
7951da177e4SLinus Torvalds 	if (current->link_count >= MAX_NESTED_LINKS)
7961da177e4SLinus Torvalds 		goto loop;
7971da177e4SLinus Torvalds 	if (current->total_link_count >= 40)
7981da177e4SLinus Torvalds 		goto loop;
7991da177e4SLinus Torvalds 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
8001da177e4SLinus Torvalds 	cond_resched();
80190ebe565SAl Viro 	err = security_inode_follow_link(path->dentry, nd);
8021da177e4SLinus Torvalds 	if (err)
8031da177e4SLinus Torvalds 		goto loop;
8041da177e4SLinus Torvalds 	current->link_count++;
8051da177e4SLinus Torvalds 	current->total_link_count++;
8061da177e4SLinus Torvalds 	nd->depth++;
807def4af30SAl Viro 	err = __do_follow_link(path, nd, &cookie);
808def4af30SAl Viro 	if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link)
809def4af30SAl Viro 		path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie);
810258fa999SAl Viro 	path_put(path);
8111da177e4SLinus Torvalds 	current->link_count--;
8121da177e4SLinus Torvalds 	nd->depth--;
8131da177e4SLinus Torvalds 	return err;
8141da177e4SLinus Torvalds loop:
8151d957f9bSJan Blunck 	path_put_conditional(path, nd);
8161d957f9bSJan Blunck 	path_put(&nd->path);
8171da177e4SLinus Torvalds 	return err;
8181da177e4SLinus Torvalds }
8191da177e4SLinus Torvalds 
82031e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
82131e6b01fSNick Piggin {
82231e6b01fSNick Piggin 	struct vfsmount *parent;
82331e6b01fSNick Piggin 	struct dentry *mountpoint;
82431e6b01fSNick Piggin 
82531e6b01fSNick Piggin 	parent = path->mnt->mnt_parent;
82631e6b01fSNick Piggin 	if (parent == path->mnt)
82731e6b01fSNick Piggin 		return 0;
82831e6b01fSNick Piggin 	mountpoint = path->mnt->mnt_mountpoint;
82931e6b01fSNick Piggin 	path->dentry = mountpoint;
83031e6b01fSNick Piggin 	path->mnt = parent;
83131e6b01fSNick Piggin 	return 1;
83231e6b01fSNick Piggin }
83331e6b01fSNick Piggin 
834bab77ebfSAl Viro int follow_up(struct path *path)
8351da177e4SLinus Torvalds {
8361da177e4SLinus Torvalds 	struct vfsmount *parent;
8371da177e4SLinus Torvalds 	struct dentry *mountpoint;
83899b7db7bSNick Piggin 
83999b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
840bab77ebfSAl Viro 	parent = path->mnt->mnt_parent;
841bab77ebfSAl Viro 	if (parent == path->mnt) {
84299b7db7bSNick Piggin 		br_read_unlock(vfsmount_lock);
8431da177e4SLinus Torvalds 		return 0;
8441da177e4SLinus Torvalds 	}
8451da177e4SLinus Torvalds 	mntget(parent);
846bab77ebfSAl Viro 	mountpoint = dget(path->mnt->mnt_mountpoint);
84799b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
848bab77ebfSAl Viro 	dput(path->dentry);
849bab77ebfSAl Viro 	path->dentry = mountpoint;
850bab77ebfSAl Viro 	mntput(path->mnt);
851bab77ebfSAl Viro 	path->mnt = parent;
8521da177e4SLinus Torvalds 	return 1;
8531da177e4SLinus Torvalds }
8541da177e4SLinus Torvalds 
855b5c84bf6SNick Piggin /*
856b5c84bf6SNick Piggin  * serialization is taken care of in namespace.c
8571da177e4SLinus Torvalds  */
85831e6b01fSNick Piggin static void __follow_mount_rcu(struct nameidata *nd, struct path *path,
85931e6b01fSNick Piggin 				struct inode **inode)
86031e6b01fSNick Piggin {
86131e6b01fSNick Piggin 	while (d_mountpoint(path->dentry)) {
86231e6b01fSNick Piggin 		struct vfsmount *mounted;
86331e6b01fSNick Piggin 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
86431e6b01fSNick Piggin 		if (!mounted)
86531e6b01fSNick Piggin 			return;
86631e6b01fSNick Piggin 		path->mnt = mounted;
86731e6b01fSNick Piggin 		path->dentry = mounted->mnt_root;
86831e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
86931e6b01fSNick Piggin 		*inode = path->dentry->d_inode;
87031e6b01fSNick Piggin 	}
87131e6b01fSNick Piggin }
87231e6b01fSNick Piggin 
873463ffb2eSAl Viro static int __follow_mount(struct path *path)
874463ffb2eSAl Viro {
875463ffb2eSAl Viro 	int res = 0;
876463ffb2eSAl Viro 	while (d_mountpoint(path->dentry)) {
8771c755af4SAl Viro 		struct vfsmount *mounted = lookup_mnt(path);
878463ffb2eSAl Viro 		if (!mounted)
879463ffb2eSAl Viro 			break;
880463ffb2eSAl Viro 		dput(path->dentry);
881463ffb2eSAl Viro 		if (res)
882463ffb2eSAl Viro 			mntput(path->mnt);
883463ffb2eSAl Viro 		path->mnt = mounted;
884463ffb2eSAl Viro 		path->dentry = dget(mounted->mnt_root);
885463ffb2eSAl Viro 		res = 1;
886463ffb2eSAl Viro 	}
887463ffb2eSAl Viro 	return res;
888463ffb2eSAl Viro }
889463ffb2eSAl Viro 
89079ed0226SAl Viro static void follow_mount(struct path *path)
8911da177e4SLinus Torvalds {
89279ed0226SAl Viro 	while (d_mountpoint(path->dentry)) {
8931c755af4SAl Viro 		struct vfsmount *mounted = lookup_mnt(path);
8941da177e4SLinus Torvalds 		if (!mounted)
8951da177e4SLinus Torvalds 			break;
89679ed0226SAl Viro 		dput(path->dentry);
89779ed0226SAl Viro 		mntput(path->mnt);
89879ed0226SAl Viro 		path->mnt = mounted;
89979ed0226SAl Viro 		path->dentry = dget(mounted->mnt_root);
9001da177e4SLinus Torvalds 	}
9011da177e4SLinus Torvalds }
9021da177e4SLinus Torvalds 
9039393bd07SAl Viro int follow_down(struct path *path)
9041da177e4SLinus Torvalds {
9051da177e4SLinus Torvalds 	struct vfsmount *mounted;
9061da177e4SLinus Torvalds 
9071c755af4SAl Viro 	mounted = lookup_mnt(path);
9081da177e4SLinus Torvalds 	if (mounted) {
9099393bd07SAl Viro 		dput(path->dentry);
9109393bd07SAl Viro 		mntput(path->mnt);
9119393bd07SAl Viro 		path->mnt = mounted;
9129393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
9131da177e4SLinus Torvalds 		return 1;
9141da177e4SLinus Torvalds 	}
9151da177e4SLinus Torvalds 	return 0;
9161da177e4SLinus Torvalds }
9171da177e4SLinus Torvalds 
91831e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
91931e6b01fSNick Piggin {
92031e6b01fSNick Piggin 	struct inode *inode = nd->inode;
92131e6b01fSNick Piggin 
92231e6b01fSNick Piggin 	set_root_rcu(nd);
92331e6b01fSNick Piggin 
92431e6b01fSNick Piggin 	while(1) {
92531e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
92631e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
92731e6b01fSNick Piggin 			break;
92831e6b01fSNick Piggin 		}
92931e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
93031e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
93131e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
93231e6b01fSNick Piggin 			unsigned seq;
93331e6b01fSNick Piggin 
93431e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
93531e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
93631e6b01fSNick Piggin 				return -ECHILD;
93731e6b01fSNick Piggin 			inode = parent->d_inode;
93831e6b01fSNick Piggin 			nd->path.dentry = parent;
93931e6b01fSNick Piggin 			nd->seq = seq;
94031e6b01fSNick Piggin 			break;
94131e6b01fSNick Piggin 		}
94231e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
94331e6b01fSNick Piggin 			break;
94431e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
94531e6b01fSNick Piggin 		inode = nd->path.dentry->d_inode;
94631e6b01fSNick Piggin 	}
94731e6b01fSNick Piggin 	__follow_mount_rcu(nd, &nd->path, &inode);
94831e6b01fSNick Piggin 	nd->inode = inode;
94931e6b01fSNick Piggin 
95031e6b01fSNick Piggin 	return 0;
95131e6b01fSNick Piggin }
95231e6b01fSNick Piggin 
95331e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
9541da177e4SLinus Torvalds {
9552a737871SAl Viro 	set_root(nd);
956e518ddb7SAndreas Mohr 
9571da177e4SLinus Torvalds 	while(1) {
9584ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
9591da177e4SLinus Torvalds 
9602a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
9612a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
9621da177e4SLinus Torvalds 			break;
9631da177e4SLinus Torvalds 		}
9644ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
9653088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
9663088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
9671da177e4SLinus Torvalds 			dput(old);
9681da177e4SLinus Torvalds 			break;
9691da177e4SLinus Torvalds 		}
9703088dd70SAl Viro 		if (!follow_up(&nd->path))
9711da177e4SLinus Torvalds 			break;
9721da177e4SLinus Torvalds 	}
97379ed0226SAl Viro 	follow_mount(&nd->path);
97431e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
9751da177e4SLinus Torvalds }
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds /*
978baa03890SNick Piggin  * Allocate a dentry with name and parent, and perform a parent
979baa03890SNick Piggin  * directory ->lookup on it. Returns the new dentry, or ERR_PTR
980baa03890SNick Piggin  * on error. parent->d_inode->i_mutex must be held. d_lookup must
981baa03890SNick Piggin  * have verified that no child exists while under i_mutex.
982baa03890SNick Piggin  */
983baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent,
984baa03890SNick Piggin 				struct qstr *name, struct nameidata *nd)
985baa03890SNick Piggin {
986baa03890SNick Piggin 	struct inode *inode = parent->d_inode;
987baa03890SNick Piggin 	struct dentry *dentry;
988baa03890SNick Piggin 	struct dentry *old;
989baa03890SNick Piggin 
990baa03890SNick Piggin 	/* Don't create child dentry for a dead directory. */
991baa03890SNick Piggin 	if (unlikely(IS_DEADDIR(inode)))
992baa03890SNick Piggin 		return ERR_PTR(-ENOENT);
993baa03890SNick Piggin 
994baa03890SNick Piggin 	dentry = d_alloc(parent, name);
995baa03890SNick Piggin 	if (unlikely(!dentry))
996baa03890SNick Piggin 		return ERR_PTR(-ENOMEM);
997baa03890SNick Piggin 
998baa03890SNick Piggin 	old = inode->i_op->lookup(inode, dentry, nd);
999baa03890SNick Piggin 	if (unlikely(old)) {
1000baa03890SNick Piggin 		dput(dentry);
1001baa03890SNick Piggin 		dentry = old;
1002baa03890SNick Piggin 	}
1003baa03890SNick Piggin 	return dentry;
1004baa03890SNick Piggin }
1005baa03890SNick Piggin 
1006baa03890SNick Piggin /*
10071da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
10081da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
10091da177e4SLinus Torvalds  *  It _is_ time-critical.
10101da177e4SLinus Torvalds  */
10111da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
101231e6b01fSNick Piggin 			struct path *path, struct inode **inode)
10131da177e4SLinus Torvalds {
10144ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
101531e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
10166e6b1bd1SAl Viro 	struct inode *dir;
10173cac260aSAl Viro 	/*
10183cac260aSAl Viro 	 * See if the low-level filesystem might want
10193cac260aSAl Viro 	 * to use its own hash..
10203cac260aSAl Viro 	 */
1021fb045adbSNick Piggin 	if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
102231e6b01fSNick Piggin 		int err = parent->d_op->d_hash(parent, nd->inode, name);
10233cac260aSAl Viro 		if (err < 0)
10243cac260aSAl Viro 			return err;
10253cac260aSAl Viro 	}
10261da177e4SLinus Torvalds 
1027b04f784eSNick Piggin 	/*
1028b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1029b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1030b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1031b04f784eSNick Piggin 	 */
103231e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
103331e6b01fSNick Piggin 		unsigned seq;
103431e6b01fSNick Piggin 
103531e6b01fSNick Piggin 		*inode = nd->inode;
103631e6b01fSNick Piggin 		dentry = __d_lookup_rcu(parent, name, &seq, inode);
103731e6b01fSNick Piggin 		if (!dentry) {
103831e6b01fSNick Piggin 			if (nameidata_drop_rcu(nd))
103931e6b01fSNick Piggin 				return -ECHILD;
104031e6b01fSNick Piggin 			goto need_lookup;
104131e6b01fSNick Piggin 		}
104231e6b01fSNick Piggin 		/* Memory barrier in read_seqcount_begin of child is enough */
104331e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
104431e6b01fSNick Piggin 			return -ECHILD;
104531e6b01fSNick Piggin 
104631e6b01fSNick Piggin 		nd->seq = seq;
104734286d66SNick Piggin 		if (dentry->d_flags & DCACHE_OP_REVALIDATE)
104831e6b01fSNick Piggin 			goto need_revalidate;
104931e6b01fSNick Piggin 		path->mnt = mnt;
105031e6b01fSNick Piggin 		path->dentry = dentry;
105131e6b01fSNick Piggin 		__follow_mount_rcu(nd, path, inode);
105231e6b01fSNick Piggin 	} else {
105331e6b01fSNick Piggin 		dentry = __d_lookup(parent, name);
10541da177e4SLinus Torvalds 		if (!dentry)
10551da177e4SLinus Torvalds 			goto need_lookup;
10562e2e88eaSNick Piggin found:
1057fb045adbSNick Piggin 		if (dentry->d_flags & DCACHE_OP_REVALIDATE)
10581da177e4SLinus Torvalds 			goto need_revalidate;
10591da177e4SLinus Torvalds done:
10601da177e4SLinus Torvalds 		path->mnt = mnt;
10611da177e4SLinus Torvalds 		path->dentry = dentry;
1062634ee701SAl Viro 		__follow_mount(path);
106331e6b01fSNick Piggin 		*inode = path->dentry->d_inode;
106431e6b01fSNick Piggin 	}
10651da177e4SLinus Torvalds 	return 0;
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds need_lookup:
10686e6b1bd1SAl Viro 	dir = parent->d_inode;
106931e6b01fSNick Piggin 	BUG_ON(nd->inode != dir);
10706e6b1bd1SAl Viro 
10716e6b1bd1SAl Viro 	mutex_lock(&dir->i_mutex);
10726e6b1bd1SAl Viro 	/*
10736e6b1bd1SAl Viro 	 * First re-do the cached lookup just in case it was created
1074b04f784eSNick Piggin 	 * while we waited for the directory semaphore, or the first
1075b04f784eSNick Piggin 	 * lookup failed due to an unrelated rename.
10766e6b1bd1SAl Viro 	 *
1077b04f784eSNick Piggin 	 * This could use version numbering or similar to avoid unnecessary
1078b04f784eSNick Piggin 	 * cache lookups, but then we'd have to do the first lookup in the
1079b04f784eSNick Piggin 	 * non-racy way. However in the common case here, everything should
1080b04f784eSNick Piggin 	 * be hot in cache, so would it be a big win?
10816e6b1bd1SAl Viro 	 */
10826e6b1bd1SAl Viro 	dentry = d_lookup(parent, name);
1083baa03890SNick Piggin 	if (likely(!dentry)) {
1084baa03890SNick Piggin 		dentry = d_alloc_and_lookup(parent, name, nd);
10856e6b1bd1SAl Viro 		mutex_unlock(&dir->i_mutex);
10866e6b1bd1SAl Viro 		if (IS_ERR(dentry))
10876e6b1bd1SAl Viro 			goto fail;
10886e6b1bd1SAl Viro 		goto done;
10896e6b1bd1SAl Viro 	}
10906e6b1bd1SAl Viro 	/*
10916e6b1bd1SAl Viro 	 * Uhhuh! Nasty case: the cache was re-populated while
10926e6b1bd1SAl Viro 	 * we waited on the semaphore. Need to revalidate.
10936e6b1bd1SAl Viro 	 */
10946e6b1bd1SAl Viro 	mutex_unlock(&dir->i_mutex);
10952e2e88eaSNick Piggin 	goto found;
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds need_revalidate:
1098bcdc5e01SIan Kent 	dentry = do_revalidate(dentry, nd);
1099bcdc5e01SIan Kent 	if (!dentry)
11001da177e4SLinus Torvalds 		goto need_lookup;
1101bcdc5e01SIan Kent 	if (IS_ERR(dentry))
1102bcdc5e01SIan Kent 		goto fail;
1103bcdc5e01SIan Kent 	goto done;
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds fail:
11061da177e4SLinus Torvalds 	return PTR_ERR(dentry);
11071da177e4SLinus Torvalds }
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds /*
1110ac278a9cSAl Viro  * This is a temporary kludge to deal with "automount" symlinks; proper
1111ac278a9cSAl Viro  * solution is to trigger them on follow_mount(), so that do_lookup()
1112ac278a9cSAl Viro  * would DTRT.  To be killed before 2.6.34-final.
1113ac278a9cSAl Viro  */
1114ac278a9cSAl Viro static inline int follow_on_final(struct inode *inode, unsigned lookup_flags)
1115ac278a9cSAl Viro {
1116ac278a9cSAl Viro 	return inode && unlikely(inode->i_op->follow_link) &&
1117ac278a9cSAl Viro 		((lookup_flags & LOOKUP_FOLLOW) || S_ISDIR(inode->i_mode));
1118ac278a9cSAl Viro }
1119ac278a9cSAl Viro 
1120ac278a9cSAl Viro /*
11211da177e4SLinus Torvalds  * Name resolution.
1122ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1123ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
11241da177e4SLinus Torvalds  *
1125ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1126ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
11271da177e4SLinus Torvalds  */
11286de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
11291da177e4SLinus Torvalds {
11301da177e4SLinus Torvalds 	struct path next;
11311da177e4SLinus Torvalds 	int err;
11321da177e4SLinus Torvalds 	unsigned int lookup_flags = nd->flags;
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds 	while (*name=='/')
11351da177e4SLinus Torvalds 		name++;
11361da177e4SLinus Torvalds 	if (!*name)
11371da177e4SLinus Torvalds 		goto return_reval;
11381da177e4SLinus Torvalds 
11391da177e4SLinus Torvalds 	if (nd->depth)
1140f55eab82STrond Myklebust 		lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
11431da177e4SLinus Torvalds 	for(;;) {
114431e6b01fSNick Piggin 		struct inode *inode;
11451da177e4SLinus Torvalds 		unsigned long hash;
11461da177e4SLinus Torvalds 		struct qstr this;
11471da177e4SLinus Torvalds 		unsigned int c;
11481da177e4SLinus Torvalds 
1149cdce5d6bSTrond Myklebust 		nd->flags |= LOOKUP_CONTINUE;
115031e6b01fSNick Piggin 		if (nd->flags & LOOKUP_RCU) {
1151b74c79e9SNick Piggin 			err = exec_permission(nd->inode, IPERM_FLAG_RCU);
115231e6b01fSNick Piggin 			if (err == -ECHILD) {
115331e6b01fSNick Piggin 				if (nameidata_drop_rcu(nd))
115431e6b01fSNick Piggin 					return -ECHILD;
115531e6b01fSNick Piggin 				goto exec_again;
115631e6b01fSNick Piggin 			}
115731e6b01fSNick Piggin 		} else {
115831e6b01fSNick Piggin exec_again:
1159b74c79e9SNick Piggin 			err = exec_permission(nd->inode, 0);
116031e6b01fSNick Piggin 		}
11611da177e4SLinus Torvalds  		if (err)
11621da177e4SLinus Torvalds 			break;
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 		this.name = name;
11651da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 		hash = init_name_hash();
11681da177e4SLinus Torvalds 		do {
11691da177e4SLinus Torvalds 			name++;
11701da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
11711da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
11721da177e4SLinus Torvalds 		} while (c && (c != '/'));
11731da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
11741da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 		/* remove trailing slashes? */
11771da177e4SLinus Torvalds 		if (!c)
11781da177e4SLinus Torvalds 			goto last_component;
11791da177e4SLinus Torvalds 		while (*++name == '/');
11801da177e4SLinus Torvalds 		if (!*name)
11811da177e4SLinus Torvalds 			goto last_with_slashes;
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds 		/*
11841da177e4SLinus Torvalds 		 * "." and ".." are special - ".." especially so because it has
11851da177e4SLinus Torvalds 		 * to be able to know about the current root directory and
11861da177e4SLinus Torvalds 		 * parent relationships.
11871da177e4SLinus Torvalds 		 */
11881da177e4SLinus Torvalds 		if (this.name[0] == '.') switch (this.len) {
11891da177e4SLinus Torvalds 			default:
11901da177e4SLinus Torvalds 				break;
11911da177e4SLinus Torvalds 			case 2:
11921da177e4SLinus Torvalds 				if (this.name[1] != '.')
11931da177e4SLinus Torvalds 					break;
119431e6b01fSNick Piggin 				if (nd->flags & LOOKUP_RCU) {
119531e6b01fSNick Piggin 					if (follow_dotdot_rcu(nd))
119631e6b01fSNick Piggin 						return -ECHILD;
119731e6b01fSNick Piggin 				} else
119858c465ebSAl Viro 					follow_dotdot(nd);
11991da177e4SLinus Torvalds 				/* fallthrough */
12001da177e4SLinus Torvalds 			case 1:
12011da177e4SLinus Torvalds 				continue;
12021da177e4SLinus Torvalds 		}
12031da177e4SLinus Torvalds 		/* This does the actual lookups.. */
120431e6b01fSNick Piggin 		err = do_lookup(nd, &this, &next, &inode);
12051da177e4SLinus Torvalds 		if (err)
12061da177e4SLinus Torvalds 			break;
12071da177e4SLinus Torvalds 		err = -ENOENT;
12081da177e4SLinus Torvalds 		if (!inode)
12091da177e4SLinus Torvalds 			goto out_dput;
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 		if (inode->i_op->follow_link) {
121231e6b01fSNick Piggin 			/* We commonly drop rcu-walk here */
121331e6b01fSNick Piggin 			if (nameidata_dentry_drop_rcu_maybe(nd, next.dentry))
121431e6b01fSNick Piggin 				return -ECHILD;
121531e6b01fSNick Piggin 			BUG_ON(inode != next.dentry->d_inode);
121690ebe565SAl Viro 			err = do_follow_link(&next, nd);
12171da177e4SLinus Torvalds 			if (err)
12181da177e4SLinus Torvalds 				goto return_err;
121931e6b01fSNick Piggin 			nd->inode = nd->path.dentry->d_inode;
12201da177e4SLinus Torvalds 			err = -ENOENT;
122131e6b01fSNick Piggin 			if (!nd->inode)
12221da177e4SLinus Torvalds 				break;
122331e6b01fSNick Piggin 		} else {
122409dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
122531e6b01fSNick Piggin 			nd->inode = inode;
122631e6b01fSNick Piggin 		}
12271da177e4SLinus Torvalds 		err = -ENOTDIR;
122831e6b01fSNick Piggin 		if (!nd->inode->i_op->lookup)
12291da177e4SLinus Torvalds 			break;
12301da177e4SLinus Torvalds 		continue;
12311da177e4SLinus Torvalds 		/* here ends the main loop */
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds last_with_slashes:
12341da177e4SLinus Torvalds 		lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
12351da177e4SLinus Torvalds last_component:
1236f55eab82STrond Myklebust 		/* Clear LOOKUP_CONTINUE iff it was previously unset */
1237f55eab82STrond Myklebust 		nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
12381da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_PARENT)
12391da177e4SLinus Torvalds 			goto lookup_parent;
12401da177e4SLinus Torvalds 		if (this.name[0] == '.') switch (this.len) {
12411da177e4SLinus Torvalds 			default:
12421da177e4SLinus Torvalds 				break;
12431da177e4SLinus Torvalds 			case 2:
12441da177e4SLinus Torvalds 				if (this.name[1] != '.')
12451da177e4SLinus Torvalds 					break;
124631e6b01fSNick Piggin 				if (nd->flags & LOOKUP_RCU) {
124731e6b01fSNick Piggin 					if (follow_dotdot_rcu(nd))
124831e6b01fSNick Piggin 						return -ECHILD;
124931e6b01fSNick Piggin 				} else
125058c465ebSAl Viro 					follow_dotdot(nd);
12511da177e4SLinus Torvalds 				/* fallthrough */
12521da177e4SLinus Torvalds 			case 1:
12531da177e4SLinus Torvalds 				goto return_reval;
12541da177e4SLinus Torvalds 		}
125531e6b01fSNick Piggin 		err = do_lookup(nd, &this, &next, &inode);
12561da177e4SLinus Torvalds 		if (err)
12571da177e4SLinus Torvalds 			break;
1258ac278a9cSAl Viro 		if (follow_on_final(inode, lookup_flags)) {
125931e6b01fSNick Piggin 			if (nameidata_dentry_drop_rcu_maybe(nd, next.dentry))
126031e6b01fSNick Piggin 				return -ECHILD;
126131e6b01fSNick Piggin 			BUG_ON(inode != next.dentry->d_inode);
126290ebe565SAl Viro 			err = do_follow_link(&next, nd);
12631da177e4SLinus Torvalds 			if (err)
12641da177e4SLinus Torvalds 				goto return_err;
126531e6b01fSNick Piggin 			nd->inode = nd->path.dentry->d_inode;
126631e6b01fSNick Piggin 		} else {
126709dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
126831e6b01fSNick Piggin 			nd->inode = inode;
126931e6b01fSNick Piggin 		}
12701da177e4SLinus Torvalds 		err = -ENOENT;
127131e6b01fSNick Piggin 		if (!nd->inode)
12721da177e4SLinus Torvalds 			break;
12731da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_DIRECTORY) {
12741da177e4SLinus Torvalds 			err = -ENOTDIR;
127531e6b01fSNick Piggin 			if (!nd->inode->i_op->lookup)
12761da177e4SLinus Torvalds 				break;
12771da177e4SLinus Torvalds 		}
12781da177e4SLinus Torvalds 		goto return_base;
12791da177e4SLinus Torvalds lookup_parent:
12801da177e4SLinus Torvalds 		nd->last = this;
12811da177e4SLinus Torvalds 		nd->last_type = LAST_NORM;
12821da177e4SLinus Torvalds 		if (this.name[0] != '.')
12831da177e4SLinus Torvalds 			goto return_base;
12841da177e4SLinus Torvalds 		if (this.len == 1)
12851da177e4SLinus Torvalds 			nd->last_type = LAST_DOT;
12861da177e4SLinus Torvalds 		else if (this.len == 2 && this.name[1] == '.')
12871da177e4SLinus Torvalds 			nd->last_type = LAST_DOTDOT;
12881da177e4SLinus Torvalds 		else
12891da177e4SLinus Torvalds 			goto return_base;
12901da177e4SLinus Torvalds return_reval:
12911da177e4SLinus Torvalds 		/*
12921da177e4SLinus Torvalds 		 * We bypassed the ordinary revalidation routines.
12931da177e4SLinus Torvalds 		 * We may need to check the cached dentry for staleness.
12941da177e4SLinus Torvalds 		 */
1295fb045adbSNick Piggin 		if (need_reval_dot(nd->path.dentry)) {
12961da177e4SLinus Torvalds 			/* Note: we do not d_invalidate() */
129734286d66SNick Piggin 			err = d_revalidate(nd->path.dentry, nd);
129834286d66SNick Piggin 			if (!err)
129934286d66SNick Piggin 				err = -ESTALE;
130034286d66SNick Piggin 			if (err < 0)
13011da177e4SLinus Torvalds 				break;
13021da177e4SLinus Torvalds 		}
13031da177e4SLinus Torvalds return_base:
130431e6b01fSNick Piggin 		if (nameidata_drop_rcu_last_maybe(nd))
130531e6b01fSNick Piggin 			return -ECHILD;
13061da177e4SLinus Torvalds 		return 0;
13071da177e4SLinus Torvalds out_dput:
130831e6b01fSNick Piggin 		if (!(nd->flags & LOOKUP_RCU))
13091d957f9bSJan Blunck 			path_put_conditional(&next, nd);
13101da177e4SLinus Torvalds 		break;
13111da177e4SLinus Torvalds 	}
131231e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU))
13131d957f9bSJan Blunck 		path_put(&nd->path);
13141da177e4SLinus Torvalds return_err:
13151da177e4SLinus Torvalds 	return err;
13161da177e4SLinus Torvalds }
13171da177e4SLinus Torvalds 
131831e6b01fSNick Piggin static inline int path_walk_rcu(const char *name, struct nameidata *nd)
131931e6b01fSNick Piggin {
132031e6b01fSNick Piggin 	current->total_link_count = 0;
132131e6b01fSNick Piggin 
132231e6b01fSNick Piggin 	return link_path_walk(name, nd);
132331e6b01fSNick Piggin }
132431e6b01fSNick Piggin 
132531e6b01fSNick Piggin static inline int path_walk_simple(const char *name, struct nameidata *nd)
132631e6b01fSNick Piggin {
132731e6b01fSNick Piggin 	current->total_link_count = 0;
132831e6b01fSNick Piggin 
132931e6b01fSNick Piggin 	return link_path_walk(name, nd);
133031e6b01fSNick Piggin }
133131e6b01fSNick Piggin 
1332fc9b52cdSHarvey Harrison static int path_walk(const char *name, struct nameidata *nd)
13331da177e4SLinus Torvalds {
13346de88d72SAl Viro 	struct path save = nd->path;
13356de88d72SAl Viro 	int result;
13366de88d72SAl Viro 
13371da177e4SLinus Torvalds 	current->total_link_count = 0;
13386de88d72SAl Viro 
13396de88d72SAl Viro 	/* make sure the stuff we saved doesn't go away */
13406de88d72SAl Viro 	path_get(&save);
13416de88d72SAl Viro 
13426de88d72SAl Viro 	result = link_path_walk(name, nd);
13436de88d72SAl Viro 	if (result == -ESTALE) {
13446de88d72SAl Viro 		/* nd->path had been dropped */
13456de88d72SAl Viro 		current->total_link_count = 0;
13466de88d72SAl Viro 		nd->path = save;
13476de88d72SAl Viro 		path_get(&nd->path);
13486de88d72SAl Viro 		nd->flags |= LOOKUP_REVAL;
13496de88d72SAl Viro 		result = link_path_walk(name, nd);
13506de88d72SAl Viro 	}
13516de88d72SAl Viro 
13526de88d72SAl Viro 	path_put(&save);
13536de88d72SAl Viro 
13546de88d72SAl Viro 	return result;
13551da177e4SLinus Torvalds }
13561da177e4SLinus Torvalds 
135731e6b01fSNick Piggin static void path_finish_rcu(struct nameidata *nd)
135831e6b01fSNick Piggin {
135931e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
136031e6b01fSNick Piggin 		/* RCU dangling. Cancel it. */
136131e6b01fSNick Piggin 		nd->flags &= ~LOOKUP_RCU;
136231e6b01fSNick Piggin 		nd->root.mnt = NULL;
136331e6b01fSNick Piggin 		rcu_read_unlock();
136431e6b01fSNick Piggin 		br_read_unlock(vfsmount_lock);
136531e6b01fSNick Piggin 	}
136631e6b01fSNick Piggin 	if (nd->file)
136731e6b01fSNick Piggin 		fput(nd->file);
136831e6b01fSNick Piggin }
136931e6b01fSNick Piggin 
137031e6b01fSNick Piggin static int path_init_rcu(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
137131e6b01fSNick Piggin {
137231e6b01fSNick Piggin 	int retval = 0;
137331e6b01fSNick Piggin 	int fput_needed;
137431e6b01fSNick Piggin 	struct file *file;
137531e6b01fSNick Piggin 
137631e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
137731e6b01fSNick Piggin 	nd->flags = flags | LOOKUP_RCU;
137831e6b01fSNick Piggin 	nd->depth = 0;
137931e6b01fSNick Piggin 	nd->root.mnt = NULL;
138031e6b01fSNick Piggin 	nd->file = NULL;
138131e6b01fSNick Piggin 
138231e6b01fSNick Piggin 	if (*name=='/') {
138331e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
1384c28cc364SNick Piggin 		unsigned seq;
138531e6b01fSNick Piggin 
138631e6b01fSNick Piggin 		br_read_lock(vfsmount_lock);
138731e6b01fSNick Piggin 		rcu_read_lock();
138831e6b01fSNick Piggin 
1389c28cc364SNick Piggin 		do {
1390c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
139131e6b01fSNick Piggin 			nd->root = fs->root;
139231e6b01fSNick Piggin 			nd->path = nd->root;
1393c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1394c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
139531e6b01fSNick Piggin 
139631e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
139731e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
1398c28cc364SNick Piggin 		unsigned seq;
139931e6b01fSNick Piggin 
140031e6b01fSNick Piggin 		br_read_lock(vfsmount_lock);
140131e6b01fSNick Piggin 		rcu_read_lock();
140231e6b01fSNick Piggin 
1403c28cc364SNick Piggin 		do {
1404c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
140531e6b01fSNick Piggin 			nd->path = fs->pwd;
1406c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1407c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
1408c28cc364SNick Piggin 
140931e6b01fSNick Piggin 	} else {
141031e6b01fSNick Piggin 		struct dentry *dentry;
141131e6b01fSNick Piggin 
141231e6b01fSNick Piggin 		file = fget_light(dfd, &fput_needed);
141331e6b01fSNick Piggin 		retval = -EBADF;
141431e6b01fSNick Piggin 		if (!file)
141531e6b01fSNick Piggin 			goto out_fail;
141631e6b01fSNick Piggin 
141731e6b01fSNick Piggin 		dentry = file->f_path.dentry;
141831e6b01fSNick Piggin 
141931e6b01fSNick Piggin 		retval = -ENOTDIR;
142031e6b01fSNick Piggin 		if (!S_ISDIR(dentry->d_inode->i_mode))
142131e6b01fSNick Piggin 			goto fput_fail;
142231e6b01fSNick Piggin 
142331e6b01fSNick Piggin 		retval = file_permission(file, MAY_EXEC);
142431e6b01fSNick Piggin 		if (retval)
142531e6b01fSNick Piggin 			goto fput_fail;
142631e6b01fSNick Piggin 
142731e6b01fSNick Piggin 		nd->path = file->f_path;
142831e6b01fSNick Piggin 		if (fput_needed)
142931e6b01fSNick Piggin 			nd->file = file;
143031e6b01fSNick Piggin 
1431c28cc364SNick Piggin 		nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
143231e6b01fSNick Piggin 		br_read_lock(vfsmount_lock);
143331e6b01fSNick Piggin 		rcu_read_lock();
143431e6b01fSNick Piggin 	}
143531e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
143631e6b01fSNick Piggin 	return 0;
143731e6b01fSNick Piggin 
143831e6b01fSNick Piggin fput_fail:
143931e6b01fSNick Piggin 	fput_light(file, fput_needed);
144031e6b01fSNick Piggin out_fail:
144131e6b01fSNick Piggin 	return retval;
144231e6b01fSNick Piggin }
144331e6b01fSNick Piggin 
14449b4a9b14SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
14451da177e4SLinus Torvalds {
1446ea3834d9SPrasanna Meda 	int retval = 0;
1447170aa3d0SUlrich Drepper 	int fput_needed;
1448170aa3d0SUlrich Drepper 	struct file *file;
14491da177e4SLinus Torvalds 
14501da177e4SLinus Torvalds 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
14511da177e4SLinus Torvalds 	nd->flags = flags;
14521da177e4SLinus Torvalds 	nd->depth = 0;
14532a737871SAl Viro 	nd->root.mnt = NULL;
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 	if (*name=='/') {
14562a737871SAl Viro 		set_root(nd);
14572a737871SAl Viro 		nd->path = nd->root;
14582a737871SAl Viro 		path_get(&nd->root);
14595590ff0dSUlrich Drepper 	} else if (dfd == AT_FDCWD) {
1460f7ad3c6bSMiklos Szeredi 		get_fs_pwd(current->fs, &nd->path);
14615590ff0dSUlrich Drepper 	} else {
14625590ff0dSUlrich Drepper 		struct dentry *dentry;
14635590ff0dSUlrich Drepper 
14645590ff0dSUlrich Drepper 		file = fget_light(dfd, &fput_needed);
14655590ff0dSUlrich Drepper 		retval = -EBADF;
1466170aa3d0SUlrich Drepper 		if (!file)
14676d09bb62STrond Myklebust 			goto out_fail;
14685590ff0dSUlrich Drepper 
14690f7fc9e4SJosef "Jeff" Sipek 		dentry = file->f_path.dentry;
14705590ff0dSUlrich Drepper 
14715590ff0dSUlrich Drepper 		retval = -ENOTDIR;
1472170aa3d0SUlrich Drepper 		if (!S_ISDIR(dentry->d_inode->i_mode))
14736d09bb62STrond Myklebust 			goto fput_fail;
14745590ff0dSUlrich Drepper 
14755590ff0dSUlrich Drepper 		retval = file_permission(file, MAY_EXEC);
1476170aa3d0SUlrich Drepper 		if (retval)
14776d09bb62STrond Myklebust 			goto fput_fail;
14785590ff0dSUlrich Drepper 
14795dd784d0SJan Blunck 		nd->path = file->f_path;
14805dd784d0SJan Blunck 		path_get(&file->f_path);
14815590ff0dSUlrich Drepper 
14825590ff0dSUlrich Drepper 		fput_light(file, fput_needed);
14831da177e4SLinus Torvalds 	}
148431e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
14859b4a9b14SAl Viro 	return 0;
14862dfdd266SJosef 'Jeff' Sipek 
14879b4a9b14SAl Viro fput_fail:
14889b4a9b14SAl Viro 	fput_light(file, fput_needed);
14899b4a9b14SAl Viro out_fail:
14909b4a9b14SAl Viro 	return retval;
14919b4a9b14SAl Viro }
14929b4a9b14SAl Viro 
14939b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
14949b4a9b14SAl Viro static int do_path_lookup(int dfd, const char *name,
14959b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
14969b4a9b14SAl Viro {
149731e6b01fSNick Piggin 	int retval;
149831e6b01fSNick Piggin 
149931e6b01fSNick Piggin 	/*
150031e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
150131e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
150231e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
150331e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
150431e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
150531e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
150631e6b01fSNick Piggin 	 * analogue, foo_rcu().
150731e6b01fSNick Piggin 	 *
150831e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
150931e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
151031e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
151131e6b01fSNick Piggin 	 * be able to complete).
151231e6b01fSNick Piggin 	 */
151331e6b01fSNick Piggin 	retval = path_init_rcu(dfd, name, flags, nd);
151431e6b01fSNick Piggin 	if (unlikely(retval))
151531e6b01fSNick Piggin 		return retval;
151631e6b01fSNick Piggin 	retval = path_walk_rcu(name, nd);
151731e6b01fSNick Piggin 	path_finish_rcu(nd);
15182a737871SAl Viro 	if (nd->root.mnt) {
15192a737871SAl Viro 		path_put(&nd->root);
15202a737871SAl Viro 		nd->root.mnt = NULL;
15212a737871SAl Viro 	}
152231e6b01fSNick Piggin 
152331e6b01fSNick Piggin 	if (unlikely(retval == -ECHILD || retval == -ESTALE)) {
152431e6b01fSNick Piggin 		/* slower, locked walk */
152531e6b01fSNick Piggin 		if (retval == -ESTALE)
152631e6b01fSNick Piggin 			flags |= LOOKUP_REVAL;
152731e6b01fSNick Piggin 		retval = path_init(dfd, name, flags, nd);
152831e6b01fSNick Piggin 		if (unlikely(retval))
152931e6b01fSNick Piggin 			return retval;
153031e6b01fSNick Piggin 		retval = path_walk(name, nd);
153131e6b01fSNick Piggin 		if (nd->root.mnt) {
153231e6b01fSNick Piggin 			path_put(&nd->root);
153331e6b01fSNick Piggin 			nd->root.mnt = NULL;
153431e6b01fSNick Piggin 		}
153531e6b01fSNick Piggin 	}
153631e6b01fSNick Piggin 
153731e6b01fSNick Piggin 	if (likely(!retval)) {
153831e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
153931e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
154031e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
154131e6b01fSNick Piggin 		}
154231e6b01fSNick Piggin 	}
154331e6b01fSNick Piggin 
1544170aa3d0SUlrich Drepper 	return retval;
15451da177e4SLinus Torvalds }
15461da177e4SLinus Torvalds 
1547fc9b52cdSHarvey Harrison int path_lookup(const char *name, unsigned int flags,
15485590ff0dSUlrich Drepper 			struct nameidata *nd)
15495590ff0dSUlrich Drepper {
15505590ff0dSUlrich Drepper 	return do_path_lookup(AT_FDCWD, name, flags, nd);
15515590ff0dSUlrich Drepper }
15525590ff0dSUlrich Drepper 
1553d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1554d1811465SAl Viro {
1555d1811465SAl Viro 	struct nameidata nd;
1556d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1557d1811465SAl Viro 	if (!res)
1558d1811465SAl Viro 		*path = nd.path;
1559d1811465SAl Viro 	return res;
1560d1811465SAl Viro }
1561d1811465SAl Viro 
156216f18200SJosef 'Jeff' Sipek /**
156316f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
156416f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
156516f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
156616f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
156716f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
156816f18200SJosef 'Jeff' Sipek  * @nd: pointer to nameidata
156916f18200SJosef 'Jeff' Sipek  */
157016f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
157116f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
157216f18200SJosef 'Jeff' Sipek 		    struct nameidata *nd)
157316f18200SJosef 'Jeff' Sipek {
157416f18200SJosef 'Jeff' Sipek 	int retval;
157516f18200SJosef 'Jeff' Sipek 
157616f18200SJosef 'Jeff' Sipek 	/* same as do_path_lookup */
157716f18200SJosef 'Jeff' Sipek 	nd->last_type = LAST_ROOT;
157816f18200SJosef 'Jeff' Sipek 	nd->flags = flags;
157916f18200SJosef 'Jeff' Sipek 	nd->depth = 0;
158016f18200SJosef 'Jeff' Sipek 
1581c8e7f449SJan Blunck 	nd->path.dentry = dentry;
1582c8e7f449SJan Blunck 	nd->path.mnt = mnt;
1583c8e7f449SJan Blunck 	path_get(&nd->path);
15845b857119SAl Viro 	nd->root = nd->path;
15855b857119SAl Viro 	path_get(&nd->root);
158631e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
158716f18200SJosef 'Jeff' Sipek 
158816f18200SJosef 'Jeff' Sipek 	retval = path_walk(name, nd);
15894ac91378SJan Blunck 	if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
159031e6b01fSNick Piggin 				nd->inode))
15914ac91378SJan Blunck 		audit_inode(name, nd->path.dentry);
159216f18200SJosef 'Jeff' Sipek 
15932a737871SAl Viro 	path_put(&nd->root);
15942a737871SAl Viro 	nd->root.mnt = NULL;
159516f18200SJosef 'Jeff' Sipek 
15962a737871SAl Viro 	return retval;
159716f18200SJosef 'Jeff' Sipek }
159816f18200SJosef 'Jeff' Sipek 
1599eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1600eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
16011da177e4SLinus Torvalds {
160281fca444SChristoph Hellwig 	struct inode *inode = base->d_inode;
16031da177e4SLinus Torvalds 	struct dentry *dentry;
16041da177e4SLinus Torvalds 	int err;
16051da177e4SLinus Torvalds 
1606b74c79e9SNick Piggin 	err = exec_permission(inode, 0);
160781fca444SChristoph Hellwig 	if (err)
160881fca444SChristoph Hellwig 		return ERR_PTR(err);
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds 	/*
16111da177e4SLinus Torvalds 	 * See if the low-level filesystem might want
16121da177e4SLinus Torvalds 	 * to use its own hash..
16131da177e4SLinus Torvalds 	 */
1614fb045adbSNick Piggin 	if (base->d_flags & DCACHE_OP_HASH) {
1615b1e6a015SNick Piggin 		err = base->d_op->d_hash(base, inode, name);
16161da177e4SLinus Torvalds 		dentry = ERR_PTR(err);
16171da177e4SLinus Torvalds 		if (err < 0)
16181da177e4SLinus Torvalds 			goto out;
16191da177e4SLinus Torvalds 	}
16201da177e4SLinus Torvalds 
1621b04f784eSNick Piggin 	/*
1622b04f784eSNick Piggin 	 * Don't bother with __d_lookup: callers are for creat as
1623b04f784eSNick Piggin 	 * well as unlink, so a lot of the time it would cost
1624b04f784eSNick Piggin 	 * a double lookup.
16256e6b1bd1SAl Viro 	 */
16266e6b1bd1SAl Viro 	dentry = d_lookup(base, name);
16276e6b1bd1SAl Viro 
1628fb045adbSNick Piggin 	if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
16296e6b1bd1SAl Viro 		dentry = do_revalidate(dentry, nd);
16306e6b1bd1SAl Viro 
16311da177e4SLinus Torvalds 	if (!dentry)
1632baa03890SNick Piggin 		dentry = d_alloc_and_lookup(base, name, nd);
16331da177e4SLinus Torvalds out:
16341da177e4SLinus Torvalds 	return dentry;
16351da177e4SLinus Torvalds }
16361da177e4SLinus Torvalds 
1637057f6c01SJames Morris /*
1638057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1639057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1640057f6c01SJames Morris  * SMP-safe.
1641057f6c01SJames Morris  */
1642a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
16431da177e4SLinus Torvalds {
16444ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
16451da177e4SLinus Torvalds }
16461da177e4SLinus Torvalds 
1647eead1911SChristoph Hellwig static int __lookup_one_len(const char *name, struct qstr *this,
1648eead1911SChristoph Hellwig 		struct dentry *base, int len)
16491da177e4SLinus Torvalds {
16501da177e4SLinus Torvalds 	unsigned long hash;
16511da177e4SLinus Torvalds 	unsigned int c;
16521da177e4SLinus Torvalds 
1653057f6c01SJames Morris 	this->name = name;
1654057f6c01SJames Morris 	this->len = len;
16551da177e4SLinus Torvalds 	if (!len)
1656057f6c01SJames Morris 		return -EACCES;
16571da177e4SLinus Torvalds 
16581da177e4SLinus Torvalds 	hash = init_name_hash();
16591da177e4SLinus Torvalds 	while (len--) {
16601da177e4SLinus Torvalds 		c = *(const unsigned char *)name++;
16611da177e4SLinus Torvalds 		if (c == '/' || c == '\0')
1662057f6c01SJames Morris 			return -EACCES;
16631da177e4SLinus Torvalds 		hash = partial_name_hash(c, hash);
16641da177e4SLinus Torvalds 	}
1665057f6c01SJames Morris 	this->hash = end_name_hash(hash);
1666057f6c01SJames Morris 	return 0;
1667057f6c01SJames Morris }
16681da177e4SLinus Torvalds 
1669eead1911SChristoph Hellwig /**
1670a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1671eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1672eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1673eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1674eead1911SChristoph Hellwig  *
1675a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1676a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1677eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1678eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1679eead1911SChristoph Hellwig  */
1680057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1681057f6c01SJames Morris {
1682057f6c01SJames Morris 	int err;
1683057f6c01SJames Morris 	struct qstr this;
1684057f6c01SJames Morris 
16852f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
16862f9092e1SDavid Woodhouse 
1687057f6c01SJames Morris 	err = __lookup_one_len(name, &this, base, len);
1688057f6c01SJames Morris 	if (err)
1689057f6c01SJames Morris 		return ERR_PTR(err);
1690eead1911SChristoph Hellwig 
169149705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1692057f6c01SJames Morris }
1693057f6c01SJames Morris 
16942d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags,
16952d8f3038SAl Viro 		 struct path *path)
16961da177e4SLinus Torvalds {
16972d8f3038SAl Viro 	struct nameidata nd;
16981da177e4SLinus Torvalds 	char *tmp = getname(name);
16991da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
17001da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
17012d8f3038SAl Viro 
17022d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
17032d8f3038SAl Viro 
17042d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
17051da177e4SLinus Torvalds 		putname(tmp);
17062d8f3038SAl Viro 		if (!err)
17072d8f3038SAl Viro 			*path = nd.path;
17081da177e4SLinus Torvalds 	}
17091da177e4SLinus Torvalds 	return err;
17101da177e4SLinus Torvalds }
17111da177e4SLinus Torvalds 
17122ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
17132ad94ae6SAl Viro 			struct nameidata *nd, char **name)
17142ad94ae6SAl Viro {
17152ad94ae6SAl Viro 	char *s = getname(path);
17162ad94ae6SAl Viro 	int error;
17172ad94ae6SAl Viro 
17182ad94ae6SAl Viro 	if (IS_ERR(s))
17192ad94ae6SAl Viro 		return PTR_ERR(s);
17202ad94ae6SAl Viro 
17212ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
17222ad94ae6SAl Viro 	if (error)
17232ad94ae6SAl Viro 		putname(s);
17242ad94ae6SAl Viro 	else
17252ad94ae6SAl Viro 		*name = s;
17262ad94ae6SAl Viro 
17272ad94ae6SAl Viro 	return error;
17282ad94ae6SAl Viro }
17292ad94ae6SAl Viro 
17301da177e4SLinus Torvalds /*
17311da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
17321da177e4SLinus Torvalds  * minimal.
17331da177e4SLinus Torvalds  */
17341da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
17351da177e4SLinus Torvalds {
1736da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1737da9592edSDavid Howells 
17381da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
17391da177e4SLinus Torvalds 		return 0;
1740da9592edSDavid Howells 	if (inode->i_uid == fsuid)
17411da177e4SLinus Torvalds 		return 0;
1742da9592edSDavid Howells 	if (dir->i_uid == fsuid)
17431da177e4SLinus Torvalds 		return 0;
17441da177e4SLinus Torvalds 	return !capable(CAP_FOWNER);
17451da177e4SLinus Torvalds }
17461da177e4SLinus Torvalds 
17471da177e4SLinus Torvalds /*
17481da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
17491da177e4SLinus Torvalds  *  whether the type of victim is right.
17501da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
17511da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
17521da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
17531da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
17541da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
17551da177e4SLinus Torvalds  *	a. be owner of dir, or
17561da177e4SLinus Torvalds  *	b. be owner of victim, or
17571da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
17581da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
17591da177e4SLinus Torvalds  *     links pointing to it.
17601da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
17611da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
17621da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
17631da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
17641da177e4SLinus Torvalds  *     nfs_async_unlink().
17651da177e4SLinus Torvalds  */
1766858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
17671da177e4SLinus Torvalds {
17681da177e4SLinus Torvalds 	int error;
17691da177e4SLinus Torvalds 
17701da177e4SLinus Torvalds 	if (!victim->d_inode)
17711da177e4SLinus Torvalds 		return -ENOENT;
17721da177e4SLinus Torvalds 
17731da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
1774cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
17751da177e4SLinus Torvalds 
1776f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
17771da177e4SLinus Torvalds 	if (error)
17781da177e4SLinus Torvalds 		return error;
17791da177e4SLinus Torvalds 	if (IS_APPEND(dir))
17801da177e4SLinus Torvalds 		return -EPERM;
17811da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1782f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
17831da177e4SLinus Torvalds 		return -EPERM;
17841da177e4SLinus Torvalds 	if (isdir) {
17851da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
17861da177e4SLinus Torvalds 			return -ENOTDIR;
17871da177e4SLinus Torvalds 		if (IS_ROOT(victim))
17881da177e4SLinus Torvalds 			return -EBUSY;
17891da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
17901da177e4SLinus Torvalds 		return -EISDIR;
17911da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
17921da177e4SLinus Torvalds 		return -ENOENT;
17931da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
17941da177e4SLinus Torvalds 		return -EBUSY;
17951da177e4SLinus Torvalds 	return 0;
17961da177e4SLinus Torvalds }
17971da177e4SLinus Torvalds 
17981da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
17991da177e4SLinus Torvalds  *  dir.
18001da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
18011da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
18021da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
18031da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
18041da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
18051da177e4SLinus Torvalds  */
1806a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
18071da177e4SLinus Torvalds {
18081da177e4SLinus Torvalds 	if (child->d_inode)
18091da177e4SLinus Torvalds 		return -EEXIST;
18101da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
18111da177e4SLinus Torvalds 		return -ENOENT;
1812f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
18131da177e4SLinus Torvalds }
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds /*
18161da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
18171da177e4SLinus Torvalds  */
18181da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
18191da177e4SLinus Torvalds {
18201da177e4SLinus Torvalds 	struct dentry *p;
18211da177e4SLinus Torvalds 
18221da177e4SLinus Torvalds 	if (p1 == p2) {
1823f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
18241da177e4SLinus Torvalds 		return NULL;
18251da177e4SLinus Torvalds 	}
18261da177e4SLinus Torvalds 
1827a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
18281da177e4SLinus Torvalds 
1829e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1830e2761a11SOGAWA Hirofumi 	if (p) {
1831f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1832f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
18331da177e4SLinus Torvalds 		return p;
18341da177e4SLinus Torvalds 	}
18351da177e4SLinus Torvalds 
1836e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1837e2761a11SOGAWA Hirofumi 	if (p) {
1838f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1839f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
18401da177e4SLinus Torvalds 		return p;
18411da177e4SLinus Torvalds 	}
18421da177e4SLinus Torvalds 
1843f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1844f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
18451da177e4SLinus Torvalds 	return NULL;
18461da177e4SLinus Torvalds }
18471da177e4SLinus Torvalds 
18481da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
18491da177e4SLinus Torvalds {
18501b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
18511da177e4SLinus Torvalds 	if (p1 != p2) {
18521b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1853a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
18541da177e4SLinus Torvalds 	}
18551da177e4SLinus Torvalds }
18561da177e4SLinus Torvalds 
18571da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
18581da177e4SLinus Torvalds 		struct nameidata *nd)
18591da177e4SLinus Torvalds {
1860a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds 	if (error)
18631da177e4SLinus Torvalds 		return error;
18641da177e4SLinus Torvalds 
1865acfa4380SAl Viro 	if (!dir->i_op->create)
18661da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
18671da177e4SLinus Torvalds 	mode &= S_IALLUGO;
18681da177e4SLinus Torvalds 	mode |= S_IFREG;
18691da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
18701da177e4SLinus Torvalds 	if (error)
18711da177e4SLinus Torvalds 		return error;
18721da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
1873a74574aaSStephen Smalley 	if (!error)
1874f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
18751da177e4SLinus Torvalds 	return error;
18761da177e4SLinus Torvalds }
18771da177e4SLinus Torvalds 
18783fb64190SChristoph Hellwig int may_open(struct path *path, int acc_mode, int flag)
18791da177e4SLinus Torvalds {
18803fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
18811da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
18821da177e4SLinus Torvalds 	int error;
18831da177e4SLinus Torvalds 
18841da177e4SLinus Torvalds 	if (!inode)
18851da177e4SLinus Torvalds 		return -ENOENT;
18861da177e4SLinus Torvalds 
1887c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
1888c8fe8f30SChristoph Hellwig 	case S_IFLNK:
18891da177e4SLinus Torvalds 		return -ELOOP;
1890c8fe8f30SChristoph Hellwig 	case S_IFDIR:
1891c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
18921da177e4SLinus Torvalds 			return -EISDIR;
1893c8fe8f30SChristoph Hellwig 		break;
1894c8fe8f30SChristoph Hellwig 	case S_IFBLK:
1895c8fe8f30SChristoph Hellwig 	case S_IFCHR:
18963fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
18971da177e4SLinus Torvalds 			return -EACCES;
1898c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
1899c8fe8f30SChristoph Hellwig 	case S_IFIFO:
1900c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
19011da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
1902c8fe8f30SChristoph Hellwig 		break;
19034a3fd211SDave Hansen 	}
1904b41572e9SDave Hansen 
19053fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
1906b41572e9SDave Hansen 	if (error)
1907b41572e9SDave Hansen 		return error;
19086146f0d5SMimi Zohar 
19091da177e4SLinus Torvalds 	/*
19101da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
19111da177e4SLinus Torvalds 	 */
19121da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
19138737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
19147715b521SAl Viro 			return -EPERM;
19151da177e4SLinus Torvalds 		if (flag & O_TRUNC)
19167715b521SAl Viro 			return -EPERM;
19171da177e4SLinus Torvalds 	}
19181da177e4SLinus Torvalds 
19191da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
19207715b521SAl Viro 	if (flag & O_NOATIME && !is_owner_or_cap(inode))
19217715b521SAl Viro 		return -EPERM;
19221da177e4SLinus Torvalds 
19231da177e4SLinus Torvalds 	/*
19241da177e4SLinus Torvalds 	 * Ensure there are no outstanding leases on the file.
19251da177e4SLinus Torvalds 	 */
1926b65a9cfcSAl Viro 	return break_lease(inode, flag);
19277715b521SAl Viro }
19287715b521SAl Viro 
19297715b521SAl Viro static int handle_truncate(struct path *path)
19307715b521SAl Viro {
19317715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
19327715b521SAl Viro 	int error = get_write_access(inode);
19331da177e4SLinus Torvalds 	if (error)
19347715b521SAl Viro 		return error;
19351da177e4SLinus Torvalds 	/*
19361da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
19371da177e4SLinus Torvalds 	 */
19381da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
1939be6d3e56SKentaro Takeda 	if (!error)
1940ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
19411da177e4SLinus Torvalds 	if (!error) {
19427715b521SAl Viro 		error = do_truncate(path->dentry, 0,
1943d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
1944d139d7ffSMiklos Szeredi 				    NULL);
19451da177e4SLinus Torvalds 	}
19461da177e4SLinus Torvalds 	put_write_access(inode);
1947acd0c935SMimi Zohar 	return error;
19481da177e4SLinus Torvalds }
19491da177e4SLinus Torvalds 
1950d57999e1SDave Hansen /*
1951d57999e1SDave Hansen  * Be careful about ever adding any more callers of this
1952d57999e1SDave Hansen  * function.  Its flags must be in the namei format, not
1953d57999e1SDave Hansen  * what get passed to sys_open().
1954d57999e1SDave Hansen  */
1955d57999e1SDave Hansen static int __open_namei_create(struct nameidata *nd, struct path *path,
19568737c930SAl Viro 				int open_flag, int mode)
1957aab520e2SDave Hansen {
1958aab520e2SDave Hansen 	int error;
19594ac91378SJan Blunck 	struct dentry *dir = nd->path.dentry;
1960aab520e2SDave Hansen 
1961aab520e2SDave Hansen 	if (!IS_POSIXACL(dir->d_inode))
1962ce3b0f8dSAl Viro 		mode &= ~current_umask();
1963be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd->path, path->dentry, mode, 0);
1964be6d3e56SKentaro Takeda 	if (error)
1965be6d3e56SKentaro Takeda 		goto out_unlock;
1966aab520e2SDave Hansen 	error = vfs_create(dir->d_inode, path->dentry, mode, nd);
1967be6d3e56SKentaro Takeda out_unlock:
1968aab520e2SDave Hansen 	mutex_unlock(&dir->d_inode->i_mutex);
19694ac91378SJan Blunck 	dput(nd->path.dentry);
19704ac91378SJan Blunck 	nd->path.dentry = path->dentry;
197131e6b01fSNick Piggin 
1972aab520e2SDave Hansen 	if (error)
1973aab520e2SDave Hansen 		return error;
1974aab520e2SDave Hansen 	/* Don't check for write permission, don't truncate */
19758737c930SAl Viro 	return may_open(&nd->path, 0, open_flag & ~O_TRUNC);
1976aab520e2SDave Hansen }
1977aab520e2SDave Hansen 
19781da177e4SLinus Torvalds /*
1979d57999e1SDave Hansen  * Note that while the flag value (low two bits) for sys_open means:
1980d57999e1SDave Hansen  *	00 - read-only
1981d57999e1SDave Hansen  *	01 - write-only
1982d57999e1SDave Hansen  *	10 - read-write
1983d57999e1SDave Hansen  *	11 - special
1984d57999e1SDave Hansen  * it is changed into
1985d57999e1SDave Hansen  *	00 - no permissions needed
1986d57999e1SDave Hansen  *	01 - read-permission
1987d57999e1SDave Hansen  *	10 - write-permission
1988d57999e1SDave Hansen  *	11 - read-write
1989d57999e1SDave Hansen  * for the internal routines (ie open_namei()/follow_link() etc)
1990d57999e1SDave Hansen  * This is more logical, and also allows the 00 "no perm needed"
1991d57999e1SDave Hansen  * to be used for symlinks (where the permissions are checked
1992d57999e1SDave Hansen  * later).
1993d57999e1SDave Hansen  *
1994d57999e1SDave Hansen */
1995d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
1996d57999e1SDave Hansen {
1997d57999e1SDave Hansen 	if ((flag+1) & O_ACCMODE)
1998d57999e1SDave Hansen 		flag++;
1999d57999e1SDave Hansen 	return flag;
2000d57999e1SDave Hansen }
2001d57999e1SDave Hansen 
20027715b521SAl Viro static int open_will_truncate(int flag, struct inode *inode)
20034a3fd211SDave Hansen {
2004d57999e1SDave Hansen 	/*
20054a3fd211SDave Hansen 	 * We'll never write to the fs underlying
20064a3fd211SDave Hansen 	 * a device file.
20074a3fd211SDave Hansen 	 */
20084a3fd211SDave Hansen 	if (special_file(inode->i_mode))
20094a3fd211SDave Hansen 		return 0;
20104a3fd211SDave Hansen 	return (flag & O_TRUNC);
20114a3fd211SDave Hansen }
20124a3fd211SDave Hansen 
2013648fa861SAl Viro static struct file *finish_open(struct nameidata *nd,
20149a66179eSAl Viro 				int open_flag, int acc_mode)
2015648fa861SAl Viro {
2016648fa861SAl Viro 	struct file *filp;
2017648fa861SAl Viro 	int will_truncate;
2018648fa861SAl Viro 	int error;
2019648fa861SAl Viro 
20209a66179eSAl Viro 	will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode);
2021648fa861SAl Viro 	if (will_truncate) {
2022648fa861SAl Viro 		error = mnt_want_write(nd->path.mnt);
2023648fa861SAl Viro 		if (error)
2024648fa861SAl Viro 			goto exit;
2025648fa861SAl Viro 	}
2026648fa861SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2027648fa861SAl Viro 	if (error) {
2028648fa861SAl Viro 		if (will_truncate)
2029648fa861SAl Viro 			mnt_drop_write(nd->path.mnt);
2030648fa861SAl Viro 		goto exit;
2031648fa861SAl Viro 	}
2032648fa861SAl Viro 	filp = nameidata_to_filp(nd);
2033648fa861SAl Viro 	if (!IS_ERR(filp)) {
2034648fa861SAl Viro 		error = ima_file_check(filp, acc_mode);
2035648fa861SAl Viro 		if (error) {
2036648fa861SAl Viro 			fput(filp);
2037648fa861SAl Viro 			filp = ERR_PTR(error);
2038648fa861SAl Viro 		}
2039648fa861SAl Viro 	}
2040648fa861SAl Viro 	if (!IS_ERR(filp)) {
2041648fa861SAl Viro 		if (will_truncate) {
2042648fa861SAl Viro 			error = handle_truncate(&nd->path);
2043648fa861SAl Viro 			if (error) {
2044648fa861SAl Viro 				fput(filp);
2045648fa861SAl Viro 				filp = ERR_PTR(error);
2046648fa861SAl Viro 			}
2047648fa861SAl Viro 		}
2048648fa861SAl Viro 	}
2049648fa861SAl Viro 	/*
2050648fa861SAl Viro 	 * It is now safe to drop the mnt write
2051648fa861SAl Viro 	 * because the filp has had a write taken
2052648fa861SAl Viro 	 * on its behalf.
2053648fa861SAl Viro 	 */
2054648fa861SAl Viro 	if (will_truncate)
2055648fa861SAl Viro 		mnt_drop_write(nd->path.mnt);
2056d893f1bcSAl Viro 	path_put(&nd->path);
2057648fa861SAl Viro 	return filp;
2058648fa861SAl Viro 
2059648fa861SAl Viro exit:
2060648fa861SAl Viro 	if (!IS_ERR(nd->intent.open.file))
2061648fa861SAl Viro 		release_open_intent(nd);
2062648fa861SAl Viro 	path_put(&nd->path);
2063648fa861SAl Viro 	return ERR_PTR(error);
2064648fa861SAl Viro }
2065648fa861SAl Viro 
206631e6b01fSNick Piggin /*
206731e6b01fSNick Piggin  * Handle O_CREAT case for do_filp_open
206831e6b01fSNick Piggin  */
2069fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
20705b369df8SAl Viro 			    int open_flag, int acc_mode,
20713e297b61SAl Viro 			    int mode, const char *pathname)
2072fb1cc555SAl Viro {
2073a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
2074fb1cc555SAl Viro 	struct file *filp;
20751f36f774SAl Viro 	int error = -EISDIR;
2076fb1cc555SAl Viro 
20771f36f774SAl Viro 	switch (nd->last_type) {
20781f36f774SAl Viro 	case LAST_DOTDOT:
20791f36f774SAl Viro 		follow_dotdot(nd);
20801f36f774SAl Viro 		dir = nd->path.dentry;
2081176306f5SNeil Brown 	case LAST_DOT:
2082fb045adbSNick Piggin 		if (need_reval_dot(dir)) {
208334286d66SNick Piggin 			error = d_revalidate(nd->path.dentry, nd);
208434286d66SNick Piggin 			if (!error)
20851f36f774SAl Viro 				error = -ESTALE;
208634286d66SNick Piggin 			if (error < 0)
2087a2c36b45SAl Viro 				goto exit;
20881f36f774SAl Viro 		}
20891f36f774SAl Viro 		/* fallthrough */
20901f36f774SAl Viro 	case LAST_ROOT:
20911f36f774SAl Viro 		goto exit;
20921f36f774SAl Viro 	case LAST_BIND:
20931f36f774SAl Viro 		audit_inode(pathname, dir);
20941f36f774SAl Viro 		goto ok;
20951f36f774SAl Viro 	}
2096a2c36b45SAl Viro 
20971f36f774SAl Viro 	/* trailing slashes? */
209831e6b01fSNick Piggin 	if (nd->last.name[nd->last.len])
20991f36f774SAl Viro 		goto exit;
21001f36f774SAl Viro 
2101a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
2102a1e28038SAl Viro 
2103a1e28038SAl Viro 	path->dentry = lookup_hash(nd);
2104a1e28038SAl Viro 	path->mnt = nd->path.mnt;
2105a1e28038SAl Viro 
2106fb1cc555SAl Viro 	error = PTR_ERR(path->dentry);
2107fb1cc555SAl Viro 	if (IS_ERR(path->dentry)) {
2108fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
2109fb1cc555SAl Viro 		goto exit;
2110fb1cc555SAl Viro 	}
2111fb1cc555SAl Viro 
2112fb1cc555SAl Viro 	if (IS_ERR(nd->intent.open.file)) {
2113fb1cc555SAl Viro 		error = PTR_ERR(nd->intent.open.file);
2114fb1cc555SAl Viro 		goto exit_mutex_unlock;
2115fb1cc555SAl Viro 	}
2116fb1cc555SAl Viro 
2117fb1cc555SAl Viro 	/* Negative dentry, just create the file */
2118fb1cc555SAl Viro 	if (!path->dentry->d_inode) {
2119fb1cc555SAl Viro 		/*
2120fb1cc555SAl Viro 		 * This write is needed to ensure that a
2121fb1cc555SAl Viro 		 * ro->rw transition does not occur between
2122fb1cc555SAl Viro 		 * the time when the file is created and when
2123fb1cc555SAl Viro 		 * a permanent write count is taken through
2124fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
2125fb1cc555SAl Viro 		 */
2126fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
2127fb1cc555SAl Viro 		if (error)
2128fb1cc555SAl Viro 			goto exit_mutex_unlock;
2129fb1cc555SAl Viro 		error = __open_namei_create(nd, path, open_flag, mode);
2130fb1cc555SAl Viro 		if (error) {
2131fb1cc555SAl Viro 			mnt_drop_write(nd->path.mnt);
2132fb1cc555SAl Viro 			goto exit;
2133fb1cc555SAl Viro 		}
2134fb1cc555SAl Viro 		filp = nameidata_to_filp(nd);
2135fb1cc555SAl Viro 		mnt_drop_write(nd->path.mnt);
2136d893f1bcSAl Viro 		path_put(&nd->path);
2137fb1cc555SAl Viro 		if (!IS_ERR(filp)) {
2138fb1cc555SAl Viro 			error = ima_file_check(filp, acc_mode);
2139fb1cc555SAl Viro 			if (error) {
2140fb1cc555SAl Viro 				fput(filp);
2141fb1cc555SAl Viro 				filp = ERR_PTR(error);
2142fb1cc555SAl Viro 			}
2143fb1cc555SAl Viro 		}
2144fb1cc555SAl Viro 		return filp;
2145fb1cc555SAl Viro 	}
2146fb1cc555SAl Viro 
2147fb1cc555SAl Viro 	/*
2148fb1cc555SAl Viro 	 * It already exists.
2149fb1cc555SAl Viro 	 */
2150fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2151fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2152fb1cc555SAl Viro 
2153fb1cc555SAl Viro 	error = -EEXIST;
21545b369df8SAl Viro 	if (open_flag & O_EXCL)
2155fb1cc555SAl Viro 		goto exit_dput;
2156fb1cc555SAl Viro 
2157fb1cc555SAl Viro 	if (__follow_mount(path)) {
2158fb1cc555SAl Viro 		error = -ELOOP;
21595b369df8SAl Viro 		if (open_flag & O_NOFOLLOW)
2160fb1cc555SAl Viro 			goto exit_dput;
2161fb1cc555SAl Viro 	}
2162fb1cc555SAl Viro 
2163fb1cc555SAl Viro 	error = -ENOENT;
2164fb1cc555SAl Viro 	if (!path->dentry->d_inode)
2165fb1cc555SAl Viro 		goto exit_dput;
21669e67f361SAl Viro 
21679e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
2168fb1cc555SAl Viro 		return NULL;
2169fb1cc555SAl Viro 
2170fb1cc555SAl Viro 	path_to_nameidata(path, nd);
217131e6b01fSNick Piggin 	nd->inode = path->dentry->d_inode;
2172fb1cc555SAl Viro 	error = -EISDIR;
217331e6b01fSNick Piggin 	if (S_ISDIR(nd->inode->i_mode))
2174fb1cc555SAl Viro 		goto exit;
217567ee3ad2SAl Viro ok:
21769a66179eSAl Viro 	filp = finish_open(nd, open_flag, acc_mode);
2177fb1cc555SAl Viro 	return filp;
2178fb1cc555SAl Viro 
2179fb1cc555SAl Viro exit_mutex_unlock:
2180fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2181fb1cc555SAl Viro exit_dput:
2182fb1cc555SAl Viro 	path_put_conditional(path, nd);
2183fb1cc555SAl Viro exit:
2184fb1cc555SAl Viro 	if (!IS_ERR(nd->intent.open.file))
2185fb1cc555SAl Viro 		release_open_intent(nd);
2186fb1cc555SAl Viro 	path_put(&nd->path);
2187fb1cc555SAl Viro 	return ERR_PTR(error);
2188fb1cc555SAl Viro }
2189fb1cc555SAl Viro 
21904a3fd211SDave Hansen /*
21914a3fd211SDave Hansen  * Note that the low bits of the passed in "open_flag"
21924a3fd211SDave Hansen  * are not the same as in the local variable "flag". See
21934a3fd211SDave Hansen  * open_to_namei_flags() for more details.
21941da177e4SLinus Torvalds  */
2195a70e65dfSChristoph Hellwig struct file *do_filp_open(int dfd, const char *pathname,
21966e8341a1SAl Viro 		int open_flag, int mode, int acc_mode)
21971da177e4SLinus Torvalds {
21984a3fd211SDave Hansen 	struct file *filp;
2199a70e65dfSChristoph Hellwig 	struct nameidata nd;
22006e8341a1SAl Viro 	int error;
22019850c056SAl Viro 	struct path path;
22021da177e4SLinus Torvalds 	int count = 0;
2203d57999e1SDave Hansen 	int flag = open_to_namei_flags(open_flag);
220431e6b01fSNick Piggin 	int flags;
22051f36f774SAl Viro 
22061f36f774SAl Viro 	if (!(open_flag & O_CREAT))
22071f36f774SAl Viro 		mode = 0;
22081da177e4SLinus Torvalds 
2209b1085ba8SLino Sanfilippo 	/* Must never be set by userspace */
2210b1085ba8SLino Sanfilippo 	open_flag &= ~FMODE_NONOTIFY;
2211b1085ba8SLino Sanfilippo 
22126b2f3d1fSChristoph Hellwig 	/*
22136b2f3d1fSChristoph Hellwig 	 * O_SYNC is implemented as __O_SYNC|O_DSYNC.  As many places only
22146b2f3d1fSChristoph Hellwig 	 * check for O_DSYNC if the need any syncing at all we enforce it's
22156b2f3d1fSChristoph Hellwig 	 * always set instead of having to deal with possibly weird behaviour
22166b2f3d1fSChristoph Hellwig 	 * for malicious applications setting only __O_SYNC.
22176b2f3d1fSChristoph Hellwig 	 */
22186b2f3d1fSChristoph Hellwig 	if (open_flag & __O_SYNC)
22196b2f3d1fSChristoph Hellwig 		open_flag |= O_DSYNC;
22206b2f3d1fSChristoph Hellwig 
22216e8341a1SAl Viro 	if (!acc_mode)
22226d125529SAl Viro 		acc_mode = MAY_OPEN | ACC_MODE(open_flag);
22231da177e4SLinus Torvalds 
2224834f2a4aSTrond Myklebust 	/* O_TRUNC implies we need access checks for write permissions */
22254296e2cbSAl Viro 	if (open_flag & O_TRUNC)
2226834f2a4aSTrond Myklebust 		acc_mode |= MAY_WRITE;
2227834f2a4aSTrond Myklebust 
22281da177e4SLinus Torvalds 	/* Allow the LSM permission hook to distinguish append
22291da177e4SLinus Torvalds 	   access from general write access. */
22304296e2cbSAl Viro 	if (open_flag & O_APPEND)
22311da177e4SLinus Torvalds 		acc_mode |= MAY_APPEND;
22321da177e4SLinus Torvalds 
223331e6b01fSNick Piggin 	flags = LOOKUP_OPEN;
223431e6b01fSNick Piggin 	if (open_flag & O_CREAT) {
223531e6b01fSNick Piggin 		flags |= LOOKUP_CREATE;
223631e6b01fSNick Piggin 		if (open_flag & O_EXCL)
223731e6b01fSNick Piggin 			flags |= LOOKUP_EXCL;
2238654f562cSJ. R. Okajima 	}
223931e6b01fSNick Piggin 	if (open_flag & O_DIRECTORY)
224031e6b01fSNick Piggin 		flags |= LOOKUP_DIRECTORY;
224131e6b01fSNick Piggin 	if (!(open_flag & O_NOFOLLOW))
224231e6b01fSNick Piggin 		flags |= LOOKUP_FOLLOW;
224331e6b01fSNick Piggin 
224431e6b01fSNick Piggin 	filp = get_empty_filp();
224531e6b01fSNick Piggin 	if (!filp)
224631e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
224731e6b01fSNick Piggin 
224831e6b01fSNick Piggin 	filp->f_flags = open_flag;
224931e6b01fSNick Piggin 	nd.intent.open.file = filp;
225031e6b01fSNick Piggin 	nd.intent.open.flags = flag;
225131e6b01fSNick Piggin 	nd.intent.open.create_mode = mode;
225231e6b01fSNick Piggin 
225331e6b01fSNick Piggin 	if (open_flag & O_CREAT)
225431e6b01fSNick Piggin 		goto creat;
225531e6b01fSNick Piggin 
225631e6b01fSNick Piggin 	/* !O_CREAT, simple open */
225731e6b01fSNick Piggin 	error = do_path_lookup(dfd, pathname, flags, &nd);
225831e6b01fSNick Piggin 	if (unlikely(error))
225931e6b01fSNick Piggin 		goto out_filp;
226031e6b01fSNick Piggin 	error = -ELOOP;
226131e6b01fSNick Piggin 	if (!(nd.flags & LOOKUP_FOLLOW)) {
226231e6b01fSNick Piggin 		if (nd.inode->i_op->follow_link)
226331e6b01fSNick Piggin 			goto out_path;
226431e6b01fSNick Piggin 	}
226531e6b01fSNick Piggin 	error = -ENOTDIR;
226631e6b01fSNick Piggin 	if (nd.flags & LOOKUP_DIRECTORY) {
226731e6b01fSNick Piggin 		if (!nd.inode->i_op->lookup)
226831e6b01fSNick Piggin 			goto out_path;
226931e6b01fSNick Piggin 	}
227031e6b01fSNick Piggin 	audit_inode(pathname, nd.path.dentry);
227131e6b01fSNick Piggin 	filp = finish_open(&nd, open_flag, acc_mode);
227231e6b01fSNick Piggin 	return filp;
227331e6b01fSNick Piggin 
227431e6b01fSNick Piggin creat:
227531e6b01fSNick Piggin 	/* OK, have to create the file. Find the parent. */
227631e6b01fSNick Piggin 	error = path_init_rcu(dfd, pathname,
227731e6b01fSNick Piggin 			LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd);
227831e6b01fSNick Piggin 	if (error)
227931e6b01fSNick Piggin 		goto out_filp;
228031e6b01fSNick Piggin 	error = path_walk_rcu(pathname, &nd);
228131e6b01fSNick Piggin 	path_finish_rcu(&nd);
228231e6b01fSNick Piggin 	if (unlikely(error == -ECHILD || error == -ESTALE)) {
228331e6b01fSNick Piggin 		/* slower, locked walk */
228431e6b01fSNick Piggin 		if (error == -ESTALE) {
228531e6b01fSNick Piggin reval:
228631e6b01fSNick Piggin 			flags |= LOOKUP_REVAL;
228731e6b01fSNick Piggin 		}
228831e6b01fSNick Piggin 		error = path_init(dfd, pathname,
228931e6b01fSNick Piggin 				LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd);
229031e6b01fSNick Piggin 		if (error)
229131e6b01fSNick Piggin 			goto out_filp;
229231e6b01fSNick Piggin 
229331e6b01fSNick Piggin 		error = path_walk_simple(pathname, &nd);
229431e6b01fSNick Piggin 	}
229531e6b01fSNick Piggin 	if (unlikely(error))
229631e6b01fSNick Piggin 		goto out_filp;
229731e6b01fSNick Piggin 	if (unlikely(!audit_dummy_context()))
22989b4a9b14SAl Viro 		audit_inode(pathname, nd.path.dentry);
22991da177e4SLinus Torvalds 
23001da177e4SLinus Torvalds 	/*
2301a2c36b45SAl Viro 	 * We have the parent and last component.
23021da177e4SLinus Torvalds 	 */
230331e6b01fSNick Piggin 	nd.flags = flags;
23043e297b61SAl Viro 	filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
2305806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
2306def4af30SAl Viro 		struct path holder;
2307def4af30SAl Viro 		void *cookie;
2308806b681cSAl Viro 		error = -ELOOP;
23091f36f774SAl Viro 		/* S_ISDIR part is a temporary automount kludge */
231031e6b01fSNick Piggin 		if (!(nd.flags & LOOKUP_FOLLOW) && !S_ISDIR(nd.inode->i_mode))
23111f36f774SAl Viro 			goto exit_dput;
23121f36f774SAl Viro 		if (count++ == 32)
2313806b681cSAl Viro 			goto exit_dput;
2314806b681cSAl Viro 		/*
2315806b681cSAl Viro 		 * This is subtle. Instead of calling do_follow_link() we do
2316806b681cSAl Viro 		 * the thing by hands. The reason is that this way we have zero
2317806b681cSAl Viro 		 * link_count and path_walk() (called from ->follow_link)
2318806b681cSAl Viro 		 * honoring LOOKUP_PARENT.  After that we have the parent and
2319806b681cSAl Viro 		 * last component, i.e. we are in the same situation as after
2320806b681cSAl Viro 		 * the first path_walk().  Well, almost - if the last component
2321806b681cSAl Viro 		 * is normal we get its copy stored in nd->last.name and we will
2322806b681cSAl Viro 		 * have to putname() it when we are done. Procfs-like symlinks
2323806b681cSAl Viro 		 * just set LAST_BIND.
2324806b681cSAl Viro 		 */
2325806b681cSAl Viro 		nd.flags |= LOOKUP_PARENT;
2326806b681cSAl Viro 		error = security_inode_follow_link(path.dentry, &nd);
2327806b681cSAl Viro 		if (error)
2328806b681cSAl Viro 			goto exit_dput;
2329def4af30SAl Viro 		error = __do_follow_link(&path, &nd, &cookie);
2330def4af30SAl Viro 		if (unlikely(error)) {
233131e6b01fSNick Piggin 			if (!IS_ERR(cookie) && nd.inode->i_op->put_link)
233231e6b01fSNick Piggin 				nd.inode->i_op->put_link(path.dentry, &nd, cookie);
2333806b681cSAl Viro 			/* nd.path had been dropped */
233431e6b01fSNick Piggin 			nd.path = path;
233531e6b01fSNick Piggin 			goto out_path;
2336806b681cSAl Viro 		}
2337def4af30SAl Viro 		holder = path;
2338806b681cSAl Viro 		nd.flags &= ~LOOKUP_PARENT;
23393e297b61SAl Viro 		filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
234031e6b01fSNick Piggin 		if (nd.inode->i_op->put_link)
234131e6b01fSNick Piggin 			nd.inode->i_op->put_link(holder.dentry, &nd, cookie);
2342def4af30SAl Viro 		path_put(&holder);
2343806b681cSAl Viro 	}
234410fa8e62SAl Viro out:
23452a737871SAl Viro 	if (nd.root.mnt)
23462a737871SAl Viro 		path_put(&nd.root);
234731e6b01fSNick Piggin 	if (filp == ERR_PTR(-ESTALE) && !(flags & LOOKUP_REVAL))
234810fa8e62SAl Viro 		goto reval;
234910fa8e62SAl Viro 	return filp;
23501da177e4SLinus Torvalds 
2351806b681cSAl Viro exit_dput:
2352806b681cSAl Viro 	path_put_conditional(&path, &nd);
235331e6b01fSNick Piggin out_path:
235431e6b01fSNick Piggin 	path_put(&nd.path);
235531e6b01fSNick Piggin out_filp:
2356806b681cSAl Viro 	if (!IS_ERR(nd.intent.open.file))
2357a70e65dfSChristoph Hellwig 		release_open_intent(&nd);
235810fa8e62SAl Viro 	filp = ERR_PTR(error);
235910fa8e62SAl Viro 	goto out;
2360de459215SKirill Korotaev }
23611da177e4SLinus Torvalds 
23621da177e4SLinus Torvalds /**
2363a70e65dfSChristoph Hellwig  * filp_open - open file and return file pointer
2364a70e65dfSChristoph Hellwig  *
2365a70e65dfSChristoph Hellwig  * @filename:	path to open
2366a70e65dfSChristoph Hellwig  * @flags:	open flags as per the open(2) second argument
2367a70e65dfSChristoph Hellwig  * @mode:	mode for the new file if O_CREAT is set, else ignored
2368a70e65dfSChristoph Hellwig  *
2369a70e65dfSChristoph Hellwig  * This is the helper to open a file from kernelspace if you really
2370a70e65dfSChristoph Hellwig  * have to.  But in generally you should not do this, so please move
2371a70e65dfSChristoph Hellwig  * along, nothing to see here..
2372a70e65dfSChristoph Hellwig  */
2373a70e65dfSChristoph Hellwig struct file *filp_open(const char *filename, int flags, int mode)
2374a70e65dfSChristoph Hellwig {
23756e8341a1SAl Viro 	return do_filp_open(AT_FDCWD, filename, flags, mode, 0);
2376a70e65dfSChristoph Hellwig }
2377a70e65dfSChristoph Hellwig EXPORT_SYMBOL(filp_open);
2378a70e65dfSChristoph Hellwig 
2379a70e65dfSChristoph Hellwig /**
23801da177e4SLinus Torvalds  * lookup_create - lookup a dentry, creating it if it doesn't exist
23811da177e4SLinus Torvalds  * @nd: nameidata info
23821da177e4SLinus Torvalds  * @is_dir: directory flag
23831da177e4SLinus Torvalds  *
23841da177e4SLinus Torvalds  * Simple function to lookup and return a dentry and create it
23851da177e4SLinus Torvalds  * if it doesn't exist.  Is SMP-safe.
2386c663e5d8SChristoph Hellwig  *
23874ac91378SJan Blunck  * Returns with nd->path.dentry->d_inode->i_mutex locked.
23881da177e4SLinus Torvalds  */
23891da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir)
23901da177e4SLinus Torvalds {
2391c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
23921da177e4SLinus Torvalds 
23934ac91378SJan Blunck 	mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2394c663e5d8SChristoph Hellwig 	/*
2395c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2396c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2397c663e5d8SChristoph Hellwig 	 */
23981da177e4SLinus Torvalds 	if (nd->last_type != LAST_NORM)
23991da177e4SLinus Torvalds 		goto fail;
24001da177e4SLinus Torvalds 	nd->flags &= ~LOOKUP_PARENT;
24013516586aSAl Viro 	nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2402a634904aSASANO Masahiro 	nd->intent.open.flags = O_EXCL;
2403c663e5d8SChristoph Hellwig 
2404c663e5d8SChristoph Hellwig 	/*
2405c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2406c663e5d8SChristoph Hellwig 	 */
240749705b77SChristoph Hellwig 	dentry = lookup_hash(nd);
24081da177e4SLinus Torvalds 	if (IS_ERR(dentry))
24091da177e4SLinus Torvalds 		goto fail;
2410c663e5d8SChristoph Hellwig 
2411e9baf6e5SAl Viro 	if (dentry->d_inode)
2412e9baf6e5SAl Viro 		goto eexist;
2413c663e5d8SChristoph Hellwig 	/*
2414c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2415c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2416c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2417c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2418c663e5d8SChristoph Hellwig 	 */
2419e9baf6e5SAl Viro 	if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
24201da177e4SLinus Torvalds 		dput(dentry);
24211da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2422e9baf6e5SAl Viro 	}
2423e9baf6e5SAl Viro 	return dentry;
2424e9baf6e5SAl Viro eexist:
2425e9baf6e5SAl Viro 	dput(dentry);
2426e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
24271da177e4SLinus Torvalds fail:
24281da177e4SLinus Torvalds 	return dentry;
24291da177e4SLinus Torvalds }
2430f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create);
24311da177e4SLinus Torvalds 
24321da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
24331da177e4SLinus Torvalds {
2434a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
24351da177e4SLinus Torvalds 
24361da177e4SLinus Torvalds 	if (error)
24371da177e4SLinus Torvalds 		return error;
24381da177e4SLinus Torvalds 
24391da177e4SLinus Torvalds 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
24401da177e4SLinus Torvalds 		return -EPERM;
24411da177e4SLinus Torvalds 
2442acfa4380SAl Viro 	if (!dir->i_op->mknod)
24431da177e4SLinus Torvalds 		return -EPERM;
24441da177e4SLinus Torvalds 
244508ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
244608ce5f16SSerge E. Hallyn 	if (error)
244708ce5f16SSerge E. Hallyn 		return error;
244808ce5f16SSerge E. Hallyn 
24491da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
24501da177e4SLinus Torvalds 	if (error)
24511da177e4SLinus Torvalds 		return error;
24521da177e4SLinus Torvalds 
24531da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2454a74574aaSStephen Smalley 	if (!error)
2455f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
24561da177e4SLinus Torvalds 	return error;
24571da177e4SLinus Torvalds }
24581da177e4SLinus Torvalds 
2459463c3197SDave Hansen static int may_mknod(mode_t mode)
2460463c3197SDave Hansen {
2461463c3197SDave Hansen 	switch (mode & S_IFMT) {
2462463c3197SDave Hansen 	case S_IFREG:
2463463c3197SDave Hansen 	case S_IFCHR:
2464463c3197SDave Hansen 	case S_IFBLK:
2465463c3197SDave Hansen 	case S_IFIFO:
2466463c3197SDave Hansen 	case S_IFSOCK:
2467463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2468463c3197SDave Hansen 		return 0;
2469463c3197SDave Hansen 	case S_IFDIR:
2470463c3197SDave Hansen 		return -EPERM;
2471463c3197SDave Hansen 	default:
2472463c3197SDave Hansen 		return -EINVAL;
2473463c3197SDave Hansen 	}
2474463c3197SDave Hansen }
2475463c3197SDave Hansen 
24762e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
24772e4d0924SHeiko Carstens 		unsigned, dev)
24781da177e4SLinus Torvalds {
24792ad94ae6SAl Viro 	int error;
24801da177e4SLinus Torvalds 	char *tmp;
24811da177e4SLinus Torvalds 	struct dentry *dentry;
24821da177e4SLinus Torvalds 	struct nameidata nd;
24831da177e4SLinus Torvalds 
24841da177e4SLinus Torvalds 	if (S_ISDIR(mode))
24851da177e4SLinus Torvalds 		return -EPERM;
24861da177e4SLinus Torvalds 
24872ad94ae6SAl Viro 	error = user_path_parent(dfd, filename, &nd, &tmp);
24881da177e4SLinus Torvalds 	if (error)
24892ad94ae6SAl Viro 		return error;
24902ad94ae6SAl Viro 
24911da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
2492463c3197SDave Hansen 	if (IS_ERR(dentry)) {
24931da177e4SLinus Torvalds 		error = PTR_ERR(dentry);
2494463c3197SDave Hansen 		goto out_unlock;
2495463c3197SDave Hansen 	}
24964ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2497ce3b0f8dSAl Viro 		mode &= ~current_umask();
2498463c3197SDave Hansen 	error = may_mknod(mode);
2499463c3197SDave Hansen 	if (error)
2500463c3197SDave Hansen 		goto out_dput;
2501463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2502463c3197SDave Hansen 	if (error)
2503463c3197SDave Hansen 		goto out_dput;
2504be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd.path, dentry, mode, dev);
2505be6d3e56SKentaro Takeda 	if (error)
2506be6d3e56SKentaro Takeda 		goto out_drop_write;
25071da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
25081da177e4SLinus Torvalds 		case 0: case S_IFREG:
25094ac91378SJan Blunck 			error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
25101da177e4SLinus Torvalds 			break;
25111da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
25124ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
25131da177e4SLinus Torvalds 					new_decode_dev(dev));
25141da177e4SLinus Torvalds 			break;
25151da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
25164ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
25171da177e4SLinus Torvalds 			break;
25181da177e4SLinus Torvalds 	}
2519be6d3e56SKentaro Takeda out_drop_write:
2520463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2521463c3197SDave Hansen out_dput:
25221da177e4SLinus Torvalds 	dput(dentry);
2523463c3197SDave Hansen out_unlock:
25244ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
25251d957f9bSJan Blunck 	path_put(&nd.path);
25261da177e4SLinus Torvalds 	putname(tmp);
25271da177e4SLinus Torvalds 
25281da177e4SLinus Torvalds 	return error;
25291da177e4SLinus Torvalds }
25301da177e4SLinus Torvalds 
25313480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
25325590ff0dSUlrich Drepper {
25335590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
25345590ff0dSUlrich Drepper }
25355590ff0dSUlrich Drepper 
25361da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
25371da177e4SLinus Torvalds {
2538a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
25391da177e4SLinus Torvalds 
25401da177e4SLinus Torvalds 	if (error)
25411da177e4SLinus Torvalds 		return error;
25421da177e4SLinus Torvalds 
2543acfa4380SAl Viro 	if (!dir->i_op->mkdir)
25441da177e4SLinus Torvalds 		return -EPERM;
25451da177e4SLinus Torvalds 
25461da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
25471da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
25481da177e4SLinus Torvalds 	if (error)
25491da177e4SLinus Torvalds 		return error;
25501da177e4SLinus Torvalds 
25511da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2552a74574aaSStephen Smalley 	if (!error)
2553f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
25541da177e4SLinus Torvalds 	return error;
25551da177e4SLinus Torvalds }
25561da177e4SLinus Torvalds 
25572e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
25581da177e4SLinus Torvalds {
25591da177e4SLinus Torvalds 	int error = 0;
25601da177e4SLinus Torvalds 	char * tmp;
25616902d925SDave Hansen 	struct dentry *dentry;
25626902d925SDave Hansen 	struct nameidata nd;
25631da177e4SLinus Torvalds 
25642ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &tmp);
25652ad94ae6SAl Viro 	if (error)
25666902d925SDave Hansen 		goto out_err;
25671da177e4SLinus Torvalds 
25681da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 1);
25691da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
25706902d925SDave Hansen 	if (IS_ERR(dentry))
25716902d925SDave Hansen 		goto out_unlock;
25726902d925SDave Hansen 
25734ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2574ce3b0f8dSAl Viro 		mode &= ~current_umask();
2575463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2576463c3197SDave Hansen 	if (error)
2577463c3197SDave Hansen 		goto out_dput;
2578be6d3e56SKentaro Takeda 	error = security_path_mkdir(&nd.path, dentry, mode);
2579be6d3e56SKentaro Takeda 	if (error)
2580be6d3e56SKentaro Takeda 		goto out_drop_write;
25814ac91378SJan Blunck 	error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2582be6d3e56SKentaro Takeda out_drop_write:
2583463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2584463c3197SDave Hansen out_dput:
25851da177e4SLinus Torvalds 	dput(dentry);
25866902d925SDave Hansen out_unlock:
25874ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
25881d957f9bSJan Blunck 	path_put(&nd.path);
25891da177e4SLinus Torvalds 	putname(tmp);
25906902d925SDave Hansen out_err:
25911da177e4SLinus Torvalds 	return error;
25921da177e4SLinus Torvalds }
25931da177e4SLinus Torvalds 
25943cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
25955590ff0dSUlrich Drepper {
25965590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
25975590ff0dSUlrich Drepper }
25985590ff0dSUlrich Drepper 
25991da177e4SLinus Torvalds /*
26001da177e4SLinus Torvalds  * We try to drop the dentry early: we should have
26011da177e4SLinus Torvalds  * a usage count of 2 if we're the only user of this
26021da177e4SLinus Torvalds  * dentry, and if that is true (possibly after pruning
26031da177e4SLinus Torvalds  * the dcache), then we drop the dentry now.
26041da177e4SLinus Torvalds  *
26051da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
26061da177e4SLinus Torvalds  * do a
26071da177e4SLinus Torvalds  *
26081da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
26091da177e4SLinus Torvalds  *		return -EBUSY;
26101da177e4SLinus Torvalds  *
26111da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
26121da177e4SLinus Torvalds  * that is still in use by something else..
26131da177e4SLinus Torvalds  */
26141da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
26151da177e4SLinus Torvalds {
26161da177e4SLinus Torvalds 	dget(dentry);
26171da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
26181da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
2619b7ab39f6SNick Piggin 	if (dentry->d_count == 2)
26201da177e4SLinus Torvalds 		__d_drop(dentry);
26211da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
26221da177e4SLinus Torvalds }
26231da177e4SLinus Torvalds 
26241da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
26251da177e4SLinus Torvalds {
26261da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
26271da177e4SLinus Torvalds 
26281da177e4SLinus Torvalds 	if (error)
26291da177e4SLinus Torvalds 		return error;
26301da177e4SLinus Torvalds 
2631acfa4380SAl Viro 	if (!dir->i_op->rmdir)
26321da177e4SLinus Torvalds 		return -EPERM;
26331da177e4SLinus Torvalds 
26341b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
26351da177e4SLinus Torvalds 	dentry_unhash(dentry);
26361da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
26371da177e4SLinus Torvalds 		error = -EBUSY;
26381da177e4SLinus Torvalds 	else {
26391da177e4SLinus Torvalds 		error = security_inode_rmdir(dir, dentry);
26401da177e4SLinus Torvalds 		if (!error) {
26411da177e4SLinus Torvalds 			error = dir->i_op->rmdir(dir, dentry);
2642d83c49f3SAl Viro 			if (!error) {
26431da177e4SLinus Torvalds 				dentry->d_inode->i_flags |= S_DEAD;
2644d83c49f3SAl Viro 				dont_mount(dentry);
2645d83c49f3SAl Viro 			}
26461da177e4SLinus Torvalds 		}
26471da177e4SLinus Torvalds 	}
26481b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
26491da177e4SLinus Torvalds 	if (!error) {
26501da177e4SLinus Torvalds 		d_delete(dentry);
26511da177e4SLinus Torvalds 	}
26521da177e4SLinus Torvalds 	dput(dentry);
26531da177e4SLinus Torvalds 
26541da177e4SLinus Torvalds 	return error;
26551da177e4SLinus Torvalds }
26561da177e4SLinus Torvalds 
26575590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
26581da177e4SLinus Torvalds {
26591da177e4SLinus Torvalds 	int error = 0;
26601da177e4SLinus Torvalds 	char * name;
26611da177e4SLinus Torvalds 	struct dentry *dentry;
26621da177e4SLinus Torvalds 	struct nameidata nd;
26631da177e4SLinus Torvalds 
26642ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
26651da177e4SLinus Torvalds 	if (error)
26662ad94ae6SAl Viro 		return error;
26671da177e4SLinus Torvalds 
26681da177e4SLinus Torvalds 	switch(nd.last_type) {
26691da177e4SLinus Torvalds 	case LAST_DOTDOT:
26701da177e4SLinus Torvalds 		error = -ENOTEMPTY;
26711da177e4SLinus Torvalds 		goto exit1;
26721da177e4SLinus Torvalds 	case LAST_DOT:
26731da177e4SLinus Torvalds 		error = -EINVAL;
26741da177e4SLinus Torvalds 		goto exit1;
26751da177e4SLinus Torvalds 	case LAST_ROOT:
26761da177e4SLinus Torvalds 		error = -EBUSY;
26771da177e4SLinus Torvalds 		goto exit1;
26781da177e4SLinus Torvalds 	}
26790612d9fbSOGAWA Hirofumi 
26800612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
26810612d9fbSOGAWA Hirofumi 
26824ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
268349705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
26841da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
26856902d925SDave Hansen 	if (IS_ERR(dentry))
26866902d925SDave Hansen 		goto exit2;
26870622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
26880622753bSDave Hansen 	if (error)
26890622753bSDave Hansen 		goto exit3;
2690be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2691be6d3e56SKentaro Takeda 	if (error)
2692be6d3e56SKentaro Takeda 		goto exit4;
26934ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2694be6d3e56SKentaro Takeda exit4:
26950622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
26960622753bSDave Hansen exit3:
26971da177e4SLinus Torvalds 	dput(dentry);
26986902d925SDave Hansen exit2:
26994ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
27001da177e4SLinus Torvalds exit1:
27011d957f9bSJan Blunck 	path_put(&nd.path);
27021da177e4SLinus Torvalds 	putname(name);
27031da177e4SLinus Torvalds 	return error;
27041da177e4SLinus Torvalds }
27051da177e4SLinus Torvalds 
27063cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
27075590ff0dSUlrich Drepper {
27085590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
27095590ff0dSUlrich Drepper }
27105590ff0dSUlrich Drepper 
27111da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
27121da177e4SLinus Torvalds {
27131da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
27141da177e4SLinus Torvalds 
27151da177e4SLinus Torvalds 	if (error)
27161da177e4SLinus Torvalds 		return error;
27171da177e4SLinus Torvalds 
2718acfa4380SAl Viro 	if (!dir->i_op->unlink)
27191da177e4SLinus Torvalds 		return -EPERM;
27201da177e4SLinus Torvalds 
27211b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
27221da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
27231da177e4SLinus Torvalds 		error = -EBUSY;
27241da177e4SLinus Torvalds 	else {
27251da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2726bec1052eSAl Viro 		if (!error) {
27271da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2728bec1052eSAl Viro 			if (!error)
2729d83c49f3SAl Viro 				dont_mount(dentry);
2730bec1052eSAl Viro 		}
27311da177e4SLinus Torvalds 	}
27321b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
27331da177e4SLinus Torvalds 
27341da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
27351da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2736ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
27371da177e4SLinus Torvalds 		d_delete(dentry);
27381da177e4SLinus Torvalds 	}
27390eeca283SRobert Love 
27401da177e4SLinus Torvalds 	return error;
27411da177e4SLinus Torvalds }
27421da177e4SLinus Torvalds 
27431da177e4SLinus Torvalds /*
27441da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
27451b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
27461da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
27471da177e4SLinus Torvalds  * while waiting on the I/O.
27481da177e4SLinus Torvalds  */
27495590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
27501da177e4SLinus Torvalds {
27512ad94ae6SAl Viro 	int error;
27521da177e4SLinus Torvalds 	char *name;
27531da177e4SLinus Torvalds 	struct dentry *dentry;
27541da177e4SLinus Torvalds 	struct nameidata nd;
27551da177e4SLinus Torvalds 	struct inode *inode = NULL;
27561da177e4SLinus Torvalds 
27572ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
27581da177e4SLinus Torvalds 	if (error)
27592ad94ae6SAl Viro 		return error;
27602ad94ae6SAl Viro 
27611da177e4SLinus Torvalds 	error = -EISDIR;
27621da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
27631da177e4SLinus Torvalds 		goto exit1;
27640612d9fbSOGAWA Hirofumi 
27650612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
27660612d9fbSOGAWA Hirofumi 
27674ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
276849705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
27691da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
27701da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
27711da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
27721da177e4SLinus Torvalds 		if (nd.last.name[nd.last.len])
27731da177e4SLinus Torvalds 			goto slashes;
27741da177e4SLinus Torvalds 		inode = dentry->d_inode;
27751da177e4SLinus Torvalds 		if (inode)
27767de9c6eeSAl Viro 			ihold(inode);
27770622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
27780622753bSDave Hansen 		if (error)
27790622753bSDave Hansen 			goto exit2;
2780be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2781be6d3e56SKentaro Takeda 		if (error)
2782be6d3e56SKentaro Takeda 			goto exit3;
27834ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2784be6d3e56SKentaro Takeda exit3:
27850622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
27861da177e4SLinus Torvalds 	exit2:
27871da177e4SLinus Torvalds 		dput(dentry);
27881da177e4SLinus Torvalds 	}
27894ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
27901da177e4SLinus Torvalds 	if (inode)
27911da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
27921da177e4SLinus Torvalds exit1:
27931d957f9bSJan Blunck 	path_put(&nd.path);
27941da177e4SLinus Torvalds 	putname(name);
27951da177e4SLinus Torvalds 	return error;
27961da177e4SLinus Torvalds 
27971da177e4SLinus Torvalds slashes:
27981da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
27991da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
28001da177e4SLinus Torvalds 	goto exit2;
28011da177e4SLinus Torvalds }
28021da177e4SLinus Torvalds 
28032e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
28045590ff0dSUlrich Drepper {
28055590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
28065590ff0dSUlrich Drepper 		return -EINVAL;
28075590ff0dSUlrich Drepper 
28085590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
28095590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
28105590ff0dSUlrich Drepper 
28115590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
28125590ff0dSUlrich Drepper }
28135590ff0dSUlrich Drepper 
28143480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
28155590ff0dSUlrich Drepper {
28165590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
28175590ff0dSUlrich Drepper }
28185590ff0dSUlrich Drepper 
2819db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
28201da177e4SLinus Torvalds {
2821a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
28221da177e4SLinus Torvalds 
28231da177e4SLinus Torvalds 	if (error)
28241da177e4SLinus Torvalds 		return error;
28251da177e4SLinus Torvalds 
2826acfa4380SAl Viro 	if (!dir->i_op->symlink)
28271da177e4SLinus Torvalds 		return -EPERM;
28281da177e4SLinus Torvalds 
28291da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
28301da177e4SLinus Torvalds 	if (error)
28311da177e4SLinus Torvalds 		return error;
28321da177e4SLinus Torvalds 
28331da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2834a74574aaSStephen Smalley 	if (!error)
2835f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
28361da177e4SLinus Torvalds 	return error;
28371da177e4SLinus Torvalds }
28381da177e4SLinus Torvalds 
28392e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
28402e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
28411da177e4SLinus Torvalds {
28422ad94ae6SAl Viro 	int error;
28431da177e4SLinus Torvalds 	char *from;
28441da177e4SLinus Torvalds 	char *to;
28456902d925SDave Hansen 	struct dentry *dentry;
28466902d925SDave Hansen 	struct nameidata nd;
28471da177e4SLinus Torvalds 
28481da177e4SLinus Torvalds 	from = getname(oldname);
28491da177e4SLinus Torvalds 	if (IS_ERR(from))
28501da177e4SLinus Torvalds 		return PTR_ERR(from);
28512ad94ae6SAl Viro 
28522ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
28532ad94ae6SAl Viro 	if (error)
28546902d925SDave Hansen 		goto out_putname;
28551da177e4SLinus Torvalds 
28561da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
28571da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
28586902d925SDave Hansen 	if (IS_ERR(dentry))
28596902d925SDave Hansen 		goto out_unlock;
28606902d925SDave Hansen 
286175c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
286275c3f29dSDave Hansen 	if (error)
286375c3f29dSDave Hansen 		goto out_dput;
2864be6d3e56SKentaro Takeda 	error = security_path_symlink(&nd.path, dentry, from);
2865be6d3e56SKentaro Takeda 	if (error)
2866be6d3e56SKentaro Takeda 		goto out_drop_write;
2867db2e747bSMiklos Szeredi 	error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2868be6d3e56SKentaro Takeda out_drop_write:
286975c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
287075c3f29dSDave Hansen out_dput:
28711da177e4SLinus Torvalds 	dput(dentry);
28726902d925SDave Hansen out_unlock:
28734ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
28741d957f9bSJan Blunck 	path_put(&nd.path);
28751da177e4SLinus Torvalds 	putname(to);
28766902d925SDave Hansen out_putname:
28771da177e4SLinus Torvalds 	putname(from);
28781da177e4SLinus Torvalds 	return error;
28791da177e4SLinus Torvalds }
28801da177e4SLinus Torvalds 
28813480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
28825590ff0dSUlrich Drepper {
28835590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
28845590ff0dSUlrich Drepper }
28855590ff0dSUlrich Drepper 
28861da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
28871da177e4SLinus Torvalds {
28881da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
28891da177e4SLinus Torvalds 	int error;
28901da177e4SLinus Torvalds 
28911da177e4SLinus Torvalds 	if (!inode)
28921da177e4SLinus Torvalds 		return -ENOENT;
28931da177e4SLinus Torvalds 
2894a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
28951da177e4SLinus Torvalds 	if (error)
28961da177e4SLinus Torvalds 		return error;
28971da177e4SLinus Torvalds 
28981da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
28991da177e4SLinus Torvalds 		return -EXDEV;
29001da177e4SLinus Torvalds 
29011da177e4SLinus Torvalds 	/*
29021da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
29031da177e4SLinus Torvalds 	 */
29041da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
29051da177e4SLinus Torvalds 		return -EPERM;
2906acfa4380SAl Viro 	if (!dir->i_op->link)
29071da177e4SLinus Torvalds 		return -EPERM;
29087e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
29091da177e4SLinus Torvalds 		return -EPERM;
29101da177e4SLinus Torvalds 
29111da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
29121da177e4SLinus Torvalds 	if (error)
29131da177e4SLinus Torvalds 		return error;
29141da177e4SLinus Torvalds 
29157e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
29161da177e4SLinus Torvalds 	error = dir->i_op->link(old_dentry, dir, new_dentry);
29177e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
2918e31e14ecSStephen Smalley 	if (!error)
29197e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
29201da177e4SLinus Torvalds 	return error;
29211da177e4SLinus Torvalds }
29221da177e4SLinus Torvalds 
29231da177e4SLinus Torvalds /*
29241da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
29251da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
29261da177e4SLinus Torvalds  * newname.  --KAB
29271da177e4SLinus Torvalds  *
29281da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
29291da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
29301da177e4SLinus Torvalds  * and other special files.  --ADM
29311da177e4SLinus Torvalds  */
29322e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
29332e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
29341da177e4SLinus Torvalds {
29351da177e4SLinus Torvalds 	struct dentry *new_dentry;
29362d8f3038SAl Viro 	struct nameidata nd;
29372d8f3038SAl Viro 	struct path old_path;
29381da177e4SLinus Torvalds 	int error;
29391da177e4SLinus Torvalds 	char *to;
29401da177e4SLinus Torvalds 
294145c9b11aSUlrich Drepper 	if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
2942c04030e1SUlrich Drepper 		return -EINVAL;
2943c04030e1SUlrich Drepper 
29442d8f3038SAl Viro 	error = user_path_at(olddfd, oldname,
294545c9b11aSUlrich Drepper 			     flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
29462d8f3038SAl Viro 			     &old_path);
29471da177e4SLinus Torvalds 	if (error)
29482ad94ae6SAl Viro 		return error;
29492ad94ae6SAl Viro 
29502ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
29511da177e4SLinus Torvalds 	if (error)
29521da177e4SLinus Torvalds 		goto out;
29531da177e4SLinus Torvalds 	error = -EXDEV;
29542d8f3038SAl Viro 	if (old_path.mnt != nd.path.mnt)
29551da177e4SLinus Torvalds 		goto out_release;
29561da177e4SLinus Torvalds 	new_dentry = lookup_create(&nd, 0);
29571da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
29586902d925SDave Hansen 	if (IS_ERR(new_dentry))
29596902d925SDave Hansen 		goto out_unlock;
296075c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
296175c3f29dSDave Hansen 	if (error)
296275c3f29dSDave Hansen 		goto out_dput;
2963be6d3e56SKentaro Takeda 	error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2964be6d3e56SKentaro Takeda 	if (error)
2965be6d3e56SKentaro Takeda 		goto out_drop_write;
29662d8f3038SAl Viro 	error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
2967be6d3e56SKentaro Takeda out_drop_write:
296875c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
296975c3f29dSDave Hansen out_dput:
29701da177e4SLinus Torvalds 	dput(new_dentry);
29716902d925SDave Hansen out_unlock:
29724ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
29731da177e4SLinus Torvalds out_release:
29741d957f9bSJan Blunck 	path_put(&nd.path);
29752ad94ae6SAl Viro 	putname(to);
29761da177e4SLinus Torvalds out:
29772d8f3038SAl Viro 	path_put(&old_path);
29781da177e4SLinus Torvalds 
29791da177e4SLinus Torvalds 	return error;
29801da177e4SLinus Torvalds }
29811da177e4SLinus Torvalds 
29823480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
29835590ff0dSUlrich Drepper {
2984c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
29855590ff0dSUlrich Drepper }
29865590ff0dSUlrich Drepper 
29871da177e4SLinus Torvalds /*
29881da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
29891da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
29901da177e4SLinus Torvalds  * Problems:
29911da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
29921da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
29931da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
2994a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
29951da177e4SLinus Torvalds  *	   story.
29961da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
29971b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
29981da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
29991da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
3000a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
30011da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
30021da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
30031da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
3004a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
30051da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
30061da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
30071da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
30081da177e4SLinus Torvalds  *	d) some filesystems don't support opened-but-unlinked directories,
30091da177e4SLinus Torvalds  *	   either because of layout or because they are not ready to deal with
30101da177e4SLinus Torvalds  *	   all cases correctly. The latter will be fixed (taking this sort of
30111da177e4SLinus Torvalds  *	   stuff into VFS), but the former is not going away. Solution: the same
30121da177e4SLinus Torvalds  *	   trick as in rmdir().
30131da177e4SLinus Torvalds  *	e) conversion from fhandle to dentry may come in the wrong moment - when
30141b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
30151da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3016c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
30171da177e4SLinus Torvalds  *	   locking].
30181da177e4SLinus Torvalds  */
301975c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
30201da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
30211da177e4SLinus Torvalds {
30221da177e4SLinus Torvalds 	int error = 0;
30231da177e4SLinus Torvalds 	struct inode *target;
30241da177e4SLinus Torvalds 
30251da177e4SLinus Torvalds 	/*
30261da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
30271da177e4SLinus Torvalds 	 * we'll need to flip '..'.
30281da177e4SLinus Torvalds 	 */
30291da177e4SLinus Torvalds 	if (new_dir != old_dir) {
3030f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
30311da177e4SLinus Torvalds 		if (error)
30321da177e4SLinus Torvalds 			return error;
30331da177e4SLinus Torvalds 	}
30341da177e4SLinus Torvalds 
30351da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30361da177e4SLinus Torvalds 	if (error)
30371da177e4SLinus Torvalds 		return error;
30381da177e4SLinus Torvalds 
30391da177e4SLinus Torvalds 	target = new_dentry->d_inode;
3040d83c49f3SAl Viro 	if (target)
30411b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
30421da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
30431da177e4SLinus Torvalds 		error = -EBUSY;
3044d83c49f3SAl Viro 	else {
3045d83c49f3SAl Viro 		if (target)
3046d83c49f3SAl Viro 			dentry_unhash(new_dentry);
30471da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
3048d83c49f3SAl Viro 	}
30491da177e4SLinus Torvalds 	if (target) {
3050d83c49f3SAl Viro 		if (!error) {
30511da177e4SLinus Torvalds 			target->i_flags |= S_DEAD;
3052d83c49f3SAl Viro 			dont_mount(new_dentry);
3053d83c49f3SAl Viro 		}
30541b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
30551da177e4SLinus Torvalds 		if (d_unhashed(new_dentry))
30561da177e4SLinus Torvalds 			d_rehash(new_dentry);
30571da177e4SLinus Torvalds 		dput(new_dentry);
30581da177e4SLinus Torvalds 	}
3059e31e14ecSStephen Smalley 	if (!error)
3060349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30611da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
30621da177e4SLinus Torvalds 	return error;
30631da177e4SLinus Torvalds }
30641da177e4SLinus Torvalds 
306575c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
30661da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
30671da177e4SLinus Torvalds {
30681da177e4SLinus Torvalds 	struct inode *target;
30691da177e4SLinus Torvalds 	int error;
30701da177e4SLinus Torvalds 
30711da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30721da177e4SLinus Torvalds 	if (error)
30731da177e4SLinus Torvalds 		return error;
30741da177e4SLinus Torvalds 
30751da177e4SLinus Torvalds 	dget(new_dentry);
30761da177e4SLinus Torvalds 	target = new_dentry->d_inode;
30771da177e4SLinus Torvalds 	if (target)
30781b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
30791da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
30801da177e4SLinus Torvalds 		error = -EBUSY;
30811da177e4SLinus Torvalds 	else
30821da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
30831da177e4SLinus Torvalds 	if (!error) {
3084bec1052eSAl Viro 		if (target)
3085d83c49f3SAl Viro 			dont_mount(new_dentry);
3086349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30871da177e4SLinus Torvalds 			d_move(old_dentry, new_dentry);
30881da177e4SLinus Torvalds 	}
30891da177e4SLinus Torvalds 	if (target)
30901b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
30911da177e4SLinus Torvalds 	dput(new_dentry);
30921da177e4SLinus Torvalds 	return error;
30931da177e4SLinus Torvalds }
30941da177e4SLinus Torvalds 
30951da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
30961da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
30971da177e4SLinus Torvalds {
30981da177e4SLinus Torvalds 	int error;
30991da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
310059b0df21SEric Paris 	const unsigned char *old_name;
31011da177e4SLinus Torvalds 
31021da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
31031da177e4SLinus Torvalds  		return 0;
31041da177e4SLinus Torvalds 
31051da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
31061da177e4SLinus Torvalds 	if (error)
31071da177e4SLinus Torvalds 		return error;
31081da177e4SLinus Torvalds 
31091da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3110a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
31111da177e4SLinus Torvalds 	else
31121da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
31131da177e4SLinus Torvalds 	if (error)
31141da177e4SLinus Torvalds 		return error;
31151da177e4SLinus Torvalds 
3116acfa4380SAl Viro 	if (!old_dir->i_op->rename)
31171da177e4SLinus Torvalds 		return -EPERM;
31181da177e4SLinus Torvalds 
31190eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
31200eeca283SRobert Love 
31211da177e4SLinus Torvalds 	if (is_dir)
31221da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
31231da177e4SLinus Torvalds 	else
31241da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3125123df294SAl Viro 	if (!error)
3126123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
31275a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
31280eeca283SRobert Love 	fsnotify_oldname_free(old_name);
31290eeca283SRobert Love 
31301da177e4SLinus Torvalds 	return error;
31311da177e4SLinus Torvalds }
31321da177e4SLinus Torvalds 
31332e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
31342e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
31351da177e4SLinus Torvalds {
31361da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
31371da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
31381da177e4SLinus Torvalds 	struct dentry *trap;
31391da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
31402ad94ae6SAl Viro 	char *from;
31412ad94ae6SAl Viro 	char *to;
31422ad94ae6SAl Viro 	int error;
31431da177e4SLinus Torvalds 
31442ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
31451da177e4SLinus Torvalds 	if (error)
31461da177e4SLinus Torvalds 		goto exit;
31471da177e4SLinus Torvalds 
31482ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
31491da177e4SLinus Torvalds 	if (error)
31501da177e4SLinus Torvalds 		goto exit1;
31511da177e4SLinus Torvalds 
31521da177e4SLinus Torvalds 	error = -EXDEV;
31534ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
31541da177e4SLinus Torvalds 		goto exit2;
31551da177e4SLinus Torvalds 
31564ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
31571da177e4SLinus Torvalds 	error = -EBUSY;
31581da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
31591da177e4SLinus Torvalds 		goto exit2;
31601da177e4SLinus Torvalds 
31614ac91378SJan Blunck 	new_dir = newnd.path.dentry;
31621da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
31631da177e4SLinus Torvalds 		goto exit2;
31641da177e4SLinus Torvalds 
31650612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
31660612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
31674e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
31680612d9fbSOGAWA Hirofumi 
31691da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
31701da177e4SLinus Torvalds 
317149705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
31721da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
31731da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
31741da177e4SLinus Torvalds 		goto exit3;
31751da177e4SLinus Torvalds 	/* source must exist */
31761da177e4SLinus Torvalds 	error = -ENOENT;
31771da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
31781da177e4SLinus Torvalds 		goto exit4;
31791da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
31801da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
31811da177e4SLinus Torvalds 		error = -ENOTDIR;
31821da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
31831da177e4SLinus Torvalds 			goto exit4;
31841da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
31851da177e4SLinus Torvalds 			goto exit4;
31861da177e4SLinus Torvalds 	}
31871da177e4SLinus Torvalds 	/* source should not be ancestor of target */
31881da177e4SLinus Torvalds 	error = -EINVAL;
31891da177e4SLinus Torvalds 	if (old_dentry == trap)
31901da177e4SLinus Torvalds 		goto exit4;
319149705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
31921da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
31931da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
31941da177e4SLinus Torvalds 		goto exit4;
31951da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
31961da177e4SLinus Torvalds 	error = -ENOTEMPTY;
31971da177e4SLinus Torvalds 	if (new_dentry == trap)
31981da177e4SLinus Torvalds 		goto exit5;
31991da177e4SLinus Torvalds 
32009079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
32019079b1ebSDave Hansen 	if (error)
32029079b1ebSDave Hansen 		goto exit5;
3203be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3204be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3205be6d3e56SKentaro Takeda 	if (error)
3206be6d3e56SKentaro Takeda 		goto exit6;
32071da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
32081da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3209be6d3e56SKentaro Takeda exit6:
32109079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
32111da177e4SLinus Torvalds exit5:
32121da177e4SLinus Torvalds 	dput(new_dentry);
32131da177e4SLinus Torvalds exit4:
32141da177e4SLinus Torvalds 	dput(old_dentry);
32151da177e4SLinus Torvalds exit3:
32161da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
32171da177e4SLinus Torvalds exit2:
32181d957f9bSJan Blunck 	path_put(&newnd.path);
32192ad94ae6SAl Viro 	putname(to);
32201da177e4SLinus Torvalds exit1:
32211d957f9bSJan Blunck 	path_put(&oldnd.path);
32221da177e4SLinus Torvalds 	putname(from);
32232ad94ae6SAl Viro exit:
32241da177e4SLinus Torvalds 	return error;
32251da177e4SLinus Torvalds }
32261da177e4SLinus Torvalds 
3227a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
32285590ff0dSUlrich Drepper {
32295590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
32305590ff0dSUlrich Drepper }
32315590ff0dSUlrich Drepper 
32321da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
32331da177e4SLinus Torvalds {
32341da177e4SLinus Torvalds 	int len;
32351da177e4SLinus Torvalds 
32361da177e4SLinus Torvalds 	len = PTR_ERR(link);
32371da177e4SLinus Torvalds 	if (IS_ERR(link))
32381da177e4SLinus Torvalds 		goto out;
32391da177e4SLinus Torvalds 
32401da177e4SLinus Torvalds 	len = strlen(link);
32411da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
32421da177e4SLinus Torvalds 		len = buflen;
32431da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
32441da177e4SLinus Torvalds 		len = -EFAULT;
32451da177e4SLinus Torvalds out:
32461da177e4SLinus Torvalds 	return len;
32471da177e4SLinus Torvalds }
32481da177e4SLinus Torvalds 
32491da177e4SLinus Torvalds /*
32501da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
32511da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
32521da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
32531da177e4SLinus Torvalds  */
32541da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
32551da177e4SLinus Torvalds {
32561da177e4SLinus Torvalds 	struct nameidata nd;
3257cc314eefSLinus Torvalds 	void *cookie;
3258694a1764SMarcin Slusarz 	int res;
3259cc314eefSLinus Torvalds 
32601da177e4SLinus Torvalds 	nd.depth = 0;
3261cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3262694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3263694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3264694a1764SMarcin Slusarz 
3265694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
32661da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3267cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3268694a1764SMarcin Slusarz 	return res;
32691da177e4SLinus Torvalds }
32701da177e4SLinus Torvalds 
32711da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
32721da177e4SLinus Torvalds {
32731da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
32741da177e4SLinus Torvalds }
32751da177e4SLinus Torvalds 
32761da177e4SLinus Torvalds /* get the link contents into pagecache */
32771da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
32781da177e4SLinus Torvalds {
3279ebd09abbSDuane Griffin 	char *kaddr;
32801da177e4SLinus Torvalds 	struct page *page;
32811da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3282090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
32831da177e4SLinus Torvalds 	if (IS_ERR(page))
32846fe6900eSNick Piggin 		return (char*)page;
32851da177e4SLinus Torvalds 	*ppage = page;
3286ebd09abbSDuane Griffin 	kaddr = kmap(page);
3287ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3288ebd09abbSDuane Griffin 	return kaddr;
32891da177e4SLinus Torvalds }
32901da177e4SLinus Torvalds 
32911da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
32921da177e4SLinus Torvalds {
32931da177e4SLinus Torvalds 	struct page *page = NULL;
32941da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
32951da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
32961da177e4SLinus Torvalds 	if (page) {
32971da177e4SLinus Torvalds 		kunmap(page);
32981da177e4SLinus Torvalds 		page_cache_release(page);
32991da177e4SLinus Torvalds 	}
33001da177e4SLinus Torvalds 	return res;
33011da177e4SLinus Torvalds }
33021da177e4SLinus Torvalds 
3303cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
33041da177e4SLinus Torvalds {
3305cc314eefSLinus Torvalds 	struct page *page = NULL;
33061da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3307cc314eefSLinus Torvalds 	return page;
33081da177e4SLinus Torvalds }
33091da177e4SLinus Torvalds 
3310cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
33111da177e4SLinus Torvalds {
3312cc314eefSLinus Torvalds 	struct page *page = cookie;
3313cc314eefSLinus Torvalds 
3314cc314eefSLinus Torvalds 	if (page) {
33151da177e4SLinus Torvalds 		kunmap(page);
33161da177e4SLinus Torvalds 		page_cache_release(page);
33171da177e4SLinus Torvalds 	}
33181da177e4SLinus Torvalds }
33191da177e4SLinus Torvalds 
332054566b2cSNick Piggin /*
332154566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
332254566b2cSNick Piggin  */
332354566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
33241da177e4SLinus Torvalds {
33251da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
33260adb25d2SKirill Korotaev 	struct page *page;
3327afddba49SNick Piggin 	void *fsdata;
3328beb497abSDmitriy Monakhov 	int err;
33291da177e4SLinus Torvalds 	char *kaddr;
333054566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
333154566b2cSNick Piggin 	if (nofs)
333254566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
33331da177e4SLinus Torvalds 
33347e53cac4SNeilBrown retry:
3335afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
333654566b2cSNick Piggin 				flags, &page, &fsdata);
33371da177e4SLinus Torvalds 	if (err)
3338afddba49SNick Piggin 		goto fail;
3339afddba49SNick Piggin 
33401da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
33411da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
33421da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
3343afddba49SNick Piggin 
3344afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3345afddba49SNick Piggin 							page, fsdata);
33461da177e4SLinus Torvalds 	if (err < 0)
33471da177e4SLinus Torvalds 		goto fail;
3348afddba49SNick Piggin 	if (err < len-1)
3349afddba49SNick Piggin 		goto retry;
3350afddba49SNick Piggin 
33511da177e4SLinus Torvalds 	mark_inode_dirty(inode);
33521da177e4SLinus Torvalds 	return 0;
33531da177e4SLinus Torvalds fail:
33541da177e4SLinus Torvalds 	return err;
33551da177e4SLinus Torvalds }
33561da177e4SLinus Torvalds 
33570adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
33580adb25d2SKirill Korotaev {
33590adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
336054566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
33610adb25d2SKirill Korotaev }
33620adb25d2SKirill Korotaev 
336392e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
33641da177e4SLinus Torvalds 	.readlink	= generic_readlink,
33651da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
33661da177e4SLinus Torvalds 	.put_link	= page_put_link,
33671da177e4SLinus Torvalds };
33681da177e4SLinus Torvalds 
33692d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
33701da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
33711da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
33721da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
33731da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
33741da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
33751da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
33761da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
33771da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
33781da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
33790adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
33801da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
33811da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
33821da177e4SLinus Torvalds EXPORT_SYMBOL(path_lookup);
3383d1811465SAl Viro EXPORT_SYMBOL(kern_path);
338416f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3385f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
33868c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission);
33871da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
33881da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
33891da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
33901da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
33911da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
33921da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
33931da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
33941da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
33951da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
33961da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
33971da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
33981da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
33991da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
34001da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3401