xref: /openbmc/linux/fs/namei.c (revision 921a1650)
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 
43032a7991bSAl Viro static inline void lock_rcu_walk(void)
43132a7991bSAl Viro {
43232a7991bSAl Viro 	br_read_lock(&vfsmount_lock);
43332a7991bSAl Viro 	rcu_read_lock();
43432a7991bSAl Viro }
43532a7991bSAl Viro 
43632a7991bSAl Viro static inline void unlock_rcu_walk(void)
43732a7991bSAl Viro {
43832a7991bSAl Viro 	rcu_read_unlock();
43932a7991bSAl Viro 	br_read_unlock(&vfsmount_lock);
44032a7991bSAl Viro }
44132a7991bSAl Viro 
44231e6b01fSNick Piggin /**
44319660af7SAl Viro  * unlazy_walk - try to switch to ref-walk mode.
44419660af7SAl Viro  * @nd: nameidata pathwalk data
44519660af7SAl Viro  * @dentry: child of nd->path.dentry or NULL
44639191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
44731e6b01fSNick Piggin  *
44819660af7SAl Viro  * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
44919660af7SAl Viro  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
45019660af7SAl Viro  * @nd or NULL.  Must be called from rcu-walk context.
45131e6b01fSNick Piggin  */
45219660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
45331e6b01fSNick Piggin {
45431e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
45531e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
4565b6ca027SAl Viro 	int want_root = 0;
45731e6b01fSNick Piggin 
45831e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
4595b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
4605b6ca027SAl Viro 		want_root = 1;
46131e6b01fSNick Piggin 		spin_lock(&fs->lock);
46231e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
46331e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
46431e6b01fSNick Piggin 			goto err_root;
46531e6b01fSNick Piggin 	}
46631e6b01fSNick Piggin 	spin_lock(&parent->d_lock);
46719660af7SAl Viro 	if (!dentry) {
46819660af7SAl Viro 		if (!__d_rcu_to_refcount(parent, nd->seq))
46919660af7SAl Viro 			goto err_parent;
47019660af7SAl Viro 		BUG_ON(nd->inode != parent->d_inode);
47119660af7SAl Viro 	} else {
47294c0d4ecSAl Viro 		if (dentry->d_parent != parent)
47394c0d4ecSAl Viro 			goto err_parent;
47431e6b01fSNick Piggin 		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
47531e6b01fSNick Piggin 		if (!__d_rcu_to_refcount(dentry, nd->seq))
47619660af7SAl Viro 			goto err_child;
47731e6b01fSNick Piggin 		/*
47819660af7SAl Viro 		 * If the sequence check on the child dentry passed, then
47919660af7SAl Viro 		 * the child has not been removed from its parent. This
48019660af7SAl Viro 		 * means the parent dentry must be valid and able to take
48119660af7SAl Viro 		 * a reference at this point.
48231e6b01fSNick Piggin 		 */
48331e6b01fSNick Piggin 		BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
48431e6b01fSNick Piggin 		BUG_ON(!parent->d_count);
48531e6b01fSNick Piggin 		parent->d_count++;
48631e6b01fSNick Piggin 		spin_unlock(&dentry->d_lock);
48719660af7SAl Viro 	}
48831e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
4895b6ca027SAl Viro 	if (want_root) {
49031e6b01fSNick Piggin 		path_get(&nd->root);
49131e6b01fSNick Piggin 		spin_unlock(&fs->lock);
49231e6b01fSNick Piggin 	}
49331e6b01fSNick Piggin 	mntget(nd->path.mnt);
49431e6b01fSNick Piggin 
49532a7991bSAl Viro 	unlock_rcu_walk();
49631e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
49731e6b01fSNick Piggin 	return 0;
49819660af7SAl Viro 
49919660af7SAl Viro err_child:
50031e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
50119660af7SAl Viro err_parent:
50231e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
50331e6b01fSNick Piggin err_root:
5045b6ca027SAl Viro 	if (want_root)
50531e6b01fSNick Piggin 		spin_unlock(&fs->lock);
50631e6b01fSNick Piggin 	return -ECHILD;
50731e6b01fSNick Piggin }
50831e6b01fSNick Piggin 
5094ce16ef3SAl Viro static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
51034286d66SNick Piggin {
5114ce16ef3SAl Viro 	return dentry->d_op->d_revalidate(dentry, flags);
51234286d66SNick Piggin }
51334286d66SNick Piggin 
5149f1fafeeSAl Viro /**
5159f1fafeeSAl Viro  * complete_walk - successful completion of path walk
5169f1fafeeSAl Viro  * @nd:  pointer nameidata
51739159de2SJeff Layton  *
5189f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
5199f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
5209f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
5219f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
5229f1fafeeSAl Viro  * need to drop nd->path.
52339159de2SJeff Layton  */
5249f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
52539159de2SJeff Layton {
52616c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
52739159de2SJeff Layton 	int status;
52839159de2SJeff Layton 
5299f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
5309f1fafeeSAl Viro 		nd->flags &= ~LOOKUP_RCU;
5319f1fafeeSAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
5329f1fafeeSAl Viro 			nd->root.mnt = NULL;
5339f1fafeeSAl Viro 		spin_lock(&dentry->d_lock);
5349f1fafeeSAl Viro 		if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
5359f1fafeeSAl Viro 			spin_unlock(&dentry->d_lock);
53632a7991bSAl Viro 			unlock_rcu_walk();
5379f1fafeeSAl Viro 			return -ECHILD;
5389f1fafeeSAl Viro 		}
5399f1fafeeSAl Viro 		BUG_ON(nd->inode != dentry->d_inode);
5409f1fafeeSAl Viro 		spin_unlock(&dentry->d_lock);
5419f1fafeeSAl Viro 		mntget(nd->path.mnt);
54232a7991bSAl Viro 		unlock_rcu_walk();
5439f1fafeeSAl Viro 	}
5449f1fafeeSAl Viro 
54516c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
54639159de2SJeff Layton 		return 0;
54739159de2SJeff Layton 
54816c2cd71SAl Viro 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
54916c2cd71SAl Viro 		return 0;
55016c2cd71SAl Viro 
55116c2cd71SAl Viro 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
55216c2cd71SAl Viro 		return 0;
55316c2cd71SAl Viro 
55416c2cd71SAl Viro 	/* Note: we do not d_invalidate() */
5554ce16ef3SAl Viro 	status = d_revalidate(dentry, nd->flags);
55639159de2SJeff Layton 	if (status > 0)
55739159de2SJeff Layton 		return 0;
55839159de2SJeff Layton 
55916c2cd71SAl Viro 	if (!status)
56039159de2SJeff Layton 		status = -ESTALE;
56116c2cd71SAl Viro 
5629f1fafeeSAl Viro 	path_put(&nd->path);
56339159de2SJeff Layton 	return status;
56439159de2SJeff Layton }
56539159de2SJeff Layton 
5662a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
5672a737871SAl Viro {
568f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
569f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
5702a737871SAl Viro }
5712a737871SAl Viro 
5726de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
5736de88d72SAl Viro 
57431e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
57531e6b01fSNick Piggin {
57631e6b01fSNick Piggin 	if (!nd->root.mnt) {
57731e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
578c28cc364SNick Piggin 		unsigned seq;
579c28cc364SNick Piggin 
580c28cc364SNick Piggin 		do {
581c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
58231e6b01fSNick Piggin 			nd->root = fs->root;
583c1530019STim Chen 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
584c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
58531e6b01fSNick Piggin 	}
58631e6b01fSNick Piggin }
58731e6b01fSNick Piggin 
588f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
5891da177e4SLinus Torvalds {
59031e6b01fSNick Piggin 	int ret;
59131e6b01fSNick Piggin 
5921da177e4SLinus Torvalds 	if (IS_ERR(link))
5931da177e4SLinus Torvalds 		goto fail;
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 	if (*link == '/') {
5962a737871SAl Viro 		set_root(nd);
5971d957f9bSJan Blunck 		path_put(&nd->path);
5982a737871SAl Viro 		nd->path = nd->root;
5992a737871SAl Viro 		path_get(&nd->root);
60016c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
6011da177e4SLinus Torvalds 	}
60231e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
603b4091d5fSChristoph Hellwig 
60431e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
60531e6b01fSNick Piggin 	return ret;
6061da177e4SLinus Torvalds fail:
6071d957f9bSJan Blunck 	path_put(&nd->path);
6081da177e4SLinus Torvalds 	return PTR_ERR(link);
6091da177e4SLinus Torvalds }
6101da177e4SLinus Torvalds 
6111d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
612051d3812SIan Kent {
613051d3812SIan Kent 	dput(path->dentry);
6144ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
615051d3812SIan Kent 		mntput(path->mnt);
616051d3812SIan Kent }
617051d3812SIan Kent 
6187b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
6197b9337aaSNick Piggin 					struct nameidata *nd)
620051d3812SIan Kent {
62131e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
6224ac91378SJan Blunck 		dput(nd->path.dentry);
62331e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
6244ac91378SJan Blunck 			mntput(nd->path.mnt);
6259a229683SHuang Shijie 	}
62631e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
6274ac91378SJan Blunck 	nd->path.dentry = path->dentry;
628051d3812SIan Kent }
629051d3812SIan Kent 
630b5fb63c1SChristoph Hellwig /*
631b5fb63c1SChristoph Hellwig  * Helper to directly jump to a known parsed path from ->follow_link,
632b5fb63c1SChristoph Hellwig  * caller must have taken a reference to path beforehand.
633b5fb63c1SChristoph Hellwig  */
634b5fb63c1SChristoph Hellwig void nd_jump_link(struct nameidata *nd, struct path *path)
635b5fb63c1SChristoph Hellwig {
636b5fb63c1SChristoph Hellwig 	path_put(&nd->path);
637b5fb63c1SChristoph Hellwig 
638b5fb63c1SChristoph Hellwig 	nd->path = *path;
639b5fb63c1SChristoph Hellwig 	nd->inode = nd->path.dentry->d_inode;
640b5fb63c1SChristoph Hellwig 	nd->flags |= LOOKUP_JUMPED;
641b5fb63c1SChristoph Hellwig 
642b5fb63c1SChristoph Hellwig 	BUG_ON(nd->inode->i_op->follow_link);
643b5fb63c1SChristoph Hellwig }
644b5fb63c1SChristoph Hellwig 
645574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
646574197e0SAl Viro {
647574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
6486d7b5aaeSAl Viro 	if (inode->i_op->put_link)
649574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
650574197e0SAl Viro 	path_put(link);
651574197e0SAl Viro }
652574197e0SAl Viro 
653def4af30SAl Viro static __always_inline int
654574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
6551da177e4SLinus Torvalds {
6567b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
6576d7b5aaeSAl Viro 	int error;
6586d7b5aaeSAl Viro 	char *s;
6591da177e4SLinus Torvalds 
660844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
661844a3917SAl Viro 
6620e794589SAl Viro 	if (link->mnt == nd->path.mnt)
6630e794589SAl Viro 		mntget(link->mnt);
6640e794589SAl Viro 
6656d7b5aaeSAl Viro 	error = -ELOOP;
6666d7b5aaeSAl Viro 	if (unlikely(current->total_link_count >= 40))
6676d7b5aaeSAl Viro 		goto out_put_nd_path;
6686d7b5aaeSAl Viro 
669574197e0SAl Viro 	cond_resched();
670574197e0SAl Viro 	current->total_link_count++;
671574197e0SAl Viro 
67268ac1234SAl Viro 	touch_atime(link);
6731da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
674cd4e91d3SAl Viro 
67536f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
6766d7b5aaeSAl Viro 	if (error)
6776d7b5aaeSAl Viro 		goto out_put_nd_path;
67836f3b4f6SAl Viro 
67986acdca1SAl Viro 	nd->last_type = LAST_BIND;
680def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
681def4af30SAl Viro 	error = PTR_ERR(*p);
6826d7b5aaeSAl Viro 	if (IS_ERR(*p))
683408ef013SChristoph Hellwig 		goto out_put_nd_path;
6846d7b5aaeSAl Viro 
685cc314eefSLinus Torvalds 	error = 0;
6866d7b5aaeSAl Viro 	s = nd_get_link(nd);
6876d7b5aaeSAl Viro 	if (s) {
6881da177e4SLinus Torvalds 		error = __vfs_follow_link(nd, s);
6896d7b5aaeSAl Viro 		if (unlikely(error))
6906d7b5aaeSAl Viro 			put_link(nd, link, *p);
691b5fb63c1SChristoph Hellwig 	}
6926d7b5aaeSAl Viro 
6936d7b5aaeSAl Viro 	return error;
6946d7b5aaeSAl Viro 
6956d7b5aaeSAl Viro out_put_nd_path:
6966d7b5aaeSAl Viro 	path_put(&nd->path);
6976d7b5aaeSAl Viro 	path_put(link);
6981da177e4SLinus Torvalds 	return error;
6991da177e4SLinus Torvalds }
7001da177e4SLinus Torvalds 
70131e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
70231e6b01fSNick Piggin {
7030714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
7040714a533SAl Viro 	struct mount *parent;
70531e6b01fSNick Piggin 	struct dentry *mountpoint;
70631e6b01fSNick Piggin 
7070714a533SAl Viro 	parent = mnt->mnt_parent;
7080714a533SAl Viro 	if (&parent->mnt == path->mnt)
70931e6b01fSNick Piggin 		return 0;
710a73324daSAl Viro 	mountpoint = mnt->mnt_mountpoint;
71131e6b01fSNick Piggin 	path->dentry = mountpoint;
7120714a533SAl Viro 	path->mnt = &parent->mnt;
71331e6b01fSNick Piggin 	return 1;
71431e6b01fSNick Piggin }
71531e6b01fSNick Piggin 
716f015f126SDavid Howells /*
717f015f126SDavid Howells  * follow_up - Find the mountpoint of path's vfsmount
718f015f126SDavid Howells  *
719f015f126SDavid Howells  * Given a path, find the mountpoint of its source file system.
720f015f126SDavid Howells  * Replace @path with the path of the mountpoint in the parent mount.
721f015f126SDavid Howells  * Up is towards /.
722f015f126SDavid Howells  *
723f015f126SDavid Howells  * Return 1 if we went up a level and 0 if we were already at the
724f015f126SDavid Howells  * root.
725f015f126SDavid Howells  */
726bab77ebfSAl Viro int follow_up(struct path *path)
7271da177e4SLinus Torvalds {
7280714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
7290714a533SAl Viro 	struct mount *parent;
7301da177e4SLinus Torvalds 	struct dentry *mountpoint;
73199b7db7bSNick Piggin 
732962830dfSAndi Kleen 	br_read_lock(&vfsmount_lock);
7330714a533SAl Viro 	parent = mnt->mnt_parent;
7343c0a6163SAl Viro 	if (parent == mnt) {
735962830dfSAndi Kleen 		br_read_unlock(&vfsmount_lock);
7361da177e4SLinus Torvalds 		return 0;
7371da177e4SLinus Torvalds 	}
7380714a533SAl Viro 	mntget(&parent->mnt);
739a73324daSAl Viro 	mountpoint = dget(mnt->mnt_mountpoint);
740962830dfSAndi Kleen 	br_read_unlock(&vfsmount_lock);
741bab77ebfSAl Viro 	dput(path->dentry);
742bab77ebfSAl Viro 	path->dentry = mountpoint;
743bab77ebfSAl Viro 	mntput(path->mnt);
7440714a533SAl Viro 	path->mnt = &parent->mnt;
7451da177e4SLinus Torvalds 	return 1;
7461da177e4SLinus Torvalds }
7471da177e4SLinus Torvalds 
748b5c84bf6SNick Piggin /*
7499875cf80SDavid Howells  * Perform an automount
7509875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
7519875cf80SDavid Howells  *   were called with.
7521da177e4SLinus Torvalds  */
7539875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
7549875cf80SDavid Howells 			    bool *need_mntput)
75531e6b01fSNick Piggin {
7569875cf80SDavid Howells 	struct vfsmount *mnt;
757ea5b778aSDavid Howells 	int err;
7589875cf80SDavid Howells 
7599875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
7609875cf80SDavid Howells 		return -EREMOTE;
7619875cf80SDavid Howells 
7620ec26fd0SMiklos Szeredi 	/* We don't want to mount if someone's just doing a stat -
7630ec26fd0SMiklos Szeredi 	 * unless they're stat'ing a directory and appended a '/' to
7640ec26fd0SMiklos Szeredi 	 * the name.
7650ec26fd0SMiklos Szeredi 	 *
7660ec26fd0SMiklos Szeredi 	 * We do, however, want to mount if someone wants to open or
7675a30d8a2SDavid Howells 	 * create a file of any type under the mountpoint, wants to
7680ec26fd0SMiklos Szeredi 	 * traverse through the mountpoint or wants to open the
7690ec26fd0SMiklos Szeredi 	 * mounted directory.  Also, autofs may mark negative dentries
7700ec26fd0SMiklos Szeredi 	 * as being automount points.  These will need the attentions
7710ec26fd0SMiklos Szeredi 	 * of the daemon to instantiate them before they can be used.
7725a30d8a2SDavid Howells 	 */
7735a30d8a2SDavid Howells 	if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
774d94c177bSLinus Torvalds 		     LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
7755a30d8a2SDavid Howells 	    path->dentry->d_inode)
7769875cf80SDavid Howells 		return -EISDIR;
7770ec26fd0SMiklos Szeredi 
7789875cf80SDavid Howells 	current->total_link_count++;
7799875cf80SDavid Howells 	if (current->total_link_count >= 40)
7809875cf80SDavid Howells 		return -ELOOP;
7819875cf80SDavid Howells 
7829875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
7839875cf80SDavid Howells 	if (IS_ERR(mnt)) {
7849875cf80SDavid Howells 		/*
7859875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
7869875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
7879875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
7889875cf80SDavid Howells 		 *
7899875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
7909875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
7919875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
7929875cf80SDavid Howells 		 */
79349084c3bSAl Viro 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
7949875cf80SDavid Howells 			return -EREMOTE;
7959875cf80SDavid Howells 		return PTR_ERR(mnt);
79631e6b01fSNick Piggin 	}
797ea5b778aSDavid Howells 
7989875cf80SDavid Howells 	if (!mnt) /* mount collision */
7999875cf80SDavid Howells 		return 0;
8009875cf80SDavid Howells 
8018aef1884SAl Viro 	if (!*need_mntput) {
8028aef1884SAl Viro 		/* lock_mount() may release path->mnt on error */
8038aef1884SAl Viro 		mntget(path->mnt);
8048aef1884SAl Viro 		*need_mntput = true;
8058aef1884SAl Viro 	}
80619a167afSAl Viro 	err = finish_automount(mnt, path);
807ea5b778aSDavid Howells 
808ea5b778aSDavid Howells 	switch (err) {
809ea5b778aSDavid Howells 	case -EBUSY:
810ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
81119a167afSAl Viro 		return 0;
812ea5b778aSDavid Howells 	case 0:
8138aef1884SAl Viro 		path_put(path);
8149875cf80SDavid Howells 		path->mnt = mnt;
8159875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
8169875cf80SDavid Howells 		return 0;
81719a167afSAl Viro 	default:
81819a167afSAl Viro 		return err;
8199875cf80SDavid Howells 	}
82019a167afSAl Viro 
821ea5b778aSDavid Howells }
8229875cf80SDavid Howells 
8239875cf80SDavid Howells /*
8249875cf80SDavid Howells  * Handle a dentry that is managed in some way.
825cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
8269875cf80SDavid Howells  * - Flagged as mountpoint
8279875cf80SDavid Howells  * - Flagged as automount point
8289875cf80SDavid Howells  *
8299875cf80SDavid Howells  * This may only be called in refwalk mode.
8309875cf80SDavid Howells  *
8319875cf80SDavid Howells  * Serialization is taken care of in namespace.c
8329875cf80SDavid Howells  */
8339875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
8349875cf80SDavid Howells {
8358aef1884SAl Viro 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
8369875cf80SDavid Howells 	unsigned managed;
8379875cf80SDavid Howells 	bool need_mntput = false;
8388aef1884SAl Viro 	int ret = 0;
8399875cf80SDavid Howells 
8409875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
8419875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
8429875cf80SDavid Howells 	 * the components of that value change under us */
8439875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
8449875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
8459875cf80SDavid Howells 	       unlikely(managed != 0)) {
846cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
847cc53ce53SDavid Howells 		 * being held. */
848cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
849cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
850cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
8511aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
852cc53ce53SDavid Howells 			if (ret < 0)
8538aef1884SAl Viro 				break;
854cc53ce53SDavid Howells 		}
855cc53ce53SDavid Howells 
8569875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
8579875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
8589875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
8599875cf80SDavid Howells 			if (mounted) {
8609875cf80SDavid Howells 				dput(path->dentry);
8619875cf80SDavid Howells 				if (need_mntput)
862463ffb2eSAl Viro 					mntput(path->mnt);
863463ffb2eSAl Viro 				path->mnt = mounted;
864463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
8659875cf80SDavid Howells 				need_mntput = true;
8669875cf80SDavid Howells 				continue;
867463ffb2eSAl Viro 			}
868463ffb2eSAl Viro 
8699875cf80SDavid Howells 			/* Something is mounted on this dentry in another
8709875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
8719875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
8729875cf80SDavid Howells 			 * vfsmount_lock */
8731da177e4SLinus Torvalds 		}
8749875cf80SDavid Howells 
8759875cf80SDavid Howells 		/* Handle an automount point */
8769875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
8779875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
8789875cf80SDavid Howells 			if (ret < 0)
8798aef1884SAl Viro 				break;
8809875cf80SDavid Howells 			continue;
8819875cf80SDavid Howells 		}
8829875cf80SDavid Howells 
8839875cf80SDavid Howells 		/* We didn't change the current path point */
8849875cf80SDavid Howells 		break;
8859875cf80SDavid Howells 	}
8868aef1884SAl Viro 
8878aef1884SAl Viro 	if (need_mntput && path->mnt == mnt)
8888aef1884SAl Viro 		mntput(path->mnt);
8898aef1884SAl Viro 	if (ret == -EISDIR)
8908aef1884SAl Viro 		ret = 0;
891a3fbbde7SAl Viro 	return ret < 0 ? ret : need_mntput;
8921da177e4SLinus Torvalds }
8931da177e4SLinus Torvalds 
894cc53ce53SDavid Howells int follow_down_one(struct path *path)
8951da177e4SLinus Torvalds {
8961da177e4SLinus Torvalds 	struct vfsmount *mounted;
8971da177e4SLinus Torvalds 
8981c755af4SAl Viro 	mounted = lookup_mnt(path);
8991da177e4SLinus Torvalds 	if (mounted) {
9009393bd07SAl Viro 		dput(path->dentry);
9019393bd07SAl Viro 		mntput(path->mnt);
9029393bd07SAl Viro 		path->mnt = mounted;
9039393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
9041da177e4SLinus Torvalds 		return 1;
9051da177e4SLinus Torvalds 	}
9061da177e4SLinus Torvalds 	return 0;
9071da177e4SLinus Torvalds }
9081da177e4SLinus Torvalds 
90962a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
91062a7375eSIan Kent {
91162a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
91262a7375eSIan Kent 		dentry->d_op->d_manage(dentry, true) < 0);
91362a7375eSIan Kent }
91462a7375eSIan Kent 
9159875cf80SDavid Howells /*
916287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
917287548e4SAl Viro  * we meet a managed dentry that would need blocking.
9189875cf80SDavid Howells  */
9199875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
920287548e4SAl Viro 			       struct inode **inode)
9219875cf80SDavid Howells {
92262a7375eSIan Kent 	for (;;) {
923c7105365SAl Viro 		struct mount *mounted;
92462a7375eSIan Kent 		/*
92562a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
92662a7375eSIan Kent 		 * that wants to block transit.
92762a7375eSIan Kent 		 */
928287548e4SAl Viro 		if (unlikely(managed_dentry_might_block(path->dentry)))
929ab90911fSDavid Howells 			return false;
93062a7375eSIan Kent 
93162a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
93262a7375eSIan Kent 			break;
93362a7375eSIan Kent 
9349875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
9359875cf80SDavid Howells 		if (!mounted)
9369875cf80SDavid Howells 			break;
937c7105365SAl Viro 		path->mnt = &mounted->mnt;
938c7105365SAl Viro 		path->dentry = mounted->mnt.mnt_root;
939a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
9409875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
94159430262SLinus Torvalds 		/*
94259430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
94359430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
94459430262SLinus Torvalds 		 * because a mount-point is always pinned.
94559430262SLinus Torvalds 		 */
94659430262SLinus Torvalds 		*inode = path->dentry->d_inode;
9479875cf80SDavid Howells 	}
9489875cf80SDavid Howells 	return true;
9499875cf80SDavid Howells }
9509875cf80SDavid Howells 
951dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd)
952287548e4SAl Viro {
953dea39376SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
954c7105365SAl Viro 		struct mount *mounted;
955dea39376SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
956287548e4SAl Viro 		if (!mounted)
957287548e4SAl Viro 			break;
958c7105365SAl Viro 		nd->path.mnt = &mounted->mnt;
959c7105365SAl Viro 		nd->path.dentry = mounted->mnt.mnt_root;
960dea39376SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
961287548e4SAl Viro 	}
962287548e4SAl Viro }
963287548e4SAl Viro 
96431e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
96531e6b01fSNick Piggin {
96631e6b01fSNick Piggin 	set_root_rcu(nd);
96731e6b01fSNick Piggin 
96831e6b01fSNick Piggin 	while (1) {
96931e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
97031e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
97131e6b01fSNick Piggin 			break;
97231e6b01fSNick Piggin 		}
97331e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
97431e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
97531e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
97631e6b01fSNick Piggin 			unsigned seq;
97731e6b01fSNick Piggin 
97831e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
97931e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
980ef7562d5SAl Viro 				goto failed;
98131e6b01fSNick Piggin 			nd->path.dentry = parent;
98231e6b01fSNick Piggin 			nd->seq = seq;
98331e6b01fSNick Piggin 			break;
98431e6b01fSNick Piggin 		}
98531e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
98631e6b01fSNick Piggin 			break;
98731e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
98831e6b01fSNick Piggin 	}
989dea39376SAl Viro 	follow_mount_rcu(nd);
990dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
99131e6b01fSNick Piggin 	return 0;
992ef7562d5SAl Viro 
993ef7562d5SAl Viro failed:
994ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
9955b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
996ef7562d5SAl Viro 		nd->root.mnt = NULL;
99732a7991bSAl Viro 	unlock_rcu_walk();
998ef7562d5SAl Viro 	return -ECHILD;
99931e6b01fSNick Piggin }
100031e6b01fSNick Piggin 
10019875cf80SDavid Howells /*
1002cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
1003cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
1004cc53ce53SDavid Howells  * caller is permitted to proceed or not.
1005cc53ce53SDavid Howells  */
10067cc90cc3SAl Viro int follow_down(struct path *path)
1007cc53ce53SDavid Howells {
1008cc53ce53SDavid Howells 	unsigned managed;
1009cc53ce53SDavid Howells 	int ret;
1010cc53ce53SDavid Howells 
1011cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
1012cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1013cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1014cc53ce53SDavid Howells 		 * being held.
1015cc53ce53SDavid Howells 		 *
1016cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
1017cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
1018cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
1019cc53ce53SDavid Howells 		 * superstructure.
1020cc53ce53SDavid Howells 		 *
1021cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
1022cc53ce53SDavid Howells 		 */
1023cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
1024cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1025cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
1026ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
10271aed3e42SAl Viro 				path->dentry, false);
1028cc53ce53SDavid Howells 			if (ret < 0)
1029cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
1030cc53ce53SDavid Howells 		}
1031cc53ce53SDavid Howells 
1032cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
1033cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
1034cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
1035cc53ce53SDavid Howells 			if (!mounted)
1036cc53ce53SDavid Howells 				break;
1037cc53ce53SDavid Howells 			dput(path->dentry);
1038cc53ce53SDavid Howells 			mntput(path->mnt);
1039cc53ce53SDavid Howells 			path->mnt = mounted;
1040cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1041cc53ce53SDavid Howells 			continue;
1042cc53ce53SDavid Howells 		}
1043cc53ce53SDavid Howells 
1044cc53ce53SDavid Howells 		/* Don't handle automount points here */
1045cc53ce53SDavid Howells 		break;
1046cc53ce53SDavid Howells 	}
1047cc53ce53SDavid Howells 	return 0;
1048cc53ce53SDavid Howells }
1049cc53ce53SDavid Howells 
1050cc53ce53SDavid Howells /*
10519875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
10529875cf80SDavid Howells  */
10539875cf80SDavid Howells static void follow_mount(struct path *path)
10549875cf80SDavid Howells {
10559875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
10569875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
10579875cf80SDavid Howells 		if (!mounted)
10589875cf80SDavid Howells 			break;
10599875cf80SDavid Howells 		dput(path->dentry);
10609875cf80SDavid Howells 		mntput(path->mnt);
10619875cf80SDavid Howells 		path->mnt = mounted;
10629875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
10639875cf80SDavid Howells 	}
10649875cf80SDavid Howells }
10659875cf80SDavid Howells 
106631e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
10671da177e4SLinus Torvalds {
10682a737871SAl Viro 	set_root(nd);
1069e518ddb7SAndreas Mohr 
10701da177e4SLinus Torvalds 	while(1) {
10714ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
10721da177e4SLinus Torvalds 
10732a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
10742a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
10751da177e4SLinus Torvalds 			break;
10761da177e4SLinus Torvalds 		}
10774ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
10783088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
10793088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
10801da177e4SLinus Torvalds 			dput(old);
10811da177e4SLinus Torvalds 			break;
10821da177e4SLinus Torvalds 		}
10833088dd70SAl Viro 		if (!follow_up(&nd->path))
10841da177e4SLinus Torvalds 			break;
10851da177e4SLinus Torvalds 	}
108679ed0226SAl Viro 	follow_mount(&nd->path);
108731e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
10881da177e4SLinus Torvalds }
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds /*
1091bad61189SMiklos Szeredi  * This looks up the name in dcache, possibly revalidates the old dentry and
1092bad61189SMiklos Szeredi  * allocates a new one if not found or not valid.  In the need_lookup argument
1093bad61189SMiklos Szeredi  * returns whether i_op->lookup is necessary.
1094bad61189SMiklos Szeredi  *
1095bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
1096baa03890SNick Piggin  */
1097bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1098201f956eSAl Viro 				    unsigned int flags, bool *need_lookup)
1099baa03890SNick Piggin {
1100baa03890SNick Piggin 	struct dentry *dentry;
1101bad61189SMiklos Szeredi 	int error;
1102baa03890SNick Piggin 
1103bad61189SMiklos Szeredi 	*need_lookup = false;
1104bad61189SMiklos Szeredi 	dentry = d_lookup(dir, name);
1105bad61189SMiklos Szeredi 	if (dentry) {
1106bad61189SMiklos Szeredi 		if (d_need_lookup(dentry)) {
1107bad61189SMiklos Szeredi 			*need_lookup = true;
1108bad61189SMiklos Szeredi 		} else if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1109201f956eSAl Viro 			error = d_revalidate(dentry, flags);
1110bad61189SMiklos Szeredi 			if (unlikely(error <= 0)) {
1111bad61189SMiklos Szeredi 				if (error < 0) {
1112bad61189SMiklos Szeredi 					dput(dentry);
1113bad61189SMiklos Szeredi 					return ERR_PTR(error);
1114bad61189SMiklos Szeredi 				} else if (!d_invalidate(dentry)) {
1115bad61189SMiklos Szeredi 					dput(dentry);
1116bad61189SMiklos Szeredi 					dentry = NULL;
1117bad61189SMiklos Szeredi 				}
1118bad61189SMiklos Szeredi 			}
1119bad61189SMiklos Szeredi 		}
1120bad61189SMiklos Szeredi 	}
1121baa03890SNick Piggin 
1122bad61189SMiklos Szeredi 	if (!dentry) {
1123bad61189SMiklos Szeredi 		dentry = d_alloc(dir, name);
1124baa03890SNick Piggin 		if (unlikely(!dentry))
1125baa03890SNick Piggin 			return ERR_PTR(-ENOMEM);
1126baa03890SNick Piggin 
1127bad61189SMiklos Szeredi 		*need_lookup = true;
1128baa03890SNick Piggin 	}
1129baa03890SNick Piggin 	return dentry;
1130baa03890SNick Piggin }
1131baa03890SNick Piggin 
1132baa03890SNick Piggin /*
1133bad61189SMiklos Szeredi  * Call i_op->lookup on the dentry.  The dentry must be negative but may be
1134bad61189SMiklos Szeredi  * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1135bad61189SMiklos Szeredi  *
1136bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
113744396f4bSJosef Bacik  */
1138bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
113972bd866aSAl Viro 				  unsigned int flags)
114044396f4bSJosef Bacik {
114144396f4bSJosef Bacik 	struct dentry *old;
114244396f4bSJosef Bacik 
114344396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
1144bad61189SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
1145e188dc02SMiklos Szeredi 		dput(dentry);
114644396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
1147e188dc02SMiklos Szeredi 	}
114844396f4bSJosef Bacik 
114972bd866aSAl Viro 	old = dir->i_op->lookup(dir, dentry, flags);
115044396f4bSJosef Bacik 	if (unlikely(old)) {
115144396f4bSJosef Bacik 		dput(dentry);
115244396f4bSJosef Bacik 		dentry = old;
115344396f4bSJosef Bacik 	}
115444396f4bSJosef Bacik 	return dentry;
115544396f4bSJosef Bacik }
115644396f4bSJosef Bacik 
1157a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name,
115872bd866aSAl Viro 		struct dentry *base, unsigned int flags)
1159a3255546SAl Viro {
1160bad61189SMiklos Szeredi 	bool need_lookup;
1161a3255546SAl Viro 	struct dentry *dentry;
1162a3255546SAl Viro 
116372bd866aSAl Viro 	dentry = lookup_dcache(name, base, flags, &need_lookup);
1164bad61189SMiklos Szeredi 	if (!need_lookup)
1165a3255546SAl Viro 		return dentry;
1166bad61189SMiklos Szeredi 
116772bd866aSAl Viro 	return lookup_real(base->d_inode, dentry, flags);
1168a3255546SAl Viro }
1169a3255546SAl Viro 
117044396f4bSJosef Bacik /*
11711da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
11721da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
11731da177e4SLinus Torvalds  *  It _is_ time-critical.
11741da177e4SLinus Torvalds  */
1175697f514dSMiklos Szeredi static int lookup_fast(struct nameidata *nd, struct qstr *name,
117631e6b01fSNick Piggin 		       struct path *path, struct inode **inode)
11771da177e4SLinus Torvalds {
11784ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
117931e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
11805a18fff2SAl Viro 	int need_reval = 1;
11815a18fff2SAl Viro 	int status = 1;
11829875cf80SDavid Howells 	int err;
11839875cf80SDavid Howells 
11843cac260aSAl Viro 	/*
1185b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1186b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1187b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1188b04f784eSNick Piggin 	 */
118931e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
119031e6b01fSNick Piggin 		unsigned seq;
119112f8ad4bSLinus Torvalds 		dentry = __d_lookup_rcu(parent, name, &seq, nd->inode);
11925a18fff2SAl Viro 		if (!dentry)
11935a18fff2SAl Viro 			goto unlazy;
11945a18fff2SAl Viro 
119512f8ad4bSLinus Torvalds 		/*
119612f8ad4bSLinus Torvalds 		 * This sequence count validates that the inode matches
119712f8ad4bSLinus Torvalds 		 * the dentry name information from lookup.
119812f8ad4bSLinus Torvalds 		 */
119912f8ad4bSLinus Torvalds 		*inode = dentry->d_inode;
120012f8ad4bSLinus Torvalds 		if (read_seqcount_retry(&dentry->d_seq, seq))
120112f8ad4bSLinus Torvalds 			return -ECHILD;
120212f8ad4bSLinus Torvalds 
120312f8ad4bSLinus Torvalds 		/*
120412f8ad4bSLinus Torvalds 		 * This sequence count validates that the parent had no
120512f8ad4bSLinus Torvalds 		 * changes while we did the lookup of the dentry above.
120612f8ad4bSLinus Torvalds 		 *
120712f8ad4bSLinus Torvalds 		 * The memory barrier in read_seqcount_begin of child is
120812f8ad4bSLinus Torvalds 		 *  enough, we can use __read_seqcount_retry here.
120912f8ad4bSLinus Torvalds 		 */
121031e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
121131e6b01fSNick Piggin 			return -ECHILD;
121231e6b01fSNick Piggin 		nd->seq = seq;
12135a18fff2SAl Viro 
1214fa4ee159SMiklos Szeredi 		if (unlikely(d_need_lookup(dentry)))
1215fa4ee159SMiklos Szeredi 			goto unlazy;
121624643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
12174ce16ef3SAl Viro 			status = d_revalidate(dentry, nd->flags);
12185a18fff2SAl Viro 			if (unlikely(status <= 0)) {
12195a18fff2SAl Viro 				if (status != -ECHILD)
12205a18fff2SAl Viro 					need_reval = 0;
12215a18fff2SAl Viro 				goto unlazy;
12225a18fff2SAl Viro 			}
122324643087SAl Viro 		}
122431e6b01fSNick Piggin 		path->mnt = mnt;
122531e6b01fSNick Piggin 		path->dentry = dentry;
1226d6e9bd25SAl Viro 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1227d6e9bd25SAl Viro 			goto unlazy;
1228d6e9bd25SAl Viro 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1229d6e9bd25SAl Viro 			goto unlazy;
12309875cf80SDavid Howells 		return 0;
12315a18fff2SAl Viro unlazy:
123219660af7SAl Viro 		if (unlazy_walk(nd, dentry))
12335a18fff2SAl Viro 			return -ECHILD;
12345a18fff2SAl Viro 	} else {
123531e6b01fSNick Piggin 		dentry = __d_lookup(parent, name);
123624643087SAl Viro 	}
12375a18fff2SAl Viro 
123881e6f520SAl Viro 	if (unlikely(!dentry))
123981e6f520SAl Viro 		goto need_lookup;
12405a18fff2SAl Viro 
124181e6f520SAl Viro 	if (unlikely(d_need_lookup(dentry))) {
124281e6f520SAl Viro 		dput(dentry);
124381e6f520SAl Viro 		goto need_lookup;
12445a18fff2SAl Viro 	}
124581e6f520SAl Viro 
12465a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
12474ce16ef3SAl Viro 		status = d_revalidate(dentry, nd->flags);
12485a18fff2SAl Viro 	if (unlikely(status <= 0)) {
12495a18fff2SAl Viro 		if (status < 0) {
12505a18fff2SAl Viro 			dput(dentry);
12515a18fff2SAl Viro 			return status;
12525a18fff2SAl Viro 		}
12535a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
12545a18fff2SAl Viro 			dput(dentry);
125581e6f520SAl Viro 			goto need_lookup;
12565a18fff2SAl Viro 		}
12575a18fff2SAl Viro 	}
1258697f514dSMiklos Szeredi 
12591da177e4SLinus Torvalds 	path->mnt = mnt;
12601da177e4SLinus Torvalds 	path->dentry = dentry;
12619875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
126289312214SIan Kent 	if (unlikely(err < 0)) {
126389312214SIan Kent 		path_put_conditional(path, nd);
12649875cf80SDavid Howells 		return err;
126589312214SIan Kent 	}
1266a3fbbde7SAl Viro 	if (err)
1267a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
126831e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
12691da177e4SLinus Torvalds 	return 0;
127081e6f520SAl Viro 
127181e6f520SAl Viro need_lookup:
1272697f514dSMiklos Szeredi 	return 1;
1273697f514dSMiklos Szeredi }
1274697f514dSMiklos Szeredi 
1275697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */
1276697f514dSMiklos Szeredi static int lookup_slow(struct nameidata *nd, struct qstr *name,
1277697f514dSMiklos Szeredi 		       struct path *path)
1278697f514dSMiklos Szeredi {
1279697f514dSMiklos Szeredi 	struct dentry *dentry, *parent;
1280697f514dSMiklos Szeredi 	int err;
1281697f514dSMiklos Szeredi 
1282697f514dSMiklos Szeredi 	parent = nd->path.dentry;
128381e6f520SAl Viro 	BUG_ON(nd->inode != parent->d_inode);
128481e6f520SAl Viro 
128581e6f520SAl Viro 	mutex_lock(&parent->d_inode->i_mutex);
128672bd866aSAl Viro 	dentry = __lookup_hash(name, parent, nd->flags);
128781e6f520SAl Viro 	mutex_unlock(&parent->d_inode->i_mutex);
128881e6f520SAl Viro 	if (IS_ERR(dentry))
128981e6f520SAl Viro 		return PTR_ERR(dentry);
1290697f514dSMiklos Szeredi 	path->mnt = nd->path.mnt;
1291697f514dSMiklos Szeredi 	path->dentry = dentry;
1292697f514dSMiklos Szeredi 	err = follow_managed(path, nd->flags);
1293697f514dSMiklos Szeredi 	if (unlikely(err < 0)) {
1294697f514dSMiklos Szeredi 		path_put_conditional(path, nd);
1295697f514dSMiklos Szeredi 		return err;
1296697f514dSMiklos Szeredi 	}
1297697f514dSMiklos Szeredi 	if (err)
1298697f514dSMiklos Szeredi 		nd->flags |= LOOKUP_JUMPED;
1299697f514dSMiklos Szeredi 	return 0;
13001da177e4SLinus Torvalds }
13011da177e4SLinus Torvalds 
130252094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
130352094c8aSAl Viro {
130452094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
13054ad5abb3SAl Viro 		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
130652094c8aSAl Viro 		if (err != -ECHILD)
130752094c8aSAl Viro 			return err;
130819660af7SAl Viro 		if (unlazy_walk(nd, NULL))
130952094c8aSAl Viro 			return -ECHILD;
131052094c8aSAl Viro 	}
13114ad5abb3SAl Viro 	return inode_permission(nd->inode, MAY_EXEC);
131252094c8aSAl Viro }
131352094c8aSAl Viro 
13149856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
13159856fa1bSAl Viro {
13169856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
13179856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
13189856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
13199856fa1bSAl Viro 				return -ECHILD;
13209856fa1bSAl Viro 		} else
13219856fa1bSAl Viro 			follow_dotdot(nd);
13229856fa1bSAl Viro 	}
13239856fa1bSAl Viro 	return 0;
13249856fa1bSAl Viro }
13259856fa1bSAl Viro 
1326951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1327951361f9SAl Viro {
1328951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1329951361f9SAl Viro 		path_put(&nd->path);
1330951361f9SAl Viro 	} else {
1331951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
13325b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1333951361f9SAl Viro 			nd->root.mnt = NULL;
133432a7991bSAl Viro 		unlock_rcu_walk();
1335951361f9SAl Viro 	}
1336951361f9SAl Viro }
1337951361f9SAl Viro 
13383ddcd056SLinus Torvalds /*
13393ddcd056SLinus Torvalds  * Do we need to follow links? We _really_ want to be able
13403ddcd056SLinus Torvalds  * to do this check without having to look at inode->i_op,
13413ddcd056SLinus Torvalds  * so we keep a cache of "no, this doesn't need follow_link"
13423ddcd056SLinus Torvalds  * for the common case.
13433ddcd056SLinus Torvalds  */
13447813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow)
13453ddcd056SLinus Torvalds {
13463ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
13473ddcd056SLinus Torvalds 		if (likely(inode->i_op->follow_link))
13483ddcd056SLinus Torvalds 			return follow;
13493ddcd056SLinus Torvalds 
13503ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
13513ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
13523ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_NOFOLLOW;
13533ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
13543ddcd056SLinus Torvalds 	}
13553ddcd056SLinus Torvalds 	return 0;
13563ddcd056SLinus Torvalds }
13573ddcd056SLinus Torvalds 
1358ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
1359ce57dfc1SAl Viro 		struct qstr *name, int type, int follow)
1360ce57dfc1SAl Viro {
1361ce57dfc1SAl Viro 	struct inode *inode;
1362ce57dfc1SAl Viro 	int err;
1363ce57dfc1SAl Viro 	/*
1364ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1365ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1366ce57dfc1SAl Viro 	 * parent relationships.
1367ce57dfc1SAl Viro 	 */
1368ce57dfc1SAl Viro 	if (unlikely(type != LAST_NORM))
1369ce57dfc1SAl Viro 		return handle_dots(nd, type);
1370697f514dSMiklos Szeredi 	err = lookup_fast(nd, name, path, &inode);
1371ce57dfc1SAl Viro 	if (unlikely(err)) {
1372697f514dSMiklos Szeredi 		if (err < 0)
1373697f514dSMiklos Szeredi 			goto out_err;
1374697f514dSMiklos Szeredi 
1375697f514dSMiklos Szeredi 		err = lookup_slow(nd, name, path);
1376697f514dSMiklos Szeredi 		if (err < 0)
1377697f514dSMiklos Szeredi 			goto out_err;
1378697f514dSMiklos Szeredi 
1379697f514dSMiklos Szeredi 		inode = path->dentry->d_inode;
1380ce57dfc1SAl Viro 	}
1381697f514dSMiklos Szeredi 	err = -ENOENT;
1382697f514dSMiklos Szeredi 	if (!inode)
1383697f514dSMiklos Szeredi 		goto out_path_put;
1384697f514dSMiklos Szeredi 
13857813b94aSLinus Torvalds 	if (should_follow_link(inode, follow)) {
138619660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
138719660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
1388697f514dSMiklos Szeredi 				err = -ECHILD;
1389697f514dSMiklos Szeredi 				goto out_err;
139019660af7SAl Viro 			}
139119660af7SAl Viro 		}
1392ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1393ce57dfc1SAl Viro 		return 1;
1394ce57dfc1SAl Viro 	}
1395ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1396ce57dfc1SAl Viro 	nd->inode = inode;
1397ce57dfc1SAl Viro 	return 0;
1398697f514dSMiklos Szeredi 
1399697f514dSMiklos Szeredi out_path_put:
1400697f514dSMiklos Szeredi 	path_to_nameidata(path, nd);
1401697f514dSMiklos Szeredi out_err:
1402697f514dSMiklos Szeredi 	terminate_walk(nd);
1403697f514dSMiklos Szeredi 	return err;
1404ce57dfc1SAl Viro }
1405ce57dfc1SAl Viro 
14061da177e4SLinus Torvalds /*
1407b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1408b356379aSAl Viro  * limiting consecutive symlinks to 40.
1409b356379aSAl Viro  *
1410b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1411b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1412b356379aSAl Viro  */
1413b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1414b356379aSAl Viro {
1415b356379aSAl Viro 	int res;
1416b356379aSAl Viro 
1417b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1418b356379aSAl Viro 		path_put_conditional(path, nd);
1419b356379aSAl Viro 		path_put(&nd->path);
1420b356379aSAl Viro 		return -ELOOP;
1421b356379aSAl Viro 	}
14221a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1423b356379aSAl Viro 
1424b356379aSAl Viro 	nd->depth++;
1425b356379aSAl Viro 	current->link_count++;
1426b356379aSAl Viro 
1427b356379aSAl Viro 	do {
1428b356379aSAl Viro 		struct path link = *path;
1429b356379aSAl Viro 		void *cookie;
1430574197e0SAl Viro 
1431574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
14326d7b5aaeSAl Viro 		if (res)
14336d7b5aaeSAl Viro 			break;
1434b356379aSAl Viro 		res = walk_component(nd, path, &nd->last,
1435b356379aSAl Viro 				     nd->last_type, LOOKUP_FOLLOW);
1436574197e0SAl Viro 		put_link(nd, &link, cookie);
1437b356379aSAl Viro 	} while (res > 0);
1438b356379aSAl Viro 
1439b356379aSAl Viro 	current->link_count--;
1440b356379aSAl Viro 	nd->depth--;
1441b356379aSAl Viro 	return res;
1442b356379aSAl Viro }
1443b356379aSAl Viro 
1444b356379aSAl Viro /*
14453ddcd056SLinus Torvalds  * We really don't want to look at inode->i_op->lookup
14463ddcd056SLinus Torvalds  * when we don't have to. So we keep a cache bit in
14473ddcd056SLinus Torvalds  * the inode ->i_opflags field that says "yes, we can
14483ddcd056SLinus Torvalds  * do lookup on this inode".
14493ddcd056SLinus Torvalds  */
14503ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode)
14513ddcd056SLinus Torvalds {
14523ddcd056SLinus Torvalds 	if (likely(inode->i_opflags & IOP_LOOKUP))
14533ddcd056SLinus Torvalds 		return 1;
14543ddcd056SLinus Torvalds 	if (likely(!inode->i_op->lookup))
14553ddcd056SLinus Torvalds 		return 0;
14563ddcd056SLinus Torvalds 
14573ddcd056SLinus Torvalds 	/* We do this once for the lifetime of the inode */
14583ddcd056SLinus Torvalds 	spin_lock(&inode->i_lock);
14593ddcd056SLinus Torvalds 	inode->i_opflags |= IOP_LOOKUP;
14603ddcd056SLinus Torvalds 	spin_unlock(&inode->i_lock);
14613ddcd056SLinus Torvalds 	return 1;
14623ddcd056SLinus Torvalds }
14633ddcd056SLinus Torvalds 
1464bfcfaa77SLinus Torvalds /*
1465bfcfaa77SLinus Torvalds  * We can do the critical dentry name comparison and hashing
1466bfcfaa77SLinus Torvalds  * operations one word at a time, but we are limited to:
1467bfcfaa77SLinus Torvalds  *
1468bfcfaa77SLinus Torvalds  * - Architectures with fast unaligned word accesses. We could
1469bfcfaa77SLinus Torvalds  *   do a "get_unaligned()" if this helps and is sufficiently
1470bfcfaa77SLinus Torvalds  *   fast.
1471bfcfaa77SLinus Torvalds  *
1472bfcfaa77SLinus Torvalds  * - Little-endian machines (so that we can generate the mask
1473bfcfaa77SLinus Torvalds  *   of low bytes efficiently). Again, we *could* do a byte
1474bfcfaa77SLinus Torvalds  *   swapping load on big-endian architectures if that is not
1475bfcfaa77SLinus Torvalds  *   expensive enough to make the optimization worthless.
1476bfcfaa77SLinus Torvalds  *
1477bfcfaa77SLinus Torvalds  * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1478bfcfaa77SLinus Torvalds  *   do not trap on the (extremely unlikely) case of a page
1479bfcfaa77SLinus Torvalds  *   crossing operation.
1480bfcfaa77SLinus Torvalds  *
1481bfcfaa77SLinus Torvalds  * - Furthermore, we need an efficient 64-bit compile for the
1482bfcfaa77SLinus Torvalds  *   64-bit case in order to generate the "number of bytes in
1483bfcfaa77SLinus Torvalds  *   the final mask". Again, that could be replaced with a
1484bfcfaa77SLinus Torvalds  *   efficient population count instruction or similar.
1485bfcfaa77SLinus Torvalds  */
1486bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS
1487bfcfaa77SLinus Torvalds 
1488f68e556eSLinus Torvalds #include <asm/word-at-a-time.h>
1489bfcfaa77SLinus Torvalds 
1490f68e556eSLinus Torvalds #ifdef CONFIG_64BIT
1491bfcfaa77SLinus Torvalds 
1492bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash)
1493bfcfaa77SLinus Torvalds {
1494bfcfaa77SLinus Torvalds 	hash += hash >> (8*sizeof(int));
1495bfcfaa77SLinus Torvalds 	return hash;
1496bfcfaa77SLinus Torvalds }
1497bfcfaa77SLinus Torvalds 
1498bfcfaa77SLinus Torvalds #else	/* 32-bit case */
1499bfcfaa77SLinus Torvalds 
1500bfcfaa77SLinus Torvalds #define fold_hash(x) (x)
1501bfcfaa77SLinus Torvalds 
1502bfcfaa77SLinus Torvalds #endif
1503bfcfaa77SLinus Torvalds 
1504bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1505bfcfaa77SLinus Torvalds {
1506bfcfaa77SLinus Torvalds 	unsigned long a, mask;
1507bfcfaa77SLinus Torvalds 	unsigned long hash = 0;
1508bfcfaa77SLinus Torvalds 
1509bfcfaa77SLinus Torvalds 	for (;;) {
1510e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name);
1511bfcfaa77SLinus Torvalds 		if (len < sizeof(unsigned long))
1512bfcfaa77SLinus Torvalds 			break;
1513bfcfaa77SLinus Torvalds 		hash += a;
1514f132c5beSAl Viro 		hash *= 9;
1515bfcfaa77SLinus Torvalds 		name += sizeof(unsigned long);
1516bfcfaa77SLinus Torvalds 		len -= sizeof(unsigned long);
1517bfcfaa77SLinus Torvalds 		if (!len)
1518bfcfaa77SLinus Torvalds 			goto done;
1519bfcfaa77SLinus Torvalds 	}
1520bfcfaa77SLinus Torvalds 	mask = ~(~0ul << len*8);
1521bfcfaa77SLinus Torvalds 	hash += mask & a;
1522bfcfaa77SLinus Torvalds done:
1523bfcfaa77SLinus Torvalds 	return fold_hash(hash);
1524bfcfaa77SLinus Torvalds }
1525bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
1526bfcfaa77SLinus Torvalds 
1527bfcfaa77SLinus Torvalds /*
1528bfcfaa77SLinus Torvalds  * Calculate the length and hash of the path component, and
1529bfcfaa77SLinus Torvalds  * return the length of the component;
1530bfcfaa77SLinus Torvalds  */
1531bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1532bfcfaa77SLinus Torvalds {
153336126f8fSLinus Torvalds 	unsigned long a, b, adata, bdata, mask, hash, len;
153436126f8fSLinus Torvalds 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1535bfcfaa77SLinus Torvalds 
1536bfcfaa77SLinus Torvalds 	hash = a = 0;
1537bfcfaa77SLinus Torvalds 	len = -sizeof(unsigned long);
1538bfcfaa77SLinus Torvalds 	do {
1539bfcfaa77SLinus Torvalds 		hash = (hash + a) * 9;
1540bfcfaa77SLinus Torvalds 		len += sizeof(unsigned long);
1541e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name+len);
154236126f8fSLinus Torvalds 		b = a ^ REPEAT_BYTE('/');
154336126f8fSLinus Torvalds 	} while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1544bfcfaa77SLinus Torvalds 
154536126f8fSLinus Torvalds 	adata = prep_zero_mask(a, adata, &constants);
154636126f8fSLinus Torvalds 	bdata = prep_zero_mask(b, bdata, &constants);
154736126f8fSLinus Torvalds 
154836126f8fSLinus Torvalds 	mask = create_zero_mask(adata | bdata);
154936126f8fSLinus Torvalds 
155036126f8fSLinus Torvalds 	hash += a & zero_bytemask(mask);
1551bfcfaa77SLinus Torvalds 	*hashp = fold_hash(hash);
1552bfcfaa77SLinus Torvalds 
155336126f8fSLinus Torvalds 	return len + find_zero(mask);
1554bfcfaa77SLinus Torvalds }
1555bfcfaa77SLinus Torvalds 
1556bfcfaa77SLinus Torvalds #else
1557bfcfaa77SLinus Torvalds 
15580145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
15590145acc2SLinus Torvalds {
15600145acc2SLinus Torvalds 	unsigned long hash = init_name_hash();
15610145acc2SLinus Torvalds 	while (len--)
15620145acc2SLinus Torvalds 		hash = partial_name_hash(*name++, hash);
15630145acc2SLinus Torvalds 	return end_name_hash(hash);
15640145acc2SLinus Torvalds }
1565ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
15660145acc2SLinus Torvalds 
15673ddcd056SLinus Torvalds /*
1568200e9ef7SLinus Torvalds  * We know there's a real path component here of at least
1569200e9ef7SLinus Torvalds  * one character.
1570200e9ef7SLinus Torvalds  */
1571200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1572200e9ef7SLinus Torvalds {
1573200e9ef7SLinus Torvalds 	unsigned long hash = init_name_hash();
1574200e9ef7SLinus Torvalds 	unsigned long len = 0, c;
1575200e9ef7SLinus Torvalds 
1576200e9ef7SLinus Torvalds 	c = (unsigned char)*name;
1577200e9ef7SLinus Torvalds 	do {
1578200e9ef7SLinus Torvalds 		len++;
1579200e9ef7SLinus Torvalds 		hash = partial_name_hash(c, hash);
1580200e9ef7SLinus Torvalds 		c = (unsigned char)name[len];
1581200e9ef7SLinus Torvalds 	} while (c && c != '/');
1582200e9ef7SLinus Torvalds 	*hashp = end_name_hash(hash);
1583200e9ef7SLinus Torvalds 	return len;
1584200e9ef7SLinus Torvalds }
1585200e9ef7SLinus Torvalds 
1586bfcfaa77SLinus Torvalds #endif
1587bfcfaa77SLinus Torvalds 
1588200e9ef7SLinus Torvalds /*
15891da177e4SLinus Torvalds  * Name resolution.
1590ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1591ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
15921da177e4SLinus Torvalds  *
1593ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1594ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
15951da177e4SLinus Torvalds  */
15966de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
15971da177e4SLinus Torvalds {
15981da177e4SLinus Torvalds 	struct path next;
15991da177e4SLinus Torvalds 	int err;
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds 	while (*name=='/')
16021da177e4SLinus Torvalds 		name++;
16031da177e4SLinus Torvalds 	if (!*name)
1604086e183aSAl Viro 		return 0;
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
16071da177e4SLinus Torvalds 	for(;;) {
16081da177e4SLinus Torvalds 		struct qstr this;
1609200e9ef7SLinus Torvalds 		long len;
1610fe479a58SAl Viro 		int type;
16111da177e4SLinus Torvalds 
161252094c8aSAl Viro 		err = may_lookup(nd);
16131da177e4SLinus Torvalds  		if (err)
16141da177e4SLinus Torvalds 			break;
16151da177e4SLinus Torvalds 
1616200e9ef7SLinus Torvalds 		len = hash_name(name, &this.hash);
16171da177e4SLinus Torvalds 		this.name = name;
1618200e9ef7SLinus Torvalds 		this.len = len;
16191da177e4SLinus Torvalds 
1620fe479a58SAl Viro 		type = LAST_NORM;
1621200e9ef7SLinus Torvalds 		if (name[0] == '.') switch (len) {
1622fe479a58SAl Viro 			case 2:
1623200e9ef7SLinus Torvalds 				if (name[1] == '.') {
1624fe479a58SAl Viro 					type = LAST_DOTDOT;
162516c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
162616c2cd71SAl Viro 				}
1627fe479a58SAl Viro 				break;
1628fe479a58SAl Viro 			case 1:
1629fe479a58SAl Viro 				type = LAST_DOT;
1630fe479a58SAl Viro 		}
16315a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
16325a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
163316c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
16345a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
16355a202bcdSAl Viro 				err = parent->d_op->d_hash(parent, nd->inode,
16365a202bcdSAl Viro 							   &this);
16375a202bcdSAl Viro 				if (err < 0)
16385a202bcdSAl Viro 					break;
16395a202bcdSAl Viro 			}
16405a202bcdSAl Viro 		}
1641fe479a58SAl Viro 
1642200e9ef7SLinus Torvalds 		if (!name[len])
16431da177e4SLinus Torvalds 			goto last_component;
1644200e9ef7SLinus Torvalds 		/*
1645200e9ef7SLinus Torvalds 		 * If it wasn't NUL, we know it was '/'. Skip that
1646200e9ef7SLinus Torvalds 		 * slash, and continue until no more slashes.
1647200e9ef7SLinus Torvalds 		 */
1648200e9ef7SLinus Torvalds 		do {
1649200e9ef7SLinus Torvalds 			len++;
1650200e9ef7SLinus Torvalds 		} while (unlikely(name[len] == '/'));
1651200e9ef7SLinus Torvalds 		if (!name[len])
1652b356379aSAl Viro 			goto last_component;
1653200e9ef7SLinus Torvalds 		name += len;
16541da177e4SLinus Torvalds 
1655ce57dfc1SAl Viro 		err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1656ce57dfc1SAl Viro 		if (err < 0)
1657ce57dfc1SAl Viro 			return err;
1658fe479a58SAl Viro 
1659ce57dfc1SAl Viro 		if (err) {
1660b356379aSAl Viro 			err = nested_symlink(&next, nd);
16611da177e4SLinus Torvalds 			if (err)
1662a7472babSAl Viro 				return err;
166331e6b01fSNick Piggin 		}
16643ddcd056SLinus Torvalds 		if (can_lookup(nd->inode))
16651da177e4SLinus Torvalds 			continue;
16663ddcd056SLinus Torvalds 		err = -ENOTDIR;
16673ddcd056SLinus Torvalds 		break;
16681da177e4SLinus Torvalds 		/* here ends the main loop */
16691da177e4SLinus Torvalds 
16701da177e4SLinus Torvalds last_component:
1671ce57dfc1SAl Viro 		nd->last = this;
1672ce57dfc1SAl Viro 		nd->last_type = type;
1673ce57dfc1SAl Viro 		return 0;
1674ce57dfc1SAl Viro 	}
1675951361f9SAl Viro 	terminate_walk(nd);
16761da177e4SLinus Torvalds 	return err;
16771da177e4SLinus Torvalds }
16781da177e4SLinus Torvalds 
167970e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
168070e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
168131e6b01fSNick Piggin {
168231e6b01fSNick Piggin 	int retval = 0;
168331e6b01fSNick Piggin 	int fput_needed;
168431e6b01fSNick Piggin 	struct file *file;
168531e6b01fSNick Piggin 
168631e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
168716c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
168831e6b01fSNick Piggin 	nd->depth = 0;
16895b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
16905b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
169173d049a4SAl Viro 		if (*name) {
16925b6ca027SAl Viro 			if (!inode->i_op->lookup)
16935b6ca027SAl Viro 				return -ENOTDIR;
16945b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
16955b6ca027SAl Viro 			if (retval)
16965b6ca027SAl Viro 				return retval;
169773d049a4SAl Viro 		}
16985b6ca027SAl Viro 		nd->path = nd->root;
16995b6ca027SAl Viro 		nd->inode = inode;
17005b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
170132a7991bSAl Viro 			lock_rcu_walk();
17025b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
17035b6ca027SAl Viro 		} else {
17045b6ca027SAl Viro 			path_get(&nd->path);
17055b6ca027SAl Viro 		}
17065b6ca027SAl Viro 		return 0;
17075b6ca027SAl Viro 	}
17085b6ca027SAl Viro 
170931e6b01fSNick Piggin 	nd->root.mnt = NULL;
171031e6b01fSNick Piggin 
171131e6b01fSNick Piggin 	if (*name=='/') {
1712e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
171332a7991bSAl Viro 			lock_rcu_walk();
1714e41f7d4eSAl Viro 			set_root_rcu(nd);
1715e41f7d4eSAl Viro 		} else {
1716e41f7d4eSAl Viro 			set_root(nd);
1717e41f7d4eSAl Viro 			path_get(&nd->root);
1718e41f7d4eSAl Viro 		}
171931e6b01fSNick Piggin 		nd->path = nd->root;
172031e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1721e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
172231e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1723c28cc364SNick Piggin 			unsigned seq;
172431e6b01fSNick Piggin 
172532a7991bSAl Viro 			lock_rcu_walk();
172631e6b01fSNick Piggin 
1727c28cc364SNick Piggin 			do {
1728c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
172931e6b01fSNick Piggin 				nd->path = fs->pwd;
1730c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1731c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1732e41f7d4eSAl Viro 		} else {
1733e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1734e41f7d4eSAl Viro 		}
173531e6b01fSNick Piggin 	} else {
173631e6b01fSNick Piggin 		struct dentry *dentry;
173731e6b01fSNick Piggin 
17381abf0c71SAl Viro 		file = fget_raw_light(dfd, &fput_needed);
173931e6b01fSNick Piggin 		retval = -EBADF;
174031e6b01fSNick Piggin 		if (!file)
174131e6b01fSNick Piggin 			goto out_fail;
174231e6b01fSNick Piggin 
174331e6b01fSNick Piggin 		dentry = file->f_path.dentry;
174431e6b01fSNick Piggin 
1745f52e0c11SAl Viro 		if (*name) {
174631e6b01fSNick Piggin 			retval = -ENOTDIR;
174731e6b01fSNick Piggin 			if (!S_ISDIR(dentry->d_inode->i_mode))
174831e6b01fSNick Piggin 				goto fput_fail;
174931e6b01fSNick Piggin 
17504ad5abb3SAl Viro 			retval = inode_permission(dentry->d_inode, MAY_EXEC);
175131e6b01fSNick Piggin 			if (retval)
175231e6b01fSNick Piggin 				goto fput_fail;
1753f52e0c11SAl Viro 		}
175431e6b01fSNick Piggin 
175531e6b01fSNick Piggin 		nd->path = file->f_path;
1756e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
175731e6b01fSNick Piggin 			if (fput_needed)
175870e9b357SAl Viro 				*fp = file;
1759c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
176032a7991bSAl Viro 			lock_rcu_walk();
17615590ff0dSUlrich Drepper 		} else {
17625dd784d0SJan Blunck 			path_get(&file->f_path);
17635590ff0dSUlrich Drepper 			fput_light(file, fput_needed);
17641da177e4SLinus Torvalds 		}
1765e41f7d4eSAl Viro 	}
1766e41f7d4eSAl Viro 
176731e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
17689b4a9b14SAl Viro 	return 0;
17692dfdd266SJosef 'Jeff' Sipek 
17709b4a9b14SAl Viro fput_fail:
17719b4a9b14SAl Viro 	fput_light(file, fput_needed);
17729b4a9b14SAl Viro out_fail:
17739b4a9b14SAl Viro 	return retval;
17749b4a9b14SAl Viro }
17759b4a9b14SAl Viro 
1776bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1777bd92d7feSAl Viro {
1778bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1779bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1780bd92d7feSAl Viro 
1781bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
1782bd92d7feSAl Viro 	return walk_component(nd, path, &nd->last, nd->last_type,
1783bd92d7feSAl Viro 					nd->flags & LOOKUP_FOLLOW);
1784bd92d7feSAl Viro }
1785bd92d7feSAl Viro 
17869b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1787ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
17889b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
17899b4a9b14SAl Viro {
179070e9b357SAl Viro 	struct file *base = NULL;
1791bd92d7feSAl Viro 	struct path path;
1792bd92d7feSAl Viro 	int err;
179331e6b01fSNick Piggin 
179431e6b01fSNick Piggin 	/*
179531e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
179631e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
179731e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
179831e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
179931e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
180031e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
180131e6b01fSNick Piggin 	 * analogue, foo_rcu().
180231e6b01fSNick Piggin 	 *
180331e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
180431e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
180531e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
180631e6b01fSNick Piggin 	 * be able to complete).
180731e6b01fSNick Piggin 	 */
1808bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1809ee0827cdSAl Viro 
1810bd92d7feSAl Viro 	if (unlikely(err))
1811bd92d7feSAl Viro 		return err;
1812ee0827cdSAl Viro 
1813ee0827cdSAl Viro 	current->total_link_count = 0;
1814bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1815bd92d7feSAl Viro 
1816bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1817bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1818bd92d7feSAl Viro 		while (err > 0) {
1819bd92d7feSAl Viro 			void *cookie;
1820bd92d7feSAl Viro 			struct path link = path;
1821bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1822574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
18236d7b5aaeSAl Viro 			if (err)
18246d7b5aaeSAl Viro 				break;
1825bd92d7feSAl Viro 			err = lookup_last(nd, &path);
1826574197e0SAl Viro 			put_link(nd, &link, cookie);
1827bd92d7feSAl Viro 		}
1828bd92d7feSAl Viro 	}
1829ee0827cdSAl Viro 
18309f1fafeeSAl Viro 	if (!err)
18319f1fafeeSAl Viro 		err = complete_walk(nd);
1832bd92d7feSAl Viro 
1833bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
1834bd92d7feSAl Viro 		if (!nd->inode->i_op->lookup) {
1835bd92d7feSAl Viro 			path_put(&nd->path);
1836bd23a539SAl Viro 			err = -ENOTDIR;
1837bd92d7feSAl Viro 		}
1838bd92d7feSAl Viro 	}
183916c2cd71SAl Viro 
184070e9b357SAl Viro 	if (base)
184170e9b357SAl Viro 		fput(base);
1842ee0827cdSAl Viro 
18435b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
184431e6b01fSNick Piggin 		path_put(&nd->root);
184531e6b01fSNick Piggin 		nd->root.mnt = NULL;
184631e6b01fSNick Piggin 	}
1847bd92d7feSAl Viro 	return err;
184831e6b01fSNick Piggin }
184931e6b01fSNick Piggin 
1850ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1851ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1852ee0827cdSAl Viro {
1853ee0827cdSAl Viro 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1854ee0827cdSAl Viro 	if (unlikely(retval == -ECHILD))
1855ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags, nd);
1856ee0827cdSAl Viro 	if (unlikely(retval == -ESTALE))
1857ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1858ee0827cdSAl Viro 
185931e6b01fSNick Piggin 	if (likely(!retval)) {
186031e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
186131e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
186231e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
186331e6b01fSNick Piggin 		}
186431e6b01fSNick Piggin 	}
1865170aa3d0SUlrich Drepper 	return retval;
18661da177e4SLinus Torvalds }
18671da177e4SLinus Torvalds 
186879714f72SAl Viro /* does lookup, returns the object with parent locked */
186979714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path)
18705590ff0dSUlrich Drepper {
187179714f72SAl Viro 	struct nameidata nd;
187279714f72SAl Viro 	struct dentry *d;
187379714f72SAl Viro 	int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
187479714f72SAl Viro 	if (err)
187579714f72SAl Viro 		return ERR_PTR(err);
187679714f72SAl Viro 	if (nd.last_type != LAST_NORM) {
187779714f72SAl Viro 		path_put(&nd.path);
187879714f72SAl Viro 		return ERR_PTR(-EINVAL);
187979714f72SAl Viro 	}
188079714f72SAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
18811e0ea001SAl Viro 	d = __lookup_hash(&nd.last, nd.path.dentry, 0);
188279714f72SAl Viro 	if (IS_ERR(d)) {
188379714f72SAl Viro 		mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
188479714f72SAl Viro 		path_put(&nd.path);
188579714f72SAl Viro 		return d;
188679714f72SAl Viro 	}
188779714f72SAl Viro 	*path = nd.path;
188879714f72SAl Viro 	return d;
18895590ff0dSUlrich Drepper }
18905590ff0dSUlrich Drepper 
1891d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1892d1811465SAl Viro {
1893d1811465SAl Viro 	struct nameidata nd;
1894d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1895d1811465SAl Viro 	if (!res)
1896d1811465SAl Viro 		*path = nd.path;
1897d1811465SAl Viro 	return res;
1898d1811465SAl Viro }
1899d1811465SAl Viro 
190016f18200SJosef 'Jeff' Sipek /**
190116f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
190216f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
190316f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
190416f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
190516f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
1906e0a01249SAl Viro  * @path: pointer to struct path to fill
190716f18200SJosef 'Jeff' Sipek  */
190816f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
190916f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
1910e0a01249SAl Viro 		    struct path *path)
191116f18200SJosef 'Jeff' Sipek {
1912e0a01249SAl Viro 	struct nameidata nd;
1913e0a01249SAl Viro 	int err;
1914e0a01249SAl Viro 	nd.root.dentry = dentry;
1915e0a01249SAl Viro 	nd.root.mnt = mnt;
1916e0a01249SAl Viro 	BUG_ON(flags & LOOKUP_PARENT);
19175b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1918e0a01249SAl Viro 	err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1919e0a01249SAl Viro 	if (!err)
1920e0a01249SAl Viro 		*path = nd.path;
1921e0a01249SAl Viro 	return err;
192216f18200SJosef 'Jeff' Sipek }
192316f18200SJosef 'Jeff' Sipek 
1924057f6c01SJames Morris /*
1925057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1926057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1927057f6c01SJames Morris  * SMP-safe.
1928057f6c01SJames Morris  */
1929a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
19301da177e4SLinus Torvalds {
193172bd866aSAl Viro 	return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
19321da177e4SLinus Torvalds }
19331da177e4SLinus Torvalds 
1934eead1911SChristoph Hellwig /**
1935a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1936eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1937eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1938eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1939eead1911SChristoph Hellwig  *
1940a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1941a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1942eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1943eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1944eead1911SChristoph Hellwig  */
1945057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1946057f6c01SJames Morris {
1947057f6c01SJames Morris 	struct qstr this;
19486a96ba54SAl Viro 	unsigned int c;
1949cda309deSMiklos Szeredi 	int err;
1950057f6c01SJames Morris 
19512f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
19522f9092e1SDavid Woodhouse 
19536a96ba54SAl Viro 	this.name = name;
19546a96ba54SAl Viro 	this.len = len;
19550145acc2SLinus Torvalds 	this.hash = full_name_hash(name, len);
19566a96ba54SAl Viro 	if (!len)
19576a96ba54SAl Viro 		return ERR_PTR(-EACCES);
19586a96ba54SAl Viro 
19596a96ba54SAl Viro 	while (len--) {
19606a96ba54SAl Viro 		c = *(const unsigned char *)name++;
19616a96ba54SAl Viro 		if (c == '/' || c == '\0')
19626a96ba54SAl Viro 			return ERR_PTR(-EACCES);
19636a96ba54SAl Viro 	}
19645a202bcdSAl Viro 	/*
19655a202bcdSAl Viro 	 * See if the low-level filesystem might want
19665a202bcdSAl Viro 	 * to use its own hash..
19675a202bcdSAl Viro 	 */
19685a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
19695a202bcdSAl Viro 		int err = base->d_op->d_hash(base, base->d_inode, &this);
19705a202bcdSAl Viro 		if (err < 0)
19715a202bcdSAl Viro 			return ERR_PTR(err);
19725a202bcdSAl Viro 	}
1973eead1911SChristoph Hellwig 
1974cda309deSMiklos Szeredi 	err = inode_permission(base->d_inode, MAY_EXEC);
1975cda309deSMiklos Szeredi 	if (err)
1976cda309deSMiklos Szeredi 		return ERR_PTR(err);
1977cda309deSMiklos Szeredi 
197872bd866aSAl Viro 	return __lookup_hash(&this, base, 0);
1979057f6c01SJames Morris }
1980057f6c01SJames Morris 
19811fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
19821fa1e7f6SAndy Whitcroft 		 struct path *path, int *empty)
19831da177e4SLinus Torvalds {
19842d8f3038SAl Viro 	struct nameidata nd;
19851fa1e7f6SAndy Whitcroft 	char *tmp = getname_flags(name, flags, empty);
19861da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
19871da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
19882d8f3038SAl Viro 
19892d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
19902d8f3038SAl Viro 
19912d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
19921da177e4SLinus Torvalds 		putname(tmp);
19932d8f3038SAl Viro 		if (!err)
19942d8f3038SAl Viro 			*path = nd.path;
19951da177e4SLinus Torvalds 	}
19961da177e4SLinus Torvalds 	return err;
19971da177e4SLinus Torvalds }
19981da177e4SLinus Torvalds 
19991fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags,
20001fa1e7f6SAndy Whitcroft 		 struct path *path)
20011fa1e7f6SAndy Whitcroft {
2002f7493e5dSLinus Torvalds 	return user_path_at_empty(dfd, name, flags, path, NULL);
20031fa1e7f6SAndy Whitcroft }
20041fa1e7f6SAndy Whitcroft 
20052ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
20062ad94ae6SAl Viro 			struct nameidata *nd, char **name)
20072ad94ae6SAl Viro {
20082ad94ae6SAl Viro 	char *s = getname(path);
20092ad94ae6SAl Viro 	int error;
20102ad94ae6SAl Viro 
20112ad94ae6SAl Viro 	if (IS_ERR(s))
20122ad94ae6SAl Viro 		return PTR_ERR(s);
20132ad94ae6SAl Viro 
20142ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
20152ad94ae6SAl Viro 	if (error)
20162ad94ae6SAl Viro 		putname(s);
20172ad94ae6SAl Viro 	else
20182ad94ae6SAl Viro 		*name = s;
20192ad94ae6SAl Viro 
20202ad94ae6SAl Viro 	return error;
20212ad94ae6SAl Viro }
20222ad94ae6SAl Viro 
20231da177e4SLinus Torvalds /*
20241da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
20251da177e4SLinus Torvalds  * minimal.
20261da177e4SLinus Torvalds  */
20271da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
20281da177e4SLinus Torvalds {
20298e96e3b7SEric W. Biederman 	kuid_t fsuid = current_fsuid();
2030da9592edSDavid Howells 
20311da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
20321da177e4SLinus Torvalds 		return 0;
20338e96e3b7SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
20341da177e4SLinus Torvalds 		return 0;
20358e96e3b7SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
20361da177e4SLinus Torvalds 		return 0;
20371a48e2acSEric W. Biederman 	return !inode_capable(inode, CAP_FOWNER);
20381da177e4SLinus Torvalds }
20391da177e4SLinus Torvalds 
20401da177e4SLinus Torvalds /*
20411da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
20421da177e4SLinus Torvalds  *  whether the type of victim is right.
20431da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
20441da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
20451da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
20461da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
20471da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
20481da177e4SLinus Torvalds  *	a. be owner of dir, or
20491da177e4SLinus Torvalds  *	b. be owner of victim, or
20501da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
20511da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
20521da177e4SLinus Torvalds  *     links pointing to it.
20531da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
20541da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
20551da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
20561da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
20571da177e4SLinus Torvalds  *     nfs_async_unlink().
20581da177e4SLinus Torvalds  */
2059858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
20601da177e4SLinus Torvalds {
20611da177e4SLinus Torvalds 	int error;
20621da177e4SLinus Torvalds 
20631da177e4SLinus Torvalds 	if (!victim->d_inode)
20641da177e4SLinus Torvalds 		return -ENOENT;
20651da177e4SLinus Torvalds 
20661da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
2067cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
20681da177e4SLinus Torvalds 
2069f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
20701da177e4SLinus Torvalds 	if (error)
20711da177e4SLinus Torvalds 		return error;
20721da177e4SLinus Torvalds 	if (IS_APPEND(dir))
20731da177e4SLinus Torvalds 		return -EPERM;
20741da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
2075f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
20761da177e4SLinus Torvalds 		return -EPERM;
20771da177e4SLinus Torvalds 	if (isdir) {
20781da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
20791da177e4SLinus Torvalds 			return -ENOTDIR;
20801da177e4SLinus Torvalds 		if (IS_ROOT(victim))
20811da177e4SLinus Torvalds 			return -EBUSY;
20821da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
20831da177e4SLinus Torvalds 		return -EISDIR;
20841da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
20851da177e4SLinus Torvalds 		return -ENOENT;
20861da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
20871da177e4SLinus Torvalds 		return -EBUSY;
20881da177e4SLinus Torvalds 	return 0;
20891da177e4SLinus Torvalds }
20901da177e4SLinus Torvalds 
20911da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
20921da177e4SLinus Torvalds  *  dir.
20931da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
20941da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
20951da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
20961da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
20971da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
20981da177e4SLinus Torvalds  */
2099a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
21001da177e4SLinus Torvalds {
21011da177e4SLinus Torvalds 	if (child->d_inode)
21021da177e4SLinus Torvalds 		return -EEXIST;
21031da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
21041da177e4SLinus Torvalds 		return -ENOENT;
2105f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
21061da177e4SLinus Torvalds }
21071da177e4SLinus Torvalds 
21081da177e4SLinus Torvalds /*
21091da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
21101da177e4SLinus Torvalds  */
21111da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
21121da177e4SLinus Torvalds {
21131da177e4SLinus Torvalds 	struct dentry *p;
21141da177e4SLinus Torvalds 
21151da177e4SLinus Torvalds 	if (p1 == p2) {
2116f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
21171da177e4SLinus Torvalds 		return NULL;
21181da177e4SLinus Torvalds 	}
21191da177e4SLinus Torvalds 
2120a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
21211da177e4SLinus Torvalds 
2122e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
2123e2761a11SOGAWA Hirofumi 	if (p) {
2124f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2125f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
21261da177e4SLinus Torvalds 		return p;
21271da177e4SLinus Torvalds 	}
21281da177e4SLinus Torvalds 
2129e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
2130e2761a11SOGAWA Hirofumi 	if (p) {
2131f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2132f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
21331da177e4SLinus Torvalds 		return p;
21341da177e4SLinus Torvalds 	}
21351da177e4SLinus Torvalds 
2136f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2137f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
21381da177e4SLinus Torvalds 	return NULL;
21391da177e4SLinus Torvalds }
21401da177e4SLinus Torvalds 
21411da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
21421da177e4SLinus Torvalds {
21431b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
21441da177e4SLinus Torvalds 	if (p1 != p2) {
21451b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
2146a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
21471da177e4SLinus Torvalds 	}
21481da177e4SLinus Torvalds }
21491da177e4SLinus Torvalds 
21504acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2151312b63fbSAl Viro 		bool want_excl)
21521da177e4SLinus Torvalds {
2153a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
21541da177e4SLinus Torvalds 	if (error)
21551da177e4SLinus Torvalds 		return error;
21561da177e4SLinus Torvalds 
2157acfa4380SAl Viro 	if (!dir->i_op->create)
21581da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
21591da177e4SLinus Torvalds 	mode &= S_IALLUGO;
21601da177e4SLinus Torvalds 	mode |= S_IFREG;
21611da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
21621da177e4SLinus Torvalds 	if (error)
21631da177e4SLinus Torvalds 		return error;
2164312b63fbSAl Viro 	error = dir->i_op->create(dir, dentry, mode, want_excl);
2165a74574aaSStephen Smalley 	if (!error)
2166f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
21671da177e4SLinus Torvalds 	return error;
21681da177e4SLinus Torvalds }
21691da177e4SLinus Torvalds 
217073d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
21711da177e4SLinus Torvalds {
21723fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
21731da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
21741da177e4SLinus Torvalds 	int error;
21751da177e4SLinus Torvalds 
2176bcda7652SAl Viro 	/* O_PATH? */
2177bcda7652SAl Viro 	if (!acc_mode)
2178bcda7652SAl Viro 		return 0;
2179bcda7652SAl Viro 
21801da177e4SLinus Torvalds 	if (!inode)
21811da177e4SLinus Torvalds 		return -ENOENT;
21821da177e4SLinus Torvalds 
2183c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2184c8fe8f30SChristoph Hellwig 	case S_IFLNK:
21851da177e4SLinus Torvalds 		return -ELOOP;
2186c8fe8f30SChristoph Hellwig 	case S_IFDIR:
2187c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
21881da177e4SLinus Torvalds 			return -EISDIR;
2189c8fe8f30SChristoph Hellwig 		break;
2190c8fe8f30SChristoph Hellwig 	case S_IFBLK:
2191c8fe8f30SChristoph Hellwig 	case S_IFCHR:
21923fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
21931da177e4SLinus Torvalds 			return -EACCES;
2194c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
2195c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2196c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
21971da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2198c8fe8f30SChristoph Hellwig 		break;
21994a3fd211SDave Hansen 	}
2200b41572e9SDave Hansen 
22013fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
2202b41572e9SDave Hansen 	if (error)
2203b41572e9SDave Hansen 		return error;
22046146f0d5SMimi Zohar 
22051da177e4SLinus Torvalds 	/*
22061da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
22071da177e4SLinus Torvalds 	 */
22081da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
22098737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
22107715b521SAl Viro 			return -EPERM;
22111da177e4SLinus Torvalds 		if (flag & O_TRUNC)
22127715b521SAl Viro 			return -EPERM;
22131da177e4SLinus Torvalds 	}
22141da177e4SLinus Torvalds 
22151da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
22162e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
22177715b521SAl Viro 		return -EPERM;
22181da177e4SLinus Torvalds 
2219f3c7691eSJ. Bruce Fields 	return 0;
22207715b521SAl Viro }
22217715b521SAl Viro 
2222e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
22237715b521SAl Viro {
2224e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
22257715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
22267715b521SAl Viro 	int error = get_write_access(inode);
22271da177e4SLinus Torvalds 	if (error)
22287715b521SAl Viro 		return error;
22291da177e4SLinus Torvalds 	/*
22301da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
22311da177e4SLinus Torvalds 	 */
22321da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2233be6d3e56SKentaro Takeda 	if (!error)
2234ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
22351da177e4SLinus Torvalds 	if (!error) {
22367715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2237d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2238e1181ee6SJeff Layton 				    filp);
22391da177e4SLinus Torvalds 	}
22401da177e4SLinus Torvalds 	put_write_access(inode);
2241acd0c935SMimi Zohar 	return error;
22421da177e4SLinus Torvalds }
22431da177e4SLinus Torvalds 
2244d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2245d57999e1SDave Hansen {
22468a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
22478a5e929dSAl Viro 		flag--;
2248d57999e1SDave Hansen 	return flag;
2249d57999e1SDave Hansen }
2250d57999e1SDave Hansen 
2251d18e9008SMiklos Szeredi static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2252d18e9008SMiklos Szeredi {
2253d18e9008SMiklos Szeredi 	int error = security_path_mknod(dir, dentry, mode, 0);
2254d18e9008SMiklos Szeredi 	if (error)
2255d18e9008SMiklos Szeredi 		return error;
2256d18e9008SMiklos Szeredi 
2257d18e9008SMiklos Szeredi 	error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2258d18e9008SMiklos Szeredi 	if (error)
2259d18e9008SMiklos Szeredi 		return error;
2260d18e9008SMiklos Szeredi 
2261d18e9008SMiklos Szeredi 	return security_inode_create(dir->dentry->d_inode, dentry, mode);
2262d18e9008SMiklos Szeredi }
2263d18e9008SMiklos Szeredi 
22641acf0af9SDavid Howells /*
22651acf0af9SDavid Howells  * Attempt to atomically look up, create and open a file from a negative
22661acf0af9SDavid Howells  * dentry.
22671acf0af9SDavid Howells  *
22681acf0af9SDavid Howells  * Returns 0 if successful.  The file will have been created and attached to
22691acf0af9SDavid Howells  * @file by the filesystem calling finish_open().
22701acf0af9SDavid Howells  *
22711acf0af9SDavid Howells  * Returns 1 if the file was looked up only or didn't need creating.  The
22721acf0af9SDavid Howells  * caller will need to perform the open themselves.  @path will have been
22731acf0af9SDavid Howells  * updated to point to the new dentry.  This may be negative.
22741acf0af9SDavid Howells  *
22751acf0af9SDavid Howells  * Returns an error code otherwise.
22761acf0af9SDavid Howells  */
22772675a4ebSAl Viro static int atomic_open(struct nameidata *nd, struct dentry *dentry,
227830d90494SAl Viro 			struct path *path, struct file *file,
2279015c3bbcSMiklos Szeredi 			const struct open_flags *op,
228077d660a8SMiklos Szeredi 			bool *want_write, bool need_lookup,
228147237687SAl Viro 			int *opened)
2282d18e9008SMiklos Szeredi {
2283d18e9008SMiklos Szeredi 	struct inode *dir =  nd->path.dentry->d_inode;
2284d18e9008SMiklos Szeredi 	unsigned open_flag = open_to_namei_flags(op->open_flag);
2285d18e9008SMiklos Szeredi 	umode_t mode;
2286d18e9008SMiklos Szeredi 	int error;
2287d18e9008SMiklos Szeredi 	int acc_mode;
2288d18e9008SMiklos Szeredi 	int create_error = 0;
2289d18e9008SMiklos Szeredi 	struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2290d18e9008SMiklos Szeredi 
2291d18e9008SMiklos Szeredi 	BUG_ON(dentry->d_inode);
2292d18e9008SMiklos Szeredi 
2293d18e9008SMiklos Szeredi 	/* Don't create child dentry for a dead directory. */
2294d18e9008SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
22952675a4ebSAl Viro 		error = -ENOENT;
2296d18e9008SMiklos Szeredi 		goto out;
2297d18e9008SMiklos Szeredi 	}
2298d18e9008SMiklos Szeredi 
2299d18e9008SMiklos Szeredi 	mode = op->mode & S_IALLUGO;
2300d18e9008SMiklos Szeredi 	if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2301d18e9008SMiklos Szeredi 		mode &= ~current_umask();
2302d18e9008SMiklos Szeredi 
2303d18e9008SMiklos Szeredi 	if (open_flag & O_EXCL) {
2304d18e9008SMiklos Szeredi 		open_flag &= ~O_TRUNC;
230547237687SAl Viro 		*opened |= FILE_CREATED;
2306d18e9008SMiklos Szeredi 	}
2307d18e9008SMiklos Szeredi 
2308d18e9008SMiklos Szeredi 	/*
2309d18e9008SMiklos Szeredi 	 * Checking write permission is tricky, bacuse we don't know if we are
2310d18e9008SMiklos Szeredi 	 * going to actually need it: O_CREAT opens should work as long as the
2311d18e9008SMiklos Szeredi 	 * file exists.  But checking existence breaks atomicity.  The trick is
2312d18e9008SMiklos Szeredi 	 * to check access and if not granted clear O_CREAT from the flags.
2313d18e9008SMiklos Szeredi 	 *
2314d18e9008SMiklos Szeredi 	 * Another problem is returing the "right" error value (e.g. for an
2315d18e9008SMiklos Szeredi 	 * O_EXCL open we want to return EEXIST not EROFS).
2316d18e9008SMiklos Szeredi 	 */
2317d18e9008SMiklos Szeredi 	if ((open_flag & (O_CREAT | O_TRUNC)) ||
2318d18e9008SMiklos Szeredi 	    (open_flag & O_ACCMODE) != O_RDONLY) {
2319d18e9008SMiklos Szeredi 		error = mnt_want_write(nd->path.mnt);
2320d18e9008SMiklos Szeredi 		if (!error) {
232177d660a8SMiklos Szeredi 			*want_write = true;
2322d18e9008SMiklos Szeredi 		} else if (!(open_flag & O_CREAT)) {
2323d18e9008SMiklos Szeredi 			/*
2324d18e9008SMiklos Szeredi 			 * No O_CREATE -> atomicity not a requirement -> fall
2325d18e9008SMiklos Szeredi 			 * back to lookup + open
2326d18e9008SMiklos Szeredi 			 */
2327d18e9008SMiklos Szeredi 			goto no_open;
2328d18e9008SMiklos Szeredi 		} else if (open_flag & (O_EXCL | O_TRUNC)) {
2329d18e9008SMiklos Szeredi 			/* Fall back and fail with the right error */
2330d18e9008SMiklos Szeredi 			create_error = error;
2331d18e9008SMiklos Szeredi 			goto no_open;
2332d18e9008SMiklos Szeredi 		} else {
2333d18e9008SMiklos Szeredi 			/* No side effects, safe to clear O_CREAT */
2334d18e9008SMiklos Szeredi 			create_error = error;
2335d18e9008SMiklos Szeredi 			open_flag &= ~O_CREAT;
2336d18e9008SMiklos Szeredi 		}
2337d18e9008SMiklos Szeredi 	}
2338d18e9008SMiklos Szeredi 
2339d18e9008SMiklos Szeredi 	if (open_flag & O_CREAT) {
2340d18e9008SMiklos Szeredi 		error = may_o_create(&nd->path, dentry, op->mode);
2341d18e9008SMiklos Szeredi 		if (error) {
2342d18e9008SMiklos Szeredi 			create_error = error;
2343d18e9008SMiklos Szeredi 			if (open_flag & O_EXCL)
2344d18e9008SMiklos Szeredi 				goto no_open;
2345d18e9008SMiklos Szeredi 			open_flag &= ~O_CREAT;
2346d18e9008SMiklos Szeredi 		}
2347d18e9008SMiklos Szeredi 	}
2348d18e9008SMiklos Szeredi 
2349d18e9008SMiklos Szeredi 	if (nd->flags & LOOKUP_DIRECTORY)
2350d18e9008SMiklos Szeredi 		open_flag |= O_DIRECTORY;
2351d18e9008SMiklos Szeredi 
235230d90494SAl Viro 	file->f_path.dentry = DENTRY_NOT_SET;
235330d90494SAl Viro 	file->f_path.mnt = nd->path.mnt;
235430d90494SAl Viro 	error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
235547237687SAl Viro 				      opened);
2356d9585277SAl Viro 	if (error < 0) {
2357d9585277SAl Viro 		if (create_error && error == -ENOENT)
2358d9585277SAl Viro 			error = create_error;
2359d18e9008SMiklos Szeredi 		goto out;
2360d18e9008SMiklos Szeredi 	}
2361d18e9008SMiklos Szeredi 
2362d18e9008SMiklos Szeredi 	acc_mode = op->acc_mode;
236347237687SAl Viro 	if (*opened & FILE_CREATED) {
2364d18e9008SMiklos Szeredi 		fsnotify_create(dir, dentry);
2365d18e9008SMiklos Szeredi 		acc_mode = MAY_OPEN;
2366d18e9008SMiklos Szeredi 	}
2367d18e9008SMiklos Szeredi 
2368d9585277SAl Viro 	if (error) {	/* returned 1, that is */
236930d90494SAl Viro 		if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
23702675a4ebSAl Viro 			error = -EIO;
2371d18e9008SMiklos Szeredi 			goto out;
2372d18e9008SMiklos Szeredi 		}
237330d90494SAl Viro 		if (file->f_path.dentry) {
2374d18e9008SMiklos Szeredi 			dput(dentry);
237530d90494SAl Viro 			dentry = file->f_path.dentry;
2376d18e9008SMiklos Szeredi 		}
2377d18e9008SMiklos Szeredi 		goto looked_up;
2378d18e9008SMiklos Szeredi 	}
2379d18e9008SMiklos Szeredi 
2380d18e9008SMiklos Szeredi 	/*
2381d18e9008SMiklos Szeredi 	 * We didn't have the inode before the open, so check open permission
2382d18e9008SMiklos Szeredi 	 * here.
2383d18e9008SMiklos Szeredi 	 */
23842675a4ebSAl Viro 	error = may_open(&file->f_path, acc_mode, open_flag);
23852675a4ebSAl Viro 	if (error)
23862675a4ebSAl Viro 		fput(file);
2387d18e9008SMiklos Szeredi 
2388d18e9008SMiklos Szeredi out:
2389d18e9008SMiklos Szeredi 	dput(dentry);
23902675a4ebSAl Viro 	return error;
2391d18e9008SMiklos Szeredi 
2392d18e9008SMiklos Szeredi no_open:
2393d18e9008SMiklos Szeredi 	if (need_lookup) {
239472bd866aSAl Viro 		dentry = lookup_real(dir, dentry, nd->flags);
2395d18e9008SMiklos Szeredi 		if (IS_ERR(dentry))
23962675a4ebSAl Viro 			return PTR_ERR(dentry);
2397d18e9008SMiklos Szeredi 
2398d18e9008SMiklos Szeredi 		if (create_error) {
2399d18e9008SMiklos Szeredi 			int open_flag = op->open_flag;
2400d18e9008SMiklos Szeredi 
24012675a4ebSAl Viro 			error = create_error;
2402d18e9008SMiklos Szeredi 			if ((open_flag & O_EXCL)) {
2403d18e9008SMiklos Szeredi 				if (!dentry->d_inode)
2404d18e9008SMiklos Szeredi 					goto out;
2405d18e9008SMiklos Szeredi 			} else if (!dentry->d_inode) {
2406d18e9008SMiklos Szeredi 				goto out;
2407d18e9008SMiklos Szeredi 			} else if ((open_flag & O_TRUNC) &&
2408d18e9008SMiklos Szeredi 				   S_ISREG(dentry->d_inode->i_mode)) {
2409d18e9008SMiklos Szeredi 				goto out;
2410d18e9008SMiklos Szeredi 			}
2411d18e9008SMiklos Szeredi 			/* will fail later, go on to get the right error */
2412d18e9008SMiklos Szeredi 		}
2413d18e9008SMiklos Szeredi 	}
2414d18e9008SMiklos Szeredi looked_up:
2415d18e9008SMiklos Szeredi 	path->dentry = dentry;
2416d18e9008SMiklos Szeredi 	path->mnt = nd->path.mnt;
24172675a4ebSAl Viro 	return 1;
2418d18e9008SMiklos Szeredi }
2419d18e9008SMiklos Szeredi 
242031e6b01fSNick Piggin /*
24211acf0af9SDavid Howells  * Look up and maybe create and open the last component.
2422d58ffd35SMiklos Szeredi  *
2423d58ffd35SMiklos Szeredi  * Must be called with i_mutex held on parent.
2424d58ffd35SMiklos Szeredi  *
24251acf0af9SDavid Howells  * Returns 0 if the file was successfully atomically created (if necessary) and
24261acf0af9SDavid Howells  * opened.  In this case the file will be returned attached to @file.
24271acf0af9SDavid Howells  *
24281acf0af9SDavid Howells  * Returns 1 if the file was not completely opened at this time, though lookups
24291acf0af9SDavid Howells  * and creations will have been performed and the dentry returned in @path will
24301acf0af9SDavid Howells  * be positive upon return if O_CREAT was specified.  If O_CREAT wasn't
24311acf0af9SDavid Howells  * specified then a negative dentry may be returned.
24321acf0af9SDavid Howells  *
24331acf0af9SDavid Howells  * An error code is returned otherwise.
24341acf0af9SDavid Howells  *
24351acf0af9SDavid Howells  * FILE_CREATE will be set in @*opened if the dentry was created and will be
24361acf0af9SDavid Howells  * cleared otherwise prior to returning.
2437d58ffd35SMiklos Szeredi  */
24382675a4ebSAl Viro static int lookup_open(struct nameidata *nd, struct path *path,
243930d90494SAl Viro 			struct file *file,
2440d58ffd35SMiklos Szeredi 			const struct open_flags *op,
244147237687SAl Viro 			bool *want_write, int *opened)
2442d58ffd35SMiklos Szeredi {
2443d58ffd35SMiklos Szeredi 	struct dentry *dir = nd->path.dentry;
244454ef4872SMiklos Szeredi 	struct inode *dir_inode = dir->d_inode;
2445d58ffd35SMiklos Szeredi 	struct dentry *dentry;
2446d58ffd35SMiklos Szeredi 	int error;
244754ef4872SMiklos Szeredi 	bool need_lookup;
2448d58ffd35SMiklos Szeredi 
244947237687SAl Viro 	*opened &= ~FILE_CREATED;
2450201f956eSAl Viro 	dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
2451d58ffd35SMiklos Szeredi 	if (IS_ERR(dentry))
24522675a4ebSAl Viro 		return PTR_ERR(dentry);
2453d58ffd35SMiklos Szeredi 
2454d18e9008SMiklos Szeredi 	/* Cached positive dentry: will open in f_op->open */
2455d18e9008SMiklos Szeredi 	if (!need_lookup && dentry->d_inode)
2456d18e9008SMiklos Szeredi 		goto out_no_open;
2457d18e9008SMiklos Szeredi 
2458d18e9008SMiklos Szeredi 	if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
245930d90494SAl Viro 		return atomic_open(nd, dentry, path, file, op, want_write,
246047237687SAl Viro 				   need_lookup, opened);
2461d18e9008SMiklos Szeredi 	}
2462d18e9008SMiklos Szeredi 
246354ef4872SMiklos Szeredi 	if (need_lookup) {
246454ef4872SMiklos Szeredi 		BUG_ON(dentry->d_inode);
246554ef4872SMiklos Szeredi 
246672bd866aSAl Viro 		dentry = lookup_real(dir_inode, dentry, nd->flags);
246754ef4872SMiklos Szeredi 		if (IS_ERR(dentry))
24682675a4ebSAl Viro 			return PTR_ERR(dentry);
246954ef4872SMiklos Szeredi 	}
247054ef4872SMiklos Szeredi 
2471d58ffd35SMiklos Szeredi 	/* Negative dentry, just create the file */
2472d58ffd35SMiklos Szeredi 	if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2473d58ffd35SMiklos Szeredi 		umode_t mode = op->mode;
2474d58ffd35SMiklos Szeredi 		if (!IS_POSIXACL(dir->d_inode))
2475d58ffd35SMiklos Szeredi 			mode &= ~current_umask();
2476d58ffd35SMiklos Szeredi 		/*
2477d58ffd35SMiklos Szeredi 		 * This write is needed to ensure that a
2478d58ffd35SMiklos Szeredi 		 * rw->ro transition does not occur between
2479d58ffd35SMiklos Szeredi 		 * the time when the file is created and when
2480d58ffd35SMiklos Szeredi 		 * a permanent write count is taken through
2481015c3bbcSMiklos Szeredi 		 * the 'struct file' in finish_open().
2482d58ffd35SMiklos Szeredi 		 */
2483d58ffd35SMiklos Szeredi 		error = mnt_want_write(nd->path.mnt);
2484d58ffd35SMiklos Szeredi 		if (error)
2485d58ffd35SMiklos Szeredi 			goto out_dput;
248677d660a8SMiklos Szeredi 		*want_write = true;
248747237687SAl Viro 		*opened |= FILE_CREATED;
2488d58ffd35SMiklos Szeredi 		error = security_path_mknod(&nd->path, dentry, mode, 0);
2489d58ffd35SMiklos Szeredi 		if (error)
2490d58ffd35SMiklos Szeredi 			goto out_dput;
2491312b63fbSAl Viro 		error = vfs_create(dir->d_inode, dentry, mode,
2492312b63fbSAl Viro 				   nd->flags & LOOKUP_EXCL);
2493d58ffd35SMiklos Szeredi 		if (error)
2494d58ffd35SMiklos Szeredi 			goto out_dput;
2495d58ffd35SMiklos Szeredi 	}
2496d18e9008SMiklos Szeredi out_no_open:
2497d58ffd35SMiklos Szeredi 	path->dentry = dentry;
2498d58ffd35SMiklos Szeredi 	path->mnt = nd->path.mnt;
24992675a4ebSAl Viro 	return 1;
2500d58ffd35SMiklos Szeredi 
2501d58ffd35SMiklos Szeredi out_dput:
2502d58ffd35SMiklos Szeredi 	dput(dentry);
25032675a4ebSAl Viro 	return error;
2504d58ffd35SMiklos Szeredi }
2505d58ffd35SMiklos Szeredi 
2506d58ffd35SMiklos Szeredi /*
2507fe2d35ffSAl Viro  * Handle the last step of open()
250831e6b01fSNick Piggin  */
25092675a4ebSAl Viro static int do_last(struct nameidata *nd, struct path *path,
251030d90494SAl Viro 		   struct file *file, const struct open_flags *op,
251147237687SAl Viro 		   int *opened, const char *pathname)
2512fb1cc555SAl Viro {
2513a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
2514ca344a89SAl Viro 	int open_flag = op->open_flag;
251577d660a8SMiklos Szeredi 	bool will_truncate = (open_flag & O_TRUNC) != 0;
251677d660a8SMiklos Szeredi 	bool want_write = false;
2517bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2518a1eb3315SMiklos Szeredi 	struct inode *inode;
251977d660a8SMiklos Szeredi 	bool symlink_ok = false;
252016b1c1cdSMiklos Szeredi 	struct path save_parent = { .dentry = NULL, .mnt = NULL };
252116b1c1cdSMiklos Szeredi 	bool retried = false;
252216c2cd71SAl Viro 	int error;
2523fb1cc555SAl Viro 
2524c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2525c3e380b0SAl Viro 	nd->flags |= op->intent;
2526c3e380b0SAl Viro 
25271f36f774SAl Viro 	switch (nd->last_type) {
25281f36f774SAl Viro 	case LAST_DOTDOT:
2529176306f5SNeil Brown 	case LAST_DOT:
2530fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2531fe2d35ffSAl Viro 		if (error)
25322675a4ebSAl Viro 			return error;
25331f36f774SAl Viro 		/* fallthrough */
25341f36f774SAl Viro 	case LAST_ROOT:
25359f1fafeeSAl Viro 		error = complete_walk(nd);
253616c2cd71SAl Viro 		if (error)
25372675a4ebSAl Viro 			return error;
2538fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2539ca344a89SAl Viro 		if (open_flag & O_CREAT) {
254016c2cd71SAl Viro 			error = -EISDIR;
25412675a4ebSAl Viro 			goto out;
2542fe2d35ffSAl Viro 		}
2543e83db167SMiklos Szeredi 		goto finish_open;
25441f36f774SAl Viro 	case LAST_BIND:
25459f1fafeeSAl Viro 		error = complete_walk(nd);
254616c2cd71SAl Viro 		if (error)
25472675a4ebSAl Viro 			return error;
25481f36f774SAl Viro 		audit_inode(pathname, dir);
2549e83db167SMiklos Szeredi 		goto finish_open;
25501f36f774SAl Viro 	}
2551a2c36b45SAl Viro 
2552ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2553fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2554fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2555bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
255677d660a8SMiklos Szeredi 			symlink_ok = true;
2557fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2558a1eb3315SMiklos Szeredi 		error = lookup_fast(nd, &nd->last, path, &inode);
255971574865SMiklos Szeredi 		if (likely(!error))
256071574865SMiklos Szeredi 			goto finish_lookup;
256171574865SMiklos Szeredi 
2562ce57dfc1SAl Viro 		if (error < 0)
25632675a4ebSAl Viro 			goto out;
2564a1eb3315SMiklos Szeredi 
256537d7fffcSMiklos Szeredi 		BUG_ON(nd->inode != dir->d_inode);
2566b6183df7SMiklos Szeredi 	} else {
2567fe2d35ffSAl Viro 		/* create side of things */
2568a3fbbde7SAl Viro 		/*
2569b6183df7SMiklos Szeredi 		 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2570b6183df7SMiklos Szeredi 		 * has been cleared when we got to the last component we are
2571b6183df7SMiklos Szeredi 		 * about to look up
2572a3fbbde7SAl Viro 		 */
25739f1fafeeSAl Viro 		error = complete_walk(nd);
25749f1fafeeSAl Viro 		if (error)
25752675a4ebSAl Viro 			return error;
2576fe2d35ffSAl Viro 
2577fe2d35ffSAl Viro 		audit_inode(pathname, dir);
257816c2cd71SAl Viro 		error = -EISDIR;
25791f36f774SAl Viro 		/* trailing slashes? */
258031e6b01fSNick Piggin 		if (nd->last.name[nd->last.len])
25812675a4ebSAl Viro 			goto out;
2582b6183df7SMiklos Szeredi 	}
25831f36f774SAl Viro 
258416b1c1cdSMiklos Szeredi retry_lookup:
2585a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
25862675a4ebSAl Viro 	error = lookup_open(nd, path, file, op, &want_write, opened);
2587fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2588fb1cc555SAl Viro 
25892675a4ebSAl Viro 	if (error <= 0) {
25902675a4ebSAl Viro 		if (error)
2591d58ffd35SMiklos Szeredi 			goto out;
25926c0d46c4SAl Viro 
259347237687SAl Viro 		if ((*opened & FILE_CREATED) ||
25942675a4ebSAl Viro 		    !S_ISREG(file->f_path.dentry->d_inode->i_mode))
259577d660a8SMiklos Szeredi 			will_truncate = false;
2596d18e9008SMiklos Szeredi 
25972675a4ebSAl Viro 		audit_inode(pathname, file->f_path.dentry);
2598d18e9008SMiklos Szeredi 		goto opened;
2599d18e9008SMiklos Szeredi 	}
2600d18e9008SMiklos Szeredi 
260147237687SAl Viro 	if (*opened & FILE_CREATED) {
26029b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2603ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
260477d660a8SMiklos Szeredi 		will_truncate = false;
2605bcda7652SAl Viro 		acc_mode = MAY_OPEN;
2606d58ffd35SMiklos Szeredi 		path_to_nameidata(path, nd);
2607e83db167SMiklos Szeredi 		goto finish_open_created;
2608fb1cc555SAl Viro 	}
2609fb1cc555SAl Viro 
2610fb1cc555SAl Viro 	/*
2611fb1cc555SAl Viro 	 * It already exists.
2612fb1cc555SAl Viro 	 */
2613fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2614fb1cc555SAl Viro 
2615d18e9008SMiklos Szeredi 	/*
2616d18e9008SMiklos Szeredi 	 * If atomic_open() acquired write access it is dropped now due to
2617d18e9008SMiklos Szeredi 	 * possible mount and symlink following (this might be optimized away if
2618d18e9008SMiklos Szeredi 	 * necessary...)
2619d18e9008SMiklos Szeredi 	 */
2620d18e9008SMiklos Szeredi 	if (want_write) {
2621d18e9008SMiklos Szeredi 		mnt_drop_write(nd->path.mnt);
262277d660a8SMiklos Szeredi 		want_write = false;
2623d18e9008SMiklos Szeredi 	}
2624d18e9008SMiklos Szeredi 
2625fb1cc555SAl Viro 	error = -EEXIST;
2626ca344a89SAl Viro 	if (open_flag & O_EXCL)
2627fb1cc555SAl Viro 		goto exit_dput;
2628fb1cc555SAl Viro 
26299875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
26309875cf80SDavid Howells 	if (error < 0)
2631fb1cc555SAl Viro 		goto exit_dput;
2632fb1cc555SAl Viro 
2633a3fbbde7SAl Viro 	if (error)
2634a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
2635a3fbbde7SAl Viro 
2636decf3400SMiklos Szeredi 	BUG_ON(nd->flags & LOOKUP_RCU);
2637decf3400SMiklos Szeredi 	inode = path->dentry->d_inode;
26385f5daac1SMiklos Szeredi finish_lookup:
26395f5daac1SMiklos Szeredi 	/* we _can_ be in RCU mode here */
2640fb1cc555SAl Viro 	error = -ENOENT;
264154c33e7fSMiklos Szeredi 	if (!inode) {
264254c33e7fSMiklos Szeredi 		path_to_nameidata(path, nd);
26432675a4ebSAl Viro 		goto out;
264454c33e7fSMiklos Szeredi 	}
26459e67f361SAl Viro 
2646d45ea867SMiklos Szeredi 	if (should_follow_link(inode, !symlink_ok)) {
2647d45ea867SMiklos Szeredi 		if (nd->flags & LOOKUP_RCU) {
2648d45ea867SMiklos Szeredi 			if (unlikely(unlazy_walk(nd, path->dentry))) {
2649d45ea867SMiklos Szeredi 				error = -ECHILD;
26502675a4ebSAl Viro 				goto out;
2651d45ea867SMiklos Szeredi 			}
2652d45ea867SMiklos Szeredi 		}
2653d45ea867SMiklos Szeredi 		BUG_ON(inode != path->dentry->d_inode);
26542675a4ebSAl Viro 		return 1;
2655d45ea867SMiklos Szeredi 	}
2656fb1cc555SAl Viro 
265716b1c1cdSMiklos Szeredi 	if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
2658fb1cc555SAl Viro 		path_to_nameidata(path, nd);
265916b1c1cdSMiklos Szeredi 	} else {
266016b1c1cdSMiklos Szeredi 		save_parent.dentry = nd->path.dentry;
266116b1c1cdSMiklos Szeredi 		save_parent.mnt = mntget(path->mnt);
266216b1c1cdSMiklos Szeredi 		nd->path.dentry = path->dentry;
266316b1c1cdSMiklos Szeredi 
266416b1c1cdSMiklos Szeredi 	}
2665decf3400SMiklos Szeredi 	nd->inode = inode;
2666a3fbbde7SAl Viro 	/* Why this, you ask?  _Now_ we might have grown LOOKUP_JUMPED... */
2667a3fbbde7SAl Viro 	error = complete_walk(nd);
266816b1c1cdSMiklos Szeredi 	if (error) {
266916b1c1cdSMiklos Szeredi 		path_put(&save_parent);
26702675a4ebSAl Viro 		return error;
267116b1c1cdSMiklos Szeredi 	}
2672fb1cc555SAl Viro 	error = -EISDIR;
2673050ac841SMiklos Szeredi 	if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
26742675a4ebSAl Viro 		goto out;
2675af2f5542SMiklos Szeredi 	error = -ENOTDIR;
2676af2f5542SMiklos Szeredi 	if ((nd->flags & LOOKUP_DIRECTORY) && !nd->inode->i_op->lookup)
26772675a4ebSAl Viro 		goto out;
2678d7fdd7f6SMiklos Szeredi 	audit_inode(pathname, nd->path.dentry);
2679e83db167SMiklos Szeredi finish_open:
26806c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
268177d660a8SMiklos Szeredi 		will_truncate = false;
26826c0d46c4SAl Viro 
26830f9d1a10SAl Viro 	if (will_truncate) {
26840f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
26850f9d1a10SAl Viro 		if (error)
26862675a4ebSAl Viro 			goto out;
268777d660a8SMiklos Szeredi 		want_write = true;
26880f9d1a10SAl Viro 	}
2689e83db167SMiklos Szeredi finish_open_created:
2690bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2691ca344a89SAl Viro 	if (error)
26922675a4ebSAl Viro 		goto out;
269330d90494SAl Viro 	file->f_path.mnt = nd->path.mnt;
269430d90494SAl Viro 	error = finish_open(file, nd->path.dentry, NULL, opened);
269530d90494SAl Viro 	if (error) {
269630d90494SAl Viro 		if (error == -EOPENSTALE)
2697f60dc3dbSMiklos Szeredi 			goto stale_open;
2698015c3bbcSMiklos Szeredi 		goto out;
2699f60dc3dbSMiklos Szeredi 	}
2700a8277b9bSMiklos Szeredi opened:
27012675a4ebSAl Viro 	error = open_check_o_direct(file);
2702015c3bbcSMiklos Szeredi 	if (error)
2703015c3bbcSMiklos Szeredi 		goto exit_fput;
27042675a4ebSAl Viro 	error = ima_file_check(file, op->acc_mode);
2705aa4caadbSMiklos Szeredi 	if (error)
2706aa4caadbSMiklos Szeredi 		goto exit_fput;
2707aa4caadbSMiklos Szeredi 
27080f9d1a10SAl Viro 	if (will_truncate) {
27092675a4ebSAl Viro 		error = handle_truncate(file);
2710aa4caadbSMiklos Szeredi 		if (error)
2711aa4caadbSMiklos Szeredi 			goto exit_fput;
27120f9d1a10SAl Viro 	}
2713ca344a89SAl Viro out:
2714ca344a89SAl Viro 	if (want_write)
27150f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
271616b1c1cdSMiklos Szeredi 	path_put(&save_parent);
2717e276ae67SMiklos Szeredi 	terminate_walk(nd);
27182675a4ebSAl Viro 	return error;
2719fb1cc555SAl Viro 
2720fb1cc555SAl Viro exit_dput:
2721fb1cc555SAl Viro 	path_put_conditional(path, nd);
2722ca344a89SAl Viro 	goto out;
2723015c3bbcSMiklos Szeredi exit_fput:
27242675a4ebSAl Viro 	fput(file);
27252675a4ebSAl Viro 	goto out;
2726015c3bbcSMiklos Szeredi 
2727f60dc3dbSMiklos Szeredi stale_open:
2728f60dc3dbSMiklos Szeredi 	/* If no saved parent or already retried then can't retry */
2729f60dc3dbSMiklos Szeredi 	if (!save_parent.dentry || retried)
2730f60dc3dbSMiklos Szeredi 		goto out;
2731f60dc3dbSMiklos Szeredi 
2732f60dc3dbSMiklos Szeredi 	BUG_ON(save_parent.dentry != dir);
2733f60dc3dbSMiklos Szeredi 	path_put(&nd->path);
2734f60dc3dbSMiklos Szeredi 	nd->path = save_parent;
2735f60dc3dbSMiklos Szeredi 	nd->inode = dir->d_inode;
2736f60dc3dbSMiklos Szeredi 	save_parent.mnt = NULL;
2737f60dc3dbSMiklos Szeredi 	save_parent.dentry = NULL;
2738f60dc3dbSMiklos Szeredi 	if (want_write) {
2739f60dc3dbSMiklos Szeredi 		mnt_drop_write(nd->path.mnt);
2740f60dc3dbSMiklos Szeredi 		want_write = false;
2741f60dc3dbSMiklos Szeredi 	}
2742f60dc3dbSMiklos Szeredi 	retried = true;
2743f60dc3dbSMiklos Szeredi 	goto retry_lookup;
2744fb1cc555SAl Viro }
2745fb1cc555SAl Viro 
274613aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname,
274773d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
27481da177e4SLinus Torvalds {
2749fe2d35ffSAl Viro 	struct file *base = NULL;
275030d90494SAl Viro 	struct file *file;
27519850c056SAl Viro 	struct path path;
275247237687SAl Viro 	int opened = 0;
275313aab428SAl Viro 	int error;
275431e6b01fSNick Piggin 
275530d90494SAl Viro 	file = get_empty_filp();
275630d90494SAl Viro 	if (!file)
275731e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
275831e6b01fSNick Piggin 
275930d90494SAl Viro 	file->f_flags = op->open_flag;
276031e6b01fSNick Piggin 
276173d049a4SAl Viro 	error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
276231e6b01fSNick Piggin 	if (unlikely(error))
27632675a4ebSAl Viro 		goto out;
276431e6b01fSNick Piggin 
2765fe2d35ffSAl Viro 	current->total_link_count = 0;
276673d049a4SAl Viro 	error = link_path_walk(pathname, nd);
276731e6b01fSNick Piggin 	if (unlikely(error))
27682675a4ebSAl Viro 		goto out;
27691da177e4SLinus Torvalds 
27702675a4ebSAl Viro 	error = do_last(nd, &path, file, op, &opened, pathname);
27712675a4ebSAl Viro 	while (unlikely(error > 0)) { /* trailing symlink */
27727b9337aaSNick Piggin 		struct path link = path;
2773def4af30SAl Viro 		void *cookie;
2774574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
277573d049a4SAl Viro 			path_put_conditional(&path, nd);
277673d049a4SAl Viro 			path_put(&nd->path);
27772675a4ebSAl Viro 			error = -ELOOP;
277840b39136SAl Viro 			break;
277940b39136SAl Viro 		}
278073d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
278173d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2782574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
2783c3e380b0SAl Viro 		if (unlikely(error))
27842675a4ebSAl Viro 			break;
27852675a4ebSAl Viro 		error = do_last(nd, &path, file, op, &opened, pathname);
2786574197e0SAl Viro 		put_link(nd, &link, cookie);
2787806b681cSAl Viro 	}
278810fa8e62SAl Viro out:
278973d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
279073d049a4SAl Viro 		path_put(&nd->root);
2791fe2d35ffSAl Viro 	if (base)
2792fe2d35ffSAl Viro 		fput(base);
27932675a4ebSAl Viro 	if (!(opened & FILE_OPENED)) {
27942675a4ebSAl Viro 		BUG_ON(!error);
279530d90494SAl Viro 		put_filp(file);
2796015c3bbcSMiklos Szeredi 	}
27972675a4ebSAl Viro 	if (unlikely(error)) {
27982675a4ebSAl Viro 		if (error == -EOPENSTALE) {
27992675a4ebSAl Viro 			if (flags & LOOKUP_RCU)
28002675a4ebSAl Viro 				error = -ECHILD;
28012675a4ebSAl Viro 			else
28022675a4ebSAl Viro 				error = -ESTALE;
28032675a4ebSAl Viro 		}
28042675a4ebSAl Viro 		file = ERR_PTR(error);
28052675a4ebSAl Viro 	}
28062675a4ebSAl Viro 	return file;
2807de459215SKirill Korotaev }
28081da177e4SLinus Torvalds 
280913aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname,
281013aab428SAl Viro 		const struct open_flags *op, int flags)
281113aab428SAl Viro {
281273d049a4SAl Viro 	struct nameidata nd;
281313aab428SAl Viro 	struct file *filp;
281413aab428SAl Viro 
281573d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
281613aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
281773d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
281813aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
281973d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
282013aab428SAl Viro 	return filp;
282113aab428SAl Viro }
282213aab428SAl Viro 
282373d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
282473d049a4SAl Viro 		const char *name, const struct open_flags *op, int flags)
282573d049a4SAl Viro {
282673d049a4SAl Viro 	struct nameidata nd;
282773d049a4SAl Viro 	struct file *file;
282873d049a4SAl Viro 
282973d049a4SAl Viro 	nd.root.mnt = mnt;
283073d049a4SAl Viro 	nd.root.dentry = dentry;
283173d049a4SAl Viro 
283273d049a4SAl Viro 	flags |= LOOKUP_ROOT;
283373d049a4SAl Viro 
2834bcda7652SAl Viro 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
283573d049a4SAl Viro 		return ERR_PTR(-ELOOP);
283673d049a4SAl Viro 
283773d049a4SAl Viro 	file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
283873d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
283973d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags);
284073d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
284173d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
284273d049a4SAl Viro 	return file;
284373d049a4SAl Viro }
284473d049a4SAl Viro 
2845ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
28461da177e4SLinus Torvalds {
2847c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
2848ed75e95dSAl Viro 	struct nameidata nd;
2849ed75e95dSAl Viro 	int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2850ed75e95dSAl Viro 	if (error)
2851ed75e95dSAl Viro 		return ERR_PTR(error);
28521da177e4SLinus Torvalds 
2853c663e5d8SChristoph Hellwig 	/*
2854c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2855c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2856c663e5d8SChristoph Hellwig 	 */
2857ed75e95dSAl Viro 	if (nd.last_type != LAST_NORM)
2858ed75e95dSAl Viro 		goto out;
2859ed75e95dSAl Viro 	nd.flags &= ~LOOKUP_PARENT;
2860ed75e95dSAl Viro 	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2861c663e5d8SChristoph Hellwig 
2862c663e5d8SChristoph Hellwig 	/*
2863c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2864c663e5d8SChristoph Hellwig 	 */
2865ed75e95dSAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2866ed75e95dSAl Viro 	dentry = lookup_hash(&nd);
28671da177e4SLinus Torvalds 	if (IS_ERR(dentry))
28681da177e4SLinus Torvalds 		goto fail;
2869c663e5d8SChristoph Hellwig 
2870e9baf6e5SAl Viro 	if (dentry->d_inode)
2871e9baf6e5SAl Viro 		goto eexist;
2872c663e5d8SChristoph Hellwig 	/*
2873c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2874c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2875c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2876c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2877c663e5d8SChristoph Hellwig 	 */
2878ed75e95dSAl Viro 	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
28791da177e4SLinus Torvalds 		dput(dentry);
28801da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2881ed75e95dSAl Viro 		goto fail;
2882e9baf6e5SAl Viro 	}
2883ed75e95dSAl Viro 	*path = nd.path;
2884e9baf6e5SAl Viro 	return dentry;
2885e9baf6e5SAl Viro eexist:
2886e9baf6e5SAl Viro 	dput(dentry);
2887e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
28881da177e4SLinus Torvalds fail:
2889dae6ad8fSAl Viro 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2890ed75e95dSAl Viro out:
2891dae6ad8fSAl Viro 	path_put(&nd.path);
2892ed75e95dSAl Viro 	return dentry;
2893dae6ad8fSAl Viro }
2894dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create);
2895dae6ad8fSAl Viro 
2896921a1650SAl Viro void done_path_create(struct path *path, struct dentry *dentry)
2897921a1650SAl Viro {
2898921a1650SAl Viro 	dput(dentry);
2899921a1650SAl Viro 	mutex_unlock(&path->dentry->d_inode->i_mutex);
2900921a1650SAl Viro 	path_put(path);
2901921a1650SAl Viro }
2902921a1650SAl Viro EXPORT_SYMBOL(done_path_create);
2903921a1650SAl Viro 
2904dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2905dae6ad8fSAl Viro {
2906dae6ad8fSAl Viro 	char *tmp = getname(pathname);
2907dae6ad8fSAl Viro 	struct dentry *res;
2908dae6ad8fSAl Viro 	if (IS_ERR(tmp))
2909dae6ad8fSAl Viro 		return ERR_CAST(tmp);
2910dae6ad8fSAl Viro 	res = kern_path_create(dfd, tmp, path, is_dir);
2911dae6ad8fSAl Viro 	putname(tmp);
2912dae6ad8fSAl Viro 	return res;
2913dae6ad8fSAl Viro }
2914dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create);
2915dae6ad8fSAl Viro 
29161a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
29171da177e4SLinus Torvalds {
2918a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
29191da177e4SLinus Torvalds 
29201da177e4SLinus Torvalds 	if (error)
29211da177e4SLinus Torvalds 		return error;
29221da177e4SLinus Torvalds 
2923975d6b39SEric W. Biederman 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
29241da177e4SLinus Torvalds 		return -EPERM;
29251da177e4SLinus Torvalds 
2926acfa4380SAl Viro 	if (!dir->i_op->mknod)
29271da177e4SLinus Torvalds 		return -EPERM;
29281da177e4SLinus Torvalds 
292908ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
293008ce5f16SSerge E. Hallyn 	if (error)
293108ce5f16SSerge E. Hallyn 		return error;
293208ce5f16SSerge E. Hallyn 
29331da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
29341da177e4SLinus Torvalds 	if (error)
29351da177e4SLinus Torvalds 		return error;
29361da177e4SLinus Torvalds 
29371da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2938a74574aaSStephen Smalley 	if (!error)
2939f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
29401da177e4SLinus Torvalds 	return error;
29411da177e4SLinus Torvalds }
29421da177e4SLinus Torvalds 
2943f69aac00SAl Viro static int may_mknod(umode_t mode)
2944463c3197SDave Hansen {
2945463c3197SDave Hansen 	switch (mode & S_IFMT) {
2946463c3197SDave Hansen 	case S_IFREG:
2947463c3197SDave Hansen 	case S_IFCHR:
2948463c3197SDave Hansen 	case S_IFBLK:
2949463c3197SDave Hansen 	case S_IFIFO:
2950463c3197SDave Hansen 	case S_IFSOCK:
2951463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2952463c3197SDave Hansen 		return 0;
2953463c3197SDave Hansen 	case S_IFDIR:
2954463c3197SDave Hansen 		return -EPERM;
2955463c3197SDave Hansen 	default:
2956463c3197SDave Hansen 		return -EINVAL;
2957463c3197SDave Hansen 	}
2958463c3197SDave Hansen }
2959463c3197SDave Hansen 
29608208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
29612e4d0924SHeiko Carstens 		unsigned, dev)
29621da177e4SLinus Torvalds {
29631da177e4SLinus Torvalds 	struct dentry *dentry;
2964dae6ad8fSAl Viro 	struct path path;
2965dae6ad8fSAl Viro 	int error;
29661da177e4SLinus Torvalds 
29671da177e4SLinus Torvalds 	if (S_ISDIR(mode))
29681da177e4SLinus Torvalds 		return -EPERM;
29691da177e4SLinus Torvalds 
2970dae6ad8fSAl Viro 	dentry = user_path_create(dfd, filename, &path, 0);
2971dae6ad8fSAl Viro 	if (IS_ERR(dentry))
2972dae6ad8fSAl Viro 		return PTR_ERR(dentry);
29732ad94ae6SAl Viro 
2974dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
2975ce3b0f8dSAl Viro 		mode &= ~current_umask();
2976463c3197SDave Hansen 	error = may_mknod(mode);
2977463c3197SDave Hansen 	if (error)
2978463c3197SDave Hansen 		goto out_dput;
2979dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
2980463c3197SDave Hansen 	if (error)
2981463c3197SDave Hansen 		goto out_dput;
2982dae6ad8fSAl Viro 	error = security_path_mknod(&path, dentry, mode, dev);
2983be6d3e56SKentaro Takeda 	if (error)
2984be6d3e56SKentaro Takeda 		goto out_drop_write;
29851da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
29861da177e4SLinus Torvalds 		case 0: case S_IFREG:
2987312b63fbSAl Viro 			error = vfs_create(path.dentry->d_inode,dentry,mode,true);
29881da177e4SLinus Torvalds 			break;
29891da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
2990dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
29911da177e4SLinus Torvalds 					new_decode_dev(dev));
29921da177e4SLinus Torvalds 			break;
29931da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
2994dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
29951da177e4SLinus Torvalds 			break;
29961da177e4SLinus Torvalds 	}
2997be6d3e56SKentaro Takeda out_drop_write:
2998dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
2999463c3197SDave Hansen out_dput:
3000921a1650SAl Viro 	done_path_create(&path, dentry);
30011da177e4SLinus Torvalds 
30021da177e4SLinus Torvalds 	return error;
30031da177e4SLinus Torvalds }
30041da177e4SLinus Torvalds 
30058208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
30065590ff0dSUlrich Drepper {
30075590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
30085590ff0dSUlrich Drepper }
30095590ff0dSUlrich Drepper 
301018bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
30111da177e4SLinus Torvalds {
3012a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
30138de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
30141da177e4SLinus Torvalds 
30151da177e4SLinus Torvalds 	if (error)
30161da177e4SLinus Torvalds 		return error;
30171da177e4SLinus Torvalds 
3018acfa4380SAl Viro 	if (!dir->i_op->mkdir)
30191da177e4SLinus Torvalds 		return -EPERM;
30201da177e4SLinus Torvalds 
30211da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
30221da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
30231da177e4SLinus Torvalds 	if (error)
30241da177e4SLinus Torvalds 		return error;
30251da177e4SLinus Torvalds 
30268de52778SAl Viro 	if (max_links && dir->i_nlink >= max_links)
30278de52778SAl Viro 		return -EMLINK;
30288de52778SAl Viro 
30291da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
3030a74574aaSStephen Smalley 	if (!error)
3031f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
30321da177e4SLinus Torvalds 	return error;
30331da177e4SLinus Torvalds }
30341da177e4SLinus Torvalds 
3035a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
30361da177e4SLinus Torvalds {
30376902d925SDave Hansen 	struct dentry *dentry;
3038dae6ad8fSAl Viro 	struct path path;
3039dae6ad8fSAl Viro 	int error;
30401da177e4SLinus Torvalds 
3041dae6ad8fSAl Viro 	dentry = user_path_create(dfd, pathname, &path, 1);
30426902d925SDave Hansen 	if (IS_ERR(dentry))
3043dae6ad8fSAl Viro 		return PTR_ERR(dentry);
30446902d925SDave Hansen 
3045dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3046ce3b0f8dSAl Viro 		mode &= ~current_umask();
3047dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
3048463c3197SDave Hansen 	if (error)
3049463c3197SDave Hansen 		goto out_dput;
3050dae6ad8fSAl Viro 	error = security_path_mkdir(&path, dentry, mode);
3051be6d3e56SKentaro Takeda 	if (error)
3052be6d3e56SKentaro Takeda 		goto out_drop_write;
3053dae6ad8fSAl Viro 	error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3054be6d3e56SKentaro Takeda out_drop_write:
3055dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
3056463c3197SDave Hansen out_dput:
3057921a1650SAl Viro 	done_path_create(&path, dentry);
30581da177e4SLinus Torvalds 	return error;
30591da177e4SLinus Torvalds }
30601da177e4SLinus Torvalds 
3061a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
30625590ff0dSUlrich Drepper {
30635590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
30645590ff0dSUlrich Drepper }
30655590ff0dSUlrich Drepper 
30661da177e4SLinus Torvalds /*
3067a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
3068c0d02594SJ. Bruce Fields  * should have a usage count of 1 if we're the only user of this
3069a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
3070a71905f0SSage Weil  * then we drop the dentry now.
30711da177e4SLinus Torvalds  *
30721da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
30731da177e4SLinus Torvalds  * do a
30741da177e4SLinus Torvalds  *
30751da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
30761da177e4SLinus Torvalds  *		return -EBUSY;
30771da177e4SLinus Torvalds  *
30781da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
30791da177e4SLinus Torvalds  * that is still in use by something else..
30801da177e4SLinus Torvalds  */
30811da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
30821da177e4SLinus Torvalds {
30831da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
30841da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
308564252c75SSage Weil 	if (dentry->d_count == 1)
30861da177e4SLinus Torvalds 		__d_drop(dentry);
30871da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
30881da177e4SLinus Torvalds }
30891da177e4SLinus Torvalds 
30901da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
30911da177e4SLinus Torvalds {
30921da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
30931da177e4SLinus Torvalds 
30941da177e4SLinus Torvalds 	if (error)
30951da177e4SLinus Torvalds 		return error;
30961da177e4SLinus Torvalds 
3097acfa4380SAl Viro 	if (!dir->i_op->rmdir)
30981da177e4SLinus Torvalds 		return -EPERM;
30991da177e4SLinus Torvalds 
31001d2ef590SAl Viro 	dget(dentry);
31011b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
3102912dbc15SSage Weil 
31031da177e4SLinus Torvalds 	error = -EBUSY;
3104912dbc15SSage Weil 	if (d_mountpoint(dentry))
3105912dbc15SSage Weil 		goto out;
3106912dbc15SSage Weil 
31071da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
3108912dbc15SSage Weil 	if (error)
3109912dbc15SSage Weil 		goto out;
3110912dbc15SSage Weil 
31113cebde24SSage Weil 	shrink_dcache_parent(dentry);
31121da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
3113912dbc15SSage Weil 	if (error)
3114912dbc15SSage Weil 		goto out;
3115912dbc15SSage Weil 
31161da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
3117d83c49f3SAl Viro 	dont_mount(dentry);
31181da177e4SLinus Torvalds 
3119912dbc15SSage Weil out:
3120912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
31211d2ef590SAl Viro 	dput(dentry);
3122912dbc15SSage Weil 	if (!error)
3123912dbc15SSage Weil 		d_delete(dentry);
31241da177e4SLinus Torvalds 	return error;
31251da177e4SLinus Torvalds }
31261da177e4SLinus Torvalds 
31275590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
31281da177e4SLinus Torvalds {
31291da177e4SLinus Torvalds 	int error = 0;
31301da177e4SLinus Torvalds 	char * name;
31311da177e4SLinus Torvalds 	struct dentry *dentry;
31321da177e4SLinus Torvalds 	struct nameidata nd;
31331da177e4SLinus Torvalds 
31342ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
31351da177e4SLinus Torvalds 	if (error)
31362ad94ae6SAl Viro 		return error;
31371da177e4SLinus Torvalds 
31381da177e4SLinus Torvalds 	switch(nd.last_type) {
31391da177e4SLinus Torvalds 	case LAST_DOTDOT:
31401da177e4SLinus Torvalds 		error = -ENOTEMPTY;
31411da177e4SLinus Torvalds 		goto exit1;
31421da177e4SLinus Torvalds 	case LAST_DOT:
31431da177e4SLinus Torvalds 		error = -EINVAL;
31441da177e4SLinus Torvalds 		goto exit1;
31451da177e4SLinus Torvalds 	case LAST_ROOT:
31461da177e4SLinus Torvalds 		error = -EBUSY;
31471da177e4SLinus Torvalds 		goto exit1;
31481da177e4SLinus Torvalds 	}
31490612d9fbSOGAWA Hirofumi 
31500612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
31510612d9fbSOGAWA Hirofumi 
31524ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
315349705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
31541da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
31556902d925SDave Hansen 	if (IS_ERR(dentry))
31566902d925SDave Hansen 		goto exit2;
3157e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
3158e6bc45d6STheodore Ts'o 		error = -ENOENT;
3159e6bc45d6STheodore Ts'o 		goto exit3;
3160e6bc45d6STheodore Ts'o 	}
31610622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
31620622753bSDave Hansen 	if (error)
31630622753bSDave Hansen 		goto exit3;
3164be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
3165be6d3e56SKentaro Takeda 	if (error)
3166be6d3e56SKentaro Takeda 		goto exit4;
31674ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
3168be6d3e56SKentaro Takeda exit4:
31690622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
31700622753bSDave Hansen exit3:
31711da177e4SLinus Torvalds 	dput(dentry);
31726902d925SDave Hansen exit2:
31734ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
31741da177e4SLinus Torvalds exit1:
31751d957f9bSJan Blunck 	path_put(&nd.path);
31761da177e4SLinus Torvalds 	putname(name);
31771da177e4SLinus Torvalds 	return error;
31781da177e4SLinus Torvalds }
31791da177e4SLinus Torvalds 
31803cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
31815590ff0dSUlrich Drepper {
31825590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
31835590ff0dSUlrich Drepper }
31845590ff0dSUlrich Drepper 
31851da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
31861da177e4SLinus Torvalds {
31871da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
31881da177e4SLinus Torvalds 
31891da177e4SLinus Torvalds 	if (error)
31901da177e4SLinus Torvalds 		return error;
31911da177e4SLinus Torvalds 
3192acfa4380SAl Viro 	if (!dir->i_op->unlink)
31931da177e4SLinus Torvalds 		return -EPERM;
31941da177e4SLinus Torvalds 
31951b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
31961da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
31971da177e4SLinus Torvalds 		error = -EBUSY;
31981da177e4SLinus Torvalds 	else {
31991da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
3200bec1052eSAl Viro 		if (!error) {
32011da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
3202bec1052eSAl Viro 			if (!error)
3203d83c49f3SAl Viro 				dont_mount(dentry);
3204bec1052eSAl Viro 		}
32051da177e4SLinus Torvalds 	}
32061b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
32071da177e4SLinus Torvalds 
32081da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
32091da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
3210ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
32111da177e4SLinus Torvalds 		d_delete(dentry);
32121da177e4SLinus Torvalds 	}
32130eeca283SRobert Love 
32141da177e4SLinus Torvalds 	return error;
32151da177e4SLinus Torvalds }
32161da177e4SLinus Torvalds 
32171da177e4SLinus Torvalds /*
32181da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
32191b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
32201da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
32211da177e4SLinus Torvalds  * while waiting on the I/O.
32221da177e4SLinus Torvalds  */
32235590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
32241da177e4SLinus Torvalds {
32252ad94ae6SAl Viro 	int error;
32261da177e4SLinus Torvalds 	char *name;
32271da177e4SLinus Torvalds 	struct dentry *dentry;
32281da177e4SLinus Torvalds 	struct nameidata nd;
32291da177e4SLinus Torvalds 	struct inode *inode = NULL;
32301da177e4SLinus Torvalds 
32312ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
32321da177e4SLinus Torvalds 	if (error)
32332ad94ae6SAl Viro 		return error;
32342ad94ae6SAl Viro 
32351da177e4SLinus Torvalds 	error = -EISDIR;
32361da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
32371da177e4SLinus Torvalds 		goto exit1;
32380612d9fbSOGAWA Hirofumi 
32390612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
32400612d9fbSOGAWA Hirofumi 
32414ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
324249705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
32431da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
32441da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
32451da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
324650338b88STörök Edwin 		if (nd.last.name[nd.last.len])
324750338b88STörök Edwin 			goto slashes;
32481da177e4SLinus Torvalds 		inode = dentry->d_inode;
324950338b88STörök Edwin 		if (!inode)
3250e6bc45d6STheodore Ts'o 			goto slashes;
32517de9c6eeSAl Viro 		ihold(inode);
32520622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
32530622753bSDave Hansen 		if (error)
32540622753bSDave Hansen 			goto exit2;
3255be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
3256be6d3e56SKentaro Takeda 		if (error)
3257be6d3e56SKentaro Takeda 			goto exit3;
32584ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
3259be6d3e56SKentaro Takeda exit3:
32600622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
32611da177e4SLinus Torvalds 	exit2:
32621da177e4SLinus Torvalds 		dput(dentry);
32631da177e4SLinus Torvalds 	}
32644ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
32651da177e4SLinus Torvalds 	if (inode)
32661da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
32671da177e4SLinus Torvalds exit1:
32681d957f9bSJan Blunck 	path_put(&nd.path);
32691da177e4SLinus Torvalds 	putname(name);
32701da177e4SLinus Torvalds 	return error;
32711da177e4SLinus Torvalds 
32721da177e4SLinus Torvalds slashes:
32731da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
32741da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
32751da177e4SLinus Torvalds 	goto exit2;
32761da177e4SLinus Torvalds }
32771da177e4SLinus Torvalds 
32782e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
32795590ff0dSUlrich Drepper {
32805590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
32815590ff0dSUlrich Drepper 		return -EINVAL;
32825590ff0dSUlrich Drepper 
32835590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
32845590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
32855590ff0dSUlrich Drepper 
32865590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
32875590ff0dSUlrich Drepper }
32885590ff0dSUlrich Drepper 
32893480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
32905590ff0dSUlrich Drepper {
32915590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
32925590ff0dSUlrich Drepper }
32935590ff0dSUlrich Drepper 
3294db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
32951da177e4SLinus Torvalds {
3296a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
32971da177e4SLinus Torvalds 
32981da177e4SLinus Torvalds 	if (error)
32991da177e4SLinus Torvalds 		return error;
33001da177e4SLinus Torvalds 
3301acfa4380SAl Viro 	if (!dir->i_op->symlink)
33021da177e4SLinus Torvalds 		return -EPERM;
33031da177e4SLinus Torvalds 
33041da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
33051da177e4SLinus Torvalds 	if (error)
33061da177e4SLinus Torvalds 		return error;
33071da177e4SLinus Torvalds 
33081da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
3309a74574aaSStephen Smalley 	if (!error)
3310f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
33111da177e4SLinus Torvalds 	return error;
33121da177e4SLinus Torvalds }
33131da177e4SLinus Torvalds 
33142e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
33152e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
33161da177e4SLinus Torvalds {
33172ad94ae6SAl Viro 	int error;
33181da177e4SLinus Torvalds 	char *from;
33196902d925SDave Hansen 	struct dentry *dentry;
3320dae6ad8fSAl Viro 	struct path path;
33211da177e4SLinus Torvalds 
33221da177e4SLinus Torvalds 	from = getname(oldname);
33231da177e4SLinus Torvalds 	if (IS_ERR(from))
33241da177e4SLinus Torvalds 		return PTR_ERR(from);
33252ad94ae6SAl Viro 
3326dae6ad8fSAl Viro 	dentry = user_path_create(newdfd, newname, &path, 0);
33271da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
33286902d925SDave Hansen 	if (IS_ERR(dentry))
3329dae6ad8fSAl Viro 		goto out_putname;
33306902d925SDave Hansen 
3331dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
333275c3f29dSDave Hansen 	if (error)
333375c3f29dSDave Hansen 		goto out_dput;
3334dae6ad8fSAl Viro 	error = security_path_symlink(&path, dentry, from);
3335be6d3e56SKentaro Takeda 	if (error)
3336be6d3e56SKentaro Takeda 		goto out_drop_write;
3337dae6ad8fSAl Viro 	error = vfs_symlink(path.dentry->d_inode, dentry, from);
3338be6d3e56SKentaro Takeda out_drop_write:
3339dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
334075c3f29dSDave Hansen out_dput:
3341921a1650SAl Viro 	done_path_create(&path, dentry);
33426902d925SDave Hansen out_putname:
33431da177e4SLinus Torvalds 	putname(from);
33441da177e4SLinus Torvalds 	return error;
33451da177e4SLinus Torvalds }
33461da177e4SLinus Torvalds 
33473480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
33485590ff0dSUlrich Drepper {
33495590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
33505590ff0dSUlrich Drepper }
33515590ff0dSUlrich Drepper 
33521da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
33531da177e4SLinus Torvalds {
33541da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
33558de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
33561da177e4SLinus Torvalds 	int error;
33571da177e4SLinus Torvalds 
33581da177e4SLinus Torvalds 	if (!inode)
33591da177e4SLinus Torvalds 		return -ENOENT;
33601da177e4SLinus Torvalds 
3361a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
33621da177e4SLinus Torvalds 	if (error)
33631da177e4SLinus Torvalds 		return error;
33641da177e4SLinus Torvalds 
33651da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
33661da177e4SLinus Torvalds 		return -EXDEV;
33671da177e4SLinus Torvalds 
33681da177e4SLinus Torvalds 	/*
33691da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
33701da177e4SLinus Torvalds 	 */
33711da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
33721da177e4SLinus Torvalds 		return -EPERM;
3373acfa4380SAl Viro 	if (!dir->i_op->link)
33741da177e4SLinus Torvalds 		return -EPERM;
33757e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
33761da177e4SLinus Torvalds 		return -EPERM;
33771da177e4SLinus Torvalds 
33781da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
33791da177e4SLinus Torvalds 	if (error)
33801da177e4SLinus Torvalds 		return error;
33811da177e4SLinus Torvalds 
33827e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
3383aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
3384aae8a97dSAneesh Kumar K.V 	if (inode->i_nlink == 0)
3385aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
33868de52778SAl Viro 	else if (max_links && inode->i_nlink >= max_links)
33878de52778SAl Viro 		error = -EMLINK;
3388aae8a97dSAneesh Kumar K.V 	else
33891da177e4SLinus Torvalds 		error = dir->i_op->link(old_dentry, dir, new_dentry);
33907e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
3391e31e14ecSStephen Smalley 	if (!error)
33927e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
33931da177e4SLinus Torvalds 	return error;
33941da177e4SLinus Torvalds }
33951da177e4SLinus Torvalds 
33961da177e4SLinus Torvalds /*
33971da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
33981da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
33991da177e4SLinus Torvalds  * newname.  --KAB
34001da177e4SLinus Torvalds  *
34011da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
34021da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
34031da177e4SLinus Torvalds  * and other special files.  --ADM
34041da177e4SLinus Torvalds  */
34052e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
34062e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
34071da177e4SLinus Torvalds {
34081da177e4SLinus Torvalds 	struct dentry *new_dentry;
3409dae6ad8fSAl Viro 	struct path old_path, new_path;
341011a7b371SAneesh Kumar K.V 	int how = 0;
34111da177e4SLinus Torvalds 	int error;
34121da177e4SLinus Torvalds 
341311a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3414c04030e1SUlrich Drepper 		return -EINVAL;
341511a7b371SAneesh Kumar K.V 	/*
341611a7b371SAneesh Kumar K.V 	 * To use null names we require CAP_DAC_READ_SEARCH
341711a7b371SAneesh Kumar K.V 	 * This ensures that not everyone will be able to create
341811a7b371SAneesh Kumar K.V 	 * handlink using the passed filedescriptor.
341911a7b371SAneesh Kumar K.V 	 */
342011a7b371SAneesh Kumar K.V 	if (flags & AT_EMPTY_PATH) {
342111a7b371SAneesh Kumar K.V 		if (!capable(CAP_DAC_READ_SEARCH))
342211a7b371SAneesh Kumar K.V 			return -ENOENT;
342311a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
342411a7b371SAneesh Kumar K.V 	}
3425c04030e1SUlrich Drepper 
342611a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
342711a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
342811a7b371SAneesh Kumar K.V 
342911a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
34301da177e4SLinus Torvalds 	if (error)
34312ad94ae6SAl Viro 		return error;
34322ad94ae6SAl Viro 
3433dae6ad8fSAl Viro 	new_dentry = user_path_create(newdfd, newname, &new_path, 0);
34341da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
34356902d925SDave Hansen 	if (IS_ERR(new_dentry))
3436dae6ad8fSAl Viro 		goto out;
3437dae6ad8fSAl Viro 
3438dae6ad8fSAl Viro 	error = -EXDEV;
3439dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt)
3440dae6ad8fSAl Viro 		goto out_dput;
3441dae6ad8fSAl Viro 	error = mnt_want_write(new_path.mnt);
344275c3f29dSDave Hansen 	if (error)
344375c3f29dSDave Hansen 		goto out_dput;
3444dae6ad8fSAl Viro 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
3445be6d3e56SKentaro Takeda 	if (error)
3446be6d3e56SKentaro Takeda 		goto out_drop_write;
3447dae6ad8fSAl Viro 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
3448be6d3e56SKentaro Takeda out_drop_write:
3449dae6ad8fSAl Viro 	mnt_drop_write(new_path.mnt);
345075c3f29dSDave Hansen out_dput:
3451921a1650SAl Viro 	done_path_create(&new_path, new_dentry);
34521da177e4SLinus Torvalds out:
34532d8f3038SAl Viro 	path_put(&old_path);
34541da177e4SLinus Torvalds 
34551da177e4SLinus Torvalds 	return error;
34561da177e4SLinus Torvalds }
34571da177e4SLinus Torvalds 
34583480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
34595590ff0dSUlrich Drepper {
3460c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
34615590ff0dSUlrich Drepper }
34625590ff0dSUlrich Drepper 
34631da177e4SLinus Torvalds /*
34641da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
34651da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
34661da177e4SLinus Torvalds  * Problems:
34671da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
34681da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
34691da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
3470a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
34711da177e4SLinus Torvalds  *	   story.
34721da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
34731b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
34741da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
34751da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
3476a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
34771da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
34781da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
34791da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
3480a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
34811da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
34821da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
34831da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
3484e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
34851b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
34861da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3487c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
34881da177e4SLinus Torvalds  *	   locking].
34891da177e4SLinus Torvalds  */
349075c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
34911da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
34921da177e4SLinus Torvalds {
34931da177e4SLinus Torvalds 	int error = 0;
34949055cba7SSage Weil 	struct inode *target = new_dentry->d_inode;
34958de52778SAl Viro 	unsigned max_links = new_dir->i_sb->s_max_links;
34961da177e4SLinus Torvalds 
34971da177e4SLinus Torvalds 	/*
34981da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
34991da177e4SLinus Torvalds 	 * we'll need to flip '..'.
35001da177e4SLinus Torvalds 	 */
35011da177e4SLinus Torvalds 	if (new_dir != old_dir) {
3502f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
35031da177e4SLinus Torvalds 		if (error)
35041da177e4SLinus Torvalds 			return error;
35051da177e4SLinus Torvalds 	}
35061da177e4SLinus Torvalds 
35071da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
35081da177e4SLinus Torvalds 	if (error)
35091da177e4SLinus Torvalds 		return error;
35101da177e4SLinus Torvalds 
35111d2ef590SAl Viro 	dget(new_dentry);
3512d83c49f3SAl Viro 	if (target)
35131b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
35149055cba7SSage Weil 
35151da177e4SLinus Torvalds 	error = -EBUSY;
35169055cba7SSage Weil 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
35179055cba7SSage Weil 		goto out;
35189055cba7SSage Weil 
35198de52778SAl Viro 	error = -EMLINK;
35208de52778SAl Viro 	if (max_links && !target && new_dir != old_dir &&
35218de52778SAl Viro 	    new_dir->i_nlink >= max_links)
35228de52778SAl Viro 		goto out;
35238de52778SAl Viro 
35243cebde24SSage Weil 	if (target)
35253cebde24SSage Weil 		shrink_dcache_parent(new_dentry);
35261da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
35279055cba7SSage Weil 	if (error)
35289055cba7SSage Weil 		goto out;
35299055cba7SSage Weil 
35301da177e4SLinus Torvalds 	if (target) {
35311da177e4SLinus Torvalds 		target->i_flags |= S_DEAD;
3532d83c49f3SAl Viro 		dont_mount(new_dentry);
3533d83c49f3SAl Viro 	}
35349055cba7SSage Weil out:
35359055cba7SSage Weil 	if (target)
35361b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
35371d2ef590SAl Viro 	dput(new_dentry);
3538e31e14ecSStephen Smalley 	if (!error)
3539349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
35401da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
35411da177e4SLinus Torvalds 	return error;
35421da177e4SLinus Torvalds }
35431da177e4SLinus Torvalds 
354475c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
35451da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
35461da177e4SLinus Torvalds {
354751892bbbSSage Weil 	struct inode *target = new_dentry->d_inode;
35481da177e4SLinus Torvalds 	int error;
35491da177e4SLinus Torvalds 
35501da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
35511da177e4SLinus Torvalds 	if (error)
35521da177e4SLinus Torvalds 		return error;
35531da177e4SLinus Torvalds 
35541da177e4SLinus Torvalds 	dget(new_dentry);
35551da177e4SLinus Torvalds 	if (target)
35561b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
355751892bbbSSage Weil 
35581da177e4SLinus Torvalds 	error = -EBUSY;
355951892bbbSSage Weil 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
356051892bbbSSage Weil 		goto out;
356151892bbbSSage Weil 
35621da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
356351892bbbSSage Weil 	if (error)
356451892bbbSSage Weil 		goto out;
356551892bbbSSage Weil 
3566bec1052eSAl Viro 	if (target)
3567d83c49f3SAl Viro 		dont_mount(new_dentry);
3568349457ccSMark Fasheh 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
35691da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
357051892bbbSSage Weil out:
35711da177e4SLinus Torvalds 	if (target)
35721b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
35731da177e4SLinus Torvalds 	dput(new_dentry);
35741da177e4SLinus Torvalds 	return error;
35751da177e4SLinus Torvalds }
35761da177e4SLinus Torvalds 
35771da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
35781da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
35791da177e4SLinus Torvalds {
35801da177e4SLinus Torvalds 	int error;
35811da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
358259b0df21SEric Paris 	const unsigned char *old_name;
35831da177e4SLinus Torvalds 
35841da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
35851da177e4SLinus Torvalds  		return 0;
35861da177e4SLinus Torvalds 
35871da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
35881da177e4SLinus Torvalds 	if (error)
35891da177e4SLinus Torvalds 		return error;
35901da177e4SLinus Torvalds 
35911da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3592a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
35931da177e4SLinus Torvalds 	else
35941da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
35951da177e4SLinus Torvalds 	if (error)
35961da177e4SLinus Torvalds 		return error;
35971da177e4SLinus Torvalds 
3598acfa4380SAl Viro 	if (!old_dir->i_op->rename)
35991da177e4SLinus Torvalds 		return -EPERM;
36001da177e4SLinus Torvalds 
36010eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
36020eeca283SRobert Love 
36031da177e4SLinus Torvalds 	if (is_dir)
36041da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
36051da177e4SLinus Torvalds 	else
36061da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3607123df294SAl Viro 	if (!error)
3608123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
36095a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
36100eeca283SRobert Love 	fsnotify_oldname_free(old_name);
36110eeca283SRobert Love 
36121da177e4SLinus Torvalds 	return error;
36131da177e4SLinus Torvalds }
36141da177e4SLinus Torvalds 
36152e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
36162e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
36171da177e4SLinus Torvalds {
36181da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
36191da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
36201da177e4SLinus Torvalds 	struct dentry *trap;
36211da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
36222ad94ae6SAl Viro 	char *from;
36232ad94ae6SAl Viro 	char *to;
36242ad94ae6SAl Viro 	int error;
36251da177e4SLinus Torvalds 
36262ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
36271da177e4SLinus Torvalds 	if (error)
36281da177e4SLinus Torvalds 		goto exit;
36291da177e4SLinus Torvalds 
36302ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
36311da177e4SLinus Torvalds 	if (error)
36321da177e4SLinus Torvalds 		goto exit1;
36331da177e4SLinus Torvalds 
36341da177e4SLinus Torvalds 	error = -EXDEV;
36354ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
36361da177e4SLinus Torvalds 		goto exit2;
36371da177e4SLinus Torvalds 
36384ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
36391da177e4SLinus Torvalds 	error = -EBUSY;
36401da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
36411da177e4SLinus Torvalds 		goto exit2;
36421da177e4SLinus Torvalds 
36434ac91378SJan Blunck 	new_dir = newnd.path.dentry;
36441da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
36451da177e4SLinus Torvalds 		goto exit2;
36461da177e4SLinus Torvalds 
36470612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
36480612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
36494e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
36500612d9fbSOGAWA Hirofumi 
36511da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
36521da177e4SLinus Torvalds 
365349705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
36541da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
36551da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
36561da177e4SLinus Torvalds 		goto exit3;
36571da177e4SLinus Torvalds 	/* source must exist */
36581da177e4SLinus Torvalds 	error = -ENOENT;
36591da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
36601da177e4SLinus Torvalds 		goto exit4;
36611da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
36621da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
36631da177e4SLinus Torvalds 		error = -ENOTDIR;
36641da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
36651da177e4SLinus Torvalds 			goto exit4;
36661da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
36671da177e4SLinus Torvalds 			goto exit4;
36681da177e4SLinus Torvalds 	}
36691da177e4SLinus Torvalds 	/* source should not be ancestor of target */
36701da177e4SLinus Torvalds 	error = -EINVAL;
36711da177e4SLinus Torvalds 	if (old_dentry == trap)
36721da177e4SLinus Torvalds 		goto exit4;
367349705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
36741da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
36751da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
36761da177e4SLinus Torvalds 		goto exit4;
36771da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
36781da177e4SLinus Torvalds 	error = -ENOTEMPTY;
36791da177e4SLinus Torvalds 	if (new_dentry == trap)
36801da177e4SLinus Torvalds 		goto exit5;
36811da177e4SLinus Torvalds 
36829079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
36839079b1ebSDave Hansen 	if (error)
36849079b1ebSDave Hansen 		goto exit5;
3685be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3686be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3687be6d3e56SKentaro Takeda 	if (error)
3688be6d3e56SKentaro Takeda 		goto exit6;
36891da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
36901da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3691be6d3e56SKentaro Takeda exit6:
36929079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
36931da177e4SLinus Torvalds exit5:
36941da177e4SLinus Torvalds 	dput(new_dentry);
36951da177e4SLinus Torvalds exit4:
36961da177e4SLinus Torvalds 	dput(old_dentry);
36971da177e4SLinus Torvalds exit3:
36981da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
36991da177e4SLinus Torvalds exit2:
37001d957f9bSJan Blunck 	path_put(&newnd.path);
37012ad94ae6SAl Viro 	putname(to);
37021da177e4SLinus Torvalds exit1:
37031d957f9bSJan Blunck 	path_put(&oldnd.path);
37041da177e4SLinus Torvalds 	putname(from);
37052ad94ae6SAl Viro exit:
37061da177e4SLinus Torvalds 	return error;
37071da177e4SLinus Torvalds }
37081da177e4SLinus Torvalds 
3709a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
37105590ff0dSUlrich Drepper {
37115590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
37125590ff0dSUlrich Drepper }
37135590ff0dSUlrich Drepper 
37141da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
37151da177e4SLinus Torvalds {
37161da177e4SLinus Torvalds 	int len;
37171da177e4SLinus Torvalds 
37181da177e4SLinus Torvalds 	len = PTR_ERR(link);
37191da177e4SLinus Torvalds 	if (IS_ERR(link))
37201da177e4SLinus Torvalds 		goto out;
37211da177e4SLinus Torvalds 
37221da177e4SLinus Torvalds 	len = strlen(link);
37231da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
37241da177e4SLinus Torvalds 		len = buflen;
37251da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
37261da177e4SLinus Torvalds 		len = -EFAULT;
37271da177e4SLinus Torvalds out:
37281da177e4SLinus Torvalds 	return len;
37291da177e4SLinus Torvalds }
37301da177e4SLinus Torvalds 
37311da177e4SLinus Torvalds /*
37321da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
37331da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
37341da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
37351da177e4SLinus Torvalds  */
37361da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
37371da177e4SLinus Torvalds {
37381da177e4SLinus Torvalds 	struct nameidata nd;
3739cc314eefSLinus Torvalds 	void *cookie;
3740694a1764SMarcin Slusarz 	int res;
3741cc314eefSLinus Torvalds 
37421da177e4SLinus Torvalds 	nd.depth = 0;
3743cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3744694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3745694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3746694a1764SMarcin Slusarz 
3747694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
37481da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3749cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3750694a1764SMarcin Slusarz 	return res;
37511da177e4SLinus Torvalds }
37521da177e4SLinus Torvalds 
37531da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
37541da177e4SLinus Torvalds {
37551da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
37561da177e4SLinus Torvalds }
37571da177e4SLinus Torvalds 
37581da177e4SLinus Torvalds /* get the link contents into pagecache */
37591da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
37601da177e4SLinus Torvalds {
3761ebd09abbSDuane Griffin 	char *kaddr;
37621da177e4SLinus Torvalds 	struct page *page;
37631da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3764090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
37651da177e4SLinus Torvalds 	if (IS_ERR(page))
37666fe6900eSNick Piggin 		return (char*)page;
37671da177e4SLinus Torvalds 	*ppage = page;
3768ebd09abbSDuane Griffin 	kaddr = kmap(page);
3769ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3770ebd09abbSDuane Griffin 	return kaddr;
37711da177e4SLinus Torvalds }
37721da177e4SLinus Torvalds 
37731da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
37741da177e4SLinus Torvalds {
37751da177e4SLinus Torvalds 	struct page *page = NULL;
37761da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
37771da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
37781da177e4SLinus Torvalds 	if (page) {
37791da177e4SLinus Torvalds 		kunmap(page);
37801da177e4SLinus Torvalds 		page_cache_release(page);
37811da177e4SLinus Torvalds 	}
37821da177e4SLinus Torvalds 	return res;
37831da177e4SLinus Torvalds }
37841da177e4SLinus Torvalds 
3785cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
37861da177e4SLinus Torvalds {
3787cc314eefSLinus Torvalds 	struct page *page = NULL;
37881da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3789cc314eefSLinus Torvalds 	return page;
37901da177e4SLinus Torvalds }
37911da177e4SLinus Torvalds 
3792cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
37931da177e4SLinus Torvalds {
3794cc314eefSLinus Torvalds 	struct page *page = cookie;
3795cc314eefSLinus Torvalds 
3796cc314eefSLinus Torvalds 	if (page) {
37971da177e4SLinus Torvalds 		kunmap(page);
37981da177e4SLinus Torvalds 		page_cache_release(page);
37991da177e4SLinus Torvalds 	}
38001da177e4SLinus Torvalds }
38011da177e4SLinus Torvalds 
380254566b2cSNick Piggin /*
380354566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
380454566b2cSNick Piggin  */
380554566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
38061da177e4SLinus Torvalds {
38071da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
38080adb25d2SKirill Korotaev 	struct page *page;
3809afddba49SNick Piggin 	void *fsdata;
3810beb497abSDmitriy Monakhov 	int err;
38111da177e4SLinus Torvalds 	char *kaddr;
381254566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
381354566b2cSNick Piggin 	if (nofs)
381454566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
38151da177e4SLinus Torvalds 
38167e53cac4SNeilBrown retry:
3817afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
381854566b2cSNick Piggin 				flags, &page, &fsdata);
38191da177e4SLinus Torvalds 	if (err)
3820afddba49SNick Piggin 		goto fail;
3821afddba49SNick Piggin 
3822e8e3c3d6SCong Wang 	kaddr = kmap_atomic(page);
38231da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
3824e8e3c3d6SCong Wang 	kunmap_atomic(kaddr);
3825afddba49SNick Piggin 
3826afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3827afddba49SNick Piggin 							page, fsdata);
38281da177e4SLinus Torvalds 	if (err < 0)
38291da177e4SLinus Torvalds 		goto fail;
3830afddba49SNick Piggin 	if (err < len-1)
3831afddba49SNick Piggin 		goto retry;
3832afddba49SNick Piggin 
38331da177e4SLinus Torvalds 	mark_inode_dirty(inode);
38341da177e4SLinus Torvalds 	return 0;
38351da177e4SLinus Torvalds fail:
38361da177e4SLinus Torvalds 	return err;
38371da177e4SLinus Torvalds }
38381da177e4SLinus Torvalds 
38390adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
38400adb25d2SKirill Korotaev {
38410adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
384254566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
38430adb25d2SKirill Korotaev }
38440adb25d2SKirill Korotaev 
384592e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
38461da177e4SLinus Torvalds 	.readlink	= generic_readlink,
38471da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
38481da177e4SLinus Torvalds 	.put_link	= page_put_link,
38491da177e4SLinus Torvalds };
38501da177e4SLinus Torvalds 
38512d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
3852cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
38531da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
38541da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
38551da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
38561da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
38571da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
38581da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
38591da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
38601da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
38611da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
38620adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
38631da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
38641da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
3865d1811465SAl Viro EXPORT_SYMBOL(kern_path);
386616f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3867f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
38681da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
38691da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
38701da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
38711da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
38721da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
38731da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
38741da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
38751da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
38761da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
38771da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
38781da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
38791da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
38801da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
38811da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3882