xref: /openbmc/linux/fs/namei.c (revision fb1cc555)
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) {
6924ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
6931da177e4SLinus Torvalds 
6942a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
6952a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
6961da177e4SLinus Torvalds 			break;
6971da177e4SLinus Torvalds 		}
6984ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
6993088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
7003088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
7011da177e4SLinus Torvalds 			dput(old);
7021da177e4SLinus Torvalds 			break;
7031da177e4SLinus Torvalds 		}
7043088dd70SAl Viro 		if (!follow_up(&nd->path))
7051da177e4SLinus Torvalds 			break;
7061da177e4SLinus Torvalds 	}
70779ed0226SAl Viro 	follow_mount(&nd->path);
7081da177e4SLinus Torvalds }
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds /*
7111da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
7121da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
7131da177e4SLinus Torvalds  *  It _is_ time-critical.
7141da177e4SLinus Torvalds  */
7151da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
7161da177e4SLinus Torvalds 		     struct path *path)
7171da177e4SLinus Torvalds {
7184ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
7196e6b1bd1SAl Viro 	struct dentry *dentry, *parent;
7206e6b1bd1SAl Viro 	struct inode *dir;
7213cac260aSAl Viro 	/*
7223cac260aSAl Viro 	 * See if the low-level filesystem might want
7233cac260aSAl Viro 	 * to use its own hash..
7243cac260aSAl Viro 	 */
7253cac260aSAl Viro 	if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
7263cac260aSAl Viro 		int err = nd->path.dentry->d_op->d_hash(nd->path.dentry, name);
7273cac260aSAl Viro 		if (err < 0)
7283cac260aSAl Viro 			return err;
7293cac260aSAl Viro 	}
7301da177e4SLinus Torvalds 
7313cac260aSAl Viro 	dentry = __d_lookup(nd->path.dentry, name);
7321da177e4SLinus Torvalds 	if (!dentry)
7331da177e4SLinus Torvalds 		goto need_lookup;
7341da177e4SLinus Torvalds 	if (dentry->d_op && dentry->d_op->d_revalidate)
7351da177e4SLinus Torvalds 		goto need_revalidate;
7361da177e4SLinus Torvalds done:
7371da177e4SLinus Torvalds 	path->mnt = mnt;
7381da177e4SLinus Torvalds 	path->dentry = dentry;
739634ee701SAl Viro 	__follow_mount(path);
7401da177e4SLinus Torvalds 	return 0;
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds need_lookup:
7436e6b1bd1SAl Viro 	parent = nd->path.dentry;
7446e6b1bd1SAl Viro 	dir = parent->d_inode;
7456e6b1bd1SAl Viro 
7466e6b1bd1SAl Viro 	mutex_lock(&dir->i_mutex);
7476e6b1bd1SAl Viro 	/*
7486e6b1bd1SAl Viro 	 * First re-do the cached lookup just in case it was created
7496e6b1bd1SAl Viro 	 * while we waited for the directory semaphore..
7506e6b1bd1SAl Viro 	 *
7516e6b1bd1SAl Viro 	 * FIXME! This could use version numbering or similar to
7526e6b1bd1SAl Viro 	 * avoid unnecessary cache lookups.
7536e6b1bd1SAl Viro 	 *
7546e6b1bd1SAl Viro 	 * The "dcache_lock" is purely to protect the RCU list walker
7556e6b1bd1SAl Viro 	 * from concurrent renames at this point (we mustn't get false
7566e6b1bd1SAl Viro 	 * negatives from the RCU list walk here, unlike the optimistic
7576e6b1bd1SAl Viro 	 * fast walk).
7586e6b1bd1SAl Viro 	 *
7596e6b1bd1SAl Viro 	 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
7606e6b1bd1SAl Viro 	 */
7616e6b1bd1SAl Viro 	dentry = d_lookup(parent, name);
7626e6b1bd1SAl Viro 	if (!dentry) {
7636e6b1bd1SAl Viro 		struct dentry *new;
7646e6b1bd1SAl Viro 
7656e6b1bd1SAl Viro 		/* Don't create child dentry for a dead directory. */
7666e6b1bd1SAl Viro 		dentry = ERR_PTR(-ENOENT);
7676e6b1bd1SAl Viro 		if (IS_DEADDIR(dir))
7686e6b1bd1SAl Viro 			goto out_unlock;
7696e6b1bd1SAl Viro 
7706e6b1bd1SAl Viro 		new = d_alloc(parent, name);
7716e6b1bd1SAl Viro 		dentry = ERR_PTR(-ENOMEM);
7726e6b1bd1SAl Viro 		if (new) {
7736e6b1bd1SAl Viro 			dentry = dir->i_op->lookup(dir, new, nd);
7746e6b1bd1SAl Viro 			if (dentry)
7756e6b1bd1SAl Viro 				dput(new);
7766e6b1bd1SAl Viro 			else
7776e6b1bd1SAl Viro 				dentry = new;
7786e6b1bd1SAl Viro 		}
7796e6b1bd1SAl Viro out_unlock:
7806e6b1bd1SAl Viro 		mutex_unlock(&dir->i_mutex);
7816e6b1bd1SAl Viro 		if (IS_ERR(dentry))
7826e6b1bd1SAl Viro 			goto fail;
7836e6b1bd1SAl Viro 		goto done;
7846e6b1bd1SAl Viro 	}
7856e6b1bd1SAl Viro 
7866e6b1bd1SAl Viro 	/*
7876e6b1bd1SAl Viro 	 * Uhhuh! Nasty case: the cache was re-populated while
7886e6b1bd1SAl Viro 	 * we waited on the semaphore. Need to revalidate.
7896e6b1bd1SAl Viro 	 */
7906e6b1bd1SAl Viro 	mutex_unlock(&dir->i_mutex);
7916e6b1bd1SAl Viro 	if (dentry->d_op && dentry->d_op->d_revalidate) {
7926e6b1bd1SAl Viro 		dentry = do_revalidate(dentry, nd);
7936e6b1bd1SAl Viro 		if (!dentry)
7946e6b1bd1SAl Viro 			dentry = ERR_PTR(-ENOENT);
7956e6b1bd1SAl Viro 	}
7961da177e4SLinus Torvalds 	if (IS_ERR(dentry))
7971da177e4SLinus Torvalds 		goto fail;
7981da177e4SLinus Torvalds 	goto done;
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds need_revalidate:
801bcdc5e01SIan Kent 	dentry = do_revalidate(dentry, nd);
802bcdc5e01SIan Kent 	if (!dentry)
8031da177e4SLinus Torvalds 		goto need_lookup;
804bcdc5e01SIan Kent 	if (IS_ERR(dentry))
805bcdc5e01SIan Kent 		goto fail;
806bcdc5e01SIan Kent 	goto done;
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds fail:
8091da177e4SLinus Torvalds 	return PTR_ERR(dentry);
8101da177e4SLinus Torvalds }
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds /*
813ac278a9cSAl Viro  * This is a temporary kludge to deal with "automount" symlinks; proper
814ac278a9cSAl Viro  * solution is to trigger them on follow_mount(), so that do_lookup()
815ac278a9cSAl Viro  * would DTRT.  To be killed before 2.6.34-final.
816ac278a9cSAl Viro  */
817ac278a9cSAl Viro static inline int follow_on_final(struct inode *inode, unsigned lookup_flags)
818ac278a9cSAl Viro {
819ac278a9cSAl Viro 	return inode && unlikely(inode->i_op->follow_link) &&
820ac278a9cSAl Viro 		((lookup_flags & LOOKUP_FOLLOW) || S_ISDIR(inode->i_mode));
821ac278a9cSAl Viro }
822ac278a9cSAl Viro 
823ac278a9cSAl Viro /*
8241da177e4SLinus Torvalds  * Name resolution.
825ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
826ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
8271da177e4SLinus Torvalds  *
828ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
829ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
8301da177e4SLinus Torvalds  */
8316de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
8321da177e4SLinus Torvalds {
8331da177e4SLinus Torvalds 	struct path next;
8341da177e4SLinus Torvalds 	struct inode *inode;
8351da177e4SLinus Torvalds 	int err;
8361da177e4SLinus Torvalds 	unsigned int lookup_flags = nd->flags;
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	while (*name=='/')
8391da177e4SLinus Torvalds 		name++;
8401da177e4SLinus Torvalds 	if (!*name)
8411da177e4SLinus Torvalds 		goto return_reval;
8421da177e4SLinus Torvalds 
8434ac91378SJan Blunck 	inode = nd->path.dentry->d_inode;
8441da177e4SLinus Torvalds 	if (nd->depth)
845f55eab82STrond Myklebust 		lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
8481da177e4SLinus Torvalds 	for(;;) {
8491da177e4SLinus Torvalds 		unsigned long hash;
8501da177e4SLinus Torvalds 		struct qstr this;
8511da177e4SLinus Torvalds 		unsigned int c;
8521da177e4SLinus Torvalds 
853cdce5d6bSTrond Myklebust 		nd->flags |= LOOKUP_CONTINUE;
854b75b5086SAl Viro 		err = exec_permission(inode);
8551da177e4SLinus Torvalds  		if (err)
8561da177e4SLinus Torvalds 			break;
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 		this.name = name;
8591da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 		hash = init_name_hash();
8621da177e4SLinus Torvalds 		do {
8631da177e4SLinus Torvalds 			name++;
8641da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
8651da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
8661da177e4SLinus Torvalds 		} while (c && (c != '/'));
8671da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
8681da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 		/* remove trailing slashes? */
8711da177e4SLinus Torvalds 		if (!c)
8721da177e4SLinus Torvalds 			goto last_component;
8731da177e4SLinus Torvalds 		while (*++name == '/');
8741da177e4SLinus Torvalds 		if (!*name)
8751da177e4SLinus Torvalds 			goto last_with_slashes;
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 		/*
8781da177e4SLinus Torvalds 		 * "." and ".." are special - ".." especially so because it has
8791da177e4SLinus Torvalds 		 * to be able to know about the current root directory and
8801da177e4SLinus Torvalds 		 * parent relationships.
8811da177e4SLinus Torvalds 		 */
8821da177e4SLinus Torvalds 		if (this.name[0] == '.') switch (this.len) {
8831da177e4SLinus Torvalds 			default:
8841da177e4SLinus Torvalds 				break;
8851da177e4SLinus Torvalds 			case 2:
8861da177e4SLinus Torvalds 				if (this.name[1] != '.')
8871da177e4SLinus Torvalds 					break;
88858c465ebSAl Viro 				follow_dotdot(nd);
8894ac91378SJan Blunck 				inode = nd->path.dentry->d_inode;
8901da177e4SLinus Torvalds 				/* fallthrough */
8911da177e4SLinus Torvalds 			case 1:
8921da177e4SLinus Torvalds 				continue;
8931da177e4SLinus Torvalds 		}
8941da177e4SLinus Torvalds 		/* This does the actual lookups.. */
8951da177e4SLinus Torvalds 		err = do_lookup(nd, &this, &next);
8961da177e4SLinus Torvalds 		if (err)
8971da177e4SLinus Torvalds 			break;
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 		err = -ENOENT;
9001da177e4SLinus Torvalds 		inode = next.dentry->d_inode;
9011da177e4SLinus Torvalds 		if (!inode)
9021da177e4SLinus Torvalds 			goto out_dput;
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds 		if (inode->i_op->follow_link) {
90590ebe565SAl Viro 			err = do_follow_link(&next, nd);
9061da177e4SLinus Torvalds 			if (err)
9071da177e4SLinus Torvalds 				goto return_err;
9081da177e4SLinus Torvalds 			err = -ENOENT;
9094ac91378SJan Blunck 			inode = nd->path.dentry->d_inode;
9101da177e4SLinus Torvalds 			if (!inode)
9111da177e4SLinus Torvalds 				break;
91209dd17d3SMiklos Szeredi 		} else
91309dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
9141da177e4SLinus Torvalds 		err = -ENOTDIR;
9151da177e4SLinus Torvalds 		if (!inode->i_op->lookup)
9161da177e4SLinus Torvalds 			break;
9171da177e4SLinus Torvalds 		continue;
9181da177e4SLinus Torvalds 		/* here ends the main loop */
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds last_with_slashes:
9211da177e4SLinus Torvalds 		lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
9221da177e4SLinus Torvalds last_component:
923f55eab82STrond Myklebust 		/* Clear LOOKUP_CONTINUE iff it was previously unset */
924f55eab82STrond Myklebust 		nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
9251da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_PARENT)
9261da177e4SLinus Torvalds 			goto lookup_parent;
9271da177e4SLinus Torvalds 		if (this.name[0] == '.') switch (this.len) {
9281da177e4SLinus Torvalds 			default:
9291da177e4SLinus Torvalds 				break;
9301da177e4SLinus Torvalds 			case 2:
9311da177e4SLinus Torvalds 				if (this.name[1] != '.')
9321da177e4SLinus Torvalds 					break;
93358c465ebSAl Viro 				follow_dotdot(nd);
9344ac91378SJan Blunck 				inode = nd->path.dentry->d_inode;
9351da177e4SLinus Torvalds 				/* fallthrough */
9361da177e4SLinus Torvalds 			case 1:
9371da177e4SLinus Torvalds 				goto return_reval;
9381da177e4SLinus Torvalds 		}
9391da177e4SLinus Torvalds 		err = do_lookup(nd, &this, &next);
9401da177e4SLinus Torvalds 		if (err)
9411da177e4SLinus Torvalds 			break;
9421da177e4SLinus Torvalds 		inode = next.dentry->d_inode;
943ac278a9cSAl Viro 		if (follow_on_final(inode, lookup_flags)) {
94490ebe565SAl Viro 			err = do_follow_link(&next, nd);
9451da177e4SLinus Torvalds 			if (err)
9461da177e4SLinus Torvalds 				goto return_err;
9474ac91378SJan Blunck 			inode = nd->path.dentry->d_inode;
94809dd17d3SMiklos Szeredi 		} else
94909dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
9501da177e4SLinus Torvalds 		err = -ENOENT;
9511da177e4SLinus Torvalds 		if (!inode)
9521da177e4SLinus Torvalds 			break;
9531da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_DIRECTORY) {
9541da177e4SLinus Torvalds 			err = -ENOTDIR;
955acfa4380SAl Viro 			if (!inode->i_op->lookup)
9561da177e4SLinus Torvalds 				break;
9571da177e4SLinus Torvalds 		}
9581da177e4SLinus Torvalds 		goto return_base;
9591da177e4SLinus Torvalds lookup_parent:
9601da177e4SLinus Torvalds 		nd->last = this;
9611da177e4SLinus Torvalds 		nd->last_type = LAST_NORM;
9621da177e4SLinus Torvalds 		if (this.name[0] != '.')
9631da177e4SLinus Torvalds 			goto return_base;
9641da177e4SLinus Torvalds 		if (this.len == 1)
9651da177e4SLinus Torvalds 			nd->last_type = LAST_DOT;
9661da177e4SLinus Torvalds 		else if (this.len == 2 && this.name[1] == '.')
9671da177e4SLinus Torvalds 			nd->last_type = LAST_DOTDOT;
9681da177e4SLinus Torvalds 		else
9691da177e4SLinus Torvalds 			goto return_base;
9701da177e4SLinus Torvalds return_reval:
9711da177e4SLinus Torvalds 		/*
9721da177e4SLinus Torvalds 		 * We bypassed the ordinary revalidation routines.
9731da177e4SLinus Torvalds 		 * We may need to check the cached dentry for staleness.
9741da177e4SLinus Torvalds 		 */
9754ac91378SJan Blunck 		if (nd->path.dentry && nd->path.dentry->d_sb &&
9764ac91378SJan Blunck 		    (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
9771da177e4SLinus Torvalds 			err = -ESTALE;
9781da177e4SLinus Torvalds 			/* Note: we do not d_invalidate() */
9794ac91378SJan Blunck 			if (!nd->path.dentry->d_op->d_revalidate(
9804ac91378SJan Blunck 					nd->path.dentry, nd))
9811da177e4SLinus Torvalds 				break;
9821da177e4SLinus Torvalds 		}
9831da177e4SLinus Torvalds return_base:
9841da177e4SLinus Torvalds 		return 0;
9851da177e4SLinus Torvalds out_dput:
9861d957f9bSJan Blunck 		path_put_conditional(&next, nd);
9871da177e4SLinus Torvalds 		break;
9881da177e4SLinus Torvalds 	}
9891d957f9bSJan Blunck 	path_put(&nd->path);
9901da177e4SLinus Torvalds return_err:
9911da177e4SLinus Torvalds 	return err;
9921da177e4SLinus Torvalds }
9931da177e4SLinus Torvalds 
994fc9b52cdSHarvey Harrison static int path_walk(const char *name, struct nameidata *nd)
9951da177e4SLinus Torvalds {
9966de88d72SAl Viro 	struct path save = nd->path;
9976de88d72SAl Viro 	int result;
9986de88d72SAl Viro 
9991da177e4SLinus Torvalds 	current->total_link_count = 0;
10006de88d72SAl Viro 
10016de88d72SAl Viro 	/* make sure the stuff we saved doesn't go away */
10026de88d72SAl Viro 	path_get(&save);
10036de88d72SAl Viro 
10046de88d72SAl Viro 	result = link_path_walk(name, nd);
10056de88d72SAl Viro 	if (result == -ESTALE) {
10066de88d72SAl Viro 		/* nd->path had been dropped */
10076de88d72SAl Viro 		current->total_link_count = 0;
10086de88d72SAl Viro 		nd->path = save;
10096de88d72SAl Viro 		path_get(&nd->path);
10106de88d72SAl Viro 		nd->flags |= LOOKUP_REVAL;
10116de88d72SAl Viro 		result = link_path_walk(name, nd);
10126de88d72SAl Viro 	}
10136de88d72SAl Viro 
10146de88d72SAl Viro 	path_put(&save);
10156de88d72SAl Viro 
10166de88d72SAl Viro 	return result;
10171da177e4SLinus Torvalds }
10181da177e4SLinus Torvalds 
10199b4a9b14SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
10201da177e4SLinus Torvalds {
1021ea3834d9SPrasanna Meda 	int retval = 0;
1022170aa3d0SUlrich Drepper 	int fput_needed;
1023170aa3d0SUlrich Drepper 	struct file *file;
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
10261da177e4SLinus Torvalds 	nd->flags = flags;
10271da177e4SLinus Torvalds 	nd->depth = 0;
10282a737871SAl Viro 	nd->root.mnt = NULL;
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds 	if (*name=='/') {
10312a737871SAl Viro 		set_root(nd);
10322a737871SAl Viro 		nd->path = nd->root;
10332a737871SAl Viro 		path_get(&nd->root);
10345590ff0dSUlrich Drepper 	} else if (dfd == AT_FDCWD) {
10352a737871SAl Viro 		struct fs_struct *fs = current->fs;
1036e518ddb7SAndreas Mohr 		read_lock(&fs->lock);
10376ac08c39SJan Blunck 		nd->path = fs->pwd;
10386ac08c39SJan Blunck 		path_get(&fs->pwd);
1039e518ddb7SAndreas Mohr 		read_unlock(&fs->lock);
10405590ff0dSUlrich Drepper 	} else {
10415590ff0dSUlrich Drepper 		struct dentry *dentry;
10425590ff0dSUlrich Drepper 
10435590ff0dSUlrich Drepper 		file = fget_light(dfd, &fput_needed);
10445590ff0dSUlrich Drepper 		retval = -EBADF;
1045170aa3d0SUlrich Drepper 		if (!file)
10466d09bb62STrond Myklebust 			goto out_fail;
10475590ff0dSUlrich Drepper 
10480f7fc9e4SJosef "Jeff" Sipek 		dentry = file->f_path.dentry;
10495590ff0dSUlrich Drepper 
10505590ff0dSUlrich Drepper 		retval = -ENOTDIR;
1051170aa3d0SUlrich Drepper 		if (!S_ISDIR(dentry->d_inode->i_mode))
10526d09bb62STrond Myklebust 			goto fput_fail;
10535590ff0dSUlrich Drepper 
10545590ff0dSUlrich Drepper 		retval = file_permission(file, MAY_EXEC);
1055170aa3d0SUlrich Drepper 		if (retval)
10566d09bb62STrond Myklebust 			goto fput_fail;
10575590ff0dSUlrich Drepper 
10585dd784d0SJan Blunck 		nd->path = file->f_path;
10595dd784d0SJan Blunck 		path_get(&file->f_path);
10605590ff0dSUlrich Drepper 
10615590ff0dSUlrich Drepper 		fput_light(file, fput_needed);
10621da177e4SLinus Torvalds 	}
10639b4a9b14SAl Viro 	return 0;
10642dfdd266SJosef 'Jeff' Sipek 
10659b4a9b14SAl Viro fput_fail:
10669b4a9b14SAl Viro 	fput_light(file, fput_needed);
10679b4a9b14SAl Viro out_fail:
10689b4a9b14SAl Viro 	return retval;
10699b4a9b14SAl Viro }
10709b4a9b14SAl Viro 
10719b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
10729b4a9b14SAl Viro static int do_path_lookup(int dfd, const char *name,
10739b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
10749b4a9b14SAl Viro {
10759b4a9b14SAl Viro 	int retval = path_init(dfd, name, flags, nd);
10769b4a9b14SAl Viro 	if (!retval)
10772dfdd266SJosef 'Jeff' Sipek 		retval = path_walk(name, nd);
10784ac91378SJan Blunck 	if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
10794ac91378SJan Blunck 				nd->path.dentry->d_inode))
10804ac91378SJan Blunck 		audit_inode(name, nd->path.dentry);
10812a737871SAl Viro 	if (nd->root.mnt) {
10822a737871SAl Viro 		path_put(&nd->root);
10832a737871SAl Viro 		nd->root.mnt = NULL;
10842a737871SAl Viro 	}
1085170aa3d0SUlrich Drepper 	return retval;
10861da177e4SLinus Torvalds }
10871da177e4SLinus Torvalds 
1088fc9b52cdSHarvey Harrison int path_lookup(const char *name, unsigned int flags,
10895590ff0dSUlrich Drepper 			struct nameidata *nd)
10905590ff0dSUlrich Drepper {
10915590ff0dSUlrich Drepper 	return do_path_lookup(AT_FDCWD, name, flags, nd);
10925590ff0dSUlrich Drepper }
10935590ff0dSUlrich Drepper 
1094d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1095d1811465SAl Viro {
1096d1811465SAl Viro 	struct nameidata nd;
1097d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1098d1811465SAl Viro 	if (!res)
1099d1811465SAl Viro 		*path = nd.path;
1100d1811465SAl Viro 	return res;
1101d1811465SAl Viro }
1102d1811465SAl Viro 
110316f18200SJosef 'Jeff' Sipek /**
110416f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
110516f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
110616f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
110716f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
110816f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
110916f18200SJosef 'Jeff' Sipek  * @nd: pointer to nameidata
111016f18200SJosef 'Jeff' Sipek  */
111116f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
111216f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
111316f18200SJosef 'Jeff' Sipek 		    struct nameidata *nd)
111416f18200SJosef 'Jeff' Sipek {
111516f18200SJosef 'Jeff' Sipek 	int retval;
111616f18200SJosef 'Jeff' Sipek 
111716f18200SJosef 'Jeff' Sipek 	/* same as do_path_lookup */
111816f18200SJosef 'Jeff' Sipek 	nd->last_type = LAST_ROOT;
111916f18200SJosef 'Jeff' Sipek 	nd->flags = flags;
112016f18200SJosef 'Jeff' Sipek 	nd->depth = 0;
112116f18200SJosef 'Jeff' Sipek 
1122c8e7f449SJan Blunck 	nd->path.dentry = dentry;
1123c8e7f449SJan Blunck 	nd->path.mnt = mnt;
1124c8e7f449SJan Blunck 	path_get(&nd->path);
11255b857119SAl Viro 	nd->root = nd->path;
11265b857119SAl Viro 	path_get(&nd->root);
112716f18200SJosef 'Jeff' Sipek 
112816f18200SJosef 'Jeff' Sipek 	retval = path_walk(name, nd);
11294ac91378SJan Blunck 	if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
11304ac91378SJan Blunck 				nd->path.dentry->d_inode))
11314ac91378SJan Blunck 		audit_inode(name, nd->path.dentry);
113216f18200SJosef 'Jeff' Sipek 
11332a737871SAl Viro 	path_put(&nd->root);
11342a737871SAl Viro 	nd->root.mnt = NULL;
113516f18200SJosef 'Jeff' Sipek 
11362a737871SAl Viro 	return retval;
113716f18200SJosef 'Jeff' Sipek }
113816f18200SJosef 'Jeff' Sipek 
1139eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1140eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
11411da177e4SLinus Torvalds {
11421da177e4SLinus Torvalds 	struct dentry *dentry;
11431da177e4SLinus Torvalds 	struct inode *inode;
11441da177e4SLinus Torvalds 	int err;
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 	inode = base->d_inode;
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds 	/*
11491da177e4SLinus Torvalds 	 * See if the low-level filesystem might want
11501da177e4SLinus Torvalds 	 * to use its own hash..
11511da177e4SLinus Torvalds 	 */
11521da177e4SLinus Torvalds 	if (base->d_op && base->d_op->d_hash) {
11531da177e4SLinus Torvalds 		err = base->d_op->d_hash(base, name);
11541da177e4SLinus Torvalds 		dentry = ERR_PTR(err);
11551da177e4SLinus Torvalds 		if (err < 0)
11561da177e4SLinus Torvalds 			goto out;
11571da177e4SLinus Torvalds 	}
11581da177e4SLinus Torvalds 
11596e6b1bd1SAl Viro 	dentry = __d_lookup(base, name);
11606e6b1bd1SAl Viro 
11616e6b1bd1SAl Viro 	/* lockess __d_lookup may fail due to concurrent d_move()
11626e6b1bd1SAl Viro 	 * in some unrelated directory, so try with d_lookup
11636e6b1bd1SAl Viro 	 */
11646e6b1bd1SAl Viro 	if (!dentry)
11656e6b1bd1SAl Viro 		dentry = d_lookup(base, name);
11666e6b1bd1SAl Viro 
11676e6b1bd1SAl Viro 	if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
11686e6b1bd1SAl Viro 		dentry = do_revalidate(dentry, nd);
11696e6b1bd1SAl Viro 
11701da177e4SLinus Torvalds 	if (!dentry) {
1171d70b67c8SMiklos Szeredi 		struct dentry *new;
1172d70b67c8SMiklos Szeredi 
1173d70b67c8SMiklos Szeredi 		/* Don't create child dentry for a dead directory. */
1174d70b67c8SMiklos Szeredi 		dentry = ERR_PTR(-ENOENT);
1175d70b67c8SMiklos Szeredi 		if (IS_DEADDIR(inode))
1176d70b67c8SMiklos Szeredi 			goto out;
1177d70b67c8SMiklos Szeredi 
1178d70b67c8SMiklos Szeredi 		new = d_alloc(base, name);
11791da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOMEM);
11801da177e4SLinus Torvalds 		if (!new)
11811da177e4SLinus Torvalds 			goto out;
11821da177e4SLinus Torvalds 		dentry = inode->i_op->lookup(inode, new, nd);
11831da177e4SLinus Torvalds 		if (!dentry)
11841da177e4SLinus Torvalds 			dentry = new;
11851da177e4SLinus Torvalds 		else
11861da177e4SLinus Torvalds 			dput(new);
11871da177e4SLinus Torvalds 	}
11881da177e4SLinus Torvalds out:
11891da177e4SLinus Torvalds 	return dentry;
11901da177e4SLinus Torvalds }
11911da177e4SLinus Torvalds 
1192057f6c01SJames Morris /*
1193057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1194057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1195057f6c01SJames Morris  * SMP-safe.
1196057f6c01SJames Morris  */
1197a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
11981da177e4SLinus Torvalds {
1199eead1911SChristoph Hellwig 	int err;
1200eead1911SChristoph Hellwig 
1201b75b5086SAl Viro 	err = exec_permission(nd->path.dentry->d_inode);
1202eead1911SChristoph Hellwig 	if (err)
1203eead1911SChristoph Hellwig 		return ERR_PTR(err);
12044ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
12051da177e4SLinus Torvalds }
12061da177e4SLinus Torvalds 
1207eead1911SChristoph Hellwig static int __lookup_one_len(const char *name, struct qstr *this,
1208eead1911SChristoph Hellwig 		struct dentry *base, int len)
12091da177e4SLinus Torvalds {
12101da177e4SLinus Torvalds 	unsigned long hash;
12111da177e4SLinus Torvalds 	unsigned int c;
12121da177e4SLinus Torvalds 
1213057f6c01SJames Morris 	this->name = name;
1214057f6c01SJames Morris 	this->len = len;
12151da177e4SLinus Torvalds 	if (!len)
1216057f6c01SJames Morris 		return -EACCES;
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 	hash = init_name_hash();
12191da177e4SLinus Torvalds 	while (len--) {
12201da177e4SLinus Torvalds 		c = *(const unsigned char *)name++;
12211da177e4SLinus Torvalds 		if (c == '/' || c == '\0')
1222057f6c01SJames Morris 			return -EACCES;
12231da177e4SLinus Torvalds 		hash = partial_name_hash(c, hash);
12241da177e4SLinus Torvalds 	}
1225057f6c01SJames Morris 	this->hash = end_name_hash(hash);
1226057f6c01SJames Morris 	return 0;
1227057f6c01SJames Morris }
12281da177e4SLinus Torvalds 
1229eead1911SChristoph Hellwig /**
1230a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1231eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1232eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1233eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1234eead1911SChristoph Hellwig  *
1235a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1236a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1237eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1238eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1239eead1911SChristoph Hellwig  */
1240057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1241057f6c01SJames Morris {
1242057f6c01SJames Morris 	int err;
1243057f6c01SJames Morris 	struct qstr this;
1244057f6c01SJames Morris 
12452f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
12462f9092e1SDavid Woodhouse 
1247057f6c01SJames Morris 	err = __lookup_one_len(name, &this, base, len);
1248057f6c01SJames Morris 	if (err)
1249057f6c01SJames Morris 		return ERR_PTR(err);
1250eead1911SChristoph Hellwig 
1251b75b5086SAl Viro 	err = exec_permission(base->d_inode);
1252eead1911SChristoph Hellwig 	if (err)
1253eead1911SChristoph Hellwig 		return ERR_PTR(err);
125449705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1255057f6c01SJames Morris }
1256057f6c01SJames Morris 
12572d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags,
12582d8f3038SAl Viro 		 struct path *path)
12591da177e4SLinus Torvalds {
12602d8f3038SAl Viro 	struct nameidata nd;
12611da177e4SLinus Torvalds 	char *tmp = getname(name);
12621da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
12631da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
12642d8f3038SAl Viro 
12652d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
12662d8f3038SAl Viro 
12672d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
12681da177e4SLinus Torvalds 		putname(tmp);
12692d8f3038SAl Viro 		if (!err)
12702d8f3038SAl Viro 			*path = nd.path;
12711da177e4SLinus Torvalds 	}
12721da177e4SLinus Torvalds 	return err;
12731da177e4SLinus Torvalds }
12741da177e4SLinus Torvalds 
12752ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
12762ad94ae6SAl Viro 			struct nameidata *nd, char **name)
12772ad94ae6SAl Viro {
12782ad94ae6SAl Viro 	char *s = getname(path);
12792ad94ae6SAl Viro 	int error;
12802ad94ae6SAl Viro 
12812ad94ae6SAl Viro 	if (IS_ERR(s))
12822ad94ae6SAl Viro 		return PTR_ERR(s);
12832ad94ae6SAl Viro 
12842ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
12852ad94ae6SAl Viro 	if (error)
12862ad94ae6SAl Viro 		putname(s);
12872ad94ae6SAl Viro 	else
12882ad94ae6SAl Viro 		*name = s;
12892ad94ae6SAl Viro 
12902ad94ae6SAl Viro 	return error;
12912ad94ae6SAl Viro }
12922ad94ae6SAl Viro 
12931da177e4SLinus Torvalds /*
12941da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
12951da177e4SLinus Torvalds  * minimal.
12961da177e4SLinus Torvalds  */
12971da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
12981da177e4SLinus Torvalds {
1299da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1300da9592edSDavid Howells 
13011da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
13021da177e4SLinus Torvalds 		return 0;
1303da9592edSDavid Howells 	if (inode->i_uid == fsuid)
13041da177e4SLinus Torvalds 		return 0;
1305da9592edSDavid Howells 	if (dir->i_uid == fsuid)
13061da177e4SLinus Torvalds 		return 0;
13071da177e4SLinus Torvalds 	return !capable(CAP_FOWNER);
13081da177e4SLinus Torvalds }
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds /*
13111da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
13121da177e4SLinus Torvalds  *  whether the type of victim is right.
13131da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
13141da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
13151da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
13161da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
13171da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
13181da177e4SLinus Torvalds  *	a. be owner of dir, or
13191da177e4SLinus Torvalds  *	b. be owner of victim, or
13201da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
13211da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
13221da177e4SLinus Torvalds  *     links pointing to it.
13231da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
13241da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
13251da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
13261da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
13271da177e4SLinus Torvalds  *     nfs_async_unlink().
13281da177e4SLinus Torvalds  */
1329858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
13301da177e4SLinus Torvalds {
13311da177e4SLinus Torvalds 	int error;
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds 	if (!victim->d_inode)
13341da177e4SLinus Torvalds 		return -ENOENT;
13351da177e4SLinus Torvalds 
13361da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
1337cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
13381da177e4SLinus Torvalds 
1339f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
13401da177e4SLinus Torvalds 	if (error)
13411da177e4SLinus Torvalds 		return error;
13421da177e4SLinus Torvalds 	if (IS_APPEND(dir))
13431da177e4SLinus Torvalds 		return -EPERM;
13441da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1345f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
13461da177e4SLinus Torvalds 		return -EPERM;
13471da177e4SLinus Torvalds 	if (isdir) {
13481da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
13491da177e4SLinus Torvalds 			return -ENOTDIR;
13501da177e4SLinus Torvalds 		if (IS_ROOT(victim))
13511da177e4SLinus Torvalds 			return -EBUSY;
13521da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
13531da177e4SLinus Torvalds 		return -EISDIR;
13541da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
13551da177e4SLinus Torvalds 		return -ENOENT;
13561da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
13571da177e4SLinus Torvalds 		return -EBUSY;
13581da177e4SLinus Torvalds 	return 0;
13591da177e4SLinus Torvalds }
13601da177e4SLinus Torvalds 
13611da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
13621da177e4SLinus Torvalds  *  dir.
13631da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
13641da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
13651da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
13661da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
13671da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
13681da177e4SLinus Torvalds  */
1369a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
13701da177e4SLinus Torvalds {
13711da177e4SLinus Torvalds 	if (child->d_inode)
13721da177e4SLinus Torvalds 		return -EEXIST;
13731da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
13741da177e4SLinus Torvalds 		return -ENOENT;
1375f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
13761da177e4SLinus Torvalds }
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds /*
13791da177e4SLinus Torvalds  * O_DIRECTORY translates into forcing a directory lookup.
13801da177e4SLinus Torvalds  */
13811da177e4SLinus Torvalds static inline int lookup_flags(unsigned int f)
13821da177e4SLinus Torvalds {
13831da177e4SLinus Torvalds 	unsigned long retval = LOOKUP_FOLLOW;
13841da177e4SLinus Torvalds 
13851da177e4SLinus Torvalds 	if (f & O_NOFOLLOW)
13861da177e4SLinus Torvalds 		retval &= ~LOOKUP_FOLLOW;
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds 	if (f & O_DIRECTORY)
13891da177e4SLinus Torvalds 		retval |= LOOKUP_DIRECTORY;
13901da177e4SLinus Torvalds 
13911da177e4SLinus Torvalds 	return retval;
13921da177e4SLinus Torvalds }
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds /*
13951da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
13961da177e4SLinus Torvalds  */
13971da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
13981da177e4SLinus Torvalds {
13991da177e4SLinus Torvalds 	struct dentry *p;
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 	if (p1 == p2) {
1402f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
14031da177e4SLinus Torvalds 		return NULL;
14041da177e4SLinus Torvalds 	}
14051da177e4SLinus Torvalds 
1406a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
14071da177e4SLinus Torvalds 
1408e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1409e2761a11SOGAWA Hirofumi 	if (p) {
1410f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1411f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
14121da177e4SLinus Torvalds 		return p;
14131da177e4SLinus Torvalds 	}
14141da177e4SLinus Torvalds 
1415e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1416e2761a11SOGAWA Hirofumi 	if (p) {
1417f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1418f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
14191da177e4SLinus Torvalds 		return p;
14201da177e4SLinus Torvalds 	}
14211da177e4SLinus Torvalds 
1422f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1423f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
14241da177e4SLinus Torvalds 	return NULL;
14251da177e4SLinus Torvalds }
14261da177e4SLinus Torvalds 
14271da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
14281da177e4SLinus Torvalds {
14291b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
14301da177e4SLinus Torvalds 	if (p1 != p2) {
14311b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1432a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
14331da177e4SLinus Torvalds 	}
14341da177e4SLinus Torvalds }
14351da177e4SLinus Torvalds 
14361da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
14371da177e4SLinus Torvalds 		struct nameidata *nd)
14381da177e4SLinus Torvalds {
1439a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds 	if (error)
14421da177e4SLinus Torvalds 		return error;
14431da177e4SLinus Torvalds 
1444acfa4380SAl Viro 	if (!dir->i_op->create)
14451da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
14461da177e4SLinus Torvalds 	mode &= S_IALLUGO;
14471da177e4SLinus Torvalds 	mode |= S_IFREG;
14481da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
14491da177e4SLinus Torvalds 	if (error)
14501da177e4SLinus Torvalds 		return error;
14519e3509e2SJan Kara 	vfs_dq_init(dir);
14521da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
1453a74574aaSStephen Smalley 	if (!error)
1454f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
14551da177e4SLinus Torvalds 	return error;
14561da177e4SLinus Torvalds }
14571da177e4SLinus Torvalds 
14583fb64190SChristoph Hellwig int may_open(struct path *path, int acc_mode, int flag)
14591da177e4SLinus Torvalds {
14603fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
14611da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14621da177e4SLinus Torvalds 	int error;
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds 	if (!inode)
14651da177e4SLinus Torvalds 		return -ENOENT;
14661da177e4SLinus Torvalds 
1467c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
1468c8fe8f30SChristoph Hellwig 	case S_IFLNK:
14691da177e4SLinus Torvalds 		return -ELOOP;
1470c8fe8f30SChristoph Hellwig 	case S_IFDIR:
1471c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
14721da177e4SLinus Torvalds 			return -EISDIR;
1473c8fe8f30SChristoph Hellwig 		break;
1474c8fe8f30SChristoph Hellwig 	case S_IFBLK:
1475c8fe8f30SChristoph Hellwig 	case S_IFCHR:
14763fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
14771da177e4SLinus Torvalds 			return -EACCES;
1478c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
1479c8fe8f30SChristoph Hellwig 	case S_IFIFO:
1480c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
14811da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
1482c8fe8f30SChristoph Hellwig 		break;
14834a3fd211SDave Hansen 	}
1484b41572e9SDave Hansen 
14853fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
1486b41572e9SDave Hansen 	if (error)
1487b41572e9SDave Hansen 		return error;
14886146f0d5SMimi Zohar 
14891da177e4SLinus Torvalds 	/*
14901da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
14911da177e4SLinus Torvalds 	 */
14921da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
14938737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
14947715b521SAl Viro 			return -EPERM;
14951da177e4SLinus Torvalds 		if (flag & O_TRUNC)
14967715b521SAl Viro 			return -EPERM;
14971da177e4SLinus Torvalds 	}
14981da177e4SLinus Torvalds 
14991da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
15007715b521SAl Viro 	if (flag & O_NOATIME && !is_owner_or_cap(inode))
15017715b521SAl Viro 		return -EPERM;
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds 	/*
15041da177e4SLinus Torvalds 	 * Ensure there are no outstanding leases on the file.
15051da177e4SLinus Torvalds 	 */
1506b65a9cfcSAl Viro 	return break_lease(inode, flag);
15077715b521SAl Viro }
15087715b521SAl Viro 
15097715b521SAl Viro static int handle_truncate(struct path *path)
15107715b521SAl Viro {
15117715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
15127715b521SAl Viro 	int error = get_write_access(inode);
15131da177e4SLinus Torvalds 	if (error)
15147715b521SAl Viro 		return error;
15151da177e4SLinus Torvalds 	/*
15161da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
15171da177e4SLinus Torvalds 	 */
15181da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
1519be6d3e56SKentaro Takeda 	if (!error)
15203fb64190SChristoph Hellwig 		error = security_path_truncate(path, 0,
1521be6d3e56SKentaro Takeda 				       ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
15221da177e4SLinus Torvalds 	if (!error) {
15237715b521SAl Viro 		error = do_truncate(path->dentry, 0,
1524d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
1525d139d7ffSMiklos Szeredi 				    NULL);
15261da177e4SLinus Torvalds 	}
15271da177e4SLinus Torvalds 	put_write_access(inode);
1528acd0c935SMimi Zohar 	return error;
15291da177e4SLinus Torvalds }
15301da177e4SLinus Torvalds 
1531d57999e1SDave Hansen /*
1532d57999e1SDave Hansen  * Be careful about ever adding any more callers of this
1533d57999e1SDave Hansen  * function.  Its flags must be in the namei format, not
1534d57999e1SDave Hansen  * what get passed to sys_open().
1535d57999e1SDave Hansen  */
1536d57999e1SDave Hansen static int __open_namei_create(struct nameidata *nd, struct path *path,
15378737c930SAl Viro 				int open_flag, int mode)
1538aab520e2SDave Hansen {
1539aab520e2SDave Hansen 	int error;
15404ac91378SJan Blunck 	struct dentry *dir = nd->path.dentry;
1541aab520e2SDave Hansen 
1542aab520e2SDave Hansen 	if (!IS_POSIXACL(dir->d_inode))
1543ce3b0f8dSAl Viro 		mode &= ~current_umask();
1544be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd->path, path->dentry, mode, 0);
1545be6d3e56SKentaro Takeda 	if (error)
1546be6d3e56SKentaro Takeda 		goto out_unlock;
1547aab520e2SDave Hansen 	error = vfs_create(dir->d_inode, path->dentry, mode, nd);
1548be6d3e56SKentaro Takeda out_unlock:
1549aab520e2SDave Hansen 	mutex_unlock(&dir->d_inode->i_mutex);
15504ac91378SJan Blunck 	dput(nd->path.dentry);
15514ac91378SJan Blunck 	nd->path.dentry = path->dentry;
1552aab520e2SDave Hansen 	if (error)
1553aab520e2SDave Hansen 		return error;
1554aab520e2SDave Hansen 	/* Don't check for write permission, don't truncate */
15558737c930SAl Viro 	return may_open(&nd->path, 0, open_flag & ~O_TRUNC);
1556aab520e2SDave Hansen }
1557aab520e2SDave Hansen 
15581da177e4SLinus Torvalds /*
1559d57999e1SDave Hansen  * Note that while the flag value (low two bits) for sys_open means:
1560d57999e1SDave Hansen  *	00 - read-only
1561d57999e1SDave Hansen  *	01 - write-only
1562d57999e1SDave Hansen  *	10 - read-write
1563d57999e1SDave Hansen  *	11 - special
1564d57999e1SDave Hansen  * it is changed into
1565d57999e1SDave Hansen  *	00 - no permissions needed
1566d57999e1SDave Hansen  *	01 - read-permission
1567d57999e1SDave Hansen  *	10 - write-permission
1568d57999e1SDave Hansen  *	11 - read-write
1569d57999e1SDave Hansen  * for the internal routines (ie open_namei()/follow_link() etc)
1570d57999e1SDave Hansen  * This is more logical, and also allows the 00 "no perm needed"
1571d57999e1SDave Hansen  * to be used for symlinks (where the permissions are checked
1572d57999e1SDave Hansen  * later).
1573d57999e1SDave Hansen  *
1574d57999e1SDave Hansen */
1575d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
1576d57999e1SDave Hansen {
1577d57999e1SDave Hansen 	if ((flag+1) & O_ACCMODE)
1578d57999e1SDave Hansen 		flag++;
1579d57999e1SDave Hansen 	return flag;
1580d57999e1SDave Hansen }
1581d57999e1SDave Hansen 
15827715b521SAl Viro static int open_will_truncate(int flag, struct inode *inode)
15834a3fd211SDave Hansen {
1584d57999e1SDave Hansen 	/*
15854a3fd211SDave Hansen 	 * We'll never write to the fs underlying
15864a3fd211SDave Hansen 	 * a device file.
15874a3fd211SDave Hansen 	 */
15884a3fd211SDave Hansen 	if (special_file(inode->i_mode))
15894a3fd211SDave Hansen 		return 0;
15904a3fd211SDave Hansen 	return (flag & O_TRUNC);
15914a3fd211SDave Hansen }
15924a3fd211SDave Hansen 
1593648fa861SAl Viro static struct file *finish_open(struct nameidata *nd,
1594648fa861SAl Viro 				int open_flag, int flag, int acc_mode)
1595648fa861SAl Viro {
1596648fa861SAl Viro 	struct file *filp;
1597648fa861SAl Viro 	int will_truncate;
1598648fa861SAl Viro 	int error;
1599648fa861SAl Viro 
1600648fa861SAl Viro 	will_truncate = open_will_truncate(flag, nd->path.dentry->d_inode);
1601648fa861SAl Viro 	if (will_truncate) {
1602648fa861SAl Viro 		error = mnt_want_write(nd->path.mnt);
1603648fa861SAl Viro 		if (error)
1604648fa861SAl Viro 			goto exit;
1605648fa861SAl Viro 	}
1606648fa861SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
1607648fa861SAl Viro 	if (error) {
1608648fa861SAl Viro 		if (will_truncate)
1609648fa861SAl Viro 			mnt_drop_write(nd->path.mnt);
1610648fa861SAl Viro 		goto exit;
1611648fa861SAl Viro 	}
1612648fa861SAl Viro 	filp = nameidata_to_filp(nd);
1613648fa861SAl Viro 	if (!IS_ERR(filp)) {
1614648fa861SAl Viro 		error = ima_file_check(filp, acc_mode);
1615648fa861SAl Viro 		if (error) {
1616648fa861SAl Viro 			fput(filp);
1617648fa861SAl Viro 			filp = ERR_PTR(error);
1618648fa861SAl Viro 		}
1619648fa861SAl Viro 	}
1620648fa861SAl Viro 	if (!IS_ERR(filp)) {
1621648fa861SAl Viro 		if (acc_mode & MAY_WRITE)
1622648fa861SAl Viro 			vfs_dq_init(nd->path.dentry->d_inode);
1623648fa861SAl Viro 
1624648fa861SAl Viro 		if (will_truncate) {
1625648fa861SAl Viro 			error = handle_truncate(&nd->path);
1626648fa861SAl Viro 			if (error) {
1627648fa861SAl Viro 				fput(filp);
1628648fa861SAl Viro 				filp = ERR_PTR(error);
1629648fa861SAl Viro 			}
1630648fa861SAl Viro 		}
1631648fa861SAl Viro 	}
1632648fa861SAl Viro 	/*
1633648fa861SAl Viro 	 * It is now safe to drop the mnt write
1634648fa861SAl Viro 	 * because the filp has had a write taken
1635648fa861SAl Viro 	 * on its behalf.
1636648fa861SAl Viro 	 */
1637648fa861SAl Viro 	if (will_truncate)
1638648fa861SAl Viro 		mnt_drop_write(nd->path.mnt);
1639648fa861SAl Viro 	return filp;
1640648fa861SAl Viro 
1641648fa861SAl Viro exit:
1642648fa861SAl Viro 	if (!IS_ERR(nd->intent.open.file))
1643648fa861SAl Viro 		release_open_intent(nd);
1644648fa861SAl Viro 	path_put(&nd->path);
1645648fa861SAl Viro 	return ERR_PTR(error);
1646648fa861SAl Viro }
1647648fa861SAl Viro 
1648fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
1649fb1cc555SAl Viro 			    int open_flag, int flag, int acc_mode,
1650fb1cc555SAl Viro 			    int mode, const char *pathname,
1651fb1cc555SAl Viro 			    struct dentry *dir, int *is_link)
1652fb1cc555SAl Viro {
1653fb1cc555SAl Viro 	struct file *filp;
1654fb1cc555SAl Viro 	int error;
1655fb1cc555SAl Viro 
1656fb1cc555SAl Viro 	*is_link = 0;
1657fb1cc555SAl Viro 
1658fb1cc555SAl Viro 	error = PTR_ERR(path->dentry);
1659fb1cc555SAl Viro 	if (IS_ERR(path->dentry)) {
1660fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
1661fb1cc555SAl Viro 		goto exit;
1662fb1cc555SAl Viro 	}
1663fb1cc555SAl Viro 
1664fb1cc555SAl Viro 	if (IS_ERR(nd->intent.open.file)) {
1665fb1cc555SAl Viro 		error = PTR_ERR(nd->intent.open.file);
1666fb1cc555SAl Viro 		goto exit_mutex_unlock;
1667fb1cc555SAl Viro 	}
1668fb1cc555SAl Viro 
1669fb1cc555SAl Viro 	/* Negative dentry, just create the file */
1670fb1cc555SAl Viro 	if (!path->dentry->d_inode) {
1671fb1cc555SAl Viro 		/*
1672fb1cc555SAl Viro 		 * This write is needed to ensure that a
1673fb1cc555SAl Viro 		 * ro->rw transition does not occur between
1674fb1cc555SAl Viro 		 * the time when the file is created and when
1675fb1cc555SAl Viro 		 * a permanent write count is taken through
1676fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
1677fb1cc555SAl Viro 		 */
1678fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
1679fb1cc555SAl Viro 		if (error)
1680fb1cc555SAl Viro 			goto exit_mutex_unlock;
1681fb1cc555SAl Viro 		error = __open_namei_create(nd, path, open_flag, mode);
1682fb1cc555SAl Viro 		if (error) {
1683fb1cc555SAl Viro 			mnt_drop_write(nd->path.mnt);
1684fb1cc555SAl Viro 			goto exit;
1685fb1cc555SAl Viro 		}
1686fb1cc555SAl Viro 		filp = nameidata_to_filp(nd);
1687fb1cc555SAl Viro 		mnt_drop_write(nd->path.mnt);
1688fb1cc555SAl Viro 		if (nd->root.mnt)
1689fb1cc555SAl Viro 			path_put(&nd->root);
1690fb1cc555SAl Viro 		if (!IS_ERR(filp)) {
1691fb1cc555SAl Viro 			error = ima_file_check(filp, acc_mode);
1692fb1cc555SAl Viro 			if (error) {
1693fb1cc555SAl Viro 				fput(filp);
1694fb1cc555SAl Viro 				filp = ERR_PTR(error);
1695fb1cc555SAl Viro 			}
1696fb1cc555SAl Viro 		}
1697fb1cc555SAl Viro 		return filp;
1698fb1cc555SAl Viro 	}
1699fb1cc555SAl Viro 
1700fb1cc555SAl Viro 	/*
1701fb1cc555SAl Viro 	 * It already exists.
1702fb1cc555SAl Viro 	 */
1703fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
1704fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
1705fb1cc555SAl Viro 
1706fb1cc555SAl Viro 	error = -EEXIST;
1707fb1cc555SAl Viro 	if (flag & O_EXCL)
1708fb1cc555SAl Viro 		goto exit_dput;
1709fb1cc555SAl Viro 
1710fb1cc555SAl Viro 	if (__follow_mount(path)) {
1711fb1cc555SAl Viro 		error = -ELOOP;
1712fb1cc555SAl Viro 		if (flag & O_NOFOLLOW)
1713fb1cc555SAl Viro 			goto exit_dput;
1714fb1cc555SAl Viro 	}
1715fb1cc555SAl Viro 
1716fb1cc555SAl Viro 	error = -ENOENT;
1717fb1cc555SAl Viro 	if (!path->dentry->d_inode)
1718fb1cc555SAl Viro 		goto exit_dput;
1719fb1cc555SAl Viro 	if (path->dentry->d_inode->i_op->follow_link) {
1720fb1cc555SAl Viro 		*is_link = 1;
1721fb1cc555SAl Viro 		return NULL;
1722fb1cc555SAl Viro 	}
1723fb1cc555SAl Viro 
1724fb1cc555SAl Viro 	path_to_nameidata(path, nd);
1725fb1cc555SAl Viro 	error = -EISDIR;
1726fb1cc555SAl Viro 	if (S_ISDIR(path->dentry->d_inode->i_mode))
1727fb1cc555SAl Viro 		goto exit;
1728fb1cc555SAl Viro 	filp = finish_open(nd, open_flag, flag, acc_mode);
1729fb1cc555SAl Viro 	if (nd->root.mnt)
1730fb1cc555SAl Viro 		path_put(&nd->root);
1731fb1cc555SAl Viro 	return filp;
1732fb1cc555SAl Viro 
1733fb1cc555SAl Viro exit_mutex_unlock:
1734fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
1735fb1cc555SAl Viro exit_dput:
1736fb1cc555SAl Viro 	path_put_conditional(path, nd);
1737fb1cc555SAl Viro exit:
1738fb1cc555SAl Viro 	if (!IS_ERR(nd->intent.open.file))
1739fb1cc555SAl Viro 		release_open_intent(nd);
1740fb1cc555SAl Viro 	if (nd->root.mnt)
1741fb1cc555SAl Viro 		path_put(&nd->root);
1742fb1cc555SAl Viro 	path_put(&nd->path);
1743fb1cc555SAl Viro 	return ERR_PTR(error);
1744fb1cc555SAl Viro }
1745fb1cc555SAl Viro 
17464a3fd211SDave Hansen /*
17474a3fd211SDave Hansen  * Note that the low bits of the passed in "open_flag"
17484a3fd211SDave Hansen  * are not the same as in the local variable "flag". See
17494a3fd211SDave Hansen  * open_to_namei_flags() for more details.
17501da177e4SLinus Torvalds  */
1751a70e65dfSChristoph Hellwig struct file *do_filp_open(int dfd, const char *pathname,
17526e8341a1SAl Viro 		int open_flag, int mode, int acc_mode)
17531da177e4SLinus Torvalds {
17544a3fd211SDave Hansen 	struct file *filp;
1755a70e65dfSChristoph Hellwig 	struct nameidata nd;
17566e8341a1SAl Viro 	int error;
17579850c056SAl Viro 	struct path path;
17581da177e4SLinus Torvalds 	struct dentry *dir;
17591da177e4SLinus Torvalds 	int count = 0;
1760d57999e1SDave Hansen 	int flag = open_to_namei_flags(open_flag);
17619850c056SAl Viro 	int force_reval = 0;
1762fb1cc555SAl Viro 	int is_link;
17631da177e4SLinus Torvalds 
17646b2f3d1fSChristoph Hellwig 	/*
17656b2f3d1fSChristoph Hellwig 	 * O_SYNC is implemented as __O_SYNC|O_DSYNC.  As many places only
17666b2f3d1fSChristoph Hellwig 	 * check for O_DSYNC if the need any syncing at all we enforce it's
17676b2f3d1fSChristoph Hellwig 	 * always set instead of having to deal with possibly weird behaviour
17686b2f3d1fSChristoph Hellwig 	 * for malicious applications setting only __O_SYNC.
17696b2f3d1fSChristoph Hellwig 	 */
17706b2f3d1fSChristoph Hellwig 	if (open_flag & __O_SYNC)
17716b2f3d1fSChristoph Hellwig 		open_flag |= O_DSYNC;
17726b2f3d1fSChristoph Hellwig 
17736e8341a1SAl Viro 	if (!acc_mode)
17746d125529SAl Viro 		acc_mode = MAY_OPEN | ACC_MODE(open_flag);
17751da177e4SLinus Torvalds 
1776834f2a4aSTrond Myklebust 	/* O_TRUNC implies we need access checks for write permissions */
1777834f2a4aSTrond Myklebust 	if (flag & O_TRUNC)
1778834f2a4aSTrond Myklebust 		acc_mode |= MAY_WRITE;
1779834f2a4aSTrond Myklebust 
17801da177e4SLinus Torvalds 	/* Allow the LSM permission hook to distinguish append
17811da177e4SLinus Torvalds 	   access from general write access. */
17821da177e4SLinus Torvalds 	if (flag & O_APPEND)
17831da177e4SLinus Torvalds 		acc_mode |= MAY_APPEND;
17841da177e4SLinus Torvalds 
17851da177e4SLinus Torvalds 	/*
17861da177e4SLinus Torvalds 	 * The simplest case - just a plain lookup.
17871da177e4SLinus Torvalds 	 */
17881da177e4SLinus Torvalds 	if (!(flag & O_CREAT)) {
17892dd6d1f4SAl Viro 		filp = get_empty_filp();
17902dd6d1f4SAl Viro 
17912dd6d1f4SAl Viro 		if (filp == NULL)
17922dd6d1f4SAl Viro 			return ERR_PTR(-ENFILE);
17932dd6d1f4SAl Viro 		nd.intent.open.file = filp;
1794482928d5SAl Viro 		filp->f_flags = open_flag;
17952dd6d1f4SAl Viro 		nd.intent.open.flags = flag;
17962dd6d1f4SAl Viro 		nd.intent.open.create_mode = 0;
17972dd6d1f4SAl Viro 		error = do_path_lookup(dfd, pathname,
17982dd6d1f4SAl Viro 					lookup_flags(flag)|LOOKUP_OPEN, &nd);
17992dd6d1f4SAl Viro 		if (IS_ERR(nd.intent.open.file)) {
18002dd6d1f4SAl Viro 			if (error == 0) {
18012dd6d1f4SAl Viro 				error = PTR_ERR(nd.intent.open.file);
18022dd6d1f4SAl Viro 				path_put(&nd.path);
18032dd6d1f4SAl Viro 			}
18042dd6d1f4SAl Viro 		} else if (error)
18052dd6d1f4SAl Viro 			release_open_intent(&nd);
18061da177e4SLinus Torvalds 		if (error)
1807a70e65dfSChristoph Hellwig 			return ERR_PTR(error);
18081da177e4SLinus Torvalds 		goto ok;
18091da177e4SLinus Torvalds 	}
18101da177e4SLinus Torvalds 
18111da177e4SLinus Torvalds 	/*
18121da177e4SLinus Torvalds 	 * Create - we need to know the parent.
18131da177e4SLinus Torvalds 	 */
18149850c056SAl Viro reval:
18159b4a9b14SAl Viro 	error = path_init(dfd, pathname, LOOKUP_PARENT, &nd);
18161da177e4SLinus Torvalds 	if (error)
1817a70e65dfSChristoph Hellwig 		return ERR_PTR(error);
18189850c056SAl Viro 	if (force_reval)
18199850c056SAl Viro 		nd.flags |= LOOKUP_REVAL;
18209b4a9b14SAl Viro 	error = path_walk(pathname, &nd);
1821654f562cSJ. R. Okajima 	if (error) {
1822654f562cSJ. R. Okajima 		if (nd.root.mnt)
1823654f562cSJ. R. Okajima 			path_put(&nd.root);
18249b4a9b14SAl Viro 		return ERR_PTR(error);
1825654f562cSJ. R. Okajima 	}
18269b4a9b14SAl Viro 	if (unlikely(!audit_dummy_context()))
18279b4a9b14SAl Viro 		audit_inode(pathname, nd.path.dentry);
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds 	/*
18301da177e4SLinus Torvalds 	 * We have the parent and last component. First of all, check
18311da177e4SLinus Torvalds 	 * that we are not asked to creat(2) an obvious directory - that
18321da177e4SLinus Torvalds 	 * will not do.
18331da177e4SLinus Torvalds 	 */
18341da177e4SLinus Torvalds 	error = -EISDIR;
1835a70e65dfSChristoph Hellwig 	if (nd.last_type != LAST_NORM || nd.last.name[nd.last.len])
18368737f3a1SAl Viro 		goto exit_parent;
18371da177e4SLinus Torvalds 
18388737f3a1SAl Viro 	error = -ENFILE;
18398737f3a1SAl Viro 	filp = get_empty_filp();
18408737f3a1SAl Viro 	if (filp == NULL)
18418737f3a1SAl Viro 		goto exit_parent;
18428737f3a1SAl Viro 	nd.intent.open.file = filp;
1843482928d5SAl Viro 	filp->f_flags = open_flag;
18448737f3a1SAl Viro 	nd.intent.open.flags = flag;
18458737f3a1SAl Viro 	nd.intent.open.create_mode = mode;
1846a70e65dfSChristoph Hellwig 	dir = nd.path.dentry;
1847a70e65dfSChristoph Hellwig 	nd.flags &= ~LOOKUP_PARENT;
18488737f3a1SAl Viro 	nd.flags |= LOOKUP_CREATE | LOOKUP_OPEN;
18493516586aSAl Viro 	if (flag & O_EXCL)
18503516586aSAl Viro 		nd.flags |= LOOKUP_EXCL;
18511b1dcc1bSJes Sorensen 	mutex_lock(&dir->d_inode->i_mutex);
1852a70e65dfSChristoph Hellwig 	path.dentry = lookup_hash(&nd);
1853a70e65dfSChristoph Hellwig 	path.mnt = nd.path.mnt;
18541da177e4SLinus Torvalds 
18551da177e4SLinus Torvalds do_last:
1856fb1cc555SAl Viro 	filp = do_last(&nd, &path, open_flag, flag, acc_mode, mode,
1857fb1cc555SAl Viro 		       pathname, dir, &is_link);
1858fb1cc555SAl Viro 	if (is_link)
18591da177e4SLinus Torvalds 		goto do_link;
1860fb1cc555SAl Viro 	return filp;
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds ok:
1863648fa861SAl Viro 	filp = finish_open(&nd, open_flag, flag, acc_mode);
1864654f562cSJ. R. Okajima 	if (nd.root.mnt)
1865654f562cSJ. R. Okajima 		path_put(&nd.root);
18664a3fd211SDave Hansen 	return filp;
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds exit_dput:
1869a70e65dfSChristoph Hellwig 	path_put_conditional(&path, &nd);
18701da177e4SLinus Torvalds exit:
1871a70e65dfSChristoph Hellwig 	if (!IS_ERR(nd.intent.open.file))
1872a70e65dfSChristoph Hellwig 		release_open_intent(&nd);
18738737f3a1SAl Viro exit_parent:
18742a737871SAl Viro 	if (nd.root.mnt)
18752a737871SAl Viro 		path_put(&nd.root);
1876a70e65dfSChristoph Hellwig 	path_put(&nd.path);
1877a70e65dfSChristoph Hellwig 	return ERR_PTR(error);
18781da177e4SLinus Torvalds 
18791da177e4SLinus Torvalds do_link:
18801da177e4SLinus Torvalds 	error = -ELOOP;
18811da177e4SLinus Torvalds 	if (flag & O_NOFOLLOW)
18821da177e4SLinus Torvalds 		goto exit_dput;
18831da177e4SLinus Torvalds 	/*
18841da177e4SLinus Torvalds 	 * This is subtle. Instead of calling do_follow_link() we do the
18851da177e4SLinus Torvalds 	 * thing by hands. The reason is that this way we have zero link_count
18861da177e4SLinus Torvalds 	 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
18871da177e4SLinus Torvalds 	 * After that we have the parent and last component, i.e.
18881da177e4SLinus Torvalds 	 * we are in the same situation as after the first path_walk().
18891da177e4SLinus Torvalds 	 * Well, almost - if the last component is normal we get its copy
18901da177e4SLinus Torvalds 	 * stored in nd->last.name and we will have to putname() it when we
18911da177e4SLinus Torvalds 	 * are done. Procfs-like symlinks just set LAST_BIND.
18921da177e4SLinus Torvalds 	 */
1893a70e65dfSChristoph Hellwig 	nd.flags |= LOOKUP_PARENT;
1894a70e65dfSChristoph Hellwig 	error = security_inode_follow_link(path.dentry, &nd);
18951da177e4SLinus Torvalds 	if (error)
18961da177e4SLinus Torvalds 		goto exit_dput;
1897a70e65dfSChristoph Hellwig 	error = __do_follow_link(&path, &nd);
1898258fa999SAl Viro 	path_put(&path);
1899de459215SKirill Korotaev 	if (error) {
1900de459215SKirill Korotaev 		/* Does someone understand code flow here? Or it is only
1901de459215SKirill Korotaev 		 * me so stupid? Anathema to whoever designed this non-sense
1902de459215SKirill Korotaev 		 * with "intent.open".
1903de459215SKirill Korotaev 		 */
1904a70e65dfSChristoph Hellwig 		release_open_intent(&nd);
1905654f562cSJ. R. Okajima 		if (nd.root.mnt)
1906654f562cSJ. R. Okajima 			path_put(&nd.root);
19079850c056SAl Viro 		if (error == -ESTALE && !force_reval) {
19089850c056SAl Viro 			force_reval = 1;
19099850c056SAl Viro 			goto reval;
19109850c056SAl Viro 		}
1911a70e65dfSChristoph Hellwig 		return ERR_PTR(error);
1912de459215SKirill Korotaev 	}
1913a70e65dfSChristoph Hellwig 	nd.flags &= ~LOOKUP_PARENT;
1914a70e65dfSChristoph Hellwig 	if (nd.last_type == LAST_BIND)
19151da177e4SLinus Torvalds 		goto ok;
19161da177e4SLinus Torvalds 	error = -EISDIR;
1917a70e65dfSChristoph Hellwig 	if (nd.last_type != LAST_NORM)
19181da177e4SLinus Torvalds 		goto exit;
1919a70e65dfSChristoph Hellwig 	if (nd.last.name[nd.last.len]) {
1920a70e65dfSChristoph Hellwig 		__putname(nd.last.name);
19211da177e4SLinus Torvalds 		goto exit;
19221da177e4SLinus Torvalds 	}
19231da177e4SLinus Torvalds 	error = -ELOOP;
19241da177e4SLinus Torvalds 	if (count++==32) {
1925a70e65dfSChristoph Hellwig 		__putname(nd.last.name);
19261da177e4SLinus Torvalds 		goto exit;
19271da177e4SLinus Torvalds 	}
1928a70e65dfSChristoph Hellwig 	dir = nd.path.dentry;
19291b1dcc1bSJes Sorensen 	mutex_lock(&dir->d_inode->i_mutex);
1930a70e65dfSChristoph Hellwig 	path.dentry = lookup_hash(&nd);
1931a70e65dfSChristoph Hellwig 	path.mnt = nd.path.mnt;
1932a70e65dfSChristoph Hellwig 	__putname(nd.last.name);
19331da177e4SLinus Torvalds 	goto do_last;
19341da177e4SLinus Torvalds }
19351da177e4SLinus Torvalds 
19361da177e4SLinus Torvalds /**
1937a70e65dfSChristoph Hellwig  * filp_open - open file and return file pointer
1938a70e65dfSChristoph Hellwig  *
1939a70e65dfSChristoph Hellwig  * @filename:	path to open
1940a70e65dfSChristoph Hellwig  * @flags:	open flags as per the open(2) second argument
1941a70e65dfSChristoph Hellwig  * @mode:	mode for the new file if O_CREAT is set, else ignored
1942a70e65dfSChristoph Hellwig  *
1943a70e65dfSChristoph Hellwig  * This is the helper to open a file from kernelspace if you really
1944a70e65dfSChristoph Hellwig  * have to.  But in generally you should not do this, so please move
1945a70e65dfSChristoph Hellwig  * along, nothing to see here..
1946a70e65dfSChristoph Hellwig  */
1947a70e65dfSChristoph Hellwig struct file *filp_open(const char *filename, int flags, int mode)
1948a70e65dfSChristoph Hellwig {
19496e8341a1SAl Viro 	return do_filp_open(AT_FDCWD, filename, flags, mode, 0);
1950a70e65dfSChristoph Hellwig }
1951a70e65dfSChristoph Hellwig EXPORT_SYMBOL(filp_open);
1952a70e65dfSChristoph Hellwig 
1953a70e65dfSChristoph Hellwig /**
19541da177e4SLinus Torvalds  * lookup_create - lookup a dentry, creating it if it doesn't exist
19551da177e4SLinus Torvalds  * @nd: nameidata info
19561da177e4SLinus Torvalds  * @is_dir: directory flag
19571da177e4SLinus Torvalds  *
19581da177e4SLinus Torvalds  * Simple function to lookup and return a dentry and create it
19591da177e4SLinus Torvalds  * if it doesn't exist.  Is SMP-safe.
1960c663e5d8SChristoph Hellwig  *
19614ac91378SJan Blunck  * Returns with nd->path.dentry->d_inode->i_mutex locked.
19621da177e4SLinus Torvalds  */
19631da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir)
19641da177e4SLinus Torvalds {
1965c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
19661da177e4SLinus Torvalds 
19674ac91378SJan Blunck 	mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
1968c663e5d8SChristoph Hellwig 	/*
1969c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
1970c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
1971c663e5d8SChristoph Hellwig 	 */
19721da177e4SLinus Torvalds 	if (nd->last_type != LAST_NORM)
19731da177e4SLinus Torvalds 		goto fail;
19741da177e4SLinus Torvalds 	nd->flags &= ~LOOKUP_PARENT;
19753516586aSAl Viro 	nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
1976a634904aSASANO Masahiro 	nd->intent.open.flags = O_EXCL;
1977c663e5d8SChristoph Hellwig 
1978c663e5d8SChristoph Hellwig 	/*
1979c663e5d8SChristoph Hellwig 	 * Do the final lookup.
1980c663e5d8SChristoph Hellwig 	 */
198149705b77SChristoph Hellwig 	dentry = lookup_hash(nd);
19821da177e4SLinus Torvalds 	if (IS_ERR(dentry))
19831da177e4SLinus Torvalds 		goto fail;
1984c663e5d8SChristoph Hellwig 
1985e9baf6e5SAl Viro 	if (dentry->d_inode)
1986e9baf6e5SAl Viro 		goto eexist;
1987c663e5d8SChristoph Hellwig 	/*
1988c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
1989c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
1990c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
1991c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
1992c663e5d8SChristoph Hellwig 	 */
1993e9baf6e5SAl Viro 	if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
19941da177e4SLinus Torvalds 		dput(dentry);
19951da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
1996e9baf6e5SAl Viro 	}
1997e9baf6e5SAl Viro 	return dentry;
1998e9baf6e5SAl Viro eexist:
1999e9baf6e5SAl Viro 	dput(dentry);
2000e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
20011da177e4SLinus Torvalds fail:
20021da177e4SLinus Torvalds 	return dentry;
20031da177e4SLinus Torvalds }
2004f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create);
20051da177e4SLinus Torvalds 
20061da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
20071da177e4SLinus Torvalds {
2008a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
20091da177e4SLinus Torvalds 
20101da177e4SLinus Torvalds 	if (error)
20111da177e4SLinus Torvalds 		return error;
20121da177e4SLinus Torvalds 
20131da177e4SLinus Torvalds 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
20141da177e4SLinus Torvalds 		return -EPERM;
20151da177e4SLinus Torvalds 
2016acfa4380SAl Viro 	if (!dir->i_op->mknod)
20171da177e4SLinus Torvalds 		return -EPERM;
20181da177e4SLinus Torvalds 
201908ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
202008ce5f16SSerge E. Hallyn 	if (error)
202108ce5f16SSerge E. Hallyn 		return error;
202208ce5f16SSerge E. Hallyn 
20231da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
20241da177e4SLinus Torvalds 	if (error)
20251da177e4SLinus Torvalds 		return error;
20261da177e4SLinus Torvalds 
20279e3509e2SJan Kara 	vfs_dq_init(dir);
20281da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2029a74574aaSStephen Smalley 	if (!error)
2030f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
20311da177e4SLinus Torvalds 	return error;
20321da177e4SLinus Torvalds }
20331da177e4SLinus Torvalds 
2034463c3197SDave Hansen static int may_mknod(mode_t mode)
2035463c3197SDave Hansen {
2036463c3197SDave Hansen 	switch (mode & S_IFMT) {
2037463c3197SDave Hansen 	case S_IFREG:
2038463c3197SDave Hansen 	case S_IFCHR:
2039463c3197SDave Hansen 	case S_IFBLK:
2040463c3197SDave Hansen 	case S_IFIFO:
2041463c3197SDave Hansen 	case S_IFSOCK:
2042463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2043463c3197SDave Hansen 		return 0;
2044463c3197SDave Hansen 	case S_IFDIR:
2045463c3197SDave Hansen 		return -EPERM;
2046463c3197SDave Hansen 	default:
2047463c3197SDave Hansen 		return -EINVAL;
2048463c3197SDave Hansen 	}
2049463c3197SDave Hansen }
2050463c3197SDave Hansen 
20512e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
20522e4d0924SHeiko Carstens 		unsigned, dev)
20531da177e4SLinus Torvalds {
20542ad94ae6SAl Viro 	int error;
20551da177e4SLinus Torvalds 	char *tmp;
20561da177e4SLinus Torvalds 	struct dentry *dentry;
20571da177e4SLinus Torvalds 	struct nameidata nd;
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds 	if (S_ISDIR(mode))
20601da177e4SLinus Torvalds 		return -EPERM;
20611da177e4SLinus Torvalds 
20622ad94ae6SAl Viro 	error = user_path_parent(dfd, filename, &nd, &tmp);
20631da177e4SLinus Torvalds 	if (error)
20642ad94ae6SAl Viro 		return error;
20652ad94ae6SAl Viro 
20661da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
2067463c3197SDave Hansen 	if (IS_ERR(dentry)) {
20681da177e4SLinus Torvalds 		error = PTR_ERR(dentry);
2069463c3197SDave Hansen 		goto out_unlock;
2070463c3197SDave Hansen 	}
20714ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2072ce3b0f8dSAl Viro 		mode &= ~current_umask();
2073463c3197SDave Hansen 	error = may_mknod(mode);
2074463c3197SDave Hansen 	if (error)
2075463c3197SDave Hansen 		goto out_dput;
2076463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2077463c3197SDave Hansen 	if (error)
2078463c3197SDave Hansen 		goto out_dput;
2079be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd.path, dentry, mode, dev);
2080be6d3e56SKentaro Takeda 	if (error)
2081be6d3e56SKentaro Takeda 		goto out_drop_write;
20821da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
20831da177e4SLinus Torvalds 		case 0: case S_IFREG:
20844ac91378SJan Blunck 			error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
20851da177e4SLinus Torvalds 			break;
20861da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
20874ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
20881da177e4SLinus Torvalds 					new_decode_dev(dev));
20891da177e4SLinus Torvalds 			break;
20901da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
20914ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
20921da177e4SLinus Torvalds 			break;
20931da177e4SLinus Torvalds 	}
2094be6d3e56SKentaro Takeda out_drop_write:
2095463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2096463c3197SDave Hansen out_dput:
20971da177e4SLinus Torvalds 	dput(dentry);
2098463c3197SDave Hansen out_unlock:
20994ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
21001d957f9bSJan Blunck 	path_put(&nd.path);
21011da177e4SLinus Torvalds 	putname(tmp);
21021da177e4SLinus Torvalds 
21031da177e4SLinus Torvalds 	return error;
21041da177e4SLinus Torvalds }
21051da177e4SLinus Torvalds 
21063480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
21075590ff0dSUlrich Drepper {
21085590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
21095590ff0dSUlrich Drepper }
21105590ff0dSUlrich Drepper 
21111da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
21121da177e4SLinus Torvalds {
2113a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
21141da177e4SLinus Torvalds 
21151da177e4SLinus Torvalds 	if (error)
21161da177e4SLinus Torvalds 		return error;
21171da177e4SLinus Torvalds 
2118acfa4380SAl Viro 	if (!dir->i_op->mkdir)
21191da177e4SLinus Torvalds 		return -EPERM;
21201da177e4SLinus Torvalds 
21211da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
21221da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
21231da177e4SLinus Torvalds 	if (error)
21241da177e4SLinus Torvalds 		return error;
21251da177e4SLinus Torvalds 
21269e3509e2SJan Kara 	vfs_dq_init(dir);
21271da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2128a74574aaSStephen Smalley 	if (!error)
2129f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
21301da177e4SLinus Torvalds 	return error;
21311da177e4SLinus Torvalds }
21321da177e4SLinus Torvalds 
21332e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
21341da177e4SLinus Torvalds {
21351da177e4SLinus Torvalds 	int error = 0;
21361da177e4SLinus Torvalds 	char * tmp;
21376902d925SDave Hansen 	struct dentry *dentry;
21386902d925SDave Hansen 	struct nameidata nd;
21391da177e4SLinus Torvalds 
21402ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &tmp);
21412ad94ae6SAl Viro 	if (error)
21426902d925SDave Hansen 		goto out_err;
21431da177e4SLinus Torvalds 
21441da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 1);
21451da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
21466902d925SDave Hansen 	if (IS_ERR(dentry))
21476902d925SDave Hansen 		goto out_unlock;
21486902d925SDave Hansen 
21494ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2150ce3b0f8dSAl Viro 		mode &= ~current_umask();
2151463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2152463c3197SDave Hansen 	if (error)
2153463c3197SDave Hansen 		goto out_dput;
2154be6d3e56SKentaro Takeda 	error = security_path_mkdir(&nd.path, dentry, mode);
2155be6d3e56SKentaro Takeda 	if (error)
2156be6d3e56SKentaro Takeda 		goto out_drop_write;
21574ac91378SJan Blunck 	error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2158be6d3e56SKentaro Takeda out_drop_write:
2159463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2160463c3197SDave Hansen out_dput:
21611da177e4SLinus Torvalds 	dput(dentry);
21626902d925SDave Hansen out_unlock:
21634ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
21641d957f9bSJan Blunck 	path_put(&nd.path);
21651da177e4SLinus Torvalds 	putname(tmp);
21666902d925SDave Hansen out_err:
21671da177e4SLinus Torvalds 	return error;
21681da177e4SLinus Torvalds }
21691da177e4SLinus Torvalds 
21703cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
21715590ff0dSUlrich Drepper {
21725590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
21735590ff0dSUlrich Drepper }
21745590ff0dSUlrich Drepper 
21751da177e4SLinus Torvalds /*
21761da177e4SLinus Torvalds  * We try to drop the dentry early: we should have
21771da177e4SLinus Torvalds  * a usage count of 2 if we're the only user of this
21781da177e4SLinus Torvalds  * dentry, and if that is true (possibly after pruning
21791da177e4SLinus Torvalds  * the dcache), then we drop the dentry now.
21801da177e4SLinus Torvalds  *
21811da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
21821da177e4SLinus Torvalds  * do a
21831da177e4SLinus Torvalds  *
21841da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
21851da177e4SLinus Torvalds  *		return -EBUSY;
21861da177e4SLinus Torvalds  *
21871da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
21881da177e4SLinus Torvalds  * that is still in use by something else..
21891da177e4SLinus Torvalds  */
21901da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
21911da177e4SLinus Torvalds {
21921da177e4SLinus Torvalds 	dget(dentry);
21931da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
21941da177e4SLinus Torvalds 	spin_lock(&dcache_lock);
21951da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
21961da177e4SLinus Torvalds 	if (atomic_read(&dentry->d_count) == 2)
21971da177e4SLinus Torvalds 		__d_drop(dentry);
21981da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
21991da177e4SLinus Torvalds 	spin_unlock(&dcache_lock);
22001da177e4SLinus Torvalds }
22011da177e4SLinus Torvalds 
22021da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
22031da177e4SLinus Torvalds {
22041da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
22051da177e4SLinus Torvalds 
22061da177e4SLinus Torvalds 	if (error)
22071da177e4SLinus Torvalds 		return error;
22081da177e4SLinus Torvalds 
2209acfa4380SAl Viro 	if (!dir->i_op->rmdir)
22101da177e4SLinus Torvalds 		return -EPERM;
22111da177e4SLinus Torvalds 
22129e3509e2SJan Kara 	vfs_dq_init(dir);
22131da177e4SLinus Torvalds 
22141b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
22151da177e4SLinus Torvalds 	dentry_unhash(dentry);
22161da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
22171da177e4SLinus Torvalds 		error = -EBUSY;
22181da177e4SLinus Torvalds 	else {
22191da177e4SLinus Torvalds 		error = security_inode_rmdir(dir, dentry);
22201da177e4SLinus Torvalds 		if (!error) {
22211da177e4SLinus Torvalds 			error = dir->i_op->rmdir(dir, dentry);
22221da177e4SLinus Torvalds 			if (!error)
22231da177e4SLinus Torvalds 				dentry->d_inode->i_flags |= S_DEAD;
22241da177e4SLinus Torvalds 		}
22251da177e4SLinus Torvalds 	}
22261b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
22271da177e4SLinus Torvalds 	if (!error) {
22281da177e4SLinus Torvalds 		d_delete(dentry);
22291da177e4SLinus Torvalds 	}
22301da177e4SLinus Torvalds 	dput(dentry);
22311da177e4SLinus Torvalds 
22321da177e4SLinus Torvalds 	return error;
22331da177e4SLinus Torvalds }
22341da177e4SLinus Torvalds 
22355590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
22361da177e4SLinus Torvalds {
22371da177e4SLinus Torvalds 	int error = 0;
22381da177e4SLinus Torvalds 	char * name;
22391da177e4SLinus Torvalds 	struct dentry *dentry;
22401da177e4SLinus Torvalds 	struct nameidata nd;
22411da177e4SLinus Torvalds 
22422ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
22431da177e4SLinus Torvalds 	if (error)
22442ad94ae6SAl Viro 		return error;
22451da177e4SLinus Torvalds 
22461da177e4SLinus Torvalds 	switch(nd.last_type) {
22471da177e4SLinus Torvalds 	case LAST_DOTDOT:
22481da177e4SLinus Torvalds 		error = -ENOTEMPTY;
22491da177e4SLinus Torvalds 		goto exit1;
22501da177e4SLinus Torvalds 	case LAST_DOT:
22511da177e4SLinus Torvalds 		error = -EINVAL;
22521da177e4SLinus Torvalds 		goto exit1;
22531da177e4SLinus Torvalds 	case LAST_ROOT:
22541da177e4SLinus Torvalds 		error = -EBUSY;
22551da177e4SLinus Torvalds 		goto exit1;
22561da177e4SLinus Torvalds 	}
22570612d9fbSOGAWA Hirofumi 
22580612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
22590612d9fbSOGAWA Hirofumi 
22604ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
226149705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
22621da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
22636902d925SDave Hansen 	if (IS_ERR(dentry))
22646902d925SDave Hansen 		goto exit2;
22650622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
22660622753bSDave Hansen 	if (error)
22670622753bSDave Hansen 		goto exit3;
2268be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2269be6d3e56SKentaro Takeda 	if (error)
2270be6d3e56SKentaro Takeda 		goto exit4;
22714ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2272be6d3e56SKentaro Takeda exit4:
22730622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
22740622753bSDave Hansen exit3:
22751da177e4SLinus Torvalds 	dput(dentry);
22766902d925SDave Hansen exit2:
22774ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
22781da177e4SLinus Torvalds exit1:
22791d957f9bSJan Blunck 	path_put(&nd.path);
22801da177e4SLinus Torvalds 	putname(name);
22811da177e4SLinus Torvalds 	return error;
22821da177e4SLinus Torvalds }
22831da177e4SLinus Torvalds 
22843cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
22855590ff0dSUlrich Drepper {
22865590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
22875590ff0dSUlrich Drepper }
22885590ff0dSUlrich Drepper 
22891da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
22901da177e4SLinus Torvalds {
22911da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
22921da177e4SLinus Torvalds 
22931da177e4SLinus Torvalds 	if (error)
22941da177e4SLinus Torvalds 		return error;
22951da177e4SLinus Torvalds 
2296acfa4380SAl Viro 	if (!dir->i_op->unlink)
22971da177e4SLinus Torvalds 		return -EPERM;
22981da177e4SLinus Torvalds 
22999e3509e2SJan Kara 	vfs_dq_init(dir);
23001da177e4SLinus Torvalds 
23011b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
23021da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
23031da177e4SLinus Torvalds 		error = -EBUSY;
23041da177e4SLinus Torvalds 	else {
23051da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2306bec1052eSAl Viro 		if (!error) {
23071da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2308bec1052eSAl Viro 			if (!error)
2309bec1052eSAl Viro 				dentry->d_inode->i_flags |= S_DEAD;
2310bec1052eSAl Viro 		}
23111da177e4SLinus Torvalds 	}
23121b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
23131da177e4SLinus Torvalds 
23141da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
23151da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2316ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
23171da177e4SLinus Torvalds 		d_delete(dentry);
23181da177e4SLinus Torvalds 	}
23190eeca283SRobert Love 
23201da177e4SLinus Torvalds 	return error;
23211da177e4SLinus Torvalds }
23221da177e4SLinus Torvalds 
23231da177e4SLinus Torvalds /*
23241da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
23251b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
23261da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
23271da177e4SLinus Torvalds  * while waiting on the I/O.
23281da177e4SLinus Torvalds  */
23295590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
23301da177e4SLinus Torvalds {
23312ad94ae6SAl Viro 	int error;
23321da177e4SLinus Torvalds 	char *name;
23331da177e4SLinus Torvalds 	struct dentry *dentry;
23341da177e4SLinus Torvalds 	struct nameidata nd;
23351da177e4SLinus Torvalds 	struct inode *inode = NULL;
23361da177e4SLinus Torvalds 
23372ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
23381da177e4SLinus Torvalds 	if (error)
23392ad94ae6SAl Viro 		return error;
23402ad94ae6SAl Viro 
23411da177e4SLinus Torvalds 	error = -EISDIR;
23421da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
23431da177e4SLinus Torvalds 		goto exit1;
23440612d9fbSOGAWA Hirofumi 
23450612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
23460612d9fbSOGAWA Hirofumi 
23474ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
234849705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
23491da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
23501da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
23511da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
23521da177e4SLinus Torvalds 		if (nd.last.name[nd.last.len])
23531da177e4SLinus Torvalds 			goto slashes;
23541da177e4SLinus Torvalds 		inode = dentry->d_inode;
23551da177e4SLinus Torvalds 		if (inode)
23561da177e4SLinus Torvalds 			atomic_inc(&inode->i_count);
23570622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
23580622753bSDave Hansen 		if (error)
23590622753bSDave Hansen 			goto exit2;
2360be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2361be6d3e56SKentaro Takeda 		if (error)
2362be6d3e56SKentaro Takeda 			goto exit3;
23634ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2364be6d3e56SKentaro Takeda exit3:
23650622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
23661da177e4SLinus Torvalds 	exit2:
23671da177e4SLinus Torvalds 		dput(dentry);
23681da177e4SLinus Torvalds 	}
23694ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
23701da177e4SLinus Torvalds 	if (inode)
23711da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
23721da177e4SLinus Torvalds exit1:
23731d957f9bSJan Blunck 	path_put(&nd.path);
23741da177e4SLinus Torvalds 	putname(name);
23751da177e4SLinus Torvalds 	return error;
23761da177e4SLinus Torvalds 
23771da177e4SLinus Torvalds slashes:
23781da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
23791da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
23801da177e4SLinus Torvalds 	goto exit2;
23811da177e4SLinus Torvalds }
23821da177e4SLinus Torvalds 
23832e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
23845590ff0dSUlrich Drepper {
23855590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
23865590ff0dSUlrich Drepper 		return -EINVAL;
23875590ff0dSUlrich Drepper 
23885590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
23895590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
23905590ff0dSUlrich Drepper 
23915590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
23925590ff0dSUlrich Drepper }
23935590ff0dSUlrich Drepper 
23943480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
23955590ff0dSUlrich Drepper {
23965590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
23975590ff0dSUlrich Drepper }
23985590ff0dSUlrich Drepper 
2399db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
24001da177e4SLinus Torvalds {
2401a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
24021da177e4SLinus Torvalds 
24031da177e4SLinus Torvalds 	if (error)
24041da177e4SLinus Torvalds 		return error;
24051da177e4SLinus Torvalds 
2406acfa4380SAl Viro 	if (!dir->i_op->symlink)
24071da177e4SLinus Torvalds 		return -EPERM;
24081da177e4SLinus Torvalds 
24091da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
24101da177e4SLinus Torvalds 	if (error)
24111da177e4SLinus Torvalds 		return error;
24121da177e4SLinus Torvalds 
24139e3509e2SJan Kara 	vfs_dq_init(dir);
24141da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2415a74574aaSStephen Smalley 	if (!error)
2416f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
24171da177e4SLinus Torvalds 	return error;
24181da177e4SLinus Torvalds }
24191da177e4SLinus Torvalds 
24202e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
24212e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
24221da177e4SLinus Torvalds {
24232ad94ae6SAl Viro 	int error;
24241da177e4SLinus Torvalds 	char *from;
24251da177e4SLinus Torvalds 	char *to;
24266902d925SDave Hansen 	struct dentry *dentry;
24276902d925SDave Hansen 	struct nameidata nd;
24281da177e4SLinus Torvalds 
24291da177e4SLinus Torvalds 	from = getname(oldname);
24301da177e4SLinus Torvalds 	if (IS_ERR(from))
24311da177e4SLinus Torvalds 		return PTR_ERR(from);
24322ad94ae6SAl Viro 
24332ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
24342ad94ae6SAl Viro 	if (error)
24356902d925SDave Hansen 		goto out_putname;
24361da177e4SLinus Torvalds 
24371da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
24381da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
24396902d925SDave Hansen 	if (IS_ERR(dentry))
24406902d925SDave Hansen 		goto out_unlock;
24416902d925SDave Hansen 
244275c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
244375c3f29dSDave Hansen 	if (error)
244475c3f29dSDave Hansen 		goto out_dput;
2445be6d3e56SKentaro Takeda 	error = security_path_symlink(&nd.path, dentry, from);
2446be6d3e56SKentaro Takeda 	if (error)
2447be6d3e56SKentaro Takeda 		goto out_drop_write;
2448db2e747bSMiklos Szeredi 	error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2449be6d3e56SKentaro Takeda out_drop_write:
245075c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
245175c3f29dSDave Hansen out_dput:
24521da177e4SLinus Torvalds 	dput(dentry);
24536902d925SDave Hansen out_unlock:
24544ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
24551d957f9bSJan Blunck 	path_put(&nd.path);
24561da177e4SLinus Torvalds 	putname(to);
24576902d925SDave Hansen out_putname:
24581da177e4SLinus Torvalds 	putname(from);
24591da177e4SLinus Torvalds 	return error;
24601da177e4SLinus Torvalds }
24611da177e4SLinus Torvalds 
24623480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
24635590ff0dSUlrich Drepper {
24645590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
24655590ff0dSUlrich Drepper }
24665590ff0dSUlrich Drepper 
24671da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
24681da177e4SLinus Torvalds {
24691da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
24701da177e4SLinus Torvalds 	int error;
24711da177e4SLinus Torvalds 
24721da177e4SLinus Torvalds 	if (!inode)
24731da177e4SLinus Torvalds 		return -ENOENT;
24741da177e4SLinus Torvalds 
2475a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
24761da177e4SLinus Torvalds 	if (error)
24771da177e4SLinus Torvalds 		return error;
24781da177e4SLinus Torvalds 
24791da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
24801da177e4SLinus Torvalds 		return -EXDEV;
24811da177e4SLinus Torvalds 
24821da177e4SLinus Torvalds 	/*
24831da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
24841da177e4SLinus Torvalds 	 */
24851da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
24861da177e4SLinus Torvalds 		return -EPERM;
2487acfa4380SAl Viro 	if (!dir->i_op->link)
24881da177e4SLinus Torvalds 		return -EPERM;
24897e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
24901da177e4SLinus Torvalds 		return -EPERM;
24911da177e4SLinus Torvalds 
24921da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
24931da177e4SLinus Torvalds 	if (error)
24941da177e4SLinus Torvalds 		return error;
24951da177e4SLinus Torvalds 
24967e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
24979e3509e2SJan Kara 	vfs_dq_init(dir);
24981da177e4SLinus Torvalds 	error = dir->i_op->link(old_dentry, dir, new_dentry);
24997e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
2500e31e14ecSStephen Smalley 	if (!error)
25017e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
25021da177e4SLinus Torvalds 	return error;
25031da177e4SLinus Torvalds }
25041da177e4SLinus Torvalds 
25051da177e4SLinus Torvalds /*
25061da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
25071da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
25081da177e4SLinus Torvalds  * newname.  --KAB
25091da177e4SLinus Torvalds  *
25101da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
25111da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
25121da177e4SLinus Torvalds  * and other special files.  --ADM
25131da177e4SLinus Torvalds  */
25142e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
25152e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
25161da177e4SLinus Torvalds {
25171da177e4SLinus Torvalds 	struct dentry *new_dentry;
25182d8f3038SAl Viro 	struct nameidata nd;
25192d8f3038SAl Viro 	struct path old_path;
25201da177e4SLinus Torvalds 	int error;
25211da177e4SLinus Torvalds 	char *to;
25221da177e4SLinus Torvalds 
252345c9b11aSUlrich Drepper 	if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
2524c04030e1SUlrich Drepper 		return -EINVAL;
2525c04030e1SUlrich Drepper 
25262d8f3038SAl Viro 	error = user_path_at(olddfd, oldname,
252745c9b11aSUlrich Drepper 			     flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
25282d8f3038SAl Viro 			     &old_path);
25291da177e4SLinus Torvalds 	if (error)
25302ad94ae6SAl Viro 		return error;
25312ad94ae6SAl Viro 
25322ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
25331da177e4SLinus Torvalds 	if (error)
25341da177e4SLinus Torvalds 		goto out;
25351da177e4SLinus Torvalds 	error = -EXDEV;
25362d8f3038SAl Viro 	if (old_path.mnt != nd.path.mnt)
25371da177e4SLinus Torvalds 		goto out_release;
25381da177e4SLinus Torvalds 	new_dentry = lookup_create(&nd, 0);
25391da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
25406902d925SDave Hansen 	if (IS_ERR(new_dentry))
25416902d925SDave Hansen 		goto out_unlock;
254275c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
254375c3f29dSDave Hansen 	if (error)
254475c3f29dSDave Hansen 		goto out_dput;
2545be6d3e56SKentaro Takeda 	error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2546be6d3e56SKentaro Takeda 	if (error)
2547be6d3e56SKentaro Takeda 		goto out_drop_write;
25482d8f3038SAl Viro 	error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
2549be6d3e56SKentaro Takeda out_drop_write:
255075c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
255175c3f29dSDave Hansen out_dput:
25521da177e4SLinus Torvalds 	dput(new_dentry);
25536902d925SDave Hansen out_unlock:
25544ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
25551da177e4SLinus Torvalds out_release:
25561d957f9bSJan Blunck 	path_put(&nd.path);
25572ad94ae6SAl Viro 	putname(to);
25581da177e4SLinus Torvalds out:
25592d8f3038SAl Viro 	path_put(&old_path);
25601da177e4SLinus Torvalds 
25611da177e4SLinus Torvalds 	return error;
25621da177e4SLinus Torvalds }
25631da177e4SLinus Torvalds 
25643480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
25655590ff0dSUlrich Drepper {
2566c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
25675590ff0dSUlrich Drepper }
25685590ff0dSUlrich Drepper 
25691da177e4SLinus Torvalds /*
25701da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
25711da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
25721da177e4SLinus Torvalds  * Problems:
25731da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
25741da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
25751da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
2576a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
25771da177e4SLinus Torvalds  *	   story.
25781da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
25791b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
25801da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
25811da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
2582a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
25831da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
25841da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
25851da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
2586a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
25871da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
25881da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
25891da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
25901da177e4SLinus Torvalds  *	d) some filesystems don't support opened-but-unlinked directories,
25911da177e4SLinus Torvalds  *	   either because of layout or because they are not ready to deal with
25921da177e4SLinus Torvalds  *	   all cases correctly. The latter will be fixed (taking this sort of
25931da177e4SLinus Torvalds  *	   stuff into VFS), but the former is not going away. Solution: the same
25941da177e4SLinus Torvalds  *	   trick as in rmdir().
25951da177e4SLinus Torvalds  *	e) conversion from fhandle to dentry may come in the wrong moment - when
25961b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
25971da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
25981b1dcc1bSJes Sorensen  *	   ->i_mutex on parents, which works but leads to some truely excessive
25991da177e4SLinus Torvalds  *	   locking].
26001da177e4SLinus Torvalds  */
260175c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
26021da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
26031da177e4SLinus Torvalds {
26041da177e4SLinus Torvalds 	int error = 0;
26051da177e4SLinus Torvalds 	struct inode *target;
26061da177e4SLinus Torvalds 
26071da177e4SLinus Torvalds 	/*
26081da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
26091da177e4SLinus Torvalds 	 * we'll need to flip '..'.
26101da177e4SLinus Torvalds 	 */
26111da177e4SLinus Torvalds 	if (new_dir != old_dir) {
2612f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
26131da177e4SLinus Torvalds 		if (error)
26141da177e4SLinus Torvalds 			return error;
26151da177e4SLinus Torvalds 	}
26161da177e4SLinus Torvalds 
26171da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
26181da177e4SLinus Torvalds 	if (error)
26191da177e4SLinus Torvalds 		return error;
26201da177e4SLinus Torvalds 
26211da177e4SLinus Torvalds 	target = new_dentry->d_inode;
26221da177e4SLinus Torvalds 	if (target) {
26231b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
26241da177e4SLinus Torvalds 		dentry_unhash(new_dentry);
26251da177e4SLinus Torvalds 	}
26261da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
26271da177e4SLinus Torvalds 		error = -EBUSY;
26281da177e4SLinus Torvalds 	else
26291da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
26301da177e4SLinus Torvalds 	if (target) {
26311da177e4SLinus Torvalds 		if (!error)
26321da177e4SLinus Torvalds 			target->i_flags |= S_DEAD;
26331b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
26341da177e4SLinus Torvalds 		if (d_unhashed(new_dentry))
26351da177e4SLinus Torvalds 			d_rehash(new_dentry);
26361da177e4SLinus Torvalds 		dput(new_dentry);
26371da177e4SLinus Torvalds 	}
2638e31e14ecSStephen Smalley 	if (!error)
2639349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
26401da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
26411da177e4SLinus Torvalds 	return error;
26421da177e4SLinus Torvalds }
26431da177e4SLinus Torvalds 
264475c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
26451da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
26461da177e4SLinus Torvalds {
26471da177e4SLinus Torvalds 	struct inode *target;
26481da177e4SLinus Torvalds 	int error;
26491da177e4SLinus Torvalds 
26501da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
26511da177e4SLinus Torvalds 	if (error)
26521da177e4SLinus Torvalds 		return error;
26531da177e4SLinus Torvalds 
26541da177e4SLinus Torvalds 	dget(new_dentry);
26551da177e4SLinus Torvalds 	target = new_dentry->d_inode;
26561da177e4SLinus Torvalds 	if (target)
26571b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
26581da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
26591da177e4SLinus Torvalds 		error = -EBUSY;
26601da177e4SLinus Torvalds 	else
26611da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
26621da177e4SLinus Torvalds 	if (!error) {
2663bec1052eSAl Viro 		if (target)
2664bec1052eSAl Viro 			target->i_flags |= S_DEAD;
2665349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
26661da177e4SLinus Torvalds 			d_move(old_dentry, new_dentry);
26671da177e4SLinus Torvalds 	}
26681da177e4SLinus Torvalds 	if (target)
26691b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
26701da177e4SLinus Torvalds 	dput(new_dentry);
26711da177e4SLinus Torvalds 	return error;
26721da177e4SLinus Torvalds }
26731da177e4SLinus Torvalds 
26741da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
26751da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
26761da177e4SLinus Torvalds {
26771da177e4SLinus Torvalds 	int error;
26781da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
26790eeca283SRobert Love 	const char *old_name;
26801da177e4SLinus Torvalds 
26811da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
26821da177e4SLinus Torvalds  		return 0;
26831da177e4SLinus Torvalds 
26841da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
26851da177e4SLinus Torvalds 	if (error)
26861da177e4SLinus Torvalds 		return error;
26871da177e4SLinus Torvalds 
26881da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
2689a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
26901da177e4SLinus Torvalds 	else
26911da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
26921da177e4SLinus Torvalds 	if (error)
26931da177e4SLinus Torvalds 		return error;
26941da177e4SLinus Torvalds 
2695acfa4380SAl Viro 	if (!old_dir->i_op->rename)
26961da177e4SLinus Torvalds 		return -EPERM;
26971da177e4SLinus Torvalds 
26989e3509e2SJan Kara 	vfs_dq_init(old_dir);
26999e3509e2SJan Kara 	vfs_dq_init(new_dir);
27001da177e4SLinus Torvalds 
27010eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
27020eeca283SRobert Love 
27031da177e4SLinus Torvalds 	if (is_dir)
27041da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
27051da177e4SLinus Torvalds 	else
27061da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2707123df294SAl Viro 	if (!error)
2708123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
27095a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
27100eeca283SRobert Love 	fsnotify_oldname_free(old_name);
27110eeca283SRobert Love 
27121da177e4SLinus Torvalds 	return error;
27131da177e4SLinus Torvalds }
27141da177e4SLinus Torvalds 
27152e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
27162e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
27171da177e4SLinus Torvalds {
27181da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
27191da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
27201da177e4SLinus Torvalds 	struct dentry *trap;
27211da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
27222ad94ae6SAl Viro 	char *from;
27232ad94ae6SAl Viro 	char *to;
27242ad94ae6SAl Viro 	int error;
27251da177e4SLinus Torvalds 
27262ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
27271da177e4SLinus Torvalds 	if (error)
27281da177e4SLinus Torvalds 		goto exit;
27291da177e4SLinus Torvalds 
27302ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
27311da177e4SLinus Torvalds 	if (error)
27321da177e4SLinus Torvalds 		goto exit1;
27331da177e4SLinus Torvalds 
27341da177e4SLinus Torvalds 	error = -EXDEV;
27354ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
27361da177e4SLinus Torvalds 		goto exit2;
27371da177e4SLinus Torvalds 
27384ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
27391da177e4SLinus Torvalds 	error = -EBUSY;
27401da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
27411da177e4SLinus Torvalds 		goto exit2;
27421da177e4SLinus Torvalds 
27434ac91378SJan Blunck 	new_dir = newnd.path.dentry;
27441da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
27451da177e4SLinus Torvalds 		goto exit2;
27461da177e4SLinus Torvalds 
27470612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
27480612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
27494e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
27500612d9fbSOGAWA Hirofumi 
27511da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
27521da177e4SLinus Torvalds 
275349705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
27541da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
27551da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
27561da177e4SLinus Torvalds 		goto exit3;
27571da177e4SLinus Torvalds 	/* source must exist */
27581da177e4SLinus Torvalds 	error = -ENOENT;
27591da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
27601da177e4SLinus Torvalds 		goto exit4;
27611da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
27621da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
27631da177e4SLinus Torvalds 		error = -ENOTDIR;
27641da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
27651da177e4SLinus Torvalds 			goto exit4;
27661da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
27671da177e4SLinus Torvalds 			goto exit4;
27681da177e4SLinus Torvalds 	}
27691da177e4SLinus Torvalds 	/* source should not be ancestor of target */
27701da177e4SLinus Torvalds 	error = -EINVAL;
27711da177e4SLinus Torvalds 	if (old_dentry == trap)
27721da177e4SLinus Torvalds 		goto exit4;
277349705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
27741da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
27751da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
27761da177e4SLinus Torvalds 		goto exit4;
27771da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
27781da177e4SLinus Torvalds 	error = -ENOTEMPTY;
27791da177e4SLinus Torvalds 	if (new_dentry == trap)
27801da177e4SLinus Torvalds 		goto exit5;
27811da177e4SLinus Torvalds 
27829079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
27839079b1ebSDave Hansen 	if (error)
27849079b1ebSDave Hansen 		goto exit5;
2785be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
2786be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
2787be6d3e56SKentaro Takeda 	if (error)
2788be6d3e56SKentaro Takeda 		goto exit6;
27891da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
27901da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
2791be6d3e56SKentaro Takeda exit6:
27929079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
27931da177e4SLinus Torvalds exit5:
27941da177e4SLinus Torvalds 	dput(new_dentry);
27951da177e4SLinus Torvalds exit4:
27961da177e4SLinus Torvalds 	dput(old_dentry);
27971da177e4SLinus Torvalds exit3:
27981da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
27991da177e4SLinus Torvalds exit2:
28001d957f9bSJan Blunck 	path_put(&newnd.path);
28012ad94ae6SAl Viro 	putname(to);
28021da177e4SLinus Torvalds exit1:
28031d957f9bSJan Blunck 	path_put(&oldnd.path);
28041da177e4SLinus Torvalds 	putname(from);
28052ad94ae6SAl Viro exit:
28061da177e4SLinus Torvalds 	return error;
28071da177e4SLinus Torvalds }
28081da177e4SLinus Torvalds 
2809a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
28105590ff0dSUlrich Drepper {
28115590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
28125590ff0dSUlrich Drepper }
28135590ff0dSUlrich Drepper 
28141da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
28151da177e4SLinus Torvalds {
28161da177e4SLinus Torvalds 	int len;
28171da177e4SLinus Torvalds 
28181da177e4SLinus Torvalds 	len = PTR_ERR(link);
28191da177e4SLinus Torvalds 	if (IS_ERR(link))
28201da177e4SLinus Torvalds 		goto out;
28211da177e4SLinus Torvalds 
28221da177e4SLinus Torvalds 	len = strlen(link);
28231da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
28241da177e4SLinus Torvalds 		len = buflen;
28251da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
28261da177e4SLinus Torvalds 		len = -EFAULT;
28271da177e4SLinus Torvalds out:
28281da177e4SLinus Torvalds 	return len;
28291da177e4SLinus Torvalds }
28301da177e4SLinus Torvalds 
28311da177e4SLinus Torvalds /*
28321da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
28331da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
28341da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
28351da177e4SLinus Torvalds  */
28361da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
28371da177e4SLinus Torvalds {
28381da177e4SLinus Torvalds 	struct nameidata nd;
2839cc314eefSLinus Torvalds 	void *cookie;
2840694a1764SMarcin Slusarz 	int res;
2841cc314eefSLinus Torvalds 
28421da177e4SLinus Torvalds 	nd.depth = 0;
2843cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
2844694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
2845694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
2846694a1764SMarcin Slusarz 
2847694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
28481da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
2849cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2850694a1764SMarcin Slusarz 	return res;
28511da177e4SLinus Torvalds }
28521da177e4SLinus Torvalds 
28531da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
28541da177e4SLinus Torvalds {
28551da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
28561da177e4SLinus Torvalds }
28571da177e4SLinus Torvalds 
28581da177e4SLinus Torvalds /* get the link contents into pagecache */
28591da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
28601da177e4SLinus Torvalds {
2861ebd09abbSDuane Griffin 	char *kaddr;
28621da177e4SLinus Torvalds 	struct page *page;
28631da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
2864090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
28651da177e4SLinus Torvalds 	if (IS_ERR(page))
28666fe6900eSNick Piggin 		return (char*)page;
28671da177e4SLinus Torvalds 	*ppage = page;
2868ebd09abbSDuane Griffin 	kaddr = kmap(page);
2869ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
2870ebd09abbSDuane Griffin 	return kaddr;
28711da177e4SLinus Torvalds }
28721da177e4SLinus Torvalds 
28731da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
28741da177e4SLinus Torvalds {
28751da177e4SLinus Torvalds 	struct page *page = NULL;
28761da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
28771da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
28781da177e4SLinus Torvalds 	if (page) {
28791da177e4SLinus Torvalds 		kunmap(page);
28801da177e4SLinus Torvalds 		page_cache_release(page);
28811da177e4SLinus Torvalds 	}
28821da177e4SLinus Torvalds 	return res;
28831da177e4SLinus Torvalds }
28841da177e4SLinus Torvalds 
2885cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
28861da177e4SLinus Torvalds {
2887cc314eefSLinus Torvalds 	struct page *page = NULL;
28881da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
2889cc314eefSLinus Torvalds 	return page;
28901da177e4SLinus Torvalds }
28911da177e4SLinus Torvalds 
2892cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
28931da177e4SLinus Torvalds {
2894cc314eefSLinus Torvalds 	struct page *page = cookie;
2895cc314eefSLinus Torvalds 
2896cc314eefSLinus Torvalds 	if (page) {
28971da177e4SLinus Torvalds 		kunmap(page);
28981da177e4SLinus Torvalds 		page_cache_release(page);
28991da177e4SLinus Torvalds 	}
29001da177e4SLinus Torvalds }
29011da177e4SLinus Torvalds 
290254566b2cSNick Piggin /*
290354566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
290454566b2cSNick Piggin  */
290554566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
29061da177e4SLinus Torvalds {
29071da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
29080adb25d2SKirill Korotaev 	struct page *page;
2909afddba49SNick Piggin 	void *fsdata;
2910beb497abSDmitriy Monakhov 	int err;
29111da177e4SLinus Torvalds 	char *kaddr;
291254566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
291354566b2cSNick Piggin 	if (nofs)
291454566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
29151da177e4SLinus Torvalds 
29167e53cac4SNeilBrown retry:
2917afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
291854566b2cSNick Piggin 				flags, &page, &fsdata);
29191da177e4SLinus Torvalds 	if (err)
2920afddba49SNick Piggin 		goto fail;
2921afddba49SNick Piggin 
29221da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
29231da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
29241da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
2925afddba49SNick Piggin 
2926afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
2927afddba49SNick Piggin 							page, fsdata);
29281da177e4SLinus Torvalds 	if (err < 0)
29291da177e4SLinus Torvalds 		goto fail;
2930afddba49SNick Piggin 	if (err < len-1)
2931afddba49SNick Piggin 		goto retry;
2932afddba49SNick Piggin 
29331da177e4SLinus Torvalds 	mark_inode_dirty(inode);
29341da177e4SLinus Torvalds 	return 0;
29351da177e4SLinus Torvalds fail:
29361da177e4SLinus Torvalds 	return err;
29371da177e4SLinus Torvalds }
29381da177e4SLinus Torvalds 
29390adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
29400adb25d2SKirill Korotaev {
29410adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
294254566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
29430adb25d2SKirill Korotaev }
29440adb25d2SKirill Korotaev 
294592e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
29461da177e4SLinus Torvalds 	.readlink	= generic_readlink,
29471da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
29481da177e4SLinus Torvalds 	.put_link	= page_put_link,
29491da177e4SLinus Torvalds };
29501da177e4SLinus Torvalds 
29512d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
29521da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
29531da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
29541da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
29551da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
29561da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
29571da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
29581da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
29591da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
29601da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
29610adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
29621da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
29631da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
29641da177e4SLinus Torvalds EXPORT_SYMBOL(path_lookup);
2965d1811465SAl Viro EXPORT_SYMBOL(kern_path);
296616f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
2967f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
29688c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission);
29691da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
29701da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
29711da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
29721da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
29731da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
29741da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
29751da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
29761da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
29771da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
29781da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
29791da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
29801da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
29811da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
29821da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
2983