xref: /openbmc/linux/fs/namei.c (revision f7493e5d)
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>
181da177e4SLinus Torvalds #include <linux/module.h>
191da177e4SLinus Torvalds #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/fs.h>
211da177e4SLinus Torvalds #include <linux/namei.h>
221da177e4SLinus Torvalds #include <linux/pagemap.h>
230eeca283SRobert Love #include <linux/fsnotify.h>
241da177e4SLinus Torvalds #include <linux/personality.h>
251da177e4SLinus Torvalds #include <linux/security.h>
266146f0d5SMimi Zohar #include <linux/ima.h>
271da177e4SLinus Torvalds #include <linux/syscalls.h>
281da177e4SLinus Torvalds #include <linux/mount.h>
291da177e4SLinus Torvalds #include <linux/audit.h>
3016f7e0feSRandy Dunlap #include <linux/capability.h>
31834f2a4aSTrond Myklebust #include <linux/file.h>
325590ff0dSUlrich Drepper #include <linux/fcntl.h>
3308ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h>
345ad4e53bSAl Viro #include <linux/fs_struct.h>
35e77819e5SLinus Torvalds #include <linux/posix_acl.h>
361da177e4SLinus Torvalds #include <asm/uaccess.h>
371da177e4SLinus Torvalds 
38e81e3f4dSEric Paris #include "internal.h"
39c7105365SAl Viro #include "mount.h"
40e81e3f4dSEric Paris 
411da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer]
421da177e4SLinus Torvalds  * Fundamental changes in the pathname lookup mechanisms (namei)
431da177e4SLinus Torvalds  * were necessary because of omirr.  The reason is that omirr needs
441da177e4SLinus Torvalds  * to know the _real_ pathname, not the user-supplied one, in case
451da177e4SLinus Torvalds  * of symlinks (and also when transname replacements occur).
461da177e4SLinus Torvalds  *
471da177e4SLinus Torvalds  * The new code replaces the old recursive symlink resolution with
481da177e4SLinus Torvalds  * an iterative one (in case of non-nested symlink chains).  It does
491da177e4SLinus Torvalds  * this with calls to <fs>_follow_link().
501da177e4SLinus Torvalds  * As a side effect, dir_namei(), _namei() and follow_link() are now
511da177e4SLinus Torvalds  * replaced with a single function lookup_dentry() that can handle all
521da177e4SLinus Torvalds  * the special cases of the former code.
531da177e4SLinus Torvalds  *
541da177e4SLinus Torvalds  * With the new dcache, the pathname is stored at each inode, at least as
551da177e4SLinus Torvalds  * long as the refcount of the inode is positive.  As a side effect, the
561da177e4SLinus Torvalds  * size of the dcache depends on the inode cache and thus is dynamic.
571da177e4SLinus Torvalds  *
581da177e4SLinus Torvalds  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
591da177e4SLinus Torvalds  * resolution to correspond with current state of the code.
601da177e4SLinus Torvalds  *
611da177e4SLinus Torvalds  * Note that the symlink resolution is not *completely* iterative.
621da177e4SLinus Torvalds  * There is still a significant amount of tail- and mid- recursion in
631da177e4SLinus Torvalds  * the algorithm.  Also, note that <fs>_readlink() is not used in
641da177e4SLinus Torvalds  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
651da177e4SLinus Torvalds  * may return different results than <fs>_follow_link().  Many virtual
661da177e4SLinus Torvalds  * filesystems (including /proc) exhibit this behavior.
671da177e4SLinus Torvalds  */
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
701da177e4SLinus Torvalds  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
711da177e4SLinus Torvalds  * and the name already exists in form of a symlink, try to create the new
721da177e4SLinus Torvalds  * name indicated by the symlink. The old code always complained that the
731da177e4SLinus Torvalds  * name already exists, due to not following the symlink even if its target
741da177e4SLinus Torvalds  * is nonexistent.  The new semantics affects also mknod() and link() when
7525985edcSLucas De Marchi  * the name is a symlink pointing to a non-existent name.
761da177e4SLinus Torvalds  *
771da177e4SLinus Torvalds  * I don't know which semantics is the right one, since I have no access
781da177e4SLinus Torvalds  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
791da177e4SLinus Torvalds  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
801da177e4SLinus Torvalds  * "old" one. Personally, I think the new semantics is much more logical.
811da177e4SLinus Torvalds  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
821da177e4SLinus Torvalds  * file does succeed in both HP-UX and SunOs, but not in Solaris
831da177e4SLinus Torvalds  * and in the old Linux semantics.
841da177e4SLinus Torvalds  */
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
871da177e4SLinus Torvalds  * semantics.  See the comments in "open_namei" and "do_link" below.
881da177e4SLinus Torvalds  *
891da177e4SLinus Torvalds  * [10-Sep-98 Alan Modra] Another symlink change.
901da177e4SLinus Torvalds  */
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
931da177e4SLinus Torvalds  *	inside the path - always follow.
941da177e4SLinus Torvalds  *	in the last component in creation/removal/renaming - never follow.
951da177e4SLinus Torvalds  *	if LOOKUP_FOLLOW passed - follow.
961da177e4SLinus Torvalds  *	if the pathname has trailing slashes - follow.
971da177e4SLinus Torvalds  *	otherwise - don't follow.
981da177e4SLinus Torvalds  * (applied in that order).
991da177e4SLinus Torvalds  *
1001da177e4SLinus Torvalds  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
1011da177e4SLinus Torvalds  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
1021da177e4SLinus Torvalds  * During the 2.4 we need to fix the userland stuff depending on it -
1031da177e4SLinus Torvalds  * hopefully we will be able to get rid of that wart in 2.5. So far only
1041da177e4SLinus Torvalds  * XEmacs seems to be relying on it...
1051da177e4SLinus Torvalds  */
1061da177e4SLinus Torvalds /*
1071da177e4SLinus Torvalds  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
108a11f3a05SArjan van de Ven  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
1091da177e4SLinus Torvalds  * any extra contention...
1101da177e4SLinus Torvalds  */
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional
1131da177e4SLinus Torvalds  * checking and hopefully speeding things up, we copy filenames to the
1141da177e4SLinus Torvalds  * kernel data space before using them..
1151da177e4SLinus Torvalds  *
1161da177e4SLinus Torvalds  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
1171da177e4SLinus Torvalds  * PATH_MAX includes the nul terminator --RR.
1181da177e4SLinus Torvalds  */
119858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page)
1201da177e4SLinus Torvalds {
1211da177e4SLinus Torvalds 	int retval;
1221da177e4SLinus Torvalds 	unsigned long len = PATH_MAX;
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	if (!segment_eq(get_fs(), KERNEL_DS)) {
1251da177e4SLinus Torvalds 		if ((unsigned long) filename >= TASK_SIZE)
1261da177e4SLinus Torvalds 			return -EFAULT;
1271da177e4SLinus Torvalds 		if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
1281da177e4SLinus Torvalds 			len = TASK_SIZE - (unsigned long) filename;
1291da177e4SLinus Torvalds 	}
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	retval = strncpy_from_user(page, filename, len);
1321da177e4SLinus Torvalds 	if (retval > 0) {
1331da177e4SLinus Torvalds 		if (retval < len)
1341da177e4SLinus Torvalds 			return 0;
1351da177e4SLinus Torvalds 		return -ENAMETOOLONG;
1361da177e4SLinus Torvalds 	} else if (!retval)
1371da177e4SLinus Torvalds 		retval = -ENOENT;
1381da177e4SLinus Torvalds 	return retval;
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds 
1411fa1e7f6SAndy Whitcroft static char *getname_flags(const char __user *filename, int flags, int *empty)
1421da177e4SLinus Torvalds {
1434043cde8SEric Paris 	char *result = __getname();
1444043cde8SEric Paris 	int retval;
1451da177e4SLinus Torvalds 
1464043cde8SEric Paris 	if (!result)
1474043cde8SEric Paris 		return ERR_PTR(-ENOMEM);
1481da177e4SLinus Torvalds 
1494043cde8SEric Paris 	retval = do_getname(filename, result);
1501da177e4SLinus Torvalds 	if (retval < 0) {
1511fa1e7f6SAndy Whitcroft 		if (retval == -ENOENT && empty)
1521fa1e7f6SAndy Whitcroft 			*empty = 1;
153f52e0c11SAl Viro 		if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
1544043cde8SEric Paris 			__putname(result);
1554043cde8SEric Paris 			return ERR_PTR(retval);
1561da177e4SLinus Torvalds 		}
157f52e0c11SAl Viro 	}
1581da177e4SLinus Torvalds 	audit_getname(result);
1591da177e4SLinus Torvalds 	return result;
1601da177e4SLinus Torvalds }
1611da177e4SLinus Torvalds 
162f52e0c11SAl Viro char *getname(const char __user * filename)
163f52e0c11SAl Viro {
164f7493e5dSLinus Torvalds 	return getname_flags(filename, 0, NULL);
165f52e0c11SAl Viro }
166f52e0c11SAl Viro 
1671da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
1681da177e4SLinus Torvalds void putname(const char *name)
1691da177e4SLinus Torvalds {
1705ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
1711da177e4SLinus Torvalds 		audit_putname(name);
1721da177e4SLinus Torvalds 	else
1731da177e4SLinus Torvalds 		__putname(name);
1741da177e4SLinus Torvalds }
1751da177e4SLinus Torvalds EXPORT_SYMBOL(putname);
1761da177e4SLinus Torvalds #endif
1771da177e4SLinus Torvalds 
178e77819e5SLinus Torvalds static int check_acl(struct inode *inode, int mask)
179e77819e5SLinus Torvalds {
18084635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL
181e77819e5SLinus Torvalds 	struct posix_acl *acl;
182e77819e5SLinus Torvalds 
183e77819e5SLinus Torvalds 	if (mask & MAY_NOT_BLOCK) {
1843567866bSAl Viro 		acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
1853567866bSAl Viro 	        if (!acl)
186e77819e5SLinus Torvalds 	                return -EAGAIN;
1873567866bSAl Viro 		/* no ->get_acl() calls in RCU mode... */
1883567866bSAl Viro 		if (acl == ACL_NOT_CACHED)
189e77819e5SLinus Torvalds 			return -ECHILD;
190206b1d09SAri Savolainen 	        return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
191e77819e5SLinus Torvalds 	}
192e77819e5SLinus Torvalds 
193e77819e5SLinus Torvalds 	acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
194e77819e5SLinus Torvalds 
195e77819e5SLinus Torvalds 	/*
1964e34e719SChristoph Hellwig 	 * A filesystem can force a ACL callback by just never filling the
1974e34e719SChristoph Hellwig 	 * ACL cache. But normally you'd fill the cache either at inode
1984e34e719SChristoph Hellwig 	 * instantiation time, or on the first ->get_acl call.
199e77819e5SLinus Torvalds 	 *
2004e34e719SChristoph Hellwig 	 * If the filesystem doesn't have a get_acl() function at all, we'll
2014e34e719SChristoph Hellwig 	 * just create the negative cache entry.
202e77819e5SLinus Torvalds 	 */
203e77819e5SLinus Torvalds 	if (acl == ACL_NOT_CACHED) {
2044e34e719SChristoph Hellwig 	        if (inode->i_op->get_acl) {
2054e34e719SChristoph Hellwig 			acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
2064e34e719SChristoph Hellwig 			if (IS_ERR(acl))
2074e34e719SChristoph Hellwig 				return PTR_ERR(acl);
2084e34e719SChristoph Hellwig 		} else {
209e77819e5SLinus Torvalds 		        set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
210e77819e5SLinus Torvalds 		        return -EAGAIN;
211e77819e5SLinus Torvalds 		}
2124e34e719SChristoph Hellwig 	}
213e77819e5SLinus Torvalds 
214e77819e5SLinus Torvalds 	if (acl) {
215e77819e5SLinus Torvalds 	        int error = posix_acl_permission(inode, acl, mask);
216e77819e5SLinus Torvalds 	        posix_acl_release(acl);
217e77819e5SLinus Torvalds 	        return error;
218e77819e5SLinus Torvalds 	}
21984635d68SLinus Torvalds #endif
220e77819e5SLinus Torvalds 
221e77819e5SLinus Torvalds 	return -EAGAIN;
222e77819e5SLinus Torvalds }
223e77819e5SLinus Torvalds 
2245909ccaaSLinus Torvalds /*
225948409c7SAndreas Gruenbacher  * This does the basic permission checking
2265909ccaaSLinus Torvalds  */
2277e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask)
2285909ccaaSLinus Torvalds {
22926cf46beSLinus Torvalds 	unsigned int mode = inode->i_mode;
2305909ccaaSLinus Torvalds 
231e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
232e795b717SSerge E. Hallyn 		goto other_perms;
233e795b717SSerge E. Hallyn 
23414067ff5SLinus Torvalds 	if (likely(current_fsuid() == inode->i_uid))
2355909ccaaSLinus Torvalds 		mode >>= 6;
2365909ccaaSLinus Torvalds 	else {
237e77819e5SLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
2387e40145eSAl Viro 			int error = check_acl(inode, mask);
2395909ccaaSLinus Torvalds 			if (error != -EAGAIN)
2405909ccaaSLinus Torvalds 				return error;
2415909ccaaSLinus Torvalds 		}
2425909ccaaSLinus Torvalds 
2435909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
2445909ccaaSLinus Torvalds 			mode >>= 3;
2455909ccaaSLinus Torvalds 	}
2465909ccaaSLinus Torvalds 
247e795b717SSerge E. Hallyn other_perms:
2485909ccaaSLinus Torvalds 	/*
2495909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
2505909ccaaSLinus Torvalds 	 */
2519c2c7039SAl Viro 	if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
2525909ccaaSLinus Torvalds 		return 0;
2535909ccaaSLinus Torvalds 	return -EACCES;
2545909ccaaSLinus Torvalds }
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds /**
2571da177e4SLinus Torvalds  * generic_permission -  check for access rights on a Posix-like filesystem
2581da177e4SLinus Torvalds  * @inode:	inode to check access rights for
2598fd90c8dSAndreas Gruenbacher  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
2601da177e4SLinus Torvalds  *
2611da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
2621da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
2631da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
264b74c79e9SNick Piggin  * are used for other things.
265b74c79e9SNick Piggin  *
266b74c79e9SNick Piggin  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
267b74c79e9SNick Piggin  * request cannot be satisfied (eg. requires blocking or too much complexity).
268b74c79e9SNick Piggin  * It would then be called again in ref-walk mode.
2691da177e4SLinus Torvalds  */
2702830ba7fSAl Viro int generic_permission(struct inode *inode, int mask)
2711da177e4SLinus Torvalds {
2725909ccaaSLinus Torvalds 	int ret;
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	/*
275948409c7SAndreas Gruenbacher 	 * Do the basic permission checks.
2761da177e4SLinus Torvalds 	 */
2777e40145eSAl Viro 	ret = acl_permission_check(inode, mask);
2785909ccaaSLinus Torvalds 	if (ret != -EACCES)
2795909ccaaSLinus Torvalds 		return ret;
2801da177e4SLinus Torvalds 
281d594e7ecSAl Viro 	if (S_ISDIR(inode->i_mode)) {
282d594e7ecSAl Viro 		/* DACs are overridable for directories */
283d594e7ecSAl Viro 		if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
284d594e7ecSAl Viro 			return 0;
285d594e7ecSAl Viro 		if (!(mask & MAY_WRITE))
286d594e7ecSAl Viro 			if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
287d594e7ecSAl Viro 				return 0;
288d594e7ecSAl Viro 		return -EACCES;
289d594e7ecSAl Viro 	}
2901da177e4SLinus Torvalds 	/*
2911da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
292d594e7ecSAl Viro 	 * Executable DACs are overridable when there is
293d594e7ecSAl Viro 	 * at least one exec bit set.
2941da177e4SLinus Torvalds 	 */
295d594e7ecSAl Viro 	if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
296e795b717SSerge E. Hallyn 		if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
2971da177e4SLinus Torvalds 			return 0;
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	/*
3001da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
3011da177e4SLinus Torvalds 	 */
3027ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
303d594e7ecSAl Viro 	if (mask == MAY_READ)
304e795b717SSerge E. Hallyn 		if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
3051da177e4SLinus Torvalds 			return 0;
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	return -EACCES;
3081da177e4SLinus Torvalds }
3091da177e4SLinus Torvalds 
3103ddcd056SLinus Torvalds /*
3113ddcd056SLinus Torvalds  * We _really_ want to just do "generic_permission()" without
3123ddcd056SLinus Torvalds  * even looking at the inode->i_op values. So we keep a cache
3133ddcd056SLinus Torvalds  * flag in inode->i_opflags, that says "this has not special
3143ddcd056SLinus Torvalds  * permission function, use the fast case".
3153ddcd056SLinus Torvalds  */
3163ddcd056SLinus Torvalds static inline int do_inode_permission(struct inode *inode, int mask)
3173ddcd056SLinus Torvalds {
3183ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
3193ddcd056SLinus Torvalds 		if (likely(inode->i_op->permission))
3203ddcd056SLinus Torvalds 			return inode->i_op->permission(inode, mask);
3213ddcd056SLinus Torvalds 
3223ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
3233ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
3243ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_FASTPERM;
3253ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
3263ddcd056SLinus Torvalds 	}
3273ddcd056SLinus Torvalds 	return generic_permission(inode, mask);
3283ddcd056SLinus Torvalds }
3293ddcd056SLinus Torvalds 
330cb23beb5SChristoph Hellwig /**
331cb23beb5SChristoph Hellwig  * inode_permission  -  check for access rights to a given inode
332cb23beb5SChristoph Hellwig  * @inode:	inode to check permission on
3338fd90c8dSAndreas Gruenbacher  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
334cb23beb5SChristoph Hellwig  *
335cb23beb5SChristoph Hellwig  * Used to check for read/write/execute permissions on an inode.
336cb23beb5SChristoph Hellwig  * We use "fsuid" for this, letting us set arbitrary permissions
337cb23beb5SChristoph Hellwig  * for filesystem access without changing the "normal" uids which
338cb23beb5SChristoph Hellwig  * are used for other things.
339948409c7SAndreas Gruenbacher  *
340948409c7SAndreas Gruenbacher  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
341cb23beb5SChristoph Hellwig  */
342f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask)
3431da177e4SLinus Torvalds {
344e6305c43SAl Viro 	int retval;
3451da177e4SLinus Torvalds 
3463ddcd056SLinus Torvalds 	if (unlikely(mask & MAY_WRITE)) {
34722590e41SMiklos Szeredi 		umode_t mode = inode->i_mode;
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 		/*
3501da177e4SLinus Torvalds 		 * Nobody gets write access to a read-only fs.
3511da177e4SLinus Torvalds 		 */
3521da177e4SLinus Torvalds 		if (IS_RDONLY(inode) &&
3531da177e4SLinus Torvalds 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
3541da177e4SLinus Torvalds 			return -EROFS;
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 		/*
3571da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
3581da177e4SLinus Torvalds 		 */
3591da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
3601da177e4SLinus Torvalds 			return -EACCES;
3611da177e4SLinus Torvalds 	}
3621da177e4SLinus Torvalds 
3633ddcd056SLinus Torvalds 	retval = do_inode_permission(inode, mask);
3641da177e4SLinus Torvalds 	if (retval)
3651da177e4SLinus Torvalds 		return retval;
3661da177e4SLinus Torvalds 
36708ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
36808ce5f16SSerge E. Hallyn 	if (retval)
36908ce5f16SSerge E. Hallyn 		return retval;
37008ce5f16SSerge E. Hallyn 
371d09ca739SEric Paris 	return security_inode_permission(inode, mask);
3721da177e4SLinus Torvalds }
3731da177e4SLinus Torvalds 
374f4d6ff89SAl Viro /**
3755dd784d0SJan Blunck  * path_get - get a reference to a path
3765dd784d0SJan Blunck  * @path: path to get the reference to
3775dd784d0SJan Blunck  *
3785dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
3795dd784d0SJan Blunck  */
3805dd784d0SJan Blunck void path_get(struct path *path)
3815dd784d0SJan Blunck {
3825dd784d0SJan Blunck 	mntget(path->mnt);
3835dd784d0SJan Blunck 	dget(path->dentry);
3845dd784d0SJan Blunck }
3855dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
3865dd784d0SJan Blunck 
3875dd784d0SJan Blunck /**
3881d957f9bSJan Blunck  * path_put - put a reference to a path
3891d957f9bSJan Blunck  * @path: path to put the reference to
3901d957f9bSJan Blunck  *
3911d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
3921d957f9bSJan Blunck  */
3931d957f9bSJan Blunck void path_put(struct path *path)
3941da177e4SLinus Torvalds {
3951d957f9bSJan Blunck 	dput(path->dentry);
3961d957f9bSJan Blunck 	mntput(path->mnt);
3971da177e4SLinus Torvalds }
3981d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
3991da177e4SLinus Torvalds 
40019660af7SAl Viro /*
40131e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
40219660af7SAl Viro  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
40319660af7SAl Viro  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
40419660af7SAl Viro  * normal reference counts on dentries and vfsmounts to transition to rcu-walk
40519660af7SAl Viro  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
40619660af7SAl Viro  * got stuck, so ref-walk may continue from there. If this is not successful
40719660af7SAl Viro  * (eg. a seqcount has changed), then failure is returned and it's up to caller
40819660af7SAl Viro  * to restart the path walk from the beginning in ref-walk mode.
40931e6b01fSNick Piggin  */
41031e6b01fSNick Piggin 
41131e6b01fSNick Piggin /**
41219660af7SAl Viro  * unlazy_walk - try to switch to ref-walk mode.
41319660af7SAl Viro  * @nd: nameidata pathwalk data
41419660af7SAl Viro  * @dentry: child of nd->path.dentry or NULL
41539191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
41631e6b01fSNick Piggin  *
41719660af7SAl Viro  * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
41819660af7SAl Viro  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
41919660af7SAl Viro  * @nd or NULL.  Must be called from rcu-walk context.
42031e6b01fSNick Piggin  */
42119660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
42231e6b01fSNick Piggin {
42331e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
42431e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
4255b6ca027SAl Viro 	int want_root = 0;
42631e6b01fSNick Piggin 
42731e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
4285b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
4295b6ca027SAl Viro 		want_root = 1;
43031e6b01fSNick Piggin 		spin_lock(&fs->lock);
43131e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
43231e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
43331e6b01fSNick Piggin 			goto err_root;
43431e6b01fSNick Piggin 	}
43531e6b01fSNick Piggin 	spin_lock(&parent->d_lock);
43619660af7SAl Viro 	if (!dentry) {
43719660af7SAl Viro 		if (!__d_rcu_to_refcount(parent, nd->seq))
43819660af7SAl Viro 			goto err_parent;
43919660af7SAl Viro 		BUG_ON(nd->inode != parent->d_inode);
44019660af7SAl Viro 	} else {
44194c0d4ecSAl Viro 		if (dentry->d_parent != parent)
44294c0d4ecSAl Viro 			goto err_parent;
44331e6b01fSNick Piggin 		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
44431e6b01fSNick Piggin 		if (!__d_rcu_to_refcount(dentry, nd->seq))
44519660af7SAl Viro 			goto err_child;
44631e6b01fSNick Piggin 		/*
44719660af7SAl Viro 		 * If the sequence check on the child dentry passed, then
44819660af7SAl Viro 		 * the child has not been removed from its parent. This
44919660af7SAl Viro 		 * means the parent dentry must be valid and able to take
45019660af7SAl Viro 		 * a reference at this point.
45131e6b01fSNick Piggin 		 */
45231e6b01fSNick Piggin 		BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
45331e6b01fSNick Piggin 		BUG_ON(!parent->d_count);
45431e6b01fSNick Piggin 		parent->d_count++;
45531e6b01fSNick Piggin 		spin_unlock(&dentry->d_lock);
45619660af7SAl Viro 	}
45731e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
4585b6ca027SAl Viro 	if (want_root) {
45931e6b01fSNick Piggin 		path_get(&nd->root);
46031e6b01fSNick Piggin 		spin_unlock(&fs->lock);
46131e6b01fSNick Piggin 	}
46231e6b01fSNick Piggin 	mntget(nd->path.mnt);
46331e6b01fSNick Piggin 
46431e6b01fSNick Piggin 	rcu_read_unlock();
46531e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
46631e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
46731e6b01fSNick Piggin 	return 0;
46819660af7SAl Viro 
46919660af7SAl Viro err_child:
47031e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
47119660af7SAl Viro err_parent:
47231e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
47331e6b01fSNick Piggin err_root:
4745b6ca027SAl Viro 	if (want_root)
47531e6b01fSNick Piggin 		spin_unlock(&fs->lock);
47631e6b01fSNick Piggin 	return -ECHILD;
47731e6b01fSNick Piggin }
47831e6b01fSNick Piggin 
47931e6b01fSNick Piggin /**
480834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
481834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
482834f2a4aSTrond Myklebust  */
483834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
484834f2a4aSTrond Myklebust {
4852dab5974SLinus Torvalds 	struct file *file = nd->intent.open.file;
4862dab5974SLinus Torvalds 
4872dab5974SLinus Torvalds 	if (file && !IS_ERR(file)) {
4882dab5974SLinus Torvalds 		if (file->f_path.dentry == NULL)
4892dab5974SLinus Torvalds 			put_filp(file);
490834f2a4aSTrond Myklebust 		else
4912dab5974SLinus Torvalds 			fput(file);
4922dab5974SLinus Torvalds 	}
493834f2a4aSTrond Myklebust }
494834f2a4aSTrond Myklebust 
495f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
49634286d66SNick Piggin {
497f60aef7eSAl Viro 	return dentry->d_op->d_revalidate(dentry, nd);
49834286d66SNick Piggin }
49934286d66SNick Piggin 
5009f1fafeeSAl Viro /**
5019f1fafeeSAl Viro  * complete_walk - successful completion of path walk
5029f1fafeeSAl Viro  * @nd:  pointer nameidata
50339159de2SJeff Layton  *
5049f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
5059f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
5069f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
5079f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
5089f1fafeeSAl Viro  * need to drop nd->path.
50939159de2SJeff Layton  */
5109f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
51139159de2SJeff Layton {
51216c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
51339159de2SJeff Layton 	int status;
51439159de2SJeff Layton 
5159f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
5169f1fafeeSAl Viro 		nd->flags &= ~LOOKUP_RCU;
5179f1fafeeSAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
5189f1fafeeSAl Viro 			nd->root.mnt = NULL;
5199f1fafeeSAl Viro 		spin_lock(&dentry->d_lock);
5209f1fafeeSAl Viro 		if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
5219f1fafeeSAl Viro 			spin_unlock(&dentry->d_lock);
5229f1fafeeSAl Viro 			rcu_read_unlock();
5239f1fafeeSAl Viro 			br_read_unlock(vfsmount_lock);
5249f1fafeeSAl Viro 			return -ECHILD;
5259f1fafeeSAl Viro 		}
5269f1fafeeSAl Viro 		BUG_ON(nd->inode != dentry->d_inode);
5279f1fafeeSAl Viro 		spin_unlock(&dentry->d_lock);
5289f1fafeeSAl Viro 		mntget(nd->path.mnt);
5299f1fafeeSAl Viro 		rcu_read_unlock();
5309f1fafeeSAl Viro 		br_read_unlock(vfsmount_lock);
5319f1fafeeSAl Viro 	}
5329f1fafeeSAl Viro 
53316c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
53439159de2SJeff Layton 		return 0;
53539159de2SJeff Layton 
53616c2cd71SAl Viro 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
53716c2cd71SAl Viro 		return 0;
53816c2cd71SAl Viro 
53916c2cd71SAl Viro 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
54016c2cd71SAl Viro 		return 0;
54116c2cd71SAl Viro 
54216c2cd71SAl Viro 	/* Note: we do not d_invalidate() */
54334286d66SNick Piggin 	status = d_revalidate(dentry, nd);
54439159de2SJeff Layton 	if (status > 0)
54539159de2SJeff Layton 		return 0;
54639159de2SJeff Layton 
54716c2cd71SAl Viro 	if (!status)
54839159de2SJeff Layton 		status = -ESTALE;
54916c2cd71SAl Viro 
5509f1fafeeSAl Viro 	path_put(&nd->path);
55139159de2SJeff Layton 	return status;
55239159de2SJeff Layton }
55339159de2SJeff Layton 
5542a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
5552a737871SAl Viro {
556f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
557f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
5582a737871SAl Viro }
5592a737871SAl Viro 
5606de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
5616de88d72SAl Viro 
56231e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
56331e6b01fSNick Piggin {
56431e6b01fSNick Piggin 	if (!nd->root.mnt) {
56531e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
566c28cc364SNick Piggin 		unsigned seq;
567c28cc364SNick Piggin 
568c28cc364SNick Piggin 		do {
569c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
57031e6b01fSNick Piggin 			nd->root = fs->root;
571c1530019STim Chen 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
572c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
57331e6b01fSNick Piggin 	}
57431e6b01fSNick Piggin }
57531e6b01fSNick Piggin 
576f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
5771da177e4SLinus Torvalds {
57831e6b01fSNick Piggin 	int ret;
57931e6b01fSNick Piggin 
5801da177e4SLinus Torvalds 	if (IS_ERR(link))
5811da177e4SLinus Torvalds 		goto fail;
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	if (*link == '/') {
5842a737871SAl Viro 		set_root(nd);
5851d957f9bSJan Blunck 		path_put(&nd->path);
5862a737871SAl Viro 		nd->path = nd->root;
5872a737871SAl Viro 		path_get(&nd->root);
58816c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
5891da177e4SLinus Torvalds 	}
59031e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
591b4091d5fSChristoph Hellwig 
59231e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
59331e6b01fSNick Piggin 	return ret;
5941da177e4SLinus Torvalds fail:
5951d957f9bSJan Blunck 	path_put(&nd->path);
5961da177e4SLinus Torvalds 	return PTR_ERR(link);
5971da177e4SLinus Torvalds }
5981da177e4SLinus Torvalds 
5991d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
600051d3812SIan Kent {
601051d3812SIan Kent 	dput(path->dentry);
6024ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
603051d3812SIan Kent 		mntput(path->mnt);
604051d3812SIan Kent }
605051d3812SIan Kent 
6067b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
6077b9337aaSNick Piggin 					struct nameidata *nd)
608051d3812SIan Kent {
60931e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
6104ac91378SJan Blunck 		dput(nd->path.dentry);
61131e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
6124ac91378SJan Blunck 			mntput(nd->path.mnt);
6139a229683SHuang Shijie 	}
61431e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
6154ac91378SJan Blunck 	nd->path.dentry = path->dentry;
616051d3812SIan Kent }
617051d3812SIan Kent 
618574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
619574197e0SAl Viro {
620574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
621574197e0SAl Viro 	if (!IS_ERR(cookie) && inode->i_op->put_link)
622574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
623574197e0SAl Viro 	path_put(link);
624574197e0SAl Viro }
625574197e0SAl Viro 
626def4af30SAl Viro static __always_inline int
627574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
6281da177e4SLinus Torvalds {
6291da177e4SLinus Torvalds 	int error;
6307b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
6311da177e4SLinus Torvalds 
632844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
633844a3917SAl Viro 
6340e794589SAl Viro 	if (link->mnt == nd->path.mnt)
6350e794589SAl Viro 		mntget(link->mnt);
6360e794589SAl Viro 
637574197e0SAl Viro 	if (unlikely(current->total_link_count >= 40)) {
638574197e0SAl Viro 		*p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
639574197e0SAl Viro 		path_put(&nd->path);
640574197e0SAl Viro 		return -ELOOP;
641574197e0SAl Viro 	}
642574197e0SAl Viro 	cond_resched();
643574197e0SAl Viro 	current->total_link_count++;
644574197e0SAl Viro 
64568ac1234SAl Viro 	touch_atime(link);
6461da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
647cd4e91d3SAl Viro 
64836f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
64936f3b4f6SAl Viro 	if (error) {
65036f3b4f6SAl Viro 		*p = ERR_PTR(error); /* no ->put_link(), please */
65136f3b4f6SAl Viro 		path_put(&nd->path);
65236f3b4f6SAl Viro 		return error;
65336f3b4f6SAl Viro 	}
65436f3b4f6SAl Viro 
65586acdca1SAl Viro 	nd->last_type = LAST_BIND;
656def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
657def4af30SAl Viro 	error = PTR_ERR(*p);
658def4af30SAl Viro 	if (!IS_ERR(*p)) {
6591da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
660cc314eefSLinus Torvalds 		error = 0;
6611da177e4SLinus Torvalds 		if (s)
6621da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
663bcda7652SAl Viro 		else if (nd->last_type == LAST_BIND) {
66416c2cd71SAl Viro 			nd->flags |= LOOKUP_JUMPED;
665b21041d0SAl Viro 			nd->inode = nd->path.dentry->d_inode;
666b21041d0SAl Viro 			if (nd->inode->i_op->follow_link) {
667bcda7652SAl Viro 				/* stepped on a _really_ weird one */
668bcda7652SAl Viro 				path_put(&nd->path);
669bcda7652SAl Viro 				error = -ELOOP;
670bcda7652SAl Viro 			}
671bcda7652SAl Viro 		}
6721da177e4SLinus Torvalds 	}
6731da177e4SLinus Torvalds 	return error;
6741da177e4SLinus Torvalds }
6751da177e4SLinus Torvalds 
67631e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
67731e6b01fSNick Piggin {
6780714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
6790714a533SAl Viro 	struct mount *parent;
68031e6b01fSNick Piggin 	struct dentry *mountpoint;
68131e6b01fSNick Piggin 
6820714a533SAl Viro 	parent = mnt->mnt_parent;
6830714a533SAl Viro 	if (&parent->mnt == path->mnt)
68431e6b01fSNick Piggin 		return 0;
685a73324daSAl Viro 	mountpoint = mnt->mnt_mountpoint;
68631e6b01fSNick Piggin 	path->dentry = mountpoint;
6870714a533SAl Viro 	path->mnt = &parent->mnt;
68831e6b01fSNick Piggin 	return 1;
68931e6b01fSNick Piggin }
69031e6b01fSNick Piggin 
691bab77ebfSAl Viro int follow_up(struct path *path)
6921da177e4SLinus Torvalds {
6930714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
6940714a533SAl Viro 	struct mount *parent;
6951da177e4SLinus Torvalds 	struct dentry *mountpoint;
69699b7db7bSNick Piggin 
69799b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
6980714a533SAl Viro 	parent = mnt->mnt_parent;
6990714a533SAl Viro 	if (&parent->mnt == path->mnt) {
70099b7db7bSNick Piggin 		br_read_unlock(vfsmount_lock);
7011da177e4SLinus Torvalds 		return 0;
7021da177e4SLinus Torvalds 	}
7030714a533SAl Viro 	mntget(&parent->mnt);
704a73324daSAl Viro 	mountpoint = dget(mnt->mnt_mountpoint);
70599b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
706bab77ebfSAl Viro 	dput(path->dentry);
707bab77ebfSAl Viro 	path->dentry = mountpoint;
708bab77ebfSAl Viro 	mntput(path->mnt);
7090714a533SAl Viro 	path->mnt = &parent->mnt;
7101da177e4SLinus Torvalds 	return 1;
7111da177e4SLinus Torvalds }
7121da177e4SLinus Torvalds 
713b5c84bf6SNick Piggin /*
7149875cf80SDavid Howells  * Perform an automount
7159875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
7169875cf80SDavid Howells  *   were called with.
7171da177e4SLinus Torvalds  */
7189875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
7199875cf80SDavid Howells 			    bool *need_mntput)
72031e6b01fSNick Piggin {
7219875cf80SDavid Howells 	struct vfsmount *mnt;
722ea5b778aSDavid Howells 	int err;
7239875cf80SDavid Howells 
7249875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
7259875cf80SDavid Howells 		return -EREMOTE;
7269875cf80SDavid Howells 
7270ec26fd0SMiklos Szeredi 	/* We don't want to mount if someone's just doing a stat -
7280ec26fd0SMiklos Szeredi 	 * unless they're stat'ing a directory and appended a '/' to
7290ec26fd0SMiklos Szeredi 	 * the name.
7300ec26fd0SMiklos Szeredi 	 *
7310ec26fd0SMiklos Szeredi 	 * We do, however, want to mount if someone wants to open or
7325a30d8a2SDavid Howells 	 * create a file of any type under the mountpoint, wants to
7330ec26fd0SMiklos Szeredi 	 * traverse through the mountpoint or wants to open the
7340ec26fd0SMiklos Szeredi 	 * mounted directory.  Also, autofs may mark negative dentries
7350ec26fd0SMiklos Szeredi 	 * as being automount points.  These will need the attentions
7360ec26fd0SMiklos Szeredi 	 * of the daemon to instantiate them before they can be used.
7375a30d8a2SDavid Howells 	 */
7385a30d8a2SDavid Howells 	if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
739d94c177bSLinus Torvalds 		     LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
7405a30d8a2SDavid Howells 	    path->dentry->d_inode)
7419875cf80SDavid Howells 		return -EISDIR;
7420ec26fd0SMiklos Szeredi 
7439875cf80SDavid Howells 	current->total_link_count++;
7449875cf80SDavid Howells 	if (current->total_link_count >= 40)
7459875cf80SDavid Howells 		return -ELOOP;
7469875cf80SDavid Howells 
7479875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
7489875cf80SDavid Howells 	if (IS_ERR(mnt)) {
7499875cf80SDavid Howells 		/*
7509875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
7519875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
7529875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
7539875cf80SDavid Howells 		 *
7549875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
7559875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
7569875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
7579875cf80SDavid Howells 		 */
75849084c3bSAl Viro 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
7599875cf80SDavid Howells 			return -EREMOTE;
7609875cf80SDavid Howells 		return PTR_ERR(mnt);
76131e6b01fSNick Piggin 	}
762ea5b778aSDavid Howells 
7639875cf80SDavid Howells 	if (!mnt) /* mount collision */
7649875cf80SDavid Howells 		return 0;
7659875cf80SDavid Howells 
7668aef1884SAl Viro 	if (!*need_mntput) {
7678aef1884SAl Viro 		/* lock_mount() may release path->mnt on error */
7688aef1884SAl Viro 		mntget(path->mnt);
7698aef1884SAl Viro 		*need_mntput = true;
7708aef1884SAl Viro 	}
77119a167afSAl Viro 	err = finish_automount(mnt, path);
772ea5b778aSDavid Howells 
773ea5b778aSDavid Howells 	switch (err) {
774ea5b778aSDavid Howells 	case -EBUSY:
775ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
77619a167afSAl Viro 		return 0;
777ea5b778aSDavid Howells 	case 0:
7788aef1884SAl Viro 		path_put(path);
7799875cf80SDavid Howells 		path->mnt = mnt;
7809875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
7819875cf80SDavid Howells 		return 0;
78219a167afSAl Viro 	default:
78319a167afSAl Viro 		return err;
7849875cf80SDavid Howells 	}
78519a167afSAl Viro 
786ea5b778aSDavid Howells }
7879875cf80SDavid Howells 
7889875cf80SDavid Howells /*
7899875cf80SDavid Howells  * Handle a dentry that is managed in some way.
790cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
7919875cf80SDavid Howells  * - Flagged as mountpoint
7929875cf80SDavid Howells  * - Flagged as automount point
7939875cf80SDavid Howells  *
7949875cf80SDavid Howells  * This may only be called in refwalk mode.
7959875cf80SDavid Howells  *
7969875cf80SDavid Howells  * Serialization is taken care of in namespace.c
7979875cf80SDavid Howells  */
7989875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
7999875cf80SDavid Howells {
8008aef1884SAl Viro 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
8019875cf80SDavid Howells 	unsigned managed;
8029875cf80SDavid Howells 	bool need_mntput = false;
8038aef1884SAl Viro 	int ret = 0;
8049875cf80SDavid Howells 
8059875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
8069875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
8079875cf80SDavid Howells 	 * the components of that value change under us */
8089875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
8099875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
8109875cf80SDavid Howells 	       unlikely(managed != 0)) {
811cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
812cc53ce53SDavid Howells 		 * being held. */
813cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
814cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
815cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
8161aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
817cc53ce53SDavid Howells 			if (ret < 0)
8188aef1884SAl Viro 				break;
819cc53ce53SDavid Howells 		}
820cc53ce53SDavid Howells 
8219875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
8229875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
8239875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
8249875cf80SDavid Howells 			if (mounted) {
8259875cf80SDavid Howells 				dput(path->dentry);
8269875cf80SDavid Howells 				if (need_mntput)
827463ffb2eSAl Viro 					mntput(path->mnt);
828463ffb2eSAl Viro 				path->mnt = mounted;
829463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
8309875cf80SDavid Howells 				need_mntput = true;
8319875cf80SDavid Howells 				continue;
832463ffb2eSAl Viro 			}
833463ffb2eSAl Viro 
8349875cf80SDavid Howells 			/* Something is mounted on this dentry in another
8359875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
8369875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
8379875cf80SDavid Howells 			 * vfsmount_lock */
8381da177e4SLinus Torvalds 		}
8399875cf80SDavid Howells 
8409875cf80SDavid Howells 		/* Handle an automount point */
8419875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
8429875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
8439875cf80SDavid Howells 			if (ret < 0)
8448aef1884SAl Viro 				break;
8459875cf80SDavid Howells 			continue;
8469875cf80SDavid Howells 		}
8479875cf80SDavid Howells 
8489875cf80SDavid Howells 		/* We didn't change the current path point */
8499875cf80SDavid Howells 		break;
8509875cf80SDavid Howells 	}
8518aef1884SAl Viro 
8528aef1884SAl Viro 	if (need_mntput && path->mnt == mnt)
8538aef1884SAl Viro 		mntput(path->mnt);
8548aef1884SAl Viro 	if (ret == -EISDIR)
8558aef1884SAl Viro 		ret = 0;
856a3fbbde7SAl Viro 	return ret < 0 ? ret : need_mntput;
8571da177e4SLinus Torvalds }
8581da177e4SLinus Torvalds 
859cc53ce53SDavid Howells int follow_down_one(struct path *path)
8601da177e4SLinus Torvalds {
8611da177e4SLinus Torvalds 	struct vfsmount *mounted;
8621da177e4SLinus Torvalds 
8631c755af4SAl Viro 	mounted = lookup_mnt(path);
8641da177e4SLinus Torvalds 	if (mounted) {
8659393bd07SAl Viro 		dput(path->dentry);
8669393bd07SAl Viro 		mntput(path->mnt);
8679393bd07SAl Viro 		path->mnt = mounted;
8689393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
8691da177e4SLinus Torvalds 		return 1;
8701da177e4SLinus Torvalds 	}
8711da177e4SLinus Torvalds 	return 0;
8721da177e4SLinus Torvalds }
8731da177e4SLinus Torvalds 
87462a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
87562a7375eSIan Kent {
87662a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
87762a7375eSIan Kent 		dentry->d_op->d_manage(dentry, true) < 0);
87862a7375eSIan Kent }
87962a7375eSIan Kent 
8809875cf80SDavid Howells /*
881287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
882287548e4SAl Viro  * we meet a managed dentry that would need blocking.
8839875cf80SDavid Howells  */
8849875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
885287548e4SAl Viro 			       struct inode **inode)
8869875cf80SDavid Howells {
88762a7375eSIan Kent 	for (;;) {
888c7105365SAl Viro 		struct mount *mounted;
88962a7375eSIan Kent 		/*
89062a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
89162a7375eSIan Kent 		 * that wants to block transit.
89262a7375eSIan Kent 		 */
893287548e4SAl Viro 		if (unlikely(managed_dentry_might_block(path->dentry)))
894ab90911fSDavid Howells 			return false;
89562a7375eSIan Kent 
89662a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
89762a7375eSIan Kent 			break;
89862a7375eSIan Kent 
8999875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
9009875cf80SDavid Howells 		if (!mounted)
9019875cf80SDavid Howells 			break;
902c7105365SAl Viro 		path->mnt = &mounted->mnt;
903c7105365SAl Viro 		path->dentry = mounted->mnt.mnt_root;
904a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
9059875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
90659430262SLinus Torvalds 		/*
90759430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
90859430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
90959430262SLinus Torvalds 		 * because a mount-point is always pinned.
91059430262SLinus Torvalds 		 */
91159430262SLinus Torvalds 		*inode = path->dentry->d_inode;
9129875cf80SDavid Howells 	}
9139875cf80SDavid Howells 	return true;
9149875cf80SDavid Howells }
9159875cf80SDavid Howells 
916dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd)
917287548e4SAl Viro {
918dea39376SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
919c7105365SAl Viro 		struct mount *mounted;
920dea39376SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
921287548e4SAl Viro 		if (!mounted)
922287548e4SAl Viro 			break;
923c7105365SAl Viro 		nd->path.mnt = &mounted->mnt;
924c7105365SAl Viro 		nd->path.dentry = mounted->mnt.mnt_root;
925dea39376SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
926287548e4SAl Viro 	}
927287548e4SAl Viro }
928287548e4SAl Viro 
92931e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
93031e6b01fSNick Piggin {
93131e6b01fSNick Piggin 	set_root_rcu(nd);
93231e6b01fSNick Piggin 
93331e6b01fSNick Piggin 	while (1) {
93431e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
93531e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
93631e6b01fSNick Piggin 			break;
93731e6b01fSNick Piggin 		}
93831e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
93931e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
94031e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
94131e6b01fSNick Piggin 			unsigned seq;
94231e6b01fSNick Piggin 
94331e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
94431e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
945ef7562d5SAl Viro 				goto failed;
94631e6b01fSNick Piggin 			nd->path.dentry = parent;
94731e6b01fSNick Piggin 			nd->seq = seq;
94831e6b01fSNick Piggin 			break;
94931e6b01fSNick Piggin 		}
95031e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
95131e6b01fSNick Piggin 			break;
95231e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
95331e6b01fSNick Piggin 	}
954dea39376SAl Viro 	follow_mount_rcu(nd);
955dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
95631e6b01fSNick Piggin 	return 0;
957ef7562d5SAl Viro 
958ef7562d5SAl Viro failed:
959ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
9605b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
961ef7562d5SAl Viro 		nd->root.mnt = NULL;
962ef7562d5SAl Viro 	rcu_read_unlock();
963ef7562d5SAl Viro 	br_read_unlock(vfsmount_lock);
964ef7562d5SAl Viro 	return -ECHILD;
96531e6b01fSNick Piggin }
96631e6b01fSNick Piggin 
9679875cf80SDavid Howells /*
968cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
969cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
970cc53ce53SDavid Howells  * caller is permitted to proceed or not.
971cc53ce53SDavid Howells  */
9727cc90cc3SAl Viro int follow_down(struct path *path)
973cc53ce53SDavid Howells {
974cc53ce53SDavid Howells 	unsigned managed;
975cc53ce53SDavid Howells 	int ret;
976cc53ce53SDavid Howells 
977cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
978cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
979cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
980cc53ce53SDavid Howells 		 * being held.
981cc53ce53SDavid Howells 		 *
982cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
983cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
984cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
985cc53ce53SDavid Howells 		 * superstructure.
986cc53ce53SDavid Howells 		 *
987cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
988cc53ce53SDavid Howells 		 */
989cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
990cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
991cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
992ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
9931aed3e42SAl Viro 				path->dentry, false);
994cc53ce53SDavid Howells 			if (ret < 0)
995cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
996cc53ce53SDavid Howells 		}
997cc53ce53SDavid Howells 
998cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
999cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
1000cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
1001cc53ce53SDavid Howells 			if (!mounted)
1002cc53ce53SDavid Howells 				break;
1003cc53ce53SDavid Howells 			dput(path->dentry);
1004cc53ce53SDavid Howells 			mntput(path->mnt);
1005cc53ce53SDavid Howells 			path->mnt = mounted;
1006cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1007cc53ce53SDavid Howells 			continue;
1008cc53ce53SDavid Howells 		}
1009cc53ce53SDavid Howells 
1010cc53ce53SDavid Howells 		/* Don't handle automount points here */
1011cc53ce53SDavid Howells 		break;
1012cc53ce53SDavid Howells 	}
1013cc53ce53SDavid Howells 	return 0;
1014cc53ce53SDavid Howells }
1015cc53ce53SDavid Howells 
1016cc53ce53SDavid Howells /*
10179875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
10189875cf80SDavid Howells  */
10199875cf80SDavid Howells static void follow_mount(struct path *path)
10209875cf80SDavid Howells {
10219875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
10229875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
10239875cf80SDavid Howells 		if (!mounted)
10249875cf80SDavid Howells 			break;
10259875cf80SDavid Howells 		dput(path->dentry);
10269875cf80SDavid Howells 		mntput(path->mnt);
10279875cf80SDavid Howells 		path->mnt = mounted;
10289875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
10299875cf80SDavid Howells 	}
10309875cf80SDavid Howells }
10319875cf80SDavid Howells 
103231e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
10331da177e4SLinus Torvalds {
10342a737871SAl Viro 	set_root(nd);
1035e518ddb7SAndreas Mohr 
10361da177e4SLinus Torvalds 	while(1) {
10374ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
10381da177e4SLinus Torvalds 
10392a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
10402a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
10411da177e4SLinus Torvalds 			break;
10421da177e4SLinus Torvalds 		}
10434ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
10443088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
10453088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
10461da177e4SLinus Torvalds 			dput(old);
10471da177e4SLinus Torvalds 			break;
10481da177e4SLinus Torvalds 		}
10493088dd70SAl Viro 		if (!follow_up(&nd->path))
10501da177e4SLinus Torvalds 			break;
10511da177e4SLinus Torvalds 	}
105279ed0226SAl Viro 	follow_mount(&nd->path);
105331e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
10541da177e4SLinus Torvalds }
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds /*
1057baa03890SNick Piggin  * Allocate a dentry with name and parent, and perform a parent
1058baa03890SNick Piggin  * directory ->lookup on it. Returns the new dentry, or ERR_PTR
1059baa03890SNick Piggin  * on error. parent->d_inode->i_mutex must be held. d_lookup must
1060baa03890SNick Piggin  * have verified that no child exists while under i_mutex.
1061baa03890SNick Piggin  */
1062baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent,
1063baa03890SNick Piggin 				struct qstr *name, struct nameidata *nd)
1064baa03890SNick Piggin {
1065baa03890SNick Piggin 	struct inode *inode = parent->d_inode;
1066baa03890SNick Piggin 	struct dentry *dentry;
1067baa03890SNick Piggin 	struct dentry *old;
1068baa03890SNick Piggin 
1069baa03890SNick Piggin 	/* Don't create child dentry for a dead directory. */
1070baa03890SNick Piggin 	if (unlikely(IS_DEADDIR(inode)))
1071baa03890SNick Piggin 		return ERR_PTR(-ENOENT);
1072baa03890SNick Piggin 
1073baa03890SNick Piggin 	dentry = d_alloc(parent, name);
1074baa03890SNick Piggin 	if (unlikely(!dentry))
1075baa03890SNick Piggin 		return ERR_PTR(-ENOMEM);
1076baa03890SNick Piggin 
1077baa03890SNick Piggin 	old = inode->i_op->lookup(inode, dentry, nd);
1078baa03890SNick Piggin 	if (unlikely(old)) {
1079baa03890SNick Piggin 		dput(dentry);
1080baa03890SNick Piggin 		dentry = old;
1081baa03890SNick Piggin 	}
1082baa03890SNick Piggin 	return dentry;
1083baa03890SNick Piggin }
1084baa03890SNick Piggin 
1085baa03890SNick Piggin /*
108644396f4bSJosef Bacik  * We already have a dentry, but require a lookup to be performed on the parent
108744396f4bSJosef Bacik  * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error.
108844396f4bSJosef Bacik  * parent->d_inode->i_mutex must be held. d_lookup must have verified that no
108944396f4bSJosef Bacik  * child exists while under i_mutex.
109044396f4bSJosef Bacik  */
109144396f4bSJosef Bacik static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry,
109244396f4bSJosef Bacik 				     struct nameidata *nd)
109344396f4bSJosef Bacik {
109444396f4bSJosef Bacik 	struct inode *inode = parent->d_inode;
109544396f4bSJosef Bacik 	struct dentry *old;
109644396f4bSJosef Bacik 
109744396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
1098e188dc02SMiklos Szeredi 	if (unlikely(IS_DEADDIR(inode))) {
1099e188dc02SMiklos Szeredi 		dput(dentry);
110044396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
1101e188dc02SMiklos Szeredi 	}
110244396f4bSJosef Bacik 
110344396f4bSJosef Bacik 	old = inode->i_op->lookup(inode, dentry, nd);
110444396f4bSJosef Bacik 	if (unlikely(old)) {
110544396f4bSJosef Bacik 		dput(dentry);
110644396f4bSJosef Bacik 		dentry = old;
110744396f4bSJosef Bacik 	}
110844396f4bSJosef Bacik 	return dentry;
110944396f4bSJosef Bacik }
111044396f4bSJosef Bacik 
111144396f4bSJosef Bacik /*
11121da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
11131da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
11141da177e4SLinus Torvalds  *  It _is_ time-critical.
11151da177e4SLinus Torvalds  */
11161da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
111731e6b01fSNick Piggin 			struct path *path, struct inode **inode)
11181da177e4SLinus Torvalds {
11194ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
112031e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
11215a18fff2SAl Viro 	int need_reval = 1;
11225a18fff2SAl Viro 	int status = 1;
11239875cf80SDavid Howells 	int err;
11249875cf80SDavid Howells 
11253cac260aSAl Viro 	/*
1126b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1127b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1128b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1129b04f784eSNick Piggin 	 */
113031e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
113131e6b01fSNick Piggin 		unsigned seq;
113231e6b01fSNick Piggin 		*inode = nd->inode;
113331e6b01fSNick Piggin 		dentry = __d_lookup_rcu(parent, name, &seq, inode);
11345a18fff2SAl Viro 		if (!dentry)
11355a18fff2SAl Viro 			goto unlazy;
11365a18fff2SAl Viro 
113731e6b01fSNick Piggin 		/* Memory barrier in read_seqcount_begin of child is enough */
113831e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
113931e6b01fSNick Piggin 			return -ECHILD;
114031e6b01fSNick Piggin 		nd->seq = seq;
11415a18fff2SAl Viro 
114224643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
11435a18fff2SAl Viro 			status = d_revalidate(dentry, nd);
11445a18fff2SAl Viro 			if (unlikely(status <= 0)) {
11455a18fff2SAl Viro 				if (status != -ECHILD)
11465a18fff2SAl Viro 					need_reval = 0;
11475a18fff2SAl Viro 				goto unlazy;
11485a18fff2SAl Viro 			}
114924643087SAl Viro 		}
115044396f4bSJosef Bacik 		if (unlikely(d_need_lookup(dentry)))
115144396f4bSJosef Bacik 			goto unlazy;
115231e6b01fSNick Piggin 		path->mnt = mnt;
115331e6b01fSNick Piggin 		path->dentry = dentry;
1154d6e9bd25SAl Viro 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1155d6e9bd25SAl Viro 			goto unlazy;
1156d6e9bd25SAl Viro 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1157d6e9bd25SAl Viro 			goto unlazy;
11589875cf80SDavid Howells 		return 0;
11595a18fff2SAl Viro unlazy:
116019660af7SAl Viro 		if (unlazy_walk(nd, dentry))
11615a18fff2SAl Viro 			return -ECHILD;
11625a18fff2SAl Viro 	} else {
116331e6b01fSNick Piggin 		dentry = __d_lookup(parent, name);
116424643087SAl Viro 	}
11655a18fff2SAl Viro 
116644396f4bSJosef Bacik 	if (dentry && unlikely(d_need_lookup(dentry))) {
116744396f4bSJosef Bacik 		dput(dentry);
116844396f4bSJosef Bacik 		dentry = NULL;
116944396f4bSJosef Bacik 	}
11705a18fff2SAl Viro retry:
11715a18fff2SAl Viro 	if (unlikely(!dentry)) {
11725a18fff2SAl Viro 		struct inode *dir = parent->d_inode;
11735a18fff2SAl Viro 		BUG_ON(nd->inode != dir);
11745a18fff2SAl Viro 
11755a18fff2SAl Viro 		mutex_lock(&dir->i_mutex);
11765a18fff2SAl Viro 		dentry = d_lookup(parent, name);
11775a18fff2SAl Viro 		if (likely(!dentry)) {
11785a18fff2SAl Viro 			dentry = d_alloc_and_lookup(parent, name, nd);
11795a18fff2SAl Viro 			if (IS_ERR(dentry)) {
11805a18fff2SAl Viro 				mutex_unlock(&dir->i_mutex);
11815a18fff2SAl Viro 				return PTR_ERR(dentry);
11825a18fff2SAl Viro 			}
11835a18fff2SAl Viro 			/* known good */
11845a18fff2SAl Viro 			need_reval = 0;
11855a18fff2SAl Viro 			status = 1;
118644396f4bSJosef Bacik 		} else if (unlikely(d_need_lookup(dentry))) {
118744396f4bSJosef Bacik 			dentry = d_inode_lookup(parent, dentry, nd);
118844396f4bSJosef Bacik 			if (IS_ERR(dentry)) {
118944396f4bSJosef Bacik 				mutex_unlock(&dir->i_mutex);
119044396f4bSJosef Bacik 				return PTR_ERR(dentry);
119144396f4bSJosef Bacik 			}
119244396f4bSJosef Bacik 			/* known good */
119344396f4bSJosef Bacik 			need_reval = 0;
119444396f4bSJosef Bacik 			status = 1;
11955a18fff2SAl Viro 		}
11965a18fff2SAl Viro 		mutex_unlock(&dir->i_mutex);
11975a18fff2SAl Viro 	}
11985a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
11995a18fff2SAl Viro 		status = d_revalidate(dentry, nd);
12005a18fff2SAl Viro 	if (unlikely(status <= 0)) {
12015a18fff2SAl Viro 		if (status < 0) {
12025a18fff2SAl Viro 			dput(dentry);
12035a18fff2SAl Viro 			return status;
12045a18fff2SAl Viro 		}
12055a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
12065a18fff2SAl Viro 			dput(dentry);
12075a18fff2SAl Viro 			dentry = NULL;
12085a18fff2SAl Viro 			need_reval = 1;
12095a18fff2SAl Viro 			goto retry;
12105a18fff2SAl Viro 		}
12115a18fff2SAl Viro 	}
12125a18fff2SAl Viro 
12131da177e4SLinus Torvalds 	path->mnt = mnt;
12141da177e4SLinus Torvalds 	path->dentry = dentry;
12159875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
121689312214SIan Kent 	if (unlikely(err < 0)) {
121789312214SIan Kent 		path_put_conditional(path, nd);
12189875cf80SDavid Howells 		return err;
121989312214SIan Kent 	}
1220a3fbbde7SAl Viro 	if (err)
1221a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
122231e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
12231da177e4SLinus Torvalds 	return 0;
12241da177e4SLinus Torvalds }
12251da177e4SLinus Torvalds 
122652094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
122752094c8aSAl Viro {
122852094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
12294ad5abb3SAl Viro 		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
123052094c8aSAl Viro 		if (err != -ECHILD)
123152094c8aSAl Viro 			return err;
123219660af7SAl Viro 		if (unlazy_walk(nd, NULL))
123352094c8aSAl Viro 			return -ECHILD;
123452094c8aSAl Viro 	}
12354ad5abb3SAl Viro 	return inode_permission(nd->inode, MAY_EXEC);
123652094c8aSAl Viro }
123752094c8aSAl Viro 
12389856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
12399856fa1bSAl Viro {
12409856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
12419856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
12429856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
12439856fa1bSAl Viro 				return -ECHILD;
12449856fa1bSAl Viro 		} else
12459856fa1bSAl Viro 			follow_dotdot(nd);
12469856fa1bSAl Viro 	}
12479856fa1bSAl Viro 	return 0;
12489856fa1bSAl Viro }
12499856fa1bSAl Viro 
1250951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1251951361f9SAl Viro {
1252951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1253951361f9SAl Viro 		path_put(&nd->path);
1254951361f9SAl Viro 	} else {
1255951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
12565b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1257951361f9SAl Viro 			nd->root.mnt = NULL;
1258951361f9SAl Viro 		rcu_read_unlock();
1259951361f9SAl Viro 		br_read_unlock(vfsmount_lock);
1260951361f9SAl Viro 	}
1261951361f9SAl Viro }
1262951361f9SAl Viro 
12633ddcd056SLinus Torvalds /*
12643ddcd056SLinus Torvalds  * Do we need to follow links? We _really_ want to be able
12653ddcd056SLinus Torvalds  * to do this check without having to look at inode->i_op,
12663ddcd056SLinus Torvalds  * so we keep a cache of "no, this doesn't need follow_link"
12673ddcd056SLinus Torvalds  * for the common case.
12683ddcd056SLinus Torvalds  */
12697813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow)
12703ddcd056SLinus Torvalds {
12713ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
12723ddcd056SLinus Torvalds 		if (likely(inode->i_op->follow_link))
12733ddcd056SLinus Torvalds 			return follow;
12743ddcd056SLinus Torvalds 
12753ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
12763ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
12773ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_NOFOLLOW;
12783ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
12793ddcd056SLinus Torvalds 	}
12803ddcd056SLinus Torvalds 	return 0;
12813ddcd056SLinus Torvalds }
12823ddcd056SLinus Torvalds 
1283ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
1284ce57dfc1SAl Viro 		struct qstr *name, int type, int follow)
1285ce57dfc1SAl Viro {
1286ce57dfc1SAl Viro 	struct inode *inode;
1287ce57dfc1SAl Viro 	int err;
1288ce57dfc1SAl Viro 	/*
1289ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1290ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1291ce57dfc1SAl Viro 	 * parent relationships.
1292ce57dfc1SAl Viro 	 */
1293ce57dfc1SAl Viro 	if (unlikely(type != LAST_NORM))
1294ce57dfc1SAl Viro 		return handle_dots(nd, type);
1295ce57dfc1SAl Viro 	err = do_lookup(nd, name, path, &inode);
1296ce57dfc1SAl Viro 	if (unlikely(err)) {
1297ce57dfc1SAl Viro 		terminate_walk(nd);
1298ce57dfc1SAl Viro 		return err;
1299ce57dfc1SAl Viro 	}
1300ce57dfc1SAl Viro 	if (!inode) {
1301ce57dfc1SAl Viro 		path_to_nameidata(path, nd);
1302ce57dfc1SAl Viro 		terminate_walk(nd);
1303ce57dfc1SAl Viro 		return -ENOENT;
1304ce57dfc1SAl Viro 	}
13057813b94aSLinus Torvalds 	if (should_follow_link(inode, follow)) {
130619660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
130719660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
130819660af7SAl Viro 				terminate_walk(nd);
1309ce57dfc1SAl Viro 				return -ECHILD;
131019660af7SAl Viro 			}
131119660af7SAl Viro 		}
1312ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1313ce57dfc1SAl Viro 		return 1;
1314ce57dfc1SAl Viro 	}
1315ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1316ce57dfc1SAl Viro 	nd->inode = inode;
1317ce57dfc1SAl Viro 	return 0;
1318ce57dfc1SAl Viro }
1319ce57dfc1SAl Viro 
13201da177e4SLinus Torvalds /*
1321b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1322b356379aSAl Viro  * limiting consecutive symlinks to 40.
1323b356379aSAl Viro  *
1324b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1325b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1326b356379aSAl Viro  */
1327b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1328b356379aSAl Viro {
1329b356379aSAl Viro 	int res;
1330b356379aSAl Viro 
1331b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1332b356379aSAl Viro 		path_put_conditional(path, nd);
1333b356379aSAl Viro 		path_put(&nd->path);
1334b356379aSAl Viro 		return -ELOOP;
1335b356379aSAl Viro 	}
13361a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1337b356379aSAl Viro 
1338b356379aSAl Viro 	nd->depth++;
1339b356379aSAl Viro 	current->link_count++;
1340b356379aSAl Viro 
1341b356379aSAl Viro 	do {
1342b356379aSAl Viro 		struct path link = *path;
1343b356379aSAl Viro 		void *cookie;
1344574197e0SAl Viro 
1345574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
1346b356379aSAl Viro 		if (!res)
1347b356379aSAl Viro 			res = walk_component(nd, path, &nd->last,
1348b356379aSAl Viro 					     nd->last_type, LOOKUP_FOLLOW);
1349574197e0SAl Viro 		put_link(nd, &link, cookie);
1350b356379aSAl Viro 	} while (res > 0);
1351b356379aSAl Viro 
1352b356379aSAl Viro 	current->link_count--;
1353b356379aSAl Viro 	nd->depth--;
1354b356379aSAl Viro 	return res;
1355b356379aSAl Viro }
1356b356379aSAl Viro 
1357b356379aSAl Viro /*
13583ddcd056SLinus Torvalds  * We really don't want to look at inode->i_op->lookup
13593ddcd056SLinus Torvalds  * when we don't have to. So we keep a cache bit in
13603ddcd056SLinus Torvalds  * the inode ->i_opflags field that says "yes, we can
13613ddcd056SLinus Torvalds  * do lookup on this inode".
13623ddcd056SLinus Torvalds  */
13633ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode)
13643ddcd056SLinus Torvalds {
13653ddcd056SLinus Torvalds 	if (likely(inode->i_opflags & IOP_LOOKUP))
13663ddcd056SLinus Torvalds 		return 1;
13673ddcd056SLinus Torvalds 	if (likely(!inode->i_op->lookup))
13683ddcd056SLinus Torvalds 		return 0;
13693ddcd056SLinus Torvalds 
13703ddcd056SLinus Torvalds 	/* We do this once for the lifetime of the inode */
13713ddcd056SLinus Torvalds 	spin_lock(&inode->i_lock);
13723ddcd056SLinus Torvalds 	inode->i_opflags |= IOP_LOOKUP;
13733ddcd056SLinus Torvalds 	spin_unlock(&inode->i_lock);
13743ddcd056SLinus Torvalds 	return 1;
13753ddcd056SLinus Torvalds }
13763ddcd056SLinus Torvalds 
1377bfcfaa77SLinus Torvalds /*
1378bfcfaa77SLinus Torvalds  * We can do the critical dentry name comparison and hashing
1379bfcfaa77SLinus Torvalds  * operations one word at a time, but we are limited to:
1380bfcfaa77SLinus Torvalds  *
1381bfcfaa77SLinus Torvalds  * - Architectures with fast unaligned word accesses. We could
1382bfcfaa77SLinus Torvalds  *   do a "get_unaligned()" if this helps and is sufficiently
1383bfcfaa77SLinus Torvalds  *   fast.
1384bfcfaa77SLinus Torvalds  *
1385bfcfaa77SLinus Torvalds  * - Little-endian machines (so that we can generate the mask
1386bfcfaa77SLinus Torvalds  *   of low bytes efficiently). Again, we *could* do a byte
1387bfcfaa77SLinus Torvalds  *   swapping load on big-endian architectures if that is not
1388bfcfaa77SLinus Torvalds  *   expensive enough to make the optimization worthless.
1389bfcfaa77SLinus Torvalds  *
1390bfcfaa77SLinus Torvalds  * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1391bfcfaa77SLinus Torvalds  *   do not trap on the (extremely unlikely) case of a page
1392bfcfaa77SLinus Torvalds  *   crossing operation.
1393bfcfaa77SLinus Torvalds  *
1394bfcfaa77SLinus Torvalds  * - Furthermore, we need an efficient 64-bit compile for the
1395bfcfaa77SLinus Torvalds  *   64-bit case in order to generate the "number of bytes in
1396bfcfaa77SLinus Torvalds  *   the final mask". Again, that could be replaced with a
1397bfcfaa77SLinus Torvalds  *   efficient population count instruction or similar.
1398bfcfaa77SLinus Torvalds  */
1399bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS
1400bfcfaa77SLinus Torvalds 
1401bfcfaa77SLinus Torvalds #ifdef CONFIG_64BIT
1402bfcfaa77SLinus Torvalds 
1403bfcfaa77SLinus Torvalds /*
1404bfcfaa77SLinus Torvalds  * Jan Achrenius on G+: microoptimized version of
1405bfcfaa77SLinus Torvalds  * the simpler "(mask & ONEBYTES) * ONEBYTES >> 56"
1406bfcfaa77SLinus Torvalds  * that works for the bytemasks without having to
1407bfcfaa77SLinus Torvalds  * mask them first.
1408bfcfaa77SLinus Torvalds  */
1409bfcfaa77SLinus Torvalds static inline long count_masked_bytes(unsigned long mask)
1410bfcfaa77SLinus Torvalds {
1411f7493e5dSLinus Torvalds 	return mask*0x0001020304050608ul >> 56;
1412bfcfaa77SLinus Torvalds }
1413bfcfaa77SLinus Torvalds 
1414bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash)
1415bfcfaa77SLinus Torvalds {
1416bfcfaa77SLinus Torvalds 	hash += hash >> (8*sizeof(int));
1417bfcfaa77SLinus Torvalds 	return hash;
1418bfcfaa77SLinus Torvalds }
1419bfcfaa77SLinus Torvalds 
1420bfcfaa77SLinus Torvalds #else	/* 32-bit case */
1421bfcfaa77SLinus Torvalds 
1422bfcfaa77SLinus Torvalds /* Carl Chatfield / Jan Achrenius G+ version for 32-bit */
1423bfcfaa77SLinus Torvalds static inline long count_masked_bytes(long mask)
1424bfcfaa77SLinus Torvalds {
1425bfcfaa77SLinus Torvalds 	/* (000000 0000ff 00ffff ffffff) -> ( 1 1 2 3 ) */
1426bfcfaa77SLinus Torvalds 	long a = (0x0ff0001+mask) >> 23;
1427bfcfaa77SLinus Torvalds 	/* Fix the 1 for 00 case */
1428bfcfaa77SLinus Torvalds 	return a & mask;
1429bfcfaa77SLinus Torvalds }
1430bfcfaa77SLinus Torvalds 
1431bfcfaa77SLinus Torvalds #define fold_hash(x) (x)
1432bfcfaa77SLinus Torvalds 
1433bfcfaa77SLinus Torvalds #endif
1434bfcfaa77SLinus Torvalds 
1435bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1436bfcfaa77SLinus Torvalds {
1437bfcfaa77SLinus Torvalds 	unsigned long a, mask;
1438bfcfaa77SLinus Torvalds 	unsigned long hash = 0;
1439bfcfaa77SLinus Torvalds 
1440bfcfaa77SLinus Torvalds 	for (;;) {
1441bfcfaa77SLinus Torvalds 		a = *(unsigned long *)name;
1442bfcfaa77SLinus Torvalds 		if (len < sizeof(unsigned long))
1443bfcfaa77SLinus Torvalds 			break;
1444bfcfaa77SLinus Torvalds 		hash += a;
1445f132c5beSAl Viro 		hash *= 9;
1446bfcfaa77SLinus Torvalds 		name += sizeof(unsigned long);
1447bfcfaa77SLinus Torvalds 		len -= sizeof(unsigned long);
1448bfcfaa77SLinus Torvalds 		if (!len)
1449bfcfaa77SLinus Torvalds 			goto done;
1450bfcfaa77SLinus Torvalds 	}
1451bfcfaa77SLinus Torvalds 	mask = ~(~0ul << len*8);
1452bfcfaa77SLinus Torvalds 	hash += mask & a;
1453bfcfaa77SLinus Torvalds done:
1454bfcfaa77SLinus Torvalds 	return fold_hash(hash);
1455bfcfaa77SLinus Torvalds }
1456bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
1457bfcfaa77SLinus Torvalds 
1458989412bbSLinus Torvalds #define REPEAT_BYTE(x)	((~0ul / 0xff) * (x))
1459989412bbSLinus Torvalds #define ONEBYTES	REPEAT_BYTE(0x01)
1460989412bbSLinus Torvalds #define SLASHBYTES	REPEAT_BYTE('/')
1461989412bbSLinus Torvalds #define HIGHBITS	REPEAT_BYTE(0x80)
1462bfcfaa77SLinus Torvalds 
1463bfcfaa77SLinus Torvalds /* Return the high bit set in the first byte that is a zero */
1464bfcfaa77SLinus Torvalds static inline unsigned long has_zero(unsigned long a)
1465bfcfaa77SLinus Torvalds {
1466bfcfaa77SLinus Torvalds 	return ((a - ONEBYTES) & ~a) & HIGHBITS;
1467bfcfaa77SLinus Torvalds }
1468bfcfaa77SLinus Torvalds 
1469bfcfaa77SLinus Torvalds /*
1470bfcfaa77SLinus Torvalds  * Calculate the length and hash of the path component, and
1471bfcfaa77SLinus Torvalds  * return the length of the component;
1472bfcfaa77SLinus Torvalds  */
1473bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1474bfcfaa77SLinus Torvalds {
1475bfcfaa77SLinus Torvalds 	unsigned long a, mask, hash, len;
1476bfcfaa77SLinus Torvalds 
1477bfcfaa77SLinus Torvalds 	hash = a = 0;
1478bfcfaa77SLinus Torvalds 	len = -sizeof(unsigned long);
1479bfcfaa77SLinus Torvalds 	do {
1480bfcfaa77SLinus Torvalds 		hash = (hash + a) * 9;
1481bfcfaa77SLinus Torvalds 		len += sizeof(unsigned long);
1482bfcfaa77SLinus Torvalds 		a = *(unsigned long *)(name+len);
1483bfcfaa77SLinus Torvalds 		/* Do we have any NUL or '/' bytes in this word? */
1484bfcfaa77SLinus Torvalds 		mask = has_zero(a) | has_zero(a ^ SLASHBYTES);
1485bfcfaa77SLinus Torvalds 	} while (!mask);
1486bfcfaa77SLinus Torvalds 
1487bfcfaa77SLinus Torvalds 	/* The mask *below* the first high bit set */
1488bfcfaa77SLinus Torvalds 	mask = (mask - 1) & ~mask;
1489bfcfaa77SLinus Torvalds 	mask >>= 7;
1490bfcfaa77SLinus Torvalds 	hash += a & mask;
1491bfcfaa77SLinus Torvalds 	*hashp = fold_hash(hash);
1492bfcfaa77SLinus Torvalds 
1493bfcfaa77SLinus Torvalds 	return len + count_masked_bytes(mask);
1494bfcfaa77SLinus Torvalds }
1495bfcfaa77SLinus Torvalds 
1496bfcfaa77SLinus Torvalds #else
1497bfcfaa77SLinus Torvalds 
14980145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
14990145acc2SLinus Torvalds {
15000145acc2SLinus Torvalds 	unsigned long hash = init_name_hash();
15010145acc2SLinus Torvalds 	while (len--)
15020145acc2SLinus Torvalds 		hash = partial_name_hash(*name++, hash);
15030145acc2SLinus Torvalds 	return end_name_hash(hash);
15040145acc2SLinus Torvalds }
1505ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
15060145acc2SLinus Torvalds 
15073ddcd056SLinus Torvalds /*
1508200e9ef7SLinus Torvalds  * We know there's a real path component here of at least
1509200e9ef7SLinus Torvalds  * one character.
1510200e9ef7SLinus Torvalds  */
1511200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1512200e9ef7SLinus Torvalds {
1513200e9ef7SLinus Torvalds 	unsigned long hash = init_name_hash();
1514200e9ef7SLinus Torvalds 	unsigned long len = 0, c;
1515200e9ef7SLinus Torvalds 
1516200e9ef7SLinus Torvalds 	c = (unsigned char)*name;
1517200e9ef7SLinus Torvalds 	do {
1518200e9ef7SLinus Torvalds 		len++;
1519200e9ef7SLinus Torvalds 		hash = partial_name_hash(c, hash);
1520200e9ef7SLinus Torvalds 		c = (unsigned char)name[len];
1521200e9ef7SLinus Torvalds 	} while (c && c != '/');
1522200e9ef7SLinus Torvalds 	*hashp = end_name_hash(hash);
1523200e9ef7SLinus Torvalds 	return len;
1524200e9ef7SLinus Torvalds }
1525200e9ef7SLinus Torvalds 
1526bfcfaa77SLinus Torvalds #endif
1527bfcfaa77SLinus Torvalds 
1528200e9ef7SLinus Torvalds /*
15291da177e4SLinus Torvalds  * Name resolution.
1530ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1531ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
15321da177e4SLinus Torvalds  *
1533ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1534ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
15351da177e4SLinus Torvalds  */
15366de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
15371da177e4SLinus Torvalds {
15381da177e4SLinus Torvalds 	struct path next;
15391da177e4SLinus Torvalds 	int err;
15401da177e4SLinus Torvalds 
15411da177e4SLinus Torvalds 	while (*name=='/')
15421da177e4SLinus Torvalds 		name++;
15431da177e4SLinus Torvalds 	if (!*name)
1544086e183aSAl Viro 		return 0;
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
15471da177e4SLinus Torvalds 	for(;;) {
15481da177e4SLinus Torvalds 		struct qstr this;
1549200e9ef7SLinus Torvalds 		long len;
1550fe479a58SAl Viro 		int type;
15511da177e4SLinus Torvalds 
155252094c8aSAl Viro 		err = may_lookup(nd);
15531da177e4SLinus Torvalds  		if (err)
15541da177e4SLinus Torvalds 			break;
15551da177e4SLinus Torvalds 
1556200e9ef7SLinus Torvalds 		len = hash_name(name, &this.hash);
15571da177e4SLinus Torvalds 		this.name = name;
1558200e9ef7SLinus Torvalds 		this.len = len;
15591da177e4SLinus Torvalds 
1560fe479a58SAl Viro 		type = LAST_NORM;
1561200e9ef7SLinus Torvalds 		if (name[0] == '.') switch (len) {
1562fe479a58SAl Viro 			case 2:
1563200e9ef7SLinus Torvalds 				if (name[1] == '.') {
1564fe479a58SAl Viro 					type = LAST_DOTDOT;
156516c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
156616c2cd71SAl Viro 				}
1567fe479a58SAl Viro 				break;
1568fe479a58SAl Viro 			case 1:
1569fe479a58SAl Viro 				type = LAST_DOT;
1570fe479a58SAl Viro 		}
15715a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
15725a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
157316c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
15745a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
15755a202bcdSAl Viro 				err = parent->d_op->d_hash(parent, nd->inode,
15765a202bcdSAl Viro 							   &this);
15775a202bcdSAl Viro 				if (err < 0)
15785a202bcdSAl Viro 					break;
15795a202bcdSAl Viro 			}
15805a202bcdSAl Viro 		}
1581fe479a58SAl Viro 
1582200e9ef7SLinus Torvalds 		if (!name[len])
15831da177e4SLinus Torvalds 			goto last_component;
1584200e9ef7SLinus Torvalds 		/*
1585200e9ef7SLinus Torvalds 		 * If it wasn't NUL, we know it was '/'. Skip that
1586200e9ef7SLinus Torvalds 		 * slash, and continue until no more slashes.
1587200e9ef7SLinus Torvalds 		 */
1588200e9ef7SLinus Torvalds 		do {
1589200e9ef7SLinus Torvalds 			len++;
1590200e9ef7SLinus Torvalds 		} while (unlikely(name[len] == '/'));
1591200e9ef7SLinus Torvalds 		if (!name[len])
1592b356379aSAl Viro 			goto last_component;
1593200e9ef7SLinus Torvalds 		name += len;
15941da177e4SLinus Torvalds 
1595ce57dfc1SAl Viro 		err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1596ce57dfc1SAl Viro 		if (err < 0)
1597ce57dfc1SAl Viro 			return err;
1598fe479a58SAl Viro 
1599ce57dfc1SAl Viro 		if (err) {
1600b356379aSAl Viro 			err = nested_symlink(&next, nd);
16011da177e4SLinus Torvalds 			if (err)
1602a7472babSAl Viro 				return err;
160331e6b01fSNick Piggin 		}
16043ddcd056SLinus Torvalds 		if (can_lookup(nd->inode))
16051da177e4SLinus Torvalds 			continue;
16063ddcd056SLinus Torvalds 		err = -ENOTDIR;
16073ddcd056SLinus Torvalds 		break;
16081da177e4SLinus Torvalds 		/* here ends the main loop */
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds last_component:
1611ce57dfc1SAl Viro 		nd->last = this;
1612ce57dfc1SAl Viro 		nd->last_type = type;
1613ce57dfc1SAl Viro 		return 0;
1614ce57dfc1SAl Viro 	}
1615951361f9SAl Viro 	terminate_walk(nd);
16161da177e4SLinus Torvalds 	return err;
16171da177e4SLinus Torvalds }
16181da177e4SLinus Torvalds 
161970e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
162070e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
162131e6b01fSNick Piggin {
162231e6b01fSNick Piggin 	int retval = 0;
162331e6b01fSNick Piggin 	int fput_needed;
162431e6b01fSNick Piggin 	struct file *file;
162531e6b01fSNick Piggin 
162631e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
162716c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
162831e6b01fSNick Piggin 	nd->depth = 0;
16295b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
16305b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
163173d049a4SAl Viro 		if (*name) {
16325b6ca027SAl Viro 			if (!inode->i_op->lookup)
16335b6ca027SAl Viro 				return -ENOTDIR;
16345b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
16355b6ca027SAl Viro 			if (retval)
16365b6ca027SAl Viro 				return retval;
163773d049a4SAl Viro 		}
16385b6ca027SAl Viro 		nd->path = nd->root;
16395b6ca027SAl Viro 		nd->inode = inode;
16405b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
16415b6ca027SAl Viro 			br_read_lock(vfsmount_lock);
16425b6ca027SAl Viro 			rcu_read_lock();
16435b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
16445b6ca027SAl Viro 		} else {
16455b6ca027SAl Viro 			path_get(&nd->path);
16465b6ca027SAl Viro 		}
16475b6ca027SAl Viro 		return 0;
16485b6ca027SAl Viro 	}
16495b6ca027SAl Viro 
165031e6b01fSNick Piggin 	nd->root.mnt = NULL;
165131e6b01fSNick Piggin 
165231e6b01fSNick Piggin 	if (*name=='/') {
1653e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
165431e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
165531e6b01fSNick Piggin 			rcu_read_lock();
1656e41f7d4eSAl Viro 			set_root_rcu(nd);
1657e41f7d4eSAl Viro 		} else {
1658e41f7d4eSAl Viro 			set_root(nd);
1659e41f7d4eSAl Viro 			path_get(&nd->root);
1660e41f7d4eSAl Viro 		}
166131e6b01fSNick Piggin 		nd->path = nd->root;
166231e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1663e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
166431e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1665c28cc364SNick Piggin 			unsigned seq;
166631e6b01fSNick Piggin 
166731e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
166831e6b01fSNick Piggin 			rcu_read_lock();
166931e6b01fSNick Piggin 
1670c28cc364SNick Piggin 			do {
1671c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
167231e6b01fSNick Piggin 				nd->path = fs->pwd;
1673c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1674c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1675e41f7d4eSAl Viro 		} else {
1676e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1677e41f7d4eSAl Viro 		}
167831e6b01fSNick Piggin 	} else {
167931e6b01fSNick Piggin 		struct dentry *dentry;
168031e6b01fSNick Piggin 
16811abf0c71SAl Viro 		file = fget_raw_light(dfd, &fput_needed);
168231e6b01fSNick Piggin 		retval = -EBADF;
168331e6b01fSNick Piggin 		if (!file)
168431e6b01fSNick Piggin 			goto out_fail;
168531e6b01fSNick Piggin 
168631e6b01fSNick Piggin 		dentry = file->f_path.dentry;
168731e6b01fSNick Piggin 
1688f52e0c11SAl Viro 		if (*name) {
168931e6b01fSNick Piggin 			retval = -ENOTDIR;
169031e6b01fSNick Piggin 			if (!S_ISDIR(dentry->d_inode->i_mode))
169131e6b01fSNick Piggin 				goto fput_fail;
169231e6b01fSNick Piggin 
16934ad5abb3SAl Viro 			retval = inode_permission(dentry->d_inode, MAY_EXEC);
169431e6b01fSNick Piggin 			if (retval)
169531e6b01fSNick Piggin 				goto fput_fail;
1696f52e0c11SAl Viro 		}
169731e6b01fSNick Piggin 
169831e6b01fSNick Piggin 		nd->path = file->f_path;
1699e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
170031e6b01fSNick Piggin 			if (fput_needed)
170170e9b357SAl Viro 				*fp = file;
1702c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
170331e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
170431e6b01fSNick Piggin 			rcu_read_lock();
17055590ff0dSUlrich Drepper 		} else {
17065dd784d0SJan Blunck 			path_get(&file->f_path);
17075590ff0dSUlrich Drepper 			fput_light(file, fput_needed);
17081da177e4SLinus Torvalds 		}
1709e41f7d4eSAl Viro 	}
1710e41f7d4eSAl Viro 
171131e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
17129b4a9b14SAl Viro 	return 0;
17132dfdd266SJosef 'Jeff' Sipek 
17149b4a9b14SAl Viro fput_fail:
17159b4a9b14SAl Viro 	fput_light(file, fput_needed);
17169b4a9b14SAl Viro out_fail:
17179b4a9b14SAl Viro 	return retval;
17189b4a9b14SAl Viro }
17199b4a9b14SAl Viro 
1720bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1721bd92d7feSAl Viro {
1722bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1723bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1724bd92d7feSAl Viro 
1725bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
1726bd92d7feSAl Viro 	return walk_component(nd, path, &nd->last, nd->last_type,
1727bd92d7feSAl Viro 					nd->flags & LOOKUP_FOLLOW);
1728bd92d7feSAl Viro }
1729bd92d7feSAl Viro 
17309b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1731ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
17329b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
17339b4a9b14SAl Viro {
173470e9b357SAl Viro 	struct file *base = NULL;
1735bd92d7feSAl Viro 	struct path path;
1736bd92d7feSAl Viro 	int err;
173731e6b01fSNick Piggin 
173831e6b01fSNick Piggin 	/*
173931e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
174031e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
174131e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
174231e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
174331e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
174431e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
174531e6b01fSNick Piggin 	 * analogue, foo_rcu().
174631e6b01fSNick Piggin 	 *
174731e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
174831e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
174931e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
175031e6b01fSNick Piggin 	 * be able to complete).
175131e6b01fSNick Piggin 	 */
1752bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1753ee0827cdSAl Viro 
1754bd92d7feSAl Viro 	if (unlikely(err))
1755bd92d7feSAl Viro 		return err;
1756ee0827cdSAl Viro 
1757ee0827cdSAl Viro 	current->total_link_count = 0;
1758bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1759bd92d7feSAl Viro 
1760bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1761bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1762bd92d7feSAl Viro 		while (err > 0) {
1763bd92d7feSAl Viro 			void *cookie;
1764bd92d7feSAl Viro 			struct path link = path;
1765bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1766574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
1767bd92d7feSAl Viro 			if (!err)
1768bd92d7feSAl Viro 				err = lookup_last(nd, &path);
1769574197e0SAl Viro 			put_link(nd, &link, cookie);
1770bd92d7feSAl Viro 		}
1771bd92d7feSAl Viro 	}
1772ee0827cdSAl Viro 
17739f1fafeeSAl Viro 	if (!err)
17749f1fafeeSAl Viro 		err = complete_walk(nd);
1775bd92d7feSAl Viro 
1776bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
1777bd92d7feSAl Viro 		if (!nd->inode->i_op->lookup) {
1778bd92d7feSAl Viro 			path_put(&nd->path);
1779bd23a539SAl Viro 			err = -ENOTDIR;
1780bd92d7feSAl Viro 		}
1781bd92d7feSAl Viro 	}
178216c2cd71SAl Viro 
178370e9b357SAl Viro 	if (base)
178470e9b357SAl Viro 		fput(base);
1785ee0827cdSAl Viro 
17865b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
178731e6b01fSNick Piggin 		path_put(&nd->root);
178831e6b01fSNick Piggin 		nd->root.mnt = NULL;
178931e6b01fSNick Piggin 	}
1790bd92d7feSAl Viro 	return err;
179131e6b01fSNick Piggin }
179231e6b01fSNick Piggin 
1793ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1794ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1795ee0827cdSAl Viro {
1796ee0827cdSAl Viro 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1797ee0827cdSAl Viro 	if (unlikely(retval == -ECHILD))
1798ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags, nd);
1799ee0827cdSAl Viro 	if (unlikely(retval == -ESTALE))
1800ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1801ee0827cdSAl Viro 
180231e6b01fSNick Piggin 	if (likely(!retval)) {
180331e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
180431e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
180531e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
180631e6b01fSNick Piggin 		}
180731e6b01fSNick Piggin 	}
1808170aa3d0SUlrich Drepper 	return retval;
18091da177e4SLinus Torvalds }
18101da177e4SLinus Torvalds 
1811c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd)
18125590ff0dSUlrich Drepper {
1813c9c6cac0SAl Viro 	return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
18145590ff0dSUlrich Drepper }
18155590ff0dSUlrich Drepper 
1816d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1817d1811465SAl Viro {
1818d1811465SAl Viro 	struct nameidata nd;
1819d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1820d1811465SAl Viro 	if (!res)
1821d1811465SAl Viro 		*path = nd.path;
1822d1811465SAl Viro 	return res;
1823d1811465SAl Viro }
1824d1811465SAl Viro 
182516f18200SJosef 'Jeff' Sipek /**
182616f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
182716f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
182816f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
182916f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
183016f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
1831e0a01249SAl Viro  * @path: pointer to struct path to fill
183216f18200SJosef 'Jeff' Sipek  */
183316f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
183416f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
1835e0a01249SAl Viro 		    struct path *path)
183616f18200SJosef 'Jeff' Sipek {
1837e0a01249SAl Viro 	struct nameidata nd;
1838e0a01249SAl Viro 	int err;
1839e0a01249SAl Viro 	nd.root.dentry = dentry;
1840e0a01249SAl Viro 	nd.root.mnt = mnt;
1841e0a01249SAl Viro 	BUG_ON(flags & LOOKUP_PARENT);
18425b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1843e0a01249SAl Viro 	err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1844e0a01249SAl Viro 	if (!err)
1845e0a01249SAl Viro 		*path = nd.path;
1846e0a01249SAl Viro 	return err;
184716f18200SJosef 'Jeff' Sipek }
184816f18200SJosef 'Jeff' Sipek 
1849eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1850eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
18511da177e4SLinus Torvalds {
185281fca444SChristoph Hellwig 	struct inode *inode = base->d_inode;
18531da177e4SLinus Torvalds 	struct dentry *dentry;
18541da177e4SLinus Torvalds 	int err;
18551da177e4SLinus Torvalds 
18564ad5abb3SAl Viro 	err = inode_permission(inode, MAY_EXEC);
185781fca444SChristoph Hellwig 	if (err)
185881fca444SChristoph Hellwig 		return ERR_PTR(err);
18591da177e4SLinus Torvalds 
18601da177e4SLinus Torvalds 	/*
1861b04f784eSNick Piggin 	 * Don't bother with __d_lookup: callers are for creat as
1862b04f784eSNick Piggin 	 * well as unlink, so a lot of the time it would cost
1863b04f784eSNick Piggin 	 * a double lookup.
18646e6b1bd1SAl Viro 	 */
18656e6b1bd1SAl Viro 	dentry = d_lookup(base, name);
18666e6b1bd1SAl Viro 
186744396f4bSJosef Bacik 	if (dentry && d_need_lookup(dentry)) {
186844396f4bSJosef Bacik 		/*
186944396f4bSJosef Bacik 		 * __lookup_hash is called with the parent dir's i_mutex already
187044396f4bSJosef Bacik 		 * held, so we are good to go here.
187144396f4bSJosef Bacik 		 */
187244396f4bSJosef Bacik 		dentry = d_inode_lookup(base, dentry, nd);
187344396f4bSJosef Bacik 		if (IS_ERR(dentry))
187444396f4bSJosef Bacik 			return dentry;
187544396f4bSJosef Bacik 	}
187644396f4bSJosef Bacik 
1877d2d9e9fbSAl Viro 	if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) {
1878d2d9e9fbSAl Viro 		int status = d_revalidate(dentry, nd);
1879d2d9e9fbSAl Viro 		if (unlikely(status <= 0)) {
1880d2d9e9fbSAl Viro 			/*
1881d2d9e9fbSAl Viro 			 * The dentry failed validation.
1882d2d9e9fbSAl Viro 			 * If d_revalidate returned 0 attempt to invalidate
1883d2d9e9fbSAl Viro 			 * the dentry otherwise d_revalidate is asking us
1884d2d9e9fbSAl Viro 			 * to return a fail status.
1885d2d9e9fbSAl Viro 			 */
1886d2d9e9fbSAl Viro 			if (status < 0) {
1887d2d9e9fbSAl Viro 				dput(dentry);
1888d2d9e9fbSAl Viro 				return ERR_PTR(status);
1889d2d9e9fbSAl Viro 			} else if (!d_invalidate(dentry)) {
1890d2d9e9fbSAl Viro 				dput(dentry);
1891d2d9e9fbSAl Viro 				dentry = NULL;
1892d2d9e9fbSAl Viro 			}
1893d2d9e9fbSAl Viro 		}
1894d2d9e9fbSAl Viro 	}
18956e6b1bd1SAl Viro 
18961da177e4SLinus Torvalds 	if (!dentry)
1897baa03890SNick Piggin 		dentry = d_alloc_and_lookup(base, name, nd);
18985a202bcdSAl Viro 
18991da177e4SLinus Torvalds 	return dentry;
19001da177e4SLinus Torvalds }
19011da177e4SLinus Torvalds 
1902057f6c01SJames Morris /*
1903057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1904057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1905057f6c01SJames Morris  * SMP-safe.
1906057f6c01SJames Morris  */
1907a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
19081da177e4SLinus Torvalds {
19094ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
19101da177e4SLinus Torvalds }
19111da177e4SLinus Torvalds 
1912eead1911SChristoph Hellwig /**
1913a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1914eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1915eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1916eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1917eead1911SChristoph Hellwig  *
1918a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1919a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1920eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1921eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1922eead1911SChristoph Hellwig  */
1923057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1924057f6c01SJames Morris {
1925057f6c01SJames Morris 	struct qstr this;
19266a96ba54SAl Viro 	unsigned int c;
1927057f6c01SJames Morris 
19282f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
19292f9092e1SDavid Woodhouse 
19306a96ba54SAl Viro 	this.name = name;
19316a96ba54SAl Viro 	this.len = len;
19320145acc2SLinus Torvalds 	this.hash = full_name_hash(name, len);
19336a96ba54SAl Viro 	if (!len)
19346a96ba54SAl Viro 		return ERR_PTR(-EACCES);
19356a96ba54SAl Viro 
19366a96ba54SAl Viro 	while (len--) {
19376a96ba54SAl Viro 		c = *(const unsigned char *)name++;
19386a96ba54SAl Viro 		if (c == '/' || c == '\0')
19396a96ba54SAl Viro 			return ERR_PTR(-EACCES);
19406a96ba54SAl Viro 	}
19415a202bcdSAl Viro 	/*
19425a202bcdSAl Viro 	 * See if the low-level filesystem might want
19435a202bcdSAl Viro 	 * to use its own hash..
19445a202bcdSAl Viro 	 */
19455a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
19465a202bcdSAl Viro 		int err = base->d_op->d_hash(base, base->d_inode, &this);
19475a202bcdSAl Viro 		if (err < 0)
19485a202bcdSAl Viro 			return ERR_PTR(err);
19495a202bcdSAl Viro 	}
1950eead1911SChristoph Hellwig 
195149705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1952057f6c01SJames Morris }
1953057f6c01SJames Morris 
19541fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
19551fa1e7f6SAndy Whitcroft 		 struct path *path, int *empty)
19561da177e4SLinus Torvalds {
19572d8f3038SAl Viro 	struct nameidata nd;
19581fa1e7f6SAndy Whitcroft 	char *tmp = getname_flags(name, flags, empty);
19591da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
19601da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
19612d8f3038SAl Viro 
19622d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
19632d8f3038SAl Viro 
19642d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
19651da177e4SLinus Torvalds 		putname(tmp);
19662d8f3038SAl Viro 		if (!err)
19672d8f3038SAl Viro 			*path = nd.path;
19681da177e4SLinus Torvalds 	}
19691da177e4SLinus Torvalds 	return err;
19701da177e4SLinus Torvalds }
19711da177e4SLinus Torvalds 
19721fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags,
19731fa1e7f6SAndy Whitcroft 		 struct path *path)
19741fa1e7f6SAndy Whitcroft {
1975f7493e5dSLinus Torvalds 	return user_path_at_empty(dfd, name, flags, path, NULL);
19761fa1e7f6SAndy Whitcroft }
19771fa1e7f6SAndy Whitcroft 
19782ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
19792ad94ae6SAl Viro 			struct nameidata *nd, char **name)
19802ad94ae6SAl Viro {
19812ad94ae6SAl Viro 	char *s = getname(path);
19822ad94ae6SAl Viro 	int error;
19832ad94ae6SAl Viro 
19842ad94ae6SAl Viro 	if (IS_ERR(s))
19852ad94ae6SAl Viro 		return PTR_ERR(s);
19862ad94ae6SAl Viro 
19872ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
19882ad94ae6SAl Viro 	if (error)
19892ad94ae6SAl Viro 		putname(s);
19902ad94ae6SAl Viro 	else
19912ad94ae6SAl Viro 		*name = s;
19922ad94ae6SAl Viro 
19932ad94ae6SAl Viro 	return error;
19942ad94ae6SAl Viro }
19952ad94ae6SAl Viro 
19961da177e4SLinus Torvalds /*
19971da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
19981da177e4SLinus Torvalds  * minimal.
19991da177e4SLinus Torvalds  */
20001da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
20011da177e4SLinus Torvalds {
2002da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
2003da9592edSDavid Howells 
20041da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
20051da177e4SLinus Torvalds 		return 0;
2006e795b717SSerge E. Hallyn 	if (current_user_ns() != inode_userns(inode))
2007e795b717SSerge E. Hallyn 		goto other_userns;
2008da9592edSDavid Howells 	if (inode->i_uid == fsuid)
20091da177e4SLinus Torvalds 		return 0;
2010da9592edSDavid Howells 	if (dir->i_uid == fsuid)
20111da177e4SLinus Torvalds 		return 0;
2012e795b717SSerge E. Hallyn 
2013e795b717SSerge E. Hallyn other_userns:
2014e795b717SSerge E. Hallyn 	return !ns_capable(inode_userns(inode), CAP_FOWNER);
20151da177e4SLinus Torvalds }
20161da177e4SLinus Torvalds 
20171da177e4SLinus Torvalds /*
20181da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
20191da177e4SLinus Torvalds  *  whether the type of victim is right.
20201da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
20211da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
20221da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
20231da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
20241da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
20251da177e4SLinus Torvalds  *	a. be owner of dir, or
20261da177e4SLinus Torvalds  *	b. be owner of victim, or
20271da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
20281da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
20291da177e4SLinus Torvalds  *     links pointing to it.
20301da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
20311da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
20321da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
20331da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
20341da177e4SLinus Torvalds  *     nfs_async_unlink().
20351da177e4SLinus Torvalds  */
2036858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
20371da177e4SLinus Torvalds {
20381da177e4SLinus Torvalds 	int error;
20391da177e4SLinus Torvalds 
20401da177e4SLinus Torvalds 	if (!victim->d_inode)
20411da177e4SLinus Torvalds 		return -ENOENT;
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
2044cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
20451da177e4SLinus Torvalds 
2046f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
20471da177e4SLinus Torvalds 	if (error)
20481da177e4SLinus Torvalds 		return error;
20491da177e4SLinus Torvalds 	if (IS_APPEND(dir))
20501da177e4SLinus Torvalds 		return -EPERM;
20511da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
2052f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
20531da177e4SLinus Torvalds 		return -EPERM;
20541da177e4SLinus Torvalds 	if (isdir) {
20551da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
20561da177e4SLinus Torvalds 			return -ENOTDIR;
20571da177e4SLinus Torvalds 		if (IS_ROOT(victim))
20581da177e4SLinus Torvalds 			return -EBUSY;
20591da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
20601da177e4SLinus Torvalds 		return -EISDIR;
20611da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
20621da177e4SLinus Torvalds 		return -ENOENT;
20631da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
20641da177e4SLinus Torvalds 		return -EBUSY;
20651da177e4SLinus Torvalds 	return 0;
20661da177e4SLinus Torvalds }
20671da177e4SLinus Torvalds 
20681da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
20691da177e4SLinus Torvalds  *  dir.
20701da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
20711da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
20721da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
20731da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
20741da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
20751da177e4SLinus Torvalds  */
2076a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
20771da177e4SLinus Torvalds {
20781da177e4SLinus Torvalds 	if (child->d_inode)
20791da177e4SLinus Torvalds 		return -EEXIST;
20801da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
20811da177e4SLinus Torvalds 		return -ENOENT;
2082f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
20831da177e4SLinus Torvalds }
20841da177e4SLinus Torvalds 
20851da177e4SLinus Torvalds /*
20861da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
20871da177e4SLinus Torvalds  */
20881da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
20891da177e4SLinus Torvalds {
20901da177e4SLinus Torvalds 	struct dentry *p;
20911da177e4SLinus Torvalds 
20921da177e4SLinus Torvalds 	if (p1 == p2) {
2093f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
20941da177e4SLinus Torvalds 		return NULL;
20951da177e4SLinus Torvalds 	}
20961da177e4SLinus Torvalds 
2097a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
20981da177e4SLinus Torvalds 
2099e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
2100e2761a11SOGAWA Hirofumi 	if (p) {
2101f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2102f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
21031da177e4SLinus Torvalds 		return p;
21041da177e4SLinus Torvalds 	}
21051da177e4SLinus Torvalds 
2106e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
2107e2761a11SOGAWA Hirofumi 	if (p) {
2108f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2109f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
21101da177e4SLinus Torvalds 		return p;
21111da177e4SLinus Torvalds 	}
21121da177e4SLinus Torvalds 
2113f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2114f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
21151da177e4SLinus Torvalds 	return NULL;
21161da177e4SLinus Torvalds }
21171da177e4SLinus Torvalds 
21181da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
21191da177e4SLinus Torvalds {
21201b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
21211da177e4SLinus Torvalds 	if (p1 != p2) {
21221b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
2123a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
21241da177e4SLinus Torvalds 	}
21251da177e4SLinus Torvalds }
21261da177e4SLinus Torvalds 
21274acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
21281da177e4SLinus Torvalds 		struct nameidata *nd)
21291da177e4SLinus Torvalds {
2130a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
21311da177e4SLinus Torvalds 
21321da177e4SLinus Torvalds 	if (error)
21331da177e4SLinus Torvalds 		return error;
21341da177e4SLinus Torvalds 
2135acfa4380SAl Viro 	if (!dir->i_op->create)
21361da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
21371da177e4SLinus Torvalds 	mode &= S_IALLUGO;
21381da177e4SLinus Torvalds 	mode |= S_IFREG;
21391da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
21401da177e4SLinus Torvalds 	if (error)
21411da177e4SLinus Torvalds 		return error;
21421da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
2143a74574aaSStephen Smalley 	if (!error)
2144f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
21451da177e4SLinus Torvalds 	return error;
21461da177e4SLinus Torvalds }
21471da177e4SLinus Torvalds 
214873d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
21491da177e4SLinus Torvalds {
21503fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
21511da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
21521da177e4SLinus Torvalds 	int error;
21531da177e4SLinus Torvalds 
2154bcda7652SAl Viro 	/* O_PATH? */
2155bcda7652SAl Viro 	if (!acc_mode)
2156bcda7652SAl Viro 		return 0;
2157bcda7652SAl Viro 
21581da177e4SLinus Torvalds 	if (!inode)
21591da177e4SLinus Torvalds 		return -ENOENT;
21601da177e4SLinus Torvalds 
2161c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2162c8fe8f30SChristoph Hellwig 	case S_IFLNK:
21631da177e4SLinus Torvalds 		return -ELOOP;
2164c8fe8f30SChristoph Hellwig 	case S_IFDIR:
2165c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
21661da177e4SLinus Torvalds 			return -EISDIR;
2167c8fe8f30SChristoph Hellwig 		break;
2168c8fe8f30SChristoph Hellwig 	case S_IFBLK:
2169c8fe8f30SChristoph Hellwig 	case S_IFCHR:
21703fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
21711da177e4SLinus Torvalds 			return -EACCES;
2172c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
2173c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2174c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
21751da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2176c8fe8f30SChristoph Hellwig 		break;
21774a3fd211SDave Hansen 	}
2178b41572e9SDave Hansen 
21793fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
2180b41572e9SDave Hansen 	if (error)
2181b41572e9SDave Hansen 		return error;
21826146f0d5SMimi Zohar 
21831da177e4SLinus Torvalds 	/*
21841da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
21851da177e4SLinus Torvalds 	 */
21861da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
21878737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
21887715b521SAl Viro 			return -EPERM;
21891da177e4SLinus Torvalds 		if (flag & O_TRUNC)
21907715b521SAl Viro 			return -EPERM;
21911da177e4SLinus Torvalds 	}
21921da177e4SLinus Torvalds 
21931da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
21942e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
21957715b521SAl Viro 		return -EPERM;
21961da177e4SLinus Torvalds 
2197f3c7691eSJ. Bruce Fields 	return 0;
21987715b521SAl Viro }
21997715b521SAl Viro 
2200e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
22017715b521SAl Viro {
2202e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
22037715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
22047715b521SAl Viro 	int error = get_write_access(inode);
22051da177e4SLinus Torvalds 	if (error)
22067715b521SAl Viro 		return error;
22071da177e4SLinus Torvalds 	/*
22081da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
22091da177e4SLinus Torvalds 	 */
22101da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2211be6d3e56SKentaro Takeda 	if (!error)
2212ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
22131da177e4SLinus Torvalds 	if (!error) {
22147715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2215d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2216e1181ee6SJeff Layton 				    filp);
22171da177e4SLinus Torvalds 	}
22181da177e4SLinus Torvalds 	put_write_access(inode);
2219acd0c935SMimi Zohar 	return error;
22201da177e4SLinus Torvalds }
22211da177e4SLinus Torvalds 
2222d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2223d57999e1SDave Hansen {
22248a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
22258a5e929dSAl Viro 		flag--;
2226d57999e1SDave Hansen 	return flag;
2227d57999e1SDave Hansen }
2228d57999e1SDave Hansen 
222931e6b01fSNick Piggin /*
2230fe2d35ffSAl Viro  * Handle the last step of open()
223131e6b01fSNick Piggin  */
2232fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
2233c3e380b0SAl Viro 			    const struct open_flags *op, const char *pathname)
2234fb1cc555SAl Viro {
2235a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
22366c0d46c4SAl Viro 	struct dentry *dentry;
2237ca344a89SAl Viro 	int open_flag = op->open_flag;
22386c0d46c4SAl Viro 	int will_truncate = open_flag & O_TRUNC;
2239ca344a89SAl Viro 	int want_write = 0;
2240bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2241fb1cc555SAl Viro 	struct file *filp;
224216c2cd71SAl Viro 	int error;
2243fb1cc555SAl Viro 
2244c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2245c3e380b0SAl Viro 	nd->flags |= op->intent;
2246c3e380b0SAl Viro 
22471f36f774SAl Viro 	switch (nd->last_type) {
22481f36f774SAl Viro 	case LAST_DOTDOT:
2249176306f5SNeil Brown 	case LAST_DOT:
2250fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2251fe2d35ffSAl Viro 		if (error)
2252fe2d35ffSAl Viro 			return ERR_PTR(error);
22531f36f774SAl Viro 		/* fallthrough */
22541f36f774SAl Viro 	case LAST_ROOT:
22559f1fafeeSAl Viro 		error = complete_walk(nd);
225616c2cd71SAl Viro 		if (error)
22579f1fafeeSAl Viro 			return ERR_PTR(error);
2258fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2259ca344a89SAl Viro 		if (open_flag & O_CREAT) {
226016c2cd71SAl Viro 			error = -EISDIR;
22611f36f774SAl Viro 			goto exit;
2262fe2d35ffSAl Viro 		}
2263fe2d35ffSAl Viro 		goto ok;
22641f36f774SAl Viro 	case LAST_BIND:
22659f1fafeeSAl Viro 		error = complete_walk(nd);
226616c2cd71SAl Viro 		if (error)
22679f1fafeeSAl Viro 			return ERR_PTR(error);
22681f36f774SAl Viro 		audit_inode(pathname, dir);
22691f36f774SAl Viro 		goto ok;
22701f36f774SAl Viro 	}
2271a2c36b45SAl Viro 
2272ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2273bcda7652SAl Viro 		int symlink_ok = 0;
2274fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2275fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2276bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2277bcda7652SAl Viro 			symlink_ok = 1;
2278fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2279ce57dfc1SAl Viro 		error = walk_component(nd, path, &nd->last, LAST_NORM,
2280ce57dfc1SAl Viro 					!symlink_ok);
2281ce57dfc1SAl Viro 		if (error < 0)
2282fe2d35ffSAl Viro 			return ERR_PTR(error);
2283ce57dfc1SAl Viro 		if (error) /* symlink */
2284fe2d35ffSAl Viro 			return NULL;
2285fe2d35ffSAl Viro 		/* sayonara */
22869f1fafeeSAl Viro 		error = complete_walk(nd);
22879f1fafeeSAl Viro 		if (error)
22887f6c7e62SMiklos Szeredi 			return ERR_PTR(error);
2289fe2d35ffSAl Viro 
2290fe2d35ffSAl Viro 		error = -ENOTDIR;
2291fe2d35ffSAl Viro 		if (nd->flags & LOOKUP_DIRECTORY) {
2292ce57dfc1SAl Viro 			if (!nd->inode->i_op->lookup)
2293fe2d35ffSAl Viro 				goto exit;
2294fe2d35ffSAl Viro 		}
2295fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2296fe2d35ffSAl Viro 		goto ok;
2297fe2d35ffSAl Viro 	}
2298fe2d35ffSAl Viro 
2299fe2d35ffSAl Viro 	/* create side of things */
2300a3fbbde7SAl Viro 	/*
2301a3fbbde7SAl Viro 	 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED has been
2302a3fbbde7SAl Viro 	 * cleared when we got to the last component we are about to look up
2303a3fbbde7SAl Viro 	 */
23049f1fafeeSAl Viro 	error = complete_walk(nd);
23059f1fafeeSAl Viro 	if (error)
23069f1fafeeSAl Viro 		return ERR_PTR(error);
2307fe2d35ffSAl Viro 
2308fe2d35ffSAl Viro 	audit_inode(pathname, dir);
230916c2cd71SAl Viro 	error = -EISDIR;
23101f36f774SAl Viro 	/* trailing slashes? */
231131e6b01fSNick Piggin 	if (nd->last.name[nd->last.len])
23121f36f774SAl Viro 		goto exit;
23131f36f774SAl Viro 
2314a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
2315a1e28038SAl Viro 
23166c0d46c4SAl Viro 	dentry = lookup_hash(nd);
23176c0d46c4SAl Viro 	error = PTR_ERR(dentry);
23186c0d46c4SAl Viro 	if (IS_ERR(dentry)) {
2319fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
2320fb1cc555SAl Viro 		goto exit;
2321fb1cc555SAl Viro 	}
2322fb1cc555SAl Viro 
23236c0d46c4SAl Viro 	path->dentry = dentry;
23246c0d46c4SAl Viro 	path->mnt = nd->path.mnt;
23256c0d46c4SAl Viro 
2326fb1cc555SAl Viro 	/* Negative dentry, just create the file */
23276c0d46c4SAl Viro 	if (!dentry->d_inode) {
2328a218d0fdSAl Viro 		umode_t mode = op->mode;
23296c0d46c4SAl Viro 		if (!IS_POSIXACL(dir->d_inode))
23306c0d46c4SAl Viro 			mode &= ~current_umask();
2331fb1cc555SAl Viro 		/*
2332fb1cc555SAl Viro 		 * This write is needed to ensure that a
23336c0d46c4SAl Viro 		 * rw->ro transition does not occur between
2334fb1cc555SAl Viro 		 * the time when the file is created and when
2335fb1cc555SAl Viro 		 * a permanent write count is taken through
2336fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
2337fb1cc555SAl Viro 		 */
2338fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
2339fb1cc555SAl Viro 		if (error)
2340fb1cc555SAl Viro 			goto exit_mutex_unlock;
2341ca344a89SAl Viro 		want_write = 1;
23429b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2343ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
23446c0d46c4SAl Viro 		will_truncate = 0;
2345bcda7652SAl Viro 		acc_mode = MAY_OPEN;
23466c0d46c4SAl Viro 		error = security_path_mknod(&nd->path, dentry, mode, 0);
23476c0d46c4SAl Viro 		if (error)
23486c0d46c4SAl Viro 			goto exit_mutex_unlock;
23496c0d46c4SAl Viro 		error = vfs_create(dir->d_inode, dentry, mode, nd);
23506c0d46c4SAl Viro 		if (error)
23516c0d46c4SAl Viro 			goto exit_mutex_unlock;
23526c0d46c4SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
23536c0d46c4SAl Viro 		dput(nd->path.dentry);
23546c0d46c4SAl Viro 		nd->path.dentry = dentry;
2355ca344a89SAl Viro 		goto common;
2356fb1cc555SAl Viro 	}
2357fb1cc555SAl Viro 
2358fb1cc555SAl Viro 	/*
2359fb1cc555SAl Viro 	 * It already exists.
2360fb1cc555SAl Viro 	 */
2361fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2362fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2363fb1cc555SAl Viro 
2364fb1cc555SAl Viro 	error = -EEXIST;
2365ca344a89SAl Viro 	if (open_flag & O_EXCL)
2366fb1cc555SAl Viro 		goto exit_dput;
2367fb1cc555SAl Viro 
23689875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
23699875cf80SDavid Howells 	if (error < 0)
2370fb1cc555SAl Viro 		goto exit_dput;
2371fb1cc555SAl Viro 
2372a3fbbde7SAl Viro 	if (error)
2373a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
2374a3fbbde7SAl Viro 
2375fb1cc555SAl Viro 	error = -ENOENT;
2376fb1cc555SAl Viro 	if (!path->dentry->d_inode)
2377fb1cc555SAl Viro 		goto exit_dput;
23789e67f361SAl Viro 
23799e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
2380fb1cc555SAl Viro 		return NULL;
2381fb1cc555SAl Viro 
2382fb1cc555SAl Viro 	path_to_nameidata(path, nd);
238331e6b01fSNick Piggin 	nd->inode = path->dentry->d_inode;
2384a3fbbde7SAl Viro 	/* Why this, you ask?  _Now_ we might have grown LOOKUP_JUMPED... */
2385a3fbbde7SAl Viro 	error = complete_walk(nd);
2386a3fbbde7SAl Viro 	if (error)
2387097b180cSMiklos Szeredi 		return ERR_PTR(error);
2388fb1cc555SAl Viro 	error = -EISDIR;
238931e6b01fSNick Piggin 	if (S_ISDIR(nd->inode->i_mode))
2390fb1cc555SAl Viro 		goto exit;
239167ee3ad2SAl Viro ok:
23926c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
23936c0d46c4SAl Viro 		will_truncate = 0;
23946c0d46c4SAl Viro 
23950f9d1a10SAl Viro 	if (will_truncate) {
23960f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
23970f9d1a10SAl Viro 		if (error)
23980f9d1a10SAl Viro 			goto exit;
2399ca344a89SAl Viro 		want_write = 1;
24000f9d1a10SAl Viro 	}
2401ca344a89SAl Viro common:
2402bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2403ca344a89SAl Viro 	if (error)
24040f9d1a10SAl Viro 		goto exit;
24050f9d1a10SAl Viro 	filp = nameidata_to_filp(nd);
24060f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
24070f9d1a10SAl Viro 		error = ima_file_check(filp, op->acc_mode);
24080f9d1a10SAl Viro 		if (error) {
24090f9d1a10SAl Viro 			fput(filp);
24100f9d1a10SAl Viro 			filp = ERR_PTR(error);
24110f9d1a10SAl Viro 		}
24120f9d1a10SAl Viro 	}
24130f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
24140f9d1a10SAl Viro 		if (will_truncate) {
24150f9d1a10SAl Viro 			error = handle_truncate(filp);
24160f9d1a10SAl Viro 			if (error) {
24170f9d1a10SAl Viro 				fput(filp);
24180f9d1a10SAl Viro 				filp = ERR_PTR(error);
24190f9d1a10SAl Viro 			}
24200f9d1a10SAl Viro 		}
24210f9d1a10SAl Viro 	}
2422ca344a89SAl Viro out:
2423ca344a89SAl Viro 	if (want_write)
24240f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
24250f9d1a10SAl Viro 	path_put(&nd->path);
2426fb1cc555SAl Viro 	return filp;
2427fb1cc555SAl Viro 
2428fb1cc555SAl Viro exit_mutex_unlock:
2429fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2430fb1cc555SAl Viro exit_dput:
2431fb1cc555SAl Viro 	path_put_conditional(path, nd);
2432fb1cc555SAl Viro exit:
2433ca344a89SAl Viro 	filp = ERR_PTR(error);
2434ca344a89SAl Viro 	goto out;
2435fb1cc555SAl Viro }
2436fb1cc555SAl Viro 
243713aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname,
243873d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
24391da177e4SLinus Torvalds {
2440fe2d35ffSAl Viro 	struct file *base = NULL;
24414a3fd211SDave Hansen 	struct file *filp;
24429850c056SAl Viro 	struct path path;
244313aab428SAl Viro 	int error;
244431e6b01fSNick Piggin 
244531e6b01fSNick Piggin 	filp = get_empty_filp();
244631e6b01fSNick Piggin 	if (!filp)
244731e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
244831e6b01fSNick Piggin 
244947c805dcSAl Viro 	filp->f_flags = op->open_flag;
245073d049a4SAl Viro 	nd->intent.open.file = filp;
245173d049a4SAl Viro 	nd->intent.open.flags = open_to_namei_flags(op->open_flag);
245273d049a4SAl Viro 	nd->intent.open.create_mode = op->mode;
245331e6b01fSNick Piggin 
245473d049a4SAl Viro 	error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
245531e6b01fSNick Piggin 	if (unlikely(error))
245613aab428SAl Viro 		goto out_filp;
245731e6b01fSNick Piggin 
2458fe2d35ffSAl Viro 	current->total_link_count = 0;
245973d049a4SAl Viro 	error = link_path_walk(pathname, nd);
246031e6b01fSNick Piggin 	if (unlikely(error))
246131e6b01fSNick Piggin 		goto out_filp;
24621da177e4SLinus Torvalds 
246373d049a4SAl Viro 	filp = do_last(nd, &path, op, pathname);
2464806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
24657b9337aaSNick Piggin 		struct path link = path;
2466def4af30SAl Viro 		void *cookie;
2467574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
246873d049a4SAl Viro 			path_put_conditional(&path, nd);
246973d049a4SAl Viro 			path_put(&nd->path);
247040b39136SAl Viro 			filp = ERR_PTR(-ELOOP);
247140b39136SAl Viro 			break;
247240b39136SAl Viro 		}
247373d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
247473d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2475574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
2476c3e380b0SAl Viro 		if (unlikely(error))
2477f1afe9efSAl Viro 			filp = ERR_PTR(error);
2478c3e380b0SAl Viro 		else
247973d049a4SAl Viro 			filp = do_last(nd, &path, op, pathname);
2480574197e0SAl Viro 		put_link(nd, &link, cookie);
2481806b681cSAl Viro 	}
248210fa8e62SAl Viro out:
248373d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
248473d049a4SAl Viro 		path_put(&nd->root);
2485fe2d35ffSAl Viro 	if (base)
2486fe2d35ffSAl Viro 		fput(base);
248773d049a4SAl Viro 	release_open_intent(nd);
248810fa8e62SAl Viro 	return filp;
24891da177e4SLinus Torvalds 
249031e6b01fSNick Piggin out_filp:
249110fa8e62SAl Viro 	filp = ERR_PTR(error);
249210fa8e62SAl Viro 	goto out;
2493de459215SKirill Korotaev }
24941da177e4SLinus Torvalds 
249513aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname,
249613aab428SAl Viro 		const struct open_flags *op, int flags)
249713aab428SAl Viro {
249873d049a4SAl Viro 	struct nameidata nd;
249913aab428SAl Viro 	struct file *filp;
250013aab428SAl Viro 
250173d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
250213aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
250373d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
250413aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
250573d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
250613aab428SAl Viro 	return filp;
250713aab428SAl Viro }
250813aab428SAl Viro 
250973d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
251073d049a4SAl Viro 		const char *name, const struct open_flags *op, int flags)
251173d049a4SAl Viro {
251273d049a4SAl Viro 	struct nameidata nd;
251373d049a4SAl Viro 	struct file *file;
251473d049a4SAl Viro 
251573d049a4SAl Viro 	nd.root.mnt = mnt;
251673d049a4SAl Viro 	nd.root.dentry = dentry;
251773d049a4SAl Viro 
251873d049a4SAl Viro 	flags |= LOOKUP_ROOT;
251973d049a4SAl Viro 
2520bcda7652SAl Viro 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
252173d049a4SAl Viro 		return ERR_PTR(-ELOOP);
252273d049a4SAl Viro 
252373d049a4SAl Viro 	file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
252473d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
252573d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags);
252673d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
252773d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
252873d049a4SAl Viro 	return file;
252973d049a4SAl Viro }
253073d049a4SAl Viro 
2531ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
25321da177e4SLinus Torvalds {
2533c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
2534ed75e95dSAl Viro 	struct nameidata nd;
2535ed75e95dSAl Viro 	int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2536ed75e95dSAl Viro 	if (error)
2537ed75e95dSAl Viro 		return ERR_PTR(error);
25381da177e4SLinus Torvalds 
2539c663e5d8SChristoph Hellwig 	/*
2540c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2541c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2542c663e5d8SChristoph Hellwig 	 */
2543ed75e95dSAl Viro 	if (nd.last_type != LAST_NORM)
2544ed75e95dSAl Viro 		goto out;
2545ed75e95dSAl Viro 	nd.flags &= ~LOOKUP_PARENT;
2546ed75e95dSAl Viro 	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2547ed75e95dSAl Viro 	nd.intent.open.flags = O_EXCL;
2548c663e5d8SChristoph Hellwig 
2549c663e5d8SChristoph Hellwig 	/*
2550c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2551c663e5d8SChristoph Hellwig 	 */
2552ed75e95dSAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2553ed75e95dSAl Viro 	dentry = lookup_hash(&nd);
25541da177e4SLinus Torvalds 	if (IS_ERR(dentry))
25551da177e4SLinus Torvalds 		goto fail;
2556c663e5d8SChristoph Hellwig 
2557e9baf6e5SAl Viro 	if (dentry->d_inode)
2558e9baf6e5SAl Viro 		goto eexist;
2559c663e5d8SChristoph Hellwig 	/*
2560c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2561c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2562c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2563c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2564c663e5d8SChristoph Hellwig 	 */
2565ed75e95dSAl Viro 	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
25661da177e4SLinus Torvalds 		dput(dentry);
25671da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2568ed75e95dSAl Viro 		goto fail;
2569e9baf6e5SAl Viro 	}
2570ed75e95dSAl Viro 	*path = nd.path;
2571e9baf6e5SAl Viro 	return dentry;
2572e9baf6e5SAl Viro eexist:
2573e9baf6e5SAl Viro 	dput(dentry);
2574e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
25751da177e4SLinus Torvalds fail:
2576dae6ad8fSAl Viro 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2577ed75e95dSAl Viro out:
2578dae6ad8fSAl Viro 	path_put(&nd.path);
2579ed75e95dSAl Viro 	return dentry;
2580dae6ad8fSAl Viro }
2581dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create);
2582dae6ad8fSAl Viro 
2583dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2584dae6ad8fSAl Viro {
2585dae6ad8fSAl Viro 	char *tmp = getname(pathname);
2586dae6ad8fSAl Viro 	struct dentry *res;
2587dae6ad8fSAl Viro 	if (IS_ERR(tmp))
2588dae6ad8fSAl Viro 		return ERR_CAST(tmp);
2589dae6ad8fSAl Viro 	res = kern_path_create(dfd, tmp, path, is_dir);
2590dae6ad8fSAl Viro 	putname(tmp);
2591dae6ad8fSAl Viro 	return res;
2592dae6ad8fSAl Viro }
2593dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create);
2594dae6ad8fSAl Viro 
25951a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
25961da177e4SLinus Torvalds {
2597a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
25981da177e4SLinus Torvalds 
25991da177e4SLinus Torvalds 	if (error)
26001da177e4SLinus Torvalds 		return error;
26011da177e4SLinus Torvalds 
2602e795b717SSerge E. Hallyn 	if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2603e795b717SSerge E. Hallyn 	    !ns_capable(inode_userns(dir), CAP_MKNOD))
26041da177e4SLinus Torvalds 		return -EPERM;
26051da177e4SLinus Torvalds 
2606acfa4380SAl Viro 	if (!dir->i_op->mknod)
26071da177e4SLinus Torvalds 		return -EPERM;
26081da177e4SLinus Torvalds 
260908ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
261008ce5f16SSerge E. Hallyn 	if (error)
261108ce5f16SSerge E. Hallyn 		return error;
261208ce5f16SSerge E. Hallyn 
26131da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
26141da177e4SLinus Torvalds 	if (error)
26151da177e4SLinus Torvalds 		return error;
26161da177e4SLinus Torvalds 
26171da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2618a74574aaSStephen Smalley 	if (!error)
2619f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
26201da177e4SLinus Torvalds 	return error;
26211da177e4SLinus Torvalds }
26221da177e4SLinus Torvalds 
2623f69aac00SAl Viro static int may_mknod(umode_t mode)
2624463c3197SDave Hansen {
2625463c3197SDave Hansen 	switch (mode & S_IFMT) {
2626463c3197SDave Hansen 	case S_IFREG:
2627463c3197SDave Hansen 	case S_IFCHR:
2628463c3197SDave Hansen 	case S_IFBLK:
2629463c3197SDave Hansen 	case S_IFIFO:
2630463c3197SDave Hansen 	case S_IFSOCK:
2631463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2632463c3197SDave Hansen 		return 0;
2633463c3197SDave Hansen 	case S_IFDIR:
2634463c3197SDave Hansen 		return -EPERM;
2635463c3197SDave Hansen 	default:
2636463c3197SDave Hansen 		return -EINVAL;
2637463c3197SDave Hansen 	}
2638463c3197SDave Hansen }
2639463c3197SDave Hansen 
26408208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
26412e4d0924SHeiko Carstens 		unsigned, dev)
26421da177e4SLinus Torvalds {
26431da177e4SLinus Torvalds 	struct dentry *dentry;
2644dae6ad8fSAl Viro 	struct path path;
2645dae6ad8fSAl Viro 	int error;
26461da177e4SLinus Torvalds 
26471da177e4SLinus Torvalds 	if (S_ISDIR(mode))
26481da177e4SLinus Torvalds 		return -EPERM;
26491da177e4SLinus Torvalds 
2650dae6ad8fSAl Viro 	dentry = user_path_create(dfd, filename, &path, 0);
2651dae6ad8fSAl Viro 	if (IS_ERR(dentry))
2652dae6ad8fSAl Viro 		return PTR_ERR(dentry);
26532ad94ae6SAl Viro 
2654dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
2655ce3b0f8dSAl Viro 		mode &= ~current_umask();
2656463c3197SDave Hansen 	error = may_mknod(mode);
2657463c3197SDave Hansen 	if (error)
2658463c3197SDave Hansen 		goto out_dput;
2659dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
2660463c3197SDave Hansen 	if (error)
2661463c3197SDave Hansen 		goto out_dput;
2662dae6ad8fSAl Viro 	error = security_path_mknod(&path, dentry, mode, dev);
2663be6d3e56SKentaro Takeda 	if (error)
2664be6d3e56SKentaro Takeda 		goto out_drop_write;
26651da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
26661da177e4SLinus Torvalds 		case 0: case S_IFREG:
2667dae6ad8fSAl Viro 			error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
26681da177e4SLinus Torvalds 			break;
26691da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
2670dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
26711da177e4SLinus Torvalds 					new_decode_dev(dev));
26721da177e4SLinus Torvalds 			break;
26731da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
2674dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
26751da177e4SLinus Torvalds 			break;
26761da177e4SLinus Torvalds 	}
2677be6d3e56SKentaro Takeda out_drop_write:
2678dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
2679463c3197SDave Hansen out_dput:
26801da177e4SLinus Torvalds 	dput(dentry);
2681dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2682dae6ad8fSAl Viro 	path_put(&path);
26831da177e4SLinus Torvalds 
26841da177e4SLinus Torvalds 	return error;
26851da177e4SLinus Torvalds }
26861da177e4SLinus Torvalds 
26878208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
26885590ff0dSUlrich Drepper {
26895590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
26905590ff0dSUlrich Drepper }
26915590ff0dSUlrich Drepper 
269218bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
26931da177e4SLinus Torvalds {
2694a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
26958de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
26961da177e4SLinus Torvalds 
26971da177e4SLinus Torvalds 	if (error)
26981da177e4SLinus Torvalds 		return error;
26991da177e4SLinus Torvalds 
2700acfa4380SAl Viro 	if (!dir->i_op->mkdir)
27011da177e4SLinus Torvalds 		return -EPERM;
27021da177e4SLinus Torvalds 
27031da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
27041da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
27051da177e4SLinus Torvalds 	if (error)
27061da177e4SLinus Torvalds 		return error;
27071da177e4SLinus Torvalds 
27088de52778SAl Viro 	if (max_links && dir->i_nlink >= max_links)
27098de52778SAl Viro 		return -EMLINK;
27108de52778SAl Viro 
27111da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2712a74574aaSStephen Smalley 	if (!error)
2713f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
27141da177e4SLinus Torvalds 	return error;
27151da177e4SLinus Torvalds }
27161da177e4SLinus Torvalds 
2717a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
27181da177e4SLinus Torvalds {
27196902d925SDave Hansen 	struct dentry *dentry;
2720dae6ad8fSAl Viro 	struct path path;
2721dae6ad8fSAl Viro 	int error;
27221da177e4SLinus Torvalds 
2723dae6ad8fSAl Viro 	dentry = user_path_create(dfd, pathname, &path, 1);
27246902d925SDave Hansen 	if (IS_ERR(dentry))
2725dae6ad8fSAl Viro 		return PTR_ERR(dentry);
27266902d925SDave Hansen 
2727dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
2728ce3b0f8dSAl Viro 		mode &= ~current_umask();
2729dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
2730463c3197SDave Hansen 	if (error)
2731463c3197SDave Hansen 		goto out_dput;
2732dae6ad8fSAl Viro 	error = security_path_mkdir(&path, dentry, mode);
2733be6d3e56SKentaro Takeda 	if (error)
2734be6d3e56SKentaro Takeda 		goto out_drop_write;
2735dae6ad8fSAl Viro 	error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
2736be6d3e56SKentaro Takeda out_drop_write:
2737dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
2738463c3197SDave Hansen out_dput:
27391da177e4SLinus Torvalds 	dput(dentry);
2740dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2741dae6ad8fSAl Viro 	path_put(&path);
27421da177e4SLinus Torvalds 	return error;
27431da177e4SLinus Torvalds }
27441da177e4SLinus Torvalds 
2745a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
27465590ff0dSUlrich Drepper {
27475590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
27485590ff0dSUlrich Drepper }
27495590ff0dSUlrich Drepper 
27501da177e4SLinus Torvalds /*
2751a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
2752a71905f0SSage Weil  * should have a usage count of 2 if we're the only user of this
2753a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
2754a71905f0SSage Weil  * then we drop the dentry now.
27551da177e4SLinus Torvalds  *
27561da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
27571da177e4SLinus Torvalds  * do a
27581da177e4SLinus Torvalds  *
27591da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
27601da177e4SLinus Torvalds  *		return -EBUSY;
27611da177e4SLinus Torvalds  *
27621da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
27631da177e4SLinus Torvalds  * that is still in use by something else..
27641da177e4SLinus Torvalds  */
27651da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
27661da177e4SLinus Torvalds {
27671da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
27681da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
276964252c75SSage Weil 	if (dentry->d_count == 1)
27701da177e4SLinus Torvalds 		__d_drop(dentry);
27711da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
27721da177e4SLinus Torvalds }
27731da177e4SLinus Torvalds 
27741da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
27751da177e4SLinus Torvalds {
27761da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
27771da177e4SLinus Torvalds 
27781da177e4SLinus Torvalds 	if (error)
27791da177e4SLinus Torvalds 		return error;
27801da177e4SLinus Torvalds 
2781acfa4380SAl Viro 	if (!dir->i_op->rmdir)
27821da177e4SLinus Torvalds 		return -EPERM;
27831da177e4SLinus Torvalds 
27841d2ef590SAl Viro 	dget(dentry);
27851b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
2786912dbc15SSage Weil 
27871da177e4SLinus Torvalds 	error = -EBUSY;
2788912dbc15SSage Weil 	if (d_mountpoint(dentry))
2789912dbc15SSage Weil 		goto out;
2790912dbc15SSage Weil 
27911da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
2792912dbc15SSage Weil 	if (error)
2793912dbc15SSage Weil 		goto out;
2794912dbc15SSage Weil 
27953cebde24SSage Weil 	shrink_dcache_parent(dentry);
27961da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
2797912dbc15SSage Weil 	if (error)
2798912dbc15SSage Weil 		goto out;
2799912dbc15SSage Weil 
28001da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
2801d83c49f3SAl Viro 	dont_mount(dentry);
28021da177e4SLinus Torvalds 
2803912dbc15SSage Weil out:
2804912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
28051d2ef590SAl Viro 	dput(dentry);
2806912dbc15SSage Weil 	if (!error)
2807912dbc15SSage Weil 		d_delete(dentry);
28081da177e4SLinus Torvalds 	return error;
28091da177e4SLinus Torvalds }
28101da177e4SLinus Torvalds 
28115590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
28121da177e4SLinus Torvalds {
28131da177e4SLinus Torvalds 	int error = 0;
28141da177e4SLinus Torvalds 	char * name;
28151da177e4SLinus Torvalds 	struct dentry *dentry;
28161da177e4SLinus Torvalds 	struct nameidata nd;
28171da177e4SLinus Torvalds 
28182ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
28191da177e4SLinus Torvalds 	if (error)
28202ad94ae6SAl Viro 		return error;
28211da177e4SLinus Torvalds 
28221da177e4SLinus Torvalds 	switch(nd.last_type) {
28231da177e4SLinus Torvalds 	case LAST_DOTDOT:
28241da177e4SLinus Torvalds 		error = -ENOTEMPTY;
28251da177e4SLinus Torvalds 		goto exit1;
28261da177e4SLinus Torvalds 	case LAST_DOT:
28271da177e4SLinus Torvalds 		error = -EINVAL;
28281da177e4SLinus Torvalds 		goto exit1;
28291da177e4SLinus Torvalds 	case LAST_ROOT:
28301da177e4SLinus Torvalds 		error = -EBUSY;
28311da177e4SLinus Torvalds 		goto exit1;
28321da177e4SLinus Torvalds 	}
28330612d9fbSOGAWA Hirofumi 
28340612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
28350612d9fbSOGAWA Hirofumi 
28364ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
283749705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
28381da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
28396902d925SDave Hansen 	if (IS_ERR(dentry))
28406902d925SDave Hansen 		goto exit2;
2841e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
2842e6bc45d6STheodore Ts'o 		error = -ENOENT;
2843e6bc45d6STheodore Ts'o 		goto exit3;
2844e6bc45d6STheodore Ts'o 	}
28450622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
28460622753bSDave Hansen 	if (error)
28470622753bSDave Hansen 		goto exit3;
2848be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2849be6d3e56SKentaro Takeda 	if (error)
2850be6d3e56SKentaro Takeda 		goto exit4;
28514ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2852be6d3e56SKentaro Takeda exit4:
28530622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
28540622753bSDave Hansen exit3:
28551da177e4SLinus Torvalds 	dput(dentry);
28566902d925SDave Hansen exit2:
28574ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
28581da177e4SLinus Torvalds exit1:
28591d957f9bSJan Blunck 	path_put(&nd.path);
28601da177e4SLinus Torvalds 	putname(name);
28611da177e4SLinus Torvalds 	return error;
28621da177e4SLinus Torvalds }
28631da177e4SLinus Torvalds 
28643cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
28655590ff0dSUlrich Drepper {
28665590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
28675590ff0dSUlrich Drepper }
28685590ff0dSUlrich Drepper 
28691da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
28701da177e4SLinus Torvalds {
28711da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
28721da177e4SLinus Torvalds 
28731da177e4SLinus Torvalds 	if (error)
28741da177e4SLinus Torvalds 		return error;
28751da177e4SLinus Torvalds 
2876acfa4380SAl Viro 	if (!dir->i_op->unlink)
28771da177e4SLinus Torvalds 		return -EPERM;
28781da177e4SLinus Torvalds 
28791b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
28801da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
28811da177e4SLinus Torvalds 		error = -EBUSY;
28821da177e4SLinus Torvalds 	else {
28831da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2884bec1052eSAl Viro 		if (!error) {
28851da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2886bec1052eSAl Viro 			if (!error)
2887d83c49f3SAl Viro 				dont_mount(dentry);
2888bec1052eSAl Viro 		}
28891da177e4SLinus Torvalds 	}
28901b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
28911da177e4SLinus Torvalds 
28921da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
28931da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2894ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
28951da177e4SLinus Torvalds 		d_delete(dentry);
28961da177e4SLinus Torvalds 	}
28970eeca283SRobert Love 
28981da177e4SLinus Torvalds 	return error;
28991da177e4SLinus Torvalds }
29001da177e4SLinus Torvalds 
29011da177e4SLinus Torvalds /*
29021da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
29031b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
29041da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
29051da177e4SLinus Torvalds  * while waiting on the I/O.
29061da177e4SLinus Torvalds  */
29075590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
29081da177e4SLinus Torvalds {
29092ad94ae6SAl Viro 	int error;
29101da177e4SLinus Torvalds 	char *name;
29111da177e4SLinus Torvalds 	struct dentry *dentry;
29121da177e4SLinus Torvalds 	struct nameidata nd;
29131da177e4SLinus Torvalds 	struct inode *inode = NULL;
29141da177e4SLinus Torvalds 
29152ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
29161da177e4SLinus Torvalds 	if (error)
29172ad94ae6SAl Viro 		return error;
29182ad94ae6SAl Viro 
29191da177e4SLinus Torvalds 	error = -EISDIR;
29201da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
29211da177e4SLinus Torvalds 		goto exit1;
29220612d9fbSOGAWA Hirofumi 
29230612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
29240612d9fbSOGAWA Hirofumi 
29254ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
292649705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
29271da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
29281da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
29291da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
293050338b88STörök Edwin 		if (nd.last.name[nd.last.len])
293150338b88STörök Edwin 			goto slashes;
29321da177e4SLinus Torvalds 		inode = dentry->d_inode;
293350338b88STörök Edwin 		if (!inode)
2934e6bc45d6STheodore Ts'o 			goto slashes;
29357de9c6eeSAl Viro 		ihold(inode);
29360622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
29370622753bSDave Hansen 		if (error)
29380622753bSDave Hansen 			goto exit2;
2939be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2940be6d3e56SKentaro Takeda 		if (error)
2941be6d3e56SKentaro Takeda 			goto exit3;
29424ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2943be6d3e56SKentaro Takeda exit3:
29440622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
29451da177e4SLinus Torvalds 	exit2:
29461da177e4SLinus Torvalds 		dput(dentry);
29471da177e4SLinus Torvalds 	}
29484ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
29491da177e4SLinus Torvalds 	if (inode)
29501da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
29511da177e4SLinus Torvalds exit1:
29521d957f9bSJan Blunck 	path_put(&nd.path);
29531da177e4SLinus Torvalds 	putname(name);
29541da177e4SLinus Torvalds 	return error;
29551da177e4SLinus Torvalds 
29561da177e4SLinus Torvalds slashes:
29571da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
29581da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
29591da177e4SLinus Torvalds 	goto exit2;
29601da177e4SLinus Torvalds }
29611da177e4SLinus Torvalds 
29622e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
29635590ff0dSUlrich Drepper {
29645590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
29655590ff0dSUlrich Drepper 		return -EINVAL;
29665590ff0dSUlrich Drepper 
29675590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
29685590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
29695590ff0dSUlrich Drepper 
29705590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
29715590ff0dSUlrich Drepper }
29725590ff0dSUlrich Drepper 
29733480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
29745590ff0dSUlrich Drepper {
29755590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
29765590ff0dSUlrich Drepper }
29775590ff0dSUlrich Drepper 
2978db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
29791da177e4SLinus Torvalds {
2980a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
29811da177e4SLinus Torvalds 
29821da177e4SLinus Torvalds 	if (error)
29831da177e4SLinus Torvalds 		return error;
29841da177e4SLinus Torvalds 
2985acfa4380SAl Viro 	if (!dir->i_op->symlink)
29861da177e4SLinus Torvalds 		return -EPERM;
29871da177e4SLinus Torvalds 
29881da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
29891da177e4SLinus Torvalds 	if (error)
29901da177e4SLinus Torvalds 		return error;
29911da177e4SLinus Torvalds 
29921da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2993a74574aaSStephen Smalley 	if (!error)
2994f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
29951da177e4SLinus Torvalds 	return error;
29961da177e4SLinus Torvalds }
29971da177e4SLinus Torvalds 
29982e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
29992e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
30001da177e4SLinus Torvalds {
30012ad94ae6SAl Viro 	int error;
30021da177e4SLinus Torvalds 	char *from;
30036902d925SDave Hansen 	struct dentry *dentry;
3004dae6ad8fSAl Viro 	struct path path;
30051da177e4SLinus Torvalds 
30061da177e4SLinus Torvalds 	from = getname(oldname);
30071da177e4SLinus Torvalds 	if (IS_ERR(from))
30081da177e4SLinus Torvalds 		return PTR_ERR(from);
30092ad94ae6SAl Viro 
3010dae6ad8fSAl Viro 	dentry = user_path_create(newdfd, newname, &path, 0);
30111da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
30126902d925SDave Hansen 	if (IS_ERR(dentry))
3013dae6ad8fSAl Viro 		goto out_putname;
30146902d925SDave Hansen 
3015dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
301675c3f29dSDave Hansen 	if (error)
301775c3f29dSDave Hansen 		goto out_dput;
3018dae6ad8fSAl Viro 	error = security_path_symlink(&path, dentry, from);
3019be6d3e56SKentaro Takeda 	if (error)
3020be6d3e56SKentaro Takeda 		goto out_drop_write;
3021dae6ad8fSAl Viro 	error = vfs_symlink(path.dentry->d_inode, dentry, from);
3022be6d3e56SKentaro Takeda out_drop_write:
3023dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
302475c3f29dSDave Hansen out_dput:
30251da177e4SLinus Torvalds 	dput(dentry);
3026dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
3027dae6ad8fSAl Viro 	path_put(&path);
30286902d925SDave Hansen out_putname:
30291da177e4SLinus Torvalds 	putname(from);
30301da177e4SLinus Torvalds 	return error;
30311da177e4SLinus Torvalds }
30321da177e4SLinus Torvalds 
30333480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
30345590ff0dSUlrich Drepper {
30355590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
30365590ff0dSUlrich Drepper }
30375590ff0dSUlrich Drepper 
30381da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
30391da177e4SLinus Torvalds {
30401da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
30418de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
30421da177e4SLinus Torvalds 	int error;
30431da177e4SLinus Torvalds 
30441da177e4SLinus Torvalds 	if (!inode)
30451da177e4SLinus Torvalds 		return -ENOENT;
30461da177e4SLinus Torvalds 
3047a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
30481da177e4SLinus Torvalds 	if (error)
30491da177e4SLinus Torvalds 		return error;
30501da177e4SLinus Torvalds 
30511da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
30521da177e4SLinus Torvalds 		return -EXDEV;
30531da177e4SLinus Torvalds 
30541da177e4SLinus Torvalds 	/*
30551da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
30561da177e4SLinus Torvalds 	 */
30571da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
30581da177e4SLinus Torvalds 		return -EPERM;
3059acfa4380SAl Viro 	if (!dir->i_op->link)
30601da177e4SLinus Torvalds 		return -EPERM;
30617e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
30621da177e4SLinus Torvalds 		return -EPERM;
30631da177e4SLinus Torvalds 
30641da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
30651da177e4SLinus Torvalds 	if (error)
30661da177e4SLinus Torvalds 		return error;
30671da177e4SLinus Torvalds 
30687e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
3069aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
3070aae8a97dSAneesh Kumar K.V 	if (inode->i_nlink == 0)
3071aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
30728de52778SAl Viro 	else if (max_links && inode->i_nlink >= max_links)
30738de52778SAl Viro 		error = -EMLINK;
3074aae8a97dSAneesh Kumar K.V 	else
30751da177e4SLinus Torvalds 		error = dir->i_op->link(old_dentry, dir, new_dentry);
30767e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
3077e31e14ecSStephen Smalley 	if (!error)
30787e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
30791da177e4SLinus Torvalds 	return error;
30801da177e4SLinus Torvalds }
30811da177e4SLinus Torvalds 
30821da177e4SLinus Torvalds /*
30831da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
30841da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
30851da177e4SLinus Torvalds  * newname.  --KAB
30861da177e4SLinus Torvalds  *
30871da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
30881da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
30891da177e4SLinus Torvalds  * and other special files.  --ADM
30901da177e4SLinus Torvalds  */
30912e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
30922e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
30931da177e4SLinus Torvalds {
30941da177e4SLinus Torvalds 	struct dentry *new_dentry;
3095dae6ad8fSAl Viro 	struct path old_path, new_path;
309611a7b371SAneesh Kumar K.V 	int how = 0;
30971da177e4SLinus Torvalds 	int error;
30981da177e4SLinus Torvalds 
309911a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3100c04030e1SUlrich Drepper 		return -EINVAL;
310111a7b371SAneesh Kumar K.V 	/*
310211a7b371SAneesh Kumar K.V 	 * To use null names we require CAP_DAC_READ_SEARCH
310311a7b371SAneesh Kumar K.V 	 * This ensures that not everyone will be able to create
310411a7b371SAneesh Kumar K.V 	 * handlink using the passed filedescriptor.
310511a7b371SAneesh Kumar K.V 	 */
310611a7b371SAneesh Kumar K.V 	if (flags & AT_EMPTY_PATH) {
310711a7b371SAneesh Kumar K.V 		if (!capable(CAP_DAC_READ_SEARCH))
310811a7b371SAneesh Kumar K.V 			return -ENOENT;
310911a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
311011a7b371SAneesh Kumar K.V 	}
3111c04030e1SUlrich Drepper 
311211a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
311311a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
311411a7b371SAneesh Kumar K.V 
311511a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
31161da177e4SLinus Torvalds 	if (error)
31172ad94ae6SAl Viro 		return error;
31182ad94ae6SAl Viro 
3119dae6ad8fSAl Viro 	new_dentry = user_path_create(newdfd, newname, &new_path, 0);
31201da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
31216902d925SDave Hansen 	if (IS_ERR(new_dentry))
3122dae6ad8fSAl Viro 		goto out;
3123dae6ad8fSAl Viro 
3124dae6ad8fSAl Viro 	error = -EXDEV;
3125dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt)
3126dae6ad8fSAl Viro 		goto out_dput;
3127dae6ad8fSAl Viro 	error = mnt_want_write(new_path.mnt);
312875c3f29dSDave Hansen 	if (error)
312975c3f29dSDave Hansen 		goto out_dput;
3130dae6ad8fSAl Viro 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
3131be6d3e56SKentaro Takeda 	if (error)
3132be6d3e56SKentaro Takeda 		goto out_drop_write;
3133dae6ad8fSAl Viro 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
3134be6d3e56SKentaro Takeda out_drop_write:
3135dae6ad8fSAl Viro 	mnt_drop_write(new_path.mnt);
313675c3f29dSDave Hansen out_dput:
31371da177e4SLinus Torvalds 	dput(new_dentry);
3138dae6ad8fSAl Viro 	mutex_unlock(&new_path.dentry->d_inode->i_mutex);
3139dae6ad8fSAl Viro 	path_put(&new_path);
31401da177e4SLinus Torvalds out:
31412d8f3038SAl Viro 	path_put(&old_path);
31421da177e4SLinus Torvalds 
31431da177e4SLinus Torvalds 	return error;
31441da177e4SLinus Torvalds }
31451da177e4SLinus Torvalds 
31463480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
31475590ff0dSUlrich Drepper {
3148c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
31495590ff0dSUlrich Drepper }
31505590ff0dSUlrich Drepper 
31511da177e4SLinus Torvalds /*
31521da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
31531da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
31541da177e4SLinus Torvalds  * Problems:
31551da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
31561da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
31571da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
3158a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
31591da177e4SLinus Torvalds  *	   story.
31601da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
31611b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
31621da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
31631da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
3164a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
31651da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
31661da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
31671da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
3168a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
31691da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
31701da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
31711da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
3172e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
31731b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
31741da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3175c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
31761da177e4SLinus Torvalds  *	   locking].
31771da177e4SLinus Torvalds  */
317875c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
31791da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
31801da177e4SLinus Torvalds {
31811da177e4SLinus Torvalds 	int error = 0;
31829055cba7SSage Weil 	struct inode *target = new_dentry->d_inode;
31838de52778SAl Viro 	unsigned max_links = new_dir->i_sb->s_max_links;
31841da177e4SLinus Torvalds 
31851da177e4SLinus Torvalds 	/*
31861da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
31871da177e4SLinus Torvalds 	 * we'll need to flip '..'.
31881da177e4SLinus Torvalds 	 */
31891da177e4SLinus Torvalds 	if (new_dir != old_dir) {
3190f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
31911da177e4SLinus Torvalds 		if (error)
31921da177e4SLinus Torvalds 			return error;
31931da177e4SLinus Torvalds 	}
31941da177e4SLinus Torvalds 
31951da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
31961da177e4SLinus Torvalds 	if (error)
31971da177e4SLinus Torvalds 		return error;
31981da177e4SLinus Torvalds 
31991d2ef590SAl Viro 	dget(new_dentry);
3200d83c49f3SAl Viro 	if (target)
32011b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
32029055cba7SSage Weil 
32031da177e4SLinus Torvalds 	error = -EBUSY;
32049055cba7SSage Weil 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
32059055cba7SSage Weil 		goto out;
32069055cba7SSage Weil 
32078de52778SAl Viro 	error = -EMLINK;
32088de52778SAl Viro 	if (max_links && !target && new_dir != old_dir &&
32098de52778SAl Viro 	    new_dir->i_nlink >= max_links)
32108de52778SAl Viro 		goto out;
32118de52778SAl Viro 
32123cebde24SSage Weil 	if (target)
32133cebde24SSage Weil 		shrink_dcache_parent(new_dentry);
32141da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
32159055cba7SSage Weil 	if (error)
32169055cba7SSage Weil 		goto out;
32179055cba7SSage Weil 
32181da177e4SLinus Torvalds 	if (target) {
32191da177e4SLinus Torvalds 		target->i_flags |= S_DEAD;
3220d83c49f3SAl Viro 		dont_mount(new_dentry);
3221d83c49f3SAl Viro 	}
32229055cba7SSage Weil out:
32239055cba7SSage Weil 	if (target)
32241b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
32251d2ef590SAl Viro 	dput(new_dentry);
3226e31e14ecSStephen Smalley 	if (!error)
3227349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
32281da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
32291da177e4SLinus Torvalds 	return error;
32301da177e4SLinus Torvalds }
32311da177e4SLinus Torvalds 
323275c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
32331da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
32341da177e4SLinus Torvalds {
323551892bbbSSage Weil 	struct inode *target = new_dentry->d_inode;
32361da177e4SLinus Torvalds 	int error;
32371da177e4SLinus Torvalds 
32381da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
32391da177e4SLinus Torvalds 	if (error)
32401da177e4SLinus Torvalds 		return error;
32411da177e4SLinus Torvalds 
32421da177e4SLinus Torvalds 	dget(new_dentry);
32431da177e4SLinus Torvalds 	if (target)
32441b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
324551892bbbSSage Weil 
32461da177e4SLinus Torvalds 	error = -EBUSY;
324751892bbbSSage Weil 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
324851892bbbSSage Weil 		goto out;
324951892bbbSSage Weil 
32501da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
325151892bbbSSage Weil 	if (error)
325251892bbbSSage Weil 		goto out;
325351892bbbSSage Weil 
3254bec1052eSAl Viro 	if (target)
3255d83c49f3SAl Viro 		dont_mount(new_dentry);
3256349457ccSMark Fasheh 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
32571da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
325851892bbbSSage Weil out:
32591da177e4SLinus Torvalds 	if (target)
32601b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
32611da177e4SLinus Torvalds 	dput(new_dentry);
32621da177e4SLinus Torvalds 	return error;
32631da177e4SLinus Torvalds }
32641da177e4SLinus Torvalds 
32651da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
32661da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
32671da177e4SLinus Torvalds {
32681da177e4SLinus Torvalds 	int error;
32691da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
327059b0df21SEric Paris 	const unsigned char *old_name;
32711da177e4SLinus Torvalds 
32721da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
32731da177e4SLinus Torvalds  		return 0;
32741da177e4SLinus Torvalds 
32751da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
32761da177e4SLinus Torvalds 	if (error)
32771da177e4SLinus Torvalds 		return error;
32781da177e4SLinus Torvalds 
32791da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3280a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
32811da177e4SLinus Torvalds 	else
32821da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
32831da177e4SLinus Torvalds 	if (error)
32841da177e4SLinus Torvalds 		return error;
32851da177e4SLinus Torvalds 
3286acfa4380SAl Viro 	if (!old_dir->i_op->rename)
32871da177e4SLinus Torvalds 		return -EPERM;
32881da177e4SLinus Torvalds 
32890eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
32900eeca283SRobert Love 
32911da177e4SLinus Torvalds 	if (is_dir)
32921da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
32931da177e4SLinus Torvalds 	else
32941da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3295123df294SAl Viro 	if (!error)
3296123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
32975a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
32980eeca283SRobert Love 	fsnotify_oldname_free(old_name);
32990eeca283SRobert Love 
33001da177e4SLinus Torvalds 	return error;
33011da177e4SLinus Torvalds }
33021da177e4SLinus Torvalds 
33032e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
33042e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
33051da177e4SLinus Torvalds {
33061da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
33071da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
33081da177e4SLinus Torvalds 	struct dentry *trap;
33091da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
33102ad94ae6SAl Viro 	char *from;
33112ad94ae6SAl Viro 	char *to;
33122ad94ae6SAl Viro 	int error;
33131da177e4SLinus Torvalds 
33142ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
33151da177e4SLinus Torvalds 	if (error)
33161da177e4SLinus Torvalds 		goto exit;
33171da177e4SLinus Torvalds 
33182ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
33191da177e4SLinus Torvalds 	if (error)
33201da177e4SLinus Torvalds 		goto exit1;
33211da177e4SLinus Torvalds 
33221da177e4SLinus Torvalds 	error = -EXDEV;
33234ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
33241da177e4SLinus Torvalds 		goto exit2;
33251da177e4SLinus Torvalds 
33264ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
33271da177e4SLinus Torvalds 	error = -EBUSY;
33281da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
33291da177e4SLinus Torvalds 		goto exit2;
33301da177e4SLinus Torvalds 
33314ac91378SJan Blunck 	new_dir = newnd.path.dentry;
33321da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
33331da177e4SLinus Torvalds 		goto exit2;
33341da177e4SLinus Torvalds 
33350612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
33360612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
33374e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
33380612d9fbSOGAWA Hirofumi 
33391da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
33401da177e4SLinus Torvalds 
334149705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
33421da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
33431da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
33441da177e4SLinus Torvalds 		goto exit3;
33451da177e4SLinus Torvalds 	/* source must exist */
33461da177e4SLinus Torvalds 	error = -ENOENT;
33471da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
33481da177e4SLinus Torvalds 		goto exit4;
33491da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
33501da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
33511da177e4SLinus Torvalds 		error = -ENOTDIR;
33521da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
33531da177e4SLinus Torvalds 			goto exit4;
33541da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
33551da177e4SLinus Torvalds 			goto exit4;
33561da177e4SLinus Torvalds 	}
33571da177e4SLinus Torvalds 	/* source should not be ancestor of target */
33581da177e4SLinus Torvalds 	error = -EINVAL;
33591da177e4SLinus Torvalds 	if (old_dentry == trap)
33601da177e4SLinus Torvalds 		goto exit4;
336149705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
33621da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
33631da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
33641da177e4SLinus Torvalds 		goto exit4;
33651da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
33661da177e4SLinus Torvalds 	error = -ENOTEMPTY;
33671da177e4SLinus Torvalds 	if (new_dentry == trap)
33681da177e4SLinus Torvalds 		goto exit5;
33691da177e4SLinus Torvalds 
33709079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
33719079b1ebSDave Hansen 	if (error)
33729079b1ebSDave Hansen 		goto exit5;
3373be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3374be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3375be6d3e56SKentaro Takeda 	if (error)
3376be6d3e56SKentaro Takeda 		goto exit6;
33771da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
33781da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3379be6d3e56SKentaro Takeda exit6:
33809079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
33811da177e4SLinus Torvalds exit5:
33821da177e4SLinus Torvalds 	dput(new_dentry);
33831da177e4SLinus Torvalds exit4:
33841da177e4SLinus Torvalds 	dput(old_dentry);
33851da177e4SLinus Torvalds exit3:
33861da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
33871da177e4SLinus Torvalds exit2:
33881d957f9bSJan Blunck 	path_put(&newnd.path);
33892ad94ae6SAl Viro 	putname(to);
33901da177e4SLinus Torvalds exit1:
33911d957f9bSJan Blunck 	path_put(&oldnd.path);
33921da177e4SLinus Torvalds 	putname(from);
33932ad94ae6SAl Viro exit:
33941da177e4SLinus Torvalds 	return error;
33951da177e4SLinus Torvalds }
33961da177e4SLinus Torvalds 
3397a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
33985590ff0dSUlrich Drepper {
33995590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
34005590ff0dSUlrich Drepper }
34015590ff0dSUlrich Drepper 
34021da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
34031da177e4SLinus Torvalds {
34041da177e4SLinus Torvalds 	int len;
34051da177e4SLinus Torvalds 
34061da177e4SLinus Torvalds 	len = PTR_ERR(link);
34071da177e4SLinus Torvalds 	if (IS_ERR(link))
34081da177e4SLinus Torvalds 		goto out;
34091da177e4SLinus Torvalds 
34101da177e4SLinus Torvalds 	len = strlen(link);
34111da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
34121da177e4SLinus Torvalds 		len = buflen;
34131da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
34141da177e4SLinus Torvalds 		len = -EFAULT;
34151da177e4SLinus Torvalds out:
34161da177e4SLinus Torvalds 	return len;
34171da177e4SLinus Torvalds }
34181da177e4SLinus Torvalds 
34191da177e4SLinus Torvalds /*
34201da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
34211da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
34221da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
34231da177e4SLinus Torvalds  */
34241da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
34251da177e4SLinus Torvalds {
34261da177e4SLinus Torvalds 	struct nameidata nd;
3427cc314eefSLinus Torvalds 	void *cookie;
3428694a1764SMarcin Slusarz 	int res;
3429cc314eefSLinus Torvalds 
34301da177e4SLinus Torvalds 	nd.depth = 0;
3431cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3432694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3433694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3434694a1764SMarcin Slusarz 
3435694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
34361da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3437cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3438694a1764SMarcin Slusarz 	return res;
34391da177e4SLinus Torvalds }
34401da177e4SLinus Torvalds 
34411da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
34421da177e4SLinus Torvalds {
34431da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
34441da177e4SLinus Torvalds }
34451da177e4SLinus Torvalds 
34461da177e4SLinus Torvalds /* get the link contents into pagecache */
34471da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
34481da177e4SLinus Torvalds {
3449ebd09abbSDuane Griffin 	char *kaddr;
34501da177e4SLinus Torvalds 	struct page *page;
34511da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3452090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
34531da177e4SLinus Torvalds 	if (IS_ERR(page))
34546fe6900eSNick Piggin 		return (char*)page;
34551da177e4SLinus Torvalds 	*ppage = page;
3456ebd09abbSDuane Griffin 	kaddr = kmap(page);
3457ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3458ebd09abbSDuane Griffin 	return kaddr;
34591da177e4SLinus Torvalds }
34601da177e4SLinus Torvalds 
34611da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
34621da177e4SLinus Torvalds {
34631da177e4SLinus Torvalds 	struct page *page = NULL;
34641da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
34651da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
34661da177e4SLinus Torvalds 	if (page) {
34671da177e4SLinus Torvalds 		kunmap(page);
34681da177e4SLinus Torvalds 		page_cache_release(page);
34691da177e4SLinus Torvalds 	}
34701da177e4SLinus Torvalds 	return res;
34711da177e4SLinus Torvalds }
34721da177e4SLinus Torvalds 
3473cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
34741da177e4SLinus Torvalds {
3475cc314eefSLinus Torvalds 	struct page *page = NULL;
34761da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3477cc314eefSLinus Torvalds 	return page;
34781da177e4SLinus Torvalds }
34791da177e4SLinus Torvalds 
3480cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
34811da177e4SLinus Torvalds {
3482cc314eefSLinus Torvalds 	struct page *page = cookie;
3483cc314eefSLinus Torvalds 
3484cc314eefSLinus Torvalds 	if (page) {
34851da177e4SLinus Torvalds 		kunmap(page);
34861da177e4SLinus Torvalds 		page_cache_release(page);
34871da177e4SLinus Torvalds 	}
34881da177e4SLinus Torvalds }
34891da177e4SLinus Torvalds 
349054566b2cSNick Piggin /*
349154566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
349254566b2cSNick Piggin  */
349354566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
34941da177e4SLinus Torvalds {
34951da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
34960adb25d2SKirill Korotaev 	struct page *page;
3497afddba49SNick Piggin 	void *fsdata;
3498beb497abSDmitriy Monakhov 	int err;
34991da177e4SLinus Torvalds 	char *kaddr;
350054566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
350154566b2cSNick Piggin 	if (nofs)
350254566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
35031da177e4SLinus Torvalds 
35047e53cac4SNeilBrown retry:
3505afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
350654566b2cSNick Piggin 				flags, &page, &fsdata);
35071da177e4SLinus Torvalds 	if (err)
3508afddba49SNick Piggin 		goto fail;
3509afddba49SNick Piggin 
3510e8e3c3d6SCong Wang 	kaddr = kmap_atomic(page);
35111da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
3512e8e3c3d6SCong Wang 	kunmap_atomic(kaddr);
3513afddba49SNick Piggin 
3514afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3515afddba49SNick Piggin 							page, fsdata);
35161da177e4SLinus Torvalds 	if (err < 0)
35171da177e4SLinus Torvalds 		goto fail;
3518afddba49SNick Piggin 	if (err < len-1)
3519afddba49SNick Piggin 		goto retry;
3520afddba49SNick Piggin 
35211da177e4SLinus Torvalds 	mark_inode_dirty(inode);
35221da177e4SLinus Torvalds 	return 0;
35231da177e4SLinus Torvalds fail:
35241da177e4SLinus Torvalds 	return err;
35251da177e4SLinus Torvalds }
35261da177e4SLinus Torvalds 
35270adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
35280adb25d2SKirill Korotaev {
35290adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
353054566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
35310adb25d2SKirill Korotaev }
35320adb25d2SKirill Korotaev 
353392e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
35341da177e4SLinus Torvalds 	.readlink	= generic_readlink,
35351da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
35361da177e4SLinus Torvalds 	.put_link	= page_put_link,
35371da177e4SLinus Torvalds };
35381da177e4SLinus Torvalds 
35392d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
3540cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
35411da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
35421da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
35431da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
35441da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
35451da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
35461da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
35471da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
35481da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
35491da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
35500adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
35511da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
35521da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
3553d1811465SAl Viro EXPORT_SYMBOL(kern_path);
355416f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3555f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
35561da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
35571da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
35581da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
35591da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
35601da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
35611da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
35621da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
35631da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
35641da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
35651da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
35661da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
35671da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
35681da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
35691da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3570