xref: /openbmc/linux/fs/namei.c (revision 8737c930)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/namei.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  * Some corrections by tytso.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
121da177e4SLinus Torvalds  * lookup logic.
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/init.h>
181da177e4SLinus Torvalds #include <linux/module.h>
191da177e4SLinus Torvalds #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/fs.h>
211da177e4SLinus Torvalds #include <linux/namei.h>
221da177e4SLinus Torvalds #include <linux/quotaops.h>
231da177e4SLinus Torvalds #include <linux/pagemap.h>
240eeca283SRobert Love #include <linux/fsnotify.h>
251da177e4SLinus Torvalds #include <linux/personality.h>
261da177e4SLinus Torvalds #include <linux/security.h>
276146f0d5SMimi Zohar #include <linux/ima.h>
281da177e4SLinus Torvalds #include <linux/syscalls.h>
291da177e4SLinus Torvalds #include <linux/mount.h>
301da177e4SLinus Torvalds #include <linux/audit.h>
3116f7e0feSRandy Dunlap #include <linux/capability.h>
32834f2a4aSTrond Myklebust #include <linux/file.h>
335590ff0dSUlrich Drepper #include <linux/fcntl.h>
3408ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h>
355ad4e53bSAl Viro #include <linux/fs_struct.h>
361da177e4SLinus Torvalds #include <asm/uaccess.h>
371da177e4SLinus Torvalds 
38e81e3f4dSEric Paris #include "internal.h"
39e81e3f4dSEric Paris 
401da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer]
411da177e4SLinus Torvalds  * Fundamental changes in the pathname lookup mechanisms (namei)
421da177e4SLinus Torvalds  * were necessary because of omirr.  The reason is that omirr needs
431da177e4SLinus Torvalds  * to know the _real_ pathname, not the user-supplied one, in case
441da177e4SLinus Torvalds  * of symlinks (and also when transname replacements occur).
451da177e4SLinus Torvalds  *
461da177e4SLinus Torvalds  * The new code replaces the old recursive symlink resolution with
471da177e4SLinus Torvalds  * an iterative one (in case of non-nested symlink chains).  It does
481da177e4SLinus Torvalds  * this with calls to <fs>_follow_link().
491da177e4SLinus Torvalds  * As a side effect, dir_namei(), _namei() and follow_link() are now
501da177e4SLinus Torvalds  * replaced with a single function lookup_dentry() that can handle all
511da177e4SLinus Torvalds  * the special cases of the former code.
521da177e4SLinus Torvalds  *
531da177e4SLinus Torvalds  * With the new dcache, the pathname is stored at each inode, at least as
541da177e4SLinus Torvalds  * long as the refcount of the inode is positive.  As a side effect, the
551da177e4SLinus Torvalds  * size of the dcache depends on the inode cache and thus is dynamic.
561da177e4SLinus Torvalds  *
571da177e4SLinus Torvalds  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
581da177e4SLinus Torvalds  * resolution to correspond with current state of the code.
591da177e4SLinus Torvalds  *
601da177e4SLinus Torvalds  * Note that the symlink resolution is not *completely* iterative.
611da177e4SLinus Torvalds  * There is still a significant amount of tail- and mid- recursion in
621da177e4SLinus Torvalds  * the algorithm.  Also, note that <fs>_readlink() is not used in
631da177e4SLinus Torvalds  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
641da177e4SLinus Torvalds  * may return different results than <fs>_follow_link().  Many virtual
651da177e4SLinus Torvalds  * filesystems (including /proc) exhibit this behavior.
661da177e4SLinus Torvalds  */
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
691da177e4SLinus Torvalds  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
701da177e4SLinus Torvalds  * and the name already exists in form of a symlink, try to create the new
711da177e4SLinus Torvalds  * name indicated by the symlink. The old code always complained that the
721da177e4SLinus Torvalds  * name already exists, due to not following the symlink even if its target
731da177e4SLinus Torvalds  * is nonexistent.  The new semantics affects also mknod() and link() when
741da177e4SLinus Torvalds  * the name is a symlink pointing to a non-existant name.
751da177e4SLinus Torvalds  *
761da177e4SLinus Torvalds  * I don't know which semantics is the right one, since I have no access
771da177e4SLinus Torvalds  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
781da177e4SLinus Torvalds  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
791da177e4SLinus Torvalds  * "old" one. Personally, I think the new semantics is much more logical.
801da177e4SLinus Torvalds  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
811da177e4SLinus Torvalds  * file does succeed in both HP-UX and SunOs, but not in Solaris
821da177e4SLinus Torvalds  * and in the old Linux semantics.
831da177e4SLinus Torvalds  */
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
861da177e4SLinus Torvalds  * semantics.  See the comments in "open_namei" and "do_link" below.
871da177e4SLinus Torvalds  *
881da177e4SLinus Torvalds  * [10-Sep-98 Alan Modra] Another symlink change.
891da177e4SLinus Torvalds  */
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
921da177e4SLinus Torvalds  *	inside the path - always follow.
931da177e4SLinus Torvalds  *	in the last component in creation/removal/renaming - never follow.
941da177e4SLinus Torvalds  *	if LOOKUP_FOLLOW passed - follow.
951da177e4SLinus Torvalds  *	if the pathname has trailing slashes - follow.
961da177e4SLinus Torvalds  *	otherwise - don't follow.
971da177e4SLinus Torvalds  * (applied in that order).
981da177e4SLinus Torvalds  *
991da177e4SLinus Torvalds  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
1001da177e4SLinus Torvalds  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
1011da177e4SLinus Torvalds  * During the 2.4 we need to fix the userland stuff depending on it -
1021da177e4SLinus Torvalds  * hopefully we will be able to get rid of that wart in 2.5. So far only
1031da177e4SLinus Torvalds  * XEmacs seems to be relying on it...
1041da177e4SLinus Torvalds  */
1051da177e4SLinus Torvalds /*
1061da177e4SLinus Torvalds  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
107a11f3a05SArjan van de Ven  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
1081da177e4SLinus Torvalds  * any extra contention...
1091da177e4SLinus Torvalds  */
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional
1121da177e4SLinus Torvalds  * checking and hopefully speeding things up, we copy filenames to the
1131da177e4SLinus Torvalds  * kernel data space before using them..
1141da177e4SLinus Torvalds  *
1151da177e4SLinus Torvalds  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
1161da177e4SLinus Torvalds  * PATH_MAX includes the nul terminator --RR.
1171da177e4SLinus Torvalds  */
118858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page)
1191da177e4SLinus Torvalds {
1201da177e4SLinus Torvalds 	int retval;
1211da177e4SLinus Torvalds 	unsigned long len = PATH_MAX;
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	if (!segment_eq(get_fs(), KERNEL_DS)) {
1241da177e4SLinus Torvalds 		if ((unsigned long) filename >= TASK_SIZE)
1251da177e4SLinus Torvalds 			return -EFAULT;
1261da177e4SLinus Torvalds 		if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
1271da177e4SLinus Torvalds 			len = TASK_SIZE - (unsigned long) filename;
1281da177e4SLinus Torvalds 	}
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	retval = strncpy_from_user(page, filename, len);
1311da177e4SLinus Torvalds 	if (retval > 0) {
1321da177e4SLinus Torvalds 		if (retval < len)
1331da177e4SLinus Torvalds 			return 0;
1341da177e4SLinus Torvalds 		return -ENAMETOOLONG;
1351da177e4SLinus Torvalds 	} else if (!retval)
1361da177e4SLinus Torvalds 		retval = -ENOENT;
1371da177e4SLinus Torvalds 	return retval;
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds char * getname(const char __user * filename)
1411da177e4SLinus Torvalds {
1421da177e4SLinus Torvalds 	char *tmp, *result;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	result = ERR_PTR(-ENOMEM);
1451da177e4SLinus Torvalds 	tmp = __getname();
1461da177e4SLinus Torvalds 	if (tmp)  {
1471da177e4SLinus Torvalds 		int retval = do_getname(filename, tmp);
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 		result = tmp;
1501da177e4SLinus Torvalds 		if (retval < 0) {
1511da177e4SLinus Torvalds 			__putname(tmp);
1521da177e4SLinus Torvalds 			result = ERR_PTR(retval);
1531da177e4SLinus Torvalds 		}
1541da177e4SLinus Torvalds 	}
1551da177e4SLinus Torvalds 	audit_getname(result);
1561da177e4SLinus Torvalds 	return result;
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
1601da177e4SLinus Torvalds void putname(const char *name)
1611da177e4SLinus Torvalds {
1625ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
1631da177e4SLinus Torvalds 		audit_putname(name);
1641da177e4SLinus Torvalds 	else
1651da177e4SLinus Torvalds 		__putname(name);
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds EXPORT_SYMBOL(putname);
1681da177e4SLinus Torvalds #endif
1691da177e4SLinus Torvalds 
1705909ccaaSLinus Torvalds /*
1715909ccaaSLinus Torvalds  * This does basic POSIX ACL permission checking
1725909ccaaSLinus Torvalds  */
1735909ccaaSLinus Torvalds static int acl_permission_check(struct inode *inode, int mask,
1745909ccaaSLinus Torvalds 		int (*check_acl)(struct inode *inode, int mask))
1755909ccaaSLinus Torvalds {
1765909ccaaSLinus Torvalds 	umode_t			mode = inode->i_mode;
1775909ccaaSLinus Torvalds 
1785909ccaaSLinus Torvalds 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
1795909ccaaSLinus Torvalds 
1805909ccaaSLinus Torvalds 	if (current_fsuid() == inode->i_uid)
1815909ccaaSLinus Torvalds 		mode >>= 6;
1825909ccaaSLinus Torvalds 	else {
1835909ccaaSLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
1845909ccaaSLinus Torvalds 			int error = check_acl(inode, mask);
1855909ccaaSLinus Torvalds 			if (error != -EAGAIN)
1865909ccaaSLinus Torvalds 				return error;
1875909ccaaSLinus Torvalds 		}
1885909ccaaSLinus Torvalds 
1895909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
1905909ccaaSLinus Torvalds 			mode >>= 3;
1915909ccaaSLinus Torvalds 	}
1925909ccaaSLinus Torvalds 
1935909ccaaSLinus Torvalds 	/*
1945909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
1955909ccaaSLinus Torvalds 	 */
1965909ccaaSLinus Torvalds 	if ((mask & ~mode) == 0)
1975909ccaaSLinus Torvalds 		return 0;
1985909ccaaSLinus Torvalds 	return -EACCES;
1995909ccaaSLinus Torvalds }
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds /**
2021da177e4SLinus Torvalds  * generic_permission  -  check for access rights on a Posix-like filesystem
2031da177e4SLinus Torvalds  * @inode:	inode to check access rights for
2041da177e4SLinus Torvalds  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
2051da177e4SLinus Torvalds  * @check_acl:	optional callback to check for Posix ACLs
2061da177e4SLinus Torvalds  *
2071da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
2081da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
2091da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
2101da177e4SLinus Torvalds  * are used for other things..
2111da177e4SLinus Torvalds  */
2121da177e4SLinus Torvalds int generic_permission(struct inode *inode, int mask,
2131da177e4SLinus Torvalds 		int (*check_acl)(struct inode *inode, int mask))
2141da177e4SLinus Torvalds {
2155909ccaaSLinus Torvalds 	int ret;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	/*
2185909ccaaSLinus Torvalds 	 * Do the basic POSIX ACL permission checks.
2191da177e4SLinus Torvalds 	 */
2205909ccaaSLinus Torvalds 	ret = acl_permission_check(inode, mask, check_acl);
2215909ccaaSLinus Torvalds 	if (ret != -EACCES)
2225909ccaaSLinus Torvalds 		return ret;
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	/*
2251da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
2261da177e4SLinus Torvalds 	 * Executable DACs are overridable if at least one exec bit is set.
2271da177e4SLinus Torvalds 	 */
228f696a365SMiklos Szeredi 	if (!(mask & MAY_EXEC) || execute_ok(inode))
2291da177e4SLinus Torvalds 		if (capable(CAP_DAC_OVERRIDE))
2301da177e4SLinus Torvalds 			return 0;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	/*
2331da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
2341da177e4SLinus Torvalds 	 */
2357ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
2361da177e4SLinus Torvalds 	if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
2371da177e4SLinus Torvalds 		if (capable(CAP_DAC_READ_SEARCH))
2381da177e4SLinus Torvalds 			return 0;
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	return -EACCES;
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds 
243cb23beb5SChristoph Hellwig /**
244cb23beb5SChristoph Hellwig  * inode_permission  -  check for access rights to a given inode
245cb23beb5SChristoph Hellwig  * @inode:	inode to check permission on
246cb23beb5SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
247cb23beb5SChristoph Hellwig  *
248cb23beb5SChristoph Hellwig  * Used to check for read/write/execute permissions on an inode.
249cb23beb5SChristoph Hellwig  * We use "fsuid" for this, letting us set arbitrary permissions
250cb23beb5SChristoph Hellwig  * for filesystem access without changing the "normal" uids which
251cb23beb5SChristoph Hellwig  * are used for other things.
252cb23beb5SChristoph Hellwig  */
253f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask)
2541da177e4SLinus Torvalds {
255e6305c43SAl Viro 	int retval;
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 	if (mask & MAY_WRITE) {
25822590e41SMiklos Szeredi 		umode_t mode = inode->i_mode;
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 		/*
2611da177e4SLinus Torvalds 		 * Nobody gets write access to a read-only fs.
2621da177e4SLinus Torvalds 		 */
2631da177e4SLinus Torvalds 		if (IS_RDONLY(inode) &&
2641da177e4SLinus Torvalds 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
2651da177e4SLinus Torvalds 			return -EROFS;
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 		/*
2681da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
2691da177e4SLinus Torvalds 		 */
2701da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
2711da177e4SLinus Torvalds 			return -EACCES;
2721da177e4SLinus Torvalds 	}
2731da177e4SLinus Torvalds 
274acfa4380SAl Viro 	if (inode->i_op->permission)
275b77b0646SAl Viro 		retval = inode->i_op->permission(inode, mask);
276f696a365SMiklos Szeredi 	else
2775909ccaaSLinus Torvalds 		retval = generic_permission(inode, mask, inode->i_op->check_acl);
278f696a365SMiklos Szeredi 
2791da177e4SLinus Torvalds 	if (retval)
2801da177e4SLinus Torvalds 		return retval;
2811da177e4SLinus Torvalds 
28208ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
28308ce5f16SSerge E. Hallyn 	if (retval)
28408ce5f16SSerge E. Hallyn 		return retval;
28508ce5f16SSerge E. Hallyn 
286e6305c43SAl Viro 	return security_inode_permission(inode,
287f418b006SStephen Smalley 			mask & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND));
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds 
290e4543eddSChristoph Hellwig /**
2918c744fb8SChristoph Hellwig  * file_permission  -  check for additional access rights to a given file
2928c744fb8SChristoph Hellwig  * @file:	file to check access rights for
2938c744fb8SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
2948c744fb8SChristoph Hellwig  *
2958c744fb8SChristoph Hellwig  * Used to check for read/write/execute permissions on an already opened
2968c744fb8SChristoph Hellwig  * file.
2978c744fb8SChristoph Hellwig  *
2988c744fb8SChristoph Hellwig  * Note:
2998c744fb8SChristoph Hellwig  *	Do not use this function in new code.  All access checks should
300cb23beb5SChristoph Hellwig  *	be done using inode_permission().
3018c744fb8SChristoph Hellwig  */
3028c744fb8SChristoph Hellwig int file_permission(struct file *file, int mask)
3038c744fb8SChristoph Hellwig {
304f419a2e3SAl Viro 	return inode_permission(file->f_path.dentry->d_inode, mask);
3058c744fb8SChristoph Hellwig }
3068c744fb8SChristoph Hellwig 
3071da177e4SLinus Torvalds /*
3081da177e4SLinus Torvalds  * get_write_access() gets write permission for a file.
3091da177e4SLinus Torvalds  * put_write_access() releases this write permission.
3101da177e4SLinus Torvalds  * This is used for regular files.
3111da177e4SLinus Torvalds  * We cannot support write (and maybe mmap read-write shared) accesses and
3121da177e4SLinus Torvalds  * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
3131da177e4SLinus Torvalds  * can have the following values:
3141da177e4SLinus Torvalds  * 0: no writers, no VM_DENYWRITE mappings
3151da177e4SLinus Torvalds  * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
3161da177e4SLinus Torvalds  * > 0: (i_writecount) users are writing to the file.
3171da177e4SLinus Torvalds  *
3181da177e4SLinus Torvalds  * Normally we operate on that counter with atomic_{inc,dec} and it's safe
3191da177e4SLinus Torvalds  * except for the cases where we don't hold i_writecount yet. Then we need to
3201da177e4SLinus Torvalds  * use {get,deny}_write_access() - these functions check the sign and refuse
3211da177e4SLinus Torvalds  * to do the change if sign is wrong. Exclusion between them is provided by
3221da177e4SLinus Torvalds  * the inode->i_lock spinlock.
3231da177e4SLinus Torvalds  */
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds int get_write_access(struct inode * inode)
3261da177e4SLinus Torvalds {
3271da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
3281da177e4SLinus Torvalds 	if (atomic_read(&inode->i_writecount) < 0) {
3291da177e4SLinus Torvalds 		spin_unlock(&inode->i_lock);
3301da177e4SLinus Torvalds 		return -ETXTBSY;
3311da177e4SLinus Torvalds 	}
3321da177e4SLinus Torvalds 	atomic_inc(&inode->i_writecount);
3331da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	return 0;
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds int deny_write_access(struct file * file)
3391da177e4SLinus Torvalds {
3400f7fc9e4SJosef "Jeff" Sipek 	struct inode *inode = file->f_path.dentry->d_inode;
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
3431da177e4SLinus Torvalds 	if (atomic_read(&inode->i_writecount) > 0) {
3441da177e4SLinus Torvalds 		spin_unlock(&inode->i_lock);
3451da177e4SLinus Torvalds 		return -ETXTBSY;
3461da177e4SLinus Torvalds 	}
3471da177e4SLinus Torvalds 	atomic_dec(&inode->i_writecount);
3481da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	return 0;
3511da177e4SLinus Torvalds }
3521da177e4SLinus Torvalds 
3531d957f9bSJan Blunck /**
3545dd784d0SJan Blunck  * path_get - get a reference to a path
3555dd784d0SJan Blunck  * @path: path to get the reference to
3565dd784d0SJan Blunck  *
3575dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
3585dd784d0SJan Blunck  */
3595dd784d0SJan Blunck void path_get(struct path *path)
3605dd784d0SJan Blunck {
3615dd784d0SJan Blunck 	mntget(path->mnt);
3625dd784d0SJan Blunck 	dget(path->dentry);
3635dd784d0SJan Blunck }
3645dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
3655dd784d0SJan Blunck 
3665dd784d0SJan Blunck /**
3671d957f9bSJan Blunck  * path_put - put a reference to a path
3681d957f9bSJan Blunck  * @path: path to put the reference to
3691d957f9bSJan Blunck  *
3701d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
3711d957f9bSJan Blunck  */
3721d957f9bSJan Blunck void path_put(struct path *path)
3731da177e4SLinus Torvalds {
3741d957f9bSJan Blunck 	dput(path->dentry);
3751d957f9bSJan Blunck 	mntput(path->mnt);
3761da177e4SLinus Torvalds }
3771d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
3781da177e4SLinus Torvalds 
379834f2a4aSTrond Myklebust /**
380834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
381834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
382834f2a4aSTrond Myklebust  */
383834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
384834f2a4aSTrond Myklebust {
3850f7fc9e4SJosef "Jeff" Sipek 	if (nd->intent.open.file->f_path.dentry == NULL)
386834f2a4aSTrond Myklebust 		put_filp(nd->intent.open.file);
387834f2a4aSTrond Myklebust 	else
388834f2a4aSTrond Myklebust 		fput(nd->intent.open.file);
389834f2a4aSTrond Myklebust }
390834f2a4aSTrond Myklebust 
391bcdc5e01SIan Kent static inline struct dentry *
392bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd)
393bcdc5e01SIan Kent {
394bcdc5e01SIan Kent 	int status = dentry->d_op->d_revalidate(dentry, nd);
395bcdc5e01SIan Kent 	if (unlikely(status <= 0)) {
396bcdc5e01SIan Kent 		/*
397bcdc5e01SIan Kent 		 * The dentry failed validation.
398bcdc5e01SIan Kent 		 * If d_revalidate returned 0 attempt to invalidate
399bcdc5e01SIan Kent 		 * the dentry otherwise d_revalidate is asking us
400bcdc5e01SIan Kent 		 * to return a fail status.
401bcdc5e01SIan Kent 		 */
402bcdc5e01SIan Kent 		if (!status) {
403bcdc5e01SIan Kent 			if (!d_invalidate(dentry)) {
404bcdc5e01SIan Kent 				dput(dentry);
405bcdc5e01SIan Kent 				dentry = NULL;
406bcdc5e01SIan Kent 			}
407bcdc5e01SIan Kent 		} else {
408bcdc5e01SIan Kent 			dput(dentry);
409bcdc5e01SIan Kent 			dentry = ERR_PTR(status);
410bcdc5e01SIan Kent 		}
411bcdc5e01SIan Kent 	}
412bcdc5e01SIan Kent 	return dentry;
413bcdc5e01SIan Kent }
414bcdc5e01SIan Kent 
4151da177e4SLinus Torvalds /*
41639159de2SJeff Layton  * force_reval_path - force revalidation of a dentry
41739159de2SJeff Layton  *
41839159de2SJeff Layton  * In some situations the path walking code will trust dentries without
41939159de2SJeff Layton  * revalidating them. This causes problems for filesystems that depend on
42039159de2SJeff Layton  * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
42139159de2SJeff Layton  * (which indicates that it's possible for the dentry to go stale), force
42239159de2SJeff Layton  * a d_revalidate call before proceeding.
42339159de2SJeff Layton  *
42439159de2SJeff Layton  * Returns 0 if the revalidation was successful. If the revalidation fails,
42539159de2SJeff Layton  * either return the error returned by d_revalidate or -ESTALE if the
42639159de2SJeff Layton  * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
42739159de2SJeff Layton  * invalidate the dentry. It's up to the caller to handle putting references
42839159de2SJeff Layton  * to the path if necessary.
42939159de2SJeff Layton  */
43039159de2SJeff Layton static int
43139159de2SJeff Layton force_reval_path(struct path *path, struct nameidata *nd)
43239159de2SJeff Layton {
43339159de2SJeff Layton 	int status;
43439159de2SJeff Layton 	struct dentry *dentry = path->dentry;
43539159de2SJeff Layton 
43639159de2SJeff Layton 	/*
43739159de2SJeff Layton 	 * only check on filesystems where it's possible for the dentry to
43839159de2SJeff Layton 	 * become stale. It's assumed that if this flag is set then the
43939159de2SJeff Layton 	 * d_revalidate op will also be defined.
44039159de2SJeff Layton 	 */
44139159de2SJeff Layton 	if (!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))
44239159de2SJeff Layton 		return 0;
44339159de2SJeff Layton 
44439159de2SJeff Layton 	status = dentry->d_op->d_revalidate(dentry, nd);
44539159de2SJeff Layton 	if (status > 0)
44639159de2SJeff Layton 		return 0;
44739159de2SJeff Layton 
44839159de2SJeff Layton 	if (!status) {
44939159de2SJeff Layton 		d_invalidate(dentry);
45039159de2SJeff Layton 		status = -ESTALE;
45139159de2SJeff Layton 	}
45239159de2SJeff Layton 	return status;
45339159de2SJeff Layton }
45439159de2SJeff Layton 
45539159de2SJeff Layton /*
456b75b5086SAl Viro  * Short-cut version of permission(), for calling on directories
457b75b5086SAl Viro  * during pathname resolution.  Combines parts of permission()
458b75b5086SAl Viro  * and generic_permission(), and tests ONLY for MAY_EXEC permission.
4591da177e4SLinus Torvalds  *
4601da177e4SLinus Torvalds  * If appropriate, check DAC only.  If not appropriate, or
461b75b5086SAl Viro  * short-cut DAC fails, then call ->permission() to do more
4621da177e4SLinus Torvalds  * complete permission check.
4631da177e4SLinus Torvalds  */
464b75b5086SAl Viro static int exec_permission(struct inode *inode)
4651da177e4SLinus Torvalds {
4665909ccaaSLinus Torvalds 	int ret;
4671da177e4SLinus Torvalds 
468cb9179eaSLinus Torvalds 	if (inode->i_op->permission) {
4695909ccaaSLinus Torvalds 		ret = inode->i_op->permission(inode, MAY_EXEC);
470cb9179eaSLinus Torvalds 		if (!ret)
471cb9179eaSLinus Torvalds 			goto ok;
472cb9179eaSLinus Torvalds 		return ret;
473cb9179eaSLinus Torvalds 	}
4745909ccaaSLinus Torvalds 	ret = acl_permission_check(inode, MAY_EXEC, inode->i_op->check_acl);
4755909ccaaSLinus Torvalds 	if (!ret)
4761da177e4SLinus Torvalds 		goto ok;
4771da177e4SLinus Torvalds 
478f1ac9f6bSLinus Torvalds 	if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH))
4791da177e4SLinus Torvalds 		goto ok;
4801da177e4SLinus Torvalds 
4815909ccaaSLinus Torvalds 	return ret;
4821da177e4SLinus Torvalds ok:
483b77b0646SAl Viro 	return security_inode_permission(inode, MAY_EXEC);
4841da177e4SLinus Torvalds }
4851da177e4SLinus Torvalds 
4862a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
4872a737871SAl Viro {
4882a737871SAl Viro 	if (!nd->root.mnt) {
4892a737871SAl Viro 		struct fs_struct *fs = current->fs;
4902a737871SAl Viro 		read_lock(&fs->lock);
4912a737871SAl Viro 		nd->root = fs->root;
4922a737871SAl Viro 		path_get(&nd->root);
4932a737871SAl Viro 		read_unlock(&fs->lock);
4942a737871SAl Viro 	}
4952a737871SAl Viro }
4962a737871SAl Viro 
4976de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
4986de88d72SAl Viro 
499f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
5001da177e4SLinus Torvalds {
5011da177e4SLinus Torvalds 	int res = 0;
5021da177e4SLinus Torvalds 	char *name;
5031da177e4SLinus Torvalds 	if (IS_ERR(link))
5041da177e4SLinus Torvalds 		goto fail;
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 	if (*link == '/') {
5072a737871SAl Viro 		set_root(nd);
5081d957f9bSJan Blunck 		path_put(&nd->path);
5092a737871SAl Viro 		nd->path = nd->root;
5102a737871SAl Viro 		path_get(&nd->root);
5111da177e4SLinus Torvalds 	}
512b4091d5fSChristoph Hellwig 
5131da177e4SLinus Torvalds 	res = link_path_walk(link, nd);
5141da177e4SLinus Torvalds 	if (nd->depth || res || nd->last_type!=LAST_NORM)
5151da177e4SLinus Torvalds 		return res;
5161da177e4SLinus Torvalds 	/*
5171da177e4SLinus Torvalds 	 * If it is an iterative symlinks resolution in open_namei() we
5181da177e4SLinus Torvalds 	 * have to copy the last component. And all that crap because of
5191da177e4SLinus Torvalds 	 * bloody create() on broken symlinks. Furrfu...
5201da177e4SLinus Torvalds 	 */
5211da177e4SLinus Torvalds 	name = __getname();
5221da177e4SLinus Torvalds 	if (unlikely(!name)) {
5231d957f9bSJan Blunck 		path_put(&nd->path);
5241da177e4SLinus Torvalds 		return -ENOMEM;
5251da177e4SLinus Torvalds 	}
5261da177e4SLinus Torvalds 	strcpy(name, nd->last.name);
5271da177e4SLinus Torvalds 	nd->last.name = name;
5281da177e4SLinus Torvalds 	return 0;
5291da177e4SLinus Torvalds fail:
5301d957f9bSJan Blunck 	path_put(&nd->path);
5311da177e4SLinus Torvalds 	return PTR_ERR(link);
5321da177e4SLinus Torvalds }
5331da177e4SLinus Torvalds 
5341d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
535051d3812SIan Kent {
536051d3812SIan Kent 	dput(path->dentry);
5374ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
538051d3812SIan Kent 		mntput(path->mnt);
539051d3812SIan Kent }
540051d3812SIan Kent 
541051d3812SIan Kent static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
542051d3812SIan Kent {
5434ac91378SJan Blunck 	dput(nd->path.dentry);
5444ac91378SJan Blunck 	if (nd->path.mnt != path->mnt)
5454ac91378SJan Blunck 		mntput(nd->path.mnt);
5464ac91378SJan Blunck 	nd->path.mnt = path->mnt;
5474ac91378SJan Blunck 	nd->path.dentry = path->dentry;
548051d3812SIan Kent }
549051d3812SIan Kent 
550f1662356SArjan van de Ven static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
5511da177e4SLinus Torvalds {
5521da177e4SLinus Torvalds 	int error;
553cc314eefSLinus Torvalds 	void *cookie;
554cd4e91d3SAl Viro 	struct dentry *dentry = path->dentry;
5551da177e4SLinus Torvalds 
556d671a1cbSAl Viro 	touch_atime(path->mnt, dentry);
5571da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
558cd4e91d3SAl Viro 
5594ac91378SJan Blunck 	if (path->mnt != nd->path.mnt) {
560051d3812SIan Kent 		path_to_nameidata(path, nd);
561051d3812SIan Kent 		dget(dentry);
562051d3812SIan Kent 	}
563cd4e91d3SAl Viro 	mntget(path->mnt);
56486acdca1SAl Viro 	nd->last_type = LAST_BIND;
565cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
566cc314eefSLinus Torvalds 	error = PTR_ERR(cookie);
567cc314eefSLinus Torvalds 	if (!IS_ERR(cookie)) {
5681da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
569cc314eefSLinus Torvalds 		error = 0;
5701da177e4SLinus Torvalds 		if (s)
5711da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
57239159de2SJeff Layton 		else if (nd->last_type == LAST_BIND) {
57339159de2SJeff Layton 			error = force_reval_path(&nd->path, nd);
57439159de2SJeff Layton 			if (error)
57539159de2SJeff Layton 				path_put(&nd->path);
57639159de2SJeff Layton 		}
5771da177e4SLinus Torvalds 		if (dentry->d_inode->i_op->put_link)
578cc314eefSLinus Torvalds 			dentry->d_inode->i_op->put_link(dentry, nd, cookie);
5791da177e4SLinus Torvalds 	}
5801da177e4SLinus Torvalds 	return error;
5811da177e4SLinus Torvalds }
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds /*
5841da177e4SLinus Torvalds  * This limits recursive symlink follows to 8, while
5851da177e4SLinus Torvalds  * limiting consecutive symlinks to 40.
5861da177e4SLinus Torvalds  *
5871da177e4SLinus Torvalds  * Without that kind of total limit, nasty chains of consecutive
5881da177e4SLinus Torvalds  * symlinks can cause almost arbitrarily long lookups.
5891da177e4SLinus Torvalds  */
59090ebe565SAl Viro static inline int do_follow_link(struct path *path, struct nameidata *nd)
5911da177e4SLinus Torvalds {
5921da177e4SLinus Torvalds 	int err = -ELOOP;
5931da177e4SLinus Torvalds 	if (current->link_count >= MAX_NESTED_LINKS)
5941da177e4SLinus Torvalds 		goto loop;
5951da177e4SLinus Torvalds 	if (current->total_link_count >= 40)
5961da177e4SLinus Torvalds 		goto loop;
5971da177e4SLinus Torvalds 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
5981da177e4SLinus Torvalds 	cond_resched();
59990ebe565SAl Viro 	err = security_inode_follow_link(path->dentry, nd);
6001da177e4SLinus Torvalds 	if (err)
6011da177e4SLinus Torvalds 		goto loop;
6021da177e4SLinus Torvalds 	current->link_count++;
6031da177e4SLinus Torvalds 	current->total_link_count++;
6041da177e4SLinus Torvalds 	nd->depth++;
605cd4e91d3SAl Viro 	err = __do_follow_link(path, nd);
606258fa999SAl Viro 	path_put(path);
6071da177e4SLinus Torvalds 	current->link_count--;
6081da177e4SLinus Torvalds 	nd->depth--;
6091da177e4SLinus Torvalds 	return err;
6101da177e4SLinus Torvalds loop:
6111d957f9bSJan Blunck 	path_put_conditional(path, nd);
6121d957f9bSJan Blunck 	path_put(&nd->path);
6131da177e4SLinus Torvalds 	return err;
6141da177e4SLinus Torvalds }
6151da177e4SLinus Torvalds 
616bab77ebfSAl Viro int follow_up(struct path *path)
6171da177e4SLinus Torvalds {
6181da177e4SLinus Torvalds 	struct vfsmount *parent;
6191da177e4SLinus Torvalds 	struct dentry *mountpoint;
6201da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
621bab77ebfSAl Viro 	parent = path->mnt->mnt_parent;
622bab77ebfSAl Viro 	if (parent == path->mnt) {
6231da177e4SLinus Torvalds 		spin_unlock(&vfsmount_lock);
6241da177e4SLinus Torvalds 		return 0;
6251da177e4SLinus Torvalds 	}
6261da177e4SLinus Torvalds 	mntget(parent);
627bab77ebfSAl Viro 	mountpoint = dget(path->mnt->mnt_mountpoint);
6281da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
629bab77ebfSAl Viro 	dput(path->dentry);
630bab77ebfSAl Viro 	path->dentry = mountpoint;
631bab77ebfSAl Viro 	mntput(path->mnt);
632bab77ebfSAl Viro 	path->mnt = parent;
6331da177e4SLinus Torvalds 	return 1;
6341da177e4SLinus Torvalds }
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds /* no need for dcache_lock, as serialization is taken care in
6371da177e4SLinus Torvalds  * namespace.c
6381da177e4SLinus Torvalds  */
639463ffb2eSAl Viro static int __follow_mount(struct path *path)
640463ffb2eSAl Viro {
641463ffb2eSAl Viro 	int res = 0;
642463ffb2eSAl Viro 	while (d_mountpoint(path->dentry)) {
6431c755af4SAl Viro 		struct vfsmount *mounted = lookup_mnt(path);
644463ffb2eSAl Viro 		if (!mounted)
645463ffb2eSAl Viro 			break;
646463ffb2eSAl Viro 		dput(path->dentry);
647463ffb2eSAl Viro 		if (res)
648463ffb2eSAl Viro 			mntput(path->mnt);
649463ffb2eSAl Viro 		path->mnt = mounted;
650463ffb2eSAl Viro 		path->dentry = dget(mounted->mnt_root);
651463ffb2eSAl Viro 		res = 1;
652463ffb2eSAl Viro 	}
653463ffb2eSAl Viro 	return res;
654463ffb2eSAl Viro }
655463ffb2eSAl Viro 
65679ed0226SAl Viro static void follow_mount(struct path *path)
6571da177e4SLinus Torvalds {
65879ed0226SAl Viro 	while (d_mountpoint(path->dentry)) {
6591c755af4SAl Viro 		struct vfsmount *mounted = lookup_mnt(path);
6601da177e4SLinus Torvalds 		if (!mounted)
6611da177e4SLinus Torvalds 			break;
66279ed0226SAl Viro 		dput(path->dentry);
66379ed0226SAl Viro 		mntput(path->mnt);
66479ed0226SAl Viro 		path->mnt = mounted;
66579ed0226SAl Viro 		path->dentry = dget(mounted->mnt_root);
6661da177e4SLinus Torvalds 	}
6671da177e4SLinus Torvalds }
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds /* no need for dcache_lock, as serialization is taken care in
6701da177e4SLinus Torvalds  * namespace.c
6711da177e4SLinus Torvalds  */
6729393bd07SAl Viro int follow_down(struct path *path)
6731da177e4SLinus Torvalds {
6741da177e4SLinus Torvalds 	struct vfsmount *mounted;
6751da177e4SLinus Torvalds 
6761c755af4SAl Viro 	mounted = lookup_mnt(path);
6771da177e4SLinus Torvalds 	if (mounted) {
6789393bd07SAl Viro 		dput(path->dentry);
6799393bd07SAl Viro 		mntput(path->mnt);
6809393bd07SAl Viro 		path->mnt = mounted;
6819393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
6821da177e4SLinus Torvalds 		return 1;
6831da177e4SLinus Torvalds 	}
6841da177e4SLinus Torvalds 	return 0;
6851da177e4SLinus Torvalds }
6861da177e4SLinus Torvalds 
687f1662356SArjan van de Ven static __always_inline void follow_dotdot(struct nameidata *nd)
6881da177e4SLinus Torvalds {
6892a737871SAl Viro 	set_root(nd);
690e518ddb7SAndreas Mohr 
6911da177e4SLinus Torvalds 	while(1) {
6921da177e4SLinus Torvalds 		struct vfsmount *parent;
6934ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
6941da177e4SLinus Torvalds 
6952a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
6962a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
6971da177e4SLinus Torvalds 			break;
6981da177e4SLinus Torvalds 		}
6991da177e4SLinus Torvalds 		spin_lock(&dcache_lock);
7004ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
7014ac91378SJan Blunck 			nd->path.dentry = dget(nd->path.dentry->d_parent);
7021da177e4SLinus Torvalds 			spin_unlock(&dcache_lock);
7031da177e4SLinus Torvalds 			dput(old);
7041da177e4SLinus Torvalds 			break;
7051da177e4SLinus Torvalds 		}
7061da177e4SLinus Torvalds 		spin_unlock(&dcache_lock);
7071da177e4SLinus Torvalds 		spin_lock(&vfsmount_lock);
7084ac91378SJan Blunck 		parent = nd->path.mnt->mnt_parent;
7094ac91378SJan Blunck 		if (parent == nd->path.mnt) {
7101da177e4SLinus Torvalds 			spin_unlock(&vfsmount_lock);
7111da177e4SLinus Torvalds 			break;
7121da177e4SLinus Torvalds 		}
7131da177e4SLinus Torvalds 		mntget(parent);
7144ac91378SJan Blunck 		nd->path.dentry = dget(nd->path.mnt->mnt_mountpoint);
7151da177e4SLinus Torvalds 		spin_unlock(&vfsmount_lock);
7161da177e4SLinus Torvalds 		dput(old);
7174ac91378SJan Blunck 		mntput(nd->path.mnt);
7184ac91378SJan Blunck 		nd->path.mnt = parent;
7191da177e4SLinus Torvalds 	}
72079ed0226SAl Viro 	follow_mount(&nd->path);
7211da177e4SLinus Torvalds }
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds /*
7241da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
7251da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
7261da177e4SLinus Torvalds  *  It _is_ time-critical.
7271da177e4SLinus Torvalds  */
7281da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
7291da177e4SLinus Torvalds 		     struct path *path)
7301da177e4SLinus Torvalds {
7314ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
7326e6b1bd1SAl Viro 	struct dentry *dentry, *parent;
7336e6b1bd1SAl Viro 	struct inode *dir;
7343cac260aSAl Viro 	/*
7353cac260aSAl Viro 	 * See if the low-level filesystem might want
7363cac260aSAl Viro 	 * to use its own hash..
7373cac260aSAl Viro 	 */
7383cac260aSAl Viro 	if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
7393cac260aSAl Viro 		int err = nd->path.dentry->d_op->d_hash(nd->path.dentry, name);
7403cac260aSAl Viro 		if (err < 0)
7413cac260aSAl Viro 			return err;
7423cac260aSAl Viro 	}
7431da177e4SLinus Torvalds 
7443cac260aSAl Viro 	dentry = __d_lookup(nd->path.dentry, name);
7451da177e4SLinus Torvalds 	if (!dentry)
7461da177e4SLinus Torvalds 		goto need_lookup;
7471da177e4SLinus Torvalds 	if (dentry->d_op && dentry->d_op->d_revalidate)
7481da177e4SLinus Torvalds 		goto need_revalidate;
7491da177e4SLinus Torvalds done:
7501da177e4SLinus Torvalds 	path->mnt = mnt;
7511da177e4SLinus Torvalds 	path->dentry = dentry;
752634ee701SAl Viro 	__follow_mount(path);
7531da177e4SLinus Torvalds 	return 0;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds need_lookup:
7566e6b1bd1SAl Viro 	parent = nd->path.dentry;
7576e6b1bd1SAl Viro 	dir = parent->d_inode;
7586e6b1bd1SAl Viro 
7596e6b1bd1SAl Viro 	mutex_lock(&dir->i_mutex);
7606e6b1bd1SAl Viro 	/*
7616e6b1bd1SAl Viro 	 * First re-do the cached lookup just in case it was created
7626e6b1bd1SAl Viro 	 * while we waited for the directory semaphore..
7636e6b1bd1SAl Viro 	 *
7646e6b1bd1SAl Viro 	 * FIXME! This could use version numbering or similar to
7656e6b1bd1SAl Viro 	 * avoid unnecessary cache lookups.
7666e6b1bd1SAl Viro 	 *
7676e6b1bd1SAl Viro 	 * The "dcache_lock" is purely to protect the RCU list walker
7686e6b1bd1SAl Viro 	 * from concurrent renames at this point (we mustn't get false
7696e6b1bd1SAl Viro 	 * negatives from the RCU list walk here, unlike the optimistic
7706e6b1bd1SAl Viro 	 * fast walk).
7716e6b1bd1SAl Viro 	 *
7726e6b1bd1SAl Viro 	 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
7736e6b1bd1SAl Viro 	 */
7746e6b1bd1SAl Viro 	dentry = d_lookup(parent, name);
7756e6b1bd1SAl Viro 	if (!dentry) {
7766e6b1bd1SAl Viro 		struct dentry *new;
7776e6b1bd1SAl Viro 
7786e6b1bd1SAl Viro 		/* Don't create child dentry for a dead directory. */
7796e6b1bd1SAl Viro 		dentry = ERR_PTR(-ENOENT);
7806e6b1bd1SAl Viro 		if (IS_DEADDIR(dir))
7816e6b1bd1SAl Viro 			goto out_unlock;
7826e6b1bd1SAl Viro 
7836e6b1bd1SAl Viro 		new = d_alloc(parent, name);
7846e6b1bd1SAl Viro 		dentry = ERR_PTR(-ENOMEM);
7856e6b1bd1SAl Viro 		if (new) {
7866e6b1bd1SAl Viro 			dentry = dir->i_op->lookup(dir, new, nd);
7876e6b1bd1SAl Viro 			if (dentry)
7886e6b1bd1SAl Viro 				dput(new);
7896e6b1bd1SAl Viro 			else
7906e6b1bd1SAl Viro 				dentry = new;
7916e6b1bd1SAl Viro 		}
7926e6b1bd1SAl Viro out_unlock:
7936e6b1bd1SAl Viro 		mutex_unlock(&dir->i_mutex);
7946e6b1bd1SAl Viro 		if (IS_ERR(dentry))
7956e6b1bd1SAl Viro 			goto fail;
7966e6b1bd1SAl Viro 		goto done;
7976e6b1bd1SAl Viro 	}
7986e6b1bd1SAl Viro 
7996e6b1bd1SAl Viro 	/*
8006e6b1bd1SAl Viro 	 * Uhhuh! Nasty case: the cache was re-populated while
8016e6b1bd1SAl Viro 	 * we waited on the semaphore. Need to revalidate.
8026e6b1bd1SAl Viro 	 */
8036e6b1bd1SAl Viro 	mutex_unlock(&dir->i_mutex);
8046e6b1bd1SAl Viro 	if (dentry->d_op && dentry->d_op->d_revalidate) {
8056e6b1bd1SAl Viro 		dentry = do_revalidate(dentry, nd);
8066e6b1bd1SAl Viro 		if (!dentry)
8076e6b1bd1SAl Viro 			dentry = ERR_PTR(-ENOENT);
8086e6b1bd1SAl Viro 	}
8091da177e4SLinus Torvalds 	if (IS_ERR(dentry))
8101da177e4SLinus Torvalds 		goto fail;
8111da177e4SLinus Torvalds 	goto done;
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds need_revalidate:
814bcdc5e01SIan Kent 	dentry = do_revalidate(dentry, nd);
815bcdc5e01SIan Kent 	if (!dentry)
8161da177e4SLinus Torvalds 		goto need_lookup;
817bcdc5e01SIan Kent 	if (IS_ERR(dentry))
818bcdc5e01SIan Kent 		goto fail;
819bcdc5e01SIan Kent 	goto done;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds fail:
8221da177e4SLinus Torvalds 	return PTR_ERR(dentry);
8231da177e4SLinus Torvalds }
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds /*
826ac278a9cSAl Viro  * This is a temporary kludge to deal with "automount" symlinks; proper
827ac278a9cSAl Viro  * solution is to trigger them on follow_mount(), so that do_lookup()
828ac278a9cSAl Viro  * would DTRT.  To be killed before 2.6.34-final.
829ac278a9cSAl Viro  */
830ac278a9cSAl Viro static inline int follow_on_final(struct inode *inode, unsigned lookup_flags)
831ac278a9cSAl Viro {
832ac278a9cSAl Viro 	return inode && unlikely(inode->i_op->follow_link) &&
833ac278a9cSAl Viro 		((lookup_flags & LOOKUP_FOLLOW) || S_ISDIR(inode->i_mode));
834ac278a9cSAl Viro }
835ac278a9cSAl Viro 
836ac278a9cSAl Viro /*
8371da177e4SLinus Torvalds  * Name resolution.
838ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
839ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
8401da177e4SLinus Torvalds  *
841ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
842ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
8431da177e4SLinus Torvalds  */
8446de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
8451da177e4SLinus Torvalds {
8461da177e4SLinus Torvalds 	struct path next;
8471da177e4SLinus Torvalds 	struct inode *inode;
8481da177e4SLinus Torvalds 	int err;
8491da177e4SLinus Torvalds 	unsigned int lookup_flags = nd->flags;
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 	while (*name=='/')
8521da177e4SLinus Torvalds 		name++;
8531da177e4SLinus Torvalds 	if (!*name)
8541da177e4SLinus Torvalds 		goto return_reval;
8551da177e4SLinus Torvalds 
8564ac91378SJan Blunck 	inode = nd->path.dentry->d_inode;
8571da177e4SLinus Torvalds 	if (nd->depth)
858f55eab82STrond Myklebust 		lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
8611da177e4SLinus Torvalds 	for(;;) {
8621da177e4SLinus Torvalds 		unsigned long hash;
8631da177e4SLinus Torvalds 		struct qstr this;
8641da177e4SLinus Torvalds 		unsigned int c;
8651da177e4SLinus Torvalds 
866cdce5d6bSTrond Myklebust 		nd->flags |= LOOKUP_CONTINUE;
867b75b5086SAl Viro 		err = exec_permission(inode);
8681da177e4SLinus Torvalds  		if (err)
8691da177e4SLinus Torvalds 			break;
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 		this.name = name;
8721da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 		hash = init_name_hash();
8751da177e4SLinus Torvalds 		do {
8761da177e4SLinus Torvalds 			name++;
8771da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
8781da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
8791da177e4SLinus Torvalds 		} while (c && (c != '/'));
8801da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
8811da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 		/* remove trailing slashes? */
8841da177e4SLinus Torvalds 		if (!c)
8851da177e4SLinus Torvalds 			goto last_component;
8861da177e4SLinus Torvalds 		while (*++name == '/');
8871da177e4SLinus Torvalds 		if (!*name)
8881da177e4SLinus Torvalds 			goto last_with_slashes;
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 		/*
8911da177e4SLinus Torvalds 		 * "." and ".." are special - ".." especially so because it has
8921da177e4SLinus Torvalds 		 * to be able to know about the current root directory and
8931da177e4SLinus Torvalds 		 * parent relationships.
8941da177e4SLinus Torvalds 		 */
8951da177e4SLinus Torvalds 		if (this.name[0] == '.') switch (this.len) {
8961da177e4SLinus Torvalds 			default:
8971da177e4SLinus Torvalds 				break;
8981da177e4SLinus Torvalds 			case 2:
8991da177e4SLinus Torvalds 				if (this.name[1] != '.')
9001da177e4SLinus Torvalds 					break;
90158c465ebSAl Viro 				follow_dotdot(nd);
9024ac91378SJan Blunck 				inode = nd->path.dentry->d_inode;
9031da177e4SLinus Torvalds 				/* fallthrough */
9041da177e4SLinus Torvalds 			case 1:
9051da177e4SLinus Torvalds 				continue;
9061da177e4SLinus Torvalds 		}
9071da177e4SLinus Torvalds 		/* This does the actual lookups.. */
9081da177e4SLinus Torvalds 		err = do_lookup(nd, &this, &next);
9091da177e4SLinus Torvalds 		if (err)
9101da177e4SLinus Torvalds 			break;
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 		err = -ENOENT;
9131da177e4SLinus Torvalds 		inode = next.dentry->d_inode;
9141da177e4SLinus Torvalds 		if (!inode)
9151da177e4SLinus Torvalds 			goto out_dput;
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 		if (inode->i_op->follow_link) {
91890ebe565SAl Viro 			err = do_follow_link(&next, nd);
9191da177e4SLinus Torvalds 			if (err)
9201da177e4SLinus Torvalds 				goto return_err;
9211da177e4SLinus Torvalds 			err = -ENOENT;
9224ac91378SJan Blunck 			inode = nd->path.dentry->d_inode;
9231da177e4SLinus Torvalds 			if (!inode)
9241da177e4SLinus Torvalds 				break;
92509dd17d3SMiklos Szeredi 		} else
92609dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
9271da177e4SLinus Torvalds 		err = -ENOTDIR;
9281da177e4SLinus Torvalds 		if (!inode->i_op->lookup)
9291da177e4SLinus Torvalds 			break;
9301da177e4SLinus Torvalds 		continue;
9311da177e4SLinus Torvalds 		/* here ends the main loop */
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds last_with_slashes:
9341da177e4SLinus Torvalds 		lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
9351da177e4SLinus Torvalds last_component:
936f55eab82STrond Myklebust 		/* Clear LOOKUP_CONTINUE iff it was previously unset */
937f55eab82STrond Myklebust 		nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
9381da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_PARENT)
9391da177e4SLinus Torvalds 			goto lookup_parent;
9401da177e4SLinus Torvalds 		if (this.name[0] == '.') switch (this.len) {
9411da177e4SLinus Torvalds 			default:
9421da177e4SLinus Torvalds 				break;
9431da177e4SLinus Torvalds 			case 2:
9441da177e4SLinus Torvalds 				if (this.name[1] != '.')
9451da177e4SLinus Torvalds 					break;
94658c465ebSAl Viro 				follow_dotdot(nd);
9474ac91378SJan Blunck 				inode = nd->path.dentry->d_inode;
9481da177e4SLinus Torvalds 				/* fallthrough */
9491da177e4SLinus Torvalds 			case 1:
9501da177e4SLinus Torvalds 				goto return_reval;
9511da177e4SLinus Torvalds 		}
9521da177e4SLinus Torvalds 		err = do_lookup(nd, &this, &next);
9531da177e4SLinus Torvalds 		if (err)
9541da177e4SLinus Torvalds 			break;
9551da177e4SLinus Torvalds 		inode = next.dentry->d_inode;
956ac278a9cSAl Viro 		if (follow_on_final(inode, lookup_flags)) {
95790ebe565SAl Viro 			err = do_follow_link(&next, nd);
9581da177e4SLinus Torvalds 			if (err)
9591da177e4SLinus Torvalds 				goto return_err;
9604ac91378SJan Blunck 			inode = nd->path.dentry->d_inode;
96109dd17d3SMiklos Szeredi 		} else
96209dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
9631da177e4SLinus Torvalds 		err = -ENOENT;
9641da177e4SLinus Torvalds 		if (!inode)
9651da177e4SLinus Torvalds 			break;
9661da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_DIRECTORY) {
9671da177e4SLinus Torvalds 			err = -ENOTDIR;
968acfa4380SAl Viro 			if (!inode->i_op->lookup)
9691da177e4SLinus Torvalds 				break;
9701da177e4SLinus Torvalds 		}
9711da177e4SLinus Torvalds 		goto return_base;
9721da177e4SLinus Torvalds lookup_parent:
9731da177e4SLinus Torvalds 		nd->last = this;
9741da177e4SLinus Torvalds 		nd->last_type = LAST_NORM;
9751da177e4SLinus Torvalds 		if (this.name[0] != '.')
9761da177e4SLinus Torvalds 			goto return_base;
9771da177e4SLinus Torvalds 		if (this.len == 1)
9781da177e4SLinus Torvalds 			nd->last_type = LAST_DOT;
9791da177e4SLinus Torvalds 		else if (this.len == 2 && this.name[1] == '.')
9801da177e4SLinus Torvalds 			nd->last_type = LAST_DOTDOT;
9811da177e4SLinus Torvalds 		else
9821da177e4SLinus Torvalds 			goto return_base;
9831da177e4SLinus Torvalds return_reval:
9841da177e4SLinus Torvalds 		/*
9851da177e4SLinus Torvalds 		 * We bypassed the ordinary revalidation routines.
9861da177e4SLinus Torvalds 		 * We may need to check the cached dentry for staleness.
9871da177e4SLinus Torvalds 		 */
9884ac91378SJan Blunck 		if (nd->path.dentry && nd->path.dentry->d_sb &&
9894ac91378SJan Blunck 		    (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
9901da177e4SLinus Torvalds 			err = -ESTALE;
9911da177e4SLinus Torvalds 			/* Note: we do not d_invalidate() */
9924ac91378SJan Blunck 			if (!nd->path.dentry->d_op->d_revalidate(
9934ac91378SJan Blunck 					nd->path.dentry, nd))
9941da177e4SLinus Torvalds 				break;
9951da177e4SLinus Torvalds 		}
9961da177e4SLinus Torvalds return_base:
9971da177e4SLinus Torvalds 		return 0;
9981da177e4SLinus Torvalds out_dput:
9991d957f9bSJan Blunck 		path_put_conditional(&next, nd);
10001da177e4SLinus Torvalds 		break;
10011da177e4SLinus Torvalds 	}
10021d957f9bSJan Blunck 	path_put(&nd->path);
10031da177e4SLinus Torvalds return_err:
10041da177e4SLinus Torvalds 	return err;
10051da177e4SLinus Torvalds }
10061da177e4SLinus Torvalds 
1007fc9b52cdSHarvey Harrison static int path_walk(const char *name, struct nameidata *nd)
10081da177e4SLinus Torvalds {
10096de88d72SAl Viro 	struct path save = nd->path;
10106de88d72SAl Viro 	int result;
10116de88d72SAl Viro 
10121da177e4SLinus Torvalds 	current->total_link_count = 0;
10136de88d72SAl Viro 
10146de88d72SAl Viro 	/* make sure the stuff we saved doesn't go away */
10156de88d72SAl Viro 	path_get(&save);
10166de88d72SAl Viro 
10176de88d72SAl Viro 	result = link_path_walk(name, nd);
10186de88d72SAl Viro 	if (result == -ESTALE) {
10196de88d72SAl Viro 		/* nd->path had been dropped */
10206de88d72SAl Viro 		current->total_link_count = 0;
10216de88d72SAl Viro 		nd->path = save;
10226de88d72SAl Viro 		path_get(&nd->path);
10236de88d72SAl Viro 		nd->flags |= LOOKUP_REVAL;
10246de88d72SAl Viro 		result = link_path_walk(name, nd);
10256de88d72SAl Viro 	}
10266de88d72SAl Viro 
10276de88d72SAl Viro 	path_put(&save);
10286de88d72SAl Viro 
10296de88d72SAl Viro 	return result;
10301da177e4SLinus Torvalds }
10311da177e4SLinus Torvalds 
10329b4a9b14SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
10331da177e4SLinus Torvalds {
1034ea3834d9SPrasanna Meda 	int retval = 0;
1035170aa3d0SUlrich Drepper 	int fput_needed;
1036170aa3d0SUlrich Drepper 	struct file *file;
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
10391da177e4SLinus Torvalds 	nd->flags = flags;
10401da177e4SLinus Torvalds 	nd->depth = 0;
10412a737871SAl Viro 	nd->root.mnt = NULL;
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds 	if (*name=='/') {
10442a737871SAl Viro 		set_root(nd);
10452a737871SAl Viro 		nd->path = nd->root;
10462a737871SAl Viro 		path_get(&nd->root);
10475590ff0dSUlrich Drepper 	} else if (dfd == AT_FDCWD) {
10482a737871SAl Viro 		struct fs_struct *fs = current->fs;
1049e518ddb7SAndreas Mohr 		read_lock(&fs->lock);
10506ac08c39SJan Blunck 		nd->path = fs->pwd;
10516ac08c39SJan Blunck 		path_get(&fs->pwd);
1052e518ddb7SAndreas Mohr 		read_unlock(&fs->lock);
10535590ff0dSUlrich Drepper 	} else {
10545590ff0dSUlrich Drepper 		struct dentry *dentry;
10555590ff0dSUlrich Drepper 
10565590ff0dSUlrich Drepper 		file = fget_light(dfd, &fput_needed);
10575590ff0dSUlrich Drepper 		retval = -EBADF;
1058170aa3d0SUlrich Drepper 		if (!file)
10596d09bb62STrond Myklebust 			goto out_fail;
10605590ff0dSUlrich Drepper 
10610f7fc9e4SJosef "Jeff" Sipek 		dentry = file->f_path.dentry;
10625590ff0dSUlrich Drepper 
10635590ff0dSUlrich Drepper 		retval = -ENOTDIR;
1064170aa3d0SUlrich Drepper 		if (!S_ISDIR(dentry->d_inode->i_mode))
10656d09bb62STrond Myklebust 			goto fput_fail;
10665590ff0dSUlrich Drepper 
10675590ff0dSUlrich Drepper 		retval = file_permission(file, MAY_EXEC);
1068170aa3d0SUlrich Drepper 		if (retval)
10696d09bb62STrond Myklebust 			goto fput_fail;
10705590ff0dSUlrich Drepper 
10715dd784d0SJan Blunck 		nd->path = file->f_path;
10725dd784d0SJan Blunck 		path_get(&file->f_path);
10735590ff0dSUlrich Drepper 
10745590ff0dSUlrich Drepper 		fput_light(file, fput_needed);
10751da177e4SLinus Torvalds 	}
10769b4a9b14SAl Viro 	return 0;
10772dfdd266SJosef 'Jeff' Sipek 
10789b4a9b14SAl Viro fput_fail:
10799b4a9b14SAl Viro 	fput_light(file, fput_needed);
10809b4a9b14SAl Viro out_fail:
10819b4a9b14SAl Viro 	return retval;
10829b4a9b14SAl Viro }
10839b4a9b14SAl Viro 
10849b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
10859b4a9b14SAl Viro static int do_path_lookup(int dfd, const char *name,
10869b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
10879b4a9b14SAl Viro {
10889b4a9b14SAl Viro 	int retval = path_init(dfd, name, flags, nd);
10899b4a9b14SAl Viro 	if (!retval)
10902dfdd266SJosef 'Jeff' Sipek 		retval = path_walk(name, nd);
10914ac91378SJan Blunck 	if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
10924ac91378SJan Blunck 				nd->path.dentry->d_inode))
10934ac91378SJan Blunck 		audit_inode(name, nd->path.dentry);
10942a737871SAl Viro 	if (nd->root.mnt) {
10952a737871SAl Viro 		path_put(&nd->root);
10962a737871SAl Viro 		nd->root.mnt = NULL;
10972a737871SAl Viro 	}
1098170aa3d0SUlrich Drepper 	return retval;
10991da177e4SLinus Torvalds }
11001da177e4SLinus Torvalds 
1101fc9b52cdSHarvey Harrison int path_lookup(const char *name, unsigned int flags,
11025590ff0dSUlrich Drepper 			struct nameidata *nd)
11035590ff0dSUlrich Drepper {
11045590ff0dSUlrich Drepper 	return do_path_lookup(AT_FDCWD, name, flags, nd);
11055590ff0dSUlrich Drepper }
11065590ff0dSUlrich Drepper 
1107d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1108d1811465SAl Viro {
1109d1811465SAl Viro 	struct nameidata nd;
1110d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1111d1811465SAl Viro 	if (!res)
1112d1811465SAl Viro 		*path = nd.path;
1113d1811465SAl Viro 	return res;
1114d1811465SAl Viro }
1115d1811465SAl Viro 
111616f18200SJosef 'Jeff' Sipek /**
111716f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
111816f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
111916f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
112016f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
112116f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
112216f18200SJosef 'Jeff' Sipek  * @nd: pointer to nameidata
112316f18200SJosef 'Jeff' Sipek  */
112416f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
112516f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
112616f18200SJosef 'Jeff' Sipek 		    struct nameidata *nd)
112716f18200SJosef 'Jeff' Sipek {
112816f18200SJosef 'Jeff' Sipek 	int retval;
112916f18200SJosef 'Jeff' Sipek 
113016f18200SJosef 'Jeff' Sipek 	/* same as do_path_lookup */
113116f18200SJosef 'Jeff' Sipek 	nd->last_type = LAST_ROOT;
113216f18200SJosef 'Jeff' Sipek 	nd->flags = flags;
113316f18200SJosef 'Jeff' Sipek 	nd->depth = 0;
113416f18200SJosef 'Jeff' Sipek 
1135c8e7f449SJan Blunck 	nd->path.dentry = dentry;
1136c8e7f449SJan Blunck 	nd->path.mnt = mnt;
1137c8e7f449SJan Blunck 	path_get(&nd->path);
11385b857119SAl Viro 	nd->root = nd->path;
11395b857119SAl Viro 	path_get(&nd->root);
114016f18200SJosef 'Jeff' Sipek 
114116f18200SJosef 'Jeff' Sipek 	retval = path_walk(name, nd);
11424ac91378SJan Blunck 	if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
11434ac91378SJan Blunck 				nd->path.dentry->d_inode))
11444ac91378SJan Blunck 		audit_inode(name, nd->path.dentry);
114516f18200SJosef 'Jeff' Sipek 
11462a737871SAl Viro 	path_put(&nd->root);
11472a737871SAl Viro 	nd->root.mnt = NULL;
114816f18200SJosef 'Jeff' Sipek 
11492a737871SAl Viro 	return retval;
115016f18200SJosef 'Jeff' Sipek }
115116f18200SJosef 'Jeff' Sipek 
1152eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1153eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
11541da177e4SLinus Torvalds {
11551da177e4SLinus Torvalds 	struct dentry *dentry;
11561da177e4SLinus Torvalds 	struct inode *inode;
11571da177e4SLinus Torvalds 	int err;
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds 	inode = base->d_inode;
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 	/*
11621da177e4SLinus Torvalds 	 * See if the low-level filesystem might want
11631da177e4SLinus Torvalds 	 * to use its own hash..
11641da177e4SLinus Torvalds 	 */
11651da177e4SLinus Torvalds 	if (base->d_op && base->d_op->d_hash) {
11661da177e4SLinus Torvalds 		err = base->d_op->d_hash(base, name);
11671da177e4SLinus Torvalds 		dentry = ERR_PTR(err);
11681da177e4SLinus Torvalds 		if (err < 0)
11691da177e4SLinus Torvalds 			goto out;
11701da177e4SLinus Torvalds 	}
11711da177e4SLinus Torvalds 
11726e6b1bd1SAl Viro 	dentry = __d_lookup(base, name);
11736e6b1bd1SAl Viro 
11746e6b1bd1SAl Viro 	/* lockess __d_lookup may fail due to concurrent d_move()
11756e6b1bd1SAl Viro 	 * in some unrelated directory, so try with d_lookup
11766e6b1bd1SAl Viro 	 */
11776e6b1bd1SAl Viro 	if (!dentry)
11786e6b1bd1SAl Viro 		dentry = d_lookup(base, name);
11796e6b1bd1SAl Viro 
11806e6b1bd1SAl Viro 	if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
11816e6b1bd1SAl Viro 		dentry = do_revalidate(dentry, nd);
11826e6b1bd1SAl Viro 
11831da177e4SLinus Torvalds 	if (!dentry) {
1184d70b67c8SMiklos Szeredi 		struct dentry *new;
1185d70b67c8SMiklos Szeredi 
1186d70b67c8SMiklos Szeredi 		/* Don't create child dentry for a dead directory. */
1187d70b67c8SMiklos Szeredi 		dentry = ERR_PTR(-ENOENT);
1188d70b67c8SMiklos Szeredi 		if (IS_DEADDIR(inode))
1189d70b67c8SMiklos Szeredi 			goto out;
1190d70b67c8SMiklos Szeredi 
1191d70b67c8SMiklos Szeredi 		new = d_alloc(base, name);
11921da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOMEM);
11931da177e4SLinus Torvalds 		if (!new)
11941da177e4SLinus Torvalds 			goto out;
11951da177e4SLinus Torvalds 		dentry = inode->i_op->lookup(inode, new, nd);
11961da177e4SLinus Torvalds 		if (!dentry)
11971da177e4SLinus Torvalds 			dentry = new;
11981da177e4SLinus Torvalds 		else
11991da177e4SLinus Torvalds 			dput(new);
12001da177e4SLinus Torvalds 	}
12011da177e4SLinus Torvalds out:
12021da177e4SLinus Torvalds 	return dentry;
12031da177e4SLinus Torvalds }
12041da177e4SLinus Torvalds 
1205057f6c01SJames Morris /*
1206057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1207057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1208057f6c01SJames Morris  * SMP-safe.
1209057f6c01SJames Morris  */
1210a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
12111da177e4SLinus Torvalds {
1212eead1911SChristoph Hellwig 	int err;
1213eead1911SChristoph Hellwig 
1214b75b5086SAl Viro 	err = exec_permission(nd->path.dentry->d_inode);
1215eead1911SChristoph Hellwig 	if (err)
1216eead1911SChristoph Hellwig 		return ERR_PTR(err);
12174ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
12181da177e4SLinus Torvalds }
12191da177e4SLinus Torvalds 
1220eead1911SChristoph Hellwig static int __lookup_one_len(const char *name, struct qstr *this,
1221eead1911SChristoph Hellwig 		struct dentry *base, int len)
12221da177e4SLinus Torvalds {
12231da177e4SLinus Torvalds 	unsigned long hash;
12241da177e4SLinus Torvalds 	unsigned int c;
12251da177e4SLinus Torvalds 
1226057f6c01SJames Morris 	this->name = name;
1227057f6c01SJames Morris 	this->len = len;
12281da177e4SLinus Torvalds 	if (!len)
1229057f6c01SJames Morris 		return -EACCES;
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 	hash = init_name_hash();
12321da177e4SLinus Torvalds 	while (len--) {
12331da177e4SLinus Torvalds 		c = *(const unsigned char *)name++;
12341da177e4SLinus Torvalds 		if (c == '/' || c == '\0')
1235057f6c01SJames Morris 			return -EACCES;
12361da177e4SLinus Torvalds 		hash = partial_name_hash(c, hash);
12371da177e4SLinus Torvalds 	}
1238057f6c01SJames Morris 	this->hash = end_name_hash(hash);
1239057f6c01SJames Morris 	return 0;
1240057f6c01SJames Morris }
12411da177e4SLinus Torvalds 
1242eead1911SChristoph Hellwig /**
1243a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1244eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1245eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1246eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1247eead1911SChristoph Hellwig  *
1248a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1249a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1250eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1251eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1252eead1911SChristoph Hellwig  */
1253057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1254057f6c01SJames Morris {
1255057f6c01SJames Morris 	int err;
1256057f6c01SJames Morris 	struct qstr this;
1257057f6c01SJames Morris 
12582f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
12592f9092e1SDavid Woodhouse 
1260057f6c01SJames Morris 	err = __lookup_one_len(name, &this, base, len);
1261057f6c01SJames Morris 	if (err)
1262057f6c01SJames Morris 		return ERR_PTR(err);
1263eead1911SChristoph Hellwig 
1264b75b5086SAl Viro 	err = exec_permission(base->d_inode);
1265eead1911SChristoph Hellwig 	if (err)
1266eead1911SChristoph Hellwig 		return ERR_PTR(err);
126749705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1268057f6c01SJames Morris }
1269057f6c01SJames Morris 
12702d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags,
12712d8f3038SAl Viro 		 struct path *path)
12721da177e4SLinus Torvalds {
12732d8f3038SAl Viro 	struct nameidata nd;
12741da177e4SLinus Torvalds 	char *tmp = getname(name);
12751da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
12761da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
12772d8f3038SAl Viro 
12782d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
12792d8f3038SAl Viro 
12802d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
12811da177e4SLinus Torvalds 		putname(tmp);
12822d8f3038SAl Viro 		if (!err)
12832d8f3038SAl Viro 			*path = nd.path;
12841da177e4SLinus Torvalds 	}
12851da177e4SLinus Torvalds 	return err;
12861da177e4SLinus Torvalds }
12871da177e4SLinus Torvalds 
12882ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
12892ad94ae6SAl Viro 			struct nameidata *nd, char **name)
12902ad94ae6SAl Viro {
12912ad94ae6SAl Viro 	char *s = getname(path);
12922ad94ae6SAl Viro 	int error;
12932ad94ae6SAl Viro 
12942ad94ae6SAl Viro 	if (IS_ERR(s))
12952ad94ae6SAl Viro 		return PTR_ERR(s);
12962ad94ae6SAl Viro 
12972ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
12982ad94ae6SAl Viro 	if (error)
12992ad94ae6SAl Viro 		putname(s);
13002ad94ae6SAl Viro 	else
13012ad94ae6SAl Viro 		*name = s;
13022ad94ae6SAl Viro 
13032ad94ae6SAl Viro 	return error;
13042ad94ae6SAl Viro }
13052ad94ae6SAl Viro 
13061da177e4SLinus Torvalds /*
13071da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
13081da177e4SLinus Torvalds  * minimal.
13091da177e4SLinus Torvalds  */
13101da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
13111da177e4SLinus Torvalds {
1312da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1313da9592edSDavid Howells 
13141da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
13151da177e4SLinus Torvalds 		return 0;
1316da9592edSDavid Howells 	if (inode->i_uid == fsuid)
13171da177e4SLinus Torvalds 		return 0;
1318da9592edSDavid Howells 	if (dir->i_uid == fsuid)
13191da177e4SLinus Torvalds 		return 0;
13201da177e4SLinus Torvalds 	return !capable(CAP_FOWNER);
13211da177e4SLinus Torvalds }
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds /*
13241da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
13251da177e4SLinus Torvalds  *  whether the type of victim is right.
13261da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
13271da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
13281da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
13291da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
13301da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
13311da177e4SLinus Torvalds  *	a. be owner of dir, or
13321da177e4SLinus Torvalds  *	b. be owner of victim, or
13331da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
13341da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
13351da177e4SLinus Torvalds  *     links pointing to it.
13361da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
13371da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
13381da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
13391da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
13401da177e4SLinus Torvalds  *     nfs_async_unlink().
13411da177e4SLinus Torvalds  */
1342858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
13431da177e4SLinus Torvalds {
13441da177e4SLinus Torvalds 	int error;
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 	if (!victim->d_inode)
13471da177e4SLinus Torvalds 		return -ENOENT;
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
13505a190ae6SAl Viro 	audit_inode_child(victim->d_name.name, victim, dir);
13511da177e4SLinus Torvalds 
1352f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
13531da177e4SLinus Torvalds 	if (error)
13541da177e4SLinus Torvalds 		return error;
13551da177e4SLinus Torvalds 	if (IS_APPEND(dir))
13561da177e4SLinus Torvalds 		return -EPERM;
13571da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1358f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
13591da177e4SLinus Torvalds 		return -EPERM;
13601da177e4SLinus Torvalds 	if (isdir) {
13611da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
13621da177e4SLinus Torvalds 			return -ENOTDIR;
13631da177e4SLinus Torvalds 		if (IS_ROOT(victim))
13641da177e4SLinus Torvalds 			return -EBUSY;
13651da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
13661da177e4SLinus Torvalds 		return -EISDIR;
13671da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
13681da177e4SLinus Torvalds 		return -ENOENT;
13691da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
13701da177e4SLinus Torvalds 		return -EBUSY;
13711da177e4SLinus Torvalds 	return 0;
13721da177e4SLinus Torvalds }
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
13751da177e4SLinus Torvalds  *  dir.
13761da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
13771da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
13781da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
13791da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
13801da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
13811da177e4SLinus Torvalds  */
1382a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
13831da177e4SLinus Torvalds {
13841da177e4SLinus Torvalds 	if (child->d_inode)
13851da177e4SLinus Torvalds 		return -EEXIST;
13861da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
13871da177e4SLinus Torvalds 		return -ENOENT;
1388f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
13891da177e4SLinus Torvalds }
13901da177e4SLinus Torvalds 
13911da177e4SLinus Torvalds /*
13921da177e4SLinus Torvalds  * O_DIRECTORY translates into forcing a directory lookup.
13931da177e4SLinus Torvalds  */
13941da177e4SLinus Torvalds static inline int lookup_flags(unsigned int f)
13951da177e4SLinus Torvalds {
13961da177e4SLinus Torvalds 	unsigned long retval = LOOKUP_FOLLOW;
13971da177e4SLinus Torvalds 
13981da177e4SLinus Torvalds 	if (f & O_NOFOLLOW)
13991da177e4SLinus Torvalds 		retval &= ~LOOKUP_FOLLOW;
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 	if (f & O_DIRECTORY)
14021da177e4SLinus Torvalds 		retval |= LOOKUP_DIRECTORY;
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds 	return retval;
14051da177e4SLinus Torvalds }
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds /*
14081da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
14091da177e4SLinus Torvalds  */
14101da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
14111da177e4SLinus Torvalds {
14121da177e4SLinus Torvalds 	struct dentry *p;
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds 	if (p1 == p2) {
1415f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
14161da177e4SLinus Torvalds 		return NULL;
14171da177e4SLinus Torvalds 	}
14181da177e4SLinus Torvalds 
1419a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
14201da177e4SLinus Torvalds 
1421e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1422e2761a11SOGAWA Hirofumi 	if (p) {
1423f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1424f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
14251da177e4SLinus Torvalds 		return p;
14261da177e4SLinus Torvalds 	}
14271da177e4SLinus Torvalds 
1428e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1429e2761a11SOGAWA Hirofumi 	if (p) {
1430f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1431f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
14321da177e4SLinus Torvalds 		return p;
14331da177e4SLinus Torvalds 	}
14341da177e4SLinus Torvalds 
1435f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1436f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
14371da177e4SLinus Torvalds 	return NULL;
14381da177e4SLinus Torvalds }
14391da177e4SLinus Torvalds 
14401da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
14411da177e4SLinus Torvalds {
14421b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
14431da177e4SLinus Torvalds 	if (p1 != p2) {
14441b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1445a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
14461da177e4SLinus Torvalds 	}
14471da177e4SLinus Torvalds }
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
14501da177e4SLinus Torvalds 		struct nameidata *nd)
14511da177e4SLinus Torvalds {
1452a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
14531da177e4SLinus Torvalds 
14541da177e4SLinus Torvalds 	if (error)
14551da177e4SLinus Torvalds 		return error;
14561da177e4SLinus Torvalds 
1457acfa4380SAl Viro 	if (!dir->i_op->create)
14581da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
14591da177e4SLinus Torvalds 	mode &= S_IALLUGO;
14601da177e4SLinus Torvalds 	mode |= S_IFREG;
14611da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
14621da177e4SLinus Torvalds 	if (error)
14631da177e4SLinus Torvalds 		return error;
14649e3509e2SJan Kara 	vfs_dq_init(dir);
14651da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
1466a74574aaSStephen Smalley 	if (!error)
1467f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
14681da177e4SLinus Torvalds 	return error;
14691da177e4SLinus Torvalds }
14701da177e4SLinus Torvalds 
14713fb64190SChristoph Hellwig int may_open(struct path *path, int acc_mode, int flag)
14721da177e4SLinus Torvalds {
14733fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
14741da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14751da177e4SLinus Torvalds 	int error;
14761da177e4SLinus Torvalds 
14771da177e4SLinus Torvalds 	if (!inode)
14781da177e4SLinus Torvalds 		return -ENOENT;
14791da177e4SLinus Torvalds 
1480c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
1481c8fe8f30SChristoph Hellwig 	case S_IFLNK:
14821da177e4SLinus Torvalds 		return -ELOOP;
1483c8fe8f30SChristoph Hellwig 	case S_IFDIR:
1484c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
14851da177e4SLinus Torvalds 			return -EISDIR;
1486c8fe8f30SChristoph Hellwig 		break;
1487c8fe8f30SChristoph Hellwig 	case S_IFBLK:
1488c8fe8f30SChristoph Hellwig 	case S_IFCHR:
14893fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
14901da177e4SLinus Torvalds 			return -EACCES;
1491c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
1492c8fe8f30SChristoph Hellwig 	case S_IFIFO:
1493c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
14941da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
1495c8fe8f30SChristoph Hellwig 		break;
14964a3fd211SDave Hansen 	}
1497b41572e9SDave Hansen 
14983fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
1499b41572e9SDave Hansen 	if (error)
1500b41572e9SDave Hansen 		return error;
15016146f0d5SMimi Zohar 
15021da177e4SLinus Torvalds 	/*
15031da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
15041da177e4SLinus Torvalds 	 */
15051da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
15068737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
15077715b521SAl Viro 			return -EPERM;
15081da177e4SLinus Torvalds 		if (flag & O_TRUNC)
15097715b521SAl Viro 			return -EPERM;
15101da177e4SLinus Torvalds 	}
15111da177e4SLinus Torvalds 
15121da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
15137715b521SAl Viro 	if (flag & O_NOATIME && !is_owner_or_cap(inode))
15147715b521SAl Viro 		return -EPERM;
15151da177e4SLinus Torvalds 
15161da177e4SLinus Torvalds 	/*
15171da177e4SLinus Torvalds 	 * Ensure there are no outstanding leases on the file.
15181da177e4SLinus Torvalds 	 */
1519b65a9cfcSAl Viro 	return break_lease(inode, flag);
15207715b521SAl Viro }
15217715b521SAl Viro 
15227715b521SAl Viro static int handle_truncate(struct path *path)
15237715b521SAl Viro {
15247715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
15257715b521SAl Viro 	int error = get_write_access(inode);
15261da177e4SLinus Torvalds 	if (error)
15277715b521SAl Viro 		return error;
15281da177e4SLinus Torvalds 	/*
15291da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
15301da177e4SLinus Torvalds 	 */
15311da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
1532be6d3e56SKentaro Takeda 	if (!error)
15333fb64190SChristoph Hellwig 		error = security_path_truncate(path, 0,
1534be6d3e56SKentaro Takeda 				       ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
15351da177e4SLinus Torvalds 	if (!error) {
15367715b521SAl Viro 		error = do_truncate(path->dentry, 0,
1537d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
1538d139d7ffSMiklos Szeredi 				    NULL);
15391da177e4SLinus Torvalds 	}
15401da177e4SLinus Torvalds 	put_write_access(inode);
1541acd0c935SMimi Zohar 	return error;
15421da177e4SLinus Torvalds }
15431da177e4SLinus Torvalds 
1544d57999e1SDave Hansen /*
1545d57999e1SDave Hansen  * Be careful about ever adding any more callers of this
1546d57999e1SDave Hansen  * function.  Its flags must be in the namei format, not
1547d57999e1SDave Hansen  * what get passed to sys_open().
1548d57999e1SDave Hansen  */
1549d57999e1SDave Hansen static int __open_namei_create(struct nameidata *nd, struct path *path,
15508737c930SAl Viro 				int open_flag, int mode)
1551aab520e2SDave Hansen {
1552aab520e2SDave Hansen 	int error;
15534ac91378SJan Blunck 	struct dentry *dir = nd->path.dentry;
1554aab520e2SDave Hansen 
1555aab520e2SDave Hansen 	if (!IS_POSIXACL(dir->d_inode))
1556ce3b0f8dSAl Viro 		mode &= ~current_umask();
1557be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd->path, path->dentry, mode, 0);
1558be6d3e56SKentaro Takeda 	if (error)
1559be6d3e56SKentaro Takeda 		goto out_unlock;
1560aab520e2SDave Hansen 	error = vfs_create(dir->d_inode, path->dentry, mode, nd);
1561be6d3e56SKentaro Takeda out_unlock:
1562aab520e2SDave Hansen 	mutex_unlock(&dir->d_inode->i_mutex);
15634ac91378SJan Blunck 	dput(nd->path.dentry);
15644ac91378SJan Blunck 	nd->path.dentry = path->dentry;
1565aab520e2SDave Hansen 	if (error)
1566aab520e2SDave Hansen 		return error;
1567aab520e2SDave Hansen 	/* Don't check for write permission, don't truncate */
15688737c930SAl Viro 	return may_open(&nd->path, 0, open_flag & ~O_TRUNC);
1569aab520e2SDave Hansen }
1570aab520e2SDave Hansen 
15711da177e4SLinus Torvalds /*
1572d57999e1SDave Hansen  * Note that while the flag value (low two bits) for sys_open means:
1573d57999e1SDave Hansen  *	00 - read-only
1574d57999e1SDave Hansen  *	01 - write-only
1575d57999e1SDave Hansen  *	10 - read-write
1576d57999e1SDave Hansen  *	11 - special
1577d57999e1SDave Hansen  * it is changed into
1578d57999e1SDave Hansen  *	00 - no permissions needed
1579d57999e1SDave Hansen  *	01 - read-permission
1580d57999e1SDave Hansen  *	10 - write-permission
1581d57999e1SDave Hansen  *	11 - read-write
1582d57999e1SDave Hansen  * for the internal routines (ie open_namei()/follow_link() etc)
1583d57999e1SDave Hansen  * This is more logical, and also allows the 00 "no perm needed"
1584d57999e1SDave Hansen  * to be used for symlinks (where the permissions are checked
1585d57999e1SDave Hansen  * later).
1586d57999e1SDave Hansen  *
1587d57999e1SDave Hansen */
1588d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
1589d57999e1SDave Hansen {
1590d57999e1SDave Hansen 	if ((flag+1) & O_ACCMODE)
1591d57999e1SDave Hansen 		flag++;
1592d57999e1SDave Hansen 	return flag;
1593d57999e1SDave Hansen }
1594d57999e1SDave Hansen 
15957715b521SAl Viro static int open_will_truncate(int flag, struct inode *inode)
15964a3fd211SDave Hansen {
1597d57999e1SDave Hansen 	/*
15984a3fd211SDave Hansen 	 * We'll never write to the fs underlying
15994a3fd211SDave Hansen 	 * a device file.
16004a3fd211SDave Hansen 	 */
16014a3fd211SDave Hansen 	if (special_file(inode->i_mode))
16024a3fd211SDave Hansen 		return 0;
16034a3fd211SDave Hansen 	return (flag & O_TRUNC);
16044a3fd211SDave Hansen }
16054a3fd211SDave Hansen 
16064a3fd211SDave Hansen /*
16074a3fd211SDave Hansen  * Note that the low bits of the passed in "open_flag"
16084a3fd211SDave Hansen  * are not the same as in the local variable "flag". See
16094a3fd211SDave Hansen  * open_to_namei_flags() for more details.
16101da177e4SLinus Torvalds  */
1611a70e65dfSChristoph Hellwig struct file *do_filp_open(int dfd, const char *pathname,
16126e8341a1SAl Viro 		int open_flag, int mode, int acc_mode)
16131da177e4SLinus Torvalds {
16144a3fd211SDave Hansen 	struct file *filp;
1615a70e65dfSChristoph Hellwig 	struct nameidata nd;
16166e8341a1SAl Viro 	int error;
16179850c056SAl Viro 	struct path path;
16181da177e4SLinus Torvalds 	struct dentry *dir;
16191da177e4SLinus Torvalds 	int count = 0;
16207715b521SAl Viro 	int will_truncate;
1621d57999e1SDave Hansen 	int flag = open_to_namei_flags(open_flag);
16229850c056SAl Viro 	int force_reval = 0;
16231da177e4SLinus Torvalds 
16246b2f3d1fSChristoph Hellwig 	/*
16256b2f3d1fSChristoph Hellwig 	 * O_SYNC is implemented as __O_SYNC|O_DSYNC.  As many places only
16266b2f3d1fSChristoph Hellwig 	 * check for O_DSYNC if the need any syncing at all we enforce it's
16276b2f3d1fSChristoph Hellwig 	 * always set instead of having to deal with possibly weird behaviour
16286b2f3d1fSChristoph Hellwig 	 * for malicious applications setting only __O_SYNC.
16296b2f3d1fSChristoph Hellwig 	 */
16306b2f3d1fSChristoph Hellwig 	if (open_flag & __O_SYNC)
16316b2f3d1fSChristoph Hellwig 		open_flag |= O_DSYNC;
16326b2f3d1fSChristoph Hellwig 
16336e8341a1SAl Viro 	if (!acc_mode)
16346d125529SAl Viro 		acc_mode = MAY_OPEN | ACC_MODE(open_flag);
16351da177e4SLinus Torvalds 
1636834f2a4aSTrond Myklebust 	/* O_TRUNC implies we need access checks for write permissions */
1637834f2a4aSTrond Myklebust 	if (flag & O_TRUNC)
1638834f2a4aSTrond Myklebust 		acc_mode |= MAY_WRITE;
1639834f2a4aSTrond Myklebust 
16401da177e4SLinus Torvalds 	/* Allow the LSM permission hook to distinguish append
16411da177e4SLinus Torvalds 	   access from general write access. */
16421da177e4SLinus Torvalds 	if (flag & O_APPEND)
16431da177e4SLinus Torvalds 		acc_mode |= MAY_APPEND;
16441da177e4SLinus Torvalds 
16451da177e4SLinus Torvalds 	/*
16461da177e4SLinus Torvalds 	 * The simplest case - just a plain lookup.
16471da177e4SLinus Torvalds 	 */
16481da177e4SLinus Torvalds 	if (!(flag & O_CREAT)) {
16492dd6d1f4SAl Viro 		filp = get_empty_filp();
16502dd6d1f4SAl Viro 
16512dd6d1f4SAl Viro 		if (filp == NULL)
16522dd6d1f4SAl Viro 			return ERR_PTR(-ENFILE);
16532dd6d1f4SAl Viro 		nd.intent.open.file = filp;
1654482928d5SAl Viro 		filp->f_flags = open_flag;
16552dd6d1f4SAl Viro 		nd.intent.open.flags = flag;
16562dd6d1f4SAl Viro 		nd.intent.open.create_mode = 0;
16572dd6d1f4SAl Viro 		error = do_path_lookup(dfd, pathname,
16582dd6d1f4SAl Viro 					lookup_flags(flag)|LOOKUP_OPEN, &nd);
16592dd6d1f4SAl Viro 		if (IS_ERR(nd.intent.open.file)) {
16602dd6d1f4SAl Viro 			if (error == 0) {
16612dd6d1f4SAl Viro 				error = PTR_ERR(nd.intent.open.file);
16622dd6d1f4SAl Viro 				path_put(&nd.path);
16632dd6d1f4SAl Viro 			}
16642dd6d1f4SAl Viro 		} else if (error)
16652dd6d1f4SAl Viro 			release_open_intent(&nd);
16661da177e4SLinus Torvalds 		if (error)
1667a70e65dfSChristoph Hellwig 			return ERR_PTR(error);
16681da177e4SLinus Torvalds 		goto ok;
16691da177e4SLinus Torvalds 	}
16701da177e4SLinus Torvalds 
16711da177e4SLinus Torvalds 	/*
16721da177e4SLinus Torvalds 	 * Create - we need to know the parent.
16731da177e4SLinus Torvalds 	 */
16749850c056SAl Viro reval:
16759b4a9b14SAl Viro 	error = path_init(dfd, pathname, LOOKUP_PARENT, &nd);
16761da177e4SLinus Torvalds 	if (error)
1677a70e65dfSChristoph Hellwig 		return ERR_PTR(error);
16789850c056SAl Viro 	if (force_reval)
16799850c056SAl Viro 		nd.flags |= LOOKUP_REVAL;
16809b4a9b14SAl Viro 	error = path_walk(pathname, &nd);
1681654f562cSJ. R. Okajima 	if (error) {
1682654f562cSJ. R. Okajima 		if (nd.root.mnt)
1683654f562cSJ. R. Okajima 			path_put(&nd.root);
16849b4a9b14SAl Viro 		return ERR_PTR(error);
1685654f562cSJ. R. Okajima 	}
16869b4a9b14SAl Viro 	if (unlikely(!audit_dummy_context()))
16879b4a9b14SAl Viro 		audit_inode(pathname, nd.path.dentry);
16881da177e4SLinus Torvalds 
16891da177e4SLinus Torvalds 	/*
16901da177e4SLinus Torvalds 	 * We have the parent and last component. First of all, check
16911da177e4SLinus Torvalds 	 * that we are not asked to creat(2) an obvious directory - that
16921da177e4SLinus Torvalds 	 * will not do.
16931da177e4SLinus Torvalds 	 */
16941da177e4SLinus Torvalds 	error = -EISDIR;
1695a70e65dfSChristoph Hellwig 	if (nd.last_type != LAST_NORM || nd.last.name[nd.last.len])
16968737f3a1SAl Viro 		goto exit_parent;
16971da177e4SLinus Torvalds 
16988737f3a1SAl Viro 	error = -ENFILE;
16998737f3a1SAl Viro 	filp = get_empty_filp();
17008737f3a1SAl Viro 	if (filp == NULL)
17018737f3a1SAl Viro 		goto exit_parent;
17028737f3a1SAl Viro 	nd.intent.open.file = filp;
1703482928d5SAl Viro 	filp->f_flags = open_flag;
17048737f3a1SAl Viro 	nd.intent.open.flags = flag;
17058737f3a1SAl Viro 	nd.intent.open.create_mode = mode;
1706a70e65dfSChristoph Hellwig 	dir = nd.path.dentry;
1707a70e65dfSChristoph Hellwig 	nd.flags &= ~LOOKUP_PARENT;
17088737f3a1SAl Viro 	nd.flags |= LOOKUP_CREATE | LOOKUP_OPEN;
17093516586aSAl Viro 	if (flag & O_EXCL)
17103516586aSAl Viro 		nd.flags |= LOOKUP_EXCL;
17111b1dcc1bSJes Sorensen 	mutex_lock(&dir->d_inode->i_mutex);
1712a70e65dfSChristoph Hellwig 	path.dentry = lookup_hash(&nd);
1713a70e65dfSChristoph Hellwig 	path.mnt = nd.path.mnt;
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds do_last:
17164e7506e4SAl Viro 	error = PTR_ERR(path.dentry);
17174e7506e4SAl Viro 	if (IS_ERR(path.dentry)) {
17181b1dcc1bSJes Sorensen 		mutex_unlock(&dir->d_inode->i_mutex);
17191da177e4SLinus Torvalds 		goto exit;
17201da177e4SLinus Torvalds 	}
17211da177e4SLinus Torvalds 
1722a70e65dfSChristoph Hellwig 	if (IS_ERR(nd.intent.open.file)) {
1723a70e65dfSChristoph Hellwig 		error = PTR_ERR(nd.intent.open.file);
17244a3fd211SDave Hansen 		goto exit_mutex_unlock;
17254af4c52fSOleg Drokin 	}
17264af4c52fSOleg Drokin 
17271da177e4SLinus Torvalds 	/* Negative dentry, just create the file */
17284e7506e4SAl Viro 	if (!path.dentry->d_inode) {
17294a3fd211SDave Hansen 		/*
17304a3fd211SDave Hansen 		 * This write is needed to ensure that a
17314a3fd211SDave Hansen 		 * ro->rw transition does not occur between
17324a3fd211SDave Hansen 		 * the time when the file is created and when
17334a3fd211SDave Hansen 		 * a permanent write count is taken through
17344a3fd211SDave Hansen 		 * the 'struct file' in nameidata_to_filp().
17354a3fd211SDave Hansen 		 */
17364a3fd211SDave Hansen 		error = mnt_want_write(nd.path.mnt);
17371da177e4SLinus Torvalds 		if (error)
17384a3fd211SDave Hansen 			goto exit_mutex_unlock;
17398737c930SAl Viro 		error = __open_namei_create(&nd, &path, open_flag, mode);
17404a3fd211SDave Hansen 		if (error) {
17414a3fd211SDave Hansen 			mnt_drop_write(nd.path.mnt);
17421da177e4SLinus Torvalds 			goto exit;
17434a3fd211SDave Hansen 		}
1744482928d5SAl Viro 		filp = nameidata_to_filp(&nd);
17454a3fd211SDave Hansen 		mnt_drop_write(nd.path.mnt);
1746654f562cSJ. R. Okajima 		if (nd.root.mnt)
1747654f562cSJ. R. Okajima 			path_put(&nd.root);
1748b65a9cfcSAl Viro 		if (!IS_ERR(filp)) {
17499bbb6cadSMimi Zohar 			error = ima_file_check(filp, acc_mode);
1750b65a9cfcSAl Viro 			if (error) {
1751b65a9cfcSAl Viro 				fput(filp);
1752b65a9cfcSAl Viro 				filp = ERR_PTR(error);
1753b65a9cfcSAl Viro 			}
1754b65a9cfcSAl Viro 		}
17554a3fd211SDave Hansen 		return filp;
17561da177e4SLinus Torvalds 	}
17571da177e4SLinus Torvalds 
17581da177e4SLinus Torvalds 	/*
17591da177e4SLinus Torvalds 	 * It already exists.
17601da177e4SLinus Torvalds 	 */
17611b1dcc1bSJes Sorensen 	mutex_unlock(&dir->d_inode->i_mutex);
17625a190ae6SAl Viro 	audit_inode(pathname, path.dentry);
17631da177e4SLinus Torvalds 
17641da177e4SLinus Torvalds 	error = -EEXIST;
17651da177e4SLinus Torvalds 	if (flag & O_EXCL)
17661da177e4SLinus Torvalds 		goto exit_dput;
17671da177e4SLinus Torvalds 
1768e13b210fSAl Viro 	if (__follow_mount(&path)) {
17691da177e4SLinus Torvalds 		error = -ELOOP;
1770ba7a4c1aSAl Viro 		if (flag & O_NOFOLLOW)
1771ba7a4c1aSAl Viro 			goto exit_dput;
17721da177e4SLinus Torvalds 	}
17733e2efce0SAmy Griffis 
17741da177e4SLinus Torvalds 	error = -ENOENT;
17754e7506e4SAl Viro 	if (!path.dentry->d_inode)
17761da177e4SLinus Torvalds 		goto exit_dput;
1777acfa4380SAl Viro 	if (path.dentry->d_inode->i_op->follow_link)
17781da177e4SLinus Torvalds 		goto do_link;
17791da177e4SLinus Torvalds 
1780a70e65dfSChristoph Hellwig 	path_to_nameidata(&path, &nd);
17811da177e4SLinus Torvalds 	error = -EISDIR;
1782cb59861fSJeff Layton 	if (S_ISDIR(path.dentry->d_inode->i_mode))
17831da177e4SLinus Torvalds 		goto exit;
17841da177e4SLinus Torvalds ok:
17854a3fd211SDave Hansen 	/*
17864a3fd211SDave Hansen 	 * Consider:
17874a3fd211SDave Hansen 	 * 1. may_open() truncates a file
17884a3fd211SDave Hansen 	 * 2. a rw->ro mount transition occurs
17894a3fd211SDave Hansen 	 * 3. nameidata_to_filp() fails due to
17904a3fd211SDave Hansen 	 *    the ro mount.
17914a3fd211SDave Hansen 	 * That would be inconsistent, and should
17924a3fd211SDave Hansen 	 * be avoided. Taking this mnt write here
17934a3fd211SDave Hansen 	 * ensures that (2) can not occur.
17944a3fd211SDave Hansen 	 */
17957715b521SAl Viro 	will_truncate = open_will_truncate(flag, nd.path.dentry->d_inode);
17967715b521SAl Viro 	if (will_truncate) {
17974a3fd211SDave Hansen 		error = mnt_want_write(nd.path.mnt);
17981da177e4SLinus Torvalds 		if (error)
17991da177e4SLinus Torvalds 			goto exit;
18004a3fd211SDave Hansen 	}
18018737c930SAl Viro 	error = may_open(&nd.path, acc_mode, open_flag);
18024a3fd211SDave Hansen 	if (error) {
18037715b521SAl Viro 		if (will_truncate)
18044a3fd211SDave Hansen 			mnt_drop_write(nd.path.mnt);
18054a3fd211SDave Hansen 		goto exit;
18064a3fd211SDave Hansen 	}
1807482928d5SAl Viro 	filp = nameidata_to_filp(&nd);
1808b65a9cfcSAl Viro 	if (!IS_ERR(filp)) {
18099bbb6cadSMimi Zohar 		error = ima_file_check(filp, acc_mode);
1810b65a9cfcSAl Viro 		if (error) {
1811b65a9cfcSAl Viro 			fput(filp);
1812b65a9cfcSAl Viro 			filp = ERR_PTR(error);
18137715b521SAl Viro 		}
1814b65a9cfcSAl Viro 	}
1815b65a9cfcSAl Viro 	if (!IS_ERR(filp)) {
18167715b521SAl Viro 		if (acc_mode & MAY_WRITE)
18177715b521SAl Viro 			vfs_dq_init(nd.path.dentry->d_inode);
18187715b521SAl Viro 
18197715b521SAl Viro 		if (will_truncate) {
18207715b521SAl Viro 			error = handle_truncate(&nd.path);
18217715b521SAl Viro 			if (error) {
18227715b521SAl Viro 				fput(filp);
1823b65a9cfcSAl Viro 				filp = ERR_PTR(error);
1824b65a9cfcSAl Viro 			}
18257715b521SAl Viro 		}
18267715b521SAl Viro 	}
18274a3fd211SDave Hansen 	/*
18284a3fd211SDave Hansen 	 * It is now safe to drop the mnt write
18294a3fd211SDave Hansen 	 * because the filp has had a write taken
18304a3fd211SDave Hansen 	 * on its behalf.
18314a3fd211SDave Hansen 	 */
18327715b521SAl Viro 	if (will_truncate)
18334a3fd211SDave Hansen 		mnt_drop_write(nd.path.mnt);
1834654f562cSJ. R. Okajima 	if (nd.root.mnt)
1835654f562cSJ. R. Okajima 		path_put(&nd.root);
18364a3fd211SDave Hansen 	return filp;
18371da177e4SLinus Torvalds 
18384a3fd211SDave Hansen exit_mutex_unlock:
18394a3fd211SDave Hansen 	mutex_unlock(&dir->d_inode->i_mutex);
18401da177e4SLinus Torvalds exit_dput:
1841a70e65dfSChristoph Hellwig 	path_put_conditional(&path, &nd);
18421da177e4SLinus Torvalds exit:
1843a70e65dfSChristoph Hellwig 	if (!IS_ERR(nd.intent.open.file))
1844a70e65dfSChristoph Hellwig 		release_open_intent(&nd);
18458737f3a1SAl Viro exit_parent:
18462a737871SAl Viro 	if (nd.root.mnt)
18472a737871SAl Viro 		path_put(&nd.root);
1848a70e65dfSChristoph Hellwig 	path_put(&nd.path);
1849a70e65dfSChristoph Hellwig 	return ERR_PTR(error);
18501da177e4SLinus Torvalds 
18511da177e4SLinus Torvalds do_link:
18521da177e4SLinus Torvalds 	error = -ELOOP;
18531da177e4SLinus Torvalds 	if (flag & O_NOFOLLOW)
18541da177e4SLinus Torvalds 		goto exit_dput;
18551da177e4SLinus Torvalds 	/*
18561da177e4SLinus Torvalds 	 * This is subtle. Instead of calling do_follow_link() we do the
18571da177e4SLinus Torvalds 	 * thing by hands. The reason is that this way we have zero link_count
18581da177e4SLinus Torvalds 	 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
18591da177e4SLinus Torvalds 	 * After that we have the parent and last component, i.e.
18601da177e4SLinus Torvalds 	 * we are in the same situation as after the first path_walk().
18611da177e4SLinus Torvalds 	 * Well, almost - if the last component is normal we get its copy
18621da177e4SLinus Torvalds 	 * stored in nd->last.name and we will have to putname() it when we
18631da177e4SLinus Torvalds 	 * are done. Procfs-like symlinks just set LAST_BIND.
18641da177e4SLinus Torvalds 	 */
1865a70e65dfSChristoph Hellwig 	nd.flags |= LOOKUP_PARENT;
1866a70e65dfSChristoph Hellwig 	error = security_inode_follow_link(path.dentry, &nd);
18671da177e4SLinus Torvalds 	if (error)
18681da177e4SLinus Torvalds 		goto exit_dput;
1869a70e65dfSChristoph Hellwig 	error = __do_follow_link(&path, &nd);
1870258fa999SAl Viro 	path_put(&path);
1871de459215SKirill Korotaev 	if (error) {
1872de459215SKirill Korotaev 		/* Does someone understand code flow here? Or it is only
1873de459215SKirill Korotaev 		 * me so stupid? Anathema to whoever designed this non-sense
1874de459215SKirill Korotaev 		 * with "intent.open".
1875de459215SKirill Korotaev 		 */
1876a70e65dfSChristoph Hellwig 		release_open_intent(&nd);
1877654f562cSJ. R. Okajima 		if (nd.root.mnt)
1878654f562cSJ. R. Okajima 			path_put(&nd.root);
18799850c056SAl Viro 		if (error == -ESTALE && !force_reval) {
18809850c056SAl Viro 			force_reval = 1;
18819850c056SAl Viro 			goto reval;
18829850c056SAl Viro 		}
1883a70e65dfSChristoph Hellwig 		return ERR_PTR(error);
1884de459215SKirill Korotaev 	}
1885a70e65dfSChristoph Hellwig 	nd.flags &= ~LOOKUP_PARENT;
1886a70e65dfSChristoph Hellwig 	if (nd.last_type == LAST_BIND)
18871da177e4SLinus Torvalds 		goto ok;
18881da177e4SLinus Torvalds 	error = -EISDIR;
1889a70e65dfSChristoph Hellwig 	if (nd.last_type != LAST_NORM)
18901da177e4SLinus Torvalds 		goto exit;
1891a70e65dfSChristoph Hellwig 	if (nd.last.name[nd.last.len]) {
1892a70e65dfSChristoph Hellwig 		__putname(nd.last.name);
18931da177e4SLinus Torvalds 		goto exit;
18941da177e4SLinus Torvalds 	}
18951da177e4SLinus Torvalds 	error = -ELOOP;
18961da177e4SLinus Torvalds 	if (count++==32) {
1897a70e65dfSChristoph Hellwig 		__putname(nd.last.name);
18981da177e4SLinus Torvalds 		goto exit;
18991da177e4SLinus Torvalds 	}
1900a70e65dfSChristoph Hellwig 	dir = nd.path.dentry;
19011b1dcc1bSJes Sorensen 	mutex_lock(&dir->d_inode->i_mutex);
1902a70e65dfSChristoph Hellwig 	path.dentry = lookup_hash(&nd);
1903a70e65dfSChristoph Hellwig 	path.mnt = nd.path.mnt;
1904a70e65dfSChristoph Hellwig 	__putname(nd.last.name);
19051da177e4SLinus Torvalds 	goto do_last;
19061da177e4SLinus Torvalds }
19071da177e4SLinus Torvalds 
19081da177e4SLinus Torvalds /**
1909a70e65dfSChristoph Hellwig  * filp_open - open file and return file pointer
1910a70e65dfSChristoph Hellwig  *
1911a70e65dfSChristoph Hellwig  * @filename:	path to open
1912a70e65dfSChristoph Hellwig  * @flags:	open flags as per the open(2) second argument
1913a70e65dfSChristoph Hellwig  * @mode:	mode for the new file if O_CREAT is set, else ignored
1914a70e65dfSChristoph Hellwig  *
1915a70e65dfSChristoph Hellwig  * This is the helper to open a file from kernelspace if you really
1916a70e65dfSChristoph Hellwig  * have to.  But in generally you should not do this, so please move
1917a70e65dfSChristoph Hellwig  * along, nothing to see here..
1918a70e65dfSChristoph Hellwig  */
1919a70e65dfSChristoph Hellwig struct file *filp_open(const char *filename, int flags, int mode)
1920a70e65dfSChristoph Hellwig {
19216e8341a1SAl Viro 	return do_filp_open(AT_FDCWD, filename, flags, mode, 0);
1922a70e65dfSChristoph Hellwig }
1923a70e65dfSChristoph Hellwig EXPORT_SYMBOL(filp_open);
1924a70e65dfSChristoph Hellwig 
1925a70e65dfSChristoph Hellwig /**
19261da177e4SLinus Torvalds  * lookup_create - lookup a dentry, creating it if it doesn't exist
19271da177e4SLinus Torvalds  * @nd: nameidata info
19281da177e4SLinus Torvalds  * @is_dir: directory flag
19291da177e4SLinus Torvalds  *
19301da177e4SLinus Torvalds  * Simple function to lookup and return a dentry and create it
19311da177e4SLinus Torvalds  * if it doesn't exist.  Is SMP-safe.
1932c663e5d8SChristoph Hellwig  *
19334ac91378SJan Blunck  * Returns with nd->path.dentry->d_inode->i_mutex locked.
19341da177e4SLinus Torvalds  */
19351da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir)
19361da177e4SLinus Torvalds {
1937c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
19381da177e4SLinus Torvalds 
19394ac91378SJan Blunck 	mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
1940c663e5d8SChristoph Hellwig 	/*
1941c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
1942c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
1943c663e5d8SChristoph Hellwig 	 */
19441da177e4SLinus Torvalds 	if (nd->last_type != LAST_NORM)
19451da177e4SLinus Torvalds 		goto fail;
19461da177e4SLinus Torvalds 	nd->flags &= ~LOOKUP_PARENT;
19473516586aSAl Viro 	nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
1948a634904aSASANO Masahiro 	nd->intent.open.flags = O_EXCL;
1949c663e5d8SChristoph Hellwig 
1950c663e5d8SChristoph Hellwig 	/*
1951c663e5d8SChristoph Hellwig 	 * Do the final lookup.
1952c663e5d8SChristoph Hellwig 	 */
195349705b77SChristoph Hellwig 	dentry = lookup_hash(nd);
19541da177e4SLinus Torvalds 	if (IS_ERR(dentry))
19551da177e4SLinus Torvalds 		goto fail;
1956c663e5d8SChristoph Hellwig 
1957e9baf6e5SAl Viro 	if (dentry->d_inode)
1958e9baf6e5SAl Viro 		goto eexist;
1959c663e5d8SChristoph Hellwig 	/*
1960c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
1961c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
1962c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
1963c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
1964c663e5d8SChristoph Hellwig 	 */
1965e9baf6e5SAl Viro 	if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
19661da177e4SLinus Torvalds 		dput(dentry);
19671da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
1968e9baf6e5SAl Viro 	}
1969e9baf6e5SAl Viro 	return dentry;
1970e9baf6e5SAl Viro eexist:
1971e9baf6e5SAl Viro 	dput(dentry);
1972e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
19731da177e4SLinus Torvalds fail:
19741da177e4SLinus Torvalds 	return dentry;
19751da177e4SLinus Torvalds }
1976f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create);
19771da177e4SLinus Torvalds 
19781da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
19791da177e4SLinus Torvalds {
1980a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
19811da177e4SLinus Torvalds 
19821da177e4SLinus Torvalds 	if (error)
19831da177e4SLinus Torvalds 		return error;
19841da177e4SLinus Torvalds 
19851da177e4SLinus Torvalds 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
19861da177e4SLinus Torvalds 		return -EPERM;
19871da177e4SLinus Torvalds 
1988acfa4380SAl Viro 	if (!dir->i_op->mknod)
19891da177e4SLinus Torvalds 		return -EPERM;
19901da177e4SLinus Torvalds 
199108ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
199208ce5f16SSerge E. Hallyn 	if (error)
199308ce5f16SSerge E. Hallyn 		return error;
199408ce5f16SSerge E. Hallyn 
19951da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
19961da177e4SLinus Torvalds 	if (error)
19971da177e4SLinus Torvalds 		return error;
19981da177e4SLinus Torvalds 
19999e3509e2SJan Kara 	vfs_dq_init(dir);
20001da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2001a74574aaSStephen Smalley 	if (!error)
2002f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
20031da177e4SLinus Torvalds 	return error;
20041da177e4SLinus Torvalds }
20051da177e4SLinus Torvalds 
2006463c3197SDave Hansen static int may_mknod(mode_t mode)
2007463c3197SDave Hansen {
2008463c3197SDave Hansen 	switch (mode & S_IFMT) {
2009463c3197SDave Hansen 	case S_IFREG:
2010463c3197SDave Hansen 	case S_IFCHR:
2011463c3197SDave Hansen 	case S_IFBLK:
2012463c3197SDave Hansen 	case S_IFIFO:
2013463c3197SDave Hansen 	case S_IFSOCK:
2014463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2015463c3197SDave Hansen 		return 0;
2016463c3197SDave Hansen 	case S_IFDIR:
2017463c3197SDave Hansen 		return -EPERM;
2018463c3197SDave Hansen 	default:
2019463c3197SDave Hansen 		return -EINVAL;
2020463c3197SDave Hansen 	}
2021463c3197SDave Hansen }
2022463c3197SDave Hansen 
20232e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
20242e4d0924SHeiko Carstens 		unsigned, dev)
20251da177e4SLinus Torvalds {
20262ad94ae6SAl Viro 	int error;
20271da177e4SLinus Torvalds 	char *tmp;
20281da177e4SLinus Torvalds 	struct dentry *dentry;
20291da177e4SLinus Torvalds 	struct nameidata nd;
20301da177e4SLinus Torvalds 
20311da177e4SLinus Torvalds 	if (S_ISDIR(mode))
20321da177e4SLinus Torvalds 		return -EPERM;
20331da177e4SLinus Torvalds 
20342ad94ae6SAl Viro 	error = user_path_parent(dfd, filename, &nd, &tmp);
20351da177e4SLinus Torvalds 	if (error)
20362ad94ae6SAl Viro 		return error;
20372ad94ae6SAl Viro 
20381da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
2039463c3197SDave Hansen 	if (IS_ERR(dentry)) {
20401da177e4SLinus Torvalds 		error = PTR_ERR(dentry);
2041463c3197SDave Hansen 		goto out_unlock;
2042463c3197SDave Hansen 	}
20434ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2044ce3b0f8dSAl Viro 		mode &= ~current_umask();
2045463c3197SDave Hansen 	error = may_mknod(mode);
2046463c3197SDave Hansen 	if (error)
2047463c3197SDave Hansen 		goto out_dput;
2048463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2049463c3197SDave Hansen 	if (error)
2050463c3197SDave Hansen 		goto out_dput;
2051be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd.path, dentry, mode, dev);
2052be6d3e56SKentaro Takeda 	if (error)
2053be6d3e56SKentaro Takeda 		goto out_drop_write;
20541da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
20551da177e4SLinus Torvalds 		case 0: case S_IFREG:
20564ac91378SJan Blunck 			error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
20571da177e4SLinus Torvalds 			break;
20581da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
20594ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
20601da177e4SLinus Torvalds 					new_decode_dev(dev));
20611da177e4SLinus Torvalds 			break;
20621da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
20634ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
20641da177e4SLinus Torvalds 			break;
20651da177e4SLinus Torvalds 	}
2066be6d3e56SKentaro Takeda out_drop_write:
2067463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2068463c3197SDave Hansen out_dput:
20691da177e4SLinus Torvalds 	dput(dentry);
2070463c3197SDave Hansen out_unlock:
20714ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
20721d957f9bSJan Blunck 	path_put(&nd.path);
20731da177e4SLinus Torvalds 	putname(tmp);
20741da177e4SLinus Torvalds 
20751da177e4SLinus Torvalds 	return error;
20761da177e4SLinus Torvalds }
20771da177e4SLinus Torvalds 
20783480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
20795590ff0dSUlrich Drepper {
20805590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
20815590ff0dSUlrich Drepper }
20825590ff0dSUlrich Drepper 
20831da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
20841da177e4SLinus Torvalds {
2085a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
20861da177e4SLinus Torvalds 
20871da177e4SLinus Torvalds 	if (error)
20881da177e4SLinus Torvalds 		return error;
20891da177e4SLinus Torvalds 
2090acfa4380SAl Viro 	if (!dir->i_op->mkdir)
20911da177e4SLinus Torvalds 		return -EPERM;
20921da177e4SLinus Torvalds 
20931da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
20941da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
20951da177e4SLinus Torvalds 	if (error)
20961da177e4SLinus Torvalds 		return error;
20971da177e4SLinus Torvalds 
20989e3509e2SJan Kara 	vfs_dq_init(dir);
20991da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2100a74574aaSStephen Smalley 	if (!error)
2101f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
21021da177e4SLinus Torvalds 	return error;
21031da177e4SLinus Torvalds }
21041da177e4SLinus Torvalds 
21052e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
21061da177e4SLinus Torvalds {
21071da177e4SLinus Torvalds 	int error = 0;
21081da177e4SLinus Torvalds 	char * tmp;
21096902d925SDave Hansen 	struct dentry *dentry;
21106902d925SDave Hansen 	struct nameidata nd;
21111da177e4SLinus Torvalds 
21122ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &tmp);
21132ad94ae6SAl Viro 	if (error)
21146902d925SDave Hansen 		goto out_err;
21151da177e4SLinus Torvalds 
21161da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 1);
21171da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
21186902d925SDave Hansen 	if (IS_ERR(dentry))
21196902d925SDave Hansen 		goto out_unlock;
21206902d925SDave Hansen 
21214ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2122ce3b0f8dSAl Viro 		mode &= ~current_umask();
2123463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2124463c3197SDave Hansen 	if (error)
2125463c3197SDave Hansen 		goto out_dput;
2126be6d3e56SKentaro Takeda 	error = security_path_mkdir(&nd.path, dentry, mode);
2127be6d3e56SKentaro Takeda 	if (error)
2128be6d3e56SKentaro Takeda 		goto out_drop_write;
21294ac91378SJan Blunck 	error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2130be6d3e56SKentaro Takeda out_drop_write:
2131463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2132463c3197SDave Hansen out_dput:
21331da177e4SLinus Torvalds 	dput(dentry);
21346902d925SDave Hansen out_unlock:
21354ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
21361d957f9bSJan Blunck 	path_put(&nd.path);
21371da177e4SLinus Torvalds 	putname(tmp);
21386902d925SDave Hansen out_err:
21391da177e4SLinus Torvalds 	return error;
21401da177e4SLinus Torvalds }
21411da177e4SLinus Torvalds 
21423cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
21435590ff0dSUlrich Drepper {
21445590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
21455590ff0dSUlrich Drepper }
21465590ff0dSUlrich Drepper 
21471da177e4SLinus Torvalds /*
21481da177e4SLinus Torvalds  * We try to drop the dentry early: we should have
21491da177e4SLinus Torvalds  * a usage count of 2 if we're the only user of this
21501da177e4SLinus Torvalds  * dentry, and if that is true (possibly after pruning
21511da177e4SLinus Torvalds  * the dcache), then we drop the dentry now.
21521da177e4SLinus Torvalds  *
21531da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
21541da177e4SLinus Torvalds  * do a
21551da177e4SLinus Torvalds  *
21561da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
21571da177e4SLinus Torvalds  *		return -EBUSY;
21581da177e4SLinus Torvalds  *
21591da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
21601da177e4SLinus Torvalds  * that is still in use by something else..
21611da177e4SLinus Torvalds  */
21621da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
21631da177e4SLinus Torvalds {
21641da177e4SLinus Torvalds 	dget(dentry);
21651da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
21661da177e4SLinus Torvalds 	spin_lock(&dcache_lock);
21671da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
21681da177e4SLinus Torvalds 	if (atomic_read(&dentry->d_count) == 2)
21691da177e4SLinus Torvalds 		__d_drop(dentry);
21701da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
21711da177e4SLinus Torvalds 	spin_unlock(&dcache_lock);
21721da177e4SLinus Torvalds }
21731da177e4SLinus Torvalds 
21741da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
21751da177e4SLinus Torvalds {
21761da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
21771da177e4SLinus Torvalds 
21781da177e4SLinus Torvalds 	if (error)
21791da177e4SLinus Torvalds 		return error;
21801da177e4SLinus Torvalds 
2181acfa4380SAl Viro 	if (!dir->i_op->rmdir)
21821da177e4SLinus Torvalds 		return -EPERM;
21831da177e4SLinus Torvalds 
21849e3509e2SJan Kara 	vfs_dq_init(dir);
21851da177e4SLinus Torvalds 
21861b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
21871da177e4SLinus Torvalds 	dentry_unhash(dentry);
21881da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
21891da177e4SLinus Torvalds 		error = -EBUSY;
21901da177e4SLinus Torvalds 	else {
21911da177e4SLinus Torvalds 		error = security_inode_rmdir(dir, dentry);
21921da177e4SLinus Torvalds 		if (!error) {
21931da177e4SLinus Torvalds 			error = dir->i_op->rmdir(dir, dentry);
21941da177e4SLinus Torvalds 			if (!error)
21951da177e4SLinus Torvalds 				dentry->d_inode->i_flags |= S_DEAD;
21961da177e4SLinus Torvalds 		}
21971da177e4SLinus Torvalds 	}
21981b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
21991da177e4SLinus Torvalds 	if (!error) {
22001da177e4SLinus Torvalds 		d_delete(dentry);
22011da177e4SLinus Torvalds 	}
22021da177e4SLinus Torvalds 	dput(dentry);
22031da177e4SLinus Torvalds 
22041da177e4SLinus Torvalds 	return error;
22051da177e4SLinus Torvalds }
22061da177e4SLinus Torvalds 
22075590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
22081da177e4SLinus Torvalds {
22091da177e4SLinus Torvalds 	int error = 0;
22101da177e4SLinus Torvalds 	char * name;
22111da177e4SLinus Torvalds 	struct dentry *dentry;
22121da177e4SLinus Torvalds 	struct nameidata nd;
22131da177e4SLinus Torvalds 
22142ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
22151da177e4SLinus Torvalds 	if (error)
22162ad94ae6SAl Viro 		return error;
22171da177e4SLinus Torvalds 
22181da177e4SLinus Torvalds 	switch(nd.last_type) {
22191da177e4SLinus Torvalds 	case LAST_DOTDOT:
22201da177e4SLinus Torvalds 		error = -ENOTEMPTY;
22211da177e4SLinus Torvalds 		goto exit1;
22221da177e4SLinus Torvalds 	case LAST_DOT:
22231da177e4SLinus Torvalds 		error = -EINVAL;
22241da177e4SLinus Torvalds 		goto exit1;
22251da177e4SLinus Torvalds 	case LAST_ROOT:
22261da177e4SLinus Torvalds 		error = -EBUSY;
22271da177e4SLinus Torvalds 		goto exit1;
22281da177e4SLinus Torvalds 	}
22290612d9fbSOGAWA Hirofumi 
22300612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
22310612d9fbSOGAWA Hirofumi 
22324ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
223349705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
22341da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
22356902d925SDave Hansen 	if (IS_ERR(dentry))
22366902d925SDave Hansen 		goto exit2;
22370622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
22380622753bSDave Hansen 	if (error)
22390622753bSDave Hansen 		goto exit3;
2240be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2241be6d3e56SKentaro Takeda 	if (error)
2242be6d3e56SKentaro Takeda 		goto exit4;
22434ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2244be6d3e56SKentaro Takeda exit4:
22450622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
22460622753bSDave Hansen exit3:
22471da177e4SLinus Torvalds 	dput(dentry);
22486902d925SDave Hansen exit2:
22494ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
22501da177e4SLinus Torvalds exit1:
22511d957f9bSJan Blunck 	path_put(&nd.path);
22521da177e4SLinus Torvalds 	putname(name);
22531da177e4SLinus Torvalds 	return error;
22541da177e4SLinus Torvalds }
22551da177e4SLinus Torvalds 
22563cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
22575590ff0dSUlrich Drepper {
22585590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
22595590ff0dSUlrich Drepper }
22605590ff0dSUlrich Drepper 
22611da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
22621da177e4SLinus Torvalds {
22631da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
22641da177e4SLinus Torvalds 
22651da177e4SLinus Torvalds 	if (error)
22661da177e4SLinus Torvalds 		return error;
22671da177e4SLinus Torvalds 
2268acfa4380SAl Viro 	if (!dir->i_op->unlink)
22691da177e4SLinus Torvalds 		return -EPERM;
22701da177e4SLinus Torvalds 
22719e3509e2SJan Kara 	vfs_dq_init(dir);
22721da177e4SLinus Torvalds 
22731b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
22741da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
22751da177e4SLinus Torvalds 		error = -EBUSY;
22761da177e4SLinus Torvalds 	else {
22771da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
22781da177e4SLinus Torvalds 		if (!error)
22791da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
22801da177e4SLinus Torvalds 	}
22811b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
22821da177e4SLinus Torvalds 
22831da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
22841da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2285ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
22861da177e4SLinus Torvalds 		d_delete(dentry);
22871da177e4SLinus Torvalds 	}
22880eeca283SRobert Love 
22891da177e4SLinus Torvalds 	return error;
22901da177e4SLinus Torvalds }
22911da177e4SLinus Torvalds 
22921da177e4SLinus Torvalds /*
22931da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
22941b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
22951da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
22961da177e4SLinus Torvalds  * while waiting on the I/O.
22971da177e4SLinus Torvalds  */
22985590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
22991da177e4SLinus Torvalds {
23002ad94ae6SAl Viro 	int error;
23011da177e4SLinus Torvalds 	char *name;
23021da177e4SLinus Torvalds 	struct dentry *dentry;
23031da177e4SLinus Torvalds 	struct nameidata nd;
23041da177e4SLinus Torvalds 	struct inode *inode = NULL;
23051da177e4SLinus Torvalds 
23062ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
23071da177e4SLinus Torvalds 	if (error)
23082ad94ae6SAl Viro 		return error;
23092ad94ae6SAl Viro 
23101da177e4SLinus Torvalds 	error = -EISDIR;
23111da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
23121da177e4SLinus Torvalds 		goto exit1;
23130612d9fbSOGAWA Hirofumi 
23140612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
23150612d9fbSOGAWA Hirofumi 
23164ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
231749705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
23181da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
23191da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
23201da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
23211da177e4SLinus Torvalds 		if (nd.last.name[nd.last.len])
23221da177e4SLinus Torvalds 			goto slashes;
23231da177e4SLinus Torvalds 		inode = dentry->d_inode;
23241da177e4SLinus Torvalds 		if (inode)
23251da177e4SLinus Torvalds 			atomic_inc(&inode->i_count);
23260622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
23270622753bSDave Hansen 		if (error)
23280622753bSDave Hansen 			goto exit2;
2329be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2330be6d3e56SKentaro Takeda 		if (error)
2331be6d3e56SKentaro Takeda 			goto exit3;
23324ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2333be6d3e56SKentaro Takeda exit3:
23340622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
23351da177e4SLinus Torvalds 	exit2:
23361da177e4SLinus Torvalds 		dput(dentry);
23371da177e4SLinus Torvalds 	}
23384ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
23391da177e4SLinus Torvalds 	if (inode)
23401da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
23411da177e4SLinus Torvalds exit1:
23421d957f9bSJan Blunck 	path_put(&nd.path);
23431da177e4SLinus Torvalds 	putname(name);
23441da177e4SLinus Torvalds 	return error;
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds slashes:
23471da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
23481da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
23491da177e4SLinus Torvalds 	goto exit2;
23501da177e4SLinus Torvalds }
23511da177e4SLinus Torvalds 
23522e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
23535590ff0dSUlrich Drepper {
23545590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
23555590ff0dSUlrich Drepper 		return -EINVAL;
23565590ff0dSUlrich Drepper 
23575590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
23585590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
23595590ff0dSUlrich Drepper 
23605590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
23615590ff0dSUlrich Drepper }
23625590ff0dSUlrich Drepper 
23633480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
23645590ff0dSUlrich Drepper {
23655590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
23665590ff0dSUlrich Drepper }
23675590ff0dSUlrich Drepper 
2368db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
23691da177e4SLinus Torvalds {
2370a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
23711da177e4SLinus Torvalds 
23721da177e4SLinus Torvalds 	if (error)
23731da177e4SLinus Torvalds 		return error;
23741da177e4SLinus Torvalds 
2375acfa4380SAl Viro 	if (!dir->i_op->symlink)
23761da177e4SLinus Torvalds 		return -EPERM;
23771da177e4SLinus Torvalds 
23781da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
23791da177e4SLinus Torvalds 	if (error)
23801da177e4SLinus Torvalds 		return error;
23811da177e4SLinus Torvalds 
23829e3509e2SJan Kara 	vfs_dq_init(dir);
23831da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2384a74574aaSStephen Smalley 	if (!error)
2385f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
23861da177e4SLinus Torvalds 	return error;
23871da177e4SLinus Torvalds }
23881da177e4SLinus Torvalds 
23892e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
23902e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
23911da177e4SLinus Torvalds {
23922ad94ae6SAl Viro 	int error;
23931da177e4SLinus Torvalds 	char *from;
23941da177e4SLinus Torvalds 	char *to;
23956902d925SDave Hansen 	struct dentry *dentry;
23966902d925SDave Hansen 	struct nameidata nd;
23971da177e4SLinus Torvalds 
23981da177e4SLinus Torvalds 	from = getname(oldname);
23991da177e4SLinus Torvalds 	if (IS_ERR(from))
24001da177e4SLinus Torvalds 		return PTR_ERR(from);
24012ad94ae6SAl Viro 
24022ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
24032ad94ae6SAl Viro 	if (error)
24046902d925SDave Hansen 		goto out_putname;
24051da177e4SLinus Torvalds 
24061da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
24071da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
24086902d925SDave Hansen 	if (IS_ERR(dentry))
24096902d925SDave Hansen 		goto out_unlock;
24106902d925SDave Hansen 
241175c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
241275c3f29dSDave Hansen 	if (error)
241375c3f29dSDave Hansen 		goto out_dput;
2414be6d3e56SKentaro Takeda 	error = security_path_symlink(&nd.path, dentry, from);
2415be6d3e56SKentaro Takeda 	if (error)
2416be6d3e56SKentaro Takeda 		goto out_drop_write;
2417db2e747bSMiklos Szeredi 	error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2418be6d3e56SKentaro Takeda out_drop_write:
241975c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
242075c3f29dSDave Hansen out_dput:
24211da177e4SLinus Torvalds 	dput(dentry);
24226902d925SDave Hansen out_unlock:
24234ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
24241d957f9bSJan Blunck 	path_put(&nd.path);
24251da177e4SLinus Torvalds 	putname(to);
24266902d925SDave Hansen out_putname:
24271da177e4SLinus Torvalds 	putname(from);
24281da177e4SLinus Torvalds 	return error;
24291da177e4SLinus Torvalds }
24301da177e4SLinus Torvalds 
24313480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
24325590ff0dSUlrich Drepper {
24335590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
24345590ff0dSUlrich Drepper }
24355590ff0dSUlrich Drepper 
24361da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
24371da177e4SLinus Torvalds {
24381da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
24391da177e4SLinus Torvalds 	int error;
24401da177e4SLinus Torvalds 
24411da177e4SLinus Torvalds 	if (!inode)
24421da177e4SLinus Torvalds 		return -ENOENT;
24431da177e4SLinus Torvalds 
2444a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
24451da177e4SLinus Torvalds 	if (error)
24461da177e4SLinus Torvalds 		return error;
24471da177e4SLinus Torvalds 
24481da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
24491da177e4SLinus Torvalds 		return -EXDEV;
24501da177e4SLinus Torvalds 
24511da177e4SLinus Torvalds 	/*
24521da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
24531da177e4SLinus Torvalds 	 */
24541da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
24551da177e4SLinus Torvalds 		return -EPERM;
2456acfa4380SAl Viro 	if (!dir->i_op->link)
24571da177e4SLinus Torvalds 		return -EPERM;
24587e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
24591da177e4SLinus Torvalds 		return -EPERM;
24601da177e4SLinus Torvalds 
24611da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
24621da177e4SLinus Torvalds 	if (error)
24631da177e4SLinus Torvalds 		return error;
24641da177e4SLinus Torvalds 
24657e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
24669e3509e2SJan Kara 	vfs_dq_init(dir);
24671da177e4SLinus Torvalds 	error = dir->i_op->link(old_dentry, dir, new_dentry);
24687e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
2469e31e14ecSStephen Smalley 	if (!error)
24707e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
24711da177e4SLinus Torvalds 	return error;
24721da177e4SLinus Torvalds }
24731da177e4SLinus Torvalds 
24741da177e4SLinus Torvalds /*
24751da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
24761da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
24771da177e4SLinus Torvalds  * newname.  --KAB
24781da177e4SLinus Torvalds  *
24791da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
24801da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
24811da177e4SLinus Torvalds  * and other special files.  --ADM
24821da177e4SLinus Torvalds  */
24832e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
24842e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
24851da177e4SLinus Torvalds {
24861da177e4SLinus Torvalds 	struct dentry *new_dentry;
24872d8f3038SAl Viro 	struct nameidata nd;
24882d8f3038SAl Viro 	struct path old_path;
24891da177e4SLinus Torvalds 	int error;
24901da177e4SLinus Torvalds 	char *to;
24911da177e4SLinus Torvalds 
249245c9b11aSUlrich Drepper 	if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
2493c04030e1SUlrich Drepper 		return -EINVAL;
2494c04030e1SUlrich Drepper 
24952d8f3038SAl Viro 	error = user_path_at(olddfd, oldname,
249645c9b11aSUlrich Drepper 			     flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
24972d8f3038SAl Viro 			     &old_path);
24981da177e4SLinus Torvalds 	if (error)
24992ad94ae6SAl Viro 		return error;
25002ad94ae6SAl Viro 
25012ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
25021da177e4SLinus Torvalds 	if (error)
25031da177e4SLinus Torvalds 		goto out;
25041da177e4SLinus Torvalds 	error = -EXDEV;
25052d8f3038SAl Viro 	if (old_path.mnt != nd.path.mnt)
25061da177e4SLinus Torvalds 		goto out_release;
25071da177e4SLinus Torvalds 	new_dentry = lookup_create(&nd, 0);
25081da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
25096902d925SDave Hansen 	if (IS_ERR(new_dentry))
25106902d925SDave Hansen 		goto out_unlock;
251175c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
251275c3f29dSDave Hansen 	if (error)
251375c3f29dSDave Hansen 		goto out_dput;
2514be6d3e56SKentaro Takeda 	error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2515be6d3e56SKentaro Takeda 	if (error)
2516be6d3e56SKentaro Takeda 		goto out_drop_write;
25172d8f3038SAl Viro 	error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
2518be6d3e56SKentaro Takeda out_drop_write:
251975c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
252075c3f29dSDave Hansen out_dput:
25211da177e4SLinus Torvalds 	dput(new_dentry);
25226902d925SDave Hansen out_unlock:
25234ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
25241da177e4SLinus Torvalds out_release:
25251d957f9bSJan Blunck 	path_put(&nd.path);
25262ad94ae6SAl Viro 	putname(to);
25271da177e4SLinus Torvalds out:
25282d8f3038SAl Viro 	path_put(&old_path);
25291da177e4SLinus Torvalds 
25301da177e4SLinus Torvalds 	return error;
25311da177e4SLinus Torvalds }
25321da177e4SLinus Torvalds 
25333480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
25345590ff0dSUlrich Drepper {
2535c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
25365590ff0dSUlrich Drepper }
25375590ff0dSUlrich Drepper 
25381da177e4SLinus Torvalds /*
25391da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
25401da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
25411da177e4SLinus Torvalds  * Problems:
25421da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
25431da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
25441da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
2545a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
25461da177e4SLinus Torvalds  *	   story.
25471da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
25481b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
25491da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
25501da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
2551a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
25521da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
25531da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
25541da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
2555a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
25561da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
25571da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
25581da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
25591da177e4SLinus Torvalds  *	d) some filesystems don't support opened-but-unlinked directories,
25601da177e4SLinus Torvalds  *	   either because of layout or because they are not ready to deal with
25611da177e4SLinus Torvalds  *	   all cases correctly. The latter will be fixed (taking this sort of
25621da177e4SLinus Torvalds  *	   stuff into VFS), but the former is not going away. Solution: the same
25631da177e4SLinus Torvalds  *	   trick as in rmdir().
25641da177e4SLinus Torvalds  *	e) conversion from fhandle to dentry may come in the wrong moment - when
25651b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
25661da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
25671b1dcc1bSJes Sorensen  *	   ->i_mutex on parents, which works but leads to some truely excessive
25681da177e4SLinus Torvalds  *	   locking].
25691da177e4SLinus Torvalds  */
257075c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
25711da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
25721da177e4SLinus Torvalds {
25731da177e4SLinus Torvalds 	int error = 0;
25741da177e4SLinus Torvalds 	struct inode *target;
25751da177e4SLinus Torvalds 
25761da177e4SLinus Torvalds 	/*
25771da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
25781da177e4SLinus Torvalds 	 * we'll need to flip '..'.
25791da177e4SLinus Torvalds 	 */
25801da177e4SLinus Torvalds 	if (new_dir != old_dir) {
2581f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
25821da177e4SLinus Torvalds 		if (error)
25831da177e4SLinus Torvalds 			return error;
25841da177e4SLinus Torvalds 	}
25851da177e4SLinus Torvalds 
25861da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
25871da177e4SLinus Torvalds 	if (error)
25881da177e4SLinus Torvalds 		return error;
25891da177e4SLinus Torvalds 
25901da177e4SLinus Torvalds 	target = new_dentry->d_inode;
25911da177e4SLinus Torvalds 	if (target) {
25921b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
25931da177e4SLinus Torvalds 		dentry_unhash(new_dentry);
25941da177e4SLinus Torvalds 	}
25951da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
25961da177e4SLinus Torvalds 		error = -EBUSY;
25971da177e4SLinus Torvalds 	else
25981da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
25991da177e4SLinus Torvalds 	if (target) {
26001da177e4SLinus Torvalds 		if (!error)
26011da177e4SLinus Torvalds 			target->i_flags |= S_DEAD;
26021b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
26031da177e4SLinus Torvalds 		if (d_unhashed(new_dentry))
26041da177e4SLinus Torvalds 			d_rehash(new_dentry);
26051da177e4SLinus Torvalds 		dput(new_dentry);
26061da177e4SLinus Torvalds 	}
2607e31e14ecSStephen Smalley 	if (!error)
2608349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
26091da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
26101da177e4SLinus Torvalds 	return error;
26111da177e4SLinus Torvalds }
26121da177e4SLinus Torvalds 
261375c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
26141da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
26151da177e4SLinus Torvalds {
26161da177e4SLinus Torvalds 	struct inode *target;
26171da177e4SLinus Torvalds 	int error;
26181da177e4SLinus Torvalds 
26191da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
26201da177e4SLinus Torvalds 	if (error)
26211da177e4SLinus Torvalds 		return error;
26221da177e4SLinus Torvalds 
26231da177e4SLinus Torvalds 	dget(new_dentry);
26241da177e4SLinus Torvalds 	target = new_dentry->d_inode;
26251da177e4SLinus Torvalds 	if (target)
26261b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
26271da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
26281da177e4SLinus Torvalds 		error = -EBUSY;
26291da177e4SLinus Torvalds 	else
26301da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
26311da177e4SLinus Torvalds 	if (!error) {
2632349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
26331da177e4SLinus Torvalds 			d_move(old_dentry, new_dentry);
26341da177e4SLinus Torvalds 	}
26351da177e4SLinus Torvalds 	if (target)
26361b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
26371da177e4SLinus Torvalds 	dput(new_dentry);
26381da177e4SLinus Torvalds 	return error;
26391da177e4SLinus Torvalds }
26401da177e4SLinus Torvalds 
26411da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
26421da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
26431da177e4SLinus Torvalds {
26441da177e4SLinus Torvalds 	int error;
26451da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
26460eeca283SRobert Love 	const char *old_name;
26471da177e4SLinus Torvalds 
26481da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
26491da177e4SLinus Torvalds  		return 0;
26501da177e4SLinus Torvalds 
26511da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
26521da177e4SLinus Torvalds 	if (error)
26531da177e4SLinus Torvalds 		return error;
26541da177e4SLinus Torvalds 
26551da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
2656a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
26571da177e4SLinus Torvalds 	else
26581da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
26591da177e4SLinus Torvalds 	if (error)
26601da177e4SLinus Torvalds 		return error;
26611da177e4SLinus Torvalds 
2662acfa4380SAl Viro 	if (!old_dir->i_op->rename)
26631da177e4SLinus Torvalds 		return -EPERM;
26641da177e4SLinus Torvalds 
26659e3509e2SJan Kara 	vfs_dq_init(old_dir);
26669e3509e2SJan Kara 	vfs_dq_init(new_dir);
26671da177e4SLinus Torvalds 
26680eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
26690eeca283SRobert Love 
26701da177e4SLinus Torvalds 	if (is_dir)
26711da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
26721da177e4SLinus Torvalds 	else
26731da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
26741da177e4SLinus Torvalds 	if (!error) {
26750eeca283SRobert Love 		const char *new_name = old_dentry->d_name.name;
267689204c40SJohn McCutchan 		fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
26775a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
26781da177e4SLinus Torvalds 	}
26790eeca283SRobert Love 	fsnotify_oldname_free(old_name);
26800eeca283SRobert Love 
26811da177e4SLinus Torvalds 	return error;
26821da177e4SLinus Torvalds }
26831da177e4SLinus Torvalds 
26842e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
26852e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
26861da177e4SLinus Torvalds {
26871da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
26881da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
26891da177e4SLinus Torvalds 	struct dentry *trap;
26901da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
26912ad94ae6SAl Viro 	char *from;
26922ad94ae6SAl Viro 	char *to;
26932ad94ae6SAl Viro 	int error;
26941da177e4SLinus Torvalds 
26952ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
26961da177e4SLinus Torvalds 	if (error)
26971da177e4SLinus Torvalds 		goto exit;
26981da177e4SLinus Torvalds 
26992ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
27001da177e4SLinus Torvalds 	if (error)
27011da177e4SLinus Torvalds 		goto exit1;
27021da177e4SLinus Torvalds 
27031da177e4SLinus Torvalds 	error = -EXDEV;
27044ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
27051da177e4SLinus Torvalds 		goto exit2;
27061da177e4SLinus Torvalds 
27074ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
27081da177e4SLinus Torvalds 	error = -EBUSY;
27091da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
27101da177e4SLinus Torvalds 		goto exit2;
27111da177e4SLinus Torvalds 
27124ac91378SJan Blunck 	new_dir = newnd.path.dentry;
27131da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
27141da177e4SLinus Torvalds 		goto exit2;
27151da177e4SLinus Torvalds 
27160612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
27170612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
27184e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
27190612d9fbSOGAWA Hirofumi 
27201da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
27211da177e4SLinus Torvalds 
272249705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
27231da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
27241da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
27251da177e4SLinus Torvalds 		goto exit3;
27261da177e4SLinus Torvalds 	/* source must exist */
27271da177e4SLinus Torvalds 	error = -ENOENT;
27281da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
27291da177e4SLinus Torvalds 		goto exit4;
27301da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
27311da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
27321da177e4SLinus Torvalds 		error = -ENOTDIR;
27331da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
27341da177e4SLinus Torvalds 			goto exit4;
27351da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
27361da177e4SLinus Torvalds 			goto exit4;
27371da177e4SLinus Torvalds 	}
27381da177e4SLinus Torvalds 	/* source should not be ancestor of target */
27391da177e4SLinus Torvalds 	error = -EINVAL;
27401da177e4SLinus Torvalds 	if (old_dentry == trap)
27411da177e4SLinus Torvalds 		goto exit4;
274249705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
27431da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
27441da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
27451da177e4SLinus Torvalds 		goto exit4;
27461da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
27471da177e4SLinus Torvalds 	error = -ENOTEMPTY;
27481da177e4SLinus Torvalds 	if (new_dentry == trap)
27491da177e4SLinus Torvalds 		goto exit5;
27501da177e4SLinus Torvalds 
27519079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
27529079b1ebSDave Hansen 	if (error)
27539079b1ebSDave Hansen 		goto exit5;
2754be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
2755be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
2756be6d3e56SKentaro Takeda 	if (error)
2757be6d3e56SKentaro Takeda 		goto exit6;
27581da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
27591da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
2760be6d3e56SKentaro Takeda exit6:
27619079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
27621da177e4SLinus Torvalds exit5:
27631da177e4SLinus Torvalds 	dput(new_dentry);
27641da177e4SLinus Torvalds exit4:
27651da177e4SLinus Torvalds 	dput(old_dentry);
27661da177e4SLinus Torvalds exit3:
27671da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
27681da177e4SLinus Torvalds exit2:
27691d957f9bSJan Blunck 	path_put(&newnd.path);
27702ad94ae6SAl Viro 	putname(to);
27711da177e4SLinus Torvalds exit1:
27721d957f9bSJan Blunck 	path_put(&oldnd.path);
27731da177e4SLinus Torvalds 	putname(from);
27742ad94ae6SAl Viro exit:
27751da177e4SLinus Torvalds 	return error;
27761da177e4SLinus Torvalds }
27771da177e4SLinus Torvalds 
2778a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
27795590ff0dSUlrich Drepper {
27805590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
27815590ff0dSUlrich Drepper }
27825590ff0dSUlrich Drepper 
27831da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
27841da177e4SLinus Torvalds {
27851da177e4SLinus Torvalds 	int len;
27861da177e4SLinus Torvalds 
27871da177e4SLinus Torvalds 	len = PTR_ERR(link);
27881da177e4SLinus Torvalds 	if (IS_ERR(link))
27891da177e4SLinus Torvalds 		goto out;
27901da177e4SLinus Torvalds 
27911da177e4SLinus Torvalds 	len = strlen(link);
27921da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
27931da177e4SLinus Torvalds 		len = buflen;
27941da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
27951da177e4SLinus Torvalds 		len = -EFAULT;
27961da177e4SLinus Torvalds out:
27971da177e4SLinus Torvalds 	return len;
27981da177e4SLinus Torvalds }
27991da177e4SLinus Torvalds 
28001da177e4SLinus Torvalds /*
28011da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
28021da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
28031da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
28041da177e4SLinus Torvalds  */
28051da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
28061da177e4SLinus Torvalds {
28071da177e4SLinus Torvalds 	struct nameidata nd;
2808cc314eefSLinus Torvalds 	void *cookie;
2809694a1764SMarcin Slusarz 	int res;
2810cc314eefSLinus Torvalds 
28111da177e4SLinus Torvalds 	nd.depth = 0;
2812cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
2813694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
2814694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
2815694a1764SMarcin Slusarz 
2816694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
28171da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
2818cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2819694a1764SMarcin Slusarz 	return res;
28201da177e4SLinus Torvalds }
28211da177e4SLinus Torvalds 
28221da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
28231da177e4SLinus Torvalds {
28241da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
28251da177e4SLinus Torvalds }
28261da177e4SLinus Torvalds 
28271da177e4SLinus Torvalds /* get the link contents into pagecache */
28281da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
28291da177e4SLinus Torvalds {
2830ebd09abbSDuane Griffin 	char *kaddr;
28311da177e4SLinus Torvalds 	struct page *page;
28321da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
2833090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
28341da177e4SLinus Torvalds 	if (IS_ERR(page))
28356fe6900eSNick Piggin 		return (char*)page;
28361da177e4SLinus Torvalds 	*ppage = page;
2837ebd09abbSDuane Griffin 	kaddr = kmap(page);
2838ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
2839ebd09abbSDuane Griffin 	return kaddr;
28401da177e4SLinus Torvalds }
28411da177e4SLinus Torvalds 
28421da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
28431da177e4SLinus Torvalds {
28441da177e4SLinus Torvalds 	struct page *page = NULL;
28451da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
28461da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
28471da177e4SLinus Torvalds 	if (page) {
28481da177e4SLinus Torvalds 		kunmap(page);
28491da177e4SLinus Torvalds 		page_cache_release(page);
28501da177e4SLinus Torvalds 	}
28511da177e4SLinus Torvalds 	return res;
28521da177e4SLinus Torvalds }
28531da177e4SLinus Torvalds 
2854cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
28551da177e4SLinus Torvalds {
2856cc314eefSLinus Torvalds 	struct page *page = NULL;
28571da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
2858cc314eefSLinus Torvalds 	return page;
28591da177e4SLinus Torvalds }
28601da177e4SLinus Torvalds 
2861cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
28621da177e4SLinus Torvalds {
2863cc314eefSLinus Torvalds 	struct page *page = cookie;
2864cc314eefSLinus Torvalds 
2865cc314eefSLinus Torvalds 	if (page) {
28661da177e4SLinus Torvalds 		kunmap(page);
28671da177e4SLinus Torvalds 		page_cache_release(page);
28681da177e4SLinus Torvalds 	}
28691da177e4SLinus Torvalds }
28701da177e4SLinus Torvalds 
287154566b2cSNick Piggin /*
287254566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
287354566b2cSNick Piggin  */
287454566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
28751da177e4SLinus Torvalds {
28761da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
28770adb25d2SKirill Korotaev 	struct page *page;
2878afddba49SNick Piggin 	void *fsdata;
2879beb497abSDmitriy Monakhov 	int err;
28801da177e4SLinus Torvalds 	char *kaddr;
288154566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
288254566b2cSNick Piggin 	if (nofs)
288354566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
28841da177e4SLinus Torvalds 
28857e53cac4SNeilBrown retry:
2886afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
288754566b2cSNick Piggin 				flags, &page, &fsdata);
28881da177e4SLinus Torvalds 	if (err)
2889afddba49SNick Piggin 		goto fail;
2890afddba49SNick Piggin 
28911da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
28921da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
28931da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
2894afddba49SNick Piggin 
2895afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
2896afddba49SNick Piggin 							page, fsdata);
28971da177e4SLinus Torvalds 	if (err < 0)
28981da177e4SLinus Torvalds 		goto fail;
2899afddba49SNick Piggin 	if (err < len-1)
2900afddba49SNick Piggin 		goto retry;
2901afddba49SNick Piggin 
29021da177e4SLinus Torvalds 	mark_inode_dirty(inode);
29031da177e4SLinus Torvalds 	return 0;
29041da177e4SLinus Torvalds fail:
29051da177e4SLinus Torvalds 	return err;
29061da177e4SLinus Torvalds }
29071da177e4SLinus Torvalds 
29080adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
29090adb25d2SKirill Korotaev {
29100adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
291154566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
29120adb25d2SKirill Korotaev }
29130adb25d2SKirill Korotaev 
291492e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
29151da177e4SLinus Torvalds 	.readlink	= generic_readlink,
29161da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
29171da177e4SLinus Torvalds 	.put_link	= page_put_link,
29181da177e4SLinus Torvalds };
29191da177e4SLinus Torvalds 
29202d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
29211da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
29221da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
29231da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
29241da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
29251da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
29261da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
29271da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
29281da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
29291da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
29300adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
29311da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
29321da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
29331da177e4SLinus Torvalds EXPORT_SYMBOL(path_lookup);
2934d1811465SAl Viro EXPORT_SYMBOL(kern_path);
293516f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
2936f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
29378c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission);
29381da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
29391da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
29401da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
29411da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
29421da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
29431da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
29441da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
29451da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
29461da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
29471da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
29481da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
29491da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
29501da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
29511da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
2952