xref: /openbmc/linux/fs/namei.c (revision 44396f4b)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/namei.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  * Some corrections by tytso.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
121da177e4SLinus Torvalds  * lookup logic.
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/init.h>
181da177e4SLinus Torvalds #include <linux/module.h>
191da177e4SLinus Torvalds #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/fs.h>
211da177e4SLinus Torvalds #include <linux/namei.h>
221da177e4SLinus Torvalds #include <linux/pagemap.h>
230eeca283SRobert Love #include <linux/fsnotify.h>
241da177e4SLinus Torvalds #include <linux/personality.h>
251da177e4SLinus Torvalds #include <linux/security.h>
266146f0d5SMimi Zohar #include <linux/ima.h>
271da177e4SLinus Torvalds #include <linux/syscalls.h>
281da177e4SLinus Torvalds #include <linux/mount.h>
291da177e4SLinus Torvalds #include <linux/audit.h>
3016f7e0feSRandy Dunlap #include <linux/capability.h>
31834f2a4aSTrond Myklebust #include <linux/file.h>
325590ff0dSUlrich Drepper #include <linux/fcntl.h>
3308ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h>
345ad4e53bSAl Viro #include <linux/fs_struct.h>
351da177e4SLinus Torvalds #include <asm/uaccess.h>
361da177e4SLinus Torvalds 
37e81e3f4dSEric Paris #include "internal.h"
38e81e3f4dSEric Paris 
391da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer]
401da177e4SLinus Torvalds  * Fundamental changes in the pathname lookup mechanisms (namei)
411da177e4SLinus Torvalds  * were necessary because of omirr.  The reason is that omirr needs
421da177e4SLinus Torvalds  * to know the _real_ pathname, not the user-supplied one, in case
431da177e4SLinus Torvalds  * of symlinks (and also when transname replacements occur).
441da177e4SLinus Torvalds  *
451da177e4SLinus Torvalds  * The new code replaces the old recursive symlink resolution with
461da177e4SLinus Torvalds  * an iterative one (in case of non-nested symlink chains).  It does
471da177e4SLinus Torvalds  * this with calls to <fs>_follow_link().
481da177e4SLinus Torvalds  * As a side effect, dir_namei(), _namei() and follow_link() are now
491da177e4SLinus Torvalds  * replaced with a single function lookup_dentry() that can handle all
501da177e4SLinus Torvalds  * the special cases of the former code.
511da177e4SLinus Torvalds  *
521da177e4SLinus Torvalds  * With the new dcache, the pathname is stored at each inode, at least as
531da177e4SLinus Torvalds  * long as the refcount of the inode is positive.  As a side effect, the
541da177e4SLinus Torvalds  * size of the dcache depends on the inode cache and thus is dynamic.
551da177e4SLinus Torvalds  *
561da177e4SLinus Torvalds  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
571da177e4SLinus Torvalds  * resolution to correspond with current state of the code.
581da177e4SLinus Torvalds  *
591da177e4SLinus Torvalds  * Note that the symlink resolution is not *completely* iterative.
601da177e4SLinus Torvalds  * There is still a significant amount of tail- and mid- recursion in
611da177e4SLinus Torvalds  * the algorithm.  Also, note that <fs>_readlink() is not used in
621da177e4SLinus Torvalds  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
631da177e4SLinus Torvalds  * may return different results than <fs>_follow_link().  Many virtual
641da177e4SLinus Torvalds  * filesystems (including /proc) exhibit this behavior.
651da177e4SLinus Torvalds  */
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
681da177e4SLinus Torvalds  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
691da177e4SLinus Torvalds  * and the name already exists in form of a symlink, try to create the new
701da177e4SLinus Torvalds  * name indicated by the symlink. The old code always complained that the
711da177e4SLinus Torvalds  * name already exists, due to not following the symlink even if its target
721da177e4SLinus Torvalds  * is nonexistent.  The new semantics affects also mknod() and link() when
7325985edcSLucas De Marchi  * the name is a symlink pointing to a non-existent name.
741da177e4SLinus Torvalds  *
751da177e4SLinus Torvalds  * I don't know which semantics is the right one, since I have no access
761da177e4SLinus Torvalds  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
771da177e4SLinus Torvalds  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
781da177e4SLinus Torvalds  * "old" one. Personally, I think the new semantics is much more logical.
791da177e4SLinus Torvalds  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
801da177e4SLinus Torvalds  * file does succeed in both HP-UX and SunOs, but not in Solaris
811da177e4SLinus Torvalds  * and in the old Linux semantics.
821da177e4SLinus Torvalds  */
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
851da177e4SLinus Torvalds  * semantics.  See the comments in "open_namei" and "do_link" below.
861da177e4SLinus Torvalds  *
871da177e4SLinus Torvalds  * [10-Sep-98 Alan Modra] Another symlink change.
881da177e4SLinus Torvalds  */
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
911da177e4SLinus Torvalds  *	inside the path - always follow.
921da177e4SLinus Torvalds  *	in the last component in creation/removal/renaming - never follow.
931da177e4SLinus Torvalds  *	if LOOKUP_FOLLOW passed - follow.
941da177e4SLinus Torvalds  *	if the pathname has trailing slashes - follow.
951da177e4SLinus Torvalds  *	otherwise - don't follow.
961da177e4SLinus Torvalds  * (applied in that order).
971da177e4SLinus Torvalds  *
981da177e4SLinus Torvalds  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
991da177e4SLinus Torvalds  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
1001da177e4SLinus Torvalds  * During the 2.4 we need to fix the userland stuff depending on it -
1011da177e4SLinus Torvalds  * hopefully we will be able to get rid of that wart in 2.5. So far only
1021da177e4SLinus Torvalds  * XEmacs seems to be relying on it...
1031da177e4SLinus Torvalds  */
1041da177e4SLinus Torvalds /*
1051da177e4SLinus Torvalds  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
106a11f3a05SArjan van de Ven  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
1071da177e4SLinus Torvalds  * any extra contention...
1081da177e4SLinus Torvalds  */
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional
1111da177e4SLinus Torvalds  * checking and hopefully speeding things up, we copy filenames to the
1121da177e4SLinus Torvalds  * kernel data space before using them..
1131da177e4SLinus Torvalds  *
1141da177e4SLinus Torvalds  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
1151da177e4SLinus Torvalds  * PATH_MAX includes the nul terminator --RR.
1161da177e4SLinus Torvalds  */
117858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page)
1181da177e4SLinus Torvalds {
1191da177e4SLinus Torvalds 	int retval;
1201da177e4SLinus Torvalds 	unsigned long len = PATH_MAX;
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	if (!segment_eq(get_fs(), KERNEL_DS)) {
1231da177e4SLinus Torvalds 		if ((unsigned long) filename >= TASK_SIZE)
1241da177e4SLinus Torvalds 			return -EFAULT;
1251da177e4SLinus Torvalds 		if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
1261da177e4SLinus Torvalds 			len = TASK_SIZE - (unsigned long) filename;
1271da177e4SLinus Torvalds 	}
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	retval = strncpy_from_user(page, filename, len);
1301da177e4SLinus Torvalds 	if (retval > 0) {
1311da177e4SLinus Torvalds 		if (retval < len)
1321da177e4SLinus Torvalds 			return 0;
1331da177e4SLinus Torvalds 		return -ENAMETOOLONG;
1341da177e4SLinus Torvalds 	} else if (!retval)
1351da177e4SLinus Torvalds 		retval = -ENOENT;
1361da177e4SLinus Torvalds 	return retval;
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
139f52e0c11SAl Viro static char *getname_flags(const char __user * filename, int flags)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	char *tmp, *result;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	result = ERR_PTR(-ENOMEM);
1441da177e4SLinus Torvalds 	tmp = __getname();
1451da177e4SLinus Torvalds 	if (tmp)  {
1461da177e4SLinus Torvalds 		int retval = do_getname(filename, tmp);
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 		result = tmp;
1491da177e4SLinus Torvalds 		if (retval < 0) {
150f52e0c11SAl Viro 			if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
1511da177e4SLinus Torvalds 				__putname(tmp);
1521da177e4SLinus Torvalds 				result = ERR_PTR(retval);
1531da177e4SLinus Torvalds 			}
1541da177e4SLinus Torvalds 		}
155f52e0c11SAl Viro 	}
1561da177e4SLinus Torvalds 	audit_getname(result);
1571da177e4SLinus Torvalds 	return result;
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
160f52e0c11SAl Viro char *getname(const char __user * filename)
161f52e0c11SAl Viro {
162f52e0c11SAl Viro 	return getname_flags(filename, 0);
163f52e0c11SAl Viro }
164f52e0c11SAl Viro 
1651da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
1661da177e4SLinus Torvalds void putname(const char *name)
1671da177e4SLinus Torvalds {
1685ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
1691da177e4SLinus Torvalds 		audit_putname(name);
1701da177e4SLinus Torvalds 	else
1711da177e4SLinus Torvalds 		__putname(name);
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds EXPORT_SYMBOL(putname);
1741da177e4SLinus Torvalds #endif
1751da177e4SLinus Torvalds 
1765909ccaaSLinus Torvalds /*
1775909ccaaSLinus Torvalds  * This does basic POSIX ACL permission checking
1785909ccaaSLinus Torvalds  */
179b74c79e9SNick Piggin static int acl_permission_check(struct inode *inode, int mask, unsigned int flags,
180b74c79e9SNick Piggin 		int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
1815909ccaaSLinus Torvalds {
18226cf46beSLinus Torvalds 	unsigned int mode = inode->i_mode;
1835909ccaaSLinus Torvalds 
1845909ccaaSLinus Torvalds 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
1855909ccaaSLinus Torvalds 
186e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
187e795b717SSerge E. Hallyn 		goto other_perms;
188e795b717SSerge E. Hallyn 
1895909ccaaSLinus Torvalds 	if (current_fsuid() == inode->i_uid)
1905909ccaaSLinus Torvalds 		mode >>= 6;
1915909ccaaSLinus Torvalds 	else {
1925909ccaaSLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
193b74c79e9SNick Piggin 			int error = check_acl(inode, mask, flags);
1945909ccaaSLinus Torvalds 			if (error != -EAGAIN)
1955909ccaaSLinus Torvalds 				return error;
1965909ccaaSLinus Torvalds 		}
1975909ccaaSLinus Torvalds 
1985909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
1995909ccaaSLinus Torvalds 			mode >>= 3;
2005909ccaaSLinus Torvalds 	}
2015909ccaaSLinus Torvalds 
202e795b717SSerge E. Hallyn other_perms:
2035909ccaaSLinus Torvalds 	/*
2045909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
2055909ccaaSLinus Torvalds 	 */
2065909ccaaSLinus Torvalds 	if ((mask & ~mode) == 0)
2075909ccaaSLinus Torvalds 		return 0;
2085909ccaaSLinus Torvalds 	return -EACCES;
2095909ccaaSLinus Torvalds }
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds /**
2121da177e4SLinus Torvalds  * generic_permission -  check for access rights on a Posix-like filesystem
2131da177e4SLinus Torvalds  * @inode:	inode to check access rights for
2141da177e4SLinus Torvalds  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
2151da177e4SLinus Torvalds  * @check_acl:	optional callback to check for Posix ACLs
21639191628SRandy Dunlap  * @flags:	IPERM_FLAG_ flags.
2171da177e4SLinus Torvalds  *
2181da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
2191da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
2201da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
221b74c79e9SNick Piggin  * are used for other things.
222b74c79e9SNick Piggin  *
223b74c79e9SNick Piggin  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
224b74c79e9SNick Piggin  * request cannot be satisfied (eg. requires blocking or too much complexity).
225b74c79e9SNick Piggin  * It would then be called again in ref-walk mode.
2261da177e4SLinus Torvalds  */
227b74c79e9SNick Piggin int generic_permission(struct inode *inode, int mask, unsigned int flags,
228b74c79e9SNick Piggin 	int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
2291da177e4SLinus Torvalds {
2305909ccaaSLinus Torvalds 	int ret;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	/*
2335909ccaaSLinus Torvalds 	 * Do the basic POSIX ACL permission checks.
2341da177e4SLinus Torvalds 	 */
235b74c79e9SNick Piggin 	ret = acl_permission_check(inode, mask, flags, check_acl);
2365909ccaaSLinus Torvalds 	if (ret != -EACCES)
2375909ccaaSLinus Torvalds 		return ret;
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	/*
2401da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
2418e833fd2SAl Viro 	 * Executable DACs are overridable for all directories and
2428e833fd2SAl Viro 	 * for non-directories that have least one exec bit set.
2431da177e4SLinus Torvalds 	 */
244f696a365SMiklos Szeredi 	if (!(mask & MAY_EXEC) || execute_ok(inode))
245e795b717SSerge E. Hallyn 		if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
2461da177e4SLinus Torvalds 			return 0;
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	/*
2491da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
2501da177e4SLinus Torvalds 	 */
2517ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
2521da177e4SLinus Torvalds 	if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
253e795b717SSerge E. Hallyn 		if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
2541da177e4SLinus Torvalds 			return 0;
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	return -EACCES;
2571da177e4SLinus Torvalds }
2581da177e4SLinus Torvalds 
259cb23beb5SChristoph Hellwig /**
260cb23beb5SChristoph Hellwig  * inode_permission  -  check for access rights to a given inode
261cb23beb5SChristoph Hellwig  * @inode:	inode to check permission on
262cb23beb5SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
263cb23beb5SChristoph Hellwig  *
264cb23beb5SChristoph Hellwig  * Used to check for read/write/execute permissions on an inode.
265cb23beb5SChristoph Hellwig  * We use "fsuid" for this, letting us set arbitrary permissions
266cb23beb5SChristoph Hellwig  * for filesystem access without changing the "normal" uids which
267cb23beb5SChristoph Hellwig  * are used for other things.
268cb23beb5SChristoph Hellwig  */
269f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask)
2701da177e4SLinus Torvalds {
271e6305c43SAl Viro 	int retval;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	if (mask & MAY_WRITE) {
27422590e41SMiklos Szeredi 		umode_t mode = inode->i_mode;
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 		/*
2771da177e4SLinus Torvalds 		 * Nobody gets write access to a read-only fs.
2781da177e4SLinus Torvalds 		 */
2791da177e4SLinus Torvalds 		if (IS_RDONLY(inode) &&
2801da177e4SLinus Torvalds 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
2811da177e4SLinus Torvalds 			return -EROFS;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 		/*
2841da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
2851da177e4SLinus Torvalds 		 */
2861da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
2871da177e4SLinus Torvalds 			return -EACCES;
2881da177e4SLinus Torvalds 	}
2891da177e4SLinus Torvalds 
290acfa4380SAl Viro 	if (inode->i_op->permission)
291b74c79e9SNick Piggin 		retval = inode->i_op->permission(inode, mask, 0);
292f696a365SMiklos Szeredi 	else
293b74c79e9SNick Piggin 		retval = generic_permission(inode, mask, 0,
294b74c79e9SNick Piggin 				inode->i_op->check_acl);
295f696a365SMiklos Szeredi 
2961da177e4SLinus Torvalds 	if (retval)
2971da177e4SLinus Torvalds 		return retval;
2981da177e4SLinus Torvalds 
29908ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
30008ce5f16SSerge E. Hallyn 	if (retval)
30108ce5f16SSerge E. Hallyn 		return retval;
30208ce5f16SSerge E. Hallyn 
303d09ca739SEric Paris 	return security_inode_permission(inode, mask);
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds 
306e4543eddSChristoph Hellwig /**
3078c744fb8SChristoph Hellwig  * file_permission  -  check for additional access rights to a given file
3088c744fb8SChristoph Hellwig  * @file:	file to check access rights for
3098c744fb8SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
3108c744fb8SChristoph Hellwig  *
3118c744fb8SChristoph Hellwig  * Used to check for read/write/execute permissions on an already opened
3128c744fb8SChristoph Hellwig  * file.
3138c744fb8SChristoph Hellwig  *
3148c744fb8SChristoph Hellwig  * Note:
3158c744fb8SChristoph Hellwig  *	Do not use this function in new code.  All access checks should
316cb23beb5SChristoph Hellwig  *	be done using inode_permission().
3178c744fb8SChristoph Hellwig  */
3188c744fb8SChristoph Hellwig int file_permission(struct file *file, int mask)
3198c744fb8SChristoph Hellwig {
320f419a2e3SAl Viro 	return inode_permission(file->f_path.dentry->d_inode, mask);
3218c744fb8SChristoph Hellwig }
3228c744fb8SChristoph Hellwig 
3231da177e4SLinus Torvalds /*
3241da177e4SLinus Torvalds  * get_write_access() gets write permission for a file.
3251da177e4SLinus Torvalds  * put_write_access() releases this write permission.
3261da177e4SLinus Torvalds  * This is used for regular files.
3271da177e4SLinus Torvalds  * We cannot support write (and maybe mmap read-write shared) accesses and
3281da177e4SLinus Torvalds  * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
3291da177e4SLinus Torvalds  * can have the following values:
3301da177e4SLinus Torvalds  * 0: no writers, no VM_DENYWRITE mappings
3311da177e4SLinus Torvalds  * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
3321da177e4SLinus Torvalds  * > 0: (i_writecount) users are writing to the file.
3331da177e4SLinus Torvalds  *
3341da177e4SLinus Torvalds  * Normally we operate on that counter with atomic_{inc,dec} and it's safe
3351da177e4SLinus Torvalds  * except for the cases where we don't hold i_writecount yet. Then we need to
3361da177e4SLinus Torvalds  * use {get,deny}_write_access() - these functions check the sign and refuse
3371da177e4SLinus Torvalds  * to do the change if sign is wrong. Exclusion between them is provided by
3381da177e4SLinus Torvalds  * the inode->i_lock spinlock.
3391da177e4SLinus Torvalds  */
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds int get_write_access(struct inode * inode)
3421da177e4SLinus Torvalds {
3431da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
3441da177e4SLinus Torvalds 	if (atomic_read(&inode->i_writecount) < 0) {
3451da177e4SLinus Torvalds 		spin_unlock(&inode->i_lock);
3461da177e4SLinus Torvalds 		return -ETXTBSY;
3471da177e4SLinus Torvalds 	}
3481da177e4SLinus Torvalds 	atomic_inc(&inode->i_writecount);
3491da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	return 0;
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds int deny_write_access(struct file * file)
3551da177e4SLinus Torvalds {
3560f7fc9e4SJosef "Jeff" Sipek 	struct inode *inode = file->f_path.dentry->d_inode;
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
3591da177e4SLinus Torvalds 	if (atomic_read(&inode->i_writecount) > 0) {
3601da177e4SLinus Torvalds 		spin_unlock(&inode->i_lock);
3611da177e4SLinus Torvalds 		return -ETXTBSY;
3621da177e4SLinus Torvalds 	}
3631da177e4SLinus Torvalds 	atomic_dec(&inode->i_writecount);
3641da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	return 0;
3671da177e4SLinus Torvalds }
3681da177e4SLinus Torvalds 
3691d957f9bSJan Blunck /**
3705dd784d0SJan Blunck  * path_get - get a reference to a path
3715dd784d0SJan Blunck  * @path: path to get the reference to
3725dd784d0SJan Blunck  *
3735dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
3745dd784d0SJan Blunck  */
3755dd784d0SJan Blunck void path_get(struct path *path)
3765dd784d0SJan Blunck {
3775dd784d0SJan Blunck 	mntget(path->mnt);
3785dd784d0SJan Blunck 	dget(path->dentry);
3795dd784d0SJan Blunck }
3805dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
3815dd784d0SJan Blunck 
3825dd784d0SJan Blunck /**
3831d957f9bSJan Blunck  * path_put - put a reference to a path
3841d957f9bSJan Blunck  * @path: path to put the reference to
3851d957f9bSJan Blunck  *
3861d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
3871d957f9bSJan Blunck  */
3881d957f9bSJan Blunck void path_put(struct path *path)
3891da177e4SLinus Torvalds {
3901d957f9bSJan Blunck 	dput(path->dentry);
3911d957f9bSJan Blunck 	mntput(path->mnt);
3921da177e4SLinus Torvalds }
3931d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
3941da177e4SLinus Torvalds 
39519660af7SAl Viro /*
39631e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
39719660af7SAl Viro  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
39819660af7SAl Viro  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
39919660af7SAl Viro  * normal reference counts on dentries and vfsmounts to transition to rcu-walk
40019660af7SAl Viro  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
40119660af7SAl Viro  * got stuck, so ref-walk may continue from there. If this is not successful
40219660af7SAl Viro  * (eg. a seqcount has changed), then failure is returned and it's up to caller
40319660af7SAl Viro  * to restart the path walk from the beginning in ref-walk mode.
40431e6b01fSNick Piggin  */
40531e6b01fSNick Piggin 
40631e6b01fSNick Piggin /**
40719660af7SAl Viro  * unlazy_walk - try to switch to ref-walk mode.
40819660af7SAl Viro  * @nd: nameidata pathwalk data
40919660af7SAl Viro  * @dentry: child of nd->path.dentry or NULL
41039191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
41131e6b01fSNick Piggin  *
41219660af7SAl Viro  * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
41319660af7SAl Viro  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
41419660af7SAl Viro  * @nd or NULL.  Must be called from rcu-walk context.
41531e6b01fSNick Piggin  */
41619660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
41731e6b01fSNick Piggin {
41831e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
41931e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
4205b6ca027SAl Viro 	int want_root = 0;
42131e6b01fSNick Piggin 
42231e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
4235b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
4245b6ca027SAl Viro 		want_root = 1;
42531e6b01fSNick Piggin 		spin_lock(&fs->lock);
42631e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
42731e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
42831e6b01fSNick Piggin 			goto err_root;
42931e6b01fSNick Piggin 	}
43031e6b01fSNick Piggin 	spin_lock(&parent->d_lock);
43119660af7SAl Viro 	if (!dentry) {
43219660af7SAl Viro 		if (!__d_rcu_to_refcount(parent, nd->seq))
43319660af7SAl Viro 			goto err_parent;
43419660af7SAl Viro 		BUG_ON(nd->inode != parent->d_inode);
43519660af7SAl Viro 	} else {
43694c0d4ecSAl Viro 		if (dentry->d_parent != parent)
43794c0d4ecSAl Viro 			goto err_parent;
43831e6b01fSNick Piggin 		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
43931e6b01fSNick Piggin 		if (!__d_rcu_to_refcount(dentry, nd->seq))
44019660af7SAl Viro 			goto err_child;
44131e6b01fSNick Piggin 		/*
44219660af7SAl Viro 		 * If the sequence check on the child dentry passed, then
44319660af7SAl Viro 		 * the child has not been removed from its parent. This
44419660af7SAl Viro 		 * means the parent dentry must be valid and able to take
44519660af7SAl Viro 		 * a reference at this point.
44631e6b01fSNick Piggin 		 */
44731e6b01fSNick Piggin 		BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
44831e6b01fSNick Piggin 		BUG_ON(!parent->d_count);
44931e6b01fSNick Piggin 		parent->d_count++;
45031e6b01fSNick Piggin 		spin_unlock(&dentry->d_lock);
45119660af7SAl Viro 	}
45231e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
4535b6ca027SAl Viro 	if (want_root) {
45431e6b01fSNick Piggin 		path_get(&nd->root);
45531e6b01fSNick Piggin 		spin_unlock(&fs->lock);
45631e6b01fSNick Piggin 	}
45731e6b01fSNick Piggin 	mntget(nd->path.mnt);
45831e6b01fSNick Piggin 
45931e6b01fSNick Piggin 	rcu_read_unlock();
46031e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
46131e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
46231e6b01fSNick Piggin 	return 0;
46319660af7SAl Viro 
46419660af7SAl Viro err_child:
46531e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
46619660af7SAl Viro err_parent:
46731e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
46831e6b01fSNick Piggin err_root:
4695b6ca027SAl Viro 	if (want_root)
47031e6b01fSNick Piggin 		spin_unlock(&fs->lock);
47131e6b01fSNick Piggin 	return -ECHILD;
47231e6b01fSNick Piggin }
47331e6b01fSNick Piggin 
47431e6b01fSNick Piggin /**
475834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
476834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
477834f2a4aSTrond Myklebust  */
478834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
479834f2a4aSTrond Myklebust {
4802dab5974SLinus Torvalds 	struct file *file = nd->intent.open.file;
4812dab5974SLinus Torvalds 
4822dab5974SLinus Torvalds 	if (file && !IS_ERR(file)) {
4832dab5974SLinus Torvalds 		if (file->f_path.dentry == NULL)
4842dab5974SLinus Torvalds 			put_filp(file);
485834f2a4aSTrond Myklebust 		else
4862dab5974SLinus Torvalds 			fput(file);
4872dab5974SLinus Torvalds 	}
488834f2a4aSTrond Myklebust }
489834f2a4aSTrond Myklebust 
490f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
49134286d66SNick Piggin {
492f60aef7eSAl Viro 	return dentry->d_op->d_revalidate(dentry, nd);
49334286d66SNick Piggin }
49434286d66SNick Piggin 
495f5e1c1c1SAl Viro static struct dentry *
496bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd)
497bcdc5e01SIan Kent {
498f5e1c1c1SAl Viro 	int status = d_revalidate(dentry, nd);
499bcdc5e01SIan Kent 	if (unlikely(status <= 0)) {
500bcdc5e01SIan Kent 		/*
501bcdc5e01SIan Kent 		 * The dentry failed validation.
502bcdc5e01SIan Kent 		 * If d_revalidate returned 0 attempt to invalidate
503bcdc5e01SIan Kent 		 * the dentry otherwise d_revalidate is asking us
504bcdc5e01SIan Kent 		 * to return a fail status.
505bcdc5e01SIan Kent 		 */
50634286d66SNick Piggin 		if (status < 0) {
50734286d66SNick Piggin 			dput(dentry);
50834286d66SNick Piggin 			dentry = ERR_PTR(status);
509f5e1c1c1SAl Viro 		} else if (!d_invalidate(dentry)) {
510bcdc5e01SIan Kent 			dput(dentry);
511bcdc5e01SIan Kent 			dentry = NULL;
512bcdc5e01SIan Kent 		}
513bcdc5e01SIan Kent 	}
514f5e1c1c1SAl Viro 	return dentry;
515f5e1c1c1SAl Viro }
516f5e1c1c1SAl Viro 
5179f1fafeeSAl Viro /**
5189f1fafeeSAl Viro  * complete_walk - successful completion of path walk
5199f1fafeeSAl Viro  * @nd:  pointer nameidata
52039159de2SJeff Layton  *
5219f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
5229f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
5239f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
5249f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
5259f1fafeeSAl Viro  * need to drop nd->path.
52639159de2SJeff Layton  */
5279f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
52839159de2SJeff Layton {
52916c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
53039159de2SJeff Layton 	int status;
53139159de2SJeff Layton 
5329f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
5339f1fafeeSAl Viro 		nd->flags &= ~LOOKUP_RCU;
5349f1fafeeSAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
5359f1fafeeSAl Viro 			nd->root.mnt = NULL;
5369f1fafeeSAl Viro 		spin_lock(&dentry->d_lock);
5379f1fafeeSAl Viro 		if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
5389f1fafeeSAl Viro 			spin_unlock(&dentry->d_lock);
5399f1fafeeSAl Viro 			rcu_read_unlock();
5409f1fafeeSAl Viro 			br_read_unlock(vfsmount_lock);
5419f1fafeeSAl Viro 			return -ECHILD;
5429f1fafeeSAl Viro 		}
5439f1fafeeSAl Viro 		BUG_ON(nd->inode != dentry->d_inode);
5449f1fafeeSAl Viro 		spin_unlock(&dentry->d_lock);
5459f1fafeeSAl Viro 		mntget(nd->path.mnt);
5469f1fafeeSAl Viro 		rcu_read_unlock();
5479f1fafeeSAl Viro 		br_read_unlock(vfsmount_lock);
5489f1fafeeSAl Viro 	}
5499f1fafeeSAl Viro 
55016c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
55139159de2SJeff Layton 		return 0;
55239159de2SJeff Layton 
55316c2cd71SAl Viro 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
55416c2cd71SAl Viro 		return 0;
55516c2cd71SAl Viro 
55616c2cd71SAl Viro 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
55716c2cd71SAl Viro 		return 0;
55816c2cd71SAl Viro 
55916c2cd71SAl Viro 	/* Note: we do not d_invalidate() */
56034286d66SNick Piggin 	status = d_revalidate(dentry, nd);
56139159de2SJeff Layton 	if (status > 0)
56239159de2SJeff Layton 		return 0;
56339159de2SJeff Layton 
56416c2cd71SAl Viro 	if (!status)
56539159de2SJeff Layton 		status = -ESTALE;
56616c2cd71SAl Viro 
5679f1fafeeSAl Viro 	path_put(&nd->path);
56839159de2SJeff Layton 	return status;
56939159de2SJeff Layton }
57039159de2SJeff Layton 
57139159de2SJeff Layton /*
572b75b5086SAl Viro  * Short-cut version of permission(), for calling on directories
573b75b5086SAl Viro  * during pathname resolution.  Combines parts of permission()
574b75b5086SAl Viro  * and generic_permission(), and tests ONLY for MAY_EXEC permission.
5751da177e4SLinus Torvalds  *
5761da177e4SLinus Torvalds  * If appropriate, check DAC only.  If not appropriate, or
577b75b5086SAl Viro  * short-cut DAC fails, then call ->permission() to do more
5781da177e4SLinus Torvalds  * complete permission check.
5791da177e4SLinus Torvalds  */
580b74c79e9SNick Piggin static inline int exec_permission(struct inode *inode, unsigned int flags)
5811da177e4SLinus Torvalds {
5825909ccaaSLinus Torvalds 	int ret;
583e795b717SSerge E. Hallyn 	struct user_namespace *ns = inode_userns(inode);
5841da177e4SLinus Torvalds 
585cb9179eaSLinus Torvalds 	if (inode->i_op->permission) {
586b74c79e9SNick Piggin 		ret = inode->i_op->permission(inode, MAY_EXEC, flags);
587b74c79e9SNick Piggin 	} else {
588b74c79e9SNick Piggin 		ret = acl_permission_check(inode, MAY_EXEC, flags,
589b74c79e9SNick Piggin 				inode->i_op->check_acl);
590cb9179eaSLinus Torvalds 	}
591b74c79e9SNick Piggin 	if (likely(!ret))
5921da177e4SLinus Torvalds 		goto ok;
593b74c79e9SNick Piggin 	if (ret == -ECHILD)
59431e6b01fSNick Piggin 		return ret;
5951da177e4SLinus Torvalds 
596e795b717SSerge E. Hallyn 	if (ns_capable(ns, CAP_DAC_OVERRIDE) ||
597e795b717SSerge E. Hallyn 			ns_capable(ns, CAP_DAC_READ_SEARCH))
5981da177e4SLinus Torvalds 		goto ok;
5991da177e4SLinus Torvalds 
6005909ccaaSLinus Torvalds 	return ret;
6011da177e4SLinus Torvalds ok:
602b74c79e9SNick Piggin 	return security_inode_exec_permission(inode, flags);
6031da177e4SLinus Torvalds }
6041da177e4SLinus Torvalds 
6052a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
6062a737871SAl Viro {
607f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
608f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
6092a737871SAl Viro }
6102a737871SAl Viro 
6116de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
6126de88d72SAl Viro 
61331e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
61431e6b01fSNick Piggin {
61531e6b01fSNick Piggin 	if (!nd->root.mnt) {
61631e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
617c28cc364SNick Piggin 		unsigned seq;
618c28cc364SNick Piggin 
619c28cc364SNick Piggin 		do {
620c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
62131e6b01fSNick Piggin 			nd->root = fs->root;
622c1530019STim Chen 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
623c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
62431e6b01fSNick Piggin 	}
62531e6b01fSNick Piggin }
62631e6b01fSNick Piggin 
627f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
6281da177e4SLinus Torvalds {
62931e6b01fSNick Piggin 	int ret;
63031e6b01fSNick Piggin 
6311da177e4SLinus Torvalds 	if (IS_ERR(link))
6321da177e4SLinus Torvalds 		goto fail;
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	if (*link == '/') {
6352a737871SAl Viro 		set_root(nd);
6361d957f9bSJan Blunck 		path_put(&nd->path);
6372a737871SAl Viro 		nd->path = nd->root;
6382a737871SAl Viro 		path_get(&nd->root);
63916c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
6401da177e4SLinus Torvalds 	}
64131e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
642b4091d5fSChristoph Hellwig 
64331e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
64431e6b01fSNick Piggin 	return ret;
6451da177e4SLinus Torvalds fail:
6461d957f9bSJan Blunck 	path_put(&nd->path);
6471da177e4SLinus Torvalds 	return PTR_ERR(link);
6481da177e4SLinus Torvalds }
6491da177e4SLinus Torvalds 
6501d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
651051d3812SIan Kent {
652051d3812SIan Kent 	dput(path->dentry);
6534ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
654051d3812SIan Kent 		mntput(path->mnt);
655051d3812SIan Kent }
656051d3812SIan Kent 
6577b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
6587b9337aaSNick Piggin 					struct nameidata *nd)
659051d3812SIan Kent {
66031e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
6614ac91378SJan Blunck 		dput(nd->path.dentry);
66231e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
6634ac91378SJan Blunck 			mntput(nd->path.mnt);
6649a229683SHuang Shijie 	}
66531e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
6664ac91378SJan Blunck 	nd->path.dentry = path->dentry;
667051d3812SIan Kent }
668051d3812SIan Kent 
669574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
670574197e0SAl Viro {
671574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
672574197e0SAl Viro 	if (!IS_ERR(cookie) && inode->i_op->put_link)
673574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
674574197e0SAl Viro 	path_put(link);
675574197e0SAl Viro }
676574197e0SAl Viro 
677def4af30SAl Viro static __always_inline int
678574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
6791da177e4SLinus Torvalds {
6801da177e4SLinus Torvalds 	int error;
6817b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
6821da177e4SLinus Torvalds 
683844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
684844a3917SAl Viro 
6850e794589SAl Viro 	if (link->mnt == nd->path.mnt)
6860e794589SAl Viro 		mntget(link->mnt);
6870e794589SAl Viro 
688574197e0SAl Viro 	if (unlikely(current->total_link_count >= 40)) {
689574197e0SAl Viro 		*p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
690574197e0SAl Viro 		path_put(&nd->path);
691574197e0SAl Viro 		return -ELOOP;
692574197e0SAl Viro 	}
693574197e0SAl Viro 	cond_resched();
694574197e0SAl Viro 	current->total_link_count++;
695574197e0SAl Viro 
6967b9337aaSNick Piggin 	touch_atime(link->mnt, dentry);
6971da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
698cd4e91d3SAl Viro 
69936f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
70036f3b4f6SAl Viro 	if (error) {
70136f3b4f6SAl Viro 		*p = ERR_PTR(error); /* no ->put_link(), please */
70236f3b4f6SAl Viro 		path_put(&nd->path);
70336f3b4f6SAl Viro 		return error;
70436f3b4f6SAl Viro 	}
70536f3b4f6SAl Viro 
70686acdca1SAl Viro 	nd->last_type = LAST_BIND;
707def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
708def4af30SAl Viro 	error = PTR_ERR(*p);
709def4af30SAl Viro 	if (!IS_ERR(*p)) {
7101da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
711cc314eefSLinus Torvalds 		error = 0;
7121da177e4SLinus Torvalds 		if (s)
7131da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
714bcda7652SAl Viro 		else if (nd->last_type == LAST_BIND) {
71516c2cd71SAl Viro 			nd->flags |= LOOKUP_JUMPED;
716b21041d0SAl Viro 			nd->inode = nd->path.dentry->d_inode;
717b21041d0SAl Viro 			if (nd->inode->i_op->follow_link) {
718bcda7652SAl Viro 				/* stepped on a _really_ weird one */
719bcda7652SAl Viro 				path_put(&nd->path);
720bcda7652SAl Viro 				error = -ELOOP;
721bcda7652SAl Viro 			}
722bcda7652SAl Viro 		}
7231da177e4SLinus Torvalds 	}
7241da177e4SLinus Torvalds 	return error;
7251da177e4SLinus Torvalds }
7261da177e4SLinus Torvalds 
72731e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
72831e6b01fSNick Piggin {
72931e6b01fSNick Piggin 	struct vfsmount *parent;
73031e6b01fSNick Piggin 	struct dentry *mountpoint;
73131e6b01fSNick Piggin 
73231e6b01fSNick Piggin 	parent = path->mnt->mnt_parent;
73331e6b01fSNick Piggin 	if (parent == path->mnt)
73431e6b01fSNick Piggin 		return 0;
73531e6b01fSNick Piggin 	mountpoint = path->mnt->mnt_mountpoint;
73631e6b01fSNick Piggin 	path->dentry = mountpoint;
73731e6b01fSNick Piggin 	path->mnt = parent;
73831e6b01fSNick Piggin 	return 1;
73931e6b01fSNick Piggin }
74031e6b01fSNick Piggin 
741bab77ebfSAl Viro int follow_up(struct path *path)
7421da177e4SLinus Torvalds {
7431da177e4SLinus Torvalds 	struct vfsmount *parent;
7441da177e4SLinus Torvalds 	struct dentry *mountpoint;
74599b7db7bSNick Piggin 
74699b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
747bab77ebfSAl Viro 	parent = path->mnt->mnt_parent;
748bab77ebfSAl Viro 	if (parent == path->mnt) {
74999b7db7bSNick Piggin 		br_read_unlock(vfsmount_lock);
7501da177e4SLinus Torvalds 		return 0;
7511da177e4SLinus Torvalds 	}
7521da177e4SLinus Torvalds 	mntget(parent);
753bab77ebfSAl Viro 	mountpoint = dget(path->mnt->mnt_mountpoint);
75499b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
755bab77ebfSAl Viro 	dput(path->dentry);
756bab77ebfSAl Viro 	path->dentry = mountpoint;
757bab77ebfSAl Viro 	mntput(path->mnt);
758bab77ebfSAl Viro 	path->mnt = parent;
7591da177e4SLinus Torvalds 	return 1;
7601da177e4SLinus Torvalds }
7611da177e4SLinus Torvalds 
762b5c84bf6SNick Piggin /*
7639875cf80SDavid Howells  * Perform an automount
7649875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
7659875cf80SDavid Howells  *   were called with.
7661da177e4SLinus Torvalds  */
7679875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
7689875cf80SDavid Howells 			    bool *need_mntput)
76931e6b01fSNick Piggin {
7709875cf80SDavid Howells 	struct vfsmount *mnt;
771ea5b778aSDavid Howells 	int err;
7729875cf80SDavid Howells 
7739875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
7749875cf80SDavid Howells 		return -EREMOTE;
7759875cf80SDavid Howells 
7766f45b656SDavid Howells 	/* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
7776f45b656SDavid Howells 	 * and this is the terminal part of the path.
7786f45b656SDavid Howells 	 */
7796f45b656SDavid Howells 	if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
7806f45b656SDavid Howells 		return -EISDIR; /* we actually want to stop here */
7816f45b656SDavid Howells 
7829875cf80SDavid Howells 	/* We want to mount if someone is trying to open/create a file of any
7839875cf80SDavid Howells 	 * type under the mountpoint, wants to traverse through the mountpoint
7849875cf80SDavid Howells 	 * or wants to open the mounted directory.
7859875cf80SDavid Howells 	 *
7869875cf80SDavid Howells 	 * We don't want to mount if someone's just doing a stat and they've
7879875cf80SDavid Howells 	 * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
7889875cf80SDavid Howells 	 * appended a '/' to the name.
7899875cf80SDavid Howells 	 */
7909875cf80SDavid Howells 	if (!(flags & LOOKUP_FOLLOW) &&
7919875cf80SDavid Howells 	    !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
7929875cf80SDavid Howells 		       LOOKUP_OPEN | LOOKUP_CREATE)))
7939875cf80SDavid Howells 		return -EISDIR;
7949875cf80SDavid Howells 
7959875cf80SDavid Howells 	current->total_link_count++;
7969875cf80SDavid Howells 	if (current->total_link_count >= 40)
7979875cf80SDavid Howells 		return -ELOOP;
7989875cf80SDavid Howells 
7999875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
8009875cf80SDavid Howells 	if (IS_ERR(mnt)) {
8019875cf80SDavid Howells 		/*
8029875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
8039875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
8049875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
8059875cf80SDavid Howells 		 *
8069875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
8079875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
8089875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
8099875cf80SDavid Howells 		 */
8109875cf80SDavid Howells 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
8119875cf80SDavid Howells 			return -EREMOTE;
8129875cf80SDavid Howells 		return PTR_ERR(mnt);
81331e6b01fSNick Piggin 	}
814ea5b778aSDavid Howells 
8159875cf80SDavid Howells 	if (!mnt) /* mount collision */
8169875cf80SDavid Howells 		return 0;
8179875cf80SDavid Howells 
8188aef1884SAl Viro 	if (!*need_mntput) {
8198aef1884SAl Viro 		/* lock_mount() may release path->mnt on error */
8208aef1884SAl Viro 		mntget(path->mnt);
8218aef1884SAl Viro 		*need_mntput = true;
8228aef1884SAl Viro 	}
82319a167afSAl Viro 	err = finish_automount(mnt, path);
824ea5b778aSDavid Howells 
825ea5b778aSDavid Howells 	switch (err) {
826ea5b778aSDavid Howells 	case -EBUSY:
827ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
82819a167afSAl Viro 		return 0;
829ea5b778aSDavid Howells 	case 0:
8308aef1884SAl Viro 		path_put(path);
8319875cf80SDavid Howells 		path->mnt = mnt;
8329875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
8339875cf80SDavid Howells 		return 0;
83419a167afSAl Viro 	default:
83519a167afSAl Viro 		return err;
8369875cf80SDavid Howells 	}
83719a167afSAl Viro 
838ea5b778aSDavid Howells }
8399875cf80SDavid Howells 
8409875cf80SDavid Howells /*
8419875cf80SDavid Howells  * Handle a dentry that is managed in some way.
842cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
8439875cf80SDavid Howells  * - Flagged as mountpoint
8449875cf80SDavid Howells  * - Flagged as automount point
8459875cf80SDavid Howells  *
8469875cf80SDavid Howells  * This may only be called in refwalk mode.
8479875cf80SDavid Howells  *
8489875cf80SDavid Howells  * Serialization is taken care of in namespace.c
8499875cf80SDavid Howells  */
8509875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
8519875cf80SDavid Howells {
8528aef1884SAl Viro 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
8539875cf80SDavid Howells 	unsigned managed;
8549875cf80SDavid Howells 	bool need_mntput = false;
8558aef1884SAl Viro 	int ret = 0;
8569875cf80SDavid Howells 
8579875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
8589875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
8599875cf80SDavid Howells 	 * the components of that value change under us */
8609875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
8619875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
8629875cf80SDavid Howells 	       unlikely(managed != 0)) {
863cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
864cc53ce53SDavid Howells 		 * being held. */
865cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
866cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
867cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
8681aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
869cc53ce53SDavid Howells 			if (ret < 0)
8708aef1884SAl Viro 				break;
871cc53ce53SDavid Howells 		}
872cc53ce53SDavid Howells 
8739875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
8749875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
8759875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
8769875cf80SDavid Howells 			if (mounted) {
8779875cf80SDavid Howells 				dput(path->dentry);
8789875cf80SDavid Howells 				if (need_mntput)
879463ffb2eSAl Viro 					mntput(path->mnt);
880463ffb2eSAl Viro 				path->mnt = mounted;
881463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
8829875cf80SDavid Howells 				need_mntput = true;
8839875cf80SDavid Howells 				continue;
884463ffb2eSAl Viro 			}
885463ffb2eSAl Viro 
8869875cf80SDavid Howells 			/* Something is mounted on this dentry in another
8879875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
8889875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
8899875cf80SDavid Howells 			 * vfsmount_lock */
8901da177e4SLinus Torvalds 		}
8919875cf80SDavid Howells 
8929875cf80SDavid Howells 		/* Handle an automount point */
8939875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
8949875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
8959875cf80SDavid Howells 			if (ret < 0)
8968aef1884SAl Viro 				break;
8979875cf80SDavid Howells 			continue;
8989875cf80SDavid Howells 		}
8999875cf80SDavid Howells 
9009875cf80SDavid Howells 		/* We didn't change the current path point */
9019875cf80SDavid Howells 		break;
9029875cf80SDavid Howells 	}
9038aef1884SAl Viro 
9048aef1884SAl Viro 	if (need_mntput && path->mnt == mnt)
9058aef1884SAl Viro 		mntput(path->mnt);
9068aef1884SAl Viro 	if (ret == -EISDIR)
9078aef1884SAl Viro 		ret = 0;
9088aef1884SAl Viro 	return ret;
9091da177e4SLinus Torvalds }
9101da177e4SLinus Torvalds 
911cc53ce53SDavid Howells int follow_down_one(struct path *path)
9121da177e4SLinus Torvalds {
9131da177e4SLinus Torvalds 	struct vfsmount *mounted;
9141da177e4SLinus Torvalds 
9151c755af4SAl Viro 	mounted = lookup_mnt(path);
9161da177e4SLinus Torvalds 	if (mounted) {
9179393bd07SAl Viro 		dput(path->dentry);
9189393bd07SAl Viro 		mntput(path->mnt);
9199393bd07SAl Viro 		path->mnt = mounted;
9209393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
9211da177e4SLinus Torvalds 		return 1;
9221da177e4SLinus Torvalds 	}
9231da177e4SLinus Torvalds 	return 0;
9241da177e4SLinus Torvalds }
9251da177e4SLinus Torvalds 
92662a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
92762a7375eSIan Kent {
92862a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
92962a7375eSIan Kent 		dentry->d_op->d_manage(dentry, true) < 0);
93062a7375eSIan Kent }
93162a7375eSIan Kent 
9329875cf80SDavid Howells /*
933287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
934287548e4SAl Viro  * we meet a managed dentry that would need blocking.
9359875cf80SDavid Howells  */
9369875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
937287548e4SAl Viro 			       struct inode **inode)
9389875cf80SDavid Howells {
93962a7375eSIan Kent 	for (;;) {
9409875cf80SDavid Howells 		struct vfsmount *mounted;
94162a7375eSIan Kent 		/*
94262a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
94362a7375eSIan Kent 		 * that wants to block transit.
94462a7375eSIan Kent 		 */
945287548e4SAl Viro 		if (unlikely(managed_dentry_might_block(path->dentry)))
946ab90911fSDavid Howells 			return false;
94762a7375eSIan Kent 
94862a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
94962a7375eSIan Kent 			break;
95062a7375eSIan Kent 
9519875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
9529875cf80SDavid Howells 		if (!mounted)
9539875cf80SDavid Howells 			break;
9549875cf80SDavid Howells 		path->mnt = mounted;
9559875cf80SDavid Howells 		path->dentry = mounted->mnt_root;
9569875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
95759430262SLinus Torvalds 		/*
95859430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
95959430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
96059430262SLinus Torvalds 		 * because a mount-point is always pinned.
96159430262SLinus Torvalds 		 */
96259430262SLinus Torvalds 		*inode = path->dentry->d_inode;
9639875cf80SDavid Howells 	}
9649875cf80SDavid Howells 	return true;
9659875cf80SDavid Howells }
9669875cf80SDavid Howells 
967dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd)
968287548e4SAl Viro {
969dea39376SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
970287548e4SAl Viro 		struct vfsmount *mounted;
971dea39376SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
972287548e4SAl Viro 		if (!mounted)
973287548e4SAl Viro 			break;
974dea39376SAl Viro 		nd->path.mnt = mounted;
975dea39376SAl Viro 		nd->path.dentry = mounted->mnt_root;
976dea39376SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
977287548e4SAl Viro 	}
978287548e4SAl Viro }
979287548e4SAl Viro 
98031e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
98131e6b01fSNick Piggin {
98231e6b01fSNick Piggin 	set_root_rcu(nd);
98331e6b01fSNick Piggin 
98431e6b01fSNick Piggin 	while (1) {
98531e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
98631e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
98731e6b01fSNick Piggin 			break;
98831e6b01fSNick Piggin 		}
98931e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
99031e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
99131e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
99231e6b01fSNick Piggin 			unsigned seq;
99331e6b01fSNick Piggin 
99431e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
99531e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
996ef7562d5SAl Viro 				goto failed;
99731e6b01fSNick Piggin 			nd->path.dentry = parent;
99831e6b01fSNick Piggin 			nd->seq = seq;
99931e6b01fSNick Piggin 			break;
100031e6b01fSNick Piggin 		}
100131e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
100231e6b01fSNick Piggin 			break;
100331e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
100431e6b01fSNick Piggin 	}
1005dea39376SAl Viro 	follow_mount_rcu(nd);
1006dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
100731e6b01fSNick Piggin 	return 0;
1008ef7562d5SAl Viro 
1009ef7562d5SAl Viro failed:
1010ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
10115b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
1012ef7562d5SAl Viro 		nd->root.mnt = NULL;
1013ef7562d5SAl Viro 	rcu_read_unlock();
1014ef7562d5SAl Viro 	br_read_unlock(vfsmount_lock);
1015ef7562d5SAl Viro 	return -ECHILD;
101631e6b01fSNick Piggin }
101731e6b01fSNick Piggin 
10189875cf80SDavid Howells /*
1019cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
1020cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
1021cc53ce53SDavid Howells  * caller is permitted to proceed or not.
1022cc53ce53SDavid Howells  */
10237cc90cc3SAl Viro int follow_down(struct path *path)
1024cc53ce53SDavid Howells {
1025cc53ce53SDavid Howells 	unsigned managed;
1026cc53ce53SDavid Howells 	int ret;
1027cc53ce53SDavid Howells 
1028cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
1029cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1030cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1031cc53ce53SDavid Howells 		 * being held.
1032cc53ce53SDavid Howells 		 *
1033cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
1034cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
1035cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
1036cc53ce53SDavid Howells 		 * superstructure.
1037cc53ce53SDavid Howells 		 *
1038cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
1039cc53ce53SDavid Howells 		 */
1040cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
1041cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1042cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
1043ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
10441aed3e42SAl Viro 				path->dentry, false);
1045cc53ce53SDavid Howells 			if (ret < 0)
1046cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
1047cc53ce53SDavid Howells 		}
1048cc53ce53SDavid Howells 
1049cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
1050cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
1051cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
1052cc53ce53SDavid Howells 			if (!mounted)
1053cc53ce53SDavid Howells 				break;
1054cc53ce53SDavid Howells 			dput(path->dentry);
1055cc53ce53SDavid Howells 			mntput(path->mnt);
1056cc53ce53SDavid Howells 			path->mnt = mounted;
1057cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1058cc53ce53SDavid Howells 			continue;
1059cc53ce53SDavid Howells 		}
1060cc53ce53SDavid Howells 
1061cc53ce53SDavid Howells 		/* Don't handle automount points here */
1062cc53ce53SDavid Howells 		break;
1063cc53ce53SDavid Howells 	}
1064cc53ce53SDavid Howells 	return 0;
1065cc53ce53SDavid Howells }
1066cc53ce53SDavid Howells 
1067cc53ce53SDavid Howells /*
10689875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
10699875cf80SDavid Howells  */
10709875cf80SDavid Howells static void follow_mount(struct path *path)
10719875cf80SDavid Howells {
10729875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
10739875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
10749875cf80SDavid Howells 		if (!mounted)
10759875cf80SDavid Howells 			break;
10769875cf80SDavid Howells 		dput(path->dentry);
10779875cf80SDavid Howells 		mntput(path->mnt);
10789875cf80SDavid Howells 		path->mnt = mounted;
10799875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
10809875cf80SDavid Howells 	}
10819875cf80SDavid Howells }
10829875cf80SDavid Howells 
108331e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
10841da177e4SLinus Torvalds {
10852a737871SAl Viro 	set_root(nd);
1086e518ddb7SAndreas Mohr 
10871da177e4SLinus Torvalds 	while(1) {
10884ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
10891da177e4SLinus Torvalds 
10902a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
10912a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
10921da177e4SLinus Torvalds 			break;
10931da177e4SLinus Torvalds 		}
10944ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
10953088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
10963088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
10971da177e4SLinus Torvalds 			dput(old);
10981da177e4SLinus Torvalds 			break;
10991da177e4SLinus Torvalds 		}
11003088dd70SAl Viro 		if (!follow_up(&nd->path))
11011da177e4SLinus Torvalds 			break;
11021da177e4SLinus Torvalds 	}
110379ed0226SAl Viro 	follow_mount(&nd->path);
110431e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
11051da177e4SLinus Torvalds }
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds /*
1108baa03890SNick Piggin  * Allocate a dentry with name and parent, and perform a parent
1109baa03890SNick Piggin  * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1110baa03890SNick Piggin  * on error. parent->d_inode->i_mutex must be held. d_lookup must
1111baa03890SNick Piggin  * have verified that no child exists while under i_mutex.
1112baa03890SNick Piggin  */
1113baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1114baa03890SNick Piggin 				struct qstr *name, struct nameidata *nd)
1115baa03890SNick Piggin {
1116baa03890SNick Piggin 	struct inode *inode = parent->d_inode;
1117baa03890SNick Piggin 	struct dentry *dentry;
1118baa03890SNick Piggin 	struct dentry *old;
1119baa03890SNick Piggin 
1120baa03890SNick Piggin 	/* Don't create child dentry for a dead directory. */
1121baa03890SNick Piggin 	if (unlikely(IS_DEADDIR(inode)))
1122baa03890SNick Piggin 		return ERR_PTR(-ENOENT);
1123baa03890SNick Piggin 
1124baa03890SNick Piggin 	dentry = d_alloc(parent, name);
1125baa03890SNick Piggin 	if (unlikely(!dentry))
1126baa03890SNick Piggin 		return ERR_PTR(-ENOMEM);
1127baa03890SNick Piggin 
1128baa03890SNick Piggin 	old = inode->i_op->lookup(inode, dentry, nd);
1129baa03890SNick Piggin 	if (unlikely(old)) {
1130baa03890SNick Piggin 		dput(dentry);
1131baa03890SNick Piggin 		dentry = old;
1132baa03890SNick Piggin 	}
1133baa03890SNick Piggin 	return dentry;
1134baa03890SNick Piggin }
1135baa03890SNick Piggin 
1136baa03890SNick Piggin /*
113744396f4bSJosef Bacik  * We already have a dentry, but require a lookup to be performed on the parent
113844396f4bSJosef Bacik  * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
113944396f4bSJosef Bacik  * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
114044396f4bSJosef Bacik  * child exists while under i_mutex.
114144396f4bSJosef Bacik  */
114244396f4bSJosef Bacik static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
114344396f4bSJosef Bacik 				     struct nameidata *nd)
114444396f4bSJosef Bacik {
114544396f4bSJosef Bacik 	struct inode *inode = parent->d_inode;
114644396f4bSJosef Bacik 	struct dentry *old;
114744396f4bSJosef Bacik 
114844396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
114944396f4bSJosef Bacik 	if (unlikely(IS_DEADDIR(inode)))
115044396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
115144396f4bSJosef Bacik 
115244396f4bSJosef Bacik 	old = inode->i_op->lookup(inode, dentry, nd);
115344396f4bSJosef Bacik 	if (unlikely(old)) {
115444396f4bSJosef Bacik 		dput(dentry);
115544396f4bSJosef Bacik 		dentry = old;
115644396f4bSJosef Bacik 	}
115744396f4bSJosef Bacik 	return dentry;
115844396f4bSJosef Bacik }
115944396f4bSJosef Bacik 
116044396f4bSJosef Bacik /*
11611da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
11621da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
11631da177e4SLinus Torvalds  *  It _is_ time-critical.
11641da177e4SLinus Torvalds  */
11651da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
116631e6b01fSNick Piggin 			struct path *path, struct inode **inode)
11671da177e4SLinus Torvalds {
11684ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
116931e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
11705a18fff2SAl Viro 	int need_reval = 1;
11715a18fff2SAl Viro 	int status = 1;
11729875cf80SDavid Howells 	int err;
11739875cf80SDavid Howells 
11743cac260aSAl Viro 	/*
1175b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1176b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1177b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1178b04f784eSNick Piggin 	 */
117931e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
118031e6b01fSNick Piggin 		unsigned seq;
118131e6b01fSNick Piggin 		*inode = nd->inode;
118231e6b01fSNick Piggin 		dentry = __d_lookup_rcu(parent, name, &seq, inode);
11835a18fff2SAl Viro 		if (!dentry)
11845a18fff2SAl Viro 			goto unlazy;
11855a18fff2SAl Viro 
118631e6b01fSNick Piggin 		/* Memory barrier in read_seqcount_begin of child is enough */
118731e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
118831e6b01fSNick Piggin 			return -ECHILD;
118931e6b01fSNick Piggin 		nd->seq = seq;
11905a18fff2SAl Viro 
119124643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
11925a18fff2SAl Viro 			status = d_revalidate(dentry, nd);
11935a18fff2SAl Viro 			if (unlikely(status <= 0)) {
11945a18fff2SAl Viro 				if (status != -ECHILD)
11955a18fff2SAl Viro 					need_reval = 0;
11965a18fff2SAl Viro 				goto unlazy;
11975a18fff2SAl Viro 			}
119824643087SAl Viro 		}
119944396f4bSJosef Bacik 		if (unlikely(d_need_lookup(dentry)))
120044396f4bSJosef Bacik 			goto unlazy;
120131e6b01fSNick Piggin 		path->mnt = mnt;
120231e6b01fSNick Piggin 		path->dentry = dentry;
1203d6e9bd25SAl Viro 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1204d6e9bd25SAl Viro 			goto unlazy;
1205d6e9bd25SAl Viro 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1206d6e9bd25SAl Viro 			goto unlazy;
12079875cf80SDavid Howells 		return 0;
12085a18fff2SAl Viro unlazy:
120919660af7SAl Viro 		if (unlazy_walk(nd, dentry))
12105a18fff2SAl Viro 			return -ECHILD;
12115a18fff2SAl Viro 	} else {
121231e6b01fSNick Piggin 		dentry = __d_lookup(parent, name);
121324643087SAl Viro 	}
12145a18fff2SAl Viro 
121544396f4bSJosef Bacik 	if (dentry && unlikely(d_need_lookup(dentry))) {
121644396f4bSJosef Bacik 		dput(dentry);
121744396f4bSJosef Bacik 		dentry = NULL;
121844396f4bSJosef Bacik 	}
12195a18fff2SAl Viro retry:
12205a18fff2SAl Viro 	if (unlikely(!dentry)) {
12215a18fff2SAl Viro 		struct inode *dir = parent->d_inode;
12225a18fff2SAl Viro 		BUG_ON(nd->inode != dir);
12235a18fff2SAl Viro 
12245a18fff2SAl Viro 		mutex_lock(&dir->i_mutex);
12255a18fff2SAl Viro 		dentry = d_lookup(parent, name);
12265a18fff2SAl Viro 		if (likely(!dentry)) {
12275a18fff2SAl Viro 			dentry = d_alloc_and_lookup(parent, name, nd);
12285a18fff2SAl Viro 			if (IS_ERR(dentry)) {
12295a18fff2SAl Viro 				mutex_unlock(&dir->i_mutex);
12305a18fff2SAl Viro 				return PTR_ERR(dentry);
12315a18fff2SAl Viro 			}
12325a18fff2SAl Viro 			/* known good */
12335a18fff2SAl Viro 			need_reval = 0;
12345a18fff2SAl Viro 			status = 1;
123544396f4bSJosef Bacik 		} else if (unlikely(d_need_lookup(dentry))) {
123644396f4bSJosef Bacik 			dentry = d_inode_lookup(parent, dentry, nd);
123744396f4bSJosef Bacik 			if (IS_ERR(dentry)) {
123844396f4bSJosef Bacik 				mutex_unlock(&dir->i_mutex);
123944396f4bSJosef Bacik 				return PTR_ERR(dentry);
124044396f4bSJosef Bacik 			}
124144396f4bSJosef Bacik 			/* known good */
124244396f4bSJosef Bacik 			need_reval = 0;
124344396f4bSJosef Bacik 			status = 1;
12445a18fff2SAl Viro 		}
12455a18fff2SAl Viro 		mutex_unlock(&dir->i_mutex);
12465a18fff2SAl Viro 	}
12475a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
12485a18fff2SAl Viro 		status = d_revalidate(dentry, nd);
12495a18fff2SAl Viro 	if (unlikely(status <= 0)) {
12505a18fff2SAl Viro 		if (status < 0) {
12515a18fff2SAl Viro 			dput(dentry);
12525a18fff2SAl Viro 			return status;
12535a18fff2SAl Viro 		}
12545a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
12555a18fff2SAl Viro 			dput(dentry);
12565a18fff2SAl Viro 			dentry = NULL;
12575a18fff2SAl Viro 			need_reval = 1;
12585a18fff2SAl Viro 			goto retry;
12595a18fff2SAl Viro 		}
12605a18fff2SAl Viro 	}
12615a18fff2SAl Viro 
12621da177e4SLinus Torvalds 	path->mnt = mnt;
12631da177e4SLinus Torvalds 	path->dentry = dentry;
12649875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
126589312214SIan Kent 	if (unlikely(err < 0)) {
126689312214SIan Kent 		path_put_conditional(path, nd);
12679875cf80SDavid Howells 		return err;
126889312214SIan Kent 	}
126931e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
12701da177e4SLinus Torvalds 	return 0;
12711da177e4SLinus Torvalds }
12721da177e4SLinus Torvalds 
127352094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
127452094c8aSAl Viro {
127552094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
127652094c8aSAl Viro 		int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
127752094c8aSAl Viro 		if (err != -ECHILD)
127852094c8aSAl Viro 			return err;
127919660af7SAl Viro 		if (unlazy_walk(nd, NULL))
128052094c8aSAl Viro 			return -ECHILD;
128152094c8aSAl Viro 	}
128252094c8aSAl Viro 	return exec_permission(nd->inode, 0);
128352094c8aSAl Viro }
128452094c8aSAl Viro 
12859856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
12869856fa1bSAl Viro {
12879856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
12889856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
12899856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
12909856fa1bSAl Viro 				return -ECHILD;
12919856fa1bSAl Viro 		} else
12929856fa1bSAl Viro 			follow_dotdot(nd);
12939856fa1bSAl Viro 	}
12949856fa1bSAl Viro 	return 0;
12959856fa1bSAl Viro }
12969856fa1bSAl Viro 
1297951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1298951361f9SAl Viro {
1299951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1300951361f9SAl Viro 		path_put(&nd->path);
1301951361f9SAl Viro 	} else {
1302951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
13035b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1304951361f9SAl Viro 			nd->root.mnt = NULL;
1305951361f9SAl Viro 		rcu_read_unlock();
1306951361f9SAl Viro 		br_read_unlock(vfsmount_lock);
1307951361f9SAl Viro 	}
1308951361f9SAl Viro }
1309951361f9SAl Viro 
1310ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
1311ce57dfc1SAl Viro 		struct qstr *name, int type, int follow)
1312ce57dfc1SAl Viro {
1313ce57dfc1SAl Viro 	struct inode *inode;
1314ce57dfc1SAl Viro 	int err;
1315ce57dfc1SAl Viro 	/*
1316ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1317ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1318ce57dfc1SAl Viro 	 * parent relationships.
1319ce57dfc1SAl Viro 	 */
1320ce57dfc1SAl Viro 	if (unlikely(type != LAST_NORM))
1321ce57dfc1SAl Viro 		return handle_dots(nd, type);
1322ce57dfc1SAl Viro 	err = do_lookup(nd, name, path, &inode);
1323ce57dfc1SAl Viro 	if (unlikely(err)) {
1324ce57dfc1SAl Viro 		terminate_walk(nd);
1325ce57dfc1SAl Viro 		return err;
1326ce57dfc1SAl Viro 	}
1327ce57dfc1SAl Viro 	if (!inode) {
1328ce57dfc1SAl Viro 		path_to_nameidata(path, nd);
1329ce57dfc1SAl Viro 		terminate_walk(nd);
1330ce57dfc1SAl Viro 		return -ENOENT;
1331ce57dfc1SAl Viro 	}
1332ce57dfc1SAl Viro 	if (unlikely(inode->i_op->follow_link) && follow) {
133319660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
133419660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
133519660af7SAl Viro 				terminate_walk(nd);
1336ce57dfc1SAl Viro 				return -ECHILD;
133719660af7SAl Viro 			}
133819660af7SAl Viro 		}
1339ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1340ce57dfc1SAl Viro 		return 1;
1341ce57dfc1SAl Viro 	}
1342ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1343ce57dfc1SAl Viro 	nd->inode = inode;
1344ce57dfc1SAl Viro 	return 0;
1345ce57dfc1SAl Viro }
1346ce57dfc1SAl Viro 
13471da177e4SLinus Torvalds /*
1348b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1349b356379aSAl Viro  * limiting consecutive symlinks to 40.
1350b356379aSAl Viro  *
1351b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1352b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1353b356379aSAl Viro  */
1354b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1355b356379aSAl Viro {
1356b356379aSAl Viro 	int res;
1357b356379aSAl Viro 
1358b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1359b356379aSAl Viro 		path_put_conditional(path, nd);
1360b356379aSAl Viro 		path_put(&nd->path);
1361b356379aSAl Viro 		return -ELOOP;
1362b356379aSAl Viro 	}
13631a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1364b356379aSAl Viro 
1365b356379aSAl Viro 	nd->depth++;
1366b356379aSAl Viro 	current->link_count++;
1367b356379aSAl Viro 
1368b356379aSAl Viro 	do {
1369b356379aSAl Viro 		struct path link = *path;
1370b356379aSAl Viro 		void *cookie;
1371574197e0SAl Viro 
1372574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
1373b356379aSAl Viro 		if (!res)
1374b356379aSAl Viro 			res = walk_component(nd, path, &nd->last,
1375b356379aSAl Viro 					     nd->last_type, LOOKUP_FOLLOW);
1376574197e0SAl Viro 		put_link(nd, &link, cookie);
1377b356379aSAl Viro 	} while (res > 0);
1378b356379aSAl Viro 
1379b356379aSAl Viro 	current->link_count--;
1380b356379aSAl Viro 	nd->depth--;
1381b356379aSAl Viro 	return res;
1382b356379aSAl Viro }
1383b356379aSAl Viro 
1384b356379aSAl Viro /*
13851da177e4SLinus Torvalds  * Name resolution.
1386ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1387ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
13881da177e4SLinus Torvalds  *
1389ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1390ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
13911da177e4SLinus Torvalds  */
13926de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
13931da177e4SLinus Torvalds {
13941da177e4SLinus Torvalds 	struct path next;
13951da177e4SLinus Torvalds 	int err;
13961da177e4SLinus Torvalds 	unsigned int lookup_flags = nd->flags;
13971da177e4SLinus Torvalds 
13981da177e4SLinus Torvalds 	while (*name=='/')
13991da177e4SLinus Torvalds 		name++;
14001da177e4SLinus Torvalds 	if (!*name)
1401086e183aSAl Viro 		return 0;
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
14041da177e4SLinus Torvalds 	for(;;) {
14051da177e4SLinus Torvalds 		unsigned long hash;
14061da177e4SLinus Torvalds 		struct qstr this;
14071da177e4SLinus Torvalds 		unsigned int c;
1408fe479a58SAl Viro 		int type;
14091da177e4SLinus Torvalds 
1410cdce5d6bSTrond Myklebust 		nd->flags |= LOOKUP_CONTINUE;
141152094c8aSAl Viro 
141252094c8aSAl Viro 		err = may_lookup(nd);
14131da177e4SLinus Torvalds  		if (err)
14141da177e4SLinus Torvalds 			break;
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds 		this.name = name;
14171da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds 		hash = init_name_hash();
14201da177e4SLinus Torvalds 		do {
14211da177e4SLinus Torvalds 			name++;
14221da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
14231da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
14241da177e4SLinus Torvalds 		} while (c && (c != '/'));
14251da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
14261da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
14271da177e4SLinus Torvalds 
1428fe479a58SAl Viro 		type = LAST_NORM;
1429fe479a58SAl Viro 		if (this.name[0] == '.') switch (this.len) {
1430fe479a58SAl Viro 			case 2:
143116c2cd71SAl Viro 				if (this.name[1] == '.') {
1432fe479a58SAl Viro 					type = LAST_DOTDOT;
143316c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
143416c2cd71SAl Viro 				}
1435fe479a58SAl Viro 				break;
1436fe479a58SAl Viro 			case 1:
1437fe479a58SAl Viro 				type = LAST_DOT;
1438fe479a58SAl Viro 		}
14395a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
14405a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
144116c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
14425a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
14435a202bcdSAl Viro 				err = parent->d_op->d_hash(parent, nd->inode,
14445a202bcdSAl Viro 							   &this);
14455a202bcdSAl Viro 				if (err < 0)
14465a202bcdSAl Viro 					break;
14475a202bcdSAl Viro 			}
14485a202bcdSAl Viro 		}
1449fe479a58SAl Viro 
14501da177e4SLinus Torvalds 		/* remove trailing slashes? */
14511da177e4SLinus Torvalds 		if (!c)
14521da177e4SLinus Torvalds 			goto last_component;
14531da177e4SLinus Torvalds 		while (*++name == '/');
14541da177e4SLinus Torvalds 		if (!*name)
1455b356379aSAl Viro 			goto last_component;
14561da177e4SLinus Torvalds 
1457ce57dfc1SAl Viro 		err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1458ce57dfc1SAl Viro 		if (err < 0)
1459ce57dfc1SAl Viro 			return err;
1460fe479a58SAl Viro 
1461ce57dfc1SAl Viro 		if (err) {
1462b356379aSAl Viro 			err = nested_symlink(&next, nd);
14631da177e4SLinus Torvalds 			if (err)
1464a7472babSAl Viro 				return err;
146531e6b01fSNick Piggin 		}
14661da177e4SLinus Torvalds 		err = -ENOTDIR;
146731e6b01fSNick Piggin 		if (!nd->inode->i_op->lookup)
14681da177e4SLinus Torvalds 			break;
14691da177e4SLinus Torvalds 		continue;
14701da177e4SLinus Torvalds 		/* here ends the main loop */
14711da177e4SLinus Torvalds 
14721da177e4SLinus Torvalds last_component:
1473f55eab82STrond Myklebust 		/* Clear LOOKUP_CONTINUE iff it was previously unset */
1474f55eab82STrond Myklebust 		nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
1475ce57dfc1SAl Viro 		nd->last = this;
1476ce57dfc1SAl Viro 		nd->last_type = type;
1477ce57dfc1SAl Viro 		return 0;
1478ce57dfc1SAl Viro 	}
1479951361f9SAl Viro 	terminate_walk(nd);
14801da177e4SLinus Torvalds 	return err;
14811da177e4SLinus Torvalds }
14821da177e4SLinus Torvalds 
148370e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
148470e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
148531e6b01fSNick Piggin {
148631e6b01fSNick Piggin 	int retval = 0;
148731e6b01fSNick Piggin 	int fput_needed;
148831e6b01fSNick Piggin 	struct file *file;
148931e6b01fSNick Piggin 
149031e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
149116c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
149231e6b01fSNick Piggin 	nd->depth = 0;
14935b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
14945b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
149573d049a4SAl Viro 		if (*name) {
14965b6ca027SAl Viro 			if (!inode->i_op->lookup)
14975b6ca027SAl Viro 				return -ENOTDIR;
14985b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
14995b6ca027SAl Viro 			if (retval)
15005b6ca027SAl Viro 				return retval;
150173d049a4SAl Viro 		}
15025b6ca027SAl Viro 		nd->path = nd->root;
15035b6ca027SAl Viro 		nd->inode = inode;
15045b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
15055b6ca027SAl Viro 			br_read_lock(vfsmount_lock);
15065b6ca027SAl Viro 			rcu_read_lock();
15075b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
15085b6ca027SAl Viro 		} else {
15095b6ca027SAl Viro 			path_get(&nd->path);
15105b6ca027SAl Viro 		}
15115b6ca027SAl Viro 		return 0;
15125b6ca027SAl Viro 	}
15135b6ca027SAl Viro 
151431e6b01fSNick Piggin 	nd->root.mnt = NULL;
151531e6b01fSNick Piggin 
151631e6b01fSNick Piggin 	if (*name=='/') {
1517e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
151831e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
151931e6b01fSNick Piggin 			rcu_read_lock();
1520e41f7d4eSAl Viro 			set_root_rcu(nd);
1521e41f7d4eSAl Viro 		} else {
1522e41f7d4eSAl Viro 			set_root(nd);
1523e41f7d4eSAl Viro 			path_get(&nd->root);
1524e41f7d4eSAl Viro 		}
152531e6b01fSNick Piggin 		nd->path = nd->root;
152631e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1527e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
152831e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1529c28cc364SNick Piggin 			unsigned seq;
153031e6b01fSNick Piggin 
153131e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
153231e6b01fSNick Piggin 			rcu_read_lock();
153331e6b01fSNick Piggin 
1534c28cc364SNick Piggin 			do {
1535c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
153631e6b01fSNick Piggin 				nd->path = fs->pwd;
1537c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1538c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1539e41f7d4eSAl Viro 		} else {
1540e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1541e41f7d4eSAl Viro 		}
154231e6b01fSNick Piggin 	} else {
154331e6b01fSNick Piggin 		struct dentry *dentry;
154431e6b01fSNick Piggin 
15451abf0c71SAl Viro 		file = fget_raw_light(dfd, &fput_needed);
154631e6b01fSNick Piggin 		retval = -EBADF;
154731e6b01fSNick Piggin 		if (!file)
154831e6b01fSNick Piggin 			goto out_fail;
154931e6b01fSNick Piggin 
155031e6b01fSNick Piggin 		dentry = file->f_path.dentry;
155131e6b01fSNick Piggin 
1552f52e0c11SAl Viro 		if (*name) {
155331e6b01fSNick Piggin 			retval = -ENOTDIR;
155431e6b01fSNick Piggin 			if (!S_ISDIR(dentry->d_inode->i_mode))
155531e6b01fSNick Piggin 				goto fput_fail;
155631e6b01fSNick Piggin 
155731e6b01fSNick Piggin 			retval = file_permission(file, MAY_EXEC);
155831e6b01fSNick Piggin 			if (retval)
155931e6b01fSNick Piggin 				goto fput_fail;
1560f52e0c11SAl Viro 		}
156131e6b01fSNick Piggin 
156231e6b01fSNick Piggin 		nd->path = file->f_path;
1563e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
156431e6b01fSNick Piggin 			if (fput_needed)
156570e9b357SAl Viro 				*fp = file;
1566c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
156731e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
156831e6b01fSNick Piggin 			rcu_read_lock();
15695590ff0dSUlrich Drepper 		} else {
15705dd784d0SJan Blunck 			path_get(&file->f_path);
15715590ff0dSUlrich Drepper 			fput_light(file, fput_needed);
15721da177e4SLinus Torvalds 		}
1573e41f7d4eSAl Viro 	}
1574e41f7d4eSAl Viro 
157531e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
15769b4a9b14SAl Viro 	return 0;
15772dfdd266SJosef 'Jeff' Sipek 
15789b4a9b14SAl Viro fput_fail:
15799b4a9b14SAl Viro 	fput_light(file, fput_needed);
15809b4a9b14SAl Viro out_fail:
15819b4a9b14SAl Viro 	return retval;
15829b4a9b14SAl Viro }
15839b4a9b14SAl Viro 
1584bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1585bd92d7feSAl Viro {
1586bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1587bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1588bd92d7feSAl Viro 
1589bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
1590bd92d7feSAl Viro 	return walk_component(nd, path, &nd->last, nd->last_type,
1591bd92d7feSAl Viro 					nd->flags & LOOKUP_FOLLOW);
1592bd92d7feSAl Viro }
1593bd92d7feSAl Viro 
15949b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1595ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
15969b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
15979b4a9b14SAl Viro {
159870e9b357SAl Viro 	struct file *base = NULL;
1599bd92d7feSAl Viro 	struct path path;
1600bd92d7feSAl Viro 	int err;
160131e6b01fSNick Piggin 
160231e6b01fSNick Piggin 	/*
160331e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
160431e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
160531e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
160631e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
160731e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
160831e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
160931e6b01fSNick Piggin 	 * analogue, foo_rcu().
161031e6b01fSNick Piggin 	 *
161131e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
161231e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
161331e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
161431e6b01fSNick Piggin 	 * be able to complete).
161531e6b01fSNick Piggin 	 */
1616bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1617ee0827cdSAl Viro 
1618bd92d7feSAl Viro 	if (unlikely(err))
1619bd92d7feSAl Viro 		return err;
1620ee0827cdSAl Viro 
1621ee0827cdSAl Viro 	current->total_link_count = 0;
1622bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1623bd92d7feSAl Viro 
1624bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1625bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1626bd92d7feSAl Viro 		while (err > 0) {
1627bd92d7feSAl Viro 			void *cookie;
1628bd92d7feSAl Viro 			struct path link = path;
1629bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1630574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
1631bd92d7feSAl Viro 			if (!err)
1632bd92d7feSAl Viro 				err = lookup_last(nd, &path);
1633574197e0SAl Viro 			put_link(nd, &link, cookie);
1634bd92d7feSAl Viro 		}
1635bd92d7feSAl Viro 	}
1636ee0827cdSAl Viro 
16379f1fafeeSAl Viro 	if (!err)
16389f1fafeeSAl Viro 		err = complete_walk(nd);
1639bd92d7feSAl Viro 
1640bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
1641bd92d7feSAl Viro 		if (!nd->inode->i_op->lookup) {
1642bd92d7feSAl Viro 			path_put(&nd->path);
1643bd23a539SAl Viro 			err = -ENOTDIR;
1644bd92d7feSAl Viro 		}
1645bd92d7feSAl Viro 	}
164616c2cd71SAl Viro 
164770e9b357SAl Viro 	if (base)
164870e9b357SAl Viro 		fput(base);
1649ee0827cdSAl Viro 
16505b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
165131e6b01fSNick Piggin 		path_put(&nd->root);
165231e6b01fSNick Piggin 		nd->root.mnt = NULL;
165331e6b01fSNick Piggin 	}
1654bd92d7feSAl Viro 	return err;
165531e6b01fSNick Piggin }
165631e6b01fSNick Piggin 
1657ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1658ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1659ee0827cdSAl Viro {
1660ee0827cdSAl Viro 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1661ee0827cdSAl Viro 	if (unlikely(retval == -ECHILD))
1662ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags, nd);
1663ee0827cdSAl Viro 	if (unlikely(retval == -ESTALE))
1664ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1665ee0827cdSAl Viro 
166631e6b01fSNick Piggin 	if (likely(!retval)) {
166731e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
166831e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
166931e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
167031e6b01fSNick Piggin 		}
167131e6b01fSNick Piggin 	}
1672170aa3d0SUlrich Drepper 	return retval;
16731da177e4SLinus Torvalds }
16741da177e4SLinus Torvalds 
1675c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd)
16765590ff0dSUlrich Drepper {
1677c9c6cac0SAl Viro 	return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
16785590ff0dSUlrich Drepper }
16795590ff0dSUlrich Drepper 
1680d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1681d1811465SAl Viro {
1682d1811465SAl Viro 	struct nameidata nd;
1683d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1684d1811465SAl Viro 	if (!res)
1685d1811465SAl Viro 		*path = nd.path;
1686d1811465SAl Viro 	return res;
1687d1811465SAl Viro }
1688d1811465SAl Viro 
168916f18200SJosef 'Jeff' Sipek /**
169016f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
169116f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
169216f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
169316f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
169416f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
169516f18200SJosef 'Jeff' Sipek  * @nd: pointer to nameidata
169616f18200SJosef 'Jeff' Sipek  */
169716f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
169816f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
169916f18200SJosef 'Jeff' Sipek 		    struct nameidata *nd)
170016f18200SJosef 'Jeff' Sipek {
17015b6ca027SAl Viro 	nd->root.dentry = dentry;
17025b6ca027SAl Viro 	nd->root.mnt = mnt;
17035b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
17045b6ca027SAl Viro 	return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
170516f18200SJosef 'Jeff' Sipek }
170616f18200SJosef 'Jeff' Sipek 
1707eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1708eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
17091da177e4SLinus Torvalds {
171081fca444SChristoph Hellwig 	struct inode *inode = base->d_inode;
17111da177e4SLinus Torvalds 	struct dentry *dentry;
17121da177e4SLinus Torvalds 	int err;
17131da177e4SLinus Torvalds 
1714b74c79e9SNick Piggin 	err = exec_permission(inode, 0);
171581fca444SChristoph Hellwig 	if (err)
171681fca444SChristoph Hellwig 		return ERR_PTR(err);
17171da177e4SLinus Torvalds 
17181da177e4SLinus Torvalds 	/*
1719b04f784eSNick Piggin 	 * Don't bother with __d_lookup: callers are for creat as
1720b04f784eSNick Piggin 	 * well as unlink, so a lot of the time it would cost
1721b04f784eSNick Piggin 	 * a double lookup.
17226e6b1bd1SAl Viro 	 */
17236e6b1bd1SAl Viro 	dentry = d_lookup(base, name);
17246e6b1bd1SAl Viro 
172544396f4bSJosef Bacik 	if (dentry && d_need_lookup(dentry)) {
172644396f4bSJosef Bacik 		/*
172744396f4bSJosef Bacik 		 * __lookup_hash is called with the parent dir's i_mutex already
172844396f4bSJosef Bacik 		 * held, so we are good to go here.
172944396f4bSJosef Bacik 		 */
173044396f4bSJosef Bacik 		dentry = d_inode_lookup(base, dentry, nd);
173144396f4bSJosef Bacik 		if (IS_ERR(dentry))
173244396f4bSJosef Bacik 			return dentry;
173344396f4bSJosef Bacik 	}
173444396f4bSJosef Bacik 
1735fb045adbSNick Piggin 	if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
17366e6b1bd1SAl Viro 		dentry = do_revalidate(dentry, nd);
17376e6b1bd1SAl Viro 
17381da177e4SLinus Torvalds 	if (!dentry)
1739baa03890SNick Piggin 		dentry = d_alloc_and_lookup(base, name, nd);
17405a202bcdSAl Viro 
17411da177e4SLinus Torvalds 	return dentry;
17421da177e4SLinus Torvalds }
17431da177e4SLinus Torvalds 
1744057f6c01SJames Morris /*
1745057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1746057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1747057f6c01SJames Morris  * SMP-safe.
1748057f6c01SJames Morris  */
1749a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
17501da177e4SLinus Torvalds {
17514ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
17521da177e4SLinus Torvalds }
17531da177e4SLinus Torvalds 
1754eead1911SChristoph Hellwig /**
1755a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1756eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1757eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1758eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1759eead1911SChristoph Hellwig  *
1760a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1761a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1762eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1763eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1764eead1911SChristoph Hellwig  */
1765057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1766057f6c01SJames Morris {
1767057f6c01SJames Morris 	struct qstr this;
17686a96ba54SAl Viro 	unsigned long hash;
17696a96ba54SAl Viro 	unsigned int c;
1770057f6c01SJames Morris 
17712f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
17722f9092e1SDavid Woodhouse 
17736a96ba54SAl Viro 	this.name = name;
17746a96ba54SAl Viro 	this.len = len;
17756a96ba54SAl Viro 	if (!len)
17766a96ba54SAl Viro 		return ERR_PTR(-EACCES);
17776a96ba54SAl Viro 
17786a96ba54SAl Viro 	hash = init_name_hash();
17796a96ba54SAl Viro 	while (len--) {
17806a96ba54SAl Viro 		c = *(const unsigned char *)name++;
17816a96ba54SAl Viro 		if (c == '/' || c == '\0')
17826a96ba54SAl Viro 			return ERR_PTR(-EACCES);
17836a96ba54SAl Viro 		hash = partial_name_hash(c, hash);
17846a96ba54SAl Viro 	}
17856a96ba54SAl Viro 	this.hash = end_name_hash(hash);
17865a202bcdSAl Viro 	/*
17875a202bcdSAl Viro 	 * See if the low-level filesystem might want
17885a202bcdSAl Viro 	 * to use its own hash..
17895a202bcdSAl Viro 	 */
17905a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
17915a202bcdSAl Viro 		int err = base->d_op->d_hash(base, base->d_inode, &this);
17925a202bcdSAl Viro 		if (err < 0)
17935a202bcdSAl Viro 			return ERR_PTR(err);
17945a202bcdSAl Viro 	}
1795eead1911SChristoph Hellwig 
179649705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1797057f6c01SJames Morris }
1798057f6c01SJames Morris 
17992d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags,
18002d8f3038SAl Viro 		 struct path *path)
18011da177e4SLinus Torvalds {
18022d8f3038SAl Viro 	struct nameidata nd;
1803f52e0c11SAl Viro 	char *tmp = getname_flags(name, flags);
18041da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
18051da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
18062d8f3038SAl Viro 
18072d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
18082d8f3038SAl Viro 
18092d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
18101da177e4SLinus Torvalds 		putname(tmp);
18112d8f3038SAl Viro 		if (!err)
18122d8f3038SAl Viro 			*path = nd.path;
18131da177e4SLinus Torvalds 	}
18141da177e4SLinus Torvalds 	return err;
18151da177e4SLinus Torvalds }
18161da177e4SLinus Torvalds 
18172ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
18182ad94ae6SAl Viro 			struct nameidata *nd, char **name)
18192ad94ae6SAl Viro {
18202ad94ae6SAl Viro 	char *s = getname(path);
18212ad94ae6SAl Viro 	int error;
18222ad94ae6SAl Viro 
18232ad94ae6SAl Viro 	if (IS_ERR(s))
18242ad94ae6SAl Viro 		return PTR_ERR(s);
18252ad94ae6SAl Viro 
18262ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
18272ad94ae6SAl Viro 	if (error)
18282ad94ae6SAl Viro 		putname(s);
18292ad94ae6SAl Viro 	else
18302ad94ae6SAl Viro 		*name = s;
18312ad94ae6SAl Viro 
18322ad94ae6SAl Viro 	return error;
18332ad94ae6SAl Viro }
18342ad94ae6SAl Viro 
18351da177e4SLinus Torvalds /*
18361da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
18371da177e4SLinus Torvalds  * minimal.
18381da177e4SLinus Torvalds  */
18391da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
18401da177e4SLinus Torvalds {
1841da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1842da9592edSDavid Howells 
18431da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
18441da177e4SLinus Torvalds 		return 0;
1845e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
1846e795b717SSerge E. Hallyn 		goto other_userns;
1847da9592edSDavid Howells 	if (inode->i_uid == fsuid)
18481da177e4SLinus Torvalds 		return 0;
1849da9592edSDavid Howells 	if (dir->i_uid == fsuid)
18501da177e4SLinus Torvalds 		return 0;
1851e795b717SSerge E. Hallyn 
1852e795b717SSerge E. Hallyn other_userns:
1853e795b717SSerge E. Hallyn 	return !ns_capable(inode_userns(inode), CAP_FOWNER);
18541da177e4SLinus Torvalds }
18551da177e4SLinus Torvalds 
18561da177e4SLinus Torvalds /*
18571da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
18581da177e4SLinus Torvalds  *  whether the type of victim is right.
18591da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
18601da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
18611da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
18621da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
18631da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
18641da177e4SLinus Torvalds  *	a. be owner of dir, or
18651da177e4SLinus Torvalds  *	b. be owner of victim, or
18661da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
18671da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
18681da177e4SLinus Torvalds  *     links pointing to it.
18691da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
18701da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
18711da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
18721da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
18731da177e4SLinus Torvalds  *     nfs_async_unlink().
18741da177e4SLinus Torvalds  */
1875858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
18761da177e4SLinus Torvalds {
18771da177e4SLinus Torvalds 	int error;
18781da177e4SLinus Torvalds 
18791da177e4SLinus Torvalds 	if (!victim->d_inode)
18801da177e4SLinus Torvalds 		return -ENOENT;
18811da177e4SLinus Torvalds 
18821da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
1883cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
18841da177e4SLinus Torvalds 
1885f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
18861da177e4SLinus Torvalds 	if (error)
18871da177e4SLinus Torvalds 		return error;
18881da177e4SLinus Torvalds 	if (IS_APPEND(dir))
18891da177e4SLinus Torvalds 		return -EPERM;
18901da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1891f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
18921da177e4SLinus Torvalds 		return -EPERM;
18931da177e4SLinus Torvalds 	if (isdir) {
18941da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
18951da177e4SLinus Torvalds 			return -ENOTDIR;
18961da177e4SLinus Torvalds 		if (IS_ROOT(victim))
18971da177e4SLinus Torvalds 			return -EBUSY;
18981da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
18991da177e4SLinus Torvalds 		return -EISDIR;
19001da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
19011da177e4SLinus Torvalds 		return -ENOENT;
19021da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
19031da177e4SLinus Torvalds 		return -EBUSY;
19041da177e4SLinus Torvalds 	return 0;
19051da177e4SLinus Torvalds }
19061da177e4SLinus Torvalds 
19071da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
19081da177e4SLinus Torvalds  *  dir.
19091da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
19101da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
19111da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
19121da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
19131da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
19141da177e4SLinus Torvalds  */
1915a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
19161da177e4SLinus Torvalds {
19171da177e4SLinus Torvalds 	if (child->d_inode)
19181da177e4SLinus Torvalds 		return -EEXIST;
19191da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
19201da177e4SLinus Torvalds 		return -ENOENT;
1921f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
19221da177e4SLinus Torvalds }
19231da177e4SLinus Torvalds 
19241da177e4SLinus Torvalds /*
19251da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
19261da177e4SLinus Torvalds  */
19271da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
19281da177e4SLinus Torvalds {
19291da177e4SLinus Torvalds 	struct dentry *p;
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds 	if (p1 == p2) {
1932f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
19331da177e4SLinus Torvalds 		return NULL;
19341da177e4SLinus Torvalds 	}
19351da177e4SLinus Torvalds 
1936a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
19371da177e4SLinus Torvalds 
1938e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1939e2761a11SOGAWA Hirofumi 	if (p) {
1940f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1941f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
19421da177e4SLinus Torvalds 		return p;
19431da177e4SLinus Torvalds 	}
19441da177e4SLinus Torvalds 
1945e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1946e2761a11SOGAWA Hirofumi 	if (p) {
1947f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1948f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
19491da177e4SLinus Torvalds 		return p;
19501da177e4SLinus Torvalds 	}
19511da177e4SLinus Torvalds 
1952f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1953f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
19541da177e4SLinus Torvalds 	return NULL;
19551da177e4SLinus Torvalds }
19561da177e4SLinus Torvalds 
19571da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
19581da177e4SLinus Torvalds {
19591b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
19601da177e4SLinus Torvalds 	if (p1 != p2) {
19611b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1962a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
19631da177e4SLinus Torvalds 	}
19641da177e4SLinus Torvalds }
19651da177e4SLinus Torvalds 
19661da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
19671da177e4SLinus Torvalds 		struct nameidata *nd)
19681da177e4SLinus Torvalds {
1969a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
19701da177e4SLinus Torvalds 
19711da177e4SLinus Torvalds 	if (error)
19721da177e4SLinus Torvalds 		return error;
19731da177e4SLinus Torvalds 
1974acfa4380SAl Viro 	if (!dir->i_op->create)
19751da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
19761da177e4SLinus Torvalds 	mode &= S_IALLUGO;
19771da177e4SLinus Torvalds 	mode |= S_IFREG;
19781da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
19791da177e4SLinus Torvalds 	if (error)
19801da177e4SLinus Torvalds 		return error;
19811da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
1982a74574aaSStephen Smalley 	if (!error)
1983f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
19841da177e4SLinus Torvalds 	return error;
19851da177e4SLinus Torvalds }
19861da177e4SLinus Torvalds 
198773d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
19881da177e4SLinus Torvalds {
19893fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
19901da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
19911da177e4SLinus Torvalds 	int error;
19921da177e4SLinus Torvalds 
1993bcda7652SAl Viro 	/* O_PATH? */
1994bcda7652SAl Viro 	if (!acc_mode)
1995bcda7652SAl Viro 		return 0;
1996bcda7652SAl Viro 
19971da177e4SLinus Torvalds 	if (!inode)
19981da177e4SLinus Torvalds 		return -ENOENT;
19991da177e4SLinus Torvalds 
2000c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2001c8fe8f30SChristoph Hellwig 	case S_IFLNK:
20021da177e4SLinus Torvalds 		return -ELOOP;
2003c8fe8f30SChristoph Hellwig 	case S_IFDIR:
2004c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
20051da177e4SLinus Torvalds 			return -EISDIR;
2006c8fe8f30SChristoph Hellwig 		break;
2007c8fe8f30SChristoph Hellwig 	case S_IFBLK:
2008c8fe8f30SChristoph Hellwig 	case S_IFCHR:
20093fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
20101da177e4SLinus Torvalds 			return -EACCES;
2011c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
2012c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2013c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
20141da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2015c8fe8f30SChristoph Hellwig 		break;
20164a3fd211SDave Hansen 	}
2017b41572e9SDave Hansen 
20183fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
2019b41572e9SDave Hansen 	if (error)
2020b41572e9SDave Hansen 		return error;
20216146f0d5SMimi Zohar 
20221da177e4SLinus Torvalds 	/*
20231da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
20241da177e4SLinus Torvalds 	 */
20251da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
20268737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
20277715b521SAl Viro 			return -EPERM;
20281da177e4SLinus Torvalds 		if (flag & O_TRUNC)
20297715b521SAl Viro 			return -EPERM;
20301da177e4SLinus Torvalds 	}
20311da177e4SLinus Torvalds 
20321da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
20332e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
20347715b521SAl Viro 		return -EPERM;
20351da177e4SLinus Torvalds 
20361da177e4SLinus Torvalds 	/*
20371da177e4SLinus Torvalds 	 * Ensure there are no outstanding leases on the file.
20381da177e4SLinus Torvalds 	 */
2039b65a9cfcSAl Viro 	return break_lease(inode, flag);
20407715b521SAl Viro }
20417715b521SAl Viro 
2042e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
20437715b521SAl Viro {
2044e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
20457715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
20467715b521SAl Viro 	int error = get_write_access(inode);
20471da177e4SLinus Torvalds 	if (error)
20487715b521SAl Viro 		return error;
20491da177e4SLinus Torvalds 	/*
20501da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
20511da177e4SLinus Torvalds 	 */
20521da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2053be6d3e56SKentaro Takeda 	if (!error)
2054ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
20551da177e4SLinus Torvalds 	if (!error) {
20567715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2057d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2058e1181ee6SJeff Layton 				    filp);
20591da177e4SLinus Torvalds 	}
20601da177e4SLinus Torvalds 	put_write_access(inode);
2061acd0c935SMimi Zohar 	return error;
20621da177e4SLinus Torvalds }
20631da177e4SLinus Torvalds 
2064d57999e1SDave Hansen /*
2065d57999e1SDave Hansen  * Note that while the flag value (low two bits) for sys_open means:
2066d57999e1SDave Hansen  *	00 - read-only
2067d57999e1SDave Hansen  *	01 - write-only
2068d57999e1SDave Hansen  *	10 - read-write
2069d57999e1SDave Hansen  *	11 - special
2070d57999e1SDave Hansen  * it is changed into
2071d57999e1SDave Hansen  *	00 - no permissions needed
2072d57999e1SDave Hansen  *	01 - read-permission
2073d57999e1SDave Hansen  *	10 - write-permission
2074d57999e1SDave Hansen  *	11 - read-write
2075d57999e1SDave Hansen  * for the internal routines (ie open_namei()/follow_link() etc)
2076d57999e1SDave Hansen  * This is more logical, and also allows the 00 "no perm needed"
2077d57999e1SDave Hansen  * to be used for symlinks (where the permissions are checked
2078d57999e1SDave Hansen  * later).
2079d57999e1SDave Hansen  *
2080d57999e1SDave Hansen */
2081d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2082d57999e1SDave Hansen {
2083d57999e1SDave Hansen 	if ((flag+1) & O_ACCMODE)
2084d57999e1SDave Hansen 		flag++;
2085d57999e1SDave Hansen 	return flag;
2086d57999e1SDave Hansen }
2087d57999e1SDave Hansen 
208831e6b01fSNick Piggin /*
2089fe2d35ffSAl Viro  * Handle the last step of open()
209031e6b01fSNick Piggin  */
2091fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
2092c3e380b0SAl Viro 			    const struct open_flags *op, const char *pathname)
2093fb1cc555SAl Viro {
2094a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
20956c0d46c4SAl Viro 	struct dentry *dentry;
2096ca344a89SAl Viro 	int open_flag = op->open_flag;
20976c0d46c4SAl Viro 	int will_truncate = open_flag & O_TRUNC;
2098ca344a89SAl Viro 	int want_write = 0;
2099bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2100fb1cc555SAl Viro 	struct file *filp;
210116c2cd71SAl Viro 	int error;
2102fb1cc555SAl Viro 
2103c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2104c3e380b0SAl Viro 	nd->flags |= op->intent;
2105c3e380b0SAl Viro 
21061f36f774SAl Viro 	switch (nd->last_type) {
21071f36f774SAl Viro 	case LAST_DOTDOT:
2108176306f5SNeil Brown 	case LAST_DOT:
2109fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2110fe2d35ffSAl Viro 		if (error)
2111fe2d35ffSAl Viro 			return ERR_PTR(error);
21121f36f774SAl Viro 		/* fallthrough */
21131f36f774SAl Viro 	case LAST_ROOT:
21149f1fafeeSAl Viro 		error = complete_walk(nd);
211516c2cd71SAl Viro 		if (error)
21169f1fafeeSAl Viro 			return ERR_PTR(error);
2117fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2118ca344a89SAl Viro 		if (open_flag & O_CREAT) {
211916c2cd71SAl Viro 			error = -EISDIR;
21201f36f774SAl Viro 			goto exit;
2121fe2d35ffSAl Viro 		}
2122fe2d35ffSAl Viro 		goto ok;
21231f36f774SAl Viro 	case LAST_BIND:
21249f1fafeeSAl Viro 		error = complete_walk(nd);
212516c2cd71SAl Viro 		if (error)
21269f1fafeeSAl Viro 			return ERR_PTR(error);
21271f36f774SAl Viro 		audit_inode(pathname, dir);
21281f36f774SAl Viro 		goto ok;
21291f36f774SAl Viro 	}
2130a2c36b45SAl Viro 
2131ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2132bcda7652SAl Viro 		int symlink_ok = 0;
2133fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2134fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2135bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2136bcda7652SAl Viro 			symlink_ok = 1;
2137fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2138ce57dfc1SAl Viro 		error = walk_component(nd, path, &nd->last, LAST_NORM,
2139ce57dfc1SAl Viro 					!symlink_ok);
2140ce57dfc1SAl Viro 		if (error < 0)
2141fe2d35ffSAl Viro 			return ERR_PTR(error);
2142ce57dfc1SAl Viro 		if (error) /* symlink */
2143fe2d35ffSAl Viro 			return NULL;
2144fe2d35ffSAl Viro 		/* sayonara */
21459f1fafeeSAl Viro 		error = complete_walk(nd);
21469f1fafeeSAl Viro 		if (error)
2147fe2d35ffSAl Viro 			return ERR_PTR(-ECHILD);
2148fe2d35ffSAl Viro 
2149fe2d35ffSAl Viro 		error = -ENOTDIR;
2150fe2d35ffSAl Viro 		if (nd->flags & LOOKUP_DIRECTORY) {
2151ce57dfc1SAl Viro 			if (!nd->inode->i_op->lookup)
2152fe2d35ffSAl Viro 				goto exit;
2153fe2d35ffSAl Viro 		}
2154fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2155fe2d35ffSAl Viro 		goto ok;
2156fe2d35ffSAl Viro 	}
2157fe2d35ffSAl Viro 
2158fe2d35ffSAl Viro 	/* create side of things */
21599f1fafeeSAl Viro 	error = complete_walk(nd);
21609f1fafeeSAl Viro 	if (error)
21619f1fafeeSAl Viro 		return ERR_PTR(error);
2162fe2d35ffSAl Viro 
2163fe2d35ffSAl Viro 	audit_inode(pathname, dir);
216416c2cd71SAl Viro 	error = -EISDIR;
21651f36f774SAl Viro 	/* trailing slashes? */
216631e6b01fSNick Piggin 	if (nd->last.name[nd->last.len])
21671f36f774SAl Viro 		goto exit;
21681f36f774SAl Viro 
2169a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
2170a1e28038SAl Viro 
21716c0d46c4SAl Viro 	dentry = lookup_hash(nd);
21726c0d46c4SAl Viro 	error = PTR_ERR(dentry);
21736c0d46c4SAl Viro 	if (IS_ERR(dentry)) {
2174fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
2175fb1cc555SAl Viro 		goto exit;
2176fb1cc555SAl Viro 	}
2177fb1cc555SAl Viro 
21786c0d46c4SAl Viro 	path->dentry = dentry;
21796c0d46c4SAl Viro 	path->mnt = nd->path.mnt;
21806c0d46c4SAl Viro 
2181fb1cc555SAl Viro 	/* Negative dentry, just create the file */
21826c0d46c4SAl Viro 	if (!dentry->d_inode) {
21836c0d46c4SAl Viro 		int mode = op->mode;
21846c0d46c4SAl Viro 		if (!IS_POSIXACL(dir->d_inode))
21856c0d46c4SAl Viro 			mode &= ~current_umask();
2186fb1cc555SAl Viro 		/*
2187fb1cc555SAl Viro 		 * This write is needed to ensure that a
21886c0d46c4SAl Viro 		 * rw->ro transition does not occur between
2189fb1cc555SAl Viro 		 * the time when the file is created and when
2190fb1cc555SAl Viro 		 * a permanent write count is taken through
2191fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
2192fb1cc555SAl Viro 		 */
2193fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
2194fb1cc555SAl Viro 		if (error)
2195fb1cc555SAl Viro 			goto exit_mutex_unlock;
2196ca344a89SAl Viro 		want_write = 1;
21979b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2198ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
21996c0d46c4SAl Viro 		will_truncate = 0;
2200bcda7652SAl Viro 		acc_mode = MAY_OPEN;
22016c0d46c4SAl Viro 		error = security_path_mknod(&nd->path, dentry, mode, 0);
22026c0d46c4SAl Viro 		if (error)
22036c0d46c4SAl Viro 			goto exit_mutex_unlock;
22046c0d46c4SAl Viro 		error = vfs_create(dir->d_inode, dentry, mode, nd);
22056c0d46c4SAl Viro 		if (error)
22066c0d46c4SAl Viro 			goto exit_mutex_unlock;
22076c0d46c4SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
22086c0d46c4SAl Viro 		dput(nd->path.dentry);
22096c0d46c4SAl Viro 		nd->path.dentry = dentry;
2210ca344a89SAl Viro 		goto common;
2211fb1cc555SAl Viro 	}
2212fb1cc555SAl Viro 
2213fb1cc555SAl Viro 	/*
2214fb1cc555SAl Viro 	 * It already exists.
2215fb1cc555SAl Viro 	 */
2216fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2217fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2218fb1cc555SAl Viro 
2219fb1cc555SAl Viro 	error = -EEXIST;
2220ca344a89SAl Viro 	if (open_flag & O_EXCL)
2221fb1cc555SAl Viro 		goto exit_dput;
2222fb1cc555SAl Viro 
22239875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
22249875cf80SDavid Howells 	if (error < 0)
2225fb1cc555SAl Viro 		goto exit_dput;
2226fb1cc555SAl Viro 
2227fb1cc555SAl Viro 	error = -ENOENT;
2228fb1cc555SAl Viro 	if (!path->dentry->d_inode)
2229fb1cc555SAl Viro 		goto exit_dput;
22309e67f361SAl Viro 
22319e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
2232fb1cc555SAl Viro 		return NULL;
2233fb1cc555SAl Viro 
2234fb1cc555SAl Viro 	path_to_nameidata(path, nd);
223531e6b01fSNick Piggin 	nd->inode = path->dentry->d_inode;
2236fb1cc555SAl Viro 	error = -EISDIR;
223731e6b01fSNick Piggin 	if (S_ISDIR(nd->inode->i_mode))
2238fb1cc555SAl Viro 		goto exit;
223967ee3ad2SAl Viro ok:
22406c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
22416c0d46c4SAl Viro 		will_truncate = 0;
22426c0d46c4SAl Viro 
22430f9d1a10SAl Viro 	if (will_truncate) {
22440f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
22450f9d1a10SAl Viro 		if (error)
22460f9d1a10SAl Viro 			goto exit;
2247ca344a89SAl Viro 		want_write = 1;
22480f9d1a10SAl Viro 	}
2249ca344a89SAl Viro common:
2250bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2251ca344a89SAl Viro 	if (error)
22520f9d1a10SAl Viro 		goto exit;
22530f9d1a10SAl Viro 	filp = nameidata_to_filp(nd);
22540f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
22550f9d1a10SAl Viro 		error = ima_file_check(filp, op->acc_mode);
22560f9d1a10SAl Viro 		if (error) {
22570f9d1a10SAl Viro 			fput(filp);
22580f9d1a10SAl Viro 			filp = ERR_PTR(error);
22590f9d1a10SAl Viro 		}
22600f9d1a10SAl Viro 	}
22610f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
22620f9d1a10SAl Viro 		if (will_truncate) {
22630f9d1a10SAl Viro 			error = handle_truncate(filp);
22640f9d1a10SAl Viro 			if (error) {
22650f9d1a10SAl Viro 				fput(filp);
22660f9d1a10SAl Viro 				filp = ERR_PTR(error);
22670f9d1a10SAl Viro 			}
22680f9d1a10SAl Viro 		}
22690f9d1a10SAl Viro 	}
2270ca344a89SAl Viro out:
2271ca344a89SAl Viro 	if (want_write)
22720f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
22730f9d1a10SAl Viro 	path_put(&nd->path);
2274fb1cc555SAl Viro 	return filp;
2275fb1cc555SAl Viro 
2276fb1cc555SAl Viro exit_mutex_unlock:
2277fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2278fb1cc555SAl Viro exit_dput:
2279fb1cc555SAl Viro 	path_put_conditional(path, nd);
2280fb1cc555SAl Viro exit:
2281ca344a89SAl Viro 	filp = ERR_PTR(error);
2282ca344a89SAl Viro 	goto out;
2283fb1cc555SAl Viro }
2284fb1cc555SAl Viro 
228513aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname,
228673d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
22871da177e4SLinus Torvalds {
2288fe2d35ffSAl Viro 	struct file *base = NULL;
22894a3fd211SDave Hansen 	struct file *filp;
22909850c056SAl Viro 	struct path path;
229113aab428SAl Viro 	int error;
229231e6b01fSNick Piggin 
229331e6b01fSNick Piggin 	filp = get_empty_filp();
229431e6b01fSNick Piggin 	if (!filp)
229531e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
229631e6b01fSNick Piggin 
229747c805dcSAl Viro 	filp->f_flags = op->open_flag;
229873d049a4SAl Viro 	nd->intent.open.file = filp;
229973d049a4SAl Viro 	nd->intent.open.flags = open_to_namei_flags(op->open_flag);
230073d049a4SAl Viro 	nd->intent.open.create_mode = op->mode;
230131e6b01fSNick Piggin 
230273d049a4SAl Viro 	error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
230331e6b01fSNick Piggin 	if (unlikely(error))
230413aab428SAl Viro 		goto out_filp;
230531e6b01fSNick Piggin 
2306fe2d35ffSAl Viro 	current->total_link_count = 0;
230773d049a4SAl Viro 	error = link_path_walk(pathname, nd);
230831e6b01fSNick Piggin 	if (unlikely(error))
230931e6b01fSNick Piggin 		goto out_filp;
23101da177e4SLinus Torvalds 
231173d049a4SAl Viro 	filp = do_last(nd, &path, op, pathname);
2312806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
23137b9337aaSNick Piggin 		struct path link = path;
2314def4af30SAl Viro 		void *cookie;
2315574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
231673d049a4SAl Viro 			path_put_conditional(&path, nd);
231773d049a4SAl Viro 			path_put(&nd->path);
231840b39136SAl Viro 			filp = ERR_PTR(-ELOOP);
231940b39136SAl Viro 			break;
232040b39136SAl Viro 		}
232173d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
232273d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2323574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
2324c3e380b0SAl Viro 		if (unlikely(error))
2325f1afe9efSAl Viro 			filp = ERR_PTR(error);
2326c3e380b0SAl Viro 		else
232773d049a4SAl Viro 			filp = do_last(nd, &path, op, pathname);
2328574197e0SAl Viro 		put_link(nd, &link, cookie);
2329806b681cSAl Viro 	}
233010fa8e62SAl Viro out:
233173d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
233273d049a4SAl Viro 		path_put(&nd->root);
2333fe2d35ffSAl Viro 	if (base)
2334fe2d35ffSAl Viro 		fput(base);
233573d049a4SAl Viro 	release_open_intent(nd);
233610fa8e62SAl Viro 	return filp;
23371da177e4SLinus Torvalds 
233831e6b01fSNick Piggin out_filp:
233910fa8e62SAl Viro 	filp = ERR_PTR(error);
234010fa8e62SAl Viro 	goto out;
2341de459215SKirill Korotaev }
23421da177e4SLinus Torvalds 
234313aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname,
234413aab428SAl Viro 		const struct open_flags *op, int flags)
234513aab428SAl Viro {
234673d049a4SAl Viro 	struct nameidata nd;
234713aab428SAl Viro 	struct file *filp;
234813aab428SAl Viro 
234973d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
235013aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
235173d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
235213aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
235373d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
235413aab428SAl Viro 	return filp;
235513aab428SAl Viro }
235613aab428SAl Viro 
235773d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
235873d049a4SAl Viro 		const char *name, const struct open_flags *op, int flags)
235973d049a4SAl Viro {
236073d049a4SAl Viro 	struct nameidata nd;
236173d049a4SAl Viro 	struct file *file;
236273d049a4SAl Viro 
236373d049a4SAl Viro 	nd.root.mnt = mnt;
236473d049a4SAl Viro 	nd.root.dentry = dentry;
236573d049a4SAl Viro 
236673d049a4SAl Viro 	flags |= LOOKUP_ROOT;
236773d049a4SAl Viro 
2368bcda7652SAl Viro 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
236973d049a4SAl Viro 		return ERR_PTR(-ELOOP);
237073d049a4SAl Viro 
237173d049a4SAl Viro 	file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
237273d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
237373d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags);
237473d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
237573d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
237673d049a4SAl Viro 	return file;
237773d049a4SAl Viro }
237873d049a4SAl Viro 
23791da177e4SLinus Torvalds /**
23801da177e4SLinus Torvalds  * lookup_create - lookup a dentry, creating it if it doesn't exist
23811da177e4SLinus Torvalds  * @nd: nameidata info
23821da177e4SLinus Torvalds  * @is_dir: directory flag
23831da177e4SLinus Torvalds  *
23841da177e4SLinus Torvalds  * Simple function to lookup and return a dentry and create it
23851da177e4SLinus Torvalds  * if it doesn't exist.  Is SMP-safe.
2386c663e5d8SChristoph Hellwig  *
23874ac91378SJan Blunck  * Returns with nd->path.dentry->d_inode->i_mutex locked.
23881da177e4SLinus Torvalds  */
23891da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir)
23901da177e4SLinus Torvalds {
2391c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
23921da177e4SLinus Torvalds 
23934ac91378SJan Blunck 	mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2394c663e5d8SChristoph Hellwig 	/*
2395c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2396c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2397c663e5d8SChristoph Hellwig 	 */
23981da177e4SLinus Torvalds 	if (nd->last_type != LAST_NORM)
23991da177e4SLinus Torvalds 		goto fail;
24001da177e4SLinus Torvalds 	nd->flags &= ~LOOKUP_PARENT;
24013516586aSAl Viro 	nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2402a634904aSASANO Masahiro 	nd->intent.open.flags = O_EXCL;
2403c663e5d8SChristoph Hellwig 
2404c663e5d8SChristoph Hellwig 	/*
2405c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2406c663e5d8SChristoph Hellwig 	 */
240749705b77SChristoph Hellwig 	dentry = lookup_hash(nd);
24081da177e4SLinus Torvalds 	if (IS_ERR(dentry))
24091da177e4SLinus Torvalds 		goto fail;
2410c663e5d8SChristoph Hellwig 
2411e9baf6e5SAl Viro 	if (dentry->d_inode)
2412e9baf6e5SAl Viro 		goto eexist;
2413c663e5d8SChristoph Hellwig 	/*
2414c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2415c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2416c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2417c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2418c663e5d8SChristoph Hellwig 	 */
2419e9baf6e5SAl Viro 	if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
24201da177e4SLinus Torvalds 		dput(dentry);
24211da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2422e9baf6e5SAl Viro 	}
2423e9baf6e5SAl Viro 	return dentry;
2424e9baf6e5SAl Viro eexist:
2425e9baf6e5SAl Viro 	dput(dentry);
2426e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
24271da177e4SLinus Torvalds fail:
24281da177e4SLinus Torvalds 	return dentry;
24291da177e4SLinus Torvalds }
2430f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create);
24311da177e4SLinus Torvalds 
24321da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
24331da177e4SLinus Torvalds {
2434a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
24351da177e4SLinus Torvalds 
24361da177e4SLinus Torvalds 	if (error)
24371da177e4SLinus Torvalds 		return error;
24381da177e4SLinus Torvalds 
2439e795b717SSerge E. Hallyn 	if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2440e795b717SSerge E. Hallyn 	    !ns_capable(inode_userns(dir), CAP_MKNOD))
24411da177e4SLinus Torvalds 		return -EPERM;
24421da177e4SLinus Torvalds 
2443acfa4380SAl Viro 	if (!dir->i_op->mknod)
24441da177e4SLinus Torvalds 		return -EPERM;
24451da177e4SLinus Torvalds 
244608ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
244708ce5f16SSerge E. Hallyn 	if (error)
244808ce5f16SSerge E. Hallyn 		return error;
244908ce5f16SSerge E. Hallyn 
24501da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
24511da177e4SLinus Torvalds 	if (error)
24521da177e4SLinus Torvalds 		return error;
24531da177e4SLinus Torvalds 
24541da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2455a74574aaSStephen Smalley 	if (!error)
2456f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
24571da177e4SLinus Torvalds 	return error;
24581da177e4SLinus Torvalds }
24591da177e4SLinus Torvalds 
2460463c3197SDave Hansen static int may_mknod(mode_t mode)
2461463c3197SDave Hansen {
2462463c3197SDave Hansen 	switch (mode & S_IFMT) {
2463463c3197SDave Hansen 	case S_IFREG:
2464463c3197SDave Hansen 	case S_IFCHR:
2465463c3197SDave Hansen 	case S_IFBLK:
2466463c3197SDave Hansen 	case S_IFIFO:
2467463c3197SDave Hansen 	case S_IFSOCK:
2468463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2469463c3197SDave Hansen 		return 0;
2470463c3197SDave Hansen 	case S_IFDIR:
2471463c3197SDave Hansen 		return -EPERM;
2472463c3197SDave Hansen 	default:
2473463c3197SDave Hansen 		return -EINVAL;
2474463c3197SDave Hansen 	}
2475463c3197SDave Hansen }
2476463c3197SDave Hansen 
24772e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
24782e4d0924SHeiko Carstens 		unsigned, dev)
24791da177e4SLinus Torvalds {
24802ad94ae6SAl Viro 	int error;
24811da177e4SLinus Torvalds 	char *tmp;
24821da177e4SLinus Torvalds 	struct dentry *dentry;
24831da177e4SLinus Torvalds 	struct nameidata nd;
24841da177e4SLinus Torvalds 
24851da177e4SLinus Torvalds 	if (S_ISDIR(mode))
24861da177e4SLinus Torvalds 		return -EPERM;
24871da177e4SLinus Torvalds 
24882ad94ae6SAl Viro 	error = user_path_parent(dfd, filename, &nd, &tmp);
24891da177e4SLinus Torvalds 	if (error)
24902ad94ae6SAl Viro 		return error;
24912ad94ae6SAl Viro 
24921da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
2493463c3197SDave Hansen 	if (IS_ERR(dentry)) {
24941da177e4SLinus Torvalds 		error = PTR_ERR(dentry);
2495463c3197SDave Hansen 		goto out_unlock;
2496463c3197SDave Hansen 	}
24974ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2498ce3b0f8dSAl Viro 		mode &= ~current_umask();
2499463c3197SDave Hansen 	error = may_mknod(mode);
2500463c3197SDave Hansen 	if (error)
2501463c3197SDave Hansen 		goto out_dput;
2502463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2503463c3197SDave Hansen 	if (error)
2504463c3197SDave Hansen 		goto out_dput;
2505be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd.path, dentry, mode, dev);
2506be6d3e56SKentaro Takeda 	if (error)
2507be6d3e56SKentaro Takeda 		goto out_drop_write;
25081da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
25091da177e4SLinus Torvalds 		case 0: case S_IFREG:
25104ac91378SJan Blunck 			error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
25111da177e4SLinus Torvalds 			break;
25121da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
25134ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
25141da177e4SLinus Torvalds 					new_decode_dev(dev));
25151da177e4SLinus Torvalds 			break;
25161da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
25174ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
25181da177e4SLinus Torvalds 			break;
25191da177e4SLinus Torvalds 	}
2520be6d3e56SKentaro Takeda out_drop_write:
2521463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2522463c3197SDave Hansen out_dput:
25231da177e4SLinus Torvalds 	dput(dentry);
2524463c3197SDave Hansen out_unlock:
25254ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
25261d957f9bSJan Blunck 	path_put(&nd.path);
25271da177e4SLinus Torvalds 	putname(tmp);
25281da177e4SLinus Torvalds 
25291da177e4SLinus Torvalds 	return error;
25301da177e4SLinus Torvalds }
25311da177e4SLinus Torvalds 
25323480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
25335590ff0dSUlrich Drepper {
25345590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
25355590ff0dSUlrich Drepper }
25365590ff0dSUlrich Drepper 
25371da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
25381da177e4SLinus Torvalds {
2539a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
25401da177e4SLinus Torvalds 
25411da177e4SLinus Torvalds 	if (error)
25421da177e4SLinus Torvalds 		return error;
25431da177e4SLinus Torvalds 
2544acfa4380SAl Viro 	if (!dir->i_op->mkdir)
25451da177e4SLinus Torvalds 		return -EPERM;
25461da177e4SLinus Torvalds 
25471da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
25481da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
25491da177e4SLinus Torvalds 	if (error)
25501da177e4SLinus Torvalds 		return error;
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2553a74574aaSStephen Smalley 	if (!error)
2554f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
25551da177e4SLinus Torvalds 	return error;
25561da177e4SLinus Torvalds }
25571da177e4SLinus Torvalds 
25582e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
25591da177e4SLinus Torvalds {
25601da177e4SLinus Torvalds 	int error = 0;
25611da177e4SLinus Torvalds 	char * tmp;
25626902d925SDave Hansen 	struct dentry *dentry;
25636902d925SDave Hansen 	struct nameidata nd;
25641da177e4SLinus Torvalds 
25652ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &tmp);
25662ad94ae6SAl Viro 	if (error)
25676902d925SDave Hansen 		goto out_err;
25681da177e4SLinus Torvalds 
25691da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 1);
25701da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
25716902d925SDave Hansen 	if (IS_ERR(dentry))
25726902d925SDave Hansen 		goto out_unlock;
25736902d925SDave Hansen 
25744ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
2575ce3b0f8dSAl Viro 		mode &= ~current_umask();
2576463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2577463c3197SDave Hansen 	if (error)
2578463c3197SDave Hansen 		goto out_dput;
2579be6d3e56SKentaro Takeda 	error = security_path_mkdir(&nd.path, dentry, mode);
2580be6d3e56SKentaro Takeda 	if (error)
2581be6d3e56SKentaro Takeda 		goto out_drop_write;
25824ac91378SJan Blunck 	error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2583be6d3e56SKentaro Takeda out_drop_write:
2584463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2585463c3197SDave Hansen out_dput:
25861da177e4SLinus Torvalds 	dput(dentry);
25876902d925SDave Hansen out_unlock:
25884ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
25891d957f9bSJan Blunck 	path_put(&nd.path);
25901da177e4SLinus Torvalds 	putname(tmp);
25916902d925SDave Hansen out_err:
25921da177e4SLinus Torvalds 	return error;
25931da177e4SLinus Torvalds }
25941da177e4SLinus Torvalds 
25953cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
25965590ff0dSUlrich Drepper {
25975590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
25985590ff0dSUlrich Drepper }
25995590ff0dSUlrich Drepper 
26001da177e4SLinus Torvalds /*
2601a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
2602a71905f0SSage Weil  * should have a usage count of 2 if we're the only user of this
2603a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
2604a71905f0SSage Weil  * then we drop the dentry now.
26051da177e4SLinus Torvalds  *
26061da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
26071da177e4SLinus Torvalds  * do a
26081da177e4SLinus Torvalds  *
26091da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
26101da177e4SLinus Torvalds  *		return -EBUSY;
26111da177e4SLinus Torvalds  *
26121da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
26131da177e4SLinus Torvalds  * that is still in use by something else..
26141da177e4SLinus Torvalds  */
26151da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
26161da177e4SLinus Torvalds {
26171da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
26181da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
261964252c75SSage Weil 	if (dentry->d_count == 1)
26201da177e4SLinus Torvalds 		__d_drop(dentry);
26211da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
26221da177e4SLinus Torvalds }
26231da177e4SLinus Torvalds 
26241da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
26251da177e4SLinus Torvalds {
26261da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
26271da177e4SLinus Torvalds 
26281da177e4SLinus Torvalds 	if (error)
26291da177e4SLinus Torvalds 		return error;
26301da177e4SLinus Torvalds 
2631acfa4380SAl Viro 	if (!dir->i_op->rmdir)
26321da177e4SLinus Torvalds 		return -EPERM;
26331da177e4SLinus Torvalds 
26341b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
2635912dbc15SSage Weil 
26361da177e4SLinus Torvalds 	error = -EBUSY;
2637912dbc15SSage Weil 	if (d_mountpoint(dentry))
2638912dbc15SSage Weil 		goto out;
2639912dbc15SSage Weil 
26401da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
2641912dbc15SSage Weil 	if (error)
2642912dbc15SSage Weil 		goto out;
2643912dbc15SSage Weil 
26443cebde24SSage Weil 	shrink_dcache_parent(dentry);
26451da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
2646912dbc15SSage Weil 	if (error)
2647912dbc15SSage Weil 		goto out;
2648912dbc15SSage Weil 
26491da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
2650d83c49f3SAl Viro 	dont_mount(dentry);
26511da177e4SLinus Torvalds 
2652912dbc15SSage Weil out:
2653912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
2654912dbc15SSage Weil 	if (!error)
2655912dbc15SSage Weil 		d_delete(dentry);
26561da177e4SLinus Torvalds 	return error;
26571da177e4SLinus Torvalds }
26581da177e4SLinus Torvalds 
26595590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
26601da177e4SLinus Torvalds {
26611da177e4SLinus Torvalds 	int error = 0;
26621da177e4SLinus Torvalds 	char * name;
26631da177e4SLinus Torvalds 	struct dentry *dentry;
26641da177e4SLinus Torvalds 	struct nameidata nd;
26651da177e4SLinus Torvalds 
26662ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
26671da177e4SLinus Torvalds 	if (error)
26682ad94ae6SAl Viro 		return error;
26691da177e4SLinus Torvalds 
26701da177e4SLinus Torvalds 	switch(nd.last_type) {
26711da177e4SLinus Torvalds 	case LAST_DOTDOT:
26721da177e4SLinus Torvalds 		error = -ENOTEMPTY;
26731da177e4SLinus Torvalds 		goto exit1;
26741da177e4SLinus Torvalds 	case LAST_DOT:
26751da177e4SLinus Torvalds 		error = -EINVAL;
26761da177e4SLinus Torvalds 		goto exit1;
26771da177e4SLinus Torvalds 	case LAST_ROOT:
26781da177e4SLinus Torvalds 		error = -EBUSY;
26791da177e4SLinus Torvalds 		goto exit1;
26801da177e4SLinus Torvalds 	}
26810612d9fbSOGAWA Hirofumi 
26820612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
26830612d9fbSOGAWA Hirofumi 
26844ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
268549705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
26861da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
26876902d925SDave Hansen 	if (IS_ERR(dentry))
26886902d925SDave Hansen 		goto exit2;
2689e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
2690e6bc45d6STheodore Ts'o 		error = -ENOENT;
2691e6bc45d6STheodore Ts'o 		goto exit3;
2692e6bc45d6STheodore Ts'o 	}
26930622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
26940622753bSDave Hansen 	if (error)
26950622753bSDave Hansen 		goto exit3;
2696be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2697be6d3e56SKentaro Takeda 	if (error)
2698be6d3e56SKentaro Takeda 		goto exit4;
26994ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2700be6d3e56SKentaro Takeda exit4:
27010622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
27020622753bSDave Hansen exit3:
27031da177e4SLinus Torvalds 	dput(dentry);
27046902d925SDave Hansen exit2:
27054ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
27061da177e4SLinus Torvalds exit1:
27071d957f9bSJan Blunck 	path_put(&nd.path);
27081da177e4SLinus Torvalds 	putname(name);
27091da177e4SLinus Torvalds 	return error;
27101da177e4SLinus Torvalds }
27111da177e4SLinus Torvalds 
27123cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
27135590ff0dSUlrich Drepper {
27145590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
27155590ff0dSUlrich Drepper }
27165590ff0dSUlrich Drepper 
27171da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
27181da177e4SLinus Torvalds {
27191da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
27201da177e4SLinus Torvalds 
27211da177e4SLinus Torvalds 	if (error)
27221da177e4SLinus Torvalds 		return error;
27231da177e4SLinus Torvalds 
2724acfa4380SAl Viro 	if (!dir->i_op->unlink)
27251da177e4SLinus Torvalds 		return -EPERM;
27261da177e4SLinus Torvalds 
27271b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
27281da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
27291da177e4SLinus Torvalds 		error = -EBUSY;
27301da177e4SLinus Torvalds 	else {
27311da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2732bec1052eSAl Viro 		if (!error) {
27331da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2734bec1052eSAl Viro 			if (!error)
2735d83c49f3SAl Viro 				dont_mount(dentry);
2736bec1052eSAl Viro 		}
27371da177e4SLinus Torvalds 	}
27381b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
27391da177e4SLinus Torvalds 
27401da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
27411da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2742ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
27431da177e4SLinus Torvalds 		d_delete(dentry);
27441da177e4SLinus Torvalds 	}
27450eeca283SRobert Love 
27461da177e4SLinus Torvalds 	return error;
27471da177e4SLinus Torvalds }
27481da177e4SLinus Torvalds 
27491da177e4SLinus Torvalds /*
27501da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
27511b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
27521da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
27531da177e4SLinus Torvalds  * while waiting on the I/O.
27541da177e4SLinus Torvalds  */
27555590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
27561da177e4SLinus Torvalds {
27572ad94ae6SAl Viro 	int error;
27581da177e4SLinus Torvalds 	char *name;
27591da177e4SLinus Torvalds 	struct dentry *dentry;
27601da177e4SLinus Torvalds 	struct nameidata nd;
27611da177e4SLinus Torvalds 	struct inode *inode = NULL;
27621da177e4SLinus Torvalds 
27632ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
27641da177e4SLinus Torvalds 	if (error)
27652ad94ae6SAl Viro 		return error;
27662ad94ae6SAl Viro 
27671da177e4SLinus Torvalds 	error = -EISDIR;
27681da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
27691da177e4SLinus Torvalds 		goto exit1;
27700612d9fbSOGAWA Hirofumi 
27710612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
27720612d9fbSOGAWA Hirofumi 
27734ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
277449705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
27751da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
27761da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
27771da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
277850338b88STörök Edwin 		if (nd.last.name[nd.last.len])
277950338b88STörök Edwin 			goto slashes;
27801da177e4SLinus Torvalds 		inode = dentry->d_inode;
278150338b88STörök Edwin 		if (!inode)
2782e6bc45d6STheodore Ts'o 			goto slashes;
27837de9c6eeSAl Viro 		ihold(inode);
27840622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
27850622753bSDave Hansen 		if (error)
27860622753bSDave Hansen 			goto exit2;
2787be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2788be6d3e56SKentaro Takeda 		if (error)
2789be6d3e56SKentaro Takeda 			goto exit3;
27904ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2791be6d3e56SKentaro Takeda exit3:
27920622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
27931da177e4SLinus Torvalds 	exit2:
27941da177e4SLinus Torvalds 		dput(dentry);
27951da177e4SLinus Torvalds 	}
27964ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
27971da177e4SLinus Torvalds 	if (inode)
27981da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
27991da177e4SLinus Torvalds exit1:
28001d957f9bSJan Blunck 	path_put(&nd.path);
28011da177e4SLinus Torvalds 	putname(name);
28021da177e4SLinus Torvalds 	return error;
28031da177e4SLinus Torvalds 
28041da177e4SLinus Torvalds slashes:
28051da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
28061da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
28071da177e4SLinus Torvalds 	goto exit2;
28081da177e4SLinus Torvalds }
28091da177e4SLinus Torvalds 
28102e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
28115590ff0dSUlrich Drepper {
28125590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
28135590ff0dSUlrich Drepper 		return -EINVAL;
28145590ff0dSUlrich Drepper 
28155590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
28165590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
28175590ff0dSUlrich Drepper 
28185590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
28195590ff0dSUlrich Drepper }
28205590ff0dSUlrich Drepper 
28213480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
28225590ff0dSUlrich Drepper {
28235590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
28245590ff0dSUlrich Drepper }
28255590ff0dSUlrich Drepper 
2826db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
28271da177e4SLinus Torvalds {
2828a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
28291da177e4SLinus Torvalds 
28301da177e4SLinus Torvalds 	if (error)
28311da177e4SLinus Torvalds 		return error;
28321da177e4SLinus Torvalds 
2833acfa4380SAl Viro 	if (!dir->i_op->symlink)
28341da177e4SLinus Torvalds 		return -EPERM;
28351da177e4SLinus Torvalds 
28361da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
28371da177e4SLinus Torvalds 	if (error)
28381da177e4SLinus Torvalds 		return error;
28391da177e4SLinus Torvalds 
28401da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2841a74574aaSStephen Smalley 	if (!error)
2842f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
28431da177e4SLinus Torvalds 	return error;
28441da177e4SLinus Torvalds }
28451da177e4SLinus Torvalds 
28462e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
28472e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
28481da177e4SLinus Torvalds {
28492ad94ae6SAl Viro 	int error;
28501da177e4SLinus Torvalds 	char *from;
28511da177e4SLinus Torvalds 	char *to;
28526902d925SDave Hansen 	struct dentry *dentry;
28536902d925SDave Hansen 	struct nameidata nd;
28541da177e4SLinus Torvalds 
28551da177e4SLinus Torvalds 	from = getname(oldname);
28561da177e4SLinus Torvalds 	if (IS_ERR(from))
28571da177e4SLinus Torvalds 		return PTR_ERR(from);
28582ad94ae6SAl Viro 
28592ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
28602ad94ae6SAl Viro 	if (error)
28616902d925SDave Hansen 		goto out_putname;
28621da177e4SLinus Torvalds 
28631da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
28641da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
28656902d925SDave Hansen 	if (IS_ERR(dentry))
28666902d925SDave Hansen 		goto out_unlock;
28676902d925SDave Hansen 
286875c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
286975c3f29dSDave Hansen 	if (error)
287075c3f29dSDave Hansen 		goto out_dput;
2871be6d3e56SKentaro Takeda 	error = security_path_symlink(&nd.path, dentry, from);
2872be6d3e56SKentaro Takeda 	if (error)
2873be6d3e56SKentaro Takeda 		goto out_drop_write;
2874db2e747bSMiklos Szeredi 	error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2875be6d3e56SKentaro Takeda out_drop_write:
287675c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
287775c3f29dSDave Hansen out_dput:
28781da177e4SLinus Torvalds 	dput(dentry);
28796902d925SDave Hansen out_unlock:
28804ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
28811d957f9bSJan Blunck 	path_put(&nd.path);
28821da177e4SLinus Torvalds 	putname(to);
28836902d925SDave Hansen out_putname:
28841da177e4SLinus Torvalds 	putname(from);
28851da177e4SLinus Torvalds 	return error;
28861da177e4SLinus Torvalds }
28871da177e4SLinus Torvalds 
28883480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
28895590ff0dSUlrich Drepper {
28905590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
28915590ff0dSUlrich Drepper }
28925590ff0dSUlrich Drepper 
28931da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
28941da177e4SLinus Torvalds {
28951da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
28961da177e4SLinus Torvalds 	int error;
28971da177e4SLinus Torvalds 
28981da177e4SLinus Torvalds 	if (!inode)
28991da177e4SLinus Torvalds 		return -ENOENT;
29001da177e4SLinus Torvalds 
2901a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
29021da177e4SLinus Torvalds 	if (error)
29031da177e4SLinus Torvalds 		return error;
29041da177e4SLinus Torvalds 
29051da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
29061da177e4SLinus Torvalds 		return -EXDEV;
29071da177e4SLinus Torvalds 
29081da177e4SLinus Torvalds 	/*
29091da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
29101da177e4SLinus Torvalds 	 */
29111da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
29121da177e4SLinus Torvalds 		return -EPERM;
2913acfa4380SAl Viro 	if (!dir->i_op->link)
29141da177e4SLinus Torvalds 		return -EPERM;
29157e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
29161da177e4SLinus Torvalds 		return -EPERM;
29171da177e4SLinus Torvalds 
29181da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
29191da177e4SLinus Torvalds 	if (error)
29201da177e4SLinus Torvalds 		return error;
29211da177e4SLinus Torvalds 
29227e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
2923aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
2924aae8a97dSAneesh Kumar K.V 	if (inode->i_nlink == 0)
2925aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
2926aae8a97dSAneesh Kumar K.V 	else
29271da177e4SLinus Torvalds 		error = dir->i_op->link(old_dentry, dir, new_dentry);
29287e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
2929e31e14ecSStephen Smalley 	if (!error)
29307e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
29311da177e4SLinus Torvalds 	return error;
29321da177e4SLinus Torvalds }
29331da177e4SLinus Torvalds 
29341da177e4SLinus Torvalds /*
29351da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
29361da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
29371da177e4SLinus Torvalds  * newname.  --KAB
29381da177e4SLinus Torvalds  *
29391da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
29401da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
29411da177e4SLinus Torvalds  * and other special files.  --ADM
29421da177e4SLinus Torvalds  */
29432e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
29442e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
29451da177e4SLinus Torvalds {
29461da177e4SLinus Torvalds 	struct dentry *new_dentry;
29472d8f3038SAl Viro 	struct nameidata nd;
29482d8f3038SAl Viro 	struct path old_path;
294911a7b371SAneesh Kumar K.V 	int how = 0;
29501da177e4SLinus Torvalds 	int error;
29511da177e4SLinus Torvalds 	char *to;
29521da177e4SLinus Torvalds 
295311a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
2954c04030e1SUlrich Drepper 		return -EINVAL;
295511a7b371SAneesh Kumar K.V 	/*
295611a7b371SAneesh Kumar K.V 	 * To use null names we require CAP_DAC_READ_SEARCH
295711a7b371SAneesh Kumar K.V 	 * This ensures that not everyone will be able to create
295811a7b371SAneesh Kumar K.V 	 * handlink using the passed filedescriptor.
295911a7b371SAneesh Kumar K.V 	 */
296011a7b371SAneesh Kumar K.V 	if (flags & AT_EMPTY_PATH) {
296111a7b371SAneesh Kumar K.V 		if (!capable(CAP_DAC_READ_SEARCH))
296211a7b371SAneesh Kumar K.V 			return -ENOENT;
296311a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
296411a7b371SAneesh Kumar K.V 	}
2965c04030e1SUlrich Drepper 
296611a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
296711a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
296811a7b371SAneesh Kumar K.V 
296911a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
29701da177e4SLinus Torvalds 	if (error)
29712ad94ae6SAl Viro 		return error;
29722ad94ae6SAl Viro 
29732ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
29741da177e4SLinus Torvalds 	if (error)
29751da177e4SLinus Torvalds 		goto out;
29761da177e4SLinus Torvalds 	error = -EXDEV;
29772d8f3038SAl Viro 	if (old_path.mnt != nd.path.mnt)
29781da177e4SLinus Torvalds 		goto out_release;
29791da177e4SLinus Torvalds 	new_dentry = lookup_create(&nd, 0);
29801da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
29816902d925SDave Hansen 	if (IS_ERR(new_dentry))
29826902d925SDave Hansen 		goto out_unlock;
298375c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
298475c3f29dSDave Hansen 	if (error)
298575c3f29dSDave Hansen 		goto out_dput;
2986be6d3e56SKentaro Takeda 	error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2987be6d3e56SKentaro Takeda 	if (error)
2988be6d3e56SKentaro Takeda 		goto out_drop_write;
29892d8f3038SAl Viro 	error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
2990be6d3e56SKentaro Takeda out_drop_write:
299175c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
299275c3f29dSDave Hansen out_dput:
29931da177e4SLinus Torvalds 	dput(new_dentry);
29946902d925SDave Hansen out_unlock:
29954ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
29961da177e4SLinus Torvalds out_release:
29971d957f9bSJan Blunck 	path_put(&nd.path);
29982ad94ae6SAl Viro 	putname(to);
29991da177e4SLinus Torvalds out:
30002d8f3038SAl Viro 	path_put(&old_path);
30011da177e4SLinus Torvalds 
30021da177e4SLinus Torvalds 	return error;
30031da177e4SLinus Torvalds }
30041da177e4SLinus Torvalds 
30053480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
30065590ff0dSUlrich Drepper {
3007c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
30085590ff0dSUlrich Drepper }
30095590ff0dSUlrich Drepper 
30101da177e4SLinus Torvalds /*
30111da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
30121da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
30131da177e4SLinus Torvalds  * Problems:
30141da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
30151da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
30161da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
3017a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
30181da177e4SLinus Torvalds  *	   story.
30191da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
30201b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
30211da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
30221da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
3023a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
30241da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
30251da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
30261da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
3027a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
30281da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
30291da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
30301da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
3031e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
30321b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
30331da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3034c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
30351da177e4SLinus Torvalds  *	   locking].
30361da177e4SLinus Torvalds  */
303775c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
30381da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
30391da177e4SLinus Torvalds {
30401da177e4SLinus Torvalds 	int error = 0;
30419055cba7SSage Weil 	struct inode *target = new_dentry->d_inode;
30421da177e4SLinus Torvalds 
30431da177e4SLinus Torvalds 	/*
30441da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
30451da177e4SLinus Torvalds 	 * we'll need to flip '..'.
30461da177e4SLinus Torvalds 	 */
30471da177e4SLinus Torvalds 	if (new_dir != old_dir) {
3048f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
30491da177e4SLinus Torvalds 		if (error)
30501da177e4SLinus Torvalds 			return error;
30511da177e4SLinus Torvalds 	}
30521da177e4SLinus Torvalds 
30531da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30541da177e4SLinus Torvalds 	if (error)
30551da177e4SLinus Torvalds 		return error;
30561da177e4SLinus Torvalds 
3057d83c49f3SAl Viro 	if (target)
30581b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
30599055cba7SSage Weil 
30601da177e4SLinus Torvalds 	error = -EBUSY;
30619055cba7SSage Weil 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
30629055cba7SSage Weil 		goto out;
30639055cba7SSage Weil 
30643cebde24SSage Weil 	if (target)
30653cebde24SSage Weil 		shrink_dcache_parent(new_dentry);
30661da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
30679055cba7SSage Weil 	if (error)
30689055cba7SSage Weil 		goto out;
30699055cba7SSage Weil 
30701da177e4SLinus Torvalds 	if (target) {
30711da177e4SLinus Torvalds 		target->i_flags |= S_DEAD;
3072d83c49f3SAl Viro 		dont_mount(new_dentry);
3073d83c49f3SAl Viro 	}
30749055cba7SSage Weil out:
30759055cba7SSage Weil 	if (target)
30761b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
3077e31e14ecSStephen Smalley 	if (!error)
3078349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
30791da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
30801da177e4SLinus Torvalds 	return error;
30811da177e4SLinus Torvalds }
30821da177e4SLinus Torvalds 
308375c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
30841da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
30851da177e4SLinus Torvalds {
308651892bbbSSage Weil 	struct inode *target = new_dentry->d_inode;
30871da177e4SLinus Torvalds 	int error;
30881da177e4SLinus Torvalds 
30891da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
30901da177e4SLinus Torvalds 	if (error)
30911da177e4SLinus Torvalds 		return error;
30921da177e4SLinus Torvalds 
30931da177e4SLinus Torvalds 	dget(new_dentry);
30941da177e4SLinus Torvalds 	if (target)
30951b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
309651892bbbSSage Weil 
30971da177e4SLinus Torvalds 	error = -EBUSY;
309851892bbbSSage Weil 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
309951892bbbSSage Weil 		goto out;
310051892bbbSSage Weil 
31011da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
310251892bbbSSage Weil 	if (error)
310351892bbbSSage Weil 		goto out;
310451892bbbSSage Weil 
3105bec1052eSAl Viro 	if (target)
3106d83c49f3SAl Viro 		dont_mount(new_dentry);
3107349457ccSMark Fasheh 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
31081da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
310951892bbbSSage Weil out:
31101da177e4SLinus Torvalds 	if (target)
31111b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
31121da177e4SLinus Torvalds 	dput(new_dentry);
31131da177e4SLinus Torvalds 	return error;
31141da177e4SLinus Torvalds }
31151da177e4SLinus Torvalds 
31161da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
31171da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
31181da177e4SLinus Torvalds {
31191da177e4SLinus Torvalds 	int error;
31201da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
312159b0df21SEric Paris 	const unsigned char *old_name;
31221da177e4SLinus Torvalds 
31231da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
31241da177e4SLinus Torvalds  		return 0;
31251da177e4SLinus Torvalds 
31261da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
31271da177e4SLinus Torvalds 	if (error)
31281da177e4SLinus Torvalds 		return error;
31291da177e4SLinus Torvalds 
31301da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3131a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
31321da177e4SLinus Torvalds 	else
31331da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
31341da177e4SLinus Torvalds 	if (error)
31351da177e4SLinus Torvalds 		return error;
31361da177e4SLinus Torvalds 
3137acfa4380SAl Viro 	if (!old_dir->i_op->rename)
31381da177e4SLinus Torvalds 		return -EPERM;
31391da177e4SLinus Torvalds 
31400eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
31410eeca283SRobert Love 
31421da177e4SLinus Torvalds 	if (is_dir)
31431da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
31441da177e4SLinus Torvalds 	else
31451da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3146123df294SAl Viro 	if (!error)
3147123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
31485a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
31490eeca283SRobert Love 	fsnotify_oldname_free(old_name);
31500eeca283SRobert Love 
31511da177e4SLinus Torvalds 	return error;
31521da177e4SLinus Torvalds }
31531da177e4SLinus Torvalds 
31542e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
31552e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
31561da177e4SLinus Torvalds {
31571da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
31581da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
31591da177e4SLinus Torvalds 	struct dentry *trap;
31601da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
31612ad94ae6SAl Viro 	char *from;
31622ad94ae6SAl Viro 	char *to;
31632ad94ae6SAl Viro 	int error;
31641da177e4SLinus Torvalds 
31652ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
31661da177e4SLinus Torvalds 	if (error)
31671da177e4SLinus Torvalds 		goto exit;
31681da177e4SLinus Torvalds 
31692ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
31701da177e4SLinus Torvalds 	if (error)
31711da177e4SLinus Torvalds 		goto exit1;
31721da177e4SLinus Torvalds 
31731da177e4SLinus Torvalds 	error = -EXDEV;
31744ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
31751da177e4SLinus Torvalds 		goto exit2;
31761da177e4SLinus Torvalds 
31774ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
31781da177e4SLinus Torvalds 	error = -EBUSY;
31791da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
31801da177e4SLinus Torvalds 		goto exit2;
31811da177e4SLinus Torvalds 
31824ac91378SJan Blunck 	new_dir = newnd.path.dentry;
31831da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
31841da177e4SLinus Torvalds 		goto exit2;
31851da177e4SLinus Torvalds 
31860612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
31870612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
31884e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
31890612d9fbSOGAWA Hirofumi 
31901da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
31911da177e4SLinus Torvalds 
319249705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
31931da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
31941da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
31951da177e4SLinus Torvalds 		goto exit3;
31961da177e4SLinus Torvalds 	/* source must exist */
31971da177e4SLinus Torvalds 	error = -ENOENT;
31981da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
31991da177e4SLinus Torvalds 		goto exit4;
32001da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
32011da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
32021da177e4SLinus Torvalds 		error = -ENOTDIR;
32031da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
32041da177e4SLinus Torvalds 			goto exit4;
32051da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
32061da177e4SLinus Torvalds 			goto exit4;
32071da177e4SLinus Torvalds 	}
32081da177e4SLinus Torvalds 	/* source should not be ancestor of target */
32091da177e4SLinus Torvalds 	error = -EINVAL;
32101da177e4SLinus Torvalds 	if (old_dentry == trap)
32111da177e4SLinus Torvalds 		goto exit4;
321249705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
32131da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
32141da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
32151da177e4SLinus Torvalds 		goto exit4;
32161da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
32171da177e4SLinus Torvalds 	error = -ENOTEMPTY;
32181da177e4SLinus Torvalds 	if (new_dentry == trap)
32191da177e4SLinus Torvalds 		goto exit5;
32201da177e4SLinus Torvalds 
32219079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
32229079b1ebSDave Hansen 	if (error)
32239079b1ebSDave Hansen 		goto exit5;
3224be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3225be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3226be6d3e56SKentaro Takeda 	if (error)
3227be6d3e56SKentaro Takeda 		goto exit6;
32281da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
32291da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3230be6d3e56SKentaro Takeda exit6:
32319079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
32321da177e4SLinus Torvalds exit5:
32331da177e4SLinus Torvalds 	dput(new_dentry);
32341da177e4SLinus Torvalds exit4:
32351da177e4SLinus Torvalds 	dput(old_dentry);
32361da177e4SLinus Torvalds exit3:
32371da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
32381da177e4SLinus Torvalds exit2:
32391d957f9bSJan Blunck 	path_put(&newnd.path);
32402ad94ae6SAl Viro 	putname(to);
32411da177e4SLinus Torvalds exit1:
32421d957f9bSJan Blunck 	path_put(&oldnd.path);
32431da177e4SLinus Torvalds 	putname(from);
32442ad94ae6SAl Viro exit:
32451da177e4SLinus Torvalds 	return error;
32461da177e4SLinus Torvalds }
32471da177e4SLinus Torvalds 
3248a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
32495590ff0dSUlrich Drepper {
32505590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
32515590ff0dSUlrich Drepper }
32525590ff0dSUlrich Drepper 
32531da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
32541da177e4SLinus Torvalds {
32551da177e4SLinus Torvalds 	int len;
32561da177e4SLinus Torvalds 
32571da177e4SLinus Torvalds 	len = PTR_ERR(link);
32581da177e4SLinus Torvalds 	if (IS_ERR(link))
32591da177e4SLinus Torvalds 		goto out;
32601da177e4SLinus Torvalds 
32611da177e4SLinus Torvalds 	len = strlen(link);
32621da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
32631da177e4SLinus Torvalds 		len = buflen;
32641da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
32651da177e4SLinus Torvalds 		len = -EFAULT;
32661da177e4SLinus Torvalds out:
32671da177e4SLinus Torvalds 	return len;
32681da177e4SLinus Torvalds }
32691da177e4SLinus Torvalds 
32701da177e4SLinus Torvalds /*
32711da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
32721da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
32731da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
32741da177e4SLinus Torvalds  */
32751da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
32761da177e4SLinus Torvalds {
32771da177e4SLinus Torvalds 	struct nameidata nd;
3278cc314eefSLinus Torvalds 	void *cookie;
3279694a1764SMarcin Slusarz 	int res;
3280cc314eefSLinus Torvalds 
32811da177e4SLinus Torvalds 	nd.depth = 0;
3282cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3283694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3284694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3285694a1764SMarcin Slusarz 
3286694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
32871da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3288cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3289694a1764SMarcin Slusarz 	return res;
32901da177e4SLinus Torvalds }
32911da177e4SLinus Torvalds 
32921da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
32931da177e4SLinus Torvalds {
32941da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
32951da177e4SLinus Torvalds }
32961da177e4SLinus Torvalds 
32971da177e4SLinus Torvalds /* get the link contents into pagecache */
32981da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
32991da177e4SLinus Torvalds {
3300ebd09abbSDuane Griffin 	char *kaddr;
33011da177e4SLinus Torvalds 	struct page *page;
33021da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3303090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
33041da177e4SLinus Torvalds 	if (IS_ERR(page))
33056fe6900eSNick Piggin 		return (char*)page;
33061da177e4SLinus Torvalds 	*ppage = page;
3307ebd09abbSDuane Griffin 	kaddr = kmap(page);
3308ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3309ebd09abbSDuane Griffin 	return kaddr;
33101da177e4SLinus Torvalds }
33111da177e4SLinus Torvalds 
33121da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
33131da177e4SLinus Torvalds {
33141da177e4SLinus Torvalds 	struct page *page = NULL;
33151da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
33161da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
33171da177e4SLinus Torvalds 	if (page) {
33181da177e4SLinus Torvalds 		kunmap(page);
33191da177e4SLinus Torvalds 		page_cache_release(page);
33201da177e4SLinus Torvalds 	}
33211da177e4SLinus Torvalds 	return res;
33221da177e4SLinus Torvalds }
33231da177e4SLinus Torvalds 
3324cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
33251da177e4SLinus Torvalds {
3326cc314eefSLinus Torvalds 	struct page *page = NULL;
33271da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3328cc314eefSLinus Torvalds 	return page;
33291da177e4SLinus Torvalds }
33301da177e4SLinus Torvalds 
3331cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
33321da177e4SLinus Torvalds {
3333cc314eefSLinus Torvalds 	struct page *page = cookie;
3334cc314eefSLinus Torvalds 
3335cc314eefSLinus Torvalds 	if (page) {
33361da177e4SLinus Torvalds 		kunmap(page);
33371da177e4SLinus Torvalds 		page_cache_release(page);
33381da177e4SLinus Torvalds 	}
33391da177e4SLinus Torvalds }
33401da177e4SLinus Torvalds 
334154566b2cSNick Piggin /*
334254566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
334354566b2cSNick Piggin  */
334454566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
33451da177e4SLinus Torvalds {
33461da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
33470adb25d2SKirill Korotaev 	struct page *page;
3348afddba49SNick Piggin 	void *fsdata;
3349beb497abSDmitriy Monakhov 	int err;
33501da177e4SLinus Torvalds 	char *kaddr;
335154566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
335254566b2cSNick Piggin 	if (nofs)
335354566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
33541da177e4SLinus Torvalds 
33557e53cac4SNeilBrown retry:
3356afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
335754566b2cSNick Piggin 				flags, &page, &fsdata);
33581da177e4SLinus Torvalds 	if (err)
3359afddba49SNick Piggin 		goto fail;
3360afddba49SNick Piggin 
33611da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
33621da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
33631da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
3364afddba49SNick Piggin 
3365afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3366afddba49SNick Piggin 							page, fsdata);
33671da177e4SLinus Torvalds 	if (err < 0)
33681da177e4SLinus Torvalds 		goto fail;
3369afddba49SNick Piggin 	if (err < len-1)
3370afddba49SNick Piggin 		goto retry;
3371afddba49SNick Piggin 
33721da177e4SLinus Torvalds 	mark_inode_dirty(inode);
33731da177e4SLinus Torvalds 	return 0;
33741da177e4SLinus Torvalds fail:
33751da177e4SLinus Torvalds 	return err;
33761da177e4SLinus Torvalds }
33771da177e4SLinus Torvalds 
33780adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
33790adb25d2SKirill Korotaev {
33800adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
338154566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
33820adb25d2SKirill Korotaev }
33830adb25d2SKirill Korotaev 
338492e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
33851da177e4SLinus Torvalds 	.readlink	= generic_readlink,
33861da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
33871da177e4SLinus Torvalds 	.put_link	= page_put_link,
33881da177e4SLinus Torvalds };
33891da177e4SLinus Torvalds 
33902d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
3391cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
33921da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
33931da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
33941da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
33951da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
33961da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
33971da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
33981da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
33991da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
34001da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
34010adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
34021da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
34031da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
3404c9c6cac0SAl Viro EXPORT_SYMBOL(kern_path_parent);
3405d1811465SAl Viro EXPORT_SYMBOL(kern_path);
340616f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3407f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
34088c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission);
34091da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
34101da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
34111da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
34121da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
34131da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
34141da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
34151da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
34161da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
34171da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
34181da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
34191da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
34201da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
34211da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
34221da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3423