xref: /openbmc/linux/fs/namei.c (revision 697f514d)
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 /**
318cb23beb5SChristoph Hellwig  * inode_permission  -  check for access rights to a given inode
319cb23beb5SChristoph Hellwig  * @inode:	inode to check permission on
3208fd90c8dSAndreas Gruenbacher  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
321cb23beb5SChristoph Hellwig  *
322cb23beb5SChristoph Hellwig  * Used to check for read/write/execute permissions on an inode.
323cb23beb5SChristoph Hellwig  * We use "fsuid" for this, letting us set arbitrary permissions
324cb23beb5SChristoph Hellwig  * for filesystem access without changing the "normal" uids which
325cb23beb5SChristoph Hellwig  * are used for other things.
326948409c7SAndreas Gruenbacher  *
327948409c7SAndreas Gruenbacher  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
328cb23beb5SChristoph Hellwig  */
329f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask)
3301da177e4SLinus Torvalds {
331e6305c43SAl Viro 	int retval;
3321da177e4SLinus Torvalds 
3333ddcd056SLinus Torvalds 	if (unlikely(mask & MAY_WRITE)) {
33422590e41SMiklos Szeredi 		umode_t mode = inode->i_mode;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 		/*
3371da177e4SLinus Torvalds 		 * Nobody gets write access to a read-only fs.
3381da177e4SLinus Torvalds 		 */
3391da177e4SLinus Torvalds 		if (IS_RDONLY(inode) &&
3401da177e4SLinus Torvalds 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
3411da177e4SLinus Torvalds 			return -EROFS;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 		/*
3441da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
3451da177e4SLinus Torvalds 		 */
3461da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
3471da177e4SLinus Torvalds 			return -EACCES;
3481da177e4SLinus Torvalds 	}
3491da177e4SLinus Torvalds 
3503ddcd056SLinus Torvalds 	retval = do_inode_permission(inode, mask);
3511da177e4SLinus Torvalds 	if (retval)
3521da177e4SLinus Torvalds 		return retval;
3531da177e4SLinus Torvalds 
35408ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
35508ce5f16SSerge E. Hallyn 	if (retval)
35608ce5f16SSerge E. Hallyn 		return retval;
35708ce5f16SSerge E. Hallyn 
358d09ca739SEric Paris 	return security_inode_permission(inode, mask);
3591da177e4SLinus Torvalds }
3601da177e4SLinus Torvalds 
361f4d6ff89SAl Viro /**
3625dd784d0SJan Blunck  * path_get - get a reference to a path
3635dd784d0SJan Blunck  * @path: path to get the reference to
3645dd784d0SJan Blunck  *
3655dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
3665dd784d0SJan Blunck  */
3675dd784d0SJan Blunck void path_get(struct path *path)
3685dd784d0SJan Blunck {
3695dd784d0SJan Blunck 	mntget(path->mnt);
3705dd784d0SJan Blunck 	dget(path->dentry);
3715dd784d0SJan Blunck }
3725dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
3735dd784d0SJan Blunck 
3745dd784d0SJan Blunck /**
3751d957f9bSJan Blunck  * path_put - put a reference to a path
3761d957f9bSJan Blunck  * @path: path to put the reference to
3771d957f9bSJan Blunck  *
3781d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
3791d957f9bSJan Blunck  */
3801d957f9bSJan Blunck void path_put(struct path *path)
3811da177e4SLinus Torvalds {
3821d957f9bSJan Blunck 	dput(path->dentry);
3831d957f9bSJan Blunck 	mntput(path->mnt);
3841da177e4SLinus Torvalds }
3851d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
3861da177e4SLinus Torvalds 
38719660af7SAl Viro /*
38831e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
38919660af7SAl Viro  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
39019660af7SAl Viro  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
39119660af7SAl Viro  * normal reference counts on dentries and vfsmounts to transition to rcu-walk
39219660af7SAl Viro  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
39319660af7SAl Viro  * got stuck, so ref-walk may continue from there. If this is not successful
39419660af7SAl Viro  * (eg. a seqcount has changed), then failure is returned and it's up to caller
39519660af7SAl Viro  * to restart the path walk from the beginning in ref-walk mode.
39631e6b01fSNick Piggin  */
39731e6b01fSNick Piggin 
39831e6b01fSNick Piggin /**
39919660af7SAl Viro  * unlazy_walk - try to switch to ref-walk mode.
40019660af7SAl Viro  * @nd: nameidata pathwalk data
40119660af7SAl Viro  * @dentry: child of nd->path.dentry or NULL
40239191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
40331e6b01fSNick Piggin  *
40419660af7SAl Viro  * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
40519660af7SAl Viro  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
40619660af7SAl Viro  * @nd or NULL.  Must be called from rcu-walk context.
40731e6b01fSNick Piggin  */
40819660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
40931e6b01fSNick Piggin {
41031e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
41131e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
4125b6ca027SAl Viro 	int want_root = 0;
41331e6b01fSNick Piggin 
41431e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
4155b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
4165b6ca027SAl Viro 		want_root = 1;
41731e6b01fSNick Piggin 		spin_lock(&fs->lock);
41831e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
41931e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
42031e6b01fSNick Piggin 			goto err_root;
42131e6b01fSNick Piggin 	}
42231e6b01fSNick Piggin 	spin_lock(&parent->d_lock);
42319660af7SAl Viro 	if (!dentry) {
42419660af7SAl Viro 		if (!__d_rcu_to_refcount(parent, nd->seq))
42519660af7SAl Viro 			goto err_parent;
42619660af7SAl Viro 		BUG_ON(nd->inode != parent->d_inode);
42719660af7SAl Viro 	} else {
42894c0d4ecSAl Viro 		if (dentry->d_parent != parent)
42994c0d4ecSAl Viro 			goto err_parent;
43031e6b01fSNick Piggin 		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
43131e6b01fSNick Piggin 		if (!__d_rcu_to_refcount(dentry, nd->seq))
43219660af7SAl Viro 			goto err_child;
43331e6b01fSNick Piggin 		/*
43419660af7SAl Viro 		 * If the sequence check on the child dentry passed, then
43519660af7SAl Viro 		 * the child has not been removed from its parent. This
43619660af7SAl Viro 		 * means the parent dentry must be valid and able to take
43719660af7SAl Viro 		 * a reference at this point.
43831e6b01fSNick Piggin 		 */
43931e6b01fSNick Piggin 		BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
44031e6b01fSNick Piggin 		BUG_ON(!parent->d_count);
44131e6b01fSNick Piggin 		parent->d_count++;
44231e6b01fSNick Piggin 		spin_unlock(&dentry->d_lock);
44319660af7SAl Viro 	}
44431e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
4455b6ca027SAl Viro 	if (want_root) {
44631e6b01fSNick Piggin 		path_get(&nd->root);
44731e6b01fSNick Piggin 		spin_unlock(&fs->lock);
44831e6b01fSNick Piggin 	}
44931e6b01fSNick Piggin 	mntget(nd->path.mnt);
45031e6b01fSNick Piggin 
45131e6b01fSNick Piggin 	rcu_read_unlock();
452962830dfSAndi Kleen 	br_read_unlock(&vfsmount_lock);
45331e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
45431e6b01fSNick Piggin 	return 0;
45519660af7SAl Viro 
45619660af7SAl Viro err_child:
45731e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
45819660af7SAl Viro err_parent:
45931e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
46031e6b01fSNick Piggin err_root:
4615b6ca027SAl Viro 	if (want_root)
46231e6b01fSNick Piggin 		spin_unlock(&fs->lock);
46331e6b01fSNick Piggin 	return -ECHILD;
46431e6b01fSNick Piggin }
46531e6b01fSNick Piggin 
46631e6b01fSNick Piggin /**
467834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
468834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
469834f2a4aSTrond Myklebust  */
470834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
471834f2a4aSTrond Myklebust {
4722dab5974SLinus Torvalds 	struct file *file = nd->intent.open.file;
4732dab5974SLinus Torvalds 
4742dab5974SLinus Torvalds 	if (file && !IS_ERR(file)) {
4752dab5974SLinus Torvalds 		if (file->f_path.dentry == NULL)
4762dab5974SLinus Torvalds 			put_filp(file);
477834f2a4aSTrond Myklebust 		else
4782dab5974SLinus Torvalds 			fput(file);
4792dab5974SLinus Torvalds 	}
480834f2a4aSTrond Myklebust }
481834f2a4aSTrond Myklebust 
482f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
48334286d66SNick Piggin {
484f60aef7eSAl Viro 	return dentry->d_op->d_revalidate(dentry, nd);
48534286d66SNick Piggin }
48634286d66SNick Piggin 
4879f1fafeeSAl Viro /**
4889f1fafeeSAl Viro  * complete_walk - successful completion of path walk
4899f1fafeeSAl Viro  * @nd:  pointer nameidata
49039159de2SJeff Layton  *
4919f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
4929f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
4939f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
4949f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
4959f1fafeeSAl Viro  * need to drop nd->path.
49639159de2SJeff Layton  */
4979f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
49839159de2SJeff Layton {
49916c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
50039159de2SJeff Layton 	int status;
50139159de2SJeff Layton 
5029f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
5039f1fafeeSAl Viro 		nd->flags &= ~LOOKUP_RCU;
5049f1fafeeSAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
5059f1fafeeSAl Viro 			nd->root.mnt = NULL;
5069f1fafeeSAl Viro 		spin_lock(&dentry->d_lock);
5079f1fafeeSAl Viro 		if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
5089f1fafeeSAl Viro 			spin_unlock(&dentry->d_lock);
5099f1fafeeSAl Viro 			rcu_read_unlock();
510962830dfSAndi Kleen 			br_read_unlock(&vfsmount_lock);
5119f1fafeeSAl Viro 			return -ECHILD;
5129f1fafeeSAl Viro 		}
5139f1fafeeSAl Viro 		BUG_ON(nd->inode != dentry->d_inode);
5149f1fafeeSAl Viro 		spin_unlock(&dentry->d_lock);
5159f1fafeeSAl Viro 		mntget(nd->path.mnt);
5169f1fafeeSAl Viro 		rcu_read_unlock();
517962830dfSAndi Kleen 		br_read_unlock(&vfsmount_lock);
5189f1fafeeSAl Viro 	}
5199f1fafeeSAl Viro 
52016c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
52139159de2SJeff Layton 		return 0;
52239159de2SJeff Layton 
52316c2cd71SAl Viro 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
52416c2cd71SAl Viro 		return 0;
52516c2cd71SAl Viro 
52616c2cd71SAl Viro 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
52716c2cd71SAl Viro 		return 0;
52816c2cd71SAl Viro 
52916c2cd71SAl Viro 	/* Note: we do not d_invalidate() */
53034286d66SNick Piggin 	status = d_revalidate(dentry, nd);
53139159de2SJeff Layton 	if (status > 0)
53239159de2SJeff Layton 		return 0;
53339159de2SJeff Layton 
53416c2cd71SAl Viro 	if (!status)
53539159de2SJeff Layton 		status = -ESTALE;
53616c2cd71SAl Viro 
5379f1fafeeSAl Viro 	path_put(&nd->path);
53839159de2SJeff Layton 	return status;
53939159de2SJeff Layton }
54039159de2SJeff Layton 
5412a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
5422a737871SAl Viro {
543f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
544f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
5452a737871SAl Viro }
5462a737871SAl Viro 
5476de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
5486de88d72SAl Viro 
54931e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
55031e6b01fSNick Piggin {
55131e6b01fSNick Piggin 	if (!nd->root.mnt) {
55231e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
553c28cc364SNick Piggin 		unsigned seq;
554c28cc364SNick Piggin 
555c28cc364SNick Piggin 		do {
556c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
55731e6b01fSNick Piggin 			nd->root = fs->root;
558c1530019STim Chen 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
559c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
56031e6b01fSNick Piggin 	}
56131e6b01fSNick Piggin }
56231e6b01fSNick Piggin 
563f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
5641da177e4SLinus Torvalds {
56531e6b01fSNick Piggin 	int ret;
56631e6b01fSNick Piggin 
5671da177e4SLinus Torvalds 	if (IS_ERR(link))
5681da177e4SLinus Torvalds 		goto fail;
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	if (*link == '/') {
5712a737871SAl Viro 		set_root(nd);
5721d957f9bSJan Blunck 		path_put(&nd->path);
5732a737871SAl Viro 		nd->path = nd->root;
5742a737871SAl Viro 		path_get(&nd->root);
57516c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
5761da177e4SLinus Torvalds 	}
57731e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
578b4091d5fSChristoph Hellwig 
57931e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
58031e6b01fSNick Piggin 	return ret;
5811da177e4SLinus Torvalds fail:
5821d957f9bSJan Blunck 	path_put(&nd->path);
5831da177e4SLinus Torvalds 	return PTR_ERR(link);
5841da177e4SLinus Torvalds }
5851da177e4SLinus Torvalds 
5861d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
587051d3812SIan Kent {
588051d3812SIan Kent 	dput(path->dentry);
5894ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
590051d3812SIan Kent 		mntput(path->mnt);
591051d3812SIan Kent }
592051d3812SIan Kent 
5937b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
5947b9337aaSNick Piggin 					struct nameidata *nd)
595051d3812SIan Kent {
59631e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
5974ac91378SJan Blunck 		dput(nd->path.dentry);
59831e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
5994ac91378SJan Blunck 			mntput(nd->path.mnt);
6009a229683SHuang Shijie 	}
60131e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
6024ac91378SJan Blunck 	nd->path.dentry = path->dentry;
603051d3812SIan Kent }
604051d3812SIan Kent 
605574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
606574197e0SAl Viro {
607574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
608574197e0SAl Viro 	if (!IS_ERR(cookie) && inode->i_op->put_link)
609574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
610574197e0SAl Viro 	path_put(link);
611574197e0SAl Viro }
612574197e0SAl Viro 
613def4af30SAl Viro static __always_inline int
614574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
6151da177e4SLinus Torvalds {
6161da177e4SLinus Torvalds 	int error;
6177b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
6181da177e4SLinus Torvalds 
619844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
620844a3917SAl Viro 
6210e794589SAl Viro 	if (link->mnt == nd->path.mnt)
6220e794589SAl Viro 		mntget(link->mnt);
6230e794589SAl Viro 
624574197e0SAl Viro 	if (unlikely(current->total_link_count >= 40)) {
625574197e0SAl Viro 		*p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
626574197e0SAl Viro 		path_put(&nd->path);
627574197e0SAl Viro 		return -ELOOP;
628574197e0SAl Viro 	}
629574197e0SAl Viro 	cond_resched();
630574197e0SAl Viro 	current->total_link_count++;
631574197e0SAl Viro 
63268ac1234SAl Viro 	touch_atime(link);
6331da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
634cd4e91d3SAl Viro 
63536f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
63636f3b4f6SAl Viro 	if (error) {
63736f3b4f6SAl Viro 		*p = ERR_PTR(error); /* no ->put_link(), please */
63836f3b4f6SAl Viro 		path_put(&nd->path);
63936f3b4f6SAl Viro 		return error;
64036f3b4f6SAl Viro 	}
64136f3b4f6SAl Viro 
64286acdca1SAl Viro 	nd->last_type = LAST_BIND;
643def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
644def4af30SAl Viro 	error = PTR_ERR(*p);
645def4af30SAl Viro 	if (!IS_ERR(*p)) {
6461da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
647cc314eefSLinus Torvalds 		error = 0;
6481da177e4SLinus Torvalds 		if (s)
6491da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
650bcda7652SAl Viro 		else if (nd->last_type == LAST_BIND) {
65116c2cd71SAl Viro 			nd->flags |= LOOKUP_JUMPED;
652b21041d0SAl Viro 			nd->inode = nd->path.dentry->d_inode;
653b21041d0SAl Viro 			if (nd->inode->i_op->follow_link) {
654bcda7652SAl Viro 				/* stepped on a _really_ weird one */
655bcda7652SAl Viro 				path_put(&nd->path);
656bcda7652SAl Viro 				error = -ELOOP;
657bcda7652SAl Viro 			}
658bcda7652SAl Viro 		}
6591da177e4SLinus Torvalds 	}
6601da177e4SLinus Torvalds 	return error;
6611da177e4SLinus Torvalds }
6621da177e4SLinus Torvalds 
66331e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
66431e6b01fSNick Piggin {
6650714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
6660714a533SAl Viro 	struct mount *parent;
66731e6b01fSNick Piggin 	struct dentry *mountpoint;
66831e6b01fSNick Piggin 
6690714a533SAl Viro 	parent = mnt->mnt_parent;
6700714a533SAl Viro 	if (&parent->mnt == path->mnt)
67131e6b01fSNick Piggin 		return 0;
672a73324daSAl Viro 	mountpoint = mnt->mnt_mountpoint;
67331e6b01fSNick Piggin 	path->dentry = mountpoint;
6740714a533SAl Viro 	path->mnt = &parent->mnt;
67531e6b01fSNick Piggin 	return 1;
67631e6b01fSNick Piggin }
67731e6b01fSNick Piggin 
678bab77ebfSAl Viro int follow_up(struct path *path)
6791da177e4SLinus Torvalds {
6800714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
6810714a533SAl Viro 	struct mount *parent;
6821da177e4SLinus Torvalds 	struct dentry *mountpoint;
68399b7db7bSNick Piggin 
684962830dfSAndi Kleen 	br_read_lock(&vfsmount_lock);
6850714a533SAl Viro 	parent = mnt->mnt_parent;
6860714a533SAl Viro 	if (&parent->mnt == path->mnt) {
687962830dfSAndi Kleen 		br_read_unlock(&vfsmount_lock);
6881da177e4SLinus Torvalds 		return 0;
6891da177e4SLinus Torvalds 	}
6900714a533SAl Viro 	mntget(&parent->mnt);
691a73324daSAl Viro 	mountpoint = dget(mnt->mnt_mountpoint);
692962830dfSAndi Kleen 	br_read_unlock(&vfsmount_lock);
693bab77ebfSAl Viro 	dput(path->dentry);
694bab77ebfSAl Viro 	path->dentry = mountpoint;
695bab77ebfSAl Viro 	mntput(path->mnt);
6960714a533SAl Viro 	path->mnt = &parent->mnt;
6971da177e4SLinus Torvalds 	return 1;
6981da177e4SLinus Torvalds }
6991da177e4SLinus Torvalds 
700b5c84bf6SNick Piggin /*
7019875cf80SDavid Howells  * Perform an automount
7029875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
7039875cf80SDavid Howells  *   were called with.
7041da177e4SLinus Torvalds  */
7059875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
7069875cf80SDavid Howells 			    bool *need_mntput)
70731e6b01fSNick Piggin {
7089875cf80SDavid Howells 	struct vfsmount *mnt;
709ea5b778aSDavid Howells 	int err;
7109875cf80SDavid Howells 
7119875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
7129875cf80SDavid Howells 		return -EREMOTE;
7139875cf80SDavid Howells 
7140ec26fd0SMiklos Szeredi 	/* We don't want to mount if someone's just doing a stat -
7150ec26fd0SMiklos Szeredi 	 * unless they're stat'ing a directory and appended a '/' to
7160ec26fd0SMiklos Szeredi 	 * the name.
7170ec26fd0SMiklos Szeredi 	 *
7180ec26fd0SMiklos Szeredi 	 * We do, however, want to mount if someone wants to open or
7195a30d8a2SDavid Howells 	 * create a file of any type under the mountpoint, wants to
7200ec26fd0SMiklos Szeredi 	 * traverse through the mountpoint or wants to open the
7210ec26fd0SMiklos Szeredi 	 * mounted directory.  Also, autofs may mark negative dentries
7220ec26fd0SMiklos Szeredi 	 * as being automount points.  These will need the attentions
7230ec26fd0SMiklos Szeredi 	 * of the daemon to instantiate them before they can be used.
7245a30d8a2SDavid Howells 	 */
7255a30d8a2SDavid Howells 	if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
726d94c177bSLinus Torvalds 		     LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
7275a30d8a2SDavid Howells 	    path->dentry->d_inode)
7289875cf80SDavid Howells 		return -EISDIR;
7290ec26fd0SMiklos Szeredi 
7309875cf80SDavid Howells 	current->total_link_count++;
7319875cf80SDavid Howells 	if (current->total_link_count >= 40)
7329875cf80SDavid Howells 		return -ELOOP;
7339875cf80SDavid Howells 
7349875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
7359875cf80SDavid Howells 	if (IS_ERR(mnt)) {
7369875cf80SDavid Howells 		/*
7379875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
7389875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
7399875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
7409875cf80SDavid Howells 		 *
7419875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
7429875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
7439875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
7449875cf80SDavid Howells 		 */
74549084c3bSAl Viro 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
7469875cf80SDavid Howells 			return -EREMOTE;
7479875cf80SDavid Howells 		return PTR_ERR(mnt);
74831e6b01fSNick Piggin 	}
749ea5b778aSDavid Howells 
7509875cf80SDavid Howells 	if (!mnt) /* mount collision */
7519875cf80SDavid Howells 		return 0;
7529875cf80SDavid Howells 
7538aef1884SAl Viro 	if (!*need_mntput) {
7548aef1884SAl Viro 		/* lock_mount() may release path->mnt on error */
7558aef1884SAl Viro 		mntget(path->mnt);
7568aef1884SAl Viro 		*need_mntput = true;
7578aef1884SAl Viro 	}
75819a167afSAl Viro 	err = finish_automount(mnt, path);
759ea5b778aSDavid Howells 
760ea5b778aSDavid Howells 	switch (err) {
761ea5b778aSDavid Howells 	case -EBUSY:
762ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
76319a167afSAl Viro 		return 0;
764ea5b778aSDavid Howells 	case 0:
7658aef1884SAl Viro 		path_put(path);
7669875cf80SDavid Howells 		path->mnt = mnt;
7679875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
7689875cf80SDavid Howells 		return 0;
76919a167afSAl Viro 	default:
77019a167afSAl Viro 		return err;
7719875cf80SDavid Howells 	}
77219a167afSAl Viro 
773ea5b778aSDavid Howells }
7749875cf80SDavid Howells 
7759875cf80SDavid Howells /*
7769875cf80SDavid Howells  * Handle a dentry that is managed in some way.
777cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
7789875cf80SDavid Howells  * - Flagged as mountpoint
7799875cf80SDavid Howells  * - Flagged as automount point
7809875cf80SDavid Howells  *
7819875cf80SDavid Howells  * This may only be called in refwalk mode.
7829875cf80SDavid Howells  *
7839875cf80SDavid Howells  * Serialization is taken care of in namespace.c
7849875cf80SDavid Howells  */
7859875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
7869875cf80SDavid Howells {
7878aef1884SAl Viro 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
7889875cf80SDavid Howells 	unsigned managed;
7899875cf80SDavid Howells 	bool need_mntput = false;
7908aef1884SAl Viro 	int ret = 0;
7919875cf80SDavid Howells 
7929875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
7939875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
7949875cf80SDavid Howells 	 * the components of that value change under us */
7959875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
7969875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
7979875cf80SDavid Howells 	       unlikely(managed != 0)) {
798cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
799cc53ce53SDavid Howells 		 * being held. */
800cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
801cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
802cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
8031aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
804cc53ce53SDavid Howells 			if (ret < 0)
8058aef1884SAl Viro 				break;
806cc53ce53SDavid Howells 		}
807cc53ce53SDavid Howells 
8089875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
8099875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
8109875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
8119875cf80SDavid Howells 			if (mounted) {
8129875cf80SDavid Howells 				dput(path->dentry);
8139875cf80SDavid Howells 				if (need_mntput)
814463ffb2eSAl Viro 					mntput(path->mnt);
815463ffb2eSAl Viro 				path->mnt = mounted;
816463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
8179875cf80SDavid Howells 				need_mntput = true;
8189875cf80SDavid Howells 				continue;
819463ffb2eSAl Viro 			}
820463ffb2eSAl Viro 
8219875cf80SDavid Howells 			/* Something is mounted on this dentry in another
8229875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
8239875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
8249875cf80SDavid Howells 			 * vfsmount_lock */
8251da177e4SLinus Torvalds 		}
8269875cf80SDavid Howells 
8279875cf80SDavid Howells 		/* Handle an automount point */
8289875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
8299875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
8309875cf80SDavid Howells 			if (ret < 0)
8318aef1884SAl Viro 				break;
8329875cf80SDavid Howells 			continue;
8339875cf80SDavid Howells 		}
8349875cf80SDavid Howells 
8359875cf80SDavid Howells 		/* We didn't change the current path point */
8369875cf80SDavid Howells 		break;
8379875cf80SDavid Howells 	}
8388aef1884SAl Viro 
8398aef1884SAl Viro 	if (need_mntput && path->mnt == mnt)
8408aef1884SAl Viro 		mntput(path->mnt);
8418aef1884SAl Viro 	if (ret == -EISDIR)
8428aef1884SAl Viro 		ret = 0;
843a3fbbde7SAl Viro 	return ret < 0 ? ret : need_mntput;
8441da177e4SLinus Torvalds }
8451da177e4SLinus Torvalds 
846cc53ce53SDavid Howells int follow_down_one(struct path *path)
8471da177e4SLinus Torvalds {
8481da177e4SLinus Torvalds 	struct vfsmount *mounted;
8491da177e4SLinus Torvalds 
8501c755af4SAl Viro 	mounted = lookup_mnt(path);
8511da177e4SLinus Torvalds 	if (mounted) {
8529393bd07SAl Viro 		dput(path->dentry);
8539393bd07SAl Viro 		mntput(path->mnt);
8549393bd07SAl Viro 		path->mnt = mounted;
8559393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
8561da177e4SLinus Torvalds 		return 1;
8571da177e4SLinus Torvalds 	}
8581da177e4SLinus Torvalds 	return 0;
8591da177e4SLinus Torvalds }
8601da177e4SLinus Torvalds 
86162a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
86262a7375eSIan Kent {
86362a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
86462a7375eSIan Kent 		dentry->d_op->d_manage(dentry, true) < 0);
86562a7375eSIan Kent }
86662a7375eSIan Kent 
8679875cf80SDavid Howells /*
868287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
869287548e4SAl Viro  * we meet a managed dentry that would need blocking.
8709875cf80SDavid Howells  */
8719875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
872287548e4SAl Viro 			       struct inode **inode)
8739875cf80SDavid Howells {
87462a7375eSIan Kent 	for (;;) {
875c7105365SAl Viro 		struct mount *mounted;
87662a7375eSIan Kent 		/*
87762a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
87862a7375eSIan Kent 		 * that wants to block transit.
87962a7375eSIan Kent 		 */
880287548e4SAl Viro 		if (unlikely(managed_dentry_might_block(path->dentry)))
881ab90911fSDavid Howells 			return false;
88262a7375eSIan Kent 
88362a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
88462a7375eSIan Kent 			break;
88562a7375eSIan Kent 
8869875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
8879875cf80SDavid Howells 		if (!mounted)
8889875cf80SDavid Howells 			break;
889c7105365SAl Viro 		path->mnt = &mounted->mnt;
890c7105365SAl Viro 		path->dentry = mounted->mnt.mnt_root;
891a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
8929875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
89359430262SLinus Torvalds 		/*
89459430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
89559430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
89659430262SLinus Torvalds 		 * because a mount-point is always pinned.
89759430262SLinus Torvalds 		 */
89859430262SLinus Torvalds 		*inode = path->dentry->d_inode;
8999875cf80SDavid Howells 	}
9009875cf80SDavid Howells 	return true;
9019875cf80SDavid Howells }
9029875cf80SDavid Howells 
903dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd)
904287548e4SAl Viro {
905dea39376SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
906c7105365SAl Viro 		struct mount *mounted;
907dea39376SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
908287548e4SAl Viro 		if (!mounted)
909287548e4SAl Viro 			break;
910c7105365SAl Viro 		nd->path.mnt = &mounted->mnt;
911c7105365SAl Viro 		nd->path.dentry = mounted->mnt.mnt_root;
912dea39376SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
913287548e4SAl Viro 	}
914287548e4SAl Viro }
915287548e4SAl Viro 
91631e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
91731e6b01fSNick Piggin {
91831e6b01fSNick Piggin 	set_root_rcu(nd);
91931e6b01fSNick Piggin 
92031e6b01fSNick Piggin 	while (1) {
92131e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
92231e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
92331e6b01fSNick Piggin 			break;
92431e6b01fSNick Piggin 		}
92531e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
92631e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
92731e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
92831e6b01fSNick Piggin 			unsigned seq;
92931e6b01fSNick Piggin 
93031e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
93131e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
932ef7562d5SAl Viro 				goto failed;
93331e6b01fSNick Piggin 			nd->path.dentry = parent;
93431e6b01fSNick Piggin 			nd->seq = seq;
93531e6b01fSNick Piggin 			break;
93631e6b01fSNick Piggin 		}
93731e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
93831e6b01fSNick Piggin 			break;
93931e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
94031e6b01fSNick Piggin 	}
941dea39376SAl Viro 	follow_mount_rcu(nd);
942dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
94331e6b01fSNick Piggin 	return 0;
944ef7562d5SAl Viro 
945ef7562d5SAl Viro failed:
946ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
9475b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
948ef7562d5SAl Viro 		nd->root.mnt = NULL;
949ef7562d5SAl Viro 	rcu_read_unlock();
950962830dfSAndi Kleen 	br_read_unlock(&vfsmount_lock);
951ef7562d5SAl Viro 	return -ECHILD;
95231e6b01fSNick Piggin }
95331e6b01fSNick Piggin 
9549875cf80SDavid Howells /*
955cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
956cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
957cc53ce53SDavid Howells  * caller is permitted to proceed or not.
958cc53ce53SDavid Howells  */
9597cc90cc3SAl Viro int follow_down(struct path *path)
960cc53ce53SDavid Howells {
961cc53ce53SDavid Howells 	unsigned managed;
962cc53ce53SDavid Howells 	int ret;
963cc53ce53SDavid Howells 
964cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
965cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
966cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
967cc53ce53SDavid Howells 		 * being held.
968cc53ce53SDavid Howells 		 *
969cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
970cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
971cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
972cc53ce53SDavid Howells 		 * superstructure.
973cc53ce53SDavid Howells 		 *
974cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
975cc53ce53SDavid Howells 		 */
976cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
977cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
978cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
979ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
9801aed3e42SAl Viro 				path->dentry, false);
981cc53ce53SDavid Howells 			if (ret < 0)
982cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
983cc53ce53SDavid Howells 		}
984cc53ce53SDavid Howells 
985cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
986cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
987cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
988cc53ce53SDavid Howells 			if (!mounted)
989cc53ce53SDavid Howells 				break;
990cc53ce53SDavid Howells 			dput(path->dentry);
991cc53ce53SDavid Howells 			mntput(path->mnt);
992cc53ce53SDavid Howells 			path->mnt = mounted;
993cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
994cc53ce53SDavid Howells 			continue;
995cc53ce53SDavid Howells 		}
996cc53ce53SDavid Howells 
997cc53ce53SDavid Howells 		/* Don't handle automount points here */
998cc53ce53SDavid Howells 		break;
999cc53ce53SDavid Howells 	}
1000cc53ce53SDavid Howells 	return 0;
1001cc53ce53SDavid Howells }
1002cc53ce53SDavid Howells 
1003cc53ce53SDavid Howells /*
10049875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
10059875cf80SDavid Howells  */
10069875cf80SDavid Howells static void follow_mount(struct path *path)
10079875cf80SDavid Howells {
10089875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
10099875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
10109875cf80SDavid Howells 		if (!mounted)
10119875cf80SDavid Howells 			break;
10129875cf80SDavid Howells 		dput(path->dentry);
10139875cf80SDavid Howells 		mntput(path->mnt);
10149875cf80SDavid Howells 		path->mnt = mounted;
10159875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
10169875cf80SDavid Howells 	}
10179875cf80SDavid Howells }
10189875cf80SDavid Howells 
101931e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
10201da177e4SLinus Torvalds {
10212a737871SAl Viro 	set_root(nd);
1022e518ddb7SAndreas Mohr 
10231da177e4SLinus Torvalds 	while(1) {
10244ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
10251da177e4SLinus Torvalds 
10262a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
10272a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
10281da177e4SLinus Torvalds 			break;
10291da177e4SLinus Torvalds 		}
10304ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
10313088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
10323088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
10331da177e4SLinus Torvalds 			dput(old);
10341da177e4SLinus Torvalds 			break;
10351da177e4SLinus Torvalds 		}
10363088dd70SAl Viro 		if (!follow_up(&nd->path))
10371da177e4SLinus Torvalds 			break;
10381da177e4SLinus Torvalds 	}
103979ed0226SAl Viro 	follow_mount(&nd->path);
104031e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
10411da177e4SLinus Torvalds }
10421da177e4SLinus Torvalds 
10431da177e4SLinus Torvalds /*
1044bad61189SMiklos Szeredi  * This looks up the name in dcache, possibly revalidates the old dentry and
1045bad61189SMiklos Szeredi  * allocates a new one if not found or not valid.  In the need_lookup argument
1046bad61189SMiklos Szeredi  * returns whether i_op->lookup is necessary.
1047bad61189SMiklos Szeredi  *
1048bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
1049baa03890SNick Piggin  */
1050bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1051bad61189SMiklos Szeredi 				    struct nameidata *nd, bool *need_lookup)
1052baa03890SNick Piggin {
1053baa03890SNick Piggin 	struct dentry *dentry;
1054bad61189SMiklos Szeredi 	int error;
1055baa03890SNick Piggin 
1056bad61189SMiklos Szeredi 	*need_lookup = false;
1057bad61189SMiklos Szeredi 	dentry = d_lookup(dir, name);
1058bad61189SMiklos Szeredi 	if (dentry) {
1059bad61189SMiklos Szeredi 		if (d_need_lookup(dentry)) {
1060bad61189SMiklos Szeredi 			*need_lookup = true;
1061bad61189SMiklos Szeredi 		} else if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1062bad61189SMiklos Szeredi 			error = d_revalidate(dentry, nd);
1063bad61189SMiklos Szeredi 			if (unlikely(error <= 0)) {
1064bad61189SMiklos Szeredi 				if (error < 0) {
1065bad61189SMiklos Szeredi 					dput(dentry);
1066bad61189SMiklos Szeredi 					return ERR_PTR(error);
1067bad61189SMiklos Szeredi 				} else if (!d_invalidate(dentry)) {
1068bad61189SMiklos Szeredi 					dput(dentry);
1069bad61189SMiklos Szeredi 					dentry = NULL;
1070bad61189SMiklos Szeredi 				}
1071bad61189SMiklos Szeredi 			}
1072bad61189SMiklos Szeredi 		}
1073bad61189SMiklos Szeredi 	}
1074baa03890SNick Piggin 
1075bad61189SMiklos Szeredi 	if (!dentry) {
1076bad61189SMiklos Szeredi 		dentry = d_alloc(dir, name);
1077baa03890SNick Piggin 		if (unlikely(!dentry))
1078baa03890SNick Piggin 			return ERR_PTR(-ENOMEM);
1079baa03890SNick Piggin 
1080bad61189SMiklos Szeredi 		*need_lookup = true;
1081baa03890SNick Piggin 	}
1082baa03890SNick Piggin 	return dentry;
1083baa03890SNick Piggin }
1084baa03890SNick Piggin 
1085baa03890SNick Piggin /*
1086bad61189SMiklos Szeredi  * Call i_op->lookup on the dentry.  The dentry must be negative but may be
1087bad61189SMiklos Szeredi  * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1088bad61189SMiklos Szeredi  *
1089bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
109044396f4bSJosef Bacik  */
1091bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
109244396f4bSJosef Bacik 				  struct nameidata *nd)
109344396f4bSJosef Bacik {
109444396f4bSJosef Bacik 	struct dentry *old;
109544396f4bSJosef Bacik 
109644396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
1097bad61189SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
1098e188dc02SMiklos Szeredi 		dput(dentry);
109944396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
1100e188dc02SMiklos Szeredi 	}
110144396f4bSJosef Bacik 
1102bad61189SMiklos Szeredi 	old = dir->i_op->lookup(dir, dentry, nd);
110344396f4bSJosef Bacik 	if (unlikely(old)) {
110444396f4bSJosef Bacik 		dput(dentry);
110544396f4bSJosef Bacik 		dentry = old;
110644396f4bSJosef Bacik 	}
110744396f4bSJosef Bacik 	return dentry;
110844396f4bSJosef Bacik }
110944396f4bSJosef Bacik 
1110a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name,
1111a3255546SAl Viro 		struct dentry *base, struct nameidata *nd)
1112a3255546SAl Viro {
1113bad61189SMiklos Szeredi 	bool need_lookup;
1114a3255546SAl Viro 	struct dentry *dentry;
1115a3255546SAl Viro 
1116bad61189SMiklos Szeredi 	dentry = lookup_dcache(name, base, nd, &need_lookup);
1117bad61189SMiklos Szeredi 	if (!need_lookup)
1118a3255546SAl Viro 		return dentry;
1119bad61189SMiklos Szeredi 
1120bad61189SMiklos Szeredi 	return lookup_real(base->d_inode, dentry, nd);
1121a3255546SAl Viro }
1122a3255546SAl Viro 
112344396f4bSJosef Bacik /*
11241da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
11251da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
11261da177e4SLinus Torvalds  *  It _is_ time-critical.
11271da177e4SLinus Torvalds  */
1128697f514dSMiklos Szeredi static int lookup_fast(struct nameidata *nd, struct qstr *name,
112931e6b01fSNick Piggin 		       struct path *path, struct inode **inode)
11301da177e4SLinus Torvalds {
11314ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
113231e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
11335a18fff2SAl Viro 	int need_reval = 1;
11345a18fff2SAl Viro 	int status = 1;
11359875cf80SDavid Howells 	int err;
11369875cf80SDavid Howells 
11373cac260aSAl Viro 	/*
1138b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1139b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1140b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1141b04f784eSNick Piggin 	 */
114231e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
114331e6b01fSNick Piggin 		unsigned seq;
114412f8ad4bSLinus Torvalds 		dentry = __d_lookup_rcu(parent, name, &seq, nd->inode);
11455a18fff2SAl Viro 		if (!dentry)
11465a18fff2SAl Viro 			goto unlazy;
11475a18fff2SAl Viro 
114812f8ad4bSLinus Torvalds 		/*
114912f8ad4bSLinus Torvalds 		 * This sequence count validates that the inode matches
115012f8ad4bSLinus Torvalds 		 * the dentry name information from lookup.
115112f8ad4bSLinus Torvalds 		 */
115212f8ad4bSLinus Torvalds 		*inode = dentry->d_inode;
115312f8ad4bSLinus Torvalds 		if (read_seqcount_retry(&dentry->d_seq, seq))
115412f8ad4bSLinus Torvalds 			return -ECHILD;
115512f8ad4bSLinus Torvalds 
115612f8ad4bSLinus Torvalds 		/*
115712f8ad4bSLinus Torvalds 		 * This sequence count validates that the parent had no
115812f8ad4bSLinus Torvalds 		 * changes while we did the lookup of the dentry above.
115912f8ad4bSLinus Torvalds 		 *
116012f8ad4bSLinus Torvalds 		 * The memory barrier in read_seqcount_begin of child is
116112f8ad4bSLinus Torvalds 		 *  enough, we can use __read_seqcount_retry here.
116212f8ad4bSLinus Torvalds 		 */
116331e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
116431e6b01fSNick Piggin 			return -ECHILD;
116531e6b01fSNick Piggin 		nd->seq = seq;
11665a18fff2SAl Viro 
1167fa4ee159SMiklos Szeredi 		if (unlikely(d_need_lookup(dentry)))
1168fa4ee159SMiklos Szeredi 			goto unlazy;
116924643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
11705a18fff2SAl Viro 			status = d_revalidate(dentry, nd);
11715a18fff2SAl Viro 			if (unlikely(status <= 0)) {
11725a18fff2SAl Viro 				if (status != -ECHILD)
11735a18fff2SAl Viro 					need_reval = 0;
11745a18fff2SAl Viro 				goto unlazy;
11755a18fff2SAl Viro 			}
117624643087SAl Viro 		}
117731e6b01fSNick Piggin 		path->mnt = mnt;
117831e6b01fSNick Piggin 		path->dentry = dentry;
1179d6e9bd25SAl Viro 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1180d6e9bd25SAl Viro 			goto unlazy;
1181d6e9bd25SAl Viro 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1182d6e9bd25SAl Viro 			goto unlazy;
11839875cf80SDavid Howells 		return 0;
11845a18fff2SAl Viro unlazy:
118519660af7SAl Viro 		if (unlazy_walk(nd, dentry))
11865a18fff2SAl Viro 			return -ECHILD;
11875a18fff2SAl Viro 	} else {
118831e6b01fSNick Piggin 		dentry = __d_lookup(parent, name);
118924643087SAl Viro 	}
11905a18fff2SAl Viro 
119181e6f520SAl Viro 	if (unlikely(!dentry))
119281e6f520SAl Viro 		goto need_lookup;
11935a18fff2SAl Viro 
119481e6f520SAl Viro 	if (unlikely(d_need_lookup(dentry))) {
119581e6f520SAl Viro 		dput(dentry);
119681e6f520SAl Viro 		goto need_lookup;
11975a18fff2SAl Viro 	}
119881e6f520SAl Viro 
11995a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
12005a18fff2SAl Viro 		status = d_revalidate(dentry, nd);
12015a18fff2SAl Viro 	if (unlikely(status <= 0)) {
12025a18fff2SAl Viro 		if (status < 0) {
12035a18fff2SAl Viro 			dput(dentry);
12045a18fff2SAl Viro 			return status;
12055a18fff2SAl Viro 		}
12065a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
12075a18fff2SAl Viro 			dput(dentry);
120881e6f520SAl Viro 			goto need_lookup;
12095a18fff2SAl Viro 		}
12105a18fff2SAl Viro 	}
1211697f514dSMiklos Szeredi 
12121da177e4SLinus Torvalds 	path->mnt = mnt;
12131da177e4SLinus Torvalds 	path->dentry = dentry;
12149875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
121589312214SIan Kent 	if (unlikely(err < 0)) {
121689312214SIan Kent 		path_put_conditional(path, nd);
12179875cf80SDavid Howells 		return err;
121889312214SIan Kent 	}
1219a3fbbde7SAl Viro 	if (err)
1220a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
122131e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
12221da177e4SLinus Torvalds 	return 0;
122381e6f520SAl Viro 
122481e6f520SAl Viro need_lookup:
1225697f514dSMiklos Szeredi 	return 1;
1226697f514dSMiklos Szeredi }
1227697f514dSMiklos Szeredi 
1228697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */
1229697f514dSMiklos Szeredi static int lookup_slow(struct nameidata *nd, struct qstr *name,
1230697f514dSMiklos Szeredi 		       struct path *path)
1231697f514dSMiklos Szeredi {
1232697f514dSMiklos Szeredi 	struct dentry *dentry, *parent;
1233697f514dSMiklos Szeredi 	int err;
1234697f514dSMiklos Szeredi 
1235697f514dSMiklos Szeredi 	parent = nd->path.dentry;
123681e6f520SAl Viro 	BUG_ON(nd->inode != parent->d_inode);
123781e6f520SAl Viro 
123881e6f520SAl Viro 	mutex_lock(&parent->d_inode->i_mutex);
123981e6f520SAl Viro 	dentry = __lookup_hash(name, parent, nd);
124081e6f520SAl Viro 	mutex_unlock(&parent->d_inode->i_mutex);
124181e6f520SAl Viro 	if (IS_ERR(dentry))
124281e6f520SAl Viro 		return PTR_ERR(dentry);
1243697f514dSMiklos Szeredi 	path->mnt = nd->path.mnt;
1244697f514dSMiklos Szeredi 	path->dentry = dentry;
1245697f514dSMiklos Szeredi 	err = follow_managed(path, nd->flags);
1246697f514dSMiklos Szeredi 	if (unlikely(err < 0)) {
1247697f514dSMiklos Szeredi 		path_put_conditional(path, nd);
1248697f514dSMiklos Szeredi 		return err;
1249697f514dSMiklos Szeredi 	}
1250697f514dSMiklos Szeredi 	if (err)
1251697f514dSMiklos Szeredi 		nd->flags |= LOOKUP_JUMPED;
1252697f514dSMiklos Szeredi 	return 0;
12531da177e4SLinus Torvalds }
12541da177e4SLinus Torvalds 
125552094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
125652094c8aSAl Viro {
125752094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
12584ad5abb3SAl Viro 		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
125952094c8aSAl Viro 		if (err != -ECHILD)
126052094c8aSAl Viro 			return err;
126119660af7SAl Viro 		if (unlazy_walk(nd, NULL))
126252094c8aSAl Viro 			return -ECHILD;
126352094c8aSAl Viro 	}
12644ad5abb3SAl Viro 	return inode_permission(nd->inode, MAY_EXEC);
126552094c8aSAl Viro }
126652094c8aSAl Viro 
12679856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
12689856fa1bSAl Viro {
12699856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
12709856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
12719856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
12729856fa1bSAl Viro 				return -ECHILD;
12739856fa1bSAl Viro 		} else
12749856fa1bSAl Viro 			follow_dotdot(nd);
12759856fa1bSAl Viro 	}
12769856fa1bSAl Viro 	return 0;
12779856fa1bSAl Viro }
12789856fa1bSAl Viro 
1279951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1280951361f9SAl Viro {
1281951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1282951361f9SAl Viro 		path_put(&nd->path);
1283951361f9SAl Viro 	} else {
1284951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
12855b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1286951361f9SAl Viro 			nd->root.mnt = NULL;
1287951361f9SAl Viro 		rcu_read_unlock();
1288962830dfSAndi Kleen 		br_read_unlock(&vfsmount_lock);
1289951361f9SAl Viro 	}
1290951361f9SAl Viro }
1291951361f9SAl Viro 
12923ddcd056SLinus Torvalds /*
12933ddcd056SLinus Torvalds  * Do we need to follow links? We _really_ want to be able
12943ddcd056SLinus Torvalds  * to do this check without having to look at inode->i_op,
12953ddcd056SLinus Torvalds  * so we keep a cache of "no, this doesn't need follow_link"
12963ddcd056SLinus Torvalds  * for the common case.
12973ddcd056SLinus Torvalds  */
12987813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow)
12993ddcd056SLinus Torvalds {
13003ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
13013ddcd056SLinus Torvalds 		if (likely(inode->i_op->follow_link))
13023ddcd056SLinus Torvalds 			return follow;
13033ddcd056SLinus Torvalds 
13043ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
13053ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
13063ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_NOFOLLOW;
13073ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
13083ddcd056SLinus Torvalds 	}
13093ddcd056SLinus Torvalds 	return 0;
13103ddcd056SLinus Torvalds }
13113ddcd056SLinus Torvalds 
1312ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
1313ce57dfc1SAl Viro 		struct qstr *name, int type, int follow)
1314ce57dfc1SAl Viro {
1315ce57dfc1SAl Viro 	struct inode *inode;
1316ce57dfc1SAl Viro 	int err;
1317ce57dfc1SAl Viro 	/*
1318ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1319ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1320ce57dfc1SAl Viro 	 * parent relationships.
1321ce57dfc1SAl Viro 	 */
1322ce57dfc1SAl Viro 	if (unlikely(type != LAST_NORM))
1323ce57dfc1SAl Viro 		return handle_dots(nd, type);
1324697f514dSMiklos Szeredi 	err = lookup_fast(nd, name, path, &inode);
1325ce57dfc1SAl Viro 	if (unlikely(err)) {
1326697f514dSMiklos Szeredi 		if (err < 0)
1327697f514dSMiklos Szeredi 			goto out_err;
1328697f514dSMiklos Szeredi 
1329697f514dSMiklos Szeredi 		err = lookup_slow(nd, name, path);
1330697f514dSMiklos Szeredi 		if (err < 0)
1331697f514dSMiklos Szeredi 			goto out_err;
1332697f514dSMiklos Szeredi 
1333697f514dSMiklos Szeredi 		inode = path->dentry->d_inode;
1334ce57dfc1SAl Viro 	}
1335697f514dSMiklos Szeredi 	err = -ENOENT;
1336697f514dSMiklos Szeredi 	if (!inode)
1337697f514dSMiklos Szeredi 		goto out_path_put;
1338697f514dSMiklos Szeredi 
13397813b94aSLinus Torvalds 	if (should_follow_link(inode, follow)) {
134019660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
134119660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
1342697f514dSMiklos Szeredi 				err = -ECHILD;
1343697f514dSMiklos Szeredi 				goto out_err;
134419660af7SAl Viro 			}
134519660af7SAl Viro 		}
1346ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1347ce57dfc1SAl Viro 		return 1;
1348ce57dfc1SAl Viro 	}
1349ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1350ce57dfc1SAl Viro 	nd->inode = inode;
1351ce57dfc1SAl Viro 	return 0;
1352697f514dSMiklos Szeredi 
1353697f514dSMiklos Szeredi out_path_put:
1354697f514dSMiklos Szeredi 	path_to_nameidata(path, nd);
1355697f514dSMiklos Szeredi out_err:
1356697f514dSMiklos Szeredi 	terminate_walk(nd);
1357697f514dSMiklos Szeredi 	return err;
1358ce57dfc1SAl Viro }
1359ce57dfc1SAl Viro 
13601da177e4SLinus Torvalds /*
1361b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1362b356379aSAl Viro  * limiting consecutive symlinks to 40.
1363b356379aSAl Viro  *
1364b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1365b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1366b356379aSAl Viro  */
1367b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1368b356379aSAl Viro {
1369b356379aSAl Viro 	int res;
1370b356379aSAl Viro 
1371b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1372b356379aSAl Viro 		path_put_conditional(path, nd);
1373b356379aSAl Viro 		path_put(&nd->path);
1374b356379aSAl Viro 		return -ELOOP;
1375b356379aSAl Viro 	}
13761a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1377b356379aSAl Viro 
1378b356379aSAl Viro 	nd->depth++;
1379b356379aSAl Viro 	current->link_count++;
1380b356379aSAl Viro 
1381b356379aSAl Viro 	do {
1382b356379aSAl Viro 		struct path link = *path;
1383b356379aSAl Viro 		void *cookie;
1384574197e0SAl Viro 
1385574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
1386b356379aSAl Viro 		if (!res)
1387b356379aSAl Viro 			res = walk_component(nd, path, &nd->last,
1388b356379aSAl Viro 					     nd->last_type, LOOKUP_FOLLOW);
1389574197e0SAl Viro 		put_link(nd, &link, cookie);
1390b356379aSAl Viro 	} while (res > 0);
1391b356379aSAl Viro 
1392b356379aSAl Viro 	current->link_count--;
1393b356379aSAl Viro 	nd->depth--;
1394b356379aSAl Viro 	return res;
1395b356379aSAl Viro }
1396b356379aSAl Viro 
1397b356379aSAl Viro /*
13983ddcd056SLinus Torvalds  * We really don't want to look at inode->i_op->lookup
13993ddcd056SLinus Torvalds  * when we don't have to. So we keep a cache bit in
14003ddcd056SLinus Torvalds  * the inode ->i_opflags field that says "yes, we can
14013ddcd056SLinus Torvalds  * do lookup on this inode".
14023ddcd056SLinus Torvalds  */
14033ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode)
14043ddcd056SLinus Torvalds {
14053ddcd056SLinus Torvalds 	if (likely(inode->i_opflags & IOP_LOOKUP))
14063ddcd056SLinus Torvalds 		return 1;
14073ddcd056SLinus Torvalds 	if (likely(!inode->i_op->lookup))
14083ddcd056SLinus Torvalds 		return 0;
14093ddcd056SLinus Torvalds 
14103ddcd056SLinus Torvalds 	/* We do this once for the lifetime of the inode */
14113ddcd056SLinus Torvalds 	spin_lock(&inode->i_lock);
14123ddcd056SLinus Torvalds 	inode->i_opflags |= IOP_LOOKUP;
14133ddcd056SLinus Torvalds 	spin_unlock(&inode->i_lock);
14143ddcd056SLinus Torvalds 	return 1;
14153ddcd056SLinus Torvalds }
14163ddcd056SLinus Torvalds 
1417bfcfaa77SLinus Torvalds /*
1418bfcfaa77SLinus Torvalds  * We can do the critical dentry name comparison and hashing
1419bfcfaa77SLinus Torvalds  * operations one word at a time, but we are limited to:
1420bfcfaa77SLinus Torvalds  *
1421bfcfaa77SLinus Torvalds  * - Architectures with fast unaligned word accesses. We could
1422bfcfaa77SLinus Torvalds  *   do a "get_unaligned()" if this helps and is sufficiently
1423bfcfaa77SLinus Torvalds  *   fast.
1424bfcfaa77SLinus Torvalds  *
1425bfcfaa77SLinus Torvalds  * - Little-endian machines (so that we can generate the mask
1426bfcfaa77SLinus Torvalds  *   of low bytes efficiently). Again, we *could* do a byte
1427bfcfaa77SLinus Torvalds  *   swapping load on big-endian architectures if that is not
1428bfcfaa77SLinus Torvalds  *   expensive enough to make the optimization worthless.
1429bfcfaa77SLinus Torvalds  *
1430bfcfaa77SLinus Torvalds  * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1431bfcfaa77SLinus Torvalds  *   do not trap on the (extremely unlikely) case of a page
1432bfcfaa77SLinus Torvalds  *   crossing operation.
1433bfcfaa77SLinus Torvalds  *
1434bfcfaa77SLinus Torvalds  * - Furthermore, we need an efficient 64-bit compile for the
1435bfcfaa77SLinus Torvalds  *   64-bit case in order to generate the "number of bytes in
1436bfcfaa77SLinus Torvalds  *   the final mask". Again, that could be replaced with a
1437bfcfaa77SLinus Torvalds  *   efficient population count instruction or similar.
1438bfcfaa77SLinus Torvalds  */
1439bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS
1440bfcfaa77SLinus Torvalds 
1441f68e556eSLinus Torvalds #include <asm/word-at-a-time.h>
1442bfcfaa77SLinus Torvalds 
1443f68e556eSLinus Torvalds #ifdef CONFIG_64BIT
1444bfcfaa77SLinus Torvalds 
1445bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash)
1446bfcfaa77SLinus Torvalds {
1447bfcfaa77SLinus Torvalds 	hash += hash >> (8*sizeof(int));
1448bfcfaa77SLinus Torvalds 	return hash;
1449bfcfaa77SLinus Torvalds }
1450bfcfaa77SLinus Torvalds 
1451bfcfaa77SLinus Torvalds #else	/* 32-bit case */
1452bfcfaa77SLinus Torvalds 
1453bfcfaa77SLinus Torvalds #define fold_hash(x) (x)
1454bfcfaa77SLinus Torvalds 
1455bfcfaa77SLinus Torvalds #endif
1456bfcfaa77SLinus Torvalds 
1457bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1458bfcfaa77SLinus Torvalds {
1459bfcfaa77SLinus Torvalds 	unsigned long a, mask;
1460bfcfaa77SLinus Torvalds 	unsigned long hash = 0;
1461bfcfaa77SLinus Torvalds 
1462bfcfaa77SLinus Torvalds 	for (;;) {
1463e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name);
1464bfcfaa77SLinus Torvalds 		if (len < sizeof(unsigned long))
1465bfcfaa77SLinus Torvalds 			break;
1466bfcfaa77SLinus Torvalds 		hash += a;
1467f132c5beSAl Viro 		hash *= 9;
1468bfcfaa77SLinus Torvalds 		name += sizeof(unsigned long);
1469bfcfaa77SLinus Torvalds 		len -= sizeof(unsigned long);
1470bfcfaa77SLinus Torvalds 		if (!len)
1471bfcfaa77SLinus Torvalds 			goto done;
1472bfcfaa77SLinus Torvalds 	}
1473bfcfaa77SLinus Torvalds 	mask = ~(~0ul << len*8);
1474bfcfaa77SLinus Torvalds 	hash += mask & a;
1475bfcfaa77SLinus Torvalds done:
1476bfcfaa77SLinus Torvalds 	return fold_hash(hash);
1477bfcfaa77SLinus Torvalds }
1478bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
1479bfcfaa77SLinus Torvalds 
1480bfcfaa77SLinus Torvalds /*
1481bfcfaa77SLinus Torvalds  * Calculate the length and hash of the path component, and
1482bfcfaa77SLinus Torvalds  * return the length of the component;
1483bfcfaa77SLinus Torvalds  */
1484bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1485bfcfaa77SLinus Torvalds {
148636126f8fSLinus Torvalds 	unsigned long a, b, adata, bdata, mask, hash, len;
148736126f8fSLinus Torvalds 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1488bfcfaa77SLinus Torvalds 
1489bfcfaa77SLinus Torvalds 	hash = a = 0;
1490bfcfaa77SLinus Torvalds 	len = -sizeof(unsigned long);
1491bfcfaa77SLinus Torvalds 	do {
1492bfcfaa77SLinus Torvalds 		hash = (hash + a) * 9;
1493bfcfaa77SLinus Torvalds 		len += sizeof(unsigned long);
1494e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name+len);
149536126f8fSLinus Torvalds 		b = a ^ REPEAT_BYTE('/');
149636126f8fSLinus Torvalds 	} while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1497bfcfaa77SLinus Torvalds 
149836126f8fSLinus Torvalds 	adata = prep_zero_mask(a, adata, &constants);
149936126f8fSLinus Torvalds 	bdata = prep_zero_mask(b, bdata, &constants);
150036126f8fSLinus Torvalds 
150136126f8fSLinus Torvalds 	mask = create_zero_mask(adata | bdata);
150236126f8fSLinus Torvalds 
150336126f8fSLinus Torvalds 	hash += a & zero_bytemask(mask);
1504bfcfaa77SLinus Torvalds 	*hashp = fold_hash(hash);
1505bfcfaa77SLinus Torvalds 
150636126f8fSLinus Torvalds 	return len + find_zero(mask);
1507bfcfaa77SLinus Torvalds }
1508bfcfaa77SLinus Torvalds 
1509bfcfaa77SLinus Torvalds #else
1510bfcfaa77SLinus Torvalds 
15110145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
15120145acc2SLinus Torvalds {
15130145acc2SLinus Torvalds 	unsigned long hash = init_name_hash();
15140145acc2SLinus Torvalds 	while (len--)
15150145acc2SLinus Torvalds 		hash = partial_name_hash(*name++, hash);
15160145acc2SLinus Torvalds 	return end_name_hash(hash);
15170145acc2SLinus Torvalds }
1518ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
15190145acc2SLinus Torvalds 
15203ddcd056SLinus Torvalds /*
1521200e9ef7SLinus Torvalds  * We know there's a real path component here of at least
1522200e9ef7SLinus Torvalds  * one character.
1523200e9ef7SLinus Torvalds  */
1524200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1525200e9ef7SLinus Torvalds {
1526200e9ef7SLinus Torvalds 	unsigned long hash = init_name_hash();
1527200e9ef7SLinus Torvalds 	unsigned long len = 0, c;
1528200e9ef7SLinus Torvalds 
1529200e9ef7SLinus Torvalds 	c = (unsigned char)*name;
1530200e9ef7SLinus Torvalds 	do {
1531200e9ef7SLinus Torvalds 		len++;
1532200e9ef7SLinus Torvalds 		hash = partial_name_hash(c, hash);
1533200e9ef7SLinus Torvalds 		c = (unsigned char)name[len];
1534200e9ef7SLinus Torvalds 	} while (c && c != '/');
1535200e9ef7SLinus Torvalds 	*hashp = end_name_hash(hash);
1536200e9ef7SLinus Torvalds 	return len;
1537200e9ef7SLinus Torvalds }
1538200e9ef7SLinus Torvalds 
1539bfcfaa77SLinus Torvalds #endif
1540bfcfaa77SLinus Torvalds 
1541200e9ef7SLinus Torvalds /*
15421da177e4SLinus Torvalds  * Name resolution.
1543ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1544ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
15451da177e4SLinus Torvalds  *
1546ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1547ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
15481da177e4SLinus Torvalds  */
15496de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
15501da177e4SLinus Torvalds {
15511da177e4SLinus Torvalds 	struct path next;
15521da177e4SLinus Torvalds 	int err;
15531da177e4SLinus Torvalds 
15541da177e4SLinus Torvalds 	while (*name=='/')
15551da177e4SLinus Torvalds 		name++;
15561da177e4SLinus Torvalds 	if (!*name)
1557086e183aSAl Viro 		return 0;
15581da177e4SLinus Torvalds 
15591da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
15601da177e4SLinus Torvalds 	for(;;) {
15611da177e4SLinus Torvalds 		struct qstr this;
1562200e9ef7SLinus Torvalds 		long len;
1563fe479a58SAl Viro 		int type;
15641da177e4SLinus Torvalds 
156552094c8aSAl Viro 		err = may_lookup(nd);
15661da177e4SLinus Torvalds  		if (err)
15671da177e4SLinus Torvalds 			break;
15681da177e4SLinus Torvalds 
1569200e9ef7SLinus Torvalds 		len = hash_name(name, &this.hash);
15701da177e4SLinus Torvalds 		this.name = name;
1571200e9ef7SLinus Torvalds 		this.len = len;
15721da177e4SLinus Torvalds 
1573fe479a58SAl Viro 		type = LAST_NORM;
1574200e9ef7SLinus Torvalds 		if (name[0] == '.') switch (len) {
1575fe479a58SAl Viro 			case 2:
1576200e9ef7SLinus Torvalds 				if (name[1] == '.') {
1577fe479a58SAl Viro 					type = LAST_DOTDOT;
157816c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
157916c2cd71SAl Viro 				}
1580fe479a58SAl Viro 				break;
1581fe479a58SAl Viro 			case 1:
1582fe479a58SAl Viro 				type = LAST_DOT;
1583fe479a58SAl Viro 		}
15845a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
15855a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
158616c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
15875a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
15885a202bcdSAl Viro 				err = parent->d_op->d_hash(parent, nd->inode,
15895a202bcdSAl Viro 							   &this);
15905a202bcdSAl Viro 				if (err < 0)
15915a202bcdSAl Viro 					break;
15925a202bcdSAl Viro 			}
15935a202bcdSAl Viro 		}
1594fe479a58SAl Viro 
1595200e9ef7SLinus Torvalds 		if (!name[len])
15961da177e4SLinus Torvalds 			goto last_component;
1597200e9ef7SLinus Torvalds 		/*
1598200e9ef7SLinus Torvalds 		 * If it wasn't NUL, we know it was '/'. Skip that
1599200e9ef7SLinus Torvalds 		 * slash, and continue until no more slashes.
1600200e9ef7SLinus Torvalds 		 */
1601200e9ef7SLinus Torvalds 		do {
1602200e9ef7SLinus Torvalds 			len++;
1603200e9ef7SLinus Torvalds 		} while (unlikely(name[len] == '/'));
1604200e9ef7SLinus Torvalds 		if (!name[len])
1605b356379aSAl Viro 			goto last_component;
1606200e9ef7SLinus Torvalds 		name += len;
16071da177e4SLinus Torvalds 
1608ce57dfc1SAl Viro 		err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1609ce57dfc1SAl Viro 		if (err < 0)
1610ce57dfc1SAl Viro 			return err;
1611fe479a58SAl Viro 
1612ce57dfc1SAl Viro 		if (err) {
1613b356379aSAl Viro 			err = nested_symlink(&next, nd);
16141da177e4SLinus Torvalds 			if (err)
1615a7472babSAl Viro 				return err;
161631e6b01fSNick Piggin 		}
16173ddcd056SLinus Torvalds 		if (can_lookup(nd->inode))
16181da177e4SLinus Torvalds 			continue;
16193ddcd056SLinus Torvalds 		err = -ENOTDIR;
16203ddcd056SLinus Torvalds 		break;
16211da177e4SLinus Torvalds 		/* here ends the main loop */
16221da177e4SLinus Torvalds 
16231da177e4SLinus Torvalds last_component:
1624ce57dfc1SAl Viro 		nd->last = this;
1625ce57dfc1SAl Viro 		nd->last_type = type;
1626ce57dfc1SAl Viro 		return 0;
1627ce57dfc1SAl Viro 	}
1628951361f9SAl Viro 	terminate_walk(nd);
16291da177e4SLinus Torvalds 	return err;
16301da177e4SLinus Torvalds }
16311da177e4SLinus Torvalds 
163270e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
163370e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
163431e6b01fSNick Piggin {
163531e6b01fSNick Piggin 	int retval = 0;
163631e6b01fSNick Piggin 	int fput_needed;
163731e6b01fSNick Piggin 	struct file *file;
163831e6b01fSNick Piggin 
163931e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
164016c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
164131e6b01fSNick Piggin 	nd->depth = 0;
16425b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
16435b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
164473d049a4SAl Viro 		if (*name) {
16455b6ca027SAl Viro 			if (!inode->i_op->lookup)
16465b6ca027SAl Viro 				return -ENOTDIR;
16475b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
16485b6ca027SAl Viro 			if (retval)
16495b6ca027SAl Viro 				return retval;
165073d049a4SAl Viro 		}
16515b6ca027SAl Viro 		nd->path = nd->root;
16525b6ca027SAl Viro 		nd->inode = inode;
16535b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
1654962830dfSAndi Kleen 			br_read_lock(&vfsmount_lock);
16555b6ca027SAl Viro 			rcu_read_lock();
16565b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
16575b6ca027SAl Viro 		} else {
16585b6ca027SAl Viro 			path_get(&nd->path);
16595b6ca027SAl Viro 		}
16605b6ca027SAl Viro 		return 0;
16615b6ca027SAl Viro 	}
16625b6ca027SAl Viro 
166331e6b01fSNick Piggin 	nd->root.mnt = NULL;
166431e6b01fSNick Piggin 
166531e6b01fSNick Piggin 	if (*name=='/') {
1666e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
1667962830dfSAndi Kleen 			br_read_lock(&vfsmount_lock);
166831e6b01fSNick Piggin 			rcu_read_lock();
1669e41f7d4eSAl Viro 			set_root_rcu(nd);
1670e41f7d4eSAl Viro 		} else {
1671e41f7d4eSAl Viro 			set_root(nd);
1672e41f7d4eSAl Viro 			path_get(&nd->root);
1673e41f7d4eSAl Viro 		}
167431e6b01fSNick Piggin 		nd->path = nd->root;
167531e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1676e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
167731e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1678c28cc364SNick Piggin 			unsigned seq;
167931e6b01fSNick Piggin 
1680962830dfSAndi Kleen 			br_read_lock(&vfsmount_lock);
168131e6b01fSNick Piggin 			rcu_read_lock();
168231e6b01fSNick Piggin 
1683c28cc364SNick Piggin 			do {
1684c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
168531e6b01fSNick Piggin 				nd->path = fs->pwd;
1686c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1687c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1688e41f7d4eSAl Viro 		} else {
1689e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1690e41f7d4eSAl Viro 		}
169131e6b01fSNick Piggin 	} else {
169231e6b01fSNick Piggin 		struct dentry *dentry;
169331e6b01fSNick Piggin 
16941abf0c71SAl Viro 		file = fget_raw_light(dfd, &fput_needed);
169531e6b01fSNick Piggin 		retval = -EBADF;
169631e6b01fSNick Piggin 		if (!file)
169731e6b01fSNick Piggin 			goto out_fail;
169831e6b01fSNick Piggin 
169931e6b01fSNick Piggin 		dentry = file->f_path.dentry;
170031e6b01fSNick Piggin 
1701f52e0c11SAl Viro 		if (*name) {
170231e6b01fSNick Piggin 			retval = -ENOTDIR;
170331e6b01fSNick Piggin 			if (!S_ISDIR(dentry->d_inode->i_mode))
170431e6b01fSNick Piggin 				goto fput_fail;
170531e6b01fSNick Piggin 
17064ad5abb3SAl Viro 			retval = inode_permission(dentry->d_inode, MAY_EXEC);
170731e6b01fSNick Piggin 			if (retval)
170831e6b01fSNick Piggin 				goto fput_fail;
1709f52e0c11SAl Viro 		}
171031e6b01fSNick Piggin 
171131e6b01fSNick Piggin 		nd->path = file->f_path;
1712e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
171331e6b01fSNick Piggin 			if (fput_needed)
171470e9b357SAl Viro 				*fp = file;
1715c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1716962830dfSAndi Kleen 			br_read_lock(&vfsmount_lock);
171731e6b01fSNick Piggin 			rcu_read_lock();
17185590ff0dSUlrich Drepper 		} else {
17195dd784d0SJan Blunck 			path_get(&file->f_path);
17205590ff0dSUlrich Drepper 			fput_light(file, fput_needed);
17211da177e4SLinus Torvalds 		}
1722e41f7d4eSAl Viro 	}
1723e41f7d4eSAl Viro 
172431e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
17259b4a9b14SAl Viro 	return 0;
17262dfdd266SJosef 'Jeff' Sipek 
17279b4a9b14SAl Viro fput_fail:
17289b4a9b14SAl Viro 	fput_light(file, fput_needed);
17299b4a9b14SAl Viro out_fail:
17309b4a9b14SAl Viro 	return retval;
17319b4a9b14SAl Viro }
17329b4a9b14SAl Viro 
1733bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1734bd92d7feSAl Viro {
1735bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1736bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1737bd92d7feSAl Viro 
1738bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
1739bd92d7feSAl Viro 	return walk_component(nd, path, &nd->last, nd->last_type,
1740bd92d7feSAl Viro 					nd->flags & LOOKUP_FOLLOW);
1741bd92d7feSAl Viro }
1742bd92d7feSAl Viro 
17439b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1744ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
17459b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
17469b4a9b14SAl Viro {
174770e9b357SAl Viro 	struct file *base = NULL;
1748bd92d7feSAl Viro 	struct path path;
1749bd92d7feSAl Viro 	int err;
175031e6b01fSNick Piggin 
175131e6b01fSNick Piggin 	/*
175231e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
175331e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
175431e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
175531e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
175631e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
175731e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
175831e6b01fSNick Piggin 	 * analogue, foo_rcu().
175931e6b01fSNick Piggin 	 *
176031e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
176131e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
176231e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
176331e6b01fSNick Piggin 	 * be able to complete).
176431e6b01fSNick Piggin 	 */
1765bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1766ee0827cdSAl Viro 
1767bd92d7feSAl Viro 	if (unlikely(err))
1768bd92d7feSAl Viro 		return err;
1769ee0827cdSAl Viro 
1770ee0827cdSAl Viro 	current->total_link_count = 0;
1771bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1772bd92d7feSAl Viro 
1773bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1774bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1775bd92d7feSAl Viro 		while (err > 0) {
1776bd92d7feSAl Viro 			void *cookie;
1777bd92d7feSAl Viro 			struct path link = path;
1778bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1779574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
1780bd92d7feSAl Viro 			if (!err)
1781bd92d7feSAl Viro 				err = lookup_last(nd, &path);
1782574197e0SAl Viro 			put_link(nd, &link, cookie);
1783bd92d7feSAl Viro 		}
1784bd92d7feSAl Viro 	}
1785ee0827cdSAl Viro 
17869f1fafeeSAl Viro 	if (!err)
17879f1fafeeSAl Viro 		err = complete_walk(nd);
1788bd92d7feSAl Viro 
1789bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
1790bd92d7feSAl Viro 		if (!nd->inode->i_op->lookup) {
1791bd92d7feSAl Viro 			path_put(&nd->path);
1792bd23a539SAl Viro 			err = -ENOTDIR;
1793bd92d7feSAl Viro 		}
1794bd92d7feSAl Viro 	}
179516c2cd71SAl Viro 
179670e9b357SAl Viro 	if (base)
179770e9b357SAl Viro 		fput(base);
1798ee0827cdSAl Viro 
17995b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
180031e6b01fSNick Piggin 		path_put(&nd->root);
180131e6b01fSNick Piggin 		nd->root.mnt = NULL;
180231e6b01fSNick Piggin 	}
1803bd92d7feSAl Viro 	return err;
180431e6b01fSNick Piggin }
180531e6b01fSNick Piggin 
1806ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1807ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1808ee0827cdSAl Viro {
1809ee0827cdSAl Viro 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1810ee0827cdSAl Viro 	if (unlikely(retval == -ECHILD))
1811ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags, nd);
1812ee0827cdSAl Viro 	if (unlikely(retval == -ESTALE))
1813ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1814ee0827cdSAl Viro 
181531e6b01fSNick Piggin 	if (likely(!retval)) {
181631e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
181731e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
181831e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
181931e6b01fSNick Piggin 		}
182031e6b01fSNick Piggin 	}
1821170aa3d0SUlrich Drepper 	return retval;
18221da177e4SLinus Torvalds }
18231da177e4SLinus Torvalds 
1824c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd)
18255590ff0dSUlrich Drepper {
1826c9c6cac0SAl Viro 	return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
18275590ff0dSUlrich Drepper }
18285590ff0dSUlrich Drepper 
1829d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1830d1811465SAl Viro {
1831d1811465SAl Viro 	struct nameidata nd;
1832d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1833d1811465SAl Viro 	if (!res)
1834d1811465SAl Viro 		*path = nd.path;
1835d1811465SAl Viro 	return res;
1836d1811465SAl Viro }
1837d1811465SAl Viro 
183816f18200SJosef 'Jeff' Sipek /**
183916f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
184016f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
184116f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
184216f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
184316f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
1844e0a01249SAl Viro  * @path: pointer to struct path to fill
184516f18200SJosef 'Jeff' Sipek  */
184616f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
184716f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
1848e0a01249SAl Viro 		    struct path *path)
184916f18200SJosef 'Jeff' Sipek {
1850e0a01249SAl Viro 	struct nameidata nd;
1851e0a01249SAl Viro 	int err;
1852e0a01249SAl Viro 	nd.root.dentry = dentry;
1853e0a01249SAl Viro 	nd.root.mnt = mnt;
1854e0a01249SAl Viro 	BUG_ON(flags & LOOKUP_PARENT);
18555b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1856e0a01249SAl Viro 	err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1857e0a01249SAl Viro 	if (!err)
1858e0a01249SAl Viro 		*path = nd.path;
1859e0a01249SAl Viro 	return err;
186016f18200SJosef 'Jeff' Sipek }
186116f18200SJosef 'Jeff' Sipek 
1862057f6c01SJames Morris /*
1863057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1864057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1865057f6c01SJames Morris  * SMP-safe.
1866057f6c01SJames Morris  */
1867a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
18681da177e4SLinus Torvalds {
18694ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
18701da177e4SLinus Torvalds }
18711da177e4SLinus Torvalds 
1872eead1911SChristoph Hellwig /**
1873a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1874eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1875eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1876eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1877eead1911SChristoph Hellwig  *
1878a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1879a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1880eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1881eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1882eead1911SChristoph Hellwig  */
1883057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1884057f6c01SJames Morris {
1885057f6c01SJames Morris 	struct qstr this;
18866a96ba54SAl Viro 	unsigned int c;
1887cda309deSMiklos Szeredi 	int err;
1888057f6c01SJames Morris 
18892f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
18902f9092e1SDavid Woodhouse 
18916a96ba54SAl Viro 	this.name = name;
18926a96ba54SAl Viro 	this.len = len;
18930145acc2SLinus Torvalds 	this.hash = full_name_hash(name, len);
18946a96ba54SAl Viro 	if (!len)
18956a96ba54SAl Viro 		return ERR_PTR(-EACCES);
18966a96ba54SAl Viro 
18976a96ba54SAl Viro 	while (len--) {
18986a96ba54SAl Viro 		c = *(const unsigned char *)name++;
18996a96ba54SAl Viro 		if (c == '/' || c == '\0')
19006a96ba54SAl Viro 			return ERR_PTR(-EACCES);
19016a96ba54SAl Viro 	}
19025a202bcdSAl Viro 	/*
19035a202bcdSAl Viro 	 * See if the low-level filesystem might want
19045a202bcdSAl Viro 	 * to use its own hash..
19055a202bcdSAl Viro 	 */
19065a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
19075a202bcdSAl Viro 		int err = base->d_op->d_hash(base, base->d_inode, &this);
19085a202bcdSAl Viro 		if (err < 0)
19095a202bcdSAl Viro 			return ERR_PTR(err);
19105a202bcdSAl Viro 	}
1911eead1911SChristoph Hellwig 
1912cda309deSMiklos Szeredi 	err = inode_permission(base->d_inode, MAY_EXEC);
1913cda309deSMiklos Szeredi 	if (err)
1914cda309deSMiklos Szeredi 		return ERR_PTR(err);
1915cda309deSMiklos Szeredi 
191649705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1917057f6c01SJames Morris }
1918057f6c01SJames Morris 
19191fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
19201fa1e7f6SAndy Whitcroft 		 struct path *path, int *empty)
19211da177e4SLinus Torvalds {
19222d8f3038SAl Viro 	struct nameidata nd;
19231fa1e7f6SAndy Whitcroft 	char *tmp = getname_flags(name, flags, empty);
19241da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
19251da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
19262d8f3038SAl Viro 
19272d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
19282d8f3038SAl Viro 
19292d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
19301da177e4SLinus Torvalds 		putname(tmp);
19312d8f3038SAl Viro 		if (!err)
19322d8f3038SAl Viro 			*path = nd.path;
19331da177e4SLinus Torvalds 	}
19341da177e4SLinus Torvalds 	return err;
19351da177e4SLinus Torvalds }
19361da177e4SLinus Torvalds 
19371fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags,
19381fa1e7f6SAndy Whitcroft 		 struct path *path)
19391fa1e7f6SAndy Whitcroft {
1940f7493e5dSLinus Torvalds 	return user_path_at_empty(dfd, name, flags, path, NULL);
19411fa1e7f6SAndy Whitcroft }
19421fa1e7f6SAndy Whitcroft 
19432ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
19442ad94ae6SAl Viro 			struct nameidata *nd, char **name)
19452ad94ae6SAl Viro {
19462ad94ae6SAl Viro 	char *s = getname(path);
19472ad94ae6SAl Viro 	int error;
19482ad94ae6SAl Viro 
19492ad94ae6SAl Viro 	if (IS_ERR(s))
19502ad94ae6SAl Viro 		return PTR_ERR(s);
19512ad94ae6SAl Viro 
19522ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
19532ad94ae6SAl Viro 	if (error)
19542ad94ae6SAl Viro 		putname(s);
19552ad94ae6SAl Viro 	else
19562ad94ae6SAl Viro 		*name = s;
19572ad94ae6SAl Viro 
19582ad94ae6SAl Viro 	return error;
19592ad94ae6SAl Viro }
19602ad94ae6SAl Viro 
19611da177e4SLinus Torvalds /*
19621da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
19631da177e4SLinus Torvalds  * minimal.
19641da177e4SLinus Torvalds  */
19651da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
19661da177e4SLinus Torvalds {
19678e96e3b7SEric W. Biederman 	kuid_t fsuid = current_fsuid();
1968da9592edSDavid Howells 
19691da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
19701da177e4SLinus Torvalds 		return 0;
19718e96e3b7SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
19721da177e4SLinus Torvalds 		return 0;
19738e96e3b7SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
19741da177e4SLinus Torvalds 		return 0;
19751a48e2acSEric W. Biederman 	return !inode_capable(inode, CAP_FOWNER);
19761da177e4SLinus Torvalds }
19771da177e4SLinus Torvalds 
19781da177e4SLinus Torvalds /*
19791da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
19801da177e4SLinus Torvalds  *  whether the type of victim is right.
19811da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
19821da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
19831da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
19841da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
19851da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
19861da177e4SLinus Torvalds  *	a. be owner of dir, or
19871da177e4SLinus Torvalds  *	b. be owner of victim, or
19881da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
19891da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
19901da177e4SLinus Torvalds  *     links pointing to it.
19911da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
19921da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
19931da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
19941da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
19951da177e4SLinus Torvalds  *     nfs_async_unlink().
19961da177e4SLinus Torvalds  */
1997858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
19981da177e4SLinus Torvalds {
19991da177e4SLinus Torvalds 	int error;
20001da177e4SLinus Torvalds 
20011da177e4SLinus Torvalds 	if (!victim->d_inode)
20021da177e4SLinus Torvalds 		return -ENOENT;
20031da177e4SLinus Torvalds 
20041da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
2005cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
20061da177e4SLinus Torvalds 
2007f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
20081da177e4SLinus Torvalds 	if (error)
20091da177e4SLinus Torvalds 		return error;
20101da177e4SLinus Torvalds 	if (IS_APPEND(dir))
20111da177e4SLinus Torvalds 		return -EPERM;
20121da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
2013f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
20141da177e4SLinus Torvalds 		return -EPERM;
20151da177e4SLinus Torvalds 	if (isdir) {
20161da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
20171da177e4SLinus Torvalds 			return -ENOTDIR;
20181da177e4SLinus Torvalds 		if (IS_ROOT(victim))
20191da177e4SLinus Torvalds 			return -EBUSY;
20201da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
20211da177e4SLinus Torvalds 		return -EISDIR;
20221da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
20231da177e4SLinus Torvalds 		return -ENOENT;
20241da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
20251da177e4SLinus Torvalds 		return -EBUSY;
20261da177e4SLinus Torvalds 	return 0;
20271da177e4SLinus Torvalds }
20281da177e4SLinus Torvalds 
20291da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
20301da177e4SLinus Torvalds  *  dir.
20311da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
20321da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
20331da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
20341da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
20351da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
20361da177e4SLinus Torvalds  */
2037a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
20381da177e4SLinus Torvalds {
20391da177e4SLinus Torvalds 	if (child->d_inode)
20401da177e4SLinus Torvalds 		return -EEXIST;
20411da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
20421da177e4SLinus Torvalds 		return -ENOENT;
2043f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
20441da177e4SLinus Torvalds }
20451da177e4SLinus Torvalds 
20461da177e4SLinus Torvalds /*
20471da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
20481da177e4SLinus Torvalds  */
20491da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
20501da177e4SLinus Torvalds {
20511da177e4SLinus Torvalds 	struct dentry *p;
20521da177e4SLinus Torvalds 
20531da177e4SLinus Torvalds 	if (p1 == p2) {
2054f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
20551da177e4SLinus Torvalds 		return NULL;
20561da177e4SLinus Torvalds 	}
20571da177e4SLinus Torvalds 
2058a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
20591da177e4SLinus Torvalds 
2060e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
2061e2761a11SOGAWA Hirofumi 	if (p) {
2062f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2063f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
20641da177e4SLinus Torvalds 		return p;
20651da177e4SLinus Torvalds 	}
20661da177e4SLinus Torvalds 
2067e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
2068e2761a11SOGAWA Hirofumi 	if (p) {
2069f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2070f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
20711da177e4SLinus Torvalds 		return p;
20721da177e4SLinus Torvalds 	}
20731da177e4SLinus Torvalds 
2074f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2075f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
20761da177e4SLinus Torvalds 	return NULL;
20771da177e4SLinus Torvalds }
20781da177e4SLinus Torvalds 
20791da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
20801da177e4SLinus Torvalds {
20811b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
20821da177e4SLinus Torvalds 	if (p1 != p2) {
20831b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
2084a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
20851da177e4SLinus Torvalds 	}
20861da177e4SLinus Torvalds }
20871da177e4SLinus Torvalds 
20884acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
20891da177e4SLinus Torvalds 		struct nameidata *nd)
20901da177e4SLinus Torvalds {
2091a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
20921da177e4SLinus Torvalds 
20931da177e4SLinus Torvalds 	if (error)
20941da177e4SLinus Torvalds 		return error;
20951da177e4SLinus Torvalds 
2096acfa4380SAl Viro 	if (!dir->i_op->create)
20971da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
20981da177e4SLinus Torvalds 	mode &= S_IALLUGO;
20991da177e4SLinus Torvalds 	mode |= S_IFREG;
21001da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
21011da177e4SLinus Torvalds 	if (error)
21021da177e4SLinus Torvalds 		return error;
21031da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
2104a74574aaSStephen Smalley 	if (!error)
2105f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
21061da177e4SLinus Torvalds 	return error;
21071da177e4SLinus Torvalds }
21081da177e4SLinus Torvalds 
210973d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
21101da177e4SLinus Torvalds {
21113fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
21121da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
21131da177e4SLinus Torvalds 	int error;
21141da177e4SLinus Torvalds 
2115bcda7652SAl Viro 	/* O_PATH? */
2116bcda7652SAl Viro 	if (!acc_mode)
2117bcda7652SAl Viro 		return 0;
2118bcda7652SAl Viro 
21191da177e4SLinus Torvalds 	if (!inode)
21201da177e4SLinus Torvalds 		return -ENOENT;
21211da177e4SLinus Torvalds 
2122c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2123c8fe8f30SChristoph Hellwig 	case S_IFLNK:
21241da177e4SLinus Torvalds 		return -ELOOP;
2125c8fe8f30SChristoph Hellwig 	case S_IFDIR:
2126c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
21271da177e4SLinus Torvalds 			return -EISDIR;
2128c8fe8f30SChristoph Hellwig 		break;
2129c8fe8f30SChristoph Hellwig 	case S_IFBLK:
2130c8fe8f30SChristoph Hellwig 	case S_IFCHR:
21313fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
21321da177e4SLinus Torvalds 			return -EACCES;
2133c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
2134c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2135c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
21361da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2137c8fe8f30SChristoph Hellwig 		break;
21384a3fd211SDave Hansen 	}
2139b41572e9SDave Hansen 
21403fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
2141b41572e9SDave Hansen 	if (error)
2142b41572e9SDave Hansen 		return error;
21436146f0d5SMimi Zohar 
21441da177e4SLinus Torvalds 	/*
21451da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
21461da177e4SLinus Torvalds 	 */
21471da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
21488737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
21497715b521SAl Viro 			return -EPERM;
21501da177e4SLinus Torvalds 		if (flag & O_TRUNC)
21517715b521SAl Viro 			return -EPERM;
21521da177e4SLinus Torvalds 	}
21531da177e4SLinus Torvalds 
21541da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
21552e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
21567715b521SAl Viro 		return -EPERM;
21571da177e4SLinus Torvalds 
2158f3c7691eSJ. Bruce Fields 	return 0;
21597715b521SAl Viro }
21607715b521SAl Viro 
2161e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
21627715b521SAl Viro {
2163e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
21647715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
21657715b521SAl Viro 	int error = get_write_access(inode);
21661da177e4SLinus Torvalds 	if (error)
21677715b521SAl Viro 		return error;
21681da177e4SLinus Torvalds 	/*
21691da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
21701da177e4SLinus Torvalds 	 */
21711da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2172be6d3e56SKentaro Takeda 	if (!error)
2173ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
21741da177e4SLinus Torvalds 	if (!error) {
21757715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2176d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2177e1181ee6SJeff Layton 				    filp);
21781da177e4SLinus Torvalds 	}
21791da177e4SLinus Torvalds 	put_write_access(inode);
2180acd0c935SMimi Zohar 	return error;
21811da177e4SLinus Torvalds }
21821da177e4SLinus Torvalds 
2183d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2184d57999e1SDave Hansen {
21858a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
21868a5e929dSAl Viro 		flag--;
2187d57999e1SDave Hansen 	return flag;
2188d57999e1SDave Hansen }
2189d57999e1SDave Hansen 
219031e6b01fSNick Piggin /*
2191fe2d35ffSAl Viro  * Handle the last step of open()
219231e6b01fSNick Piggin  */
2193fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
2194c3e380b0SAl Viro 			    const struct open_flags *op, const char *pathname)
2195fb1cc555SAl Viro {
2196a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
21976c0d46c4SAl Viro 	struct dentry *dentry;
2198ca344a89SAl Viro 	int open_flag = op->open_flag;
21996c0d46c4SAl Viro 	int will_truncate = open_flag & O_TRUNC;
2200ca344a89SAl Viro 	int want_write = 0;
2201bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2202fb1cc555SAl Viro 	struct file *filp;
220316c2cd71SAl Viro 	int error;
2204fb1cc555SAl Viro 
2205c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2206c3e380b0SAl Viro 	nd->flags |= op->intent;
2207c3e380b0SAl Viro 
22081f36f774SAl Viro 	switch (nd->last_type) {
22091f36f774SAl Viro 	case LAST_DOTDOT:
2210176306f5SNeil Brown 	case LAST_DOT:
2211fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2212fe2d35ffSAl Viro 		if (error)
2213fe2d35ffSAl Viro 			return ERR_PTR(error);
22141f36f774SAl Viro 		/* fallthrough */
22151f36f774SAl Viro 	case LAST_ROOT:
22169f1fafeeSAl Viro 		error = complete_walk(nd);
221716c2cd71SAl Viro 		if (error)
22189f1fafeeSAl Viro 			return ERR_PTR(error);
2219fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2220ca344a89SAl Viro 		if (open_flag & O_CREAT) {
222116c2cd71SAl Viro 			error = -EISDIR;
22221f36f774SAl Viro 			goto exit;
2223fe2d35ffSAl Viro 		}
2224fe2d35ffSAl Viro 		goto ok;
22251f36f774SAl Viro 	case LAST_BIND:
22269f1fafeeSAl Viro 		error = complete_walk(nd);
222716c2cd71SAl Viro 		if (error)
22289f1fafeeSAl Viro 			return ERR_PTR(error);
22291f36f774SAl Viro 		audit_inode(pathname, dir);
22301f36f774SAl Viro 		goto ok;
22311f36f774SAl Viro 	}
2232a2c36b45SAl Viro 
2233ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2234bcda7652SAl Viro 		int symlink_ok = 0;
2235fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2236fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2237bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2238bcda7652SAl Viro 			symlink_ok = 1;
2239fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2240ce57dfc1SAl Viro 		error = walk_component(nd, path, &nd->last, LAST_NORM,
2241ce57dfc1SAl Viro 					!symlink_ok);
2242ce57dfc1SAl Viro 		if (error < 0)
2243fe2d35ffSAl Viro 			return ERR_PTR(error);
2244ce57dfc1SAl Viro 		if (error) /* symlink */
2245fe2d35ffSAl Viro 			return NULL;
2246fe2d35ffSAl Viro 		/* sayonara */
22479f1fafeeSAl Viro 		error = complete_walk(nd);
22489f1fafeeSAl Viro 		if (error)
22497f6c7e62SMiklos Szeredi 			return ERR_PTR(error);
2250fe2d35ffSAl Viro 
2251fe2d35ffSAl Viro 		error = -ENOTDIR;
2252fe2d35ffSAl Viro 		if (nd->flags & LOOKUP_DIRECTORY) {
2253ce57dfc1SAl Viro 			if (!nd->inode->i_op->lookup)
2254fe2d35ffSAl Viro 				goto exit;
2255fe2d35ffSAl Viro 		}
2256fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2257fe2d35ffSAl Viro 		goto ok;
2258fe2d35ffSAl Viro 	}
2259fe2d35ffSAl Viro 
2260fe2d35ffSAl Viro 	/* create side of things */
2261a3fbbde7SAl Viro 	/*
2262a3fbbde7SAl Viro 	 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED has been
2263a3fbbde7SAl Viro 	 * cleared when we got to the last component we are about to look up
2264a3fbbde7SAl Viro 	 */
22659f1fafeeSAl Viro 	error = complete_walk(nd);
22669f1fafeeSAl Viro 	if (error)
22679f1fafeeSAl Viro 		return ERR_PTR(error);
2268fe2d35ffSAl Viro 
2269fe2d35ffSAl Viro 	audit_inode(pathname, dir);
227016c2cd71SAl Viro 	error = -EISDIR;
22711f36f774SAl Viro 	/* trailing slashes? */
227231e6b01fSNick Piggin 	if (nd->last.name[nd->last.len])
22731f36f774SAl Viro 		goto exit;
22741f36f774SAl Viro 
2275a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
2276a1e28038SAl Viro 
22776c0d46c4SAl Viro 	dentry = lookup_hash(nd);
22786c0d46c4SAl Viro 	error = PTR_ERR(dentry);
22796c0d46c4SAl Viro 	if (IS_ERR(dentry)) {
2280fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
2281fb1cc555SAl Viro 		goto exit;
2282fb1cc555SAl Viro 	}
2283fb1cc555SAl Viro 
22846c0d46c4SAl Viro 	path->dentry = dentry;
22856c0d46c4SAl Viro 	path->mnt = nd->path.mnt;
22866c0d46c4SAl Viro 
2287fb1cc555SAl Viro 	/* Negative dentry, just create the file */
22886c0d46c4SAl Viro 	if (!dentry->d_inode) {
2289a218d0fdSAl Viro 		umode_t mode = op->mode;
22906c0d46c4SAl Viro 		if (!IS_POSIXACL(dir->d_inode))
22916c0d46c4SAl Viro 			mode &= ~current_umask();
2292fb1cc555SAl Viro 		/*
2293fb1cc555SAl Viro 		 * This write is needed to ensure that a
22946c0d46c4SAl Viro 		 * rw->ro transition does not occur between
2295fb1cc555SAl Viro 		 * the time when the file is created and when
2296fb1cc555SAl Viro 		 * a permanent write count is taken through
2297fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
2298fb1cc555SAl Viro 		 */
2299fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
2300fb1cc555SAl Viro 		if (error)
2301fb1cc555SAl Viro 			goto exit_mutex_unlock;
2302ca344a89SAl Viro 		want_write = 1;
23039b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2304ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
23056c0d46c4SAl Viro 		will_truncate = 0;
2306bcda7652SAl Viro 		acc_mode = MAY_OPEN;
23076c0d46c4SAl Viro 		error = security_path_mknod(&nd->path, dentry, mode, 0);
23086c0d46c4SAl Viro 		if (error)
23096c0d46c4SAl Viro 			goto exit_mutex_unlock;
23106c0d46c4SAl Viro 		error = vfs_create(dir->d_inode, dentry, mode, nd);
23116c0d46c4SAl Viro 		if (error)
23126c0d46c4SAl Viro 			goto exit_mutex_unlock;
23136c0d46c4SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
23146c0d46c4SAl Viro 		dput(nd->path.dentry);
23156c0d46c4SAl Viro 		nd->path.dentry = dentry;
2316ca344a89SAl Viro 		goto common;
2317fb1cc555SAl Viro 	}
2318fb1cc555SAl Viro 
2319fb1cc555SAl Viro 	/*
2320fb1cc555SAl Viro 	 * It already exists.
2321fb1cc555SAl Viro 	 */
2322fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2323fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2324fb1cc555SAl Viro 
2325fb1cc555SAl Viro 	error = -EEXIST;
2326ca344a89SAl Viro 	if (open_flag & O_EXCL)
2327fb1cc555SAl Viro 		goto exit_dput;
2328fb1cc555SAl Viro 
23299875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
23309875cf80SDavid Howells 	if (error < 0)
2331fb1cc555SAl Viro 		goto exit_dput;
2332fb1cc555SAl Viro 
2333a3fbbde7SAl Viro 	if (error)
2334a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
2335a3fbbde7SAl Viro 
2336fb1cc555SAl Viro 	error = -ENOENT;
2337fb1cc555SAl Viro 	if (!path->dentry->d_inode)
2338fb1cc555SAl Viro 		goto exit_dput;
23399e67f361SAl Viro 
23409e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
2341fb1cc555SAl Viro 		return NULL;
2342fb1cc555SAl Viro 
2343fb1cc555SAl Viro 	path_to_nameidata(path, nd);
234431e6b01fSNick Piggin 	nd->inode = path->dentry->d_inode;
2345a3fbbde7SAl Viro 	/* Why this, you ask?  _Now_ we might have grown LOOKUP_JUMPED... */
2346a3fbbde7SAl Viro 	error = complete_walk(nd);
2347a3fbbde7SAl Viro 	if (error)
2348097b180cSMiklos Szeredi 		return ERR_PTR(error);
2349fb1cc555SAl Viro 	error = -EISDIR;
235031e6b01fSNick Piggin 	if (S_ISDIR(nd->inode->i_mode))
2351fb1cc555SAl Viro 		goto exit;
235267ee3ad2SAl Viro ok:
23536c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
23546c0d46c4SAl Viro 		will_truncate = 0;
23556c0d46c4SAl Viro 
23560f9d1a10SAl Viro 	if (will_truncate) {
23570f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
23580f9d1a10SAl Viro 		if (error)
23590f9d1a10SAl Viro 			goto exit;
2360ca344a89SAl Viro 		want_write = 1;
23610f9d1a10SAl Viro 	}
2362ca344a89SAl Viro common:
2363bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2364ca344a89SAl Viro 	if (error)
23650f9d1a10SAl Viro 		goto exit;
23660f9d1a10SAl Viro 	filp = nameidata_to_filp(nd);
23670f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
23680f9d1a10SAl Viro 		error = ima_file_check(filp, op->acc_mode);
23690f9d1a10SAl Viro 		if (error) {
23700f9d1a10SAl Viro 			fput(filp);
23710f9d1a10SAl Viro 			filp = ERR_PTR(error);
23720f9d1a10SAl Viro 		}
23730f9d1a10SAl Viro 	}
23740f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
23750f9d1a10SAl Viro 		if (will_truncate) {
23760f9d1a10SAl Viro 			error = handle_truncate(filp);
23770f9d1a10SAl Viro 			if (error) {
23780f9d1a10SAl Viro 				fput(filp);
23790f9d1a10SAl Viro 				filp = ERR_PTR(error);
23800f9d1a10SAl Viro 			}
23810f9d1a10SAl Viro 		}
23820f9d1a10SAl Viro 	}
2383ca344a89SAl Viro out:
2384ca344a89SAl Viro 	if (want_write)
23850f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
23860f9d1a10SAl Viro 	path_put(&nd->path);
2387fb1cc555SAl Viro 	return filp;
2388fb1cc555SAl Viro 
2389fb1cc555SAl Viro exit_mutex_unlock:
2390fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2391fb1cc555SAl Viro exit_dput:
2392fb1cc555SAl Viro 	path_put_conditional(path, nd);
2393fb1cc555SAl Viro exit:
2394ca344a89SAl Viro 	filp = ERR_PTR(error);
2395ca344a89SAl Viro 	goto out;
2396fb1cc555SAl Viro }
2397fb1cc555SAl Viro 
239813aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname,
239973d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
24001da177e4SLinus Torvalds {
2401fe2d35ffSAl Viro 	struct file *base = NULL;
24024a3fd211SDave Hansen 	struct file *filp;
24039850c056SAl Viro 	struct path path;
240413aab428SAl Viro 	int error;
240531e6b01fSNick Piggin 
240631e6b01fSNick Piggin 	filp = get_empty_filp();
240731e6b01fSNick Piggin 	if (!filp)
240831e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
240931e6b01fSNick Piggin 
241047c805dcSAl Viro 	filp->f_flags = op->open_flag;
241173d049a4SAl Viro 	nd->intent.open.file = filp;
241273d049a4SAl Viro 	nd->intent.open.flags = open_to_namei_flags(op->open_flag);
241373d049a4SAl Viro 	nd->intent.open.create_mode = op->mode;
241431e6b01fSNick Piggin 
241573d049a4SAl Viro 	error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
241631e6b01fSNick Piggin 	if (unlikely(error))
241713aab428SAl Viro 		goto out_filp;
241831e6b01fSNick Piggin 
2419fe2d35ffSAl Viro 	current->total_link_count = 0;
242073d049a4SAl Viro 	error = link_path_walk(pathname, nd);
242131e6b01fSNick Piggin 	if (unlikely(error))
242231e6b01fSNick Piggin 		goto out_filp;
24231da177e4SLinus Torvalds 
242473d049a4SAl Viro 	filp = do_last(nd, &path, op, pathname);
2425806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
24267b9337aaSNick Piggin 		struct path link = path;
2427def4af30SAl Viro 		void *cookie;
2428574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
242973d049a4SAl Viro 			path_put_conditional(&path, nd);
243073d049a4SAl Viro 			path_put(&nd->path);
243140b39136SAl Viro 			filp = ERR_PTR(-ELOOP);
243240b39136SAl Viro 			break;
243340b39136SAl Viro 		}
243473d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
243573d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2436574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
2437c3e380b0SAl Viro 		if (unlikely(error))
2438f1afe9efSAl Viro 			filp = ERR_PTR(error);
2439c3e380b0SAl Viro 		else
244073d049a4SAl Viro 			filp = do_last(nd, &path, op, pathname);
2441574197e0SAl Viro 		put_link(nd, &link, cookie);
2442806b681cSAl Viro 	}
244310fa8e62SAl Viro out:
244473d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
244573d049a4SAl Viro 		path_put(&nd->root);
2446fe2d35ffSAl Viro 	if (base)
2447fe2d35ffSAl Viro 		fput(base);
244873d049a4SAl Viro 	release_open_intent(nd);
244910fa8e62SAl Viro 	return filp;
24501da177e4SLinus Torvalds 
245131e6b01fSNick Piggin out_filp:
245210fa8e62SAl Viro 	filp = ERR_PTR(error);
245310fa8e62SAl Viro 	goto out;
2454de459215SKirill Korotaev }
24551da177e4SLinus Torvalds 
245613aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname,
245713aab428SAl Viro 		const struct open_flags *op, int flags)
245813aab428SAl Viro {
245973d049a4SAl Viro 	struct nameidata nd;
246013aab428SAl Viro 	struct file *filp;
246113aab428SAl Viro 
246273d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
246313aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
246473d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
246513aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
246673d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
246713aab428SAl Viro 	return filp;
246813aab428SAl Viro }
246913aab428SAl Viro 
247073d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
247173d049a4SAl Viro 		const char *name, const struct open_flags *op, int flags)
247273d049a4SAl Viro {
247373d049a4SAl Viro 	struct nameidata nd;
247473d049a4SAl Viro 	struct file *file;
247573d049a4SAl Viro 
247673d049a4SAl Viro 	nd.root.mnt = mnt;
247773d049a4SAl Viro 	nd.root.dentry = dentry;
247873d049a4SAl Viro 
247973d049a4SAl Viro 	flags |= LOOKUP_ROOT;
248073d049a4SAl Viro 
2481bcda7652SAl Viro 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
248273d049a4SAl Viro 		return ERR_PTR(-ELOOP);
248373d049a4SAl Viro 
248473d049a4SAl Viro 	file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
248573d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
248673d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags);
248773d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
248873d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
248973d049a4SAl Viro 	return file;
249073d049a4SAl Viro }
249173d049a4SAl Viro 
2492ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
24931da177e4SLinus Torvalds {
2494c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
2495ed75e95dSAl Viro 	struct nameidata nd;
2496ed75e95dSAl Viro 	int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2497ed75e95dSAl Viro 	if (error)
2498ed75e95dSAl Viro 		return ERR_PTR(error);
24991da177e4SLinus Torvalds 
2500c663e5d8SChristoph Hellwig 	/*
2501c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2502c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2503c663e5d8SChristoph Hellwig 	 */
2504ed75e95dSAl Viro 	if (nd.last_type != LAST_NORM)
2505ed75e95dSAl Viro 		goto out;
2506ed75e95dSAl Viro 	nd.flags &= ~LOOKUP_PARENT;
2507ed75e95dSAl Viro 	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2508ed75e95dSAl Viro 	nd.intent.open.flags = O_EXCL;
2509c663e5d8SChristoph Hellwig 
2510c663e5d8SChristoph Hellwig 	/*
2511c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2512c663e5d8SChristoph Hellwig 	 */
2513ed75e95dSAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2514ed75e95dSAl Viro 	dentry = lookup_hash(&nd);
25151da177e4SLinus Torvalds 	if (IS_ERR(dentry))
25161da177e4SLinus Torvalds 		goto fail;
2517c663e5d8SChristoph Hellwig 
2518e9baf6e5SAl Viro 	if (dentry->d_inode)
2519e9baf6e5SAl Viro 		goto eexist;
2520c663e5d8SChristoph Hellwig 	/*
2521c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2522c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2523c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2524c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2525c663e5d8SChristoph Hellwig 	 */
2526ed75e95dSAl Viro 	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
25271da177e4SLinus Torvalds 		dput(dentry);
25281da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2529ed75e95dSAl Viro 		goto fail;
2530e9baf6e5SAl Viro 	}
2531ed75e95dSAl Viro 	*path = nd.path;
2532e9baf6e5SAl Viro 	return dentry;
2533e9baf6e5SAl Viro eexist:
2534e9baf6e5SAl Viro 	dput(dentry);
2535e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
25361da177e4SLinus Torvalds fail:
2537dae6ad8fSAl Viro 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2538ed75e95dSAl Viro out:
2539dae6ad8fSAl Viro 	path_put(&nd.path);
2540ed75e95dSAl Viro 	return dentry;
2541dae6ad8fSAl Viro }
2542dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create);
2543dae6ad8fSAl Viro 
2544dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2545dae6ad8fSAl Viro {
2546dae6ad8fSAl Viro 	char *tmp = getname(pathname);
2547dae6ad8fSAl Viro 	struct dentry *res;
2548dae6ad8fSAl Viro 	if (IS_ERR(tmp))
2549dae6ad8fSAl Viro 		return ERR_CAST(tmp);
2550dae6ad8fSAl Viro 	res = kern_path_create(dfd, tmp, path, is_dir);
2551dae6ad8fSAl Viro 	putname(tmp);
2552dae6ad8fSAl Viro 	return res;
2553dae6ad8fSAl Viro }
2554dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create);
2555dae6ad8fSAl Viro 
25561a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
25571da177e4SLinus Torvalds {
2558a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
25591da177e4SLinus Torvalds 
25601da177e4SLinus Torvalds 	if (error)
25611da177e4SLinus Torvalds 		return error;
25621da177e4SLinus Torvalds 
2563975d6b39SEric W. Biederman 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
25641da177e4SLinus Torvalds 		return -EPERM;
25651da177e4SLinus Torvalds 
2566acfa4380SAl Viro 	if (!dir->i_op->mknod)
25671da177e4SLinus Torvalds 		return -EPERM;
25681da177e4SLinus Torvalds 
256908ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
257008ce5f16SSerge E. Hallyn 	if (error)
257108ce5f16SSerge E. Hallyn 		return error;
257208ce5f16SSerge E. Hallyn 
25731da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
25741da177e4SLinus Torvalds 	if (error)
25751da177e4SLinus Torvalds 		return error;
25761da177e4SLinus Torvalds 
25771da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2578a74574aaSStephen Smalley 	if (!error)
2579f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
25801da177e4SLinus Torvalds 	return error;
25811da177e4SLinus Torvalds }
25821da177e4SLinus Torvalds 
2583f69aac00SAl Viro static int may_mknod(umode_t mode)
2584463c3197SDave Hansen {
2585463c3197SDave Hansen 	switch (mode & S_IFMT) {
2586463c3197SDave Hansen 	case S_IFREG:
2587463c3197SDave Hansen 	case S_IFCHR:
2588463c3197SDave Hansen 	case S_IFBLK:
2589463c3197SDave Hansen 	case S_IFIFO:
2590463c3197SDave Hansen 	case S_IFSOCK:
2591463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2592463c3197SDave Hansen 		return 0;
2593463c3197SDave Hansen 	case S_IFDIR:
2594463c3197SDave Hansen 		return -EPERM;
2595463c3197SDave Hansen 	default:
2596463c3197SDave Hansen 		return -EINVAL;
2597463c3197SDave Hansen 	}
2598463c3197SDave Hansen }
2599463c3197SDave Hansen 
26008208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
26012e4d0924SHeiko Carstens 		unsigned, dev)
26021da177e4SLinus Torvalds {
26031da177e4SLinus Torvalds 	struct dentry *dentry;
2604dae6ad8fSAl Viro 	struct path path;
2605dae6ad8fSAl Viro 	int error;
26061da177e4SLinus Torvalds 
26071da177e4SLinus Torvalds 	if (S_ISDIR(mode))
26081da177e4SLinus Torvalds 		return -EPERM;
26091da177e4SLinus Torvalds 
2610dae6ad8fSAl Viro 	dentry = user_path_create(dfd, filename, &path, 0);
2611dae6ad8fSAl Viro 	if (IS_ERR(dentry))
2612dae6ad8fSAl Viro 		return PTR_ERR(dentry);
26132ad94ae6SAl Viro 
2614dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
2615ce3b0f8dSAl Viro 		mode &= ~current_umask();
2616463c3197SDave Hansen 	error = may_mknod(mode);
2617463c3197SDave Hansen 	if (error)
2618463c3197SDave Hansen 		goto out_dput;
2619dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
2620463c3197SDave Hansen 	if (error)
2621463c3197SDave Hansen 		goto out_dput;
2622dae6ad8fSAl Viro 	error = security_path_mknod(&path, dentry, mode, dev);
2623be6d3e56SKentaro Takeda 	if (error)
2624be6d3e56SKentaro Takeda 		goto out_drop_write;
26251da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
26261da177e4SLinus Torvalds 		case 0: case S_IFREG:
2627dae6ad8fSAl Viro 			error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
26281da177e4SLinus Torvalds 			break;
26291da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
2630dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
26311da177e4SLinus Torvalds 					new_decode_dev(dev));
26321da177e4SLinus Torvalds 			break;
26331da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
2634dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
26351da177e4SLinus Torvalds 			break;
26361da177e4SLinus Torvalds 	}
2637be6d3e56SKentaro Takeda out_drop_write:
2638dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
2639463c3197SDave Hansen out_dput:
26401da177e4SLinus Torvalds 	dput(dentry);
2641dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2642dae6ad8fSAl Viro 	path_put(&path);
26431da177e4SLinus Torvalds 
26441da177e4SLinus Torvalds 	return error;
26451da177e4SLinus Torvalds }
26461da177e4SLinus Torvalds 
26478208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
26485590ff0dSUlrich Drepper {
26495590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
26505590ff0dSUlrich Drepper }
26515590ff0dSUlrich Drepper 
265218bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
26531da177e4SLinus Torvalds {
2654a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
26558de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
26561da177e4SLinus Torvalds 
26571da177e4SLinus Torvalds 	if (error)
26581da177e4SLinus Torvalds 		return error;
26591da177e4SLinus Torvalds 
2660acfa4380SAl Viro 	if (!dir->i_op->mkdir)
26611da177e4SLinus Torvalds 		return -EPERM;
26621da177e4SLinus Torvalds 
26631da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
26641da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
26651da177e4SLinus Torvalds 	if (error)
26661da177e4SLinus Torvalds 		return error;
26671da177e4SLinus Torvalds 
26688de52778SAl Viro 	if (max_links && dir->i_nlink >= max_links)
26698de52778SAl Viro 		return -EMLINK;
26708de52778SAl Viro 
26711da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2672a74574aaSStephen Smalley 	if (!error)
2673f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
26741da177e4SLinus Torvalds 	return error;
26751da177e4SLinus Torvalds }
26761da177e4SLinus Torvalds 
2677a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
26781da177e4SLinus Torvalds {
26796902d925SDave Hansen 	struct dentry *dentry;
2680dae6ad8fSAl Viro 	struct path path;
2681dae6ad8fSAl Viro 	int error;
26821da177e4SLinus Torvalds 
2683dae6ad8fSAl Viro 	dentry = user_path_create(dfd, pathname, &path, 1);
26846902d925SDave Hansen 	if (IS_ERR(dentry))
2685dae6ad8fSAl Viro 		return PTR_ERR(dentry);
26866902d925SDave Hansen 
2687dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
2688ce3b0f8dSAl Viro 		mode &= ~current_umask();
2689dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
2690463c3197SDave Hansen 	if (error)
2691463c3197SDave Hansen 		goto out_dput;
2692dae6ad8fSAl Viro 	error = security_path_mkdir(&path, dentry, mode);
2693be6d3e56SKentaro Takeda 	if (error)
2694be6d3e56SKentaro Takeda 		goto out_drop_write;
2695dae6ad8fSAl Viro 	error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
2696be6d3e56SKentaro Takeda out_drop_write:
2697dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
2698463c3197SDave Hansen out_dput:
26991da177e4SLinus Torvalds 	dput(dentry);
2700dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2701dae6ad8fSAl Viro 	path_put(&path);
27021da177e4SLinus Torvalds 	return error;
27031da177e4SLinus Torvalds }
27041da177e4SLinus Torvalds 
2705a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
27065590ff0dSUlrich Drepper {
27075590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
27085590ff0dSUlrich Drepper }
27095590ff0dSUlrich Drepper 
27101da177e4SLinus Torvalds /*
2711a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
2712c0d02594SJ. Bruce Fields  * should have a usage count of 1 if we're the only user of this
2713a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
2714a71905f0SSage Weil  * then we drop the dentry now.
27151da177e4SLinus Torvalds  *
27161da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
27171da177e4SLinus Torvalds  * do a
27181da177e4SLinus Torvalds  *
27191da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
27201da177e4SLinus Torvalds  *		return -EBUSY;
27211da177e4SLinus Torvalds  *
27221da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
27231da177e4SLinus Torvalds  * that is still in use by something else..
27241da177e4SLinus Torvalds  */
27251da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
27261da177e4SLinus Torvalds {
27271da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
27281da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
272964252c75SSage Weil 	if (dentry->d_count == 1)
27301da177e4SLinus Torvalds 		__d_drop(dentry);
27311da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
27321da177e4SLinus Torvalds }
27331da177e4SLinus Torvalds 
27341da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
27351da177e4SLinus Torvalds {
27361da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
27371da177e4SLinus Torvalds 
27381da177e4SLinus Torvalds 	if (error)
27391da177e4SLinus Torvalds 		return error;
27401da177e4SLinus Torvalds 
2741acfa4380SAl Viro 	if (!dir->i_op->rmdir)
27421da177e4SLinus Torvalds 		return -EPERM;
27431da177e4SLinus Torvalds 
27441d2ef590SAl Viro 	dget(dentry);
27451b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
2746912dbc15SSage Weil 
27471da177e4SLinus Torvalds 	error = -EBUSY;
2748912dbc15SSage Weil 	if (d_mountpoint(dentry))
2749912dbc15SSage Weil 		goto out;
2750912dbc15SSage Weil 
27511da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
2752912dbc15SSage Weil 	if (error)
2753912dbc15SSage Weil 		goto out;
2754912dbc15SSage Weil 
27553cebde24SSage Weil 	shrink_dcache_parent(dentry);
27561da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
2757912dbc15SSage Weil 	if (error)
2758912dbc15SSage Weil 		goto out;
2759912dbc15SSage Weil 
27601da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
2761d83c49f3SAl Viro 	dont_mount(dentry);
27621da177e4SLinus Torvalds 
2763912dbc15SSage Weil out:
2764912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
27651d2ef590SAl Viro 	dput(dentry);
2766912dbc15SSage Weil 	if (!error)
2767912dbc15SSage Weil 		d_delete(dentry);
27681da177e4SLinus Torvalds 	return error;
27691da177e4SLinus Torvalds }
27701da177e4SLinus Torvalds 
27715590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
27721da177e4SLinus Torvalds {
27731da177e4SLinus Torvalds 	int error = 0;
27741da177e4SLinus Torvalds 	char * name;
27751da177e4SLinus Torvalds 	struct dentry *dentry;
27761da177e4SLinus Torvalds 	struct nameidata nd;
27771da177e4SLinus Torvalds 
27782ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
27791da177e4SLinus Torvalds 	if (error)
27802ad94ae6SAl Viro 		return error;
27811da177e4SLinus Torvalds 
27821da177e4SLinus Torvalds 	switch(nd.last_type) {
27831da177e4SLinus Torvalds 	case LAST_DOTDOT:
27841da177e4SLinus Torvalds 		error = -ENOTEMPTY;
27851da177e4SLinus Torvalds 		goto exit1;
27861da177e4SLinus Torvalds 	case LAST_DOT:
27871da177e4SLinus Torvalds 		error = -EINVAL;
27881da177e4SLinus Torvalds 		goto exit1;
27891da177e4SLinus Torvalds 	case LAST_ROOT:
27901da177e4SLinus Torvalds 		error = -EBUSY;
27911da177e4SLinus Torvalds 		goto exit1;
27921da177e4SLinus Torvalds 	}
27930612d9fbSOGAWA Hirofumi 
27940612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
27950612d9fbSOGAWA Hirofumi 
27964ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
279749705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
27981da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
27996902d925SDave Hansen 	if (IS_ERR(dentry))
28006902d925SDave Hansen 		goto exit2;
2801e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
2802e6bc45d6STheodore Ts'o 		error = -ENOENT;
2803e6bc45d6STheodore Ts'o 		goto exit3;
2804e6bc45d6STheodore Ts'o 	}
28050622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
28060622753bSDave Hansen 	if (error)
28070622753bSDave Hansen 		goto exit3;
2808be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2809be6d3e56SKentaro Takeda 	if (error)
2810be6d3e56SKentaro Takeda 		goto exit4;
28114ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2812be6d3e56SKentaro Takeda exit4:
28130622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
28140622753bSDave Hansen exit3:
28151da177e4SLinus Torvalds 	dput(dentry);
28166902d925SDave Hansen exit2:
28174ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
28181da177e4SLinus Torvalds exit1:
28191d957f9bSJan Blunck 	path_put(&nd.path);
28201da177e4SLinus Torvalds 	putname(name);
28211da177e4SLinus Torvalds 	return error;
28221da177e4SLinus Torvalds }
28231da177e4SLinus Torvalds 
28243cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
28255590ff0dSUlrich Drepper {
28265590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
28275590ff0dSUlrich Drepper }
28285590ff0dSUlrich Drepper 
28291da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
28301da177e4SLinus Torvalds {
28311da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
28321da177e4SLinus Torvalds 
28331da177e4SLinus Torvalds 	if (error)
28341da177e4SLinus Torvalds 		return error;
28351da177e4SLinus Torvalds 
2836acfa4380SAl Viro 	if (!dir->i_op->unlink)
28371da177e4SLinus Torvalds 		return -EPERM;
28381da177e4SLinus Torvalds 
28391b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
28401da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
28411da177e4SLinus Torvalds 		error = -EBUSY;
28421da177e4SLinus Torvalds 	else {
28431da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2844bec1052eSAl Viro 		if (!error) {
28451da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2846bec1052eSAl Viro 			if (!error)
2847d83c49f3SAl Viro 				dont_mount(dentry);
2848bec1052eSAl Viro 		}
28491da177e4SLinus Torvalds 	}
28501b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
28511da177e4SLinus Torvalds 
28521da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
28531da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2854ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
28551da177e4SLinus Torvalds 		d_delete(dentry);
28561da177e4SLinus Torvalds 	}
28570eeca283SRobert Love 
28581da177e4SLinus Torvalds 	return error;
28591da177e4SLinus Torvalds }
28601da177e4SLinus Torvalds 
28611da177e4SLinus Torvalds /*
28621da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
28631b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
28641da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
28651da177e4SLinus Torvalds  * while waiting on the I/O.
28661da177e4SLinus Torvalds  */
28675590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
28681da177e4SLinus Torvalds {
28692ad94ae6SAl Viro 	int error;
28701da177e4SLinus Torvalds 	char *name;
28711da177e4SLinus Torvalds 	struct dentry *dentry;
28721da177e4SLinus Torvalds 	struct nameidata nd;
28731da177e4SLinus Torvalds 	struct inode *inode = NULL;
28741da177e4SLinus Torvalds 
28752ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
28761da177e4SLinus Torvalds 	if (error)
28772ad94ae6SAl Viro 		return error;
28782ad94ae6SAl Viro 
28791da177e4SLinus Torvalds 	error = -EISDIR;
28801da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
28811da177e4SLinus Torvalds 		goto exit1;
28820612d9fbSOGAWA Hirofumi 
28830612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
28840612d9fbSOGAWA Hirofumi 
28854ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
288649705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
28871da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
28881da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
28891da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
289050338b88STörök Edwin 		if (nd.last.name[nd.last.len])
289150338b88STörök Edwin 			goto slashes;
28921da177e4SLinus Torvalds 		inode = dentry->d_inode;
289350338b88STörök Edwin 		if (!inode)
2894e6bc45d6STheodore Ts'o 			goto slashes;
28957de9c6eeSAl Viro 		ihold(inode);
28960622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
28970622753bSDave Hansen 		if (error)
28980622753bSDave Hansen 			goto exit2;
2899be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2900be6d3e56SKentaro Takeda 		if (error)
2901be6d3e56SKentaro Takeda 			goto exit3;
29024ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2903be6d3e56SKentaro Takeda exit3:
29040622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
29051da177e4SLinus Torvalds 	exit2:
29061da177e4SLinus Torvalds 		dput(dentry);
29071da177e4SLinus Torvalds 	}
29084ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
29091da177e4SLinus Torvalds 	if (inode)
29101da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
29111da177e4SLinus Torvalds exit1:
29121d957f9bSJan Blunck 	path_put(&nd.path);
29131da177e4SLinus Torvalds 	putname(name);
29141da177e4SLinus Torvalds 	return error;
29151da177e4SLinus Torvalds 
29161da177e4SLinus Torvalds slashes:
29171da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
29181da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
29191da177e4SLinus Torvalds 	goto exit2;
29201da177e4SLinus Torvalds }
29211da177e4SLinus Torvalds 
29222e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
29235590ff0dSUlrich Drepper {
29245590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
29255590ff0dSUlrich Drepper 		return -EINVAL;
29265590ff0dSUlrich Drepper 
29275590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
29285590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
29295590ff0dSUlrich Drepper 
29305590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
29315590ff0dSUlrich Drepper }
29325590ff0dSUlrich Drepper 
29333480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
29345590ff0dSUlrich Drepper {
29355590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
29365590ff0dSUlrich Drepper }
29375590ff0dSUlrich Drepper 
2938db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
29391da177e4SLinus Torvalds {
2940a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
29411da177e4SLinus Torvalds 
29421da177e4SLinus Torvalds 	if (error)
29431da177e4SLinus Torvalds 		return error;
29441da177e4SLinus Torvalds 
2945acfa4380SAl Viro 	if (!dir->i_op->symlink)
29461da177e4SLinus Torvalds 		return -EPERM;
29471da177e4SLinus Torvalds 
29481da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
29491da177e4SLinus Torvalds 	if (error)
29501da177e4SLinus Torvalds 		return error;
29511da177e4SLinus Torvalds 
29521da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2953a74574aaSStephen Smalley 	if (!error)
2954f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
29551da177e4SLinus Torvalds 	return error;
29561da177e4SLinus Torvalds }
29571da177e4SLinus Torvalds 
29582e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
29592e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
29601da177e4SLinus Torvalds {
29612ad94ae6SAl Viro 	int error;
29621da177e4SLinus Torvalds 	char *from;
29636902d925SDave Hansen 	struct dentry *dentry;
2964dae6ad8fSAl Viro 	struct path path;
29651da177e4SLinus Torvalds 
29661da177e4SLinus Torvalds 	from = getname(oldname);
29671da177e4SLinus Torvalds 	if (IS_ERR(from))
29681da177e4SLinus Torvalds 		return PTR_ERR(from);
29692ad94ae6SAl Viro 
2970dae6ad8fSAl Viro 	dentry = user_path_create(newdfd, newname, &path, 0);
29711da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
29726902d925SDave Hansen 	if (IS_ERR(dentry))
2973dae6ad8fSAl Viro 		goto out_putname;
29746902d925SDave Hansen 
2975dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
297675c3f29dSDave Hansen 	if (error)
297775c3f29dSDave Hansen 		goto out_dput;
2978dae6ad8fSAl Viro 	error = security_path_symlink(&path, dentry, from);
2979be6d3e56SKentaro Takeda 	if (error)
2980be6d3e56SKentaro Takeda 		goto out_drop_write;
2981dae6ad8fSAl Viro 	error = vfs_symlink(path.dentry->d_inode, dentry, from);
2982be6d3e56SKentaro Takeda out_drop_write:
2983dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
298475c3f29dSDave Hansen out_dput:
29851da177e4SLinus Torvalds 	dput(dentry);
2986dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2987dae6ad8fSAl Viro 	path_put(&path);
29886902d925SDave Hansen out_putname:
29891da177e4SLinus Torvalds 	putname(from);
29901da177e4SLinus Torvalds 	return error;
29911da177e4SLinus Torvalds }
29921da177e4SLinus Torvalds 
29933480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
29945590ff0dSUlrich Drepper {
29955590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
29965590ff0dSUlrich Drepper }
29975590ff0dSUlrich Drepper 
29981da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
29991da177e4SLinus Torvalds {
30001da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
30018de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
30021da177e4SLinus Torvalds 	int error;
30031da177e4SLinus Torvalds 
30041da177e4SLinus Torvalds 	if (!inode)
30051da177e4SLinus Torvalds 		return -ENOENT;
30061da177e4SLinus Torvalds 
3007a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
30081da177e4SLinus Torvalds 	if (error)
30091da177e4SLinus Torvalds 		return error;
30101da177e4SLinus Torvalds 
30111da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
30121da177e4SLinus Torvalds 		return -EXDEV;
30131da177e4SLinus Torvalds 
30141da177e4SLinus Torvalds 	/*
30151da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
30161da177e4SLinus Torvalds 	 */
30171da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
30181da177e4SLinus Torvalds 		return -EPERM;
3019acfa4380SAl Viro 	if (!dir->i_op->link)
30201da177e4SLinus Torvalds 		return -EPERM;
30217e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
30221da177e4SLinus Torvalds 		return -EPERM;
30231da177e4SLinus Torvalds 
30241da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
30251da177e4SLinus Torvalds 	if (error)
30261da177e4SLinus Torvalds 		return error;
30271da177e4SLinus Torvalds 
30287e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
3029aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
3030aae8a97dSAneesh Kumar K.V 	if (inode->i_nlink == 0)
3031aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
30328de52778SAl Viro 	else if (max_links && inode->i_nlink >= max_links)
30338de52778SAl Viro 		error = -EMLINK;
3034aae8a97dSAneesh Kumar K.V 	else
30351da177e4SLinus Torvalds 		error = dir->i_op->link(old_dentry, dir, new_dentry);
30367e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
3037e31e14ecSStephen Smalley 	if (!error)
30387e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
30391da177e4SLinus Torvalds 	return error;
30401da177e4SLinus Torvalds }
30411da177e4SLinus Torvalds 
30421da177e4SLinus Torvalds /*
30431da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
30441da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
30451da177e4SLinus Torvalds  * newname.  --KAB
30461da177e4SLinus Torvalds  *
30471da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
30481da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
30491da177e4SLinus Torvalds  * and other special files.  --ADM
30501da177e4SLinus Torvalds  */
30512e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
30522e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
30531da177e4SLinus Torvalds {
30541da177e4SLinus Torvalds 	struct dentry *new_dentry;
3055dae6ad8fSAl Viro 	struct path old_path, new_path;
305611a7b371SAneesh Kumar K.V 	int how = 0;
30571da177e4SLinus Torvalds 	int error;
30581da177e4SLinus Torvalds 
305911a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3060c04030e1SUlrich Drepper 		return -EINVAL;
306111a7b371SAneesh Kumar K.V 	/*
306211a7b371SAneesh Kumar K.V 	 * To use null names we require CAP_DAC_READ_SEARCH
306311a7b371SAneesh Kumar K.V 	 * This ensures that not everyone will be able to create
306411a7b371SAneesh Kumar K.V 	 * handlink using the passed filedescriptor.
306511a7b371SAneesh Kumar K.V 	 */
306611a7b371SAneesh Kumar K.V 	if (flags & AT_EMPTY_PATH) {
306711a7b371SAneesh Kumar K.V 		if (!capable(CAP_DAC_READ_SEARCH))
306811a7b371SAneesh Kumar K.V 			return -ENOENT;
306911a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
307011a7b371SAneesh Kumar K.V 	}
3071c04030e1SUlrich Drepper 
307211a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
307311a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
307411a7b371SAneesh Kumar K.V 
307511a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
30761da177e4SLinus Torvalds 	if (error)
30772ad94ae6SAl Viro 		return error;
30782ad94ae6SAl Viro 
3079dae6ad8fSAl Viro 	new_dentry = user_path_create(newdfd, newname, &new_path, 0);
30801da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
30816902d925SDave Hansen 	if (IS_ERR(new_dentry))
3082dae6ad8fSAl Viro 		goto out;
3083dae6ad8fSAl Viro 
3084dae6ad8fSAl Viro 	error = -EXDEV;
3085dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt)
3086dae6ad8fSAl Viro 		goto out_dput;
3087dae6ad8fSAl Viro 	error = mnt_want_write(new_path.mnt);
308875c3f29dSDave Hansen 	if (error)
308975c3f29dSDave Hansen 		goto out_dput;
3090dae6ad8fSAl Viro 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
3091be6d3e56SKentaro Takeda 	if (error)
3092be6d3e56SKentaro Takeda 		goto out_drop_write;
3093dae6ad8fSAl Viro 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
3094be6d3e56SKentaro Takeda out_drop_write:
3095dae6ad8fSAl Viro 	mnt_drop_write(new_path.mnt);
309675c3f29dSDave Hansen out_dput:
30971da177e4SLinus Torvalds 	dput(new_dentry);
3098dae6ad8fSAl Viro 	mutex_unlock(&new_path.dentry->d_inode->i_mutex);
3099dae6ad8fSAl Viro 	path_put(&new_path);
31001da177e4SLinus Torvalds out:
31012d8f3038SAl Viro 	path_put(&old_path);
31021da177e4SLinus Torvalds 
31031da177e4SLinus Torvalds 	return error;
31041da177e4SLinus Torvalds }
31051da177e4SLinus Torvalds 
31063480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
31075590ff0dSUlrich Drepper {
3108c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
31095590ff0dSUlrich Drepper }
31105590ff0dSUlrich Drepper 
31111da177e4SLinus Torvalds /*
31121da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
31131da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
31141da177e4SLinus Torvalds  * Problems:
31151da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
31161da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
31171da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
3118a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
31191da177e4SLinus Torvalds  *	   story.
31201da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
31211b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
31221da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
31231da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
3124a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
31251da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
31261da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
31271da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
3128a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
31291da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
31301da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
31311da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
3132e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
31331b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
31341da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3135c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
31361da177e4SLinus Torvalds  *	   locking].
31371da177e4SLinus Torvalds  */
313875c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
31391da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
31401da177e4SLinus Torvalds {
31411da177e4SLinus Torvalds 	int error = 0;
31429055cba7SSage Weil 	struct inode *target = new_dentry->d_inode;
31438de52778SAl Viro 	unsigned max_links = new_dir->i_sb->s_max_links;
31441da177e4SLinus Torvalds 
31451da177e4SLinus Torvalds 	/*
31461da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
31471da177e4SLinus Torvalds 	 * we'll need to flip '..'.
31481da177e4SLinus Torvalds 	 */
31491da177e4SLinus Torvalds 	if (new_dir != old_dir) {
3150f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
31511da177e4SLinus Torvalds 		if (error)
31521da177e4SLinus Torvalds 			return error;
31531da177e4SLinus Torvalds 	}
31541da177e4SLinus Torvalds 
31551da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
31561da177e4SLinus Torvalds 	if (error)
31571da177e4SLinus Torvalds 		return error;
31581da177e4SLinus Torvalds 
31591d2ef590SAl Viro 	dget(new_dentry);
3160d83c49f3SAl Viro 	if (target)
31611b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
31629055cba7SSage Weil 
31631da177e4SLinus Torvalds 	error = -EBUSY;
31649055cba7SSage Weil 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
31659055cba7SSage Weil 		goto out;
31669055cba7SSage Weil 
31678de52778SAl Viro 	error = -EMLINK;
31688de52778SAl Viro 	if (max_links && !target && new_dir != old_dir &&
31698de52778SAl Viro 	    new_dir->i_nlink >= max_links)
31708de52778SAl Viro 		goto out;
31718de52778SAl Viro 
31723cebde24SSage Weil 	if (target)
31733cebde24SSage Weil 		shrink_dcache_parent(new_dentry);
31741da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
31759055cba7SSage Weil 	if (error)
31769055cba7SSage Weil 		goto out;
31779055cba7SSage Weil 
31781da177e4SLinus Torvalds 	if (target) {
31791da177e4SLinus Torvalds 		target->i_flags |= S_DEAD;
3180d83c49f3SAl Viro 		dont_mount(new_dentry);
3181d83c49f3SAl Viro 	}
31829055cba7SSage Weil out:
31839055cba7SSage Weil 	if (target)
31841b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
31851d2ef590SAl Viro 	dput(new_dentry);
3186e31e14ecSStephen Smalley 	if (!error)
3187349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
31881da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
31891da177e4SLinus Torvalds 	return error;
31901da177e4SLinus Torvalds }
31911da177e4SLinus Torvalds 
319275c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
31931da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
31941da177e4SLinus Torvalds {
319551892bbbSSage Weil 	struct inode *target = new_dentry->d_inode;
31961da177e4SLinus Torvalds 	int error;
31971da177e4SLinus Torvalds 
31981da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
31991da177e4SLinus Torvalds 	if (error)
32001da177e4SLinus Torvalds 		return error;
32011da177e4SLinus Torvalds 
32021da177e4SLinus Torvalds 	dget(new_dentry);
32031da177e4SLinus Torvalds 	if (target)
32041b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
320551892bbbSSage Weil 
32061da177e4SLinus Torvalds 	error = -EBUSY;
320751892bbbSSage Weil 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
320851892bbbSSage Weil 		goto out;
320951892bbbSSage Weil 
32101da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
321151892bbbSSage Weil 	if (error)
321251892bbbSSage Weil 		goto out;
321351892bbbSSage Weil 
3214bec1052eSAl Viro 	if (target)
3215d83c49f3SAl Viro 		dont_mount(new_dentry);
3216349457ccSMark Fasheh 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
32171da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
321851892bbbSSage Weil out:
32191da177e4SLinus Torvalds 	if (target)
32201b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
32211da177e4SLinus Torvalds 	dput(new_dentry);
32221da177e4SLinus Torvalds 	return error;
32231da177e4SLinus Torvalds }
32241da177e4SLinus Torvalds 
32251da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
32261da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
32271da177e4SLinus Torvalds {
32281da177e4SLinus Torvalds 	int error;
32291da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
323059b0df21SEric Paris 	const unsigned char *old_name;
32311da177e4SLinus Torvalds 
32321da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
32331da177e4SLinus Torvalds  		return 0;
32341da177e4SLinus Torvalds 
32351da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
32361da177e4SLinus Torvalds 	if (error)
32371da177e4SLinus Torvalds 		return error;
32381da177e4SLinus Torvalds 
32391da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3240a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
32411da177e4SLinus Torvalds 	else
32421da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
32431da177e4SLinus Torvalds 	if (error)
32441da177e4SLinus Torvalds 		return error;
32451da177e4SLinus Torvalds 
3246acfa4380SAl Viro 	if (!old_dir->i_op->rename)
32471da177e4SLinus Torvalds 		return -EPERM;
32481da177e4SLinus Torvalds 
32490eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
32500eeca283SRobert Love 
32511da177e4SLinus Torvalds 	if (is_dir)
32521da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
32531da177e4SLinus Torvalds 	else
32541da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3255123df294SAl Viro 	if (!error)
3256123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
32575a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
32580eeca283SRobert Love 	fsnotify_oldname_free(old_name);
32590eeca283SRobert Love 
32601da177e4SLinus Torvalds 	return error;
32611da177e4SLinus Torvalds }
32621da177e4SLinus Torvalds 
32632e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
32642e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
32651da177e4SLinus Torvalds {
32661da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
32671da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
32681da177e4SLinus Torvalds 	struct dentry *trap;
32691da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
32702ad94ae6SAl Viro 	char *from;
32712ad94ae6SAl Viro 	char *to;
32722ad94ae6SAl Viro 	int error;
32731da177e4SLinus Torvalds 
32742ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
32751da177e4SLinus Torvalds 	if (error)
32761da177e4SLinus Torvalds 		goto exit;
32771da177e4SLinus Torvalds 
32782ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
32791da177e4SLinus Torvalds 	if (error)
32801da177e4SLinus Torvalds 		goto exit1;
32811da177e4SLinus Torvalds 
32821da177e4SLinus Torvalds 	error = -EXDEV;
32834ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
32841da177e4SLinus Torvalds 		goto exit2;
32851da177e4SLinus Torvalds 
32864ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
32871da177e4SLinus Torvalds 	error = -EBUSY;
32881da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
32891da177e4SLinus Torvalds 		goto exit2;
32901da177e4SLinus Torvalds 
32914ac91378SJan Blunck 	new_dir = newnd.path.dentry;
32921da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
32931da177e4SLinus Torvalds 		goto exit2;
32941da177e4SLinus Torvalds 
32950612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
32960612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
32974e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
32980612d9fbSOGAWA Hirofumi 
32991da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
33001da177e4SLinus Torvalds 
330149705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
33021da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
33031da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
33041da177e4SLinus Torvalds 		goto exit3;
33051da177e4SLinus Torvalds 	/* source must exist */
33061da177e4SLinus Torvalds 	error = -ENOENT;
33071da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
33081da177e4SLinus Torvalds 		goto exit4;
33091da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
33101da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
33111da177e4SLinus Torvalds 		error = -ENOTDIR;
33121da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
33131da177e4SLinus Torvalds 			goto exit4;
33141da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
33151da177e4SLinus Torvalds 			goto exit4;
33161da177e4SLinus Torvalds 	}
33171da177e4SLinus Torvalds 	/* source should not be ancestor of target */
33181da177e4SLinus Torvalds 	error = -EINVAL;
33191da177e4SLinus Torvalds 	if (old_dentry == trap)
33201da177e4SLinus Torvalds 		goto exit4;
332149705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
33221da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
33231da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
33241da177e4SLinus Torvalds 		goto exit4;
33251da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
33261da177e4SLinus Torvalds 	error = -ENOTEMPTY;
33271da177e4SLinus Torvalds 	if (new_dentry == trap)
33281da177e4SLinus Torvalds 		goto exit5;
33291da177e4SLinus Torvalds 
33309079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
33319079b1ebSDave Hansen 	if (error)
33329079b1ebSDave Hansen 		goto exit5;
3333be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3334be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3335be6d3e56SKentaro Takeda 	if (error)
3336be6d3e56SKentaro Takeda 		goto exit6;
33371da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
33381da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3339be6d3e56SKentaro Takeda exit6:
33409079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
33411da177e4SLinus Torvalds exit5:
33421da177e4SLinus Torvalds 	dput(new_dentry);
33431da177e4SLinus Torvalds exit4:
33441da177e4SLinus Torvalds 	dput(old_dentry);
33451da177e4SLinus Torvalds exit3:
33461da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
33471da177e4SLinus Torvalds exit2:
33481d957f9bSJan Blunck 	path_put(&newnd.path);
33492ad94ae6SAl Viro 	putname(to);
33501da177e4SLinus Torvalds exit1:
33511d957f9bSJan Blunck 	path_put(&oldnd.path);
33521da177e4SLinus Torvalds 	putname(from);
33532ad94ae6SAl Viro exit:
33541da177e4SLinus Torvalds 	return error;
33551da177e4SLinus Torvalds }
33561da177e4SLinus Torvalds 
3357a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
33585590ff0dSUlrich Drepper {
33595590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
33605590ff0dSUlrich Drepper }
33615590ff0dSUlrich Drepper 
33621da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
33631da177e4SLinus Torvalds {
33641da177e4SLinus Torvalds 	int len;
33651da177e4SLinus Torvalds 
33661da177e4SLinus Torvalds 	len = PTR_ERR(link);
33671da177e4SLinus Torvalds 	if (IS_ERR(link))
33681da177e4SLinus Torvalds 		goto out;
33691da177e4SLinus Torvalds 
33701da177e4SLinus Torvalds 	len = strlen(link);
33711da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
33721da177e4SLinus Torvalds 		len = buflen;
33731da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
33741da177e4SLinus Torvalds 		len = -EFAULT;
33751da177e4SLinus Torvalds out:
33761da177e4SLinus Torvalds 	return len;
33771da177e4SLinus Torvalds }
33781da177e4SLinus Torvalds 
33791da177e4SLinus Torvalds /*
33801da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
33811da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
33821da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
33831da177e4SLinus Torvalds  */
33841da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
33851da177e4SLinus Torvalds {
33861da177e4SLinus Torvalds 	struct nameidata nd;
3387cc314eefSLinus Torvalds 	void *cookie;
3388694a1764SMarcin Slusarz 	int res;
3389cc314eefSLinus Torvalds 
33901da177e4SLinus Torvalds 	nd.depth = 0;
3391cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3392694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3393694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3394694a1764SMarcin Slusarz 
3395694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
33961da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3397cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3398694a1764SMarcin Slusarz 	return res;
33991da177e4SLinus Torvalds }
34001da177e4SLinus Torvalds 
34011da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
34021da177e4SLinus Torvalds {
34031da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
34041da177e4SLinus Torvalds }
34051da177e4SLinus Torvalds 
34061da177e4SLinus Torvalds /* get the link contents into pagecache */
34071da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
34081da177e4SLinus Torvalds {
3409ebd09abbSDuane Griffin 	char *kaddr;
34101da177e4SLinus Torvalds 	struct page *page;
34111da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3412090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
34131da177e4SLinus Torvalds 	if (IS_ERR(page))
34146fe6900eSNick Piggin 		return (char*)page;
34151da177e4SLinus Torvalds 	*ppage = page;
3416ebd09abbSDuane Griffin 	kaddr = kmap(page);
3417ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3418ebd09abbSDuane Griffin 	return kaddr;
34191da177e4SLinus Torvalds }
34201da177e4SLinus Torvalds 
34211da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
34221da177e4SLinus Torvalds {
34231da177e4SLinus Torvalds 	struct page *page = NULL;
34241da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
34251da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
34261da177e4SLinus Torvalds 	if (page) {
34271da177e4SLinus Torvalds 		kunmap(page);
34281da177e4SLinus Torvalds 		page_cache_release(page);
34291da177e4SLinus Torvalds 	}
34301da177e4SLinus Torvalds 	return res;
34311da177e4SLinus Torvalds }
34321da177e4SLinus Torvalds 
3433cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
34341da177e4SLinus Torvalds {
3435cc314eefSLinus Torvalds 	struct page *page = NULL;
34361da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3437cc314eefSLinus Torvalds 	return page;
34381da177e4SLinus Torvalds }
34391da177e4SLinus Torvalds 
3440cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
34411da177e4SLinus Torvalds {
3442cc314eefSLinus Torvalds 	struct page *page = cookie;
3443cc314eefSLinus Torvalds 
3444cc314eefSLinus Torvalds 	if (page) {
34451da177e4SLinus Torvalds 		kunmap(page);
34461da177e4SLinus Torvalds 		page_cache_release(page);
34471da177e4SLinus Torvalds 	}
34481da177e4SLinus Torvalds }
34491da177e4SLinus Torvalds 
345054566b2cSNick Piggin /*
345154566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
345254566b2cSNick Piggin  */
345354566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
34541da177e4SLinus Torvalds {
34551da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
34560adb25d2SKirill Korotaev 	struct page *page;
3457afddba49SNick Piggin 	void *fsdata;
3458beb497abSDmitriy Monakhov 	int err;
34591da177e4SLinus Torvalds 	char *kaddr;
346054566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
346154566b2cSNick Piggin 	if (nofs)
346254566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
34631da177e4SLinus Torvalds 
34647e53cac4SNeilBrown retry:
3465afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
346654566b2cSNick Piggin 				flags, &page, &fsdata);
34671da177e4SLinus Torvalds 	if (err)
3468afddba49SNick Piggin 		goto fail;
3469afddba49SNick Piggin 
3470e8e3c3d6SCong Wang 	kaddr = kmap_atomic(page);
34711da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
3472e8e3c3d6SCong Wang 	kunmap_atomic(kaddr);
3473afddba49SNick Piggin 
3474afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3475afddba49SNick Piggin 							page, fsdata);
34761da177e4SLinus Torvalds 	if (err < 0)
34771da177e4SLinus Torvalds 		goto fail;
3478afddba49SNick Piggin 	if (err < len-1)
3479afddba49SNick Piggin 		goto retry;
3480afddba49SNick Piggin 
34811da177e4SLinus Torvalds 	mark_inode_dirty(inode);
34821da177e4SLinus Torvalds 	return 0;
34831da177e4SLinus Torvalds fail:
34841da177e4SLinus Torvalds 	return err;
34851da177e4SLinus Torvalds }
34861da177e4SLinus Torvalds 
34870adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
34880adb25d2SKirill Korotaev {
34890adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
349054566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
34910adb25d2SKirill Korotaev }
34920adb25d2SKirill Korotaev 
349392e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
34941da177e4SLinus Torvalds 	.readlink	= generic_readlink,
34951da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
34961da177e4SLinus Torvalds 	.put_link	= page_put_link,
34971da177e4SLinus Torvalds };
34981da177e4SLinus Torvalds 
34992d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
3500cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
35011da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
35021da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
35031da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
35041da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
35051da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
35061da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
35071da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
35081da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
35091da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
35100adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
35111da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
35121da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
3513d1811465SAl Viro EXPORT_SYMBOL(kern_path);
351416f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3515f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
35161da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
35171da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
35181da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
35191da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
35201da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
35211da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
35221da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
35231da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
35241da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
35251da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
35261da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
35271da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
35281da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
35291da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3530