xref: /openbmc/linux/fs/namei.c (revision 1f36f774)
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/quotaops.h>
231da177e4SLinus Torvalds #include <linux/pagemap.h>
240eeca283SRobert Love #include <linux/fsnotify.h>
251da177e4SLinus Torvalds #include <linux/personality.h>
261da177e4SLinus Torvalds #include <linux/security.h>
276146f0d5SMimi Zohar #include <linux/ima.h>
281da177e4SLinus Torvalds #include <linux/syscalls.h>
291da177e4SLinus Torvalds #include <linux/mount.h>
301da177e4SLinus Torvalds #include <linux/audit.h>
3116f7e0feSRandy Dunlap #include <linux/capability.h>
32834f2a4aSTrond Myklebust #include <linux/file.h>
335590ff0dSUlrich Drepper #include <linux/fcntl.h>
3408ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h>
355ad4e53bSAl Viro #include <linux/fs_struct.h>
361da177e4SLinus Torvalds #include <asm/uaccess.h>
371da177e4SLinus Torvalds 
38e81e3f4dSEric Paris #include "internal.h"
39e81e3f4dSEric Paris 
401da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer]
411da177e4SLinus Torvalds  * Fundamental changes in the pathname lookup mechanisms (namei)
421da177e4SLinus Torvalds  * were necessary because of omirr.  The reason is that omirr needs
431da177e4SLinus Torvalds  * to know the _real_ pathname, not the user-supplied one, in case
441da177e4SLinus Torvalds  * of symlinks (and also when transname replacements occur).
451da177e4SLinus Torvalds  *
461da177e4SLinus Torvalds  * The new code replaces the old recursive symlink resolution with
471da177e4SLinus Torvalds  * an iterative one (in case of non-nested symlink chains).  It does
481da177e4SLinus Torvalds  * this with calls to <fs>_follow_link().
491da177e4SLinus Torvalds  * As a side effect, dir_namei(), _namei() and follow_link() are now
501da177e4SLinus Torvalds  * replaced with a single function lookup_dentry() that can handle all
511da177e4SLinus Torvalds  * the special cases of the former code.
521da177e4SLinus Torvalds  *
531da177e4SLinus Torvalds  * With the new dcache, the pathname is stored at each inode, at least as
541da177e4SLinus Torvalds  * long as the refcount of the inode is positive.  As a side effect, the
551da177e4SLinus Torvalds  * size of the dcache depends on the inode cache and thus is dynamic.
561da177e4SLinus Torvalds  *
571da177e4SLinus Torvalds  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
581da177e4SLinus Torvalds  * resolution to correspond with current state of the code.
591da177e4SLinus Torvalds  *
601da177e4SLinus Torvalds  * Note that the symlink resolution is not *completely* iterative.
611da177e4SLinus Torvalds  * There is still a significant amount of tail- and mid- recursion in
621da177e4SLinus Torvalds  * the algorithm.  Also, note that <fs>_readlink() is not used in
631da177e4SLinus Torvalds  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
641da177e4SLinus Torvalds  * may return different results than <fs>_follow_link().  Many virtual
651da177e4SLinus Torvalds  * filesystems (including /proc) exhibit this behavior.
661da177e4SLinus Torvalds  */
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
691da177e4SLinus Torvalds  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
701da177e4SLinus Torvalds  * and the name already exists in form of a symlink, try to create the new
711da177e4SLinus Torvalds  * name indicated by the symlink. The old code always complained that the
721da177e4SLinus Torvalds  * name already exists, due to not following the symlink even if its target
731da177e4SLinus Torvalds  * is nonexistent.  The new semantics affects also mknod() and link() when
741da177e4SLinus Torvalds  * the name is a symlink pointing to a non-existant name.
751da177e4SLinus Torvalds  *
761da177e4SLinus Torvalds  * I don't know which semantics is the right one, since I have no access
771da177e4SLinus Torvalds  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
781da177e4SLinus Torvalds  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
791da177e4SLinus Torvalds  * "old" one. Personally, I think the new semantics is much more logical.
801da177e4SLinus Torvalds  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
811da177e4SLinus Torvalds  * file does succeed in both HP-UX and SunOs, but not in Solaris
821da177e4SLinus Torvalds  * and in the old Linux semantics.
831da177e4SLinus Torvalds  */
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
861da177e4SLinus Torvalds  * semantics.  See the comments in "open_namei" and "do_link" below.
871da177e4SLinus Torvalds  *
881da177e4SLinus Torvalds  * [10-Sep-98 Alan Modra] Another symlink change.
891da177e4SLinus Torvalds  */
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
921da177e4SLinus Torvalds  *	inside the path - always follow.
931da177e4SLinus Torvalds  *	in the last component in creation/removal/renaming - never follow.
941da177e4SLinus Torvalds  *	if LOOKUP_FOLLOW passed - follow.
951da177e4SLinus Torvalds  *	if the pathname has trailing slashes - follow.
961da177e4SLinus Torvalds  *	otherwise - don't follow.
971da177e4SLinus Torvalds  * (applied in that order).
981da177e4SLinus Torvalds  *
991da177e4SLinus Torvalds  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
1001da177e4SLinus Torvalds  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
1011da177e4SLinus Torvalds  * During the 2.4 we need to fix the userland stuff depending on it -
1021da177e4SLinus Torvalds  * hopefully we will be able to get rid of that wart in 2.5. So far only
1031da177e4SLinus Torvalds  * XEmacs seems to be relying on it...
1041da177e4SLinus Torvalds  */
1051da177e4SLinus Torvalds /*
1061da177e4SLinus Torvalds  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
107a11f3a05SArjan van de Ven  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
1081da177e4SLinus Torvalds  * any extra contention...
1091da177e4SLinus Torvalds  */
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional
1121da177e4SLinus Torvalds  * checking and hopefully speeding things up, we copy filenames to the
1131da177e4SLinus Torvalds  * kernel data space before using them..
1141da177e4SLinus Torvalds  *
1151da177e4SLinus Torvalds  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
1161da177e4SLinus Torvalds  * PATH_MAX includes the nul terminator --RR.
1171da177e4SLinus Torvalds  */
118858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page)
1191da177e4SLinus Torvalds {
1201da177e4SLinus Torvalds 	int retval;
1211da177e4SLinus Torvalds 	unsigned long len = PATH_MAX;
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	if (!segment_eq(get_fs(), KERNEL_DS)) {
1241da177e4SLinus Torvalds 		if ((unsigned long) filename >= TASK_SIZE)
1251da177e4SLinus Torvalds 			return -EFAULT;
1261da177e4SLinus Torvalds 		if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
1271da177e4SLinus Torvalds 			len = TASK_SIZE - (unsigned long) filename;
1281da177e4SLinus Torvalds 	}
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	retval = strncpy_from_user(page, filename, len);
1311da177e4SLinus Torvalds 	if (retval > 0) {
1321da177e4SLinus Torvalds 		if (retval < len)
1331da177e4SLinus Torvalds 			return 0;
1341da177e4SLinus Torvalds 		return -ENAMETOOLONG;
1351da177e4SLinus Torvalds 	} else if (!retval)
1361da177e4SLinus Torvalds 		retval = -ENOENT;
1371da177e4SLinus Torvalds 	return retval;
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds char * getname(const char __user * filename)
1411da177e4SLinus Torvalds {
1421da177e4SLinus Torvalds 	char *tmp, *result;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	result = ERR_PTR(-ENOMEM);
1451da177e4SLinus Torvalds 	tmp = __getname();
1461da177e4SLinus Torvalds 	if (tmp)  {
1471da177e4SLinus Torvalds 		int retval = do_getname(filename, tmp);
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 		result = tmp;
1501da177e4SLinus Torvalds 		if (retval < 0) {
1511da177e4SLinus Torvalds 			__putname(tmp);
1521da177e4SLinus Torvalds 			result = ERR_PTR(retval);
1531da177e4SLinus Torvalds 		}
1541da177e4SLinus Torvalds 	}
1551da177e4SLinus Torvalds 	audit_getname(result);
1561da177e4SLinus Torvalds 	return result;
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
1601da177e4SLinus Torvalds void putname(const char *name)
1611da177e4SLinus Torvalds {
1625ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
1631da177e4SLinus Torvalds 		audit_putname(name);
1641da177e4SLinus Torvalds 	else
1651da177e4SLinus Torvalds 		__putname(name);
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds EXPORT_SYMBOL(putname);
1681da177e4SLinus Torvalds #endif
1691da177e4SLinus Torvalds 
1705909ccaaSLinus Torvalds /*
1715909ccaaSLinus Torvalds  * This does basic POSIX ACL permission checking
1725909ccaaSLinus Torvalds  */
1735909ccaaSLinus Torvalds static int acl_permission_check(struct inode *inode, int mask,
1745909ccaaSLinus Torvalds 		int (*check_acl)(struct inode *inode, int mask))
1755909ccaaSLinus Torvalds {
1765909ccaaSLinus Torvalds 	umode_t			mode = inode->i_mode;
1775909ccaaSLinus Torvalds 
1785909ccaaSLinus Torvalds 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
1795909ccaaSLinus Torvalds 
1805909ccaaSLinus Torvalds 	if (current_fsuid() == inode->i_uid)
1815909ccaaSLinus Torvalds 		mode >>= 6;
1825909ccaaSLinus Torvalds 	else {
1835909ccaaSLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
1845909ccaaSLinus Torvalds 			int error = check_acl(inode, mask);
1855909ccaaSLinus Torvalds 			if (error != -EAGAIN)
1865909ccaaSLinus Torvalds 				return error;
1875909ccaaSLinus Torvalds 		}
1885909ccaaSLinus Torvalds 
1895909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
1905909ccaaSLinus Torvalds 			mode >>= 3;
1915909ccaaSLinus Torvalds 	}
1925909ccaaSLinus Torvalds 
1935909ccaaSLinus Torvalds 	/*
1945909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
1955909ccaaSLinus Torvalds 	 */
1965909ccaaSLinus Torvalds 	if ((mask & ~mode) == 0)
1975909ccaaSLinus Torvalds 		return 0;
1985909ccaaSLinus Torvalds 	return -EACCES;
1995909ccaaSLinus Torvalds }
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds /**
2021da177e4SLinus Torvalds  * generic_permission  -  check for access rights on a Posix-like filesystem
2031da177e4SLinus Torvalds  * @inode:	inode to check access rights for
2041da177e4SLinus Torvalds  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
2051da177e4SLinus Torvalds  * @check_acl:	optional callback to check for Posix ACLs
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
2101da177e4SLinus Torvalds  * are used for other things..
2111da177e4SLinus Torvalds  */
2121da177e4SLinus Torvalds int generic_permission(struct inode *inode, int mask,
2131da177e4SLinus Torvalds 		int (*check_acl)(struct inode *inode, int mask))
2141da177e4SLinus Torvalds {
2155909ccaaSLinus Torvalds 	int ret;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	/*
2185909ccaaSLinus Torvalds 	 * Do the basic POSIX ACL permission checks.
2191da177e4SLinus Torvalds 	 */
2205909ccaaSLinus Torvalds 	ret = acl_permission_check(inode, mask, check_acl);
2215909ccaaSLinus Torvalds 	if (ret != -EACCES)
2225909ccaaSLinus Torvalds 		return ret;
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	/*
2251da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
2261da177e4SLinus Torvalds 	 * Executable DACs are overridable if at least one exec bit is set.
2271da177e4SLinus Torvalds 	 */
228f696a365SMiklos Szeredi 	if (!(mask & MAY_EXEC) || execute_ok(inode))
2291da177e4SLinus Torvalds 		if (capable(CAP_DAC_OVERRIDE))
2301da177e4SLinus Torvalds 			return 0;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	/*
2331da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
2341da177e4SLinus Torvalds 	 */
2357ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
2361da177e4SLinus Torvalds 	if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
2371da177e4SLinus Torvalds 		if (capable(CAP_DAC_READ_SEARCH))
2381da177e4SLinus Torvalds 			return 0;
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	return -EACCES;
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds 
243cb23beb5SChristoph Hellwig /**
244cb23beb5SChristoph Hellwig  * inode_permission  -  check for access rights to a given inode
245cb23beb5SChristoph Hellwig  * @inode:	inode to check permission on
246cb23beb5SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
247cb23beb5SChristoph Hellwig  *
248cb23beb5SChristoph Hellwig  * Used to check for read/write/execute permissions on an inode.
249cb23beb5SChristoph Hellwig  * We use "fsuid" for this, letting us set arbitrary permissions
250cb23beb5SChristoph Hellwig  * for filesystem access without changing the "normal" uids which
251cb23beb5SChristoph Hellwig  * are used for other things.
252cb23beb5SChristoph Hellwig  */
253f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask)
2541da177e4SLinus Torvalds {
255e6305c43SAl Viro 	int retval;
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	if (mask & MAY_WRITE) {
25822590e41SMiklos Szeredi 		umode_t mode = inode->i_mode;
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 		/*
2611da177e4SLinus Torvalds 		 * Nobody gets write access to a read-only fs.
2621da177e4SLinus Torvalds 		 */
2631da177e4SLinus Torvalds 		if (IS_RDONLY(inode) &&
2641da177e4SLinus Torvalds 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
2651da177e4SLinus Torvalds 			return -EROFS;
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 		/*
2681da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
2691da177e4SLinus Torvalds 		 */
2701da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
2711da177e4SLinus Torvalds 			return -EACCES;
2721da177e4SLinus Torvalds 	}
2731da177e4SLinus Torvalds 
274acfa4380SAl Viro 	if (inode->i_op->permission)
275b77b0646SAl Viro 		retval = inode->i_op->permission(inode, mask);
276f696a365SMiklos Szeredi 	else
2775909ccaaSLinus Torvalds 		retval = generic_permission(inode, mask, inode->i_op->check_acl);
278f696a365SMiklos Szeredi 
2791da177e4SLinus Torvalds 	if (retval)
2801da177e4SLinus Torvalds 		return retval;
2811da177e4SLinus Torvalds 
28208ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
28308ce5f16SSerge E. Hallyn 	if (retval)
28408ce5f16SSerge E. Hallyn 		return retval;
28508ce5f16SSerge E. Hallyn 
286e6305c43SAl Viro 	return security_inode_permission(inode,
287f418b006SStephen Smalley 			mask & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND));
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds 
290e4543eddSChristoph Hellwig /**
2918c744fb8SChristoph Hellwig  * file_permission  -  check for additional access rights to a given file
2928c744fb8SChristoph Hellwig  * @file:	file to check access rights for
2938c744fb8SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
2948c744fb8SChristoph Hellwig  *
2958c744fb8SChristoph Hellwig  * Used to check for read/write/execute permissions on an already opened
2968c744fb8SChristoph Hellwig  * file.
2978c744fb8SChristoph Hellwig  *
2988c744fb8SChristoph Hellwig  * Note:
2998c744fb8SChristoph Hellwig  *	Do not use this function in new code.  All access checks should
300cb23beb5SChristoph Hellwig  *	be done using inode_permission().
3018c744fb8SChristoph Hellwig  */
3028c744fb8SChristoph Hellwig int file_permission(struct file *file, int mask)
3038c744fb8SChristoph Hellwig {
304f419a2e3SAl Viro 	return inode_permission(file->f_path.dentry->d_inode, mask);
3058c744fb8SChristoph Hellwig }
3068c744fb8SChristoph Hellwig 
3071da177e4SLinus Torvalds /*
3081da177e4SLinus Torvalds  * get_write_access() gets write permission for a file.
3091da177e4SLinus Torvalds  * put_write_access() releases this write permission.
3101da177e4SLinus Torvalds  * This is used for regular files.
3111da177e4SLinus Torvalds  * We cannot support write (and maybe mmap read-write shared) accesses and
3121da177e4SLinus Torvalds  * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
3131da177e4SLinus Torvalds  * can have the following values:
3141da177e4SLinus Torvalds  * 0: no writers, no VM_DENYWRITE mappings
3151da177e4SLinus Torvalds  * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
3161da177e4SLinus Torvalds  * > 0: (i_writecount) users are writing to the file.
3171da177e4SLinus Torvalds  *
3181da177e4SLinus Torvalds  * Normally we operate on that counter with atomic_{inc,dec} and it's safe
3191da177e4SLinus Torvalds  * except for the cases where we don't hold i_writecount yet. Then we need to
3201da177e4SLinus Torvalds  * use {get,deny}_write_access() - these functions check the sign and refuse
3211da177e4SLinus Torvalds  * to do the change if sign is wrong. Exclusion between them is provided by
3221da177e4SLinus Torvalds  * the inode->i_lock spinlock.
3231da177e4SLinus Torvalds  */
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds int get_write_access(struct inode * inode)
3261da177e4SLinus Torvalds {
3271da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
3281da177e4SLinus Torvalds 	if (atomic_read(&inode->i_writecount) < 0) {
3291da177e4SLinus Torvalds 		spin_unlock(&inode->i_lock);
3301da177e4SLinus Torvalds 		return -ETXTBSY;
3311da177e4SLinus Torvalds 	}
3321da177e4SLinus Torvalds 	atomic_inc(&inode->i_writecount);
3331da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	return 0;
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds int deny_write_access(struct file * file)
3391da177e4SLinus Torvalds {
3400f7fc9e4SJosef "Jeff" Sipek 	struct inode *inode = file->f_path.dentry->d_inode;
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
3431da177e4SLinus Torvalds 	if (atomic_read(&inode->i_writecount) > 0) {
3441da177e4SLinus Torvalds 		spin_unlock(&inode->i_lock);
3451da177e4SLinus Torvalds 		return -ETXTBSY;
3461da177e4SLinus Torvalds 	}
3471da177e4SLinus Torvalds 	atomic_dec(&inode->i_writecount);
3481da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	return 0;
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds 
3531d957f9bSJan Blunck /**
3545dd784d0SJan Blunck  * path_get - get a reference to a path
3555dd784d0SJan Blunck  * @path: path to get the reference to
3565dd784d0SJan Blunck  *
3575dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
3585dd784d0SJan Blunck  */
3595dd784d0SJan Blunck void path_get(struct path *path)
3605dd784d0SJan Blunck {
3615dd784d0SJan Blunck 	mntget(path->mnt);
3625dd784d0SJan Blunck 	dget(path->dentry);
3635dd784d0SJan Blunck }
3645dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
3655dd784d0SJan Blunck 
3665dd784d0SJan Blunck /**
3671d957f9bSJan Blunck  * path_put - put a reference to a path
3681d957f9bSJan Blunck  * @path: path to put the reference to
3691d957f9bSJan Blunck  *
3701d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
3711d957f9bSJan Blunck  */
3721d957f9bSJan Blunck void path_put(struct path *path)
3731da177e4SLinus Torvalds {
3741d957f9bSJan Blunck 	dput(path->dentry);
3751d957f9bSJan Blunck 	mntput(path->mnt);
3761da177e4SLinus Torvalds }
3771d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
3781da177e4SLinus Torvalds 
379834f2a4aSTrond Myklebust /**
380834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
381834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
382834f2a4aSTrond Myklebust  */
383834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
384834f2a4aSTrond Myklebust {
3850f7fc9e4SJosef "Jeff" Sipek 	if (nd->intent.open.file->f_path.dentry == NULL)
386834f2a4aSTrond Myklebust 		put_filp(nd->intent.open.file);
387834f2a4aSTrond Myklebust 	else
388834f2a4aSTrond Myklebust 		fput(nd->intent.open.file);
389834f2a4aSTrond Myklebust }
390834f2a4aSTrond Myklebust 
391bcdc5e01SIan Kent static inline struct dentry *
392bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd)
393bcdc5e01SIan Kent {
394bcdc5e01SIan Kent 	int status = dentry->d_op->d_revalidate(dentry, nd);
395bcdc5e01SIan Kent 	if (unlikely(status <= 0)) {
396bcdc5e01SIan Kent 		/*
397bcdc5e01SIan Kent 		 * The dentry failed validation.
398bcdc5e01SIan Kent 		 * If d_revalidate returned 0 attempt to invalidate
399bcdc5e01SIan Kent 		 * the dentry otherwise d_revalidate is asking us
400bcdc5e01SIan Kent 		 * to return a fail status.
401bcdc5e01SIan Kent 		 */
402bcdc5e01SIan Kent 		if (!status) {
403bcdc5e01SIan Kent 			if (!d_invalidate(dentry)) {
404bcdc5e01SIan Kent 				dput(dentry);
405bcdc5e01SIan Kent 				dentry = NULL;
406bcdc5e01SIan Kent 			}
407bcdc5e01SIan Kent 		} else {
408bcdc5e01SIan Kent 			dput(dentry);
409bcdc5e01SIan Kent 			dentry = ERR_PTR(status);
410bcdc5e01SIan Kent 		}
411bcdc5e01SIan Kent 	}
412bcdc5e01SIan Kent 	return dentry;
413bcdc5e01SIan Kent }
414bcdc5e01SIan Kent 
4151da177e4SLinus Torvalds /*
41639159de2SJeff Layton  * force_reval_path - force revalidation of a dentry
41739159de2SJeff Layton  *
41839159de2SJeff Layton  * In some situations the path walking code will trust dentries without
41939159de2SJeff Layton  * revalidating them. This causes problems for filesystems that depend on
42039159de2SJeff Layton  * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
42139159de2SJeff Layton  * (which indicates that it's possible for the dentry to go stale), force
42239159de2SJeff Layton  * a d_revalidate call before proceeding.
42339159de2SJeff Layton  *
42439159de2SJeff Layton  * Returns 0 if the revalidation was successful. If the revalidation fails,
42539159de2SJeff Layton  * either return the error returned by d_revalidate or -ESTALE if the
42639159de2SJeff Layton  * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
42739159de2SJeff Layton  * invalidate the dentry. It's up to the caller to handle putting references
42839159de2SJeff Layton  * to the path if necessary.
42939159de2SJeff Layton  */
43039159de2SJeff Layton static int
43139159de2SJeff Layton force_reval_path(struct path *path, struct nameidata *nd)
43239159de2SJeff Layton {
43339159de2SJeff Layton 	int status;
43439159de2SJeff Layton 	struct dentry *dentry = path->dentry;
43539159de2SJeff Layton 
43639159de2SJeff Layton 	/*
43739159de2SJeff Layton 	 * only check on filesystems where it's possible for the dentry to
43839159de2SJeff Layton 	 * become stale. It's assumed that if this flag is set then the
43939159de2SJeff Layton 	 * d_revalidate op will also be defined.
44039159de2SJeff Layton 	 */
44139159de2SJeff Layton 	if (!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))
44239159de2SJeff Layton 		return 0;
44339159de2SJeff Layton 
44439159de2SJeff Layton 	status = dentry->d_op->d_revalidate(dentry, nd);
44539159de2SJeff Layton 	if (status > 0)
44639159de2SJeff Layton 		return 0;
44739159de2SJeff Layton 
44839159de2SJeff Layton 	if (!status) {
44939159de2SJeff Layton 		d_invalidate(dentry);
45039159de2SJeff Layton 		status = -ESTALE;
45139159de2SJeff Layton 	}
45239159de2SJeff Layton 	return status;
45339159de2SJeff Layton }
45439159de2SJeff Layton 
45539159de2SJeff Layton /*
456b75b5086SAl Viro  * Short-cut version of permission(), for calling on directories
457b75b5086SAl Viro  * during pathname resolution.  Combines parts of permission()
458b75b5086SAl Viro  * and generic_permission(), and tests ONLY for MAY_EXEC permission.
4591da177e4SLinus Torvalds  *
4601da177e4SLinus Torvalds  * If appropriate, check DAC only.  If not appropriate, or
461b75b5086SAl Viro  * short-cut DAC fails, then call ->permission() to do more
4621da177e4SLinus Torvalds  * complete permission check.
4631da177e4SLinus Torvalds  */
464b75b5086SAl Viro static int exec_permission(struct inode *inode)
4651da177e4SLinus Torvalds {
4665909ccaaSLinus Torvalds 	int ret;
4671da177e4SLinus Torvalds 
468cb9179eaSLinus Torvalds 	if (inode->i_op->permission) {
4695909ccaaSLinus Torvalds 		ret = inode->i_op->permission(inode, MAY_EXEC);
470cb9179eaSLinus Torvalds 		if (!ret)
471cb9179eaSLinus Torvalds 			goto ok;
472cb9179eaSLinus Torvalds 		return ret;
473cb9179eaSLinus Torvalds 	}
4745909ccaaSLinus Torvalds 	ret = acl_permission_check(inode, MAY_EXEC, inode->i_op->check_acl);
4755909ccaaSLinus Torvalds 	if (!ret)
4761da177e4SLinus Torvalds 		goto ok;
4771da177e4SLinus Torvalds 
478f1ac9f6bSLinus Torvalds 	if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH))
4791da177e4SLinus Torvalds 		goto ok;
4801da177e4SLinus Torvalds 
4815909ccaaSLinus Torvalds 	return ret;
4821da177e4SLinus Torvalds ok:
483b77b0646SAl Viro 	return security_inode_permission(inode, MAY_EXEC);
4841da177e4SLinus Torvalds }
4851da177e4SLinus Torvalds 
4862a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
4872a737871SAl Viro {
4882a737871SAl Viro 	if (!nd->root.mnt) {
4892a737871SAl Viro 		struct fs_struct *fs = current->fs;
4902a737871SAl Viro 		read_lock(&fs->lock);
4912a737871SAl Viro 		nd->root = fs->root;
4922a737871SAl Viro 		path_get(&nd->root);
4932a737871SAl Viro 		read_unlock(&fs->lock);
4942a737871SAl Viro 	}
4952a737871SAl Viro }
4962a737871SAl Viro 
4976de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
4986de88d72SAl Viro 
499f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
5001da177e4SLinus Torvalds {
5011da177e4SLinus Torvalds 	if (IS_ERR(link))
5021da177e4SLinus Torvalds 		goto fail;
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	if (*link == '/') {
5052a737871SAl Viro 		set_root(nd);
5061d957f9bSJan Blunck 		path_put(&nd->path);
5072a737871SAl Viro 		nd->path = nd->root;
5082a737871SAl Viro 		path_get(&nd->root);
5091da177e4SLinus Torvalds 	}
510b4091d5fSChristoph Hellwig 
511def4af30SAl Viro 	return link_path_walk(link, nd);
5121da177e4SLinus Torvalds fail:
5131d957f9bSJan Blunck 	path_put(&nd->path);
5141da177e4SLinus Torvalds 	return PTR_ERR(link);
5151da177e4SLinus Torvalds }
5161da177e4SLinus Torvalds 
5171d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
518051d3812SIan Kent {
519051d3812SIan Kent 	dput(path->dentry);
5204ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
521051d3812SIan Kent 		mntput(path->mnt);
522051d3812SIan Kent }
523051d3812SIan Kent 
524051d3812SIan Kent static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
525051d3812SIan Kent {
5264ac91378SJan Blunck 	dput(nd->path.dentry);
5274ac91378SJan Blunck 	if (nd->path.mnt != path->mnt)
5284ac91378SJan Blunck 		mntput(nd->path.mnt);
5294ac91378SJan Blunck 	nd->path.mnt = path->mnt;
5304ac91378SJan Blunck 	nd->path.dentry = path->dentry;
531051d3812SIan Kent }
532051d3812SIan Kent 
533def4af30SAl Viro static __always_inline int
534def4af30SAl Viro __do_follow_link(struct path *path, struct nameidata *nd, void **p)
5351da177e4SLinus Torvalds {
5361da177e4SLinus Torvalds 	int error;
537cd4e91d3SAl Viro 	struct dentry *dentry = path->dentry;
5381da177e4SLinus Torvalds 
539d671a1cbSAl Viro 	touch_atime(path->mnt, dentry);
5401da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
541cd4e91d3SAl Viro 
5424ac91378SJan Blunck 	if (path->mnt != nd->path.mnt) {
543051d3812SIan Kent 		path_to_nameidata(path, nd);
544051d3812SIan Kent 		dget(dentry);
545051d3812SIan Kent 	}
546cd4e91d3SAl Viro 	mntget(path->mnt);
54786acdca1SAl Viro 	nd->last_type = LAST_BIND;
548def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
549def4af30SAl Viro 	error = PTR_ERR(*p);
550def4af30SAl Viro 	if (!IS_ERR(*p)) {
5511da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
552cc314eefSLinus Torvalds 		error = 0;
5531da177e4SLinus Torvalds 		if (s)
5541da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
55539159de2SJeff Layton 		else if (nd->last_type == LAST_BIND) {
55639159de2SJeff Layton 			error = force_reval_path(&nd->path, nd);
55739159de2SJeff Layton 			if (error)
55839159de2SJeff Layton 				path_put(&nd->path);
55939159de2SJeff Layton 		}
5601da177e4SLinus Torvalds 	}
5611da177e4SLinus Torvalds 	return error;
5621da177e4SLinus Torvalds }
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds /*
5651da177e4SLinus Torvalds  * This limits recursive symlink follows to 8, while
5661da177e4SLinus Torvalds  * limiting consecutive symlinks to 40.
5671da177e4SLinus Torvalds  *
5681da177e4SLinus Torvalds  * Without that kind of total limit, nasty chains of consecutive
5691da177e4SLinus Torvalds  * symlinks can cause almost arbitrarily long lookups.
5701da177e4SLinus Torvalds  */
57190ebe565SAl Viro static inline int do_follow_link(struct path *path, struct nameidata *nd)
5721da177e4SLinus Torvalds {
573def4af30SAl Viro 	void *cookie;
5741da177e4SLinus Torvalds 	int err = -ELOOP;
5751da177e4SLinus Torvalds 	if (current->link_count >= MAX_NESTED_LINKS)
5761da177e4SLinus Torvalds 		goto loop;
5771da177e4SLinus Torvalds 	if (current->total_link_count >= 40)
5781da177e4SLinus Torvalds 		goto loop;
5791da177e4SLinus Torvalds 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
5801da177e4SLinus Torvalds 	cond_resched();
58190ebe565SAl Viro 	err = security_inode_follow_link(path->dentry, nd);
5821da177e4SLinus Torvalds 	if (err)
5831da177e4SLinus Torvalds 		goto loop;
5841da177e4SLinus Torvalds 	current->link_count++;
5851da177e4SLinus Torvalds 	current->total_link_count++;
5861da177e4SLinus Torvalds 	nd->depth++;
587def4af30SAl Viro 	err = __do_follow_link(path, nd, &cookie);
588def4af30SAl Viro 	if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link)
589def4af30SAl Viro 		path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie);
590258fa999SAl Viro 	path_put(path);
5911da177e4SLinus Torvalds 	current->link_count--;
5921da177e4SLinus Torvalds 	nd->depth--;
5931da177e4SLinus Torvalds 	return err;
5941da177e4SLinus Torvalds loop:
5951d957f9bSJan Blunck 	path_put_conditional(path, nd);
5961d957f9bSJan Blunck 	path_put(&nd->path);
5971da177e4SLinus Torvalds 	return err;
5981da177e4SLinus Torvalds }
5991da177e4SLinus Torvalds 
600bab77ebfSAl Viro int follow_up(struct path *path)
6011da177e4SLinus Torvalds {
6021da177e4SLinus Torvalds 	struct vfsmount *parent;
6031da177e4SLinus Torvalds 	struct dentry *mountpoint;
6041da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
605bab77ebfSAl Viro 	parent = path->mnt->mnt_parent;
606bab77ebfSAl Viro 	if (parent == path->mnt) {
6071da177e4SLinus Torvalds 		spin_unlock(&vfsmount_lock);
6081da177e4SLinus Torvalds 		return 0;
6091da177e4SLinus Torvalds 	}
6101da177e4SLinus Torvalds 	mntget(parent);
611bab77ebfSAl Viro 	mountpoint = dget(path->mnt->mnt_mountpoint);
6121da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
613bab77ebfSAl Viro 	dput(path->dentry);
614bab77ebfSAl Viro 	path->dentry = mountpoint;
615bab77ebfSAl Viro 	mntput(path->mnt);
616bab77ebfSAl Viro 	path->mnt = parent;
6171da177e4SLinus Torvalds 	return 1;
6181da177e4SLinus Torvalds }
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds /* no need for dcache_lock, as serialization is taken care in
6211da177e4SLinus Torvalds  * namespace.c
6221da177e4SLinus Torvalds  */
623463ffb2eSAl Viro static int __follow_mount(struct path *path)
624463ffb2eSAl Viro {
625463ffb2eSAl Viro 	int res = 0;
626463ffb2eSAl Viro 	while (d_mountpoint(path->dentry)) {
6271c755af4SAl Viro 		struct vfsmount *mounted = lookup_mnt(path);
628463ffb2eSAl Viro 		if (!mounted)
629463ffb2eSAl Viro 			break;
630463ffb2eSAl Viro 		dput(path->dentry);
631463ffb2eSAl Viro 		if (res)
632463ffb2eSAl Viro 			mntput(path->mnt);
633463ffb2eSAl Viro 		path->mnt = mounted;
634463ffb2eSAl Viro 		path->dentry = dget(mounted->mnt_root);
635463ffb2eSAl Viro 		res = 1;
636463ffb2eSAl Viro 	}
637463ffb2eSAl Viro 	return res;
638463ffb2eSAl Viro }
639463ffb2eSAl Viro 
64079ed0226SAl Viro static void follow_mount(struct path *path)
6411da177e4SLinus Torvalds {
64279ed0226SAl Viro 	while (d_mountpoint(path->dentry)) {
6431c755af4SAl Viro 		struct vfsmount *mounted = lookup_mnt(path);
6441da177e4SLinus Torvalds 		if (!mounted)
6451da177e4SLinus Torvalds 			break;
64679ed0226SAl Viro 		dput(path->dentry);
64779ed0226SAl Viro 		mntput(path->mnt);
64879ed0226SAl Viro 		path->mnt = mounted;
64979ed0226SAl Viro 		path->dentry = dget(mounted->mnt_root);
6501da177e4SLinus Torvalds 	}
6511da177e4SLinus Torvalds }
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds /* no need for dcache_lock, as serialization is taken care in
6541da177e4SLinus Torvalds  * namespace.c
6551da177e4SLinus Torvalds  */
6569393bd07SAl Viro int follow_down(struct path *path)
6571da177e4SLinus Torvalds {
6581da177e4SLinus Torvalds 	struct vfsmount *mounted;
6591da177e4SLinus Torvalds 
6601c755af4SAl Viro 	mounted = lookup_mnt(path);
6611da177e4SLinus Torvalds 	if (mounted) {
6629393bd07SAl Viro 		dput(path->dentry);
6639393bd07SAl Viro 		mntput(path->mnt);
6649393bd07SAl Viro 		path->mnt = mounted;
6659393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
6661da177e4SLinus Torvalds 		return 1;
6671da177e4SLinus Torvalds 	}
6681da177e4SLinus Torvalds 	return 0;
6691da177e4SLinus Torvalds }
6701da177e4SLinus Torvalds 
671f1662356SArjan van de Ven static __always_inline void follow_dotdot(struct nameidata *nd)
6721da177e4SLinus Torvalds {
6732a737871SAl Viro 	set_root(nd);
674e518ddb7SAndreas Mohr 
6751da177e4SLinus Torvalds 	while(1) {
6764ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
6771da177e4SLinus Torvalds 
6782a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
6792a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
6801da177e4SLinus Torvalds 			break;
6811da177e4SLinus Torvalds 		}
6824ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
6833088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
6843088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
6851da177e4SLinus Torvalds 			dput(old);
6861da177e4SLinus Torvalds 			break;
6871da177e4SLinus Torvalds 		}
6883088dd70SAl Viro 		if (!follow_up(&nd->path))
6891da177e4SLinus Torvalds 			break;
6901da177e4SLinus Torvalds 	}
69179ed0226SAl Viro 	follow_mount(&nd->path);
6921da177e4SLinus Torvalds }
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds /*
6951da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
6961da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
6971da177e4SLinus Torvalds  *  It _is_ time-critical.
6981da177e4SLinus Torvalds  */
6991da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
7001da177e4SLinus Torvalds 		     struct path *path)
7011da177e4SLinus Torvalds {
7024ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
7036e6b1bd1SAl Viro 	struct dentry *dentry, *parent;
7046e6b1bd1SAl Viro 	struct inode *dir;
7053cac260aSAl Viro 	/*
7063cac260aSAl Viro 	 * See if the low-level filesystem might want
7073cac260aSAl Viro 	 * to use its own hash..
7083cac260aSAl Viro 	 */
7093cac260aSAl Viro 	if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
7103cac260aSAl Viro 		int err = nd->path.dentry->d_op->d_hash(nd->path.dentry, name);
7113cac260aSAl Viro 		if (err < 0)
7123cac260aSAl Viro 			return err;
7133cac260aSAl Viro 	}
7141da177e4SLinus Torvalds 
7153cac260aSAl Viro 	dentry = __d_lookup(nd->path.dentry, name);
7161da177e4SLinus Torvalds 	if (!dentry)
7171da177e4SLinus Torvalds 		goto need_lookup;
7181da177e4SLinus Torvalds 	if (dentry->d_op && dentry->d_op->d_revalidate)
7191da177e4SLinus Torvalds 		goto need_revalidate;
7201da177e4SLinus Torvalds done:
7211da177e4SLinus Torvalds 	path->mnt = mnt;
7221da177e4SLinus Torvalds 	path->dentry = dentry;
723634ee701SAl Viro 	__follow_mount(path);
7241da177e4SLinus Torvalds 	return 0;
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds need_lookup:
7276e6b1bd1SAl Viro 	parent = nd->path.dentry;
7286e6b1bd1SAl Viro 	dir = parent->d_inode;
7296e6b1bd1SAl Viro 
7306e6b1bd1SAl Viro 	mutex_lock(&dir->i_mutex);
7316e6b1bd1SAl Viro 	/*
7326e6b1bd1SAl Viro 	 * First re-do the cached lookup just in case it was created
7336e6b1bd1SAl Viro 	 * while we waited for the directory semaphore..
7346e6b1bd1SAl Viro 	 *
7356e6b1bd1SAl Viro 	 * FIXME! This could use version numbering or similar to
7366e6b1bd1SAl Viro 	 * avoid unnecessary cache lookups.
7376e6b1bd1SAl Viro 	 *
7386e6b1bd1SAl Viro 	 * The "dcache_lock" is purely to protect the RCU list walker
7396e6b1bd1SAl Viro 	 * from concurrent renames at this point (we mustn't get false
7406e6b1bd1SAl Viro 	 * negatives from the RCU list walk here, unlike the optimistic
7416e6b1bd1SAl Viro 	 * fast walk).
7426e6b1bd1SAl Viro 	 *
7436e6b1bd1SAl Viro 	 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
7446e6b1bd1SAl Viro 	 */
7456e6b1bd1SAl Viro 	dentry = d_lookup(parent, name);
7466e6b1bd1SAl Viro 	if (!dentry) {
7476e6b1bd1SAl Viro 		struct dentry *new;
7486e6b1bd1SAl Viro 
7496e6b1bd1SAl Viro 		/* Don't create child dentry for a dead directory. */
7506e6b1bd1SAl Viro 		dentry = ERR_PTR(-ENOENT);
7516e6b1bd1SAl Viro 		if (IS_DEADDIR(dir))
7526e6b1bd1SAl Viro 			goto out_unlock;
7536e6b1bd1SAl Viro 
7546e6b1bd1SAl Viro 		new = d_alloc(parent, name);
7556e6b1bd1SAl Viro 		dentry = ERR_PTR(-ENOMEM);
7566e6b1bd1SAl Viro 		if (new) {
7576e6b1bd1SAl Viro 			dentry = dir->i_op->lookup(dir, new, nd);
7586e6b1bd1SAl Viro 			if (dentry)
7596e6b1bd1SAl Viro 				dput(new);
7606e6b1bd1SAl Viro 			else
7616e6b1bd1SAl Viro 				dentry = new;
7626e6b1bd1SAl Viro 		}
7636e6b1bd1SAl Viro out_unlock:
7646e6b1bd1SAl Viro 		mutex_unlock(&dir->i_mutex);
7656e6b1bd1SAl Viro 		if (IS_ERR(dentry))
7666e6b1bd1SAl Viro 			goto fail;
7676e6b1bd1SAl Viro 		goto done;
7686e6b1bd1SAl Viro 	}
7696e6b1bd1SAl Viro 
7706e6b1bd1SAl Viro 	/*
7716e6b1bd1SAl Viro 	 * Uhhuh! Nasty case: the cache was re-populated while
7726e6b1bd1SAl Viro 	 * we waited on the semaphore. Need to revalidate.
7736e6b1bd1SAl Viro 	 */
7746e6b1bd1SAl Viro 	mutex_unlock(&dir->i_mutex);
7756e6b1bd1SAl Viro 	if (dentry->d_op && dentry->d_op->d_revalidate) {
7766e6b1bd1SAl Viro 		dentry = do_revalidate(dentry, nd);
7776e6b1bd1SAl Viro 		if (!dentry)
7786e6b1bd1SAl Viro 			dentry = ERR_PTR(-ENOENT);
7796e6b1bd1SAl Viro 	}
7801da177e4SLinus Torvalds 	if (IS_ERR(dentry))
7811da177e4SLinus Torvalds 		goto fail;
7821da177e4SLinus Torvalds 	goto done;
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds need_revalidate:
785bcdc5e01SIan Kent 	dentry = do_revalidate(dentry, nd);
786bcdc5e01SIan Kent 	if (!dentry)
7871da177e4SLinus Torvalds 		goto need_lookup;
788bcdc5e01SIan Kent 	if (IS_ERR(dentry))
789bcdc5e01SIan Kent 		goto fail;
790bcdc5e01SIan Kent 	goto done;
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds fail:
7931da177e4SLinus Torvalds 	return PTR_ERR(dentry);
7941da177e4SLinus Torvalds }
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds /*
797ac278a9cSAl Viro  * This is a temporary kludge to deal with "automount" symlinks; proper
798ac278a9cSAl Viro  * solution is to trigger them on follow_mount(), so that do_lookup()
799ac278a9cSAl Viro  * would DTRT.  To be killed before 2.6.34-final.
800ac278a9cSAl Viro  */
801ac278a9cSAl Viro static inline int follow_on_final(struct inode *inode, unsigned lookup_flags)
802ac278a9cSAl Viro {
803ac278a9cSAl Viro 	return inode && unlikely(inode->i_op->follow_link) &&
804ac278a9cSAl Viro 		((lookup_flags & LOOKUP_FOLLOW) || S_ISDIR(inode->i_mode));
805ac278a9cSAl Viro }
806ac278a9cSAl Viro 
807ac278a9cSAl Viro /*
8081da177e4SLinus Torvalds  * Name resolution.
809ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
810ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
8111da177e4SLinus Torvalds  *
812ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
813ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
8141da177e4SLinus Torvalds  */
8156de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
8161da177e4SLinus Torvalds {
8171da177e4SLinus Torvalds 	struct path next;
8181da177e4SLinus Torvalds 	struct inode *inode;
8191da177e4SLinus Torvalds 	int err;
8201da177e4SLinus Torvalds 	unsigned int lookup_flags = nd->flags;
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds 	while (*name=='/')
8231da177e4SLinus Torvalds 		name++;
8241da177e4SLinus Torvalds 	if (!*name)
8251da177e4SLinus Torvalds 		goto return_reval;
8261da177e4SLinus Torvalds 
8274ac91378SJan Blunck 	inode = nd->path.dentry->d_inode;
8281da177e4SLinus Torvalds 	if (nd->depth)
829f55eab82STrond Myklebust 		lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
8321da177e4SLinus Torvalds 	for(;;) {
8331da177e4SLinus Torvalds 		unsigned long hash;
8341da177e4SLinus Torvalds 		struct qstr this;
8351da177e4SLinus Torvalds 		unsigned int c;
8361da177e4SLinus Torvalds 
837cdce5d6bSTrond Myklebust 		nd->flags |= LOOKUP_CONTINUE;
838b75b5086SAl Viro 		err = exec_permission(inode);
8391da177e4SLinus Torvalds  		if (err)
8401da177e4SLinus Torvalds 			break;
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 		this.name = name;
8431da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 		hash = init_name_hash();
8461da177e4SLinus Torvalds 		do {
8471da177e4SLinus Torvalds 			name++;
8481da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
8491da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
8501da177e4SLinus Torvalds 		} while (c && (c != '/'));
8511da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
8521da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 		/* remove trailing slashes? */
8551da177e4SLinus Torvalds 		if (!c)
8561da177e4SLinus Torvalds 			goto last_component;
8571da177e4SLinus Torvalds 		while (*++name == '/');
8581da177e4SLinus Torvalds 		if (!*name)
8591da177e4SLinus Torvalds 			goto last_with_slashes;
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 		/*
8621da177e4SLinus Torvalds 		 * "." and ".." are special - ".." especially so because it has
8631da177e4SLinus Torvalds 		 * to be able to know about the current root directory and
8641da177e4SLinus Torvalds 		 * parent relationships.
8651da177e4SLinus Torvalds 		 */
8661da177e4SLinus Torvalds 		if (this.name[0] == '.') switch (this.len) {
8671da177e4SLinus Torvalds 			default:
8681da177e4SLinus Torvalds 				break;
8691da177e4SLinus Torvalds 			case 2:
8701da177e4SLinus Torvalds 				if (this.name[1] != '.')
8711da177e4SLinus Torvalds 					break;
87258c465ebSAl Viro 				follow_dotdot(nd);
8734ac91378SJan Blunck 				inode = nd->path.dentry->d_inode;
8741da177e4SLinus Torvalds 				/* fallthrough */
8751da177e4SLinus Torvalds 			case 1:
8761da177e4SLinus Torvalds 				continue;
8771da177e4SLinus Torvalds 		}
8781da177e4SLinus Torvalds 		/* This does the actual lookups.. */
8791da177e4SLinus Torvalds 		err = do_lookup(nd, &this, &next);
8801da177e4SLinus Torvalds 		if (err)
8811da177e4SLinus Torvalds 			break;
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 		err = -ENOENT;
8841da177e4SLinus Torvalds 		inode = next.dentry->d_inode;
8851da177e4SLinus Torvalds 		if (!inode)
8861da177e4SLinus Torvalds 			goto out_dput;
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 		if (inode->i_op->follow_link) {
88990ebe565SAl Viro 			err = do_follow_link(&next, nd);
8901da177e4SLinus Torvalds 			if (err)
8911da177e4SLinus Torvalds 				goto return_err;
8921da177e4SLinus Torvalds 			err = -ENOENT;
8934ac91378SJan Blunck 			inode = nd->path.dentry->d_inode;
8941da177e4SLinus Torvalds 			if (!inode)
8951da177e4SLinus Torvalds 				break;
89609dd17d3SMiklos Szeredi 		} else
89709dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
8981da177e4SLinus Torvalds 		err = -ENOTDIR;
8991da177e4SLinus Torvalds 		if (!inode->i_op->lookup)
9001da177e4SLinus Torvalds 			break;
9011da177e4SLinus Torvalds 		continue;
9021da177e4SLinus Torvalds 		/* here ends the main loop */
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds last_with_slashes:
9051da177e4SLinus Torvalds 		lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
9061da177e4SLinus Torvalds last_component:
907f55eab82STrond Myklebust 		/* Clear LOOKUP_CONTINUE iff it was previously unset */
908f55eab82STrond Myklebust 		nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
9091da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_PARENT)
9101da177e4SLinus Torvalds 			goto lookup_parent;
9111da177e4SLinus Torvalds 		if (this.name[0] == '.') switch (this.len) {
9121da177e4SLinus Torvalds 			default:
9131da177e4SLinus Torvalds 				break;
9141da177e4SLinus Torvalds 			case 2:
9151da177e4SLinus Torvalds 				if (this.name[1] != '.')
9161da177e4SLinus Torvalds 					break;
91758c465ebSAl Viro 				follow_dotdot(nd);
9184ac91378SJan Blunck 				inode = nd->path.dentry->d_inode;
9191da177e4SLinus Torvalds 				/* fallthrough */
9201da177e4SLinus Torvalds 			case 1:
9211da177e4SLinus Torvalds 				goto return_reval;
9221da177e4SLinus Torvalds 		}
9231da177e4SLinus Torvalds 		err = do_lookup(nd, &this, &next);
9241da177e4SLinus Torvalds 		if (err)
9251da177e4SLinus Torvalds 			break;
9261da177e4SLinus Torvalds 		inode = next.dentry->d_inode;
927ac278a9cSAl Viro 		if (follow_on_final(inode, lookup_flags)) {
92890ebe565SAl Viro 			err = do_follow_link(&next, nd);
9291da177e4SLinus Torvalds 			if (err)
9301da177e4SLinus Torvalds 				goto return_err;
9314ac91378SJan Blunck 			inode = nd->path.dentry->d_inode;
93209dd17d3SMiklos Szeredi 		} else
93309dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
9341da177e4SLinus Torvalds 		err = -ENOENT;
9351da177e4SLinus Torvalds 		if (!inode)
9361da177e4SLinus Torvalds 			break;
9371da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_DIRECTORY) {
9381da177e4SLinus Torvalds 			err = -ENOTDIR;
939acfa4380SAl Viro 			if (!inode->i_op->lookup)
9401da177e4SLinus Torvalds 				break;
9411da177e4SLinus Torvalds 		}
9421da177e4SLinus Torvalds 		goto return_base;
9431da177e4SLinus Torvalds lookup_parent:
9441da177e4SLinus Torvalds 		nd->last = this;
9451da177e4SLinus Torvalds 		nd->last_type = LAST_NORM;
9461da177e4SLinus Torvalds 		if (this.name[0] != '.')
9471da177e4SLinus Torvalds 			goto return_base;
9481da177e4SLinus Torvalds 		if (this.len == 1)
9491da177e4SLinus Torvalds 			nd->last_type = LAST_DOT;
9501da177e4SLinus Torvalds 		else if (this.len == 2 && this.name[1] == '.')
9511da177e4SLinus Torvalds 			nd->last_type = LAST_DOTDOT;
9521da177e4SLinus Torvalds 		else
9531da177e4SLinus Torvalds 			goto return_base;
9541da177e4SLinus Torvalds return_reval:
9551da177e4SLinus Torvalds 		/*
9561da177e4SLinus Torvalds 		 * We bypassed the ordinary revalidation routines.
9571da177e4SLinus Torvalds 		 * We may need to check the cached dentry for staleness.
9581da177e4SLinus Torvalds 		 */
9594ac91378SJan Blunck 		if (nd->path.dentry && nd->path.dentry->d_sb &&
9604ac91378SJan Blunck 		    (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
9611da177e4SLinus Torvalds 			err = -ESTALE;
9621da177e4SLinus Torvalds 			/* Note: we do not d_invalidate() */
9634ac91378SJan Blunck 			if (!nd->path.dentry->d_op->d_revalidate(
9644ac91378SJan Blunck 					nd->path.dentry, nd))
9651da177e4SLinus Torvalds 				break;
9661da177e4SLinus Torvalds 		}
9671da177e4SLinus Torvalds return_base:
9681da177e4SLinus Torvalds 		return 0;
9691da177e4SLinus Torvalds out_dput:
9701d957f9bSJan Blunck 		path_put_conditional(&next, nd);
9711da177e4SLinus Torvalds 		break;
9721da177e4SLinus Torvalds 	}
9731d957f9bSJan Blunck 	path_put(&nd->path);
9741da177e4SLinus Torvalds return_err:
9751da177e4SLinus Torvalds 	return err;
9761da177e4SLinus Torvalds }
9771da177e4SLinus Torvalds 
978fc9b52cdSHarvey Harrison static int path_walk(const char *name, struct nameidata *nd)
9791da177e4SLinus Torvalds {
9806de88d72SAl Viro 	struct path save = nd->path;
9816de88d72SAl Viro 	int result;
9826de88d72SAl Viro 
9831da177e4SLinus Torvalds 	current->total_link_count = 0;
9846de88d72SAl Viro 
9856de88d72SAl Viro 	/* make sure the stuff we saved doesn't go away */
9866de88d72SAl Viro 	path_get(&save);
9876de88d72SAl Viro 
9886de88d72SAl Viro 	result = link_path_walk(name, nd);
9896de88d72SAl Viro 	if (result == -ESTALE) {
9906de88d72SAl Viro 		/* nd->path had been dropped */
9916de88d72SAl Viro 		current->total_link_count = 0;
9926de88d72SAl Viro 		nd->path = save;
9936de88d72SAl Viro 		path_get(&nd->path);
9946de88d72SAl Viro 		nd->flags |= LOOKUP_REVAL;
9956de88d72SAl Viro 		result = link_path_walk(name, nd);
9966de88d72SAl Viro 	}
9976de88d72SAl Viro 
9986de88d72SAl Viro 	path_put(&save);
9996de88d72SAl Viro 
10006de88d72SAl Viro 	return result;
10011da177e4SLinus Torvalds }
10021da177e4SLinus Torvalds 
10039b4a9b14SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
10041da177e4SLinus Torvalds {
1005ea3834d9SPrasanna Meda 	int retval = 0;
1006170aa3d0SUlrich Drepper 	int fput_needed;
1007170aa3d0SUlrich Drepper 	struct file *file;
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
10101da177e4SLinus Torvalds 	nd->flags = flags;
10111da177e4SLinus Torvalds 	nd->depth = 0;
10122a737871SAl Viro 	nd->root.mnt = NULL;
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds 	if (*name=='/') {
10152a737871SAl Viro 		set_root(nd);
10162a737871SAl Viro 		nd->path = nd->root;
10172a737871SAl Viro 		path_get(&nd->root);
10185590ff0dSUlrich Drepper 	} else if (dfd == AT_FDCWD) {
10192a737871SAl Viro 		struct fs_struct *fs = current->fs;
1020e518ddb7SAndreas Mohr 		read_lock(&fs->lock);
10216ac08c39SJan Blunck 		nd->path = fs->pwd;
10226ac08c39SJan Blunck 		path_get(&fs->pwd);
1023e518ddb7SAndreas Mohr 		read_unlock(&fs->lock);
10245590ff0dSUlrich Drepper 	} else {
10255590ff0dSUlrich Drepper 		struct dentry *dentry;
10265590ff0dSUlrich Drepper 
10275590ff0dSUlrich Drepper 		file = fget_light(dfd, &fput_needed);
10285590ff0dSUlrich Drepper 		retval = -EBADF;
1029170aa3d0SUlrich Drepper 		if (!file)
10306d09bb62STrond Myklebust 			goto out_fail;
10315590ff0dSUlrich Drepper 
10320f7fc9e4SJosef "Jeff" Sipek 		dentry = file->f_path.dentry;
10335590ff0dSUlrich Drepper 
10345590ff0dSUlrich Drepper 		retval = -ENOTDIR;
1035170aa3d0SUlrich Drepper 		if (!S_ISDIR(dentry->d_inode->i_mode))
10366d09bb62STrond Myklebust 			goto fput_fail;
10375590ff0dSUlrich Drepper 
10385590ff0dSUlrich Drepper 		retval = file_permission(file, MAY_EXEC);
1039170aa3d0SUlrich Drepper 		if (retval)
10406d09bb62STrond Myklebust 			goto fput_fail;
10415590ff0dSUlrich Drepper 
10425dd784d0SJan Blunck 		nd->path = file->f_path;
10435dd784d0SJan Blunck 		path_get(&file->f_path);
10445590ff0dSUlrich Drepper 
10455590ff0dSUlrich Drepper 		fput_light(file, fput_needed);
10461da177e4SLinus Torvalds 	}
10479b4a9b14SAl Viro 	return 0;
10482dfdd266SJosef 'Jeff' Sipek 
10499b4a9b14SAl Viro fput_fail:
10509b4a9b14SAl Viro 	fput_light(file, fput_needed);
10519b4a9b14SAl Viro out_fail:
10529b4a9b14SAl Viro 	return retval;
10539b4a9b14SAl Viro }
10549b4a9b14SAl Viro 
10559b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
10569b4a9b14SAl Viro static int do_path_lookup(int dfd, const char *name,
10579b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
10589b4a9b14SAl Viro {
10599b4a9b14SAl Viro 	int retval = path_init(dfd, name, flags, nd);
10609b4a9b14SAl Viro 	if (!retval)
10612dfdd266SJosef 'Jeff' Sipek 		retval = path_walk(name, nd);
10624ac91378SJan Blunck 	if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
10634ac91378SJan Blunck 				nd->path.dentry->d_inode))
10644ac91378SJan Blunck 		audit_inode(name, nd->path.dentry);
10652a737871SAl Viro 	if (nd->root.mnt) {
10662a737871SAl Viro 		path_put(&nd->root);
10672a737871SAl Viro 		nd->root.mnt = NULL;
10682a737871SAl Viro 	}
1069170aa3d0SUlrich Drepper 	return retval;
10701da177e4SLinus Torvalds }
10711da177e4SLinus Torvalds 
1072fc9b52cdSHarvey Harrison int path_lookup(const char *name, unsigned int flags,
10735590ff0dSUlrich Drepper 			struct nameidata *nd)
10745590ff0dSUlrich Drepper {
10755590ff0dSUlrich Drepper 	return do_path_lookup(AT_FDCWD, name, flags, nd);
10765590ff0dSUlrich Drepper }
10775590ff0dSUlrich Drepper 
1078d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1079d1811465SAl Viro {
1080d1811465SAl Viro 	struct nameidata nd;
1081d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1082d1811465SAl Viro 	if (!res)
1083d1811465SAl Viro 		*path = nd.path;
1084d1811465SAl Viro 	return res;
1085d1811465SAl Viro }
1086d1811465SAl Viro 
108716f18200SJosef 'Jeff' Sipek /**
108816f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
108916f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
109016f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
109116f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
109216f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
109316f18200SJosef 'Jeff' Sipek  * @nd: pointer to nameidata
109416f18200SJosef 'Jeff' Sipek  */
109516f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
109616f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
109716f18200SJosef 'Jeff' Sipek 		    struct nameidata *nd)
109816f18200SJosef 'Jeff' Sipek {
109916f18200SJosef 'Jeff' Sipek 	int retval;
110016f18200SJosef 'Jeff' Sipek 
110116f18200SJosef 'Jeff' Sipek 	/* same as do_path_lookup */
110216f18200SJosef 'Jeff' Sipek 	nd->last_type = LAST_ROOT;
110316f18200SJosef 'Jeff' Sipek 	nd->flags = flags;
110416f18200SJosef 'Jeff' Sipek 	nd->depth = 0;
110516f18200SJosef 'Jeff' Sipek 
1106c8e7f449SJan Blunck 	nd->path.dentry = dentry;
1107c8e7f449SJan Blunck 	nd->path.mnt = mnt;
1108c8e7f449SJan Blunck 	path_get(&nd->path);
11095b857119SAl Viro 	nd->root = nd->path;
11105b857119SAl Viro 	path_get(&nd->root);
111116f18200SJosef 'Jeff' Sipek 
111216f18200SJosef 'Jeff' Sipek 	retval = path_walk(name, nd);
11134ac91378SJan Blunck 	if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
11144ac91378SJan Blunck 				nd->path.dentry->d_inode))
11154ac91378SJan Blunck 		audit_inode(name, nd->path.dentry);
111616f18200SJosef 'Jeff' Sipek 
11172a737871SAl Viro 	path_put(&nd->root);
11182a737871SAl Viro 	nd->root.mnt = NULL;
111916f18200SJosef 'Jeff' Sipek 
11202a737871SAl Viro 	return retval;
112116f18200SJosef 'Jeff' Sipek }
112216f18200SJosef 'Jeff' Sipek 
1123eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1124eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
11251da177e4SLinus Torvalds {
11261da177e4SLinus Torvalds 	struct dentry *dentry;
11271da177e4SLinus Torvalds 	struct inode *inode;
11281da177e4SLinus Torvalds 	int err;
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds 	inode = base->d_inode;
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds 	/*
11331da177e4SLinus Torvalds 	 * See if the low-level filesystem might want
11341da177e4SLinus Torvalds 	 * to use its own hash..
11351da177e4SLinus Torvalds 	 */
11361da177e4SLinus Torvalds 	if (base->d_op && base->d_op->d_hash) {
11371da177e4SLinus Torvalds 		err = base->d_op->d_hash(base, name);
11381da177e4SLinus Torvalds 		dentry = ERR_PTR(err);
11391da177e4SLinus Torvalds 		if (err < 0)
11401da177e4SLinus Torvalds 			goto out;
11411da177e4SLinus Torvalds 	}
11421da177e4SLinus Torvalds 
11436e6b1bd1SAl Viro 	dentry = __d_lookup(base, name);
11446e6b1bd1SAl Viro 
11456e6b1bd1SAl Viro 	/* lockess __d_lookup may fail due to concurrent d_move()
11466e6b1bd1SAl Viro 	 * in some unrelated directory, so try with d_lookup
11476e6b1bd1SAl Viro 	 */
11486e6b1bd1SAl Viro 	if (!dentry)
11496e6b1bd1SAl Viro 		dentry = d_lookup(base, name);
11506e6b1bd1SAl Viro 
11516e6b1bd1SAl Viro 	if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
11526e6b1bd1SAl Viro 		dentry = do_revalidate(dentry, nd);
11536e6b1bd1SAl Viro 
11541da177e4SLinus Torvalds 	if (!dentry) {
1155d70b67c8SMiklos Szeredi 		struct dentry *new;
1156d70b67c8SMiklos Szeredi 
1157d70b67c8SMiklos Szeredi 		/* Don't create child dentry for a dead directory. */
1158d70b67c8SMiklos Szeredi 		dentry = ERR_PTR(-ENOENT);
1159d70b67c8SMiklos Szeredi 		if (IS_DEADDIR(inode))
1160d70b67c8SMiklos Szeredi 			goto out;
1161d70b67c8SMiklos Szeredi 
1162d70b67c8SMiklos Szeredi 		new = d_alloc(base, name);
11631da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOMEM);
11641da177e4SLinus Torvalds 		if (!new)
11651da177e4SLinus Torvalds 			goto out;
11661da177e4SLinus Torvalds 		dentry = inode->i_op->lookup(inode, new, nd);
11671da177e4SLinus Torvalds 		if (!dentry)
11681da177e4SLinus Torvalds 			dentry = new;
11691da177e4SLinus Torvalds 		else
11701da177e4SLinus Torvalds 			dput(new);
11711da177e4SLinus Torvalds 	}
11721da177e4SLinus Torvalds out:
11731da177e4SLinus Torvalds 	return dentry;
11741da177e4SLinus Torvalds }
11751da177e4SLinus Torvalds 
1176057f6c01SJames Morris /*
1177057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1178057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1179057f6c01SJames Morris  * SMP-safe.
1180057f6c01SJames Morris  */
1181a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
11821da177e4SLinus Torvalds {
1183eead1911SChristoph Hellwig 	int err;
1184eead1911SChristoph Hellwig 
1185b75b5086SAl Viro 	err = exec_permission(nd->path.dentry->d_inode);
1186eead1911SChristoph Hellwig 	if (err)
1187eead1911SChristoph Hellwig 		return ERR_PTR(err);
11884ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
11891da177e4SLinus Torvalds }
11901da177e4SLinus Torvalds 
1191eead1911SChristoph Hellwig static int __lookup_one_len(const char *name, struct qstr *this,
1192eead1911SChristoph Hellwig 		struct dentry *base, int len)
11931da177e4SLinus Torvalds {
11941da177e4SLinus Torvalds 	unsigned long hash;
11951da177e4SLinus Torvalds 	unsigned int c;
11961da177e4SLinus Torvalds 
1197057f6c01SJames Morris 	this->name = name;
1198057f6c01SJames Morris 	this->len = len;
11991da177e4SLinus Torvalds 	if (!len)
1200057f6c01SJames Morris 		return -EACCES;
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 	hash = init_name_hash();
12031da177e4SLinus Torvalds 	while (len--) {
12041da177e4SLinus Torvalds 		c = *(const unsigned char *)name++;
12051da177e4SLinus Torvalds 		if (c == '/' || c == '\0')
1206057f6c01SJames Morris 			return -EACCES;
12071da177e4SLinus Torvalds 		hash = partial_name_hash(c, hash);
12081da177e4SLinus Torvalds 	}
1209057f6c01SJames Morris 	this->hash = end_name_hash(hash);
1210057f6c01SJames Morris 	return 0;
1211057f6c01SJames Morris }
12121da177e4SLinus Torvalds 
1213eead1911SChristoph Hellwig /**
1214a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1215eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1216eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1217eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1218eead1911SChristoph Hellwig  *
1219a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1220a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1221eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1222eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1223eead1911SChristoph Hellwig  */
1224057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1225057f6c01SJames Morris {
1226057f6c01SJames Morris 	int err;
1227057f6c01SJames Morris 	struct qstr this;
1228057f6c01SJames Morris 
12292f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
12302f9092e1SDavid Woodhouse 
1231057f6c01SJames Morris 	err = __lookup_one_len(name, &this, base, len);
1232057f6c01SJames Morris 	if (err)
1233057f6c01SJames Morris 		return ERR_PTR(err);
1234eead1911SChristoph Hellwig 
1235b75b5086SAl Viro 	err = exec_permission(base->d_inode);
1236eead1911SChristoph Hellwig 	if (err)
1237eead1911SChristoph Hellwig 		return ERR_PTR(err);
123849705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1239057f6c01SJames Morris }
1240057f6c01SJames Morris 
12412d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags,
12422d8f3038SAl Viro 		 struct path *path)
12431da177e4SLinus Torvalds {
12442d8f3038SAl Viro 	struct nameidata nd;
12451da177e4SLinus Torvalds 	char *tmp = getname(name);
12461da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
12471da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
12482d8f3038SAl Viro 
12492d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
12502d8f3038SAl Viro 
12512d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
12521da177e4SLinus Torvalds 		putname(tmp);
12532d8f3038SAl Viro 		if (!err)
12542d8f3038SAl Viro 			*path = nd.path;
12551da177e4SLinus Torvalds 	}
12561da177e4SLinus Torvalds 	return err;
12571da177e4SLinus Torvalds }
12581da177e4SLinus Torvalds 
12592ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
12602ad94ae6SAl Viro 			struct nameidata *nd, char **name)
12612ad94ae6SAl Viro {
12622ad94ae6SAl Viro 	char *s = getname(path);
12632ad94ae6SAl Viro 	int error;
12642ad94ae6SAl Viro 
12652ad94ae6SAl Viro 	if (IS_ERR(s))
12662ad94ae6SAl Viro 		return PTR_ERR(s);
12672ad94ae6SAl Viro 
12682ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
12692ad94ae6SAl Viro 	if (error)
12702ad94ae6SAl Viro 		putname(s);
12712ad94ae6SAl Viro 	else
12722ad94ae6SAl Viro 		*name = s;
12732ad94ae6SAl Viro 
12742ad94ae6SAl Viro 	return error;
12752ad94ae6SAl Viro }
12762ad94ae6SAl Viro 
12771da177e4SLinus Torvalds /*
12781da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
12791da177e4SLinus Torvalds  * minimal.
12801da177e4SLinus Torvalds  */
12811da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
12821da177e4SLinus Torvalds {
1283da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1284da9592edSDavid Howells 
12851da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
12861da177e4SLinus Torvalds 		return 0;
1287da9592edSDavid Howells 	if (inode->i_uid == fsuid)
12881da177e4SLinus Torvalds 		return 0;
1289da9592edSDavid Howells 	if (dir->i_uid == fsuid)
12901da177e4SLinus Torvalds 		return 0;
12911da177e4SLinus Torvalds 	return !capable(CAP_FOWNER);
12921da177e4SLinus Torvalds }
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds /*
12951da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
12961da177e4SLinus Torvalds  *  whether the type of victim is right.
12971da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
12981da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
12991da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
13001da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
13011da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
13021da177e4SLinus Torvalds  *	a. be owner of dir, or
13031da177e4SLinus Torvalds  *	b. be owner of victim, or
13041da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
13051da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
13061da177e4SLinus Torvalds  *     links pointing to it.
13071da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
13081da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
13091da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
13101da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
13111da177e4SLinus Torvalds  *     nfs_async_unlink().
13121da177e4SLinus Torvalds  */
1313858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
13141da177e4SLinus Torvalds {
13151da177e4SLinus Torvalds 	int error;
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds 	if (!victim->d_inode)
13181da177e4SLinus Torvalds 		return -ENOENT;
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
1321cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
13221da177e4SLinus Torvalds 
1323f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
13241da177e4SLinus Torvalds 	if (error)
13251da177e4SLinus Torvalds 		return error;
13261da177e4SLinus Torvalds 	if (IS_APPEND(dir))
13271da177e4SLinus Torvalds 		return -EPERM;
13281da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1329f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
13301da177e4SLinus Torvalds 		return -EPERM;
13311da177e4SLinus Torvalds 	if (isdir) {
13321da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
13331da177e4SLinus Torvalds 			return -ENOTDIR;
13341da177e4SLinus Torvalds 		if (IS_ROOT(victim))
13351da177e4SLinus Torvalds 			return -EBUSY;
13361da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
13371da177e4SLinus Torvalds 		return -EISDIR;
13381da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
13391da177e4SLinus Torvalds 		return -ENOENT;
13401da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
13411da177e4SLinus Torvalds 		return -EBUSY;
13421da177e4SLinus Torvalds 	return 0;
13431da177e4SLinus Torvalds }
13441da177e4SLinus Torvalds 
13451da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
13461da177e4SLinus Torvalds  *  dir.
13471da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
13481da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
13491da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
13501da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
13511da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
13521da177e4SLinus Torvalds  */
1353a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
13541da177e4SLinus Torvalds {
13551da177e4SLinus Torvalds 	if (child->d_inode)
13561da177e4SLinus Torvalds 		return -EEXIST;
13571da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
13581da177e4SLinus Torvalds 		return -ENOENT;
1359f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
13601da177e4SLinus Torvalds }
13611da177e4SLinus Torvalds 
13621da177e4SLinus Torvalds /*
13631da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
13641da177e4SLinus Torvalds  */
13651da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
13661da177e4SLinus Torvalds {
13671da177e4SLinus Torvalds 	struct dentry *p;
13681da177e4SLinus Torvalds 
13691da177e4SLinus Torvalds 	if (p1 == p2) {
1370f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
13711da177e4SLinus Torvalds 		return NULL;
13721da177e4SLinus Torvalds 	}
13731da177e4SLinus Torvalds 
1374a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
13751da177e4SLinus Torvalds 
1376e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1377e2761a11SOGAWA Hirofumi 	if (p) {
1378f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1379f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
13801da177e4SLinus Torvalds 		return p;
13811da177e4SLinus Torvalds 	}
13821da177e4SLinus Torvalds 
1383e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1384e2761a11SOGAWA Hirofumi 	if (p) {
1385f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1386f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
13871da177e4SLinus Torvalds 		return p;
13881da177e4SLinus Torvalds 	}
13891da177e4SLinus Torvalds 
1390f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1391f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
13921da177e4SLinus Torvalds 	return NULL;
13931da177e4SLinus Torvalds }
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
13961da177e4SLinus Torvalds {
13971b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
13981da177e4SLinus Torvalds 	if (p1 != p2) {
13991b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1400a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
14011da177e4SLinus Torvalds 	}
14021da177e4SLinus Torvalds }
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
14051da177e4SLinus Torvalds 		struct nameidata *nd)
14061da177e4SLinus Torvalds {
1407a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
14081da177e4SLinus Torvalds 
14091da177e4SLinus Torvalds 	if (error)
14101da177e4SLinus Torvalds 		return error;
14111da177e4SLinus Torvalds 
1412acfa4380SAl Viro 	if (!dir->i_op->create)
14131da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
14141da177e4SLinus Torvalds 	mode &= S_IALLUGO;
14151da177e4SLinus Torvalds 	mode |= S_IFREG;
14161da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
14171da177e4SLinus Torvalds 	if (error)
14181da177e4SLinus Torvalds 		return error;
14199e3509e2SJan Kara 	vfs_dq_init(dir);
14201da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
1421a74574aaSStephen Smalley 	if (!error)
1422f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
14231da177e4SLinus Torvalds 	return error;
14241da177e4SLinus Torvalds }
14251da177e4SLinus Torvalds 
14263fb64190SChristoph Hellwig int may_open(struct path *path, int acc_mode, int flag)
14271da177e4SLinus Torvalds {
14283fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
14291da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14301da177e4SLinus Torvalds 	int error;
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds 	if (!inode)
14331da177e4SLinus Torvalds 		return -ENOENT;
14341da177e4SLinus Torvalds 
1435c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
1436c8fe8f30SChristoph Hellwig 	case S_IFLNK:
14371da177e4SLinus Torvalds 		return -ELOOP;
1438c8fe8f30SChristoph Hellwig 	case S_IFDIR:
1439c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
14401da177e4SLinus Torvalds 			return -EISDIR;
1441c8fe8f30SChristoph Hellwig 		break;
1442c8fe8f30SChristoph Hellwig 	case S_IFBLK:
1443c8fe8f30SChristoph Hellwig 	case S_IFCHR:
14443fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
14451da177e4SLinus Torvalds 			return -EACCES;
1446c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
1447c8fe8f30SChristoph Hellwig 	case S_IFIFO:
1448c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
14491da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
1450c8fe8f30SChristoph Hellwig 		break;
14514a3fd211SDave Hansen 	}
1452b41572e9SDave Hansen 
14533fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
1454b41572e9SDave Hansen 	if (error)
1455b41572e9SDave Hansen 		return error;
14566146f0d5SMimi Zohar 
14571da177e4SLinus Torvalds 	/*
14581da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
14591da177e4SLinus Torvalds 	 */
14601da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
14618737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
14627715b521SAl Viro 			return -EPERM;
14631da177e4SLinus Torvalds 		if (flag & O_TRUNC)
14647715b521SAl Viro 			return -EPERM;
14651da177e4SLinus Torvalds 	}
14661da177e4SLinus Torvalds 
14671da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
14687715b521SAl Viro 	if (flag & O_NOATIME && !is_owner_or_cap(inode))
14697715b521SAl Viro 		return -EPERM;
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds 	/*
14721da177e4SLinus Torvalds 	 * Ensure there are no outstanding leases on the file.
14731da177e4SLinus Torvalds 	 */
1474b65a9cfcSAl Viro 	return break_lease(inode, flag);
14757715b521SAl Viro }
14767715b521SAl Viro 
14777715b521SAl Viro static int handle_truncate(struct path *path)
14787715b521SAl Viro {
14797715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
14807715b521SAl Viro 	int error = get_write_access(inode);
14811da177e4SLinus Torvalds 	if (error)
14827715b521SAl Viro 		return error;
14831da177e4SLinus Torvalds 	/*
14841da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
14851da177e4SLinus Torvalds 	 */
14861da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
1487be6d3e56SKentaro Takeda 	if (!error)
14883fb64190SChristoph Hellwig 		error = security_path_truncate(path, 0,
1489be6d3e56SKentaro Takeda 				       ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
14901da177e4SLinus Torvalds 	if (!error) {
14917715b521SAl Viro 		error = do_truncate(path->dentry, 0,
1492d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
1493d139d7ffSMiklos Szeredi 				    NULL);
14941da177e4SLinus Torvalds 	}
14951da177e4SLinus Torvalds 	put_write_access(inode);
1496acd0c935SMimi Zohar 	return error;
14971da177e4SLinus Torvalds }
14981da177e4SLinus Torvalds 
1499d57999e1SDave Hansen /*
1500d57999e1SDave Hansen  * Be careful about ever adding any more callers of this
1501d57999e1SDave Hansen  * function.  Its flags must be in the namei format, not
1502d57999e1SDave Hansen  * what get passed to sys_open().
1503d57999e1SDave Hansen  */
1504d57999e1SDave Hansen static int __open_namei_create(struct nameidata *nd, struct path *path,
15058737c930SAl Viro 				int open_flag, int mode)
1506aab520e2SDave Hansen {
1507aab520e2SDave Hansen 	int error;
15084ac91378SJan Blunck 	struct dentry *dir = nd->path.dentry;
1509aab520e2SDave Hansen 
1510aab520e2SDave Hansen 	if (!IS_POSIXACL(dir->d_inode))
1511ce3b0f8dSAl Viro 		mode &= ~current_umask();
1512be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd->path, path->dentry, mode, 0);
1513be6d3e56SKentaro Takeda 	if (error)
1514be6d3e56SKentaro Takeda 		goto out_unlock;
1515aab520e2SDave Hansen 	error = vfs_create(dir->d_inode, path->dentry, mode, nd);
1516be6d3e56SKentaro Takeda out_unlock:
1517aab520e2SDave Hansen 	mutex_unlock(&dir->d_inode->i_mutex);
15184ac91378SJan Blunck 	dput(nd->path.dentry);
15194ac91378SJan Blunck 	nd->path.dentry = path->dentry;
1520aab520e2SDave Hansen 	if (error)
1521aab520e2SDave Hansen 		return error;
1522aab520e2SDave Hansen 	/* Don't check for write permission, don't truncate */
15238737c930SAl Viro 	return may_open(&nd->path, 0, open_flag & ~O_TRUNC);
1524aab520e2SDave Hansen }
1525aab520e2SDave Hansen 
15261da177e4SLinus Torvalds /*
1527d57999e1SDave Hansen  * Note that while the flag value (low two bits) for sys_open means:
1528d57999e1SDave Hansen  *	00 - read-only
1529d57999e1SDave Hansen  *	01 - write-only
1530d57999e1SDave Hansen  *	10 - read-write
1531d57999e1SDave Hansen  *	11 - special
1532d57999e1SDave Hansen  * it is changed into
1533d57999e1SDave Hansen  *	00 - no permissions needed
1534d57999e1SDave Hansen  *	01 - read-permission
1535d57999e1SDave Hansen  *	10 - write-permission
1536d57999e1SDave Hansen  *	11 - read-write
1537d57999e1SDave Hansen  * for the internal routines (ie open_namei()/follow_link() etc)
1538d57999e1SDave Hansen  * This is more logical, and also allows the 00 "no perm needed"
1539d57999e1SDave Hansen  * to be used for symlinks (where the permissions are checked
1540d57999e1SDave Hansen  * later).
1541d57999e1SDave Hansen  *
1542d57999e1SDave Hansen */
1543d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
1544d57999e1SDave Hansen {
1545d57999e1SDave Hansen 	if ((flag+1) & O_ACCMODE)
1546d57999e1SDave Hansen 		flag++;
1547d57999e1SDave Hansen 	return flag;
1548d57999e1SDave Hansen }
1549d57999e1SDave Hansen 
15507715b521SAl Viro static int open_will_truncate(int flag, struct inode *inode)
15514a3fd211SDave Hansen {
1552d57999e1SDave Hansen 	/*
15534a3fd211SDave Hansen 	 * We'll never write to the fs underlying
15544a3fd211SDave Hansen 	 * a device file.
15554a3fd211SDave Hansen 	 */
15564a3fd211SDave Hansen 	if (special_file(inode->i_mode))
15574a3fd211SDave Hansen 		return 0;
15584a3fd211SDave Hansen 	return (flag & O_TRUNC);
15594a3fd211SDave Hansen }
15604a3fd211SDave Hansen 
1561648fa861SAl Viro static struct file *finish_open(struct nameidata *nd,
15629a66179eSAl Viro 				int open_flag, int acc_mode)
1563648fa861SAl Viro {
1564648fa861SAl Viro 	struct file *filp;
1565648fa861SAl Viro 	int will_truncate;
1566648fa861SAl Viro 	int error;
1567648fa861SAl Viro 
15689a66179eSAl Viro 	will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode);
1569648fa861SAl Viro 	if (will_truncate) {
1570648fa861SAl Viro 		error = mnt_want_write(nd->path.mnt);
1571648fa861SAl Viro 		if (error)
1572648fa861SAl Viro 			goto exit;
1573648fa861SAl Viro 	}
1574648fa861SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
1575648fa861SAl Viro 	if (error) {
1576648fa861SAl Viro 		if (will_truncate)
1577648fa861SAl Viro 			mnt_drop_write(nd->path.mnt);
1578648fa861SAl Viro 		goto exit;
1579648fa861SAl Viro 	}
1580648fa861SAl Viro 	filp = nameidata_to_filp(nd);
1581648fa861SAl Viro 	if (!IS_ERR(filp)) {
1582648fa861SAl Viro 		error = ima_file_check(filp, acc_mode);
1583648fa861SAl Viro 		if (error) {
1584648fa861SAl Viro 			fput(filp);
1585648fa861SAl Viro 			filp = ERR_PTR(error);
1586648fa861SAl Viro 		}
1587648fa861SAl Viro 	}
1588648fa861SAl Viro 	if (!IS_ERR(filp)) {
1589648fa861SAl Viro 		if (acc_mode & MAY_WRITE)
1590648fa861SAl Viro 			vfs_dq_init(nd->path.dentry->d_inode);
1591648fa861SAl Viro 
1592648fa861SAl Viro 		if (will_truncate) {
1593648fa861SAl Viro 			error = handle_truncate(&nd->path);
1594648fa861SAl Viro 			if (error) {
1595648fa861SAl Viro 				fput(filp);
1596648fa861SAl Viro 				filp = ERR_PTR(error);
1597648fa861SAl Viro 			}
1598648fa861SAl Viro 		}
1599648fa861SAl Viro 	}
1600648fa861SAl Viro 	/*
1601648fa861SAl Viro 	 * It is now safe to drop the mnt write
1602648fa861SAl Viro 	 * because the filp has had a write taken
1603648fa861SAl Viro 	 * on its behalf.
1604648fa861SAl Viro 	 */
1605648fa861SAl Viro 	if (will_truncate)
1606648fa861SAl Viro 		mnt_drop_write(nd->path.mnt);
1607648fa861SAl Viro 	return filp;
1608648fa861SAl Viro 
1609648fa861SAl Viro exit:
1610648fa861SAl Viro 	if (!IS_ERR(nd->intent.open.file))
1611648fa861SAl Viro 		release_open_intent(nd);
1612648fa861SAl Viro 	path_put(&nd->path);
1613648fa861SAl Viro 	return ERR_PTR(error);
1614648fa861SAl Viro }
1615648fa861SAl Viro 
1616fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
16175b369df8SAl Viro 			    int open_flag, int acc_mode,
16181f36f774SAl Viro 			    int mode, const char *pathname,
16191f36f774SAl Viro 			    int *want_dir)
1620fb1cc555SAl Viro {
1621a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
1622fb1cc555SAl Viro 	struct file *filp;
16231f36f774SAl Viro 	int error = -EISDIR;
1624fb1cc555SAl Viro 
16251f36f774SAl Viro 	switch (nd->last_type) {
16261f36f774SAl Viro 	case LAST_DOTDOT:
16271f36f774SAl Viro 		follow_dotdot(nd);
16281f36f774SAl Viro 		dir = nd->path.dentry;
16291f36f774SAl Viro 		if (nd->path.mnt->mnt_sb->s_type->fs_flags & FS_REVAL_DOT) {
16301f36f774SAl Viro 			if (!dir->d_op->d_revalidate(dir, nd)) {
16311f36f774SAl Viro 				error = -ESTALE;
1632a2c36b45SAl Viro 				goto exit;
16331f36f774SAl Viro 			}
16341f36f774SAl Viro 		}
16351f36f774SAl Viro 		/* fallthrough */
16361f36f774SAl Viro 	case LAST_DOT:
16371f36f774SAl Viro 	case LAST_ROOT:
16381f36f774SAl Viro 		if (open_flag & O_CREAT)
16391f36f774SAl Viro 			goto exit;
16401f36f774SAl Viro 		/* fallthrough */
16411f36f774SAl Viro 	case LAST_BIND:
16421f36f774SAl Viro 		audit_inode(pathname, dir);
16431f36f774SAl Viro 		goto ok;
16441f36f774SAl Viro 	}
1645a2c36b45SAl Viro 
16461f36f774SAl Viro 	/* trailing slashes? */
16471f36f774SAl Viro 	if (nd->last.name[nd->last.len]) {
16481f36f774SAl Viro 		if (open_flag & O_CREAT)
16491f36f774SAl Viro 			goto exit;
16501f36f774SAl Viro 		*want_dir = 1;
16511f36f774SAl Viro 	}
16521f36f774SAl Viro 
16531f36f774SAl Viro 	/* just plain open? */
16541f36f774SAl Viro 	if (!(open_flag & O_CREAT)) {
16551f36f774SAl Viro 		error = do_lookup(nd, &nd->last, path);
16561f36f774SAl Viro 		if (error)
16571f36f774SAl Viro 			goto exit;
16581f36f774SAl Viro 		error = -ENOENT;
16591f36f774SAl Viro 		if (!path->dentry->d_inode)
16601f36f774SAl Viro 			goto exit_dput;
16611f36f774SAl Viro 		if (path->dentry->d_inode->i_op->follow_link)
16621f36f774SAl Viro 			return NULL;
16631f36f774SAl Viro 		error = -ENOTDIR;
16641f36f774SAl Viro 		if (*want_dir & !path->dentry->d_inode->i_op->lookup)
16651f36f774SAl Viro 			goto exit_dput;
16661f36f774SAl Viro 		path_to_nameidata(path, nd);
16671f36f774SAl Viro 		audit_inode(pathname, nd->path.dentry);
16681f36f774SAl Viro 		goto ok;
16691f36f774SAl Viro 	}
16701f36f774SAl Viro 
16711f36f774SAl Viro 	/* OK, it's O_CREAT */
1672a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
1673a1e28038SAl Viro 
1674a1e28038SAl Viro 	path->dentry = lookup_hash(nd);
1675a1e28038SAl Viro 	path->mnt = nd->path.mnt;
1676a1e28038SAl Viro 
1677fb1cc555SAl Viro 	error = PTR_ERR(path->dentry);
1678fb1cc555SAl Viro 	if (IS_ERR(path->dentry)) {
1679fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
1680fb1cc555SAl Viro 		goto exit;
1681fb1cc555SAl Viro 	}
1682fb1cc555SAl Viro 
1683fb1cc555SAl Viro 	if (IS_ERR(nd->intent.open.file)) {
1684fb1cc555SAl Viro 		error = PTR_ERR(nd->intent.open.file);
1685fb1cc555SAl Viro 		goto exit_mutex_unlock;
1686fb1cc555SAl Viro 	}
1687fb1cc555SAl Viro 
1688fb1cc555SAl Viro 	/* Negative dentry, just create the file */
1689fb1cc555SAl Viro 	if (!path->dentry->d_inode) {
1690fb1cc555SAl Viro 		/*
1691fb1cc555SAl Viro 		 * This write is needed to ensure that a
1692fb1cc555SAl Viro 		 * ro->rw transition does not occur between
1693fb1cc555SAl Viro 		 * the time when the file is created and when
1694fb1cc555SAl Viro 		 * a permanent write count is taken through
1695fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
1696fb1cc555SAl Viro 		 */
1697fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
1698fb1cc555SAl Viro 		if (error)
1699fb1cc555SAl Viro 			goto exit_mutex_unlock;
1700fb1cc555SAl Viro 		error = __open_namei_create(nd, path, open_flag, mode);
1701fb1cc555SAl Viro 		if (error) {
1702fb1cc555SAl Viro 			mnt_drop_write(nd->path.mnt);
1703fb1cc555SAl Viro 			goto exit;
1704fb1cc555SAl Viro 		}
1705fb1cc555SAl Viro 		filp = nameidata_to_filp(nd);
1706fb1cc555SAl Viro 		mnt_drop_write(nd->path.mnt);
1707fb1cc555SAl Viro 		if (!IS_ERR(filp)) {
1708fb1cc555SAl Viro 			error = ima_file_check(filp, acc_mode);
1709fb1cc555SAl Viro 			if (error) {
1710fb1cc555SAl Viro 				fput(filp);
1711fb1cc555SAl Viro 				filp = ERR_PTR(error);
1712fb1cc555SAl Viro 			}
1713fb1cc555SAl Viro 		}
1714fb1cc555SAl Viro 		return filp;
1715fb1cc555SAl Viro 	}
1716fb1cc555SAl Viro 
1717fb1cc555SAl Viro 	/*
1718fb1cc555SAl Viro 	 * It already exists.
1719fb1cc555SAl Viro 	 */
1720fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
1721fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
1722fb1cc555SAl Viro 
1723fb1cc555SAl Viro 	error = -EEXIST;
17245b369df8SAl Viro 	if (open_flag & O_EXCL)
1725fb1cc555SAl Viro 		goto exit_dput;
1726fb1cc555SAl Viro 
1727fb1cc555SAl Viro 	if (__follow_mount(path)) {
1728fb1cc555SAl Viro 		error = -ELOOP;
17295b369df8SAl Viro 		if (open_flag & O_NOFOLLOW)
1730fb1cc555SAl Viro 			goto exit_dput;
1731fb1cc555SAl Viro 	}
1732fb1cc555SAl Viro 
1733fb1cc555SAl Viro 	error = -ENOENT;
1734fb1cc555SAl Viro 	if (!path->dentry->d_inode)
1735fb1cc555SAl Viro 		goto exit_dput;
17369e67f361SAl Viro 
17379e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
1738fb1cc555SAl Viro 		return NULL;
1739fb1cc555SAl Viro 
1740fb1cc555SAl Viro 	path_to_nameidata(path, nd);
1741fb1cc555SAl Viro 	error = -EISDIR;
1742fb1cc555SAl Viro 	if (S_ISDIR(path->dentry->d_inode->i_mode))
1743fb1cc555SAl Viro 		goto exit;
174467ee3ad2SAl Viro ok:
17459a66179eSAl Viro 	filp = finish_open(nd, open_flag, acc_mode);
1746fb1cc555SAl Viro 	return filp;
1747fb1cc555SAl Viro 
1748fb1cc555SAl Viro exit_mutex_unlock:
1749fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
1750fb1cc555SAl Viro exit_dput:
1751fb1cc555SAl Viro 	path_put_conditional(path, nd);
1752fb1cc555SAl Viro exit:
1753fb1cc555SAl Viro 	if (!IS_ERR(nd->intent.open.file))
1754fb1cc555SAl Viro 		release_open_intent(nd);
1755fb1cc555SAl Viro 	path_put(&nd->path);
1756fb1cc555SAl Viro 	return ERR_PTR(error);
1757fb1cc555SAl Viro }
1758fb1cc555SAl Viro 
17594a3fd211SDave Hansen /*
17604a3fd211SDave Hansen  * Note that the low bits of the passed in "open_flag"
17614a3fd211SDave Hansen  * are not the same as in the local variable "flag". See
17624a3fd211SDave Hansen  * open_to_namei_flags() for more details.
17631da177e4SLinus Torvalds  */
1764a70e65dfSChristoph Hellwig struct file *do_filp_open(int dfd, const char *pathname,
17656e8341a1SAl Viro 		int open_flag, int mode, int acc_mode)
17661da177e4SLinus Torvalds {
17674a3fd211SDave Hansen 	struct file *filp;
1768a70e65dfSChristoph Hellwig 	struct nameidata nd;
17696e8341a1SAl Viro 	int error;
17709850c056SAl Viro 	struct path path;
17711da177e4SLinus Torvalds 	int count = 0;
1772d57999e1SDave Hansen 	int flag = open_to_namei_flags(open_flag);
17739850c056SAl Viro 	int force_reval = 0;
17741f36f774SAl Viro 	int want_dir = open_flag & O_DIRECTORY;
17751f36f774SAl Viro 
17761f36f774SAl Viro 	if (!(open_flag & O_CREAT))
17771f36f774SAl Viro 		mode = 0;
17781da177e4SLinus Torvalds 
17796b2f3d1fSChristoph Hellwig 	/*
17806b2f3d1fSChristoph Hellwig 	 * O_SYNC is implemented as __O_SYNC|O_DSYNC.  As many places only
17816b2f3d1fSChristoph Hellwig 	 * check for O_DSYNC if the need any syncing at all we enforce it's
17826b2f3d1fSChristoph Hellwig 	 * always set instead of having to deal with possibly weird behaviour
17836b2f3d1fSChristoph Hellwig 	 * for malicious applications setting only __O_SYNC.
17846b2f3d1fSChristoph Hellwig 	 */
17856b2f3d1fSChristoph Hellwig 	if (open_flag & __O_SYNC)
17866b2f3d1fSChristoph Hellwig 		open_flag |= O_DSYNC;
17876b2f3d1fSChristoph Hellwig 
17886e8341a1SAl Viro 	if (!acc_mode)
17896d125529SAl Viro 		acc_mode = MAY_OPEN | ACC_MODE(open_flag);
17901da177e4SLinus Torvalds 
1791834f2a4aSTrond Myklebust 	/* O_TRUNC implies we need access checks for write permissions */
17924296e2cbSAl Viro 	if (open_flag & O_TRUNC)
1793834f2a4aSTrond Myklebust 		acc_mode |= MAY_WRITE;
1794834f2a4aSTrond Myklebust 
17951da177e4SLinus Torvalds 	/* Allow the LSM permission hook to distinguish append
17961da177e4SLinus Torvalds 	   access from general write access. */
17974296e2cbSAl Viro 	if (open_flag & O_APPEND)
17981da177e4SLinus Torvalds 		acc_mode |= MAY_APPEND;
17991da177e4SLinus Torvalds 
18001f36f774SAl Viro 	/* find the parent */
18019850c056SAl Viro reval:
18029b4a9b14SAl Viro 	error = path_init(dfd, pathname, LOOKUP_PARENT, &nd);
18031da177e4SLinus Torvalds 	if (error)
1804a70e65dfSChristoph Hellwig 		return ERR_PTR(error);
18059850c056SAl Viro 	if (force_reval)
18069850c056SAl Viro 		nd.flags |= LOOKUP_REVAL;
18073866248eSAl Viro 
18083866248eSAl Viro 	current->total_link_count = 0;
18093866248eSAl Viro 	error = link_path_walk(pathname, &nd);
1810654f562cSJ. R. Okajima 	if (error) {
181110fa8e62SAl Viro 		filp = ERR_PTR(error);
181210fa8e62SAl Viro 		goto out;
1813654f562cSJ. R. Okajima 	}
18141f36f774SAl Viro 	if (unlikely(!audit_dummy_context()) && (open_flag & O_CREAT))
18159b4a9b14SAl Viro 		audit_inode(pathname, nd.path.dentry);
18161da177e4SLinus Torvalds 
18171da177e4SLinus Torvalds 	/*
1818a2c36b45SAl Viro 	 * We have the parent and last component.
18191da177e4SLinus Torvalds 	 */
18201da177e4SLinus Torvalds 
18218737f3a1SAl Viro 	error = -ENFILE;
18228737f3a1SAl Viro 	filp = get_empty_filp();
18238737f3a1SAl Viro 	if (filp == NULL)
18248737f3a1SAl Viro 		goto exit_parent;
18258737f3a1SAl Viro 	nd.intent.open.file = filp;
1826482928d5SAl Viro 	filp->f_flags = open_flag;
18278737f3a1SAl Viro 	nd.intent.open.flags = flag;
18288737f3a1SAl Viro 	nd.intent.open.create_mode = mode;
1829a70e65dfSChristoph Hellwig 	nd.flags &= ~LOOKUP_PARENT;
18301f36f774SAl Viro 	nd.flags |= LOOKUP_OPEN;
18311f36f774SAl Viro 	if (open_flag & O_CREAT) {
18321f36f774SAl Viro 		nd.flags |= LOOKUP_CREATE;
18334296e2cbSAl Viro 		if (open_flag & O_EXCL)
18343516586aSAl Viro 			nd.flags |= LOOKUP_EXCL;
18351f36f774SAl Viro 	}
18361f36f774SAl Viro 	filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname, &want_dir);
1837806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
1838def4af30SAl Viro 		struct path holder;
18391f36f774SAl Viro 		struct inode *inode = path.dentry->d_inode;
1840def4af30SAl Viro 		void *cookie;
1841806b681cSAl Viro 		error = -ELOOP;
18421f36f774SAl Viro 		/* S_ISDIR part is a temporary automount kludge */
18431f36f774SAl Viro 		if ((open_flag & O_NOFOLLOW) && !S_ISDIR(inode->i_mode))
18441f36f774SAl Viro 			goto exit_dput;
18451f36f774SAl Viro 		if (count++ == 32)
1846806b681cSAl Viro 			goto exit_dput;
1847806b681cSAl Viro 		/*
1848806b681cSAl Viro 		 * This is subtle. Instead of calling do_follow_link() we do
1849806b681cSAl Viro 		 * the thing by hands. The reason is that this way we have zero
1850806b681cSAl Viro 		 * link_count and path_walk() (called from ->follow_link)
1851806b681cSAl Viro 		 * honoring LOOKUP_PARENT.  After that we have the parent and
1852806b681cSAl Viro 		 * last component, i.e. we are in the same situation as after
1853806b681cSAl Viro 		 * the first path_walk().  Well, almost - if the last component
1854806b681cSAl Viro 		 * is normal we get its copy stored in nd->last.name and we will
1855806b681cSAl Viro 		 * have to putname() it when we are done. Procfs-like symlinks
1856806b681cSAl Viro 		 * just set LAST_BIND.
1857806b681cSAl Viro 		 */
1858806b681cSAl Viro 		nd.flags |= LOOKUP_PARENT;
1859806b681cSAl Viro 		error = security_inode_follow_link(path.dentry, &nd);
1860806b681cSAl Viro 		if (error)
1861806b681cSAl Viro 			goto exit_dput;
1862def4af30SAl Viro 		error = __do_follow_link(&path, &nd, &cookie);
1863def4af30SAl Viro 		if (unlikely(error)) {
1864806b681cSAl Viro 			/* nd.path had been dropped */
1865def4af30SAl Viro 			if (!IS_ERR(cookie) && inode->i_op->put_link)
1866def4af30SAl Viro 				inode->i_op->put_link(path.dentry, &nd, cookie);
1867def4af30SAl Viro 			path_put(&path);
1868a70e65dfSChristoph Hellwig 			release_open_intent(&nd);
186910fa8e62SAl Viro 			filp = ERR_PTR(error);
1870806b681cSAl Viro 			goto out;
1871806b681cSAl Viro 		}
1872def4af30SAl Viro 		holder = path;
1873806b681cSAl Viro 		nd.flags &= ~LOOKUP_PARENT;
18741f36f774SAl Viro 		filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname, &want_dir);
1875def4af30SAl Viro 		if (inode->i_op->put_link)
1876def4af30SAl Viro 			inode->i_op->put_link(holder.dentry, &nd, cookie);
1877def4af30SAl Viro 		path_put(&holder);
1878806b681cSAl Viro 	}
187910fa8e62SAl Viro out:
18802a737871SAl Viro 	if (nd.root.mnt)
18812a737871SAl Viro 		path_put(&nd.root);
188210fa8e62SAl Viro 	if (filp == ERR_PTR(-ESTALE) && !force_reval) {
188310fa8e62SAl Viro 		force_reval = 1;
188410fa8e62SAl Viro 		goto reval;
188510fa8e62SAl Viro 	}
188610fa8e62SAl Viro 	return filp;
18871da177e4SLinus Torvalds 
1888806b681cSAl Viro exit_dput:
1889806b681cSAl Viro 	path_put_conditional(&path, &nd);
1890806b681cSAl Viro 	if (!IS_ERR(nd.intent.open.file))
1891a70e65dfSChristoph Hellwig 		release_open_intent(&nd);
1892806b681cSAl Viro exit_parent:
1893806b681cSAl Viro 	path_put(&nd.path);
189410fa8e62SAl Viro 	filp = ERR_PTR(error);
189510fa8e62SAl Viro 	goto out;
1896de459215SKirill Korotaev }
18971da177e4SLinus Torvalds 
18981da177e4SLinus Torvalds /**
1899a70e65dfSChristoph Hellwig  * filp_open - open file and return file pointer
1900a70e65dfSChristoph Hellwig  *
1901a70e65dfSChristoph Hellwig  * @filename:	path to open
1902a70e65dfSChristoph Hellwig  * @flags:	open flags as per the open(2) second argument
1903a70e65dfSChristoph Hellwig  * @mode:	mode for the new file if O_CREAT is set, else ignored
1904a70e65dfSChristoph Hellwig  *
1905a70e65dfSChristoph Hellwig  * This is the helper to open a file from kernelspace if you really
1906a70e65dfSChristoph Hellwig  * have to.  But in generally you should not do this, so please move
1907a70e65dfSChristoph Hellwig  * along, nothing to see here..
1908a70e65dfSChristoph Hellwig  */
1909a70e65dfSChristoph Hellwig struct file *filp_open(const char *filename, int flags, int mode)
1910a70e65dfSChristoph Hellwig {
19116e8341a1SAl Viro 	return do_filp_open(AT_FDCWD, filename, flags, mode, 0);
1912a70e65dfSChristoph Hellwig }
1913a70e65dfSChristoph Hellwig EXPORT_SYMBOL(filp_open);
1914a70e65dfSChristoph Hellwig 
1915a70e65dfSChristoph Hellwig /**
19161da177e4SLinus Torvalds  * lookup_create - lookup a dentry, creating it if it doesn't exist
19171da177e4SLinus Torvalds  * @nd: nameidata info
19181da177e4SLinus Torvalds  * @is_dir: directory flag
19191da177e4SLinus Torvalds  *
19201da177e4SLinus Torvalds  * Simple function to lookup and return a dentry and create it
19211da177e4SLinus Torvalds  * if it doesn't exist.  Is SMP-safe.
1922c663e5d8SChristoph Hellwig  *
19234ac91378SJan Blunck  * Returns with nd->path.dentry->d_inode->i_mutex locked.
19241da177e4SLinus Torvalds  */
19251da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir)
19261da177e4SLinus Torvalds {
1927c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
19281da177e4SLinus Torvalds 
19294ac91378SJan Blunck 	mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
1930c663e5d8SChristoph Hellwig 	/*
1931c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
1932c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
1933c663e5d8SChristoph Hellwig 	 */
19341da177e4SLinus Torvalds 	if (nd->last_type != LAST_NORM)
19351da177e4SLinus Torvalds 		goto fail;
19361da177e4SLinus Torvalds 	nd->flags &= ~LOOKUP_PARENT;
19373516586aSAl Viro 	nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
1938a634904aSASANO Masahiro 	nd->intent.open.flags = O_EXCL;
1939c663e5d8SChristoph Hellwig 
1940c663e5d8SChristoph Hellwig 	/*
1941c663e5d8SChristoph Hellwig 	 * Do the final lookup.
1942c663e5d8SChristoph Hellwig 	 */
194349705b77SChristoph Hellwig 	dentry = lookup_hash(nd);
19441da177e4SLinus Torvalds 	if (IS_ERR(dentry))
19451da177e4SLinus Torvalds 		goto fail;
1946c663e5d8SChristoph Hellwig 
1947e9baf6e5SAl Viro 	if (dentry->d_inode)
1948e9baf6e5SAl Viro 		goto eexist;
1949c663e5d8SChristoph Hellwig 	/*
1950c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
1951c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
1952c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
1953c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
1954c663e5d8SChristoph Hellwig 	 */
1955e9baf6e5SAl Viro 	if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
19561da177e4SLinus Torvalds 		dput(dentry);
19571da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
1958e9baf6e5SAl Viro 	}
1959e9baf6e5SAl Viro 	return dentry;
1960e9baf6e5SAl Viro eexist:
1961e9baf6e5SAl Viro 	dput(dentry);
1962e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
19631da177e4SLinus Torvalds fail:
19641da177e4SLinus Torvalds 	return dentry;
19651da177e4SLinus Torvalds }
1966f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create);
19671da177e4SLinus Torvalds 
19681da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
19691da177e4SLinus Torvalds {
1970a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
19711da177e4SLinus Torvalds 
19721da177e4SLinus Torvalds 	if (error)
19731da177e4SLinus Torvalds 		return error;
19741da177e4SLinus Torvalds 
19751da177e4SLinus Torvalds 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
19761da177e4SLinus Torvalds 		return -EPERM;
19771da177e4SLinus Torvalds 
1978acfa4380SAl Viro 	if (!dir->i_op->mknod)
19791da177e4SLinus Torvalds 		return -EPERM;
19801da177e4SLinus Torvalds 
198108ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
198208ce5f16SSerge E. Hallyn 	if (error)
198308ce5f16SSerge E. Hallyn 		return error;
198408ce5f16SSerge E. Hallyn 
19851da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
19861da177e4SLinus Torvalds 	if (error)
19871da177e4SLinus Torvalds 		return error;
19881da177e4SLinus Torvalds 
19899e3509e2SJan Kara 	vfs_dq_init(dir);
19901da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
1991a74574aaSStephen Smalley 	if (!error)
1992f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
19931da177e4SLinus Torvalds 	return error;
19941da177e4SLinus Torvalds }
19951da177e4SLinus Torvalds 
1996463c3197SDave Hansen static int may_mknod(mode_t mode)
1997463c3197SDave Hansen {
1998463c3197SDave Hansen 	switch (mode & S_IFMT) {
1999463c3197SDave Hansen 	case S_IFREG:
2000463c3197SDave Hansen 	case S_IFCHR:
2001463c3197SDave Hansen 	case S_IFBLK:
2002463c3197SDave Hansen 	case S_IFIFO:
2003463c3197SDave Hansen 	case S_IFSOCK:
2004463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2005463c3197SDave Hansen 		return 0;
2006463c3197SDave Hansen 	case S_IFDIR:
2007463c3197SDave Hansen 		return -EPERM;
2008463c3197SDave Hansen 	default:
2009463c3197SDave Hansen 		return -EINVAL;
2010463c3197SDave Hansen 	}
2011463c3197SDave Hansen }
2012463c3197SDave Hansen 
20132e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
20142e4d0924SHeiko Carstens 		unsigned, dev)
20151da177e4SLinus Torvalds {
20162ad94ae6SAl Viro 	int error;
20171da177e4SLinus Torvalds 	char *tmp;
20181da177e4SLinus Torvalds 	struct dentry *dentry;
20191da177e4SLinus Torvalds 	struct nameidata nd;
20201da177e4SLinus Torvalds 
20211da177e4SLinus Torvalds 	if (S_ISDIR(mode))
20221da177e4SLinus Torvalds 		return -EPERM;
20231da177e4SLinus Torvalds 
20242ad94ae6SAl Viro 	error = user_path_parent(dfd, filename, &nd, &tmp);
20251da177e4SLinus Torvalds 	if (error)
20262ad94ae6SAl Viro 		return error;
20272ad94ae6SAl Viro 
20281da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
2029463c3197SDave Hansen 	if (IS_ERR(dentry)) {
20301da177e4SLinus Torvalds 		error = PTR_ERR(dentry);
2031463c3197SDave Hansen 		goto out_unlock;
2032463c3197SDave Hansen 	}
20334ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2034ce3b0f8dSAl Viro 		mode &= ~current_umask();
2035463c3197SDave Hansen 	error = may_mknod(mode);
2036463c3197SDave Hansen 	if (error)
2037463c3197SDave Hansen 		goto out_dput;
2038463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2039463c3197SDave Hansen 	if (error)
2040463c3197SDave Hansen 		goto out_dput;
2041be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd.path, dentry, mode, dev);
2042be6d3e56SKentaro Takeda 	if (error)
2043be6d3e56SKentaro Takeda 		goto out_drop_write;
20441da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
20451da177e4SLinus Torvalds 		case 0: case S_IFREG:
20464ac91378SJan Blunck 			error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
20471da177e4SLinus Torvalds 			break;
20481da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
20494ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
20501da177e4SLinus Torvalds 					new_decode_dev(dev));
20511da177e4SLinus Torvalds 			break;
20521da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
20534ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
20541da177e4SLinus Torvalds 			break;
20551da177e4SLinus Torvalds 	}
2056be6d3e56SKentaro Takeda out_drop_write:
2057463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2058463c3197SDave Hansen out_dput:
20591da177e4SLinus Torvalds 	dput(dentry);
2060463c3197SDave Hansen out_unlock:
20614ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
20621d957f9bSJan Blunck 	path_put(&nd.path);
20631da177e4SLinus Torvalds 	putname(tmp);
20641da177e4SLinus Torvalds 
20651da177e4SLinus Torvalds 	return error;
20661da177e4SLinus Torvalds }
20671da177e4SLinus Torvalds 
20683480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
20695590ff0dSUlrich Drepper {
20705590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
20715590ff0dSUlrich Drepper }
20725590ff0dSUlrich Drepper 
20731da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
20741da177e4SLinus Torvalds {
2075a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
20761da177e4SLinus Torvalds 
20771da177e4SLinus Torvalds 	if (error)
20781da177e4SLinus Torvalds 		return error;
20791da177e4SLinus Torvalds 
2080acfa4380SAl Viro 	if (!dir->i_op->mkdir)
20811da177e4SLinus Torvalds 		return -EPERM;
20821da177e4SLinus Torvalds 
20831da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
20841da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
20851da177e4SLinus Torvalds 	if (error)
20861da177e4SLinus Torvalds 		return error;
20871da177e4SLinus Torvalds 
20889e3509e2SJan Kara 	vfs_dq_init(dir);
20891da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2090a74574aaSStephen Smalley 	if (!error)
2091f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
20921da177e4SLinus Torvalds 	return error;
20931da177e4SLinus Torvalds }
20941da177e4SLinus Torvalds 
20952e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
20961da177e4SLinus Torvalds {
20971da177e4SLinus Torvalds 	int error = 0;
20981da177e4SLinus Torvalds 	char * tmp;
20996902d925SDave Hansen 	struct dentry *dentry;
21006902d925SDave Hansen 	struct nameidata nd;
21011da177e4SLinus Torvalds 
21022ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &tmp);
21032ad94ae6SAl Viro 	if (error)
21046902d925SDave Hansen 		goto out_err;
21051da177e4SLinus Torvalds 
21061da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 1);
21071da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
21086902d925SDave Hansen 	if (IS_ERR(dentry))
21096902d925SDave Hansen 		goto out_unlock;
21106902d925SDave Hansen 
21114ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2112ce3b0f8dSAl Viro 		mode &= ~current_umask();
2113463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2114463c3197SDave Hansen 	if (error)
2115463c3197SDave Hansen 		goto out_dput;
2116be6d3e56SKentaro Takeda 	error = security_path_mkdir(&nd.path, dentry, mode);
2117be6d3e56SKentaro Takeda 	if (error)
2118be6d3e56SKentaro Takeda 		goto out_drop_write;
21194ac91378SJan Blunck 	error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2120be6d3e56SKentaro Takeda out_drop_write:
2121463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2122463c3197SDave Hansen out_dput:
21231da177e4SLinus Torvalds 	dput(dentry);
21246902d925SDave Hansen out_unlock:
21254ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
21261d957f9bSJan Blunck 	path_put(&nd.path);
21271da177e4SLinus Torvalds 	putname(tmp);
21286902d925SDave Hansen out_err:
21291da177e4SLinus Torvalds 	return error;
21301da177e4SLinus Torvalds }
21311da177e4SLinus Torvalds 
21323cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
21335590ff0dSUlrich Drepper {
21345590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
21355590ff0dSUlrich Drepper }
21365590ff0dSUlrich Drepper 
21371da177e4SLinus Torvalds /*
21381da177e4SLinus Torvalds  * We try to drop the dentry early: we should have
21391da177e4SLinus Torvalds  * a usage count of 2 if we're the only user of this
21401da177e4SLinus Torvalds  * dentry, and if that is true (possibly after pruning
21411da177e4SLinus Torvalds  * the dcache), then we drop the dentry now.
21421da177e4SLinus Torvalds  *
21431da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
21441da177e4SLinus Torvalds  * do a
21451da177e4SLinus Torvalds  *
21461da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
21471da177e4SLinus Torvalds  *		return -EBUSY;
21481da177e4SLinus Torvalds  *
21491da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
21501da177e4SLinus Torvalds  * that is still in use by something else..
21511da177e4SLinus Torvalds  */
21521da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
21531da177e4SLinus Torvalds {
21541da177e4SLinus Torvalds 	dget(dentry);
21551da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
21561da177e4SLinus Torvalds 	spin_lock(&dcache_lock);
21571da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
21581da177e4SLinus Torvalds 	if (atomic_read(&dentry->d_count) == 2)
21591da177e4SLinus Torvalds 		__d_drop(dentry);
21601da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
21611da177e4SLinus Torvalds 	spin_unlock(&dcache_lock);
21621da177e4SLinus Torvalds }
21631da177e4SLinus Torvalds 
21641da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
21651da177e4SLinus Torvalds {
21661da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
21671da177e4SLinus Torvalds 
21681da177e4SLinus Torvalds 	if (error)
21691da177e4SLinus Torvalds 		return error;
21701da177e4SLinus Torvalds 
2171acfa4380SAl Viro 	if (!dir->i_op->rmdir)
21721da177e4SLinus Torvalds 		return -EPERM;
21731da177e4SLinus Torvalds 
21749e3509e2SJan Kara 	vfs_dq_init(dir);
21751da177e4SLinus Torvalds 
21761b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
21771da177e4SLinus Torvalds 	dentry_unhash(dentry);
21781da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
21791da177e4SLinus Torvalds 		error = -EBUSY;
21801da177e4SLinus Torvalds 	else {
21811da177e4SLinus Torvalds 		error = security_inode_rmdir(dir, dentry);
21821da177e4SLinus Torvalds 		if (!error) {
21831da177e4SLinus Torvalds 			error = dir->i_op->rmdir(dir, dentry);
21841da177e4SLinus Torvalds 			if (!error)
21851da177e4SLinus Torvalds 				dentry->d_inode->i_flags |= S_DEAD;
21861da177e4SLinus Torvalds 		}
21871da177e4SLinus Torvalds 	}
21881b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
21891da177e4SLinus Torvalds 	if (!error) {
21901da177e4SLinus Torvalds 		d_delete(dentry);
21911da177e4SLinus Torvalds 	}
21921da177e4SLinus Torvalds 	dput(dentry);
21931da177e4SLinus Torvalds 
21941da177e4SLinus Torvalds 	return error;
21951da177e4SLinus Torvalds }
21961da177e4SLinus Torvalds 
21975590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
21981da177e4SLinus Torvalds {
21991da177e4SLinus Torvalds 	int error = 0;
22001da177e4SLinus Torvalds 	char * name;
22011da177e4SLinus Torvalds 	struct dentry *dentry;
22021da177e4SLinus Torvalds 	struct nameidata nd;
22031da177e4SLinus Torvalds 
22042ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
22051da177e4SLinus Torvalds 	if (error)
22062ad94ae6SAl Viro 		return error;
22071da177e4SLinus Torvalds 
22081da177e4SLinus Torvalds 	switch(nd.last_type) {
22091da177e4SLinus Torvalds 	case LAST_DOTDOT:
22101da177e4SLinus Torvalds 		error = -ENOTEMPTY;
22111da177e4SLinus Torvalds 		goto exit1;
22121da177e4SLinus Torvalds 	case LAST_DOT:
22131da177e4SLinus Torvalds 		error = -EINVAL;
22141da177e4SLinus Torvalds 		goto exit1;
22151da177e4SLinus Torvalds 	case LAST_ROOT:
22161da177e4SLinus Torvalds 		error = -EBUSY;
22171da177e4SLinus Torvalds 		goto exit1;
22181da177e4SLinus Torvalds 	}
22190612d9fbSOGAWA Hirofumi 
22200612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
22210612d9fbSOGAWA Hirofumi 
22224ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
222349705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
22241da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
22256902d925SDave Hansen 	if (IS_ERR(dentry))
22266902d925SDave Hansen 		goto exit2;
22270622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
22280622753bSDave Hansen 	if (error)
22290622753bSDave Hansen 		goto exit3;
2230be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2231be6d3e56SKentaro Takeda 	if (error)
2232be6d3e56SKentaro Takeda 		goto exit4;
22334ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2234be6d3e56SKentaro Takeda exit4:
22350622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
22360622753bSDave Hansen exit3:
22371da177e4SLinus Torvalds 	dput(dentry);
22386902d925SDave Hansen exit2:
22394ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
22401da177e4SLinus Torvalds exit1:
22411d957f9bSJan Blunck 	path_put(&nd.path);
22421da177e4SLinus Torvalds 	putname(name);
22431da177e4SLinus Torvalds 	return error;
22441da177e4SLinus Torvalds }
22451da177e4SLinus Torvalds 
22463cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
22475590ff0dSUlrich Drepper {
22485590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
22495590ff0dSUlrich Drepper }
22505590ff0dSUlrich Drepper 
22511da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
22521da177e4SLinus Torvalds {
22531da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
22541da177e4SLinus Torvalds 
22551da177e4SLinus Torvalds 	if (error)
22561da177e4SLinus Torvalds 		return error;
22571da177e4SLinus Torvalds 
2258acfa4380SAl Viro 	if (!dir->i_op->unlink)
22591da177e4SLinus Torvalds 		return -EPERM;
22601da177e4SLinus Torvalds 
22619e3509e2SJan Kara 	vfs_dq_init(dir);
22621da177e4SLinus Torvalds 
22631b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
22641da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
22651da177e4SLinus Torvalds 		error = -EBUSY;
22661da177e4SLinus Torvalds 	else {
22671da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2268bec1052eSAl Viro 		if (!error) {
22691da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2270bec1052eSAl Viro 			if (!error)
2271bec1052eSAl Viro 				dentry->d_inode->i_flags |= S_DEAD;
2272bec1052eSAl Viro 		}
22731da177e4SLinus Torvalds 	}
22741b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
22751da177e4SLinus Torvalds 
22761da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
22771da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2278ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
22791da177e4SLinus Torvalds 		d_delete(dentry);
22801da177e4SLinus Torvalds 	}
22810eeca283SRobert Love 
22821da177e4SLinus Torvalds 	return error;
22831da177e4SLinus Torvalds }
22841da177e4SLinus Torvalds 
22851da177e4SLinus Torvalds /*
22861da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
22871b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
22881da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
22891da177e4SLinus Torvalds  * while waiting on the I/O.
22901da177e4SLinus Torvalds  */
22915590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
22921da177e4SLinus Torvalds {
22932ad94ae6SAl Viro 	int error;
22941da177e4SLinus Torvalds 	char *name;
22951da177e4SLinus Torvalds 	struct dentry *dentry;
22961da177e4SLinus Torvalds 	struct nameidata nd;
22971da177e4SLinus Torvalds 	struct inode *inode = NULL;
22981da177e4SLinus Torvalds 
22992ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
23001da177e4SLinus Torvalds 	if (error)
23012ad94ae6SAl Viro 		return error;
23022ad94ae6SAl Viro 
23031da177e4SLinus Torvalds 	error = -EISDIR;
23041da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
23051da177e4SLinus Torvalds 		goto exit1;
23060612d9fbSOGAWA Hirofumi 
23070612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
23080612d9fbSOGAWA Hirofumi 
23094ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
231049705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
23111da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
23121da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
23131da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
23141da177e4SLinus Torvalds 		if (nd.last.name[nd.last.len])
23151da177e4SLinus Torvalds 			goto slashes;
23161da177e4SLinus Torvalds 		inode = dentry->d_inode;
23171da177e4SLinus Torvalds 		if (inode)
23181da177e4SLinus Torvalds 			atomic_inc(&inode->i_count);
23190622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
23200622753bSDave Hansen 		if (error)
23210622753bSDave Hansen 			goto exit2;
2322be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2323be6d3e56SKentaro Takeda 		if (error)
2324be6d3e56SKentaro Takeda 			goto exit3;
23254ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2326be6d3e56SKentaro Takeda exit3:
23270622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
23281da177e4SLinus Torvalds 	exit2:
23291da177e4SLinus Torvalds 		dput(dentry);
23301da177e4SLinus Torvalds 	}
23314ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
23321da177e4SLinus Torvalds 	if (inode)
23331da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
23341da177e4SLinus Torvalds exit1:
23351d957f9bSJan Blunck 	path_put(&nd.path);
23361da177e4SLinus Torvalds 	putname(name);
23371da177e4SLinus Torvalds 	return error;
23381da177e4SLinus Torvalds 
23391da177e4SLinus Torvalds slashes:
23401da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
23411da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
23421da177e4SLinus Torvalds 	goto exit2;
23431da177e4SLinus Torvalds }
23441da177e4SLinus Torvalds 
23452e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
23465590ff0dSUlrich Drepper {
23475590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
23485590ff0dSUlrich Drepper 		return -EINVAL;
23495590ff0dSUlrich Drepper 
23505590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
23515590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
23525590ff0dSUlrich Drepper 
23535590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
23545590ff0dSUlrich Drepper }
23555590ff0dSUlrich Drepper 
23563480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
23575590ff0dSUlrich Drepper {
23585590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
23595590ff0dSUlrich Drepper }
23605590ff0dSUlrich Drepper 
2361db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
23621da177e4SLinus Torvalds {
2363a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
23641da177e4SLinus Torvalds 
23651da177e4SLinus Torvalds 	if (error)
23661da177e4SLinus Torvalds 		return error;
23671da177e4SLinus Torvalds 
2368acfa4380SAl Viro 	if (!dir->i_op->symlink)
23691da177e4SLinus Torvalds 		return -EPERM;
23701da177e4SLinus Torvalds 
23711da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
23721da177e4SLinus Torvalds 	if (error)
23731da177e4SLinus Torvalds 		return error;
23741da177e4SLinus Torvalds 
23759e3509e2SJan Kara 	vfs_dq_init(dir);
23761da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2377a74574aaSStephen Smalley 	if (!error)
2378f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
23791da177e4SLinus Torvalds 	return error;
23801da177e4SLinus Torvalds }
23811da177e4SLinus Torvalds 
23822e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
23832e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
23841da177e4SLinus Torvalds {
23852ad94ae6SAl Viro 	int error;
23861da177e4SLinus Torvalds 	char *from;
23871da177e4SLinus Torvalds 	char *to;
23886902d925SDave Hansen 	struct dentry *dentry;
23896902d925SDave Hansen 	struct nameidata nd;
23901da177e4SLinus Torvalds 
23911da177e4SLinus Torvalds 	from = getname(oldname);
23921da177e4SLinus Torvalds 	if (IS_ERR(from))
23931da177e4SLinus Torvalds 		return PTR_ERR(from);
23942ad94ae6SAl Viro 
23952ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
23962ad94ae6SAl Viro 	if (error)
23976902d925SDave Hansen 		goto out_putname;
23981da177e4SLinus Torvalds 
23991da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
24001da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
24016902d925SDave Hansen 	if (IS_ERR(dentry))
24026902d925SDave Hansen 		goto out_unlock;
24036902d925SDave Hansen 
240475c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
240575c3f29dSDave Hansen 	if (error)
240675c3f29dSDave Hansen 		goto out_dput;
2407be6d3e56SKentaro Takeda 	error = security_path_symlink(&nd.path, dentry, from);
2408be6d3e56SKentaro Takeda 	if (error)
2409be6d3e56SKentaro Takeda 		goto out_drop_write;
2410db2e747bSMiklos Szeredi 	error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2411be6d3e56SKentaro Takeda out_drop_write:
241275c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
241375c3f29dSDave Hansen out_dput:
24141da177e4SLinus Torvalds 	dput(dentry);
24156902d925SDave Hansen out_unlock:
24164ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
24171d957f9bSJan Blunck 	path_put(&nd.path);
24181da177e4SLinus Torvalds 	putname(to);
24196902d925SDave Hansen out_putname:
24201da177e4SLinus Torvalds 	putname(from);
24211da177e4SLinus Torvalds 	return error;
24221da177e4SLinus Torvalds }
24231da177e4SLinus Torvalds 
24243480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
24255590ff0dSUlrich Drepper {
24265590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
24275590ff0dSUlrich Drepper }
24285590ff0dSUlrich Drepper 
24291da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
24301da177e4SLinus Torvalds {
24311da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
24321da177e4SLinus Torvalds 	int error;
24331da177e4SLinus Torvalds 
24341da177e4SLinus Torvalds 	if (!inode)
24351da177e4SLinus Torvalds 		return -ENOENT;
24361da177e4SLinus Torvalds 
2437a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
24381da177e4SLinus Torvalds 	if (error)
24391da177e4SLinus Torvalds 		return error;
24401da177e4SLinus Torvalds 
24411da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
24421da177e4SLinus Torvalds 		return -EXDEV;
24431da177e4SLinus Torvalds 
24441da177e4SLinus Torvalds 	/*
24451da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
24461da177e4SLinus Torvalds 	 */
24471da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
24481da177e4SLinus Torvalds 		return -EPERM;
2449acfa4380SAl Viro 	if (!dir->i_op->link)
24501da177e4SLinus Torvalds 		return -EPERM;
24517e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
24521da177e4SLinus Torvalds 		return -EPERM;
24531da177e4SLinus Torvalds 
24541da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
24551da177e4SLinus Torvalds 	if (error)
24561da177e4SLinus Torvalds 		return error;
24571da177e4SLinus Torvalds 
24587e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
24599e3509e2SJan Kara 	vfs_dq_init(dir);
24601da177e4SLinus Torvalds 	error = dir->i_op->link(old_dentry, dir, new_dentry);
24617e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
2462e31e14ecSStephen Smalley 	if (!error)
24637e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
24641da177e4SLinus Torvalds 	return error;
24651da177e4SLinus Torvalds }
24661da177e4SLinus Torvalds 
24671da177e4SLinus Torvalds /*
24681da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
24691da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
24701da177e4SLinus Torvalds  * newname.  --KAB
24711da177e4SLinus Torvalds  *
24721da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
24731da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
24741da177e4SLinus Torvalds  * and other special files.  --ADM
24751da177e4SLinus Torvalds  */
24762e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
24772e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
24781da177e4SLinus Torvalds {
24791da177e4SLinus Torvalds 	struct dentry *new_dentry;
24802d8f3038SAl Viro 	struct nameidata nd;
24812d8f3038SAl Viro 	struct path old_path;
24821da177e4SLinus Torvalds 	int error;
24831da177e4SLinus Torvalds 	char *to;
24841da177e4SLinus Torvalds 
248545c9b11aSUlrich Drepper 	if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
2486c04030e1SUlrich Drepper 		return -EINVAL;
2487c04030e1SUlrich Drepper 
24882d8f3038SAl Viro 	error = user_path_at(olddfd, oldname,
248945c9b11aSUlrich Drepper 			     flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
24902d8f3038SAl Viro 			     &old_path);
24911da177e4SLinus Torvalds 	if (error)
24922ad94ae6SAl Viro 		return error;
24932ad94ae6SAl Viro 
24942ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
24951da177e4SLinus Torvalds 	if (error)
24961da177e4SLinus Torvalds 		goto out;
24971da177e4SLinus Torvalds 	error = -EXDEV;
24982d8f3038SAl Viro 	if (old_path.mnt != nd.path.mnt)
24991da177e4SLinus Torvalds 		goto out_release;
25001da177e4SLinus Torvalds 	new_dentry = lookup_create(&nd, 0);
25011da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
25026902d925SDave Hansen 	if (IS_ERR(new_dentry))
25036902d925SDave Hansen 		goto out_unlock;
250475c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
250575c3f29dSDave Hansen 	if (error)
250675c3f29dSDave Hansen 		goto out_dput;
2507be6d3e56SKentaro Takeda 	error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2508be6d3e56SKentaro Takeda 	if (error)
2509be6d3e56SKentaro Takeda 		goto out_drop_write;
25102d8f3038SAl Viro 	error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
2511be6d3e56SKentaro Takeda out_drop_write:
251275c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
251375c3f29dSDave Hansen out_dput:
25141da177e4SLinus Torvalds 	dput(new_dentry);
25156902d925SDave Hansen out_unlock:
25164ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
25171da177e4SLinus Torvalds out_release:
25181d957f9bSJan Blunck 	path_put(&nd.path);
25192ad94ae6SAl Viro 	putname(to);
25201da177e4SLinus Torvalds out:
25212d8f3038SAl Viro 	path_put(&old_path);
25221da177e4SLinus Torvalds 
25231da177e4SLinus Torvalds 	return error;
25241da177e4SLinus Torvalds }
25251da177e4SLinus Torvalds 
25263480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
25275590ff0dSUlrich Drepper {
2528c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
25295590ff0dSUlrich Drepper }
25305590ff0dSUlrich Drepper 
25311da177e4SLinus Torvalds /*
25321da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
25331da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
25341da177e4SLinus Torvalds  * Problems:
25351da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
25361da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
25371da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
2538a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
25391da177e4SLinus Torvalds  *	   story.
25401da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
25411b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
25421da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
25431da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
2544a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
25451da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
25461da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
25471da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
2548a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
25491da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
25501da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
25511da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
25521da177e4SLinus Torvalds  *	d) some filesystems don't support opened-but-unlinked directories,
25531da177e4SLinus Torvalds  *	   either because of layout or because they are not ready to deal with
25541da177e4SLinus Torvalds  *	   all cases correctly. The latter will be fixed (taking this sort of
25551da177e4SLinus Torvalds  *	   stuff into VFS), but the former is not going away. Solution: the same
25561da177e4SLinus Torvalds  *	   trick as in rmdir().
25571da177e4SLinus Torvalds  *	e) conversion from fhandle to dentry may come in the wrong moment - when
25581b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
25591da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
25601b1dcc1bSJes Sorensen  *	   ->i_mutex on parents, which works but leads to some truely excessive
25611da177e4SLinus Torvalds  *	   locking].
25621da177e4SLinus Torvalds  */
256375c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
25641da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
25651da177e4SLinus Torvalds {
25661da177e4SLinus Torvalds 	int error = 0;
25671da177e4SLinus Torvalds 	struct inode *target;
25681da177e4SLinus Torvalds 
25691da177e4SLinus Torvalds 	/*
25701da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
25711da177e4SLinus Torvalds 	 * we'll need to flip '..'.
25721da177e4SLinus Torvalds 	 */
25731da177e4SLinus Torvalds 	if (new_dir != old_dir) {
2574f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
25751da177e4SLinus Torvalds 		if (error)
25761da177e4SLinus Torvalds 			return error;
25771da177e4SLinus Torvalds 	}
25781da177e4SLinus Torvalds 
25791da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
25801da177e4SLinus Torvalds 	if (error)
25811da177e4SLinus Torvalds 		return error;
25821da177e4SLinus Torvalds 
25831da177e4SLinus Torvalds 	target = new_dentry->d_inode;
25841da177e4SLinus Torvalds 	if (target) {
25851b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
25861da177e4SLinus Torvalds 		dentry_unhash(new_dentry);
25871da177e4SLinus Torvalds 	}
25881da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
25891da177e4SLinus Torvalds 		error = -EBUSY;
25901da177e4SLinus Torvalds 	else
25911da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
25921da177e4SLinus Torvalds 	if (target) {
25931da177e4SLinus Torvalds 		if (!error)
25941da177e4SLinus Torvalds 			target->i_flags |= S_DEAD;
25951b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
25961da177e4SLinus Torvalds 		if (d_unhashed(new_dentry))
25971da177e4SLinus Torvalds 			d_rehash(new_dentry);
25981da177e4SLinus Torvalds 		dput(new_dentry);
25991da177e4SLinus Torvalds 	}
2600e31e14ecSStephen Smalley 	if (!error)
2601349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
26021da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
26031da177e4SLinus Torvalds 	return error;
26041da177e4SLinus Torvalds }
26051da177e4SLinus Torvalds 
260675c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
26071da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
26081da177e4SLinus Torvalds {
26091da177e4SLinus Torvalds 	struct inode *target;
26101da177e4SLinus Torvalds 	int error;
26111da177e4SLinus Torvalds 
26121da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
26131da177e4SLinus Torvalds 	if (error)
26141da177e4SLinus Torvalds 		return error;
26151da177e4SLinus Torvalds 
26161da177e4SLinus Torvalds 	dget(new_dentry);
26171da177e4SLinus Torvalds 	target = new_dentry->d_inode;
26181da177e4SLinus Torvalds 	if (target)
26191b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
26201da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
26211da177e4SLinus Torvalds 		error = -EBUSY;
26221da177e4SLinus Torvalds 	else
26231da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
26241da177e4SLinus Torvalds 	if (!error) {
2625bec1052eSAl Viro 		if (target)
2626bec1052eSAl Viro 			target->i_flags |= S_DEAD;
2627349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
26281da177e4SLinus Torvalds 			d_move(old_dentry, new_dentry);
26291da177e4SLinus Torvalds 	}
26301da177e4SLinus Torvalds 	if (target)
26311b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
26321da177e4SLinus Torvalds 	dput(new_dentry);
26331da177e4SLinus Torvalds 	return error;
26341da177e4SLinus Torvalds }
26351da177e4SLinus Torvalds 
26361da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
26371da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
26381da177e4SLinus Torvalds {
26391da177e4SLinus Torvalds 	int error;
26401da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
26410eeca283SRobert Love 	const char *old_name;
26421da177e4SLinus Torvalds 
26431da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
26441da177e4SLinus Torvalds  		return 0;
26451da177e4SLinus Torvalds 
26461da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
26471da177e4SLinus Torvalds 	if (error)
26481da177e4SLinus Torvalds 		return error;
26491da177e4SLinus Torvalds 
26501da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
2651a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
26521da177e4SLinus Torvalds 	else
26531da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
26541da177e4SLinus Torvalds 	if (error)
26551da177e4SLinus Torvalds 		return error;
26561da177e4SLinus Torvalds 
2657acfa4380SAl Viro 	if (!old_dir->i_op->rename)
26581da177e4SLinus Torvalds 		return -EPERM;
26591da177e4SLinus Torvalds 
26609e3509e2SJan Kara 	vfs_dq_init(old_dir);
26619e3509e2SJan Kara 	vfs_dq_init(new_dir);
26621da177e4SLinus Torvalds 
26630eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
26640eeca283SRobert Love 
26651da177e4SLinus Torvalds 	if (is_dir)
26661da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
26671da177e4SLinus Torvalds 	else
26681da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2669123df294SAl Viro 	if (!error)
2670123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
26715a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
26720eeca283SRobert Love 	fsnotify_oldname_free(old_name);
26730eeca283SRobert Love 
26741da177e4SLinus Torvalds 	return error;
26751da177e4SLinus Torvalds }
26761da177e4SLinus Torvalds 
26772e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
26782e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
26791da177e4SLinus Torvalds {
26801da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
26811da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
26821da177e4SLinus Torvalds 	struct dentry *trap;
26831da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
26842ad94ae6SAl Viro 	char *from;
26852ad94ae6SAl Viro 	char *to;
26862ad94ae6SAl Viro 	int error;
26871da177e4SLinus Torvalds 
26882ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
26891da177e4SLinus Torvalds 	if (error)
26901da177e4SLinus Torvalds 		goto exit;
26911da177e4SLinus Torvalds 
26922ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
26931da177e4SLinus Torvalds 	if (error)
26941da177e4SLinus Torvalds 		goto exit1;
26951da177e4SLinus Torvalds 
26961da177e4SLinus Torvalds 	error = -EXDEV;
26974ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
26981da177e4SLinus Torvalds 		goto exit2;
26991da177e4SLinus Torvalds 
27004ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
27011da177e4SLinus Torvalds 	error = -EBUSY;
27021da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
27031da177e4SLinus Torvalds 		goto exit2;
27041da177e4SLinus Torvalds 
27054ac91378SJan Blunck 	new_dir = newnd.path.dentry;
27061da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
27071da177e4SLinus Torvalds 		goto exit2;
27081da177e4SLinus Torvalds 
27090612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
27100612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
27114e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
27120612d9fbSOGAWA Hirofumi 
27131da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
27141da177e4SLinus Torvalds 
271549705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
27161da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
27171da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
27181da177e4SLinus Torvalds 		goto exit3;
27191da177e4SLinus Torvalds 	/* source must exist */
27201da177e4SLinus Torvalds 	error = -ENOENT;
27211da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
27221da177e4SLinus Torvalds 		goto exit4;
27231da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
27241da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
27251da177e4SLinus Torvalds 		error = -ENOTDIR;
27261da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
27271da177e4SLinus Torvalds 			goto exit4;
27281da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
27291da177e4SLinus Torvalds 			goto exit4;
27301da177e4SLinus Torvalds 	}
27311da177e4SLinus Torvalds 	/* source should not be ancestor of target */
27321da177e4SLinus Torvalds 	error = -EINVAL;
27331da177e4SLinus Torvalds 	if (old_dentry == trap)
27341da177e4SLinus Torvalds 		goto exit4;
273549705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
27361da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
27371da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
27381da177e4SLinus Torvalds 		goto exit4;
27391da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
27401da177e4SLinus Torvalds 	error = -ENOTEMPTY;
27411da177e4SLinus Torvalds 	if (new_dentry == trap)
27421da177e4SLinus Torvalds 		goto exit5;
27431da177e4SLinus Torvalds 
27449079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
27459079b1ebSDave Hansen 	if (error)
27469079b1ebSDave Hansen 		goto exit5;
2747be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
2748be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
2749be6d3e56SKentaro Takeda 	if (error)
2750be6d3e56SKentaro Takeda 		goto exit6;
27511da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
27521da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
2753be6d3e56SKentaro Takeda exit6:
27549079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
27551da177e4SLinus Torvalds exit5:
27561da177e4SLinus Torvalds 	dput(new_dentry);
27571da177e4SLinus Torvalds exit4:
27581da177e4SLinus Torvalds 	dput(old_dentry);
27591da177e4SLinus Torvalds exit3:
27601da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
27611da177e4SLinus Torvalds exit2:
27621d957f9bSJan Blunck 	path_put(&newnd.path);
27632ad94ae6SAl Viro 	putname(to);
27641da177e4SLinus Torvalds exit1:
27651d957f9bSJan Blunck 	path_put(&oldnd.path);
27661da177e4SLinus Torvalds 	putname(from);
27672ad94ae6SAl Viro exit:
27681da177e4SLinus Torvalds 	return error;
27691da177e4SLinus Torvalds }
27701da177e4SLinus Torvalds 
2771a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
27725590ff0dSUlrich Drepper {
27735590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
27745590ff0dSUlrich Drepper }
27755590ff0dSUlrich Drepper 
27761da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
27771da177e4SLinus Torvalds {
27781da177e4SLinus Torvalds 	int len;
27791da177e4SLinus Torvalds 
27801da177e4SLinus Torvalds 	len = PTR_ERR(link);
27811da177e4SLinus Torvalds 	if (IS_ERR(link))
27821da177e4SLinus Torvalds 		goto out;
27831da177e4SLinus Torvalds 
27841da177e4SLinus Torvalds 	len = strlen(link);
27851da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
27861da177e4SLinus Torvalds 		len = buflen;
27871da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
27881da177e4SLinus Torvalds 		len = -EFAULT;
27891da177e4SLinus Torvalds out:
27901da177e4SLinus Torvalds 	return len;
27911da177e4SLinus Torvalds }
27921da177e4SLinus Torvalds 
27931da177e4SLinus Torvalds /*
27941da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
27951da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
27961da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
27971da177e4SLinus Torvalds  */
27981da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
27991da177e4SLinus Torvalds {
28001da177e4SLinus Torvalds 	struct nameidata nd;
2801cc314eefSLinus Torvalds 	void *cookie;
2802694a1764SMarcin Slusarz 	int res;
2803cc314eefSLinus Torvalds 
28041da177e4SLinus Torvalds 	nd.depth = 0;
2805cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
2806694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
2807694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
2808694a1764SMarcin Slusarz 
2809694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
28101da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
2811cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2812694a1764SMarcin Slusarz 	return res;
28131da177e4SLinus Torvalds }
28141da177e4SLinus Torvalds 
28151da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
28161da177e4SLinus Torvalds {
28171da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
28181da177e4SLinus Torvalds }
28191da177e4SLinus Torvalds 
28201da177e4SLinus Torvalds /* get the link contents into pagecache */
28211da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
28221da177e4SLinus Torvalds {
2823ebd09abbSDuane Griffin 	char *kaddr;
28241da177e4SLinus Torvalds 	struct page *page;
28251da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
2826090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
28271da177e4SLinus Torvalds 	if (IS_ERR(page))
28286fe6900eSNick Piggin 		return (char*)page;
28291da177e4SLinus Torvalds 	*ppage = page;
2830ebd09abbSDuane Griffin 	kaddr = kmap(page);
2831ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
2832ebd09abbSDuane Griffin 	return kaddr;
28331da177e4SLinus Torvalds }
28341da177e4SLinus Torvalds 
28351da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
28361da177e4SLinus Torvalds {
28371da177e4SLinus Torvalds 	struct page *page = NULL;
28381da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
28391da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
28401da177e4SLinus Torvalds 	if (page) {
28411da177e4SLinus Torvalds 		kunmap(page);
28421da177e4SLinus Torvalds 		page_cache_release(page);
28431da177e4SLinus Torvalds 	}
28441da177e4SLinus Torvalds 	return res;
28451da177e4SLinus Torvalds }
28461da177e4SLinus Torvalds 
2847cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
28481da177e4SLinus Torvalds {
2849cc314eefSLinus Torvalds 	struct page *page = NULL;
28501da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
2851cc314eefSLinus Torvalds 	return page;
28521da177e4SLinus Torvalds }
28531da177e4SLinus Torvalds 
2854cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
28551da177e4SLinus Torvalds {
2856cc314eefSLinus Torvalds 	struct page *page = cookie;
2857cc314eefSLinus Torvalds 
2858cc314eefSLinus Torvalds 	if (page) {
28591da177e4SLinus Torvalds 		kunmap(page);
28601da177e4SLinus Torvalds 		page_cache_release(page);
28611da177e4SLinus Torvalds 	}
28621da177e4SLinus Torvalds }
28631da177e4SLinus Torvalds 
286454566b2cSNick Piggin /*
286554566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
286654566b2cSNick Piggin  */
286754566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
28681da177e4SLinus Torvalds {
28691da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
28700adb25d2SKirill Korotaev 	struct page *page;
2871afddba49SNick Piggin 	void *fsdata;
2872beb497abSDmitriy Monakhov 	int err;
28731da177e4SLinus Torvalds 	char *kaddr;
287454566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
287554566b2cSNick Piggin 	if (nofs)
287654566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
28771da177e4SLinus Torvalds 
28787e53cac4SNeilBrown retry:
2879afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
288054566b2cSNick Piggin 				flags, &page, &fsdata);
28811da177e4SLinus Torvalds 	if (err)
2882afddba49SNick Piggin 		goto fail;
2883afddba49SNick Piggin 
28841da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
28851da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
28861da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
2887afddba49SNick Piggin 
2888afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
2889afddba49SNick Piggin 							page, fsdata);
28901da177e4SLinus Torvalds 	if (err < 0)
28911da177e4SLinus Torvalds 		goto fail;
2892afddba49SNick Piggin 	if (err < len-1)
2893afddba49SNick Piggin 		goto retry;
2894afddba49SNick Piggin 
28951da177e4SLinus Torvalds 	mark_inode_dirty(inode);
28961da177e4SLinus Torvalds 	return 0;
28971da177e4SLinus Torvalds fail:
28981da177e4SLinus Torvalds 	return err;
28991da177e4SLinus Torvalds }
29001da177e4SLinus Torvalds 
29010adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
29020adb25d2SKirill Korotaev {
29030adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
290454566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
29050adb25d2SKirill Korotaev }
29060adb25d2SKirill Korotaev 
290792e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
29081da177e4SLinus Torvalds 	.readlink	= generic_readlink,
29091da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
29101da177e4SLinus Torvalds 	.put_link	= page_put_link,
29111da177e4SLinus Torvalds };
29121da177e4SLinus Torvalds 
29132d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
29141da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
29151da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
29161da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
29171da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
29181da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
29191da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
29201da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
29211da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
29221da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
29230adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
29241da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
29251da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
29261da177e4SLinus Torvalds EXPORT_SYMBOL(path_lookup);
2927d1811465SAl Viro EXPORT_SYMBOL(kern_path);
292816f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
2929f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
29308c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission);
29311da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
29321da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
29331da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
29341da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
29351da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
29361da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
29371da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
29381da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
29391da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
29401da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
29411da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
29421da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
29431da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
29441da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
2945