xref: /openbmc/linux/fs/namei.c (revision 7de9c6ee)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/namei.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  * Some corrections by tytso.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
121da177e4SLinus Torvalds  * lookup logic.
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/init.h>
181da177e4SLinus Torvalds #include <linux/module.h>
191da177e4SLinus Torvalds #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/fs.h>
211da177e4SLinus Torvalds #include <linux/namei.h>
221da177e4SLinus Torvalds #include <linux/pagemap.h>
230eeca283SRobert Love #include <linux/fsnotify.h>
241da177e4SLinus Torvalds #include <linux/personality.h>
251da177e4SLinus Torvalds #include <linux/security.h>
266146f0d5SMimi Zohar #include <linux/ima.h>
271da177e4SLinus Torvalds #include <linux/syscalls.h>
281da177e4SLinus Torvalds #include <linux/mount.h>
291da177e4SLinus Torvalds #include <linux/audit.h>
3016f7e0feSRandy Dunlap #include <linux/capability.h>
31834f2a4aSTrond Myklebust #include <linux/file.h>
325590ff0dSUlrich Drepper #include <linux/fcntl.h>
3308ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h>
345ad4e53bSAl Viro #include <linux/fs_struct.h>
351da177e4SLinus Torvalds #include <asm/uaccess.h>
361da177e4SLinus Torvalds 
37e81e3f4dSEric Paris #include "internal.h"
38e81e3f4dSEric Paris 
391da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer]
401da177e4SLinus Torvalds  * Fundamental changes in the pathname lookup mechanisms (namei)
411da177e4SLinus Torvalds  * were necessary because of omirr.  The reason is that omirr needs
421da177e4SLinus Torvalds  * to know the _real_ pathname, not the user-supplied one, in case
431da177e4SLinus Torvalds  * of symlinks (and also when transname replacements occur).
441da177e4SLinus Torvalds  *
451da177e4SLinus Torvalds  * The new code replaces the old recursive symlink resolution with
461da177e4SLinus Torvalds  * an iterative one (in case of non-nested symlink chains).  It does
471da177e4SLinus Torvalds  * this with calls to <fs>_follow_link().
481da177e4SLinus Torvalds  * As a side effect, dir_namei(), _namei() and follow_link() are now
491da177e4SLinus Torvalds  * replaced with a single function lookup_dentry() that can handle all
501da177e4SLinus Torvalds  * the special cases of the former code.
511da177e4SLinus Torvalds  *
521da177e4SLinus Torvalds  * With the new dcache, the pathname is stored at each inode, at least as
531da177e4SLinus Torvalds  * long as the refcount of the inode is positive.  As a side effect, the
541da177e4SLinus Torvalds  * size of the dcache depends on the inode cache and thus is dynamic.
551da177e4SLinus Torvalds  *
561da177e4SLinus Torvalds  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
571da177e4SLinus Torvalds  * resolution to correspond with current state of the code.
581da177e4SLinus Torvalds  *
591da177e4SLinus Torvalds  * Note that the symlink resolution is not *completely* iterative.
601da177e4SLinus Torvalds  * There is still a significant amount of tail- and mid- recursion in
611da177e4SLinus Torvalds  * the algorithm.  Also, note that <fs>_readlink() is not used in
621da177e4SLinus Torvalds  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
631da177e4SLinus Torvalds  * may return different results than <fs>_follow_link().  Many virtual
641da177e4SLinus Torvalds  * filesystems (including /proc) exhibit this behavior.
651da177e4SLinus Torvalds  */
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
681da177e4SLinus Torvalds  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
691da177e4SLinus Torvalds  * and the name already exists in form of a symlink, try to create the new
701da177e4SLinus Torvalds  * name indicated by the symlink. The old code always complained that the
711da177e4SLinus Torvalds  * name already exists, due to not following the symlink even if its target
721da177e4SLinus Torvalds  * is nonexistent.  The new semantics affects also mknod() and link() when
731da177e4SLinus Torvalds  * the name is a symlink pointing to a non-existant name.
741da177e4SLinus Torvalds  *
751da177e4SLinus Torvalds  * I don't know which semantics is the right one, since I have no access
761da177e4SLinus Torvalds  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
771da177e4SLinus Torvalds  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
781da177e4SLinus Torvalds  * "old" one. Personally, I think the new semantics is much more logical.
791da177e4SLinus Torvalds  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
801da177e4SLinus Torvalds  * file does succeed in both HP-UX and SunOs, but not in Solaris
811da177e4SLinus Torvalds  * and in the old Linux semantics.
821da177e4SLinus Torvalds  */
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
851da177e4SLinus Torvalds  * semantics.  See the comments in "open_namei" and "do_link" below.
861da177e4SLinus Torvalds  *
871da177e4SLinus Torvalds  * [10-Sep-98 Alan Modra] Another symlink change.
881da177e4SLinus Torvalds  */
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
911da177e4SLinus Torvalds  *	inside the path - always follow.
921da177e4SLinus Torvalds  *	in the last component in creation/removal/renaming - never follow.
931da177e4SLinus Torvalds  *	if LOOKUP_FOLLOW passed - follow.
941da177e4SLinus Torvalds  *	if the pathname has trailing slashes - follow.
951da177e4SLinus Torvalds  *	otherwise - don't follow.
961da177e4SLinus Torvalds  * (applied in that order).
971da177e4SLinus Torvalds  *
981da177e4SLinus Torvalds  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
991da177e4SLinus Torvalds  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
1001da177e4SLinus Torvalds  * During the 2.4 we need to fix the userland stuff depending on it -
1011da177e4SLinus Torvalds  * hopefully we will be able to get rid of that wart in 2.5. So far only
1021da177e4SLinus Torvalds  * XEmacs seems to be relying on it...
1031da177e4SLinus Torvalds  */
1041da177e4SLinus Torvalds /*
1051da177e4SLinus Torvalds  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
106a11f3a05SArjan van de Ven  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
1071da177e4SLinus Torvalds  * any extra contention...
1081da177e4SLinus Torvalds  */
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional
1111da177e4SLinus Torvalds  * checking and hopefully speeding things up, we copy filenames to the
1121da177e4SLinus Torvalds  * kernel data space before using them..
1131da177e4SLinus Torvalds  *
1141da177e4SLinus Torvalds  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
1151da177e4SLinus Torvalds  * PATH_MAX includes the nul terminator --RR.
1161da177e4SLinus Torvalds  */
117858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page)
1181da177e4SLinus Torvalds {
1191da177e4SLinus Torvalds 	int retval;
1201da177e4SLinus Torvalds 	unsigned long len = PATH_MAX;
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	if (!segment_eq(get_fs(), KERNEL_DS)) {
1231da177e4SLinus Torvalds 		if ((unsigned long) filename >= TASK_SIZE)
1241da177e4SLinus Torvalds 			return -EFAULT;
1251da177e4SLinus Torvalds 		if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
1261da177e4SLinus Torvalds 			len = TASK_SIZE - (unsigned long) filename;
1271da177e4SLinus Torvalds 	}
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	retval = strncpy_from_user(page, filename, len);
1301da177e4SLinus Torvalds 	if (retval > 0) {
1311da177e4SLinus Torvalds 		if (retval < len)
1321da177e4SLinus Torvalds 			return 0;
1331da177e4SLinus Torvalds 		return -ENAMETOOLONG;
1341da177e4SLinus Torvalds 	} else if (!retval)
1351da177e4SLinus Torvalds 		retval = -ENOENT;
1361da177e4SLinus Torvalds 	return retval;
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds char * getname(const char __user * filename)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	char *tmp, *result;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	result = ERR_PTR(-ENOMEM);
1441da177e4SLinus Torvalds 	tmp = __getname();
1451da177e4SLinus Torvalds 	if (tmp)  {
1461da177e4SLinus Torvalds 		int retval = do_getname(filename, tmp);
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 		result = tmp;
1491da177e4SLinus Torvalds 		if (retval < 0) {
1501da177e4SLinus Torvalds 			__putname(tmp);
1511da177e4SLinus Torvalds 			result = ERR_PTR(retval);
1521da177e4SLinus Torvalds 		}
1531da177e4SLinus Torvalds 	}
1541da177e4SLinus Torvalds 	audit_getname(result);
1551da177e4SLinus Torvalds 	return result;
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
1591da177e4SLinus Torvalds void putname(const char *name)
1601da177e4SLinus Torvalds {
1615ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
1621da177e4SLinus Torvalds 		audit_putname(name);
1631da177e4SLinus Torvalds 	else
1641da177e4SLinus Torvalds 		__putname(name);
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds EXPORT_SYMBOL(putname);
1671da177e4SLinus Torvalds #endif
1681da177e4SLinus Torvalds 
1695909ccaaSLinus Torvalds /*
1705909ccaaSLinus Torvalds  * This does basic POSIX ACL permission checking
1715909ccaaSLinus Torvalds  */
1725909ccaaSLinus Torvalds static int acl_permission_check(struct inode *inode, int mask,
1735909ccaaSLinus Torvalds 		int (*check_acl)(struct inode *inode, int mask))
1745909ccaaSLinus Torvalds {
1755909ccaaSLinus Torvalds 	umode_t			mode = inode->i_mode;
1765909ccaaSLinus Torvalds 
1775909ccaaSLinus Torvalds 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
1785909ccaaSLinus Torvalds 
1795909ccaaSLinus Torvalds 	if (current_fsuid() == inode->i_uid)
1805909ccaaSLinus Torvalds 		mode >>= 6;
1815909ccaaSLinus Torvalds 	else {
1825909ccaaSLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
1835909ccaaSLinus Torvalds 			int error = check_acl(inode, mask);
1845909ccaaSLinus Torvalds 			if (error != -EAGAIN)
1855909ccaaSLinus Torvalds 				return error;
1865909ccaaSLinus Torvalds 		}
1875909ccaaSLinus Torvalds 
1885909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
1895909ccaaSLinus Torvalds 			mode >>= 3;
1905909ccaaSLinus Torvalds 	}
1915909ccaaSLinus Torvalds 
1925909ccaaSLinus Torvalds 	/*
1935909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
1945909ccaaSLinus Torvalds 	 */
1955909ccaaSLinus Torvalds 	if ((mask & ~mode) == 0)
1965909ccaaSLinus Torvalds 		return 0;
1975909ccaaSLinus Torvalds 	return -EACCES;
1985909ccaaSLinus Torvalds }
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds /**
2011da177e4SLinus Torvalds  * generic_permission  -  check for access rights on a Posix-like filesystem
2021da177e4SLinus Torvalds  * @inode:	inode to check access rights for
2031da177e4SLinus Torvalds  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
2041da177e4SLinus Torvalds  * @check_acl:	optional callback to check for Posix ACLs
2051da177e4SLinus Torvalds  *
2061da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
2071da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
2081da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
2091da177e4SLinus Torvalds  * are used for other things..
2101da177e4SLinus Torvalds  */
2111da177e4SLinus Torvalds int generic_permission(struct inode *inode, int mask,
2121da177e4SLinus Torvalds 		int (*check_acl)(struct inode *inode, int mask))
2131da177e4SLinus Torvalds {
2145909ccaaSLinus Torvalds 	int ret;
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	/*
2175909ccaaSLinus Torvalds 	 * Do the basic POSIX ACL permission checks.
2181da177e4SLinus Torvalds 	 */
2195909ccaaSLinus Torvalds 	ret = acl_permission_check(inode, mask, check_acl);
2205909ccaaSLinus Torvalds 	if (ret != -EACCES)
2215909ccaaSLinus Torvalds 		return ret;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	/*
2241da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
2251da177e4SLinus Torvalds 	 * Executable DACs are overridable if at least one exec bit is set.
2261da177e4SLinus Torvalds 	 */
227f696a365SMiklos Szeredi 	if (!(mask & MAY_EXEC) || execute_ok(inode))
2281da177e4SLinus Torvalds 		if (capable(CAP_DAC_OVERRIDE))
2291da177e4SLinus Torvalds 			return 0;
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 	/*
2321da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
2331da177e4SLinus Torvalds 	 */
2347ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
2351da177e4SLinus Torvalds 	if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
2361da177e4SLinus Torvalds 		if (capable(CAP_DAC_READ_SEARCH))
2371da177e4SLinus Torvalds 			return 0;
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	return -EACCES;
2401da177e4SLinus Torvalds }
2411da177e4SLinus Torvalds 
242cb23beb5SChristoph Hellwig /**
243cb23beb5SChristoph Hellwig  * inode_permission  -  check for access rights to a given inode
244cb23beb5SChristoph Hellwig  * @inode:	inode to check permission on
245cb23beb5SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
246cb23beb5SChristoph Hellwig  *
247cb23beb5SChristoph Hellwig  * Used to check for read/write/execute permissions on an inode.
248cb23beb5SChristoph Hellwig  * We use "fsuid" for this, letting us set arbitrary permissions
249cb23beb5SChristoph Hellwig  * for filesystem access without changing the "normal" uids which
250cb23beb5SChristoph Hellwig  * are used for other things.
251cb23beb5SChristoph Hellwig  */
252f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask)
2531da177e4SLinus Torvalds {
254e6305c43SAl Viro 	int retval;
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	if (mask & MAY_WRITE) {
25722590e41SMiklos Szeredi 		umode_t mode = inode->i_mode;
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 		/*
2601da177e4SLinus Torvalds 		 * Nobody gets write access to a read-only fs.
2611da177e4SLinus Torvalds 		 */
2621da177e4SLinus Torvalds 		if (IS_RDONLY(inode) &&
2631da177e4SLinus Torvalds 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
2641da177e4SLinus Torvalds 			return -EROFS;
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 		/*
2671da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
2681da177e4SLinus Torvalds 		 */
2691da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
2701da177e4SLinus Torvalds 			return -EACCES;
2711da177e4SLinus Torvalds 	}
2721da177e4SLinus Torvalds 
273acfa4380SAl Viro 	if (inode->i_op->permission)
274b77b0646SAl Viro 		retval = inode->i_op->permission(inode, mask);
275f696a365SMiklos Szeredi 	else
2765909ccaaSLinus Torvalds 		retval = generic_permission(inode, mask, inode->i_op->check_acl);
277f696a365SMiklos Szeredi 
2781da177e4SLinus Torvalds 	if (retval)
2791da177e4SLinus Torvalds 		return retval;
2801da177e4SLinus Torvalds 
28108ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
28208ce5f16SSerge E. Hallyn 	if (retval)
28308ce5f16SSerge E. Hallyn 		return retval;
28408ce5f16SSerge E. Hallyn 
285d09ca739SEric Paris 	return security_inode_permission(inode, mask);
2861da177e4SLinus Torvalds }
2871da177e4SLinus Torvalds 
288e4543eddSChristoph Hellwig /**
2898c744fb8SChristoph Hellwig  * file_permission  -  check for additional access rights to a given file
2908c744fb8SChristoph Hellwig  * @file:	file to check access rights for
2918c744fb8SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
2928c744fb8SChristoph Hellwig  *
2938c744fb8SChristoph Hellwig  * Used to check for read/write/execute permissions on an already opened
2948c744fb8SChristoph Hellwig  * file.
2958c744fb8SChristoph Hellwig  *
2968c744fb8SChristoph Hellwig  * Note:
2978c744fb8SChristoph Hellwig  *	Do not use this function in new code.  All access checks should
298cb23beb5SChristoph Hellwig  *	be done using inode_permission().
2998c744fb8SChristoph Hellwig  */
3008c744fb8SChristoph Hellwig int file_permission(struct file *file, int mask)
3018c744fb8SChristoph Hellwig {
302f419a2e3SAl Viro 	return inode_permission(file->f_path.dentry->d_inode, mask);
3038c744fb8SChristoph Hellwig }
3048c744fb8SChristoph Hellwig 
3051da177e4SLinus Torvalds /*
3061da177e4SLinus Torvalds  * get_write_access() gets write permission for a file.
3071da177e4SLinus Torvalds  * put_write_access() releases this write permission.
3081da177e4SLinus Torvalds  * This is used for regular files.
3091da177e4SLinus Torvalds  * We cannot support write (and maybe mmap read-write shared) accesses and
3101da177e4SLinus Torvalds  * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
3111da177e4SLinus Torvalds  * can have the following values:
3121da177e4SLinus Torvalds  * 0: no writers, no VM_DENYWRITE mappings
3131da177e4SLinus Torvalds  * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
3141da177e4SLinus Torvalds  * > 0: (i_writecount) users are writing to the file.
3151da177e4SLinus Torvalds  *
3161da177e4SLinus Torvalds  * Normally we operate on that counter with atomic_{inc,dec} and it's safe
3171da177e4SLinus Torvalds  * except for the cases where we don't hold i_writecount yet. Then we need to
3181da177e4SLinus Torvalds  * use {get,deny}_write_access() - these functions check the sign and refuse
3191da177e4SLinus Torvalds  * to do the change if sign is wrong. Exclusion between them is provided by
3201da177e4SLinus Torvalds  * the inode->i_lock spinlock.
3211da177e4SLinus Torvalds  */
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds int get_write_access(struct inode * inode)
3241da177e4SLinus Torvalds {
3251da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
3261da177e4SLinus Torvalds 	if (atomic_read(&inode->i_writecount) < 0) {
3271da177e4SLinus Torvalds 		spin_unlock(&inode->i_lock);
3281da177e4SLinus Torvalds 		return -ETXTBSY;
3291da177e4SLinus Torvalds 	}
3301da177e4SLinus Torvalds 	atomic_inc(&inode->i_writecount);
3311da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	return 0;
3341da177e4SLinus Torvalds }
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds int deny_write_access(struct file * file)
3371da177e4SLinus Torvalds {
3380f7fc9e4SJosef "Jeff" Sipek 	struct inode *inode = file->f_path.dentry->d_inode;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
3411da177e4SLinus Torvalds 	if (atomic_read(&inode->i_writecount) > 0) {
3421da177e4SLinus Torvalds 		spin_unlock(&inode->i_lock);
3431da177e4SLinus Torvalds 		return -ETXTBSY;
3441da177e4SLinus Torvalds 	}
3451da177e4SLinus Torvalds 	atomic_dec(&inode->i_writecount);
3461da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	return 0;
3491da177e4SLinus Torvalds }
3501da177e4SLinus Torvalds 
3511d957f9bSJan Blunck /**
3525dd784d0SJan Blunck  * path_get - get a reference to a path
3535dd784d0SJan Blunck  * @path: path to get the reference to
3545dd784d0SJan Blunck  *
3555dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
3565dd784d0SJan Blunck  */
3575dd784d0SJan Blunck void path_get(struct path *path)
3585dd784d0SJan Blunck {
3595dd784d0SJan Blunck 	mntget(path->mnt);
3605dd784d0SJan Blunck 	dget(path->dentry);
3615dd784d0SJan Blunck }
3625dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
3635dd784d0SJan Blunck 
3645dd784d0SJan Blunck /**
3651d957f9bSJan Blunck  * path_put - put a reference to a path
3661d957f9bSJan Blunck  * @path: path to put the reference to
3671d957f9bSJan Blunck  *
3681d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
3691d957f9bSJan Blunck  */
3701d957f9bSJan Blunck void path_put(struct path *path)
3711da177e4SLinus Torvalds {
3721d957f9bSJan Blunck 	dput(path->dentry);
3731d957f9bSJan Blunck 	mntput(path->mnt);
3741da177e4SLinus Torvalds }
3751d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
3761da177e4SLinus Torvalds 
377834f2a4aSTrond Myklebust /**
378834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
379834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
380834f2a4aSTrond Myklebust  */
381834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
382834f2a4aSTrond Myklebust {
3830f7fc9e4SJosef "Jeff" Sipek 	if (nd->intent.open.file->f_path.dentry == NULL)
384834f2a4aSTrond Myklebust 		put_filp(nd->intent.open.file);
385834f2a4aSTrond Myklebust 	else
386834f2a4aSTrond Myklebust 		fput(nd->intent.open.file);
387834f2a4aSTrond Myklebust }
388834f2a4aSTrond Myklebust 
389bcdc5e01SIan Kent static inline struct dentry *
390bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd)
391bcdc5e01SIan Kent {
392bcdc5e01SIan Kent 	int status = dentry->d_op->d_revalidate(dentry, nd);
393bcdc5e01SIan Kent 	if (unlikely(status <= 0)) {
394bcdc5e01SIan Kent 		/*
395bcdc5e01SIan Kent 		 * The dentry failed validation.
396bcdc5e01SIan Kent 		 * If d_revalidate returned 0 attempt to invalidate
397bcdc5e01SIan Kent 		 * the dentry otherwise d_revalidate is asking us
398bcdc5e01SIan Kent 		 * to return a fail status.
399bcdc5e01SIan Kent 		 */
400bcdc5e01SIan Kent 		if (!status) {
401bcdc5e01SIan Kent 			if (!d_invalidate(dentry)) {
402bcdc5e01SIan Kent 				dput(dentry);
403bcdc5e01SIan Kent 				dentry = NULL;
404bcdc5e01SIan Kent 			}
405bcdc5e01SIan Kent 		} else {
406bcdc5e01SIan Kent 			dput(dentry);
407bcdc5e01SIan Kent 			dentry = ERR_PTR(status);
408bcdc5e01SIan Kent 		}
409bcdc5e01SIan Kent 	}
410bcdc5e01SIan Kent 	return dentry;
411bcdc5e01SIan Kent }
412bcdc5e01SIan Kent 
4131da177e4SLinus Torvalds /*
41439159de2SJeff Layton  * force_reval_path - force revalidation of a dentry
41539159de2SJeff Layton  *
41639159de2SJeff Layton  * In some situations the path walking code will trust dentries without
41739159de2SJeff Layton  * revalidating them. This causes problems for filesystems that depend on
41839159de2SJeff Layton  * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
41939159de2SJeff Layton  * (which indicates that it's possible for the dentry to go stale), force
42039159de2SJeff Layton  * a d_revalidate call before proceeding.
42139159de2SJeff Layton  *
42239159de2SJeff Layton  * Returns 0 if the revalidation was successful. If the revalidation fails,
42339159de2SJeff Layton  * either return the error returned by d_revalidate or -ESTALE if the
42439159de2SJeff Layton  * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
42539159de2SJeff Layton  * invalidate the dentry. It's up to the caller to handle putting references
42639159de2SJeff Layton  * to the path if necessary.
42739159de2SJeff Layton  */
42839159de2SJeff Layton static int
42939159de2SJeff Layton force_reval_path(struct path *path, struct nameidata *nd)
43039159de2SJeff Layton {
43139159de2SJeff Layton 	int status;
43239159de2SJeff Layton 	struct dentry *dentry = path->dentry;
43339159de2SJeff Layton 
43439159de2SJeff Layton 	/*
43539159de2SJeff Layton 	 * only check on filesystems where it's possible for the dentry to
43639159de2SJeff Layton 	 * become stale. It's assumed that if this flag is set then the
43739159de2SJeff Layton 	 * d_revalidate op will also be defined.
43839159de2SJeff Layton 	 */
43939159de2SJeff Layton 	if (!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))
44039159de2SJeff Layton 		return 0;
44139159de2SJeff Layton 
44239159de2SJeff Layton 	status = dentry->d_op->d_revalidate(dentry, nd);
44339159de2SJeff Layton 	if (status > 0)
44439159de2SJeff Layton 		return 0;
44539159de2SJeff Layton 
44639159de2SJeff Layton 	if (!status) {
44739159de2SJeff Layton 		d_invalidate(dentry);
44839159de2SJeff Layton 		status = -ESTALE;
44939159de2SJeff Layton 	}
45039159de2SJeff Layton 	return status;
45139159de2SJeff Layton }
45239159de2SJeff Layton 
45339159de2SJeff Layton /*
454b75b5086SAl Viro  * Short-cut version of permission(), for calling on directories
455b75b5086SAl Viro  * during pathname resolution.  Combines parts of permission()
456b75b5086SAl Viro  * and generic_permission(), and tests ONLY for MAY_EXEC permission.
4571da177e4SLinus Torvalds  *
4581da177e4SLinus Torvalds  * If appropriate, check DAC only.  If not appropriate, or
459b75b5086SAl Viro  * short-cut DAC fails, then call ->permission() to do more
4601da177e4SLinus Torvalds  * complete permission check.
4611da177e4SLinus Torvalds  */
462b75b5086SAl Viro static int exec_permission(struct inode *inode)
4631da177e4SLinus Torvalds {
4645909ccaaSLinus Torvalds 	int ret;
4651da177e4SLinus Torvalds 
466cb9179eaSLinus Torvalds 	if (inode->i_op->permission) {
4675909ccaaSLinus Torvalds 		ret = inode->i_op->permission(inode, MAY_EXEC);
468cb9179eaSLinus Torvalds 		if (!ret)
469cb9179eaSLinus Torvalds 			goto ok;
470cb9179eaSLinus Torvalds 		return ret;
471cb9179eaSLinus Torvalds 	}
4725909ccaaSLinus Torvalds 	ret = acl_permission_check(inode, MAY_EXEC, inode->i_op->check_acl);
4735909ccaaSLinus Torvalds 	if (!ret)
4741da177e4SLinus Torvalds 		goto ok;
4751da177e4SLinus Torvalds 
476f1ac9f6bSLinus Torvalds 	if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH))
4771da177e4SLinus Torvalds 		goto ok;
4781da177e4SLinus Torvalds 
4795909ccaaSLinus Torvalds 	return ret;
4801da177e4SLinus Torvalds ok:
481b77b0646SAl Viro 	return security_inode_permission(inode, MAY_EXEC);
4821da177e4SLinus Torvalds }
4831da177e4SLinus Torvalds 
4842a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
4852a737871SAl Viro {
486f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
487f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
4882a737871SAl Viro }
4892a737871SAl Viro 
4906de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
4916de88d72SAl Viro 
492f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
4931da177e4SLinus Torvalds {
4941da177e4SLinus Torvalds 	if (IS_ERR(link))
4951da177e4SLinus Torvalds 		goto fail;
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	if (*link == '/') {
4982a737871SAl Viro 		set_root(nd);
4991d957f9bSJan Blunck 		path_put(&nd->path);
5002a737871SAl Viro 		nd->path = nd->root;
5012a737871SAl Viro 		path_get(&nd->root);
5021da177e4SLinus Torvalds 	}
503b4091d5fSChristoph Hellwig 
504def4af30SAl Viro 	return link_path_walk(link, nd);
5051da177e4SLinus Torvalds fail:
5061d957f9bSJan Blunck 	path_put(&nd->path);
5071da177e4SLinus Torvalds 	return PTR_ERR(link);
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds 
5101d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
511051d3812SIan Kent {
512051d3812SIan Kent 	dput(path->dentry);
5134ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
514051d3812SIan Kent 		mntput(path->mnt);
515051d3812SIan Kent }
516051d3812SIan Kent 
517051d3812SIan Kent static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
518051d3812SIan Kent {
5194ac91378SJan Blunck 	dput(nd->path.dentry);
5209a229683SHuang Shijie 	if (nd->path.mnt != path->mnt) {
5214ac91378SJan Blunck 		mntput(nd->path.mnt);
5224ac91378SJan Blunck 		nd->path.mnt = path->mnt;
5239a229683SHuang Shijie 	}
5244ac91378SJan Blunck 	nd->path.dentry = path->dentry;
525051d3812SIan Kent }
526051d3812SIan Kent 
527def4af30SAl Viro static __always_inline int
528def4af30SAl Viro __do_follow_link(struct path *path, struct nameidata *nd, void **p)
5291da177e4SLinus Torvalds {
5301da177e4SLinus Torvalds 	int error;
531cd4e91d3SAl Viro 	struct dentry *dentry = path->dentry;
5321da177e4SLinus Torvalds 
533d671a1cbSAl Viro 	touch_atime(path->mnt, dentry);
5341da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
535cd4e91d3SAl Viro 
5364ac91378SJan Blunck 	if (path->mnt != nd->path.mnt) {
537051d3812SIan Kent 		path_to_nameidata(path, nd);
538051d3812SIan Kent 		dget(dentry);
539051d3812SIan Kent 	}
540cd4e91d3SAl Viro 	mntget(path->mnt);
54186acdca1SAl Viro 	nd->last_type = LAST_BIND;
542def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
543def4af30SAl Viro 	error = PTR_ERR(*p);
544def4af30SAl Viro 	if (!IS_ERR(*p)) {
5451da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
546cc314eefSLinus Torvalds 		error = 0;
5471da177e4SLinus Torvalds 		if (s)
5481da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
54939159de2SJeff Layton 		else if (nd->last_type == LAST_BIND) {
55039159de2SJeff Layton 			error = force_reval_path(&nd->path, nd);
55139159de2SJeff Layton 			if (error)
55239159de2SJeff Layton 				path_put(&nd->path);
55339159de2SJeff Layton 		}
5541da177e4SLinus Torvalds 	}
5551da177e4SLinus Torvalds 	return error;
5561da177e4SLinus Torvalds }
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds /*
5591da177e4SLinus Torvalds  * This limits recursive symlink follows to 8, while
5601da177e4SLinus Torvalds  * limiting consecutive symlinks to 40.
5611da177e4SLinus Torvalds  *
5621da177e4SLinus Torvalds  * Without that kind of total limit, nasty chains of consecutive
5631da177e4SLinus Torvalds  * symlinks can cause almost arbitrarily long lookups.
5641da177e4SLinus Torvalds  */
56590ebe565SAl Viro static inline int do_follow_link(struct path *path, struct nameidata *nd)
5661da177e4SLinus Torvalds {
567def4af30SAl Viro 	void *cookie;
5681da177e4SLinus Torvalds 	int err = -ELOOP;
5691da177e4SLinus Torvalds 	if (current->link_count >= MAX_NESTED_LINKS)
5701da177e4SLinus Torvalds 		goto loop;
5711da177e4SLinus Torvalds 	if (current->total_link_count >= 40)
5721da177e4SLinus Torvalds 		goto loop;
5731da177e4SLinus Torvalds 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
5741da177e4SLinus Torvalds 	cond_resched();
57590ebe565SAl Viro 	err = security_inode_follow_link(path->dentry, nd);
5761da177e4SLinus Torvalds 	if (err)
5771da177e4SLinus Torvalds 		goto loop;
5781da177e4SLinus Torvalds 	current->link_count++;
5791da177e4SLinus Torvalds 	current->total_link_count++;
5801da177e4SLinus Torvalds 	nd->depth++;
581def4af30SAl Viro 	err = __do_follow_link(path, nd, &cookie);
582def4af30SAl Viro 	if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link)
583def4af30SAl Viro 		path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie);
584258fa999SAl Viro 	path_put(path);
5851da177e4SLinus Torvalds 	current->link_count--;
5861da177e4SLinus Torvalds 	nd->depth--;
5871da177e4SLinus Torvalds 	return err;
5881da177e4SLinus Torvalds loop:
5891d957f9bSJan Blunck 	path_put_conditional(path, nd);
5901d957f9bSJan Blunck 	path_put(&nd->path);
5911da177e4SLinus Torvalds 	return err;
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds 
594bab77ebfSAl Viro int follow_up(struct path *path)
5951da177e4SLinus Torvalds {
5961da177e4SLinus Torvalds 	struct vfsmount *parent;
5971da177e4SLinus Torvalds 	struct dentry *mountpoint;
59899b7db7bSNick Piggin 
59999b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
600bab77ebfSAl Viro 	parent = path->mnt->mnt_parent;
601bab77ebfSAl Viro 	if (parent == path->mnt) {
60299b7db7bSNick Piggin 		br_read_unlock(vfsmount_lock);
6031da177e4SLinus Torvalds 		return 0;
6041da177e4SLinus Torvalds 	}
6051da177e4SLinus Torvalds 	mntget(parent);
606bab77ebfSAl Viro 	mountpoint = dget(path->mnt->mnt_mountpoint);
60799b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
608bab77ebfSAl Viro 	dput(path->dentry);
609bab77ebfSAl Viro 	path->dentry = mountpoint;
610bab77ebfSAl Viro 	mntput(path->mnt);
611bab77ebfSAl Viro 	path->mnt = parent;
6121da177e4SLinus Torvalds 	return 1;
6131da177e4SLinus Torvalds }
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds /* no need for dcache_lock, as serialization is taken care in
6161da177e4SLinus Torvalds  * namespace.c
6171da177e4SLinus Torvalds  */
618463ffb2eSAl Viro static int __follow_mount(struct path *path)
619463ffb2eSAl Viro {
620463ffb2eSAl Viro 	int res = 0;
621463ffb2eSAl Viro 	while (d_mountpoint(path->dentry)) {
6221c755af4SAl Viro 		struct vfsmount *mounted = lookup_mnt(path);
623463ffb2eSAl Viro 		if (!mounted)
624463ffb2eSAl Viro 			break;
625463ffb2eSAl Viro 		dput(path->dentry);
626463ffb2eSAl Viro 		if (res)
627463ffb2eSAl Viro 			mntput(path->mnt);
628463ffb2eSAl Viro 		path->mnt = mounted;
629463ffb2eSAl Viro 		path->dentry = dget(mounted->mnt_root);
630463ffb2eSAl Viro 		res = 1;
631463ffb2eSAl Viro 	}
632463ffb2eSAl Viro 	return res;
633463ffb2eSAl Viro }
634463ffb2eSAl Viro 
63579ed0226SAl Viro static void follow_mount(struct path *path)
6361da177e4SLinus Torvalds {
63779ed0226SAl Viro 	while (d_mountpoint(path->dentry)) {
6381c755af4SAl Viro 		struct vfsmount *mounted = lookup_mnt(path);
6391da177e4SLinus Torvalds 		if (!mounted)
6401da177e4SLinus Torvalds 			break;
64179ed0226SAl Viro 		dput(path->dentry);
64279ed0226SAl Viro 		mntput(path->mnt);
64379ed0226SAl Viro 		path->mnt = mounted;
64479ed0226SAl Viro 		path->dentry = dget(mounted->mnt_root);
6451da177e4SLinus Torvalds 	}
6461da177e4SLinus Torvalds }
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds /* no need for dcache_lock, as serialization is taken care in
6491da177e4SLinus Torvalds  * namespace.c
6501da177e4SLinus Torvalds  */
6519393bd07SAl Viro int follow_down(struct path *path)
6521da177e4SLinus Torvalds {
6531da177e4SLinus Torvalds 	struct vfsmount *mounted;
6541da177e4SLinus Torvalds 
6551c755af4SAl Viro 	mounted = lookup_mnt(path);
6561da177e4SLinus Torvalds 	if (mounted) {
6579393bd07SAl Viro 		dput(path->dentry);
6589393bd07SAl Viro 		mntput(path->mnt);
6599393bd07SAl Viro 		path->mnt = mounted;
6609393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
6611da177e4SLinus Torvalds 		return 1;
6621da177e4SLinus Torvalds 	}
6631da177e4SLinus Torvalds 	return 0;
6641da177e4SLinus Torvalds }
6651da177e4SLinus Torvalds 
666f1662356SArjan van de Ven static __always_inline void follow_dotdot(struct nameidata *nd)
6671da177e4SLinus Torvalds {
6682a737871SAl Viro 	set_root(nd);
669e518ddb7SAndreas Mohr 
6701da177e4SLinus Torvalds 	while(1) {
6714ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
6721da177e4SLinus Torvalds 
6732a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
6742a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
6751da177e4SLinus Torvalds 			break;
6761da177e4SLinus Torvalds 		}
6774ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
6783088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
6793088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
6801da177e4SLinus Torvalds 			dput(old);
6811da177e4SLinus Torvalds 			break;
6821da177e4SLinus Torvalds 		}
6833088dd70SAl Viro 		if (!follow_up(&nd->path))
6841da177e4SLinus Torvalds 			break;
6851da177e4SLinus Torvalds 	}
68679ed0226SAl Viro 	follow_mount(&nd->path);
6871da177e4SLinus Torvalds }
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds /*
690baa03890SNick Piggin  * Allocate a dentry with name and parent, and perform a parent
691baa03890SNick Piggin  * directory ->lookup on it. Returns the new dentry, or ERR_PTR
692baa03890SNick Piggin  * on error. parent->d_inode->i_mutex must be held. d_lookup must
693baa03890SNick Piggin  * have verified that no child exists while under i_mutex.
694baa03890SNick Piggin  */
695baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent,
696baa03890SNick Piggin 				struct qstr *name, struct nameidata *nd)
697baa03890SNick Piggin {
698baa03890SNick Piggin 	struct inode *inode = parent->d_inode;
699baa03890SNick Piggin 	struct dentry *dentry;
700baa03890SNick Piggin 	struct dentry *old;
701baa03890SNick Piggin 
702baa03890SNick Piggin 	/* Don't create child dentry for a dead directory. */
703baa03890SNick Piggin 	if (unlikely(IS_DEADDIR(inode)))
704baa03890SNick Piggin 		return ERR_PTR(-ENOENT);
705baa03890SNick Piggin 
706baa03890SNick Piggin 	dentry = d_alloc(parent, name);
707baa03890SNick Piggin 	if (unlikely(!dentry))
708baa03890SNick Piggin 		return ERR_PTR(-ENOMEM);
709baa03890SNick Piggin 
710baa03890SNick Piggin 	old = inode->i_op->lookup(inode, dentry, nd);
711baa03890SNick Piggin 	if (unlikely(old)) {
712baa03890SNick Piggin 		dput(dentry);
713baa03890SNick Piggin 		dentry = old;
714baa03890SNick Piggin 	}
715baa03890SNick Piggin 	return dentry;
716baa03890SNick Piggin }
717baa03890SNick Piggin 
718baa03890SNick Piggin /*
7191da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
7201da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
7211da177e4SLinus Torvalds  *  It _is_ time-critical.
7221da177e4SLinus Torvalds  */
7231da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
7241da177e4SLinus Torvalds 		     struct path *path)
7251da177e4SLinus Torvalds {
7264ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
7276e6b1bd1SAl Viro 	struct dentry *dentry, *parent;
7286e6b1bd1SAl Viro 	struct inode *dir;
7293cac260aSAl Viro 	/*
7303cac260aSAl Viro 	 * See if the low-level filesystem might want
7313cac260aSAl Viro 	 * to use its own hash..
7323cac260aSAl Viro 	 */
7333cac260aSAl Viro 	if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
7343cac260aSAl Viro 		int err = nd->path.dentry->d_op->d_hash(nd->path.dentry, name);
7353cac260aSAl Viro 		if (err < 0)
7363cac260aSAl Viro 			return err;
7373cac260aSAl Viro 	}
7381da177e4SLinus Torvalds 
739b04f784eSNick Piggin 	/*
740b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
741b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
742b04f784eSNick Piggin 	 * do the non-racy lookup, below.
743b04f784eSNick Piggin 	 */
7443cac260aSAl Viro 	dentry = __d_lookup(nd->path.dentry, name);
7451da177e4SLinus Torvalds 	if (!dentry)
7461da177e4SLinus Torvalds 		goto need_lookup;
7472e2e88eaSNick Piggin found:
7481da177e4SLinus Torvalds 	if (dentry->d_op && dentry->d_op->d_revalidate)
7491da177e4SLinus Torvalds 		goto need_revalidate;
7501da177e4SLinus Torvalds done:
7511da177e4SLinus Torvalds 	path->mnt = mnt;
7521da177e4SLinus Torvalds 	path->dentry = dentry;
753634ee701SAl Viro 	__follow_mount(path);
7541da177e4SLinus Torvalds 	return 0;
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds need_lookup:
7576e6b1bd1SAl Viro 	parent = nd->path.dentry;
7586e6b1bd1SAl Viro 	dir = parent->d_inode;
7596e6b1bd1SAl Viro 
7606e6b1bd1SAl Viro 	mutex_lock(&dir->i_mutex);
7616e6b1bd1SAl Viro 	/*
7626e6b1bd1SAl Viro 	 * First re-do the cached lookup just in case it was created
763b04f784eSNick Piggin 	 * while we waited for the directory semaphore, or the first
764b04f784eSNick Piggin 	 * lookup failed due to an unrelated rename.
7656e6b1bd1SAl Viro 	 *
766b04f784eSNick Piggin 	 * This could use version numbering or similar to avoid unnecessary
767b04f784eSNick Piggin 	 * cache lookups, but then we'd have to do the first lookup in the
768b04f784eSNick Piggin 	 * non-racy way. However in the common case here, everything should
769b04f784eSNick Piggin 	 * be hot in cache, so would it be a big win?
7706e6b1bd1SAl Viro 	 */
7716e6b1bd1SAl Viro 	dentry = d_lookup(parent, name);
772baa03890SNick Piggin 	if (likely(!dentry)) {
773baa03890SNick Piggin 		dentry = d_alloc_and_lookup(parent, name, nd);
7746e6b1bd1SAl Viro 		mutex_unlock(&dir->i_mutex);
7756e6b1bd1SAl Viro 		if (IS_ERR(dentry))
7766e6b1bd1SAl Viro 			goto fail;
7776e6b1bd1SAl Viro 		goto done;
7786e6b1bd1SAl Viro 	}
7796e6b1bd1SAl Viro 	/*
7806e6b1bd1SAl Viro 	 * Uhhuh! Nasty case: the cache was re-populated while
7816e6b1bd1SAl Viro 	 * we waited on the semaphore. Need to revalidate.
7826e6b1bd1SAl Viro 	 */
7836e6b1bd1SAl Viro 	mutex_unlock(&dir->i_mutex);
7842e2e88eaSNick Piggin 	goto found;
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds need_revalidate:
787bcdc5e01SIan Kent 	dentry = do_revalidate(dentry, nd);
788bcdc5e01SIan Kent 	if (!dentry)
7891da177e4SLinus Torvalds 		goto need_lookup;
790bcdc5e01SIan Kent 	if (IS_ERR(dentry))
791bcdc5e01SIan Kent 		goto fail;
792bcdc5e01SIan Kent 	goto done;
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds fail:
7951da177e4SLinus Torvalds 	return PTR_ERR(dentry);
7961da177e4SLinus Torvalds }
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds /*
799ac278a9cSAl Viro  * This is a temporary kludge to deal with "automount" symlinks; proper
800ac278a9cSAl Viro  * solution is to trigger them on follow_mount(), so that do_lookup()
801ac278a9cSAl Viro  * would DTRT.  To be killed before 2.6.34-final.
802ac278a9cSAl Viro  */
803ac278a9cSAl Viro static inline int follow_on_final(struct inode *inode, unsigned lookup_flags)
804ac278a9cSAl Viro {
805ac278a9cSAl Viro 	return inode && unlikely(inode->i_op->follow_link) &&
806ac278a9cSAl Viro 		((lookup_flags & LOOKUP_FOLLOW) || S_ISDIR(inode->i_mode));
807ac278a9cSAl Viro }
808ac278a9cSAl Viro 
809ac278a9cSAl Viro /*
8101da177e4SLinus Torvalds  * Name resolution.
811ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
812ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
8131da177e4SLinus Torvalds  *
814ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
815ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
8161da177e4SLinus Torvalds  */
8176de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
8181da177e4SLinus Torvalds {
8191da177e4SLinus Torvalds 	struct path next;
8201da177e4SLinus Torvalds 	struct inode *inode;
8211da177e4SLinus Torvalds 	int err;
8221da177e4SLinus Torvalds 	unsigned int lookup_flags = nd->flags;
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 	while (*name=='/')
8251da177e4SLinus Torvalds 		name++;
8261da177e4SLinus Torvalds 	if (!*name)
8271da177e4SLinus Torvalds 		goto return_reval;
8281da177e4SLinus Torvalds 
8294ac91378SJan Blunck 	inode = nd->path.dentry->d_inode;
8301da177e4SLinus Torvalds 	if (nd->depth)
831f55eab82STrond Myklebust 		lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
8341da177e4SLinus Torvalds 	for(;;) {
8351da177e4SLinus Torvalds 		unsigned long hash;
8361da177e4SLinus Torvalds 		struct qstr this;
8371da177e4SLinus Torvalds 		unsigned int c;
8381da177e4SLinus Torvalds 
839cdce5d6bSTrond Myklebust 		nd->flags |= LOOKUP_CONTINUE;
840b75b5086SAl Viro 		err = exec_permission(inode);
8411da177e4SLinus Torvalds  		if (err)
8421da177e4SLinus Torvalds 			break;
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 		this.name = name;
8451da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 		hash = init_name_hash();
8481da177e4SLinus Torvalds 		do {
8491da177e4SLinus Torvalds 			name++;
8501da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
8511da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
8521da177e4SLinus Torvalds 		} while (c && (c != '/'));
8531da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
8541da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 		/* remove trailing slashes? */
8571da177e4SLinus Torvalds 		if (!c)
8581da177e4SLinus Torvalds 			goto last_component;
8591da177e4SLinus Torvalds 		while (*++name == '/');
8601da177e4SLinus Torvalds 		if (!*name)
8611da177e4SLinus Torvalds 			goto last_with_slashes;
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 		/*
8641da177e4SLinus Torvalds 		 * "." and ".." are special - ".." especially so because it has
8651da177e4SLinus Torvalds 		 * to be able to know about the current root directory and
8661da177e4SLinus Torvalds 		 * parent relationships.
8671da177e4SLinus Torvalds 		 */
8681da177e4SLinus Torvalds 		if (this.name[0] == '.') switch (this.len) {
8691da177e4SLinus Torvalds 			default:
8701da177e4SLinus Torvalds 				break;
8711da177e4SLinus Torvalds 			case 2:
8721da177e4SLinus Torvalds 				if (this.name[1] != '.')
8731da177e4SLinus Torvalds 					break;
87458c465ebSAl Viro 				follow_dotdot(nd);
8754ac91378SJan Blunck 				inode = nd->path.dentry->d_inode;
8761da177e4SLinus Torvalds 				/* fallthrough */
8771da177e4SLinus Torvalds 			case 1:
8781da177e4SLinus Torvalds 				continue;
8791da177e4SLinus Torvalds 		}
8801da177e4SLinus Torvalds 		/* This does the actual lookups.. */
8811da177e4SLinus Torvalds 		err = do_lookup(nd, &this, &next);
8821da177e4SLinus Torvalds 		if (err)
8831da177e4SLinus Torvalds 			break;
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds 		err = -ENOENT;
8861da177e4SLinus Torvalds 		inode = next.dentry->d_inode;
8871da177e4SLinus Torvalds 		if (!inode)
8881da177e4SLinus Torvalds 			goto out_dput;
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 		if (inode->i_op->follow_link) {
89190ebe565SAl Viro 			err = do_follow_link(&next, nd);
8921da177e4SLinus Torvalds 			if (err)
8931da177e4SLinus Torvalds 				goto return_err;
8941da177e4SLinus Torvalds 			err = -ENOENT;
8954ac91378SJan Blunck 			inode = nd->path.dentry->d_inode;
8961da177e4SLinus Torvalds 			if (!inode)
8971da177e4SLinus Torvalds 				break;
89809dd17d3SMiklos Szeredi 		} else
89909dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
9001da177e4SLinus Torvalds 		err = -ENOTDIR;
9011da177e4SLinus Torvalds 		if (!inode->i_op->lookup)
9021da177e4SLinus Torvalds 			break;
9031da177e4SLinus Torvalds 		continue;
9041da177e4SLinus Torvalds 		/* here ends the main loop */
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds last_with_slashes:
9071da177e4SLinus Torvalds 		lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
9081da177e4SLinus Torvalds last_component:
909f55eab82STrond Myklebust 		/* Clear LOOKUP_CONTINUE iff it was previously unset */
910f55eab82STrond Myklebust 		nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
9111da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_PARENT)
9121da177e4SLinus Torvalds 			goto lookup_parent;
9131da177e4SLinus Torvalds 		if (this.name[0] == '.') switch (this.len) {
9141da177e4SLinus Torvalds 			default:
9151da177e4SLinus Torvalds 				break;
9161da177e4SLinus Torvalds 			case 2:
9171da177e4SLinus Torvalds 				if (this.name[1] != '.')
9181da177e4SLinus Torvalds 					break;
91958c465ebSAl Viro 				follow_dotdot(nd);
9204ac91378SJan Blunck 				inode = nd->path.dentry->d_inode;
9211da177e4SLinus Torvalds 				/* fallthrough */
9221da177e4SLinus Torvalds 			case 1:
9231da177e4SLinus Torvalds 				goto return_reval;
9241da177e4SLinus Torvalds 		}
9251da177e4SLinus Torvalds 		err = do_lookup(nd, &this, &next);
9261da177e4SLinus Torvalds 		if (err)
9271da177e4SLinus Torvalds 			break;
9281da177e4SLinus Torvalds 		inode = next.dentry->d_inode;
929ac278a9cSAl Viro 		if (follow_on_final(inode, lookup_flags)) {
93090ebe565SAl Viro 			err = do_follow_link(&next, nd);
9311da177e4SLinus Torvalds 			if (err)
9321da177e4SLinus Torvalds 				goto return_err;
9334ac91378SJan Blunck 			inode = nd->path.dentry->d_inode;
93409dd17d3SMiklos Szeredi 		} else
93509dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
9361da177e4SLinus Torvalds 		err = -ENOENT;
9371da177e4SLinus Torvalds 		if (!inode)
9381da177e4SLinus Torvalds 			break;
9391da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_DIRECTORY) {
9401da177e4SLinus Torvalds 			err = -ENOTDIR;
941acfa4380SAl Viro 			if (!inode->i_op->lookup)
9421da177e4SLinus Torvalds 				break;
9431da177e4SLinus Torvalds 		}
9441da177e4SLinus Torvalds 		goto return_base;
9451da177e4SLinus Torvalds lookup_parent:
9461da177e4SLinus Torvalds 		nd->last = this;
9471da177e4SLinus Torvalds 		nd->last_type = LAST_NORM;
9481da177e4SLinus Torvalds 		if (this.name[0] != '.')
9491da177e4SLinus Torvalds 			goto return_base;
9501da177e4SLinus Torvalds 		if (this.len == 1)
9511da177e4SLinus Torvalds 			nd->last_type = LAST_DOT;
9521da177e4SLinus Torvalds 		else if (this.len == 2 && this.name[1] == '.')
9531da177e4SLinus Torvalds 			nd->last_type = LAST_DOTDOT;
9541da177e4SLinus Torvalds 		else
9551da177e4SLinus Torvalds 			goto return_base;
9561da177e4SLinus Torvalds return_reval:
9571da177e4SLinus Torvalds 		/*
9581da177e4SLinus Torvalds 		 * We bypassed the ordinary revalidation routines.
9591da177e4SLinus Torvalds 		 * We may need to check the cached dentry for staleness.
9601da177e4SLinus Torvalds 		 */
9614ac91378SJan Blunck 		if (nd->path.dentry && nd->path.dentry->d_sb &&
9624ac91378SJan Blunck 		    (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
9631da177e4SLinus Torvalds 			err = -ESTALE;
9641da177e4SLinus Torvalds 			/* Note: we do not d_invalidate() */
9654ac91378SJan Blunck 			if (!nd->path.dentry->d_op->d_revalidate(
9664ac91378SJan Blunck 					nd->path.dentry, nd))
9671da177e4SLinus Torvalds 				break;
9681da177e4SLinus Torvalds 		}
9691da177e4SLinus Torvalds return_base:
9701da177e4SLinus Torvalds 		return 0;
9711da177e4SLinus Torvalds out_dput:
9721d957f9bSJan Blunck 		path_put_conditional(&next, nd);
9731da177e4SLinus Torvalds 		break;
9741da177e4SLinus Torvalds 	}
9751d957f9bSJan Blunck 	path_put(&nd->path);
9761da177e4SLinus Torvalds return_err:
9771da177e4SLinus Torvalds 	return err;
9781da177e4SLinus Torvalds }
9791da177e4SLinus Torvalds 
980fc9b52cdSHarvey Harrison static int path_walk(const char *name, struct nameidata *nd)
9811da177e4SLinus Torvalds {
9826de88d72SAl Viro 	struct path save = nd->path;
9836de88d72SAl Viro 	int result;
9846de88d72SAl Viro 
9851da177e4SLinus Torvalds 	current->total_link_count = 0;
9866de88d72SAl Viro 
9876de88d72SAl Viro 	/* make sure the stuff we saved doesn't go away */
9886de88d72SAl Viro 	path_get(&save);
9896de88d72SAl Viro 
9906de88d72SAl Viro 	result = link_path_walk(name, nd);
9916de88d72SAl Viro 	if (result == -ESTALE) {
9926de88d72SAl Viro 		/* nd->path had been dropped */
9936de88d72SAl Viro 		current->total_link_count = 0;
9946de88d72SAl Viro 		nd->path = save;
9956de88d72SAl Viro 		path_get(&nd->path);
9966de88d72SAl Viro 		nd->flags |= LOOKUP_REVAL;
9976de88d72SAl Viro 		result = link_path_walk(name, nd);
9986de88d72SAl Viro 	}
9996de88d72SAl Viro 
10006de88d72SAl Viro 	path_put(&save);
10016de88d72SAl Viro 
10026de88d72SAl Viro 	return result;
10031da177e4SLinus Torvalds }
10041da177e4SLinus Torvalds 
10059b4a9b14SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
10061da177e4SLinus Torvalds {
1007ea3834d9SPrasanna Meda 	int retval = 0;
1008170aa3d0SUlrich Drepper 	int fput_needed;
1009170aa3d0SUlrich Drepper 	struct file *file;
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
10121da177e4SLinus Torvalds 	nd->flags = flags;
10131da177e4SLinus Torvalds 	nd->depth = 0;
10142a737871SAl Viro 	nd->root.mnt = NULL;
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds 	if (*name=='/') {
10172a737871SAl Viro 		set_root(nd);
10182a737871SAl Viro 		nd->path = nd->root;
10192a737871SAl Viro 		path_get(&nd->root);
10205590ff0dSUlrich Drepper 	} else if (dfd == AT_FDCWD) {
1021f7ad3c6bSMiklos Szeredi 		get_fs_pwd(current->fs, &nd->path);
10225590ff0dSUlrich Drepper 	} else {
10235590ff0dSUlrich Drepper 		struct dentry *dentry;
10245590ff0dSUlrich Drepper 
10255590ff0dSUlrich Drepper 		file = fget_light(dfd, &fput_needed);
10265590ff0dSUlrich Drepper 		retval = -EBADF;
1027170aa3d0SUlrich Drepper 		if (!file)
10286d09bb62STrond Myklebust 			goto out_fail;
10295590ff0dSUlrich Drepper 
10300f7fc9e4SJosef "Jeff" Sipek 		dentry = file->f_path.dentry;
10315590ff0dSUlrich Drepper 
10325590ff0dSUlrich Drepper 		retval = -ENOTDIR;
1033170aa3d0SUlrich Drepper 		if (!S_ISDIR(dentry->d_inode->i_mode))
10346d09bb62STrond Myklebust 			goto fput_fail;
10355590ff0dSUlrich Drepper 
10365590ff0dSUlrich Drepper 		retval = file_permission(file, MAY_EXEC);
1037170aa3d0SUlrich Drepper 		if (retval)
10386d09bb62STrond Myklebust 			goto fput_fail;
10395590ff0dSUlrich Drepper 
10405dd784d0SJan Blunck 		nd->path = file->f_path;
10415dd784d0SJan Blunck 		path_get(&file->f_path);
10425590ff0dSUlrich Drepper 
10435590ff0dSUlrich Drepper 		fput_light(file, fput_needed);
10441da177e4SLinus Torvalds 	}
10459b4a9b14SAl Viro 	return 0;
10462dfdd266SJosef 'Jeff' Sipek 
10479b4a9b14SAl Viro fput_fail:
10489b4a9b14SAl Viro 	fput_light(file, fput_needed);
10499b4a9b14SAl Viro out_fail:
10509b4a9b14SAl Viro 	return retval;
10519b4a9b14SAl Viro }
10529b4a9b14SAl Viro 
10539b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
10549b4a9b14SAl Viro static int do_path_lookup(int dfd, const char *name,
10559b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
10569b4a9b14SAl Viro {
10579b4a9b14SAl Viro 	int retval = path_init(dfd, name, flags, nd);
10589b4a9b14SAl Viro 	if (!retval)
10592dfdd266SJosef 'Jeff' Sipek 		retval = path_walk(name, nd);
10604ac91378SJan Blunck 	if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
10614ac91378SJan Blunck 				nd->path.dentry->d_inode))
10624ac91378SJan Blunck 		audit_inode(name, nd->path.dentry);
10632a737871SAl Viro 	if (nd->root.mnt) {
10642a737871SAl Viro 		path_put(&nd->root);
10652a737871SAl Viro 		nd->root.mnt = NULL;
10662a737871SAl Viro 	}
1067170aa3d0SUlrich Drepper 	return retval;
10681da177e4SLinus Torvalds }
10691da177e4SLinus Torvalds 
1070fc9b52cdSHarvey Harrison int path_lookup(const char *name, unsigned int flags,
10715590ff0dSUlrich Drepper 			struct nameidata *nd)
10725590ff0dSUlrich Drepper {
10735590ff0dSUlrich Drepper 	return do_path_lookup(AT_FDCWD, name, flags, nd);
10745590ff0dSUlrich Drepper }
10755590ff0dSUlrich Drepper 
1076d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1077d1811465SAl Viro {
1078d1811465SAl Viro 	struct nameidata nd;
1079d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1080d1811465SAl Viro 	if (!res)
1081d1811465SAl Viro 		*path = nd.path;
1082d1811465SAl Viro 	return res;
1083d1811465SAl Viro }
1084d1811465SAl Viro 
108516f18200SJosef 'Jeff' Sipek /**
108616f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
108716f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
108816f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
108916f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
109016f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
109116f18200SJosef 'Jeff' Sipek  * @nd: pointer to nameidata
109216f18200SJosef 'Jeff' Sipek  */
109316f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
109416f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
109516f18200SJosef 'Jeff' Sipek 		    struct nameidata *nd)
109616f18200SJosef 'Jeff' Sipek {
109716f18200SJosef 'Jeff' Sipek 	int retval;
109816f18200SJosef 'Jeff' Sipek 
109916f18200SJosef 'Jeff' Sipek 	/* same as do_path_lookup */
110016f18200SJosef 'Jeff' Sipek 	nd->last_type = LAST_ROOT;
110116f18200SJosef 'Jeff' Sipek 	nd->flags = flags;
110216f18200SJosef 'Jeff' Sipek 	nd->depth = 0;
110316f18200SJosef 'Jeff' Sipek 
1104c8e7f449SJan Blunck 	nd->path.dentry = dentry;
1105c8e7f449SJan Blunck 	nd->path.mnt = mnt;
1106c8e7f449SJan Blunck 	path_get(&nd->path);
11075b857119SAl Viro 	nd->root = nd->path;
11085b857119SAl Viro 	path_get(&nd->root);
110916f18200SJosef 'Jeff' Sipek 
111016f18200SJosef 'Jeff' Sipek 	retval = path_walk(name, nd);
11114ac91378SJan Blunck 	if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
11124ac91378SJan Blunck 				nd->path.dentry->d_inode))
11134ac91378SJan Blunck 		audit_inode(name, nd->path.dentry);
111416f18200SJosef 'Jeff' Sipek 
11152a737871SAl Viro 	path_put(&nd->root);
11162a737871SAl Viro 	nd->root.mnt = NULL;
111716f18200SJosef 'Jeff' Sipek 
11182a737871SAl Viro 	return retval;
111916f18200SJosef 'Jeff' Sipek }
112016f18200SJosef 'Jeff' Sipek 
1121eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1122eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
11231da177e4SLinus Torvalds {
112481fca444SChristoph Hellwig 	struct inode *inode = base->d_inode;
11251da177e4SLinus Torvalds 	struct dentry *dentry;
11261da177e4SLinus Torvalds 	int err;
11271da177e4SLinus Torvalds 
112881fca444SChristoph Hellwig 	err = exec_permission(inode);
112981fca444SChristoph Hellwig 	if (err)
113081fca444SChristoph Hellwig 		return ERR_PTR(err);
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds 	/*
11331da177e4SLinus Torvalds 	 * See if the low-level filesystem might want
11341da177e4SLinus Torvalds 	 * to use its own hash..
11351da177e4SLinus Torvalds 	 */
11361da177e4SLinus Torvalds 	if (base->d_op && base->d_op->d_hash) {
11371da177e4SLinus Torvalds 		err = base->d_op->d_hash(base, name);
11381da177e4SLinus Torvalds 		dentry = ERR_PTR(err);
11391da177e4SLinus Torvalds 		if (err < 0)
11401da177e4SLinus Torvalds 			goto out;
11411da177e4SLinus Torvalds 	}
11421da177e4SLinus Torvalds 
1143b04f784eSNick Piggin 	/*
1144b04f784eSNick Piggin 	 * Don't bother with __d_lookup: callers are for creat as
1145b04f784eSNick Piggin 	 * well as unlink, so a lot of the time it would cost
1146b04f784eSNick Piggin 	 * a double lookup.
11476e6b1bd1SAl Viro 	 */
11486e6b1bd1SAl Viro 	dentry = d_lookup(base, name);
11496e6b1bd1SAl Viro 
11506e6b1bd1SAl Viro 	if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
11516e6b1bd1SAl Viro 		dentry = do_revalidate(dentry, nd);
11526e6b1bd1SAl Viro 
11531da177e4SLinus Torvalds 	if (!dentry)
1154baa03890SNick Piggin 		dentry = d_alloc_and_lookup(base, name, nd);
11551da177e4SLinus Torvalds out:
11561da177e4SLinus Torvalds 	return dentry;
11571da177e4SLinus Torvalds }
11581da177e4SLinus Torvalds 
1159057f6c01SJames Morris /*
1160057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1161057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1162057f6c01SJames Morris  * SMP-safe.
1163057f6c01SJames Morris  */
1164a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
11651da177e4SLinus Torvalds {
11664ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
11671da177e4SLinus Torvalds }
11681da177e4SLinus Torvalds 
1169eead1911SChristoph Hellwig static int __lookup_one_len(const char *name, struct qstr *this,
1170eead1911SChristoph Hellwig 		struct dentry *base, int len)
11711da177e4SLinus Torvalds {
11721da177e4SLinus Torvalds 	unsigned long hash;
11731da177e4SLinus Torvalds 	unsigned int c;
11741da177e4SLinus Torvalds 
1175057f6c01SJames Morris 	this->name = name;
1176057f6c01SJames Morris 	this->len = len;
11771da177e4SLinus Torvalds 	if (!len)
1178057f6c01SJames Morris 		return -EACCES;
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds 	hash = init_name_hash();
11811da177e4SLinus Torvalds 	while (len--) {
11821da177e4SLinus Torvalds 		c = *(const unsigned char *)name++;
11831da177e4SLinus Torvalds 		if (c == '/' || c == '\0')
1184057f6c01SJames Morris 			return -EACCES;
11851da177e4SLinus Torvalds 		hash = partial_name_hash(c, hash);
11861da177e4SLinus Torvalds 	}
1187057f6c01SJames Morris 	this->hash = end_name_hash(hash);
1188057f6c01SJames Morris 	return 0;
1189057f6c01SJames Morris }
11901da177e4SLinus Torvalds 
1191eead1911SChristoph Hellwig /**
1192a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1193eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1194eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1195eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1196eead1911SChristoph Hellwig  *
1197a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1198a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1199eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1200eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1201eead1911SChristoph Hellwig  */
1202057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1203057f6c01SJames Morris {
1204057f6c01SJames Morris 	int err;
1205057f6c01SJames Morris 	struct qstr this;
1206057f6c01SJames Morris 
12072f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
12082f9092e1SDavid Woodhouse 
1209057f6c01SJames Morris 	err = __lookup_one_len(name, &this, base, len);
1210057f6c01SJames Morris 	if (err)
1211057f6c01SJames Morris 		return ERR_PTR(err);
1212eead1911SChristoph Hellwig 
121349705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1214057f6c01SJames Morris }
1215057f6c01SJames Morris 
12162d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags,
12172d8f3038SAl Viro 		 struct path *path)
12181da177e4SLinus Torvalds {
12192d8f3038SAl Viro 	struct nameidata nd;
12201da177e4SLinus Torvalds 	char *tmp = getname(name);
12211da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
12221da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
12232d8f3038SAl Viro 
12242d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
12252d8f3038SAl Viro 
12262d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
12271da177e4SLinus Torvalds 		putname(tmp);
12282d8f3038SAl Viro 		if (!err)
12292d8f3038SAl Viro 			*path = nd.path;
12301da177e4SLinus Torvalds 	}
12311da177e4SLinus Torvalds 	return err;
12321da177e4SLinus Torvalds }
12331da177e4SLinus Torvalds 
12342ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
12352ad94ae6SAl Viro 			struct nameidata *nd, char **name)
12362ad94ae6SAl Viro {
12372ad94ae6SAl Viro 	char *s = getname(path);
12382ad94ae6SAl Viro 	int error;
12392ad94ae6SAl Viro 
12402ad94ae6SAl Viro 	if (IS_ERR(s))
12412ad94ae6SAl Viro 		return PTR_ERR(s);
12422ad94ae6SAl Viro 
12432ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
12442ad94ae6SAl Viro 	if (error)
12452ad94ae6SAl Viro 		putname(s);
12462ad94ae6SAl Viro 	else
12472ad94ae6SAl Viro 		*name = s;
12482ad94ae6SAl Viro 
12492ad94ae6SAl Viro 	return error;
12502ad94ae6SAl Viro }
12512ad94ae6SAl Viro 
12521da177e4SLinus Torvalds /*
12531da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
12541da177e4SLinus Torvalds  * minimal.
12551da177e4SLinus Torvalds  */
12561da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
12571da177e4SLinus Torvalds {
1258da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1259da9592edSDavid Howells 
12601da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
12611da177e4SLinus Torvalds 		return 0;
1262da9592edSDavid Howells 	if (inode->i_uid == fsuid)
12631da177e4SLinus Torvalds 		return 0;
1264da9592edSDavid Howells 	if (dir->i_uid == fsuid)
12651da177e4SLinus Torvalds 		return 0;
12661da177e4SLinus Torvalds 	return !capable(CAP_FOWNER);
12671da177e4SLinus Torvalds }
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds /*
12701da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
12711da177e4SLinus Torvalds  *  whether the type of victim is right.
12721da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
12731da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
12741da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
12751da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
12761da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
12771da177e4SLinus Torvalds  *	a. be owner of dir, or
12781da177e4SLinus Torvalds  *	b. be owner of victim, or
12791da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
12801da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
12811da177e4SLinus Torvalds  *     links pointing to it.
12821da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
12831da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
12841da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
12851da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
12861da177e4SLinus Torvalds  *     nfs_async_unlink().
12871da177e4SLinus Torvalds  */
1288858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
12891da177e4SLinus Torvalds {
12901da177e4SLinus Torvalds 	int error;
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 	if (!victim->d_inode)
12931da177e4SLinus Torvalds 		return -ENOENT;
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
1296cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
12971da177e4SLinus Torvalds 
1298f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
12991da177e4SLinus Torvalds 	if (error)
13001da177e4SLinus Torvalds 		return error;
13011da177e4SLinus Torvalds 	if (IS_APPEND(dir))
13021da177e4SLinus Torvalds 		return -EPERM;
13031da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1304f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
13051da177e4SLinus Torvalds 		return -EPERM;
13061da177e4SLinus Torvalds 	if (isdir) {
13071da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
13081da177e4SLinus Torvalds 			return -ENOTDIR;
13091da177e4SLinus Torvalds 		if (IS_ROOT(victim))
13101da177e4SLinus Torvalds 			return -EBUSY;
13111da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
13121da177e4SLinus Torvalds 		return -EISDIR;
13131da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
13141da177e4SLinus Torvalds 		return -ENOENT;
13151da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
13161da177e4SLinus Torvalds 		return -EBUSY;
13171da177e4SLinus Torvalds 	return 0;
13181da177e4SLinus Torvalds }
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
13211da177e4SLinus Torvalds  *  dir.
13221da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
13231da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
13241da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
13251da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
13261da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
13271da177e4SLinus Torvalds  */
1328a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
13291da177e4SLinus Torvalds {
13301da177e4SLinus Torvalds 	if (child->d_inode)
13311da177e4SLinus Torvalds 		return -EEXIST;
13321da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
13331da177e4SLinus Torvalds 		return -ENOENT;
1334f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
13351da177e4SLinus Torvalds }
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds /*
13381da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
13391da177e4SLinus Torvalds  */
13401da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
13411da177e4SLinus Torvalds {
13421da177e4SLinus Torvalds 	struct dentry *p;
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds 	if (p1 == p2) {
1345f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
13461da177e4SLinus Torvalds 		return NULL;
13471da177e4SLinus Torvalds 	}
13481da177e4SLinus Torvalds 
1349a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
13501da177e4SLinus Torvalds 
1351e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1352e2761a11SOGAWA Hirofumi 	if (p) {
1353f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1354f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
13551da177e4SLinus Torvalds 		return p;
13561da177e4SLinus Torvalds 	}
13571da177e4SLinus Torvalds 
1358e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1359e2761a11SOGAWA Hirofumi 	if (p) {
1360f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1361f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
13621da177e4SLinus Torvalds 		return p;
13631da177e4SLinus Torvalds 	}
13641da177e4SLinus Torvalds 
1365f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1366f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
13671da177e4SLinus Torvalds 	return NULL;
13681da177e4SLinus Torvalds }
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
13711da177e4SLinus Torvalds {
13721b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
13731da177e4SLinus Torvalds 	if (p1 != p2) {
13741b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1375a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
13761da177e4SLinus Torvalds 	}
13771da177e4SLinus Torvalds }
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
13801da177e4SLinus Torvalds 		struct nameidata *nd)
13811da177e4SLinus Torvalds {
1382a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds 	if (error)
13851da177e4SLinus Torvalds 		return error;
13861da177e4SLinus Torvalds 
1387acfa4380SAl Viro 	if (!dir->i_op->create)
13881da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
13891da177e4SLinus Torvalds 	mode &= S_IALLUGO;
13901da177e4SLinus Torvalds 	mode |= S_IFREG;
13911da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
13921da177e4SLinus Torvalds 	if (error)
13931da177e4SLinus Torvalds 		return error;
13941da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
1395a74574aaSStephen Smalley 	if (!error)
1396f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
13971da177e4SLinus Torvalds 	return error;
13981da177e4SLinus Torvalds }
13991da177e4SLinus Torvalds 
14003fb64190SChristoph Hellwig int may_open(struct path *path, int acc_mode, int flag)
14011da177e4SLinus Torvalds {
14023fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
14031da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14041da177e4SLinus Torvalds 	int error;
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds 	if (!inode)
14071da177e4SLinus Torvalds 		return -ENOENT;
14081da177e4SLinus Torvalds 
1409c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
1410c8fe8f30SChristoph Hellwig 	case S_IFLNK:
14111da177e4SLinus Torvalds 		return -ELOOP;
1412c8fe8f30SChristoph Hellwig 	case S_IFDIR:
1413c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
14141da177e4SLinus Torvalds 			return -EISDIR;
1415c8fe8f30SChristoph Hellwig 		break;
1416c8fe8f30SChristoph Hellwig 	case S_IFBLK:
1417c8fe8f30SChristoph Hellwig 	case S_IFCHR:
14183fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
14191da177e4SLinus Torvalds 			return -EACCES;
1420c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
1421c8fe8f30SChristoph Hellwig 	case S_IFIFO:
1422c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
14231da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
1424c8fe8f30SChristoph Hellwig 		break;
14254a3fd211SDave Hansen 	}
1426b41572e9SDave Hansen 
14273fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
1428b41572e9SDave Hansen 	if (error)
1429b41572e9SDave Hansen 		return error;
14306146f0d5SMimi Zohar 
14311da177e4SLinus Torvalds 	/*
14321da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
14331da177e4SLinus Torvalds 	 */
14341da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
14358737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
14367715b521SAl Viro 			return -EPERM;
14371da177e4SLinus Torvalds 		if (flag & O_TRUNC)
14387715b521SAl Viro 			return -EPERM;
14391da177e4SLinus Torvalds 	}
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
14427715b521SAl Viro 	if (flag & O_NOATIME && !is_owner_or_cap(inode))
14437715b521SAl Viro 		return -EPERM;
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds 	/*
14461da177e4SLinus Torvalds 	 * Ensure there are no outstanding leases on the file.
14471da177e4SLinus Torvalds 	 */
1448b65a9cfcSAl Viro 	return break_lease(inode, flag);
14497715b521SAl Viro }
14507715b521SAl Viro 
14517715b521SAl Viro static int handle_truncate(struct path *path)
14527715b521SAl Viro {
14537715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
14547715b521SAl Viro 	int error = get_write_access(inode);
14551da177e4SLinus Torvalds 	if (error)
14567715b521SAl Viro 		return error;
14571da177e4SLinus Torvalds 	/*
14581da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
14591da177e4SLinus Torvalds 	 */
14601da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
1461be6d3e56SKentaro Takeda 	if (!error)
1462ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
14631da177e4SLinus Torvalds 	if (!error) {
14647715b521SAl Viro 		error = do_truncate(path->dentry, 0,
1465d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
1466d139d7ffSMiklos Szeredi 				    NULL);
14671da177e4SLinus Torvalds 	}
14681da177e4SLinus Torvalds 	put_write_access(inode);
1469acd0c935SMimi Zohar 	return error;
14701da177e4SLinus Torvalds }
14711da177e4SLinus Torvalds 
1472d57999e1SDave Hansen /*
1473d57999e1SDave Hansen  * Be careful about ever adding any more callers of this
1474d57999e1SDave Hansen  * function.  Its flags must be in the namei format, not
1475d57999e1SDave Hansen  * what get passed to sys_open().
1476d57999e1SDave Hansen  */
1477d57999e1SDave Hansen static int __open_namei_create(struct nameidata *nd, struct path *path,
14788737c930SAl Viro 				int open_flag, int mode)
1479aab520e2SDave Hansen {
1480aab520e2SDave Hansen 	int error;
14814ac91378SJan Blunck 	struct dentry *dir = nd->path.dentry;
1482aab520e2SDave Hansen 
1483aab520e2SDave Hansen 	if (!IS_POSIXACL(dir->d_inode))
1484ce3b0f8dSAl Viro 		mode &= ~current_umask();
1485be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd->path, path->dentry, mode, 0);
1486be6d3e56SKentaro Takeda 	if (error)
1487be6d3e56SKentaro Takeda 		goto out_unlock;
1488aab520e2SDave Hansen 	error = vfs_create(dir->d_inode, path->dentry, mode, nd);
1489be6d3e56SKentaro Takeda out_unlock:
1490aab520e2SDave Hansen 	mutex_unlock(&dir->d_inode->i_mutex);
14914ac91378SJan Blunck 	dput(nd->path.dentry);
14924ac91378SJan Blunck 	nd->path.dentry = path->dentry;
1493aab520e2SDave Hansen 	if (error)
1494aab520e2SDave Hansen 		return error;
1495aab520e2SDave Hansen 	/* Don't check for write permission, don't truncate */
14968737c930SAl Viro 	return may_open(&nd->path, 0, open_flag & ~O_TRUNC);
1497aab520e2SDave Hansen }
1498aab520e2SDave Hansen 
14991da177e4SLinus Torvalds /*
1500d57999e1SDave Hansen  * Note that while the flag value (low two bits) for sys_open means:
1501d57999e1SDave Hansen  *	00 - read-only
1502d57999e1SDave Hansen  *	01 - write-only
1503d57999e1SDave Hansen  *	10 - read-write
1504d57999e1SDave Hansen  *	11 - special
1505d57999e1SDave Hansen  * it is changed into
1506d57999e1SDave Hansen  *	00 - no permissions needed
1507d57999e1SDave Hansen  *	01 - read-permission
1508d57999e1SDave Hansen  *	10 - write-permission
1509d57999e1SDave Hansen  *	11 - read-write
1510d57999e1SDave Hansen  * for the internal routines (ie open_namei()/follow_link() etc)
1511d57999e1SDave Hansen  * This is more logical, and also allows the 00 "no perm needed"
1512d57999e1SDave Hansen  * to be used for symlinks (where the permissions are checked
1513d57999e1SDave Hansen  * later).
1514d57999e1SDave Hansen  *
1515d57999e1SDave Hansen */
1516d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
1517d57999e1SDave Hansen {
1518d57999e1SDave Hansen 	if ((flag+1) & O_ACCMODE)
1519d57999e1SDave Hansen 		flag++;
1520d57999e1SDave Hansen 	return flag;
1521d57999e1SDave Hansen }
1522d57999e1SDave Hansen 
15237715b521SAl Viro static int open_will_truncate(int flag, struct inode *inode)
15244a3fd211SDave Hansen {
1525d57999e1SDave Hansen 	/*
15264a3fd211SDave Hansen 	 * We'll never write to the fs underlying
15274a3fd211SDave Hansen 	 * a device file.
15284a3fd211SDave Hansen 	 */
15294a3fd211SDave Hansen 	if (special_file(inode->i_mode))
15304a3fd211SDave Hansen 		return 0;
15314a3fd211SDave Hansen 	return (flag & O_TRUNC);
15324a3fd211SDave Hansen }
15334a3fd211SDave Hansen 
1534648fa861SAl Viro static struct file *finish_open(struct nameidata *nd,
15359a66179eSAl Viro 				int open_flag, int acc_mode)
1536648fa861SAl Viro {
1537648fa861SAl Viro 	struct file *filp;
1538648fa861SAl Viro 	int will_truncate;
1539648fa861SAl Viro 	int error;
1540648fa861SAl Viro 
15419a66179eSAl Viro 	will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode);
1542648fa861SAl Viro 	if (will_truncate) {
1543648fa861SAl Viro 		error = mnt_want_write(nd->path.mnt);
1544648fa861SAl Viro 		if (error)
1545648fa861SAl Viro 			goto exit;
1546648fa861SAl Viro 	}
1547648fa861SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
1548648fa861SAl Viro 	if (error) {
1549648fa861SAl Viro 		if (will_truncate)
1550648fa861SAl Viro 			mnt_drop_write(nd->path.mnt);
1551648fa861SAl Viro 		goto exit;
1552648fa861SAl Viro 	}
1553648fa861SAl Viro 	filp = nameidata_to_filp(nd);
1554648fa861SAl Viro 	if (!IS_ERR(filp)) {
1555648fa861SAl Viro 		error = ima_file_check(filp, acc_mode);
1556648fa861SAl Viro 		if (error) {
1557648fa861SAl Viro 			fput(filp);
1558648fa861SAl Viro 			filp = ERR_PTR(error);
1559648fa861SAl Viro 		}
1560648fa861SAl Viro 	}
1561648fa861SAl Viro 	if (!IS_ERR(filp)) {
1562648fa861SAl Viro 		if (will_truncate) {
1563648fa861SAl Viro 			error = handle_truncate(&nd->path);
1564648fa861SAl Viro 			if (error) {
1565648fa861SAl Viro 				fput(filp);
1566648fa861SAl Viro 				filp = ERR_PTR(error);
1567648fa861SAl Viro 			}
1568648fa861SAl Viro 		}
1569648fa861SAl Viro 	}
1570648fa861SAl Viro 	/*
1571648fa861SAl Viro 	 * It is now safe to drop the mnt write
1572648fa861SAl Viro 	 * because the filp has had a write taken
1573648fa861SAl Viro 	 * on its behalf.
1574648fa861SAl Viro 	 */
1575648fa861SAl Viro 	if (will_truncate)
1576648fa861SAl Viro 		mnt_drop_write(nd->path.mnt);
1577648fa861SAl Viro 	return filp;
1578648fa861SAl Viro 
1579648fa861SAl Viro exit:
1580648fa861SAl Viro 	if (!IS_ERR(nd->intent.open.file))
1581648fa861SAl Viro 		release_open_intent(nd);
1582648fa861SAl Viro 	path_put(&nd->path);
1583648fa861SAl Viro 	return ERR_PTR(error);
1584648fa861SAl Viro }
1585648fa861SAl Viro 
1586fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
15875b369df8SAl Viro 			    int open_flag, int acc_mode,
15883e297b61SAl Viro 			    int mode, const char *pathname)
1589fb1cc555SAl Viro {
1590a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
1591fb1cc555SAl Viro 	struct file *filp;
15921f36f774SAl Viro 	int error = -EISDIR;
1593fb1cc555SAl Viro 
15941f36f774SAl Viro 	switch (nd->last_type) {
15951f36f774SAl Viro 	case LAST_DOTDOT:
15961f36f774SAl Viro 		follow_dotdot(nd);
15971f36f774SAl Viro 		dir = nd->path.dentry;
1598176306f5SNeil Brown 	case LAST_DOT:
15991f36f774SAl Viro 		if (nd->path.mnt->mnt_sb->s_type->fs_flags & FS_REVAL_DOT) {
16001f36f774SAl Viro 			if (!dir->d_op->d_revalidate(dir, nd)) {
16011f36f774SAl Viro 				error = -ESTALE;
1602a2c36b45SAl Viro 				goto exit;
16031f36f774SAl Viro 			}
16041f36f774SAl Viro 		}
16051f36f774SAl Viro 		/* fallthrough */
16061f36f774SAl Viro 	case LAST_ROOT:
16071f36f774SAl Viro 		if (open_flag & O_CREAT)
16081f36f774SAl Viro 			goto exit;
16091f36f774SAl Viro 		/* fallthrough */
16101f36f774SAl Viro 	case LAST_BIND:
16111f36f774SAl Viro 		audit_inode(pathname, dir);
16121f36f774SAl Viro 		goto ok;
16131f36f774SAl Viro 	}
1614a2c36b45SAl Viro 
16151f36f774SAl Viro 	/* trailing slashes? */
16161f36f774SAl Viro 	if (nd->last.name[nd->last.len]) {
16171f36f774SAl Viro 		if (open_flag & O_CREAT)
16181f36f774SAl Viro 			goto exit;
1619002baeecSJan Kara 		nd->flags |= LOOKUP_DIRECTORY | LOOKUP_FOLLOW;
16201f36f774SAl Viro 	}
16211f36f774SAl Viro 
16221f36f774SAl Viro 	/* just plain open? */
16231f36f774SAl Viro 	if (!(open_flag & O_CREAT)) {
16241f36f774SAl Viro 		error = do_lookup(nd, &nd->last, path);
16251f36f774SAl Viro 		if (error)
16261f36f774SAl Viro 			goto exit;
16271f36f774SAl Viro 		error = -ENOENT;
16281f36f774SAl Viro 		if (!path->dentry->d_inode)
16291f36f774SAl Viro 			goto exit_dput;
16301f36f774SAl Viro 		if (path->dentry->d_inode->i_op->follow_link)
16311f36f774SAl Viro 			return NULL;
16321f36f774SAl Viro 		error = -ENOTDIR;
16333e297b61SAl Viro 		if (nd->flags & LOOKUP_DIRECTORY) {
16343e297b61SAl Viro 			if (!path->dentry->d_inode->i_op->lookup)
16351f36f774SAl Viro 				goto exit_dput;
16363e297b61SAl Viro 		}
16371f36f774SAl Viro 		path_to_nameidata(path, nd);
16381f36f774SAl Viro 		audit_inode(pathname, nd->path.dentry);
16391f36f774SAl Viro 		goto ok;
16401f36f774SAl Viro 	}
16411f36f774SAl Viro 
16421f36f774SAl Viro 	/* OK, it's O_CREAT */
1643a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
1644a1e28038SAl Viro 
1645a1e28038SAl Viro 	path->dentry = lookup_hash(nd);
1646a1e28038SAl Viro 	path->mnt = nd->path.mnt;
1647a1e28038SAl Viro 
1648fb1cc555SAl Viro 	error = PTR_ERR(path->dentry);
1649fb1cc555SAl Viro 	if (IS_ERR(path->dentry)) {
1650fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
1651fb1cc555SAl Viro 		goto exit;
1652fb1cc555SAl Viro 	}
1653fb1cc555SAl Viro 
1654fb1cc555SAl Viro 	if (IS_ERR(nd->intent.open.file)) {
1655fb1cc555SAl Viro 		error = PTR_ERR(nd->intent.open.file);
1656fb1cc555SAl Viro 		goto exit_mutex_unlock;
1657fb1cc555SAl Viro 	}
1658fb1cc555SAl Viro 
1659fb1cc555SAl Viro 	/* Negative dentry, just create the file */
1660fb1cc555SAl Viro 	if (!path->dentry->d_inode) {
1661fb1cc555SAl Viro 		/*
1662fb1cc555SAl Viro 		 * This write is needed to ensure that a
1663fb1cc555SAl Viro 		 * ro->rw transition does not occur between
1664fb1cc555SAl Viro 		 * the time when the file is created and when
1665fb1cc555SAl Viro 		 * a permanent write count is taken through
1666fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
1667fb1cc555SAl Viro 		 */
1668fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
1669fb1cc555SAl Viro 		if (error)
1670fb1cc555SAl Viro 			goto exit_mutex_unlock;
1671fb1cc555SAl Viro 		error = __open_namei_create(nd, path, open_flag, mode);
1672fb1cc555SAl Viro 		if (error) {
1673fb1cc555SAl Viro 			mnt_drop_write(nd->path.mnt);
1674fb1cc555SAl Viro 			goto exit;
1675fb1cc555SAl Viro 		}
1676fb1cc555SAl Viro 		filp = nameidata_to_filp(nd);
1677fb1cc555SAl Viro 		mnt_drop_write(nd->path.mnt);
1678fb1cc555SAl Viro 		if (!IS_ERR(filp)) {
1679fb1cc555SAl Viro 			error = ima_file_check(filp, acc_mode);
1680fb1cc555SAl Viro 			if (error) {
1681fb1cc555SAl Viro 				fput(filp);
1682fb1cc555SAl Viro 				filp = ERR_PTR(error);
1683fb1cc555SAl Viro 			}
1684fb1cc555SAl Viro 		}
1685fb1cc555SAl Viro 		return filp;
1686fb1cc555SAl Viro 	}
1687fb1cc555SAl Viro 
1688fb1cc555SAl Viro 	/*
1689fb1cc555SAl Viro 	 * It already exists.
1690fb1cc555SAl Viro 	 */
1691fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
1692fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
1693fb1cc555SAl Viro 
1694fb1cc555SAl Viro 	error = -EEXIST;
16955b369df8SAl Viro 	if (open_flag & O_EXCL)
1696fb1cc555SAl Viro 		goto exit_dput;
1697fb1cc555SAl Viro 
1698fb1cc555SAl Viro 	if (__follow_mount(path)) {
1699fb1cc555SAl Viro 		error = -ELOOP;
17005b369df8SAl Viro 		if (open_flag & O_NOFOLLOW)
1701fb1cc555SAl Viro 			goto exit_dput;
1702fb1cc555SAl Viro 	}
1703fb1cc555SAl Viro 
1704fb1cc555SAl Viro 	error = -ENOENT;
1705fb1cc555SAl Viro 	if (!path->dentry->d_inode)
1706fb1cc555SAl Viro 		goto exit_dput;
17079e67f361SAl Viro 
17089e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
1709fb1cc555SAl Viro 		return NULL;
1710fb1cc555SAl Viro 
1711fb1cc555SAl Viro 	path_to_nameidata(path, nd);
1712fb1cc555SAl Viro 	error = -EISDIR;
1713fb1cc555SAl Viro 	if (S_ISDIR(path->dentry->d_inode->i_mode))
1714fb1cc555SAl Viro 		goto exit;
171567ee3ad2SAl Viro ok:
17169a66179eSAl Viro 	filp = finish_open(nd, open_flag, acc_mode);
1717fb1cc555SAl Viro 	return filp;
1718fb1cc555SAl Viro 
1719fb1cc555SAl Viro exit_mutex_unlock:
1720fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
1721fb1cc555SAl Viro exit_dput:
1722fb1cc555SAl Viro 	path_put_conditional(path, nd);
1723fb1cc555SAl Viro exit:
1724fb1cc555SAl Viro 	if (!IS_ERR(nd->intent.open.file))
1725fb1cc555SAl Viro 		release_open_intent(nd);
1726fb1cc555SAl Viro 	path_put(&nd->path);
1727fb1cc555SAl Viro 	return ERR_PTR(error);
1728fb1cc555SAl Viro }
1729fb1cc555SAl Viro 
17304a3fd211SDave Hansen /*
17314a3fd211SDave Hansen  * Note that the low bits of the passed in "open_flag"
17324a3fd211SDave Hansen  * are not the same as in the local variable "flag". See
17334a3fd211SDave Hansen  * open_to_namei_flags() for more details.
17341da177e4SLinus Torvalds  */
1735a70e65dfSChristoph Hellwig struct file *do_filp_open(int dfd, const char *pathname,
17366e8341a1SAl Viro 		int open_flag, int mode, int acc_mode)
17371da177e4SLinus Torvalds {
17384a3fd211SDave Hansen 	struct file *filp;
1739a70e65dfSChristoph Hellwig 	struct nameidata nd;
17406e8341a1SAl Viro 	int error;
17419850c056SAl Viro 	struct path path;
17421da177e4SLinus Torvalds 	int count = 0;
1743d57999e1SDave Hansen 	int flag = open_to_namei_flags(open_flag);
17449850c056SAl Viro 	int force_reval = 0;
17451f36f774SAl Viro 
17461f36f774SAl Viro 	if (!(open_flag & O_CREAT))
17471f36f774SAl Viro 		mode = 0;
17481da177e4SLinus Torvalds 
17496b2f3d1fSChristoph Hellwig 	/*
17506b2f3d1fSChristoph Hellwig 	 * O_SYNC is implemented as __O_SYNC|O_DSYNC.  As many places only
17516b2f3d1fSChristoph Hellwig 	 * check for O_DSYNC if the need any syncing at all we enforce it's
17526b2f3d1fSChristoph Hellwig 	 * always set instead of having to deal with possibly weird behaviour
17536b2f3d1fSChristoph Hellwig 	 * for malicious applications setting only __O_SYNC.
17546b2f3d1fSChristoph Hellwig 	 */
17556b2f3d1fSChristoph Hellwig 	if (open_flag & __O_SYNC)
17566b2f3d1fSChristoph Hellwig 		open_flag |= O_DSYNC;
17576b2f3d1fSChristoph Hellwig 
17586e8341a1SAl Viro 	if (!acc_mode)
17596d125529SAl Viro 		acc_mode = MAY_OPEN | ACC_MODE(open_flag);
17601da177e4SLinus Torvalds 
1761834f2a4aSTrond Myklebust 	/* O_TRUNC implies we need access checks for write permissions */
17624296e2cbSAl Viro 	if (open_flag & O_TRUNC)
1763834f2a4aSTrond Myklebust 		acc_mode |= MAY_WRITE;
1764834f2a4aSTrond Myklebust 
17651da177e4SLinus Torvalds 	/* Allow the LSM permission hook to distinguish append
17661da177e4SLinus Torvalds 	   access from general write access. */
17674296e2cbSAl Viro 	if (open_flag & O_APPEND)
17681da177e4SLinus Torvalds 		acc_mode |= MAY_APPEND;
17691da177e4SLinus Torvalds 
17701f36f774SAl Viro 	/* find the parent */
17719850c056SAl Viro reval:
17729b4a9b14SAl Viro 	error = path_init(dfd, pathname, LOOKUP_PARENT, &nd);
17731da177e4SLinus Torvalds 	if (error)
1774a70e65dfSChristoph Hellwig 		return ERR_PTR(error);
17759850c056SAl Viro 	if (force_reval)
17769850c056SAl Viro 		nd.flags |= LOOKUP_REVAL;
17773866248eSAl Viro 
17783866248eSAl Viro 	current->total_link_count = 0;
17793866248eSAl Viro 	error = link_path_walk(pathname, &nd);
1780654f562cSJ. R. Okajima 	if (error) {
178110fa8e62SAl Viro 		filp = ERR_PTR(error);
178210fa8e62SAl Viro 		goto out;
1783654f562cSJ. R. Okajima 	}
17841f36f774SAl Viro 	if (unlikely(!audit_dummy_context()) && (open_flag & O_CREAT))
17859b4a9b14SAl Viro 		audit_inode(pathname, nd.path.dentry);
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds 	/*
1788a2c36b45SAl Viro 	 * We have the parent and last component.
17891da177e4SLinus Torvalds 	 */
17901da177e4SLinus Torvalds 
17918737f3a1SAl Viro 	error = -ENFILE;
17928737f3a1SAl Viro 	filp = get_empty_filp();
17938737f3a1SAl Viro 	if (filp == NULL)
17948737f3a1SAl Viro 		goto exit_parent;
17958737f3a1SAl Viro 	nd.intent.open.file = filp;
1796482928d5SAl Viro 	filp->f_flags = open_flag;
17978737f3a1SAl Viro 	nd.intent.open.flags = flag;
17988737f3a1SAl Viro 	nd.intent.open.create_mode = mode;
1799a70e65dfSChristoph Hellwig 	nd.flags &= ~LOOKUP_PARENT;
18001f36f774SAl Viro 	nd.flags |= LOOKUP_OPEN;
18011f36f774SAl Viro 	if (open_flag & O_CREAT) {
18021f36f774SAl Viro 		nd.flags |= LOOKUP_CREATE;
18034296e2cbSAl Viro 		if (open_flag & O_EXCL)
18043516586aSAl Viro 			nd.flags |= LOOKUP_EXCL;
18051f36f774SAl Viro 	}
18063e297b61SAl Viro 	if (open_flag & O_DIRECTORY)
18073e297b61SAl Viro 		nd.flags |= LOOKUP_DIRECTORY;
1808002baeecSJan Kara 	if (!(open_flag & O_NOFOLLOW))
1809002baeecSJan Kara 		nd.flags |= LOOKUP_FOLLOW;
18103e297b61SAl Viro 	filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
1811806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
1812def4af30SAl Viro 		struct path holder;
18131f36f774SAl Viro 		struct inode *inode = path.dentry->d_inode;
1814def4af30SAl Viro 		void *cookie;
1815806b681cSAl Viro 		error = -ELOOP;
18161f36f774SAl Viro 		/* S_ISDIR part is a temporary automount kludge */
1817002baeecSJan Kara 		if (!(nd.flags & LOOKUP_FOLLOW) && !S_ISDIR(inode->i_mode))
18181f36f774SAl Viro 			goto exit_dput;
18191f36f774SAl Viro 		if (count++ == 32)
1820806b681cSAl Viro 			goto exit_dput;
1821806b681cSAl Viro 		/*
1822806b681cSAl Viro 		 * This is subtle. Instead of calling do_follow_link() we do
1823806b681cSAl Viro 		 * the thing by hands. The reason is that this way we have zero
1824806b681cSAl Viro 		 * link_count and path_walk() (called from ->follow_link)
1825806b681cSAl Viro 		 * honoring LOOKUP_PARENT.  After that we have the parent and
1826806b681cSAl Viro 		 * last component, i.e. we are in the same situation as after
1827806b681cSAl Viro 		 * the first path_walk().  Well, almost - if the last component
1828806b681cSAl Viro 		 * is normal we get its copy stored in nd->last.name and we will
1829806b681cSAl Viro 		 * have to putname() it when we are done. Procfs-like symlinks
1830806b681cSAl Viro 		 * just set LAST_BIND.
1831806b681cSAl Viro 		 */
1832806b681cSAl Viro 		nd.flags |= LOOKUP_PARENT;
1833806b681cSAl Viro 		error = security_inode_follow_link(path.dentry, &nd);
1834806b681cSAl Viro 		if (error)
1835806b681cSAl Viro 			goto exit_dput;
1836def4af30SAl Viro 		error = __do_follow_link(&path, &nd, &cookie);
1837def4af30SAl Viro 		if (unlikely(error)) {
1838806b681cSAl Viro 			/* nd.path had been dropped */
1839def4af30SAl Viro 			if (!IS_ERR(cookie) && inode->i_op->put_link)
1840def4af30SAl Viro 				inode->i_op->put_link(path.dentry, &nd, cookie);
1841def4af30SAl Viro 			path_put(&path);
1842a70e65dfSChristoph Hellwig 			release_open_intent(&nd);
184310fa8e62SAl Viro 			filp = ERR_PTR(error);
1844806b681cSAl Viro 			goto out;
1845806b681cSAl Viro 		}
1846def4af30SAl Viro 		holder = path;
1847806b681cSAl Viro 		nd.flags &= ~LOOKUP_PARENT;
18483e297b61SAl Viro 		filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
1849def4af30SAl Viro 		if (inode->i_op->put_link)
1850def4af30SAl Viro 			inode->i_op->put_link(holder.dentry, &nd, cookie);
1851def4af30SAl Viro 		path_put(&holder);
1852806b681cSAl Viro 	}
185310fa8e62SAl Viro out:
18542a737871SAl Viro 	if (nd.root.mnt)
18552a737871SAl Viro 		path_put(&nd.root);
185610fa8e62SAl Viro 	if (filp == ERR_PTR(-ESTALE) && !force_reval) {
185710fa8e62SAl Viro 		force_reval = 1;
185810fa8e62SAl Viro 		goto reval;
185910fa8e62SAl Viro 	}
186010fa8e62SAl Viro 	return filp;
18611da177e4SLinus Torvalds 
1862806b681cSAl Viro exit_dput:
1863806b681cSAl Viro 	path_put_conditional(&path, &nd);
1864806b681cSAl Viro 	if (!IS_ERR(nd.intent.open.file))
1865a70e65dfSChristoph Hellwig 		release_open_intent(&nd);
1866806b681cSAl Viro exit_parent:
1867806b681cSAl Viro 	path_put(&nd.path);
186810fa8e62SAl Viro 	filp = ERR_PTR(error);
186910fa8e62SAl Viro 	goto out;
1870de459215SKirill Korotaev }
18711da177e4SLinus Torvalds 
18721da177e4SLinus Torvalds /**
1873a70e65dfSChristoph Hellwig  * filp_open - open file and return file pointer
1874a70e65dfSChristoph Hellwig  *
1875a70e65dfSChristoph Hellwig  * @filename:	path to open
1876a70e65dfSChristoph Hellwig  * @flags:	open flags as per the open(2) second argument
1877a70e65dfSChristoph Hellwig  * @mode:	mode for the new file if O_CREAT is set, else ignored
1878a70e65dfSChristoph Hellwig  *
1879a70e65dfSChristoph Hellwig  * This is the helper to open a file from kernelspace if you really
1880a70e65dfSChristoph Hellwig  * have to.  But in generally you should not do this, so please move
1881a70e65dfSChristoph Hellwig  * along, nothing to see here..
1882a70e65dfSChristoph Hellwig  */
1883a70e65dfSChristoph Hellwig struct file *filp_open(const char *filename, int flags, int mode)
1884a70e65dfSChristoph Hellwig {
18856e8341a1SAl Viro 	return do_filp_open(AT_FDCWD, filename, flags, mode, 0);
1886a70e65dfSChristoph Hellwig }
1887a70e65dfSChristoph Hellwig EXPORT_SYMBOL(filp_open);
1888a70e65dfSChristoph Hellwig 
1889a70e65dfSChristoph Hellwig /**
18901da177e4SLinus Torvalds  * lookup_create - lookup a dentry, creating it if it doesn't exist
18911da177e4SLinus Torvalds  * @nd: nameidata info
18921da177e4SLinus Torvalds  * @is_dir: directory flag
18931da177e4SLinus Torvalds  *
18941da177e4SLinus Torvalds  * Simple function to lookup and return a dentry and create it
18951da177e4SLinus Torvalds  * if it doesn't exist.  Is SMP-safe.
1896c663e5d8SChristoph Hellwig  *
18974ac91378SJan Blunck  * Returns with nd->path.dentry->d_inode->i_mutex locked.
18981da177e4SLinus Torvalds  */
18991da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir)
19001da177e4SLinus Torvalds {
1901c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
19021da177e4SLinus Torvalds 
19034ac91378SJan Blunck 	mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
1904c663e5d8SChristoph Hellwig 	/*
1905c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
1906c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
1907c663e5d8SChristoph Hellwig 	 */
19081da177e4SLinus Torvalds 	if (nd->last_type != LAST_NORM)
19091da177e4SLinus Torvalds 		goto fail;
19101da177e4SLinus Torvalds 	nd->flags &= ~LOOKUP_PARENT;
19113516586aSAl Viro 	nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
1912a634904aSASANO Masahiro 	nd->intent.open.flags = O_EXCL;
1913c663e5d8SChristoph Hellwig 
1914c663e5d8SChristoph Hellwig 	/*
1915c663e5d8SChristoph Hellwig 	 * Do the final lookup.
1916c663e5d8SChristoph Hellwig 	 */
191749705b77SChristoph Hellwig 	dentry = lookup_hash(nd);
19181da177e4SLinus Torvalds 	if (IS_ERR(dentry))
19191da177e4SLinus Torvalds 		goto fail;
1920c663e5d8SChristoph Hellwig 
1921e9baf6e5SAl Viro 	if (dentry->d_inode)
1922e9baf6e5SAl Viro 		goto eexist;
1923c663e5d8SChristoph Hellwig 	/*
1924c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
1925c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
1926c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
1927c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
1928c663e5d8SChristoph Hellwig 	 */
1929e9baf6e5SAl Viro 	if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
19301da177e4SLinus Torvalds 		dput(dentry);
19311da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
1932e9baf6e5SAl Viro 	}
1933e9baf6e5SAl Viro 	return dentry;
1934e9baf6e5SAl Viro eexist:
1935e9baf6e5SAl Viro 	dput(dentry);
1936e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
19371da177e4SLinus Torvalds fail:
19381da177e4SLinus Torvalds 	return dentry;
19391da177e4SLinus Torvalds }
1940f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create);
19411da177e4SLinus Torvalds 
19421da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
19431da177e4SLinus Torvalds {
1944a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
19451da177e4SLinus Torvalds 
19461da177e4SLinus Torvalds 	if (error)
19471da177e4SLinus Torvalds 		return error;
19481da177e4SLinus Torvalds 
19491da177e4SLinus Torvalds 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
19501da177e4SLinus Torvalds 		return -EPERM;
19511da177e4SLinus Torvalds 
1952acfa4380SAl Viro 	if (!dir->i_op->mknod)
19531da177e4SLinus Torvalds 		return -EPERM;
19541da177e4SLinus Torvalds 
195508ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
195608ce5f16SSerge E. Hallyn 	if (error)
195708ce5f16SSerge E. Hallyn 		return error;
195808ce5f16SSerge E. Hallyn 
19591da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
19601da177e4SLinus Torvalds 	if (error)
19611da177e4SLinus Torvalds 		return error;
19621da177e4SLinus Torvalds 
19631da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
1964a74574aaSStephen Smalley 	if (!error)
1965f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
19661da177e4SLinus Torvalds 	return error;
19671da177e4SLinus Torvalds }
19681da177e4SLinus Torvalds 
1969463c3197SDave Hansen static int may_mknod(mode_t mode)
1970463c3197SDave Hansen {
1971463c3197SDave Hansen 	switch (mode & S_IFMT) {
1972463c3197SDave Hansen 	case S_IFREG:
1973463c3197SDave Hansen 	case S_IFCHR:
1974463c3197SDave Hansen 	case S_IFBLK:
1975463c3197SDave Hansen 	case S_IFIFO:
1976463c3197SDave Hansen 	case S_IFSOCK:
1977463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
1978463c3197SDave Hansen 		return 0;
1979463c3197SDave Hansen 	case S_IFDIR:
1980463c3197SDave Hansen 		return -EPERM;
1981463c3197SDave Hansen 	default:
1982463c3197SDave Hansen 		return -EINVAL;
1983463c3197SDave Hansen 	}
1984463c3197SDave Hansen }
1985463c3197SDave Hansen 
19862e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
19872e4d0924SHeiko Carstens 		unsigned, dev)
19881da177e4SLinus Torvalds {
19892ad94ae6SAl Viro 	int error;
19901da177e4SLinus Torvalds 	char *tmp;
19911da177e4SLinus Torvalds 	struct dentry *dentry;
19921da177e4SLinus Torvalds 	struct nameidata nd;
19931da177e4SLinus Torvalds 
19941da177e4SLinus Torvalds 	if (S_ISDIR(mode))
19951da177e4SLinus Torvalds 		return -EPERM;
19961da177e4SLinus Torvalds 
19972ad94ae6SAl Viro 	error = user_path_parent(dfd, filename, &nd, &tmp);
19981da177e4SLinus Torvalds 	if (error)
19992ad94ae6SAl Viro 		return error;
20002ad94ae6SAl Viro 
20011da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
2002463c3197SDave Hansen 	if (IS_ERR(dentry)) {
20031da177e4SLinus Torvalds 		error = PTR_ERR(dentry);
2004463c3197SDave Hansen 		goto out_unlock;
2005463c3197SDave Hansen 	}
20064ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2007ce3b0f8dSAl Viro 		mode &= ~current_umask();
2008463c3197SDave Hansen 	error = may_mknod(mode);
2009463c3197SDave Hansen 	if (error)
2010463c3197SDave Hansen 		goto out_dput;
2011463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2012463c3197SDave Hansen 	if (error)
2013463c3197SDave Hansen 		goto out_dput;
2014be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd.path, dentry, mode, dev);
2015be6d3e56SKentaro Takeda 	if (error)
2016be6d3e56SKentaro Takeda 		goto out_drop_write;
20171da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
20181da177e4SLinus Torvalds 		case 0: case S_IFREG:
20194ac91378SJan Blunck 			error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
20201da177e4SLinus Torvalds 			break;
20211da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
20224ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
20231da177e4SLinus Torvalds 					new_decode_dev(dev));
20241da177e4SLinus Torvalds 			break;
20251da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
20264ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
20271da177e4SLinus Torvalds 			break;
20281da177e4SLinus Torvalds 	}
2029be6d3e56SKentaro Takeda out_drop_write:
2030463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2031463c3197SDave Hansen out_dput:
20321da177e4SLinus Torvalds 	dput(dentry);
2033463c3197SDave Hansen out_unlock:
20344ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
20351d957f9bSJan Blunck 	path_put(&nd.path);
20361da177e4SLinus Torvalds 	putname(tmp);
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds 	return error;
20391da177e4SLinus Torvalds }
20401da177e4SLinus Torvalds 
20413480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
20425590ff0dSUlrich Drepper {
20435590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
20445590ff0dSUlrich Drepper }
20455590ff0dSUlrich Drepper 
20461da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
20471da177e4SLinus Torvalds {
2048a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
20491da177e4SLinus Torvalds 
20501da177e4SLinus Torvalds 	if (error)
20511da177e4SLinus Torvalds 		return error;
20521da177e4SLinus Torvalds 
2053acfa4380SAl Viro 	if (!dir->i_op->mkdir)
20541da177e4SLinus Torvalds 		return -EPERM;
20551da177e4SLinus Torvalds 
20561da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
20571da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
20581da177e4SLinus Torvalds 	if (error)
20591da177e4SLinus Torvalds 		return error;
20601da177e4SLinus Torvalds 
20611da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2062a74574aaSStephen Smalley 	if (!error)
2063f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
20641da177e4SLinus Torvalds 	return error;
20651da177e4SLinus Torvalds }
20661da177e4SLinus Torvalds 
20672e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
20681da177e4SLinus Torvalds {
20691da177e4SLinus Torvalds 	int error = 0;
20701da177e4SLinus Torvalds 	char * tmp;
20716902d925SDave Hansen 	struct dentry *dentry;
20726902d925SDave Hansen 	struct nameidata nd;
20731da177e4SLinus Torvalds 
20742ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &tmp);
20752ad94ae6SAl Viro 	if (error)
20766902d925SDave Hansen 		goto out_err;
20771da177e4SLinus Torvalds 
20781da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 1);
20791da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
20806902d925SDave Hansen 	if (IS_ERR(dentry))
20816902d925SDave Hansen 		goto out_unlock;
20826902d925SDave Hansen 
20834ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2084ce3b0f8dSAl Viro 		mode &= ~current_umask();
2085463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2086463c3197SDave Hansen 	if (error)
2087463c3197SDave Hansen 		goto out_dput;
2088be6d3e56SKentaro Takeda 	error = security_path_mkdir(&nd.path, dentry, mode);
2089be6d3e56SKentaro Takeda 	if (error)
2090be6d3e56SKentaro Takeda 		goto out_drop_write;
20914ac91378SJan Blunck 	error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2092be6d3e56SKentaro Takeda out_drop_write:
2093463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2094463c3197SDave Hansen out_dput:
20951da177e4SLinus Torvalds 	dput(dentry);
20966902d925SDave Hansen out_unlock:
20974ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
20981d957f9bSJan Blunck 	path_put(&nd.path);
20991da177e4SLinus Torvalds 	putname(tmp);
21006902d925SDave Hansen out_err:
21011da177e4SLinus Torvalds 	return error;
21021da177e4SLinus Torvalds }
21031da177e4SLinus Torvalds 
21043cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
21055590ff0dSUlrich Drepper {
21065590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
21075590ff0dSUlrich Drepper }
21085590ff0dSUlrich Drepper 
21091da177e4SLinus Torvalds /*
21101da177e4SLinus Torvalds  * We try to drop the dentry early: we should have
21111da177e4SLinus Torvalds  * a usage count of 2 if we're the only user of this
21121da177e4SLinus Torvalds  * dentry, and if that is true (possibly after pruning
21131da177e4SLinus Torvalds  * the dcache), then we drop the dentry now.
21141da177e4SLinus Torvalds  *
21151da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
21161da177e4SLinus Torvalds  * do a
21171da177e4SLinus Torvalds  *
21181da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
21191da177e4SLinus Torvalds  *		return -EBUSY;
21201da177e4SLinus Torvalds  *
21211da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
21221da177e4SLinus Torvalds  * that is still in use by something else..
21231da177e4SLinus Torvalds  */
21241da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
21251da177e4SLinus Torvalds {
21261da177e4SLinus Torvalds 	dget(dentry);
21271da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
21281da177e4SLinus Torvalds 	spin_lock(&dcache_lock);
21291da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
21301da177e4SLinus Torvalds 	if (atomic_read(&dentry->d_count) == 2)
21311da177e4SLinus Torvalds 		__d_drop(dentry);
21321da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
21331da177e4SLinus Torvalds 	spin_unlock(&dcache_lock);
21341da177e4SLinus Torvalds }
21351da177e4SLinus Torvalds 
21361da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
21371da177e4SLinus Torvalds {
21381da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
21391da177e4SLinus Torvalds 
21401da177e4SLinus Torvalds 	if (error)
21411da177e4SLinus Torvalds 		return error;
21421da177e4SLinus Torvalds 
2143acfa4380SAl Viro 	if (!dir->i_op->rmdir)
21441da177e4SLinus Torvalds 		return -EPERM;
21451da177e4SLinus Torvalds 
21461b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
21471da177e4SLinus Torvalds 	dentry_unhash(dentry);
21481da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
21491da177e4SLinus Torvalds 		error = -EBUSY;
21501da177e4SLinus Torvalds 	else {
21511da177e4SLinus Torvalds 		error = security_inode_rmdir(dir, dentry);
21521da177e4SLinus Torvalds 		if (!error) {
21531da177e4SLinus Torvalds 			error = dir->i_op->rmdir(dir, dentry);
2154d83c49f3SAl Viro 			if (!error) {
21551da177e4SLinus Torvalds 				dentry->d_inode->i_flags |= S_DEAD;
2156d83c49f3SAl Viro 				dont_mount(dentry);
2157d83c49f3SAl Viro 			}
21581da177e4SLinus Torvalds 		}
21591da177e4SLinus Torvalds 	}
21601b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
21611da177e4SLinus Torvalds 	if (!error) {
21621da177e4SLinus Torvalds 		d_delete(dentry);
21631da177e4SLinus Torvalds 	}
21641da177e4SLinus Torvalds 	dput(dentry);
21651da177e4SLinus Torvalds 
21661da177e4SLinus Torvalds 	return error;
21671da177e4SLinus Torvalds }
21681da177e4SLinus Torvalds 
21695590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
21701da177e4SLinus Torvalds {
21711da177e4SLinus Torvalds 	int error = 0;
21721da177e4SLinus Torvalds 	char * name;
21731da177e4SLinus Torvalds 	struct dentry *dentry;
21741da177e4SLinus Torvalds 	struct nameidata nd;
21751da177e4SLinus Torvalds 
21762ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
21771da177e4SLinus Torvalds 	if (error)
21782ad94ae6SAl Viro 		return error;
21791da177e4SLinus Torvalds 
21801da177e4SLinus Torvalds 	switch(nd.last_type) {
21811da177e4SLinus Torvalds 	case LAST_DOTDOT:
21821da177e4SLinus Torvalds 		error = -ENOTEMPTY;
21831da177e4SLinus Torvalds 		goto exit1;
21841da177e4SLinus Torvalds 	case LAST_DOT:
21851da177e4SLinus Torvalds 		error = -EINVAL;
21861da177e4SLinus Torvalds 		goto exit1;
21871da177e4SLinus Torvalds 	case LAST_ROOT:
21881da177e4SLinus Torvalds 		error = -EBUSY;
21891da177e4SLinus Torvalds 		goto exit1;
21901da177e4SLinus Torvalds 	}
21910612d9fbSOGAWA Hirofumi 
21920612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
21930612d9fbSOGAWA Hirofumi 
21944ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
219549705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
21961da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
21976902d925SDave Hansen 	if (IS_ERR(dentry))
21986902d925SDave Hansen 		goto exit2;
21990622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
22000622753bSDave Hansen 	if (error)
22010622753bSDave Hansen 		goto exit3;
2202be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2203be6d3e56SKentaro Takeda 	if (error)
2204be6d3e56SKentaro Takeda 		goto exit4;
22054ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2206be6d3e56SKentaro Takeda exit4:
22070622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
22080622753bSDave Hansen exit3:
22091da177e4SLinus Torvalds 	dput(dentry);
22106902d925SDave Hansen exit2:
22114ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
22121da177e4SLinus Torvalds exit1:
22131d957f9bSJan Blunck 	path_put(&nd.path);
22141da177e4SLinus Torvalds 	putname(name);
22151da177e4SLinus Torvalds 	return error;
22161da177e4SLinus Torvalds }
22171da177e4SLinus Torvalds 
22183cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
22195590ff0dSUlrich Drepper {
22205590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
22215590ff0dSUlrich Drepper }
22225590ff0dSUlrich Drepper 
22231da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
22241da177e4SLinus Torvalds {
22251da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
22261da177e4SLinus Torvalds 
22271da177e4SLinus Torvalds 	if (error)
22281da177e4SLinus Torvalds 		return error;
22291da177e4SLinus Torvalds 
2230acfa4380SAl Viro 	if (!dir->i_op->unlink)
22311da177e4SLinus Torvalds 		return -EPERM;
22321da177e4SLinus Torvalds 
22331b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
22341da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
22351da177e4SLinus Torvalds 		error = -EBUSY;
22361da177e4SLinus Torvalds 	else {
22371da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2238bec1052eSAl Viro 		if (!error) {
22391da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2240bec1052eSAl Viro 			if (!error)
2241d83c49f3SAl Viro 				dont_mount(dentry);
2242bec1052eSAl Viro 		}
22431da177e4SLinus Torvalds 	}
22441b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
22451da177e4SLinus Torvalds 
22461da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
22471da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2248ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
22491da177e4SLinus Torvalds 		d_delete(dentry);
22501da177e4SLinus Torvalds 	}
22510eeca283SRobert Love 
22521da177e4SLinus Torvalds 	return error;
22531da177e4SLinus Torvalds }
22541da177e4SLinus Torvalds 
22551da177e4SLinus Torvalds /*
22561da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
22571b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
22581da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
22591da177e4SLinus Torvalds  * while waiting on the I/O.
22601da177e4SLinus Torvalds  */
22615590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
22621da177e4SLinus Torvalds {
22632ad94ae6SAl Viro 	int error;
22641da177e4SLinus Torvalds 	char *name;
22651da177e4SLinus Torvalds 	struct dentry *dentry;
22661da177e4SLinus Torvalds 	struct nameidata nd;
22671da177e4SLinus Torvalds 	struct inode *inode = NULL;
22681da177e4SLinus Torvalds 
22692ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
22701da177e4SLinus Torvalds 	if (error)
22712ad94ae6SAl Viro 		return error;
22722ad94ae6SAl Viro 
22731da177e4SLinus Torvalds 	error = -EISDIR;
22741da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
22751da177e4SLinus Torvalds 		goto exit1;
22760612d9fbSOGAWA Hirofumi 
22770612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
22780612d9fbSOGAWA Hirofumi 
22794ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
228049705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
22811da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
22821da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
22831da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
22841da177e4SLinus Torvalds 		if (nd.last.name[nd.last.len])
22851da177e4SLinus Torvalds 			goto slashes;
22861da177e4SLinus Torvalds 		inode = dentry->d_inode;
22871da177e4SLinus Torvalds 		if (inode)
22887de9c6eeSAl Viro 			ihold(inode);
22890622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
22900622753bSDave Hansen 		if (error)
22910622753bSDave Hansen 			goto exit2;
2292be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2293be6d3e56SKentaro Takeda 		if (error)
2294be6d3e56SKentaro Takeda 			goto exit3;
22954ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2296be6d3e56SKentaro Takeda exit3:
22970622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
22981da177e4SLinus Torvalds 	exit2:
22991da177e4SLinus Torvalds 		dput(dentry);
23001da177e4SLinus Torvalds 	}
23014ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
23021da177e4SLinus Torvalds 	if (inode)
23031da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
23041da177e4SLinus Torvalds exit1:
23051d957f9bSJan Blunck 	path_put(&nd.path);
23061da177e4SLinus Torvalds 	putname(name);
23071da177e4SLinus Torvalds 	return error;
23081da177e4SLinus Torvalds 
23091da177e4SLinus Torvalds slashes:
23101da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
23111da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
23121da177e4SLinus Torvalds 	goto exit2;
23131da177e4SLinus Torvalds }
23141da177e4SLinus Torvalds 
23152e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
23165590ff0dSUlrich Drepper {
23175590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
23185590ff0dSUlrich Drepper 		return -EINVAL;
23195590ff0dSUlrich Drepper 
23205590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
23215590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
23225590ff0dSUlrich Drepper 
23235590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
23245590ff0dSUlrich Drepper }
23255590ff0dSUlrich Drepper 
23263480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
23275590ff0dSUlrich Drepper {
23285590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
23295590ff0dSUlrich Drepper }
23305590ff0dSUlrich Drepper 
2331db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
23321da177e4SLinus Torvalds {
2333a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
23341da177e4SLinus Torvalds 
23351da177e4SLinus Torvalds 	if (error)
23361da177e4SLinus Torvalds 		return error;
23371da177e4SLinus Torvalds 
2338acfa4380SAl Viro 	if (!dir->i_op->symlink)
23391da177e4SLinus Torvalds 		return -EPERM;
23401da177e4SLinus Torvalds 
23411da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
23421da177e4SLinus Torvalds 	if (error)
23431da177e4SLinus Torvalds 		return error;
23441da177e4SLinus Torvalds 
23451da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2346a74574aaSStephen Smalley 	if (!error)
2347f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
23481da177e4SLinus Torvalds 	return error;
23491da177e4SLinus Torvalds }
23501da177e4SLinus Torvalds 
23512e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
23522e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
23531da177e4SLinus Torvalds {
23542ad94ae6SAl Viro 	int error;
23551da177e4SLinus Torvalds 	char *from;
23561da177e4SLinus Torvalds 	char *to;
23576902d925SDave Hansen 	struct dentry *dentry;
23586902d925SDave Hansen 	struct nameidata nd;
23591da177e4SLinus Torvalds 
23601da177e4SLinus Torvalds 	from = getname(oldname);
23611da177e4SLinus Torvalds 	if (IS_ERR(from))
23621da177e4SLinus Torvalds 		return PTR_ERR(from);
23632ad94ae6SAl Viro 
23642ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
23652ad94ae6SAl Viro 	if (error)
23666902d925SDave Hansen 		goto out_putname;
23671da177e4SLinus Torvalds 
23681da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
23691da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
23706902d925SDave Hansen 	if (IS_ERR(dentry))
23716902d925SDave Hansen 		goto out_unlock;
23726902d925SDave Hansen 
237375c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
237475c3f29dSDave Hansen 	if (error)
237575c3f29dSDave Hansen 		goto out_dput;
2376be6d3e56SKentaro Takeda 	error = security_path_symlink(&nd.path, dentry, from);
2377be6d3e56SKentaro Takeda 	if (error)
2378be6d3e56SKentaro Takeda 		goto out_drop_write;
2379db2e747bSMiklos Szeredi 	error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2380be6d3e56SKentaro Takeda out_drop_write:
238175c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
238275c3f29dSDave Hansen out_dput:
23831da177e4SLinus Torvalds 	dput(dentry);
23846902d925SDave Hansen out_unlock:
23854ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
23861d957f9bSJan Blunck 	path_put(&nd.path);
23871da177e4SLinus Torvalds 	putname(to);
23886902d925SDave Hansen out_putname:
23891da177e4SLinus Torvalds 	putname(from);
23901da177e4SLinus Torvalds 	return error;
23911da177e4SLinus Torvalds }
23921da177e4SLinus Torvalds 
23933480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
23945590ff0dSUlrich Drepper {
23955590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
23965590ff0dSUlrich Drepper }
23975590ff0dSUlrich Drepper 
23981da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
23991da177e4SLinus Torvalds {
24001da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
24011da177e4SLinus Torvalds 	int error;
24021da177e4SLinus Torvalds 
24031da177e4SLinus Torvalds 	if (!inode)
24041da177e4SLinus Torvalds 		return -ENOENT;
24051da177e4SLinus Torvalds 
2406a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
24071da177e4SLinus Torvalds 	if (error)
24081da177e4SLinus Torvalds 		return error;
24091da177e4SLinus Torvalds 
24101da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
24111da177e4SLinus Torvalds 		return -EXDEV;
24121da177e4SLinus Torvalds 
24131da177e4SLinus Torvalds 	/*
24141da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
24151da177e4SLinus Torvalds 	 */
24161da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
24171da177e4SLinus Torvalds 		return -EPERM;
2418acfa4380SAl Viro 	if (!dir->i_op->link)
24191da177e4SLinus Torvalds 		return -EPERM;
24207e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
24211da177e4SLinus Torvalds 		return -EPERM;
24221da177e4SLinus Torvalds 
24231da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
24241da177e4SLinus Torvalds 	if (error)
24251da177e4SLinus Torvalds 		return error;
24261da177e4SLinus Torvalds 
24277e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
24281da177e4SLinus Torvalds 	error = dir->i_op->link(old_dentry, dir, new_dentry);
24297e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
2430e31e14ecSStephen Smalley 	if (!error)
24317e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
24321da177e4SLinus Torvalds 	return error;
24331da177e4SLinus Torvalds }
24341da177e4SLinus Torvalds 
24351da177e4SLinus Torvalds /*
24361da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
24371da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
24381da177e4SLinus Torvalds  * newname.  --KAB
24391da177e4SLinus Torvalds  *
24401da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
24411da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
24421da177e4SLinus Torvalds  * and other special files.  --ADM
24431da177e4SLinus Torvalds  */
24442e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
24452e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
24461da177e4SLinus Torvalds {
24471da177e4SLinus Torvalds 	struct dentry *new_dentry;
24482d8f3038SAl Viro 	struct nameidata nd;
24492d8f3038SAl Viro 	struct path old_path;
24501da177e4SLinus Torvalds 	int error;
24511da177e4SLinus Torvalds 	char *to;
24521da177e4SLinus Torvalds 
245345c9b11aSUlrich Drepper 	if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
2454c04030e1SUlrich Drepper 		return -EINVAL;
2455c04030e1SUlrich Drepper 
24562d8f3038SAl Viro 	error = user_path_at(olddfd, oldname,
245745c9b11aSUlrich Drepper 			     flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
24582d8f3038SAl Viro 			     &old_path);
24591da177e4SLinus Torvalds 	if (error)
24602ad94ae6SAl Viro 		return error;
24612ad94ae6SAl Viro 
24622ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
24631da177e4SLinus Torvalds 	if (error)
24641da177e4SLinus Torvalds 		goto out;
24651da177e4SLinus Torvalds 	error = -EXDEV;
24662d8f3038SAl Viro 	if (old_path.mnt != nd.path.mnt)
24671da177e4SLinus Torvalds 		goto out_release;
24681da177e4SLinus Torvalds 	new_dentry = lookup_create(&nd, 0);
24691da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
24706902d925SDave Hansen 	if (IS_ERR(new_dentry))
24716902d925SDave Hansen 		goto out_unlock;
247275c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
247375c3f29dSDave Hansen 	if (error)
247475c3f29dSDave Hansen 		goto out_dput;
2475be6d3e56SKentaro Takeda 	error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2476be6d3e56SKentaro Takeda 	if (error)
2477be6d3e56SKentaro Takeda 		goto out_drop_write;
24782d8f3038SAl Viro 	error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
2479be6d3e56SKentaro Takeda out_drop_write:
248075c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
248175c3f29dSDave Hansen out_dput:
24821da177e4SLinus Torvalds 	dput(new_dentry);
24836902d925SDave Hansen out_unlock:
24844ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
24851da177e4SLinus Torvalds out_release:
24861d957f9bSJan Blunck 	path_put(&nd.path);
24872ad94ae6SAl Viro 	putname(to);
24881da177e4SLinus Torvalds out:
24892d8f3038SAl Viro 	path_put(&old_path);
24901da177e4SLinus Torvalds 
24911da177e4SLinus Torvalds 	return error;
24921da177e4SLinus Torvalds }
24931da177e4SLinus Torvalds 
24943480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
24955590ff0dSUlrich Drepper {
2496c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
24975590ff0dSUlrich Drepper }
24985590ff0dSUlrich Drepper 
24991da177e4SLinus Torvalds /*
25001da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
25011da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
25021da177e4SLinus Torvalds  * Problems:
25031da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
25041da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
25051da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
2506a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
25071da177e4SLinus Torvalds  *	   story.
25081da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
25091b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
25101da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
25111da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
2512a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
25131da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
25141da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
25151da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
2516a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
25171da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
25181da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
25191da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
25201da177e4SLinus Torvalds  *	d) some filesystems don't support opened-but-unlinked directories,
25211da177e4SLinus Torvalds  *	   either because of layout or because they are not ready to deal with
25221da177e4SLinus Torvalds  *	   all cases correctly. The latter will be fixed (taking this sort of
25231da177e4SLinus Torvalds  *	   stuff into VFS), but the former is not going away. Solution: the same
25241da177e4SLinus Torvalds  *	   trick as in rmdir().
25251da177e4SLinus Torvalds  *	e) conversion from fhandle to dentry may come in the wrong moment - when
25261b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
25271da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
2528c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
25291da177e4SLinus Torvalds  *	   locking].
25301da177e4SLinus Torvalds  */
253175c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
25321da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
25331da177e4SLinus Torvalds {
25341da177e4SLinus Torvalds 	int error = 0;
25351da177e4SLinus Torvalds 	struct inode *target;
25361da177e4SLinus Torvalds 
25371da177e4SLinus Torvalds 	/*
25381da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
25391da177e4SLinus Torvalds 	 * we'll need to flip '..'.
25401da177e4SLinus Torvalds 	 */
25411da177e4SLinus Torvalds 	if (new_dir != old_dir) {
2542f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
25431da177e4SLinus Torvalds 		if (error)
25441da177e4SLinus Torvalds 			return error;
25451da177e4SLinus Torvalds 	}
25461da177e4SLinus Torvalds 
25471da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
25481da177e4SLinus Torvalds 	if (error)
25491da177e4SLinus Torvalds 		return error;
25501da177e4SLinus Torvalds 
25511da177e4SLinus Torvalds 	target = new_dentry->d_inode;
2552d83c49f3SAl Viro 	if (target)
25531b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
25541da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
25551da177e4SLinus Torvalds 		error = -EBUSY;
2556d83c49f3SAl Viro 	else {
2557d83c49f3SAl Viro 		if (target)
2558d83c49f3SAl Viro 			dentry_unhash(new_dentry);
25591da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2560d83c49f3SAl Viro 	}
25611da177e4SLinus Torvalds 	if (target) {
2562d83c49f3SAl Viro 		if (!error) {
25631da177e4SLinus Torvalds 			target->i_flags |= S_DEAD;
2564d83c49f3SAl Viro 			dont_mount(new_dentry);
2565d83c49f3SAl Viro 		}
25661b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
25671da177e4SLinus Torvalds 		if (d_unhashed(new_dentry))
25681da177e4SLinus Torvalds 			d_rehash(new_dentry);
25691da177e4SLinus Torvalds 		dput(new_dentry);
25701da177e4SLinus Torvalds 	}
2571e31e14ecSStephen Smalley 	if (!error)
2572349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
25731da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
25741da177e4SLinus Torvalds 	return error;
25751da177e4SLinus Torvalds }
25761da177e4SLinus Torvalds 
257775c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
25781da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
25791da177e4SLinus Torvalds {
25801da177e4SLinus Torvalds 	struct inode *target;
25811da177e4SLinus Torvalds 	int error;
25821da177e4SLinus Torvalds 
25831da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
25841da177e4SLinus Torvalds 	if (error)
25851da177e4SLinus Torvalds 		return error;
25861da177e4SLinus Torvalds 
25871da177e4SLinus Torvalds 	dget(new_dentry);
25881da177e4SLinus Torvalds 	target = new_dentry->d_inode;
25891da177e4SLinus Torvalds 	if (target)
25901b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
25911da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
25921da177e4SLinus Torvalds 		error = -EBUSY;
25931da177e4SLinus Torvalds 	else
25941da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
25951da177e4SLinus Torvalds 	if (!error) {
2596bec1052eSAl Viro 		if (target)
2597d83c49f3SAl Viro 			dont_mount(new_dentry);
2598349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
25991da177e4SLinus Torvalds 			d_move(old_dentry, new_dentry);
26001da177e4SLinus Torvalds 	}
26011da177e4SLinus Torvalds 	if (target)
26021b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
26031da177e4SLinus Torvalds 	dput(new_dentry);
26041da177e4SLinus Torvalds 	return error;
26051da177e4SLinus Torvalds }
26061da177e4SLinus Torvalds 
26071da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
26081da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
26091da177e4SLinus Torvalds {
26101da177e4SLinus Torvalds 	int error;
26111da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
261259b0df21SEric Paris 	const unsigned char *old_name;
26131da177e4SLinus Torvalds 
26141da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
26151da177e4SLinus Torvalds  		return 0;
26161da177e4SLinus Torvalds 
26171da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
26181da177e4SLinus Torvalds 	if (error)
26191da177e4SLinus Torvalds 		return error;
26201da177e4SLinus Torvalds 
26211da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
2622a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
26231da177e4SLinus Torvalds 	else
26241da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
26251da177e4SLinus Torvalds 	if (error)
26261da177e4SLinus Torvalds 		return error;
26271da177e4SLinus Torvalds 
2628acfa4380SAl Viro 	if (!old_dir->i_op->rename)
26291da177e4SLinus Torvalds 		return -EPERM;
26301da177e4SLinus Torvalds 
26310eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
26320eeca283SRobert Love 
26331da177e4SLinus Torvalds 	if (is_dir)
26341da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
26351da177e4SLinus Torvalds 	else
26361da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2637123df294SAl Viro 	if (!error)
2638123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
26395a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
26400eeca283SRobert Love 	fsnotify_oldname_free(old_name);
26410eeca283SRobert Love 
26421da177e4SLinus Torvalds 	return error;
26431da177e4SLinus Torvalds }
26441da177e4SLinus Torvalds 
26452e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
26462e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
26471da177e4SLinus Torvalds {
26481da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
26491da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
26501da177e4SLinus Torvalds 	struct dentry *trap;
26511da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
26522ad94ae6SAl Viro 	char *from;
26532ad94ae6SAl Viro 	char *to;
26542ad94ae6SAl Viro 	int error;
26551da177e4SLinus Torvalds 
26562ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
26571da177e4SLinus Torvalds 	if (error)
26581da177e4SLinus Torvalds 		goto exit;
26591da177e4SLinus Torvalds 
26602ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
26611da177e4SLinus Torvalds 	if (error)
26621da177e4SLinus Torvalds 		goto exit1;
26631da177e4SLinus Torvalds 
26641da177e4SLinus Torvalds 	error = -EXDEV;
26654ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
26661da177e4SLinus Torvalds 		goto exit2;
26671da177e4SLinus Torvalds 
26684ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
26691da177e4SLinus Torvalds 	error = -EBUSY;
26701da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
26711da177e4SLinus Torvalds 		goto exit2;
26721da177e4SLinus Torvalds 
26734ac91378SJan Blunck 	new_dir = newnd.path.dentry;
26741da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
26751da177e4SLinus Torvalds 		goto exit2;
26761da177e4SLinus Torvalds 
26770612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
26780612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
26794e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
26800612d9fbSOGAWA Hirofumi 
26811da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
26821da177e4SLinus Torvalds 
268349705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
26841da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
26851da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
26861da177e4SLinus Torvalds 		goto exit3;
26871da177e4SLinus Torvalds 	/* source must exist */
26881da177e4SLinus Torvalds 	error = -ENOENT;
26891da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
26901da177e4SLinus Torvalds 		goto exit4;
26911da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
26921da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
26931da177e4SLinus Torvalds 		error = -ENOTDIR;
26941da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
26951da177e4SLinus Torvalds 			goto exit4;
26961da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
26971da177e4SLinus Torvalds 			goto exit4;
26981da177e4SLinus Torvalds 	}
26991da177e4SLinus Torvalds 	/* source should not be ancestor of target */
27001da177e4SLinus Torvalds 	error = -EINVAL;
27011da177e4SLinus Torvalds 	if (old_dentry == trap)
27021da177e4SLinus Torvalds 		goto exit4;
270349705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
27041da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
27051da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
27061da177e4SLinus Torvalds 		goto exit4;
27071da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
27081da177e4SLinus Torvalds 	error = -ENOTEMPTY;
27091da177e4SLinus Torvalds 	if (new_dentry == trap)
27101da177e4SLinus Torvalds 		goto exit5;
27111da177e4SLinus Torvalds 
27129079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
27139079b1ebSDave Hansen 	if (error)
27149079b1ebSDave Hansen 		goto exit5;
2715be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
2716be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
2717be6d3e56SKentaro Takeda 	if (error)
2718be6d3e56SKentaro Takeda 		goto exit6;
27191da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
27201da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
2721be6d3e56SKentaro Takeda exit6:
27229079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
27231da177e4SLinus Torvalds exit5:
27241da177e4SLinus Torvalds 	dput(new_dentry);
27251da177e4SLinus Torvalds exit4:
27261da177e4SLinus Torvalds 	dput(old_dentry);
27271da177e4SLinus Torvalds exit3:
27281da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
27291da177e4SLinus Torvalds exit2:
27301d957f9bSJan Blunck 	path_put(&newnd.path);
27312ad94ae6SAl Viro 	putname(to);
27321da177e4SLinus Torvalds exit1:
27331d957f9bSJan Blunck 	path_put(&oldnd.path);
27341da177e4SLinus Torvalds 	putname(from);
27352ad94ae6SAl Viro exit:
27361da177e4SLinus Torvalds 	return error;
27371da177e4SLinus Torvalds }
27381da177e4SLinus Torvalds 
2739a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
27405590ff0dSUlrich Drepper {
27415590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
27425590ff0dSUlrich Drepper }
27435590ff0dSUlrich Drepper 
27441da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
27451da177e4SLinus Torvalds {
27461da177e4SLinus Torvalds 	int len;
27471da177e4SLinus Torvalds 
27481da177e4SLinus Torvalds 	len = PTR_ERR(link);
27491da177e4SLinus Torvalds 	if (IS_ERR(link))
27501da177e4SLinus Torvalds 		goto out;
27511da177e4SLinus Torvalds 
27521da177e4SLinus Torvalds 	len = strlen(link);
27531da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
27541da177e4SLinus Torvalds 		len = buflen;
27551da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
27561da177e4SLinus Torvalds 		len = -EFAULT;
27571da177e4SLinus Torvalds out:
27581da177e4SLinus Torvalds 	return len;
27591da177e4SLinus Torvalds }
27601da177e4SLinus Torvalds 
27611da177e4SLinus Torvalds /*
27621da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
27631da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
27641da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
27651da177e4SLinus Torvalds  */
27661da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
27671da177e4SLinus Torvalds {
27681da177e4SLinus Torvalds 	struct nameidata nd;
2769cc314eefSLinus Torvalds 	void *cookie;
2770694a1764SMarcin Slusarz 	int res;
2771cc314eefSLinus Torvalds 
27721da177e4SLinus Torvalds 	nd.depth = 0;
2773cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
2774694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
2775694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
2776694a1764SMarcin Slusarz 
2777694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
27781da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
2779cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2780694a1764SMarcin Slusarz 	return res;
27811da177e4SLinus Torvalds }
27821da177e4SLinus Torvalds 
27831da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
27841da177e4SLinus Torvalds {
27851da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
27861da177e4SLinus Torvalds }
27871da177e4SLinus Torvalds 
27881da177e4SLinus Torvalds /* get the link contents into pagecache */
27891da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
27901da177e4SLinus Torvalds {
2791ebd09abbSDuane Griffin 	char *kaddr;
27921da177e4SLinus Torvalds 	struct page *page;
27931da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
2794090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
27951da177e4SLinus Torvalds 	if (IS_ERR(page))
27966fe6900eSNick Piggin 		return (char*)page;
27971da177e4SLinus Torvalds 	*ppage = page;
2798ebd09abbSDuane Griffin 	kaddr = kmap(page);
2799ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
2800ebd09abbSDuane Griffin 	return kaddr;
28011da177e4SLinus Torvalds }
28021da177e4SLinus Torvalds 
28031da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
28041da177e4SLinus Torvalds {
28051da177e4SLinus Torvalds 	struct page *page = NULL;
28061da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
28071da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
28081da177e4SLinus Torvalds 	if (page) {
28091da177e4SLinus Torvalds 		kunmap(page);
28101da177e4SLinus Torvalds 		page_cache_release(page);
28111da177e4SLinus Torvalds 	}
28121da177e4SLinus Torvalds 	return res;
28131da177e4SLinus Torvalds }
28141da177e4SLinus Torvalds 
2815cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
28161da177e4SLinus Torvalds {
2817cc314eefSLinus Torvalds 	struct page *page = NULL;
28181da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
2819cc314eefSLinus Torvalds 	return page;
28201da177e4SLinus Torvalds }
28211da177e4SLinus Torvalds 
2822cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
28231da177e4SLinus Torvalds {
2824cc314eefSLinus Torvalds 	struct page *page = cookie;
2825cc314eefSLinus Torvalds 
2826cc314eefSLinus Torvalds 	if (page) {
28271da177e4SLinus Torvalds 		kunmap(page);
28281da177e4SLinus Torvalds 		page_cache_release(page);
28291da177e4SLinus Torvalds 	}
28301da177e4SLinus Torvalds }
28311da177e4SLinus Torvalds 
283254566b2cSNick Piggin /*
283354566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
283454566b2cSNick Piggin  */
283554566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
28361da177e4SLinus Torvalds {
28371da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
28380adb25d2SKirill Korotaev 	struct page *page;
2839afddba49SNick Piggin 	void *fsdata;
2840beb497abSDmitriy Monakhov 	int err;
28411da177e4SLinus Torvalds 	char *kaddr;
284254566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
284354566b2cSNick Piggin 	if (nofs)
284454566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
28451da177e4SLinus Torvalds 
28467e53cac4SNeilBrown retry:
2847afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
284854566b2cSNick Piggin 				flags, &page, &fsdata);
28491da177e4SLinus Torvalds 	if (err)
2850afddba49SNick Piggin 		goto fail;
2851afddba49SNick Piggin 
28521da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
28531da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
28541da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
2855afddba49SNick Piggin 
2856afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
2857afddba49SNick Piggin 							page, fsdata);
28581da177e4SLinus Torvalds 	if (err < 0)
28591da177e4SLinus Torvalds 		goto fail;
2860afddba49SNick Piggin 	if (err < len-1)
2861afddba49SNick Piggin 		goto retry;
2862afddba49SNick Piggin 
28631da177e4SLinus Torvalds 	mark_inode_dirty(inode);
28641da177e4SLinus Torvalds 	return 0;
28651da177e4SLinus Torvalds fail:
28661da177e4SLinus Torvalds 	return err;
28671da177e4SLinus Torvalds }
28681da177e4SLinus Torvalds 
28690adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
28700adb25d2SKirill Korotaev {
28710adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
287254566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
28730adb25d2SKirill Korotaev }
28740adb25d2SKirill Korotaev 
287592e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
28761da177e4SLinus Torvalds 	.readlink	= generic_readlink,
28771da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
28781da177e4SLinus Torvalds 	.put_link	= page_put_link,
28791da177e4SLinus Torvalds };
28801da177e4SLinus Torvalds 
28812d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
28821da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
28831da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
28841da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
28851da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
28861da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
28871da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
28881da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
28891da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
28901da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
28910adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
28921da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
28931da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
28941da177e4SLinus Torvalds EXPORT_SYMBOL(path_lookup);
2895d1811465SAl Viro EXPORT_SYMBOL(kern_path);
289616f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
2897f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
28988c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission);
28991da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
29001da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
29011da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
29021da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
29031da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
29041da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
29051da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
29061da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
29071da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
29081da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
29091da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
29101da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
29111da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
29121da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
2913