xref: /openbmc/linux/fs/namei.c (revision 55852635)
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>
18630d9c47SPaul Gortmaker #include <linux/export.h>
1944696908SDavid S. Miller #include <linux/kernel.h>
201da177e4SLinus Torvalds #include <linux/slab.h>
211da177e4SLinus Torvalds #include <linux/fs.h>
221da177e4SLinus Torvalds #include <linux/namei.h>
231da177e4SLinus Torvalds #include <linux/pagemap.h>
240eeca283SRobert Love #include <linux/fsnotify.h>
251da177e4SLinus Torvalds #include <linux/personality.h>
261da177e4SLinus Torvalds #include <linux/security.h>
276146f0d5SMimi Zohar #include <linux/ima.h>
281da177e4SLinus Torvalds #include <linux/syscalls.h>
291da177e4SLinus Torvalds #include <linux/mount.h>
301da177e4SLinus Torvalds #include <linux/audit.h>
3116f7e0feSRandy Dunlap #include <linux/capability.h>
32834f2a4aSTrond Myklebust #include <linux/file.h>
335590ff0dSUlrich Drepper #include <linux/fcntl.h>
3408ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h>
355ad4e53bSAl Viro #include <linux/fs_struct.h>
36e77819e5SLinus Torvalds #include <linux/posix_acl.h>
371da177e4SLinus Torvalds #include <asm/uaccess.h>
381da177e4SLinus Torvalds 
39e81e3f4dSEric Paris #include "internal.h"
40c7105365SAl Viro #include "mount.h"
41e81e3f4dSEric Paris 
421da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer]
431da177e4SLinus Torvalds  * Fundamental changes in the pathname lookup mechanisms (namei)
441da177e4SLinus Torvalds  * were necessary because of omirr.  The reason is that omirr needs
451da177e4SLinus Torvalds  * to know the _real_ pathname, not the user-supplied one, in case
461da177e4SLinus Torvalds  * of symlinks (and also when transname replacements occur).
471da177e4SLinus Torvalds  *
481da177e4SLinus Torvalds  * The new code replaces the old recursive symlink resolution with
491da177e4SLinus Torvalds  * an iterative one (in case of non-nested symlink chains).  It does
501da177e4SLinus Torvalds  * this with calls to <fs>_follow_link().
511da177e4SLinus Torvalds  * As a side effect, dir_namei(), _namei() and follow_link() are now
521da177e4SLinus Torvalds  * replaced with a single function lookup_dentry() that can handle all
531da177e4SLinus Torvalds  * the special cases of the former code.
541da177e4SLinus Torvalds  *
551da177e4SLinus Torvalds  * With the new dcache, the pathname is stored at each inode, at least as
561da177e4SLinus Torvalds  * long as the refcount of the inode is positive.  As a side effect, the
571da177e4SLinus Torvalds  * size of the dcache depends on the inode cache and thus is dynamic.
581da177e4SLinus Torvalds  *
591da177e4SLinus Torvalds  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
601da177e4SLinus Torvalds  * resolution to correspond with current state of the code.
611da177e4SLinus Torvalds  *
621da177e4SLinus Torvalds  * Note that the symlink resolution is not *completely* iterative.
631da177e4SLinus Torvalds  * There is still a significant amount of tail- and mid- recursion in
641da177e4SLinus Torvalds  * the algorithm.  Also, note that <fs>_readlink() is not used in
651da177e4SLinus Torvalds  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
661da177e4SLinus Torvalds  * may return different results than <fs>_follow_link().  Many virtual
671da177e4SLinus Torvalds  * filesystems (including /proc) exhibit this behavior.
681da177e4SLinus Torvalds  */
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
711da177e4SLinus Torvalds  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
721da177e4SLinus Torvalds  * and the name already exists in form of a symlink, try to create the new
731da177e4SLinus Torvalds  * name indicated by the symlink. The old code always complained that the
741da177e4SLinus Torvalds  * name already exists, due to not following the symlink even if its target
751da177e4SLinus Torvalds  * is nonexistent.  The new semantics affects also mknod() and link() when
7625985edcSLucas De Marchi  * the name is a symlink pointing to a non-existent name.
771da177e4SLinus Torvalds  *
781da177e4SLinus Torvalds  * I don't know which semantics is the right one, since I have no access
791da177e4SLinus Torvalds  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
801da177e4SLinus Torvalds  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
811da177e4SLinus Torvalds  * "old" one. Personally, I think the new semantics is much more logical.
821da177e4SLinus Torvalds  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
831da177e4SLinus Torvalds  * file does succeed in both HP-UX and SunOs, but not in Solaris
841da177e4SLinus Torvalds  * and in the old Linux semantics.
851da177e4SLinus Torvalds  */
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
881da177e4SLinus Torvalds  * semantics.  See the comments in "open_namei" and "do_link" below.
891da177e4SLinus Torvalds  *
901da177e4SLinus Torvalds  * [10-Sep-98 Alan Modra] Another symlink change.
911da177e4SLinus Torvalds  */
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
941da177e4SLinus Torvalds  *	inside the path - always follow.
951da177e4SLinus Torvalds  *	in the last component in creation/removal/renaming - never follow.
961da177e4SLinus Torvalds  *	if LOOKUP_FOLLOW passed - follow.
971da177e4SLinus Torvalds  *	if the pathname has trailing slashes - follow.
981da177e4SLinus Torvalds  *	otherwise - don't follow.
991da177e4SLinus Torvalds  * (applied in that order).
1001da177e4SLinus Torvalds  *
1011da177e4SLinus Torvalds  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
1021da177e4SLinus Torvalds  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
1031da177e4SLinus Torvalds  * During the 2.4 we need to fix the userland stuff depending on it -
1041da177e4SLinus Torvalds  * hopefully we will be able to get rid of that wart in 2.5. So far only
1051da177e4SLinus Torvalds  * XEmacs seems to be relying on it...
1061da177e4SLinus Torvalds  */
1071da177e4SLinus Torvalds /*
1081da177e4SLinus Torvalds  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
109a11f3a05SArjan van de Ven  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
1101da177e4SLinus Torvalds  * any extra contention...
1111da177e4SLinus Torvalds  */
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional
1141da177e4SLinus Torvalds  * checking and hopefully speeding things up, we copy filenames to the
1151da177e4SLinus Torvalds  * kernel data space before using them..
1161da177e4SLinus Torvalds  *
1171da177e4SLinus Torvalds  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
1181da177e4SLinus Torvalds  * PATH_MAX includes the nul terminator --RR.
1191da177e4SLinus Torvalds  */
1201fa1e7f6SAndy Whitcroft static char *getname_flags(const char __user *filename, int flags, int *empty)
1211da177e4SLinus Torvalds {
1223f9f0aa6SLinus Torvalds 	char *result = __getname(), *err;
1233f9f0aa6SLinus Torvalds 	int len;
1241da177e4SLinus Torvalds 
1253f9f0aa6SLinus Torvalds 	if (unlikely(!result))
1264043cde8SEric Paris 		return ERR_PTR(-ENOMEM);
1271da177e4SLinus Torvalds 
1283f9f0aa6SLinus Torvalds 	len = strncpy_from_user(result, filename, PATH_MAX);
1293f9f0aa6SLinus Torvalds 	err = ERR_PTR(len);
1303f9f0aa6SLinus Torvalds 	if (unlikely(len < 0))
1313f9f0aa6SLinus Torvalds 		goto error;
1323f9f0aa6SLinus Torvalds 
1333f9f0aa6SLinus Torvalds 	/* The empty path is special. */
1343f9f0aa6SLinus Torvalds 	if (unlikely(!len)) {
1353f9f0aa6SLinus Torvalds 		if (empty)
1361fa1e7f6SAndy Whitcroft 			*empty = 1;
1373f9f0aa6SLinus Torvalds 		err = ERR_PTR(-ENOENT);
1383f9f0aa6SLinus Torvalds 		if (!(flags & LOOKUP_EMPTY))
1393f9f0aa6SLinus Torvalds 			goto error;
1401da177e4SLinus Torvalds 	}
1413f9f0aa6SLinus Torvalds 
1423f9f0aa6SLinus Torvalds 	err = ERR_PTR(-ENAMETOOLONG);
1433f9f0aa6SLinus Torvalds 	if (likely(len < PATH_MAX)) {
1441da177e4SLinus Torvalds 		audit_getname(result);
1451da177e4SLinus Torvalds 		return result;
1461da177e4SLinus Torvalds 	}
1471da177e4SLinus Torvalds 
1483f9f0aa6SLinus Torvalds error:
1493f9f0aa6SLinus Torvalds 	__putname(result);
1503f9f0aa6SLinus Torvalds 	return err;
1513f9f0aa6SLinus Torvalds }
1523f9f0aa6SLinus Torvalds 
153f52e0c11SAl Viro char *getname(const char __user * filename)
154f52e0c11SAl Viro {
155f7493e5dSLinus Torvalds 	return getname_flags(filename, 0, NULL);
156f52e0c11SAl Viro }
157f52e0c11SAl Viro 
1581da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
1591da177e4SLinus Torvalds void putname(const char *name)
1601da177e4SLinus Torvalds {
1615ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
1621da177e4SLinus Torvalds 		audit_putname(name);
1631da177e4SLinus Torvalds 	else
1641da177e4SLinus Torvalds 		__putname(name);
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds EXPORT_SYMBOL(putname);
1671da177e4SLinus Torvalds #endif
1681da177e4SLinus Torvalds 
169e77819e5SLinus Torvalds static int check_acl(struct inode *inode, int mask)
170e77819e5SLinus Torvalds {
17184635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL
172e77819e5SLinus Torvalds 	struct posix_acl *acl;
173e77819e5SLinus Torvalds 
174e77819e5SLinus Torvalds 	if (mask & MAY_NOT_BLOCK) {
1753567866bSAl Viro 		acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
1763567866bSAl Viro 	        if (!acl)
177e77819e5SLinus Torvalds 	                return -EAGAIN;
1783567866bSAl Viro 		/* no ->get_acl() calls in RCU mode... */
1793567866bSAl Viro 		if (acl == ACL_NOT_CACHED)
180e77819e5SLinus Torvalds 			return -ECHILD;
181206b1d09SAri Savolainen 	        return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
182e77819e5SLinus Torvalds 	}
183e77819e5SLinus Torvalds 
184e77819e5SLinus Torvalds 	acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
185e77819e5SLinus Torvalds 
186e77819e5SLinus Torvalds 	/*
1874e34e719SChristoph Hellwig 	 * A filesystem can force a ACL callback by just never filling the
1884e34e719SChristoph Hellwig 	 * ACL cache. But normally you'd fill the cache either at inode
1894e34e719SChristoph Hellwig 	 * instantiation time, or on the first ->get_acl call.
190e77819e5SLinus Torvalds 	 *
1914e34e719SChristoph Hellwig 	 * If the filesystem doesn't have a get_acl() function at all, we'll
1924e34e719SChristoph Hellwig 	 * just create the negative cache entry.
193e77819e5SLinus Torvalds 	 */
194e77819e5SLinus Torvalds 	if (acl == ACL_NOT_CACHED) {
1954e34e719SChristoph Hellwig 	        if (inode->i_op->get_acl) {
1964e34e719SChristoph Hellwig 			acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
1974e34e719SChristoph Hellwig 			if (IS_ERR(acl))
1984e34e719SChristoph Hellwig 				return PTR_ERR(acl);
1994e34e719SChristoph Hellwig 		} else {
200e77819e5SLinus Torvalds 		        set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
201e77819e5SLinus Torvalds 		        return -EAGAIN;
202e77819e5SLinus Torvalds 		}
2034e34e719SChristoph Hellwig 	}
204e77819e5SLinus Torvalds 
205e77819e5SLinus Torvalds 	if (acl) {
206e77819e5SLinus Torvalds 	        int error = posix_acl_permission(inode, acl, mask);
207e77819e5SLinus Torvalds 	        posix_acl_release(acl);
208e77819e5SLinus Torvalds 	        return error;
209e77819e5SLinus Torvalds 	}
21084635d68SLinus Torvalds #endif
211e77819e5SLinus Torvalds 
212e77819e5SLinus Torvalds 	return -EAGAIN;
213e77819e5SLinus Torvalds }
214e77819e5SLinus Torvalds 
2155909ccaaSLinus Torvalds /*
216948409c7SAndreas Gruenbacher  * This does the basic permission checking
2175909ccaaSLinus Torvalds  */
2187e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask)
2195909ccaaSLinus Torvalds {
22026cf46beSLinus Torvalds 	unsigned int mode = inode->i_mode;
2215909ccaaSLinus Torvalds 
2228e96e3b7SEric W. Biederman 	if (likely(uid_eq(current_fsuid(), inode->i_uid)))
2235909ccaaSLinus Torvalds 		mode >>= 6;
2245909ccaaSLinus Torvalds 	else {
225e77819e5SLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
2267e40145eSAl Viro 			int error = check_acl(inode, mask);
2275909ccaaSLinus Torvalds 			if (error != -EAGAIN)
2285909ccaaSLinus Torvalds 				return error;
2295909ccaaSLinus Torvalds 		}
2305909ccaaSLinus Torvalds 
2315909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
2325909ccaaSLinus Torvalds 			mode >>= 3;
2335909ccaaSLinus Torvalds 	}
2345909ccaaSLinus Torvalds 
2355909ccaaSLinus Torvalds 	/*
2365909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
2375909ccaaSLinus Torvalds 	 */
2389c2c7039SAl Viro 	if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
2395909ccaaSLinus Torvalds 		return 0;
2405909ccaaSLinus Torvalds 	return -EACCES;
2415909ccaaSLinus Torvalds }
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds /**
2441da177e4SLinus Torvalds  * generic_permission -  check for access rights on a Posix-like filesystem
2451da177e4SLinus Torvalds  * @inode:	inode to check access rights for
2468fd90c8dSAndreas Gruenbacher  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
2471da177e4SLinus Torvalds  *
2481da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
2491da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
2501da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
251b74c79e9SNick Piggin  * are used for other things.
252b74c79e9SNick Piggin  *
253b74c79e9SNick Piggin  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
254b74c79e9SNick Piggin  * request cannot be satisfied (eg. requires blocking or too much complexity).
255b74c79e9SNick Piggin  * It would then be called again in ref-walk mode.
2561da177e4SLinus Torvalds  */
2572830ba7fSAl Viro int generic_permission(struct inode *inode, int mask)
2581da177e4SLinus Torvalds {
2595909ccaaSLinus Torvalds 	int ret;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	/*
262948409c7SAndreas Gruenbacher 	 * Do the basic permission checks.
2631da177e4SLinus Torvalds 	 */
2647e40145eSAl Viro 	ret = acl_permission_check(inode, mask);
2655909ccaaSLinus Torvalds 	if (ret != -EACCES)
2665909ccaaSLinus Torvalds 		return ret;
2671da177e4SLinus Torvalds 
268d594e7ecSAl Viro 	if (S_ISDIR(inode->i_mode)) {
269d594e7ecSAl Viro 		/* DACs are overridable for directories */
2701a48e2acSEric W. Biederman 		if (inode_capable(inode, CAP_DAC_OVERRIDE))
271d594e7ecSAl Viro 			return 0;
272d594e7ecSAl Viro 		if (!(mask & MAY_WRITE))
2731a48e2acSEric W. Biederman 			if (inode_capable(inode, CAP_DAC_READ_SEARCH))
274d594e7ecSAl Viro 				return 0;
275d594e7ecSAl Viro 		return -EACCES;
276d594e7ecSAl Viro 	}
2771da177e4SLinus Torvalds 	/*
2781da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
279d594e7ecSAl Viro 	 * Executable DACs are overridable when there is
280d594e7ecSAl Viro 	 * at least one exec bit set.
2811da177e4SLinus Torvalds 	 */
282d594e7ecSAl Viro 	if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
2831a48e2acSEric W. Biederman 		if (inode_capable(inode, CAP_DAC_OVERRIDE))
2841da177e4SLinus Torvalds 			return 0;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	/*
2871da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
2881da177e4SLinus Torvalds 	 */
2897ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
290d594e7ecSAl Viro 	if (mask == MAY_READ)
2911a48e2acSEric W. Biederman 		if (inode_capable(inode, CAP_DAC_READ_SEARCH))
2921da177e4SLinus Torvalds 			return 0;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	return -EACCES;
2951da177e4SLinus Torvalds }
2961da177e4SLinus Torvalds 
2973ddcd056SLinus Torvalds /*
2983ddcd056SLinus Torvalds  * We _really_ want to just do "generic_permission()" without
2993ddcd056SLinus Torvalds  * even looking at the inode->i_op values. So we keep a cache
3003ddcd056SLinus Torvalds  * flag in inode->i_opflags, that says "this has not special
3013ddcd056SLinus Torvalds  * permission function, use the fast case".
3023ddcd056SLinus Torvalds  */
3033ddcd056SLinus Torvalds static inline int do_inode_permission(struct inode *inode, int mask)
3043ddcd056SLinus Torvalds {
3053ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
3063ddcd056SLinus Torvalds 		if (likely(inode->i_op->permission))
3073ddcd056SLinus Torvalds 			return inode->i_op->permission(inode, mask);
3083ddcd056SLinus Torvalds 
3093ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
3103ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
3113ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_FASTPERM;
3123ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
3133ddcd056SLinus Torvalds 	}
3143ddcd056SLinus Torvalds 	return generic_permission(inode, mask);
3153ddcd056SLinus Torvalds }
3163ddcd056SLinus Torvalds 
317cb23beb5SChristoph Hellwig /**
3180bdaea90SDavid Howells  * __inode_permission - Check for access rights to a given inode
3190bdaea90SDavid Howells  * @inode: Inode to check permission on
3200bdaea90SDavid Howells  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
321cb23beb5SChristoph Hellwig  *
3220bdaea90SDavid Howells  * Check for read/write/execute permissions on an inode.
323948409c7SAndreas Gruenbacher  *
324948409c7SAndreas Gruenbacher  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
3250bdaea90SDavid Howells  *
3260bdaea90SDavid Howells  * This does not check for a read-only file system.  You probably want
3270bdaea90SDavid Howells  * inode_permission().
328cb23beb5SChristoph Hellwig  */
3290bdaea90SDavid Howells int __inode_permission(struct inode *inode, int mask)
3301da177e4SLinus Torvalds {
331e6305c43SAl Viro 	int retval;
3321da177e4SLinus Torvalds 
3333ddcd056SLinus Torvalds 	if (unlikely(mask & MAY_WRITE)) {
3341da177e4SLinus Torvalds 		/*
3351da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
3361da177e4SLinus Torvalds 		 */
3371da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
3381da177e4SLinus Torvalds 			return -EACCES;
3391da177e4SLinus Torvalds 	}
3401da177e4SLinus Torvalds 
3413ddcd056SLinus Torvalds 	retval = do_inode_permission(inode, mask);
3421da177e4SLinus Torvalds 	if (retval)
3431da177e4SLinus Torvalds 		return retval;
3441da177e4SLinus Torvalds 
34508ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
34608ce5f16SSerge E. Hallyn 	if (retval)
34708ce5f16SSerge E. Hallyn 		return retval;
34808ce5f16SSerge E. Hallyn 
349d09ca739SEric Paris 	return security_inode_permission(inode, mask);
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
352f4d6ff89SAl Viro /**
3530bdaea90SDavid Howells  * sb_permission - Check superblock-level permissions
3540bdaea90SDavid Howells  * @sb: Superblock of inode to check permission on
35555852635SRandy Dunlap  * @inode: Inode to check permission on
3560bdaea90SDavid Howells  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
3570bdaea90SDavid Howells  *
3580bdaea90SDavid Howells  * Separate out file-system wide checks from inode-specific permission checks.
3590bdaea90SDavid Howells  */
3600bdaea90SDavid Howells static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
3610bdaea90SDavid Howells {
3620bdaea90SDavid Howells 	if (unlikely(mask & MAY_WRITE)) {
3630bdaea90SDavid Howells 		umode_t mode = inode->i_mode;
3640bdaea90SDavid Howells 
3650bdaea90SDavid Howells 		/* Nobody gets write access to a read-only fs. */
3660bdaea90SDavid Howells 		if ((sb->s_flags & MS_RDONLY) &&
3670bdaea90SDavid Howells 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
3680bdaea90SDavid Howells 			return -EROFS;
3690bdaea90SDavid Howells 	}
3700bdaea90SDavid Howells 	return 0;
3710bdaea90SDavid Howells }
3720bdaea90SDavid Howells 
3730bdaea90SDavid Howells /**
3740bdaea90SDavid Howells  * inode_permission - Check for access rights to a given inode
3750bdaea90SDavid Howells  * @inode: Inode to check permission on
3760bdaea90SDavid Howells  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
3770bdaea90SDavid Howells  *
3780bdaea90SDavid Howells  * Check for read/write/execute permissions on an inode.  We use fs[ug]id for
3790bdaea90SDavid Howells  * this, letting us set arbitrary permissions for filesystem access without
3800bdaea90SDavid Howells  * changing the "normal" UIDs which are used for other things.
3810bdaea90SDavid Howells  *
3820bdaea90SDavid Howells  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
3830bdaea90SDavid Howells  */
3840bdaea90SDavid Howells int inode_permission(struct inode *inode, int mask)
3850bdaea90SDavid Howells {
3860bdaea90SDavid Howells 	int retval;
3870bdaea90SDavid Howells 
3880bdaea90SDavid Howells 	retval = sb_permission(inode->i_sb, inode, mask);
3890bdaea90SDavid Howells 	if (retval)
3900bdaea90SDavid Howells 		return retval;
3910bdaea90SDavid Howells 	return __inode_permission(inode, mask);
3920bdaea90SDavid Howells }
3930bdaea90SDavid Howells 
3940bdaea90SDavid Howells /**
3955dd784d0SJan Blunck  * path_get - get a reference to a path
3965dd784d0SJan Blunck  * @path: path to get the reference to
3975dd784d0SJan Blunck  *
3985dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
3995dd784d0SJan Blunck  */
4005dd784d0SJan Blunck void path_get(struct path *path)
4015dd784d0SJan Blunck {
4025dd784d0SJan Blunck 	mntget(path->mnt);
4035dd784d0SJan Blunck 	dget(path->dentry);
4045dd784d0SJan Blunck }
4055dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
4065dd784d0SJan Blunck 
4075dd784d0SJan Blunck /**
4081d957f9bSJan Blunck  * path_put - put a reference to a path
4091d957f9bSJan Blunck  * @path: path to put the reference to
4101d957f9bSJan Blunck  *
4111d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
4121d957f9bSJan Blunck  */
4131d957f9bSJan Blunck void path_put(struct path *path)
4141da177e4SLinus Torvalds {
4151d957f9bSJan Blunck 	dput(path->dentry);
4161d957f9bSJan Blunck 	mntput(path->mnt);
4171da177e4SLinus Torvalds }
4181d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
4191da177e4SLinus Torvalds 
42019660af7SAl Viro /*
42131e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
42219660af7SAl Viro  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
42319660af7SAl Viro  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
42419660af7SAl Viro  * normal reference counts on dentries and vfsmounts to transition to rcu-walk
42519660af7SAl Viro  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
42619660af7SAl Viro  * got stuck, so ref-walk may continue from there. If this is not successful
42719660af7SAl Viro  * (eg. a seqcount has changed), then failure is returned and it's up to caller
42819660af7SAl Viro  * to restart the path walk from the beginning in ref-walk mode.
42931e6b01fSNick Piggin  */
43031e6b01fSNick Piggin 
43132a7991bSAl Viro static inline void lock_rcu_walk(void)
43232a7991bSAl Viro {
43332a7991bSAl Viro 	br_read_lock(&vfsmount_lock);
43432a7991bSAl Viro 	rcu_read_lock();
43532a7991bSAl Viro }
43632a7991bSAl Viro 
43732a7991bSAl Viro static inline void unlock_rcu_walk(void)
43832a7991bSAl Viro {
43932a7991bSAl Viro 	rcu_read_unlock();
44032a7991bSAl Viro 	br_read_unlock(&vfsmount_lock);
44132a7991bSAl Viro }
44232a7991bSAl Viro 
44331e6b01fSNick Piggin /**
44419660af7SAl Viro  * unlazy_walk - try to switch to ref-walk mode.
44519660af7SAl Viro  * @nd: nameidata pathwalk data
44619660af7SAl Viro  * @dentry: child of nd->path.dentry or NULL
44739191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
44831e6b01fSNick Piggin  *
44919660af7SAl Viro  * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
45019660af7SAl Viro  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
45119660af7SAl Viro  * @nd or NULL.  Must be called from rcu-walk context.
45231e6b01fSNick Piggin  */
45319660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
45431e6b01fSNick Piggin {
45531e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
45631e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
4575b6ca027SAl Viro 	int want_root = 0;
45831e6b01fSNick Piggin 
45931e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
4605b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
4615b6ca027SAl Viro 		want_root = 1;
46231e6b01fSNick Piggin 		spin_lock(&fs->lock);
46331e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
46431e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
46531e6b01fSNick Piggin 			goto err_root;
46631e6b01fSNick Piggin 	}
46731e6b01fSNick Piggin 	spin_lock(&parent->d_lock);
46819660af7SAl Viro 	if (!dentry) {
46919660af7SAl Viro 		if (!__d_rcu_to_refcount(parent, nd->seq))
47019660af7SAl Viro 			goto err_parent;
47119660af7SAl Viro 		BUG_ON(nd->inode != parent->d_inode);
47219660af7SAl Viro 	} else {
47394c0d4ecSAl Viro 		if (dentry->d_parent != parent)
47494c0d4ecSAl Viro 			goto err_parent;
47531e6b01fSNick Piggin 		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
47631e6b01fSNick Piggin 		if (!__d_rcu_to_refcount(dentry, nd->seq))
47719660af7SAl Viro 			goto err_child;
47831e6b01fSNick Piggin 		/*
47919660af7SAl Viro 		 * If the sequence check on the child dentry passed, then
48019660af7SAl Viro 		 * the child has not been removed from its parent. This
48119660af7SAl Viro 		 * means the parent dentry must be valid and able to take
48219660af7SAl Viro 		 * a reference at this point.
48331e6b01fSNick Piggin 		 */
48431e6b01fSNick Piggin 		BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
48531e6b01fSNick Piggin 		BUG_ON(!parent->d_count);
48631e6b01fSNick Piggin 		parent->d_count++;
48731e6b01fSNick Piggin 		spin_unlock(&dentry->d_lock);
48819660af7SAl Viro 	}
48931e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
4905b6ca027SAl Viro 	if (want_root) {
49131e6b01fSNick Piggin 		path_get(&nd->root);
49231e6b01fSNick Piggin 		spin_unlock(&fs->lock);
49331e6b01fSNick Piggin 	}
49431e6b01fSNick Piggin 	mntget(nd->path.mnt);
49531e6b01fSNick Piggin 
49632a7991bSAl Viro 	unlock_rcu_walk();
49731e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
49831e6b01fSNick Piggin 	return 0;
49919660af7SAl Viro 
50019660af7SAl Viro err_child:
50131e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
50219660af7SAl Viro err_parent:
50331e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
50431e6b01fSNick Piggin err_root:
5055b6ca027SAl Viro 	if (want_root)
50631e6b01fSNick Piggin 		spin_unlock(&fs->lock);
50731e6b01fSNick Piggin 	return -ECHILD;
50831e6b01fSNick Piggin }
50931e6b01fSNick Piggin 
5104ce16ef3SAl Viro static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
51134286d66SNick Piggin {
5124ce16ef3SAl Viro 	return dentry->d_op->d_revalidate(dentry, flags);
51334286d66SNick Piggin }
51434286d66SNick Piggin 
5159f1fafeeSAl Viro /**
5169f1fafeeSAl Viro  * complete_walk - successful completion of path walk
5179f1fafeeSAl Viro  * @nd:  pointer nameidata
51839159de2SJeff Layton  *
5199f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
5209f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
5219f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
5229f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
5239f1fafeeSAl Viro  * need to drop nd->path.
52439159de2SJeff Layton  */
5259f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
52639159de2SJeff Layton {
52716c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
52839159de2SJeff Layton 	int status;
52939159de2SJeff Layton 
5309f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
5319f1fafeeSAl Viro 		nd->flags &= ~LOOKUP_RCU;
5329f1fafeeSAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
5339f1fafeeSAl Viro 			nd->root.mnt = NULL;
5349f1fafeeSAl Viro 		spin_lock(&dentry->d_lock);
5359f1fafeeSAl Viro 		if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
5369f1fafeeSAl Viro 			spin_unlock(&dentry->d_lock);
53732a7991bSAl Viro 			unlock_rcu_walk();
5389f1fafeeSAl Viro 			return -ECHILD;
5399f1fafeeSAl Viro 		}
5409f1fafeeSAl Viro 		BUG_ON(nd->inode != dentry->d_inode);
5419f1fafeeSAl Viro 		spin_unlock(&dentry->d_lock);
5429f1fafeeSAl Viro 		mntget(nd->path.mnt);
54332a7991bSAl Viro 		unlock_rcu_walk();
5449f1fafeeSAl Viro 	}
5459f1fafeeSAl Viro 
54616c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
54739159de2SJeff Layton 		return 0;
54839159de2SJeff Layton 
54916c2cd71SAl Viro 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
55016c2cd71SAl Viro 		return 0;
55116c2cd71SAl Viro 
55216c2cd71SAl Viro 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
55316c2cd71SAl Viro 		return 0;
55416c2cd71SAl Viro 
55516c2cd71SAl Viro 	/* Note: we do not d_invalidate() */
5564ce16ef3SAl Viro 	status = d_revalidate(dentry, nd->flags);
55739159de2SJeff Layton 	if (status > 0)
55839159de2SJeff Layton 		return 0;
55939159de2SJeff Layton 
56016c2cd71SAl Viro 	if (!status)
56139159de2SJeff Layton 		status = -ESTALE;
56216c2cd71SAl Viro 
5639f1fafeeSAl Viro 	path_put(&nd->path);
56439159de2SJeff Layton 	return status;
56539159de2SJeff Layton }
56639159de2SJeff Layton 
5672a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
5682a737871SAl Viro {
569f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
570f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
5712a737871SAl Viro }
5722a737871SAl Viro 
5736de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
5746de88d72SAl Viro 
57531e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
57631e6b01fSNick Piggin {
57731e6b01fSNick Piggin 	if (!nd->root.mnt) {
57831e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
579c28cc364SNick Piggin 		unsigned seq;
580c28cc364SNick Piggin 
581c28cc364SNick Piggin 		do {
582c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
58331e6b01fSNick Piggin 			nd->root = fs->root;
584c1530019STim Chen 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
585c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
58631e6b01fSNick Piggin 	}
58731e6b01fSNick Piggin }
58831e6b01fSNick Piggin 
589f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
5901da177e4SLinus Torvalds {
59131e6b01fSNick Piggin 	int ret;
59231e6b01fSNick Piggin 
5931da177e4SLinus Torvalds 	if (IS_ERR(link))
5941da177e4SLinus Torvalds 		goto fail;
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	if (*link == '/') {
5972a737871SAl Viro 		set_root(nd);
5981d957f9bSJan Blunck 		path_put(&nd->path);
5992a737871SAl Viro 		nd->path = nd->root;
6002a737871SAl Viro 		path_get(&nd->root);
60116c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
6021da177e4SLinus Torvalds 	}
60331e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
604b4091d5fSChristoph Hellwig 
60531e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
60631e6b01fSNick Piggin 	return ret;
6071da177e4SLinus Torvalds fail:
6081d957f9bSJan Blunck 	path_put(&nd->path);
6091da177e4SLinus Torvalds 	return PTR_ERR(link);
6101da177e4SLinus Torvalds }
6111da177e4SLinus Torvalds 
6121d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
613051d3812SIan Kent {
614051d3812SIan Kent 	dput(path->dentry);
6154ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
616051d3812SIan Kent 		mntput(path->mnt);
617051d3812SIan Kent }
618051d3812SIan Kent 
6197b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
6207b9337aaSNick Piggin 					struct nameidata *nd)
621051d3812SIan Kent {
62231e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
6234ac91378SJan Blunck 		dput(nd->path.dentry);
62431e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
6254ac91378SJan Blunck 			mntput(nd->path.mnt);
6269a229683SHuang Shijie 	}
62731e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
6284ac91378SJan Blunck 	nd->path.dentry = path->dentry;
629051d3812SIan Kent }
630051d3812SIan Kent 
631b5fb63c1SChristoph Hellwig /*
632b5fb63c1SChristoph Hellwig  * Helper to directly jump to a known parsed path from ->follow_link,
633b5fb63c1SChristoph Hellwig  * caller must have taken a reference to path beforehand.
634b5fb63c1SChristoph Hellwig  */
635b5fb63c1SChristoph Hellwig void nd_jump_link(struct nameidata *nd, struct path *path)
636b5fb63c1SChristoph Hellwig {
637b5fb63c1SChristoph Hellwig 	path_put(&nd->path);
638b5fb63c1SChristoph Hellwig 
639b5fb63c1SChristoph Hellwig 	nd->path = *path;
640b5fb63c1SChristoph Hellwig 	nd->inode = nd->path.dentry->d_inode;
641b5fb63c1SChristoph Hellwig 	nd->flags |= LOOKUP_JUMPED;
642b5fb63c1SChristoph Hellwig 
643b5fb63c1SChristoph Hellwig 	BUG_ON(nd->inode->i_op->follow_link);
644b5fb63c1SChristoph Hellwig }
645b5fb63c1SChristoph Hellwig 
646574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
647574197e0SAl Viro {
648574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
6496d7b5aaeSAl Viro 	if (inode->i_op->put_link)
650574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
651574197e0SAl Viro 	path_put(link);
652574197e0SAl Viro }
653574197e0SAl Viro 
654800179c9SKees Cook int sysctl_protected_symlinks __read_mostly = 1;
655800179c9SKees Cook int sysctl_protected_hardlinks __read_mostly = 1;
656800179c9SKees Cook 
657800179c9SKees Cook /**
658800179c9SKees Cook  * may_follow_link - Check symlink following for unsafe situations
659800179c9SKees Cook  * @link: The path of the symlink
66055852635SRandy Dunlap  * @nd: nameidata pathwalk data
661800179c9SKees Cook  *
662800179c9SKees Cook  * In the case of the sysctl_protected_symlinks sysctl being enabled,
663800179c9SKees Cook  * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
664800179c9SKees Cook  * in a sticky world-writable directory. This is to protect privileged
665800179c9SKees Cook  * processes from failing races against path names that may change out
666800179c9SKees Cook  * from under them by way of other users creating malicious symlinks.
667800179c9SKees Cook  * It will permit symlinks to be followed only when outside a sticky
668800179c9SKees Cook  * world-writable directory, or when the uid of the symlink and follower
669800179c9SKees Cook  * match, or when the directory owner matches the symlink's owner.
670800179c9SKees Cook  *
671800179c9SKees Cook  * Returns 0 if following the symlink is allowed, -ve on error.
672800179c9SKees Cook  */
673800179c9SKees Cook static inline int may_follow_link(struct path *link, struct nameidata *nd)
674800179c9SKees Cook {
675800179c9SKees Cook 	const struct inode *inode;
676800179c9SKees Cook 	const struct inode *parent;
677800179c9SKees Cook 
678800179c9SKees Cook 	if (!sysctl_protected_symlinks)
679800179c9SKees Cook 		return 0;
680800179c9SKees Cook 
681800179c9SKees Cook 	/* Allowed if owner and follower match. */
682800179c9SKees Cook 	inode = link->dentry->d_inode;
683800179c9SKees Cook 	if (current_cred()->fsuid == inode->i_uid)
684800179c9SKees Cook 		return 0;
685800179c9SKees Cook 
686800179c9SKees Cook 	/* Allowed if parent directory not sticky and world-writable. */
687800179c9SKees Cook 	parent = nd->path.dentry->d_inode;
688800179c9SKees Cook 	if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
689800179c9SKees Cook 		return 0;
690800179c9SKees Cook 
691800179c9SKees Cook 	/* Allowed if parent directory and link owner match. */
692800179c9SKees Cook 	if (parent->i_uid == inode->i_uid)
693800179c9SKees Cook 		return 0;
694800179c9SKees Cook 
695800179c9SKees Cook 	path_put_conditional(link, nd);
696800179c9SKees Cook 	path_put(&nd->path);
697a51d9eaaSKees Cook 	audit_log_link_denied("follow_link", link);
698800179c9SKees Cook 	return -EACCES;
699800179c9SKees Cook }
700800179c9SKees Cook 
701800179c9SKees Cook /**
702800179c9SKees Cook  * safe_hardlink_source - Check for safe hardlink conditions
703800179c9SKees Cook  * @inode: the source inode to hardlink from
704800179c9SKees Cook  *
705800179c9SKees Cook  * Return false if at least one of the following conditions:
706800179c9SKees Cook  *    - inode is not a regular file
707800179c9SKees Cook  *    - inode is setuid
708800179c9SKees Cook  *    - inode is setgid and group-exec
709800179c9SKees Cook  *    - access failure for read and write
710800179c9SKees Cook  *
711800179c9SKees Cook  * Otherwise returns true.
712800179c9SKees Cook  */
713800179c9SKees Cook static bool safe_hardlink_source(struct inode *inode)
714800179c9SKees Cook {
715800179c9SKees Cook 	umode_t mode = inode->i_mode;
716800179c9SKees Cook 
717800179c9SKees Cook 	/* Special files should not get pinned to the filesystem. */
718800179c9SKees Cook 	if (!S_ISREG(mode))
719800179c9SKees Cook 		return false;
720800179c9SKees Cook 
721800179c9SKees Cook 	/* Setuid files should not get pinned to the filesystem. */
722800179c9SKees Cook 	if (mode & S_ISUID)
723800179c9SKees Cook 		return false;
724800179c9SKees Cook 
725800179c9SKees Cook 	/* Executable setgid files should not get pinned to the filesystem. */
726800179c9SKees Cook 	if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
727800179c9SKees Cook 		return false;
728800179c9SKees Cook 
729800179c9SKees Cook 	/* Hardlinking to unreadable or unwritable sources is dangerous. */
730800179c9SKees Cook 	if (inode_permission(inode, MAY_READ | MAY_WRITE))
731800179c9SKees Cook 		return false;
732800179c9SKees Cook 
733800179c9SKees Cook 	return true;
734800179c9SKees Cook }
735800179c9SKees Cook 
736800179c9SKees Cook /**
737800179c9SKees Cook  * may_linkat - Check permissions for creating a hardlink
738800179c9SKees Cook  * @link: the source to hardlink from
739800179c9SKees Cook  *
740800179c9SKees Cook  * Block hardlink when all of:
741800179c9SKees Cook  *  - sysctl_protected_hardlinks enabled
742800179c9SKees Cook  *  - fsuid does not match inode
743800179c9SKees Cook  *  - hardlink source is unsafe (see safe_hardlink_source() above)
744800179c9SKees Cook  *  - not CAP_FOWNER
745800179c9SKees Cook  *
746800179c9SKees Cook  * Returns 0 if successful, -ve on error.
747800179c9SKees Cook  */
748800179c9SKees Cook static int may_linkat(struct path *link)
749800179c9SKees Cook {
750800179c9SKees Cook 	const struct cred *cred;
751800179c9SKees Cook 	struct inode *inode;
752800179c9SKees Cook 
753800179c9SKees Cook 	if (!sysctl_protected_hardlinks)
754800179c9SKees Cook 		return 0;
755800179c9SKees Cook 
756800179c9SKees Cook 	cred = current_cred();
757800179c9SKees Cook 	inode = link->dentry->d_inode;
758800179c9SKees Cook 
759800179c9SKees Cook 	/* Source inode owner (or CAP_FOWNER) can hardlink all they like,
760800179c9SKees Cook 	 * otherwise, it must be a safe source.
761800179c9SKees Cook 	 */
762800179c9SKees Cook 	if (cred->fsuid == inode->i_uid || safe_hardlink_source(inode) ||
763800179c9SKees Cook 	    capable(CAP_FOWNER))
764800179c9SKees Cook 		return 0;
765800179c9SKees Cook 
766a51d9eaaSKees Cook 	audit_log_link_denied("linkat", link);
767800179c9SKees Cook 	return -EPERM;
768800179c9SKees Cook }
769800179c9SKees Cook 
770def4af30SAl Viro static __always_inline int
771574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
7721da177e4SLinus Torvalds {
7737b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
7746d7b5aaeSAl Viro 	int error;
7756d7b5aaeSAl Viro 	char *s;
7761da177e4SLinus Torvalds 
777844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
778844a3917SAl Viro 
7790e794589SAl Viro 	if (link->mnt == nd->path.mnt)
7800e794589SAl Viro 		mntget(link->mnt);
7810e794589SAl Viro 
7826d7b5aaeSAl Viro 	error = -ELOOP;
7836d7b5aaeSAl Viro 	if (unlikely(current->total_link_count >= 40))
7846d7b5aaeSAl Viro 		goto out_put_nd_path;
7856d7b5aaeSAl Viro 
786574197e0SAl Viro 	cond_resched();
787574197e0SAl Viro 	current->total_link_count++;
788574197e0SAl Viro 
78968ac1234SAl Viro 	touch_atime(link);
7901da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
791cd4e91d3SAl Viro 
79236f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
7936d7b5aaeSAl Viro 	if (error)
7946d7b5aaeSAl Viro 		goto out_put_nd_path;
79536f3b4f6SAl Viro 
79686acdca1SAl Viro 	nd->last_type = LAST_BIND;
797def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
798def4af30SAl Viro 	error = PTR_ERR(*p);
7996d7b5aaeSAl Viro 	if (IS_ERR(*p))
800408ef013SChristoph Hellwig 		goto out_put_nd_path;
8016d7b5aaeSAl Viro 
802cc314eefSLinus Torvalds 	error = 0;
8036d7b5aaeSAl Viro 	s = nd_get_link(nd);
8046d7b5aaeSAl Viro 	if (s) {
8051da177e4SLinus Torvalds 		error = __vfs_follow_link(nd, s);
8066d7b5aaeSAl Viro 		if (unlikely(error))
8076d7b5aaeSAl Viro 			put_link(nd, link, *p);
808b5fb63c1SChristoph Hellwig 	}
8096d7b5aaeSAl Viro 
8106d7b5aaeSAl Viro 	return error;
8116d7b5aaeSAl Viro 
8126d7b5aaeSAl Viro out_put_nd_path:
8136d7b5aaeSAl Viro 	path_put(&nd->path);
8146d7b5aaeSAl Viro 	path_put(link);
8151da177e4SLinus Torvalds 	return error;
8161da177e4SLinus Torvalds }
8171da177e4SLinus Torvalds 
81831e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
81931e6b01fSNick Piggin {
8200714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
8210714a533SAl Viro 	struct mount *parent;
82231e6b01fSNick Piggin 	struct dentry *mountpoint;
82331e6b01fSNick Piggin 
8240714a533SAl Viro 	parent = mnt->mnt_parent;
8250714a533SAl Viro 	if (&parent->mnt == path->mnt)
82631e6b01fSNick Piggin 		return 0;
827a73324daSAl Viro 	mountpoint = mnt->mnt_mountpoint;
82831e6b01fSNick Piggin 	path->dentry = mountpoint;
8290714a533SAl Viro 	path->mnt = &parent->mnt;
83031e6b01fSNick Piggin 	return 1;
83131e6b01fSNick Piggin }
83231e6b01fSNick Piggin 
833f015f126SDavid Howells /*
834f015f126SDavid Howells  * follow_up - Find the mountpoint of path's vfsmount
835f015f126SDavid Howells  *
836f015f126SDavid Howells  * Given a path, find the mountpoint of its source file system.
837f015f126SDavid Howells  * Replace @path with the path of the mountpoint in the parent mount.
838f015f126SDavid Howells  * Up is towards /.
839f015f126SDavid Howells  *
840f015f126SDavid Howells  * Return 1 if we went up a level and 0 if we were already at the
841f015f126SDavid Howells  * root.
842f015f126SDavid Howells  */
843bab77ebfSAl Viro int follow_up(struct path *path)
8441da177e4SLinus Torvalds {
8450714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
8460714a533SAl Viro 	struct mount *parent;
8471da177e4SLinus Torvalds 	struct dentry *mountpoint;
84899b7db7bSNick Piggin 
849962830dfSAndi Kleen 	br_read_lock(&vfsmount_lock);
8500714a533SAl Viro 	parent = mnt->mnt_parent;
8513c0a6163SAl Viro 	if (parent == mnt) {
852962830dfSAndi Kleen 		br_read_unlock(&vfsmount_lock);
8531da177e4SLinus Torvalds 		return 0;
8541da177e4SLinus Torvalds 	}
8550714a533SAl Viro 	mntget(&parent->mnt);
856a73324daSAl Viro 	mountpoint = dget(mnt->mnt_mountpoint);
857962830dfSAndi Kleen 	br_read_unlock(&vfsmount_lock);
858bab77ebfSAl Viro 	dput(path->dentry);
859bab77ebfSAl Viro 	path->dentry = mountpoint;
860bab77ebfSAl Viro 	mntput(path->mnt);
8610714a533SAl Viro 	path->mnt = &parent->mnt;
8621da177e4SLinus Torvalds 	return 1;
8631da177e4SLinus Torvalds }
8641da177e4SLinus Torvalds 
865b5c84bf6SNick Piggin /*
8669875cf80SDavid Howells  * Perform an automount
8679875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
8689875cf80SDavid Howells  *   were called with.
8691da177e4SLinus Torvalds  */
8709875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
8719875cf80SDavid Howells 			    bool *need_mntput)
87231e6b01fSNick Piggin {
8739875cf80SDavid Howells 	struct vfsmount *mnt;
874ea5b778aSDavid Howells 	int err;
8759875cf80SDavid Howells 
8769875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
8779875cf80SDavid Howells 		return -EREMOTE;
8789875cf80SDavid Howells 
8790ec26fd0SMiklos Szeredi 	/* We don't want to mount if someone's just doing a stat -
8800ec26fd0SMiklos Szeredi 	 * unless they're stat'ing a directory and appended a '/' to
8810ec26fd0SMiklos Szeredi 	 * the name.
8820ec26fd0SMiklos Szeredi 	 *
8830ec26fd0SMiklos Szeredi 	 * We do, however, want to mount if someone wants to open or
8845a30d8a2SDavid Howells 	 * create a file of any type under the mountpoint, wants to
8850ec26fd0SMiklos Szeredi 	 * traverse through the mountpoint or wants to open the
8860ec26fd0SMiklos Szeredi 	 * mounted directory.  Also, autofs may mark negative dentries
8870ec26fd0SMiklos Szeredi 	 * as being automount points.  These will need the attentions
8880ec26fd0SMiklos Szeredi 	 * of the daemon to instantiate them before they can be used.
8895a30d8a2SDavid Howells 	 */
8905a30d8a2SDavid Howells 	if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
891d94c177bSLinus Torvalds 		     LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
8925a30d8a2SDavid Howells 	    path->dentry->d_inode)
8939875cf80SDavid Howells 		return -EISDIR;
8940ec26fd0SMiklos Szeredi 
8959875cf80SDavid Howells 	current->total_link_count++;
8969875cf80SDavid Howells 	if (current->total_link_count >= 40)
8979875cf80SDavid Howells 		return -ELOOP;
8989875cf80SDavid Howells 
8999875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
9009875cf80SDavid Howells 	if (IS_ERR(mnt)) {
9019875cf80SDavid Howells 		/*
9029875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
9039875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
9049875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
9059875cf80SDavid Howells 		 *
9069875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
9079875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
9089875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
9099875cf80SDavid Howells 		 */
91049084c3bSAl Viro 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
9119875cf80SDavid Howells 			return -EREMOTE;
9129875cf80SDavid Howells 		return PTR_ERR(mnt);
91331e6b01fSNick Piggin 	}
914ea5b778aSDavid Howells 
9159875cf80SDavid Howells 	if (!mnt) /* mount collision */
9169875cf80SDavid Howells 		return 0;
9179875cf80SDavid Howells 
9188aef1884SAl Viro 	if (!*need_mntput) {
9198aef1884SAl Viro 		/* lock_mount() may release path->mnt on error */
9208aef1884SAl Viro 		mntget(path->mnt);
9218aef1884SAl Viro 		*need_mntput = true;
9228aef1884SAl Viro 	}
92319a167afSAl Viro 	err = finish_automount(mnt, path);
924ea5b778aSDavid Howells 
925ea5b778aSDavid Howells 	switch (err) {
926ea5b778aSDavid Howells 	case -EBUSY:
927ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
92819a167afSAl Viro 		return 0;
929ea5b778aSDavid Howells 	case 0:
9308aef1884SAl Viro 		path_put(path);
9319875cf80SDavid Howells 		path->mnt = mnt;
9329875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
9339875cf80SDavid Howells 		return 0;
93419a167afSAl Viro 	default:
93519a167afSAl Viro 		return err;
9369875cf80SDavid Howells 	}
93719a167afSAl Viro 
938ea5b778aSDavid Howells }
9399875cf80SDavid Howells 
9409875cf80SDavid Howells /*
9419875cf80SDavid Howells  * Handle a dentry that is managed in some way.
942cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
9439875cf80SDavid Howells  * - Flagged as mountpoint
9449875cf80SDavid Howells  * - Flagged as automount point
9459875cf80SDavid Howells  *
9469875cf80SDavid Howells  * This may only be called in refwalk mode.
9479875cf80SDavid Howells  *
9489875cf80SDavid Howells  * Serialization is taken care of in namespace.c
9499875cf80SDavid Howells  */
9509875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
9519875cf80SDavid Howells {
9528aef1884SAl Viro 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
9539875cf80SDavid Howells 	unsigned managed;
9549875cf80SDavid Howells 	bool need_mntput = false;
9558aef1884SAl Viro 	int ret = 0;
9569875cf80SDavid Howells 
9579875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
9589875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
9599875cf80SDavid Howells 	 * the components of that value change under us */
9609875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
9619875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
9629875cf80SDavid Howells 	       unlikely(managed != 0)) {
963cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
964cc53ce53SDavid Howells 		 * being held. */
965cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
966cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
967cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
9681aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
969cc53ce53SDavid Howells 			if (ret < 0)
9708aef1884SAl Viro 				break;
971cc53ce53SDavid Howells 		}
972cc53ce53SDavid Howells 
9739875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
9749875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
9759875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
9769875cf80SDavid Howells 			if (mounted) {
9779875cf80SDavid Howells 				dput(path->dentry);
9789875cf80SDavid Howells 				if (need_mntput)
979463ffb2eSAl Viro 					mntput(path->mnt);
980463ffb2eSAl Viro 				path->mnt = mounted;
981463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
9829875cf80SDavid Howells 				need_mntput = true;
9839875cf80SDavid Howells 				continue;
984463ffb2eSAl Viro 			}
985463ffb2eSAl Viro 
9869875cf80SDavid Howells 			/* Something is mounted on this dentry in another
9879875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
9889875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
9899875cf80SDavid Howells 			 * vfsmount_lock */
9901da177e4SLinus Torvalds 		}
9919875cf80SDavid Howells 
9929875cf80SDavid Howells 		/* Handle an automount point */
9939875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
9949875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
9959875cf80SDavid Howells 			if (ret < 0)
9968aef1884SAl Viro 				break;
9979875cf80SDavid Howells 			continue;
9989875cf80SDavid Howells 		}
9999875cf80SDavid Howells 
10009875cf80SDavid Howells 		/* We didn't change the current path point */
10019875cf80SDavid Howells 		break;
10029875cf80SDavid Howells 	}
10038aef1884SAl Viro 
10048aef1884SAl Viro 	if (need_mntput && path->mnt == mnt)
10058aef1884SAl Viro 		mntput(path->mnt);
10068aef1884SAl Viro 	if (ret == -EISDIR)
10078aef1884SAl Viro 		ret = 0;
1008a3fbbde7SAl Viro 	return ret < 0 ? ret : need_mntput;
10091da177e4SLinus Torvalds }
10101da177e4SLinus Torvalds 
1011cc53ce53SDavid Howells int follow_down_one(struct path *path)
10121da177e4SLinus Torvalds {
10131da177e4SLinus Torvalds 	struct vfsmount *mounted;
10141da177e4SLinus Torvalds 
10151c755af4SAl Viro 	mounted = lookup_mnt(path);
10161da177e4SLinus Torvalds 	if (mounted) {
10179393bd07SAl Viro 		dput(path->dentry);
10189393bd07SAl Viro 		mntput(path->mnt);
10199393bd07SAl Viro 		path->mnt = mounted;
10209393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
10211da177e4SLinus Torvalds 		return 1;
10221da177e4SLinus Torvalds 	}
10231da177e4SLinus Torvalds 	return 0;
10241da177e4SLinus Torvalds }
10251da177e4SLinus Torvalds 
102662a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
102762a7375eSIan Kent {
102862a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
102962a7375eSIan Kent 		dentry->d_op->d_manage(dentry, true) < 0);
103062a7375eSIan Kent }
103162a7375eSIan Kent 
10329875cf80SDavid Howells /*
1033287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
1034287548e4SAl Viro  * we meet a managed dentry that would need blocking.
10359875cf80SDavid Howells  */
10369875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1037287548e4SAl Viro 			       struct inode **inode)
10389875cf80SDavid Howells {
103962a7375eSIan Kent 	for (;;) {
1040c7105365SAl Viro 		struct mount *mounted;
104162a7375eSIan Kent 		/*
104262a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
104362a7375eSIan Kent 		 * that wants to block transit.
104462a7375eSIan Kent 		 */
1045287548e4SAl Viro 		if (unlikely(managed_dentry_might_block(path->dentry)))
1046ab90911fSDavid Howells 			return false;
104762a7375eSIan Kent 
104862a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
104962a7375eSIan Kent 			break;
105062a7375eSIan Kent 
10519875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
10529875cf80SDavid Howells 		if (!mounted)
10539875cf80SDavid Howells 			break;
1054c7105365SAl Viro 		path->mnt = &mounted->mnt;
1055c7105365SAl Viro 		path->dentry = mounted->mnt.mnt_root;
1056a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
10579875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
105859430262SLinus Torvalds 		/*
105959430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
106059430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
106159430262SLinus Torvalds 		 * because a mount-point is always pinned.
106259430262SLinus Torvalds 		 */
106359430262SLinus Torvalds 		*inode = path->dentry->d_inode;
10649875cf80SDavid Howells 	}
10659875cf80SDavid Howells 	return true;
10669875cf80SDavid Howells }
10679875cf80SDavid Howells 
1068dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd)
1069287548e4SAl Viro {
1070dea39376SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
1071c7105365SAl Viro 		struct mount *mounted;
1072dea39376SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
1073287548e4SAl Viro 		if (!mounted)
1074287548e4SAl Viro 			break;
1075c7105365SAl Viro 		nd->path.mnt = &mounted->mnt;
1076c7105365SAl Viro 		nd->path.dentry = mounted->mnt.mnt_root;
1077dea39376SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1078287548e4SAl Viro 	}
1079287548e4SAl Viro }
1080287548e4SAl Viro 
108131e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
108231e6b01fSNick Piggin {
108331e6b01fSNick Piggin 	set_root_rcu(nd);
108431e6b01fSNick Piggin 
108531e6b01fSNick Piggin 	while (1) {
108631e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
108731e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
108831e6b01fSNick Piggin 			break;
108931e6b01fSNick Piggin 		}
109031e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
109131e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
109231e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
109331e6b01fSNick Piggin 			unsigned seq;
109431e6b01fSNick Piggin 
109531e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
109631e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
1097ef7562d5SAl Viro 				goto failed;
109831e6b01fSNick Piggin 			nd->path.dentry = parent;
109931e6b01fSNick Piggin 			nd->seq = seq;
110031e6b01fSNick Piggin 			break;
110131e6b01fSNick Piggin 		}
110231e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
110331e6b01fSNick Piggin 			break;
110431e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
110531e6b01fSNick Piggin 	}
1106dea39376SAl Viro 	follow_mount_rcu(nd);
1107dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
110831e6b01fSNick Piggin 	return 0;
1109ef7562d5SAl Viro 
1110ef7562d5SAl Viro failed:
1111ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
11125b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
1113ef7562d5SAl Viro 		nd->root.mnt = NULL;
111432a7991bSAl Viro 	unlock_rcu_walk();
1115ef7562d5SAl Viro 	return -ECHILD;
111631e6b01fSNick Piggin }
111731e6b01fSNick Piggin 
11189875cf80SDavid Howells /*
1119cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
1120cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
1121cc53ce53SDavid Howells  * caller is permitted to proceed or not.
1122cc53ce53SDavid Howells  */
11237cc90cc3SAl Viro int follow_down(struct path *path)
1124cc53ce53SDavid Howells {
1125cc53ce53SDavid Howells 	unsigned managed;
1126cc53ce53SDavid Howells 	int ret;
1127cc53ce53SDavid Howells 
1128cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
1129cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1130cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1131cc53ce53SDavid Howells 		 * being held.
1132cc53ce53SDavid Howells 		 *
1133cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
1134cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
1135cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
1136cc53ce53SDavid Howells 		 * superstructure.
1137cc53ce53SDavid Howells 		 *
1138cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
1139cc53ce53SDavid Howells 		 */
1140cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
1141cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1142cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
1143ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
11441aed3e42SAl Viro 				path->dentry, false);
1145cc53ce53SDavid Howells 			if (ret < 0)
1146cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
1147cc53ce53SDavid Howells 		}
1148cc53ce53SDavid Howells 
1149cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
1150cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
1151cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
1152cc53ce53SDavid Howells 			if (!mounted)
1153cc53ce53SDavid Howells 				break;
1154cc53ce53SDavid Howells 			dput(path->dentry);
1155cc53ce53SDavid Howells 			mntput(path->mnt);
1156cc53ce53SDavid Howells 			path->mnt = mounted;
1157cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1158cc53ce53SDavid Howells 			continue;
1159cc53ce53SDavid Howells 		}
1160cc53ce53SDavid Howells 
1161cc53ce53SDavid Howells 		/* Don't handle automount points here */
1162cc53ce53SDavid Howells 		break;
1163cc53ce53SDavid Howells 	}
1164cc53ce53SDavid Howells 	return 0;
1165cc53ce53SDavid Howells }
1166cc53ce53SDavid Howells 
1167cc53ce53SDavid Howells /*
11689875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
11699875cf80SDavid Howells  */
11709875cf80SDavid Howells static void follow_mount(struct path *path)
11719875cf80SDavid Howells {
11729875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
11739875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
11749875cf80SDavid Howells 		if (!mounted)
11759875cf80SDavid Howells 			break;
11769875cf80SDavid Howells 		dput(path->dentry);
11779875cf80SDavid Howells 		mntput(path->mnt);
11789875cf80SDavid Howells 		path->mnt = mounted;
11799875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
11809875cf80SDavid Howells 	}
11819875cf80SDavid Howells }
11829875cf80SDavid Howells 
118331e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
11841da177e4SLinus Torvalds {
11852a737871SAl Viro 	set_root(nd);
1186e518ddb7SAndreas Mohr 
11871da177e4SLinus Torvalds 	while(1) {
11884ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
11891da177e4SLinus Torvalds 
11902a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
11912a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
11921da177e4SLinus Torvalds 			break;
11931da177e4SLinus Torvalds 		}
11944ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
11953088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
11963088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
11971da177e4SLinus Torvalds 			dput(old);
11981da177e4SLinus Torvalds 			break;
11991da177e4SLinus Torvalds 		}
12003088dd70SAl Viro 		if (!follow_up(&nd->path))
12011da177e4SLinus Torvalds 			break;
12021da177e4SLinus Torvalds 	}
120379ed0226SAl Viro 	follow_mount(&nd->path);
120431e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
12051da177e4SLinus Torvalds }
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds /*
1208bad61189SMiklos Szeredi  * This looks up the name in dcache, possibly revalidates the old dentry and
1209bad61189SMiklos Szeredi  * allocates a new one if not found or not valid.  In the need_lookup argument
1210bad61189SMiklos Szeredi  * returns whether i_op->lookup is necessary.
1211bad61189SMiklos Szeredi  *
1212bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
1213baa03890SNick Piggin  */
1214bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1215201f956eSAl Viro 				    unsigned int flags, bool *need_lookup)
1216baa03890SNick Piggin {
1217baa03890SNick Piggin 	struct dentry *dentry;
1218bad61189SMiklos Szeredi 	int error;
1219baa03890SNick Piggin 
1220bad61189SMiklos Szeredi 	*need_lookup = false;
1221bad61189SMiklos Szeredi 	dentry = d_lookup(dir, name);
1222bad61189SMiklos Szeredi 	if (dentry) {
1223bad61189SMiklos Szeredi 		if (d_need_lookup(dentry)) {
1224bad61189SMiklos Szeredi 			*need_lookup = true;
1225bad61189SMiklos Szeredi 		} else if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1226201f956eSAl Viro 			error = d_revalidate(dentry, flags);
1227bad61189SMiklos Szeredi 			if (unlikely(error <= 0)) {
1228bad61189SMiklos Szeredi 				if (error < 0) {
1229bad61189SMiklos Szeredi 					dput(dentry);
1230bad61189SMiklos Szeredi 					return ERR_PTR(error);
1231bad61189SMiklos Szeredi 				} else if (!d_invalidate(dentry)) {
1232bad61189SMiklos Szeredi 					dput(dentry);
1233bad61189SMiklos Szeredi 					dentry = NULL;
1234bad61189SMiklos Szeredi 				}
1235bad61189SMiklos Szeredi 			}
1236bad61189SMiklos Szeredi 		}
1237bad61189SMiklos Szeredi 	}
1238baa03890SNick Piggin 
1239bad61189SMiklos Szeredi 	if (!dentry) {
1240bad61189SMiklos Szeredi 		dentry = d_alloc(dir, name);
1241baa03890SNick Piggin 		if (unlikely(!dentry))
1242baa03890SNick Piggin 			return ERR_PTR(-ENOMEM);
1243baa03890SNick Piggin 
1244bad61189SMiklos Szeredi 		*need_lookup = true;
1245baa03890SNick Piggin 	}
1246baa03890SNick Piggin 	return dentry;
1247baa03890SNick Piggin }
1248baa03890SNick Piggin 
1249baa03890SNick Piggin /*
1250bad61189SMiklos Szeredi  * Call i_op->lookup on the dentry.  The dentry must be negative but may be
1251bad61189SMiklos Szeredi  * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1252bad61189SMiklos Szeredi  *
1253bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
125444396f4bSJosef Bacik  */
1255bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
125672bd866aSAl Viro 				  unsigned int flags)
125744396f4bSJosef Bacik {
125844396f4bSJosef Bacik 	struct dentry *old;
125944396f4bSJosef Bacik 
126044396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
1261bad61189SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
1262e188dc02SMiklos Szeredi 		dput(dentry);
126344396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
1264e188dc02SMiklos Szeredi 	}
126544396f4bSJosef Bacik 
126672bd866aSAl Viro 	old = dir->i_op->lookup(dir, dentry, flags);
126744396f4bSJosef Bacik 	if (unlikely(old)) {
126844396f4bSJosef Bacik 		dput(dentry);
126944396f4bSJosef Bacik 		dentry = old;
127044396f4bSJosef Bacik 	}
127144396f4bSJosef Bacik 	return dentry;
127244396f4bSJosef Bacik }
127344396f4bSJosef Bacik 
1274a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name,
127572bd866aSAl Viro 		struct dentry *base, unsigned int flags)
1276a3255546SAl Viro {
1277bad61189SMiklos Szeredi 	bool need_lookup;
1278a3255546SAl Viro 	struct dentry *dentry;
1279a3255546SAl Viro 
128072bd866aSAl Viro 	dentry = lookup_dcache(name, base, flags, &need_lookup);
1281bad61189SMiklos Szeredi 	if (!need_lookup)
1282a3255546SAl Viro 		return dentry;
1283bad61189SMiklos Szeredi 
128472bd866aSAl Viro 	return lookup_real(base->d_inode, dentry, flags);
1285a3255546SAl Viro }
1286a3255546SAl Viro 
128744396f4bSJosef Bacik /*
12881da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
12891da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
12901da177e4SLinus Torvalds  *  It _is_ time-critical.
12911da177e4SLinus Torvalds  */
1292697f514dSMiklos Szeredi static int lookup_fast(struct nameidata *nd, struct qstr *name,
129331e6b01fSNick Piggin 		       struct path *path, struct inode **inode)
12941da177e4SLinus Torvalds {
12954ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
129631e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
12975a18fff2SAl Viro 	int need_reval = 1;
12985a18fff2SAl Viro 	int status = 1;
12999875cf80SDavid Howells 	int err;
13009875cf80SDavid Howells 
13013cac260aSAl Viro 	/*
1302b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1303b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1304b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1305b04f784eSNick Piggin 	 */
130631e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
130731e6b01fSNick Piggin 		unsigned seq;
130812f8ad4bSLinus Torvalds 		dentry = __d_lookup_rcu(parent, name, &seq, nd->inode);
13095a18fff2SAl Viro 		if (!dentry)
13105a18fff2SAl Viro 			goto unlazy;
13115a18fff2SAl Viro 
131212f8ad4bSLinus Torvalds 		/*
131312f8ad4bSLinus Torvalds 		 * This sequence count validates that the inode matches
131412f8ad4bSLinus Torvalds 		 * the dentry name information from lookup.
131512f8ad4bSLinus Torvalds 		 */
131612f8ad4bSLinus Torvalds 		*inode = dentry->d_inode;
131712f8ad4bSLinus Torvalds 		if (read_seqcount_retry(&dentry->d_seq, seq))
131812f8ad4bSLinus Torvalds 			return -ECHILD;
131912f8ad4bSLinus Torvalds 
132012f8ad4bSLinus Torvalds 		/*
132112f8ad4bSLinus Torvalds 		 * This sequence count validates that the parent had no
132212f8ad4bSLinus Torvalds 		 * changes while we did the lookup of the dentry above.
132312f8ad4bSLinus Torvalds 		 *
132412f8ad4bSLinus Torvalds 		 * The memory barrier in read_seqcount_begin of child is
132512f8ad4bSLinus Torvalds 		 *  enough, we can use __read_seqcount_retry here.
132612f8ad4bSLinus Torvalds 		 */
132731e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
132831e6b01fSNick Piggin 			return -ECHILD;
132931e6b01fSNick Piggin 		nd->seq = seq;
13305a18fff2SAl Viro 
1331fa4ee159SMiklos Szeredi 		if (unlikely(d_need_lookup(dentry)))
1332fa4ee159SMiklos Szeredi 			goto unlazy;
133324643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
13344ce16ef3SAl Viro 			status = d_revalidate(dentry, nd->flags);
13355a18fff2SAl Viro 			if (unlikely(status <= 0)) {
13365a18fff2SAl Viro 				if (status != -ECHILD)
13375a18fff2SAl Viro 					need_reval = 0;
13385a18fff2SAl Viro 				goto unlazy;
13395a18fff2SAl Viro 			}
134024643087SAl Viro 		}
134131e6b01fSNick Piggin 		path->mnt = mnt;
134231e6b01fSNick Piggin 		path->dentry = dentry;
1343d6e9bd25SAl Viro 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1344d6e9bd25SAl Viro 			goto unlazy;
1345d6e9bd25SAl Viro 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1346d6e9bd25SAl Viro 			goto unlazy;
13479875cf80SDavid Howells 		return 0;
13485a18fff2SAl Viro unlazy:
134919660af7SAl Viro 		if (unlazy_walk(nd, dentry))
13505a18fff2SAl Viro 			return -ECHILD;
13515a18fff2SAl Viro 	} else {
135231e6b01fSNick Piggin 		dentry = __d_lookup(parent, name);
135324643087SAl Viro 	}
13545a18fff2SAl Viro 
135581e6f520SAl Viro 	if (unlikely(!dentry))
135681e6f520SAl Viro 		goto need_lookup;
13575a18fff2SAl Viro 
135881e6f520SAl Viro 	if (unlikely(d_need_lookup(dentry))) {
135981e6f520SAl Viro 		dput(dentry);
136081e6f520SAl Viro 		goto need_lookup;
13615a18fff2SAl Viro 	}
136281e6f520SAl Viro 
13635a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
13644ce16ef3SAl Viro 		status = d_revalidate(dentry, nd->flags);
13655a18fff2SAl Viro 	if (unlikely(status <= 0)) {
13665a18fff2SAl Viro 		if (status < 0) {
13675a18fff2SAl Viro 			dput(dentry);
13685a18fff2SAl Viro 			return status;
13695a18fff2SAl Viro 		}
13705a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
13715a18fff2SAl Viro 			dput(dentry);
137281e6f520SAl Viro 			goto need_lookup;
13735a18fff2SAl Viro 		}
13745a18fff2SAl Viro 	}
1375697f514dSMiklos Szeredi 
13761da177e4SLinus Torvalds 	path->mnt = mnt;
13771da177e4SLinus Torvalds 	path->dentry = dentry;
13789875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
137989312214SIan Kent 	if (unlikely(err < 0)) {
138089312214SIan Kent 		path_put_conditional(path, nd);
13819875cf80SDavid Howells 		return err;
138289312214SIan Kent 	}
1383a3fbbde7SAl Viro 	if (err)
1384a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
138531e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
13861da177e4SLinus Torvalds 	return 0;
138781e6f520SAl Viro 
138881e6f520SAl Viro need_lookup:
1389697f514dSMiklos Szeredi 	return 1;
1390697f514dSMiklos Szeredi }
1391697f514dSMiklos Szeredi 
1392697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */
1393697f514dSMiklos Szeredi static int lookup_slow(struct nameidata *nd, struct qstr *name,
1394697f514dSMiklos Szeredi 		       struct path *path)
1395697f514dSMiklos Szeredi {
1396697f514dSMiklos Szeredi 	struct dentry *dentry, *parent;
1397697f514dSMiklos Szeredi 	int err;
1398697f514dSMiklos Szeredi 
1399697f514dSMiklos Szeredi 	parent = nd->path.dentry;
140081e6f520SAl Viro 	BUG_ON(nd->inode != parent->d_inode);
140181e6f520SAl Viro 
140281e6f520SAl Viro 	mutex_lock(&parent->d_inode->i_mutex);
140372bd866aSAl Viro 	dentry = __lookup_hash(name, parent, nd->flags);
140481e6f520SAl Viro 	mutex_unlock(&parent->d_inode->i_mutex);
140581e6f520SAl Viro 	if (IS_ERR(dentry))
140681e6f520SAl Viro 		return PTR_ERR(dentry);
1407697f514dSMiklos Szeredi 	path->mnt = nd->path.mnt;
1408697f514dSMiklos Szeredi 	path->dentry = dentry;
1409697f514dSMiklos Szeredi 	err = follow_managed(path, nd->flags);
1410697f514dSMiklos Szeredi 	if (unlikely(err < 0)) {
1411697f514dSMiklos Szeredi 		path_put_conditional(path, nd);
1412697f514dSMiklos Szeredi 		return err;
1413697f514dSMiklos Szeredi 	}
1414697f514dSMiklos Szeredi 	if (err)
1415697f514dSMiklos Szeredi 		nd->flags |= LOOKUP_JUMPED;
1416697f514dSMiklos Szeredi 	return 0;
14171da177e4SLinus Torvalds }
14181da177e4SLinus Torvalds 
141952094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
142052094c8aSAl Viro {
142152094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
14224ad5abb3SAl Viro 		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
142352094c8aSAl Viro 		if (err != -ECHILD)
142452094c8aSAl Viro 			return err;
142519660af7SAl Viro 		if (unlazy_walk(nd, NULL))
142652094c8aSAl Viro 			return -ECHILD;
142752094c8aSAl Viro 	}
14284ad5abb3SAl Viro 	return inode_permission(nd->inode, MAY_EXEC);
142952094c8aSAl Viro }
143052094c8aSAl Viro 
14319856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
14329856fa1bSAl Viro {
14339856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
14349856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
14359856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
14369856fa1bSAl Viro 				return -ECHILD;
14379856fa1bSAl Viro 		} else
14389856fa1bSAl Viro 			follow_dotdot(nd);
14399856fa1bSAl Viro 	}
14409856fa1bSAl Viro 	return 0;
14419856fa1bSAl Viro }
14429856fa1bSAl Viro 
1443951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1444951361f9SAl Viro {
1445951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1446951361f9SAl Viro 		path_put(&nd->path);
1447951361f9SAl Viro 	} else {
1448951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
14495b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1450951361f9SAl Viro 			nd->root.mnt = NULL;
145132a7991bSAl Viro 		unlock_rcu_walk();
1452951361f9SAl Viro 	}
1453951361f9SAl Viro }
1454951361f9SAl Viro 
14553ddcd056SLinus Torvalds /*
14563ddcd056SLinus Torvalds  * Do we need to follow links? We _really_ want to be able
14573ddcd056SLinus Torvalds  * to do this check without having to look at inode->i_op,
14583ddcd056SLinus Torvalds  * so we keep a cache of "no, this doesn't need follow_link"
14593ddcd056SLinus Torvalds  * for the common case.
14603ddcd056SLinus Torvalds  */
14617813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow)
14623ddcd056SLinus Torvalds {
14633ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
14643ddcd056SLinus Torvalds 		if (likely(inode->i_op->follow_link))
14653ddcd056SLinus Torvalds 			return follow;
14663ddcd056SLinus Torvalds 
14673ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
14683ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
14693ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_NOFOLLOW;
14703ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
14713ddcd056SLinus Torvalds 	}
14723ddcd056SLinus Torvalds 	return 0;
14733ddcd056SLinus Torvalds }
14743ddcd056SLinus Torvalds 
1475ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
1476ce57dfc1SAl Viro 		struct qstr *name, int type, int follow)
1477ce57dfc1SAl Viro {
1478ce57dfc1SAl Viro 	struct inode *inode;
1479ce57dfc1SAl Viro 	int err;
1480ce57dfc1SAl Viro 	/*
1481ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1482ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1483ce57dfc1SAl Viro 	 * parent relationships.
1484ce57dfc1SAl Viro 	 */
1485ce57dfc1SAl Viro 	if (unlikely(type != LAST_NORM))
1486ce57dfc1SAl Viro 		return handle_dots(nd, type);
1487697f514dSMiklos Szeredi 	err = lookup_fast(nd, name, path, &inode);
1488ce57dfc1SAl Viro 	if (unlikely(err)) {
1489697f514dSMiklos Szeredi 		if (err < 0)
1490697f514dSMiklos Szeredi 			goto out_err;
1491697f514dSMiklos Szeredi 
1492697f514dSMiklos Szeredi 		err = lookup_slow(nd, name, path);
1493697f514dSMiklos Szeredi 		if (err < 0)
1494697f514dSMiklos Szeredi 			goto out_err;
1495697f514dSMiklos Szeredi 
1496697f514dSMiklos Szeredi 		inode = path->dentry->d_inode;
1497ce57dfc1SAl Viro 	}
1498697f514dSMiklos Szeredi 	err = -ENOENT;
1499697f514dSMiklos Szeredi 	if (!inode)
1500697f514dSMiklos Szeredi 		goto out_path_put;
1501697f514dSMiklos Szeredi 
15027813b94aSLinus Torvalds 	if (should_follow_link(inode, follow)) {
150319660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
150419660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
1505697f514dSMiklos Szeredi 				err = -ECHILD;
1506697f514dSMiklos Szeredi 				goto out_err;
150719660af7SAl Viro 			}
150819660af7SAl Viro 		}
1509ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1510ce57dfc1SAl Viro 		return 1;
1511ce57dfc1SAl Viro 	}
1512ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1513ce57dfc1SAl Viro 	nd->inode = inode;
1514ce57dfc1SAl Viro 	return 0;
1515697f514dSMiklos Szeredi 
1516697f514dSMiklos Szeredi out_path_put:
1517697f514dSMiklos Szeredi 	path_to_nameidata(path, nd);
1518697f514dSMiklos Szeredi out_err:
1519697f514dSMiklos Szeredi 	terminate_walk(nd);
1520697f514dSMiklos Szeredi 	return err;
1521ce57dfc1SAl Viro }
1522ce57dfc1SAl Viro 
15231da177e4SLinus Torvalds /*
1524b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1525b356379aSAl Viro  * limiting consecutive symlinks to 40.
1526b356379aSAl Viro  *
1527b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1528b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1529b356379aSAl Viro  */
1530b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1531b356379aSAl Viro {
1532b356379aSAl Viro 	int res;
1533b356379aSAl Viro 
1534b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1535b356379aSAl Viro 		path_put_conditional(path, nd);
1536b356379aSAl Viro 		path_put(&nd->path);
1537b356379aSAl Viro 		return -ELOOP;
1538b356379aSAl Viro 	}
15391a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1540b356379aSAl Viro 
1541b356379aSAl Viro 	nd->depth++;
1542b356379aSAl Viro 	current->link_count++;
1543b356379aSAl Viro 
1544b356379aSAl Viro 	do {
1545b356379aSAl Viro 		struct path link = *path;
1546b356379aSAl Viro 		void *cookie;
1547574197e0SAl Viro 
1548574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
15496d7b5aaeSAl Viro 		if (res)
15506d7b5aaeSAl Viro 			break;
1551b356379aSAl Viro 		res = walk_component(nd, path, &nd->last,
1552b356379aSAl Viro 				     nd->last_type, LOOKUP_FOLLOW);
1553574197e0SAl Viro 		put_link(nd, &link, cookie);
1554b356379aSAl Viro 	} while (res > 0);
1555b356379aSAl Viro 
1556b356379aSAl Viro 	current->link_count--;
1557b356379aSAl Viro 	nd->depth--;
1558b356379aSAl Viro 	return res;
1559b356379aSAl Viro }
1560b356379aSAl Viro 
1561b356379aSAl Viro /*
15623ddcd056SLinus Torvalds  * We really don't want to look at inode->i_op->lookup
15633ddcd056SLinus Torvalds  * when we don't have to. So we keep a cache bit in
15643ddcd056SLinus Torvalds  * the inode ->i_opflags field that says "yes, we can
15653ddcd056SLinus Torvalds  * do lookup on this inode".
15663ddcd056SLinus Torvalds  */
15673ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode)
15683ddcd056SLinus Torvalds {
15693ddcd056SLinus Torvalds 	if (likely(inode->i_opflags & IOP_LOOKUP))
15703ddcd056SLinus Torvalds 		return 1;
15713ddcd056SLinus Torvalds 	if (likely(!inode->i_op->lookup))
15723ddcd056SLinus Torvalds 		return 0;
15733ddcd056SLinus Torvalds 
15743ddcd056SLinus Torvalds 	/* We do this once for the lifetime of the inode */
15753ddcd056SLinus Torvalds 	spin_lock(&inode->i_lock);
15763ddcd056SLinus Torvalds 	inode->i_opflags |= IOP_LOOKUP;
15773ddcd056SLinus Torvalds 	spin_unlock(&inode->i_lock);
15783ddcd056SLinus Torvalds 	return 1;
15793ddcd056SLinus Torvalds }
15803ddcd056SLinus Torvalds 
1581bfcfaa77SLinus Torvalds /*
1582bfcfaa77SLinus Torvalds  * We can do the critical dentry name comparison and hashing
1583bfcfaa77SLinus Torvalds  * operations one word at a time, but we are limited to:
1584bfcfaa77SLinus Torvalds  *
1585bfcfaa77SLinus Torvalds  * - Architectures with fast unaligned word accesses. We could
1586bfcfaa77SLinus Torvalds  *   do a "get_unaligned()" if this helps and is sufficiently
1587bfcfaa77SLinus Torvalds  *   fast.
1588bfcfaa77SLinus Torvalds  *
1589bfcfaa77SLinus Torvalds  * - Little-endian machines (so that we can generate the mask
1590bfcfaa77SLinus Torvalds  *   of low bytes efficiently). Again, we *could* do a byte
1591bfcfaa77SLinus Torvalds  *   swapping load on big-endian architectures if that is not
1592bfcfaa77SLinus Torvalds  *   expensive enough to make the optimization worthless.
1593bfcfaa77SLinus Torvalds  *
1594bfcfaa77SLinus Torvalds  * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1595bfcfaa77SLinus Torvalds  *   do not trap on the (extremely unlikely) case of a page
1596bfcfaa77SLinus Torvalds  *   crossing operation.
1597bfcfaa77SLinus Torvalds  *
1598bfcfaa77SLinus Torvalds  * - Furthermore, we need an efficient 64-bit compile for the
1599bfcfaa77SLinus Torvalds  *   64-bit case in order to generate the "number of bytes in
1600bfcfaa77SLinus Torvalds  *   the final mask". Again, that could be replaced with a
1601bfcfaa77SLinus Torvalds  *   efficient population count instruction or similar.
1602bfcfaa77SLinus Torvalds  */
1603bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS
1604bfcfaa77SLinus Torvalds 
1605f68e556eSLinus Torvalds #include <asm/word-at-a-time.h>
1606bfcfaa77SLinus Torvalds 
1607f68e556eSLinus Torvalds #ifdef CONFIG_64BIT
1608bfcfaa77SLinus Torvalds 
1609bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash)
1610bfcfaa77SLinus Torvalds {
1611bfcfaa77SLinus Torvalds 	hash += hash >> (8*sizeof(int));
1612bfcfaa77SLinus Torvalds 	return hash;
1613bfcfaa77SLinus Torvalds }
1614bfcfaa77SLinus Torvalds 
1615bfcfaa77SLinus Torvalds #else	/* 32-bit case */
1616bfcfaa77SLinus Torvalds 
1617bfcfaa77SLinus Torvalds #define fold_hash(x) (x)
1618bfcfaa77SLinus Torvalds 
1619bfcfaa77SLinus Torvalds #endif
1620bfcfaa77SLinus Torvalds 
1621bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1622bfcfaa77SLinus Torvalds {
1623bfcfaa77SLinus Torvalds 	unsigned long a, mask;
1624bfcfaa77SLinus Torvalds 	unsigned long hash = 0;
1625bfcfaa77SLinus Torvalds 
1626bfcfaa77SLinus Torvalds 	for (;;) {
1627e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name);
1628bfcfaa77SLinus Torvalds 		if (len < sizeof(unsigned long))
1629bfcfaa77SLinus Torvalds 			break;
1630bfcfaa77SLinus Torvalds 		hash += a;
1631f132c5beSAl Viro 		hash *= 9;
1632bfcfaa77SLinus Torvalds 		name += sizeof(unsigned long);
1633bfcfaa77SLinus Torvalds 		len -= sizeof(unsigned long);
1634bfcfaa77SLinus Torvalds 		if (!len)
1635bfcfaa77SLinus Torvalds 			goto done;
1636bfcfaa77SLinus Torvalds 	}
1637bfcfaa77SLinus Torvalds 	mask = ~(~0ul << len*8);
1638bfcfaa77SLinus Torvalds 	hash += mask & a;
1639bfcfaa77SLinus Torvalds done:
1640bfcfaa77SLinus Torvalds 	return fold_hash(hash);
1641bfcfaa77SLinus Torvalds }
1642bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
1643bfcfaa77SLinus Torvalds 
1644bfcfaa77SLinus Torvalds /*
1645bfcfaa77SLinus Torvalds  * Calculate the length and hash of the path component, and
1646bfcfaa77SLinus Torvalds  * return the length of the component;
1647bfcfaa77SLinus Torvalds  */
1648bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1649bfcfaa77SLinus Torvalds {
165036126f8fSLinus Torvalds 	unsigned long a, b, adata, bdata, mask, hash, len;
165136126f8fSLinus Torvalds 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1652bfcfaa77SLinus Torvalds 
1653bfcfaa77SLinus Torvalds 	hash = a = 0;
1654bfcfaa77SLinus Torvalds 	len = -sizeof(unsigned long);
1655bfcfaa77SLinus Torvalds 	do {
1656bfcfaa77SLinus Torvalds 		hash = (hash + a) * 9;
1657bfcfaa77SLinus Torvalds 		len += sizeof(unsigned long);
1658e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name+len);
165936126f8fSLinus Torvalds 		b = a ^ REPEAT_BYTE('/');
166036126f8fSLinus Torvalds 	} while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1661bfcfaa77SLinus Torvalds 
166236126f8fSLinus Torvalds 	adata = prep_zero_mask(a, adata, &constants);
166336126f8fSLinus Torvalds 	bdata = prep_zero_mask(b, bdata, &constants);
166436126f8fSLinus Torvalds 
166536126f8fSLinus Torvalds 	mask = create_zero_mask(adata | bdata);
166636126f8fSLinus Torvalds 
166736126f8fSLinus Torvalds 	hash += a & zero_bytemask(mask);
1668bfcfaa77SLinus Torvalds 	*hashp = fold_hash(hash);
1669bfcfaa77SLinus Torvalds 
167036126f8fSLinus Torvalds 	return len + find_zero(mask);
1671bfcfaa77SLinus Torvalds }
1672bfcfaa77SLinus Torvalds 
1673bfcfaa77SLinus Torvalds #else
1674bfcfaa77SLinus Torvalds 
16750145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
16760145acc2SLinus Torvalds {
16770145acc2SLinus Torvalds 	unsigned long hash = init_name_hash();
16780145acc2SLinus Torvalds 	while (len--)
16790145acc2SLinus Torvalds 		hash = partial_name_hash(*name++, hash);
16800145acc2SLinus Torvalds 	return end_name_hash(hash);
16810145acc2SLinus Torvalds }
1682ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
16830145acc2SLinus Torvalds 
16843ddcd056SLinus Torvalds /*
1685200e9ef7SLinus Torvalds  * We know there's a real path component here of at least
1686200e9ef7SLinus Torvalds  * one character.
1687200e9ef7SLinus Torvalds  */
1688200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1689200e9ef7SLinus Torvalds {
1690200e9ef7SLinus Torvalds 	unsigned long hash = init_name_hash();
1691200e9ef7SLinus Torvalds 	unsigned long len = 0, c;
1692200e9ef7SLinus Torvalds 
1693200e9ef7SLinus Torvalds 	c = (unsigned char)*name;
1694200e9ef7SLinus Torvalds 	do {
1695200e9ef7SLinus Torvalds 		len++;
1696200e9ef7SLinus Torvalds 		hash = partial_name_hash(c, hash);
1697200e9ef7SLinus Torvalds 		c = (unsigned char)name[len];
1698200e9ef7SLinus Torvalds 	} while (c && c != '/');
1699200e9ef7SLinus Torvalds 	*hashp = end_name_hash(hash);
1700200e9ef7SLinus Torvalds 	return len;
1701200e9ef7SLinus Torvalds }
1702200e9ef7SLinus Torvalds 
1703bfcfaa77SLinus Torvalds #endif
1704bfcfaa77SLinus Torvalds 
1705200e9ef7SLinus Torvalds /*
17061da177e4SLinus Torvalds  * Name resolution.
1707ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1708ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
17091da177e4SLinus Torvalds  *
1710ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1711ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
17121da177e4SLinus Torvalds  */
17136de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
17141da177e4SLinus Torvalds {
17151da177e4SLinus Torvalds 	struct path next;
17161da177e4SLinus Torvalds 	int err;
17171da177e4SLinus Torvalds 
17181da177e4SLinus Torvalds 	while (*name=='/')
17191da177e4SLinus Torvalds 		name++;
17201da177e4SLinus Torvalds 	if (!*name)
1721086e183aSAl Viro 		return 0;
17221da177e4SLinus Torvalds 
17231da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
17241da177e4SLinus Torvalds 	for(;;) {
17251da177e4SLinus Torvalds 		struct qstr this;
1726200e9ef7SLinus Torvalds 		long len;
1727fe479a58SAl Viro 		int type;
17281da177e4SLinus Torvalds 
172952094c8aSAl Viro 		err = may_lookup(nd);
17301da177e4SLinus Torvalds  		if (err)
17311da177e4SLinus Torvalds 			break;
17321da177e4SLinus Torvalds 
1733200e9ef7SLinus Torvalds 		len = hash_name(name, &this.hash);
17341da177e4SLinus Torvalds 		this.name = name;
1735200e9ef7SLinus Torvalds 		this.len = len;
17361da177e4SLinus Torvalds 
1737fe479a58SAl Viro 		type = LAST_NORM;
1738200e9ef7SLinus Torvalds 		if (name[0] == '.') switch (len) {
1739fe479a58SAl Viro 			case 2:
1740200e9ef7SLinus Torvalds 				if (name[1] == '.') {
1741fe479a58SAl Viro 					type = LAST_DOTDOT;
174216c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
174316c2cd71SAl Viro 				}
1744fe479a58SAl Viro 				break;
1745fe479a58SAl Viro 			case 1:
1746fe479a58SAl Viro 				type = LAST_DOT;
1747fe479a58SAl Viro 		}
17485a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
17495a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
175016c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
17515a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
17525a202bcdSAl Viro 				err = parent->d_op->d_hash(parent, nd->inode,
17535a202bcdSAl Viro 							   &this);
17545a202bcdSAl Viro 				if (err < 0)
17555a202bcdSAl Viro 					break;
17565a202bcdSAl Viro 			}
17575a202bcdSAl Viro 		}
1758fe479a58SAl Viro 
1759200e9ef7SLinus Torvalds 		if (!name[len])
17601da177e4SLinus Torvalds 			goto last_component;
1761200e9ef7SLinus Torvalds 		/*
1762200e9ef7SLinus Torvalds 		 * If it wasn't NUL, we know it was '/'. Skip that
1763200e9ef7SLinus Torvalds 		 * slash, and continue until no more slashes.
1764200e9ef7SLinus Torvalds 		 */
1765200e9ef7SLinus Torvalds 		do {
1766200e9ef7SLinus Torvalds 			len++;
1767200e9ef7SLinus Torvalds 		} while (unlikely(name[len] == '/'));
1768200e9ef7SLinus Torvalds 		if (!name[len])
1769b356379aSAl Viro 			goto last_component;
1770200e9ef7SLinus Torvalds 		name += len;
17711da177e4SLinus Torvalds 
1772ce57dfc1SAl Viro 		err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1773ce57dfc1SAl Viro 		if (err < 0)
1774ce57dfc1SAl Viro 			return err;
1775fe479a58SAl Viro 
1776ce57dfc1SAl Viro 		if (err) {
1777b356379aSAl Viro 			err = nested_symlink(&next, nd);
17781da177e4SLinus Torvalds 			if (err)
1779a7472babSAl Viro 				return err;
178031e6b01fSNick Piggin 		}
17813ddcd056SLinus Torvalds 		if (can_lookup(nd->inode))
17821da177e4SLinus Torvalds 			continue;
17833ddcd056SLinus Torvalds 		err = -ENOTDIR;
17843ddcd056SLinus Torvalds 		break;
17851da177e4SLinus Torvalds 		/* here ends the main loop */
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds last_component:
1788ce57dfc1SAl Viro 		nd->last = this;
1789ce57dfc1SAl Viro 		nd->last_type = type;
1790ce57dfc1SAl Viro 		return 0;
1791ce57dfc1SAl Viro 	}
1792951361f9SAl Viro 	terminate_walk(nd);
17931da177e4SLinus Torvalds 	return err;
17941da177e4SLinus Torvalds }
17951da177e4SLinus Torvalds 
179670e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
179770e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
179831e6b01fSNick Piggin {
179931e6b01fSNick Piggin 	int retval = 0;
180031e6b01fSNick Piggin 	int fput_needed;
180131e6b01fSNick Piggin 	struct file *file;
180231e6b01fSNick Piggin 
180331e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
180416c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
180531e6b01fSNick Piggin 	nd->depth = 0;
18065b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
18075b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
180873d049a4SAl Viro 		if (*name) {
18095b6ca027SAl Viro 			if (!inode->i_op->lookup)
18105b6ca027SAl Viro 				return -ENOTDIR;
18115b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
18125b6ca027SAl Viro 			if (retval)
18135b6ca027SAl Viro 				return retval;
181473d049a4SAl Viro 		}
18155b6ca027SAl Viro 		nd->path = nd->root;
18165b6ca027SAl Viro 		nd->inode = inode;
18175b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
181832a7991bSAl Viro 			lock_rcu_walk();
18195b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
18205b6ca027SAl Viro 		} else {
18215b6ca027SAl Viro 			path_get(&nd->path);
18225b6ca027SAl Viro 		}
18235b6ca027SAl Viro 		return 0;
18245b6ca027SAl Viro 	}
18255b6ca027SAl Viro 
182631e6b01fSNick Piggin 	nd->root.mnt = NULL;
182731e6b01fSNick Piggin 
182831e6b01fSNick Piggin 	if (*name=='/') {
1829e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
183032a7991bSAl Viro 			lock_rcu_walk();
1831e41f7d4eSAl Viro 			set_root_rcu(nd);
1832e41f7d4eSAl Viro 		} else {
1833e41f7d4eSAl Viro 			set_root(nd);
1834e41f7d4eSAl Viro 			path_get(&nd->root);
1835e41f7d4eSAl Viro 		}
183631e6b01fSNick Piggin 		nd->path = nd->root;
183731e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1838e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
183931e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1840c28cc364SNick Piggin 			unsigned seq;
184131e6b01fSNick Piggin 
184232a7991bSAl Viro 			lock_rcu_walk();
184331e6b01fSNick Piggin 
1844c28cc364SNick Piggin 			do {
1845c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
184631e6b01fSNick Piggin 				nd->path = fs->pwd;
1847c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1848c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1849e41f7d4eSAl Viro 		} else {
1850e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1851e41f7d4eSAl Viro 		}
185231e6b01fSNick Piggin 	} else {
185331e6b01fSNick Piggin 		struct dentry *dentry;
185431e6b01fSNick Piggin 
18551abf0c71SAl Viro 		file = fget_raw_light(dfd, &fput_needed);
185631e6b01fSNick Piggin 		retval = -EBADF;
185731e6b01fSNick Piggin 		if (!file)
185831e6b01fSNick Piggin 			goto out_fail;
185931e6b01fSNick Piggin 
186031e6b01fSNick Piggin 		dentry = file->f_path.dentry;
186131e6b01fSNick Piggin 
1862f52e0c11SAl Viro 		if (*name) {
186331e6b01fSNick Piggin 			retval = -ENOTDIR;
186431e6b01fSNick Piggin 			if (!S_ISDIR(dentry->d_inode->i_mode))
186531e6b01fSNick Piggin 				goto fput_fail;
186631e6b01fSNick Piggin 
18674ad5abb3SAl Viro 			retval = inode_permission(dentry->d_inode, MAY_EXEC);
186831e6b01fSNick Piggin 			if (retval)
186931e6b01fSNick Piggin 				goto fput_fail;
1870f52e0c11SAl Viro 		}
187131e6b01fSNick Piggin 
187231e6b01fSNick Piggin 		nd->path = file->f_path;
1873e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
187431e6b01fSNick Piggin 			if (fput_needed)
187570e9b357SAl Viro 				*fp = file;
1876c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
187732a7991bSAl Viro 			lock_rcu_walk();
18785590ff0dSUlrich Drepper 		} else {
18795dd784d0SJan Blunck 			path_get(&file->f_path);
18805590ff0dSUlrich Drepper 			fput_light(file, fput_needed);
18811da177e4SLinus Torvalds 		}
1882e41f7d4eSAl Viro 	}
1883e41f7d4eSAl Viro 
188431e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
18859b4a9b14SAl Viro 	return 0;
18862dfdd266SJosef 'Jeff' Sipek 
18879b4a9b14SAl Viro fput_fail:
18889b4a9b14SAl Viro 	fput_light(file, fput_needed);
18899b4a9b14SAl Viro out_fail:
18909b4a9b14SAl Viro 	return retval;
18919b4a9b14SAl Viro }
18929b4a9b14SAl Viro 
1893bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1894bd92d7feSAl Viro {
1895bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1896bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1897bd92d7feSAl Viro 
1898bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
1899bd92d7feSAl Viro 	return walk_component(nd, path, &nd->last, nd->last_type,
1900bd92d7feSAl Viro 					nd->flags & LOOKUP_FOLLOW);
1901bd92d7feSAl Viro }
1902bd92d7feSAl Viro 
19039b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1904ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
19059b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
19069b4a9b14SAl Viro {
190770e9b357SAl Viro 	struct file *base = NULL;
1908bd92d7feSAl Viro 	struct path path;
1909bd92d7feSAl Viro 	int err;
191031e6b01fSNick Piggin 
191131e6b01fSNick Piggin 	/*
191231e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
191331e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
191431e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
191531e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
191631e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
191731e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
191831e6b01fSNick Piggin 	 * analogue, foo_rcu().
191931e6b01fSNick Piggin 	 *
192031e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
192131e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
192231e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
192331e6b01fSNick Piggin 	 * be able to complete).
192431e6b01fSNick Piggin 	 */
1925bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1926ee0827cdSAl Viro 
1927bd92d7feSAl Viro 	if (unlikely(err))
1928bd92d7feSAl Viro 		return err;
1929ee0827cdSAl Viro 
1930ee0827cdSAl Viro 	current->total_link_count = 0;
1931bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1932bd92d7feSAl Viro 
1933bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1934bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1935bd92d7feSAl Viro 		while (err > 0) {
1936bd92d7feSAl Viro 			void *cookie;
1937bd92d7feSAl Viro 			struct path link = path;
1938800179c9SKees Cook 			err = may_follow_link(&link, nd);
1939800179c9SKees Cook 			if (unlikely(err))
1940800179c9SKees Cook 				break;
1941bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1942574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
19436d7b5aaeSAl Viro 			if (err)
19446d7b5aaeSAl Viro 				break;
1945bd92d7feSAl Viro 			err = lookup_last(nd, &path);
1946574197e0SAl Viro 			put_link(nd, &link, cookie);
1947bd92d7feSAl Viro 		}
1948bd92d7feSAl Viro 	}
1949ee0827cdSAl Viro 
19509f1fafeeSAl Viro 	if (!err)
19519f1fafeeSAl Viro 		err = complete_walk(nd);
1952bd92d7feSAl Viro 
1953bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
1954bd92d7feSAl Viro 		if (!nd->inode->i_op->lookup) {
1955bd92d7feSAl Viro 			path_put(&nd->path);
1956bd23a539SAl Viro 			err = -ENOTDIR;
1957bd92d7feSAl Viro 		}
1958bd92d7feSAl Viro 	}
195916c2cd71SAl Viro 
196070e9b357SAl Viro 	if (base)
196170e9b357SAl Viro 		fput(base);
1962ee0827cdSAl Viro 
19635b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
196431e6b01fSNick Piggin 		path_put(&nd->root);
196531e6b01fSNick Piggin 		nd->root.mnt = NULL;
196631e6b01fSNick Piggin 	}
1967bd92d7feSAl Viro 	return err;
196831e6b01fSNick Piggin }
196931e6b01fSNick Piggin 
1970ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1971ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1972ee0827cdSAl Viro {
1973ee0827cdSAl Viro 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1974ee0827cdSAl Viro 	if (unlikely(retval == -ECHILD))
1975ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags, nd);
1976ee0827cdSAl Viro 	if (unlikely(retval == -ESTALE))
1977ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1978ee0827cdSAl Viro 
197931e6b01fSNick Piggin 	if (likely(!retval)) {
198031e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
198131e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
198231e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
198331e6b01fSNick Piggin 		}
198431e6b01fSNick Piggin 	}
1985170aa3d0SUlrich Drepper 	return retval;
19861da177e4SLinus Torvalds }
19871da177e4SLinus Torvalds 
198879714f72SAl Viro /* does lookup, returns the object with parent locked */
198979714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path)
19905590ff0dSUlrich Drepper {
199179714f72SAl Viro 	struct nameidata nd;
199279714f72SAl Viro 	struct dentry *d;
199379714f72SAl Viro 	int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
199479714f72SAl Viro 	if (err)
199579714f72SAl Viro 		return ERR_PTR(err);
199679714f72SAl Viro 	if (nd.last_type != LAST_NORM) {
199779714f72SAl Viro 		path_put(&nd.path);
199879714f72SAl Viro 		return ERR_PTR(-EINVAL);
199979714f72SAl Viro 	}
200079714f72SAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
20011e0ea001SAl Viro 	d = __lookup_hash(&nd.last, nd.path.dentry, 0);
200279714f72SAl Viro 	if (IS_ERR(d)) {
200379714f72SAl Viro 		mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
200479714f72SAl Viro 		path_put(&nd.path);
200579714f72SAl Viro 		return d;
200679714f72SAl Viro 	}
200779714f72SAl Viro 	*path = nd.path;
200879714f72SAl Viro 	return d;
20095590ff0dSUlrich Drepper }
20105590ff0dSUlrich Drepper 
2011d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
2012d1811465SAl Viro {
2013d1811465SAl Viro 	struct nameidata nd;
2014d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2015d1811465SAl Viro 	if (!res)
2016d1811465SAl Viro 		*path = nd.path;
2017d1811465SAl Viro 	return res;
2018d1811465SAl Viro }
2019d1811465SAl Viro 
202016f18200SJosef 'Jeff' Sipek /**
202116f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
202216f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
202316f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
202416f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
202516f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
2026e0a01249SAl Viro  * @path: pointer to struct path to fill
202716f18200SJosef 'Jeff' Sipek  */
202816f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
202916f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
2030e0a01249SAl Viro 		    struct path *path)
203116f18200SJosef 'Jeff' Sipek {
2032e0a01249SAl Viro 	struct nameidata nd;
2033e0a01249SAl Viro 	int err;
2034e0a01249SAl Viro 	nd.root.dentry = dentry;
2035e0a01249SAl Viro 	nd.root.mnt = mnt;
2036e0a01249SAl Viro 	BUG_ON(flags & LOOKUP_PARENT);
20375b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
2038e0a01249SAl Viro 	err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2039e0a01249SAl Viro 	if (!err)
2040e0a01249SAl Viro 		*path = nd.path;
2041e0a01249SAl Viro 	return err;
204216f18200SJosef 'Jeff' Sipek }
204316f18200SJosef 'Jeff' Sipek 
2044057f6c01SJames Morris /*
2045057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
2046057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
2047057f6c01SJames Morris  * SMP-safe.
2048057f6c01SJames Morris  */
2049a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
20501da177e4SLinus Torvalds {
205172bd866aSAl Viro 	return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
20521da177e4SLinus Torvalds }
20531da177e4SLinus Torvalds 
2054eead1911SChristoph Hellwig /**
2055a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
2056eead1911SChristoph Hellwig  * @name:	pathname component to lookup
2057eead1911SChristoph Hellwig  * @base:	base directory to lookup from
2058eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
2059eead1911SChristoph Hellwig  *
2060a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
2061a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
2062eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
2063eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
2064eead1911SChristoph Hellwig  */
2065057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2066057f6c01SJames Morris {
2067057f6c01SJames Morris 	struct qstr this;
20686a96ba54SAl Viro 	unsigned int c;
2069cda309deSMiklos Szeredi 	int err;
2070057f6c01SJames Morris 
20712f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
20722f9092e1SDavid Woodhouse 
20736a96ba54SAl Viro 	this.name = name;
20746a96ba54SAl Viro 	this.len = len;
20750145acc2SLinus Torvalds 	this.hash = full_name_hash(name, len);
20766a96ba54SAl Viro 	if (!len)
20776a96ba54SAl Viro 		return ERR_PTR(-EACCES);
20786a96ba54SAl Viro 
20796a96ba54SAl Viro 	while (len--) {
20806a96ba54SAl Viro 		c = *(const unsigned char *)name++;
20816a96ba54SAl Viro 		if (c == '/' || c == '\0')
20826a96ba54SAl Viro 			return ERR_PTR(-EACCES);
20836a96ba54SAl Viro 	}
20845a202bcdSAl Viro 	/*
20855a202bcdSAl Viro 	 * See if the low-level filesystem might want
20865a202bcdSAl Viro 	 * to use its own hash..
20875a202bcdSAl Viro 	 */
20885a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
20895a202bcdSAl Viro 		int err = base->d_op->d_hash(base, base->d_inode, &this);
20905a202bcdSAl Viro 		if (err < 0)
20915a202bcdSAl Viro 			return ERR_PTR(err);
20925a202bcdSAl Viro 	}
2093eead1911SChristoph Hellwig 
2094cda309deSMiklos Szeredi 	err = inode_permission(base->d_inode, MAY_EXEC);
2095cda309deSMiklos Szeredi 	if (err)
2096cda309deSMiklos Szeredi 		return ERR_PTR(err);
2097cda309deSMiklos Szeredi 
209872bd866aSAl Viro 	return __lookup_hash(&this, base, 0);
2099057f6c01SJames Morris }
2100057f6c01SJames Morris 
21011fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
21021fa1e7f6SAndy Whitcroft 		 struct path *path, int *empty)
21031da177e4SLinus Torvalds {
21042d8f3038SAl Viro 	struct nameidata nd;
21051fa1e7f6SAndy Whitcroft 	char *tmp = getname_flags(name, flags, empty);
21061da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
21071da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
21082d8f3038SAl Viro 
21092d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
21102d8f3038SAl Viro 
21112d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
21121da177e4SLinus Torvalds 		putname(tmp);
21132d8f3038SAl Viro 		if (!err)
21142d8f3038SAl Viro 			*path = nd.path;
21151da177e4SLinus Torvalds 	}
21161da177e4SLinus Torvalds 	return err;
21171da177e4SLinus Torvalds }
21181da177e4SLinus Torvalds 
21191fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags,
21201fa1e7f6SAndy Whitcroft 		 struct path *path)
21211fa1e7f6SAndy Whitcroft {
2122f7493e5dSLinus Torvalds 	return user_path_at_empty(dfd, name, flags, path, NULL);
21231fa1e7f6SAndy Whitcroft }
21241fa1e7f6SAndy Whitcroft 
21252ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
21262ad94ae6SAl Viro 			struct nameidata *nd, char **name)
21272ad94ae6SAl Viro {
21282ad94ae6SAl Viro 	char *s = getname(path);
21292ad94ae6SAl Viro 	int error;
21302ad94ae6SAl Viro 
21312ad94ae6SAl Viro 	if (IS_ERR(s))
21322ad94ae6SAl Viro 		return PTR_ERR(s);
21332ad94ae6SAl Viro 
21342ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
21352ad94ae6SAl Viro 	if (error)
21362ad94ae6SAl Viro 		putname(s);
21372ad94ae6SAl Viro 	else
21382ad94ae6SAl Viro 		*name = s;
21392ad94ae6SAl Viro 
21402ad94ae6SAl Viro 	return error;
21412ad94ae6SAl Viro }
21422ad94ae6SAl Viro 
21431da177e4SLinus Torvalds /*
21441da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
21451da177e4SLinus Torvalds  * minimal.
21461da177e4SLinus Torvalds  */
21471da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
21481da177e4SLinus Torvalds {
21498e96e3b7SEric W. Biederman 	kuid_t fsuid = current_fsuid();
2150da9592edSDavid Howells 
21511da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
21521da177e4SLinus Torvalds 		return 0;
21538e96e3b7SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
21541da177e4SLinus Torvalds 		return 0;
21558e96e3b7SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
21561da177e4SLinus Torvalds 		return 0;
21571a48e2acSEric W. Biederman 	return !inode_capable(inode, CAP_FOWNER);
21581da177e4SLinus Torvalds }
21591da177e4SLinus Torvalds 
21601da177e4SLinus Torvalds /*
21611da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
21621da177e4SLinus Torvalds  *  whether the type of victim is right.
21631da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
21641da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
21651da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
21661da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
21671da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
21681da177e4SLinus Torvalds  *	a. be owner of dir, or
21691da177e4SLinus Torvalds  *	b. be owner of victim, or
21701da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
21711da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
21721da177e4SLinus Torvalds  *     links pointing to it.
21731da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
21741da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
21751da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
21761da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
21771da177e4SLinus Torvalds  *     nfs_async_unlink().
21781da177e4SLinus Torvalds  */
2179858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
21801da177e4SLinus Torvalds {
21811da177e4SLinus Torvalds 	int error;
21821da177e4SLinus Torvalds 
21831da177e4SLinus Torvalds 	if (!victim->d_inode)
21841da177e4SLinus Torvalds 		return -ENOENT;
21851da177e4SLinus Torvalds 
21861da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
2187cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
21881da177e4SLinus Torvalds 
2189f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
21901da177e4SLinus Torvalds 	if (error)
21911da177e4SLinus Torvalds 		return error;
21921da177e4SLinus Torvalds 	if (IS_APPEND(dir))
21931da177e4SLinus Torvalds 		return -EPERM;
21941da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
2195f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
21961da177e4SLinus Torvalds 		return -EPERM;
21971da177e4SLinus Torvalds 	if (isdir) {
21981da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
21991da177e4SLinus Torvalds 			return -ENOTDIR;
22001da177e4SLinus Torvalds 		if (IS_ROOT(victim))
22011da177e4SLinus Torvalds 			return -EBUSY;
22021da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
22031da177e4SLinus Torvalds 		return -EISDIR;
22041da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
22051da177e4SLinus Torvalds 		return -ENOENT;
22061da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
22071da177e4SLinus Torvalds 		return -EBUSY;
22081da177e4SLinus Torvalds 	return 0;
22091da177e4SLinus Torvalds }
22101da177e4SLinus Torvalds 
22111da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
22121da177e4SLinus Torvalds  *  dir.
22131da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
22141da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
22151da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
22161da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
22171da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
22181da177e4SLinus Torvalds  */
2219a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
22201da177e4SLinus Torvalds {
22211da177e4SLinus Torvalds 	if (child->d_inode)
22221da177e4SLinus Torvalds 		return -EEXIST;
22231da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
22241da177e4SLinus Torvalds 		return -ENOENT;
2225f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
22261da177e4SLinus Torvalds }
22271da177e4SLinus Torvalds 
22281da177e4SLinus Torvalds /*
22291da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
22301da177e4SLinus Torvalds  */
22311da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
22321da177e4SLinus Torvalds {
22331da177e4SLinus Torvalds 	struct dentry *p;
22341da177e4SLinus Torvalds 
22351da177e4SLinus Torvalds 	if (p1 == p2) {
2236f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
22371da177e4SLinus Torvalds 		return NULL;
22381da177e4SLinus Torvalds 	}
22391da177e4SLinus Torvalds 
2240a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
22411da177e4SLinus Torvalds 
2242e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
2243e2761a11SOGAWA Hirofumi 	if (p) {
2244f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2245f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
22461da177e4SLinus Torvalds 		return p;
22471da177e4SLinus Torvalds 	}
22481da177e4SLinus Torvalds 
2249e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
2250e2761a11SOGAWA Hirofumi 	if (p) {
2251f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2252f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
22531da177e4SLinus Torvalds 		return p;
22541da177e4SLinus Torvalds 	}
22551da177e4SLinus Torvalds 
2256f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2257f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
22581da177e4SLinus Torvalds 	return NULL;
22591da177e4SLinus Torvalds }
22601da177e4SLinus Torvalds 
22611da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
22621da177e4SLinus Torvalds {
22631b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
22641da177e4SLinus Torvalds 	if (p1 != p2) {
22651b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
2266a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
22671da177e4SLinus Torvalds 	}
22681da177e4SLinus Torvalds }
22691da177e4SLinus Torvalds 
22704acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2271312b63fbSAl Viro 		bool want_excl)
22721da177e4SLinus Torvalds {
2273a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
22741da177e4SLinus Torvalds 	if (error)
22751da177e4SLinus Torvalds 		return error;
22761da177e4SLinus Torvalds 
2277acfa4380SAl Viro 	if (!dir->i_op->create)
22781da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
22791da177e4SLinus Torvalds 	mode &= S_IALLUGO;
22801da177e4SLinus Torvalds 	mode |= S_IFREG;
22811da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
22821da177e4SLinus Torvalds 	if (error)
22831da177e4SLinus Torvalds 		return error;
2284312b63fbSAl Viro 	error = dir->i_op->create(dir, dentry, mode, want_excl);
2285a74574aaSStephen Smalley 	if (!error)
2286f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
22871da177e4SLinus Torvalds 	return error;
22881da177e4SLinus Torvalds }
22891da177e4SLinus Torvalds 
229073d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
22911da177e4SLinus Torvalds {
22923fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
22931da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
22941da177e4SLinus Torvalds 	int error;
22951da177e4SLinus Torvalds 
2296bcda7652SAl Viro 	/* O_PATH? */
2297bcda7652SAl Viro 	if (!acc_mode)
2298bcda7652SAl Viro 		return 0;
2299bcda7652SAl Viro 
23001da177e4SLinus Torvalds 	if (!inode)
23011da177e4SLinus Torvalds 		return -ENOENT;
23021da177e4SLinus Torvalds 
2303c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2304c8fe8f30SChristoph Hellwig 	case S_IFLNK:
23051da177e4SLinus Torvalds 		return -ELOOP;
2306c8fe8f30SChristoph Hellwig 	case S_IFDIR:
2307c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
23081da177e4SLinus Torvalds 			return -EISDIR;
2309c8fe8f30SChristoph Hellwig 		break;
2310c8fe8f30SChristoph Hellwig 	case S_IFBLK:
2311c8fe8f30SChristoph Hellwig 	case S_IFCHR:
23123fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
23131da177e4SLinus Torvalds 			return -EACCES;
2314c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
2315c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2316c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
23171da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2318c8fe8f30SChristoph Hellwig 		break;
23194a3fd211SDave Hansen 	}
2320b41572e9SDave Hansen 
23213fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
2322b41572e9SDave Hansen 	if (error)
2323b41572e9SDave Hansen 		return error;
23246146f0d5SMimi Zohar 
23251da177e4SLinus Torvalds 	/*
23261da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
23271da177e4SLinus Torvalds 	 */
23281da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
23298737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
23307715b521SAl Viro 			return -EPERM;
23311da177e4SLinus Torvalds 		if (flag & O_TRUNC)
23327715b521SAl Viro 			return -EPERM;
23331da177e4SLinus Torvalds 	}
23341da177e4SLinus Torvalds 
23351da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
23362e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
23377715b521SAl Viro 		return -EPERM;
23381da177e4SLinus Torvalds 
2339f3c7691eSJ. Bruce Fields 	return 0;
23407715b521SAl Viro }
23417715b521SAl Viro 
2342e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
23437715b521SAl Viro {
2344e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
23457715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
23467715b521SAl Viro 	int error = get_write_access(inode);
23471da177e4SLinus Torvalds 	if (error)
23487715b521SAl Viro 		return error;
23491da177e4SLinus Torvalds 	/*
23501da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
23511da177e4SLinus Torvalds 	 */
23521da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2353be6d3e56SKentaro Takeda 	if (!error)
2354ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
23551da177e4SLinus Torvalds 	if (!error) {
23567715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2357d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2358e1181ee6SJeff Layton 				    filp);
23591da177e4SLinus Torvalds 	}
23601da177e4SLinus Torvalds 	put_write_access(inode);
2361acd0c935SMimi Zohar 	return error;
23621da177e4SLinus Torvalds }
23631da177e4SLinus Torvalds 
2364d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2365d57999e1SDave Hansen {
23668a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
23678a5e929dSAl Viro 		flag--;
2368d57999e1SDave Hansen 	return flag;
2369d57999e1SDave Hansen }
2370d57999e1SDave Hansen 
2371d18e9008SMiklos Szeredi static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2372d18e9008SMiklos Szeredi {
2373d18e9008SMiklos Szeredi 	int error = security_path_mknod(dir, dentry, mode, 0);
2374d18e9008SMiklos Szeredi 	if (error)
2375d18e9008SMiklos Szeredi 		return error;
2376d18e9008SMiklos Szeredi 
2377d18e9008SMiklos Szeredi 	error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2378d18e9008SMiklos Szeredi 	if (error)
2379d18e9008SMiklos Szeredi 		return error;
2380d18e9008SMiklos Szeredi 
2381d18e9008SMiklos Szeredi 	return security_inode_create(dir->dentry->d_inode, dentry, mode);
2382d18e9008SMiklos Szeredi }
2383d18e9008SMiklos Szeredi 
23841acf0af9SDavid Howells /*
23851acf0af9SDavid Howells  * Attempt to atomically look up, create and open a file from a negative
23861acf0af9SDavid Howells  * dentry.
23871acf0af9SDavid Howells  *
23881acf0af9SDavid Howells  * Returns 0 if successful.  The file will have been created and attached to
23891acf0af9SDavid Howells  * @file by the filesystem calling finish_open().
23901acf0af9SDavid Howells  *
23911acf0af9SDavid Howells  * Returns 1 if the file was looked up only or didn't need creating.  The
23921acf0af9SDavid Howells  * caller will need to perform the open themselves.  @path will have been
23931acf0af9SDavid Howells  * updated to point to the new dentry.  This may be negative.
23941acf0af9SDavid Howells  *
23951acf0af9SDavid Howells  * Returns an error code otherwise.
23961acf0af9SDavid Howells  */
23972675a4ebSAl Viro static int atomic_open(struct nameidata *nd, struct dentry *dentry,
239830d90494SAl Viro 			struct path *path, struct file *file,
2399015c3bbcSMiklos Szeredi 			const struct open_flags *op,
240064894cf8SAl Viro 			bool got_write, bool need_lookup,
240147237687SAl Viro 			int *opened)
2402d18e9008SMiklos Szeredi {
2403d18e9008SMiklos Szeredi 	struct inode *dir =  nd->path.dentry->d_inode;
2404d18e9008SMiklos Szeredi 	unsigned open_flag = open_to_namei_flags(op->open_flag);
2405d18e9008SMiklos Szeredi 	umode_t mode;
2406d18e9008SMiklos Szeredi 	int error;
2407d18e9008SMiklos Szeredi 	int acc_mode;
2408d18e9008SMiklos Szeredi 	int create_error = 0;
2409d18e9008SMiklos Szeredi 	struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2410d18e9008SMiklos Szeredi 
2411d18e9008SMiklos Szeredi 	BUG_ON(dentry->d_inode);
2412d18e9008SMiklos Szeredi 
2413d18e9008SMiklos Szeredi 	/* Don't create child dentry for a dead directory. */
2414d18e9008SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
24152675a4ebSAl Viro 		error = -ENOENT;
2416d18e9008SMiklos Szeredi 		goto out;
2417d18e9008SMiklos Szeredi 	}
2418d18e9008SMiklos Szeredi 
241962b259d8SMiklos Szeredi 	mode = op->mode;
2420d18e9008SMiklos Szeredi 	if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2421d18e9008SMiklos Szeredi 		mode &= ~current_umask();
2422d18e9008SMiklos Szeredi 
2423f8310c59SAl Viro 	if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) {
2424d18e9008SMiklos Szeredi 		open_flag &= ~O_TRUNC;
242547237687SAl Viro 		*opened |= FILE_CREATED;
2426d18e9008SMiklos Szeredi 	}
2427d18e9008SMiklos Szeredi 
2428d18e9008SMiklos Szeredi 	/*
2429d18e9008SMiklos Szeredi 	 * Checking write permission is tricky, bacuse we don't know if we are
2430d18e9008SMiklos Szeredi 	 * going to actually need it: O_CREAT opens should work as long as the
2431d18e9008SMiklos Szeredi 	 * file exists.  But checking existence breaks atomicity.  The trick is
2432d18e9008SMiklos Szeredi 	 * to check access and if not granted clear O_CREAT from the flags.
2433d18e9008SMiklos Szeredi 	 *
2434d18e9008SMiklos Szeredi 	 * Another problem is returing the "right" error value (e.g. for an
2435d18e9008SMiklos Szeredi 	 * O_EXCL open we want to return EEXIST not EROFS).
2436d18e9008SMiklos Szeredi 	 */
243764894cf8SAl Viro 	if (((open_flag & (O_CREAT | O_TRUNC)) ||
243864894cf8SAl Viro 	    (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
243964894cf8SAl Viro 		if (!(open_flag & O_CREAT)) {
2440d18e9008SMiklos Szeredi 			/*
2441d18e9008SMiklos Szeredi 			 * No O_CREATE -> atomicity not a requirement -> fall
2442d18e9008SMiklos Szeredi 			 * back to lookup + open
2443d18e9008SMiklos Szeredi 			 */
2444d18e9008SMiklos Szeredi 			goto no_open;
2445d18e9008SMiklos Szeredi 		} else if (open_flag & (O_EXCL | O_TRUNC)) {
2446d18e9008SMiklos Szeredi 			/* Fall back and fail with the right error */
244764894cf8SAl Viro 			create_error = -EROFS;
2448d18e9008SMiklos Szeredi 			goto no_open;
2449d18e9008SMiklos Szeredi 		} else {
2450d18e9008SMiklos Szeredi 			/* No side effects, safe to clear O_CREAT */
245164894cf8SAl Viro 			create_error = -EROFS;
2452d18e9008SMiklos Szeredi 			open_flag &= ~O_CREAT;
2453d18e9008SMiklos Szeredi 		}
2454d18e9008SMiklos Szeredi 	}
2455d18e9008SMiklos Szeredi 
2456d18e9008SMiklos Szeredi 	if (open_flag & O_CREAT) {
245738227f78SMiklos Szeredi 		error = may_o_create(&nd->path, dentry, mode);
2458d18e9008SMiklos Szeredi 		if (error) {
2459d18e9008SMiklos Szeredi 			create_error = error;
2460d18e9008SMiklos Szeredi 			if (open_flag & O_EXCL)
2461d18e9008SMiklos Szeredi 				goto no_open;
2462d18e9008SMiklos Szeredi 			open_flag &= ~O_CREAT;
2463d18e9008SMiklos Szeredi 		}
2464d18e9008SMiklos Szeredi 	}
2465d18e9008SMiklos Szeredi 
2466d18e9008SMiklos Szeredi 	if (nd->flags & LOOKUP_DIRECTORY)
2467d18e9008SMiklos Szeredi 		open_flag |= O_DIRECTORY;
2468d18e9008SMiklos Szeredi 
246930d90494SAl Viro 	file->f_path.dentry = DENTRY_NOT_SET;
247030d90494SAl Viro 	file->f_path.mnt = nd->path.mnt;
247130d90494SAl Viro 	error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
247247237687SAl Viro 				      opened);
2473d9585277SAl Viro 	if (error < 0) {
2474d9585277SAl Viro 		if (create_error && error == -ENOENT)
2475d9585277SAl Viro 			error = create_error;
2476d18e9008SMiklos Szeredi 		goto out;
2477d18e9008SMiklos Szeredi 	}
2478d18e9008SMiklos Szeredi 
2479d18e9008SMiklos Szeredi 	acc_mode = op->acc_mode;
248047237687SAl Viro 	if (*opened & FILE_CREATED) {
2481d18e9008SMiklos Szeredi 		fsnotify_create(dir, dentry);
2482d18e9008SMiklos Szeredi 		acc_mode = MAY_OPEN;
2483d18e9008SMiklos Szeredi 	}
2484d18e9008SMiklos Szeredi 
2485d9585277SAl Viro 	if (error) {	/* returned 1, that is */
248630d90494SAl Viro 		if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
24872675a4ebSAl Viro 			error = -EIO;
2488d18e9008SMiklos Szeredi 			goto out;
2489d18e9008SMiklos Szeredi 		}
249030d90494SAl Viro 		if (file->f_path.dentry) {
2491d18e9008SMiklos Szeredi 			dput(dentry);
249230d90494SAl Viro 			dentry = file->f_path.dentry;
2493d18e9008SMiklos Szeredi 		}
249462b2ce96SSage Weil 		if (create_error && dentry->d_inode == NULL) {
249562b2ce96SSage Weil 			error = create_error;
249662b2ce96SSage Weil 			goto out;
249762b2ce96SSage Weil 		}
2498d18e9008SMiklos Szeredi 		goto looked_up;
2499d18e9008SMiklos Szeredi 	}
2500d18e9008SMiklos Szeredi 
2501d18e9008SMiklos Szeredi 	/*
2502d18e9008SMiklos Szeredi 	 * We didn't have the inode before the open, so check open permission
2503d18e9008SMiklos Szeredi 	 * here.
2504d18e9008SMiklos Szeredi 	 */
25052675a4ebSAl Viro 	error = may_open(&file->f_path, acc_mode, open_flag);
25062675a4ebSAl Viro 	if (error)
25072675a4ebSAl Viro 		fput(file);
2508d18e9008SMiklos Szeredi 
2509d18e9008SMiklos Szeredi out:
2510d18e9008SMiklos Szeredi 	dput(dentry);
25112675a4ebSAl Viro 	return error;
2512d18e9008SMiklos Szeredi 
2513d18e9008SMiklos Szeredi no_open:
2514d18e9008SMiklos Szeredi 	if (need_lookup) {
251572bd866aSAl Viro 		dentry = lookup_real(dir, dentry, nd->flags);
2516d18e9008SMiklos Szeredi 		if (IS_ERR(dentry))
25172675a4ebSAl Viro 			return PTR_ERR(dentry);
2518d18e9008SMiklos Szeredi 
2519d18e9008SMiklos Szeredi 		if (create_error) {
2520d18e9008SMiklos Szeredi 			int open_flag = op->open_flag;
2521d18e9008SMiklos Szeredi 
25222675a4ebSAl Viro 			error = create_error;
2523d18e9008SMiklos Szeredi 			if ((open_flag & O_EXCL)) {
2524d18e9008SMiklos Szeredi 				if (!dentry->d_inode)
2525d18e9008SMiklos Szeredi 					goto out;
2526d18e9008SMiklos Szeredi 			} else if (!dentry->d_inode) {
2527d18e9008SMiklos Szeredi 				goto out;
2528d18e9008SMiklos Szeredi 			} else if ((open_flag & O_TRUNC) &&
2529d18e9008SMiklos Szeredi 				   S_ISREG(dentry->d_inode->i_mode)) {
2530d18e9008SMiklos Szeredi 				goto out;
2531d18e9008SMiklos Szeredi 			}
2532d18e9008SMiklos Szeredi 			/* will fail later, go on to get the right error */
2533d18e9008SMiklos Szeredi 		}
2534d18e9008SMiklos Szeredi 	}
2535d18e9008SMiklos Szeredi looked_up:
2536d18e9008SMiklos Szeredi 	path->dentry = dentry;
2537d18e9008SMiklos Szeredi 	path->mnt = nd->path.mnt;
25382675a4ebSAl Viro 	return 1;
2539d18e9008SMiklos Szeredi }
2540d18e9008SMiklos Szeredi 
254131e6b01fSNick Piggin /*
25421acf0af9SDavid Howells  * Look up and maybe create and open the last component.
2543d58ffd35SMiklos Szeredi  *
2544d58ffd35SMiklos Szeredi  * Must be called with i_mutex held on parent.
2545d58ffd35SMiklos Szeredi  *
25461acf0af9SDavid Howells  * Returns 0 if the file was successfully atomically created (if necessary) and
25471acf0af9SDavid Howells  * opened.  In this case the file will be returned attached to @file.
25481acf0af9SDavid Howells  *
25491acf0af9SDavid Howells  * Returns 1 if the file was not completely opened at this time, though lookups
25501acf0af9SDavid Howells  * and creations will have been performed and the dentry returned in @path will
25511acf0af9SDavid Howells  * be positive upon return if O_CREAT was specified.  If O_CREAT wasn't
25521acf0af9SDavid Howells  * specified then a negative dentry may be returned.
25531acf0af9SDavid Howells  *
25541acf0af9SDavid Howells  * An error code is returned otherwise.
25551acf0af9SDavid Howells  *
25561acf0af9SDavid Howells  * FILE_CREATE will be set in @*opened if the dentry was created and will be
25571acf0af9SDavid Howells  * cleared otherwise prior to returning.
2558d58ffd35SMiklos Szeredi  */
25592675a4ebSAl Viro static int lookup_open(struct nameidata *nd, struct path *path,
256030d90494SAl Viro 			struct file *file,
2561d58ffd35SMiklos Szeredi 			const struct open_flags *op,
256264894cf8SAl Viro 			bool got_write, int *opened)
2563d58ffd35SMiklos Szeredi {
2564d58ffd35SMiklos Szeredi 	struct dentry *dir = nd->path.dentry;
256554ef4872SMiklos Szeredi 	struct inode *dir_inode = dir->d_inode;
2566d58ffd35SMiklos Szeredi 	struct dentry *dentry;
2567d58ffd35SMiklos Szeredi 	int error;
256854ef4872SMiklos Szeredi 	bool need_lookup;
2569d58ffd35SMiklos Szeredi 
257047237687SAl Viro 	*opened &= ~FILE_CREATED;
2571201f956eSAl Viro 	dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
2572d58ffd35SMiklos Szeredi 	if (IS_ERR(dentry))
25732675a4ebSAl Viro 		return PTR_ERR(dentry);
2574d58ffd35SMiklos Szeredi 
2575d18e9008SMiklos Szeredi 	/* Cached positive dentry: will open in f_op->open */
2576d18e9008SMiklos Szeredi 	if (!need_lookup && dentry->d_inode)
2577d18e9008SMiklos Szeredi 		goto out_no_open;
2578d18e9008SMiklos Szeredi 
2579d18e9008SMiklos Szeredi 	if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
258064894cf8SAl Viro 		return atomic_open(nd, dentry, path, file, op, got_write,
258147237687SAl Viro 				   need_lookup, opened);
2582d18e9008SMiklos Szeredi 	}
2583d18e9008SMiklos Szeredi 
258454ef4872SMiklos Szeredi 	if (need_lookup) {
258554ef4872SMiklos Szeredi 		BUG_ON(dentry->d_inode);
258654ef4872SMiklos Szeredi 
258772bd866aSAl Viro 		dentry = lookup_real(dir_inode, dentry, nd->flags);
258854ef4872SMiklos Szeredi 		if (IS_ERR(dentry))
25892675a4ebSAl Viro 			return PTR_ERR(dentry);
259054ef4872SMiklos Szeredi 	}
259154ef4872SMiklos Szeredi 
2592d58ffd35SMiklos Szeredi 	/* Negative dentry, just create the file */
2593d58ffd35SMiklos Szeredi 	if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2594d58ffd35SMiklos Szeredi 		umode_t mode = op->mode;
2595d58ffd35SMiklos Szeredi 		if (!IS_POSIXACL(dir->d_inode))
2596d58ffd35SMiklos Szeredi 			mode &= ~current_umask();
2597d58ffd35SMiklos Szeredi 		/*
2598d58ffd35SMiklos Szeredi 		 * This write is needed to ensure that a
2599d58ffd35SMiklos Szeredi 		 * rw->ro transition does not occur between
2600d58ffd35SMiklos Szeredi 		 * the time when the file is created and when
2601d58ffd35SMiklos Szeredi 		 * a permanent write count is taken through
2602015c3bbcSMiklos Szeredi 		 * the 'struct file' in finish_open().
2603d58ffd35SMiklos Szeredi 		 */
260464894cf8SAl Viro 		if (!got_write) {
260564894cf8SAl Viro 			error = -EROFS;
2606d58ffd35SMiklos Szeredi 			goto out_dput;
260764894cf8SAl Viro 		}
260847237687SAl Viro 		*opened |= FILE_CREATED;
2609d58ffd35SMiklos Szeredi 		error = security_path_mknod(&nd->path, dentry, mode, 0);
2610d58ffd35SMiklos Szeredi 		if (error)
2611d58ffd35SMiklos Szeredi 			goto out_dput;
2612312b63fbSAl Viro 		error = vfs_create(dir->d_inode, dentry, mode,
2613312b63fbSAl Viro 				   nd->flags & LOOKUP_EXCL);
2614d58ffd35SMiklos Szeredi 		if (error)
2615d58ffd35SMiklos Szeredi 			goto out_dput;
2616d58ffd35SMiklos Szeredi 	}
2617d18e9008SMiklos Szeredi out_no_open:
2618d58ffd35SMiklos Szeredi 	path->dentry = dentry;
2619d58ffd35SMiklos Szeredi 	path->mnt = nd->path.mnt;
26202675a4ebSAl Viro 	return 1;
2621d58ffd35SMiklos Szeredi 
2622d58ffd35SMiklos Szeredi out_dput:
2623d58ffd35SMiklos Szeredi 	dput(dentry);
26242675a4ebSAl Viro 	return error;
2625d58ffd35SMiklos Szeredi }
2626d58ffd35SMiklos Szeredi 
2627d58ffd35SMiklos Szeredi /*
2628fe2d35ffSAl Viro  * Handle the last step of open()
262931e6b01fSNick Piggin  */
26302675a4ebSAl Viro static int do_last(struct nameidata *nd, struct path *path,
263130d90494SAl Viro 		   struct file *file, const struct open_flags *op,
263247237687SAl Viro 		   int *opened, const char *pathname)
2633fb1cc555SAl Viro {
2634a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
2635ca344a89SAl Viro 	int open_flag = op->open_flag;
263677d660a8SMiklos Szeredi 	bool will_truncate = (open_flag & O_TRUNC) != 0;
263764894cf8SAl Viro 	bool got_write = false;
2638bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2639a1eb3315SMiklos Szeredi 	struct inode *inode;
264077d660a8SMiklos Szeredi 	bool symlink_ok = false;
264116b1c1cdSMiklos Szeredi 	struct path save_parent = { .dentry = NULL, .mnt = NULL };
264216b1c1cdSMiklos Szeredi 	bool retried = false;
264316c2cd71SAl Viro 	int error;
2644fb1cc555SAl Viro 
2645c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2646c3e380b0SAl Viro 	nd->flags |= op->intent;
2647c3e380b0SAl Viro 
26481f36f774SAl Viro 	switch (nd->last_type) {
26491f36f774SAl Viro 	case LAST_DOTDOT:
2650176306f5SNeil Brown 	case LAST_DOT:
2651fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2652fe2d35ffSAl Viro 		if (error)
26532675a4ebSAl Viro 			return error;
26541f36f774SAl Viro 		/* fallthrough */
26551f36f774SAl Viro 	case LAST_ROOT:
26569f1fafeeSAl Viro 		error = complete_walk(nd);
265716c2cd71SAl Viro 		if (error)
26582675a4ebSAl Viro 			return error;
2659fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2660ca344a89SAl Viro 		if (open_flag & O_CREAT) {
266116c2cd71SAl Viro 			error = -EISDIR;
26622675a4ebSAl Viro 			goto out;
2663fe2d35ffSAl Viro 		}
2664e83db167SMiklos Szeredi 		goto finish_open;
26651f36f774SAl Viro 	case LAST_BIND:
26669f1fafeeSAl Viro 		error = complete_walk(nd);
266716c2cd71SAl Viro 		if (error)
26682675a4ebSAl Viro 			return error;
26691f36f774SAl Viro 		audit_inode(pathname, dir);
2670e83db167SMiklos Szeredi 		goto finish_open;
26711f36f774SAl Viro 	}
2672a2c36b45SAl Viro 
2673ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2674fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2675fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2676bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
267777d660a8SMiklos Szeredi 			symlink_ok = true;
2678fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2679a1eb3315SMiklos Szeredi 		error = lookup_fast(nd, &nd->last, path, &inode);
268071574865SMiklos Szeredi 		if (likely(!error))
268171574865SMiklos Szeredi 			goto finish_lookup;
268271574865SMiklos Szeredi 
2683ce57dfc1SAl Viro 		if (error < 0)
26842675a4ebSAl Viro 			goto out;
2685a1eb3315SMiklos Szeredi 
268637d7fffcSMiklos Szeredi 		BUG_ON(nd->inode != dir->d_inode);
2687b6183df7SMiklos Szeredi 	} else {
2688fe2d35ffSAl Viro 		/* create side of things */
2689a3fbbde7SAl Viro 		/*
2690b6183df7SMiklos Szeredi 		 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2691b6183df7SMiklos Szeredi 		 * has been cleared when we got to the last component we are
2692b6183df7SMiklos Szeredi 		 * about to look up
2693a3fbbde7SAl Viro 		 */
26949f1fafeeSAl Viro 		error = complete_walk(nd);
26959f1fafeeSAl Viro 		if (error)
26962675a4ebSAl Viro 			return error;
2697fe2d35ffSAl Viro 
2698fe2d35ffSAl Viro 		audit_inode(pathname, dir);
269916c2cd71SAl Viro 		error = -EISDIR;
27001f36f774SAl Viro 		/* trailing slashes? */
270131e6b01fSNick Piggin 		if (nd->last.name[nd->last.len])
27022675a4ebSAl Viro 			goto out;
2703b6183df7SMiklos Szeredi 	}
27041f36f774SAl Viro 
270516b1c1cdSMiklos Szeredi retry_lookup:
270664894cf8SAl Viro 	if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
270764894cf8SAl Viro 		error = mnt_want_write(nd->path.mnt);
270864894cf8SAl Viro 		if (!error)
270964894cf8SAl Viro 			got_write = true;
271064894cf8SAl Viro 		/*
271164894cf8SAl Viro 		 * do _not_ fail yet - we might not need that or fail with
271264894cf8SAl Viro 		 * a different error; let lookup_open() decide; we'll be
271364894cf8SAl Viro 		 * dropping this one anyway.
271464894cf8SAl Viro 		 */
271564894cf8SAl Viro 	}
2716a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
271764894cf8SAl Viro 	error = lookup_open(nd, path, file, op, got_write, opened);
2718fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2719fb1cc555SAl Viro 
27202675a4ebSAl Viro 	if (error <= 0) {
27212675a4ebSAl Viro 		if (error)
2722d58ffd35SMiklos Szeredi 			goto out;
27236c0d46c4SAl Viro 
272447237687SAl Viro 		if ((*opened & FILE_CREATED) ||
27252675a4ebSAl Viro 		    !S_ISREG(file->f_path.dentry->d_inode->i_mode))
272677d660a8SMiklos Szeredi 			will_truncate = false;
2727d18e9008SMiklos Szeredi 
27282675a4ebSAl Viro 		audit_inode(pathname, file->f_path.dentry);
2729d18e9008SMiklos Szeredi 		goto opened;
2730d18e9008SMiklos Szeredi 	}
2731d18e9008SMiklos Szeredi 
273247237687SAl Viro 	if (*opened & FILE_CREATED) {
27339b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2734ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
273577d660a8SMiklos Szeredi 		will_truncate = false;
2736bcda7652SAl Viro 		acc_mode = MAY_OPEN;
2737d58ffd35SMiklos Szeredi 		path_to_nameidata(path, nd);
2738e83db167SMiklos Szeredi 		goto finish_open_created;
2739fb1cc555SAl Viro 	}
2740fb1cc555SAl Viro 
2741fb1cc555SAl Viro 	/*
27423134f37eSJeff Layton 	 * create/update audit record if it already exists.
2743fb1cc555SAl Viro 	 */
27443134f37eSJeff Layton 	if (path->dentry->d_inode)
2745fb1cc555SAl Viro 		audit_inode(pathname, path->dentry);
2746fb1cc555SAl Viro 
2747d18e9008SMiklos Szeredi 	/*
2748d18e9008SMiklos Szeredi 	 * If atomic_open() acquired write access it is dropped now due to
2749d18e9008SMiklos Szeredi 	 * possible mount and symlink following (this might be optimized away if
2750d18e9008SMiklos Szeredi 	 * necessary...)
2751d18e9008SMiklos Szeredi 	 */
275264894cf8SAl Viro 	if (got_write) {
2753d18e9008SMiklos Szeredi 		mnt_drop_write(nd->path.mnt);
275464894cf8SAl Viro 		got_write = false;
2755d18e9008SMiklos Szeredi 	}
2756d18e9008SMiklos Szeredi 
2757fb1cc555SAl Viro 	error = -EEXIST;
2758f8310c59SAl Viro 	if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
2759fb1cc555SAl Viro 		goto exit_dput;
2760fb1cc555SAl Viro 
27619875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
27629875cf80SDavid Howells 	if (error < 0)
2763fb1cc555SAl Viro 		goto exit_dput;
2764fb1cc555SAl Viro 
2765a3fbbde7SAl Viro 	if (error)
2766a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
2767a3fbbde7SAl Viro 
2768decf3400SMiklos Szeredi 	BUG_ON(nd->flags & LOOKUP_RCU);
2769decf3400SMiklos Szeredi 	inode = path->dentry->d_inode;
27705f5daac1SMiklos Szeredi finish_lookup:
27715f5daac1SMiklos Szeredi 	/* we _can_ be in RCU mode here */
2772fb1cc555SAl Viro 	error = -ENOENT;
277354c33e7fSMiklos Szeredi 	if (!inode) {
277454c33e7fSMiklos Szeredi 		path_to_nameidata(path, nd);
27752675a4ebSAl Viro 		goto out;
277654c33e7fSMiklos Szeredi 	}
27779e67f361SAl Viro 
2778d45ea867SMiklos Szeredi 	if (should_follow_link(inode, !symlink_ok)) {
2779d45ea867SMiklos Szeredi 		if (nd->flags & LOOKUP_RCU) {
2780d45ea867SMiklos Szeredi 			if (unlikely(unlazy_walk(nd, path->dentry))) {
2781d45ea867SMiklos Szeredi 				error = -ECHILD;
27822675a4ebSAl Viro 				goto out;
2783d45ea867SMiklos Szeredi 			}
2784d45ea867SMiklos Szeredi 		}
2785d45ea867SMiklos Szeredi 		BUG_ON(inode != path->dentry->d_inode);
27862675a4ebSAl Viro 		return 1;
2787d45ea867SMiklos Szeredi 	}
2788fb1cc555SAl Viro 
278916b1c1cdSMiklos Szeredi 	if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
2790fb1cc555SAl Viro 		path_to_nameidata(path, nd);
279116b1c1cdSMiklos Szeredi 	} else {
279216b1c1cdSMiklos Szeredi 		save_parent.dentry = nd->path.dentry;
279316b1c1cdSMiklos Szeredi 		save_parent.mnt = mntget(path->mnt);
279416b1c1cdSMiklos Szeredi 		nd->path.dentry = path->dentry;
279516b1c1cdSMiklos Szeredi 
279616b1c1cdSMiklos Szeredi 	}
2797decf3400SMiklos Szeredi 	nd->inode = inode;
2798a3fbbde7SAl Viro 	/* Why this, you ask?  _Now_ we might have grown LOOKUP_JUMPED... */
2799a3fbbde7SAl Viro 	error = complete_walk(nd);
280016b1c1cdSMiklos Szeredi 	if (error) {
280116b1c1cdSMiklos Szeredi 		path_put(&save_parent);
28022675a4ebSAl Viro 		return error;
280316b1c1cdSMiklos Szeredi 	}
2804fb1cc555SAl Viro 	error = -EISDIR;
2805050ac841SMiklos Szeredi 	if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
28062675a4ebSAl Viro 		goto out;
2807af2f5542SMiklos Szeredi 	error = -ENOTDIR;
2808af2f5542SMiklos Szeredi 	if ((nd->flags & LOOKUP_DIRECTORY) && !nd->inode->i_op->lookup)
28092675a4ebSAl Viro 		goto out;
2810d7fdd7f6SMiklos Szeredi 	audit_inode(pathname, nd->path.dentry);
2811e83db167SMiklos Szeredi finish_open:
28126c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
281377d660a8SMiklos Szeredi 		will_truncate = false;
28146c0d46c4SAl Viro 
28150f9d1a10SAl Viro 	if (will_truncate) {
28160f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
28170f9d1a10SAl Viro 		if (error)
28182675a4ebSAl Viro 			goto out;
281964894cf8SAl Viro 		got_write = true;
28200f9d1a10SAl Viro 	}
2821e83db167SMiklos Szeredi finish_open_created:
2822bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2823ca344a89SAl Viro 	if (error)
28242675a4ebSAl Viro 		goto out;
282530d90494SAl Viro 	file->f_path.mnt = nd->path.mnt;
282630d90494SAl Viro 	error = finish_open(file, nd->path.dentry, NULL, opened);
282730d90494SAl Viro 	if (error) {
282830d90494SAl Viro 		if (error == -EOPENSTALE)
2829f60dc3dbSMiklos Szeredi 			goto stale_open;
2830015c3bbcSMiklos Szeredi 		goto out;
2831f60dc3dbSMiklos Szeredi 	}
2832a8277b9bSMiklos Szeredi opened:
28332675a4ebSAl Viro 	error = open_check_o_direct(file);
2834015c3bbcSMiklos Szeredi 	if (error)
2835015c3bbcSMiklos Szeredi 		goto exit_fput;
28362675a4ebSAl Viro 	error = ima_file_check(file, op->acc_mode);
2837aa4caadbSMiklos Szeredi 	if (error)
2838aa4caadbSMiklos Szeredi 		goto exit_fput;
2839aa4caadbSMiklos Szeredi 
28400f9d1a10SAl Viro 	if (will_truncate) {
28412675a4ebSAl Viro 		error = handle_truncate(file);
2842aa4caadbSMiklos Szeredi 		if (error)
2843aa4caadbSMiklos Szeredi 			goto exit_fput;
28440f9d1a10SAl Viro 	}
2845ca344a89SAl Viro out:
284664894cf8SAl Viro 	if (got_write)
28470f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
284816b1c1cdSMiklos Szeredi 	path_put(&save_parent);
2849e276ae67SMiklos Szeredi 	terminate_walk(nd);
28502675a4ebSAl Viro 	return error;
2851fb1cc555SAl Viro 
2852fb1cc555SAl Viro exit_dput:
2853fb1cc555SAl Viro 	path_put_conditional(path, nd);
2854ca344a89SAl Viro 	goto out;
2855015c3bbcSMiklos Szeredi exit_fput:
28562675a4ebSAl Viro 	fput(file);
28572675a4ebSAl Viro 	goto out;
2858015c3bbcSMiklos Szeredi 
2859f60dc3dbSMiklos Szeredi stale_open:
2860f60dc3dbSMiklos Szeredi 	/* If no saved parent or already retried then can't retry */
2861f60dc3dbSMiklos Szeredi 	if (!save_parent.dentry || retried)
2862f60dc3dbSMiklos Szeredi 		goto out;
2863f60dc3dbSMiklos Szeredi 
2864f60dc3dbSMiklos Szeredi 	BUG_ON(save_parent.dentry != dir);
2865f60dc3dbSMiklos Szeredi 	path_put(&nd->path);
2866f60dc3dbSMiklos Szeredi 	nd->path = save_parent;
2867f60dc3dbSMiklos Szeredi 	nd->inode = dir->d_inode;
2868f60dc3dbSMiklos Szeredi 	save_parent.mnt = NULL;
2869f60dc3dbSMiklos Szeredi 	save_parent.dentry = NULL;
287064894cf8SAl Viro 	if (got_write) {
2871f60dc3dbSMiklos Szeredi 		mnt_drop_write(nd->path.mnt);
287264894cf8SAl Viro 		got_write = false;
2873f60dc3dbSMiklos Szeredi 	}
2874f60dc3dbSMiklos Szeredi 	retried = true;
2875f60dc3dbSMiklos Szeredi 	goto retry_lookup;
2876fb1cc555SAl Viro }
2877fb1cc555SAl Viro 
287813aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname,
287973d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
28801da177e4SLinus Torvalds {
2881fe2d35ffSAl Viro 	struct file *base = NULL;
288230d90494SAl Viro 	struct file *file;
28839850c056SAl Viro 	struct path path;
288447237687SAl Viro 	int opened = 0;
288513aab428SAl Viro 	int error;
288631e6b01fSNick Piggin 
288730d90494SAl Viro 	file = get_empty_filp();
288830d90494SAl Viro 	if (!file)
288931e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
289031e6b01fSNick Piggin 
289130d90494SAl Viro 	file->f_flags = op->open_flag;
289231e6b01fSNick Piggin 
289373d049a4SAl Viro 	error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
289431e6b01fSNick Piggin 	if (unlikely(error))
28952675a4ebSAl Viro 		goto out;
289631e6b01fSNick Piggin 
2897fe2d35ffSAl Viro 	current->total_link_count = 0;
289873d049a4SAl Viro 	error = link_path_walk(pathname, nd);
289931e6b01fSNick Piggin 	if (unlikely(error))
29002675a4ebSAl Viro 		goto out;
29011da177e4SLinus Torvalds 
29022675a4ebSAl Viro 	error = do_last(nd, &path, file, op, &opened, pathname);
29032675a4ebSAl Viro 	while (unlikely(error > 0)) { /* trailing symlink */
29047b9337aaSNick Piggin 		struct path link = path;
2905def4af30SAl Viro 		void *cookie;
2906574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
290773d049a4SAl Viro 			path_put_conditional(&path, nd);
290873d049a4SAl Viro 			path_put(&nd->path);
29092675a4ebSAl Viro 			error = -ELOOP;
291040b39136SAl Viro 			break;
291140b39136SAl Viro 		}
2912800179c9SKees Cook 		error = may_follow_link(&link, nd);
2913800179c9SKees Cook 		if (unlikely(error))
2914800179c9SKees Cook 			break;
291573d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
291673d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2917574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
2918c3e380b0SAl Viro 		if (unlikely(error))
29192675a4ebSAl Viro 			break;
29202675a4ebSAl Viro 		error = do_last(nd, &path, file, op, &opened, pathname);
2921574197e0SAl Viro 		put_link(nd, &link, cookie);
2922806b681cSAl Viro 	}
292310fa8e62SAl Viro out:
292473d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
292573d049a4SAl Viro 		path_put(&nd->root);
2926fe2d35ffSAl Viro 	if (base)
2927fe2d35ffSAl Viro 		fput(base);
29282675a4ebSAl Viro 	if (!(opened & FILE_OPENED)) {
29292675a4ebSAl Viro 		BUG_ON(!error);
293030d90494SAl Viro 		put_filp(file);
2931015c3bbcSMiklos Szeredi 	}
29322675a4ebSAl Viro 	if (unlikely(error)) {
29332675a4ebSAl Viro 		if (error == -EOPENSTALE) {
29342675a4ebSAl Viro 			if (flags & LOOKUP_RCU)
29352675a4ebSAl Viro 				error = -ECHILD;
29362675a4ebSAl Viro 			else
29372675a4ebSAl Viro 				error = -ESTALE;
29382675a4ebSAl Viro 		}
29392675a4ebSAl Viro 		file = ERR_PTR(error);
29402675a4ebSAl Viro 	}
29412675a4ebSAl Viro 	return file;
2942de459215SKirill Korotaev }
29431da177e4SLinus Torvalds 
294413aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname,
294513aab428SAl Viro 		const struct open_flags *op, int flags)
294613aab428SAl Viro {
294773d049a4SAl Viro 	struct nameidata nd;
294813aab428SAl Viro 	struct file *filp;
294913aab428SAl Viro 
295073d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
295113aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
295273d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
295313aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
295473d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
295513aab428SAl Viro 	return filp;
295613aab428SAl Viro }
295713aab428SAl Viro 
295873d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
295973d049a4SAl Viro 		const char *name, const struct open_flags *op, int flags)
296073d049a4SAl Viro {
296173d049a4SAl Viro 	struct nameidata nd;
296273d049a4SAl Viro 	struct file *file;
296373d049a4SAl Viro 
296473d049a4SAl Viro 	nd.root.mnt = mnt;
296573d049a4SAl Viro 	nd.root.dentry = dentry;
296673d049a4SAl Viro 
296773d049a4SAl Viro 	flags |= LOOKUP_ROOT;
296873d049a4SAl Viro 
2969bcda7652SAl Viro 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
297073d049a4SAl Viro 		return ERR_PTR(-ELOOP);
297173d049a4SAl Viro 
297273d049a4SAl Viro 	file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
297373d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
297473d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags);
297573d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
297673d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
297773d049a4SAl Viro 	return file;
297873d049a4SAl Viro }
297973d049a4SAl Viro 
2980ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
29811da177e4SLinus Torvalds {
2982c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
2983ed75e95dSAl Viro 	struct nameidata nd;
2984c30dabfeSJan Kara 	int err2;
2985ed75e95dSAl Viro 	int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2986ed75e95dSAl Viro 	if (error)
2987ed75e95dSAl Viro 		return ERR_PTR(error);
29881da177e4SLinus Torvalds 
2989c663e5d8SChristoph Hellwig 	/*
2990c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2991c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2992c663e5d8SChristoph Hellwig 	 */
2993ed75e95dSAl Viro 	if (nd.last_type != LAST_NORM)
2994ed75e95dSAl Viro 		goto out;
2995ed75e95dSAl Viro 	nd.flags &= ~LOOKUP_PARENT;
2996ed75e95dSAl Viro 	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2997c663e5d8SChristoph Hellwig 
2998c30dabfeSJan Kara 	/* don't fail immediately if it's r/o, at least try to report other errors */
2999c30dabfeSJan Kara 	err2 = mnt_want_write(nd.path.mnt);
3000c663e5d8SChristoph Hellwig 	/*
3001c663e5d8SChristoph Hellwig 	 * Do the final lookup.
3002c663e5d8SChristoph Hellwig 	 */
3003ed75e95dSAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3004ed75e95dSAl Viro 	dentry = lookup_hash(&nd);
30051da177e4SLinus Torvalds 	if (IS_ERR(dentry))
3006a8104a9fSAl Viro 		goto unlock;
3007c663e5d8SChristoph Hellwig 
3008a8104a9fSAl Viro 	error = -EEXIST;
3009e9baf6e5SAl Viro 	if (dentry->d_inode)
3010a8104a9fSAl Viro 		goto fail;
3011c663e5d8SChristoph Hellwig 	/*
3012c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
3013c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
3014c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
3015c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
3016c663e5d8SChristoph Hellwig 	 */
3017ed75e95dSAl Viro 	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
3018a8104a9fSAl Viro 		error = -ENOENT;
3019ed75e95dSAl Viro 		goto fail;
3020e9baf6e5SAl Viro 	}
3021c30dabfeSJan Kara 	if (unlikely(err2)) {
3022c30dabfeSJan Kara 		error = err2;
3023a8104a9fSAl Viro 		goto fail;
3024c30dabfeSJan Kara 	}
3025ed75e95dSAl Viro 	*path = nd.path;
3026e9baf6e5SAl Viro 	return dentry;
30271da177e4SLinus Torvalds fail:
3028a8104a9fSAl Viro 	dput(dentry);
3029a8104a9fSAl Viro 	dentry = ERR_PTR(error);
3030a8104a9fSAl Viro unlock:
3031dae6ad8fSAl Viro 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3032c30dabfeSJan Kara 	if (!err2)
3033c30dabfeSJan Kara 		mnt_drop_write(nd.path.mnt);
3034ed75e95dSAl Viro out:
3035dae6ad8fSAl Viro 	path_put(&nd.path);
3036ed75e95dSAl Viro 	return dentry;
3037dae6ad8fSAl Viro }
3038dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create);
3039dae6ad8fSAl Viro 
3040921a1650SAl Viro void done_path_create(struct path *path, struct dentry *dentry)
3041921a1650SAl Viro {
3042921a1650SAl Viro 	dput(dentry);
3043921a1650SAl Viro 	mutex_unlock(&path->dentry->d_inode->i_mutex);
3044a8104a9fSAl Viro 	mnt_drop_write(path->mnt);
3045921a1650SAl Viro 	path_put(path);
3046921a1650SAl Viro }
3047921a1650SAl Viro EXPORT_SYMBOL(done_path_create);
3048921a1650SAl Viro 
3049dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
3050dae6ad8fSAl Viro {
3051dae6ad8fSAl Viro 	char *tmp = getname(pathname);
3052dae6ad8fSAl Viro 	struct dentry *res;
3053dae6ad8fSAl Viro 	if (IS_ERR(tmp))
3054dae6ad8fSAl Viro 		return ERR_CAST(tmp);
3055dae6ad8fSAl Viro 	res = kern_path_create(dfd, tmp, path, is_dir);
3056dae6ad8fSAl Viro 	putname(tmp);
3057dae6ad8fSAl Viro 	return res;
3058dae6ad8fSAl Viro }
3059dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create);
3060dae6ad8fSAl Viro 
30611a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
30621da177e4SLinus Torvalds {
3063a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
30641da177e4SLinus Torvalds 
30651da177e4SLinus Torvalds 	if (error)
30661da177e4SLinus Torvalds 		return error;
30671da177e4SLinus Torvalds 
3068975d6b39SEric W. Biederman 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
30691da177e4SLinus Torvalds 		return -EPERM;
30701da177e4SLinus Torvalds 
3071acfa4380SAl Viro 	if (!dir->i_op->mknod)
30721da177e4SLinus Torvalds 		return -EPERM;
30731da177e4SLinus Torvalds 
307408ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
307508ce5f16SSerge E. Hallyn 	if (error)
307608ce5f16SSerge E. Hallyn 		return error;
307708ce5f16SSerge E. Hallyn 
30781da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
30791da177e4SLinus Torvalds 	if (error)
30801da177e4SLinus Torvalds 		return error;
30811da177e4SLinus Torvalds 
30821da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
3083a74574aaSStephen Smalley 	if (!error)
3084f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
30851da177e4SLinus Torvalds 	return error;
30861da177e4SLinus Torvalds }
30871da177e4SLinus Torvalds 
3088f69aac00SAl Viro static int may_mknod(umode_t mode)
3089463c3197SDave Hansen {
3090463c3197SDave Hansen 	switch (mode & S_IFMT) {
3091463c3197SDave Hansen 	case S_IFREG:
3092463c3197SDave Hansen 	case S_IFCHR:
3093463c3197SDave Hansen 	case S_IFBLK:
3094463c3197SDave Hansen 	case S_IFIFO:
3095463c3197SDave Hansen 	case S_IFSOCK:
3096463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
3097463c3197SDave Hansen 		return 0;
3098463c3197SDave Hansen 	case S_IFDIR:
3099463c3197SDave Hansen 		return -EPERM;
3100463c3197SDave Hansen 	default:
3101463c3197SDave Hansen 		return -EINVAL;
3102463c3197SDave Hansen 	}
3103463c3197SDave Hansen }
3104463c3197SDave Hansen 
31058208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
31062e4d0924SHeiko Carstens 		unsigned, dev)
31071da177e4SLinus Torvalds {
31081da177e4SLinus Torvalds 	struct dentry *dentry;
3109dae6ad8fSAl Viro 	struct path path;
3110dae6ad8fSAl Viro 	int error;
31111da177e4SLinus Torvalds 
31128e4bfca1SAl Viro 	error = may_mknod(mode);
31138e4bfca1SAl Viro 	if (error)
31148e4bfca1SAl Viro 		return error;
31151da177e4SLinus Torvalds 
3116dae6ad8fSAl Viro 	dentry = user_path_create(dfd, filename, &path, 0);
3117dae6ad8fSAl Viro 	if (IS_ERR(dentry))
3118dae6ad8fSAl Viro 		return PTR_ERR(dentry);
31192ad94ae6SAl Viro 
3120dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3121ce3b0f8dSAl Viro 		mode &= ~current_umask();
3122dae6ad8fSAl Viro 	error = security_path_mknod(&path, dentry, mode, dev);
3123be6d3e56SKentaro Takeda 	if (error)
3124a8104a9fSAl Viro 		goto out;
31251da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
31261da177e4SLinus Torvalds 		case 0: case S_IFREG:
3127312b63fbSAl Viro 			error = vfs_create(path.dentry->d_inode,dentry,mode,true);
31281da177e4SLinus Torvalds 			break;
31291da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
3130dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
31311da177e4SLinus Torvalds 					new_decode_dev(dev));
31321da177e4SLinus Torvalds 			break;
31331da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
3134dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
31351da177e4SLinus Torvalds 			break;
31361da177e4SLinus Torvalds 	}
3137a8104a9fSAl Viro out:
3138921a1650SAl Viro 	done_path_create(&path, dentry);
31391da177e4SLinus Torvalds 	return error;
31401da177e4SLinus Torvalds }
31411da177e4SLinus Torvalds 
31428208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
31435590ff0dSUlrich Drepper {
31445590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
31455590ff0dSUlrich Drepper }
31465590ff0dSUlrich Drepper 
314718bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
31481da177e4SLinus Torvalds {
3149a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
31508de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
31511da177e4SLinus Torvalds 
31521da177e4SLinus Torvalds 	if (error)
31531da177e4SLinus Torvalds 		return error;
31541da177e4SLinus Torvalds 
3155acfa4380SAl Viro 	if (!dir->i_op->mkdir)
31561da177e4SLinus Torvalds 		return -EPERM;
31571da177e4SLinus Torvalds 
31581da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
31591da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
31601da177e4SLinus Torvalds 	if (error)
31611da177e4SLinus Torvalds 		return error;
31621da177e4SLinus Torvalds 
31638de52778SAl Viro 	if (max_links && dir->i_nlink >= max_links)
31648de52778SAl Viro 		return -EMLINK;
31658de52778SAl Viro 
31661da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
3167a74574aaSStephen Smalley 	if (!error)
3168f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
31691da177e4SLinus Torvalds 	return error;
31701da177e4SLinus Torvalds }
31711da177e4SLinus Torvalds 
3172a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
31731da177e4SLinus Torvalds {
31746902d925SDave Hansen 	struct dentry *dentry;
3175dae6ad8fSAl Viro 	struct path path;
3176dae6ad8fSAl Viro 	int error;
31771da177e4SLinus Torvalds 
3178dae6ad8fSAl Viro 	dentry = user_path_create(dfd, pathname, &path, 1);
31796902d925SDave Hansen 	if (IS_ERR(dentry))
3180dae6ad8fSAl Viro 		return PTR_ERR(dentry);
31816902d925SDave Hansen 
3182dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3183ce3b0f8dSAl Viro 		mode &= ~current_umask();
3184dae6ad8fSAl Viro 	error = security_path_mkdir(&path, dentry, mode);
3185a8104a9fSAl Viro 	if (!error)
3186dae6ad8fSAl Viro 		error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3187921a1650SAl Viro 	done_path_create(&path, dentry);
31881da177e4SLinus Torvalds 	return error;
31891da177e4SLinus Torvalds }
31901da177e4SLinus Torvalds 
3191a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
31925590ff0dSUlrich Drepper {
31935590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
31945590ff0dSUlrich Drepper }
31955590ff0dSUlrich Drepper 
31961da177e4SLinus Torvalds /*
3197a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
3198c0d02594SJ. Bruce Fields  * should have a usage count of 1 if we're the only user of this
3199a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
3200a71905f0SSage Weil  * then we drop the dentry now.
32011da177e4SLinus Torvalds  *
32021da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
32031da177e4SLinus Torvalds  * do a
32041da177e4SLinus Torvalds  *
32051da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
32061da177e4SLinus Torvalds  *		return -EBUSY;
32071da177e4SLinus Torvalds  *
32081da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
32091da177e4SLinus Torvalds  * that is still in use by something else..
32101da177e4SLinus Torvalds  */
32111da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
32121da177e4SLinus Torvalds {
32131da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
32141da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
321564252c75SSage Weil 	if (dentry->d_count == 1)
32161da177e4SLinus Torvalds 		__d_drop(dentry);
32171da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
32181da177e4SLinus Torvalds }
32191da177e4SLinus Torvalds 
32201da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
32211da177e4SLinus Torvalds {
32221da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
32231da177e4SLinus Torvalds 
32241da177e4SLinus Torvalds 	if (error)
32251da177e4SLinus Torvalds 		return error;
32261da177e4SLinus Torvalds 
3227acfa4380SAl Viro 	if (!dir->i_op->rmdir)
32281da177e4SLinus Torvalds 		return -EPERM;
32291da177e4SLinus Torvalds 
32301d2ef590SAl Viro 	dget(dentry);
32311b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
3232912dbc15SSage Weil 
32331da177e4SLinus Torvalds 	error = -EBUSY;
3234912dbc15SSage Weil 	if (d_mountpoint(dentry))
3235912dbc15SSage Weil 		goto out;
3236912dbc15SSage Weil 
32371da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
3238912dbc15SSage Weil 	if (error)
3239912dbc15SSage Weil 		goto out;
3240912dbc15SSage Weil 
32413cebde24SSage Weil 	shrink_dcache_parent(dentry);
32421da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
3243912dbc15SSage Weil 	if (error)
3244912dbc15SSage Weil 		goto out;
3245912dbc15SSage Weil 
32461da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
3247d83c49f3SAl Viro 	dont_mount(dentry);
32481da177e4SLinus Torvalds 
3249912dbc15SSage Weil out:
3250912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
32511d2ef590SAl Viro 	dput(dentry);
3252912dbc15SSage Weil 	if (!error)
3253912dbc15SSage Weil 		d_delete(dentry);
32541da177e4SLinus Torvalds 	return error;
32551da177e4SLinus Torvalds }
32561da177e4SLinus Torvalds 
32575590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
32581da177e4SLinus Torvalds {
32591da177e4SLinus Torvalds 	int error = 0;
32601da177e4SLinus Torvalds 	char * name;
32611da177e4SLinus Torvalds 	struct dentry *dentry;
32621da177e4SLinus Torvalds 	struct nameidata nd;
32631da177e4SLinus Torvalds 
32642ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
32651da177e4SLinus Torvalds 	if (error)
32662ad94ae6SAl Viro 		return error;
32671da177e4SLinus Torvalds 
32681da177e4SLinus Torvalds 	switch(nd.last_type) {
32691da177e4SLinus Torvalds 	case LAST_DOTDOT:
32701da177e4SLinus Torvalds 		error = -ENOTEMPTY;
32711da177e4SLinus Torvalds 		goto exit1;
32721da177e4SLinus Torvalds 	case LAST_DOT:
32731da177e4SLinus Torvalds 		error = -EINVAL;
32741da177e4SLinus Torvalds 		goto exit1;
32751da177e4SLinus Torvalds 	case LAST_ROOT:
32761da177e4SLinus Torvalds 		error = -EBUSY;
32771da177e4SLinus Torvalds 		goto exit1;
32781da177e4SLinus Torvalds 	}
32790612d9fbSOGAWA Hirofumi 
32800612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
3281c30dabfeSJan Kara 	error = mnt_want_write(nd.path.mnt);
3282c30dabfeSJan Kara 	if (error)
3283c30dabfeSJan Kara 		goto exit1;
32840612d9fbSOGAWA Hirofumi 
32854ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
328649705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
32871da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
32886902d925SDave Hansen 	if (IS_ERR(dentry))
32896902d925SDave Hansen 		goto exit2;
3290e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
3291e6bc45d6STheodore Ts'o 		error = -ENOENT;
3292e6bc45d6STheodore Ts'o 		goto exit3;
3293e6bc45d6STheodore Ts'o 	}
3294be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
3295be6d3e56SKentaro Takeda 	if (error)
3296c30dabfeSJan Kara 		goto exit3;
32974ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
32980622753bSDave Hansen exit3:
32991da177e4SLinus Torvalds 	dput(dentry);
33006902d925SDave Hansen exit2:
33014ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3302c30dabfeSJan Kara 	mnt_drop_write(nd.path.mnt);
33031da177e4SLinus Torvalds exit1:
33041d957f9bSJan Blunck 	path_put(&nd.path);
33051da177e4SLinus Torvalds 	putname(name);
33061da177e4SLinus Torvalds 	return error;
33071da177e4SLinus Torvalds }
33081da177e4SLinus Torvalds 
33093cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
33105590ff0dSUlrich Drepper {
33115590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
33125590ff0dSUlrich Drepper }
33135590ff0dSUlrich Drepper 
33141da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
33151da177e4SLinus Torvalds {
33161da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
33171da177e4SLinus Torvalds 
33181da177e4SLinus Torvalds 	if (error)
33191da177e4SLinus Torvalds 		return error;
33201da177e4SLinus Torvalds 
3321acfa4380SAl Viro 	if (!dir->i_op->unlink)
33221da177e4SLinus Torvalds 		return -EPERM;
33231da177e4SLinus Torvalds 
33241b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
33251da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
33261da177e4SLinus Torvalds 		error = -EBUSY;
33271da177e4SLinus Torvalds 	else {
33281da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
3329bec1052eSAl Viro 		if (!error) {
33301da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
3331bec1052eSAl Viro 			if (!error)
3332d83c49f3SAl Viro 				dont_mount(dentry);
3333bec1052eSAl Viro 		}
33341da177e4SLinus Torvalds 	}
33351b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
33361da177e4SLinus Torvalds 
33371da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
33381da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
3339ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
33401da177e4SLinus Torvalds 		d_delete(dentry);
33411da177e4SLinus Torvalds 	}
33420eeca283SRobert Love 
33431da177e4SLinus Torvalds 	return error;
33441da177e4SLinus Torvalds }
33451da177e4SLinus Torvalds 
33461da177e4SLinus Torvalds /*
33471da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
33481b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
33491da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
33501da177e4SLinus Torvalds  * while waiting on the I/O.
33511da177e4SLinus Torvalds  */
33525590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
33531da177e4SLinus Torvalds {
33542ad94ae6SAl Viro 	int error;
33551da177e4SLinus Torvalds 	char *name;
33561da177e4SLinus Torvalds 	struct dentry *dentry;
33571da177e4SLinus Torvalds 	struct nameidata nd;
33581da177e4SLinus Torvalds 	struct inode *inode = NULL;
33591da177e4SLinus Torvalds 
33602ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
33611da177e4SLinus Torvalds 	if (error)
33622ad94ae6SAl Viro 		return error;
33632ad94ae6SAl Viro 
33641da177e4SLinus Torvalds 	error = -EISDIR;
33651da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
33661da177e4SLinus Torvalds 		goto exit1;
33670612d9fbSOGAWA Hirofumi 
33680612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
3369c30dabfeSJan Kara 	error = mnt_want_write(nd.path.mnt);
3370c30dabfeSJan Kara 	if (error)
3371c30dabfeSJan Kara 		goto exit1;
33720612d9fbSOGAWA Hirofumi 
33734ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
337449705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
33751da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
33761da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
33771da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
337850338b88STörök Edwin 		if (nd.last.name[nd.last.len])
337950338b88STörök Edwin 			goto slashes;
33801da177e4SLinus Torvalds 		inode = dentry->d_inode;
338150338b88STörök Edwin 		if (!inode)
3382e6bc45d6STheodore Ts'o 			goto slashes;
33837de9c6eeSAl Viro 		ihold(inode);
3384be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
3385be6d3e56SKentaro Takeda 		if (error)
3386c30dabfeSJan Kara 			goto exit2;
33874ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
33881da177e4SLinus Torvalds exit2:
33891da177e4SLinus Torvalds 		dput(dentry);
33901da177e4SLinus Torvalds 	}
33914ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
33921da177e4SLinus Torvalds 	if (inode)
33931da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
3394c30dabfeSJan Kara 	mnt_drop_write(nd.path.mnt);
33951da177e4SLinus Torvalds exit1:
33961d957f9bSJan Blunck 	path_put(&nd.path);
33971da177e4SLinus Torvalds 	putname(name);
33981da177e4SLinus Torvalds 	return error;
33991da177e4SLinus Torvalds 
34001da177e4SLinus Torvalds slashes:
34011da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
34021da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
34031da177e4SLinus Torvalds 	goto exit2;
34041da177e4SLinus Torvalds }
34051da177e4SLinus Torvalds 
34062e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
34075590ff0dSUlrich Drepper {
34085590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
34095590ff0dSUlrich Drepper 		return -EINVAL;
34105590ff0dSUlrich Drepper 
34115590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
34125590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
34135590ff0dSUlrich Drepper 
34145590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
34155590ff0dSUlrich Drepper }
34165590ff0dSUlrich Drepper 
34173480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
34185590ff0dSUlrich Drepper {
34195590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
34205590ff0dSUlrich Drepper }
34215590ff0dSUlrich Drepper 
3422db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
34231da177e4SLinus Torvalds {
3424a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
34251da177e4SLinus Torvalds 
34261da177e4SLinus Torvalds 	if (error)
34271da177e4SLinus Torvalds 		return error;
34281da177e4SLinus Torvalds 
3429acfa4380SAl Viro 	if (!dir->i_op->symlink)
34301da177e4SLinus Torvalds 		return -EPERM;
34311da177e4SLinus Torvalds 
34321da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
34331da177e4SLinus Torvalds 	if (error)
34341da177e4SLinus Torvalds 		return error;
34351da177e4SLinus Torvalds 
34361da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
3437a74574aaSStephen Smalley 	if (!error)
3438f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
34391da177e4SLinus Torvalds 	return error;
34401da177e4SLinus Torvalds }
34411da177e4SLinus Torvalds 
34422e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
34432e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
34441da177e4SLinus Torvalds {
34452ad94ae6SAl Viro 	int error;
34461da177e4SLinus Torvalds 	char *from;
34476902d925SDave Hansen 	struct dentry *dentry;
3448dae6ad8fSAl Viro 	struct path path;
34491da177e4SLinus Torvalds 
34501da177e4SLinus Torvalds 	from = getname(oldname);
34511da177e4SLinus Torvalds 	if (IS_ERR(from))
34521da177e4SLinus Torvalds 		return PTR_ERR(from);
34532ad94ae6SAl Viro 
3454dae6ad8fSAl Viro 	dentry = user_path_create(newdfd, newname, &path, 0);
34551da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
34566902d925SDave Hansen 	if (IS_ERR(dentry))
3457dae6ad8fSAl Viro 		goto out_putname;
34586902d925SDave Hansen 
3459dae6ad8fSAl Viro 	error = security_path_symlink(&path, dentry, from);
3460a8104a9fSAl Viro 	if (!error)
3461dae6ad8fSAl Viro 		error = vfs_symlink(path.dentry->d_inode, dentry, from);
3462921a1650SAl Viro 	done_path_create(&path, dentry);
34636902d925SDave Hansen out_putname:
34641da177e4SLinus Torvalds 	putname(from);
34651da177e4SLinus Torvalds 	return error;
34661da177e4SLinus Torvalds }
34671da177e4SLinus Torvalds 
34683480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
34695590ff0dSUlrich Drepper {
34705590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
34715590ff0dSUlrich Drepper }
34725590ff0dSUlrich Drepper 
34731da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
34741da177e4SLinus Torvalds {
34751da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
34768de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
34771da177e4SLinus Torvalds 	int error;
34781da177e4SLinus Torvalds 
34791da177e4SLinus Torvalds 	if (!inode)
34801da177e4SLinus Torvalds 		return -ENOENT;
34811da177e4SLinus Torvalds 
3482a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
34831da177e4SLinus Torvalds 	if (error)
34841da177e4SLinus Torvalds 		return error;
34851da177e4SLinus Torvalds 
34861da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
34871da177e4SLinus Torvalds 		return -EXDEV;
34881da177e4SLinus Torvalds 
34891da177e4SLinus Torvalds 	/*
34901da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
34911da177e4SLinus Torvalds 	 */
34921da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
34931da177e4SLinus Torvalds 		return -EPERM;
3494acfa4380SAl Viro 	if (!dir->i_op->link)
34951da177e4SLinus Torvalds 		return -EPERM;
34967e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
34971da177e4SLinus Torvalds 		return -EPERM;
34981da177e4SLinus Torvalds 
34991da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
35001da177e4SLinus Torvalds 	if (error)
35011da177e4SLinus Torvalds 		return error;
35021da177e4SLinus Torvalds 
35037e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
3504aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
3505aae8a97dSAneesh Kumar K.V 	if (inode->i_nlink == 0)
3506aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
35078de52778SAl Viro 	else if (max_links && inode->i_nlink >= max_links)
35088de52778SAl Viro 		error = -EMLINK;
3509aae8a97dSAneesh Kumar K.V 	else
35101da177e4SLinus Torvalds 		error = dir->i_op->link(old_dentry, dir, new_dentry);
35117e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
3512e31e14ecSStephen Smalley 	if (!error)
35137e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
35141da177e4SLinus Torvalds 	return error;
35151da177e4SLinus Torvalds }
35161da177e4SLinus Torvalds 
35171da177e4SLinus Torvalds /*
35181da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
35191da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
35201da177e4SLinus Torvalds  * newname.  --KAB
35211da177e4SLinus Torvalds  *
35221da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
35231da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
35241da177e4SLinus Torvalds  * and other special files.  --ADM
35251da177e4SLinus Torvalds  */
35262e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
35272e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
35281da177e4SLinus Torvalds {
35291da177e4SLinus Torvalds 	struct dentry *new_dentry;
3530dae6ad8fSAl Viro 	struct path old_path, new_path;
353111a7b371SAneesh Kumar K.V 	int how = 0;
35321da177e4SLinus Torvalds 	int error;
35331da177e4SLinus Torvalds 
353411a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3535c04030e1SUlrich Drepper 		return -EINVAL;
353611a7b371SAneesh Kumar K.V 	/*
353711a7b371SAneesh Kumar K.V 	 * To use null names we require CAP_DAC_READ_SEARCH
353811a7b371SAneesh Kumar K.V 	 * This ensures that not everyone will be able to create
353911a7b371SAneesh Kumar K.V 	 * handlink using the passed filedescriptor.
354011a7b371SAneesh Kumar K.V 	 */
354111a7b371SAneesh Kumar K.V 	if (flags & AT_EMPTY_PATH) {
354211a7b371SAneesh Kumar K.V 		if (!capable(CAP_DAC_READ_SEARCH))
354311a7b371SAneesh Kumar K.V 			return -ENOENT;
354411a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
354511a7b371SAneesh Kumar K.V 	}
3546c04030e1SUlrich Drepper 
354711a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
354811a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
354911a7b371SAneesh Kumar K.V 
355011a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
35511da177e4SLinus Torvalds 	if (error)
35522ad94ae6SAl Viro 		return error;
35532ad94ae6SAl Viro 
3554dae6ad8fSAl Viro 	new_dentry = user_path_create(newdfd, newname, &new_path, 0);
35551da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
35566902d925SDave Hansen 	if (IS_ERR(new_dentry))
3557dae6ad8fSAl Viro 		goto out;
3558dae6ad8fSAl Viro 
3559dae6ad8fSAl Viro 	error = -EXDEV;
3560dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt)
3561dae6ad8fSAl Viro 		goto out_dput;
3562800179c9SKees Cook 	error = may_linkat(&old_path);
3563800179c9SKees Cook 	if (unlikely(error))
3564800179c9SKees Cook 		goto out_dput;
3565dae6ad8fSAl Viro 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
3566be6d3e56SKentaro Takeda 	if (error)
3567a8104a9fSAl Viro 		goto out_dput;
3568dae6ad8fSAl Viro 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
356975c3f29dSDave Hansen out_dput:
3570921a1650SAl Viro 	done_path_create(&new_path, new_dentry);
35711da177e4SLinus Torvalds out:
35722d8f3038SAl Viro 	path_put(&old_path);
35731da177e4SLinus Torvalds 
35741da177e4SLinus Torvalds 	return error;
35751da177e4SLinus Torvalds }
35761da177e4SLinus Torvalds 
35773480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
35785590ff0dSUlrich Drepper {
3579c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
35805590ff0dSUlrich Drepper }
35815590ff0dSUlrich Drepper 
35821da177e4SLinus Torvalds /*
35831da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
35841da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
35851da177e4SLinus Torvalds  * Problems:
35861da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
35871da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
35881da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
3589a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
35901da177e4SLinus Torvalds  *	   story.
35911da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
35921b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
35931da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
35941da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
3595a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
35961da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
35971da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
35981da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
3599a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
36001da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
36011da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
36021da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
3603e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
36041b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
36051da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3606c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
36071da177e4SLinus Torvalds  *	   locking].
36081da177e4SLinus Torvalds  */
360975c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
36101da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
36111da177e4SLinus Torvalds {
36121da177e4SLinus Torvalds 	int error = 0;
36139055cba7SSage Weil 	struct inode *target = new_dentry->d_inode;
36148de52778SAl Viro 	unsigned max_links = new_dir->i_sb->s_max_links;
36151da177e4SLinus Torvalds 
36161da177e4SLinus Torvalds 	/*
36171da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
36181da177e4SLinus Torvalds 	 * we'll need to flip '..'.
36191da177e4SLinus Torvalds 	 */
36201da177e4SLinus Torvalds 	if (new_dir != old_dir) {
3621f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
36221da177e4SLinus Torvalds 		if (error)
36231da177e4SLinus Torvalds 			return error;
36241da177e4SLinus Torvalds 	}
36251da177e4SLinus Torvalds 
36261da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
36271da177e4SLinus Torvalds 	if (error)
36281da177e4SLinus Torvalds 		return error;
36291da177e4SLinus Torvalds 
36301d2ef590SAl Viro 	dget(new_dentry);
3631d83c49f3SAl Viro 	if (target)
36321b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
36339055cba7SSage Weil 
36341da177e4SLinus Torvalds 	error = -EBUSY;
36359055cba7SSage Weil 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
36369055cba7SSage Weil 		goto out;
36379055cba7SSage Weil 
36388de52778SAl Viro 	error = -EMLINK;
36398de52778SAl Viro 	if (max_links && !target && new_dir != old_dir &&
36408de52778SAl Viro 	    new_dir->i_nlink >= max_links)
36418de52778SAl Viro 		goto out;
36428de52778SAl Viro 
36433cebde24SSage Weil 	if (target)
36443cebde24SSage Weil 		shrink_dcache_parent(new_dentry);
36451da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
36469055cba7SSage Weil 	if (error)
36479055cba7SSage Weil 		goto out;
36489055cba7SSage Weil 
36491da177e4SLinus Torvalds 	if (target) {
36501da177e4SLinus Torvalds 		target->i_flags |= S_DEAD;
3651d83c49f3SAl Viro 		dont_mount(new_dentry);
3652d83c49f3SAl Viro 	}
36539055cba7SSage Weil out:
36549055cba7SSage Weil 	if (target)
36551b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
36561d2ef590SAl Viro 	dput(new_dentry);
3657e31e14ecSStephen Smalley 	if (!error)
3658349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
36591da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
36601da177e4SLinus Torvalds 	return error;
36611da177e4SLinus Torvalds }
36621da177e4SLinus Torvalds 
366375c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
36641da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
36651da177e4SLinus Torvalds {
366651892bbbSSage Weil 	struct inode *target = new_dentry->d_inode;
36671da177e4SLinus Torvalds 	int error;
36681da177e4SLinus Torvalds 
36691da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
36701da177e4SLinus Torvalds 	if (error)
36711da177e4SLinus Torvalds 		return error;
36721da177e4SLinus Torvalds 
36731da177e4SLinus Torvalds 	dget(new_dentry);
36741da177e4SLinus Torvalds 	if (target)
36751b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
367651892bbbSSage Weil 
36771da177e4SLinus Torvalds 	error = -EBUSY;
367851892bbbSSage Weil 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
367951892bbbSSage Weil 		goto out;
368051892bbbSSage Weil 
36811da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
368251892bbbSSage Weil 	if (error)
368351892bbbSSage Weil 		goto out;
368451892bbbSSage Weil 
3685bec1052eSAl Viro 	if (target)
3686d83c49f3SAl Viro 		dont_mount(new_dentry);
3687349457ccSMark Fasheh 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
36881da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
368951892bbbSSage Weil out:
36901da177e4SLinus Torvalds 	if (target)
36911b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
36921da177e4SLinus Torvalds 	dput(new_dentry);
36931da177e4SLinus Torvalds 	return error;
36941da177e4SLinus Torvalds }
36951da177e4SLinus Torvalds 
36961da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
36971da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
36981da177e4SLinus Torvalds {
36991da177e4SLinus Torvalds 	int error;
37001da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
370159b0df21SEric Paris 	const unsigned char *old_name;
37021da177e4SLinus Torvalds 
37031da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
37041da177e4SLinus Torvalds  		return 0;
37051da177e4SLinus Torvalds 
37061da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
37071da177e4SLinus Torvalds 	if (error)
37081da177e4SLinus Torvalds 		return error;
37091da177e4SLinus Torvalds 
37101da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3711a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
37121da177e4SLinus Torvalds 	else
37131da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
37141da177e4SLinus Torvalds 	if (error)
37151da177e4SLinus Torvalds 		return error;
37161da177e4SLinus Torvalds 
3717acfa4380SAl Viro 	if (!old_dir->i_op->rename)
37181da177e4SLinus Torvalds 		return -EPERM;
37191da177e4SLinus Torvalds 
37200eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
37210eeca283SRobert Love 
37221da177e4SLinus Torvalds 	if (is_dir)
37231da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
37241da177e4SLinus Torvalds 	else
37251da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3726123df294SAl Viro 	if (!error)
3727123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
37285a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
37290eeca283SRobert Love 	fsnotify_oldname_free(old_name);
37300eeca283SRobert Love 
37311da177e4SLinus Torvalds 	return error;
37321da177e4SLinus Torvalds }
37331da177e4SLinus Torvalds 
37342e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
37352e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
37361da177e4SLinus Torvalds {
37371da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
37381da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
37391da177e4SLinus Torvalds 	struct dentry *trap;
37401da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
37412ad94ae6SAl Viro 	char *from;
37422ad94ae6SAl Viro 	char *to;
37432ad94ae6SAl Viro 	int error;
37441da177e4SLinus Torvalds 
37452ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
37461da177e4SLinus Torvalds 	if (error)
37471da177e4SLinus Torvalds 		goto exit;
37481da177e4SLinus Torvalds 
37492ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
37501da177e4SLinus Torvalds 	if (error)
37511da177e4SLinus Torvalds 		goto exit1;
37521da177e4SLinus Torvalds 
37531da177e4SLinus Torvalds 	error = -EXDEV;
37544ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
37551da177e4SLinus Torvalds 		goto exit2;
37561da177e4SLinus Torvalds 
37574ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
37581da177e4SLinus Torvalds 	error = -EBUSY;
37591da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
37601da177e4SLinus Torvalds 		goto exit2;
37611da177e4SLinus Torvalds 
37624ac91378SJan Blunck 	new_dir = newnd.path.dentry;
37631da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
37641da177e4SLinus Torvalds 		goto exit2;
37651da177e4SLinus Torvalds 
3766c30dabfeSJan Kara 	error = mnt_want_write(oldnd.path.mnt);
3767c30dabfeSJan Kara 	if (error)
3768c30dabfeSJan Kara 		goto exit2;
3769c30dabfeSJan Kara 
37700612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
37710612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
37724e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
37730612d9fbSOGAWA Hirofumi 
37741da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
37751da177e4SLinus Torvalds 
377649705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
37771da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
37781da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
37791da177e4SLinus Torvalds 		goto exit3;
37801da177e4SLinus Torvalds 	/* source must exist */
37811da177e4SLinus Torvalds 	error = -ENOENT;
37821da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
37831da177e4SLinus Torvalds 		goto exit4;
37841da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
37851da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
37861da177e4SLinus Torvalds 		error = -ENOTDIR;
37871da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
37881da177e4SLinus Torvalds 			goto exit4;
37891da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
37901da177e4SLinus Torvalds 			goto exit4;
37911da177e4SLinus Torvalds 	}
37921da177e4SLinus Torvalds 	/* source should not be ancestor of target */
37931da177e4SLinus Torvalds 	error = -EINVAL;
37941da177e4SLinus Torvalds 	if (old_dentry == trap)
37951da177e4SLinus Torvalds 		goto exit4;
379649705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
37971da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
37981da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
37991da177e4SLinus Torvalds 		goto exit4;
38001da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
38011da177e4SLinus Torvalds 	error = -ENOTEMPTY;
38021da177e4SLinus Torvalds 	if (new_dentry == trap)
38031da177e4SLinus Torvalds 		goto exit5;
38041da177e4SLinus Torvalds 
3805be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3806be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3807be6d3e56SKentaro Takeda 	if (error)
3808c30dabfeSJan Kara 		goto exit5;
38091da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
38101da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
38111da177e4SLinus Torvalds exit5:
38121da177e4SLinus Torvalds 	dput(new_dentry);
38131da177e4SLinus Torvalds exit4:
38141da177e4SLinus Torvalds 	dput(old_dentry);
38151da177e4SLinus Torvalds exit3:
38161da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
3817c30dabfeSJan Kara 	mnt_drop_write(oldnd.path.mnt);
38181da177e4SLinus Torvalds exit2:
38191d957f9bSJan Blunck 	path_put(&newnd.path);
38202ad94ae6SAl Viro 	putname(to);
38211da177e4SLinus Torvalds exit1:
38221d957f9bSJan Blunck 	path_put(&oldnd.path);
38231da177e4SLinus Torvalds 	putname(from);
38242ad94ae6SAl Viro exit:
38251da177e4SLinus Torvalds 	return error;
38261da177e4SLinus Torvalds }
38271da177e4SLinus Torvalds 
3828a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
38295590ff0dSUlrich Drepper {
38305590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
38315590ff0dSUlrich Drepper }
38325590ff0dSUlrich Drepper 
38331da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
38341da177e4SLinus Torvalds {
38351da177e4SLinus Torvalds 	int len;
38361da177e4SLinus Torvalds 
38371da177e4SLinus Torvalds 	len = PTR_ERR(link);
38381da177e4SLinus Torvalds 	if (IS_ERR(link))
38391da177e4SLinus Torvalds 		goto out;
38401da177e4SLinus Torvalds 
38411da177e4SLinus Torvalds 	len = strlen(link);
38421da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
38431da177e4SLinus Torvalds 		len = buflen;
38441da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
38451da177e4SLinus Torvalds 		len = -EFAULT;
38461da177e4SLinus Torvalds out:
38471da177e4SLinus Torvalds 	return len;
38481da177e4SLinus Torvalds }
38491da177e4SLinus Torvalds 
38501da177e4SLinus Torvalds /*
38511da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
38521da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
38531da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
38541da177e4SLinus Torvalds  */
38551da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
38561da177e4SLinus Torvalds {
38571da177e4SLinus Torvalds 	struct nameidata nd;
3858cc314eefSLinus Torvalds 	void *cookie;
3859694a1764SMarcin Slusarz 	int res;
3860cc314eefSLinus Torvalds 
38611da177e4SLinus Torvalds 	nd.depth = 0;
3862cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3863694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3864694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3865694a1764SMarcin Slusarz 
3866694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
38671da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3868cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3869694a1764SMarcin Slusarz 	return res;
38701da177e4SLinus Torvalds }
38711da177e4SLinus Torvalds 
38721da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
38731da177e4SLinus Torvalds {
38741da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
38751da177e4SLinus Torvalds }
38761da177e4SLinus Torvalds 
38771da177e4SLinus Torvalds /* get the link contents into pagecache */
38781da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
38791da177e4SLinus Torvalds {
3880ebd09abbSDuane Griffin 	char *kaddr;
38811da177e4SLinus Torvalds 	struct page *page;
38821da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3883090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
38841da177e4SLinus Torvalds 	if (IS_ERR(page))
38856fe6900eSNick Piggin 		return (char*)page;
38861da177e4SLinus Torvalds 	*ppage = page;
3887ebd09abbSDuane Griffin 	kaddr = kmap(page);
3888ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3889ebd09abbSDuane Griffin 	return kaddr;
38901da177e4SLinus Torvalds }
38911da177e4SLinus Torvalds 
38921da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
38931da177e4SLinus Torvalds {
38941da177e4SLinus Torvalds 	struct page *page = NULL;
38951da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
38961da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
38971da177e4SLinus Torvalds 	if (page) {
38981da177e4SLinus Torvalds 		kunmap(page);
38991da177e4SLinus Torvalds 		page_cache_release(page);
39001da177e4SLinus Torvalds 	}
39011da177e4SLinus Torvalds 	return res;
39021da177e4SLinus Torvalds }
39031da177e4SLinus Torvalds 
3904cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
39051da177e4SLinus Torvalds {
3906cc314eefSLinus Torvalds 	struct page *page = NULL;
39071da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3908cc314eefSLinus Torvalds 	return page;
39091da177e4SLinus Torvalds }
39101da177e4SLinus Torvalds 
3911cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
39121da177e4SLinus Torvalds {
3913cc314eefSLinus Torvalds 	struct page *page = cookie;
3914cc314eefSLinus Torvalds 
3915cc314eefSLinus Torvalds 	if (page) {
39161da177e4SLinus Torvalds 		kunmap(page);
39171da177e4SLinus Torvalds 		page_cache_release(page);
39181da177e4SLinus Torvalds 	}
39191da177e4SLinus Torvalds }
39201da177e4SLinus Torvalds 
392154566b2cSNick Piggin /*
392254566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
392354566b2cSNick Piggin  */
392454566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
39251da177e4SLinus Torvalds {
39261da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
39270adb25d2SKirill Korotaev 	struct page *page;
3928afddba49SNick Piggin 	void *fsdata;
3929beb497abSDmitriy Monakhov 	int err;
39301da177e4SLinus Torvalds 	char *kaddr;
393154566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
393254566b2cSNick Piggin 	if (nofs)
393354566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
39341da177e4SLinus Torvalds 
39357e53cac4SNeilBrown retry:
3936afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
393754566b2cSNick Piggin 				flags, &page, &fsdata);
39381da177e4SLinus Torvalds 	if (err)
3939afddba49SNick Piggin 		goto fail;
3940afddba49SNick Piggin 
3941e8e3c3d6SCong Wang 	kaddr = kmap_atomic(page);
39421da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
3943e8e3c3d6SCong Wang 	kunmap_atomic(kaddr);
3944afddba49SNick Piggin 
3945afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3946afddba49SNick Piggin 							page, fsdata);
39471da177e4SLinus Torvalds 	if (err < 0)
39481da177e4SLinus Torvalds 		goto fail;
3949afddba49SNick Piggin 	if (err < len-1)
3950afddba49SNick Piggin 		goto retry;
3951afddba49SNick Piggin 
39521da177e4SLinus Torvalds 	mark_inode_dirty(inode);
39531da177e4SLinus Torvalds 	return 0;
39541da177e4SLinus Torvalds fail:
39551da177e4SLinus Torvalds 	return err;
39561da177e4SLinus Torvalds }
39571da177e4SLinus Torvalds 
39580adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
39590adb25d2SKirill Korotaev {
39600adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
396154566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
39620adb25d2SKirill Korotaev }
39630adb25d2SKirill Korotaev 
396492e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
39651da177e4SLinus Torvalds 	.readlink	= generic_readlink,
39661da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
39671da177e4SLinus Torvalds 	.put_link	= page_put_link,
39681da177e4SLinus Torvalds };
39691da177e4SLinus Torvalds 
39702d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
3971cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
39721da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
39731da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
39741da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
39751da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
39761da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
39771da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
39781da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
39791da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
39801da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
39810adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
39821da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
39831da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
3984d1811465SAl Viro EXPORT_SYMBOL(kern_path);
398516f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3986f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
39871da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
39881da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
39891da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
39901da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
39911da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
39921da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
39931da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
39941da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
39951da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
39961da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
39971da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
39981da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
39991da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
40001da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
4001