xref: /openbmc/linux/fs/namei.c (revision d18e9008)
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;
6086d7b5aaeSAl Viro 	if (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 {
6167b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
6176d7b5aaeSAl Viro 	int error;
6186d7b5aaeSAl Viro 	char *s;
6191da177e4SLinus Torvalds 
620844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
621844a3917SAl Viro 
6220e794589SAl Viro 	if (link->mnt == nd->path.mnt)
6230e794589SAl Viro 		mntget(link->mnt);
6240e794589SAl Viro 
6256d7b5aaeSAl Viro 	error = -ELOOP;
6266d7b5aaeSAl Viro 	if (unlikely(current->total_link_count >= 40))
6276d7b5aaeSAl Viro 		goto out_put_nd_path;
6286d7b5aaeSAl 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);
6366d7b5aaeSAl Viro 	if (error)
6376d7b5aaeSAl Viro 		goto out_put_nd_path;
63836f3b4f6SAl Viro 
63986acdca1SAl Viro 	nd->last_type = LAST_BIND;
640def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
641def4af30SAl Viro 	error = PTR_ERR(*p);
6426d7b5aaeSAl Viro 	if (IS_ERR(*p))
6436d7b5aaeSAl Viro 		goto out_put_link;
6446d7b5aaeSAl Viro 
645cc314eefSLinus Torvalds 	error = 0;
6466d7b5aaeSAl Viro 	s = nd_get_link(nd);
6476d7b5aaeSAl Viro 	if (s) {
6481da177e4SLinus Torvalds 		error = __vfs_follow_link(nd, s);
6496d7b5aaeSAl Viro 	} else if (nd->last_type == LAST_BIND) {
65016c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
651b21041d0SAl Viro 		nd->inode = nd->path.dentry->d_inode;
652b21041d0SAl Viro 		if (nd->inode->i_op->follow_link) {
653bcda7652SAl Viro 			/* stepped on a _really_ weird one */
654bcda7652SAl Viro 			path_put(&nd->path);
655bcda7652SAl Viro 			error = -ELOOP;
656bcda7652SAl Viro 		}
657bcda7652SAl Viro 	}
6586d7b5aaeSAl Viro 	if (unlikely(error))
6596d7b5aaeSAl Viro 		put_link(nd, link, *p);
6606d7b5aaeSAl Viro 
6616d7b5aaeSAl Viro 	return error;
6626d7b5aaeSAl Viro 
6636d7b5aaeSAl Viro out_put_nd_path:
6646d7b5aaeSAl Viro 	path_put(&nd->path);
6656d7b5aaeSAl Viro out_put_link:
6666d7b5aaeSAl Viro 	path_put(link);
6671da177e4SLinus Torvalds 	return error;
6681da177e4SLinus Torvalds }
6691da177e4SLinus Torvalds 
67031e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
67131e6b01fSNick Piggin {
6720714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
6730714a533SAl Viro 	struct mount *parent;
67431e6b01fSNick Piggin 	struct dentry *mountpoint;
67531e6b01fSNick Piggin 
6760714a533SAl Viro 	parent = mnt->mnt_parent;
6770714a533SAl Viro 	if (&parent->mnt == path->mnt)
67831e6b01fSNick Piggin 		return 0;
679a73324daSAl Viro 	mountpoint = mnt->mnt_mountpoint;
68031e6b01fSNick Piggin 	path->dentry = mountpoint;
6810714a533SAl Viro 	path->mnt = &parent->mnt;
68231e6b01fSNick Piggin 	return 1;
68331e6b01fSNick Piggin }
68431e6b01fSNick Piggin 
685bab77ebfSAl Viro int follow_up(struct path *path)
6861da177e4SLinus Torvalds {
6870714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
6880714a533SAl Viro 	struct mount *parent;
6891da177e4SLinus Torvalds 	struct dentry *mountpoint;
69099b7db7bSNick Piggin 
691962830dfSAndi Kleen 	br_read_lock(&vfsmount_lock);
6920714a533SAl Viro 	parent = mnt->mnt_parent;
6930714a533SAl Viro 	if (&parent->mnt == path->mnt) {
694962830dfSAndi Kleen 		br_read_unlock(&vfsmount_lock);
6951da177e4SLinus Torvalds 		return 0;
6961da177e4SLinus Torvalds 	}
6970714a533SAl Viro 	mntget(&parent->mnt);
698a73324daSAl Viro 	mountpoint = dget(mnt->mnt_mountpoint);
699962830dfSAndi Kleen 	br_read_unlock(&vfsmount_lock);
700bab77ebfSAl Viro 	dput(path->dentry);
701bab77ebfSAl Viro 	path->dentry = mountpoint;
702bab77ebfSAl Viro 	mntput(path->mnt);
7030714a533SAl Viro 	path->mnt = &parent->mnt;
7041da177e4SLinus Torvalds 	return 1;
7051da177e4SLinus Torvalds }
7061da177e4SLinus Torvalds 
707b5c84bf6SNick Piggin /*
7089875cf80SDavid Howells  * Perform an automount
7099875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
7109875cf80SDavid Howells  *   were called with.
7111da177e4SLinus Torvalds  */
7129875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
7139875cf80SDavid Howells 			    bool *need_mntput)
71431e6b01fSNick Piggin {
7159875cf80SDavid Howells 	struct vfsmount *mnt;
716ea5b778aSDavid Howells 	int err;
7179875cf80SDavid Howells 
7189875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
7199875cf80SDavid Howells 		return -EREMOTE;
7209875cf80SDavid Howells 
7210ec26fd0SMiklos Szeredi 	/* We don't want to mount if someone's just doing a stat -
7220ec26fd0SMiklos Szeredi 	 * unless they're stat'ing a directory and appended a '/' to
7230ec26fd0SMiklos Szeredi 	 * the name.
7240ec26fd0SMiklos Szeredi 	 *
7250ec26fd0SMiklos Szeredi 	 * We do, however, want to mount if someone wants to open or
7265a30d8a2SDavid Howells 	 * create a file of any type under the mountpoint, wants to
7270ec26fd0SMiklos Szeredi 	 * traverse through the mountpoint or wants to open the
7280ec26fd0SMiklos Szeredi 	 * mounted directory.  Also, autofs may mark negative dentries
7290ec26fd0SMiklos Szeredi 	 * as being automount points.  These will need the attentions
7300ec26fd0SMiklos Szeredi 	 * of the daemon to instantiate them before they can be used.
7315a30d8a2SDavid Howells 	 */
7325a30d8a2SDavid Howells 	if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
733d94c177bSLinus Torvalds 		     LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
7345a30d8a2SDavid Howells 	    path->dentry->d_inode)
7359875cf80SDavid Howells 		return -EISDIR;
7360ec26fd0SMiklos Szeredi 
7379875cf80SDavid Howells 	current->total_link_count++;
7389875cf80SDavid Howells 	if (current->total_link_count >= 40)
7399875cf80SDavid Howells 		return -ELOOP;
7409875cf80SDavid Howells 
7419875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
7429875cf80SDavid Howells 	if (IS_ERR(mnt)) {
7439875cf80SDavid Howells 		/*
7449875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
7459875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
7469875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
7479875cf80SDavid Howells 		 *
7489875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
7499875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
7509875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
7519875cf80SDavid Howells 		 */
75249084c3bSAl Viro 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
7539875cf80SDavid Howells 			return -EREMOTE;
7549875cf80SDavid Howells 		return PTR_ERR(mnt);
75531e6b01fSNick Piggin 	}
756ea5b778aSDavid Howells 
7579875cf80SDavid Howells 	if (!mnt) /* mount collision */
7589875cf80SDavid Howells 		return 0;
7599875cf80SDavid Howells 
7608aef1884SAl Viro 	if (!*need_mntput) {
7618aef1884SAl Viro 		/* lock_mount() may release path->mnt on error */
7628aef1884SAl Viro 		mntget(path->mnt);
7638aef1884SAl Viro 		*need_mntput = true;
7648aef1884SAl Viro 	}
76519a167afSAl Viro 	err = finish_automount(mnt, path);
766ea5b778aSDavid Howells 
767ea5b778aSDavid Howells 	switch (err) {
768ea5b778aSDavid Howells 	case -EBUSY:
769ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
77019a167afSAl Viro 		return 0;
771ea5b778aSDavid Howells 	case 0:
7728aef1884SAl Viro 		path_put(path);
7739875cf80SDavid Howells 		path->mnt = mnt;
7749875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
7759875cf80SDavid Howells 		return 0;
77619a167afSAl Viro 	default:
77719a167afSAl Viro 		return err;
7789875cf80SDavid Howells 	}
77919a167afSAl Viro 
780ea5b778aSDavid Howells }
7819875cf80SDavid Howells 
7829875cf80SDavid Howells /*
7839875cf80SDavid Howells  * Handle a dentry that is managed in some way.
784cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
7859875cf80SDavid Howells  * - Flagged as mountpoint
7869875cf80SDavid Howells  * - Flagged as automount point
7879875cf80SDavid Howells  *
7889875cf80SDavid Howells  * This may only be called in refwalk mode.
7899875cf80SDavid Howells  *
7909875cf80SDavid Howells  * Serialization is taken care of in namespace.c
7919875cf80SDavid Howells  */
7929875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
7939875cf80SDavid Howells {
7948aef1884SAl Viro 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
7959875cf80SDavid Howells 	unsigned managed;
7969875cf80SDavid Howells 	bool need_mntput = false;
7978aef1884SAl Viro 	int ret = 0;
7989875cf80SDavid Howells 
7999875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
8009875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
8019875cf80SDavid Howells 	 * the components of that value change under us */
8029875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
8039875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
8049875cf80SDavid Howells 	       unlikely(managed != 0)) {
805cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
806cc53ce53SDavid Howells 		 * being held. */
807cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
808cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
809cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
8101aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
811cc53ce53SDavid Howells 			if (ret < 0)
8128aef1884SAl Viro 				break;
813cc53ce53SDavid Howells 		}
814cc53ce53SDavid Howells 
8159875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
8169875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
8179875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
8189875cf80SDavid Howells 			if (mounted) {
8199875cf80SDavid Howells 				dput(path->dentry);
8209875cf80SDavid Howells 				if (need_mntput)
821463ffb2eSAl Viro 					mntput(path->mnt);
822463ffb2eSAl Viro 				path->mnt = mounted;
823463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
8249875cf80SDavid Howells 				need_mntput = true;
8259875cf80SDavid Howells 				continue;
826463ffb2eSAl Viro 			}
827463ffb2eSAl Viro 
8289875cf80SDavid Howells 			/* Something is mounted on this dentry in another
8299875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
8309875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
8319875cf80SDavid Howells 			 * vfsmount_lock */
8321da177e4SLinus Torvalds 		}
8339875cf80SDavid Howells 
8349875cf80SDavid Howells 		/* Handle an automount point */
8359875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
8369875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
8379875cf80SDavid Howells 			if (ret < 0)
8388aef1884SAl Viro 				break;
8399875cf80SDavid Howells 			continue;
8409875cf80SDavid Howells 		}
8419875cf80SDavid Howells 
8429875cf80SDavid Howells 		/* We didn't change the current path point */
8439875cf80SDavid Howells 		break;
8449875cf80SDavid Howells 	}
8458aef1884SAl Viro 
8468aef1884SAl Viro 	if (need_mntput && path->mnt == mnt)
8478aef1884SAl Viro 		mntput(path->mnt);
8488aef1884SAl Viro 	if (ret == -EISDIR)
8498aef1884SAl Viro 		ret = 0;
850a3fbbde7SAl Viro 	return ret < 0 ? ret : need_mntput;
8511da177e4SLinus Torvalds }
8521da177e4SLinus Torvalds 
853cc53ce53SDavid Howells int follow_down_one(struct path *path)
8541da177e4SLinus Torvalds {
8551da177e4SLinus Torvalds 	struct vfsmount *mounted;
8561da177e4SLinus Torvalds 
8571c755af4SAl Viro 	mounted = lookup_mnt(path);
8581da177e4SLinus Torvalds 	if (mounted) {
8599393bd07SAl Viro 		dput(path->dentry);
8609393bd07SAl Viro 		mntput(path->mnt);
8619393bd07SAl Viro 		path->mnt = mounted;
8629393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
8631da177e4SLinus Torvalds 		return 1;
8641da177e4SLinus Torvalds 	}
8651da177e4SLinus Torvalds 	return 0;
8661da177e4SLinus Torvalds }
8671da177e4SLinus Torvalds 
86862a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
86962a7375eSIan Kent {
87062a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
87162a7375eSIan Kent 		dentry->d_op->d_manage(dentry, true) < 0);
87262a7375eSIan Kent }
87362a7375eSIan Kent 
8749875cf80SDavid Howells /*
875287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
876287548e4SAl Viro  * we meet a managed dentry that would need blocking.
8779875cf80SDavid Howells  */
8789875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
879287548e4SAl Viro 			       struct inode **inode)
8809875cf80SDavid Howells {
88162a7375eSIan Kent 	for (;;) {
882c7105365SAl Viro 		struct mount *mounted;
88362a7375eSIan Kent 		/*
88462a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
88562a7375eSIan Kent 		 * that wants to block transit.
88662a7375eSIan Kent 		 */
887287548e4SAl Viro 		if (unlikely(managed_dentry_might_block(path->dentry)))
888ab90911fSDavid Howells 			return false;
88962a7375eSIan Kent 
89062a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
89162a7375eSIan Kent 			break;
89262a7375eSIan Kent 
8939875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
8949875cf80SDavid Howells 		if (!mounted)
8959875cf80SDavid Howells 			break;
896c7105365SAl Viro 		path->mnt = &mounted->mnt;
897c7105365SAl Viro 		path->dentry = mounted->mnt.mnt_root;
898a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
8999875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
90059430262SLinus Torvalds 		/*
90159430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
90259430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
90359430262SLinus Torvalds 		 * because a mount-point is always pinned.
90459430262SLinus Torvalds 		 */
90559430262SLinus Torvalds 		*inode = path->dentry->d_inode;
9069875cf80SDavid Howells 	}
9079875cf80SDavid Howells 	return true;
9089875cf80SDavid Howells }
9099875cf80SDavid Howells 
910dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd)
911287548e4SAl Viro {
912dea39376SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
913c7105365SAl Viro 		struct mount *mounted;
914dea39376SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
915287548e4SAl Viro 		if (!mounted)
916287548e4SAl Viro 			break;
917c7105365SAl Viro 		nd->path.mnt = &mounted->mnt;
918c7105365SAl Viro 		nd->path.dentry = mounted->mnt.mnt_root;
919dea39376SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
920287548e4SAl Viro 	}
921287548e4SAl Viro }
922287548e4SAl Viro 
92331e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
92431e6b01fSNick Piggin {
92531e6b01fSNick Piggin 	set_root_rcu(nd);
92631e6b01fSNick Piggin 
92731e6b01fSNick Piggin 	while (1) {
92831e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
92931e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
93031e6b01fSNick Piggin 			break;
93131e6b01fSNick Piggin 		}
93231e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
93331e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
93431e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
93531e6b01fSNick Piggin 			unsigned seq;
93631e6b01fSNick Piggin 
93731e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
93831e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
939ef7562d5SAl Viro 				goto failed;
94031e6b01fSNick Piggin 			nd->path.dentry = parent;
94131e6b01fSNick Piggin 			nd->seq = seq;
94231e6b01fSNick Piggin 			break;
94331e6b01fSNick Piggin 		}
94431e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
94531e6b01fSNick Piggin 			break;
94631e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
94731e6b01fSNick Piggin 	}
948dea39376SAl Viro 	follow_mount_rcu(nd);
949dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
95031e6b01fSNick Piggin 	return 0;
951ef7562d5SAl Viro 
952ef7562d5SAl Viro failed:
953ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
9545b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
955ef7562d5SAl Viro 		nd->root.mnt = NULL;
956ef7562d5SAl Viro 	rcu_read_unlock();
957962830dfSAndi Kleen 	br_read_unlock(&vfsmount_lock);
958ef7562d5SAl Viro 	return -ECHILD;
95931e6b01fSNick Piggin }
96031e6b01fSNick Piggin 
9619875cf80SDavid Howells /*
962cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
963cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
964cc53ce53SDavid Howells  * caller is permitted to proceed or not.
965cc53ce53SDavid Howells  */
9667cc90cc3SAl Viro int follow_down(struct path *path)
967cc53ce53SDavid Howells {
968cc53ce53SDavid Howells 	unsigned managed;
969cc53ce53SDavid Howells 	int ret;
970cc53ce53SDavid Howells 
971cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
972cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
973cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
974cc53ce53SDavid Howells 		 * being held.
975cc53ce53SDavid Howells 		 *
976cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
977cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
978cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
979cc53ce53SDavid Howells 		 * superstructure.
980cc53ce53SDavid Howells 		 *
981cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
982cc53ce53SDavid Howells 		 */
983cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
984cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
985cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
986ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
9871aed3e42SAl Viro 				path->dentry, false);
988cc53ce53SDavid Howells 			if (ret < 0)
989cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
990cc53ce53SDavid Howells 		}
991cc53ce53SDavid Howells 
992cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
993cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
994cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
995cc53ce53SDavid Howells 			if (!mounted)
996cc53ce53SDavid Howells 				break;
997cc53ce53SDavid Howells 			dput(path->dentry);
998cc53ce53SDavid Howells 			mntput(path->mnt);
999cc53ce53SDavid Howells 			path->mnt = mounted;
1000cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1001cc53ce53SDavid Howells 			continue;
1002cc53ce53SDavid Howells 		}
1003cc53ce53SDavid Howells 
1004cc53ce53SDavid Howells 		/* Don't handle automount points here */
1005cc53ce53SDavid Howells 		break;
1006cc53ce53SDavid Howells 	}
1007cc53ce53SDavid Howells 	return 0;
1008cc53ce53SDavid Howells }
1009cc53ce53SDavid Howells 
1010cc53ce53SDavid Howells /*
10119875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
10129875cf80SDavid Howells  */
10139875cf80SDavid Howells static void follow_mount(struct path *path)
10149875cf80SDavid Howells {
10159875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
10169875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
10179875cf80SDavid Howells 		if (!mounted)
10189875cf80SDavid Howells 			break;
10199875cf80SDavid Howells 		dput(path->dentry);
10209875cf80SDavid Howells 		mntput(path->mnt);
10219875cf80SDavid Howells 		path->mnt = mounted;
10229875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
10239875cf80SDavid Howells 	}
10249875cf80SDavid Howells }
10259875cf80SDavid Howells 
102631e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
10271da177e4SLinus Torvalds {
10282a737871SAl Viro 	set_root(nd);
1029e518ddb7SAndreas Mohr 
10301da177e4SLinus Torvalds 	while(1) {
10314ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
10321da177e4SLinus Torvalds 
10332a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
10342a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
10351da177e4SLinus Torvalds 			break;
10361da177e4SLinus Torvalds 		}
10374ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
10383088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
10393088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
10401da177e4SLinus Torvalds 			dput(old);
10411da177e4SLinus Torvalds 			break;
10421da177e4SLinus Torvalds 		}
10433088dd70SAl Viro 		if (!follow_up(&nd->path))
10441da177e4SLinus Torvalds 			break;
10451da177e4SLinus Torvalds 	}
104679ed0226SAl Viro 	follow_mount(&nd->path);
104731e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
10481da177e4SLinus Torvalds }
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds /*
1051bad61189SMiklos Szeredi  * This looks up the name in dcache, possibly revalidates the old dentry and
1052bad61189SMiklos Szeredi  * allocates a new one if not found or not valid.  In the need_lookup argument
1053bad61189SMiklos Szeredi  * returns whether i_op->lookup is necessary.
1054bad61189SMiklos Szeredi  *
1055bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
1056baa03890SNick Piggin  */
1057bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1058bad61189SMiklos Szeredi 				    struct nameidata *nd, bool *need_lookup)
1059baa03890SNick Piggin {
1060baa03890SNick Piggin 	struct dentry *dentry;
1061bad61189SMiklos Szeredi 	int error;
1062baa03890SNick Piggin 
1063bad61189SMiklos Szeredi 	*need_lookup = false;
1064bad61189SMiklos Szeredi 	dentry = d_lookup(dir, name);
1065bad61189SMiklos Szeredi 	if (dentry) {
1066bad61189SMiklos Szeredi 		if (d_need_lookup(dentry)) {
1067bad61189SMiklos Szeredi 			*need_lookup = true;
1068bad61189SMiklos Szeredi 		} else if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1069bad61189SMiklos Szeredi 			error = d_revalidate(dentry, nd);
1070bad61189SMiklos Szeredi 			if (unlikely(error <= 0)) {
1071bad61189SMiklos Szeredi 				if (error < 0) {
1072bad61189SMiklos Szeredi 					dput(dentry);
1073bad61189SMiklos Szeredi 					return ERR_PTR(error);
1074bad61189SMiklos Szeredi 				} else if (!d_invalidate(dentry)) {
1075bad61189SMiklos Szeredi 					dput(dentry);
1076bad61189SMiklos Szeredi 					dentry = NULL;
1077bad61189SMiklos Szeredi 				}
1078bad61189SMiklos Szeredi 			}
1079bad61189SMiklos Szeredi 		}
1080bad61189SMiklos Szeredi 	}
1081baa03890SNick Piggin 
1082bad61189SMiklos Szeredi 	if (!dentry) {
1083bad61189SMiklos Szeredi 		dentry = d_alloc(dir, name);
1084baa03890SNick Piggin 		if (unlikely(!dentry))
1085baa03890SNick Piggin 			return ERR_PTR(-ENOMEM);
1086baa03890SNick Piggin 
1087bad61189SMiklos Szeredi 		*need_lookup = true;
1088baa03890SNick Piggin 	}
1089baa03890SNick Piggin 	return dentry;
1090baa03890SNick Piggin }
1091baa03890SNick Piggin 
1092baa03890SNick Piggin /*
1093bad61189SMiklos Szeredi  * Call i_op->lookup on the dentry.  The dentry must be negative but may be
1094bad61189SMiklos Szeredi  * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1095bad61189SMiklos Szeredi  *
1096bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
109744396f4bSJosef Bacik  */
1098bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
109944396f4bSJosef Bacik 				  struct nameidata *nd)
110044396f4bSJosef Bacik {
110144396f4bSJosef Bacik 	struct dentry *old;
110244396f4bSJosef Bacik 
110344396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
1104bad61189SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
1105e188dc02SMiklos Szeredi 		dput(dentry);
110644396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
1107e188dc02SMiklos Szeredi 	}
110844396f4bSJosef Bacik 
1109bad61189SMiklos Szeredi 	old = dir->i_op->lookup(dir, dentry, nd);
111044396f4bSJosef Bacik 	if (unlikely(old)) {
111144396f4bSJosef Bacik 		dput(dentry);
111244396f4bSJosef Bacik 		dentry = old;
111344396f4bSJosef Bacik 	}
111444396f4bSJosef Bacik 	return dentry;
111544396f4bSJosef Bacik }
111644396f4bSJosef Bacik 
1117a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name,
1118a3255546SAl Viro 		struct dentry *base, struct nameidata *nd)
1119a3255546SAl Viro {
1120bad61189SMiklos Szeredi 	bool need_lookup;
1121a3255546SAl Viro 	struct dentry *dentry;
1122a3255546SAl Viro 
1123bad61189SMiklos Szeredi 	dentry = lookup_dcache(name, base, nd, &need_lookup);
1124bad61189SMiklos Szeredi 	if (!need_lookup)
1125a3255546SAl Viro 		return dentry;
1126bad61189SMiklos Szeredi 
1127bad61189SMiklos Szeredi 	return lookup_real(base->d_inode, dentry, nd);
1128a3255546SAl Viro }
1129a3255546SAl Viro 
113044396f4bSJosef Bacik /*
11311da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
11321da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
11331da177e4SLinus Torvalds  *  It _is_ time-critical.
11341da177e4SLinus Torvalds  */
1135697f514dSMiklos Szeredi static int lookup_fast(struct nameidata *nd, struct qstr *name,
113631e6b01fSNick Piggin 		       struct path *path, struct inode **inode)
11371da177e4SLinus Torvalds {
11384ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
113931e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
11405a18fff2SAl Viro 	int need_reval = 1;
11415a18fff2SAl Viro 	int status = 1;
11429875cf80SDavid Howells 	int err;
11439875cf80SDavid Howells 
11443cac260aSAl Viro 	/*
1145b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1146b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1147b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1148b04f784eSNick Piggin 	 */
114931e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
115031e6b01fSNick Piggin 		unsigned seq;
115112f8ad4bSLinus Torvalds 		dentry = __d_lookup_rcu(parent, name, &seq, nd->inode);
11525a18fff2SAl Viro 		if (!dentry)
11535a18fff2SAl Viro 			goto unlazy;
11545a18fff2SAl Viro 
115512f8ad4bSLinus Torvalds 		/*
115612f8ad4bSLinus Torvalds 		 * This sequence count validates that the inode matches
115712f8ad4bSLinus Torvalds 		 * the dentry name information from lookup.
115812f8ad4bSLinus Torvalds 		 */
115912f8ad4bSLinus Torvalds 		*inode = dentry->d_inode;
116012f8ad4bSLinus Torvalds 		if (read_seqcount_retry(&dentry->d_seq, seq))
116112f8ad4bSLinus Torvalds 			return -ECHILD;
116212f8ad4bSLinus Torvalds 
116312f8ad4bSLinus Torvalds 		/*
116412f8ad4bSLinus Torvalds 		 * This sequence count validates that the parent had no
116512f8ad4bSLinus Torvalds 		 * changes while we did the lookup of the dentry above.
116612f8ad4bSLinus Torvalds 		 *
116712f8ad4bSLinus Torvalds 		 * The memory barrier in read_seqcount_begin of child is
116812f8ad4bSLinus Torvalds 		 *  enough, we can use __read_seqcount_retry here.
116912f8ad4bSLinus Torvalds 		 */
117031e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
117131e6b01fSNick Piggin 			return -ECHILD;
117231e6b01fSNick Piggin 		nd->seq = seq;
11735a18fff2SAl Viro 
1174fa4ee159SMiklos Szeredi 		if (unlikely(d_need_lookup(dentry)))
1175fa4ee159SMiklos Szeredi 			goto unlazy;
117624643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
11775a18fff2SAl Viro 			status = d_revalidate(dentry, nd);
11785a18fff2SAl Viro 			if (unlikely(status <= 0)) {
11795a18fff2SAl Viro 				if (status != -ECHILD)
11805a18fff2SAl Viro 					need_reval = 0;
11815a18fff2SAl Viro 				goto unlazy;
11825a18fff2SAl Viro 			}
118324643087SAl Viro 		}
118431e6b01fSNick Piggin 		path->mnt = mnt;
118531e6b01fSNick Piggin 		path->dentry = dentry;
1186d6e9bd25SAl Viro 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1187d6e9bd25SAl Viro 			goto unlazy;
1188d6e9bd25SAl Viro 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1189d6e9bd25SAl Viro 			goto unlazy;
11909875cf80SDavid Howells 		return 0;
11915a18fff2SAl Viro unlazy:
119219660af7SAl Viro 		if (unlazy_walk(nd, dentry))
11935a18fff2SAl Viro 			return -ECHILD;
11945a18fff2SAl Viro 	} else {
119531e6b01fSNick Piggin 		dentry = __d_lookup(parent, name);
119624643087SAl Viro 	}
11975a18fff2SAl Viro 
119881e6f520SAl Viro 	if (unlikely(!dentry))
119981e6f520SAl Viro 		goto need_lookup;
12005a18fff2SAl Viro 
120181e6f520SAl Viro 	if (unlikely(d_need_lookup(dentry))) {
120281e6f520SAl Viro 		dput(dentry);
120381e6f520SAl Viro 		goto need_lookup;
12045a18fff2SAl Viro 	}
120581e6f520SAl Viro 
12065a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
12075a18fff2SAl Viro 		status = d_revalidate(dentry, nd);
12085a18fff2SAl Viro 	if (unlikely(status <= 0)) {
12095a18fff2SAl Viro 		if (status < 0) {
12105a18fff2SAl Viro 			dput(dentry);
12115a18fff2SAl Viro 			return status;
12125a18fff2SAl Viro 		}
12135a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
12145a18fff2SAl Viro 			dput(dentry);
121581e6f520SAl Viro 			goto need_lookup;
12165a18fff2SAl Viro 		}
12175a18fff2SAl Viro 	}
1218697f514dSMiklos Szeredi 
12191da177e4SLinus Torvalds 	path->mnt = mnt;
12201da177e4SLinus Torvalds 	path->dentry = dentry;
12219875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
122289312214SIan Kent 	if (unlikely(err < 0)) {
122389312214SIan Kent 		path_put_conditional(path, nd);
12249875cf80SDavid Howells 		return err;
122589312214SIan Kent 	}
1226a3fbbde7SAl Viro 	if (err)
1227a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
122831e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
12291da177e4SLinus Torvalds 	return 0;
123081e6f520SAl Viro 
123181e6f520SAl Viro need_lookup:
1232697f514dSMiklos Szeredi 	return 1;
1233697f514dSMiklos Szeredi }
1234697f514dSMiklos Szeredi 
1235697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */
1236697f514dSMiklos Szeredi static int lookup_slow(struct nameidata *nd, struct qstr *name,
1237697f514dSMiklos Szeredi 		       struct path *path)
1238697f514dSMiklos Szeredi {
1239697f514dSMiklos Szeredi 	struct dentry *dentry, *parent;
1240697f514dSMiklos Szeredi 	int err;
1241697f514dSMiklos Szeredi 
1242697f514dSMiklos Szeredi 	parent = nd->path.dentry;
124381e6f520SAl Viro 	BUG_ON(nd->inode != parent->d_inode);
124481e6f520SAl Viro 
124581e6f520SAl Viro 	mutex_lock(&parent->d_inode->i_mutex);
124681e6f520SAl Viro 	dentry = __lookup_hash(name, parent, nd);
124781e6f520SAl Viro 	mutex_unlock(&parent->d_inode->i_mutex);
124881e6f520SAl Viro 	if (IS_ERR(dentry))
124981e6f520SAl Viro 		return PTR_ERR(dentry);
1250697f514dSMiklos Szeredi 	path->mnt = nd->path.mnt;
1251697f514dSMiklos Szeredi 	path->dentry = dentry;
1252697f514dSMiklos Szeredi 	err = follow_managed(path, nd->flags);
1253697f514dSMiklos Szeredi 	if (unlikely(err < 0)) {
1254697f514dSMiklos Szeredi 		path_put_conditional(path, nd);
1255697f514dSMiklos Szeredi 		return err;
1256697f514dSMiklos Szeredi 	}
1257697f514dSMiklos Szeredi 	if (err)
1258697f514dSMiklos Szeredi 		nd->flags |= LOOKUP_JUMPED;
1259697f514dSMiklos Szeredi 	return 0;
12601da177e4SLinus Torvalds }
12611da177e4SLinus Torvalds 
126252094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
126352094c8aSAl Viro {
126452094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
12654ad5abb3SAl Viro 		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
126652094c8aSAl Viro 		if (err != -ECHILD)
126752094c8aSAl Viro 			return err;
126819660af7SAl Viro 		if (unlazy_walk(nd, NULL))
126952094c8aSAl Viro 			return -ECHILD;
127052094c8aSAl Viro 	}
12714ad5abb3SAl Viro 	return inode_permission(nd->inode, MAY_EXEC);
127252094c8aSAl Viro }
127352094c8aSAl Viro 
12749856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
12759856fa1bSAl Viro {
12769856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
12779856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
12789856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
12799856fa1bSAl Viro 				return -ECHILD;
12809856fa1bSAl Viro 		} else
12819856fa1bSAl Viro 			follow_dotdot(nd);
12829856fa1bSAl Viro 	}
12839856fa1bSAl Viro 	return 0;
12849856fa1bSAl Viro }
12859856fa1bSAl Viro 
1286951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1287951361f9SAl Viro {
1288951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1289951361f9SAl Viro 		path_put(&nd->path);
1290951361f9SAl Viro 	} else {
1291951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
12925b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1293951361f9SAl Viro 			nd->root.mnt = NULL;
1294951361f9SAl Viro 		rcu_read_unlock();
1295962830dfSAndi Kleen 		br_read_unlock(&vfsmount_lock);
1296951361f9SAl Viro 	}
1297951361f9SAl Viro }
1298951361f9SAl Viro 
12993ddcd056SLinus Torvalds /*
13003ddcd056SLinus Torvalds  * Do we need to follow links? We _really_ want to be able
13013ddcd056SLinus Torvalds  * to do this check without having to look at inode->i_op,
13023ddcd056SLinus Torvalds  * so we keep a cache of "no, this doesn't need follow_link"
13033ddcd056SLinus Torvalds  * for the common case.
13043ddcd056SLinus Torvalds  */
13057813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow)
13063ddcd056SLinus Torvalds {
13073ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
13083ddcd056SLinus Torvalds 		if (likely(inode->i_op->follow_link))
13093ddcd056SLinus Torvalds 			return follow;
13103ddcd056SLinus Torvalds 
13113ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
13123ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
13133ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_NOFOLLOW;
13143ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
13153ddcd056SLinus Torvalds 	}
13163ddcd056SLinus Torvalds 	return 0;
13173ddcd056SLinus Torvalds }
13183ddcd056SLinus Torvalds 
1319ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
1320ce57dfc1SAl Viro 		struct qstr *name, int type, int follow)
1321ce57dfc1SAl Viro {
1322ce57dfc1SAl Viro 	struct inode *inode;
1323ce57dfc1SAl Viro 	int err;
1324ce57dfc1SAl Viro 	/*
1325ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1326ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1327ce57dfc1SAl Viro 	 * parent relationships.
1328ce57dfc1SAl Viro 	 */
1329ce57dfc1SAl Viro 	if (unlikely(type != LAST_NORM))
1330ce57dfc1SAl Viro 		return handle_dots(nd, type);
1331697f514dSMiklos Szeredi 	err = lookup_fast(nd, name, path, &inode);
1332ce57dfc1SAl Viro 	if (unlikely(err)) {
1333697f514dSMiklos Szeredi 		if (err < 0)
1334697f514dSMiklos Szeredi 			goto out_err;
1335697f514dSMiklos Szeredi 
1336697f514dSMiklos Szeredi 		err = lookup_slow(nd, name, path);
1337697f514dSMiklos Szeredi 		if (err < 0)
1338697f514dSMiklos Szeredi 			goto out_err;
1339697f514dSMiklos Szeredi 
1340697f514dSMiklos Szeredi 		inode = path->dentry->d_inode;
1341ce57dfc1SAl Viro 	}
1342697f514dSMiklos Szeredi 	err = -ENOENT;
1343697f514dSMiklos Szeredi 	if (!inode)
1344697f514dSMiklos Szeredi 		goto out_path_put;
1345697f514dSMiklos Szeredi 
13467813b94aSLinus Torvalds 	if (should_follow_link(inode, follow)) {
134719660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
134819660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
1349697f514dSMiklos Szeredi 				err = -ECHILD;
1350697f514dSMiklos Szeredi 				goto out_err;
135119660af7SAl Viro 			}
135219660af7SAl Viro 		}
1353ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1354ce57dfc1SAl Viro 		return 1;
1355ce57dfc1SAl Viro 	}
1356ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1357ce57dfc1SAl Viro 	nd->inode = inode;
1358ce57dfc1SAl Viro 	return 0;
1359697f514dSMiklos Szeredi 
1360697f514dSMiklos Szeredi out_path_put:
1361697f514dSMiklos Szeredi 	path_to_nameidata(path, nd);
1362697f514dSMiklos Szeredi out_err:
1363697f514dSMiklos Szeredi 	terminate_walk(nd);
1364697f514dSMiklos Szeredi 	return err;
1365ce57dfc1SAl Viro }
1366ce57dfc1SAl Viro 
13671da177e4SLinus Torvalds /*
1368b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1369b356379aSAl Viro  * limiting consecutive symlinks to 40.
1370b356379aSAl Viro  *
1371b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1372b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1373b356379aSAl Viro  */
1374b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1375b356379aSAl Viro {
1376b356379aSAl Viro 	int res;
1377b356379aSAl Viro 
1378b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1379b356379aSAl Viro 		path_put_conditional(path, nd);
1380b356379aSAl Viro 		path_put(&nd->path);
1381b356379aSAl Viro 		return -ELOOP;
1382b356379aSAl Viro 	}
13831a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1384b356379aSAl Viro 
1385b356379aSAl Viro 	nd->depth++;
1386b356379aSAl Viro 	current->link_count++;
1387b356379aSAl Viro 
1388b356379aSAl Viro 	do {
1389b356379aSAl Viro 		struct path link = *path;
1390b356379aSAl Viro 		void *cookie;
1391574197e0SAl Viro 
1392574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
13936d7b5aaeSAl Viro 		if (res)
13946d7b5aaeSAl Viro 			break;
1395b356379aSAl Viro 		res = walk_component(nd, path, &nd->last,
1396b356379aSAl Viro 				     nd->last_type, LOOKUP_FOLLOW);
1397574197e0SAl Viro 		put_link(nd, &link, cookie);
1398b356379aSAl Viro 	} while (res > 0);
1399b356379aSAl Viro 
1400b356379aSAl Viro 	current->link_count--;
1401b356379aSAl Viro 	nd->depth--;
1402b356379aSAl Viro 	return res;
1403b356379aSAl Viro }
1404b356379aSAl Viro 
1405b356379aSAl Viro /*
14063ddcd056SLinus Torvalds  * We really don't want to look at inode->i_op->lookup
14073ddcd056SLinus Torvalds  * when we don't have to. So we keep a cache bit in
14083ddcd056SLinus Torvalds  * the inode ->i_opflags field that says "yes, we can
14093ddcd056SLinus Torvalds  * do lookup on this inode".
14103ddcd056SLinus Torvalds  */
14113ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode)
14123ddcd056SLinus Torvalds {
14133ddcd056SLinus Torvalds 	if (likely(inode->i_opflags & IOP_LOOKUP))
14143ddcd056SLinus Torvalds 		return 1;
14153ddcd056SLinus Torvalds 	if (likely(!inode->i_op->lookup))
14163ddcd056SLinus Torvalds 		return 0;
14173ddcd056SLinus Torvalds 
14183ddcd056SLinus Torvalds 	/* We do this once for the lifetime of the inode */
14193ddcd056SLinus Torvalds 	spin_lock(&inode->i_lock);
14203ddcd056SLinus Torvalds 	inode->i_opflags |= IOP_LOOKUP;
14213ddcd056SLinus Torvalds 	spin_unlock(&inode->i_lock);
14223ddcd056SLinus Torvalds 	return 1;
14233ddcd056SLinus Torvalds }
14243ddcd056SLinus Torvalds 
1425bfcfaa77SLinus Torvalds /*
1426bfcfaa77SLinus Torvalds  * We can do the critical dentry name comparison and hashing
1427bfcfaa77SLinus Torvalds  * operations one word at a time, but we are limited to:
1428bfcfaa77SLinus Torvalds  *
1429bfcfaa77SLinus Torvalds  * - Architectures with fast unaligned word accesses. We could
1430bfcfaa77SLinus Torvalds  *   do a "get_unaligned()" if this helps and is sufficiently
1431bfcfaa77SLinus Torvalds  *   fast.
1432bfcfaa77SLinus Torvalds  *
1433bfcfaa77SLinus Torvalds  * - Little-endian machines (so that we can generate the mask
1434bfcfaa77SLinus Torvalds  *   of low bytes efficiently). Again, we *could* do a byte
1435bfcfaa77SLinus Torvalds  *   swapping load on big-endian architectures if that is not
1436bfcfaa77SLinus Torvalds  *   expensive enough to make the optimization worthless.
1437bfcfaa77SLinus Torvalds  *
1438bfcfaa77SLinus Torvalds  * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1439bfcfaa77SLinus Torvalds  *   do not trap on the (extremely unlikely) case of a page
1440bfcfaa77SLinus Torvalds  *   crossing operation.
1441bfcfaa77SLinus Torvalds  *
1442bfcfaa77SLinus Torvalds  * - Furthermore, we need an efficient 64-bit compile for the
1443bfcfaa77SLinus Torvalds  *   64-bit case in order to generate the "number of bytes in
1444bfcfaa77SLinus Torvalds  *   the final mask". Again, that could be replaced with a
1445bfcfaa77SLinus Torvalds  *   efficient population count instruction or similar.
1446bfcfaa77SLinus Torvalds  */
1447bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS
1448bfcfaa77SLinus Torvalds 
1449f68e556eSLinus Torvalds #include <asm/word-at-a-time.h>
1450bfcfaa77SLinus Torvalds 
1451f68e556eSLinus Torvalds #ifdef CONFIG_64BIT
1452bfcfaa77SLinus Torvalds 
1453bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash)
1454bfcfaa77SLinus Torvalds {
1455bfcfaa77SLinus Torvalds 	hash += hash >> (8*sizeof(int));
1456bfcfaa77SLinus Torvalds 	return hash;
1457bfcfaa77SLinus Torvalds }
1458bfcfaa77SLinus Torvalds 
1459bfcfaa77SLinus Torvalds #else	/* 32-bit case */
1460bfcfaa77SLinus Torvalds 
1461bfcfaa77SLinus Torvalds #define fold_hash(x) (x)
1462bfcfaa77SLinus Torvalds 
1463bfcfaa77SLinus Torvalds #endif
1464bfcfaa77SLinus Torvalds 
1465bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1466bfcfaa77SLinus Torvalds {
1467bfcfaa77SLinus Torvalds 	unsigned long a, mask;
1468bfcfaa77SLinus Torvalds 	unsigned long hash = 0;
1469bfcfaa77SLinus Torvalds 
1470bfcfaa77SLinus Torvalds 	for (;;) {
1471e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name);
1472bfcfaa77SLinus Torvalds 		if (len < sizeof(unsigned long))
1473bfcfaa77SLinus Torvalds 			break;
1474bfcfaa77SLinus Torvalds 		hash += a;
1475f132c5beSAl Viro 		hash *= 9;
1476bfcfaa77SLinus Torvalds 		name += sizeof(unsigned long);
1477bfcfaa77SLinus Torvalds 		len -= sizeof(unsigned long);
1478bfcfaa77SLinus Torvalds 		if (!len)
1479bfcfaa77SLinus Torvalds 			goto done;
1480bfcfaa77SLinus Torvalds 	}
1481bfcfaa77SLinus Torvalds 	mask = ~(~0ul << len*8);
1482bfcfaa77SLinus Torvalds 	hash += mask & a;
1483bfcfaa77SLinus Torvalds done:
1484bfcfaa77SLinus Torvalds 	return fold_hash(hash);
1485bfcfaa77SLinus Torvalds }
1486bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
1487bfcfaa77SLinus Torvalds 
1488bfcfaa77SLinus Torvalds /*
1489bfcfaa77SLinus Torvalds  * Calculate the length and hash of the path component, and
1490bfcfaa77SLinus Torvalds  * return the length of the component;
1491bfcfaa77SLinus Torvalds  */
1492bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1493bfcfaa77SLinus Torvalds {
149436126f8fSLinus Torvalds 	unsigned long a, b, adata, bdata, mask, hash, len;
149536126f8fSLinus Torvalds 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1496bfcfaa77SLinus Torvalds 
1497bfcfaa77SLinus Torvalds 	hash = a = 0;
1498bfcfaa77SLinus Torvalds 	len = -sizeof(unsigned long);
1499bfcfaa77SLinus Torvalds 	do {
1500bfcfaa77SLinus Torvalds 		hash = (hash + a) * 9;
1501bfcfaa77SLinus Torvalds 		len += sizeof(unsigned long);
1502e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name+len);
150336126f8fSLinus Torvalds 		b = a ^ REPEAT_BYTE('/');
150436126f8fSLinus Torvalds 	} while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1505bfcfaa77SLinus Torvalds 
150636126f8fSLinus Torvalds 	adata = prep_zero_mask(a, adata, &constants);
150736126f8fSLinus Torvalds 	bdata = prep_zero_mask(b, bdata, &constants);
150836126f8fSLinus Torvalds 
150936126f8fSLinus Torvalds 	mask = create_zero_mask(adata | bdata);
151036126f8fSLinus Torvalds 
151136126f8fSLinus Torvalds 	hash += a & zero_bytemask(mask);
1512bfcfaa77SLinus Torvalds 	*hashp = fold_hash(hash);
1513bfcfaa77SLinus Torvalds 
151436126f8fSLinus Torvalds 	return len + find_zero(mask);
1515bfcfaa77SLinus Torvalds }
1516bfcfaa77SLinus Torvalds 
1517bfcfaa77SLinus Torvalds #else
1518bfcfaa77SLinus Torvalds 
15190145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
15200145acc2SLinus Torvalds {
15210145acc2SLinus Torvalds 	unsigned long hash = init_name_hash();
15220145acc2SLinus Torvalds 	while (len--)
15230145acc2SLinus Torvalds 		hash = partial_name_hash(*name++, hash);
15240145acc2SLinus Torvalds 	return end_name_hash(hash);
15250145acc2SLinus Torvalds }
1526ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
15270145acc2SLinus Torvalds 
15283ddcd056SLinus Torvalds /*
1529200e9ef7SLinus Torvalds  * We know there's a real path component here of at least
1530200e9ef7SLinus Torvalds  * one character.
1531200e9ef7SLinus Torvalds  */
1532200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1533200e9ef7SLinus Torvalds {
1534200e9ef7SLinus Torvalds 	unsigned long hash = init_name_hash();
1535200e9ef7SLinus Torvalds 	unsigned long len = 0, c;
1536200e9ef7SLinus Torvalds 
1537200e9ef7SLinus Torvalds 	c = (unsigned char)*name;
1538200e9ef7SLinus Torvalds 	do {
1539200e9ef7SLinus Torvalds 		len++;
1540200e9ef7SLinus Torvalds 		hash = partial_name_hash(c, hash);
1541200e9ef7SLinus Torvalds 		c = (unsigned char)name[len];
1542200e9ef7SLinus Torvalds 	} while (c && c != '/');
1543200e9ef7SLinus Torvalds 	*hashp = end_name_hash(hash);
1544200e9ef7SLinus Torvalds 	return len;
1545200e9ef7SLinus Torvalds }
1546200e9ef7SLinus Torvalds 
1547bfcfaa77SLinus Torvalds #endif
1548bfcfaa77SLinus Torvalds 
1549200e9ef7SLinus Torvalds /*
15501da177e4SLinus Torvalds  * Name resolution.
1551ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1552ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
15531da177e4SLinus Torvalds  *
1554ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1555ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
15561da177e4SLinus Torvalds  */
15576de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
15581da177e4SLinus Torvalds {
15591da177e4SLinus Torvalds 	struct path next;
15601da177e4SLinus Torvalds 	int err;
15611da177e4SLinus Torvalds 
15621da177e4SLinus Torvalds 	while (*name=='/')
15631da177e4SLinus Torvalds 		name++;
15641da177e4SLinus Torvalds 	if (!*name)
1565086e183aSAl Viro 		return 0;
15661da177e4SLinus Torvalds 
15671da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
15681da177e4SLinus Torvalds 	for(;;) {
15691da177e4SLinus Torvalds 		struct qstr this;
1570200e9ef7SLinus Torvalds 		long len;
1571fe479a58SAl Viro 		int type;
15721da177e4SLinus Torvalds 
157352094c8aSAl Viro 		err = may_lookup(nd);
15741da177e4SLinus Torvalds  		if (err)
15751da177e4SLinus Torvalds 			break;
15761da177e4SLinus Torvalds 
1577200e9ef7SLinus Torvalds 		len = hash_name(name, &this.hash);
15781da177e4SLinus Torvalds 		this.name = name;
1579200e9ef7SLinus Torvalds 		this.len = len;
15801da177e4SLinus Torvalds 
1581fe479a58SAl Viro 		type = LAST_NORM;
1582200e9ef7SLinus Torvalds 		if (name[0] == '.') switch (len) {
1583fe479a58SAl Viro 			case 2:
1584200e9ef7SLinus Torvalds 				if (name[1] == '.') {
1585fe479a58SAl Viro 					type = LAST_DOTDOT;
158616c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
158716c2cd71SAl Viro 				}
1588fe479a58SAl Viro 				break;
1589fe479a58SAl Viro 			case 1:
1590fe479a58SAl Viro 				type = LAST_DOT;
1591fe479a58SAl Viro 		}
15925a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
15935a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
159416c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
15955a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
15965a202bcdSAl Viro 				err = parent->d_op->d_hash(parent, nd->inode,
15975a202bcdSAl Viro 							   &this);
15985a202bcdSAl Viro 				if (err < 0)
15995a202bcdSAl Viro 					break;
16005a202bcdSAl Viro 			}
16015a202bcdSAl Viro 		}
1602fe479a58SAl Viro 
1603200e9ef7SLinus Torvalds 		if (!name[len])
16041da177e4SLinus Torvalds 			goto last_component;
1605200e9ef7SLinus Torvalds 		/*
1606200e9ef7SLinus Torvalds 		 * If it wasn't NUL, we know it was '/'. Skip that
1607200e9ef7SLinus Torvalds 		 * slash, and continue until no more slashes.
1608200e9ef7SLinus Torvalds 		 */
1609200e9ef7SLinus Torvalds 		do {
1610200e9ef7SLinus Torvalds 			len++;
1611200e9ef7SLinus Torvalds 		} while (unlikely(name[len] == '/'));
1612200e9ef7SLinus Torvalds 		if (!name[len])
1613b356379aSAl Viro 			goto last_component;
1614200e9ef7SLinus Torvalds 		name += len;
16151da177e4SLinus Torvalds 
1616ce57dfc1SAl Viro 		err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1617ce57dfc1SAl Viro 		if (err < 0)
1618ce57dfc1SAl Viro 			return err;
1619fe479a58SAl Viro 
1620ce57dfc1SAl Viro 		if (err) {
1621b356379aSAl Viro 			err = nested_symlink(&next, nd);
16221da177e4SLinus Torvalds 			if (err)
1623a7472babSAl Viro 				return err;
162431e6b01fSNick Piggin 		}
16253ddcd056SLinus Torvalds 		if (can_lookup(nd->inode))
16261da177e4SLinus Torvalds 			continue;
16273ddcd056SLinus Torvalds 		err = -ENOTDIR;
16283ddcd056SLinus Torvalds 		break;
16291da177e4SLinus Torvalds 		/* here ends the main loop */
16301da177e4SLinus Torvalds 
16311da177e4SLinus Torvalds last_component:
1632ce57dfc1SAl Viro 		nd->last = this;
1633ce57dfc1SAl Viro 		nd->last_type = type;
1634ce57dfc1SAl Viro 		return 0;
1635ce57dfc1SAl Viro 	}
1636951361f9SAl Viro 	terminate_walk(nd);
16371da177e4SLinus Torvalds 	return err;
16381da177e4SLinus Torvalds }
16391da177e4SLinus Torvalds 
164070e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
164170e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
164231e6b01fSNick Piggin {
164331e6b01fSNick Piggin 	int retval = 0;
164431e6b01fSNick Piggin 	int fput_needed;
164531e6b01fSNick Piggin 	struct file *file;
164631e6b01fSNick Piggin 
164731e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
164816c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
164931e6b01fSNick Piggin 	nd->depth = 0;
16505b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
16515b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
165273d049a4SAl Viro 		if (*name) {
16535b6ca027SAl Viro 			if (!inode->i_op->lookup)
16545b6ca027SAl Viro 				return -ENOTDIR;
16555b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
16565b6ca027SAl Viro 			if (retval)
16575b6ca027SAl Viro 				return retval;
165873d049a4SAl Viro 		}
16595b6ca027SAl Viro 		nd->path = nd->root;
16605b6ca027SAl Viro 		nd->inode = inode;
16615b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
1662962830dfSAndi Kleen 			br_read_lock(&vfsmount_lock);
16635b6ca027SAl Viro 			rcu_read_lock();
16645b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
16655b6ca027SAl Viro 		} else {
16665b6ca027SAl Viro 			path_get(&nd->path);
16675b6ca027SAl Viro 		}
16685b6ca027SAl Viro 		return 0;
16695b6ca027SAl Viro 	}
16705b6ca027SAl Viro 
167131e6b01fSNick Piggin 	nd->root.mnt = NULL;
167231e6b01fSNick Piggin 
167331e6b01fSNick Piggin 	if (*name=='/') {
1674e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
1675962830dfSAndi Kleen 			br_read_lock(&vfsmount_lock);
167631e6b01fSNick Piggin 			rcu_read_lock();
1677e41f7d4eSAl Viro 			set_root_rcu(nd);
1678e41f7d4eSAl Viro 		} else {
1679e41f7d4eSAl Viro 			set_root(nd);
1680e41f7d4eSAl Viro 			path_get(&nd->root);
1681e41f7d4eSAl Viro 		}
168231e6b01fSNick Piggin 		nd->path = nd->root;
168331e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1684e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
168531e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1686c28cc364SNick Piggin 			unsigned seq;
168731e6b01fSNick Piggin 
1688962830dfSAndi Kleen 			br_read_lock(&vfsmount_lock);
168931e6b01fSNick Piggin 			rcu_read_lock();
169031e6b01fSNick Piggin 
1691c28cc364SNick Piggin 			do {
1692c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
169331e6b01fSNick Piggin 				nd->path = fs->pwd;
1694c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1695c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1696e41f7d4eSAl Viro 		} else {
1697e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1698e41f7d4eSAl Viro 		}
169931e6b01fSNick Piggin 	} else {
170031e6b01fSNick Piggin 		struct dentry *dentry;
170131e6b01fSNick Piggin 
17021abf0c71SAl Viro 		file = fget_raw_light(dfd, &fput_needed);
170331e6b01fSNick Piggin 		retval = -EBADF;
170431e6b01fSNick Piggin 		if (!file)
170531e6b01fSNick Piggin 			goto out_fail;
170631e6b01fSNick Piggin 
170731e6b01fSNick Piggin 		dentry = file->f_path.dentry;
170831e6b01fSNick Piggin 
1709f52e0c11SAl Viro 		if (*name) {
171031e6b01fSNick Piggin 			retval = -ENOTDIR;
171131e6b01fSNick Piggin 			if (!S_ISDIR(dentry->d_inode->i_mode))
171231e6b01fSNick Piggin 				goto fput_fail;
171331e6b01fSNick Piggin 
17144ad5abb3SAl Viro 			retval = inode_permission(dentry->d_inode, MAY_EXEC);
171531e6b01fSNick Piggin 			if (retval)
171631e6b01fSNick Piggin 				goto fput_fail;
1717f52e0c11SAl Viro 		}
171831e6b01fSNick Piggin 
171931e6b01fSNick Piggin 		nd->path = file->f_path;
1720e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
172131e6b01fSNick Piggin 			if (fput_needed)
172270e9b357SAl Viro 				*fp = file;
1723c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1724962830dfSAndi Kleen 			br_read_lock(&vfsmount_lock);
172531e6b01fSNick Piggin 			rcu_read_lock();
17265590ff0dSUlrich Drepper 		} else {
17275dd784d0SJan Blunck 			path_get(&file->f_path);
17285590ff0dSUlrich Drepper 			fput_light(file, fput_needed);
17291da177e4SLinus Torvalds 		}
1730e41f7d4eSAl Viro 	}
1731e41f7d4eSAl Viro 
173231e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
17339b4a9b14SAl Viro 	return 0;
17342dfdd266SJosef 'Jeff' Sipek 
17359b4a9b14SAl Viro fput_fail:
17369b4a9b14SAl Viro 	fput_light(file, fput_needed);
17379b4a9b14SAl Viro out_fail:
17389b4a9b14SAl Viro 	return retval;
17399b4a9b14SAl Viro }
17409b4a9b14SAl Viro 
1741bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1742bd92d7feSAl Viro {
1743bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1744bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1745bd92d7feSAl Viro 
1746bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
1747bd92d7feSAl Viro 	return walk_component(nd, path, &nd->last, nd->last_type,
1748bd92d7feSAl Viro 					nd->flags & LOOKUP_FOLLOW);
1749bd92d7feSAl Viro }
1750bd92d7feSAl Viro 
17519b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1752ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
17539b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
17549b4a9b14SAl Viro {
175570e9b357SAl Viro 	struct file *base = NULL;
1756bd92d7feSAl Viro 	struct path path;
1757bd92d7feSAl Viro 	int err;
175831e6b01fSNick Piggin 
175931e6b01fSNick Piggin 	/*
176031e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
176131e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
176231e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
176331e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
176431e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
176531e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
176631e6b01fSNick Piggin 	 * analogue, foo_rcu().
176731e6b01fSNick Piggin 	 *
176831e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
176931e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
177031e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
177131e6b01fSNick Piggin 	 * be able to complete).
177231e6b01fSNick Piggin 	 */
1773bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1774ee0827cdSAl Viro 
1775bd92d7feSAl Viro 	if (unlikely(err))
1776bd92d7feSAl Viro 		return err;
1777ee0827cdSAl Viro 
1778ee0827cdSAl Viro 	current->total_link_count = 0;
1779bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1780bd92d7feSAl Viro 
1781bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1782bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1783bd92d7feSAl Viro 		while (err > 0) {
1784bd92d7feSAl Viro 			void *cookie;
1785bd92d7feSAl Viro 			struct path link = path;
1786bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1787574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
17886d7b5aaeSAl Viro 			if (err)
17896d7b5aaeSAl Viro 				break;
1790bd92d7feSAl Viro 			err = lookup_last(nd, &path);
1791574197e0SAl Viro 			put_link(nd, &link, cookie);
1792bd92d7feSAl Viro 		}
1793bd92d7feSAl Viro 	}
1794ee0827cdSAl Viro 
17959f1fafeeSAl Viro 	if (!err)
17969f1fafeeSAl Viro 		err = complete_walk(nd);
1797bd92d7feSAl Viro 
1798bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
1799bd92d7feSAl Viro 		if (!nd->inode->i_op->lookup) {
1800bd92d7feSAl Viro 			path_put(&nd->path);
1801bd23a539SAl Viro 			err = -ENOTDIR;
1802bd92d7feSAl Viro 		}
1803bd92d7feSAl Viro 	}
180416c2cd71SAl Viro 
180570e9b357SAl Viro 	if (base)
180670e9b357SAl Viro 		fput(base);
1807ee0827cdSAl Viro 
18085b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
180931e6b01fSNick Piggin 		path_put(&nd->root);
181031e6b01fSNick Piggin 		nd->root.mnt = NULL;
181131e6b01fSNick Piggin 	}
1812bd92d7feSAl Viro 	return err;
181331e6b01fSNick Piggin }
181431e6b01fSNick Piggin 
1815ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1816ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1817ee0827cdSAl Viro {
1818ee0827cdSAl Viro 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1819ee0827cdSAl Viro 	if (unlikely(retval == -ECHILD))
1820ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags, nd);
1821ee0827cdSAl Viro 	if (unlikely(retval == -ESTALE))
1822ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1823ee0827cdSAl Viro 
182431e6b01fSNick Piggin 	if (likely(!retval)) {
182531e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
182631e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
182731e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
182831e6b01fSNick Piggin 		}
182931e6b01fSNick Piggin 	}
1830170aa3d0SUlrich Drepper 	return retval;
18311da177e4SLinus Torvalds }
18321da177e4SLinus Torvalds 
1833c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd)
18345590ff0dSUlrich Drepper {
1835c9c6cac0SAl Viro 	return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
18365590ff0dSUlrich Drepper }
18375590ff0dSUlrich Drepper 
1838d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1839d1811465SAl Viro {
1840d1811465SAl Viro 	struct nameidata nd;
1841d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1842d1811465SAl Viro 	if (!res)
1843d1811465SAl Viro 		*path = nd.path;
1844d1811465SAl Viro 	return res;
1845d1811465SAl Viro }
1846d1811465SAl Viro 
184716f18200SJosef 'Jeff' Sipek /**
184816f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
184916f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
185016f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
185116f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
185216f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
1853e0a01249SAl Viro  * @path: pointer to struct path to fill
185416f18200SJosef 'Jeff' Sipek  */
185516f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
185616f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
1857e0a01249SAl Viro 		    struct path *path)
185816f18200SJosef 'Jeff' Sipek {
1859e0a01249SAl Viro 	struct nameidata nd;
1860e0a01249SAl Viro 	int err;
1861e0a01249SAl Viro 	nd.root.dentry = dentry;
1862e0a01249SAl Viro 	nd.root.mnt = mnt;
1863e0a01249SAl Viro 	BUG_ON(flags & LOOKUP_PARENT);
18645b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1865e0a01249SAl Viro 	err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1866e0a01249SAl Viro 	if (!err)
1867e0a01249SAl Viro 		*path = nd.path;
1868e0a01249SAl Viro 	return err;
186916f18200SJosef 'Jeff' Sipek }
187016f18200SJosef 'Jeff' Sipek 
1871057f6c01SJames Morris /*
1872057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1873057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1874057f6c01SJames Morris  * SMP-safe.
1875057f6c01SJames Morris  */
1876a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
18771da177e4SLinus Torvalds {
18784ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
18791da177e4SLinus Torvalds }
18801da177e4SLinus Torvalds 
1881eead1911SChristoph Hellwig /**
1882a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1883eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1884eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1885eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1886eead1911SChristoph Hellwig  *
1887a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1888a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1889eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1890eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1891eead1911SChristoph Hellwig  */
1892057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1893057f6c01SJames Morris {
1894057f6c01SJames Morris 	struct qstr this;
18956a96ba54SAl Viro 	unsigned int c;
1896cda309deSMiklos Szeredi 	int err;
1897057f6c01SJames Morris 
18982f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
18992f9092e1SDavid Woodhouse 
19006a96ba54SAl Viro 	this.name = name;
19016a96ba54SAl Viro 	this.len = len;
19020145acc2SLinus Torvalds 	this.hash = full_name_hash(name, len);
19036a96ba54SAl Viro 	if (!len)
19046a96ba54SAl Viro 		return ERR_PTR(-EACCES);
19056a96ba54SAl Viro 
19066a96ba54SAl Viro 	while (len--) {
19076a96ba54SAl Viro 		c = *(const unsigned char *)name++;
19086a96ba54SAl Viro 		if (c == '/' || c == '\0')
19096a96ba54SAl Viro 			return ERR_PTR(-EACCES);
19106a96ba54SAl Viro 	}
19115a202bcdSAl Viro 	/*
19125a202bcdSAl Viro 	 * See if the low-level filesystem might want
19135a202bcdSAl Viro 	 * to use its own hash..
19145a202bcdSAl Viro 	 */
19155a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
19165a202bcdSAl Viro 		int err = base->d_op->d_hash(base, base->d_inode, &this);
19175a202bcdSAl Viro 		if (err < 0)
19185a202bcdSAl Viro 			return ERR_PTR(err);
19195a202bcdSAl Viro 	}
1920eead1911SChristoph Hellwig 
1921cda309deSMiklos Szeredi 	err = inode_permission(base->d_inode, MAY_EXEC);
1922cda309deSMiklos Szeredi 	if (err)
1923cda309deSMiklos Szeredi 		return ERR_PTR(err);
1924cda309deSMiklos Szeredi 
192549705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1926057f6c01SJames Morris }
1927057f6c01SJames Morris 
19281fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
19291fa1e7f6SAndy Whitcroft 		 struct path *path, int *empty)
19301da177e4SLinus Torvalds {
19312d8f3038SAl Viro 	struct nameidata nd;
19321fa1e7f6SAndy Whitcroft 	char *tmp = getname_flags(name, flags, empty);
19331da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
19341da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
19352d8f3038SAl Viro 
19362d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
19372d8f3038SAl Viro 
19382d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
19391da177e4SLinus Torvalds 		putname(tmp);
19402d8f3038SAl Viro 		if (!err)
19412d8f3038SAl Viro 			*path = nd.path;
19421da177e4SLinus Torvalds 	}
19431da177e4SLinus Torvalds 	return err;
19441da177e4SLinus Torvalds }
19451da177e4SLinus Torvalds 
19461fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags,
19471fa1e7f6SAndy Whitcroft 		 struct path *path)
19481fa1e7f6SAndy Whitcroft {
1949f7493e5dSLinus Torvalds 	return user_path_at_empty(dfd, name, flags, path, NULL);
19501fa1e7f6SAndy Whitcroft }
19511fa1e7f6SAndy Whitcroft 
19522ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
19532ad94ae6SAl Viro 			struct nameidata *nd, char **name)
19542ad94ae6SAl Viro {
19552ad94ae6SAl Viro 	char *s = getname(path);
19562ad94ae6SAl Viro 	int error;
19572ad94ae6SAl Viro 
19582ad94ae6SAl Viro 	if (IS_ERR(s))
19592ad94ae6SAl Viro 		return PTR_ERR(s);
19602ad94ae6SAl Viro 
19612ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
19622ad94ae6SAl Viro 	if (error)
19632ad94ae6SAl Viro 		putname(s);
19642ad94ae6SAl Viro 	else
19652ad94ae6SAl Viro 		*name = s;
19662ad94ae6SAl Viro 
19672ad94ae6SAl Viro 	return error;
19682ad94ae6SAl Viro }
19692ad94ae6SAl Viro 
19701da177e4SLinus Torvalds /*
19711da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
19721da177e4SLinus Torvalds  * minimal.
19731da177e4SLinus Torvalds  */
19741da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
19751da177e4SLinus Torvalds {
19768e96e3b7SEric W. Biederman 	kuid_t fsuid = current_fsuid();
1977da9592edSDavid Howells 
19781da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
19791da177e4SLinus Torvalds 		return 0;
19808e96e3b7SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
19811da177e4SLinus Torvalds 		return 0;
19828e96e3b7SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
19831da177e4SLinus Torvalds 		return 0;
19841a48e2acSEric W. Biederman 	return !inode_capable(inode, CAP_FOWNER);
19851da177e4SLinus Torvalds }
19861da177e4SLinus Torvalds 
19871da177e4SLinus Torvalds /*
19881da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
19891da177e4SLinus Torvalds  *  whether the type of victim is right.
19901da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
19911da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
19921da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
19931da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
19941da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
19951da177e4SLinus Torvalds  *	a. be owner of dir, or
19961da177e4SLinus Torvalds  *	b. be owner of victim, or
19971da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
19981da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
19991da177e4SLinus Torvalds  *     links pointing to it.
20001da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
20011da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
20021da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
20031da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
20041da177e4SLinus Torvalds  *     nfs_async_unlink().
20051da177e4SLinus Torvalds  */
2006858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
20071da177e4SLinus Torvalds {
20081da177e4SLinus Torvalds 	int error;
20091da177e4SLinus Torvalds 
20101da177e4SLinus Torvalds 	if (!victim->d_inode)
20111da177e4SLinus Torvalds 		return -ENOENT;
20121da177e4SLinus Torvalds 
20131da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
2014cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
20151da177e4SLinus Torvalds 
2016f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
20171da177e4SLinus Torvalds 	if (error)
20181da177e4SLinus Torvalds 		return error;
20191da177e4SLinus Torvalds 	if (IS_APPEND(dir))
20201da177e4SLinus Torvalds 		return -EPERM;
20211da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
2022f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
20231da177e4SLinus Torvalds 		return -EPERM;
20241da177e4SLinus Torvalds 	if (isdir) {
20251da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
20261da177e4SLinus Torvalds 			return -ENOTDIR;
20271da177e4SLinus Torvalds 		if (IS_ROOT(victim))
20281da177e4SLinus Torvalds 			return -EBUSY;
20291da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
20301da177e4SLinus Torvalds 		return -EISDIR;
20311da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
20321da177e4SLinus Torvalds 		return -ENOENT;
20331da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
20341da177e4SLinus Torvalds 		return -EBUSY;
20351da177e4SLinus Torvalds 	return 0;
20361da177e4SLinus Torvalds }
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
20391da177e4SLinus Torvalds  *  dir.
20401da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
20411da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
20421da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
20431da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
20441da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
20451da177e4SLinus Torvalds  */
2046a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
20471da177e4SLinus Torvalds {
20481da177e4SLinus Torvalds 	if (child->d_inode)
20491da177e4SLinus Torvalds 		return -EEXIST;
20501da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
20511da177e4SLinus Torvalds 		return -ENOENT;
2052f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
20531da177e4SLinus Torvalds }
20541da177e4SLinus Torvalds 
20551da177e4SLinus Torvalds /*
20561da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
20571da177e4SLinus Torvalds  */
20581da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
20591da177e4SLinus Torvalds {
20601da177e4SLinus Torvalds 	struct dentry *p;
20611da177e4SLinus Torvalds 
20621da177e4SLinus Torvalds 	if (p1 == p2) {
2063f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
20641da177e4SLinus Torvalds 		return NULL;
20651da177e4SLinus Torvalds 	}
20661da177e4SLinus Torvalds 
2067a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
20681da177e4SLinus Torvalds 
2069e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
2070e2761a11SOGAWA Hirofumi 	if (p) {
2071f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2072f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
20731da177e4SLinus Torvalds 		return p;
20741da177e4SLinus Torvalds 	}
20751da177e4SLinus Torvalds 
2076e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
2077e2761a11SOGAWA Hirofumi 	if (p) {
2078f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2079f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
20801da177e4SLinus Torvalds 		return p;
20811da177e4SLinus Torvalds 	}
20821da177e4SLinus Torvalds 
2083f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2084f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
20851da177e4SLinus Torvalds 	return NULL;
20861da177e4SLinus Torvalds }
20871da177e4SLinus Torvalds 
20881da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
20891da177e4SLinus Torvalds {
20901b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
20911da177e4SLinus Torvalds 	if (p1 != p2) {
20921b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
2093a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
20941da177e4SLinus Torvalds 	}
20951da177e4SLinus Torvalds }
20961da177e4SLinus Torvalds 
20974acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
20981da177e4SLinus Torvalds 		struct nameidata *nd)
20991da177e4SLinus Torvalds {
2100a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
21011da177e4SLinus Torvalds 
21021da177e4SLinus Torvalds 	if (error)
21031da177e4SLinus Torvalds 		return error;
21041da177e4SLinus Torvalds 
2105acfa4380SAl Viro 	if (!dir->i_op->create)
21061da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
21071da177e4SLinus Torvalds 	mode &= S_IALLUGO;
21081da177e4SLinus Torvalds 	mode |= S_IFREG;
21091da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
21101da177e4SLinus Torvalds 	if (error)
21111da177e4SLinus Torvalds 		return error;
21121da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
2113a74574aaSStephen Smalley 	if (!error)
2114f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
21151da177e4SLinus Torvalds 	return error;
21161da177e4SLinus Torvalds }
21171da177e4SLinus Torvalds 
211873d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
21191da177e4SLinus Torvalds {
21203fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
21211da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
21221da177e4SLinus Torvalds 	int error;
21231da177e4SLinus Torvalds 
2124bcda7652SAl Viro 	/* O_PATH? */
2125bcda7652SAl Viro 	if (!acc_mode)
2126bcda7652SAl Viro 		return 0;
2127bcda7652SAl Viro 
21281da177e4SLinus Torvalds 	if (!inode)
21291da177e4SLinus Torvalds 		return -ENOENT;
21301da177e4SLinus Torvalds 
2131c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2132c8fe8f30SChristoph Hellwig 	case S_IFLNK:
21331da177e4SLinus Torvalds 		return -ELOOP;
2134c8fe8f30SChristoph Hellwig 	case S_IFDIR:
2135c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
21361da177e4SLinus Torvalds 			return -EISDIR;
2137c8fe8f30SChristoph Hellwig 		break;
2138c8fe8f30SChristoph Hellwig 	case S_IFBLK:
2139c8fe8f30SChristoph Hellwig 	case S_IFCHR:
21403fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
21411da177e4SLinus Torvalds 			return -EACCES;
2142c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
2143c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2144c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
21451da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2146c8fe8f30SChristoph Hellwig 		break;
21474a3fd211SDave Hansen 	}
2148b41572e9SDave Hansen 
21493fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
2150b41572e9SDave Hansen 	if (error)
2151b41572e9SDave Hansen 		return error;
21526146f0d5SMimi Zohar 
21531da177e4SLinus Torvalds 	/*
21541da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
21551da177e4SLinus Torvalds 	 */
21561da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
21578737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
21587715b521SAl Viro 			return -EPERM;
21591da177e4SLinus Torvalds 		if (flag & O_TRUNC)
21607715b521SAl Viro 			return -EPERM;
21611da177e4SLinus Torvalds 	}
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
21642e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
21657715b521SAl Viro 		return -EPERM;
21661da177e4SLinus Torvalds 
2167f3c7691eSJ. Bruce Fields 	return 0;
21687715b521SAl Viro }
21697715b521SAl Viro 
2170e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
21717715b521SAl Viro {
2172e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
21737715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
21747715b521SAl Viro 	int error = get_write_access(inode);
21751da177e4SLinus Torvalds 	if (error)
21767715b521SAl Viro 		return error;
21771da177e4SLinus Torvalds 	/*
21781da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
21791da177e4SLinus Torvalds 	 */
21801da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2181be6d3e56SKentaro Takeda 	if (!error)
2182ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
21831da177e4SLinus Torvalds 	if (!error) {
21847715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2185d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2186e1181ee6SJeff Layton 				    filp);
21871da177e4SLinus Torvalds 	}
21881da177e4SLinus Torvalds 	put_write_access(inode);
2189acd0c935SMimi Zohar 	return error;
21901da177e4SLinus Torvalds }
21911da177e4SLinus Torvalds 
2192d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2193d57999e1SDave Hansen {
21948a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
21958a5e929dSAl Viro 		flag--;
2196d57999e1SDave Hansen 	return flag;
2197d57999e1SDave Hansen }
2198d57999e1SDave Hansen 
2199d18e9008SMiklos Szeredi static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2200d18e9008SMiklos Szeredi {
2201d18e9008SMiklos Szeredi 	int error = security_path_mknod(dir, dentry, mode, 0);
2202d18e9008SMiklos Szeredi 	if (error)
2203d18e9008SMiklos Szeredi 		return error;
2204d18e9008SMiklos Szeredi 
2205d18e9008SMiklos Szeredi 	error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2206d18e9008SMiklos Szeredi 	if (error)
2207d18e9008SMiklos Szeredi 		return error;
2208d18e9008SMiklos Szeredi 
2209d18e9008SMiklos Szeredi 	return security_inode_create(dir->dentry->d_inode, dentry, mode);
2210d18e9008SMiklos Szeredi }
2211d18e9008SMiklos Szeredi 
2212d18e9008SMiklos Szeredi static struct file *atomic_open(struct nameidata *nd, struct dentry *dentry,
2213d18e9008SMiklos Szeredi 				struct path *path, const struct open_flags *op,
2214d18e9008SMiklos Szeredi 				int *want_write, bool need_lookup,
2215d18e9008SMiklos Szeredi 				bool *created)
2216d18e9008SMiklos Szeredi {
2217d18e9008SMiklos Szeredi 	struct inode *dir =  nd->path.dentry->d_inode;
2218d18e9008SMiklos Szeredi 	unsigned open_flag = open_to_namei_flags(op->open_flag);
2219d18e9008SMiklos Szeredi 	umode_t mode;
2220d18e9008SMiklos Szeredi 	int error;
2221d18e9008SMiklos Szeredi 	int acc_mode;
2222d18e9008SMiklos Szeredi 	struct opendata od;
2223d18e9008SMiklos Szeredi 	struct file *filp;
2224d18e9008SMiklos Szeredi 	int create_error = 0;
2225d18e9008SMiklos Szeredi 	struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2226d18e9008SMiklos Szeredi 
2227d18e9008SMiklos Szeredi 	BUG_ON(dentry->d_inode);
2228d18e9008SMiklos Szeredi 
2229d18e9008SMiklos Szeredi 	/* Don't create child dentry for a dead directory. */
2230d18e9008SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
2231d18e9008SMiklos Szeredi 		filp = ERR_PTR(-ENOENT);
2232d18e9008SMiklos Szeredi 		goto out;
2233d18e9008SMiklos Szeredi 	}
2234d18e9008SMiklos Szeredi 
2235d18e9008SMiklos Szeredi 	mode = op->mode & S_IALLUGO;
2236d18e9008SMiklos Szeredi 	if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2237d18e9008SMiklos Szeredi 		mode &= ~current_umask();
2238d18e9008SMiklos Szeredi 
2239d18e9008SMiklos Szeredi 	if (open_flag & O_EXCL) {
2240d18e9008SMiklos Szeredi 		open_flag &= ~O_TRUNC;
2241d18e9008SMiklos Szeredi 		*created = true;
2242d18e9008SMiklos Szeredi 	}
2243d18e9008SMiklos Szeredi 
2244d18e9008SMiklos Szeredi 	/*
2245d18e9008SMiklos Szeredi 	 * Checking write permission is tricky, bacuse we don't know if we are
2246d18e9008SMiklos Szeredi 	 * going to actually need it: O_CREAT opens should work as long as the
2247d18e9008SMiklos Szeredi 	 * file exists.  But checking existence breaks atomicity.  The trick is
2248d18e9008SMiklos Szeredi 	 * to check access and if not granted clear O_CREAT from the flags.
2249d18e9008SMiklos Szeredi 	 *
2250d18e9008SMiklos Szeredi 	 * Another problem is returing the "right" error value (e.g. for an
2251d18e9008SMiklos Szeredi 	 * O_EXCL open we want to return EEXIST not EROFS).
2252d18e9008SMiklos Szeredi 	 */
2253d18e9008SMiklos Szeredi 	if ((open_flag & (O_CREAT | O_TRUNC)) ||
2254d18e9008SMiklos Szeredi 	    (open_flag & O_ACCMODE) != O_RDONLY) {
2255d18e9008SMiklos Szeredi 		error = mnt_want_write(nd->path.mnt);
2256d18e9008SMiklos Szeredi 		if (!error) {
2257d18e9008SMiklos Szeredi 			*want_write = 1;
2258d18e9008SMiklos Szeredi 		} else if (!(open_flag & O_CREAT)) {
2259d18e9008SMiklos Szeredi 			/*
2260d18e9008SMiklos Szeredi 			 * No O_CREATE -> atomicity not a requirement -> fall
2261d18e9008SMiklos Szeredi 			 * back to lookup + open
2262d18e9008SMiklos Szeredi 			 */
2263d18e9008SMiklos Szeredi 			goto no_open;
2264d18e9008SMiklos Szeredi 		} else if (open_flag & (O_EXCL | O_TRUNC)) {
2265d18e9008SMiklos Szeredi 			/* Fall back and fail with the right error */
2266d18e9008SMiklos Szeredi 			create_error = error;
2267d18e9008SMiklos Szeredi 			goto no_open;
2268d18e9008SMiklos Szeredi 		} else {
2269d18e9008SMiklos Szeredi 			/* No side effects, safe to clear O_CREAT */
2270d18e9008SMiklos Szeredi 			create_error = error;
2271d18e9008SMiklos Szeredi 			open_flag &= ~O_CREAT;
2272d18e9008SMiklos Szeredi 		}
2273d18e9008SMiklos Szeredi 	}
2274d18e9008SMiklos Szeredi 
2275d18e9008SMiklos Szeredi 	if (open_flag & O_CREAT) {
2276d18e9008SMiklos Szeredi 		error = may_o_create(&nd->path, dentry, op->mode);
2277d18e9008SMiklos Szeredi 		if (error) {
2278d18e9008SMiklos Szeredi 			create_error = error;
2279d18e9008SMiklos Szeredi 			if (open_flag & O_EXCL)
2280d18e9008SMiklos Szeredi 				goto no_open;
2281d18e9008SMiklos Szeredi 			open_flag &= ~O_CREAT;
2282d18e9008SMiklos Szeredi 		}
2283d18e9008SMiklos Szeredi 	}
2284d18e9008SMiklos Szeredi 
2285d18e9008SMiklos Szeredi 	if (nd->flags & LOOKUP_DIRECTORY)
2286d18e9008SMiklos Szeredi 		open_flag |= O_DIRECTORY;
2287d18e9008SMiklos Szeredi 
2288d18e9008SMiklos Szeredi 	od.dentry = DENTRY_NOT_SET;
2289d18e9008SMiklos Szeredi 	od.mnt = nd->path.mnt;
2290d18e9008SMiklos Szeredi 	od.filp = &nd->intent.open.file;
2291d18e9008SMiklos Szeredi 	filp = dir->i_op->atomic_open(dir, dentry, &od, open_flag, mode,
2292d18e9008SMiklos Szeredi 				      created);
2293d18e9008SMiklos Szeredi 	if (IS_ERR(filp)) {
2294d18e9008SMiklos Szeredi 		if (WARN_ON(od.dentry != DENTRY_NOT_SET))
2295d18e9008SMiklos Szeredi 			dput(od.dentry);
2296d18e9008SMiklos Szeredi 
2297d18e9008SMiklos Szeredi 		if (create_error && PTR_ERR(filp) == -ENOENT)
2298d18e9008SMiklos Szeredi 			filp = ERR_PTR(create_error);
2299d18e9008SMiklos Szeredi 		goto out;
2300d18e9008SMiklos Szeredi 	}
2301d18e9008SMiklos Szeredi 
2302d18e9008SMiklos Szeredi 	acc_mode = op->acc_mode;
2303d18e9008SMiklos Szeredi 	if (*created) {
2304d18e9008SMiklos Szeredi 		fsnotify_create(dir, dentry);
2305d18e9008SMiklos Szeredi 		acc_mode = MAY_OPEN;
2306d18e9008SMiklos Szeredi 	}
2307d18e9008SMiklos Szeredi 
2308d18e9008SMiklos Szeredi 	if (!filp) {
2309d18e9008SMiklos Szeredi 		if (WARN_ON(od.dentry == DENTRY_NOT_SET)) {
2310d18e9008SMiklos Szeredi 			filp = ERR_PTR(-EIO);
2311d18e9008SMiklos Szeredi 			goto out;
2312d18e9008SMiklos Szeredi 		}
2313d18e9008SMiklos Szeredi 		if (od.dentry) {
2314d18e9008SMiklos Szeredi 			dput(dentry);
2315d18e9008SMiklos Szeredi 			dentry = od.dentry;
2316d18e9008SMiklos Szeredi 		}
2317d18e9008SMiklos Szeredi 		goto looked_up;
2318d18e9008SMiklos Szeredi 	}
2319d18e9008SMiklos Szeredi 
2320d18e9008SMiklos Szeredi 	/*
2321d18e9008SMiklos Szeredi 	 * We didn't have the inode before the open, so check open permission
2322d18e9008SMiklos Szeredi 	 * here.
2323d18e9008SMiklos Szeredi 	 */
2324d18e9008SMiklos Szeredi 	error = may_open(&filp->f_path, acc_mode, open_flag);
2325d18e9008SMiklos Szeredi 	if (error)
2326d18e9008SMiklos Szeredi 		goto out_fput;
2327d18e9008SMiklos Szeredi 
2328d18e9008SMiklos Szeredi 	error = open_check_o_direct(filp);
2329d18e9008SMiklos Szeredi 	if (error)
2330d18e9008SMiklos Szeredi 		goto out_fput;
2331d18e9008SMiklos Szeredi 
2332d18e9008SMiklos Szeredi out:
2333d18e9008SMiklos Szeredi 	dput(dentry);
2334d18e9008SMiklos Szeredi 	return filp;
2335d18e9008SMiklos Szeredi 
2336d18e9008SMiklos Szeredi out_fput:
2337d18e9008SMiklos Szeredi 	fput(filp);
2338d18e9008SMiklos Szeredi 	filp = ERR_PTR(error);
2339d18e9008SMiklos Szeredi 	goto out;
2340d18e9008SMiklos Szeredi 
2341d18e9008SMiklos Szeredi no_open:
2342d18e9008SMiklos Szeredi 	if (need_lookup) {
2343d18e9008SMiklos Szeredi 		dentry = lookup_real(dir, dentry, nd);
2344d18e9008SMiklos Szeredi 		if (IS_ERR(dentry))
2345d18e9008SMiklos Szeredi 			return ERR_CAST(dentry);
2346d18e9008SMiklos Szeredi 
2347d18e9008SMiklos Szeredi 		if (create_error) {
2348d18e9008SMiklos Szeredi 			int open_flag = op->open_flag;
2349d18e9008SMiklos Szeredi 
2350d18e9008SMiklos Szeredi 			filp = ERR_PTR(create_error);
2351d18e9008SMiklos Szeredi 			if ((open_flag & O_EXCL)) {
2352d18e9008SMiklos Szeredi 				if (!dentry->d_inode)
2353d18e9008SMiklos Szeredi 					goto out;
2354d18e9008SMiklos Szeredi 			} else if (!dentry->d_inode) {
2355d18e9008SMiklos Szeredi 				goto out;
2356d18e9008SMiklos Szeredi 			} else if ((open_flag & O_TRUNC) &&
2357d18e9008SMiklos Szeredi 				   S_ISREG(dentry->d_inode->i_mode)) {
2358d18e9008SMiklos Szeredi 				goto out;
2359d18e9008SMiklos Szeredi 			}
2360d18e9008SMiklos Szeredi 			/* will fail later, go on to get the right error */
2361d18e9008SMiklos Szeredi 		}
2362d18e9008SMiklos Szeredi 	}
2363d18e9008SMiklos Szeredi looked_up:
2364d18e9008SMiklos Szeredi 	path->dentry = dentry;
2365d18e9008SMiklos Szeredi 	path->mnt = nd->path.mnt;
2366d18e9008SMiklos Szeredi 	return NULL;
2367d18e9008SMiklos Szeredi }
2368d18e9008SMiklos Szeredi 
236931e6b01fSNick Piggin /*
2370d58ffd35SMiklos Szeredi  * Lookup, maybe create and open the last component
2371d58ffd35SMiklos Szeredi  *
2372d58ffd35SMiklos Szeredi  * Must be called with i_mutex held on parent.
2373d58ffd35SMiklos Szeredi  *
2374d58ffd35SMiklos Szeredi  * Returns open file or NULL on success, error otherwise.  NULL means no open
2375d58ffd35SMiklos Szeredi  * was performed, only lookup.
2376d58ffd35SMiklos Szeredi  */
2377d58ffd35SMiklos Szeredi static struct file *lookup_open(struct nameidata *nd, struct path *path,
2378d58ffd35SMiklos Szeredi 				const struct open_flags *op,
2379d58ffd35SMiklos Szeredi 				int *want_write, bool *created)
2380d58ffd35SMiklos Szeredi {
2381d58ffd35SMiklos Szeredi 	struct dentry *dir = nd->path.dentry;
238254ef4872SMiklos Szeredi 	struct inode *dir_inode = dir->d_inode;
2383d58ffd35SMiklos Szeredi 	struct dentry *dentry;
2384d58ffd35SMiklos Szeredi 	int error;
238554ef4872SMiklos Szeredi 	bool need_lookup;
2386d58ffd35SMiklos Szeredi 
2387d58ffd35SMiklos Szeredi 	*created = false;
238854ef4872SMiklos Szeredi 	dentry = lookup_dcache(&nd->last, dir, nd, &need_lookup);
2389d58ffd35SMiklos Szeredi 	if (IS_ERR(dentry))
2390d58ffd35SMiklos Szeredi 		return ERR_CAST(dentry);
2391d58ffd35SMiklos Szeredi 
2392d18e9008SMiklos Szeredi 	/* Cached positive dentry: will open in f_op->open */
2393d18e9008SMiklos Szeredi 	if (!need_lookup && dentry->d_inode)
2394d18e9008SMiklos Szeredi 		goto out_no_open;
2395d18e9008SMiklos Szeredi 
2396d18e9008SMiklos Szeredi 	if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
2397d18e9008SMiklos Szeredi 		return atomic_open(nd, dentry, path, op, want_write,
2398d18e9008SMiklos Szeredi 				   need_lookup, created);
2399d18e9008SMiklos Szeredi 	}
2400d18e9008SMiklos Szeredi 
240154ef4872SMiklos Szeredi 	if (need_lookup) {
240254ef4872SMiklos Szeredi 		BUG_ON(dentry->d_inode);
240354ef4872SMiklos Szeredi 
240454ef4872SMiklos Szeredi 		dentry = lookup_real(dir_inode, dentry, nd);
240554ef4872SMiklos Szeredi 		if (IS_ERR(dentry))
240654ef4872SMiklos Szeredi 			return ERR_CAST(dentry);
240754ef4872SMiklos Szeredi 	}
240854ef4872SMiklos Szeredi 
2409d58ffd35SMiklos Szeredi 	/* Negative dentry, just create the file */
2410d58ffd35SMiklos Szeredi 	if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2411d58ffd35SMiklos Szeredi 		umode_t mode = op->mode;
2412d58ffd35SMiklos Szeredi 		if (!IS_POSIXACL(dir->d_inode))
2413d58ffd35SMiklos Szeredi 			mode &= ~current_umask();
2414d58ffd35SMiklos Szeredi 		/*
2415d58ffd35SMiklos Szeredi 		 * This write is needed to ensure that a
2416d58ffd35SMiklos Szeredi 		 * rw->ro transition does not occur between
2417d58ffd35SMiklos Szeredi 		 * the time when the file is created and when
2418d58ffd35SMiklos Szeredi 		 * a permanent write count is taken through
2419d58ffd35SMiklos Szeredi 		 * the 'struct file' in nameidata_to_filp().
2420d58ffd35SMiklos Szeredi 		 */
2421d58ffd35SMiklos Szeredi 		error = mnt_want_write(nd->path.mnt);
2422d58ffd35SMiklos Szeredi 		if (error)
2423d58ffd35SMiklos Szeredi 			goto out_dput;
2424d58ffd35SMiklos Szeredi 		*want_write = 1;
2425d58ffd35SMiklos Szeredi 		*created = true;
2426d58ffd35SMiklos Szeredi 		error = security_path_mknod(&nd->path, dentry, mode, 0);
2427d58ffd35SMiklos Szeredi 		if (error)
2428d58ffd35SMiklos Szeredi 			goto out_dput;
2429d58ffd35SMiklos Szeredi 		error = vfs_create(dir->d_inode, dentry, mode, nd);
2430d58ffd35SMiklos Szeredi 		if (error)
2431d58ffd35SMiklos Szeredi 			goto out_dput;
2432d58ffd35SMiklos Szeredi 	}
2433d18e9008SMiklos Szeredi out_no_open:
2434d58ffd35SMiklos Szeredi 	path->dentry = dentry;
2435d58ffd35SMiklos Szeredi 	path->mnt = nd->path.mnt;
2436d58ffd35SMiklos Szeredi 	return NULL;
2437d58ffd35SMiklos Szeredi 
2438d58ffd35SMiklos Szeredi out_dput:
2439d58ffd35SMiklos Szeredi 	dput(dentry);
2440d58ffd35SMiklos Szeredi 	return ERR_PTR(error);
2441d58ffd35SMiklos Szeredi }
2442d58ffd35SMiklos Szeredi 
2443d58ffd35SMiklos Szeredi /*
2444fe2d35ffSAl Viro  * Handle the last step of open()
244531e6b01fSNick Piggin  */
2446fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
2447c3e380b0SAl Viro 			    const struct open_flags *op, const char *pathname)
2448fb1cc555SAl Viro {
2449a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
2450ca344a89SAl Viro 	int open_flag = op->open_flag;
24516c0d46c4SAl Viro 	int will_truncate = open_flag & O_TRUNC;
2452ca344a89SAl Viro 	int want_write = 0;
2453bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2454fb1cc555SAl Viro 	struct file *filp;
2455a1eb3315SMiklos Szeredi 	struct inode *inode;
2456d58ffd35SMiklos Szeredi 	bool created;
2457d45ea867SMiklos Szeredi 	int symlink_ok = 0;
245816b1c1cdSMiklos Szeredi 	struct path save_parent = { .dentry = NULL, .mnt = NULL };
245916b1c1cdSMiklos Szeredi 	bool retried = false;
246016c2cd71SAl Viro 	int error;
2461fb1cc555SAl Viro 
2462c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2463c3e380b0SAl Viro 	nd->flags |= op->intent;
2464c3e380b0SAl Viro 
24651f36f774SAl Viro 	switch (nd->last_type) {
24661f36f774SAl Viro 	case LAST_DOTDOT:
2467176306f5SNeil Brown 	case LAST_DOT:
2468fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2469fe2d35ffSAl Viro 		if (error)
2470fe2d35ffSAl Viro 			return ERR_PTR(error);
24711f36f774SAl Viro 		/* fallthrough */
24721f36f774SAl Viro 	case LAST_ROOT:
24739f1fafeeSAl Viro 		error = complete_walk(nd);
247416c2cd71SAl Viro 		if (error)
24759f1fafeeSAl Viro 			return ERR_PTR(error);
2476fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2477ca344a89SAl Viro 		if (open_flag & O_CREAT) {
247816c2cd71SAl Viro 			error = -EISDIR;
24791f36f774SAl Viro 			goto exit;
2480fe2d35ffSAl Viro 		}
2481fe2d35ffSAl Viro 		goto ok;
24821f36f774SAl Viro 	case LAST_BIND:
24839f1fafeeSAl Viro 		error = complete_walk(nd);
248416c2cd71SAl Viro 		if (error)
24859f1fafeeSAl Viro 			return ERR_PTR(error);
24861f36f774SAl Viro 		audit_inode(pathname, dir);
24871f36f774SAl Viro 		goto ok;
24881f36f774SAl Viro 	}
2489a2c36b45SAl Viro 
2490ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2491fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2492fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2493bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2494bcda7652SAl Viro 			symlink_ok = 1;
2495fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2496a1eb3315SMiklos Szeredi 		error = lookup_fast(nd, &nd->last, path, &inode);
249771574865SMiklos Szeredi 		if (likely(!error))
249871574865SMiklos Szeredi 			goto finish_lookup;
249971574865SMiklos Szeredi 
2500ce57dfc1SAl Viro 		if (error < 0)
2501a1eb3315SMiklos Szeredi 			goto exit;
2502a1eb3315SMiklos Szeredi 
250337d7fffcSMiklos Szeredi 		BUG_ON(nd->inode != dir->d_inode);
2504b6183df7SMiklos Szeredi 	} else {
2505fe2d35ffSAl Viro 		/* create side of things */
2506a3fbbde7SAl Viro 		/*
2507b6183df7SMiklos Szeredi 		 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2508b6183df7SMiklos Szeredi 		 * has been cleared when we got to the last component we are
2509b6183df7SMiklos Szeredi 		 * about to look up
2510a3fbbde7SAl Viro 		 */
25119f1fafeeSAl Viro 		error = complete_walk(nd);
25129f1fafeeSAl Viro 		if (error)
25139f1fafeeSAl Viro 			return ERR_PTR(error);
2514fe2d35ffSAl Viro 
2515fe2d35ffSAl Viro 		audit_inode(pathname, dir);
251616c2cd71SAl Viro 		error = -EISDIR;
25171f36f774SAl Viro 		/* trailing slashes? */
251831e6b01fSNick Piggin 		if (nd->last.name[nd->last.len])
25191f36f774SAl Viro 			goto exit;
2520b6183df7SMiklos Szeredi 	}
25211f36f774SAl Viro 
252216b1c1cdSMiklos Szeredi retry_lookup:
2523a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
2524d58ffd35SMiklos Szeredi 	filp = lookup_open(nd, path, op, &want_write, &created);
2525fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2526fb1cc555SAl Viro 
2527d18e9008SMiklos Szeredi 	if (filp) {
2528d58ffd35SMiklos Szeredi 		if (IS_ERR(filp))
2529d58ffd35SMiklos Szeredi 			goto out;
25306c0d46c4SAl Viro 
2531d18e9008SMiklos Szeredi 		if (created || !S_ISREG(filp->f_path.dentry->d_inode->i_mode))
2532d18e9008SMiklos Szeredi 			will_truncate = 0;
2533d18e9008SMiklos Szeredi 
2534d18e9008SMiklos Szeredi 		audit_inode(pathname, filp->f_path.dentry);
2535d18e9008SMiklos Szeredi 		goto opened;
2536d18e9008SMiklos Szeredi 	}
2537d18e9008SMiklos Szeredi 
2538d58ffd35SMiklos Szeredi 	if (created) {
25399b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2540ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
25416c0d46c4SAl Viro 		will_truncate = 0;
2542bcda7652SAl Viro 		acc_mode = MAY_OPEN;
2543d58ffd35SMiklos Szeredi 		path_to_nameidata(path, nd);
2544ca344a89SAl Viro 		goto common;
2545fb1cc555SAl Viro 	}
2546fb1cc555SAl Viro 
2547fb1cc555SAl Viro 	/*
2548fb1cc555SAl Viro 	 * It already exists.
2549fb1cc555SAl Viro 	 */
2550fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2551fb1cc555SAl Viro 
2552d18e9008SMiklos Szeredi 	/*
2553d18e9008SMiklos Szeredi 	 * If atomic_open() acquired write access it is dropped now due to
2554d18e9008SMiklos Szeredi 	 * possible mount and symlink following (this might be optimized away if
2555d18e9008SMiklos Szeredi 	 * necessary...)
2556d18e9008SMiklos Szeredi 	 */
2557d18e9008SMiklos Szeredi 	if (want_write) {
2558d18e9008SMiklos Szeredi 		mnt_drop_write(nd->path.mnt);
2559d18e9008SMiklos Szeredi 		want_write = 0;
2560d18e9008SMiklos Szeredi 	}
2561d18e9008SMiklos Szeredi 
2562fb1cc555SAl Viro 	error = -EEXIST;
2563ca344a89SAl Viro 	if (open_flag & O_EXCL)
2564fb1cc555SAl Viro 		goto exit_dput;
2565fb1cc555SAl Viro 
25669875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
25679875cf80SDavid Howells 	if (error < 0)
2568fb1cc555SAl Viro 		goto exit_dput;
2569fb1cc555SAl Viro 
2570a3fbbde7SAl Viro 	if (error)
2571a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
2572a3fbbde7SAl Viro 
2573decf3400SMiklos Szeredi 	BUG_ON(nd->flags & LOOKUP_RCU);
2574decf3400SMiklos Szeredi 	inode = path->dentry->d_inode;
25755f5daac1SMiklos Szeredi finish_lookup:
25765f5daac1SMiklos Szeredi 	/* we _can_ be in RCU mode here */
2577fb1cc555SAl Viro 	error = -ENOENT;
257854c33e7fSMiklos Szeredi 	if (!inode) {
257954c33e7fSMiklos Szeredi 		path_to_nameidata(path, nd);
258054c33e7fSMiklos Szeredi 		goto exit;
258154c33e7fSMiklos Szeredi 	}
25829e67f361SAl Viro 
2583d45ea867SMiklos Szeredi 	if (should_follow_link(inode, !symlink_ok)) {
2584d45ea867SMiklos Szeredi 		if (nd->flags & LOOKUP_RCU) {
2585d45ea867SMiklos Szeredi 			if (unlikely(unlazy_walk(nd, path->dentry))) {
2586d45ea867SMiklos Szeredi 				error = -ECHILD;
2587d45ea867SMiklos Szeredi 				goto exit;
2588d45ea867SMiklos Szeredi 			}
2589d45ea867SMiklos Szeredi 		}
2590d45ea867SMiklos Szeredi 		BUG_ON(inode != path->dentry->d_inode);
2591fb1cc555SAl Viro 		return NULL;
2592d45ea867SMiklos Szeredi 	}
2593fb1cc555SAl Viro 
259416b1c1cdSMiklos Szeredi 	if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
2595fb1cc555SAl Viro 		path_to_nameidata(path, nd);
259616b1c1cdSMiklos Szeredi 	} else {
259716b1c1cdSMiklos Szeredi 		save_parent.dentry = nd->path.dentry;
259816b1c1cdSMiklos Szeredi 		save_parent.mnt = mntget(path->mnt);
259916b1c1cdSMiklos Szeredi 		nd->path.dentry = path->dentry;
260016b1c1cdSMiklos Szeredi 
260116b1c1cdSMiklos Szeredi 	}
2602decf3400SMiklos Szeredi 	nd->inode = inode;
2603a3fbbde7SAl Viro 	/* Why this, you ask?  _Now_ we might have grown LOOKUP_JUMPED... */
2604a3fbbde7SAl Viro 	error = complete_walk(nd);
260516b1c1cdSMiklos Szeredi 	if (error) {
260616b1c1cdSMiklos Szeredi 		path_put(&save_parent);
2607097b180cSMiklos Szeredi 		return ERR_PTR(error);
260816b1c1cdSMiklos Szeredi 	}
2609fb1cc555SAl Viro 	error = -EISDIR;
2610050ac841SMiklos Szeredi 	if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
2611fb1cc555SAl Viro 		goto exit;
2612af2f5542SMiklos Szeredi 	error = -ENOTDIR;
2613af2f5542SMiklos Szeredi 	if ((nd->flags & LOOKUP_DIRECTORY) && !nd->inode->i_op->lookup)
2614af2f5542SMiklos Szeredi 		goto exit;
2615d7fdd7f6SMiklos Szeredi 	audit_inode(pathname, nd->path.dentry);
261667ee3ad2SAl Viro ok:
26176c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
26186c0d46c4SAl Viro 		will_truncate = 0;
26196c0d46c4SAl Viro 
26200f9d1a10SAl Viro 	if (will_truncate) {
26210f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
26220f9d1a10SAl Viro 		if (error)
26230f9d1a10SAl Viro 			goto exit;
2624ca344a89SAl Viro 		want_write = 1;
26250f9d1a10SAl Viro 	}
2626ca344a89SAl Viro common:
2627bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2628ca344a89SAl Viro 	if (error)
26290f9d1a10SAl Viro 		goto exit;
26300f9d1a10SAl Viro 	filp = nameidata_to_filp(nd);
263116b1c1cdSMiklos Szeredi 	if (filp == ERR_PTR(-EOPENSTALE) && save_parent.dentry && !retried) {
263216b1c1cdSMiklos Szeredi 		BUG_ON(save_parent.dentry != dir);
263316b1c1cdSMiklos Szeredi 		path_put(&nd->path);
263416b1c1cdSMiklos Szeredi 		nd->path = save_parent;
263516b1c1cdSMiklos Szeredi 		nd->inode = dir->d_inode;
263616b1c1cdSMiklos Szeredi 		save_parent.mnt = NULL;
263716b1c1cdSMiklos Szeredi 		save_parent.dentry = NULL;
263816b1c1cdSMiklos Szeredi 		if (want_write) {
263916b1c1cdSMiklos Szeredi 			mnt_drop_write(nd->path.mnt);
264016b1c1cdSMiklos Szeredi 			want_write = 0;
264116b1c1cdSMiklos Szeredi 		}
264216b1c1cdSMiklos Szeredi 		retried = true;
264316b1c1cdSMiklos Szeredi 		goto retry_lookup;
264416b1c1cdSMiklos Szeredi 	}
2645d18e9008SMiklos Szeredi opened:
26460f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
26470f9d1a10SAl Viro 		error = ima_file_check(filp, op->acc_mode);
26480f9d1a10SAl Viro 		if (error) {
26490f9d1a10SAl Viro 			fput(filp);
26500f9d1a10SAl Viro 			filp = ERR_PTR(error);
26510f9d1a10SAl Viro 		}
26520f9d1a10SAl Viro 	}
26530f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
26540f9d1a10SAl Viro 		if (will_truncate) {
26550f9d1a10SAl Viro 			error = handle_truncate(filp);
26560f9d1a10SAl Viro 			if (error) {
26570f9d1a10SAl Viro 				fput(filp);
26580f9d1a10SAl Viro 				filp = ERR_PTR(error);
26590f9d1a10SAl Viro 			}
26600f9d1a10SAl Viro 		}
26610f9d1a10SAl Viro 	}
2662ca344a89SAl Viro out:
2663ca344a89SAl Viro 	if (want_write)
26640f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
266516b1c1cdSMiklos Szeredi 	path_put(&save_parent);
2666e276ae67SMiklos Szeredi 	terminate_walk(nd);
2667fb1cc555SAl Viro 	return filp;
2668fb1cc555SAl Viro 
2669fb1cc555SAl Viro exit_dput:
2670fb1cc555SAl Viro 	path_put_conditional(path, nd);
2671fb1cc555SAl Viro exit:
2672ca344a89SAl Viro 	filp = ERR_PTR(error);
2673ca344a89SAl Viro 	goto out;
2674fb1cc555SAl Viro }
2675fb1cc555SAl Viro 
267613aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname,
267773d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
26781da177e4SLinus Torvalds {
2679fe2d35ffSAl Viro 	struct file *base = NULL;
26804a3fd211SDave Hansen 	struct file *filp;
26819850c056SAl Viro 	struct path path;
268213aab428SAl Viro 	int error;
268331e6b01fSNick Piggin 
268431e6b01fSNick Piggin 	filp = get_empty_filp();
268531e6b01fSNick Piggin 	if (!filp)
268631e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
268731e6b01fSNick Piggin 
268847c805dcSAl Viro 	filp->f_flags = op->open_flag;
268973d049a4SAl Viro 	nd->intent.open.file = filp;
269073d049a4SAl Viro 	nd->intent.open.flags = open_to_namei_flags(op->open_flag);
269173d049a4SAl Viro 	nd->intent.open.create_mode = op->mode;
269231e6b01fSNick Piggin 
269373d049a4SAl Viro 	error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
269431e6b01fSNick Piggin 	if (unlikely(error))
269513aab428SAl Viro 		goto out_filp;
269631e6b01fSNick Piggin 
2697fe2d35ffSAl Viro 	current->total_link_count = 0;
269873d049a4SAl Viro 	error = link_path_walk(pathname, nd);
269931e6b01fSNick Piggin 	if (unlikely(error))
270031e6b01fSNick Piggin 		goto out_filp;
27011da177e4SLinus Torvalds 
270273d049a4SAl Viro 	filp = do_last(nd, &path, op, pathname);
2703806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
27047b9337aaSNick Piggin 		struct path link = path;
2705def4af30SAl Viro 		void *cookie;
2706574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
270773d049a4SAl Viro 			path_put_conditional(&path, nd);
270873d049a4SAl Viro 			path_put(&nd->path);
270940b39136SAl Viro 			filp = ERR_PTR(-ELOOP);
271040b39136SAl Viro 			break;
271140b39136SAl Viro 		}
271273d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
271373d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2714574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
2715c3e380b0SAl Viro 		if (unlikely(error))
27166d7b5aaeSAl Viro 			goto out_filp;
271773d049a4SAl Viro 		filp = do_last(nd, &path, op, pathname);
2718574197e0SAl Viro 		put_link(nd, &link, cookie);
2719806b681cSAl Viro 	}
272010fa8e62SAl Viro out:
272173d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
272273d049a4SAl Viro 		path_put(&nd->root);
2723fe2d35ffSAl Viro 	if (base)
2724fe2d35ffSAl Viro 		fput(base);
272573d049a4SAl Viro 	release_open_intent(nd);
272616b1c1cdSMiklos Szeredi 	if (filp == ERR_PTR(-EOPENSTALE)) {
272716b1c1cdSMiklos Szeredi 		if (flags & LOOKUP_RCU)
272816b1c1cdSMiklos Szeredi 			filp = ERR_PTR(-ECHILD);
272916b1c1cdSMiklos Szeredi 		else
273016b1c1cdSMiklos Szeredi 			filp = ERR_PTR(-ESTALE);
273116b1c1cdSMiklos Szeredi 	}
273210fa8e62SAl Viro 	return filp;
27331da177e4SLinus Torvalds 
273431e6b01fSNick Piggin out_filp:
273510fa8e62SAl Viro 	filp = ERR_PTR(error);
273610fa8e62SAl Viro 	goto out;
2737de459215SKirill Korotaev }
27381da177e4SLinus Torvalds 
273913aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname,
274013aab428SAl Viro 		const struct open_flags *op, int flags)
274113aab428SAl Viro {
274273d049a4SAl Viro 	struct nameidata nd;
274313aab428SAl Viro 	struct file *filp;
274413aab428SAl Viro 
274573d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
274613aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
274773d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
274813aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
274973d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
275013aab428SAl Viro 	return filp;
275113aab428SAl Viro }
275213aab428SAl Viro 
275373d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
275473d049a4SAl Viro 		const char *name, const struct open_flags *op, int flags)
275573d049a4SAl Viro {
275673d049a4SAl Viro 	struct nameidata nd;
275773d049a4SAl Viro 	struct file *file;
275873d049a4SAl Viro 
275973d049a4SAl Viro 	nd.root.mnt = mnt;
276073d049a4SAl Viro 	nd.root.dentry = dentry;
276173d049a4SAl Viro 
276273d049a4SAl Viro 	flags |= LOOKUP_ROOT;
276373d049a4SAl Viro 
2764bcda7652SAl Viro 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
276573d049a4SAl Viro 		return ERR_PTR(-ELOOP);
276673d049a4SAl Viro 
276773d049a4SAl Viro 	file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
276873d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
276973d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags);
277073d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
277173d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
277273d049a4SAl Viro 	return file;
277373d049a4SAl Viro }
277473d049a4SAl Viro 
2775ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
27761da177e4SLinus Torvalds {
2777c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
2778ed75e95dSAl Viro 	struct nameidata nd;
2779ed75e95dSAl Viro 	int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2780ed75e95dSAl Viro 	if (error)
2781ed75e95dSAl Viro 		return ERR_PTR(error);
27821da177e4SLinus Torvalds 
2783c663e5d8SChristoph Hellwig 	/*
2784c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2785c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2786c663e5d8SChristoph Hellwig 	 */
2787ed75e95dSAl Viro 	if (nd.last_type != LAST_NORM)
2788ed75e95dSAl Viro 		goto out;
2789ed75e95dSAl Viro 	nd.flags &= ~LOOKUP_PARENT;
2790ed75e95dSAl Viro 	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2791ed75e95dSAl Viro 	nd.intent.open.flags = O_EXCL;
2792c663e5d8SChristoph Hellwig 
2793c663e5d8SChristoph Hellwig 	/*
2794c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2795c663e5d8SChristoph Hellwig 	 */
2796ed75e95dSAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2797ed75e95dSAl Viro 	dentry = lookup_hash(&nd);
27981da177e4SLinus Torvalds 	if (IS_ERR(dentry))
27991da177e4SLinus Torvalds 		goto fail;
2800c663e5d8SChristoph Hellwig 
2801e9baf6e5SAl Viro 	if (dentry->d_inode)
2802e9baf6e5SAl Viro 		goto eexist;
2803c663e5d8SChristoph Hellwig 	/*
2804c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2805c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2806c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2807c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2808c663e5d8SChristoph Hellwig 	 */
2809ed75e95dSAl Viro 	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
28101da177e4SLinus Torvalds 		dput(dentry);
28111da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2812ed75e95dSAl Viro 		goto fail;
2813e9baf6e5SAl Viro 	}
2814ed75e95dSAl Viro 	*path = nd.path;
2815e9baf6e5SAl Viro 	return dentry;
2816e9baf6e5SAl Viro eexist:
2817e9baf6e5SAl Viro 	dput(dentry);
2818e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
28191da177e4SLinus Torvalds fail:
2820dae6ad8fSAl Viro 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2821ed75e95dSAl Viro out:
2822dae6ad8fSAl Viro 	path_put(&nd.path);
2823ed75e95dSAl Viro 	return dentry;
2824dae6ad8fSAl Viro }
2825dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create);
2826dae6ad8fSAl Viro 
2827dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2828dae6ad8fSAl Viro {
2829dae6ad8fSAl Viro 	char *tmp = getname(pathname);
2830dae6ad8fSAl Viro 	struct dentry *res;
2831dae6ad8fSAl Viro 	if (IS_ERR(tmp))
2832dae6ad8fSAl Viro 		return ERR_CAST(tmp);
2833dae6ad8fSAl Viro 	res = kern_path_create(dfd, tmp, path, is_dir);
2834dae6ad8fSAl Viro 	putname(tmp);
2835dae6ad8fSAl Viro 	return res;
2836dae6ad8fSAl Viro }
2837dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create);
2838dae6ad8fSAl Viro 
28391a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
28401da177e4SLinus Torvalds {
2841a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
28421da177e4SLinus Torvalds 
28431da177e4SLinus Torvalds 	if (error)
28441da177e4SLinus Torvalds 		return error;
28451da177e4SLinus Torvalds 
2846975d6b39SEric W. Biederman 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
28471da177e4SLinus Torvalds 		return -EPERM;
28481da177e4SLinus Torvalds 
2849acfa4380SAl Viro 	if (!dir->i_op->mknod)
28501da177e4SLinus Torvalds 		return -EPERM;
28511da177e4SLinus Torvalds 
285208ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
285308ce5f16SSerge E. Hallyn 	if (error)
285408ce5f16SSerge E. Hallyn 		return error;
285508ce5f16SSerge E. Hallyn 
28561da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
28571da177e4SLinus Torvalds 	if (error)
28581da177e4SLinus Torvalds 		return error;
28591da177e4SLinus Torvalds 
28601da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2861a74574aaSStephen Smalley 	if (!error)
2862f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
28631da177e4SLinus Torvalds 	return error;
28641da177e4SLinus Torvalds }
28651da177e4SLinus Torvalds 
2866f69aac00SAl Viro static int may_mknod(umode_t mode)
2867463c3197SDave Hansen {
2868463c3197SDave Hansen 	switch (mode & S_IFMT) {
2869463c3197SDave Hansen 	case S_IFREG:
2870463c3197SDave Hansen 	case S_IFCHR:
2871463c3197SDave Hansen 	case S_IFBLK:
2872463c3197SDave Hansen 	case S_IFIFO:
2873463c3197SDave Hansen 	case S_IFSOCK:
2874463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2875463c3197SDave Hansen 		return 0;
2876463c3197SDave Hansen 	case S_IFDIR:
2877463c3197SDave Hansen 		return -EPERM;
2878463c3197SDave Hansen 	default:
2879463c3197SDave Hansen 		return -EINVAL;
2880463c3197SDave Hansen 	}
2881463c3197SDave Hansen }
2882463c3197SDave Hansen 
28838208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
28842e4d0924SHeiko Carstens 		unsigned, dev)
28851da177e4SLinus Torvalds {
28861da177e4SLinus Torvalds 	struct dentry *dentry;
2887dae6ad8fSAl Viro 	struct path path;
2888dae6ad8fSAl Viro 	int error;
28891da177e4SLinus Torvalds 
28901da177e4SLinus Torvalds 	if (S_ISDIR(mode))
28911da177e4SLinus Torvalds 		return -EPERM;
28921da177e4SLinus Torvalds 
2893dae6ad8fSAl Viro 	dentry = user_path_create(dfd, filename, &path, 0);
2894dae6ad8fSAl Viro 	if (IS_ERR(dentry))
2895dae6ad8fSAl Viro 		return PTR_ERR(dentry);
28962ad94ae6SAl Viro 
2897dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
2898ce3b0f8dSAl Viro 		mode &= ~current_umask();
2899463c3197SDave Hansen 	error = may_mknod(mode);
2900463c3197SDave Hansen 	if (error)
2901463c3197SDave Hansen 		goto out_dput;
2902dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
2903463c3197SDave Hansen 	if (error)
2904463c3197SDave Hansen 		goto out_dput;
2905dae6ad8fSAl Viro 	error = security_path_mknod(&path, dentry, mode, dev);
2906be6d3e56SKentaro Takeda 	if (error)
2907be6d3e56SKentaro Takeda 		goto out_drop_write;
29081da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
29091da177e4SLinus Torvalds 		case 0: case S_IFREG:
2910dae6ad8fSAl Viro 			error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
29111da177e4SLinus Torvalds 			break;
29121da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
2913dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
29141da177e4SLinus Torvalds 					new_decode_dev(dev));
29151da177e4SLinus Torvalds 			break;
29161da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
2917dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
29181da177e4SLinus Torvalds 			break;
29191da177e4SLinus Torvalds 	}
2920be6d3e56SKentaro Takeda out_drop_write:
2921dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
2922463c3197SDave Hansen out_dput:
29231da177e4SLinus Torvalds 	dput(dentry);
2924dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2925dae6ad8fSAl Viro 	path_put(&path);
29261da177e4SLinus Torvalds 
29271da177e4SLinus Torvalds 	return error;
29281da177e4SLinus Torvalds }
29291da177e4SLinus Torvalds 
29308208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
29315590ff0dSUlrich Drepper {
29325590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
29335590ff0dSUlrich Drepper }
29345590ff0dSUlrich Drepper 
293518bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
29361da177e4SLinus Torvalds {
2937a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
29388de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
29391da177e4SLinus Torvalds 
29401da177e4SLinus Torvalds 	if (error)
29411da177e4SLinus Torvalds 		return error;
29421da177e4SLinus Torvalds 
2943acfa4380SAl Viro 	if (!dir->i_op->mkdir)
29441da177e4SLinus Torvalds 		return -EPERM;
29451da177e4SLinus Torvalds 
29461da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
29471da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
29481da177e4SLinus Torvalds 	if (error)
29491da177e4SLinus Torvalds 		return error;
29501da177e4SLinus Torvalds 
29518de52778SAl Viro 	if (max_links && dir->i_nlink >= max_links)
29528de52778SAl Viro 		return -EMLINK;
29538de52778SAl Viro 
29541da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2955a74574aaSStephen Smalley 	if (!error)
2956f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
29571da177e4SLinus Torvalds 	return error;
29581da177e4SLinus Torvalds }
29591da177e4SLinus Torvalds 
2960a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
29611da177e4SLinus Torvalds {
29626902d925SDave Hansen 	struct dentry *dentry;
2963dae6ad8fSAl Viro 	struct path path;
2964dae6ad8fSAl Viro 	int error;
29651da177e4SLinus Torvalds 
2966dae6ad8fSAl Viro 	dentry = user_path_create(dfd, pathname, &path, 1);
29676902d925SDave Hansen 	if (IS_ERR(dentry))
2968dae6ad8fSAl Viro 		return PTR_ERR(dentry);
29696902d925SDave Hansen 
2970dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
2971ce3b0f8dSAl Viro 		mode &= ~current_umask();
2972dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
2973463c3197SDave Hansen 	if (error)
2974463c3197SDave Hansen 		goto out_dput;
2975dae6ad8fSAl Viro 	error = security_path_mkdir(&path, dentry, mode);
2976be6d3e56SKentaro Takeda 	if (error)
2977be6d3e56SKentaro Takeda 		goto out_drop_write;
2978dae6ad8fSAl Viro 	error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
2979be6d3e56SKentaro Takeda out_drop_write:
2980dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
2981463c3197SDave Hansen out_dput:
29821da177e4SLinus Torvalds 	dput(dentry);
2983dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2984dae6ad8fSAl Viro 	path_put(&path);
29851da177e4SLinus Torvalds 	return error;
29861da177e4SLinus Torvalds }
29871da177e4SLinus Torvalds 
2988a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
29895590ff0dSUlrich Drepper {
29905590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
29915590ff0dSUlrich Drepper }
29925590ff0dSUlrich Drepper 
29931da177e4SLinus Torvalds /*
2994a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
2995c0d02594SJ. Bruce Fields  * should have a usage count of 1 if we're the only user of this
2996a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
2997a71905f0SSage Weil  * then we drop the dentry now.
29981da177e4SLinus Torvalds  *
29991da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
30001da177e4SLinus Torvalds  * do a
30011da177e4SLinus Torvalds  *
30021da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
30031da177e4SLinus Torvalds  *		return -EBUSY;
30041da177e4SLinus Torvalds  *
30051da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
30061da177e4SLinus Torvalds  * that is still in use by something else..
30071da177e4SLinus Torvalds  */
30081da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
30091da177e4SLinus Torvalds {
30101da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
30111da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
301264252c75SSage Weil 	if (dentry->d_count == 1)
30131da177e4SLinus Torvalds 		__d_drop(dentry);
30141da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
30151da177e4SLinus Torvalds }
30161da177e4SLinus Torvalds 
30171da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
30181da177e4SLinus Torvalds {
30191da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
30201da177e4SLinus Torvalds 
30211da177e4SLinus Torvalds 	if (error)
30221da177e4SLinus Torvalds 		return error;
30231da177e4SLinus Torvalds 
3024acfa4380SAl Viro 	if (!dir->i_op->rmdir)
30251da177e4SLinus Torvalds 		return -EPERM;
30261da177e4SLinus Torvalds 
30271d2ef590SAl Viro 	dget(dentry);
30281b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
3029912dbc15SSage Weil 
30301da177e4SLinus Torvalds 	error = -EBUSY;
3031912dbc15SSage Weil 	if (d_mountpoint(dentry))
3032912dbc15SSage Weil 		goto out;
3033912dbc15SSage Weil 
30341da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
3035912dbc15SSage Weil 	if (error)
3036912dbc15SSage Weil 		goto out;
3037912dbc15SSage Weil 
30383cebde24SSage Weil 	shrink_dcache_parent(dentry);
30391da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
3040912dbc15SSage Weil 	if (error)
3041912dbc15SSage Weil 		goto out;
3042912dbc15SSage Weil 
30431da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
3044d83c49f3SAl Viro 	dont_mount(dentry);
30451da177e4SLinus Torvalds 
3046912dbc15SSage Weil out:
3047912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
30481d2ef590SAl Viro 	dput(dentry);
3049912dbc15SSage Weil 	if (!error)
3050912dbc15SSage Weil 		d_delete(dentry);
30511da177e4SLinus Torvalds 	return error;
30521da177e4SLinus Torvalds }
30531da177e4SLinus Torvalds 
30545590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
30551da177e4SLinus Torvalds {
30561da177e4SLinus Torvalds 	int error = 0;
30571da177e4SLinus Torvalds 	char * name;
30581da177e4SLinus Torvalds 	struct dentry *dentry;
30591da177e4SLinus Torvalds 	struct nameidata nd;
30601da177e4SLinus Torvalds 
30612ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
30621da177e4SLinus Torvalds 	if (error)
30632ad94ae6SAl Viro 		return error;
30641da177e4SLinus Torvalds 
30651da177e4SLinus Torvalds 	switch(nd.last_type) {
30661da177e4SLinus Torvalds 	case LAST_DOTDOT:
30671da177e4SLinus Torvalds 		error = -ENOTEMPTY;
30681da177e4SLinus Torvalds 		goto exit1;
30691da177e4SLinus Torvalds 	case LAST_DOT:
30701da177e4SLinus Torvalds 		error = -EINVAL;
30711da177e4SLinus Torvalds 		goto exit1;
30721da177e4SLinus Torvalds 	case LAST_ROOT:
30731da177e4SLinus Torvalds 		error = -EBUSY;
30741da177e4SLinus Torvalds 		goto exit1;
30751da177e4SLinus Torvalds 	}
30760612d9fbSOGAWA Hirofumi 
30770612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
30780612d9fbSOGAWA Hirofumi 
30794ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
308049705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
30811da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
30826902d925SDave Hansen 	if (IS_ERR(dentry))
30836902d925SDave Hansen 		goto exit2;
3084e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
3085e6bc45d6STheodore Ts'o 		error = -ENOENT;
3086e6bc45d6STheodore Ts'o 		goto exit3;
3087e6bc45d6STheodore Ts'o 	}
30880622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
30890622753bSDave Hansen 	if (error)
30900622753bSDave Hansen 		goto exit3;
3091be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
3092be6d3e56SKentaro Takeda 	if (error)
3093be6d3e56SKentaro Takeda 		goto exit4;
30944ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
3095be6d3e56SKentaro Takeda exit4:
30960622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
30970622753bSDave Hansen exit3:
30981da177e4SLinus Torvalds 	dput(dentry);
30996902d925SDave Hansen exit2:
31004ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
31011da177e4SLinus Torvalds exit1:
31021d957f9bSJan Blunck 	path_put(&nd.path);
31031da177e4SLinus Torvalds 	putname(name);
31041da177e4SLinus Torvalds 	return error;
31051da177e4SLinus Torvalds }
31061da177e4SLinus Torvalds 
31073cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
31085590ff0dSUlrich Drepper {
31095590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
31105590ff0dSUlrich Drepper }
31115590ff0dSUlrich Drepper 
31121da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
31131da177e4SLinus Torvalds {
31141da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
31151da177e4SLinus Torvalds 
31161da177e4SLinus Torvalds 	if (error)
31171da177e4SLinus Torvalds 		return error;
31181da177e4SLinus Torvalds 
3119acfa4380SAl Viro 	if (!dir->i_op->unlink)
31201da177e4SLinus Torvalds 		return -EPERM;
31211da177e4SLinus Torvalds 
31221b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
31231da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
31241da177e4SLinus Torvalds 		error = -EBUSY;
31251da177e4SLinus Torvalds 	else {
31261da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
3127bec1052eSAl Viro 		if (!error) {
31281da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
3129bec1052eSAl Viro 			if (!error)
3130d83c49f3SAl Viro 				dont_mount(dentry);
3131bec1052eSAl Viro 		}
31321da177e4SLinus Torvalds 	}
31331b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
31341da177e4SLinus Torvalds 
31351da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
31361da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
3137ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
31381da177e4SLinus Torvalds 		d_delete(dentry);
31391da177e4SLinus Torvalds 	}
31400eeca283SRobert Love 
31411da177e4SLinus Torvalds 	return error;
31421da177e4SLinus Torvalds }
31431da177e4SLinus Torvalds 
31441da177e4SLinus Torvalds /*
31451da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
31461b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
31471da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
31481da177e4SLinus Torvalds  * while waiting on the I/O.
31491da177e4SLinus Torvalds  */
31505590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
31511da177e4SLinus Torvalds {
31522ad94ae6SAl Viro 	int error;
31531da177e4SLinus Torvalds 	char *name;
31541da177e4SLinus Torvalds 	struct dentry *dentry;
31551da177e4SLinus Torvalds 	struct nameidata nd;
31561da177e4SLinus Torvalds 	struct inode *inode = NULL;
31571da177e4SLinus Torvalds 
31582ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
31591da177e4SLinus Torvalds 	if (error)
31602ad94ae6SAl Viro 		return error;
31612ad94ae6SAl Viro 
31621da177e4SLinus Torvalds 	error = -EISDIR;
31631da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
31641da177e4SLinus Torvalds 		goto exit1;
31650612d9fbSOGAWA Hirofumi 
31660612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
31670612d9fbSOGAWA Hirofumi 
31684ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
316949705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
31701da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
31711da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
31721da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
317350338b88STörök Edwin 		if (nd.last.name[nd.last.len])
317450338b88STörök Edwin 			goto slashes;
31751da177e4SLinus Torvalds 		inode = dentry->d_inode;
317650338b88STörök Edwin 		if (!inode)
3177e6bc45d6STheodore Ts'o 			goto slashes;
31787de9c6eeSAl Viro 		ihold(inode);
31790622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
31800622753bSDave Hansen 		if (error)
31810622753bSDave Hansen 			goto exit2;
3182be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
3183be6d3e56SKentaro Takeda 		if (error)
3184be6d3e56SKentaro Takeda 			goto exit3;
31854ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
3186be6d3e56SKentaro Takeda exit3:
31870622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
31881da177e4SLinus Torvalds 	exit2:
31891da177e4SLinus Torvalds 		dput(dentry);
31901da177e4SLinus Torvalds 	}
31914ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
31921da177e4SLinus Torvalds 	if (inode)
31931da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
31941da177e4SLinus Torvalds exit1:
31951d957f9bSJan Blunck 	path_put(&nd.path);
31961da177e4SLinus Torvalds 	putname(name);
31971da177e4SLinus Torvalds 	return error;
31981da177e4SLinus Torvalds 
31991da177e4SLinus Torvalds slashes:
32001da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
32011da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
32021da177e4SLinus Torvalds 	goto exit2;
32031da177e4SLinus Torvalds }
32041da177e4SLinus Torvalds 
32052e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
32065590ff0dSUlrich Drepper {
32075590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
32085590ff0dSUlrich Drepper 		return -EINVAL;
32095590ff0dSUlrich Drepper 
32105590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
32115590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
32125590ff0dSUlrich Drepper 
32135590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
32145590ff0dSUlrich Drepper }
32155590ff0dSUlrich Drepper 
32163480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
32175590ff0dSUlrich Drepper {
32185590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
32195590ff0dSUlrich Drepper }
32205590ff0dSUlrich Drepper 
3221db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
32221da177e4SLinus Torvalds {
3223a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
32241da177e4SLinus Torvalds 
32251da177e4SLinus Torvalds 	if (error)
32261da177e4SLinus Torvalds 		return error;
32271da177e4SLinus Torvalds 
3228acfa4380SAl Viro 	if (!dir->i_op->symlink)
32291da177e4SLinus Torvalds 		return -EPERM;
32301da177e4SLinus Torvalds 
32311da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
32321da177e4SLinus Torvalds 	if (error)
32331da177e4SLinus Torvalds 		return error;
32341da177e4SLinus Torvalds 
32351da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
3236a74574aaSStephen Smalley 	if (!error)
3237f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
32381da177e4SLinus Torvalds 	return error;
32391da177e4SLinus Torvalds }
32401da177e4SLinus Torvalds 
32412e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
32422e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
32431da177e4SLinus Torvalds {
32442ad94ae6SAl Viro 	int error;
32451da177e4SLinus Torvalds 	char *from;
32466902d925SDave Hansen 	struct dentry *dentry;
3247dae6ad8fSAl Viro 	struct path path;
32481da177e4SLinus Torvalds 
32491da177e4SLinus Torvalds 	from = getname(oldname);
32501da177e4SLinus Torvalds 	if (IS_ERR(from))
32511da177e4SLinus Torvalds 		return PTR_ERR(from);
32522ad94ae6SAl Viro 
3253dae6ad8fSAl Viro 	dentry = user_path_create(newdfd, newname, &path, 0);
32541da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
32556902d925SDave Hansen 	if (IS_ERR(dentry))
3256dae6ad8fSAl Viro 		goto out_putname;
32576902d925SDave Hansen 
3258dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
325975c3f29dSDave Hansen 	if (error)
326075c3f29dSDave Hansen 		goto out_dput;
3261dae6ad8fSAl Viro 	error = security_path_symlink(&path, dentry, from);
3262be6d3e56SKentaro Takeda 	if (error)
3263be6d3e56SKentaro Takeda 		goto out_drop_write;
3264dae6ad8fSAl Viro 	error = vfs_symlink(path.dentry->d_inode, dentry, from);
3265be6d3e56SKentaro Takeda out_drop_write:
3266dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
326775c3f29dSDave Hansen out_dput:
32681da177e4SLinus Torvalds 	dput(dentry);
3269dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
3270dae6ad8fSAl Viro 	path_put(&path);
32716902d925SDave Hansen out_putname:
32721da177e4SLinus Torvalds 	putname(from);
32731da177e4SLinus Torvalds 	return error;
32741da177e4SLinus Torvalds }
32751da177e4SLinus Torvalds 
32763480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
32775590ff0dSUlrich Drepper {
32785590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
32795590ff0dSUlrich Drepper }
32805590ff0dSUlrich Drepper 
32811da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
32821da177e4SLinus Torvalds {
32831da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
32848de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
32851da177e4SLinus Torvalds 	int error;
32861da177e4SLinus Torvalds 
32871da177e4SLinus Torvalds 	if (!inode)
32881da177e4SLinus Torvalds 		return -ENOENT;
32891da177e4SLinus Torvalds 
3290a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
32911da177e4SLinus Torvalds 	if (error)
32921da177e4SLinus Torvalds 		return error;
32931da177e4SLinus Torvalds 
32941da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
32951da177e4SLinus Torvalds 		return -EXDEV;
32961da177e4SLinus Torvalds 
32971da177e4SLinus Torvalds 	/*
32981da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
32991da177e4SLinus Torvalds 	 */
33001da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
33011da177e4SLinus Torvalds 		return -EPERM;
3302acfa4380SAl Viro 	if (!dir->i_op->link)
33031da177e4SLinus Torvalds 		return -EPERM;
33047e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
33051da177e4SLinus Torvalds 		return -EPERM;
33061da177e4SLinus Torvalds 
33071da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
33081da177e4SLinus Torvalds 	if (error)
33091da177e4SLinus Torvalds 		return error;
33101da177e4SLinus Torvalds 
33117e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
3312aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
3313aae8a97dSAneesh Kumar K.V 	if (inode->i_nlink == 0)
3314aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
33158de52778SAl Viro 	else if (max_links && inode->i_nlink >= max_links)
33168de52778SAl Viro 		error = -EMLINK;
3317aae8a97dSAneesh Kumar K.V 	else
33181da177e4SLinus Torvalds 		error = dir->i_op->link(old_dentry, dir, new_dentry);
33197e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
3320e31e14ecSStephen Smalley 	if (!error)
33217e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
33221da177e4SLinus Torvalds 	return error;
33231da177e4SLinus Torvalds }
33241da177e4SLinus Torvalds 
33251da177e4SLinus Torvalds /*
33261da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
33271da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
33281da177e4SLinus Torvalds  * newname.  --KAB
33291da177e4SLinus Torvalds  *
33301da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
33311da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
33321da177e4SLinus Torvalds  * and other special files.  --ADM
33331da177e4SLinus Torvalds  */
33342e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
33352e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
33361da177e4SLinus Torvalds {
33371da177e4SLinus Torvalds 	struct dentry *new_dentry;
3338dae6ad8fSAl Viro 	struct path old_path, new_path;
333911a7b371SAneesh Kumar K.V 	int how = 0;
33401da177e4SLinus Torvalds 	int error;
33411da177e4SLinus Torvalds 
334211a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3343c04030e1SUlrich Drepper 		return -EINVAL;
334411a7b371SAneesh Kumar K.V 	/*
334511a7b371SAneesh Kumar K.V 	 * To use null names we require CAP_DAC_READ_SEARCH
334611a7b371SAneesh Kumar K.V 	 * This ensures that not everyone will be able to create
334711a7b371SAneesh Kumar K.V 	 * handlink using the passed filedescriptor.
334811a7b371SAneesh Kumar K.V 	 */
334911a7b371SAneesh Kumar K.V 	if (flags & AT_EMPTY_PATH) {
335011a7b371SAneesh Kumar K.V 		if (!capable(CAP_DAC_READ_SEARCH))
335111a7b371SAneesh Kumar K.V 			return -ENOENT;
335211a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
335311a7b371SAneesh Kumar K.V 	}
3354c04030e1SUlrich Drepper 
335511a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
335611a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
335711a7b371SAneesh Kumar K.V 
335811a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
33591da177e4SLinus Torvalds 	if (error)
33602ad94ae6SAl Viro 		return error;
33612ad94ae6SAl Viro 
3362dae6ad8fSAl Viro 	new_dentry = user_path_create(newdfd, newname, &new_path, 0);
33631da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
33646902d925SDave Hansen 	if (IS_ERR(new_dentry))
3365dae6ad8fSAl Viro 		goto out;
3366dae6ad8fSAl Viro 
3367dae6ad8fSAl Viro 	error = -EXDEV;
3368dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt)
3369dae6ad8fSAl Viro 		goto out_dput;
3370dae6ad8fSAl Viro 	error = mnt_want_write(new_path.mnt);
337175c3f29dSDave Hansen 	if (error)
337275c3f29dSDave Hansen 		goto out_dput;
3373dae6ad8fSAl Viro 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
3374be6d3e56SKentaro Takeda 	if (error)
3375be6d3e56SKentaro Takeda 		goto out_drop_write;
3376dae6ad8fSAl Viro 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
3377be6d3e56SKentaro Takeda out_drop_write:
3378dae6ad8fSAl Viro 	mnt_drop_write(new_path.mnt);
337975c3f29dSDave Hansen out_dput:
33801da177e4SLinus Torvalds 	dput(new_dentry);
3381dae6ad8fSAl Viro 	mutex_unlock(&new_path.dentry->d_inode->i_mutex);
3382dae6ad8fSAl Viro 	path_put(&new_path);
33831da177e4SLinus Torvalds out:
33842d8f3038SAl Viro 	path_put(&old_path);
33851da177e4SLinus Torvalds 
33861da177e4SLinus Torvalds 	return error;
33871da177e4SLinus Torvalds }
33881da177e4SLinus Torvalds 
33893480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
33905590ff0dSUlrich Drepper {
3391c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
33925590ff0dSUlrich Drepper }
33935590ff0dSUlrich Drepper 
33941da177e4SLinus Torvalds /*
33951da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
33961da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
33971da177e4SLinus Torvalds  * Problems:
33981da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
33991da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
34001da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
3401a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
34021da177e4SLinus Torvalds  *	   story.
34031da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
34041b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
34051da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
34061da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
3407a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
34081da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
34091da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
34101da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
3411a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
34121da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
34131da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
34141da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
3415e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
34161b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
34171da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3418c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
34191da177e4SLinus Torvalds  *	   locking].
34201da177e4SLinus Torvalds  */
342175c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
34221da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
34231da177e4SLinus Torvalds {
34241da177e4SLinus Torvalds 	int error = 0;
34259055cba7SSage Weil 	struct inode *target = new_dentry->d_inode;
34268de52778SAl Viro 	unsigned max_links = new_dir->i_sb->s_max_links;
34271da177e4SLinus Torvalds 
34281da177e4SLinus Torvalds 	/*
34291da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
34301da177e4SLinus Torvalds 	 * we'll need to flip '..'.
34311da177e4SLinus Torvalds 	 */
34321da177e4SLinus Torvalds 	if (new_dir != old_dir) {
3433f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
34341da177e4SLinus Torvalds 		if (error)
34351da177e4SLinus Torvalds 			return error;
34361da177e4SLinus Torvalds 	}
34371da177e4SLinus Torvalds 
34381da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
34391da177e4SLinus Torvalds 	if (error)
34401da177e4SLinus Torvalds 		return error;
34411da177e4SLinus Torvalds 
34421d2ef590SAl Viro 	dget(new_dentry);
3443d83c49f3SAl Viro 	if (target)
34441b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
34459055cba7SSage Weil 
34461da177e4SLinus Torvalds 	error = -EBUSY;
34479055cba7SSage Weil 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
34489055cba7SSage Weil 		goto out;
34499055cba7SSage Weil 
34508de52778SAl Viro 	error = -EMLINK;
34518de52778SAl Viro 	if (max_links && !target && new_dir != old_dir &&
34528de52778SAl Viro 	    new_dir->i_nlink >= max_links)
34538de52778SAl Viro 		goto out;
34548de52778SAl Viro 
34553cebde24SSage Weil 	if (target)
34563cebde24SSage Weil 		shrink_dcache_parent(new_dentry);
34571da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
34589055cba7SSage Weil 	if (error)
34599055cba7SSage Weil 		goto out;
34609055cba7SSage Weil 
34611da177e4SLinus Torvalds 	if (target) {
34621da177e4SLinus Torvalds 		target->i_flags |= S_DEAD;
3463d83c49f3SAl Viro 		dont_mount(new_dentry);
3464d83c49f3SAl Viro 	}
34659055cba7SSage Weil out:
34669055cba7SSage Weil 	if (target)
34671b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
34681d2ef590SAl Viro 	dput(new_dentry);
3469e31e14ecSStephen Smalley 	if (!error)
3470349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
34711da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
34721da177e4SLinus Torvalds 	return error;
34731da177e4SLinus Torvalds }
34741da177e4SLinus Torvalds 
347575c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
34761da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
34771da177e4SLinus Torvalds {
347851892bbbSSage Weil 	struct inode *target = new_dentry->d_inode;
34791da177e4SLinus Torvalds 	int error;
34801da177e4SLinus Torvalds 
34811da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
34821da177e4SLinus Torvalds 	if (error)
34831da177e4SLinus Torvalds 		return error;
34841da177e4SLinus Torvalds 
34851da177e4SLinus Torvalds 	dget(new_dentry);
34861da177e4SLinus Torvalds 	if (target)
34871b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
348851892bbbSSage Weil 
34891da177e4SLinus Torvalds 	error = -EBUSY;
349051892bbbSSage Weil 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
349151892bbbSSage Weil 		goto out;
349251892bbbSSage Weil 
34931da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
349451892bbbSSage Weil 	if (error)
349551892bbbSSage Weil 		goto out;
349651892bbbSSage Weil 
3497bec1052eSAl Viro 	if (target)
3498d83c49f3SAl Viro 		dont_mount(new_dentry);
3499349457ccSMark Fasheh 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
35001da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
350151892bbbSSage Weil out:
35021da177e4SLinus Torvalds 	if (target)
35031b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
35041da177e4SLinus Torvalds 	dput(new_dentry);
35051da177e4SLinus Torvalds 	return error;
35061da177e4SLinus Torvalds }
35071da177e4SLinus Torvalds 
35081da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
35091da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
35101da177e4SLinus Torvalds {
35111da177e4SLinus Torvalds 	int error;
35121da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
351359b0df21SEric Paris 	const unsigned char *old_name;
35141da177e4SLinus Torvalds 
35151da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
35161da177e4SLinus Torvalds  		return 0;
35171da177e4SLinus Torvalds 
35181da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
35191da177e4SLinus Torvalds 	if (error)
35201da177e4SLinus Torvalds 		return error;
35211da177e4SLinus Torvalds 
35221da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3523a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
35241da177e4SLinus Torvalds 	else
35251da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
35261da177e4SLinus Torvalds 	if (error)
35271da177e4SLinus Torvalds 		return error;
35281da177e4SLinus Torvalds 
3529acfa4380SAl Viro 	if (!old_dir->i_op->rename)
35301da177e4SLinus Torvalds 		return -EPERM;
35311da177e4SLinus Torvalds 
35320eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
35330eeca283SRobert Love 
35341da177e4SLinus Torvalds 	if (is_dir)
35351da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
35361da177e4SLinus Torvalds 	else
35371da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3538123df294SAl Viro 	if (!error)
3539123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
35405a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
35410eeca283SRobert Love 	fsnotify_oldname_free(old_name);
35420eeca283SRobert Love 
35431da177e4SLinus Torvalds 	return error;
35441da177e4SLinus Torvalds }
35451da177e4SLinus Torvalds 
35462e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
35472e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
35481da177e4SLinus Torvalds {
35491da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
35501da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
35511da177e4SLinus Torvalds 	struct dentry *trap;
35521da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
35532ad94ae6SAl Viro 	char *from;
35542ad94ae6SAl Viro 	char *to;
35552ad94ae6SAl Viro 	int error;
35561da177e4SLinus Torvalds 
35572ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
35581da177e4SLinus Torvalds 	if (error)
35591da177e4SLinus Torvalds 		goto exit;
35601da177e4SLinus Torvalds 
35612ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
35621da177e4SLinus Torvalds 	if (error)
35631da177e4SLinus Torvalds 		goto exit1;
35641da177e4SLinus Torvalds 
35651da177e4SLinus Torvalds 	error = -EXDEV;
35664ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
35671da177e4SLinus Torvalds 		goto exit2;
35681da177e4SLinus Torvalds 
35694ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
35701da177e4SLinus Torvalds 	error = -EBUSY;
35711da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
35721da177e4SLinus Torvalds 		goto exit2;
35731da177e4SLinus Torvalds 
35744ac91378SJan Blunck 	new_dir = newnd.path.dentry;
35751da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
35761da177e4SLinus Torvalds 		goto exit2;
35771da177e4SLinus Torvalds 
35780612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
35790612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
35804e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
35810612d9fbSOGAWA Hirofumi 
35821da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
35831da177e4SLinus Torvalds 
358449705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
35851da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
35861da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
35871da177e4SLinus Torvalds 		goto exit3;
35881da177e4SLinus Torvalds 	/* source must exist */
35891da177e4SLinus Torvalds 	error = -ENOENT;
35901da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
35911da177e4SLinus Torvalds 		goto exit4;
35921da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
35931da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
35941da177e4SLinus Torvalds 		error = -ENOTDIR;
35951da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
35961da177e4SLinus Torvalds 			goto exit4;
35971da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
35981da177e4SLinus Torvalds 			goto exit4;
35991da177e4SLinus Torvalds 	}
36001da177e4SLinus Torvalds 	/* source should not be ancestor of target */
36011da177e4SLinus Torvalds 	error = -EINVAL;
36021da177e4SLinus Torvalds 	if (old_dentry == trap)
36031da177e4SLinus Torvalds 		goto exit4;
360449705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
36051da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
36061da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
36071da177e4SLinus Torvalds 		goto exit4;
36081da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
36091da177e4SLinus Torvalds 	error = -ENOTEMPTY;
36101da177e4SLinus Torvalds 	if (new_dentry == trap)
36111da177e4SLinus Torvalds 		goto exit5;
36121da177e4SLinus Torvalds 
36139079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
36149079b1ebSDave Hansen 	if (error)
36159079b1ebSDave Hansen 		goto exit5;
3616be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3617be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3618be6d3e56SKentaro Takeda 	if (error)
3619be6d3e56SKentaro Takeda 		goto exit6;
36201da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
36211da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3622be6d3e56SKentaro Takeda exit6:
36239079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
36241da177e4SLinus Torvalds exit5:
36251da177e4SLinus Torvalds 	dput(new_dentry);
36261da177e4SLinus Torvalds exit4:
36271da177e4SLinus Torvalds 	dput(old_dentry);
36281da177e4SLinus Torvalds exit3:
36291da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
36301da177e4SLinus Torvalds exit2:
36311d957f9bSJan Blunck 	path_put(&newnd.path);
36322ad94ae6SAl Viro 	putname(to);
36331da177e4SLinus Torvalds exit1:
36341d957f9bSJan Blunck 	path_put(&oldnd.path);
36351da177e4SLinus Torvalds 	putname(from);
36362ad94ae6SAl Viro exit:
36371da177e4SLinus Torvalds 	return error;
36381da177e4SLinus Torvalds }
36391da177e4SLinus Torvalds 
3640a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
36415590ff0dSUlrich Drepper {
36425590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
36435590ff0dSUlrich Drepper }
36445590ff0dSUlrich Drepper 
36451da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
36461da177e4SLinus Torvalds {
36471da177e4SLinus Torvalds 	int len;
36481da177e4SLinus Torvalds 
36491da177e4SLinus Torvalds 	len = PTR_ERR(link);
36501da177e4SLinus Torvalds 	if (IS_ERR(link))
36511da177e4SLinus Torvalds 		goto out;
36521da177e4SLinus Torvalds 
36531da177e4SLinus Torvalds 	len = strlen(link);
36541da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
36551da177e4SLinus Torvalds 		len = buflen;
36561da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
36571da177e4SLinus Torvalds 		len = -EFAULT;
36581da177e4SLinus Torvalds out:
36591da177e4SLinus Torvalds 	return len;
36601da177e4SLinus Torvalds }
36611da177e4SLinus Torvalds 
36621da177e4SLinus Torvalds /*
36631da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
36641da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
36651da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
36661da177e4SLinus Torvalds  */
36671da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
36681da177e4SLinus Torvalds {
36691da177e4SLinus Torvalds 	struct nameidata nd;
3670cc314eefSLinus Torvalds 	void *cookie;
3671694a1764SMarcin Slusarz 	int res;
3672cc314eefSLinus Torvalds 
36731da177e4SLinus Torvalds 	nd.depth = 0;
3674cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3675694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3676694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3677694a1764SMarcin Slusarz 
3678694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
36791da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3680cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3681694a1764SMarcin Slusarz 	return res;
36821da177e4SLinus Torvalds }
36831da177e4SLinus Torvalds 
36841da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
36851da177e4SLinus Torvalds {
36861da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
36871da177e4SLinus Torvalds }
36881da177e4SLinus Torvalds 
36891da177e4SLinus Torvalds /* get the link contents into pagecache */
36901da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
36911da177e4SLinus Torvalds {
3692ebd09abbSDuane Griffin 	char *kaddr;
36931da177e4SLinus Torvalds 	struct page *page;
36941da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3695090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
36961da177e4SLinus Torvalds 	if (IS_ERR(page))
36976fe6900eSNick Piggin 		return (char*)page;
36981da177e4SLinus Torvalds 	*ppage = page;
3699ebd09abbSDuane Griffin 	kaddr = kmap(page);
3700ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3701ebd09abbSDuane Griffin 	return kaddr;
37021da177e4SLinus Torvalds }
37031da177e4SLinus Torvalds 
37041da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
37051da177e4SLinus Torvalds {
37061da177e4SLinus Torvalds 	struct page *page = NULL;
37071da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
37081da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
37091da177e4SLinus Torvalds 	if (page) {
37101da177e4SLinus Torvalds 		kunmap(page);
37111da177e4SLinus Torvalds 		page_cache_release(page);
37121da177e4SLinus Torvalds 	}
37131da177e4SLinus Torvalds 	return res;
37141da177e4SLinus Torvalds }
37151da177e4SLinus Torvalds 
3716cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
37171da177e4SLinus Torvalds {
3718cc314eefSLinus Torvalds 	struct page *page = NULL;
37191da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3720cc314eefSLinus Torvalds 	return page;
37211da177e4SLinus Torvalds }
37221da177e4SLinus Torvalds 
3723cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
37241da177e4SLinus Torvalds {
3725cc314eefSLinus Torvalds 	struct page *page = cookie;
3726cc314eefSLinus Torvalds 
3727cc314eefSLinus Torvalds 	if (page) {
37281da177e4SLinus Torvalds 		kunmap(page);
37291da177e4SLinus Torvalds 		page_cache_release(page);
37301da177e4SLinus Torvalds 	}
37311da177e4SLinus Torvalds }
37321da177e4SLinus Torvalds 
373354566b2cSNick Piggin /*
373454566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
373554566b2cSNick Piggin  */
373654566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
37371da177e4SLinus Torvalds {
37381da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
37390adb25d2SKirill Korotaev 	struct page *page;
3740afddba49SNick Piggin 	void *fsdata;
3741beb497abSDmitriy Monakhov 	int err;
37421da177e4SLinus Torvalds 	char *kaddr;
374354566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
374454566b2cSNick Piggin 	if (nofs)
374554566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
37461da177e4SLinus Torvalds 
37477e53cac4SNeilBrown retry:
3748afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
374954566b2cSNick Piggin 				flags, &page, &fsdata);
37501da177e4SLinus Torvalds 	if (err)
3751afddba49SNick Piggin 		goto fail;
3752afddba49SNick Piggin 
3753e8e3c3d6SCong Wang 	kaddr = kmap_atomic(page);
37541da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
3755e8e3c3d6SCong Wang 	kunmap_atomic(kaddr);
3756afddba49SNick Piggin 
3757afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3758afddba49SNick Piggin 							page, fsdata);
37591da177e4SLinus Torvalds 	if (err < 0)
37601da177e4SLinus Torvalds 		goto fail;
3761afddba49SNick Piggin 	if (err < len-1)
3762afddba49SNick Piggin 		goto retry;
3763afddba49SNick Piggin 
37641da177e4SLinus Torvalds 	mark_inode_dirty(inode);
37651da177e4SLinus Torvalds 	return 0;
37661da177e4SLinus Torvalds fail:
37671da177e4SLinus Torvalds 	return err;
37681da177e4SLinus Torvalds }
37691da177e4SLinus Torvalds 
37700adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
37710adb25d2SKirill Korotaev {
37720adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
377354566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
37740adb25d2SKirill Korotaev }
37750adb25d2SKirill Korotaev 
377692e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
37771da177e4SLinus Torvalds 	.readlink	= generic_readlink,
37781da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
37791da177e4SLinus Torvalds 	.put_link	= page_put_link,
37801da177e4SLinus Torvalds };
37811da177e4SLinus Torvalds 
37822d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
3783cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
37841da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
37851da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
37861da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
37871da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
37881da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
37891da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
37901da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
37911da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
37921da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
37930adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
37941da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
37951da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
3796d1811465SAl Viro EXPORT_SYMBOL(kern_path);
379716f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3798f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
37991da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
38001da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
38011da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
38021da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
38031da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
38041da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
38051da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
38061da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
38071da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
38081da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
38091da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
38101da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
38111da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
38121da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3813