xref: /openbmc/linux/fs/namei.c (revision 7715b521)
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 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer]
431da177e4SLinus Torvalds  * Fundamental changes in the pathname lookup mechanisms (namei)
441da177e4SLinus Torvalds  * were necessary because of omirr.  The reason is that omirr needs
451da177e4SLinus Torvalds  * to know the _real_ pathname, not the user-supplied one, in case
461da177e4SLinus Torvalds  * of symlinks (and also when transname replacements occur).
471da177e4SLinus Torvalds  *
481da177e4SLinus Torvalds  * The new code replaces the old recursive symlink resolution with
491da177e4SLinus Torvalds  * an iterative one (in case of non-nested symlink chains).  It does
501da177e4SLinus Torvalds  * this with calls to <fs>_follow_link().
511da177e4SLinus Torvalds  * As a side effect, dir_namei(), _namei() and follow_link() are now
521da177e4SLinus Torvalds  * replaced with a single function lookup_dentry() that can handle all
531da177e4SLinus Torvalds  * the special cases of the former code.
541da177e4SLinus Torvalds  *
551da177e4SLinus Torvalds  * With the new dcache, the pathname is stored at each inode, at least as
561da177e4SLinus Torvalds  * long as the refcount of the inode is positive.  As a side effect, the
571da177e4SLinus Torvalds  * size of the dcache depends on the inode cache and thus is dynamic.
581da177e4SLinus Torvalds  *
591da177e4SLinus Torvalds  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
601da177e4SLinus Torvalds  * resolution to correspond with current state of the code.
611da177e4SLinus Torvalds  *
621da177e4SLinus Torvalds  * Note that the symlink resolution is not *completely* iterative.
631da177e4SLinus Torvalds  * There is still a significant amount of tail- and mid- recursion in
641da177e4SLinus Torvalds  * the algorithm.  Also, note that <fs>_readlink() is not used in
651da177e4SLinus Torvalds  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
661da177e4SLinus Torvalds  * may return different results than <fs>_follow_link().  Many virtual
671da177e4SLinus Torvalds  * filesystems (including /proc) exhibit this behavior.
681da177e4SLinus Torvalds  */
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
711da177e4SLinus Torvalds  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
721da177e4SLinus Torvalds  * and the name already exists in form of a symlink, try to create the new
731da177e4SLinus Torvalds  * name indicated by the symlink. The old code always complained that the
741da177e4SLinus Torvalds  * name already exists, due to not following the symlink even if its target
751da177e4SLinus Torvalds  * is nonexistent.  The new semantics affects also mknod() and link() when
761da177e4SLinus Torvalds  * the name is a symlink pointing to a non-existant name.
771da177e4SLinus Torvalds  *
781da177e4SLinus Torvalds  * I don't know which semantics is the right one, since I have no access
791da177e4SLinus Torvalds  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
801da177e4SLinus Torvalds  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
811da177e4SLinus Torvalds  * "old" one. Personally, I think the new semantics is much more logical.
821da177e4SLinus Torvalds  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
831da177e4SLinus Torvalds  * file does succeed in both HP-UX and SunOs, but not in Solaris
841da177e4SLinus Torvalds  * and in the old Linux semantics.
851da177e4SLinus Torvalds  */
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
881da177e4SLinus Torvalds  * semantics.  See the comments in "open_namei" and "do_link" below.
891da177e4SLinus Torvalds  *
901da177e4SLinus Torvalds  * [10-Sep-98 Alan Modra] Another symlink change.
911da177e4SLinus Torvalds  */
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
941da177e4SLinus Torvalds  *	inside the path - always follow.
951da177e4SLinus Torvalds  *	in the last component in creation/removal/renaming - never follow.
961da177e4SLinus Torvalds  *	if LOOKUP_FOLLOW passed - follow.
971da177e4SLinus Torvalds  *	if the pathname has trailing slashes - follow.
981da177e4SLinus Torvalds  *	otherwise - don't follow.
991da177e4SLinus Torvalds  * (applied in that order).
1001da177e4SLinus Torvalds  *
1011da177e4SLinus Torvalds  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
1021da177e4SLinus Torvalds  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
1031da177e4SLinus Torvalds  * During the 2.4 we need to fix the userland stuff depending on it -
1041da177e4SLinus Torvalds  * hopefully we will be able to get rid of that wart in 2.5. So far only
1051da177e4SLinus Torvalds  * XEmacs seems to be relying on it...
1061da177e4SLinus Torvalds  */
1071da177e4SLinus Torvalds /*
1081da177e4SLinus Torvalds  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
109a11f3a05SArjan van de Ven  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
1101da177e4SLinus Torvalds  * any extra contention...
1111da177e4SLinus Torvalds  */
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional
1141da177e4SLinus Torvalds  * checking and hopefully speeding things up, we copy filenames to the
1151da177e4SLinus Torvalds  * kernel data space before using them..
1161da177e4SLinus Torvalds  *
1171da177e4SLinus Torvalds  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
1181da177e4SLinus Torvalds  * PATH_MAX includes the nul terminator --RR.
1191da177e4SLinus Torvalds  */
120858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page)
1211da177e4SLinus Torvalds {
1221da177e4SLinus Torvalds 	int retval;
1231da177e4SLinus Torvalds 	unsigned long len = PATH_MAX;
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	if (!segment_eq(get_fs(), KERNEL_DS)) {
1261da177e4SLinus Torvalds 		if ((unsigned long) filename >= TASK_SIZE)
1271da177e4SLinus Torvalds 			return -EFAULT;
1281da177e4SLinus Torvalds 		if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
1291da177e4SLinus Torvalds 			len = TASK_SIZE - (unsigned long) filename;
1301da177e4SLinus Torvalds 	}
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	retval = strncpy_from_user(page, filename, len);
1331da177e4SLinus Torvalds 	if (retval > 0) {
1341da177e4SLinus Torvalds 		if (retval < len)
1351da177e4SLinus Torvalds 			return 0;
1361da177e4SLinus Torvalds 		return -ENAMETOOLONG;
1371da177e4SLinus Torvalds 	} else if (!retval)
1381da177e4SLinus Torvalds 		retval = -ENOENT;
1391da177e4SLinus Torvalds 	return retval;
1401da177e4SLinus Torvalds }
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds char * getname(const char __user * filename)
1431da177e4SLinus Torvalds {
1441da177e4SLinus Torvalds 	char *tmp, *result;
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	result = ERR_PTR(-ENOMEM);
1471da177e4SLinus Torvalds 	tmp = __getname();
1481da177e4SLinus Torvalds 	if (tmp)  {
1491da177e4SLinus Torvalds 		int retval = do_getname(filename, tmp);
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 		result = tmp;
1521da177e4SLinus Torvalds 		if (retval < 0) {
1531da177e4SLinus Torvalds 			__putname(tmp);
1541da177e4SLinus Torvalds 			result = ERR_PTR(retval);
1551da177e4SLinus Torvalds 		}
1561da177e4SLinus Torvalds 	}
1571da177e4SLinus Torvalds 	audit_getname(result);
1581da177e4SLinus Torvalds 	return result;
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
1621da177e4SLinus Torvalds void putname(const char *name)
1631da177e4SLinus Torvalds {
1645ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
1651da177e4SLinus Torvalds 		audit_putname(name);
1661da177e4SLinus Torvalds 	else
1671da177e4SLinus Torvalds 		__putname(name);
1681da177e4SLinus Torvalds }
1691da177e4SLinus Torvalds EXPORT_SYMBOL(putname);
1701da177e4SLinus Torvalds #endif
1711da177e4SLinus Torvalds 
1725909ccaaSLinus Torvalds /*
1735909ccaaSLinus Torvalds  * This does basic POSIX ACL permission checking
1745909ccaaSLinus Torvalds  */
1755909ccaaSLinus Torvalds static int acl_permission_check(struct inode *inode, int mask,
1765909ccaaSLinus Torvalds 		int (*check_acl)(struct inode *inode, int mask))
1775909ccaaSLinus Torvalds {
1785909ccaaSLinus Torvalds 	umode_t			mode = inode->i_mode;
1795909ccaaSLinus Torvalds 
1805909ccaaSLinus Torvalds 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
1815909ccaaSLinus Torvalds 
1825909ccaaSLinus Torvalds 	if (current_fsuid() == inode->i_uid)
1835909ccaaSLinus Torvalds 		mode >>= 6;
1845909ccaaSLinus Torvalds 	else {
1855909ccaaSLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
1865909ccaaSLinus Torvalds 			int error = check_acl(inode, mask);
1875909ccaaSLinus Torvalds 			if (error != -EAGAIN)
1885909ccaaSLinus Torvalds 				return error;
1895909ccaaSLinus Torvalds 		}
1905909ccaaSLinus Torvalds 
1915909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
1925909ccaaSLinus Torvalds 			mode >>= 3;
1935909ccaaSLinus Torvalds 	}
1945909ccaaSLinus Torvalds 
1955909ccaaSLinus Torvalds 	/*
1965909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
1975909ccaaSLinus Torvalds 	 */
1985909ccaaSLinus Torvalds 	if ((mask & ~mode) == 0)
1995909ccaaSLinus Torvalds 		return 0;
2005909ccaaSLinus Torvalds 	return -EACCES;
2015909ccaaSLinus Torvalds }
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds /**
2041da177e4SLinus Torvalds  * generic_permission  -  check for access rights on a Posix-like filesystem
2051da177e4SLinus Torvalds  * @inode:	inode to check access rights for
2061da177e4SLinus Torvalds  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
2071da177e4SLinus Torvalds  * @check_acl:	optional callback to check for Posix ACLs
2081da177e4SLinus Torvalds  *
2091da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
2101da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
2111da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
2121da177e4SLinus Torvalds  * are used for other things..
2131da177e4SLinus Torvalds  */
2141da177e4SLinus Torvalds int generic_permission(struct inode *inode, int mask,
2151da177e4SLinus Torvalds 		int (*check_acl)(struct inode *inode, int mask))
2161da177e4SLinus Torvalds {
2175909ccaaSLinus Torvalds 	int ret;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	/*
2205909ccaaSLinus Torvalds 	 * Do the basic POSIX ACL permission checks.
2211da177e4SLinus Torvalds 	 */
2225909ccaaSLinus Torvalds 	ret = acl_permission_check(inode, mask, check_acl);
2235909ccaaSLinus Torvalds 	if (ret != -EACCES)
2245909ccaaSLinus Torvalds 		return ret;
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	/*
2271da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
2281da177e4SLinus Torvalds 	 * Executable DACs are overridable if at least one exec bit is set.
2291da177e4SLinus Torvalds 	 */
230f696a365SMiklos Szeredi 	if (!(mask & MAY_EXEC) || execute_ok(inode))
2311da177e4SLinus Torvalds 		if (capable(CAP_DAC_OVERRIDE))
2321da177e4SLinus Torvalds 			return 0;
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	/*
2351da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
2361da177e4SLinus Torvalds 	 */
2371da177e4SLinus Torvalds 	if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
2381da177e4SLinus Torvalds 		if (capable(CAP_DAC_READ_SEARCH))
2391da177e4SLinus Torvalds 			return 0;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	return -EACCES;
2421da177e4SLinus Torvalds }
2431da177e4SLinus Torvalds 
244cb23beb5SChristoph Hellwig /**
245cb23beb5SChristoph Hellwig  * inode_permission  -  check for access rights to a given inode
246cb23beb5SChristoph Hellwig  * @inode:	inode to check permission on
247cb23beb5SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
248cb23beb5SChristoph Hellwig  *
249cb23beb5SChristoph Hellwig  * Used to check for read/write/execute permissions on an inode.
250cb23beb5SChristoph Hellwig  * We use "fsuid" for this, letting us set arbitrary permissions
251cb23beb5SChristoph Hellwig  * for filesystem access without changing the "normal" uids which
252cb23beb5SChristoph Hellwig  * are used for other things.
253cb23beb5SChristoph Hellwig  */
254f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask)
2551da177e4SLinus Torvalds {
256e6305c43SAl Viro 	int retval;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	if (mask & MAY_WRITE) {
25922590e41SMiklos Szeredi 		umode_t mode = inode->i_mode;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 		/*
2621da177e4SLinus Torvalds 		 * Nobody gets write access to a read-only fs.
2631da177e4SLinus Torvalds 		 */
2641da177e4SLinus Torvalds 		if (IS_RDONLY(inode) &&
2651da177e4SLinus Torvalds 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
2661da177e4SLinus Torvalds 			return -EROFS;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 		/*
2691da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
2701da177e4SLinus Torvalds 		 */
2711da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
2721da177e4SLinus Torvalds 			return -EACCES;
2731da177e4SLinus Torvalds 	}
2741da177e4SLinus Torvalds 
275acfa4380SAl Viro 	if (inode->i_op->permission)
276b77b0646SAl Viro 		retval = inode->i_op->permission(inode, mask);
277f696a365SMiklos Szeredi 	else
2785909ccaaSLinus Torvalds 		retval = generic_permission(inode, mask, inode->i_op->check_acl);
279f696a365SMiklos Szeredi 
2801da177e4SLinus Torvalds 	if (retval)
2811da177e4SLinus Torvalds 		return retval;
2821da177e4SLinus Torvalds 
28308ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
28408ce5f16SSerge E. Hallyn 	if (retval)
28508ce5f16SSerge E. Hallyn 		return retval;
28608ce5f16SSerge E. Hallyn 
287e6305c43SAl Viro 	return security_inode_permission(inode,
288f418b006SStephen Smalley 			mask & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND));
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds 
291e4543eddSChristoph Hellwig /**
2928c744fb8SChristoph Hellwig  * file_permission  -  check for additional access rights to a given file
2938c744fb8SChristoph Hellwig  * @file:	file to check access rights for
2948c744fb8SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
2958c744fb8SChristoph Hellwig  *
2968c744fb8SChristoph Hellwig  * Used to check for read/write/execute permissions on an already opened
2978c744fb8SChristoph Hellwig  * file.
2988c744fb8SChristoph Hellwig  *
2998c744fb8SChristoph Hellwig  * Note:
3008c744fb8SChristoph Hellwig  *	Do not use this function in new code.  All access checks should
301cb23beb5SChristoph Hellwig  *	be done using inode_permission().
3028c744fb8SChristoph Hellwig  */
3038c744fb8SChristoph Hellwig int file_permission(struct file *file, int mask)
3048c744fb8SChristoph Hellwig {
305f419a2e3SAl Viro 	return inode_permission(file->f_path.dentry->d_inode, mask);
3068c744fb8SChristoph Hellwig }
3078c744fb8SChristoph Hellwig 
3081da177e4SLinus Torvalds /*
3091da177e4SLinus Torvalds  * get_write_access() gets write permission for a file.
3101da177e4SLinus Torvalds  * put_write_access() releases this write permission.
3111da177e4SLinus Torvalds  * This is used for regular files.
3121da177e4SLinus Torvalds  * We cannot support write (and maybe mmap read-write shared) accesses and
3131da177e4SLinus Torvalds  * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
3141da177e4SLinus Torvalds  * can have the following values:
3151da177e4SLinus Torvalds  * 0: no writers, no VM_DENYWRITE mappings
3161da177e4SLinus Torvalds  * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
3171da177e4SLinus Torvalds  * > 0: (i_writecount) users are writing to the file.
3181da177e4SLinus Torvalds  *
3191da177e4SLinus Torvalds  * Normally we operate on that counter with atomic_{inc,dec} and it's safe
3201da177e4SLinus Torvalds  * except for the cases where we don't hold i_writecount yet. Then we need to
3211da177e4SLinus Torvalds  * use {get,deny}_write_access() - these functions check the sign and refuse
3221da177e4SLinus Torvalds  * to do the change if sign is wrong. Exclusion between them is provided by
3231da177e4SLinus Torvalds  * the inode->i_lock spinlock.
3241da177e4SLinus Torvalds  */
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds int get_write_access(struct inode * inode)
3271da177e4SLinus Torvalds {
3281da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
3291da177e4SLinus Torvalds 	if (atomic_read(&inode->i_writecount) < 0) {
3301da177e4SLinus Torvalds 		spin_unlock(&inode->i_lock);
3311da177e4SLinus Torvalds 		return -ETXTBSY;
3321da177e4SLinus Torvalds 	}
3331da177e4SLinus Torvalds 	atomic_inc(&inode->i_writecount);
3341da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	return 0;
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds int deny_write_access(struct file * file)
3401da177e4SLinus Torvalds {
3410f7fc9e4SJosef "Jeff" Sipek 	struct inode *inode = file->f_path.dentry->d_inode;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
3441da177e4SLinus Torvalds 	if (atomic_read(&inode->i_writecount) > 0) {
3451da177e4SLinus Torvalds 		spin_unlock(&inode->i_lock);
3461da177e4SLinus Torvalds 		return -ETXTBSY;
3471da177e4SLinus Torvalds 	}
3481da177e4SLinus Torvalds 	atomic_dec(&inode->i_writecount);
3491da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	return 0;
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds 
3541d957f9bSJan Blunck /**
3555dd784d0SJan Blunck  * path_get - get a reference to a path
3565dd784d0SJan Blunck  * @path: path to get the reference to
3575dd784d0SJan Blunck  *
3585dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
3595dd784d0SJan Blunck  */
3605dd784d0SJan Blunck void path_get(struct path *path)
3615dd784d0SJan Blunck {
3625dd784d0SJan Blunck 	mntget(path->mnt);
3635dd784d0SJan Blunck 	dget(path->dentry);
3645dd784d0SJan Blunck }
3655dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
3665dd784d0SJan Blunck 
3675dd784d0SJan Blunck /**
3681d957f9bSJan Blunck  * path_put - put a reference to a path
3691d957f9bSJan Blunck  * @path: path to put the reference to
3701d957f9bSJan Blunck  *
3711d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
3721d957f9bSJan Blunck  */
3731d957f9bSJan Blunck void path_put(struct path *path)
3741da177e4SLinus Torvalds {
3751d957f9bSJan Blunck 	dput(path->dentry);
3761d957f9bSJan Blunck 	mntput(path->mnt);
3771da177e4SLinus Torvalds }
3781d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
3791da177e4SLinus Torvalds 
380834f2a4aSTrond Myklebust /**
381834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
382834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
383834f2a4aSTrond Myklebust  */
384834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
385834f2a4aSTrond Myklebust {
3860f7fc9e4SJosef "Jeff" Sipek 	if (nd->intent.open.file->f_path.dentry == NULL)
387834f2a4aSTrond Myklebust 		put_filp(nd->intent.open.file);
388834f2a4aSTrond Myklebust 	else
389834f2a4aSTrond Myklebust 		fput(nd->intent.open.file);
390834f2a4aSTrond Myklebust }
391834f2a4aSTrond Myklebust 
392bcdc5e01SIan Kent static inline struct dentry *
393bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd)
394bcdc5e01SIan Kent {
395bcdc5e01SIan Kent 	int status = dentry->d_op->d_revalidate(dentry, nd);
396bcdc5e01SIan Kent 	if (unlikely(status <= 0)) {
397bcdc5e01SIan Kent 		/*
398bcdc5e01SIan Kent 		 * The dentry failed validation.
399bcdc5e01SIan Kent 		 * If d_revalidate returned 0 attempt to invalidate
400bcdc5e01SIan Kent 		 * the dentry otherwise d_revalidate is asking us
401bcdc5e01SIan Kent 		 * to return a fail status.
402bcdc5e01SIan Kent 		 */
403bcdc5e01SIan Kent 		if (!status) {
404bcdc5e01SIan Kent 			if (!d_invalidate(dentry)) {
405bcdc5e01SIan Kent 				dput(dentry);
406bcdc5e01SIan Kent 				dentry = NULL;
407bcdc5e01SIan Kent 			}
408bcdc5e01SIan Kent 		} else {
409bcdc5e01SIan Kent 			dput(dentry);
410bcdc5e01SIan Kent 			dentry = ERR_PTR(status);
411bcdc5e01SIan Kent 		}
412bcdc5e01SIan Kent 	}
413bcdc5e01SIan Kent 	return dentry;
414bcdc5e01SIan Kent }
415bcdc5e01SIan Kent 
4161da177e4SLinus Torvalds /*
417b75b5086SAl Viro  * Short-cut version of permission(), for calling on directories
418b75b5086SAl Viro  * during pathname resolution.  Combines parts of permission()
419b75b5086SAl Viro  * and generic_permission(), and tests ONLY for MAY_EXEC permission.
4201da177e4SLinus Torvalds  *
4211da177e4SLinus Torvalds  * If appropriate, check DAC only.  If not appropriate, or
422b75b5086SAl Viro  * short-cut DAC fails, then call ->permission() to do more
4231da177e4SLinus Torvalds  * complete permission check.
4241da177e4SLinus Torvalds  */
425b75b5086SAl Viro static int exec_permission(struct inode *inode)
4261da177e4SLinus Torvalds {
4275909ccaaSLinus Torvalds 	int ret;
4281da177e4SLinus Torvalds 
429cb9179eaSLinus Torvalds 	if (inode->i_op->permission) {
4305909ccaaSLinus Torvalds 		ret = inode->i_op->permission(inode, MAY_EXEC);
431cb9179eaSLinus Torvalds 		if (!ret)
432cb9179eaSLinus Torvalds 			goto ok;
433cb9179eaSLinus Torvalds 		return ret;
434cb9179eaSLinus Torvalds 	}
4355909ccaaSLinus Torvalds 	ret = acl_permission_check(inode, MAY_EXEC, inode->i_op->check_acl);
4365909ccaaSLinus Torvalds 	if (!ret)
4371da177e4SLinus Torvalds 		goto ok;
4381da177e4SLinus Torvalds 
439f1ac9f6bSLinus Torvalds 	if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH))
4401da177e4SLinus Torvalds 		goto ok;
4411da177e4SLinus Torvalds 
4425909ccaaSLinus Torvalds 	return ret;
4431da177e4SLinus Torvalds ok:
444b77b0646SAl Viro 	return security_inode_permission(inode, MAY_EXEC);
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds 
4472a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
4482a737871SAl Viro {
4492a737871SAl Viro 	if (!nd->root.mnt) {
4502a737871SAl Viro 		struct fs_struct *fs = current->fs;
4512a737871SAl Viro 		read_lock(&fs->lock);
4522a737871SAl Viro 		nd->root = fs->root;
4532a737871SAl Viro 		path_get(&nd->root);
4542a737871SAl Viro 		read_unlock(&fs->lock);
4552a737871SAl Viro 	}
4562a737871SAl Viro }
4572a737871SAl Viro 
4586de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
4596de88d72SAl Viro 
460f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
4611da177e4SLinus Torvalds {
4621da177e4SLinus Torvalds 	int res = 0;
4631da177e4SLinus Torvalds 	char *name;
4641da177e4SLinus Torvalds 	if (IS_ERR(link))
4651da177e4SLinus Torvalds 		goto fail;
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 	if (*link == '/') {
4682a737871SAl Viro 		set_root(nd);
4691d957f9bSJan Blunck 		path_put(&nd->path);
4702a737871SAl Viro 		nd->path = nd->root;
4712a737871SAl Viro 		path_get(&nd->root);
4721da177e4SLinus Torvalds 	}
473b4091d5fSChristoph Hellwig 
4741da177e4SLinus Torvalds 	res = link_path_walk(link, nd);
4751da177e4SLinus Torvalds 	if (nd->depth || res || nd->last_type!=LAST_NORM)
4761da177e4SLinus Torvalds 		return res;
4771da177e4SLinus Torvalds 	/*
4781da177e4SLinus Torvalds 	 * If it is an iterative symlinks resolution in open_namei() we
4791da177e4SLinus Torvalds 	 * have to copy the last component. And all that crap because of
4801da177e4SLinus Torvalds 	 * bloody create() on broken symlinks. Furrfu...
4811da177e4SLinus Torvalds 	 */
4821da177e4SLinus Torvalds 	name = __getname();
4831da177e4SLinus Torvalds 	if (unlikely(!name)) {
4841d957f9bSJan Blunck 		path_put(&nd->path);
4851da177e4SLinus Torvalds 		return -ENOMEM;
4861da177e4SLinus Torvalds 	}
4871da177e4SLinus Torvalds 	strcpy(name, nd->last.name);
4881da177e4SLinus Torvalds 	nd->last.name = name;
4891da177e4SLinus Torvalds 	return 0;
4901da177e4SLinus Torvalds fail:
4911d957f9bSJan Blunck 	path_put(&nd->path);
4921da177e4SLinus Torvalds 	return PTR_ERR(link);
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds 
4951d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
496051d3812SIan Kent {
497051d3812SIan Kent 	dput(path->dentry);
4984ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
499051d3812SIan Kent 		mntput(path->mnt);
500051d3812SIan Kent }
501051d3812SIan Kent 
502051d3812SIan Kent static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
503051d3812SIan Kent {
5044ac91378SJan Blunck 	dput(nd->path.dentry);
5054ac91378SJan Blunck 	if (nd->path.mnt != path->mnt)
5064ac91378SJan Blunck 		mntput(nd->path.mnt);
5074ac91378SJan Blunck 	nd->path.mnt = path->mnt;
5084ac91378SJan Blunck 	nd->path.dentry = path->dentry;
509051d3812SIan Kent }
510051d3812SIan Kent 
511f1662356SArjan van de Ven static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
5121da177e4SLinus Torvalds {
5131da177e4SLinus Torvalds 	int error;
514cc314eefSLinus Torvalds 	void *cookie;
515cd4e91d3SAl Viro 	struct dentry *dentry = path->dentry;
5161da177e4SLinus Torvalds 
517d671a1cbSAl Viro 	touch_atime(path->mnt, dentry);
5181da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
519cd4e91d3SAl Viro 
5204ac91378SJan Blunck 	if (path->mnt != nd->path.mnt) {
521051d3812SIan Kent 		path_to_nameidata(path, nd);
522051d3812SIan Kent 		dget(dentry);
523051d3812SIan Kent 	}
524cd4e91d3SAl Viro 	mntget(path->mnt);
525cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
526cc314eefSLinus Torvalds 	error = PTR_ERR(cookie);
527cc314eefSLinus Torvalds 	if (!IS_ERR(cookie)) {
5281da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
529cc314eefSLinus Torvalds 		error = 0;
5301da177e4SLinus Torvalds 		if (s)
5311da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
5321da177e4SLinus Torvalds 		if (dentry->d_inode->i_op->put_link)
533cc314eefSLinus Torvalds 			dentry->d_inode->i_op->put_link(dentry, nd, cookie);
5341da177e4SLinus Torvalds 	}
5351da177e4SLinus Torvalds 	return error;
5361da177e4SLinus Torvalds }
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds /*
5391da177e4SLinus Torvalds  * This limits recursive symlink follows to 8, while
5401da177e4SLinus Torvalds  * limiting consecutive symlinks to 40.
5411da177e4SLinus Torvalds  *
5421da177e4SLinus Torvalds  * Without that kind of total limit, nasty chains of consecutive
5431da177e4SLinus Torvalds  * symlinks can cause almost arbitrarily long lookups.
5441da177e4SLinus Torvalds  */
54590ebe565SAl Viro static inline int do_follow_link(struct path *path, struct nameidata *nd)
5461da177e4SLinus Torvalds {
5471da177e4SLinus Torvalds 	int err = -ELOOP;
5481da177e4SLinus Torvalds 	if (current->link_count >= MAX_NESTED_LINKS)
5491da177e4SLinus Torvalds 		goto loop;
5501da177e4SLinus Torvalds 	if (current->total_link_count >= 40)
5511da177e4SLinus Torvalds 		goto loop;
5521da177e4SLinus Torvalds 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
5531da177e4SLinus Torvalds 	cond_resched();
55490ebe565SAl Viro 	err = security_inode_follow_link(path->dentry, nd);
5551da177e4SLinus Torvalds 	if (err)
5561da177e4SLinus Torvalds 		goto loop;
5571da177e4SLinus Torvalds 	current->link_count++;
5581da177e4SLinus Torvalds 	current->total_link_count++;
5591da177e4SLinus Torvalds 	nd->depth++;
560cd4e91d3SAl Viro 	err = __do_follow_link(path, nd);
561258fa999SAl Viro 	path_put(path);
5621da177e4SLinus Torvalds 	current->link_count--;
5631da177e4SLinus Torvalds 	nd->depth--;
5641da177e4SLinus Torvalds 	return err;
5651da177e4SLinus Torvalds loop:
5661d957f9bSJan Blunck 	path_put_conditional(path, nd);
5671d957f9bSJan Blunck 	path_put(&nd->path);
5681da177e4SLinus Torvalds 	return err;
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds 
571bab77ebfSAl Viro int follow_up(struct path *path)
5721da177e4SLinus Torvalds {
5731da177e4SLinus Torvalds 	struct vfsmount *parent;
5741da177e4SLinus Torvalds 	struct dentry *mountpoint;
5751da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
576bab77ebfSAl Viro 	parent = path->mnt->mnt_parent;
577bab77ebfSAl Viro 	if (parent == path->mnt) {
5781da177e4SLinus Torvalds 		spin_unlock(&vfsmount_lock);
5791da177e4SLinus Torvalds 		return 0;
5801da177e4SLinus Torvalds 	}
5811da177e4SLinus Torvalds 	mntget(parent);
582bab77ebfSAl Viro 	mountpoint = dget(path->mnt->mnt_mountpoint);
5831da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
584bab77ebfSAl Viro 	dput(path->dentry);
585bab77ebfSAl Viro 	path->dentry = mountpoint;
586bab77ebfSAl Viro 	mntput(path->mnt);
587bab77ebfSAl Viro 	path->mnt = parent;
5881da177e4SLinus Torvalds 	return 1;
5891da177e4SLinus Torvalds }
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds /* no need for dcache_lock, as serialization is taken care in
5921da177e4SLinus Torvalds  * namespace.c
5931da177e4SLinus Torvalds  */
594463ffb2eSAl Viro static int __follow_mount(struct path *path)
595463ffb2eSAl Viro {
596463ffb2eSAl Viro 	int res = 0;
597463ffb2eSAl Viro 	while (d_mountpoint(path->dentry)) {
5981c755af4SAl Viro 		struct vfsmount *mounted = lookup_mnt(path);
599463ffb2eSAl Viro 		if (!mounted)
600463ffb2eSAl Viro 			break;
601463ffb2eSAl Viro 		dput(path->dentry);
602463ffb2eSAl Viro 		if (res)
603463ffb2eSAl Viro 			mntput(path->mnt);
604463ffb2eSAl Viro 		path->mnt = mounted;
605463ffb2eSAl Viro 		path->dentry = dget(mounted->mnt_root);
606463ffb2eSAl Viro 		res = 1;
607463ffb2eSAl Viro 	}
608463ffb2eSAl Viro 	return res;
609463ffb2eSAl Viro }
610463ffb2eSAl Viro 
61179ed0226SAl Viro static void follow_mount(struct path *path)
6121da177e4SLinus Torvalds {
61379ed0226SAl Viro 	while (d_mountpoint(path->dentry)) {
6141c755af4SAl Viro 		struct vfsmount *mounted = lookup_mnt(path);
6151da177e4SLinus Torvalds 		if (!mounted)
6161da177e4SLinus Torvalds 			break;
61779ed0226SAl Viro 		dput(path->dentry);
61879ed0226SAl Viro 		mntput(path->mnt);
61979ed0226SAl Viro 		path->mnt = mounted;
62079ed0226SAl Viro 		path->dentry = dget(mounted->mnt_root);
6211da177e4SLinus Torvalds 	}
6221da177e4SLinus Torvalds }
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds /* no need for dcache_lock, as serialization is taken care in
6251da177e4SLinus Torvalds  * namespace.c
6261da177e4SLinus Torvalds  */
6279393bd07SAl Viro int follow_down(struct path *path)
6281da177e4SLinus Torvalds {
6291da177e4SLinus Torvalds 	struct vfsmount *mounted;
6301da177e4SLinus Torvalds 
6311c755af4SAl Viro 	mounted = lookup_mnt(path);
6321da177e4SLinus Torvalds 	if (mounted) {
6339393bd07SAl Viro 		dput(path->dentry);
6349393bd07SAl Viro 		mntput(path->mnt);
6359393bd07SAl Viro 		path->mnt = mounted;
6369393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
6371da177e4SLinus Torvalds 		return 1;
6381da177e4SLinus Torvalds 	}
6391da177e4SLinus Torvalds 	return 0;
6401da177e4SLinus Torvalds }
6411da177e4SLinus Torvalds 
642f1662356SArjan van de Ven static __always_inline void follow_dotdot(struct nameidata *nd)
6431da177e4SLinus Torvalds {
6442a737871SAl Viro 	set_root(nd);
645e518ddb7SAndreas Mohr 
6461da177e4SLinus Torvalds 	while(1) {
6471da177e4SLinus Torvalds 		struct vfsmount *parent;
6484ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
6491da177e4SLinus Torvalds 
6502a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
6512a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
6521da177e4SLinus Torvalds 			break;
6531da177e4SLinus Torvalds 		}
6541da177e4SLinus Torvalds 		spin_lock(&dcache_lock);
6554ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
6564ac91378SJan Blunck 			nd->path.dentry = dget(nd->path.dentry->d_parent);
6571da177e4SLinus Torvalds 			spin_unlock(&dcache_lock);
6581da177e4SLinus Torvalds 			dput(old);
6591da177e4SLinus Torvalds 			break;
6601da177e4SLinus Torvalds 		}
6611da177e4SLinus Torvalds 		spin_unlock(&dcache_lock);
6621da177e4SLinus Torvalds 		spin_lock(&vfsmount_lock);
6634ac91378SJan Blunck 		parent = nd->path.mnt->mnt_parent;
6644ac91378SJan Blunck 		if (parent == nd->path.mnt) {
6651da177e4SLinus Torvalds 			spin_unlock(&vfsmount_lock);
6661da177e4SLinus Torvalds 			break;
6671da177e4SLinus Torvalds 		}
6681da177e4SLinus Torvalds 		mntget(parent);
6694ac91378SJan Blunck 		nd->path.dentry = dget(nd->path.mnt->mnt_mountpoint);
6701da177e4SLinus Torvalds 		spin_unlock(&vfsmount_lock);
6711da177e4SLinus Torvalds 		dput(old);
6724ac91378SJan Blunck 		mntput(nd->path.mnt);
6734ac91378SJan Blunck 		nd->path.mnt = parent;
6741da177e4SLinus Torvalds 	}
67579ed0226SAl Viro 	follow_mount(&nd->path);
6761da177e4SLinus Torvalds }
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds /*
6791da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
6801da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
6811da177e4SLinus Torvalds  *  It _is_ time-critical.
6821da177e4SLinus Torvalds  */
6831da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
6841da177e4SLinus Torvalds 		     struct path *path)
6851da177e4SLinus Torvalds {
6864ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
6876e6b1bd1SAl Viro 	struct dentry *dentry, *parent;
6886e6b1bd1SAl Viro 	struct inode *dir;
6893cac260aSAl Viro 	/*
6903cac260aSAl Viro 	 * See if the low-level filesystem might want
6913cac260aSAl Viro 	 * to use its own hash..
6923cac260aSAl Viro 	 */
6933cac260aSAl Viro 	if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
6943cac260aSAl Viro 		int err = nd->path.dentry->d_op->d_hash(nd->path.dentry, name);
6953cac260aSAl Viro 		if (err < 0)
6963cac260aSAl Viro 			return err;
6973cac260aSAl Viro 	}
6981da177e4SLinus Torvalds 
6993cac260aSAl Viro 	dentry = __d_lookup(nd->path.dentry, name);
7001da177e4SLinus Torvalds 	if (!dentry)
7011da177e4SLinus Torvalds 		goto need_lookup;
7021da177e4SLinus Torvalds 	if (dentry->d_op && dentry->d_op->d_revalidate)
7031da177e4SLinus Torvalds 		goto need_revalidate;
7041da177e4SLinus Torvalds done:
7051da177e4SLinus Torvalds 	path->mnt = mnt;
7061da177e4SLinus Torvalds 	path->dentry = dentry;
707634ee701SAl Viro 	__follow_mount(path);
7081da177e4SLinus Torvalds 	return 0;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds need_lookup:
7116e6b1bd1SAl Viro 	parent = nd->path.dentry;
7126e6b1bd1SAl Viro 	dir = parent->d_inode;
7136e6b1bd1SAl Viro 
7146e6b1bd1SAl Viro 	mutex_lock(&dir->i_mutex);
7156e6b1bd1SAl Viro 	/*
7166e6b1bd1SAl Viro 	 * First re-do the cached lookup just in case it was created
7176e6b1bd1SAl Viro 	 * while we waited for the directory semaphore..
7186e6b1bd1SAl Viro 	 *
7196e6b1bd1SAl Viro 	 * FIXME! This could use version numbering or similar to
7206e6b1bd1SAl Viro 	 * avoid unnecessary cache lookups.
7216e6b1bd1SAl Viro 	 *
7226e6b1bd1SAl Viro 	 * The "dcache_lock" is purely to protect the RCU list walker
7236e6b1bd1SAl Viro 	 * from concurrent renames at this point (we mustn't get false
7246e6b1bd1SAl Viro 	 * negatives from the RCU list walk here, unlike the optimistic
7256e6b1bd1SAl Viro 	 * fast walk).
7266e6b1bd1SAl Viro 	 *
7276e6b1bd1SAl Viro 	 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
7286e6b1bd1SAl Viro 	 */
7296e6b1bd1SAl Viro 	dentry = d_lookup(parent, name);
7306e6b1bd1SAl Viro 	if (!dentry) {
7316e6b1bd1SAl Viro 		struct dentry *new;
7326e6b1bd1SAl Viro 
7336e6b1bd1SAl Viro 		/* Don't create child dentry for a dead directory. */
7346e6b1bd1SAl Viro 		dentry = ERR_PTR(-ENOENT);
7356e6b1bd1SAl Viro 		if (IS_DEADDIR(dir))
7366e6b1bd1SAl Viro 			goto out_unlock;
7376e6b1bd1SAl Viro 
7386e6b1bd1SAl Viro 		new = d_alloc(parent, name);
7396e6b1bd1SAl Viro 		dentry = ERR_PTR(-ENOMEM);
7406e6b1bd1SAl Viro 		if (new) {
7416e6b1bd1SAl Viro 			dentry = dir->i_op->lookup(dir, new, nd);
7426e6b1bd1SAl Viro 			if (dentry)
7436e6b1bd1SAl Viro 				dput(new);
7446e6b1bd1SAl Viro 			else
7456e6b1bd1SAl Viro 				dentry = new;
7466e6b1bd1SAl Viro 		}
7476e6b1bd1SAl Viro out_unlock:
7486e6b1bd1SAl Viro 		mutex_unlock(&dir->i_mutex);
7496e6b1bd1SAl Viro 		if (IS_ERR(dentry))
7506e6b1bd1SAl Viro 			goto fail;
7516e6b1bd1SAl Viro 		goto done;
7526e6b1bd1SAl Viro 	}
7536e6b1bd1SAl Viro 
7546e6b1bd1SAl Viro 	/*
7556e6b1bd1SAl Viro 	 * Uhhuh! Nasty case: the cache was re-populated while
7566e6b1bd1SAl Viro 	 * we waited on the semaphore. Need to revalidate.
7576e6b1bd1SAl Viro 	 */
7586e6b1bd1SAl Viro 	mutex_unlock(&dir->i_mutex);
7596e6b1bd1SAl Viro 	if (dentry->d_op && dentry->d_op->d_revalidate) {
7606e6b1bd1SAl Viro 		dentry = do_revalidate(dentry, nd);
7616e6b1bd1SAl Viro 		if (!dentry)
7626e6b1bd1SAl Viro 			dentry = ERR_PTR(-ENOENT);
7636e6b1bd1SAl Viro 	}
7641da177e4SLinus Torvalds 	if (IS_ERR(dentry))
7651da177e4SLinus Torvalds 		goto fail;
7661da177e4SLinus Torvalds 	goto done;
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds need_revalidate:
769bcdc5e01SIan Kent 	dentry = do_revalidate(dentry, nd);
770bcdc5e01SIan Kent 	if (!dentry)
7711da177e4SLinus Torvalds 		goto need_lookup;
772bcdc5e01SIan Kent 	if (IS_ERR(dentry))
773bcdc5e01SIan Kent 		goto fail;
774bcdc5e01SIan Kent 	goto done;
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds fail:
7771da177e4SLinus Torvalds 	return PTR_ERR(dentry);
7781da177e4SLinus Torvalds }
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds /*
7811da177e4SLinus Torvalds  * Name resolution.
782ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
783ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
7841da177e4SLinus Torvalds  *
785ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
786ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
7871da177e4SLinus Torvalds  */
7886de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
7891da177e4SLinus Torvalds {
7901da177e4SLinus Torvalds 	struct path next;
7911da177e4SLinus Torvalds 	struct inode *inode;
7921da177e4SLinus Torvalds 	int err;
7931da177e4SLinus Torvalds 	unsigned int lookup_flags = nd->flags;
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 	while (*name=='/')
7961da177e4SLinus Torvalds 		name++;
7971da177e4SLinus Torvalds 	if (!*name)
7981da177e4SLinus Torvalds 		goto return_reval;
7991da177e4SLinus Torvalds 
8004ac91378SJan Blunck 	inode = nd->path.dentry->d_inode;
8011da177e4SLinus Torvalds 	if (nd->depth)
802f55eab82STrond Myklebust 		lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
8051da177e4SLinus Torvalds 	for(;;) {
8061da177e4SLinus Torvalds 		unsigned long hash;
8071da177e4SLinus Torvalds 		struct qstr this;
8081da177e4SLinus Torvalds 		unsigned int c;
8091da177e4SLinus Torvalds 
810cdce5d6bSTrond Myklebust 		nd->flags |= LOOKUP_CONTINUE;
811b75b5086SAl Viro 		err = exec_permission(inode);
8121da177e4SLinus Torvalds  		if (err)
8131da177e4SLinus Torvalds 			break;
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 		this.name = name;
8161da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 		hash = init_name_hash();
8191da177e4SLinus Torvalds 		do {
8201da177e4SLinus Torvalds 			name++;
8211da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
8221da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
8231da177e4SLinus Torvalds 		} while (c && (c != '/'));
8241da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
8251da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 		/* remove trailing slashes? */
8281da177e4SLinus Torvalds 		if (!c)
8291da177e4SLinus Torvalds 			goto last_component;
8301da177e4SLinus Torvalds 		while (*++name == '/');
8311da177e4SLinus Torvalds 		if (!*name)
8321da177e4SLinus Torvalds 			goto last_with_slashes;
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 		/*
8351da177e4SLinus Torvalds 		 * "." and ".." are special - ".." especially so because it has
8361da177e4SLinus Torvalds 		 * to be able to know about the current root directory and
8371da177e4SLinus Torvalds 		 * parent relationships.
8381da177e4SLinus Torvalds 		 */
8391da177e4SLinus Torvalds 		if (this.name[0] == '.') switch (this.len) {
8401da177e4SLinus Torvalds 			default:
8411da177e4SLinus Torvalds 				break;
8421da177e4SLinus Torvalds 			case 2:
8431da177e4SLinus Torvalds 				if (this.name[1] != '.')
8441da177e4SLinus Torvalds 					break;
84558c465ebSAl Viro 				follow_dotdot(nd);
8464ac91378SJan Blunck 				inode = nd->path.dentry->d_inode;
8471da177e4SLinus Torvalds 				/* fallthrough */
8481da177e4SLinus Torvalds 			case 1:
8491da177e4SLinus Torvalds 				continue;
8501da177e4SLinus Torvalds 		}
8511da177e4SLinus Torvalds 		/* This does the actual lookups.. */
8521da177e4SLinus Torvalds 		err = do_lookup(nd, &this, &next);
8531da177e4SLinus Torvalds 		if (err)
8541da177e4SLinus Torvalds 			break;
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 		err = -ENOENT;
8571da177e4SLinus Torvalds 		inode = next.dentry->d_inode;
8581da177e4SLinus Torvalds 		if (!inode)
8591da177e4SLinus Torvalds 			goto out_dput;
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 		if (inode->i_op->follow_link) {
86290ebe565SAl Viro 			err = do_follow_link(&next, nd);
8631da177e4SLinus Torvalds 			if (err)
8641da177e4SLinus Torvalds 				goto return_err;
8651da177e4SLinus Torvalds 			err = -ENOENT;
8664ac91378SJan Blunck 			inode = nd->path.dentry->d_inode;
8671da177e4SLinus Torvalds 			if (!inode)
8681da177e4SLinus Torvalds 				break;
86909dd17d3SMiklos Szeredi 		} else
87009dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
8711da177e4SLinus Torvalds 		err = -ENOTDIR;
8721da177e4SLinus Torvalds 		if (!inode->i_op->lookup)
8731da177e4SLinus Torvalds 			break;
8741da177e4SLinus Torvalds 		continue;
8751da177e4SLinus Torvalds 		/* here ends the main loop */
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds last_with_slashes:
8781da177e4SLinus Torvalds 		lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
8791da177e4SLinus Torvalds last_component:
880f55eab82STrond Myklebust 		/* Clear LOOKUP_CONTINUE iff it was previously unset */
881f55eab82STrond Myklebust 		nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
8821da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_PARENT)
8831da177e4SLinus Torvalds 			goto lookup_parent;
8841da177e4SLinus Torvalds 		if (this.name[0] == '.') switch (this.len) {
8851da177e4SLinus Torvalds 			default:
8861da177e4SLinus Torvalds 				break;
8871da177e4SLinus Torvalds 			case 2:
8881da177e4SLinus Torvalds 				if (this.name[1] != '.')
8891da177e4SLinus Torvalds 					break;
89058c465ebSAl Viro 				follow_dotdot(nd);
8914ac91378SJan Blunck 				inode = nd->path.dentry->d_inode;
8921da177e4SLinus Torvalds 				/* fallthrough */
8931da177e4SLinus Torvalds 			case 1:
8941da177e4SLinus Torvalds 				goto return_reval;
8951da177e4SLinus Torvalds 		}
8961da177e4SLinus Torvalds 		err = do_lookup(nd, &this, &next);
8971da177e4SLinus Torvalds 		if (err)
8981da177e4SLinus Torvalds 			break;
8991da177e4SLinus Torvalds 		inode = next.dentry->d_inode;
9001da177e4SLinus Torvalds 		if ((lookup_flags & LOOKUP_FOLLOW)
901acfa4380SAl Viro 		    && inode && inode->i_op->follow_link) {
90290ebe565SAl Viro 			err = do_follow_link(&next, nd);
9031da177e4SLinus Torvalds 			if (err)
9041da177e4SLinus Torvalds 				goto return_err;
9054ac91378SJan Blunck 			inode = nd->path.dentry->d_inode;
90609dd17d3SMiklos Szeredi 		} else
90709dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
9081da177e4SLinus Torvalds 		err = -ENOENT;
9091da177e4SLinus Torvalds 		if (!inode)
9101da177e4SLinus Torvalds 			break;
9111da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_DIRECTORY) {
9121da177e4SLinus Torvalds 			err = -ENOTDIR;
913acfa4380SAl Viro 			if (!inode->i_op->lookup)
9141da177e4SLinus Torvalds 				break;
9151da177e4SLinus Torvalds 		}
9161da177e4SLinus Torvalds 		goto return_base;
9171da177e4SLinus Torvalds lookup_parent:
9181da177e4SLinus Torvalds 		nd->last = this;
9191da177e4SLinus Torvalds 		nd->last_type = LAST_NORM;
9201da177e4SLinus Torvalds 		if (this.name[0] != '.')
9211da177e4SLinus Torvalds 			goto return_base;
9221da177e4SLinus Torvalds 		if (this.len == 1)
9231da177e4SLinus Torvalds 			nd->last_type = LAST_DOT;
9241da177e4SLinus Torvalds 		else if (this.len == 2 && this.name[1] == '.')
9251da177e4SLinus Torvalds 			nd->last_type = LAST_DOTDOT;
9261da177e4SLinus Torvalds 		else
9271da177e4SLinus Torvalds 			goto return_base;
9281da177e4SLinus Torvalds return_reval:
9291da177e4SLinus Torvalds 		/*
9301da177e4SLinus Torvalds 		 * We bypassed the ordinary revalidation routines.
9311da177e4SLinus Torvalds 		 * We may need to check the cached dentry for staleness.
9321da177e4SLinus Torvalds 		 */
9334ac91378SJan Blunck 		if (nd->path.dentry && nd->path.dentry->d_sb &&
9344ac91378SJan Blunck 		    (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
9351da177e4SLinus Torvalds 			err = -ESTALE;
9361da177e4SLinus Torvalds 			/* Note: we do not d_invalidate() */
9374ac91378SJan Blunck 			if (!nd->path.dentry->d_op->d_revalidate(
9384ac91378SJan Blunck 					nd->path.dentry, nd))
9391da177e4SLinus Torvalds 				break;
9401da177e4SLinus Torvalds 		}
9411da177e4SLinus Torvalds return_base:
9421da177e4SLinus Torvalds 		return 0;
9431da177e4SLinus Torvalds out_dput:
9441d957f9bSJan Blunck 		path_put_conditional(&next, nd);
9451da177e4SLinus Torvalds 		break;
9461da177e4SLinus Torvalds 	}
9471d957f9bSJan Blunck 	path_put(&nd->path);
9481da177e4SLinus Torvalds return_err:
9491da177e4SLinus Torvalds 	return err;
9501da177e4SLinus Torvalds }
9511da177e4SLinus Torvalds 
952fc9b52cdSHarvey Harrison static int path_walk(const char *name, struct nameidata *nd)
9531da177e4SLinus Torvalds {
9546de88d72SAl Viro 	struct path save = nd->path;
9556de88d72SAl Viro 	int result;
9566de88d72SAl Viro 
9571da177e4SLinus Torvalds 	current->total_link_count = 0;
9586de88d72SAl Viro 
9596de88d72SAl Viro 	/* make sure the stuff we saved doesn't go away */
9606de88d72SAl Viro 	path_get(&save);
9616de88d72SAl Viro 
9626de88d72SAl Viro 	result = link_path_walk(name, nd);
9636de88d72SAl Viro 	if (result == -ESTALE) {
9646de88d72SAl Viro 		/* nd->path had been dropped */
9656de88d72SAl Viro 		current->total_link_count = 0;
9666de88d72SAl Viro 		nd->path = save;
9676de88d72SAl Viro 		path_get(&nd->path);
9686de88d72SAl Viro 		nd->flags |= LOOKUP_REVAL;
9696de88d72SAl Viro 		result = link_path_walk(name, nd);
9706de88d72SAl Viro 	}
9716de88d72SAl Viro 
9726de88d72SAl Viro 	path_put(&save);
9736de88d72SAl Viro 
9746de88d72SAl Viro 	return result;
9751da177e4SLinus Torvalds }
9761da177e4SLinus Torvalds 
9779b4a9b14SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
9781da177e4SLinus Torvalds {
979ea3834d9SPrasanna Meda 	int retval = 0;
980170aa3d0SUlrich Drepper 	int fput_needed;
981170aa3d0SUlrich Drepper 	struct file *file;
9821da177e4SLinus Torvalds 
9831da177e4SLinus Torvalds 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
9841da177e4SLinus Torvalds 	nd->flags = flags;
9851da177e4SLinus Torvalds 	nd->depth = 0;
9862a737871SAl Viro 	nd->root.mnt = NULL;
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 	if (*name=='/') {
9892a737871SAl Viro 		set_root(nd);
9902a737871SAl Viro 		nd->path = nd->root;
9912a737871SAl Viro 		path_get(&nd->root);
9925590ff0dSUlrich Drepper 	} else if (dfd == AT_FDCWD) {
9932a737871SAl Viro 		struct fs_struct *fs = current->fs;
994e518ddb7SAndreas Mohr 		read_lock(&fs->lock);
9956ac08c39SJan Blunck 		nd->path = fs->pwd;
9966ac08c39SJan Blunck 		path_get(&fs->pwd);
997e518ddb7SAndreas Mohr 		read_unlock(&fs->lock);
9985590ff0dSUlrich Drepper 	} else {
9995590ff0dSUlrich Drepper 		struct dentry *dentry;
10005590ff0dSUlrich Drepper 
10015590ff0dSUlrich Drepper 		file = fget_light(dfd, &fput_needed);
10025590ff0dSUlrich Drepper 		retval = -EBADF;
1003170aa3d0SUlrich Drepper 		if (!file)
10046d09bb62STrond Myklebust 			goto out_fail;
10055590ff0dSUlrich Drepper 
10060f7fc9e4SJosef "Jeff" Sipek 		dentry = file->f_path.dentry;
10075590ff0dSUlrich Drepper 
10085590ff0dSUlrich Drepper 		retval = -ENOTDIR;
1009170aa3d0SUlrich Drepper 		if (!S_ISDIR(dentry->d_inode->i_mode))
10106d09bb62STrond Myklebust 			goto fput_fail;
10115590ff0dSUlrich Drepper 
10125590ff0dSUlrich Drepper 		retval = file_permission(file, MAY_EXEC);
1013170aa3d0SUlrich Drepper 		if (retval)
10146d09bb62STrond Myklebust 			goto fput_fail;
10155590ff0dSUlrich Drepper 
10165dd784d0SJan Blunck 		nd->path = file->f_path;
10175dd784d0SJan Blunck 		path_get(&file->f_path);
10185590ff0dSUlrich Drepper 
10195590ff0dSUlrich Drepper 		fput_light(file, fput_needed);
10201da177e4SLinus Torvalds 	}
10219b4a9b14SAl Viro 	return 0;
10222dfdd266SJosef 'Jeff' Sipek 
10239b4a9b14SAl Viro fput_fail:
10249b4a9b14SAl Viro 	fput_light(file, fput_needed);
10259b4a9b14SAl Viro out_fail:
10269b4a9b14SAl Viro 	return retval;
10279b4a9b14SAl Viro }
10289b4a9b14SAl Viro 
10299b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
10309b4a9b14SAl Viro static int do_path_lookup(int dfd, const char *name,
10319b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
10329b4a9b14SAl Viro {
10339b4a9b14SAl Viro 	int retval = path_init(dfd, name, flags, nd);
10349b4a9b14SAl Viro 	if (!retval)
10352dfdd266SJosef 'Jeff' Sipek 		retval = path_walk(name, nd);
10364ac91378SJan Blunck 	if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
10374ac91378SJan Blunck 				nd->path.dentry->d_inode))
10384ac91378SJan Blunck 		audit_inode(name, nd->path.dentry);
10392a737871SAl Viro 	if (nd->root.mnt) {
10402a737871SAl Viro 		path_put(&nd->root);
10412a737871SAl Viro 		nd->root.mnt = NULL;
10422a737871SAl Viro 	}
1043170aa3d0SUlrich Drepper 	return retval;
10441da177e4SLinus Torvalds }
10451da177e4SLinus Torvalds 
1046fc9b52cdSHarvey Harrison int path_lookup(const char *name, unsigned int flags,
10475590ff0dSUlrich Drepper 			struct nameidata *nd)
10485590ff0dSUlrich Drepper {
10495590ff0dSUlrich Drepper 	return do_path_lookup(AT_FDCWD, name, flags, nd);
10505590ff0dSUlrich Drepper }
10515590ff0dSUlrich Drepper 
1052d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1053d1811465SAl Viro {
1054d1811465SAl Viro 	struct nameidata nd;
1055d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1056d1811465SAl Viro 	if (!res)
1057d1811465SAl Viro 		*path = nd.path;
1058d1811465SAl Viro 	return res;
1059d1811465SAl Viro }
1060d1811465SAl Viro 
106116f18200SJosef 'Jeff' Sipek /**
106216f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
106316f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
106416f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
106516f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
106616f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
106716f18200SJosef 'Jeff' Sipek  * @nd: pointer to nameidata
106816f18200SJosef 'Jeff' Sipek  */
106916f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
107016f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
107116f18200SJosef 'Jeff' Sipek 		    struct nameidata *nd)
107216f18200SJosef 'Jeff' Sipek {
107316f18200SJosef 'Jeff' Sipek 	int retval;
107416f18200SJosef 'Jeff' Sipek 
107516f18200SJosef 'Jeff' Sipek 	/* same as do_path_lookup */
107616f18200SJosef 'Jeff' Sipek 	nd->last_type = LAST_ROOT;
107716f18200SJosef 'Jeff' Sipek 	nd->flags = flags;
107816f18200SJosef 'Jeff' Sipek 	nd->depth = 0;
107916f18200SJosef 'Jeff' Sipek 
1080c8e7f449SJan Blunck 	nd->path.dentry = dentry;
1081c8e7f449SJan Blunck 	nd->path.mnt = mnt;
1082c8e7f449SJan Blunck 	path_get(&nd->path);
10835b857119SAl Viro 	nd->root = nd->path;
10845b857119SAl Viro 	path_get(&nd->root);
108516f18200SJosef 'Jeff' Sipek 
108616f18200SJosef 'Jeff' Sipek 	retval = path_walk(name, nd);
10874ac91378SJan Blunck 	if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
10884ac91378SJan Blunck 				nd->path.dentry->d_inode))
10894ac91378SJan Blunck 		audit_inode(name, nd->path.dentry);
109016f18200SJosef 'Jeff' Sipek 
10912a737871SAl Viro 	path_put(&nd->root);
10922a737871SAl Viro 	nd->root.mnt = NULL;
109316f18200SJosef 'Jeff' Sipek 
10942a737871SAl Viro 	return retval;
109516f18200SJosef 'Jeff' Sipek }
109616f18200SJosef 'Jeff' Sipek 
1097eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1098eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
10991da177e4SLinus Torvalds {
11001da177e4SLinus Torvalds 	struct dentry *dentry;
11011da177e4SLinus Torvalds 	struct inode *inode;
11021da177e4SLinus Torvalds 	int err;
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	inode = base->d_inode;
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 	/*
11071da177e4SLinus Torvalds 	 * See if the low-level filesystem might want
11081da177e4SLinus Torvalds 	 * to use its own hash..
11091da177e4SLinus Torvalds 	 */
11101da177e4SLinus Torvalds 	if (base->d_op && base->d_op->d_hash) {
11111da177e4SLinus Torvalds 		err = base->d_op->d_hash(base, name);
11121da177e4SLinus Torvalds 		dentry = ERR_PTR(err);
11131da177e4SLinus Torvalds 		if (err < 0)
11141da177e4SLinus Torvalds 			goto out;
11151da177e4SLinus Torvalds 	}
11161da177e4SLinus Torvalds 
11176e6b1bd1SAl Viro 	dentry = __d_lookup(base, name);
11186e6b1bd1SAl Viro 
11196e6b1bd1SAl Viro 	/* lockess __d_lookup may fail due to concurrent d_move()
11206e6b1bd1SAl Viro 	 * in some unrelated directory, so try with d_lookup
11216e6b1bd1SAl Viro 	 */
11226e6b1bd1SAl Viro 	if (!dentry)
11236e6b1bd1SAl Viro 		dentry = d_lookup(base, name);
11246e6b1bd1SAl Viro 
11256e6b1bd1SAl Viro 	if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
11266e6b1bd1SAl Viro 		dentry = do_revalidate(dentry, nd);
11276e6b1bd1SAl Viro 
11281da177e4SLinus Torvalds 	if (!dentry) {
1129d70b67c8SMiklos Szeredi 		struct dentry *new;
1130d70b67c8SMiklos Szeredi 
1131d70b67c8SMiklos Szeredi 		/* Don't create child dentry for a dead directory. */
1132d70b67c8SMiklos Szeredi 		dentry = ERR_PTR(-ENOENT);
1133d70b67c8SMiklos Szeredi 		if (IS_DEADDIR(inode))
1134d70b67c8SMiklos Szeredi 			goto out;
1135d70b67c8SMiklos Szeredi 
1136d70b67c8SMiklos Szeredi 		new = d_alloc(base, name);
11371da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOMEM);
11381da177e4SLinus Torvalds 		if (!new)
11391da177e4SLinus Torvalds 			goto out;
11401da177e4SLinus Torvalds 		dentry = inode->i_op->lookup(inode, new, nd);
11411da177e4SLinus Torvalds 		if (!dentry)
11421da177e4SLinus Torvalds 			dentry = new;
11431da177e4SLinus Torvalds 		else
11441da177e4SLinus Torvalds 			dput(new);
11451da177e4SLinus Torvalds 	}
11461da177e4SLinus Torvalds out:
11471da177e4SLinus Torvalds 	return dentry;
11481da177e4SLinus Torvalds }
11491da177e4SLinus Torvalds 
1150057f6c01SJames Morris /*
1151057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1152057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1153057f6c01SJames Morris  * SMP-safe.
1154057f6c01SJames Morris  */
1155a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
11561da177e4SLinus Torvalds {
1157eead1911SChristoph Hellwig 	int err;
1158eead1911SChristoph Hellwig 
1159b75b5086SAl Viro 	err = exec_permission(nd->path.dentry->d_inode);
1160eead1911SChristoph Hellwig 	if (err)
1161eead1911SChristoph Hellwig 		return ERR_PTR(err);
11624ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
11631da177e4SLinus Torvalds }
11641da177e4SLinus Torvalds 
1165eead1911SChristoph Hellwig static int __lookup_one_len(const char *name, struct qstr *this,
1166eead1911SChristoph Hellwig 		struct dentry *base, int len)
11671da177e4SLinus Torvalds {
11681da177e4SLinus Torvalds 	unsigned long hash;
11691da177e4SLinus Torvalds 	unsigned int c;
11701da177e4SLinus Torvalds 
1171057f6c01SJames Morris 	this->name = name;
1172057f6c01SJames Morris 	this->len = len;
11731da177e4SLinus Torvalds 	if (!len)
1174057f6c01SJames Morris 		return -EACCES;
11751da177e4SLinus Torvalds 
11761da177e4SLinus Torvalds 	hash = init_name_hash();
11771da177e4SLinus Torvalds 	while (len--) {
11781da177e4SLinus Torvalds 		c = *(const unsigned char *)name++;
11791da177e4SLinus Torvalds 		if (c == '/' || c == '\0')
1180057f6c01SJames Morris 			return -EACCES;
11811da177e4SLinus Torvalds 		hash = partial_name_hash(c, hash);
11821da177e4SLinus Torvalds 	}
1183057f6c01SJames Morris 	this->hash = end_name_hash(hash);
1184057f6c01SJames Morris 	return 0;
1185057f6c01SJames Morris }
11861da177e4SLinus Torvalds 
1187eead1911SChristoph Hellwig /**
1188a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1189eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1190eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1191eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1192eead1911SChristoph Hellwig  *
1193a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1194a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1195eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1196eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1197eead1911SChristoph Hellwig  */
1198057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1199057f6c01SJames Morris {
1200057f6c01SJames Morris 	int err;
1201057f6c01SJames Morris 	struct qstr this;
1202057f6c01SJames Morris 
12032f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
12042f9092e1SDavid Woodhouse 
1205057f6c01SJames Morris 	err = __lookup_one_len(name, &this, base, len);
1206057f6c01SJames Morris 	if (err)
1207057f6c01SJames Morris 		return ERR_PTR(err);
1208eead1911SChristoph Hellwig 
1209b75b5086SAl Viro 	err = exec_permission(base->d_inode);
1210eead1911SChristoph Hellwig 	if (err)
1211eead1911SChristoph Hellwig 		return ERR_PTR(err);
121249705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1213057f6c01SJames Morris }
1214057f6c01SJames Morris 
12152d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags,
12162d8f3038SAl Viro 		 struct path *path)
12171da177e4SLinus Torvalds {
12182d8f3038SAl Viro 	struct nameidata nd;
12191da177e4SLinus Torvalds 	char *tmp = getname(name);
12201da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
12211da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
12222d8f3038SAl Viro 
12232d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
12242d8f3038SAl Viro 
12252d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
12261da177e4SLinus Torvalds 		putname(tmp);
12272d8f3038SAl Viro 		if (!err)
12282d8f3038SAl Viro 			*path = nd.path;
12291da177e4SLinus Torvalds 	}
12301da177e4SLinus Torvalds 	return err;
12311da177e4SLinus Torvalds }
12321da177e4SLinus Torvalds 
12332ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
12342ad94ae6SAl Viro 			struct nameidata *nd, char **name)
12352ad94ae6SAl Viro {
12362ad94ae6SAl Viro 	char *s = getname(path);
12372ad94ae6SAl Viro 	int error;
12382ad94ae6SAl Viro 
12392ad94ae6SAl Viro 	if (IS_ERR(s))
12402ad94ae6SAl Viro 		return PTR_ERR(s);
12412ad94ae6SAl Viro 
12422ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
12432ad94ae6SAl Viro 	if (error)
12442ad94ae6SAl Viro 		putname(s);
12452ad94ae6SAl Viro 	else
12462ad94ae6SAl Viro 		*name = s;
12472ad94ae6SAl Viro 
12482ad94ae6SAl Viro 	return error;
12492ad94ae6SAl Viro }
12502ad94ae6SAl Viro 
12511da177e4SLinus Torvalds /*
12521da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
12531da177e4SLinus Torvalds  * minimal.
12541da177e4SLinus Torvalds  */
12551da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
12561da177e4SLinus Torvalds {
1257da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1258da9592edSDavid Howells 
12591da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
12601da177e4SLinus Torvalds 		return 0;
1261da9592edSDavid Howells 	if (inode->i_uid == fsuid)
12621da177e4SLinus Torvalds 		return 0;
1263da9592edSDavid Howells 	if (dir->i_uid == fsuid)
12641da177e4SLinus Torvalds 		return 0;
12651da177e4SLinus Torvalds 	return !capable(CAP_FOWNER);
12661da177e4SLinus Torvalds }
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds /*
12691da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
12701da177e4SLinus Torvalds  *  whether the type of victim is right.
12711da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
12721da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
12731da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
12741da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
12751da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
12761da177e4SLinus Torvalds  *	a. be owner of dir, or
12771da177e4SLinus Torvalds  *	b. be owner of victim, or
12781da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
12791da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
12801da177e4SLinus Torvalds  *     links pointing to it.
12811da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
12821da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
12831da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
12841da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
12851da177e4SLinus Torvalds  *     nfs_async_unlink().
12861da177e4SLinus Torvalds  */
1287858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
12881da177e4SLinus Torvalds {
12891da177e4SLinus Torvalds 	int error;
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 	if (!victim->d_inode)
12921da177e4SLinus Torvalds 		return -ENOENT;
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
12955a190ae6SAl Viro 	audit_inode_child(victim->d_name.name, victim, dir);
12961da177e4SLinus Torvalds 
1297f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
12981da177e4SLinus Torvalds 	if (error)
12991da177e4SLinus Torvalds 		return error;
13001da177e4SLinus Torvalds 	if (IS_APPEND(dir))
13011da177e4SLinus Torvalds 		return -EPERM;
13021da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1303f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
13041da177e4SLinus Torvalds 		return -EPERM;
13051da177e4SLinus Torvalds 	if (isdir) {
13061da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
13071da177e4SLinus Torvalds 			return -ENOTDIR;
13081da177e4SLinus Torvalds 		if (IS_ROOT(victim))
13091da177e4SLinus Torvalds 			return -EBUSY;
13101da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
13111da177e4SLinus Torvalds 		return -EISDIR;
13121da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
13131da177e4SLinus Torvalds 		return -ENOENT;
13141da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
13151da177e4SLinus Torvalds 		return -EBUSY;
13161da177e4SLinus Torvalds 	return 0;
13171da177e4SLinus Torvalds }
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
13201da177e4SLinus Torvalds  *  dir.
13211da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
13221da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
13231da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
13241da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
13251da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
13261da177e4SLinus Torvalds  */
1327a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
13281da177e4SLinus Torvalds {
13291da177e4SLinus Torvalds 	if (child->d_inode)
13301da177e4SLinus Torvalds 		return -EEXIST;
13311da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
13321da177e4SLinus Torvalds 		return -ENOENT;
1333f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
13341da177e4SLinus Torvalds }
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds /*
13371da177e4SLinus Torvalds  * O_DIRECTORY translates into forcing a directory lookup.
13381da177e4SLinus Torvalds  */
13391da177e4SLinus Torvalds static inline int lookup_flags(unsigned int f)
13401da177e4SLinus Torvalds {
13411da177e4SLinus Torvalds 	unsigned long retval = LOOKUP_FOLLOW;
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 	if (f & O_NOFOLLOW)
13441da177e4SLinus Torvalds 		retval &= ~LOOKUP_FOLLOW;
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 	if (f & O_DIRECTORY)
13471da177e4SLinus Torvalds 		retval |= LOOKUP_DIRECTORY;
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds 	return retval;
13501da177e4SLinus Torvalds }
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds /*
13531da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
13541da177e4SLinus Torvalds  */
13551da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
13561da177e4SLinus Torvalds {
13571da177e4SLinus Torvalds 	struct dentry *p;
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds 	if (p1 == p2) {
1360f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
13611da177e4SLinus Torvalds 		return NULL;
13621da177e4SLinus Torvalds 	}
13631da177e4SLinus Torvalds 
1364a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
13651da177e4SLinus Torvalds 
1366e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1367e2761a11SOGAWA Hirofumi 	if (p) {
1368f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1369f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
13701da177e4SLinus Torvalds 		return p;
13711da177e4SLinus Torvalds 	}
13721da177e4SLinus Torvalds 
1373e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1374e2761a11SOGAWA Hirofumi 	if (p) {
1375f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1376f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
13771da177e4SLinus Torvalds 		return p;
13781da177e4SLinus Torvalds 	}
13791da177e4SLinus Torvalds 
1380f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1381f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
13821da177e4SLinus Torvalds 	return NULL;
13831da177e4SLinus Torvalds }
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
13861da177e4SLinus Torvalds {
13871b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
13881da177e4SLinus Torvalds 	if (p1 != p2) {
13891b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1390a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
13911da177e4SLinus Torvalds 	}
13921da177e4SLinus Torvalds }
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
13951da177e4SLinus Torvalds 		struct nameidata *nd)
13961da177e4SLinus Torvalds {
1397a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
13981da177e4SLinus Torvalds 
13991da177e4SLinus Torvalds 	if (error)
14001da177e4SLinus Torvalds 		return error;
14011da177e4SLinus Torvalds 
1402acfa4380SAl Viro 	if (!dir->i_op->create)
14031da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
14041da177e4SLinus Torvalds 	mode &= S_IALLUGO;
14051da177e4SLinus Torvalds 	mode |= S_IFREG;
14061da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
14071da177e4SLinus Torvalds 	if (error)
14081da177e4SLinus Torvalds 		return error;
14099e3509e2SJan Kara 	vfs_dq_init(dir);
14101da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
1411a74574aaSStephen Smalley 	if (!error)
1412f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
14131da177e4SLinus Torvalds 	return error;
14141da177e4SLinus Torvalds }
14151da177e4SLinus Torvalds 
14163fb64190SChristoph Hellwig int may_open(struct path *path, int acc_mode, int flag)
14171da177e4SLinus Torvalds {
14183fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
14191da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14201da177e4SLinus Torvalds 	int error;
14211da177e4SLinus Torvalds 
14221da177e4SLinus Torvalds 	if (!inode)
14231da177e4SLinus Torvalds 		return -ENOENT;
14241da177e4SLinus Torvalds 
1425c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
1426c8fe8f30SChristoph Hellwig 	case S_IFLNK:
14271da177e4SLinus Torvalds 		return -ELOOP;
1428c8fe8f30SChristoph Hellwig 	case S_IFDIR:
1429c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
14301da177e4SLinus Torvalds 			return -EISDIR;
1431c8fe8f30SChristoph Hellwig 		break;
1432c8fe8f30SChristoph Hellwig 	case S_IFBLK:
1433c8fe8f30SChristoph Hellwig 	case S_IFCHR:
14343fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
14351da177e4SLinus Torvalds 			return -EACCES;
1436c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
1437c8fe8f30SChristoph Hellwig 	case S_IFIFO:
1438c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
14391da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
1440c8fe8f30SChristoph Hellwig 		break;
14414a3fd211SDave Hansen 	}
1442b41572e9SDave Hansen 
14433fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
1444b41572e9SDave Hansen 	if (error)
1445b41572e9SDave Hansen 		return error;
14466146f0d5SMimi Zohar 
14471da177e4SLinus Torvalds 	/*
14481da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
14491da177e4SLinus Torvalds 	 */
14501da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
14511da177e4SLinus Torvalds 		if  ((flag & FMODE_WRITE) && !(flag & O_APPEND))
14527715b521SAl Viro 			return -EPERM;
14531da177e4SLinus Torvalds 		if (flag & O_TRUNC)
14547715b521SAl Viro 			return -EPERM;
14551da177e4SLinus Torvalds 	}
14561da177e4SLinus Torvalds 
14571da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
14587715b521SAl Viro 	if (flag & O_NOATIME && !is_owner_or_cap(inode))
14597715b521SAl Viro 		return -EPERM;
14601da177e4SLinus Torvalds 
14611da177e4SLinus Torvalds 	/*
14621da177e4SLinus Torvalds 	 * Ensure there are no outstanding leases on the file.
14631da177e4SLinus Torvalds 	 */
14641da177e4SLinus Torvalds 	error = break_lease(inode, flag);
14651da177e4SLinus Torvalds 	if (error)
14667715b521SAl Viro 		return error;
14671da177e4SLinus Torvalds 
14687715b521SAl Viro 	return ima_path_check(path, acc_mode ?
14697715b521SAl Viro 			       acc_mode & (MAY_READ | MAY_WRITE | MAY_EXEC) :
14707715b521SAl Viro 			       ACC_MODE(flag) & (MAY_READ | MAY_WRITE),
14717715b521SAl Viro 			       IMA_COUNT_UPDATE);
14727715b521SAl Viro }
14737715b521SAl Viro 
14747715b521SAl Viro static int handle_truncate(struct path *path)
14757715b521SAl Viro {
14767715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
14777715b521SAl Viro 	int error = get_write_access(inode);
14781da177e4SLinus Torvalds 	if (error)
14797715b521SAl Viro 		return error;
14801da177e4SLinus Torvalds 	/*
14811da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
14821da177e4SLinus Torvalds 	 */
14831da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
1484be6d3e56SKentaro Takeda 	if (!error)
14853fb64190SChristoph Hellwig 		error = security_path_truncate(path, 0,
1486be6d3e56SKentaro Takeda 				       ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
14871da177e4SLinus Torvalds 	if (!error) {
14887715b521SAl Viro 		error = do_truncate(path->dentry, 0,
1489d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
1490d139d7ffSMiklos Szeredi 				    NULL);
14911da177e4SLinus Torvalds 	}
14921da177e4SLinus Torvalds 	put_write_access(inode);
1493acd0c935SMimi Zohar 	return error;
14941da177e4SLinus Torvalds }
14951da177e4SLinus Torvalds 
1496d57999e1SDave Hansen /*
1497d57999e1SDave Hansen  * Be careful about ever adding any more callers of this
1498d57999e1SDave Hansen  * function.  Its flags must be in the namei format, not
1499d57999e1SDave Hansen  * what get passed to sys_open().
1500d57999e1SDave Hansen  */
1501d57999e1SDave Hansen static int __open_namei_create(struct nameidata *nd, struct path *path,
1502aab520e2SDave Hansen 				int flag, int mode)
1503aab520e2SDave Hansen {
1504aab520e2SDave Hansen 	int error;
15054ac91378SJan Blunck 	struct dentry *dir = nd->path.dentry;
1506aab520e2SDave Hansen 
1507aab520e2SDave Hansen 	if (!IS_POSIXACL(dir->d_inode))
1508ce3b0f8dSAl Viro 		mode &= ~current_umask();
1509be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd->path, path->dentry, mode, 0);
1510be6d3e56SKentaro Takeda 	if (error)
1511be6d3e56SKentaro Takeda 		goto out_unlock;
1512aab520e2SDave Hansen 	error = vfs_create(dir->d_inode, path->dentry, mode, nd);
1513be6d3e56SKentaro Takeda out_unlock:
1514aab520e2SDave Hansen 	mutex_unlock(&dir->d_inode->i_mutex);
15154ac91378SJan Blunck 	dput(nd->path.dentry);
15164ac91378SJan Blunck 	nd->path.dentry = path->dentry;
1517aab520e2SDave Hansen 	if (error)
1518aab520e2SDave Hansen 		return error;
1519aab520e2SDave Hansen 	/* Don't check for write permission, don't truncate */
15203fb64190SChristoph Hellwig 	return may_open(&nd->path, 0, flag & ~O_TRUNC);
1521aab520e2SDave Hansen }
1522aab520e2SDave Hansen 
15231da177e4SLinus Torvalds /*
1524d57999e1SDave Hansen  * Note that while the flag value (low two bits) for sys_open means:
1525d57999e1SDave Hansen  *	00 - read-only
1526d57999e1SDave Hansen  *	01 - write-only
1527d57999e1SDave Hansen  *	10 - read-write
1528d57999e1SDave Hansen  *	11 - special
1529d57999e1SDave Hansen  * it is changed into
1530d57999e1SDave Hansen  *	00 - no permissions needed
1531d57999e1SDave Hansen  *	01 - read-permission
1532d57999e1SDave Hansen  *	10 - write-permission
1533d57999e1SDave Hansen  *	11 - read-write
1534d57999e1SDave Hansen  * for the internal routines (ie open_namei()/follow_link() etc)
1535d57999e1SDave Hansen  * This is more logical, and also allows the 00 "no perm needed"
1536d57999e1SDave Hansen  * to be used for symlinks (where the permissions are checked
1537d57999e1SDave Hansen  * later).
1538d57999e1SDave Hansen  *
1539d57999e1SDave Hansen */
1540d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
1541d57999e1SDave Hansen {
1542d57999e1SDave Hansen 	if ((flag+1) & O_ACCMODE)
1543d57999e1SDave Hansen 		flag++;
1544d57999e1SDave Hansen 	return flag;
1545d57999e1SDave Hansen }
1546d57999e1SDave Hansen 
15477715b521SAl Viro static int open_will_truncate(int flag, struct inode *inode)
15484a3fd211SDave Hansen {
1549d57999e1SDave Hansen 	/*
15504a3fd211SDave Hansen 	 * We'll never write to the fs underlying
15514a3fd211SDave Hansen 	 * a device file.
15524a3fd211SDave Hansen 	 */
15534a3fd211SDave Hansen 	if (special_file(inode->i_mode))
15544a3fd211SDave Hansen 		return 0;
15554a3fd211SDave Hansen 	return (flag & O_TRUNC);
15564a3fd211SDave Hansen }
15574a3fd211SDave Hansen 
15584a3fd211SDave Hansen /*
15594a3fd211SDave Hansen  * Note that the low bits of the passed in "open_flag"
15604a3fd211SDave Hansen  * are not the same as in the local variable "flag". See
15614a3fd211SDave Hansen  * open_to_namei_flags() for more details.
15621da177e4SLinus Torvalds  */
1563a70e65dfSChristoph Hellwig struct file *do_filp_open(int dfd, const char *pathname,
15646e8341a1SAl Viro 		int open_flag, int mode, int acc_mode)
15651da177e4SLinus Torvalds {
15664a3fd211SDave Hansen 	struct file *filp;
1567a70e65dfSChristoph Hellwig 	struct nameidata nd;
15686e8341a1SAl Viro 	int error;
15696de88d72SAl Viro 	struct path path, save;
15701da177e4SLinus Torvalds 	struct dentry *dir;
15711da177e4SLinus Torvalds 	int count = 0;
15727715b521SAl Viro 	int will_truncate;
1573d57999e1SDave Hansen 	int flag = open_to_namei_flags(open_flag);
15741da177e4SLinus Torvalds 
15756b2f3d1fSChristoph Hellwig 	/*
15766b2f3d1fSChristoph Hellwig 	 * O_SYNC is implemented as __O_SYNC|O_DSYNC.  As many places only
15776b2f3d1fSChristoph Hellwig 	 * check for O_DSYNC if the need any syncing at all we enforce it's
15786b2f3d1fSChristoph Hellwig 	 * always set instead of having to deal with possibly weird behaviour
15796b2f3d1fSChristoph Hellwig 	 * for malicious applications setting only __O_SYNC.
15806b2f3d1fSChristoph Hellwig 	 */
15816b2f3d1fSChristoph Hellwig 	if (open_flag & __O_SYNC)
15826b2f3d1fSChristoph Hellwig 		open_flag |= O_DSYNC;
15836b2f3d1fSChristoph Hellwig 
15846e8341a1SAl Viro 	if (!acc_mode)
1585b77b0646SAl Viro 		acc_mode = MAY_OPEN | ACC_MODE(flag);
15861da177e4SLinus Torvalds 
1587834f2a4aSTrond Myklebust 	/* O_TRUNC implies we need access checks for write permissions */
1588834f2a4aSTrond Myklebust 	if (flag & O_TRUNC)
1589834f2a4aSTrond Myklebust 		acc_mode |= MAY_WRITE;
1590834f2a4aSTrond Myklebust 
15911da177e4SLinus Torvalds 	/* Allow the LSM permission hook to distinguish append
15921da177e4SLinus Torvalds 	   access from general write access. */
15931da177e4SLinus Torvalds 	if (flag & O_APPEND)
15941da177e4SLinus Torvalds 		acc_mode |= MAY_APPEND;
15951da177e4SLinus Torvalds 
15961da177e4SLinus Torvalds 	/*
15971da177e4SLinus Torvalds 	 * The simplest case - just a plain lookup.
15981da177e4SLinus Torvalds 	 */
15991da177e4SLinus Torvalds 	if (!(flag & O_CREAT)) {
16002dd6d1f4SAl Viro 		filp = get_empty_filp();
16012dd6d1f4SAl Viro 
16022dd6d1f4SAl Viro 		if (filp == NULL)
16032dd6d1f4SAl Viro 			return ERR_PTR(-ENFILE);
16042dd6d1f4SAl Viro 		nd.intent.open.file = filp;
16052dd6d1f4SAl Viro 		nd.intent.open.flags = flag;
16062dd6d1f4SAl Viro 		nd.intent.open.create_mode = 0;
16072dd6d1f4SAl Viro 		error = do_path_lookup(dfd, pathname,
16082dd6d1f4SAl Viro 					lookup_flags(flag)|LOOKUP_OPEN, &nd);
16092dd6d1f4SAl Viro 		if (IS_ERR(nd.intent.open.file)) {
16102dd6d1f4SAl Viro 			if (error == 0) {
16112dd6d1f4SAl Viro 				error = PTR_ERR(nd.intent.open.file);
16122dd6d1f4SAl Viro 				path_put(&nd.path);
16132dd6d1f4SAl Viro 			}
16142dd6d1f4SAl Viro 		} else if (error)
16152dd6d1f4SAl Viro 			release_open_intent(&nd);
16161da177e4SLinus Torvalds 		if (error)
1617a70e65dfSChristoph Hellwig 			return ERR_PTR(error);
16181da177e4SLinus Torvalds 		goto ok;
16191da177e4SLinus Torvalds 	}
16201da177e4SLinus Torvalds 
16211da177e4SLinus Torvalds 	/*
16221da177e4SLinus Torvalds 	 * Create - we need to know the parent.
16231da177e4SLinus Torvalds 	 */
16249b4a9b14SAl Viro 	error = path_init(dfd, pathname, LOOKUP_PARENT, &nd);
16251da177e4SLinus Torvalds 	if (error)
1626a70e65dfSChristoph Hellwig 		return ERR_PTR(error);
16279b4a9b14SAl Viro 	error = path_walk(pathname, &nd);
1628654f562cSJ. R. Okajima 	if (error) {
1629654f562cSJ. R. Okajima 		if (nd.root.mnt)
1630654f562cSJ. R. Okajima 			path_put(&nd.root);
16319b4a9b14SAl Viro 		return ERR_PTR(error);
1632654f562cSJ. R. Okajima 	}
16339b4a9b14SAl Viro 	if (unlikely(!audit_dummy_context()))
16349b4a9b14SAl Viro 		audit_inode(pathname, nd.path.dentry);
16351da177e4SLinus Torvalds 
16361da177e4SLinus Torvalds 	/*
16371da177e4SLinus Torvalds 	 * We have the parent and last component. First of all, check
16381da177e4SLinus Torvalds 	 * that we are not asked to creat(2) an obvious directory - that
16391da177e4SLinus Torvalds 	 * will not do.
16401da177e4SLinus Torvalds 	 */
16411da177e4SLinus Torvalds 	error = -EISDIR;
1642a70e65dfSChristoph Hellwig 	if (nd.last_type != LAST_NORM || nd.last.name[nd.last.len])
16438737f3a1SAl Viro 		goto exit_parent;
16441da177e4SLinus Torvalds 
16458737f3a1SAl Viro 	error = -ENFILE;
16468737f3a1SAl Viro 	filp = get_empty_filp();
16478737f3a1SAl Viro 	if (filp == NULL)
16488737f3a1SAl Viro 		goto exit_parent;
16498737f3a1SAl Viro 	nd.intent.open.file = filp;
16508737f3a1SAl Viro 	nd.intent.open.flags = flag;
16518737f3a1SAl Viro 	nd.intent.open.create_mode = mode;
1652a70e65dfSChristoph Hellwig 	dir = nd.path.dentry;
1653a70e65dfSChristoph Hellwig 	nd.flags &= ~LOOKUP_PARENT;
16548737f3a1SAl Viro 	nd.flags |= LOOKUP_CREATE | LOOKUP_OPEN;
16553516586aSAl Viro 	if (flag & O_EXCL)
16563516586aSAl Viro 		nd.flags |= LOOKUP_EXCL;
16571b1dcc1bSJes Sorensen 	mutex_lock(&dir->d_inode->i_mutex);
1658a70e65dfSChristoph Hellwig 	path.dentry = lookup_hash(&nd);
1659a70e65dfSChristoph Hellwig 	path.mnt = nd.path.mnt;
16601da177e4SLinus Torvalds 
16611da177e4SLinus Torvalds do_last:
16624e7506e4SAl Viro 	error = PTR_ERR(path.dentry);
16634e7506e4SAl Viro 	if (IS_ERR(path.dentry)) {
16641b1dcc1bSJes Sorensen 		mutex_unlock(&dir->d_inode->i_mutex);
16651da177e4SLinus Torvalds 		goto exit;
16661da177e4SLinus Torvalds 	}
16671da177e4SLinus Torvalds 
1668a70e65dfSChristoph Hellwig 	if (IS_ERR(nd.intent.open.file)) {
1669a70e65dfSChristoph Hellwig 		error = PTR_ERR(nd.intent.open.file);
16704a3fd211SDave Hansen 		goto exit_mutex_unlock;
16714af4c52fSOleg Drokin 	}
16724af4c52fSOleg Drokin 
16731da177e4SLinus Torvalds 	/* Negative dentry, just create the file */
16744e7506e4SAl Viro 	if (!path.dentry->d_inode) {
16754a3fd211SDave Hansen 		/*
16764a3fd211SDave Hansen 		 * This write is needed to ensure that a
16774a3fd211SDave Hansen 		 * ro->rw transition does not occur between
16784a3fd211SDave Hansen 		 * the time when the file is created and when
16794a3fd211SDave Hansen 		 * a permanent write count is taken through
16804a3fd211SDave Hansen 		 * the 'struct file' in nameidata_to_filp().
16814a3fd211SDave Hansen 		 */
16824a3fd211SDave Hansen 		error = mnt_want_write(nd.path.mnt);
16831da177e4SLinus Torvalds 		if (error)
16844a3fd211SDave Hansen 			goto exit_mutex_unlock;
16854a3fd211SDave Hansen 		error = __open_namei_create(&nd, &path, flag, mode);
16864a3fd211SDave Hansen 		if (error) {
16874a3fd211SDave Hansen 			mnt_drop_write(nd.path.mnt);
16881da177e4SLinus Torvalds 			goto exit;
16894a3fd211SDave Hansen 		}
16904a3fd211SDave Hansen 		filp = nameidata_to_filp(&nd, open_flag);
169194e5d714SMimi Zohar 		if (IS_ERR(filp))
169294e5d714SMimi Zohar 			ima_counts_put(&nd.path,
169394e5d714SMimi Zohar 				       acc_mode & (MAY_READ | MAY_WRITE |
169494e5d714SMimi Zohar 						   MAY_EXEC));
16954a3fd211SDave Hansen 		mnt_drop_write(nd.path.mnt);
1696654f562cSJ. R. Okajima 		if (nd.root.mnt)
1697654f562cSJ. R. Okajima 			path_put(&nd.root);
16984a3fd211SDave Hansen 		return filp;
16991da177e4SLinus Torvalds 	}
17001da177e4SLinus Torvalds 
17011da177e4SLinus Torvalds 	/*
17021da177e4SLinus Torvalds 	 * It already exists.
17031da177e4SLinus Torvalds 	 */
17041b1dcc1bSJes Sorensen 	mutex_unlock(&dir->d_inode->i_mutex);
17055a190ae6SAl Viro 	audit_inode(pathname, path.dentry);
17061da177e4SLinus Torvalds 
17071da177e4SLinus Torvalds 	error = -EEXIST;
17081da177e4SLinus Torvalds 	if (flag & O_EXCL)
17091da177e4SLinus Torvalds 		goto exit_dput;
17101da177e4SLinus Torvalds 
1711e13b210fSAl Viro 	if (__follow_mount(&path)) {
17121da177e4SLinus Torvalds 		error = -ELOOP;
1713ba7a4c1aSAl Viro 		if (flag & O_NOFOLLOW)
1714ba7a4c1aSAl Viro 			goto exit_dput;
17151da177e4SLinus Torvalds 	}
17163e2efce0SAmy Griffis 
17171da177e4SLinus Torvalds 	error = -ENOENT;
17184e7506e4SAl Viro 	if (!path.dentry->d_inode)
17191da177e4SLinus Torvalds 		goto exit_dput;
1720acfa4380SAl Viro 	if (path.dentry->d_inode->i_op->follow_link)
17211da177e4SLinus Torvalds 		goto do_link;
17221da177e4SLinus Torvalds 
1723a70e65dfSChristoph Hellwig 	path_to_nameidata(&path, &nd);
17241da177e4SLinus Torvalds 	error = -EISDIR;
17254e7506e4SAl Viro 	if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
17261da177e4SLinus Torvalds 		goto exit;
17271da177e4SLinus Torvalds ok:
17284a3fd211SDave Hansen 	/*
17294a3fd211SDave Hansen 	 * Consider:
17304a3fd211SDave Hansen 	 * 1. may_open() truncates a file
17314a3fd211SDave Hansen 	 * 2. a rw->ro mount transition occurs
17324a3fd211SDave Hansen 	 * 3. nameidata_to_filp() fails due to
17334a3fd211SDave Hansen 	 *    the ro mount.
17344a3fd211SDave Hansen 	 * That would be inconsistent, and should
17354a3fd211SDave Hansen 	 * be avoided. Taking this mnt write here
17364a3fd211SDave Hansen 	 * ensures that (2) can not occur.
17374a3fd211SDave Hansen 	 */
17387715b521SAl Viro 	will_truncate = open_will_truncate(flag, nd.path.dentry->d_inode);
17397715b521SAl Viro 	if (will_truncate) {
17404a3fd211SDave Hansen 		error = mnt_want_write(nd.path.mnt);
17411da177e4SLinus Torvalds 		if (error)
17421da177e4SLinus Torvalds 			goto exit;
17434a3fd211SDave Hansen 	}
17443fb64190SChristoph Hellwig 	error = may_open(&nd.path, acc_mode, flag);
17454a3fd211SDave Hansen 	if (error) {
17467715b521SAl Viro 		if (will_truncate)
17474a3fd211SDave Hansen 			mnt_drop_write(nd.path.mnt);
17484a3fd211SDave Hansen 		goto exit;
17494a3fd211SDave Hansen 	}
17504a3fd211SDave Hansen 	filp = nameidata_to_filp(&nd, open_flag);
17517715b521SAl Viro 	if (IS_ERR(filp)) {
175294e5d714SMimi Zohar 		ima_counts_put(&nd.path,
175394e5d714SMimi Zohar 			       acc_mode & (MAY_READ | MAY_WRITE | MAY_EXEC));
17547715b521SAl Viro 		if (will_truncate)
17557715b521SAl Viro 			mnt_drop_write(nd.path.mnt);
17567715b521SAl Viro 		if (nd.root.mnt)
17577715b521SAl Viro 			path_put(&nd.root);
17587715b521SAl Viro 		return filp;
17597715b521SAl Viro 	}
17607715b521SAl Viro 
17617715b521SAl Viro 	if (acc_mode & MAY_WRITE)
17627715b521SAl Viro 		vfs_dq_init(nd.path.dentry->d_inode);
17637715b521SAl Viro 
17647715b521SAl Viro 	if (will_truncate) {
17657715b521SAl Viro 		error = handle_truncate(&nd.path);
17667715b521SAl Viro 		if (error) {
17677715b521SAl Viro 			mnt_drop_write(nd.path.mnt);
17687715b521SAl Viro 			fput(filp);
17697715b521SAl Viro 			if (nd.root.mnt)
17707715b521SAl Viro 				path_put(&nd.root);
17717715b521SAl Viro 			return ERR_PTR(error);
17727715b521SAl Viro 		}
17737715b521SAl Viro 	}
17744a3fd211SDave Hansen 	/*
17754a3fd211SDave Hansen 	 * It is now safe to drop the mnt write
17764a3fd211SDave Hansen 	 * because the filp has had a write taken
17774a3fd211SDave Hansen 	 * on its behalf.
17784a3fd211SDave Hansen 	 */
17797715b521SAl Viro 	if (will_truncate)
17804a3fd211SDave Hansen 		mnt_drop_write(nd.path.mnt);
1781654f562cSJ. R. Okajima 	if (nd.root.mnt)
1782654f562cSJ. R. Okajima 		path_put(&nd.root);
17834a3fd211SDave Hansen 	return filp;
17841da177e4SLinus Torvalds 
17854a3fd211SDave Hansen exit_mutex_unlock:
17864a3fd211SDave Hansen 	mutex_unlock(&dir->d_inode->i_mutex);
17871da177e4SLinus Torvalds exit_dput:
1788a70e65dfSChristoph Hellwig 	path_put_conditional(&path, &nd);
17891da177e4SLinus Torvalds exit:
1790a70e65dfSChristoph Hellwig 	if (!IS_ERR(nd.intent.open.file))
1791a70e65dfSChristoph Hellwig 		release_open_intent(&nd);
17928737f3a1SAl Viro exit_parent:
17932a737871SAl Viro 	if (nd.root.mnt)
17942a737871SAl Viro 		path_put(&nd.root);
1795a70e65dfSChristoph Hellwig 	path_put(&nd.path);
1796a70e65dfSChristoph Hellwig 	return ERR_PTR(error);
17971da177e4SLinus Torvalds 
17981da177e4SLinus Torvalds do_link:
17991da177e4SLinus Torvalds 	error = -ELOOP;
18001da177e4SLinus Torvalds 	if (flag & O_NOFOLLOW)
18011da177e4SLinus Torvalds 		goto exit_dput;
18021da177e4SLinus Torvalds 	/*
18031da177e4SLinus Torvalds 	 * This is subtle. Instead of calling do_follow_link() we do the
18041da177e4SLinus Torvalds 	 * thing by hands. The reason is that this way we have zero link_count
18051da177e4SLinus Torvalds 	 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
18061da177e4SLinus Torvalds 	 * After that we have the parent and last component, i.e.
18071da177e4SLinus Torvalds 	 * we are in the same situation as after the first path_walk().
18081da177e4SLinus Torvalds 	 * Well, almost - if the last component is normal we get its copy
18091da177e4SLinus Torvalds 	 * stored in nd->last.name and we will have to putname() it when we
18101da177e4SLinus Torvalds 	 * are done. Procfs-like symlinks just set LAST_BIND.
18111da177e4SLinus Torvalds 	 */
1812a70e65dfSChristoph Hellwig 	nd.flags |= LOOKUP_PARENT;
1813a70e65dfSChristoph Hellwig 	error = security_inode_follow_link(path.dentry, &nd);
18141da177e4SLinus Torvalds 	if (error)
18151da177e4SLinus Torvalds 		goto exit_dput;
18166de88d72SAl Viro 	save = nd.path;
18176de88d72SAl Viro 	path_get(&save);
1818a70e65dfSChristoph Hellwig 	error = __do_follow_link(&path, &nd);
18196de88d72SAl Viro 	if (error == -ESTALE) {
18206de88d72SAl Viro 		/* nd.path had been dropped */
18216de88d72SAl Viro 		nd.path = save;
18226de88d72SAl Viro 		path_get(&nd.path);
18236de88d72SAl Viro 		nd.flags |= LOOKUP_REVAL;
18246de88d72SAl Viro 		error = __do_follow_link(&path, &nd);
18256de88d72SAl Viro 	}
18266de88d72SAl Viro 	path_put(&save);
1827258fa999SAl Viro 	path_put(&path);
1828de459215SKirill Korotaev 	if (error) {
1829de459215SKirill Korotaev 		/* Does someone understand code flow here? Or it is only
1830de459215SKirill Korotaev 		 * me so stupid? Anathema to whoever designed this non-sense
1831de459215SKirill Korotaev 		 * with "intent.open".
1832de459215SKirill Korotaev 		 */
1833a70e65dfSChristoph Hellwig 		release_open_intent(&nd);
1834654f562cSJ. R. Okajima 		if (nd.root.mnt)
1835654f562cSJ. R. Okajima 			path_put(&nd.root);
1836a70e65dfSChristoph Hellwig 		return ERR_PTR(error);
1837de459215SKirill Korotaev 	}
1838a70e65dfSChristoph Hellwig 	nd.flags &= ~LOOKUP_PARENT;
1839a70e65dfSChristoph Hellwig 	if (nd.last_type == LAST_BIND)
18401da177e4SLinus Torvalds 		goto ok;
18411da177e4SLinus Torvalds 	error = -EISDIR;
1842a70e65dfSChristoph Hellwig 	if (nd.last_type != LAST_NORM)
18431da177e4SLinus Torvalds 		goto exit;
1844a70e65dfSChristoph Hellwig 	if (nd.last.name[nd.last.len]) {
1845a70e65dfSChristoph Hellwig 		__putname(nd.last.name);
18461da177e4SLinus Torvalds 		goto exit;
18471da177e4SLinus Torvalds 	}
18481da177e4SLinus Torvalds 	error = -ELOOP;
18491da177e4SLinus Torvalds 	if (count++==32) {
1850a70e65dfSChristoph Hellwig 		__putname(nd.last.name);
18511da177e4SLinus Torvalds 		goto exit;
18521da177e4SLinus Torvalds 	}
1853a70e65dfSChristoph Hellwig 	dir = nd.path.dentry;
18541b1dcc1bSJes Sorensen 	mutex_lock(&dir->d_inode->i_mutex);
1855a70e65dfSChristoph Hellwig 	path.dentry = lookup_hash(&nd);
1856a70e65dfSChristoph Hellwig 	path.mnt = nd.path.mnt;
1857a70e65dfSChristoph Hellwig 	__putname(nd.last.name);
18581da177e4SLinus Torvalds 	goto do_last;
18591da177e4SLinus Torvalds }
18601da177e4SLinus Torvalds 
18611da177e4SLinus Torvalds /**
1862a70e65dfSChristoph Hellwig  * filp_open - open file and return file pointer
1863a70e65dfSChristoph Hellwig  *
1864a70e65dfSChristoph Hellwig  * @filename:	path to open
1865a70e65dfSChristoph Hellwig  * @flags:	open flags as per the open(2) second argument
1866a70e65dfSChristoph Hellwig  * @mode:	mode for the new file if O_CREAT is set, else ignored
1867a70e65dfSChristoph Hellwig  *
1868a70e65dfSChristoph Hellwig  * This is the helper to open a file from kernelspace if you really
1869a70e65dfSChristoph Hellwig  * have to.  But in generally you should not do this, so please move
1870a70e65dfSChristoph Hellwig  * along, nothing to see here..
1871a70e65dfSChristoph Hellwig  */
1872a70e65dfSChristoph Hellwig struct file *filp_open(const char *filename, int flags, int mode)
1873a70e65dfSChristoph Hellwig {
18746e8341a1SAl Viro 	return do_filp_open(AT_FDCWD, filename, flags, mode, 0);
1875a70e65dfSChristoph Hellwig }
1876a70e65dfSChristoph Hellwig EXPORT_SYMBOL(filp_open);
1877a70e65dfSChristoph Hellwig 
1878a70e65dfSChristoph Hellwig /**
18791da177e4SLinus Torvalds  * lookup_create - lookup a dentry, creating it if it doesn't exist
18801da177e4SLinus Torvalds  * @nd: nameidata info
18811da177e4SLinus Torvalds  * @is_dir: directory flag
18821da177e4SLinus Torvalds  *
18831da177e4SLinus Torvalds  * Simple function to lookup and return a dentry and create it
18841da177e4SLinus Torvalds  * if it doesn't exist.  Is SMP-safe.
1885c663e5d8SChristoph Hellwig  *
18864ac91378SJan Blunck  * Returns with nd->path.dentry->d_inode->i_mutex locked.
18871da177e4SLinus Torvalds  */
18881da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir)
18891da177e4SLinus Torvalds {
1890c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
18911da177e4SLinus Torvalds 
18924ac91378SJan Blunck 	mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
1893c663e5d8SChristoph Hellwig 	/*
1894c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
1895c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
1896c663e5d8SChristoph Hellwig 	 */
18971da177e4SLinus Torvalds 	if (nd->last_type != LAST_NORM)
18981da177e4SLinus Torvalds 		goto fail;
18991da177e4SLinus Torvalds 	nd->flags &= ~LOOKUP_PARENT;
19003516586aSAl Viro 	nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
1901a634904aSASANO Masahiro 	nd->intent.open.flags = O_EXCL;
1902c663e5d8SChristoph Hellwig 
1903c663e5d8SChristoph Hellwig 	/*
1904c663e5d8SChristoph Hellwig 	 * Do the final lookup.
1905c663e5d8SChristoph Hellwig 	 */
190649705b77SChristoph Hellwig 	dentry = lookup_hash(nd);
19071da177e4SLinus Torvalds 	if (IS_ERR(dentry))
19081da177e4SLinus Torvalds 		goto fail;
1909c663e5d8SChristoph Hellwig 
1910e9baf6e5SAl Viro 	if (dentry->d_inode)
1911e9baf6e5SAl Viro 		goto eexist;
1912c663e5d8SChristoph Hellwig 	/*
1913c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
1914c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
1915c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
1916c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
1917c663e5d8SChristoph Hellwig 	 */
1918e9baf6e5SAl Viro 	if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
19191da177e4SLinus Torvalds 		dput(dentry);
19201da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
1921e9baf6e5SAl Viro 	}
1922e9baf6e5SAl Viro 	return dentry;
1923e9baf6e5SAl Viro eexist:
1924e9baf6e5SAl Viro 	dput(dentry);
1925e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
19261da177e4SLinus Torvalds fail:
19271da177e4SLinus Torvalds 	return dentry;
19281da177e4SLinus Torvalds }
1929f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create);
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
19321da177e4SLinus Torvalds {
1933a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds 	if (error)
19361da177e4SLinus Torvalds 		return error;
19371da177e4SLinus Torvalds 
19381da177e4SLinus Torvalds 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
19391da177e4SLinus Torvalds 		return -EPERM;
19401da177e4SLinus Torvalds 
1941acfa4380SAl Viro 	if (!dir->i_op->mknod)
19421da177e4SLinus Torvalds 		return -EPERM;
19431da177e4SLinus Torvalds 
194408ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
194508ce5f16SSerge E. Hallyn 	if (error)
194608ce5f16SSerge E. Hallyn 		return error;
194708ce5f16SSerge E. Hallyn 
19481da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
19491da177e4SLinus Torvalds 	if (error)
19501da177e4SLinus Torvalds 		return error;
19511da177e4SLinus Torvalds 
19529e3509e2SJan Kara 	vfs_dq_init(dir);
19531da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
1954a74574aaSStephen Smalley 	if (!error)
1955f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
19561da177e4SLinus Torvalds 	return error;
19571da177e4SLinus Torvalds }
19581da177e4SLinus Torvalds 
1959463c3197SDave Hansen static int may_mknod(mode_t mode)
1960463c3197SDave Hansen {
1961463c3197SDave Hansen 	switch (mode & S_IFMT) {
1962463c3197SDave Hansen 	case S_IFREG:
1963463c3197SDave Hansen 	case S_IFCHR:
1964463c3197SDave Hansen 	case S_IFBLK:
1965463c3197SDave Hansen 	case S_IFIFO:
1966463c3197SDave Hansen 	case S_IFSOCK:
1967463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
1968463c3197SDave Hansen 		return 0;
1969463c3197SDave Hansen 	case S_IFDIR:
1970463c3197SDave Hansen 		return -EPERM;
1971463c3197SDave Hansen 	default:
1972463c3197SDave Hansen 		return -EINVAL;
1973463c3197SDave Hansen 	}
1974463c3197SDave Hansen }
1975463c3197SDave Hansen 
19762e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
19772e4d0924SHeiko Carstens 		unsigned, dev)
19781da177e4SLinus Torvalds {
19792ad94ae6SAl Viro 	int error;
19801da177e4SLinus Torvalds 	char *tmp;
19811da177e4SLinus Torvalds 	struct dentry *dentry;
19821da177e4SLinus Torvalds 	struct nameidata nd;
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds 	if (S_ISDIR(mode))
19851da177e4SLinus Torvalds 		return -EPERM;
19861da177e4SLinus Torvalds 
19872ad94ae6SAl Viro 	error = user_path_parent(dfd, filename, &nd, &tmp);
19881da177e4SLinus Torvalds 	if (error)
19892ad94ae6SAl Viro 		return error;
19902ad94ae6SAl Viro 
19911da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
1992463c3197SDave Hansen 	if (IS_ERR(dentry)) {
19931da177e4SLinus Torvalds 		error = PTR_ERR(dentry);
1994463c3197SDave Hansen 		goto out_unlock;
1995463c3197SDave Hansen 	}
19964ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
1997ce3b0f8dSAl Viro 		mode &= ~current_umask();
1998463c3197SDave Hansen 	error = may_mknod(mode);
1999463c3197SDave Hansen 	if (error)
2000463c3197SDave Hansen 		goto out_dput;
2001463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2002463c3197SDave Hansen 	if (error)
2003463c3197SDave Hansen 		goto out_dput;
2004be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd.path, dentry, mode, dev);
2005be6d3e56SKentaro Takeda 	if (error)
2006be6d3e56SKentaro Takeda 		goto out_drop_write;
20071da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
20081da177e4SLinus Torvalds 		case 0: case S_IFREG:
20094ac91378SJan Blunck 			error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
20101da177e4SLinus Torvalds 			break;
20111da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
20124ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
20131da177e4SLinus Torvalds 					new_decode_dev(dev));
20141da177e4SLinus Torvalds 			break;
20151da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
20164ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
20171da177e4SLinus Torvalds 			break;
20181da177e4SLinus Torvalds 	}
2019be6d3e56SKentaro Takeda out_drop_write:
2020463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2021463c3197SDave Hansen out_dput:
20221da177e4SLinus Torvalds 	dput(dentry);
2023463c3197SDave Hansen out_unlock:
20244ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
20251d957f9bSJan Blunck 	path_put(&nd.path);
20261da177e4SLinus Torvalds 	putname(tmp);
20271da177e4SLinus Torvalds 
20281da177e4SLinus Torvalds 	return error;
20291da177e4SLinus Torvalds }
20301da177e4SLinus Torvalds 
20313480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
20325590ff0dSUlrich Drepper {
20335590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
20345590ff0dSUlrich Drepper }
20355590ff0dSUlrich Drepper 
20361da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
20371da177e4SLinus Torvalds {
2038a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
20391da177e4SLinus Torvalds 
20401da177e4SLinus Torvalds 	if (error)
20411da177e4SLinus Torvalds 		return error;
20421da177e4SLinus Torvalds 
2043acfa4380SAl Viro 	if (!dir->i_op->mkdir)
20441da177e4SLinus Torvalds 		return -EPERM;
20451da177e4SLinus Torvalds 
20461da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
20471da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
20481da177e4SLinus Torvalds 	if (error)
20491da177e4SLinus Torvalds 		return error;
20501da177e4SLinus Torvalds 
20519e3509e2SJan Kara 	vfs_dq_init(dir);
20521da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2053a74574aaSStephen Smalley 	if (!error)
2054f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
20551da177e4SLinus Torvalds 	return error;
20561da177e4SLinus Torvalds }
20571da177e4SLinus Torvalds 
20582e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
20591da177e4SLinus Torvalds {
20601da177e4SLinus Torvalds 	int error = 0;
20611da177e4SLinus Torvalds 	char * tmp;
20626902d925SDave Hansen 	struct dentry *dentry;
20636902d925SDave Hansen 	struct nameidata nd;
20641da177e4SLinus Torvalds 
20652ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &tmp);
20662ad94ae6SAl Viro 	if (error)
20676902d925SDave Hansen 		goto out_err;
20681da177e4SLinus Torvalds 
20691da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 1);
20701da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
20716902d925SDave Hansen 	if (IS_ERR(dentry))
20726902d925SDave Hansen 		goto out_unlock;
20736902d925SDave Hansen 
20744ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2075ce3b0f8dSAl Viro 		mode &= ~current_umask();
2076463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2077463c3197SDave Hansen 	if (error)
2078463c3197SDave Hansen 		goto out_dput;
2079be6d3e56SKentaro Takeda 	error = security_path_mkdir(&nd.path, dentry, mode);
2080be6d3e56SKentaro Takeda 	if (error)
2081be6d3e56SKentaro Takeda 		goto out_drop_write;
20824ac91378SJan Blunck 	error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2083be6d3e56SKentaro Takeda out_drop_write:
2084463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2085463c3197SDave Hansen out_dput:
20861da177e4SLinus Torvalds 	dput(dentry);
20876902d925SDave Hansen out_unlock:
20884ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
20891d957f9bSJan Blunck 	path_put(&nd.path);
20901da177e4SLinus Torvalds 	putname(tmp);
20916902d925SDave Hansen out_err:
20921da177e4SLinus Torvalds 	return error;
20931da177e4SLinus Torvalds }
20941da177e4SLinus Torvalds 
20953cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
20965590ff0dSUlrich Drepper {
20975590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
20985590ff0dSUlrich Drepper }
20995590ff0dSUlrich Drepper 
21001da177e4SLinus Torvalds /*
21011da177e4SLinus Torvalds  * We try to drop the dentry early: we should have
21021da177e4SLinus Torvalds  * a usage count of 2 if we're the only user of this
21031da177e4SLinus Torvalds  * dentry, and if that is true (possibly after pruning
21041da177e4SLinus Torvalds  * the dcache), then we drop the dentry now.
21051da177e4SLinus Torvalds  *
21061da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
21071da177e4SLinus Torvalds  * do a
21081da177e4SLinus Torvalds  *
21091da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
21101da177e4SLinus Torvalds  *		return -EBUSY;
21111da177e4SLinus Torvalds  *
21121da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
21131da177e4SLinus Torvalds  * that is still in use by something else..
21141da177e4SLinus Torvalds  */
21151da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
21161da177e4SLinus Torvalds {
21171da177e4SLinus Torvalds 	dget(dentry);
21181da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
21191da177e4SLinus Torvalds 	spin_lock(&dcache_lock);
21201da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
21211da177e4SLinus Torvalds 	if (atomic_read(&dentry->d_count) == 2)
21221da177e4SLinus Torvalds 		__d_drop(dentry);
21231da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
21241da177e4SLinus Torvalds 	spin_unlock(&dcache_lock);
21251da177e4SLinus Torvalds }
21261da177e4SLinus Torvalds 
21271da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
21281da177e4SLinus Torvalds {
21291da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
21301da177e4SLinus Torvalds 
21311da177e4SLinus Torvalds 	if (error)
21321da177e4SLinus Torvalds 		return error;
21331da177e4SLinus Torvalds 
2134acfa4380SAl Viro 	if (!dir->i_op->rmdir)
21351da177e4SLinus Torvalds 		return -EPERM;
21361da177e4SLinus Torvalds 
21379e3509e2SJan Kara 	vfs_dq_init(dir);
21381da177e4SLinus Torvalds 
21391b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
21401da177e4SLinus Torvalds 	dentry_unhash(dentry);
21411da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
21421da177e4SLinus Torvalds 		error = -EBUSY;
21431da177e4SLinus Torvalds 	else {
21441da177e4SLinus Torvalds 		error = security_inode_rmdir(dir, dentry);
21451da177e4SLinus Torvalds 		if (!error) {
21461da177e4SLinus Torvalds 			error = dir->i_op->rmdir(dir, dentry);
21471da177e4SLinus Torvalds 			if (!error)
21481da177e4SLinus Torvalds 				dentry->d_inode->i_flags |= S_DEAD;
21491da177e4SLinus Torvalds 		}
21501da177e4SLinus Torvalds 	}
21511b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
21521da177e4SLinus Torvalds 	if (!error) {
21531da177e4SLinus Torvalds 		d_delete(dentry);
21541da177e4SLinus Torvalds 	}
21551da177e4SLinus Torvalds 	dput(dentry);
21561da177e4SLinus Torvalds 
21571da177e4SLinus Torvalds 	return error;
21581da177e4SLinus Torvalds }
21591da177e4SLinus Torvalds 
21605590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
21611da177e4SLinus Torvalds {
21621da177e4SLinus Torvalds 	int error = 0;
21631da177e4SLinus Torvalds 	char * name;
21641da177e4SLinus Torvalds 	struct dentry *dentry;
21651da177e4SLinus Torvalds 	struct nameidata nd;
21661da177e4SLinus Torvalds 
21672ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
21681da177e4SLinus Torvalds 	if (error)
21692ad94ae6SAl Viro 		return error;
21701da177e4SLinus Torvalds 
21711da177e4SLinus Torvalds 	switch(nd.last_type) {
21721da177e4SLinus Torvalds 	case LAST_DOTDOT:
21731da177e4SLinus Torvalds 		error = -ENOTEMPTY;
21741da177e4SLinus Torvalds 		goto exit1;
21751da177e4SLinus Torvalds 	case LAST_DOT:
21761da177e4SLinus Torvalds 		error = -EINVAL;
21771da177e4SLinus Torvalds 		goto exit1;
21781da177e4SLinus Torvalds 	case LAST_ROOT:
21791da177e4SLinus Torvalds 		error = -EBUSY;
21801da177e4SLinus Torvalds 		goto exit1;
21811da177e4SLinus Torvalds 	}
21820612d9fbSOGAWA Hirofumi 
21830612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
21840612d9fbSOGAWA Hirofumi 
21854ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
218649705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
21871da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
21886902d925SDave Hansen 	if (IS_ERR(dentry))
21896902d925SDave Hansen 		goto exit2;
21900622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
21910622753bSDave Hansen 	if (error)
21920622753bSDave Hansen 		goto exit3;
2193be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2194be6d3e56SKentaro Takeda 	if (error)
2195be6d3e56SKentaro Takeda 		goto exit4;
21964ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2197be6d3e56SKentaro Takeda exit4:
21980622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
21990622753bSDave Hansen exit3:
22001da177e4SLinus Torvalds 	dput(dentry);
22016902d925SDave Hansen exit2:
22024ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
22031da177e4SLinus Torvalds exit1:
22041d957f9bSJan Blunck 	path_put(&nd.path);
22051da177e4SLinus Torvalds 	putname(name);
22061da177e4SLinus Torvalds 	return error;
22071da177e4SLinus Torvalds }
22081da177e4SLinus Torvalds 
22093cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
22105590ff0dSUlrich Drepper {
22115590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
22125590ff0dSUlrich Drepper }
22135590ff0dSUlrich Drepper 
22141da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
22151da177e4SLinus Torvalds {
22161da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
22171da177e4SLinus Torvalds 
22181da177e4SLinus Torvalds 	if (error)
22191da177e4SLinus Torvalds 		return error;
22201da177e4SLinus Torvalds 
2221acfa4380SAl Viro 	if (!dir->i_op->unlink)
22221da177e4SLinus Torvalds 		return -EPERM;
22231da177e4SLinus Torvalds 
22249e3509e2SJan Kara 	vfs_dq_init(dir);
22251da177e4SLinus Torvalds 
22261b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
22271da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
22281da177e4SLinus Torvalds 		error = -EBUSY;
22291da177e4SLinus Torvalds 	else {
22301da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
22311da177e4SLinus Torvalds 		if (!error)
22321da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
22331da177e4SLinus Torvalds 	}
22341b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
22351da177e4SLinus Torvalds 
22361da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
22371da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2238ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
22391da177e4SLinus Torvalds 		d_delete(dentry);
22401da177e4SLinus Torvalds 	}
22410eeca283SRobert Love 
22421da177e4SLinus Torvalds 	return error;
22431da177e4SLinus Torvalds }
22441da177e4SLinus Torvalds 
22451da177e4SLinus Torvalds /*
22461da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
22471b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
22481da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
22491da177e4SLinus Torvalds  * while waiting on the I/O.
22501da177e4SLinus Torvalds  */
22515590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
22521da177e4SLinus Torvalds {
22532ad94ae6SAl Viro 	int error;
22541da177e4SLinus Torvalds 	char *name;
22551da177e4SLinus Torvalds 	struct dentry *dentry;
22561da177e4SLinus Torvalds 	struct nameidata nd;
22571da177e4SLinus Torvalds 	struct inode *inode = NULL;
22581da177e4SLinus Torvalds 
22592ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
22601da177e4SLinus Torvalds 	if (error)
22612ad94ae6SAl Viro 		return error;
22622ad94ae6SAl Viro 
22631da177e4SLinus Torvalds 	error = -EISDIR;
22641da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
22651da177e4SLinus Torvalds 		goto exit1;
22660612d9fbSOGAWA Hirofumi 
22670612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
22680612d9fbSOGAWA Hirofumi 
22694ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
227049705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
22711da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
22721da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
22731da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
22741da177e4SLinus Torvalds 		if (nd.last.name[nd.last.len])
22751da177e4SLinus Torvalds 			goto slashes;
22761da177e4SLinus Torvalds 		inode = dentry->d_inode;
22771da177e4SLinus Torvalds 		if (inode)
22781da177e4SLinus Torvalds 			atomic_inc(&inode->i_count);
22790622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
22800622753bSDave Hansen 		if (error)
22810622753bSDave Hansen 			goto exit2;
2282be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2283be6d3e56SKentaro Takeda 		if (error)
2284be6d3e56SKentaro Takeda 			goto exit3;
22854ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2286be6d3e56SKentaro Takeda exit3:
22870622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
22881da177e4SLinus Torvalds 	exit2:
22891da177e4SLinus Torvalds 		dput(dentry);
22901da177e4SLinus Torvalds 	}
22914ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
22921da177e4SLinus Torvalds 	if (inode)
22931da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
22941da177e4SLinus Torvalds exit1:
22951d957f9bSJan Blunck 	path_put(&nd.path);
22961da177e4SLinus Torvalds 	putname(name);
22971da177e4SLinus Torvalds 	return error;
22981da177e4SLinus Torvalds 
22991da177e4SLinus Torvalds slashes:
23001da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
23011da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
23021da177e4SLinus Torvalds 	goto exit2;
23031da177e4SLinus Torvalds }
23041da177e4SLinus Torvalds 
23052e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
23065590ff0dSUlrich Drepper {
23075590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
23085590ff0dSUlrich Drepper 		return -EINVAL;
23095590ff0dSUlrich Drepper 
23105590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
23115590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
23125590ff0dSUlrich Drepper 
23135590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
23145590ff0dSUlrich Drepper }
23155590ff0dSUlrich Drepper 
23163480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
23175590ff0dSUlrich Drepper {
23185590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
23195590ff0dSUlrich Drepper }
23205590ff0dSUlrich Drepper 
2321db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
23221da177e4SLinus Torvalds {
2323a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
23241da177e4SLinus Torvalds 
23251da177e4SLinus Torvalds 	if (error)
23261da177e4SLinus Torvalds 		return error;
23271da177e4SLinus Torvalds 
2328acfa4380SAl Viro 	if (!dir->i_op->symlink)
23291da177e4SLinus Torvalds 		return -EPERM;
23301da177e4SLinus Torvalds 
23311da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
23321da177e4SLinus Torvalds 	if (error)
23331da177e4SLinus Torvalds 		return error;
23341da177e4SLinus Torvalds 
23359e3509e2SJan Kara 	vfs_dq_init(dir);
23361da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2337a74574aaSStephen Smalley 	if (!error)
2338f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
23391da177e4SLinus Torvalds 	return error;
23401da177e4SLinus Torvalds }
23411da177e4SLinus Torvalds 
23422e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
23432e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
23441da177e4SLinus Torvalds {
23452ad94ae6SAl Viro 	int error;
23461da177e4SLinus Torvalds 	char *from;
23471da177e4SLinus Torvalds 	char *to;
23486902d925SDave Hansen 	struct dentry *dentry;
23496902d925SDave Hansen 	struct nameidata nd;
23501da177e4SLinus Torvalds 
23511da177e4SLinus Torvalds 	from = getname(oldname);
23521da177e4SLinus Torvalds 	if (IS_ERR(from))
23531da177e4SLinus Torvalds 		return PTR_ERR(from);
23542ad94ae6SAl Viro 
23552ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
23562ad94ae6SAl Viro 	if (error)
23576902d925SDave Hansen 		goto out_putname;
23581da177e4SLinus Torvalds 
23591da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
23601da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
23616902d925SDave Hansen 	if (IS_ERR(dentry))
23626902d925SDave Hansen 		goto out_unlock;
23636902d925SDave Hansen 
236475c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
236575c3f29dSDave Hansen 	if (error)
236675c3f29dSDave Hansen 		goto out_dput;
2367be6d3e56SKentaro Takeda 	error = security_path_symlink(&nd.path, dentry, from);
2368be6d3e56SKentaro Takeda 	if (error)
2369be6d3e56SKentaro Takeda 		goto out_drop_write;
2370db2e747bSMiklos Szeredi 	error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2371be6d3e56SKentaro Takeda out_drop_write:
237275c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
237375c3f29dSDave Hansen out_dput:
23741da177e4SLinus Torvalds 	dput(dentry);
23756902d925SDave Hansen out_unlock:
23764ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
23771d957f9bSJan Blunck 	path_put(&nd.path);
23781da177e4SLinus Torvalds 	putname(to);
23796902d925SDave Hansen out_putname:
23801da177e4SLinus Torvalds 	putname(from);
23811da177e4SLinus Torvalds 	return error;
23821da177e4SLinus Torvalds }
23831da177e4SLinus Torvalds 
23843480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
23855590ff0dSUlrich Drepper {
23865590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
23875590ff0dSUlrich Drepper }
23885590ff0dSUlrich Drepper 
23891da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
23901da177e4SLinus Torvalds {
23911da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
23921da177e4SLinus Torvalds 	int error;
23931da177e4SLinus Torvalds 
23941da177e4SLinus Torvalds 	if (!inode)
23951da177e4SLinus Torvalds 		return -ENOENT;
23961da177e4SLinus Torvalds 
2397a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
23981da177e4SLinus Torvalds 	if (error)
23991da177e4SLinus Torvalds 		return error;
24001da177e4SLinus Torvalds 
24011da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
24021da177e4SLinus Torvalds 		return -EXDEV;
24031da177e4SLinus Torvalds 
24041da177e4SLinus Torvalds 	/*
24051da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
24061da177e4SLinus Torvalds 	 */
24071da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
24081da177e4SLinus Torvalds 		return -EPERM;
2409acfa4380SAl Viro 	if (!dir->i_op->link)
24101da177e4SLinus Torvalds 		return -EPERM;
24117e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
24121da177e4SLinus Torvalds 		return -EPERM;
24131da177e4SLinus Torvalds 
24141da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
24151da177e4SLinus Torvalds 	if (error)
24161da177e4SLinus Torvalds 		return error;
24171da177e4SLinus Torvalds 
24187e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
24199e3509e2SJan Kara 	vfs_dq_init(dir);
24201da177e4SLinus Torvalds 	error = dir->i_op->link(old_dentry, dir, new_dentry);
24217e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
2422e31e14ecSStephen Smalley 	if (!error)
24237e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
24241da177e4SLinus Torvalds 	return error;
24251da177e4SLinus Torvalds }
24261da177e4SLinus Torvalds 
24271da177e4SLinus Torvalds /*
24281da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
24291da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
24301da177e4SLinus Torvalds  * newname.  --KAB
24311da177e4SLinus Torvalds  *
24321da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
24331da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
24341da177e4SLinus Torvalds  * and other special files.  --ADM
24351da177e4SLinus Torvalds  */
24362e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
24372e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
24381da177e4SLinus Torvalds {
24391da177e4SLinus Torvalds 	struct dentry *new_dentry;
24402d8f3038SAl Viro 	struct nameidata nd;
24412d8f3038SAl Viro 	struct path old_path;
24421da177e4SLinus Torvalds 	int error;
24431da177e4SLinus Torvalds 	char *to;
24441da177e4SLinus Torvalds 
244545c9b11aSUlrich Drepper 	if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
2446c04030e1SUlrich Drepper 		return -EINVAL;
2447c04030e1SUlrich Drepper 
24482d8f3038SAl Viro 	error = user_path_at(olddfd, oldname,
244945c9b11aSUlrich Drepper 			     flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
24502d8f3038SAl Viro 			     &old_path);
24511da177e4SLinus Torvalds 	if (error)
24522ad94ae6SAl Viro 		return error;
24532ad94ae6SAl Viro 
24542ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
24551da177e4SLinus Torvalds 	if (error)
24561da177e4SLinus Torvalds 		goto out;
24571da177e4SLinus Torvalds 	error = -EXDEV;
24582d8f3038SAl Viro 	if (old_path.mnt != nd.path.mnt)
24591da177e4SLinus Torvalds 		goto out_release;
24601da177e4SLinus Torvalds 	new_dentry = lookup_create(&nd, 0);
24611da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
24626902d925SDave Hansen 	if (IS_ERR(new_dentry))
24636902d925SDave Hansen 		goto out_unlock;
246475c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
246575c3f29dSDave Hansen 	if (error)
246675c3f29dSDave Hansen 		goto out_dput;
2467be6d3e56SKentaro Takeda 	error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2468be6d3e56SKentaro Takeda 	if (error)
2469be6d3e56SKentaro Takeda 		goto out_drop_write;
24702d8f3038SAl Viro 	error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
2471be6d3e56SKentaro Takeda out_drop_write:
247275c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
247375c3f29dSDave Hansen out_dput:
24741da177e4SLinus Torvalds 	dput(new_dentry);
24756902d925SDave Hansen out_unlock:
24764ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
24771da177e4SLinus Torvalds out_release:
24781d957f9bSJan Blunck 	path_put(&nd.path);
24792ad94ae6SAl Viro 	putname(to);
24801da177e4SLinus Torvalds out:
24812d8f3038SAl Viro 	path_put(&old_path);
24821da177e4SLinus Torvalds 
24831da177e4SLinus Torvalds 	return error;
24841da177e4SLinus Torvalds }
24851da177e4SLinus Torvalds 
24863480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
24875590ff0dSUlrich Drepper {
2488c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
24895590ff0dSUlrich Drepper }
24905590ff0dSUlrich Drepper 
24911da177e4SLinus Torvalds /*
24921da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
24931da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
24941da177e4SLinus Torvalds  * Problems:
24951da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
24961da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
24971da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
2498a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
24991da177e4SLinus Torvalds  *	   story.
25001da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
25011b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
25021da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
25031da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
2504a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
25051da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
25061da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
25071da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
2508a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
25091da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
25101da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
25111da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
25121da177e4SLinus Torvalds  *	d) some filesystems don't support opened-but-unlinked directories,
25131da177e4SLinus Torvalds  *	   either because of layout or because they are not ready to deal with
25141da177e4SLinus Torvalds  *	   all cases correctly. The latter will be fixed (taking this sort of
25151da177e4SLinus Torvalds  *	   stuff into VFS), but the former is not going away. Solution: the same
25161da177e4SLinus Torvalds  *	   trick as in rmdir().
25171da177e4SLinus Torvalds  *	e) conversion from fhandle to dentry may come in the wrong moment - when
25181b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
25191da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
25201b1dcc1bSJes Sorensen  *	   ->i_mutex on parents, which works but leads to some truely excessive
25211da177e4SLinus Torvalds  *	   locking].
25221da177e4SLinus Torvalds  */
252375c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
25241da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
25251da177e4SLinus Torvalds {
25261da177e4SLinus Torvalds 	int error = 0;
25271da177e4SLinus Torvalds 	struct inode *target;
25281da177e4SLinus Torvalds 
25291da177e4SLinus Torvalds 	/*
25301da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
25311da177e4SLinus Torvalds 	 * we'll need to flip '..'.
25321da177e4SLinus Torvalds 	 */
25331da177e4SLinus Torvalds 	if (new_dir != old_dir) {
2534f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
25351da177e4SLinus Torvalds 		if (error)
25361da177e4SLinus Torvalds 			return error;
25371da177e4SLinus Torvalds 	}
25381da177e4SLinus Torvalds 
25391da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
25401da177e4SLinus Torvalds 	if (error)
25411da177e4SLinus Torvalds 		return error;
25421da177e4SLinus Torvalds 
25431da177e4SLinus Torvalds 	target = new_dentry->d_inode;
25441da177e4SLinus Torvalds 	if (target) {
25451b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
25461da177e4SLinus Torvalds 		dentry_unhash(new_dentry);
25471da177e4SLinus Torvalds 	}
25481da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
25491da177e4SLinus Torvalds 		error = -EBUSY;
25501da177e4SLinus Torvalds 	else
25511da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
25521da177e4SLinus Torvalds 	if (target) {
25531da177e4SLinus Torvalds 		if (!error)
25541da177e4SLinus Torvalds 			target->i_flags |= S_DEAD;
25551b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
25561da177e4SLinus Torvalds 		if (d_unhashed(new_dentry))
25571da177e4SLinus Torvalds 			d_rehash(new_dentry);
25581da177e4SLinus Torvalds 		dput(new_dentry);
25591da177e4SLinus Torvalds 	}
2560e31e14ecSStephen Smalley 	if (!error)
2561349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
25621da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
25631da177e4SLinus Torvalds 	return error;
25641da177e4SLinus Torvalds }
25651da177e4SLinus Torvalds 
256675c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
25671da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
25681da177e4SLinus Torvalds {
25691da177e4SLinus Torvalds 	struct inode *target;
25701da177e4SLinus Torvalds 	int error;
25711da177e4SLinus Torvalds 
25721da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
25731da177e4SLinus Torvalds 	if (error)
25741da177e4SLinus Torvalds 		return error;
25751da177e4SLinus Torvalds 
25761da177e4SLinus Torvalds 	dget(new_dentry);
25771da177e4SLinus Torvalds 	target = new_dentry->d_inode;
25781da177e4SLinus Torvalds 	if (target)
25791b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
25801da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
25811da177e4SLinus Torvalds 		error = -EBUSY;
25821da177e4SLinus Torvalds 	else
25831da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
25841da177e4SLinus Torvalds 	if (!error) {
2585349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
25861da177e4SLinus Torvalds 			d_move(old_dentry, new_dentry);
25871da177e4SLinus Torvalds 	}
25881da177e4SLinus Torvalds 	if (target)
25891b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
25901da177e4SLinus Torvalds 	dput(new_dentry);
25911da177e4SLinus Torvalds 	return error;
25921da177e4SLinus Torvalds }
25931da177e4SLinus Torvalds 
25941da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
25951da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
25961da177e4SLinus Torvalds {
25971da177e4SLinus Torvalds 	int error;
25981da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
25990eeca283SRobert Love 	const char *old_name;
26001da177e4SLinus Torvalds 
26011da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
26021da177e4SLinus Torvalds  		return 0;
26031da177e4SLinus Torvalds 
26041da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
26051da177e4SLinus Torvalds 	if (error)
26061da177e4SLinus Torvalds 		return error;
26071da177e4SLinus Torvalds 
26081da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
2609a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
26101da177e4SLinus Torvalds 	else
26111da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
26121da177e4SLinus Torvalds 	if (error)
26131da177e4SLinus Torvalds 		return error;
26141da177e4SLinus Torvalds 
2615acfa4380SAl Viro 	if (!old_dir->i_op->rename)
26161da177e4SLinus Torvalds 		return -EPERM;
26171da177e4SLinus Torvalds 
26189e3509e2SJan Kara 	vfs_dq_init(old_dir);
26199e3509e2SJan Kara 	vfs_dq_init(new_dir);
26201da177e4SLinus Torvalds 
26210eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
26220eeca283SRobert Love 
26231da177e4SLinus Torvalds 	if (is_dir)
26241da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
26251da177e4SLinus Torvalds 	else
26261da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
26271da177e4SLinus Torvalds 	if (!error) {
26280eeca283SRobert Love 		const char *new_name = old_dentry->d_name.name;
262989204c40SJohn McCutchan 		fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
26305a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
26311da177e4SLinus Torvalds 	}
26320eeca283SRobert Love 	fsnotify_oldname_free(old_name);
26330eeca283SRobert Love 
26341da177e4SLinus Torvalds 	return error;
26351da177e4SLinus Torvalds }
26361da177e4SLinus Torvalds 
26372e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
26382e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
26391da177e4SLinus Torvalds {
26401da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
26411da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
26421da177e4SLinus Torvalds 	struct dentry *trap;
26431da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
26442ad94ae6SAl Viro 	char *from;
26452ad94ae6SAl Viro 	char *to;
26462ad94ae6SAl Viro 	int error;
26471da177e4SLinus Torvalds 
26482ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
26491da177e4SLinus Torvalds 	if (error)
26501da177e4SLinus Torvalds 		goto exit;
26511da177e4SLinus Torvalds 
26522ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
26531da177e4SLinus Torvalds 	if (error)
26541da177e4SLinus Torvalds 		goto exit1;
26551da177e4SLinus Torvalds 
26561da177e4SLinus Torvalds 	error = -EXDEV;
26574ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
26581da177e4SLinus Torvalds 		goto exit2;
26591da177e4SLinus Torvalds 
26604ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
26611da177e4SLinus Torvalds 	error = -EBUSY;
26621da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
26631da177e4SLinus Torvalds 		goto exit2;
26641da177e4SLinus Torvalds 
26654ac91378SJan Blunck 	new_dir = newnd.path.dentry;
26661da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
26671da177e4SLinus Torvalds 		goto exit2;
26681da177e4SLinus Torvalds 
26690612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
26700612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
26714e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
26720612d9fbSOGAWA Hirofumi 
26731da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
26741da177e4SLinus Torvalds 
267549705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
26761da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
26771da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
26781da177e4SLinus Torvalds 		goto exit3;
26791da177e4SLinus Torvalds 	/* source must exist */
26801da177e4SLinus Torvalds 	error = -ENOENT;
26811da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
26821da177e4SLinus Torvalds 		goto exit4;
26831da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
26841da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
26851da177e4SLinus Torvalds 		error = -ENOTDIR;
26861da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
26871da177e4SLinus Torvalds 			goto exit4;
26881da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
26891da177e4SLinus Torvalds 			goto exit4;
26901da177e4SLinus Torvalds 	}
26911da177e4SLinus Torvalds 	/* source should not be ancestor of target */
26921da177e4SLinus Torvalds 	error = -EINVAL;
26931da177e4SLinus Torvalds 	if (old_dentry == trap)
26941da177e4SLinus Torvalds 		goto exit4;
269549705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
26961da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
26971da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
26981da177e4SLinus Torvalds 		goto exit4;
26991da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
27001da177e4SLinus Torvalds 	error = -ENOTEMPTY;
27011da177e4SLinus Torvalds 	if (new_dentry == trap)
27021da177e4SLinus Torvalds 		goto exit5;
27031da177e4SLinus Torvalds 
27049079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
27059079b1ebSDave Hansen 	if (error)
27069079b1ebSDave Hansen 		goto exit5;
2707be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
2708be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
2709be6d3e56SKentaro Takeda 	if (error)
2710be6d3e56SKentaro Takeda 		goto exit6;
27111da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
27121da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
2713be6d3e56SKentaro Takeda exit6:
27149079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
27151da177e4SLinus Torvalds exit5:
27161da177e4SLinus Torvalds 	dput(new_dentry);
27171da177e4SLinus Torvalds exit4:
27181da177e4SLinus Torvalds 	dput(old_dentry);
27191da177e4SLinus Torvalds exit3:
27201da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
27211da177e4SLinus Torvalds exit2:
27221d957f9bSJan Blunck 	path_put(&newnd.path);
27232ad94ae6SAl Viro 	putname(to);
27241da177e4SLinus Torvalds exit1:
27251d957f9bSJan Blunck 	path_put(&oldnd.path);
27261da177e4SLinus Torvalds 	putname(from);
27272ad94ae6SAl Viro exit:
27281da177e4SLinus Torvalds 	return error;
27291da177e4SLinus Torvalds }
27301da177e4SLinus Torvalds 
2731a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
27325590ff0dSUlrich Drepper {
27335590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
27345590ff0dSUlrich Drepper }
27355590ff0dSUlrich Drepper 
27361da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
27371da177e4SLinus Torvalds {
27381da177e4SLinus Torvalds 	int len;
27391da177e4SLinus Torvalds 
27401da177e4SLinus Torvalds 	len = PTR_ERR(link);
27411da177e4SLinus Torvalds 	if (IS_ERR(link))
27421da177e4SLinus Torvalds 		goto out;
27431da177e4SLinus Torvalds 
27441da177e4SLinus Torvalds 	len = strlen(link);
27451da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
27461da177e4SLinus Torvalds 		len = buflen;
27471da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
27481da177e4SLinus Torvalds 		len = -EFAULT;
27491da177e4SLinus Torvalds out:
27501da177e4SLinus Torvalds 	return len;
27511da177e4SLinus Torvalds }
27521da177e4SLinus Torvalds 
27531da177e4SLinus Torvalds /*
27541da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
27551da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
27561da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
27571da177e4SLinus Torvalds  */
27581da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
27591da177e4SLinus Torvalds {
27601da177e4SLinus Torvalds 	struct nameidata nd;
2761cc314eefSLinus Torvalds 	void *cookie;
2762694a1764SMarcin Slusarz 	int res;
2763cc314eefSLinus Torvalds 
27641da177e4SLinus Torvalds 	nd.depth = 0;
2765cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
2766694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
2767694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
2768694a1764SMarcin Slusarz 
2769694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
27701da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
2771cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2772694a1764SMarcin Slusarz 	return res;
27731da177e4SLinus Torvalds }
27741da177e4SLinus Torvalds 
27751da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
27761da177e4SLinus Torvalds {
27771da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
27781da177e4SLinus Torvalds }
27791da177e4SLinus Torvalds 
27801da177e4SLinus Torvalds /* get the link contents into pagecache */
27811da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
27821da177e4SLinus Torvalds {
2783ebd09abbSDuane Griffin 	char *kaddr;
27841da177e4SLinus Torvalds 	struct page *page;
27851da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
2786090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
27871da177e4SLinus Torvalds 	if (IS_ERR(page))
27886fe6900eSNick Piggin 		return (char*)page;
27891da177e4SLinus Torvalds 	*ppage = page;
2790ebd09abbSDuane Griffin 	kaddr = kmap(page);
2791ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
2792ebd09abbSDuane Griffin 	return kaddr;
27931da177e4SLinus Torvalds }
27941da177e4SLinus Torvalds 
27951da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
27961da177e4SLinus Torvalds {
27971da177e4SLinus Torvalds 	struct page *page = NULL;
27981da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
27991da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
28001da177e4SLinus Torvalds 	if (page) {
28011da177e4SLinus Torvalds 		kunmap(page);
28021da177e4SLinus Torvalds 		page_cache_release(page);
28031da177e4SLinus Torvalds 	}
28041da177e4SLinus Torvalds 	return res;
28051da177e4SLinus Torvalds }
28061da177e4SLinus Torvalds 
2807cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
28081da177e4SLinus Torvalds {
2809cc314eefSLinus Torvalds 	struct page *page = NULL;
28101da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
2811cc314eefSLinus Torvalds 	return page;
28121da177e4SLinus Torvalds }
28131da177e4SLinus Torvalds 
2814cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
28151da177e4SLinus Torvalds {
2816cc314eefSLinus Torvalds 	struct page *page = cookie;
2817cc314eefSLinus Torvalds 
2818cc314eefSLinus Torvalds 	if (page) {
28191da177e4SLinus Torvalds 		kunmap(page);
28201da177e4SLinus Torvalds 		page_cache_release(page);
28211da177e4SLinus Torvalds 	}
28221da177e4SLinus Torvalds }
28231da177e4SLinus Torvalds 
282454566b2cSNick Piggin /*
282554566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
282654566b2cSNick Piggin  */
282754566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
28281da177e4SLinus Torvalds {
28291da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
28300adb25d2SKirill Korotaev 	struct page *page;
2831afddba49SNick Piggin 	void *fsdata;
2832beb497abSDmitriy Monakhov 	int err;
28331da177e4SLinus Torvalds 	char *kaddr;
283454566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
283554566b2cSNick Piggin 	if (nofs)
283654566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
28371da177e4SLinus Torvalds 
28387e53cac4SNeilBrown retry:
2839afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
284054566b2cSNick Piggin 				flags, &page, &fsdata);
28411da177e4SLinus Torvalds 	if (err)
2842afddba49SNick Piggin 		goto fail;
2843afddba49SNick Piggin 
28441da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
28451da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
28461da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
2847afddba49SNick Piggin 
2848afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
2849afddba49SNick Piggin 							page, fsdata);
28501da177e4SLinus Torvalds 	if (err < 0)
28511da177e4SLinus Torvalds 		goto fail;
2852afddba49SNick Piggin 	if (err < len-1)
2853afddba49SNick Piggin 		goto retry;
2854afddba49SNick Piggin 
28551da177e4SLinus Torvalds 	mark_inode_dirty(inode);
28561da177e4SLinus Torvalds 	return 0;
28571da177e4SLinus Torvalds fail:
28581da177e4SLinus Torvalds 	return err;
28591da177e4SLinus Torvalds }
28601da177e4SLinus Torvalds 
28610adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
28620adb25d2SKirill Korotaev {
28630adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
286454566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
28650adb25d2SKirill Korotaev }
28660adb25d2SKirill Korotaev 
286792e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
28681da177e4SLinus Torvalds 	.readlink	= generic_readlink,
28691da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
28701da177e4SLinus Torvalds 	.put_link	= page_put_link,
28711da177e4SLinus Torvalds };
28721da177e4SLinus Torvalds 
28732d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
28741da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
28751da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
28761da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
28771da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
28781da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
28791da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
28801da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
28811da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
28821da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
28830adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
28841da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
28851da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
28861da177e4SLinus Torvalds EXPORT_SYMBOL(path_lookup);
2887d1811465SAl Viro EXPORT_SYMBOL(kern_path);
288816f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
2889f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
28908c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission);
28911da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
28921da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
28931da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
28941da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
28951da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
28961da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
28971da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
28981da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
28991da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
29001da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
29011da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
29021da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
29031da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
29041da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
2905