xref: /openbmc/linux/fs/namei.c (revision 3c0a6163)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/namei.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  * Some corrections by tytso.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
121da177e4SLinus Torvalds  * lookup logic.
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/init.h>
18630d9c47SPaul Gortmaker #include <linux/export.h>
1944696908SDavid S. Miller #include <linux/kernel.h>
201da177e4SLinus Torvalds #include <linux/slab.h>
211da177e4SLinus Torvalds #include <linux/fs.h>
221da177e4SLinus Torvalds #include <linux/namei.h>
231da177e4SLinus Torvalds #include <linux/pagemap.h>
240eeca283SRobert Love #include <linux/fsnotify.h>
251da177e4SLinus Torvalds #include <linux/personality.h>
261da177e4SLinus Torvalds #include <linux/security.h>
276146f0d5SMimi Zohar #include <linux/ima.h>
281da177e4SLinus Torvalds #include <linux/syscalls.h>
291da177e4SLinus Torvalds #include <linux/mount.h>
301da177e4SLinus Torvalds #include <linux/audit.h>
3116f7e0feSRandy Dunlap #include <linux/capability.h>
32834f2a4aSTrond Myklebust #include <linux/file.h>
335590ff0dSUlrich Drepper #include <linux/fcntl.h>
3408ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h>
355ad4e53bSAl Viro #include <linux/fs_struct.h>
36e77819e5SLinus Torvalds #include <linux/posix_acl.h>
371da177e4SLinus Torvalds #include <asm/uaccess.h>
381da177e4SLinus Torvalds 
39e81e3f4dSEric Paris #include "internal.h"
40c7105365SAl Viro #include "mount.h"
41e81e3f4dSEric Paris 
421da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer]
431da177e4SLinus Torvalds  * Fundamental changes in the pathname lookup mechanisms (namei)
441da177e4SLinus Torvalds  * were necessary because of omirr.  The reason is that omirr needs
451da177e4SLinus Torvalds  * to know the _real_ pathname, not the user-supplied one, in case
461da177e4SLinus Torvalds  * of symlinks (and also when transname replacements occur).
471da177e4SLinus Torvalds  *
481da177e4SLinus Torvalds  * The new code replaces the old recursive symlink resolution with
491da177e4SLinus Torvalds  * an iterative one (in case of non-nested symlink chains).  It does
501da177e4SLinus Torvalds  * this with calls to <fs>_follow_link().
511da177e4SLinus Torvalds  * As a side effect, dir_namei(), _namei() and follow_link() are now
521da177e4SLinus Torvalds  * replaced with a single function lookup_dentry() that can handle all
531da177e4SLinus Torvalds  * the special cases of the former code.
541da177e4SLinus Torvalds  *
551da177e4SLinus Torvalds  * With the new dcache, the pathname is stored at each inode, at least as
561da177e4SLinus Torvalds  * long as the refcount of the inode is positive.  As a side effect, the
571da177e4SLinus Torvalds  * size of the dcache depends on the inode cache and thus is dynamic.
581da177e4SLinus Torvalds  *
591da177e4SLinus Torvalds  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
601da177e4SLinus Torvalds  * resolution to correspond with current state of the code.
611da177e4SLinus Torvalds  *
621da177e4SLinus Torvalds  * Note that the symlink resolution is not *completely* iterative.
631da177e4SLinus Torvalds  * There is still a significant amount of tail- and mid- recursion in
641da177e4SLinus Torvalds  * the algorithm.  Also, note that <fs>_readlink() is not used in
651da177e4SLinus Torvalds  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
661da177e4SLinus Torvalds  * may return different results than <fs>_follow_link().  Many virtual
671da177e4SLinus Torvalds  * filesystems (including /proc) exhibit this behavior.
681da177e4SLinus Torvalds  */
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
711da177e4SLinus Torvalds  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
721da177e4SLinus Torvalds  * and the name already exists in form of a symlink, try to create the new
731da177e4SLinus Torvalds  * name indicated by the symlink. The old code always complained that the
741da177e4SLinus Torvalds  * name already exists, due to not following the symlink even if its target
751da177e4SLinus Torvalds  * is nonexistent.  The new semantics affects also mknod() and link() when
7625985edcSLucas De Marchi  * the name is a symlink pointing to a non-existent name.
771da177e4SLinus Torvalds  *
781da177e4SLinus Torvalds  * I don't know which semantics is the right one, since I have no access
791da177e4SLinus Torvalds  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
801da177e4SLinus Torvalds  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
811da177e4SLinus Torvalds  * "old" one. Personally, I think the new semantics is much more logical.
821da177e4SLinus Torvalds  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
831da177e4SLinus Torvalds  * file does succeed in both HP-UX and SunOs, but not in Solaris
841da177e4SLinus Torvalds  * and in the old Linux semantics.
851da177e4SLinus Torvalds  */
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
881da177e4SLinus Torvalds  * semantics.  See the comments in "open_namei" and "do_link" below.
891da177e4SLinus Torvalds  *
901da177e4SLinus Torvalds  * [10-Sep-98 Alan Modra] Another symlink change.
911da177e4SLinus Torvalds  */
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
941da177e4SLinus Torvalds  *	inside the path - always follow.
951da177e4SLinus Torvalds  *	in the last component in creation/removal/renaming - never follow.
961da177e4SLinus Torvalds  *	if LOOKUP_FOLLOW passed - follow.
971da177e4SLinus Torvalds  *	if the pathname has trailing slashes - follow.
981da177e4SLinus Torvalds  *	otherwise - don't follow.
991da177e4SLinus Torvalds  * (applied in that order).
1001da177e4SLinus Torvalds  *
1011da177e4SLinus Torvalds  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
1021da177e4SLinus Torvalds  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
1031da177e4SLinus Torvalds  * During the 2.4 we need to fix the userland stuff depending on it -
1041da177e4SLinus Torvalds  * hopefully we will be able to get rid of that wart in 2.5. So far only
1051da177e4SLinus Torvalds  * XEmacs seems to be relying on it...
1061da177e4SLinus Torvalds  */
1071da177e4SLinus Torvalds /*
1081da177e4SLinus Torvalds  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
109a11f3a05SArjan van de Ven  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
1101da177e4SLinus Torvalds  * any extra contention...
1111da177e4SLinus Torvalds  */
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional
1141da177e4SLinus Torvalds  * checking and hopefully speeding things up, we copy filenames to the
1151da177e4SLinus Torvalds  * kernel data space before using them..
1161da177e4SLinus Torvalds  *
1171da177e4SLinus Torvalds  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
1181da177e4SLinus Torvalds  * PATH_MAX includes the nul terminator --RR.
1191da177e4SLinus Torvalds  */
1201fa1e7f6SAndy Whitcroft static char *getname_flags(const char __user *filename, int flags, int *empty)
1211da177e4SLinus Torvalds {
1223f9f0aa6SLinus Torvalds 	char *result = __getname(), *err;
1233f9f0aa6SLinus Torvalds 	int len;
1241da177e4SLinus Torvalds 
1253f9f0aa6SLinus Torvalds 	if (unlikely(!result))
1264043cde8SEric Paris 		return ERR_PTR(-ENOMEM);
1271da177e4SLinus Torvalds 
1283f9f0aa6SLinus Torvalds 	len = strncpy_from_user(result, filename, PATH_MAX);
1293f9f0aa6SLinus Torvalds 	err = ERR_PTR(len);
1303f9f0aa6SLinus Torvalds 	if (unlikely(len < 0))
1313f9f0aa6SLinus Torvalds 		goto error;
1323f9f0aa6SLinus Torvalds 
1333f9f0aa6SLinus Torvalds 	/* The empty path is special. */
1343f9f0aa6SLinus Torvalds 	if (unlikely(!len)) {
1353f9f0aa6SLinus Torvalds 		if (empty)
1361fa1e7f6SAndy Whitcroft 			*empty = 1;
1373f9f0aa6SLinus Torvalds 		err = ERR_PTR(-ENOENT);
1383f9f0aa6SLinus Torvalds 		if (!(flags & LOOKUP_EMPTY))
1393f9f0aa6SLinus Torvalds 			goto error;
1401da177e4SLinus Torvalds 	}
1413f9f0aa6SLinus Torvalds 
1423f9f0aa6SLinus Torvalds 	err = ERR_PTR(-ENAMETOOLONG);
1433f9f0aa6SLinus Torvalds 	if (likely(len < PATH_MAX)) {
1441da177e4SLinus Torvalds 		audit_getname(result);
1451da177e4SLinus Torvalds 		return result;
1461da177e4SLinus Torvalds 	}
1471da177e4SLinus Torvalds 
1483f9f0aa6SLinus Torvalds error:
1493f9f0aa6SLinus Torvalds 	__putname(result);
1503f9f0aa6SLinus Torvalds 	return err;
1513f9f0aa6SLinus Torvalds }
1523f9f0aa6SLinus Torvalds 
153f52e0c11SAl Viro char *getname(const char __user * filename)
154f52e0c11SAl Viro {
155f7493e5dSLinus Torvalds 	return getname_flags(filename, 0, NULL);
156f52e0c11SAl Viro }
157f52e0c11SAl Viro 
1581da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
1591da177e4SLinus Torvalds void putname(const char *name)
1601da177e4SLinus Torvalds {
1615ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
1621da177e4SLinus Torvalds 		audit_putname(name);
1631da177e4SLinus Torvalds 	else
1641da177e4SLinus Torvalds 		__putname(name);
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds EXPORT_SYMBOL(putname);
1671da177e4SLinus Torvalds #endif
1681da177e4SLinus Torvalds 
169e77819e5SLinus Torvalds static int check_acl(struct inode *inode, int mask)
170e77819e5SLinus Torvalds {
17184635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL
172e77819e5SLinus Torvalds 	struct posix_acl *acl;
173e77819e5SLinus Torvalds 
174e77819e5SLinus Torvalds 	if (mask & MAY_NOT_BLOCK) {
1753567866bSAl Viro 		acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
1763567866bSAl Viro 	        if (!acl)
177e77819e5SLinus Torvalds 	                return -EAGAIN;
1783567866bSAl Viro 		/* no ->get_acl() calls in RCU mode... */
1793567866bSAl Viro 		if (acl == ACL_NOT_CACHED)
180e77819e5SLinus Torvalds 			return -ECHILD;
181206b1d09SAri Savolainen 	        return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
182e77819e5SLinus Torvalds 	}
183e77819e5SLinus Torvalds 
184e77819e5SLinus Torvalds 	acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
185e77819e5SLinus Torvalds 
186e77819e5SLinus Torvalds 	/*
1874e34e719SChristoph Hellwig 	 * A filesystem can force a ACL callback by just never filling the
1884e34e719SChristoph Hellwig 	 * ACL cache. But normally you'd fill the cache either at inode
1894e34e719SChristoph Hellwig 	 * instantiation time, or on the first ->get_acl call.
190e77819e5SLinus Torvalds 	 *
1914e34e719SChristoph Hellwig 	 * If the filesystem doesn't have a get_acl() function at all, we'll
1924e34e719SChristoph Hellwig 	 * just create the negative cache entry.
193e77819e5SLinus Torvalds 	 */
194e77819e5SLinus Torvalds 	if (acl == ACL_NOT_CACHED) {
1954e34e719SChristoph Hellwig 	        if (inode->i_op->get_acl) {
1964e34e719SChristoph Hellwig 			acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
1974e34e719SChristoph Hellwig 			if (IS_ERR(acl))
1984e34e719SChristoph Hellwig 				return PTR_ERR(acl);
1994e34e719SChristoph Hellwig 		} else {
200e77819e5SLinus Torvalds 		        set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
201e77819e5SLinus Torvalds 		        return -EAGAIN;
202e77819e5SLinus Torvalds 		}
2034e34e719SChristoph Hellwig 	}
204e77819e5SLinus Torvalds 
205e77819e5SLinus Torvalds 	if (acl) {
206e77819e5SLinus Torvalds 	        int error = posix_acl_permission(inode, acl, mask);
207e77819e5SLinus Torvalds 	        posix_acl_release(acl);
208e77819e5SLinus Torvalds 	        return error;
209e77819e5SLinus Torvalds 	}
21084635d68SLinus Torvalds #endif
211e77819e5SLinus Torvalds 
212e77819e5SLinus Torvalds 	return -EAGAIN;
213e77819e5SLinus Torvalds }
214e77819e5SLinus Torvalds 
2155909ccaaSLinus Torvalds /*
216948409c7SAndreas Gruenbacher  * This does the basic permission checking
2175909ccaaSLinus Torvalds  */
2187e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask)
2195909ccaaSLinus Torvalds {
22026cf46beSLinus Torvalds 	unsigned int mode = inode->i_mode;
2215909ccaaSLinus Torvalds 
2228e96e3b7SEric W. Biederman 	if (likely(uid_eq(current_fsuid(), inode->i_uid)))
2235909ccaaSLinus Torvalds 		mode >>= 6;
2245909ccaaSLinus Torvalds 	else {
225e77819e5SLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
2267e40145eSAl Viro 			int error = check_acl(inode, mask);
2275909ccaaSLinus Torvalds 			if (error != -EAGAIN)
2285909ccaaSLinus Torvalds 				return error;
2295909ccaaSLinus Torvalds 		}
2305909ccaaSLinus Torvalds 
2315909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
2325909ccaaSLinus Torvalds 			mode >>= 3;
2335909ccaaSLinus Torvalds 	}
2345909ccaaSLinus Torvalds 
2355909ccaaSLinus Torvalds 	/*
2365909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
2375909ccaaSLinus Torvalds 	 */
2389c2c7039SAl Viro 	if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
2395909ccaaSLinus Torvalds 		return 0;
2405909ccaaSLinus Torvalds 	return -EACCES;
2415909ccaaSLinus Torvalds }
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds /**
2441da177e4SLinus Torvalds  * generic_permission -  check for access rights on a Posix-like filesystem
2451da177e4SLinus Torvalds  * @inode:	inode to check access rights for
2468fd90c8dSAndreas Gruenbacher  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
2471da177e4SLinus Torvalds  *
2481da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
2491da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
2501da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
251b74c79e9SNick Piggin  * are used for other things.
252b74c79e9SNick Piggin  *
253b74c79e9SNick Piggin  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
254b74c79e9SNick Piggin  * request cannot be satisfied (eg. requires blocking or too much complexity).
255b74c79e9SNick Piggin  * It would then be called again in ref-walk mode.
2561da177e4SLinus Torvalds  */
2572830ba7fSAl Viro int generic_permission(struct inode *inode, int mask)
2581da177e4SLinus Torvalds {
2595909ccaaSLinus Torvalds 	int ret;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	/*
262948409c7SAndreas Gruenbacher 	 * Do the basic permission checks.
2631da177e4SLinus Torvalds 	 */
2647e40145eSAl Viro 	ret = acl_permission_check(inode, mask);
2655909ccaaSLinus Torvalds 	if (ret != -EACCES)
2665909ccaaSLinus Torvalds 		return ret;
2671da177e4SLinus Torvalds 
268d594e7ecSAl Viro 	if (S_ISDIR(inode->i_mode)) {
269d594e7ecSAl Viro 		/* DACs are overridable for directories */
2701a48e2acSEric W. Biederman 		if (inode_capable(inode, CAP_DAC_OVERRIDE))
271d594e7ecSAl Viro 			return 0;
272d594e7ecSAl Viro 		if (!(mask & MAY_WRITE))
2731a48e2acSEric W. Biederman 			if (inode_capable(inode, CAP_DAC_READ_SEARCH))
274d594e7ecSAl Viro 				return 0;
275d594e7ecSAl Viro 		return -EACCES;
276d594e7ecSAl Viro 	}
2771da177e4SLinus Torvalds 	/*
2781da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
279d594e7ecSAl Viro 	 * Executable DACs are overridable when there is
280d594e7ecSAl Viro 	 * at least one exec bit set.
2811da177e4SLinus Torvalds 	 */
282d594e7ecSAl Viro 	if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
2831a48e2acSEric W. Biederman 		if (inode_capable(inode, CAP_DAC_OVERRIDE))
2841da177e4SLinus Torvalds 			return 0;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	/*
2871da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
2881da177e4SLinus Torvalds 	 */
2897ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
290d594e7ecSAl Viro 	if (mask == MAY_READ)
2911a48e2acSEric W. Biederman 		if (inode_capable(inode, CAP_DAC_READ_SEARCH))
2921da177e4SLinus Torvalds 			return 0;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	return -EACCES;
2951da177e4SLinus Torvalds }
2961da177e4SLinus Torvalds 
2973ddcd056SLinus Torvalds /*
2983ddcd056SLinus Torvalds  * We _really_ want to just do "generic_permission()" without
2993ddcd056SLinus Torvalds  * even looking at the inode->i_op values. So we keep a cache
3003ddcd056SLinus Torvalds  * flag in inode->i_opflags, that says "this has not special
3013ddcd056SLinus Torvalds  * permission function, use the fast case".
3023ddcd056SLinus Torvalds  */
3033ddcd056SLinus Torvalds static inline int do_inode_permission(struct inode *inode, int mask)
3043ddcd056SLinus Torvalds {
3053ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
3063ddcd056SLinus Torvalds 		if (likely(inode->i_op->permission))
3073ddcd056SLinus Torvalds 			return inode->i_op->permission(inode, mask);
3083ddcd056SLinus Torvalds 
3093ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
3103ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
3113ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_FASTPERM;
3123ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
3133ddcd056SLinus Torvalds 	}
3143ddcd056SLinus Torvalds 	return generic_permission(inode, mask);
3153ddcd056SLinus Torvalds }
3163ddcd056SLinus Torvalds 
317cb23beb5SChristoph Hellwig /**
3180bdaea90SDavid Howells  * __inode_permission - Check for access rights to a given inode
3190bdaea90SDavid Howells  * @inode: Inode to check permission on
3200bdaea90SDavid Howells  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
321cb23beb5SChristoph Hellwig  *
3220bdaea90SDavid Howells  * Check for read/write/execute permissions on an inode.
323948409c7SAndreas Gruenbacher  *
324948409c7SAndreas Gruenbacher  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
3250bdaea90SDavid Howells  *
3260bdaea90SDavid Howells  * This does not check for a read-only file system.  You probably want
3270bdaea90SDavid Howells  * inode_permission().
328cb23beb5SChristoph Hellwig  */
3290bdaea90SDavid Howells int __inode_permission(struct inode *inode, int mask)
3301da177e4SLinus Torvalds {
331e6305c43SAl Viro 	int retval;
3321da177e4SLinus Torvalds 
3333ddcd056SLinus Torvalds 	if (unlikely(mask & MAY_WRITE)) {
3341da177e4SLinus Torvalds 		/*
3351da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
3361da177e4SLinus Torvalds 		 */
3371da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
3381da177e4SLinus Torvalds 			return -EACCES;
3391da177e4SLinus Torvalds 	}
3401da177e4SLinus Torvalds 
3413ddcd056SLinus Torvalds 	retval = do_inode_permission(inode, mask);
3421da177e4SLinus Torvalds 	if (retval)
3431da177e4SLinus Torvalds 		return retval;
3441da177e4SLinus Torvalds 
34508ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
34608ce5f16SSerge E. Hallyn 	if (retval)
34708ce5f16SSerge E. Hallyn 		return retval;
34808ce5f16SSerge E. Hallyn 
349d09ca739SEric Paris 	return security_inode_permission(inode, mask);
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds 
352f4d6ff89SAl Viro /**
3530bdaea90SDavid Howells  * sb_permission - Check superblock-level permissions
3540bdaea90SDavid Howells  * @sb: Superblock of inode to check permission on
3550bdaea90SDavid Howells  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
3560bdaea90SDavid Howells  *
3570bdaea90SDavid Howells  * Separate out file-system wide checks from inode-specific permission checks.
3580bdaea90SDavid Howells  */
3590bdaea90SDavid Howells static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
3600bdaea90SDavid Howells {
3610bdaea90SDavid Howells 	if (unlikely(mask & MAY_WRITE)) {
3620bdaea90SDavid Howells 		umode_t mode = inode->i_mode;
3630bdaea90SDavid Howells 
3640bdaea90SDavid Howells 		/* Nobody gets write access to a read-only fs. */
3650bdaea90SDavid Howells 		if ((sb->s_flags & MS_RDONLY) &&
3660bdaea90SDavid Howells 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
3670bdaea90SDavid Howells 			return -EROFS;
3680bdaea90SDavid Howells 	}
3690bdaea90SDavid Howells 	return 0;
3700bdaea90SDavid Howells }
3710bdaea90SDavid Howells 
3720bdaea90SDavid Howells /**
3730bdaea90SDavid Howells  * inode_permission - Check for access rights to a given inode
3740bdaea90SDavid Howells  * @inode: Inode to check permission on
3750bdaea90SDavid Howells  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
3760bdaea90SDavid Howells  *
3770bdaea90SDavid Howells  * Check for read/write/execute permissions on an inode.  We use fs[ug]id for
3780bdaea90SDavid Howells  * this, letting us set arbitrary permissions for filesystem access without
3790bdaea90SDavid Howells  * changing the "normal" UIDs which are used for other things.
3800bdaea90SDavid Howells  *
3810bdaea90SDavid Howells  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
3820bdaea90SDavid Howells  */
3830bdaea90SDavid Howells int inode_permission(struct inode *inode, int mask)
3840bdaea90SDavid Howells {
3850bdaea90SDavid Howells 	int retval;
3860bdaea90SDavid Howells 
3870bdaea90SDavid Howells 	retval = sb_permission(inode->i_sb, inode, mask);
3880bdaea90SDavid Howells 	if (retval)
3890bdaea90SDavid Howells 		return retval;
3900bdaea90SDavid Howells 	return __inode_permission(inode, mask);
3910bdaea90SDavid Howells }
3920bdaea90SDavid Howells 
3930bdaea90SDavid Howells /**
3945dd784d0SJan Blunck  * path_get - get a reference to a path
3955dd784d0SJan Blunck  * @path: path to get the reference to
3965dd784d0SJan Blunck  *
3975dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
3985dd784d0SJan Blunck  */
3995dd784d0SJan Blunck void path_get(struct path *path)
4005dd784d0SJan Blunck {
4015dd784d0SJan Blunck 	mntget(path->mnt);
4025dd784d0SJan Blunck 	dget(path->dentry);
4035dd784d0SJan Blunck }
4045dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
4055dd784d0SJan Blunck 
4065dd784d0SJan Blunck /**
4071d957f9bSJan Blunck  * path_put - put a reference to a path
4081d957f9bSJan Blunck  * @path: path to put the reference to
4091d957f9bSJan Blunck  *
4101d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
4111d957f9bSJan Blunck  */
4121d957f9bSJan Blunck void path_put(struct path *path)
4131da177e4SLinus Torvalds {
4141d957f9bSJan Blunck 	dput(path->dentry);
4151d957f9bSJan Blunck 	mntput(path->mnt);
4161da177e4SLinus Torvalds }
4171d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
4181da177e4SLinus Torvalds 
41919660af7SAl Viro /*
42031e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
42119660af7SAl Viro  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
42219660af7SAl Viro  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
42319660af7SAl Viro  * normal reference counts on dentries and vfsmounts to transition to rcu-walk
42419660af7SAl Viro  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
42519660af7SAl Viro  * got stuck, so ref-walk may continue from there. If this is not successful
42619660af7SAl Viro  * (eg. a seqcount has changed), then failure is returned and it's up to caller
42719660af7SAl Viro  * to restart the path walk from the beginning in ref-walk mode.
42831e6b01fSNick Piggin  */
42931e6b01fSNick Piggin 
43031e6b01fSNick Piggin /**
43119660af7SAl Viro  * unlazy_walk - try to switch to ref-walk mode.
43219660af7SAl Viro  * @nd: nameidata pathwalk data
43319660af7SAl Viro  * @dentry: child of nd->path.dentry or NULL
43439191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
43531e6b01fSNick Piggin  *
43619660af7SAl Viro  * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
43719660af7SAl Viro  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
43819660af7SAl Viro  * @nd or NULL.  Must be called from rcu-walk context.
43931e6b01fSNick Piggin  */
44019660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
44131e6b01fSNick Piggin {
44231e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
44331e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
4445b6ca027SAl Viro 	int want_root = 0;
44531e6b01fSNick Piggin 
44631e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
4475b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
4485b6ca027SAl Viro 		want_root = 1;
44931e6b01fSNick Piggin 		spin_lock(&fs->lock);
45031e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
45131e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
45231e6b01fSNick Piggin 			goto err_root;
45331e6b01fSNick Piggin 	}
45431e6b01fSNick Piggin 	spin_lock(&parent->d_lock);
45519660af7SAl Viro 	if (!dentry) {
45619660af7SAl Viro 		if (!__d_rcu_to_refcount(parent, nd->seq))
45719660af7SAl Viro 			goto err_parent;
45819660af7SAl Viro 		BUG_ON(nd->inode != parent->d_inode);
45919660af7SAl Viro 	} else {
46094c0d4ecSAl Viro 		if (dentry->d_parent != parent)
46194c0d4ecSAl Viro 			goto err_parent;
46231e6b01fSNick Piggin 		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
46331e6b01fSNick Piggin 		if (!__d_rcu_to_refcount(dentry, nd->seq))
46419660af7SAl Viro 			goto err_child;
46531e6b01fSNick Piggin 		/*
46619660af7SAl Viro 		 * If the sequence check on the child dentry passed, then
46719660af7SAl Viro 		 * the child has not been removed from its parent. This
46819660af7SAl Viro 		 * means the parent dentry must be valid and able to take
46919660af7SAl Viro 		 * a reference at this point.
47031e6b01fSNick Piggin 		 */
47131e6b01fSNick Piggin 		BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
47231e6b01fSNick Piggin 		BUG_ON(!parent->d_count);
47331e6b01fSNick Piggin 		parent->d_count++;
47431e6b01fSNick Piggin 		spin_unlock(&dentry->d_lock);
47519660af7SAl Viro 	}
47631e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
4775b6ca027SAl Viro 	if (want_root) {
47831e6b01fSNick Piggin 		path_get(&nd->root);
47931e6b01fSNick Piggin 		spin_unlock(&fs->lock);
48031e6b01fSNick Piggin 	}
48131e6b01fSNick Piggin 	mntget(nd->path.mnt);
48231e6b01fSNick Piggin 
48331e6b01fSNick Piggin 	rcu_read_unlock();
484962830dfSAndi Kleen 	br_read_unlock(&vfsmount_lock);
48531e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
48631e6b01fSNick Piggin 	return 0;
48719660af7SAl Viro 
48819660af7SAl Viro err_child:
48931e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
49019660af7SAl Viro err_parent:
49131e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
49231e6b01fSNick Piggin err_root:
4935b6ca027SAl Viro 	if (want_root)
49431e6b01fSNick Piggin 		spin_unlock(&fs->lock);
49531e6b01fSNick Piggin 	return -ECHILD;
49631e6b01fSNick Piggin }
49731e6b01fSNick Piggin 
4984ce16ef3SAl Viro static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
49934286d66SNick Piggin {
5004ce16ef3SAl Viro 	return dentry->d_op->d_revalidate(dentry, flags);
50134286d66SNick Piggin }
50234286d66SNick Piggin 
5039f1fafeeSAl Viro /**
5049f1fafeeSAl Viro  * complete_walk - successful completion of path walk
5059f1fafeeSAl Viro  * @nd:  pointer nameidata
50639159de2SJeff Layton  *
5079f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
5089f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
5099f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
5109f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
5119f1fafeeSAl Viro  * need to drop nd->path.
51239159de2SJeff Layton  */
5139f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
51439159de2SJeff Layton {
51516c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
51639159de2SJeff Layton 	int status;
51739159de2SJeff Layton 
5189f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
5199f1fafeeSAl Viro 		nd->flags &= ~LOOKUP_RCU;
5209f1fafeeSAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
5219f1fafeeSAl Viro 			nd->root.mnt = NULL;
5229f1fafeeSAl Viro 		spin_lock(&dentry->d_lock);
5239f1fafeeSAl Viro 		if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
5249f1fafeeSAl Viro 			spin_unlock(&dentry->d_lock);
5259f1fafeeSAl Viro 			rcu_read_unlock();
526962830dfSAndi Kleen 			br_read_unlock(&vfsmount_lock);
5279f1fafeeSAl Viro 			return -ECHILD;
5289f1fafeeSAl Viro 		}
5299f1fafeeSAl Viro 		BUG_ON(nd->inode != dentry->d_inode);
5309f1fafeeSAl Viro 		spin_unlock(&dentry->d_lock);
5319f1fafeeSAl Viro 		mntget(nd->path.mnt);
5329f1fafeeSAl Viro 		rcu_read_unlock();
533962830dfSAndi Kleen 		br_read_unlock(&vfsmount_lock);
5349f1fafeeSAl Viro 	}
5359f1fafeeSAl Viro 
53616c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
53739159de2SJeff Layton 		return 0;
53839159de2SJeff Layton 
53916c2cd71SAl Viro 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
54016c2cd71SAl Viro 		return 0;
54116c2cd71SAl Viro 
54216c2cd71SAl Viro 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
54316c2cd71SAl Viro 		return 0;
54416c2cd71SAl Viro 
54516c2cd71SAl Viro 	/* Note: we do not d_invalidate() */
5464ce16ef3SAl Viro 	status = d_revalidate(dentry, nd->flags);
54739159de2SJeff Layton 	if (status > 0)
54839159de2SJeff Layton 		return 0;
54939159de2SJeff Layton 
55016c2cd71SAl Viro 	if (!status)
55139159de2SJeff Layton 		status = -ESTALE;
55216c2cd71SAl Viro 
5539f1fafeeSAl Viro 	path_put(&nd->path);
55439159de2SJeff Layton 	return status;
55539159de2SJeff Layton }
55639159de2SJeff Layton 
5572a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
5582a737871SAl Viro {
559f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
560f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
5612a737871SAl Viro }
5622a737871SAl Viro 
5636de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
5646de88d72SAl Viro 
56531e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
56631e6b01fSNick Piggin {
56731e6b01fSNick Piggin 	if (!nd->root.mnt) {
56831e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
569c28cc364SNick Piggin 		unsigned seq;
570c28cc364SNick Piggin 
571c28cc364SNick Piggin 		do {
572c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
57331e6b01fSNick Piggin 			nd->root = fs->root;
574c1530019STim Chen 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
575c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
57631e6b01fSNick Piggin 	}
57731e6b01fSNick Piggin }
57831e6b01fSNick Piggin 
579f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
5801da177e4SLinus Torvalds {
58131e6b01fSNick Piggin 	int ret;
58231e6b01fSNick Piggin 
5831da177e4SLinus Torvalds 	if (IS_ERR(link))
5841da177e4SLinus Torvalds 		goto fail;
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	if (*link == '/') {
5872a737871SAl Viro 		set_root(nd);
5881d957f9bSJan Blunck 		path_put(&nd->path);
5892a737871SAl Viro 		nd->path = nd->root;
5902a737871SAl Viro 		path_get(&nd->root);
59116c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
5921da177e4SLinus Torvalds 	}
59331e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
594b4091d5fSChristoph Hellwig 
59531e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
59631e6b01fSNick Piggin 	return ret;
5971da177e4SLinus Torvalds fail:
5981d957f9bSJan Blunck 	path_put(&nd->path);
5991da177e4SLinus Torvalds 	return PTR_ERR(link);
6001da177e4SLinus Torvalds }
6011da177e4SLinus Torvalds 
6021d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
603051d3812SIan Kent {
604051d3812SIan Kent 	dput(path->dentry);
6054ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
606051d3812SIan Kent 		mntput(path->mnt);
607051d3812SIan Kent }
608051d3812SIan Kent 
6097b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
6107b9337aaSNick Piggin 					struct nameidata *nd)
611051d3812SIan Kent {
61231e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
6134ac91378SJan Blunck 		dput(nd->path.dentry);
61431e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
6154ac91378SJan Blunck 			mntput(nd->path.mnt);
6169a229683SHuang Shijie 	}
61731e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
6184ac91378SJan Blunck 	nd->path.dentry = path->dentry;
619051d3812SIan Kent }
620051d3812SIan Kent 
621b5fb63c1SChristoph Hellwig /*
622b5fb63c1SChristoph Hellwig  * Helper to directly jump to a known parsed path from ->follow_link,
623b5fb63c1SChristoph Hellwig  * caller must have taken a reference to path beforehand.
624b5fb63c1SChristoph Hellwig  */
625b5fb63c1SChristoph Hellwig void nd_jump_link(struct nameidata *nd, struct path *path)
626b5fb63c1SChristoph Hellwig {
627b5fb63c1SChristoph Hellwig 	path_put(&nd->path);
628b5fb63c1SChristoph Hellwig 
629b5fb63c1SChristoph Hellwig 	nd->path = *path;
630b5fb63c1SChristoph Hellwig 	nd->inode = nd->path.dentry->d_inode;
631b5fb63c1SChristoph Hellwig 	nd->flags |= LOOKUP_JUMPED;
632b5fb63c1SChristoph Hellwig 
633b5fb63c1SChristoph Hellwig 	BUG_ON(nd->inode->i_op->follow_link);
634b5fb63c1SChristoph Hellwig }
635b5fb63c1SChristoph Hellwig 
636574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
637574197e0SAl Viro {
638574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
6396d7b5aaeSAl Viro 	if (inode->i_op->put_link)
640574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
641574197e0SAl Viro 	path_put(link);
642574197e0SAl Viro }
643574197e0SAl Viro 
644def4af30SAl Viro static __always_inline int
645574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
6461da177e4SLinus Torvalds {
6477b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
6486d7b5aaeSAl Viro 	int error;
6496d7b5aaeSAl Viro 	char *s;
6501da177e4SLinus Torvalds 
651844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
652844a3917SAl Viro 
6530e794589SAl Viro 	if (link->mnt == nd->path.mnt)
6540e794589SAl Viro 		mntget(link->mnt);
6550e794589SAl Viro 
6566d7b5aaeSAl Viro 	error = -ELOOP;
6576d7b5aaeSAl Viro 	if (unlikely(current->total_link_count >= 40))
6586d7b5aaeSAl Viro 		goto out_put_nd_path;
6596d7b5aaeSAl Viro 
660574197e0SAl Viro 	cond_resched();
661574197e0SAl Viro 	current->total_link_count++;
662574197e0SAl Viro 
66368ac1234SAl Viro 	touch_atime(link);
6641da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
665cd4e91d3SAl Viro 
66636f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
6676d7b5aaeSAl Viro 	if (error)
6686d7b5aaeSAl Viro 		goto out_put_nd_path;
66936f3b4f6SAl Viro 
67086acdca1SAl Viro 	nd->last_type = LAST_BIND;
671def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
672def4af30SAl Viro 	error = PTR_ERR(*p);
6736d7b5aaeSAl Viro 	if (IS_ERR(*p))
674408ef013SChristoph Hellwig 		goto out_put_nd_path;
6756d7b5aaeSAl Viro 
676cc314eefSLinus Torvalds 	error = 0;
6776d7b5aaeSAl Viro 	s = nd_get_link(nd);
6786d7b5aaeSAl Viro 	if (s) {
6791da177e4SLinus Torvalds 		error = __vfs_follow_link(nd, s);
6806d7b5aaeSAl Viro 		if (unlikely(error))
6816d7b5aaeSAl Viro 			put_link(nd, link, *p);
682b5fb63c1SChristoph Hellwig 	}
6836d7b5aaeSAl Viro 
6846d7b5aaeSAl Viro 	return error;
6856d7b5aaeSAl Viro 
6866d7b5aaeSAl Viro out_put_nd_path:
6876d7b5aaeSAl Viro 	path_put(&nd->path);
6886d7b5aaeSAl Viro 	path_put(link);
6891da177e4SLinus Torvalds 	return error;
6901da177e4SLinus Torvalds }
6911da177e4SLinus Torvalds 
69231e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
69331e6b01fSNick Piggin {
6940714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
6950714a533SAl Viro 	struct mount *parent;
69631e6b01fSNick Piggin 	struct dentry *mountpoint;
69731e6b01fSNick Piggin 
6980714a533SAl Viro 	parent = mnt->mnt_parent;
6990714a533SAl Viro 	if (&parent->mnt == path->mnt)
70031e6b01fSNick Piggin 		return 0;
701a73324daSAl Viro 	mountpoint = mnt->mnt_mountpoint;
70231e6b01fSNick Piggin 	path->dentry = mountpoint;
7030714a533SAl Viro 	path->mnt = &parent->mnt;
70431e6b01fSNick Piggin 	return 1;
70531e6b01fSNick Piggin }
70631e6b01fSNick Piggin 
707f015f126SDavid Howells /*
708f015f126SDavid Howells  * follow_up - Find the mountpoint of path's vfsmount
709f015f126SDavid Howells  *
710f015f126SDavid Howells  * Given a path, find the mountpoint of its source file system.
711f015f126SDavid Howells  * Replace @path with the path of the mountpoint in the parent mount.
712f015f126SDavid Howells  * Up is towards /.
713f015f126SDavid Howells  *
714f015f126SDavid Howells  * Return 1 if we went up a level and 0 if we were already at the
715f015f126SDavid Howells  * root.
716f015f126SDavid Howells  */
717bab77ebfSAl Viro int follow_up(struct path *path)
7181da177e4SLinus Torvalds {
7190714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
7200714a533SAl Viro 	struct mount *parent;
7211da177e4SLinus Torvalds 	struct dentry *mountpoint;
72299b7db7bSNick Piggin 
723962830dfSAndi Kleen 	br_read_lock(&vfsmount_lock);
7240714a533SAl Viro 	parent = mnt->mnt_parent;
7253c0a6163SAl Viro 	if (parent == mnt) {
726962830dfSAndi Kleen 		br_read_unlock(&vfsmount_lock);
7271da177e4SLinus Torvalds 		return 0;
7281da177e4SLinus Torvalds 	}
7290714a533SAl Viro 	mntget(&parent->mnt);
730a73324daSAl Viro 	mountpoint = dget(mnt->mnt_mountpoint);
731962830dfSAndi Kleen 	br_read_unlock(&vfsmount_lock);
732bab77ebfSAl Viro 	dput(path->dentry);
733bab77ebfSAl Viro 	path->dentry = mountpoint;
734bab77ebfSAl Viro 	mntput(path->mnt);
7350714a533SAl Viro 	path->mnt = &parent->mnt;
7361da177e4SLinus Torvalds 	return 1;
7371da177e4SLinus Torvalds }
7381da177e4SLinus Torvalds 
739b5c84bf6SNick Piggin /*
7409875cf80SDavid Howells  * Perform an automount
7419875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
7429875cf80SDavid Howells  *   were called with.
7431da177e4SLinus Torvalds  */
7449875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
7459875cf80SDavid Howells 			    bool *need_mntput)
74631e6b01fSNick Piggin {
7479875cf80SDavid Howells 	struct vfsmount *mnt;
748ea5b778aSDavid Howells 	int err;
7499875cf80SDavid Howells 
7509875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
7519875cf80SDavid Howells 		return -EREMOTE;
7529875cf80SDavid Howells 
7530ec26fd0SMiklos Szeredi 	/* We don't want to mount if someone's just doing a stat -
7540ec26fd0SMiklos Szeredi 	 * unless they're stat'ing a directory and appended a '/' to
7550ec26fd0SMiklos Szeredi 	 * the name.
7560ec26fd0SMiklos Szeredi 	 *
7570ec26fd0SMiklos Szeredi 	 * We do, however, want to mount if someone wants to open or
7585a30d8a2SDavid Howells 	 * create a file of any type under the mountpoint, wants to
7590ec26fd0SMiklos Szeredi 	 * traverse through the mountpoint or wants to open the
7600ec26fd0SMiklos Szeredi 	 * mounted directory.  Also, autofs may mark negative dentries
7610ec26fd0SMiklos Szeredi 	 * as being automount points.  These will need the attentions
7620ec26fd0SMiklos Szeredi 	 * of the daemon to instantiate them before they can be used.
7635a30d8a2SDavid Howells 	 */
7645a30d8a2SDavid Howells 	if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
765d94c177bSLinus Torvalds 		     LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
7665a30d8a2SDavid Howells 	    path->dentry->d_inode)
7679875cf80SDavid Howells 		return -EISDIR;
7680ec26fd0SMiklos Szeredi 
7699875cf80SDavid Howells 	current->total_link_count++;
7709875cf80SDavid Howells 	if (current->total_link_count >= 40)
7719875cf80SDavid Howells 		return -ELOOP;
7729875cf80SDavid Howells 
7739875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
7749875cf80SDavid Howells 	if (IS_ERR(mnt)) {
7759875cf80SDavid Howells 		/*
7769875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
7779875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
7789875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
7799875cf80SDavid Howells 		 *
7809875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
7819875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
7829875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
7839875cf80SDavid Howells 		 */
78449084c3bSAl Viro 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
7859875cf80SDavid Howells 			return -EREMOTE;
7869875cf80SDavid Howells 		return PTR_ERR(mnt);
78731e6b01fSNick Piggin 	}
788ea5b778aSDavid Howells 
7899875cf80SDavid Howells 	if (!mnt) /* mount collision */
7909875cf80SDavid Howells 		return 0;
7919875cf80SDavid Howells 
7928aef1884SAl Viro 	if (!*need_mntput) {
7938aef1884SAl Viro 		/* lock_mount() may release path->mnt on error */
7948aef1884SAl Viro 		mntget(path->mnt);
7958aef1884SAl Viro 		*need_mntput = true;
7968aef1884SAl Viro 	}
79719a167afSAl Viro 	err = finish_automount(mnt, path);
798ea5b778aSDavid Howells 
799ea5b778aSDavid Howells 	switch (err) {
800ea5b778aSDavid Howells 	case -EBUSY:
801ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
80219a167afSAl Viro 		return 0;
803ea5b778aSDavid Howells 	case 0:
8048aef1884SAl Viro 		path_put(path);
8059875cf80SDavid Howells 		path->mnt = mnt;
8069875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
8079875cf80SDavid Howells 		return 0;
80819a167afSAl Viro 	default:
80919a167afSAl Viro 		return err;
8109875cf80SDavid Howells 	}
81119a167afSAl Viro 
812ea5b778aSDavid Howells }
8139875cf80SDavid Howells 
8149875cf80SDavid Howells /*
8159875cf80SDavid Howells  * Handle a dentry that is managed in some way.
816cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
8179875cf80SDavid Howells  * - Flagged as mountpoint
8189875cf80SDavid Howells  * - Flagged as automount point
8199875cf80SDavid Howells  *
8209875cf80SDavid Howells  * This may only be called in refwalk mode.
8219875cf80SDavid Howells  *
8229875cf80SDavid Howells  * Serialization is taken care of in namespace.c
8239875cf80SDavid Howells  */
8249875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
8259875cf80SDavid Howells {
8268aef1884SAl Viro 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
8279875cf80SDavid Howells 	unsigned managed;
8289875cf80SDavid Howells 	bool need_mntput = false;
8298aef1884SAl Viro 	int ret = 0;
8309875cf80SDavid Howells 
8319875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
8329875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
8339875cf80SDavid Howells 	 * the components of that value change under us */
8349875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
8359875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
8369875cf80SDavid Howells 	       unlikely(managed != 0)) {
837cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
838cc53ce53SDavid Howells 		 * being held. */
839cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
840cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
841cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
8421aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
843cc53ce53SDavid Howells 			if (ret < 0)
8448aef1884SAl Viro 				break;
845cc53ce53SDavid Howells 		}
846cc53ce53SDavid Howells 
8479875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
8489875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
8499875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
8509875cf80SDavid Howells 			if (mounted) {
8519875cf80SDavid Howells 				dput(path->dentry);
8529875cf80SDavid Howells 				if (need_mntput)
853463ffb2eSAl Viro 					mntput(path->mnt);
854463ffb2eSAl Viro 				path->mnt = mounted;
855463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
8569875cf80SDavid Howells 				need_mntput = true;
8579875cf80SDavid Howells 				continue;
858463ffb2eSAl Viro 			}
859463ffb2eSAl Viro 
8609875cf80SDavid Howells 			/* Something is mounted on this dentry in another
8619875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
8629875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
8639875cf80SDavid Howells 			 * vfsmount_lock */
8641da177e4SLinus Torvalds 		}
8659875cf80SDavid Howells 
8669875cf80SDavid Howells 		/* Handle an automount point */
8679875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
8689875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
8699875cf80SDavid Howells 			if (ret < 0)
8708aef1884SAl Viro 				break;
8719875cf80SDavid Howells 			continue;
8729875cf80SDavid Howells 		}
8739875cf80SDavid Howells 
8749875cf80SDavid Howells 		/* We didn't change the current path point */
8759875cf80SDavid Howells 		break;
8769875cf80SDavid Howells 	}
8778aef1884SAl Viro 
8788aef1884SAl Viro 	if (need_mntput && path->mnt == mnt)
8798aef1884SAl Viro 		mntput(path->mnt);
8808aef1884SAl Viro 	if (ret == -EISDIR)
8818aef1884SAl Viro 		ret = 0;
882a3fbbde7SAl Viro 	return ret < 0 ? ret : need_mntput;
8831da177e4SLinus Torvalds }
8841da177e4SLinus Torvalds 
885cc53ce53SDavid Howells int follow_down_one(struct path *path)
8861da177e4SLinus Torvalds {
8871da177e4SLinus Torvalds 	struct vfsmount *mounted;
8881da177e4SLinus Torvalds 
8891c755af4SAl Viro 	mounted = lookup_mnt(path);
8901da177e4SLinus Torvalds 	if (mounted) {
8919393bd07SAl Viro 		dput(path->dentry);
8929393bd07SAl Viro 		mntput(path->mnt);
8939393bd07SAl Viro 		path->mnt = mounted;
8949393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
8951da177e4SLinus Torvalds 		return 1;
8961da177e4SLinus Torvalds 	}
8971da177e4SLinus Torvalds 	return 0;
8981da177e4SLinus Torvalds }
8991da177e4SLinus Torvalds 
90062a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
90162a7375eSIan Kent {
90262a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
90362a7375eSIan Kent 		dentry->d_op->d_manage(dentry, true) < 0);
90462a7375eSIan Kent }
90562a7375eSIan Kent 
9069875cf80SDavid Howells /*
907287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
908287548e4SAl Viro  * we meet a managed dentry that would need blocking.
9099875cf80SDavid Howells  */
9109875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
911287548e4SAl Viro 			       struct inode **inode)
9129875cf80SDavid Howells {
91362a7375eSIan Kent 	for (;;) {
914c7105365SAl Viro 		struct mount *mounted;
91562a7375eSIan Kent 		/*
91662a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
91762a7375eSIan Kent 		 * that wants to block transit.
91862a7375eSIan Kent 		 */
919287548e4SAl Viro 		if (unlikely(managed_dentry_might_block(path->dentry)))
920ab90911fSDavid Howells 			return false;
92162a7375eSIan Kent 
92262a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
92362a7375eSIan Kent 			break;
92462a7375eSIan Kent 
9259875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
9269875cf80SDavid Howells 		if (!mounted)
9279875cf80SDavid Howells 			break;
928c7105365SAl Viro 		path->mnt = &mounted->mnt;
929c7105365SAl Viro 		path->dentry = mounted->mnt.mnt_root;
930a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
9319875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
93259430262SLinus Torvalds 		/*
93359430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
93459430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
93559430262SLinus Torvalds 		 * because a mount-point is always pinned.
93659430262SLinus Torvalds 		 */
93759430262SLinus Torvalds 		*inode = path->dentry->d_inode;
9389875cf80SDavid Howells 	}
9399875cf80SDavid Howells 	return true;
9409875cf80SDavid Howells }
9419875cf80SDavid Howells 
942dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd)
943287548e4SAl Viro {
944dea39376SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
945c7105365SAl Viro 		struct mount *mounted;
946dea39376SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
947287548e4SAl Viro 		if (!mounted)
948287548e4SAl Viro 			break;
949c7105365SAl Viro 		nd->path.mnt = &mounted->mnt;
950c7105365SAl Viro 		nd->path.dentry = mounted->mnt.mnt_root;
951dea39376SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
952287548e4SAl Viro 	}
953287548e4SAl Viro }
954287548e4SAl Viro 
95531e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
95631e6b01fSNick Piggin {
95731e6b01fSNick Piggin 	set_root_rcu(nd);
95831e6b01fSNick Piggin 
95931e6b01fSNick Piggin 	while (1) {
96031e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
96131e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
96231e6b01fSNick Piggin 			break;
96331e6b01fSNick Piggin 		}
96431e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
96531e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
96631e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
96731e6b01fSNick Piggin 			unsigned seq;
96831e6b01fSNick Piggin 
96931e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
97031e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
971ef7562d5SAl Viro 				goto failed;
97231e6b01fSNick Piggin 			nd->path.dentry = parent;
97331e6b01fSNick Piggin 			nd->seq = seq;
97431e6b01fSNick Piggin 			break;
97531e6b01fSNick Piggin 		}
97631e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
97731e6b01fSNick Piggin 			break;
97831e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
97931e6b01fSNick Piggin 	}
980dea39376SAl Viro 	follow_mount_rcu(nd);
981dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
98231e6b01fSNick Piggin 	return 0;
983ef7562d5SAl Viro 
984ef7562d5SAl Viro failed:
985ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
9865b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
987ef7562d5SAl Viro 		nd->root.mnt = NULL;
988ef7562d5SAl Viro 	rcu_read_unlock();
989962830dfSAndi Kleen 	br_read_unlock(&vfsmount_lock);
990ef7562d5SAl Viro 	return -ECHILD;
99131e6b01fSNick Piggin }
99231e6b01fSNick Piggin 
9939875cf80SDavid Howells /*
994cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
995cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
996cc53ce53SDavid Howells  * caller is permitted to proceed or not.
997cc53ce53SDavid Howells  */
9987cc90cc3SAl Viro int follow_down(struct path *path)
999cc53ce53SDavid Howells {
1000cc53ce53SDavid Howells 	unsigned managed;
1001cc53ce53SDavid Howells 	int ret;
1002cc53ce53SDavid Howells 
1003cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
1004cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1005cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1006cc53ce53SDavid Howells 		 * being held.
1007cc53ce53SDavid Howells 		 *
1008cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
1009cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
1010cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
1011cc53ce53SDavid Howells 		 * superstructure.
1012cc53ce53SDavid Howells 		 *
1013cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
1014cc53ce53SDavid Howells 		 */
1015cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
1016cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1017cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
1018ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
10191aed3e42SAl Viro 				path->dentry, false);
1020cc53ce53SDavid Howells 			if (ret < 0)
1021cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
1022cc53ce53SDavid Howells 		}
1023cc53ce53SDavid Howells 
1024cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
1025cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
1026cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
1027cc53ce53SDavid Howells 			if (!mounted)
1028cc53ce53SDavid Howells 				break;
1029cc53ce53SDavid Howells 			dput(path->dentry);
1030cc53ce53SDavid Howells 			mntput(path->mnt);
1031cc53ce53SDavid Howells 			path->mnt = mounted;
1032cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1033cc53ce53SDavid Howells 			continue;
1034cc53ce53SDavid Howells 		}
1035cc53ce53SDavid Howells 
1036cc53ce53SDavid Howells 		/* Don't handle automount points here */
1037cc53ce53SDavid Howells 		break;
1038cc53ce53SDavid Howells 	}
1039cc53ce53SDavid Howells 	return 0;
1040cc53ce53SDavid Howells }
1041cc53ce53SDavid Howells 
1042cc53ce53SDavid Howells /*
10439875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
10449875cf80SDavid Howells  */
10459875cf80SDavid Howells static void follow_mount(struct path *path)
10469875cf80SDavid Howells {
10479875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
10489875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
10499875cf80SDavid Howells 		if (!mounted)
10509875cf80SDavid Howells 			break;
10519875cf80SDavid Howells 		dput(path->dentry);
10529875cf80SDavid Howells 		mntput(path->mnt);
10539875cf80SDavid Howells 		path->mnt = mounted;
10549875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
10559875cf80SDavid Howells 	}
10569875cf80SDavid Howells }
10579875cf80SDavid Howells 
105831e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
10591da177e4SLinus Torvalds {
10602a737871SAl Viro 	set_root(nd);
1061e518ddb7SAndreas Mohr 
10621da177e4SLinus Torvalds 	while(1) {
10634ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
10641da177e4SLinus Torvalds 
10652a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
10662a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
10671da177e4SLinus Torvalds 			break;
10681da177e4SLinus Torvalds 		}
10694ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
10703088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
10713088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
10721da177e4SLinus Torvalds 			dput(old);
10731da177e4SLinus Torvalds 			break;
10741da177e4SLinus Torvalds 		}
10753088dd70SAl Viro 		if (!follow_up(&nd->path))
10761da177e4SLinus Torvalds 			break;
10771da177e4SLinus Torvalds 	}
107879ed0226SAl Viro 	follow_mount(&nd->path);
107931e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
10801da177e4SLinus Torvalds }
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds /*
1083bad61189SMiklos Szeredi  * This looks up the name in dcache, possibly revalidates the old dentry and
1084bad61189SMiklos Szeredi  * allocates a new one if not found or not valid.  In the need_lookup argument
1085bad61189SMiklos Szeredi  * returns whether i_op->lookup is necessary.
1086bad61189SMiklos Szeredi  *
1087bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
1088baa03890SNick Piggin  */
1089bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1090201f956eSAl Viro 				    unsigned int flags, bool *need_lookup)
1091baa03890SNick Piggin {
1092baa03890SNick Piggin 	struct dentry *dentry;
1093bad61189SMiklos Szeredi 	int error;
1094baa03890SNick Piggin 
1095bad61189SMiklos Szeredi 	*need_lookup = false;
1096bad61189SMiklos Szeredi 	dentry = d_lookup(dir, name);
1097bad61189SMiklos Szeredi 	if (dentry) {
1098bad61189SMiklos Szeredi 		if (d_need_lookup(dentry)) {
1099bad61189SMiklos Szeredi 			*need_lookup = true;
1100bad61189SMiklos Szeredi 		} else if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1101201f956eSAl Viro 			error = d_revalidate(dentry, flags);
1102bad61189SMiklos Szeredi 			if (unlikely(error <= 0)) {
1103bad61189SMiklos Szeredi 				if (error < 0) {
1104bad61189SMiklos Szeredi 					dput(dentry);
1105bad61189SMiklos Szeredi 					return ERR_PTR(error);
1106bad61189SMiklos Szeredi 				} else if (!d_invalidate(dentry)) {
1107bad61189SMiklos Szeredi 					dput(dentry);
1108bad61189SMiklos Szeredi 					dentry = NULL;
1109bad61189SMiklos Szeredi 				}
1110bad61189SMiklos Szeredi 			}
1111bad61189SMiklos Szeredi 		}
1112bad61189SMiklos Szeredi 	}
1113baa03890SNick Piggin 
1114bad61189SMiklos Szeredi 	if (!dentry) {
1115bad61189SMiklos Szeredi 		dentry = d_alloc(dir, name);
1116baa03890SNick Piggin 		if (unlikely(!dentry))
1117baa03890SNick Piggin 			return ERR_PTR(-ENOMEM);
1118baa03890SNick Piggin 
1119bad61189SMiklos Szeredi 		*need_lookup = true;
1120baa03890SNick Piggin 	}
1121baa03890SNick Piggin 	return dentry;
1122baa03890SNick Piggin }
1123baa03890SNick Piggin 
1124baa03890SNick Piggin /*
1125bad61189SMiklos Szeredi  * Call i_op->lookup on the dentry.  The dentry must be negative but may be
1126bad61189SMiklos Szeredi  * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1127bad61189SMiklos Szeredi  *
1128bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
112944396f4bSJosef Bacik  */
1130bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
113172bd866aSAl Viro 				  unsigned int flags)
113244396f4bSJosef Bacik {
113344396f4bSJosef Bacik 	struct dentry *old;
113444396f4bSJosef Bacik 
113544396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
1136bad61189SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
1137e188dc02SMiklos Szeredi 		dput(dentry);
113844396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
1139e188dc02SMiklos Szeredi 	}
114044396f4bSJosef Bacik 
114172bd866aSAl Viro 	old = dir->i_op->lookup(dir, dentry, flags);
114244396f4bSJosef Bacik 	if (unlikely(old)) {
114344396f4bSJosef Bacik 		dput(dentry);
114444396f4bSJosef Bacik 		dentry = old;
114544396f4bSJosef Bacik 	}
114644396f4bSJosef Bacik 	return dentry;
114744396f4bSJosef Bacik }
114844396f4bSJosef Bacik 
1149a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name,
115072bd866aSAl Viro 		struct dentry *base, unsigned int flags)
1151a3255546SAl Viro {
1152bad61189SMiklos Szeredi 	bool need_lookup;
1153a3255546SAl Viro 	struct dentry *dentry;
1154a3255546SAl Viro 
115572bd866aSAl Viro 	dentry = lookup_dcache(name, base, flags, &need_lookup);
1156bad61189SMiklos Szeredi 	if (!need_lookup)
1157a3255546SAl Viro 		return dentry;
1158bad61189SMiklos Szeredi 
115972bd866aSAl Viro 	return lookup_real(base->d_inode, dentry, flags);
1160a3255546SAl Viro }
1161a3255546SAl Viro 
116244396f4bSJosef Bacik /*
11631da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
11641da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
11651da177e4SLinus Torvalds  *  It _is_ time-critical.
11661da177e4SLinus Torvalds  */
1167697f514dSMiklos Szeredi static int lookup_fast(struct nameidata *nd, struct qstr *name,
116831e6b01fSNick Piggin 		       struct path *path, struct inode **inode)
11691da177e4SLinus Torvalds {
11704ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
117131e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
11725a18fff2SAl Viro 	int need_reval = 1;
11735a18fff2SAl Viro 	int status = 1;
11749875cf80SDavid Howells 	int err;
11759875cf80SDavid Howells 
11763cac260aSAl Viro 	/*
1177b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1178b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1179b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1180b04f784eSNick Piggin 	 */
118131e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
118231e6b01fSNick Piggin 		unsigned seq;
118312f8ad4bSLinus Torvalds 		dentry = __d_lookup_rcu(parent, name, &seq, nd->inode);
11845a18fff2SAl Viro 		if (!dentry)
11855a18fff2SAl Viro 			goto unlazy;
11865a18fff2SAl Viro 
118712f8ad4bSLinus Torvalds 		/*
118812f8ad4bSLinus Torvalds 		 * This sequence count validates that the inode matches
118912f8ad4bSLinus Torvalds 		 * the dentry name information from lookup.
119012f8ad4bSLinus Torvalds 		 */
119112f8ad4bSLinus Torvalds 		*inode = dentry->d_inode;
119212f8ad4bSLinus Torvalds 		if (read_seqcount_retry(&dentry->d_seq, seq))
119312f8ad4bSLinus Torvalds 			return -ECHILD;
119412f8ad4bSLinus Torvalds 
119512f8ad4bSLinus Torvalds 		/*
119612f8ad4bSLinus Torvalds 		 * This sequence count validates that the parent had no
119712f8ad4bSLinus Torvalds 		 * changes while we did the lookup of the dentry above.
119812f8ad4bSLinus Torvalds 		 *
119912f8ad4bSLinus Torvalds 		 * The memory barrier in read_seqcount_begin of child is
120012f8ad4bSLinus Torvalds 		 *  enough, we can use __read_seqcount_retry here.
120112f8ad4bSLinus Torvalds 		 */
120231e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
120331e6b01fSNick Piggin 			return -ECHILD;
120431e6b01fSNick Piggin 		nd->seq = seq;
12055a18fff2SAl Viro 
1206fa4ee159SMiklos Szeredi 		if (unlikely(d_need_lookup(dentry)))
1207fa4ee159SMiklos Szeredi 			goto unlazy;
120824643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
12094ce16ef3SAl Viro 			status = d_revalidate(dentry, nd->flags);
12105a18fff2SAl Viro 			if (unlikely(status <= 0)) {
12115a18fff2SAl Viro 				if (status != -ECHILD)
12125a18fff2SAl Viro 					need_reval = 0;
12135a18fff2SAl Viro 				goto unlazy;
12145a18fff2SAl Viro 			}
121524643087SAl Viro 		}
121631e6b01fSNick Piggin 		path->mnt = mnt;
121731e6b01fSNick Piggin 		path->dentry = dentry;
1218d6e9bd25SAl Viro 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1219d6e9bd25SAl Viro 			goto unlazy;
1220d6e9bd25SAl Viro 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1221d6e9bd25SAl Viro 			goto unlazy;
12229875cf80SDavid Howells 		return 0;
12235a18fff2SAl Viro unlazy:
122419660af7SAl Viro 		if (unlazy_walk(nd, dentry))
12255a18fff2SAl Viro 			return -ECHILD;
12265a18fff2SAl Viro 	} else {
122731e6b01fSNick Piggin 		dentry = __d_lookup(parent, name);
122824643087SAl Viro 	}
12295a18fff2SAl Viro 
123081e6f520SAl Viro 	if (unlikely(!dentry))
123181e6f520SAl Viro 		goto need_lookup;
12325a18fff2SAl Viro 
123381e6f520SAl Viro 	if (unlikely(d_need_lookup(dentry))) {
123481e6f520SAl Viro 		dput(dentry);
123581e6f520SAl Viro 		goto need_lookup;
12365a18fff2SAl Viro 	}
123781e6f520SAl Viro 
12385a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
12394ce16ef3SAl Viro 		status = d_revalidate(dentry, nd->flags);
12405a18fff2SAl Viro 	if (unlikely(status <= 0)) {
12415a18fff2SAl Viro 		if (status < 0) {
12425a18fff2SAl Viro 			dput(dentry);
12435a18fff2SAl Viro 			return status;
12445a18fff2SAl Viro 		}
12455a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
12465a18fff2SAl Viro 			dput(dentry);
124781e6f520SAl Viro 			goto need_lookup;
12485a18fff2SAl Viro 		}
12495a18fff2SAl Viro 	}
1250697f514dSMiklos Szeredi 
12511da177e4SLinus Torvalds 	path->mnt = mnt;
12521da177e4SLinus Torvalds 	path->dentry = dentry;
12539875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
125489312214SIan Kent 	if (unlikely(err < 0)) {
125589312214SIan Kent 		path_put_conditional(path, nd);
12569875cf80SDavid Howells 		return err;
125789312214SIan Kent 	}
1258a3fbbde7SAl Viro 	if (err)
1259a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
126031e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
12611da177e4SLinus Torvalds 	return 0;
126281e6f520SAl Viro 
126381e6f520SAl Viro need_lookup:
1264697f514dSMiklos Szeredi 	return 1;
1265697f514dSMiklos Szeredi }
1266697f514dSMiklos Szeredi 
1267697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */
1268697f514dSMiklos Szeredi static int lookup_slow(struct nameidata *nd, struct qstr *name,
1269697f514dSMiklos Szeredi 		       struct path *path)
1270697f514dSMiklos Szeredi {
1271697f514dSMiklos Szeredi 	struct dentry *dentry, *parent;
1272697f514dSMiklos Szeredi 	int err;
1273697f514dSMiklos Szeredi 
1274697f514dSMiklos Szeredi 	parent = nd->path.dentry;
127581e6f520SAl Viro 	BUG_ON(nd->inode != parent->d_inode);
127681e6f520SAl Viro 
127781e6f520SAl Viro 	mutex_lock(&parent->d_inode->i_mutex);
127872bd866aSAl Viro 	dentry = __lookup_hash(name, parent, nd->flags);
127981e6f520SAl Viro 	mutex_unlock(&parent->d_inode->i_mutex);
128081e6f520SAl Viro 	if (IS_ERR(dentry))
128181e6f520SAl Viro 		return PTR_ERR(dentry);
1282697f514dSMiklos Szeredi 	path->mnt = nd->path.mnt;
1283697f514dSMiklos Szeredi 	path->dentry = dentry;
1284697f514dSMiklos Szeredi 	err = follow_managed(path, nd->flags);
1285697f514dSMiklos Szeredi 	if (unlikely(err < 0)) {
1286697f514dSMiklos Szeredi 		path_put_conditional(path, nd);
1287697f514dSMiklos Szeredi 		return err;
1288697f514dSMiklos Szeredi 	}
1289697f514dSMiklos Szeredi 	if (err)
1290697f514dSMiklos Szeredi 		nd->flags |= LOOKUP_JUMPED;
1291697f514dSMiklos Szeredi 	return 0;
12921da177e4SLinus Torvalds }
12931da177e4SLinus Torvalds 
129452094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
129552094c8aSAl Viro {
129652094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
12974ad5abb3SAl Viro 		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
129852094c8aSAl Viro 		if (err != -ECHILD)
129952094c8aSAl Viro 			return err;
130019660af7SAl Viro 		if (unlazy_walk(nd, NULL))
130152094c8aSAl Viro 			return -ECHILD;
130252094c8aSAl Viro 	}
13034ad5abb3SAl Viro 	return inode_permission(nd->inode, MAY_EXEC);
130452094c8aSAl Viro }
130552094c8aSAl Viro 
13069856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
13079856fa1bSAl Viro {
13089856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
13099856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
13109856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
13119856fa1bSAl Viro 				return -ECHILD;
13129856fa1bSAl Viro 		} else
13139856fa1bSAl Viro 			follow_dotdot(nd);
13149856fa1bSAl Viro 	}
13159856fa1bSAl Viro 	return 0;
13169856fa1bSAl Viro }
13179856fa1bSAl Viro 
1318951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1319951361f9SAl Viro {
1320951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1321951361f9SAl Viro 		path_put(&nd->path);
1322951361f9SAl Viro 	} else {
1323951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
13245b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1325951361f9SAl Viro 			nd->root.mnt = NULL;
1326951361f9SAl Viro 		rcu_read_unlock();
1327962830dfSAndi Kleen 		br_read_unlock(&vfsmount_lock);
1328951361f9SAl Viro 	}
1329951361f9SAl Viro }
1330951361f9SAl Viro 
13313ddcd056SLinus Torvalds /*
13323ddcd056SLinus Torvalds  * Do we need to follow links? We _really_ want to be able
13333ddcd056SLinus Torvalds  * to do this check without having to look at inode->i_op,
13343ddcd056SLinus Torvalds  * so we keep a cache of "no, this doesn't need follow_link"
13353ddcd056SLinus Torvalds  * for the common case.
13363ddcd056SLinus Torvalds  */
13377813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow)
13383ddcd056SLinus Torvalds {
13393ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
13403ddcd056SLinus Torvalds 		if (likely(inode->i_op->follow_link))
13413ddcd056SLinus Torvalds 			return follow;
13423ddcd056SLinus Torvalds 
13433ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
13443ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
13453ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_NOFOLLOW;
13463ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
13473ddcd056SLinus Torvalds 	}
13483ddcd056SLinus Torvalds 	return 0;
13493ddcd056SLinus Torvalds }
13503ddcd056SLinus Torvalds 
1351ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
1352ce57dfc1SAl Viro 		struct qstr *name, int type, int follow)
1353ce57dfc1SAl Viro {
1354ce57dfc1SAl Viro 	struct inode *inode;
1355ce57dfc1SAl Viro 	int err;
1356ce57dfc1SAl Viro 	/*
1357ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1358ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1359ce57dfc1SAl Viro 	 * parent relationships.
1360ce57dfc1SAl Viro 	 */
1361ce57dfc1SAl Viro 	if (unlikely(type != LAST_NORM))
1362ce57dfc1SAl Viro 		return handle_dots(nd, type);
1363697f514dSMiklos Szeredi 	err = lookup_fast(nd, name, path, &inode);
1364ce57dfc1SAl Viro 	if (unlikely(err)) {
1365697f514dSMiklos Szeredi 		if (err < 0)
1366697f514dSMiklos Szeredi 			goto out_err;
1367697f514dSMiklos Szeredi 
1368697f514dSMiklos Szeredi 		err = lookup_slow(nd, name, path);
1369697f514dSMiklos Szeredi 		if (err < 0)
1370697f514dSMiklos Szeredi 			goto out_err;
1371697f514dSMiklos Szeredi 
1372697f514dSMiklos Szeredi 		inode = path->dentry->d_inode;
1373ce57dfc1SAl Viro 	}
1374697f514dSMiklos Szeredi 	err = -ENOENT;
1375697f514dSMiklos Szeredi 	if (!inode)
1376697f514dSMiklos Szeredi 		goto out_path_put;
1377697f514dSMiklos Szeredi 
13787813b94aSLinus Torvalds 	if (should_follow_link(inode, follow)) {
137919660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
138019660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
1381697f514dSMiklos Szeredi 				err = -ECHILD;
1382697f514dSMiklos Szeredi 				goto out_err;
138319660af7SAl Viro 			}
138419660af7SAl Viro 		}
1385ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1386ce57dfc1SAl Viro 		return 1;
1387ce57dfc1SAl Viro 	}
1388ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1389ce57dfc1SAl Viro 	nd->inode = inode;
1390ce57dfc1SAl Viro 	return 0;
1391697f514dSMiklos Szeredi 
1392697f514dSMiklos Szeredi out_path_put:
1393697f514dSMiklos Szeredi 	path_to_nameidata(path, nd);
1394697f514dSMiklos Szeredi out_err:
1395697f514dSMiklos Szeredi 	terminate_walk(nd);
1396697f514dSMiklos Szeredi 	return err;
1397ce57dfc1SAl Viro }
1398ce57dfc1SAl Viro 
13991da177e4SLinus Torvalds /*
1400b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1401b356379aSAl Viro  * limiting consecutive symlinks to 40.
1402b356379aSAl Viro  *
1403b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1404b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1405b356379aSAl Viro  */
1406b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1407b356379aSAl Viro {
1408b356379aSAl Viro 	int res;
1409b356379aSAl Viro 
1410b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1411b356379aSAl Viro 		path_put_conditional(path, nd);
1412b356379aSAl Viro 		path_put(&nd->path);
1413b356379aSAl Viro 		return -ELOOP;
1414b356379aSAl Viro 	}
14151a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1416b356379aSAl Viro 
1417b356379aSAl Viro 	nd->depth++;
1418b356379aSAl Viro 	current->link_count++;
1419b356379aSAl Viro 
1420b356379aSAl Viro 	do {
1421b356379aSAl Viro 		struct path link = *path;
1422b356379aSAl Viro 		void *cookie;
1423574197e0SAl Viro 
1424574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
14256d7b5aaeSAl Viro 		if (res)
14266d7b5aaeSAl Viro 			break;
1427b356379aSAl Viro 		res = walk_component(nd, path, &nd->last,
1428b356379aSAl Viro 				     nd->last_type, LOOKUP_FOLLOW);
1429574197e0SAl Viro 		put_link(nd, &link, cookie);
1430b356379aSAl Viro 	} while (res > 0);
1431b356379aSAl Viro 
1432b356379aSAl Viro 	current->link_count--;
1433b356379aSAl Viro 	nd->depth--;
1434b356379aSAl Viro 	return res;
1435b356379aSAl Viro }
1436b356379aSAl Viro 
1437b356379aSAl Viro /*
14383ddcd056SLinus Torvalds  * We really don't want to look at inode->i_op->lookup
14393ddcd056SLinus Torvalds  * when we don't have to. So we keep a cache bit in
14403ddcd056SLinus Torvalds  * the inode ->i_opflags field that says "yes, we can
14413ddcd056SLinus Torvalds  * do lookup on this inode".
14423ddcd056SLinus Torvalds  */
14433ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode)
14443ddcd056SLinus Torvalds {
14453ddcd056SLinus Torvalds 	if (likely(inode->i_opflags & IOP_LOOKUP))
14463ddcd056SLinus Torvalds 		return 1;
14473ddcd056SLinus Torvalds 	if (likely(!inode->i_op->lookup))
14483ddcd056SLinus Torvalds 		return 0;
14493ddcd056SLinus Torvalds 
14503ddcd056SLinus Torvalds 	/* We do this once for the lifetime of the inode */
14513ddcd056SLinus Torvalds 	spin_lock(&inode->i_lock);
14523ddcd056SLinus Torvalds 	inode->i_opflags |= IOP_LOOKUP;
14533ddcd056SLinus Torvalds 	spin_unlock(&inode->i_lock);
14543ddcd056SLinus Torvalds 	return 1;
14553ddcd056SLinus Torvalds }
14563ddcd056SLinus Torvalds 
1457bfcfaa77SLinus Torvalds /*
1458bfcfaa77SLinus Torvalds  * We can do the critical dentry name comparison and hashing
1459bfcfaa77SLinus Torvalds  * operations one word at a time, but we are limited to:
1460bfcfaa77SLinus Torvalds  *
1461bfcfaa77SLinus Torvalds  * - Architectures with fast unaligned word accesses. We could
1462bfcfaa77SLinus Torvalds  *   do a "get_unaligned()" if this helps and is sufficiently
1463bfcfaa77SLinus Torvalds  *   fast.
1464bfcfaa77SLinus Torvalds  *
1465bfcfaa77SLinus Torvalds  * - Little-endian machines (so that we can generate the mask
1466bfcfaa77SLinus Torvalds  *   of low bytes efficiently). Again, we *could* do a byte
1467bfcfaa77SLinus Torvalds  *   swapping load on big-endian architectures if that is not
1468bfcfaa77SLinus Torvalds  *   expensive enough to make the optimization worthless.
1469bfcfaa77SLinus Torvalds  *
1470bfcfaa77SLinus Torvalds  * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1471bfcfaa77SLinus Torvalds  *   do not trap on the (extremely unlikely) case of a page
1472bfcfaa77SLinus Torvalds  *   crossing operation.
1473bfcfaa77SLinus Torvalds  *
1474bfcfaa77SLinus Torvalds  * - Furthermore, we need an efficient 64-bit compile for the
1475bfcfaa77SLinus Torvalds  *   64-bit case in order to generate the "number of bytes in
1476bfcfaa77SLinus Torvalds  *   the final mask". Again, that could be replaced with a
1477bfcfaa77SLinus Torvalds  *   efficient population count instruction or similar.
1478bfcfaa77SLinus Torvalds  */
1479bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS
1480bfcfaa77SLinus Torvalds 
1481f68e556eSLinus Torvalds #include <asm/word-at-a-time.h>
1482bfcfaa77SLinus Torvalds 
1483f68e556eSLinus Torvalds #ifdef CONFIG_64BIT
1484bfcfaa77SLinus Torvalds 
1485bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash)
1486bfcfaa77SLinus Torvalds {
1487bfcfaa77SLinus Torvalds 	hash += hash >> (8*sizeof(int));
1488bfcfaa77SLinus Torvalds 	return hash;
1489bfcfaa77SLinus Torvalds }
1490bfcfaa77SLinus Torvalds 
1491bfcfaa77SLinus Torvalds #else	/* 32-bit case */
1492bfcfaa77SLinus Torvalds 
1493bfcfaa77SLinus Torvalds #define fold_hash(x) (x)
1494bfcfaa77SLinus Torvalds 
1495bfcfaa77SLinus Torvalds #endif
1496bfcfaa77SLinus Torvalds 
1497bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1498bfcfaa77SLinus Torvalds {
1499bfcfaa77SLinus Torvalds 	unsigned long a, mask;
1500bfcfaa77SLinus Torvalds 	unsigned long hash = 0;
1501bfcfaa77SLinus Torvalds 
1502bfcfaa77SLinus Torvalds 	for (;;) {
1503e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name);
1504bfcfaa77SLinus Torvalds 		if (len < sizeof(unsigned long))
1505bfcfaa77SLinus Torvalds 			break;
1506bfcfaa77SLinus Torvalds 		hash += a;
1507f132c5beSAl Viro 		hash *= 9;
1508bfcfaa77SLinus Torvalds 		name += sizeof(unsigned long);
1509bfcfaa77SLinus Torvalds 		len -= sizeof(unsigned long);
1510bfcfaa77SLinus Torvalds 		if (!len)
1511bfcfaa77SLinus Torvalds 			goto done;
1512bfcfaa77SLinus Torvalds 	}
1513bfcfaa77SLinus Torvalds 	mask = ~(~0ul << len*8);
1514bfcfaa77SLinus Torvalds 	hash += mask & a;
1515bfcfaa77SLinus Torvalds done:
1516bfcfaa77SLinus Torvalds 	return fold_hash(hash);
1517bfcfaa77SLinus Torvalds }
1518bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
1519bfcfaa77SLinus Torvalds 
1520bfcfaa77SLinus Torvalds /*
1521bfcfaa77SLinus Torvalds  * Calculate the length and hash of the path component, and
1522bfcfaa77SLinus Torvalds  * return the length of the component;
1523bfcfaa77SLinus Torvalds  */
1524bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1525bfcfaa77SLinus Torvalds {
152636126f8fSLinus Torvalds 	unsigned long a, b, adata, bdata, mask, hash, len;
152736126f8fSLinus Torvalds 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1528bfcfaa77SLinus Torvalds 
1529bfcfaa77SLinus Torvalds 	hash = a = 0;
1530bfcfaa77SLinus Torvalds 	len = -sizeof(unsigned long);
1531bfcfaa77SLinus Torvalds 	do {
1532bfcfaa77SLinus Torvalds 		hash = (hash + a) * 9;
1533bfcfaa77SLinus Torvalds 		len += sizeof(unsigned long);
1534e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name+len);
153536126f8fSLinus Torvalds 		b = a ^ REPEAT_BYTE('/');
153636126f8fSLinus Torvalds 	} while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1537bfcfaa77SLinus Torvalds 
153836126f8fSLinus Torvalds 	adata = prep_zero_mask(a, adata, &constants);
153936126f8fSLinus Torvalds 	bdata = prep_zero_mask(b, bdata, &constants);
154036126f8fSLinus Torvalds 
154136126f8fSLinus Torvalds 	mask = create_zero_mask(adata | bdata);
154236126f8fSLinus Torvalds 
154336126f8fSLinus Torvalds 	hash += a & zero_bytemask(mask);
1544bfcfaa77SLinus Torvalds 	*hashp = fold_hash(hash);
1545bfcfaa77SLinus Torvalds 
154636126f8fSLinus Torvalds 	return len + find_zero(mask);
1547bfcfaa77SLinus Torvalds }
1548bfcfaa77SLinus Torvalds 
1549bfcfaa77SLinus Torvalds #else
1550bfcfaa77SLinus Torvalds 
15510145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
15520145acc2SLinus Torvalds {
15530145acc2SLinus Torvalds 	unsigned long hash = init_name_hash();
15540145acc2SLinus Torvalds 	while (len--)
15550145acc2SLinus Torvalds 		hash = partial_name_hash(*name++, hash);
15560145acc2SLinus Torvalds 	return end_name_hash(hash);
15570145acc2SLinus Torvalds }
1558ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
15590145acc2SLinus Torvalds 
15603ddcd056SLinus Torvalds /*
1561200e9ef7SLinus Torvalds  * We know there's a real path component here of at least
1562200e9ef7SLinus Torvalds  * one character.
1563200e9ef7SLinus Torvalds  */
1564200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1565200e9ef7SLinus Torvalds {
1566200e9ef7SLinus Torvalds 	unsigned long hash = init_name_hash();
1567200e9ef7SLinus Torvalds 	unsigned long len = 0, c;
1568200e9ef7SLinus Torvalds 
1569200e9ef7SLinus Torvalds 	c = (unsigned char)*name;
1570200e9ef7SLinus Torvalds 	do {
1571200e9ef7SLinus Torvalds 		len++;
1572200e9ef7SLinus Torvalds 		hash = partial_name_hash(c, hash);
1573200e9ef7SLinus Torvalds 		c = (unsigned char)name[len];
1574200e9ef7SLinus Torvalds 	} while (c && c != '/');
1575200e9ef7SLinus Torvalds 	*hashp = end_name_hash(hash);
1576200e9ef7SLinus Torvalds 	return len;
1577200e9ef7SLinus Torvalds }
1578200e9ef7SLinus Torvalds 
1579bfcfaa77SLinus Torvalds #endif
1580bfcfaa77SLinus Torvalds 
1581200e9ef7SLinus Torvalds /*
15821da177e4SLinus Torvalds  * Name resolution.
1583ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1584ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
15851da177e4SLinus Torvalds  *
1586ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1587ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
15881da177e4SLinus Torvalds  */
15896de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
15901da177e4SLinus Torvalds {
15911da177e4SLinus Torvalds 	struct path next;
15921da177e4SLinus Torvalds 	int err;
15931da177e4SLinus Torvalds 
15941da177e4SLinus Torvalds 	while (*name=='/')
15951da177e4SLinus Torvalds 		name++;
15961da177e4SLinus Torvalds 	if (!*name)
1597086e183aSAl Viro 		return 0;
15981da177e4SLinus Torvalds 
15991da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
16001da177e4SLinus Torvalds 	for(;;) {
16011da177e4SLinus Torvalds 		struct qstr this;
1602200e9ef7SLinus Torvalds 		long len;
1603fe479a58SAl Viro 		int type;
16041da177e4SLinus Torvalds 
160552094c8aSAl Viro 		err = may_lookup(nd);
16061da177e4SLinus Torvalds  		if (err)
16071da177e4SLinus Torvalds 			break;
16081da177e4SLinus Torvalds 
1609200e9ef7SLinus Torvalds 		len = hash_name(name, &this.hash);
16101da177e4SLinus Torvalds 		this.name = name;
1611200e9ef7SLinus Torvalds 		this.len = len;
16121da177e4SLinus Torvalds 
1613fe479a58SAl Viro 		type = LAST_NORM;
1614200e9ef7SLinus Torvalds 		if (name[0] == '.') switch (len) {
1615fe479a58SAl Viro 			case 2:
1616200e9ef7SLinus Torvalds 				if (name[1] == '.') {
1617fe479a58SAl Viro 					type = LAST_DOTDOT;
161816c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
161916c2cd71SAl Viro 				}
1620fe479a58SAl Viro 				break;
1621fe479a58SAl Viro 			case 1:
1622fe479a58SAl Viro 				type = LAST_DOT;
1623fe479a58SAl Viro 		}
16245a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
16255a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
162616c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
16275a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
16285a202bcdSAl Viro 				err = parent->d_op->d_hash(parent, nd->inode,
16295a202bcdSAl Viro 							   &this);
16305a202bcdSAl Viro 				if (err < 0)
16315a202bcdSAl Viro 					break;
16325a202bcdSAl Viro 			}
16335a202bcdSAl Viro 		}
1634fe479a58SAl Viro 
1635200e9ef7SLinus Torvalds 		if (!name[len])
16361da177e4SLinus Torvalds 			goto last_component;
1637200e9ef7SLinus Torvalds 		/*
1638200e9ef7SLinus Torvalds 		 * If it wasn't NUL, we know it was '/'. Skip that
1639200e9ef7SLinus Torvalds 		 * slash, and continue until no more slashes.
1640200e9ef7SLinus Torvalds 		 */
1641200e9ef7SLinus Torvalds 		do {
1642200e9ef7SLinus Torvalds 			len++;
1643200e9ef7SLinus Torvalds 		} while (unlikely(name[len] == '/'));
1644200e9ef7SLinus Torvalds 		if (!name[len])
1645b356379aSAl Viro 			goto last_component;
1646200e9ef7SLinus Torvalds 		name += len;
16471da177e4SLinus Torvalds 
1648ce57dfc1SAl Viro 		err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1649ce57dfc1SAl Viro 		if (err < 0)
1650ce57dfc1SAl Viro 			return err;
1651fe479a58SAl Viro 
1652ce57dfc1SAl Viro 		if (err) {
1653b356379aSAl Viro 			err = nested_symlink(&next, nd);
16541da177e4SLinus Torvalds 			if (err)
1655a7472babSAl Viro 				return err;
165631e6b01fSNick Piggin 		}
16573ddcd056SLinus Torvalds 		if (can_lookup(nd->inode))
16581da177e4SLinus Torvalds 			continue;
16593ddcd056SLinus Torvalds 		err = -ENOTDIR;
16603ddcd056SLinus Torvalds 		break;
16611da177e4SLinus Torvalds 		/* here ends the main loop */
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds last_component:
1664ce57dfc1SAl Viro 		nd->last = this;
1665ce57dfc1SAl Viro 		nd->last_type = type;
1666ce57dfc1SAl Viro 		return 0;
1667ce57dfc1SAl Viro 	}
1668951361f9SAl Viro 	terminate_walk(nd);
16691da177e4SLinus Torvalds 	return err;
16701da177e4SLinus Torvalds }
16711da177e4SLinus Torvalds 
167270e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
167370e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
167431e6b01fSNick Piggin {
167531e6b01fSNick Piggin 	int retval = 0;
167631e6b01fSNick Piggin 	int fput_needed;
167731e6b01fSNick Piggin 	struct file *file;
167831e6b01fSNick Piggin 
167931e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
168016c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
168131e6b01fSNick Piggin 	nd->depth = 0;
16825b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
16835b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
168473d049a4SAl Viro 		if (*name) {
16855b6ca027SAl Viro 			if (!inode->i_op->lookup)
16865b6ca027SAl Viro 				return -ENOTDIR;
16875b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
16885b6ca027SAl Viro 			if (retval)
16895b6ca027SAl Viro 				return retval;
169073d049a4SAl Viro 		}
16915b6ca027SAl Viro 		nd->path = nd->root;
16925b6ca027SAl Viro 		nd->inode = inode;
16935b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
1694962830dfSAndi Kleen 			br_read_lock(&vfsmount_lock);
16955b6ca027SAl Viro 			rcu_read_lock();
16965b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
16975b6ca027SAl Viro 		} else {
16985b6ca027SAl Viro 			path_get(&nd->path);
16995b6ca027SAl Viro 		}
17005b6ca027SAl Viro 		return 0;
17015b6ca027SAl Viro 	}
17025b6ca027SAl Viro 
170331e6b01fSNick Piggin 	nd->root.mnt = NULL;
170431e6b01fSNick Piggin 
170531e6b01fSNick Piggin 	if (*name=='/') {
1706e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
1707962830dfSAndi Kleen 			br_read_lock(&vfsmount_lock);
170831e6b01fSNick Piggin 			rcu_read_lock();
1709e41f7d4eSAl Viro 			set_root_rcu(nd);
1710e41f7d4eSAl Viro 		} else {
1711e41f7d4eSAl Viro 			set_root(nd);
1712e41f7d4eSAl Viro 			path_get(&nd->root);
1713e41f7d4eSAl Viro 		}
171431e6b01fSNick Piggin 		nd->path = nd->root;
171531e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1716e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
171731e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1718c28cc364SNick Piggin 			unsigned seq;
171931e6b01fSNick Piggin 
1720962830dfSAndi Kleen 			br_read_lock(&vfsmount_lock);
172131e6b01fSNick Piggin 			rcu_read_lock();
172231e6b01fSNick Piggin 
1723c28cc364SNick Piggin 			do {
1724c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
172531e6b01fSNick Piggin 				nd->path = fs->pwd;
1726c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1727c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1728e41f7d4eSAl Viro 		} else {
1729e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1730e41f7d4eSAl Viro 		}
173131e6b01fSNick Piggin 	} else {
173231e6b01fSNick Piggin 		struct dentry *dentry;
173331e6b01fSNick Piggin 
17341abf0c71SAl Viro 		file = fget_raw_light(dfd, &fput_needed);
173531e6b01fSNick Piggin 		retval = -EBADF;
173631e6b01fSNick Piggin 		if (!file)
173731e6b01fSNick Piggin 			goto out_fail;
173831e6b01fSNick Piggin 
173931e6b01fSNick Piggin 		dentry = file->f_path.dentry;
174031e6b01fSNick Piggin 
1741f52e0c11SAl Viro 		if (*name) {
174231e6b01fSNick Piggin 			retval = -ENOTDIR;
174331e6b01fSNick Piggin 			if (!S_ISDIR(dentry->d_inode->i_mode))
174431e6b01fSNick Piggin 				goto fput_fail;
174531e6b01fSNick Piggin 
17464ad5abb3SAl Viro 			retval = inode_permission(dentry->d_inode, MAY_EXEC);
174731e6b01fSNick Piggin 			if (retval)
174831e6b01fSNick Piggin 				goto fput_fail;
1749f52e0c11SAl Viro 		}
175031e6b01fSNick Piggin 
175131e6b01fSNick Piggin 		nd->path = file->f_path;
1752e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
175331e6b01fSNick Piggin 			if (fput_needed)
175470e9b357SAl Viro 				*fp = file;
1755c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1756962830dfSAndi Kleen 			br_read_lock(&vfsmount_lock);
175731e6b01fSNick Piggin 			rcu_read_lock();
17585590ff0dSUlrich Drepper 		} else {
17595dd784d0SJan Blunck 			path_get(&file->f_path);
17605590ff0dSUlrich Drepper 			fput_light(file, fput_needed);
17611da177e4SLinus Torvalds 		}
1762e41f7d4eSAl Viro 	}
1763e41f7d4eSAl Viro 
176431e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
17659b4a9b14SAl Viro 	return 0;
17662dfdd266SJosef 'Jeff' Sipek 
17679b4a9b14SAl Viro fput_fail:
17689b4a9b14SAl Viro 	fput_light(file, fput_needed);
17699b4a9b14SAl Viro out_fail:
17709b4a9b14SAl Viro 	return retval;
17719b4a9b14SAl Viro }
17729b4a9b14SAl Viro 
1773bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1774bd92d7feSAl Viro {
1775bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1776bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1777bd92d7feSAl Viro 
1778bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
1779bd92d7feSAl Viro 	return walk_component(nd, path, &nd->last, nd->last_type,
1780bd92d7feSAl Viro 					nd->flags & LOOKUP_FOLLOW);
1781bd92d7feSAl Viro }
1782bd92d7feSAl Viro 
17839b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1784ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
17859b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
17869b4a9b14SAl Viro {
178770e9b357SAl Viro 	struct file *base = NULL;
1788bd92d7feSAl Viro 	struct path path;
1789bd92d7feSAl Viro 	int err;
179031e6b01fSNick Piggin 
179131e6b01fSNick Piggin 	/*
179231e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
179331e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
179431e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
179531e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
179631e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
179731e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
179831e6b01fSNick Piggin 	 * analogue, foo_rcu().
179931e6b01fSNick Piggin 	 *
180031e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
180131e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
180231e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
180331e6b01fSNick Piggin 	 * be able to complete).
180431e6b01fSNick Piggin 	 */
1805bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1806ee0827cdSAl Viro 
1807bd92d7feSAl Viro 	if (unlikely(err))
1808bd92d7feSAl Viro 		return err;
1809ee0827cdSAl Viro 
1810ee0827cdSAl Viro 	current->total_link_count = 0;
1811bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1812bd92d7feSAl Viro 
1813bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1814bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1815bd92d7feSAl Viro 		while (err > 0) {
1816bd92d7feSAl Viro 			void *cookie;
1817bd92d7feSAl Viro 			struct path link = path;
1818bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1819574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
18206d7b5aaeSAl Viro 			if (err)
18216d7b5aaeSAl Viro 				break;
1822bd92d7feSAl Viro 			err = lookup_last(nd, &path);
1823574197e0SAl Viro 			put_link(nd, &link, cookie);
1824bd92d7feSAl Viro 		}
1825bd92d7feSAl Viro 	}
1826ee0827cdSAl Viro 
18279f1fafeeSAl Viro 	if (!err)
18289f1fafeeSAl Viro 		err = complete_walk(nd);
1829bd92d7feSAl Viro 
1830bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
1831bd92d7feSAl Viro 		if (!nd->inode->i_op->lookup) {
1832bd92d7feSAl Viro 			path_put(&nd->path);
1833bd23a539SAl Viro 			err = -ENOTDIR;
1834bd92d7feSAl Viro 		}
1835bd92d7feSAl Viro 	}
183616c2cd71SAl Viro 
183770e9b357SAl Viro 	if (base)
183870e9b357SAl Viro 		fput(base);
1839ee0827cdSAl Viro 
18405b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
184131e6b01fSNick Piggin 		path_put(&nd->root);
184231e6b01fSNick Piggin 		nd->root.mnt = NULL;
184331e6b01fSNick Piggin 	}
1844bd92d7feSAl Viro 	return err;
184531e6b01fSNick Piggin }
184631e6b01fSNick Piggin 
1847ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1848ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1849ee0827cdSAl Viro {
1850ee0827cdSAl Viro 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1851ee0827cdSAl Viro 	if (unlikely(retval == -ECHILD))
1852ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags, nd);
1853ee0827cdSAl Viro 	if (unlikely(retval == -ESTALE))
1854ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1855ee0827cdSAl Viro 
185631e6b01fSNick Piggin 	if (likely(!retval)) {
185731e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
185831e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
185931e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
186031e6b01fSNick Piggin 		}
186131e6b01fSNick Piggin 	}
1862170aa3d0SUlrich Drepper 	return retval;
18631da177e4SLinus Torvalds }
18641da177e4SLinus Torvalds 
186579714f72SAl Viro /* does lookup, returns the object with parent locked */
186679714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path)
18675590ff0dSUlrich Drepper {
186879714f72SAl Viro 	struct nameidata nd;
186979714f72SAl Viro 	struct dentry *d;
187079714f72SAl Viro 	int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
187179714f72SAl Viro 	if (err)
187279714f72SAl Viro 		return ERR_PTR(err);
187379714f72SAl Viro 	if (nd.last_type != LAST_NORM) {
187479714f72SAl Viro 		path_put(&nd.path);
187579714f72SAl Viro 		return ERR_PTR(-EINVAL);
187679714f72SAl Viro 	}
187779714f72SAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
18781e0ea001SAl Viro 	d = __lookup_hash(&nd.last, nd.path.dentry, 0);
187979714f72SAl Viro 	if (IS_ERR(d)) {
188079714f72SAl Viro 		mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
188179714f72SAl Viro 		path_put(&nd.path);
188279714f72SAl Viro 		return d;
188379714f72SAl Viro 	}
188479714f72SAl Viro 	*path = nd.path;
188579714f72SAl Viro 	return d;
18865590ff0dSUlrich Drepper }
18875590ff0dSUlrich Drepper 
1888d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1889d1811465SAl Viro {
1890d1811465SAl Viro 	struct nameidata nd;
1891d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1892d1811465SAl Viro 	if (!res)
1893d1811465SAl Viro 		*path = nd.path;
1894d1811465SAl Viro 	return res;
1895d1811465SAl Viro }
1896d1811465SAl Viro 
189716f18200SJosef 'Jeff' Sipek /**
189816f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
189916f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
190016f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
190116f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
190216f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
1903e0a01249SAl Viro  * @path: pointer to struct path to fill
190416f18200SJosef 'Jeff' Sipek  */
190516f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
190616f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
1907e0a01249SAl Viro 		    struct path *path)
190816f18200SJosef 'Jeff' Sipek {
1909e0a01249SAl Viro 	struct nameidata nd;
1910e0a01249SAl Viro 	int err;
1911e0a01249SAl Viro 	nd.root.dentry = dentry;
1912e0a01249SAl Viro 	nd.root.mnt = mnt;
1913e0a01249SAl Viro 	BUG_ON(flags & LOOKUP_PARENT);
19145b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1915e0a01249SAl Viro 	err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1916e0a01249SAl Viro 	if (!err)
1917e0a01249SAl Viro 		*path = nd.path;
1918e0a01249SAl Viro 	return err;
191916f18200SJosef 'Jeff' Sipek }
192016f18200SJosef 'Jeff' Sipek 
1921057f6c01SJames Morris /*
1922057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1923057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1924057f6c01SJames Morris  * SMP-safe.
1925057f6c01SJames Morris  */
1926a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
19271da177e4SLinus Torvalds {
192872bd866aSAl Viro 	return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
19291da177e4SLinus Torvalds }
19301da177e4SLinus Torvalds 
1931eead1911SChristoph Hellwig /**
1932a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1933eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1934eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1935eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1936eead1911SChristoph Hellwig  *
1937a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1938a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1939eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1940eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1941eead1911SChristoph Hellwig  */
1942057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1943057f6c01SJames Morris {
1944057f6c01SJames Morris 	struct qstr this;
19456a96ba54SAl Viro 	unsigned int c;
1946cda309deSMiklos Szeredi 	int err;
1947057f6c01SJames Morris 
19482f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
19492f9092e1SDavid Woodhouse 
19506a96ba54SAl Viro 	this.name = name;
19516a96ba54SAl Viro 	this.len = len;
19520145acc2SLinus Torvalds 	this.hash = full_name_hash(name, len);
19536a96ba54SAl Viro 	if (!len)
19546a96ba54SAl Viro 		return ERR_PTR(-EACCES);
19556a96ba54SAl Viro 
19566a96ba54SAl Viro 	while (len--) {
19576a96ba54SAl Viro 		c = *(const unsigned char *)name++;
19586a96ba54SAl Viro 		if (c == '/' || c == '\0')
19596a96ba54SAl Viro 			return ERR_PTR(-EACCES);
19606a96ba54SAl Viro 	}
19615a202bcdSAl Viro 	/*
19625a202bcdSAl Viro 	 * See if the low-level filesystem might want
19635a202bcdSAl Viro 	 * to use its own hash..
19645a202bcdSAl Viro 	 */
19655a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
19665a202bcdSAl Viro 		int err = base->d_op->d_hash(base, base->d_inode, &this);
19675a202bcdSAl Viro 		if (err < 0)
19685a202bcdSAl Viro 			return ERR_PTR(err);
19695a202bcdSAl Viro 	}
1970eead1911SChristoph Hellwig 
1971cda309deSMiklos Szeredi 	err = inode_permission(base->d_inode, MAY_EXEC);
1972cda309deSMiklos Szeredi 	if (err)
1973cda309deSMiklos Szeredi 		return ERR_PTR(err);
1974cda309deSMiklos Szeredi 
197572bd866aSAl Viro 	return __lookup_hash(&this, base, 0);
1976057f6c01SJames Morris }
1977057f6c01SJames Morris 
19781fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
19791fa1e7f6SAndy Whitcroft 		 struct path *path, int *empty)
19801da177e4SLinus Torvalds {
19812d8f3038SAl Viro 	struct nameidata nd;
19821fa1e7f6SAndy Whitcroft 	char *tmp = getname_flags(name, flags, empty);
19831da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
19841da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
19852d8f3038SAl Viro 
19862d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
19872d8f3038SAl Viro 
19882d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
19891da177e4SLinus Torvalds 		putname(tmp);
19902d8f3038SAl Viro 		if (!err)
19912d8f3038SAl Viro 			*path = nd.path;
19921da177e4SLinus Torvalds 	}
19931da177e4SLinus Torvalds 	return err;
19941da177e4SLinus Torvalds }
19951da177e4SLinus Torvalds 
19961fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags,
19971fa1e7f6SAndy Whitcroft 		 struct path *path)
19981fa1e7f6SAndy Whitcroft {
1999f7493e5dSLinus Torvalds 	return user_path_at_empty(dfd, name, flags, path, NULL);
20001fa1e7f6SAndy Whitcroft }
20011fa1e7f6SAndy Whitcroft 
20022ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
20032ad94ae6SAl Viro 			struct nameidata *nd, char **name)
20042ad94ae6SAl Viro {
20052ad94ae6SAl Viro 	char *s = getname(path);
20062ad94ae6SAl Viro 	int error;
20072ad94ae6SAl Viro 
20082ad94ae6SAl Viro 	if (IS_ERR(s))
20092ad94ae6SAl Viro 		return PTR_ERR(s);
20102ad94ae6SAl Viro 
20112ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
20122ad94ae6SAl Viro 	if (error)
20132ad94ae6SAl Viro 		putname(s);
20142ad94ae6SAl Viro 	else
20152ad94ae6SAl Viro 		*name = s;
20162ad94ae6SAl Viro 
20172ad94ae6SAl Viro 	return error;
20182ad94ae6SAl Viro }
20192ad94ae6SAl Viro 
20201da177e4SLinus Torvalds /*
20211da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
20221da177e4SLinus Torvalds  * minimal.
20231da177e4SLinus Torvalds  */
20241da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
20251da177e4SLinus Torvalds {
20268e96e3b7SEric W. Biederman 	kuid_t fsuid = current_fsuid();
2027da9592edSDavid Howells 
20281da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
20291da177e4SLinus Torvalds 		return 0;
20308e96e3b7SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
20311da177e4SLinus Torvalds 		return 0;
20328e96e3b7SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
20331da177e4SLinus Torvalds 		return 0;
20341a48e2acSEric W. Biederman 	return !inode_capable(inode, CAP_FOWNER);
20351da177e4SLinus Torvalds }
20361da177e4SLinus Torvalds 
20371da177e4SLinus Torvalds /*
20381da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
20391da177e4SLinus Torvalds  *  whether the type of victim is right.
20401da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
20411da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
20421da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
20431da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
20441da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
20451da177e4SLinus Torvalds  *	a. be owner of dir, or
20461da177e4SLinus Torvalds  *	b. be owner of victim, or
20471da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
20481da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
20491da177e4SLinus Torvalds  *     links pointing to it.
20501da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
20511da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
20521da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
20531da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
20541da177e4SLinus Torvalds  *     nfs_async_unlink().
20551da177e4SLinus Torvalds  */
2056858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
20571da177e4SLinus Torvalds {
20581da177e4SLinus Torvalds 	int error;
20591da177e4SLinus Torvalds 
20601da177e4SLinus Torvalds 	if (!victim->d_inode)
20611da177e4SLinus Torvalds 		return -ENOENT;
20621da177e4SLinus Torvalds 
20631da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
2064cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
20651da177e4SLinus Torvalds 
2066f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
20671da177e4SLinus Torvalds 	if (error)
20681da177e4SLinus Torvalds 		return error;
20691da177e4SLinus Torvalds 	if (IS_APPEND(dir))
20701da177e4SLinus Torvalds 		return -EPERM;
20711da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
2072f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
20731da177e4SLinus Torvalds 		return -EPERM;
20741da177e4SLinus Torvalds 	if (isdir) {
20751da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
20761da177e4SLinus Torvalds 			return -ENOTDIR;
20771da177e4SLinus Torvalds 		if (IS_ROOT(victim))
20781da177e4SLinus Torvalds 			return -EBUSY;
20791da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
20801da177e4SLinus Torvalds 		return -EISDIR;
20811da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
20821da177e4SLinus Torvalds 		return -ENOENT;
20831da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
20841da177e4SLinus Torvalds 		return -EBUSY;
20851da177e4SLinus Torvalds 	return 0;
20861da177e4SLinus Torvalds }
20871da177e4SLinus Torvalds 
20881da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
20891da177e4SLinus Torvalds  *  dir.
20901da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
20911da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
20921da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
20931da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
20941da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
20951da177e4SLinus Torvalds  */
2096a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
20971da177e4SLinus Torvalds {
20981da177e4SLinus Torvalds 	if (child->d_inode)
20991da177e4SLinus Torvalds 		return -EEXIST;
21001da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
21011da177e4SLinus Torvalds 		return -ENOENT;
2102f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
21031da177e4SLinus Torvalds }
21041da177e4SLinus Torvalds 
21051da177e4SLinus Torvalds /*
21061da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
21071da177e4SLinus Torvalds  */
21081da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
21091da177e4SLinus Torvalds {
21101da177e4SLinus Torvalds 	struct dentry *p;
21111da177e4SLinus Torvalds 
21121da177e4SLinus Torvalds 	if (p1 == p2) {
2113f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
21141da177e4SLinus Torvalds 		return NULL;
21151da177e4SLinus Torvalds 	}
21161da177e4SLinus Torvalds 
2117a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
21181da177e4SLinus Torvalds 
2119e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
2120e2761a11SOGAWA Hirofumi 	if (p) {
2121f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2122f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
21231da177e4SLinus Torvalds 		return p;
21241da177e4SLinus Torvalds 	}
21251da177e4SLinus Torvalds 
2126e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
2127e2761a11SOGAWA Hirofumi 	if (p) {
2128f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2129f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
21301da177e4SLinus Torvalds 		return p;
21311da177e4SLinus Torvalds 	}
21321da177e4SLinus Torvalds 
2133f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2134f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
21351da177e4SLinus Torvalds 	return NULL;
21361da177e4SLinus Torvalds }
21371da177e4SLinus Torvalds 
21381da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
21391da177e4SLinus Torvalds {
21401b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
21411da177e4SLinus Torvalds 	if (p1 != p2) {
21421b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
2143a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
21441da177e4SLinus Torvalds 	}
21451da177e4SLinus Torvalds }
21461da177e4SLinus Torvalds 
21474acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2148312b63fbSAl Viro 		bool want_excl)
21491da177e4SLinus Torvalds {
2150a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
21511da177e4SLinus Torvalds 	if (error)
21521da177e4SLinus Torvalds 		return error;
21531da177e4SLinus Torvalds 
2154acfa4380SAl Viro 	if (!dir->i_op->create)
21551da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
21561da177e4SLinus Torvalds 	mode &= S_IALLUGO;
21571da177e4SLinus Torvalds 	mode |= S_IFREG;
21581da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
21591da177e4SLinus Torvalds 	if (error)
21601da177e4SLinus Torvalds 		return error;
2161312b63fbSAl Viro 	error = dir->i_op->create(dir, dentry, mode, want_excl);
2162a74574aaSStephen Smalley 	if (!error)
2163f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
21641da177e4SLinus Torvalds 	return error;
21651da177e4SLinus Torvalds }
21661da177e4SLinus Torvalds 
216773d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
21681da177e4SLinus Torvalds {
21693fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
21701da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
21711da177e4SLinus Torvalds 	int error;
21721da177e4SLinus Torvalds 
2173bcda7652SAl Viro 	/* O_PATH? */
2174bcda7652SAl Viro 	if (!acc_mode)
2175bcda7652SAl Viro 		return 0;
2176bcda7652SAl Viro 
21771da177e4SLinus Torvalds 	if (!inode)
21781da177e4SLinus Torvalds 		return -ENOENT;
21791da177e4SLinus Torvalds 
2180c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2181c8fe8f30SChristoph Hellwig 	case S_IFLNK:
21821da177e4SLinus Torvalds 		return -ELOOP;
2183c8fe8f30SChristoph Hellwig 	case S_IFDIR:
2184c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
21851da177e4SLinus Torvalds 			return -EISDIR;
2186c8fe8f30SChristoph Hellwig 		break;
2187c8fe8f30SChristoph Hellwig 	case S_IFBLK:
2188c8fe8f30SChristoph Hellwig 	case S_IFCHR:
21893fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
21901da177e4SLinus Torvalds 			return -EACCES;
2191c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
2192c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2193c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
21941da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2195c8fe8f30SChristoph Hellwig 		break;
21964a3fd211SDave Hansen 	}
2197b41572e9SDave Hansen 
21983fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
2199b41572e9SDave Hansen 	if (error)
2200b41572e9SDave Hansen 		return error;
22016146f0d5SMimi Zohar 
22021da177e4SLinus Torvalds 	/*
22031da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
22041da177e4SLinus Torvalds 	 */
22051da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
22068737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
22077715b521SAl Viro 			return -EPERM;
22081da177e4SLinus Torvalds 		if (flag & O_TRUNC)
22097715b521SAl Viro 			return -EPERM;
22101da177e4SLinus Torvalds 	}
22111da177e4SLinus Torvalds 
22121da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
22132e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
22147715b521SAl Viro 		return -EPERM;
22151da177e4SLinus Torvalds 
2216f3c7691eSJ. Bruce Fields 	return 0;
22177715b521SAl Viro }
22187715b521SAl Viro 
2219e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
22207715b521SAl Viro {
2221e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
22227715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
22237715b521SAl Viro 	int error = get_write_access(inode);
22241da177e4SLinus Torvalds 	if (error)
22257715b521SAl Viro 		return error;
22261da177e4SLinus Torvalds 	/*
22271da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
22281da177e4SLinus Torvalds 	 */
22291da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2230be6d3e56SKentaro Takeda 	if (!error)
2231ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
22321da177e4SLinus Torvalds 	if (!error) {
22337715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2234d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2235e1181ee6SJeff Layton 				    filp);
22361da177e4SLinus Torvalds 	}
22371da177e4SLinus Torvalds 	put_write_access(inode);
2238acd0c935SMimi Zohar 	return error;
22391da177e4SLinus Torvalds }
22401da177e4SLinus Torvalds 
2241d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2242d57999e1SDave Hansen {
22438a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
22448a5e929dSAl Viro 		flag--;
2245d57999e1SDave Hansen 	return flag;
2246d57999e1SDave Hansen }
2247d57999e1SDave Hansen 
2248d18e9008SMiklos Szeredi static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2249d18e9008SMiklos Szeredi {
2250d18e9008SMiklos Szeredi 	int error = security_path_mknod(dir, dentry, mode, 0);
2251d18e9008SMiklos Szeredi 	if (error)
2252d18e9008SMiklos Szeredi 		return error;
2253d18e9008SMiklos Szeredi 
2254d18e9008SMiklos Szeredi 	error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2255d18e9008SMiklos Szeredi 	if (error)
2256d18e9008SMiklos Szeredi 		return error;
2257d18e9008SMiklos Szeredi 
2258d18e9008SMiklos Szeredi 	return security_inode_create(dir->dentry->d_inode, dentry, mode);
2259d18e9008SMiklos Szeredi }
2260d18e9008SMiklos Szeredi 
22611acf0af9SDavid Howells /*
22621acf0af9SDavid Howells  * Attempt to atomically look up, create and open a file from a negative
22631acf0af9SDavid Howells  * dentry.
22641acf0af9SDavid Howells  *
22651acf0af9SDavid Howells  * Returns 0 if successful.  The file will have been created and attached to
22661acf0af9SDavid Howells  * @file by the filesystem calling finish_open().
22671acf0af9SDavid Howells  *
22681acf0af9SDavid Howells  * Returns 1 if the file was looked up only or didn't need creating.  The
22691acf0af9SDavid Howells  * caller will need to perform the open themselves.  @path will have been
22701acf0af9SDavid Howells  * updated to point to the new dentry.  This may be negative.
22711acf0af9SDavid Howells  *
22721acf0af9SDavid Howells  * Returns an error code otherwise.
22731acf0af9SDavid Howells  */
22742675a4ebSAl Viro static int atomic_open(struct nameidata *nd, struct dentry *dentry,
227530d90494SAl Viro 			struct path *path, struct file *file,
2276015c3bbcSMiklos Szeredi 			const struct open_flags *op,
227777d660a8SMiklos Szeredi 			bool *want_write, bool need_lookup,
227847237687SAl Viro 			int *opened)
2279d18e9008SMiklos Szeredi {
2280d18e9008SMiklos Szeredi 	struct inode *dir =  nd->path.dentry->d_inode;
2281d18e9008SMiklos Szeredi 	unsigned open_flag = open_to_namei_flags(op->open_flag);
2282d18e9008SMiklos Szeredi 	umode_t mode;
2283d18e9008SMiklos Szeredi 	int error;
2284d18e9008SMiklos Szeredi 	int acc_mode;
2285d18e9008SMiklos Szeredi 	int create_error = 0;
2286d18e9008SMiklos Szeredi 	struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2287d18e9008SMiklos Szeredi 
2288d18e9008SMiklos Szeredi 	BUG_ON(dentry->d_inode);
2289d18e9008SMiklos Szeredi 
2290d18e9008SMiklos Szeredi 	/* Don't create child dentry for a dead directory. */
2291d18e9008SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
22922675a4ebSAl Viro 		error = -ENOENT;
2293d18e9008SMiklos Szeredi 		goto out;
2294d18e9008SMiklos Szeredi 	}
2295d18e9008SMiklos Szeredi 
2296d18e9008SMiklos Szeredi 	mode = op->mode & S_IALLUGO;
2297d18e9008SMiklos Szeredi 	if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2298d18e9008SMiklos Szeredi 		mode &= ~current_umask();
2299d18e9008SMiklos Szeredi 
2300d18e9008SMiklos Szeredi 	if (open_flag & O_EXCL) {
2301d18e9008SMiklos Szeredi 		open_flag &= ~O_TRUNC;
230247237687SAl Viro 		*opened |= FILE_CREATED;
2303d18e9008SMiklos Szeredi 	}
2304d18e9008SMiklos Szeredi 
2305d18e9008SMiklos Szeredi 	/*
2306d18e9008SMiklos Szeredi 	 * Checking write permission is tricky, bacuse we don't know if we are
2307d18e9008SMiklos Szeredi 	 * going to actually need it: O_CREAT opens should work as long as the
2308d18e9008SMiklos Szeredi 	 * file exists.  But checking existence breaks atomicity.  The trick is
2309d18e9008SMiklos Szeredi 	 * to check access and if not granted clear O_CREAT from the flags.
2310d18e9008SMiklos Szeredi 	 *
2311d18e9008SMiklos Szeredi 	 * Another problem is returing the "right" error value (e.g. for an
2312d18e9008SMiklos Szeredi 	 * O_EXCL open we want to return EEXIST not EROFS).
2313d18e9008SMiklos Szeredi 	 */
2314d18e9008SMiklos Szeredi 	if ((open_flag & (O_CREAT | O_TRUNC)) ||
2315d18e9008SMiklos Szeredi 	    (open_flag & O_ACCMODE) != O_RDONLY) {
2316d18e9008SMiklos Szeredi 		error = mnt_want_write(nd->path.mnt);
2317d18e9008SMiklos Szeredi 		if (!error) {
231877d660a8SMiklos Szeredi 			*want_write = true;
2319d18e9008SMiklos Szeredi 		} else if (!(open_flag & O_CREAT)) {
2320d18e9008SMiklos Szeredi 			/*
2321d18e9008SMiklos Szeredi 			 * No O_CREATE -> atomicity not a requirement -> fall
2322d18e9008SMiklos Szeredi 			 * back to lookup + open
2323d18e9008SMiklos Szeredi 			 */
2324d18e9008SMiklos Szeredi 			goto no_open;
2325d18e9008SMiklos Szeredi 		} else if (open_flag & (O_EXCL | O_TRUNC)) {
2326d18e9008SMiklos Szeredi 			/* Fall back and fail with the right error */
2327d18e9008SMiklos Szeredi 			create_error = error;
2328d18e9008SMiklos Szeredi 			goto no_open;
2329d18e9008SMiklos Szeredi 		} else {
2330d18e9008SMiklos Szeredi 			/* No side effects, safe to clear O_CREAT */
2331d18e9008SMiklos Szeredi 			create_error = error;
2332d18e9008SMiklos Szeredi 			open_flag &= ~O_CREAT;
2333d18e9008SMiklos Szeredi 		}
2334d18e9008SMiklos Szeredi 	}
2335d18e9008SMiklos Szeredi 
2336d18e9008SMiklos Szeredi 	if (open_flag & O_CREAT) {
2337d18e9008SMiklos Szeredi 		error = may_o_create(&nd->path, dentry, op->mode);
2338d18e9008SMiklos Szeredi 		if (error) {
2339d18e9008SMiklos Szeredi 			create_error = error;
2340d18e9008SMiklos Szeredi 			if (open_flag & O_EXCL)
2341d18e9008SMiklos Szeredi 				goto no_open;
2342d18e9008SMiklos Szeredi 			open_flag &= ~O_CREAT;
2343d18e9008SMiklos Szeredi 		}
2344d18e9008SMiklos Szeredi 	}
2345d18e9008SMiklos Szeredi 
2346d18e9008SMiklos Szeredi 	if (nd->flags & LOOKUP_DIRECTORY)
2347d18e9008SMiklos Szeredi 		open_flag |= O_DIRECTORY;
2348d18e9008SMiklos Szeredi 
234930d90494SAl Viro 	file->f_path.dentry = DENTRY_NOT_SET;
235030d90494SAl Viro 	file->f_path.mnt = nd->path.mnt;
235130d90494SAl Viro 	error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
235247237687SAl Viro 				      opened);
2353d9585277SAl Viro 	if (error < 0) {
2354d9585277SAl Viro 		if (create_error && error == -ENOENT)
2355d9585277SAl Viro 			error = create_error;
2356d18e9008SMiklos Szeredi 		goto out;
2357d18e9008SMiklos Szeredi 	}
2358d18e9008SMiklos Szeredi 
2359d18e9008SMiklos Szeredi 	acc_mode = op->acc_mode;
236047237687SAl Viro 	if (*opened & FILE_CREATED) {
2361d18e9008SMiklos Szeredi 		fsnotify_create(dir, dentry);
2362d18e9008SMiklos Szeredi 		acc_mode = MAY_OPEN;
2363d18e9008SMiklos Szeredi 	}
2364d18e9008SMiklos Szeredi 
2365d9585277SAl Viro 	if (error) {	/* returned 1, that is */
236630d90494SAl Viro 		if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
23672675a4ebSAl Viro 			error = -EIO;
2368d18e9008SMiklos Szeredi 			goto out;
2369d18e9008SMiklos Szeredi 		}
237030d90494SAl Viro 		if (file->f_path.dentry) {
2371d18e9008SMiklos Szeredi 			dput(dentry);
237230d90494SAl Viro 			dentry = file->f_path.dentry;
2373d18e9008SMiklos Szeredi 		}
2374d18e9008SMiklos Szeredi 		goto looked_up;
2375d18e9008SMiklos Szeredi 	}
2376d18e9008SMiklos Szeredi 
2377d18e9008SMiklos Szeredi 	/*
2378d18e9008SMiklos Szeredi 	 * We didn't have the inode before the open, so check open permission
2379d18e9008SMiklos Szeredi 	 * here.
2380d18e9008SMiklos Szeredi 	 */
23812675a4ebSAl Viro 	error = may_open(&file->f_path, acc_mode, open_flag);
23822675a4ebSAl Viro 	if (error)
23832675a4ebSAl Viro 		fput(file);
2384d18e9008SMiklos Szeredi 
2385d18e9008SMiklos Szeredi out:
2386d18e9008SMiklos Szeredi 	dput(dentry);
23872675a4ebSAl Viro 	return error;
2388d18e9008SMiklos Szeredi 
2389d18e9008SMiklos Szeredi no_open:
2390d18e9008SMiklos Szeredi 	if (need_lookup) {
239172bd866aSAl Viro 		dentry = lookup_real(dir, dentry, nd->flags);
2392d18e9008SMiklos Szeredi 		if (IS_ERR(dentry))
23932675a4ebSAl Viro 			return PTR_ERR(dentry);
2394d18e9008SMiklos Szeredi 
2395d18e9008SMiklos Szeredi 		if (create_error) {
2396d18e9008SMiklos Szeredi 			int open_flag = op->open_flag;
2397d18e9008SMiklos Szeredi 
23982675a4ebSAl Viro 			error = create_error;
2399d18e9008SMiklos Szeredi 			if ((open_flag & O_EXCL)) {
2400d18e9008SMiklos Szeredi 				if (!dentry->d_inode)
2401d18e9008SMiklos Szeredi 					goto out;
2402d18e9008SMiklos Szeredi 			} else if (!dentry->d_inode) {
2403d18e9008SMiklos Szeredi 				goto out;
2404d18e9008SMiklos Szeredi 			} else if ((open_flag & O_TRUNC) &&
2405d18e9008SMiklos Szeredi 				   S_ISREG(dentry->d_inode->i_mode)) {
2406d18e9008SMiklos Szeredi 				goto out;
2407d18e9008SMiklos Szeredi 			}
2408d18e9008SMiklos Szeredi 			/* will fail later, go on to get the right error */
2409d18e9008SMiklos Szeredi 		}
2410d18e9008SMiklos Szeredi 	}
2411d18e9008SMiklos Szeredi looked_up:
2412d18e9008SMiklos Szeredi 	path->dentry = dentry;
2413d18e9008SMiklos Szeredi 	path->mnt = nd->path.mnt;
24142675a4ebSAl Viro 	return 1;
2415d18e9008SMiklos Szeredi }
2416d18e9008SMiklos Szeredi 
241731e6b01fSNick Piggin /*
24181acf0af9SDavid Howells  * Look up and maybe create and open the last component.
2419d58ffd35SMiklos Szeredi  *
2420d58ffd35SMiklos Szeredi  * Must be called with i_mutex held on parent.
2421d58ffd35SMiklos Szeredi  *
24221acf0af9SDavid Howells  * Returns 0 if the file was successfully atomically created (if necessary) and
24231acf0af9SDavid Howells  * opened.  In this case the file will be returned attached to @file.
24241acf0af9SDavid Howells  *
24251acf0af9SDavid Howells  * Returns 1 if the file was not completely opened at this time, though lookups
24261acf0af9SDavid Howells  * and creations will have been performed and the dentry returned in @path will
24271acf0af9SDavid Howells  * be positive upon return if O_CREAT was specified.  If O_CREAT wasn't
24281acf0af9SDavid Howells  * specified then a negative dentry may be returned.
24291acf0af9SDavid Howells  *
24301acf0af9SDavid Howells  * An error code is returned otherwise.
24311acf0af9SDavid Howells  *
24321acf0af9SDavid Howells  * FILE_CREATE will be set in @*opened if the dentry was created and will be
24331acf0af9SDavid Howells  * cleared otherwise prior to returning.
2434d58ffd35SMiklos Szeredi  */
24352675a4ebSAl Viro static int lookup_open(struct nameidata *nd, struct path *path,
243630d90494SAl Viro 			struct file *file,
2437d58ffd35SMiklos Szeredi 			const struct open_flags *op,
243847237687SAl Viro 			bool *want_write, int *opened)
2439d58ffd35SMiklos Szeredi {
2440d58ffd35SMiklos Szeredi 	struct dentry *dir = nd->path.dentry;
244154ef4872SMiklos Szeredi 	struct inode *dir_inode = dir->d_inode;
2442d58ffd35SMiklos Szeredi 	struct dentry *dentry;
2443d58ffd35SMiklos Szeredi 	int error;
244454ef4872SMiklos Szeredi 	bool need_lookup;
2445d58ffd35SMiklos Szeredi 
244647237687SAl Viro 	*opened &= ~FILE_CREATED;
2447201f956eSAl Viro 	dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
2448d58ffd35SMiklos Szeredi 	if (IS_ERR(dentry))
24492675a4ebSAl Viro 		return PTR_ERR(dentry);
2450d58ffd35SMiklos Szeredi 
2451d18e9008SMiklos Szeredi 	/* Cached positive dentry: will open in f_op->open */
2452d18e9008SMiklos Szeredi 	if (!need_lookup && dentry->d_inode)
2453d18e9008SMiklos Szeredi 		goto out_no_open;
2454d18e9008SMiklos Szeredi 
2455d18e9008SMiklos Szeredi 	if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
245630d90494SAl Viro 		return atomic_open(nd, dentry, path, file, op, want_write,
245747237687SAl Viro 				   need_lookup, opened);
2458d18e9008SMiklos Szeredi 	}
2459d18e9008SMiklos Szeredi 
246054ef4872SMiklos Szeredi 	if (need_lookup) {
246154ef4872SMiklos Szeredi 		BUG_ON(dentry->d_inode);
246254ef4872SMiklos Szeredi 
246372bd866aSAl Viro 		dentry = lookup_real(dir_inode, dentry, nd->flags);
246454ef4872SMiklos Szeredi 		if (IS_ERR(dentry))
24652675a4ebSAl Viro 			return PTR_ERR(dentry);
246654ef4872SMiklos Szeredi 	}
246754ef4872SMiklos Szeredi 
2468d58ffd35SMiklos Szeredi 	/* Negative dentry, just create the file */
2469d58ffd35SMiklos Szeredi 	if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2470d58ffd35SMiklos Szeredi 		umode_t mode = op->mode;
2471d58ffd35SMiklos Szeredi 		if (!IS_POSIXACL(dir->d_inode))
2472d58ffd35SMiklos Szeredi 			mode &= ~current_umask();
2473d58ffd35SMiklos Szeredi 		/*
2474d58ffd35SMiklos Szeredi 		 * This write is needed to ensure that a
2475d58ffd35SMiklos Szeredi 		 * rw->ro transition does not occur between
2476d58ffd35SMiklos Szeredi 		 * the time when the file is created and when
2477d58ffd35SMiklos Szeredi 		 * a permanent write count is taken through
2478015c3bbcSMiklos Szeredi 		 * the 'struct file' in finish_open().
2479d58ffd35SMiklos Szeredi 		 */
2480d58ffd35SMiklos Szeredi 		error = mnt_want_write(nd->path.mnt);
2481d58ffd35SMiklos Szeredi 		if (error)
2482d58ffd35SMiklos Szeredi 			goto out_dput;
248377d660a8SMiklos Szeredi 		*want_write = true;
248447237687SAl Viro 		*opened |= FILE_CREATED;
2485d58ffd35SMiklos Szeredi 		error = security_path_mknod(&nd->path, dentry, mode, 0);
2486d58ffd35SMiklos Szeredi 		if (error)
2487d58ffd35SMiklos Szeredi 			goto out_dput;
2488312b63fbSAl Viro 		error = vfs_create(dir->d_inode, dentry, mode,
2489312b63fbSAl Viro 				   nd->flags & LOOKUP_EXCL);
2490d58ffd35SMiklos Szeredi 		if (error)
2491d58ffd35SMiklos Szeredi 			goto out_dput;
2492d58ffd35SMiklos Szeredi 	}
2493d18e9008SMiklos Szeredi out_no_open:
2494d58ffd35SMiklos Szeredi 	path->dentry = dentry;
2495d58ffd35SMiklos Szeredi 	path->mnt = nd->path.mnt;
24962675a4ebSAl Viro 	return 1;
2497d58ffd35SMiklos Szeredi 
2498d58ffd35SMiklos Szeredi out_dput:
2499d58ffd35SMiklos Szeredi 	dput(dentry);
25002675a4ebSAl Viro 	return error;
2501d58ffd35SMiklos Szeredi }
2502d58ffd35SMiklos Szeredi 
2503d58ffd35SMiklos Szeredi /*
2504fe2d35ffSAl Viro  * Handle the last step of open()
250531e6b01fSNick Piggin  */
25062675a4ebSAl Viro static int do_last(struct nameidata *nd, struct path *path,
250730d90494SAl Viro 		   struct file *file, const struct open_flags *op,
250847237687SAl Viro 		   int *opened, const char *pathname)
2509fb1cc555SAl Viro {
2510a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
2511ca344a89SAl Viro 	int open_flag = op->open_flag;
251277d660a8SMiklos Szeredi 	bool will_truncate = (open_flag & O_TRUNC) != 0;
251377d660a8SMiklos Szeredi 	bool want_write = false;
2514bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2515a1eb3315SMiklos Szeredi 	struct inode *inode;
251677d660a8SMiklos Szeredi 	bool symlink_ok = false;
251716b1c1cdSMiklos Szeredi 	struct path save_parent = { .dentry = NULL, .mnt = NULL };
251816b1c1cdSMiklos Szeredi 	bool retried = false;
251916c2cd71SAl Viro 	int error;
2520fb1cc555SAl Viro 
2521c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2522c3e380b0SAl Viro 	nd->flags |= op->intent;
2523c3e380b0SAl Viro 
25241f36f774SAl Viro 	switch (nd->last_type) {
25251f36f774SAl Viro 	case LAST_DOTDOT:
2526176306f5SNeil Brown 	case LAST_DOT:
2527fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2528fe2d35ffSAl Viro 		if (error)
25292675a4ebSAl Viro 			return error;
25301f36f774SAl Viro 		/* fallthrough */
25311f36f774SAl Viro 	case LAST_ROOT:
25329f1fafeeSAl Viro 		error = complete_walk(nd);
253316c2cd71SAl Viro 		if (error)
25342675a4ebSAl Viro 			return error;
2535fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2536ca344a89SAl Viro 		if (open_flag & O_CREAT) {
253716c2cd71SAl Viro 			error = -EISDIR;
25382675a4ebSAl Viro 			goto out;
2539fe2d35ffSAl Viro 		}
2540e83db167SMiklos Szeredi 		goto finish_open;
25411f36f774SAl Viro 	case LAST_BIND:
25429f1fafeeSAl Viro 		error = complete_walk(nd);
254316c2cd71SAl Viro 		if (error)
25442675a4ebSAl Viro 			return error;
25451f36f774SAl Viro 		audit_inode(pathname, dir);
2546e83db167SMiklos Szeredi 		goto finish_open;
25471f36f774SAl Viro 	}
2548a2c36b45SAl Viro 
2549ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2550fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2551fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2552bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
255377d660a8SMiklos Szeredi 			symlink_ok = true;
2554fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2555a1eb3315SMiklos Szeredi 		error = lookup_fast(nd, &nd->last, path, &inode);
255671574865SMiklos Szeredi 		if (likely(!error))
255771574865SMiklos Szeredi 			goto finish_lookup;
255871574865SMiklos Szeredi 
2559ce57dfc1SAl Viro 		if (error < 0)
25602675a4ebSAl Viro 			goto out;
2561a1eb3315SMiklos Szeredi 
256237d7fffcSMiklos Szeredi 		BUG_ON(nd->inode != dir->d_inode);
2563b6183df7SMiklos Szeredi 	} else {
2564fe2d35ffSAl Viro 		/* create side of things */
2565a3fbbde7SAl Viro 		/*
2566b6183df7SMiklos Szeredi 		 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2567b6183df7SMiklos Szeredi 		 * has been cleared when we got to the last component we are
2568b6183df7SMiklos Szeredi 		 * about to look up
2569a3fbbde7SAl Viro 		 */
25709f1fafeeSAl Viro 		error = complete_walk(nd);
25719f1fafeeSAl Viro 		if (error)
25722675a4ebSAl Viro 			return error;
2573fe2d35ffSAl Viro 
2574fe2d35ffSAl Viro 		audit_inode(pathname, dir);
257516c2cd71SAl Viro 		error = -EISDIR;
25761f36f774SAl Viro 		/* trailing slashes? */
257731e6b01fSNick Piggin 		if (nd->last.name[nd->last.len])
25782675a4ebSAl Viro 			goto out;
2579b6183df7SMiklos Szeredi 	}
25801f36f774SAl Viro 
258116b1c1cdSMiklos Szeredi retry_lookup:
2582a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
25832675a4ebSAl Viro 	error = lookup_open(nd, path, file, op, &want_write, opened);
2584fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2585fb1cc555SAl Viro 
25862675a4ebSAl Viro 	if (error <= 0) {
25872675a4ebSAl Viro 		if (error)
2588d58ffd35SMiklos Szeredi 			goto out;
25896c0d46c4SAl Viro 
259047237687SAl Viro 		if ((*opened & FILE_CREATED) ||
25912675a4ebSAl Viro 		    !S_ISREG(file->f_path.dentry->d_inode->i_mode))
259277d660a8SMiklos Szeredi 			will_truncate = false;
2593d18e9008SMiklos Szeredi 
25942675a4ebSAl Viro 		audit_inode(pathname, file->f_path.dentry);
2595d18e9008SMiklos Szeredi 		goto opened;
2596d18e9008SMiklos Szeredi 	}
2597d18e9008SMiklos Szeredi 
259847237687SAl Viro 	if (*opened & FILE_CREATED) {
25999b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2600ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
260177d660a8SMiklos Szeredi 		will_truncate = false;
2602bcda7652SAl Viro 		acc_mode = MAY_OPEN;
2603d58ffd35SMiklos Szeredi 		path_to_nameidata(path, nd);
2604e83db167SMiklos Szeredi 		goto finish_open_created;
2605fb1cc555SAl Viro 	}
2606fb1cc555SAl Viro 
2607fb1cc555SAl Viro 	/*
2608fb1cc555SAl Viro 	 * It already exists.
2609fb1cc555SAl Viro 	 */
2610fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2611fb1cc555SAl Viro 
2612d18e9008SMiklos Szeredi 	/*
2613d18e9008SMiklos Szeredi 	 * If atomic_open() acquired write access it is dropped now due to
2614d18e9008SMiklos Szeredi 	 * possible mount and symlink following (this might be optimized away if
2615d18e9008SMiklos Szeredi 	 * necessary...)
2616d18e9008SMiklos Szeredi 	 */
2617d18e9008SMiklos Szeredi 	if (want_write) {
2618d18e9008SMiklos Szeredi 		mnt_drop_write(nd->path.mnt);
261977d660a8SMiklos Szeredi 		want_write = false;
2620d18e9008SMiklos Szeredi 	}
2621d18e9008SMiklos Szeredi 
2622fb1cc555SAl Viro 	error = -EEXIST;
2623ca344a89SAl Viro 	if (open_flag & O_EXCL)
2624fb1cc555SAl Viro 		goto exit_dput;
2625fb1cc555SAl Viro 
26269875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
26279875cf80SDavid Howells 	if (error < 0)
2628fb1cc555SAl Viro 		goto exit_dput;
2629fb1cc555SAl Viro 
2630a3fbbde7SAl Viro 	if (error)
2631a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
2632a3fbbde7SAl Viro 
2633decf3400SMiklos Szeredi 	BUG_ON(nd->flags & LOOKUP_RCU);
2634decf3400SMiklos Szeredi 	inode = path->dentry->d_inode;
26355f5daac1SMiklos Szeredi finish_lookup:
26365f5daac1SMiklos Szeredi 	/* we _can_ be in RCU mode here */
2637fb1cc555SAl Viro 	error = -ENOENT;
263854c33e7fSMiklos Szeredi 	if (!inode) {
263954c33e7fSMiklos Szeredi 		path_to_nameidata(path, nd);
26402675a4ebSAl Viro 		goto out;
264154c33e7fSMiklos Szeredi 	}
26429e67f361SAl Viro 
2643d45ea867SMiklos Szeredi 	if (should_follow_link(inode, !symlink_ok)) {
2644d45ea867SMiklos Szeredi 		if (nd->flags & LOOKUP_RCU) {
2645d45ea867SMiklos Szeredi 			if (unlikely(unlazy_walk(nd, path->dentry))) {
2646d45ea867SMiklos Szeredi 				error = -ECHILD;
26472675a4ebSAl Viro 				goto out;
2648d45ea867SMiklos Szeredi 			}
2649d45ea867SMiklos Szeredi 		}
2650d45ea867SMiklos Szeredi 		BUG_ON(inode != path->dentry->d_inode);
26512675a4ebSAl Viro 		return 1;
2652d45ea867SMiklos Szeredi 	}
2653fb1cc555SAl Viro 
265416b1c1cdSMiklos Szeredi 	if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
2655fb1cc555SAl Viro 		path_to_nameidata(path, nd);
265616b1c1cdSMiklos Szeredi 	} else {
265716b1c1cdSMiklos Szeredi 		save_parent.dentry = nd->path.dentry;
265816b1c1cdSMiklos Szeredi 		save_parent.mnt = mntget(path->mnt);
265916b1c1cdSMiklos Szeredi 		nd->path.dentry = path->dentry;
266016b1c1cdSMiklos Szeredi 
266116b1c1cdSMiklos Szeredi 	}
2662decf3400SMiklos Szeredi 	nd->inode = inode;
2663a3fbbde7SAl Viro 	/* Why this, you ask?  _Now_ we might have grown LOOKUP_JUMPED... */
2664a3fbbde7SAl Viro 	error = complete_walk(nd);
266516b1c1cdSMiklos Szeredi 	if (error) {
266616b1c1cdSMiklos Szeredi 		path_put(&save_parent);
26672675a4ebSAl Viro 		return error;
266816b1c1cdSMiklos Szeredi 	}
2669fb1cc555SAl Viro 	error = -EISDIR;
2670050ac841SMiklos Szeredi 	if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
26712675a4ebSAl Viro 		goto out;
2672af2f5542SMiklos Szeredi 	error = -ENOTDIR;
2673af2f5542SMiklos Szeredi 	if ((nd->flags & LOOKUP_DIRECTORY) && !nd->inode->i_op->lookup)
26742675a4ebSAl Viro 		goto out;
2675d7fdd7f6SMiklos Szeredi 	audit_inode(pathname, nd->path.dentry);
2676e83db167SMiklos Szeredi finish_open:
26776c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
267877d660a8SMiklos Szeredi 		will_truncate = false;
26796c0d46c4SAl Viro 
26800f9d1a10SAl Viro 	if (will_truncate) {
26810f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
26820f9d1a10SAl Viro 		if (error)
26832675a4ebSAl Viro 			goto out;
268477d660a8SMiklos Szeredi 		want_write = true;
26850f9d1a10SAl Viro 	}
2686e83db167SMiklos Szeredi finish_open_created:
2687bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2688ca344a89SAl Viro 	if (error)
26892675a4ebSAl Viro 		goto out;
269030d90494SAl Viro 	file->f_path.mnt = nd->path.mnt;
269130d90494SAl Viro 	error = finish_open(file, nd->path.dentry, NULL, opened);
269230d90494SAl Viro 	if (error) {
269330d90494SAl Viro 		if (error == -EOPENSTALE)
2694f60dc3dbSMiklos Szeredi 			goto stale_open;
2695015c3bbcSMiklos Szeredi 		goto out;
2696f60dc3dbSMiklos Szeredi 	}
2697a8277b9bSMiklos Szeredi opened:
26982675a4ebSAl Viro 	error = open_check_o_direct(file);
2699015c3bbcSMiklos Szeredi 	if (error)
2700015c3bbcSMiklos Szeredi 		goto exit_fput;
27012675a4ebSAl Viro 	error = ima_file_check(file, op->acc_mode);
2702aa4caadbSMiklos Szeredi 	if (error)
2703aa4caadbSMiklos Szeredi 		goto exit_fput;
2704aa4caadbSMiklos Szeredi 
27050f9d1a10SAl Viro 	if (will_truncate) {
27062675a4ebSAl Viro 		error = handle_truncate(file);
2707aa4caadbSMiklos Szeredi 		if (error)
2708aa4caadbSMiklos Szeredi 			goto exit_fput;
27090f9d1a10SAl Viro 	}
2710ca344a89SAl Viro out:
2711ca344a89SAl Viro 	if (want_write)
27120f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
271316b1c1cdSMiklos Szeredi 	path_put(&save_parent);
2714e276ae67SMiklos Szeredi 	terminate_walk(nd);
27152675a4ebSAl Viro 	return error;
2716fb1cc555SAl Viro 
2717fb1cc555SAl Viro exit_dput:
2718fb1cc555SAl Viro 	path_put_conditional(path, nd);
2719ca344a89SAl Viro 	goto out;
2720015c3bbcSMiklos Szeredi exit_fput:
27212675a4ebSAl Viro 	fput(file);
27222675a4ebSAl Viro 	goto out;
2723015c3bbcSMiklos Szeredi 
2724f60dc3dbSMiklos Szeredi stale_open:
2725f60dc3dbSMiklos Szeredi 	/* If no saved parent or already retried then can't retry */
2726f60dc3dbSMiklos Szeredi 	if (!save_parent.dentry || retried)
2727f60dc3dbSMiklos Szeredi 		goto out;
2728f60dc3dbSMiklos Szeredi 
2729f60dc3dbSMiklos Szeredi 	BUG_ON(save_parent.dentry != dir);
2730f60dc3dbSMiklos Szeredi 	path_put(&nd->path);
2731f60dc3dbSMiklos Szeredi 	nd->path = save_parent;
2732f60dc3dbSMiklos Szeredi 	nd->inode = dir->d_inode;
2733f60dc3dbSMiklos Szeredi 	save_parent.mnt = NULL;
2734f60dc3dbSMiklos Szeredi 	save_parent.dentry = NULL;
2735f60dc3dbSMiklos Szeredi 	if (want_write) {
2736f60dc3dbSMiklos Szeredi 		mnt_drop_write(nd->path.mnt);
2737f60dc3dbSMiklos Szeredi 		want_write = false;
2738f60dc3dbSMiklos Szeredi 	}
2739f60dc3dbSMiklos Szeredi 	retried = true;
2740f60dc3dbSMiklos Szeredi 	goto retry_lookup;
2741fb1cc555SAl Viro }
2742fb1cc555SAl Viro 
274313aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname,
274473d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
27451da177e4SLinus Torvalds {
2746fe2d35ffSAl Viro 	struct file *base = NULL;
274730d90494SAl Viro 	struct file *file;
27489850c056SAl Viro 	struct path path;
274947237687SAl Viro 	int opened = 0;
275013aab428SAl Viro 	int error;
275131e6b01fSNick Piggin 
275230d90494SAl Viro 	file = get_empty_filp();
275330d90494SAl Viro 	if (!file)
275431e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
275531e6b01fSNick Piggin 
275630d90494SAl Viro 	file->f_flags = op->open_flag;
275731e6b01fSNick Piggin 
275873d049a4SAl Viro 	error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
275931e6b01fSNick Piggin 	if (unlikely(error))
27602675a4ebSAl Viro 		goto out;
276131e6b01fSNick Piggin 
2762fe2d35ffSAl Viro 	current->total_link_count = 0;
276373d049a4SAl Viro 	error = link_path_walk(pathname, nd);
276431e6b01fSNick Piggin 	if (unlikely(error))
27652675a4ebSAl Viro 		goto out;
27661da177e4SLinus Torvalds 
27672675a4ebSAl Viro 	error = do_last(nd, &path, file, op, &opened, pathname);
27682675a4ebSAl Viro 	while (unlikely(error > 0)) { /* trailing symlink */
27697b9337aaSNick Piggin 		struct path link = path;
2770def4af30SAl Viro 		void *cookie;
2771574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
277273d049a4SAl Viro 			path_put_conditional(&path, nd);
277373d049a4SAl Viro 			path_put(&nd->path);
27742675a4ebSAl Viro 			error = -ELOOP;
277540b39136SAl Viro 			break;
277640b39136SAl Viro 		}
277773d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
277873d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2779574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
2780c3e380b0SAl Viro 		if (unlikely(error))
27812675a4ebSAl Viro 			break;
27822675a4ebSAl Viro 		error = do_last(nd, &path, file, op, &opened, pathname);
2783574197e0SAl Viro 		put_link(nd, &link, cookie);
2784806b681cSAl Viro 	}
278510fa8e62SAl Viro out:
278673d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
278773d049a4SAl Viro 		path_put(&nd->root);
2788fe2d35ffSAl Viro 	if (base)
2789fe2d35ffSAl Viro 		fput(base);
27902675a4ebSAl Viro 	if (!(opened & FILE_OPENED)) {
27912675a4ebSAl Viro 		BUG_ON(!error);
279230d90494SAl Viro 		put_filp(file);
2793015c3bbcSMiklos Szeredi 	}
27942675a4ebSAl Viro 	if (unlikely(error)) {
27952675a4ebSAl Viro 		if (error == -EOPENSTALE) {
27962675a4ebSAl Viro 			if (flags & LOOKUP_RCU)
27972675a4ebSAl Viro 				error = -ECHILD;
27982675a4ebSAl Viro 			else
27992675a4ebSAl Viro 				error = -ESTALE;
28002675a4ebSAl Viro 		}
28012675a4ebSAl Viro 		file = ERR_PTR(error);
28022675a4ebSAl Viro 	}
28032675a4ebSAl Viro 	return file;
2804de459215SKirill Korotaev }
28051da177e4SLinus Torvalds 
280613aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname,
280713aab428SAl Viro 		const struct open_flags *op, int flags)
280813aab428SAl Viro {
280973d049a4SAl Viro 	struct nameidata nd;
281013aab428SAl Viro 	struct file *filp;
281113aab428SAl Viro 
281273d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
281313aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
281473d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
281513aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
281673d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
281713aab428SAl Viro 	return filp;
281813aab428SAl Viro }
281913aab428SAl Viro 
282073d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
282173d049a4SAl Viro 		const char *name, const struct open_flags *op, int flags)
282273d049a4SAl Viro {
282373d049a4SAl Viro 	struct nameidata nd;
282473d049a4SAl Viro 	struct file *file;
282573d049a4SAl Viro 
282673d049a4SAl Viro 	nd.root.mnt = mnt;
282773d049a4SAl Viro 	nd.root.dentry = dentry;
282873d049a4SAl Viro 
282973d049a4SAl Viro 	flags |= LOOKUP_ROOT;
283073d049a4SAl Viro 
2831bcda7652SAl Viro 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
283273d049a4SAl Viro 		return ERR_PTR(-ELOOP);
283373d049a4SAl Viro 
283473d049a4SAl Viro 	file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
283573d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
283673d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags);
283773d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
283873d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
283973d049a4SAl Viro 	return file;
284073d049a4SAl Viro }
284173d049a4SAl Viro 
2842ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
28431da177e4SLinus Torvalds {
2844c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
2845ed75e95dSAl Viro 	struct nameidata nd;
2846ed75e95dSAl Viro 	int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2847ed75e95dSAl Viro 	if (error)
2848ed75e95dSAl Viro 		return ERR_PTR(error);
28491da177e4SLinus Torvalds 
2850c663e5d8SChristoph Hellwig 	/*
2851c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2852c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2853c663e5d8SChristoph Hellwig 	 */
2854ed75e95dSAl Viro 	if (nd.last_type != LAST_NORM)
2855ed75e95dSAl Viro 		goto out;
2856ed75e95dSAl Viro 	nd.flags &= ~LOOKUP_PARENT;
2857ed75e95dSAl Viro 	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2858c663e5d8SChristoph Hellwig 
2859c663e5d8SChristoph Hellwig 	/*
2860c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2861c663e5d8SChristoph Hellwig 	 */
2862ed75e95dSAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2863ed75e95dSAl Viro 	dentry = lookup_hash(&nd);
28641da177e4SLinus Torvalds 	if (IS_ERR(dentry))
28651da177e4SLinus Torvalds 		goto fail;
2866c663e5d8SChristoph Hellwig 
2867e9baf6e5SAl Viro 	if (dentry->d_inode)
2868e9baf6e5SAl Viro 		goto eexist;
2869c663e5d8SChristoph Hellwig 	/*
2870c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2871c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2872c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2873c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2874c663e5d8SChristoph Hellwig 	 */
2875ed75e95dSAl Viro 	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
28761da177e4SLinus Torvalds 		dput(dentry);
28771da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2878ed75e95dSAl Viro 		goto fail;
2879e9baf6e5SAl Viro 	}
2880ed75e95dSAl Viro 	*path = nd.path;
2881e9baf6e5SAl Viro 	return dentry;
2882e9baf6e5SAl Viro eexist:
2883e9baf6e5SAl Viro 	dput(dentry);
2884e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
28851da177e4SLinus Torvalds fail:
2886dae6ad8fSAl Viro 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2887ed75e95dSAl Viro out:
2888dae6ad8fSAl Viro 	path_put(&nd.path);
2889ed75e95dSAl Viro 	return dentry;
2890dae6ad8fSAl Viro }
2891dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create);
2892dae6ad8fSAl Viro 
2893dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2894dae6ad8fSAl Viro {
2895dae6ad8fSAl Viro 	char *tmp = getname(pathname);
2896dae6ad8fSAl Viro 	struct dentry *res;
2897dae6ad8fSAl Viro 	if (IS_ERR(tmp))
2898dae6ad8fSAl Viro 		return ERR_CAST(tmp);
2899dae6ad8fSAl Viro 	res = kern_path_create(dfd, tmp, path, is_dir);
2900dae6ad8fSAl Viro 	putname(tmp);
2901dae6ad8fSAl Viro 	return res;
2902dae6ad8fSAl Viro }
2903dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create);
2904dae6ad8fSAl Viro 
29051a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
29061da177e4SLinus Torvalds {
2907a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
29081da177e4SLinus Torvalds 
29091da177e4SLinus Torvalds 	if (error)
29101da177e4SLinus Torvalds 		return error;
29111da177e4SLinus Torvalds 
2912975d6b39SEric W. Biederman 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
29131da177e4SLinus Torvalds 		return -EPERM;
29141da177e4SLinus Torvalds 
2915acfa4380SAl Viro 	if (!dir->i_op->mknod)
29161da177e4SLinus Torvalds 		return -EPERM;
29171da177e4SLinus Torvalds 
291808ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
291908ce5f16SSerge E. Hallyn 	if (error)
292008ce5f16SSerge E. Hallyn 		return error;
292108ce5f16SSerge E. Hallyn 
29221da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
29231da177e4SLinus Torvalds 	if (error)
29241da177e4SLinus Torvalds 		return error;
29251da177e4SLinus Torvalds 
29261da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2927a74574aaSStephen Smalley 	if (!error)
2928f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
29291da177e4SLinus Torvalds 	return error;
29301da177e4SLinus Torvalds }
29311da177e4SLinus Torvalds 
2932f69aac00SAl Viro static int may_mknod(umode_t mode)
2933463c3197SDave Hansen {
2934463c3197SDave Hansen 	switch (mode & S_IFMT) {
2935463c3197SDave Hansen 	case S_IFREG:
2936463c3197SDave Hansen 	case S_IFCHR:
2937463c3197SDave Hansen 	case S_IFBLK:
2938463c3197SDave Hansen 	case S_IFIFO:
2939463c3197SDave Hansen 	case S_IFSOCK:
2940463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2941463c3197SDave Hansen 		return 0;
2942463c3197SDave Hansen 	case S_IFDIR:
2943463c3197SDave Hansen 		return -EPERM;
2944463c3197SDave Hansen 	default:
2945463c3197SDave Hansen 		return -EINVAL;
2946463c3197SDave Hansen 	}
2947463c3197SDave Hansen }
2948463c3197SDave Hansen 
29498208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
29502e4d0924SHeiko Carstens 		unsigned, dev)
29511da177e4SLinus Torvalds {
29521da177e4SLinus Torvalds 	struct dentry *dentry;
2953dae6ad8fSAl Viro 	struct path path;
2954dae6ad8fSAl Viro 	int error;
29551da177e4SLinus Torvalds 
29561da177e4SLinus Torvalds 	if (S_ISDIR(mode))
29571da177e4SLinus Torvalds 		return -EPERM;
29581da177e4SLinus Torvalds 
2959dae6ad8fSAl Viro 	dentry = user_path_create(dfd, filename, &path, 0);
2960dae6ad8fSAl Viro 	if (IS_ERR(dentry))
2961dae6ad8fSAl Viro 		return PTR_ERR(dentry);
29622ad94ae6SAl Viro 
2963dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
2964ce3b0f8dSAl Viro 		mode &= ~current_umask();
2965463c3197SDave Hansen 	error = may_mknod(mode);
2966463c3197SDave Hansen 	if (error)
2967463c3197SDave Hansen 		goto out_dput;
2968dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
2969463c3197SDave Hansen 	if (error)
2970463c3197SDave Hansen 		goto out_dput;
2971dae6ad8fSAl Viro 	error = security_path_mknod(&path, dentry, mode, dev);
2972be6d3e56SKentaro Takeda 	if (error)
2973be6d3e56SKentaro Takeda 		goto out_drop_write;
29741da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
29751da177e4SLinus Torvalds 		case 0: case S_IFREG:
2976312b63fbSAl Viro 			error = vfs_create(path.dentry->d_inode,dentry,mode,true);
29771da177e4SLinus Torvalds 			break;
29781da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
2979dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
29801da177e4SLinus Torvalds 					new_decode_dev(dev));
29811da177e4SLinus Torvalds 			break;
29821da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
2983dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
29841da177e4SLinus Torvalds 			break;
29851da177e4SLinus Torvalds 	}
2986be6d3e56SKentaro Takeda out_drop_write:
2987dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
2988463c3197SDave Hansen out_dput:
29891da177e4SLinus Torvalds 	dput(dentry);
2990dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2991dae6ad8fSAl Viro 	path_put(&path);
29921da177e4SLinus Torvalds 
29931da177e4SLinus Torvalds 	return error;
29941da177e4SLinus Torvalds }
29951da177e4SLinus Torvalds 
29968208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
29975590ff0dSUlrich Drepper {
29985590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
29995590ff0dSUlrich Drepper }
30005590ff0dSUlrich Drepper 
300118bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
30021da177e4SLinus Torvalds {
3003a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
30048de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
30051da177e4SLinus Torvalds 
30061da177e4SLinus Torvalds 	if (error)
30071da177e4SLinus Torvalds 		return error;
30081da177e4SLinus Torvalds 
3009acfa4380SAl Viro 	if (!dir->i_op->mkdir)
30101da177e4SLinus Torvalds 		return -EPERM;
30111da177e4SLinus Torvalds 
30121da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
30131da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
30141da177e4SLinus Torvalds 	if (error)
30151da177e4SLinus Torvalds 		return error;
30161da177e4SLinus Torvalds 
30178de52778SAl Viro 	if (max_links && dir->i_nlink >= max_links)
30188de52778SAl Viro 		return -EMLINK;
30198de52778SAl Viro 
30201da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
3021a74574aaSStephen Smalley 	if (!error)
3022f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
30231da177e4SLinus Torvalds 	return error;
30241da177e4SLinus Torvalds }
30251da177e4SLinus Torvalds 
3026a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
30271da177e4SLinus Torvalds {
30286902d925SDave Hansen 	struct dentry *dentry;
3029dae6ad8fSAl Viro 	struct path path;
3030dae6ad8fSAl Viro 	int error;
30311da177e4SLinus Torvalds 
3032dae6ad8fSAl Viro 	dentry = user_path_create(dfd, pathname, &path, 1);
30336902d925SDave Hansen 	if (IS_ERR(dentry))
3034dae6ad8fSAl Viro 		return PTR_ERR(dentry);
30356902d925SDave Hansen 
3036dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3037ce3b0f8dSAl Viro 		mode &= ~current_umask();
3038dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
3039463c3197SDave Hansen 	if (error)
3040463c3197SDave Hansen 		goto out_dput;
3041dae6ad8fSAl Viro 	error = security_path_mkdir(&path, dentry, mode);
3042be6d3e56SKentaro Takeda 	if (error)
3043be6d3e56SKentaro Takeda 		goto out_drop_write;
3044dae6ad8fSAl Viro 	error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3045be6d3e56SKentaro Takeda out_drop_write:
3046dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
3047463c3197SDave Hansen out_dput:
30481da177e4SLinus Torvalds 	dput(dentry);
3049dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
3050dae6ad8fSAl Viro 	path_put(&path);
30511da177e4SLinus Torvalds 	return error;
30521da177e4SLinus Torvalds }
30531da177e4SLinus Torvalds 
3054a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
30555590ff0dSUlrich Drepper {
30565590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
30575590ff0dSUlrich Drepper }
30585590ff0dSUlrich Drepper 
30591da177e4SLinus Torvalds /*
3060a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
3061c0d02594SJ. Bruce Fields  * should have a usage count of 1 if we're the only user of this
3062a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
3063a71905f0SSage Weil  * then we drop the dentry now.
30641da177e4SLinus Torvalds  *
30651da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
30661da177e4SLinus Torvalds  * do a
30671da177e4SLinus Torvalds  *
30681da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
30691da177e4SLinus Torvalds  *		return -EBUSY;
30701da177e4SLinus Torvalds  *
30711da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
30721da177e4SLinus Torvalds  * that is still in use by something else..
30731da177e4SLinus Torvalds  */
30741da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
30751da177e4SLinus Torvalds {
30761da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
30771da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
307864252c75SSage Weil 	if (dentry->d_count == 1)
30791da177e4SLinus Torvalds 		__d_drop(dentry);
30801da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
30811da177e4SLinus Torvalds }
30821da177e4SLinus Torvalds 
30831da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
30841da177e4SLinus Torvalds {
30851da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
30861da177e4SLinus Torvalds 
30871da177e4SLinus Torvalds 	if (error)
30881da177e4SLinus Torvalds 		return error;
30891da177e4SLinus Torvalds 
3090acfa4380SAl Viro 	if (!dir->i_op->rmdir)
30911da177e4SLinus Torvalds 		return -EPERM;
30921da177e4SLinus Torvalds 
30931d2ef590SAl Viro 	dget(dentry);
30941b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
3095912dbc15SSage Weil 
30961da177e4SLinus Torvalds 	error = -EBUSY;
3097912dbc15SSage Weil 	if (d_mountpoint(dentry))
3098912dbc15SSage Weil 		goto out;
3099912dbc15SSage Weil 
31001da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
3101912dbc15SSage Weil 	if (error)
3102912dbc15SSage Weil 		goto out;
3103912dbc15SSage Weil 
31043cebde24SSage Weil 	shrink_dcache_parent(dentry);
31051da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
3106912dbc15SSage Weil 	if (error)
3107912dbc15SSage Weil 		goto out;
3108912dbc15SSage Weil 
31091da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
3110d83c49f3SAl Viro 	dont_mount(dentry);
31111da177e4SLinus Torvalds 
3112912dbc15SSage Weil out:
3113912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
31141d2ef590SAl Viro 	dput(dentry);
3115912dbc15SSage Weil 	if (!error)
3116912dbc15SSage Weil 		d_delete(dentry);
31171da177e4SLinus Torvalds 	return error;
31181da177e4SLinus Torvalds }
31191da177e4SLinus Torvalds 
31205590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
31211da177e4SLinus Torvalds {
31221da177e4SLinus Torvalds 	int error = 0;
31231da177e4SLinus Torvalds 	char * name;
31241da177e4SLinus Torvalds 	struct dentry *dentry;
31251da177e4SLinus Torvalds 	struct nameidata nd;
31261da177e4SLinus Torvalds 
31272ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
31281da177e4SLinus Torvalds 	if (error)
31292ad94ae6SAl Viro 		return error;
31301da177e4SLinus Torvalds 
31311da177e4SLinus Torvalds 	switch(nd.last_type) {
31321da177e4SLinus Torvalds 	case LAST_DOTDOT:
31331da177e4SLinus Torvalds 		error = -ENOTEMPTY;
31341da177e4SLinus Torvalds 		goto exit1;
31351da177e4SLinus Torvalds 	case LAST_DOT:
31361da177e4SLinus Torvalds 		error = -EINVAL;
31371da177e4SLinus Torvalds 		goto exit1;
31381da177e4SLinus Torvalds 	case LAST_ROOT:
31391da177e4SLinus Torvalds 		error = -EBUSY;
31401da177e4SLinus Torvalds 		goto exit1;
31411da177e4SLinus Torvalds 	}
31420612d9fbSOGAWA Hirofumi 
31430612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
31440612d9fbSOGAWA Hirofumi 
31454ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
314649705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
31471da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
31486902d925SDave Hansen 	if (IS_ERR(dentry))
31496902d925SDave Hansen 		goto exit2;
3150e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
3151e6bc45d6STheodore Ts'o 		error = -ENOENT;
3152e6bc45d6STheodore Ts'o 		goto exit3;
3153e6bc45d6STheodore Ts'o 	}
31540622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
31550622753bSDave Hansen 	if (error)
31560622753bSDave Hansen 		goto exit3;
3157be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
3158be6d3e56SKentaro Takeda 	if (error)
3159be6d3e56SKentaro Takeda 		goto exit4;
31604ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
3161be6d3e56SKentaro Takeda exit4:
31620622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
31630622753bSDave Hansen exit3:
31641da177e4SLinus Torvalds 	dput(dentry);
31656902d925SDave Hansen exit2:
31664ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
31671da177e4SLinus Torvalds exit1:
31681d957f9bSJan Blunck 	path_put(&nd.path);
31691da177e4SLinus Torvalds 	putname(name);
31701da177e4SLinus Torvalds 	return error;
31711da177e4SLinus Torvalds }
31721da177e4SLinus Torvalds 
31733cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
31745590ff0dSUlrich Drepper {
31755590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
31765590ff0dSUlrich Drepper }
31775590ff0dSUlrich Drepper 
31781da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
31791da177e4SLinus Torvalds {
31801da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
31811da177e4SLinus Torvalds 
31821da177e4SLinus Torvalds 	if (error)
31831da177e4SLinus Torvalds 		return error;
31841da177e4SLinus Torvalds 
3185acfa4380SAl Viro 	if (!dir->i_op->unlink)
31861da177e4SLinus Torvalds 		return -EPERM;
31871da177e4SLinus Torvalds 
31881b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
31891da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
31901da177e4SLinus Torvalds 		error = -EBUSY;
31911da177e4SLinus Torvalds 	else {
31921da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
3193bec1052eSAl Viro 		if (!error) {
31941da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
3195bec1052eSAl Viro 			if (!error)
3196d83c49f3SAl Viro 				dont_mount(dentry);
3197bec1052eSAl Viro 		}
31981da177e4SLinus Torvalds 	}
31991b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
32001da177e4SLinus Torvalds 
32011da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
32021da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
3203ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
32041da177e4SLinus Torvalds 		d_delete(dentry);
32051da177e4SLinus Torvalds 	}
32060eeca283SRobert Love 
32071da177e4SLinus Torvalds 	return error;
32081da177e4SLinus Torvalds }
32091da177e4SLinus Torvalds 
32101da177e4SLinus Torvalds /*
32111da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
32121b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
32131da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
32141da177e4SLinus Torvalds  * while waiting on the I/O.
32151da177e4SLinus Torvalds  */
32165590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
32171da177e4SLinus Torvalds {
32182ad94ae6SAl Viro 	int error;
32191da177e4SLinus Torvalds 	char *name;
32201da177e4SLinus Torvalds 	struct dentry *dentry;
32211da177e4SLinus Torvalds 	struct nameidata nd;
32221da177e4SLinus Torvalds 	struct inode *inode = NULL;
32231da177e4SLinus Torvalds 
32242ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
32251da177e4SLinus Torvalds 	if (error)
32262ad94ae6SAl Viro 		return error;
32272ad94ae6SAl Viro 
32281da177e4SLinus Torvalds 	error = -EISDIR;
32291da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
32301da177e4SLinus Torvalds 		goto exit1;
32310612d9fbSOGAWA Hirofumi 
32320612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
32330612d9fbSOGAWA Hirofumi 
32344ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
323549705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
32361da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
32371da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
32381da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
323950338b88STörök Edwin 		if (nd.last.name[nd.last.len])
324050338b88STörök Edwin 			goto slashes;
32411da177e4SLinus Torvalds 		inode = dentry->d_inode;
324250338b88STörök Edwin 		if (!inode)
3243e6bc45d6STheodore Ts'o 			goto slashes;
32447de9c6eeSAl Viro 		ihold(inode);
32450622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
32460622753bSDave Hansen 		if (error)
32470622753bSDave Hansen 			goto exit2;
3248be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
3249be6d3e56SKentaro Takeda 		if (error)
3250be6d3e56SKentaro Takeda 			goto exit3;
32514ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
3252be6d3e56SKentaro Takeda exit3:
32530622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
32541da177e4SLinus Torvalds 	exit2:
32551da177e4SLinus Torvalds 		dput(dentry);
32561da177e4SLinus Torvalds 	}
32574ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
32581da177e4SLinus Torvalds 	if (inode)
32591da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
32601da177e4SLinus Torvalds exit1:
32611d957f9bSJan Blunck 	path_put(&nd.path);
32621da177e4SLinus Torvalds 	putname(name);
32631da177e4SLinus Torvalds 	return error;
32641da177e4SLinus Torvalds 
32651da177e4SLinus Torvalds slashes:
32661da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
32671da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
32681da177e4SLinus Torvalds 	goto exit2;
32691da177e4SLinus Torvalds }
32701da177e4SLinus Torvalds 
32712e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
32725590ff0dSUlrich Drepper {
32735590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
32745590ff0dSUlrich Drepper 		return -EINVAL;
32755590ff0dSUlrich Drepper 
32765590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
32775590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
32785590ff0dSUlrich Drepper 
32795590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
32805590ff0dSUlrich Drepper }
32815590ff0dSUlrich Drepper 
32823480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
32835590ff0dSUlrich Drepper {
32845590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
32855590ff0dSUlrich Drepper }
32865590ff0dSUlrich Drepper 
3287db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
32881da177e4SLinus Torvalds {
3289a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
32901da177e4SLinus Torvalds 
32911da177e4SLinus Torvalds 	if (error)
32921da177e4SLinus Torvalds 		return error;
32931da177e4SLinus Torvalds 
3294acfa4380SAl Viro 	if (!dir->i_op->symlink)
32951da177e4SLinus Torvalds 		return -EPERM;
32961da177e4SLinus Torvalds 
32971da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
32981da177e4SLinus Torvalds 	if (error)
32991da177e4SLinus Torvalds 		return error;
33001da177e4SLinus Torvalds 
33011da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
3302a74574aaSStephen Smalley 	if (!error)
3303f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
33041da177e4SLinus Torvalds 	return error;
33051da177e4SLinus Torvalds }
33061da177e4SLinus Torvalds 
33072e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
33082e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
33091da177e4SLinus Torvalds {
33102ad94ae6SAl Viro 	int error;
33111da177e4SLinus Torvalds 	char *from;
33126902d925SDave Hansen 	struct dentry *dentry;
3313dae6ad8fSAl Viro 	struct path path;
33141da177e4SLinus Torvalds 
33151da177e4SLinus Torvalds 	from = getname(oldname);
33161da177e4SLinus Torvalds 	if (IS_ERR(from))
33171da177e4SLinus Torvalds 		return PTR_ERR(from);
33182ad94ae6SAl Viro 
3319dae6ad8fSAl Viro 	dentry = user_path_create(newdfd, newname, &path, 0);
33201da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
33216902d925SDave Hansen 	if (IS_ERR(dentry))
3322dae6ad8fSAl Viro 		goto out_putname;
33236902d925SDave Hansen 
3324dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
332575c3f29dSDave Hansen 	if (error)
332675c3f29dSDave Hansen 		goto out_dput;
3327dae6ad8fSAl Viro 	error = security_path_symlink(&path, dentry, from);
3328be6d3e56SKentaro Takeda 	if (error)
3329be6d3e56SKentaro Takeda 		goto out_drop_write;
3330dae6ad8fSAl Viro 	error = vfs_symlink(path.dentry->d_inode, dentry, from);
3331be6d3e56SKentaro Takeda out_drop_write:
3332dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
333375c3f29dSDave Hansen out_dput:
33341da177e4SLinus Torvalds 	dput(dentry);
3335dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
3336dae6ad8fSAl Viro 	path_put(&path);
33376902d925SDave Hansen out_putname:
33381da177e4SLinus Torvalds 	putname(from);
33391da177e4SLinus Torvalds 	return error;
33401da177e4SLinus Torvalds }
33411da177e4SLinus Torvalds 
33423480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
33435590ff0dSUlrich Drepper {
33445590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
33455590ff0dSUlrich Drepper }
33465590ff0dSUlrich Drepper 
33471da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
33481da177e4SLinus Torvalds {
33491da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
33508de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
33511da177e4SLinus Torvalds 	int error;
33521da177e4SLinus Torvalds 
33531da177e4SLinus Torvalds 	if (!inode)
33541da177e4SLinus Torvalds 		return -ENOENT;
33551da177e4SLinus Torvalds 
3356a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
33571da177e4SLinus Torvalds 	if (error)
33581da177e4SLinus Torvalds 		return error;
33591da177e4SLinus Torvalds 
33601da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
33611da177e4SLinus Torvalds 		return -EXDEV;
33621da177e4SLinus Torvalds 
33631da177e4SLinus Torvalds 	/*
33641da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
33651da177e4SLinus Torvalds 	 */
33661da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
33671da177e4SLinus Torvalds 		return -EPERM;
3368acfa4380SAl Viro 	if (!dir->i_op->link)
33691da177e4SLinus Torvalds 		return -EPERM;
33707e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
33711da177e4SLinus Torvalds 		return -EPERM;
33721da177e4SLinus Torvalds 
33731da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
33741da177e4SLinus Torvalds 	if (error)
33751da177e4SLinus Torvalds 		return error;
33761da177e4SLinus Torvalds 
33777e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
3378aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
3379aae8a97dSAneesh Kumar K.V 	if (inode->i_nlink == 0)
3380aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
33818de52778SAl Viro 	else if (max_links && inode->i_nlink >= max_links)
33828de52778SAl Viro 		error = -EMLINK;
3383aae8a97dSAneesh Kumar K.V 	else
33841da177e4SLinus Torvalds 		error = dir->i_op->link(old_dentry, dir, new_dentry);
33857e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
3386e31e14ecSStephen Smalley 	if (!error)
33877e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
33881da177e4SLinus Torvalds 	return error;
33891da177e4SLinus Torvalds }
33901da177e4SLinus Torvalds 
33911da177e4SLinus Torvalds /*
33921da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
33931da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
33941da177e4SLinus Torvalds  * newname.  --KAB
33951da177e4SLinus Torvalds  *
33961da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
33971da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
33981da177e4SLinus Torvalds  * and other special files.  --ADM
33991da177e4SLinus Torvalds  */
34002e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
34012e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
34021da177e4SLinus Torvalds {
34031da177e4SLinus Torvalds 	struct dentry *new_dentry;
3404dae6ad8fSAl Viro 	struct path old_path, new_path;
340511a7b371SAneesh Kumar K.V 	int how = 0;
34061da177e4SLinus Torvalds 	int error;
34071da177e4SLinus Torvalds 
340811a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3409c04030e1SUlrich Drepper 		return -EINVAL;
341011a7b371SAneesh Kumar K.V 	/*
341111a7b371SAneesh Kumar K.V 	 * To use null names we require CAP_DAC_READ_SEARCH
341211a7b371SAneesh Kumar K.V 	 * This ensures that not everyone will be able to create
341311a7b371SAneesh Kumar K.V 	 * handlink using the passed filedescriptor.
341411a7b371SAneesh Kumar K.V 	 */
341511a7b371SAneesh Kumar K.V 	if (flags & AT_EMPTY_PATH) {
341611a7b371SAneesh Kumar K.V 		if (!capable(CAP_DAC_READ_SEARCH))
341711a7b371SAneesh Kumar K.V 			return -ENOENT;
341811a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
341911a7b371SAneesh Kumar K.V 	}
3420c04030e1SUlrich Drepper 
342111a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
342211a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
342311a7b371SAneesh Kumar K.V 
342411a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
34251da177e4SLinus Torvalds 	if (error)
34262ad94ae6SAl Viro 		return error;
34272ad94ae6SAl Viro 
3428dae6ad8fSAl Viro 	new_dentry = user_path_create(newdfd, newname, &new_path, 0);
34291da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
34306902d925SDave Hansen 	if (IS_ERR(new_dentry))
3431dae6ad8fSAl Viro 		goto out;
3432dae6ad8fSAl Viro 
3433dae6ad8fSAl Viro 	error = -EXDEV;
3434dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt)
3435dae6ad8fSAl Viro 		goto out_dput;
3436dae6ad8fSAl Viro 	error = mnt_want_write(new_path.mnt);
343775c3f29dSDave Hansen 	if (error)
343875c3f29dSDave Hansen 		goto out_dput;
3439dae6ad8fSAl Viro 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
3440be6d3e56SKentaro Takeda 	if (error)
3441be6d3e56SKentaro Takeda 		goto out_drop_write;
3442dae6ad8fSAl Viro 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
3443be6d3e56SKentaro Takeda out_drop_write:
3444dae6ad8fSAl Viro 	mnt_drop_write(new_path.mnt);
344575c3f29dSDave Hansen out_dput:
34461da177e4SLinus Torvalds 	dput(new_dentry);
3447dae6ad8fSAl Viro 	mutex_unlock(&new_path.dentry->d_inode->i_mutex);
3448dae6ad8fSAl Viro 	path_put(&new_path);
34491da177e4SLinus Torvalds out:
34502d8f3038SAl Viro 	path_put(&old_path);
34511da177e4SLinus Torvalds 
34521da177e4SLinus Torvalds 	return error;
34531da177e4SLinus Torvalds }
34541da177e4SLinus Torvalds 
34553480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
34565590ff0dSUlrich Drepper {
3457c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
34585590ff0dSUlrich Drepper }
34595590ff0dSUlrich Drepper 
34601da177e4SLinus Torvalds /*
34611da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
34621da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
34631da177e4SLinus Torvalds  * Problems:
34641da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
34651da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
34661da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
3467a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
34681da177e4SLinus Torvalds  *	   story.
34691da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
34701b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
34711da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
34721da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
3473a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
34741da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
34751da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
34761da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
3477a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
34781da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
34791da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
34801da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
3481e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
34821b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
34831da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3484c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
34851da177e4SLinus Torvalds  *	   locking].
34861da177e4SLinus Torvalds  */
348775c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
34881da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
34891da177e4SLinus Torvalds {
34901da177e4SLinus Torvalds 	int error = 0;
34919055cba7SSage Weil 	struct inode *target = new_dentry->d_inode;
34928de52778SAl Viro 	unsigned max_links = new_dir->i_sb->s_max_links;
34931da177e4SLinus Torvalds 
34941da177e4SLinus Torvalds 	/*
34951da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
34961da177e4SLinus Torvalds 	 * we'll need to flip '..'.
34971da177e4SLinus Torvalds 	 */
34981da177e4SLinus Torvalds 	if (new_dir != old_dir) {
3499f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
35001da177e4SLinus Torvalds 		if (error)
35011da177e4SLinus Torvalds 			return error;
35021da177e4SLinus Torvalds 	}
35031da177e4SLinus Torvalds 
35041da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
35051da177e4SLinus Torvalds 	if (error)
35061da177e4SLinus Torvalds 		return error;
35071da177e4SLinus Torvalds 
35081d2ef590SAl Viro 	dget(new_dentry);
3509d83c49f3SAl Viro 	if (target)
35101b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
35119055cba7SSage Weil 
35121da177e4SLinus Torvalds 	error = -EBUSY;
35139055cba7SSage Weil 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
35149055cba7SSage Weil 		goto out;
35159055cba7SSage Weil 
35168de52778SAl Viro 	error = -EMLINK;
35178de52778SAl Viro 	if (max_links && !target && new_dir != old_dir &&
35188de52778SAl Viro 	    new_dir->i_nlink >= max_links)
35198de52778SAl Viro 		goto out;
35208de52778SAl Viro 
35213cebde24SSage Weil 	if (target)
35223cebde24SSage Weil 		shrink_dcache_parent(new_dentry);
35231da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
35249055cba7SSage Weil 	if (error)
35259055cba7SSage Weil 		goto out;
35269055cba7SSage Weil 
35271da177e4SLinus Torvalds 	if (target) {
35281da177e4SLinus Torvalds 		target->i_flags |= S_DEAD;
3529d83c49f3SAl Viro 		dont_mount(new_dentry);
3530d83c49f3SAl Viro 	}
35319055cba7SSage Weil out:
35329055cba7SSage Weil 	if (target)
35331b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
35341d2ef590SAl Viro 	dput(new_dentry);
3535e31e14ecSStephen Smalley 	if (!error)
3536349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
35371da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
35381da177e4SLinus Torvalds 	return error;
35391da177e4SLinus Torvalds }
35401da177e4SLinus Torvalds 
354175c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
35421da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
35431da177e4SLinus Torvalds {
354451892bbbSSage Weil 	struct inode *target = new_dentry->d_inode;
35451da177e4SLinus Torvalds 	int error;
35461da177e4SLinus Torvalds 
35471da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
35481da177e4SLinus Torvalds 	if (error)
35491da177e4SLinus Torvalds 		return error;
35501da177e4SLinus Torvalds 
35511da177e4SLinus Torvalds 	dget(new_dentry);
35521da177e4SLinus Torvalds 	if (target)
35531b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
355451892bbbSSage Weil 
35551da177e4SLinus Torvalds 	error = -EBUSY;
355651892bbbSSage Weil 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
355751892bbbSSage Weil 		goto out;
355851892bbbSSage Weil 
35591da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
356051892bbbSSage Weil 	if (error)
356151892bbbSSage Weil 		goto out;
356251892bbbSSage Weil 
3563bec1052eSAl Viro 	if (target)
3564d83c49f3SAl Viro 		dont_mount(new_dentry);
3565349457ccSMark Fasheh 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
35661da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
356751892bbbSSage Weil out:
35681da177e4SLinus Torvalds 	if (target)
35691b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
35701da177e4SLinus Torvalds 	dput(new_dentry);
35711da177e4SLinus Torvalds 	return error;
35721da177e4SLinus Torvalds }
35731da177e4SLinus Torvalds 
35741da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
35751da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
35761da177e4SLinus Torvalds {
35771da177e4SLinus Torvalds 	int error;
35781da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
357959b0df21SEric Paris 	const unsigned char *old_name;
35801da177e4SLinus Torvalds 
35811da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
35821da177e4SLinus Torvalds  		return 0;
35831da177e4SLinus Torvalds 
35841da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
35851da177e4SLinus Torvalds 	if (error)
35861da177e4SLinus Torvalds 		return error;
35871da177e4SLinus Torvalds 
35881da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3589a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
35901da177e4SLinus Torvalds 	else
35911da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
35921da177e4SLinus Torvalds 	if (error)
35931da177e4SLinus Torvalds 		return error;
35941da177e4SLinus Torvalds 
3595acfa4380SAl Viro 	if (!old_dir->i_op->rename)
35961da177e4SLinus Torvalds 		return -EPERM;
35971da177e4SLinus Torvalds 
35980eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
35990eeca283SRobert Love 
36001da177e4SLinus Torvalds 	if (is_dir)
36011da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
36021da177e4SLinus Torvalds 	else
36031da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3604123df294SAl Viro 	if (!error)
3605123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
36065a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
36070eeca283SRobert Love 	fsnotify_oldname_free(old_name);
36080eeca283SRobert Love 
36091da177e4SLinus Torvalds 	return error;
36101da177e4SLinus Torvalds }
36111da177e4SLinus Torvalds 
36122e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
36132e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
36141da177e4SLinus Torvalds {
36151da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
36161da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
36171da177e4SLinus Torvalds 	struct dentry *trap;
36181da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
36192ad94ae6SAl Viro 	char *from;
36202ad94ae6SAl Viro 	char *to;
36212ad94ae6SAl Viro 	int error;
36221da177e4SLinus Torvalds 
36232ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
36241da177e4SLinus Torvalds 	if (error)
36251da177e4SLinus Torvalds 		goto exit;
36261da177e4SLinus Torvalds 
36272ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
36281da177e4SLinus Torvalds 	if (error)
36291da177e4SLinus Torvalds 		goto exit1;
36301da177e4SLinus Torvalds 
36311da177e4SLinus Torvalds 	error = -EXDEV;
36324ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
36331da177e4SLinus Torvalds 		goto exit2;
36341da177e4SLinus Torvalds 
36354ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
36361da177e4SLinus Torvalds 	error = -EBUSY;
36371da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
36381da177e4SLinus Torvalds 		goto exit2;
36391da177e4SLinus Torvalds 
36404ac91378SJan Blunck 	new_dir = newnd.path.dentry;
36411da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
36421da177e4SLinus Torvalds 		goto exit2;
36431da177e4SLinus Torvalds 
36440612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
36450612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
36464e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
36470612d9fbSOGAWA Hirofumi 
36481da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
36491da177e4SLinus Torvalds 
365049705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
36511da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
36521da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
36531da177e4SLinus Torvalds 		goto exit3;
36541da177e4SLinus Torvalds 	/* source must exist */
36551da177e4SLinus Torvalds 	error = -ENOENT;
36561da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
36571da177e4SLinus Torvalds 		goto exit4;
36581da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
36591da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
36601da177e4SLinus Torvalds 		error = -ENOTDIR;
36611da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
36621da177e4SLinus Torvalds 			goto exit4;
36631da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
36641da177e4SLinus Torvalds 			goto exit4;
36651da177e4SLinus Torvalds 	}
36661da177e4SLinus Torvalds 	/* source should not be ancestor of target */
36671da177e4SLinus Torvalds 	error = -EINVAL;
36681da177e4SLinus Torvalds 	if (old_dentry == trap)
36691da177e4SLinus Torvalds 		goto exit4;
367049705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
36711da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
36721da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
36731da177e4SLinus Torvalds 		goto exit4;
36741da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
36751da177e4SLinus Torvalds 	error = -ENOTEMPTY;
36761da177e4SLinus Torvalds 	if (new_dentry == trap)
36771da177e4SLinus Torvalds 		goto exit5;
36781da177e4SLinus Torvalds 
36799079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
36809079b1ebSDave Hansen 	if (error)
36819079b1ebSDave Hansen 		goto exit5;
3682be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3683be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3684be6d3e56SKentaro Takeda 	if (error)
3685be6d3e56SKentaro Takeda 		goto exit6;
36861da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
36871da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3688be6d3e56SKentaro Takeda exit6:
36899079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
36901da177e4SLinus Torvalds exit5:
36911da177e4SLinus Torvalds 	dput(new_dentry);
36921da177e4SLinus Torvalds exit4:
36931da177e4SLinus Torvalds 	dput(old_dentry);
36941da177e4SLinus Torvalds exit3:
36951da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
36961da177e4SLinus Torvalds exit2:
36971d957f9bSJan Blunck 	path_put(&newnd.path);
36982ad94ae6SAl Viro 	putname(to);
36991da177e4SLinus Torvalds exit1:
37001d957f9bSJan Blunck 	path_put(&oldnd.path);
37011da177e4SLinus Torvalds 	putname(from);
37022ad94ae6SAl Viro exit:
37031da177e4SLinus Torvalds 	return error;
37041da177e4SLinus Torvalds }
37051da177e4SLinus Torvalds 
3706a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
37075590ff0dSUlrich Drepper {
37085590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
37095590ff0dSUlrich Drepper }
37105590ff0dSUlrich Drepper 
37111da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
37121da177e4SLinus Torvalds {
37131da177e4SLinus Torvalds 	int len;
37141da177e4SLinus Torvalds 
37151da177e4SLinus Torvalds 	len = PTR_ERR(link);
37161da177e4SLinus Torvalds 	if (IS_ERR(link))
37171da177e4SLinus Torvalds 		goto out;
37181da177e4SLinus Torvalds 
37191da177e4SLinus Torvalds 	len = strlen(link);
37201da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
37211da177e4SLinus Torvalds 		len = buflen;
37221da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
37231da177e4SLinus Torvalds 		len = -EFAULT;
37241da177e4SLinus Torvalds out:
37251da177e4SLinus Torvalds 	return len;
37261da177e4SLinus Torvalds }
37271da177e4SLinus Torvalds 
37281da177e4SLinus Torvalds /*
37291da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
37301da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
37311da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
37321da177e4SLinus Torvalds  */
37331da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
37341da177e4SLinus Torvalds {
37351da177e4SLinus Torvalds 	struct nameidata nd;
3736cc314eefSLinus Torvalds 	void *cookie;
3737694a1764SMarcin Slusarz 	int res;
3738cc314eefSLinus Torvalds 
37391da177e4SLinus Torvalds 	nd.depth = 0;
3740cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3741694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3742694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3743694a1764SMarcin Slusarz 
3744694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
37451da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3746cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3747694a1764SMarcin Slusarz 	return res;
37481da177e4SLinus Torvalds }
37491da177e4SLinus Torvalds 
37501da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
37511da177e4SLinus Torvalds {
37521da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
37531da177e4SLinus Torvalds }
37541da177e4SLinus Torvalds 
37551da177e4SLinus Torvalds /* get the link contents into pagecache */
37561da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
37571da177e4SLinus Torvalds {
3758ebd09abbSDuane Griffin 	char *kaddr;
37591da177e4SLinus Torvalds 	struct page *page;
37601da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3761090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
37621da177e4SLinus Torvalds 	if (IS_ERR(page))
37636fe6900eSNick Piggin 		return (char*)page;
37641da177e4SLinus Torvalds 	*ppage = page;
3765ebd09abbSDuane Griffin 	kaddr = kmap(page);
3766ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3767ebd09abbSDuane Griffin 	return kaddr;
37681da177e4SLinus Torvalds }
37691da177e4SLinus Torvalds 
37701da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
37711da177e4SLinus Torvalds {
37721da177e4SLinus Torvalds 	struct page *page = NULL;
37731da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
37741da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
37751da177e4SLinus Torvalds 	if (page) {
37761da177e4SLinus Torvalds 		kunmap(page);
37771da177e4SLinus Torvalds 		page_cache_release(page);
37781da177e4SLinus Torvalds 	}
37791da177e4SLinus Torvalds 	return res;
37801da177e4SLinus Torvalds }
37811da177e4SLinus Torvalds 
3782cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
37831da177e4SLinus Torvalds {
3784cc314eefSLinus Torvalds 	struct page *page = NULL;
37851da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3786cc314eefSLinus Torvalds 	return page;
37871da177e4SLinus Torvalds }
37881da177e4SLinus Torvalds 
3789cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
37901da177e4SLinus Torvalds {
3791cc314eefSLinus Torvalds 	struct page *page = cookie;
3792cc314eefSLinus Torvalds 
3793cc314eefSLinus Torvalds 	if (page) {
37941da177e4SLinus Torvalds 		kunmap(page);
37951da177e4SLinus Torvalds 		page_cache_release(page);
37961da177e4SLinus Torvalds 	}
37971da177e4SLinus Torvalds }
37981da177e4SLinus Torvalds 
379954566b2cSNick Piggin /*
380054566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
380154566b2cSNick Piggin  */
380254566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
38031da177e4SLinus Torvalds {
38041da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
38050adb25d2SKirill Korotaev 	struct page *page;
3806afddba49SNick Piggin 	void *fsdata;
3807beb497abSDmitriy Monakhov 	int err;
38081da177e4SLinus Torvalds 	char *kaddr;
380954566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
381054566b2cSNick Piggin 	if (nofs)
381154566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
38121da177e4SLinus Torvalds 
38137e53cac4SNeilBrown retry:
3814afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
381554566b2cSNick Piggin 				flags, &page, &fsdata);
38161da177e4SLinus Torvalds 	if (err)
3817afddba49SNick Piggin 		goto fail;
3818afddba49SNick Piggin 
3819e8e3c3d6SCong Wang 	kaddr = kmap_atomic(page);
38201da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
3821e8e3c3d6SCong Wang 	kunmap_atomic(kaddr);
3822afddba49SNick Piggin 
3823afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3824afddba49SNick Piggin 							page, fsdata);
38251da177e4SLinus Torvalds 	if (err < 0)
38261da177e4SLinus Torvalds 		goto fail;
3827afddba49SNick Piggin 	if (err < len-1)
3828afddba49SNick Piggin 		goto retry;
3829afddba49SNick Piggin 
38301da177e4SLinus Torvalds 	mark_inode_dirty(inode);
38311da177e4SLinus Torvalds 	return 0;
38321da177e4SLinus Torvalds fail:
38331da177e4SLinus Torvalds 	return err;
38341da177e4SLinus Torvalds }
38351da177e4SLinus Torvalds 
38360adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
38370adb25d2SKirill Korotaev {
38380adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
383954566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
38400adb25d2SKirill Korotaev }
38410adb25d2SKirill Korotaev 
384292e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
38431da177e4SLinus Torvalds 	.readlink	= generic_readlink,
38441da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
38451da177e4SLinus Torvalds 	.put_link	= page_put_link,
38461da177e4SLinus Torvalds };
38471da177e4SLinus Torvalds 
38482d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
3849cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
38501da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
38511da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
38521da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
38531da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
38541da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
38551da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
38561da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
38571da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
38581da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
38590adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
38601da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
38611da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
3862d1811465SAl Viro EXPORT_SYMBOL(kern_path);
386316f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3864f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
38651da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
38661da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
38671da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
38681da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
38691da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
38701da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
38711da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
38721da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
38731da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
38741da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
38751da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
38761da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
38771da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
38781da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3879