xref: /openbmc/linux/fs/namei.c (revision 8e96e3b7)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/namei.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  * Some corrections by tytso.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
121da177e4SLinus Torvalds  * lookup logic.
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/init.h>
18630d9c47SPaul Gortmaker #include <linux/export.h>
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 
2318e96e3b7SEric W. Biederman 	if (likely(uid_eq(current_fsuid(), inode->i_uid)))
2325909ccaaSLinus Torvalds 		mode >>= 6;
2335909ccaaSLinus Torvalds 	else {
234e77819e5SLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
2357e40145eSAl Viro 			int error = check_acl(inode, mask);
2365909ccaaSLinus Torvalds 			if (error != -EAGAIN)
2375909ccaaSLinus Torvalds 				return error;
2385909ccaaSLinus Torvalds 		}
2395909ccaaSLinus Torvalds 
2405909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
2415909ccaaSLinus Torvalds 			mode >>= 3;
2425909ccaaSLinus Torvalds 	}
2435909ccaaSLinus Torvalds 
2445909ccaaSLinus Torvalds 	/*
2455909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
2465909ccaaSLinus Torvalds 	 */
2479c2c7039SAl Viro 	if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
2485909ccaaSLinus Torvalds 		return 0;
2495909ccaaSLinus Torvalds 	return -EACCES;
2505909ccaaSLinus Torvalds }
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds /**
2531da177e4SLinus Torvalds  * generic_permission -  check for access rights on a Posix-like filesystem
2541da177e4SLinus Torvalds  * @inode:	inode to check access rights for
2558fd90c8dSAndreas Gruenbacher  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
2561da177e4SLinus Torvalds  *
2571da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
2581da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
2591da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
260b74c79e9SNick Piggin  * are used for other things.
261b74c79e9SNick Piggin  *
262b74c79e9SNick Piggin  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
263b74c79e9SNick Piggin  * request cannot be satisfied (eg. requires blocking or too much complexity).
264b74c79e9SNick Piggin  * It would then be called again in ref-walk mode.
2651da177e4SLinus Torvalds  */
2662830ba7fSAl Viro int generic_permission(struct inode *inode, int mask)
2671da177e4SLinus Torvalds {
2685909ccaaSLinus Torvalds 	int ret;
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	/*
271948409c7SAndreas Gruenbacher 	 * Do the basic permission checks.
2721da177e4SLinus Torvalds 	 */
2737e40145eSAl Viro 	ret = acl_permission_check(inode, mask);
2745909ccaaSLinus Torvalds 	if (ret != -EACCES)
2755909ccaaSLinus Torvalds 		return ret;
2761da177e4SLinus Torvalds 
277d594e7ecSAl Viro 	if (S_ISDIR(inode->i_mode)) {
278d594e7ecSAl Viro 		/* DACs are overridable for directories */
2791a48e2acSEric W. Biederman 		if (inode_capable(inode, CAP_DAC_OVERRIDE))
280d594e7ecSAl Viro 			return 0;
281d594e7ecSAl Viro 		if (!(mask & MAY_WRITE))
2821a48e2acSEric W. Biederman 			if (inode_capable(inode, CAP_DAC_READ_SEARCH))
283d594e7ecSAl Viro 				return 0;
284d594e7ecSAl Viro 		return -EACCES;
285d594e7ecSAl Viro 	}
2861da177e4SLinus Torvalds 	/*
2871da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
288d594e7ecSAl Viro 	 * Executable DACs are overridable when there is
289d594e7ecSAl Viro 	 * at least one exec bit set.
2901da177e4SLinus Torvalds 	 */
291d594e7ecSAl Viro 	if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
2921a48e2acSEric W. Biederman 		if (inode_capable(inode, CAP_DAC_OVERRIDE))
2931da177e4SLinus Torvalds 			return 0;
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	/*
2961da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
2971da177e4SLinus Torvalds 	 */
2987ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
299d594e7ecSAl Viro 	if (mask == MAY_READ)
3001a48e2acSEric W. Biederman 		if (inode_capable(inode, CAP_DAC_READ_SEARCH))
3011da177e4SLinus Torvalds 			return 0;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	return -EACCES;
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds 
3063ddcd056SLinus Torvalds /*
3073ddcd056SLinus Torvalds  * We _really_ want to just do "generic_permission()" without
3083ddcd056SLinus Torvalds  * even looking at the inode->i_op values. So we keep a cache
3093ddcd056SLinus Torvalds  * flag in inode->i_opflags, that says "this has not special
3103ddcd056SLinus Torvalds  * permission function, use the fast case".
3113ddcd056SLinus Torvalds  */
3123ddcd056SLinus Torvalds static inline int do_inode_permission(struct inode *inode, int mask)
3133ddcd056SLinus Torvalds {
3143ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
3153ddcd056SLinus Torvalds 		if (likely(inode->i_op->permission))
3163ddcd056SLinus Torvalds 			return inode->i_op->permission(inode, mask);
3173ddcd056SLinus Torvalds 
3183ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
3193ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
3203ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_FASTPERM;
3213ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
3223ddcd056SLinus Torvalds 	}
3233ddcd056SLinus Torvalds 	return generic_permission(inode, mask);
3243ddcd056SLinus Torvalds }
3253ddcd056SLinus Torvalds 
326cb23beb5SChristoph Hellwig /**
327cb23beb5SChristoph Hellwig  * inode_permission  -  check for access rights to a given inode
328cb23beb5SChristoph Hellwig  * @inode:	inode to check permission on
3298fd90c8dSAndreas Gruenbacher  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
330cb23beb5SChristoph Hellwig  *
331cb23beb5SChristoph Hellwig  * Used to check for read/write/execute permissions on an inode.
332cb23beb5SChristoph Hellwig  * We use "fsuid" for this, letting us set arbitrary permissions
333cb23beb5SChristoph Hellwig  * for filesystem access without changing the "normal" uids which
334cb23beb5SChristoph Hellwig  * are used for other things.
335948409c7SAndreas Gruenbacher  *
336948409c7SAndreas Gruenbacher  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
337cb23beb5SChristoph Hellwig  */
338f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask)
3391da177e4SLinus Torvalds {
340e6305c43SAl Viro 	int retval;
3411da177e4SLinus Torvalds 
3423ddcd056SLinus Torvalds 	if (unlikely(mask & MAY_WRITE)) {
34322590e41SMiklos Szeredi 		umode_t mode = inode->i_mode;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 		/*
3461da177e4SLinus Torvalds 		 * Nobody gets write access to a read-only fs.
3471da177e4SLinus Torvalds 		 */
3481da177e4SLinus Torvalds 		if (IS_RDONLY(inode) &&
3491da177e4SLinus Torvalds 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
3501da177e4SLinus Torvalds 			return -EROFS;
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 		/*
3531da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
3541da177e4SLinus Torvalds 		 */
3551da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
3561da177e4SLinus Torvalds 			return -EACCES;
3571da177e4SLinus Torvalds 	}
3581da177e4SLinus Torvalds 
3593ddcd056SLinus Torvalds 	retval = do_inode_permission(inode, mask);
3601da177e4SLinus Torvalds 	if (retval)
3611da177e4SLinus Torvalds 		return retval;
3621da177e4SLinus Torvalds 
36308ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
36408ce5f16SSerge E. Hallyn 	if (retval)
36508ce5f16SSerge E. Hallyn 		return retval;
36608ce5f16SSerge E. Hallyn 
367d09ca739SEric Paris 	return security_inode_permission(inode, mask);
3681da177e4SLinus Torvalds }
3691da177e4SLinus Torvalds 
370f4d6ff89SAl Viro /**
3715dd784d0SJan Blunck  * path_get - get a reference to a path
3725dd784d0SJan Blunck  * @path: path to get the reference to
3735dd784d0SJan Blunck  *
3745dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
3755dd784d0SJan Blunck  */
3765dd784d0SJan Blunck void path_get(struct path *path)
3775dd784d0SJan Blunck {
3785dd784d0SJan Blunck 	mntget(path->mnt);
3795dd784d0SJan Blunck 	dget(path->dentry);
3805dd784d0SJan Blunck }
3815dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
3825dd784d0SJan Blunck 
3835dd784d0SJan Blunck /**
3841d957f9bSJan Blunck  * path_put - put a reference to a path
3851d957f9bSJan Blunck  * @path: path to put the reference to
3861d957f9bSJan Blunck  *
3871d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
3881d957f9bSJan Blunck  */
3891d957f9bSJan Blunck void path_put(struct path *path)
3901da177e4SLinus Torvalds {
3911d957f9bSJan Blunck 	dput(path->dentry);
3921d957f9bSJan Blunck 	mntput(path->mnt);
3931da177e4SLinus Torvalds }
3941d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
3951da177e4SLinus Torvalds 
39619660af7SAl Viro /*
39731e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
39819660af7SAl Viro  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
39919660af7SAl Viro  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
40019660af7SAl Viro  * normal reference counts on dentries and vfsmounts to transition to rcu-walk
40119660af7SAl Viro  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
40219660af7SAl Viro  * got stuck, so ref-walk may continue from there. If this is not successful
40319660af7SAl Viro  * (eg. a seqcount has changed), then failure is returned and it's up to caller
40419660af7SAl Viro  * to restart the path walk from the beginning in ref-walk mode.
40531e6b01fSNick Piggin  */
40631e6b01fSNick Piggin 
40731e6b01fSNick Piggin /**
40819660af7SAl Viro  * unlazy_walk - try to switch to ref-walk mode.
40919660af7SAl Viro  * @nd: nameidata pathwalk data
41019660af7SAl Viro  * @dentry: child of nd->path.dentry or NULL
41139191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
41231e6b01fSNick Piggin  *
41319660af7SAl Viro  * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
41419660af7SAl Viro  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
41519660af7SAl Viro  * @nd or NULL.  Must be called from rcu-walk context.
41631e6b01fSNick Piggin  */
41719660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
41831e6b01fSNick Piggin {
41931e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
42031e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
4215b6ca027SAl Viro 	int want_root = 0;
42231e6b01fSNick Piggin 
42331e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
4245b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
4255b6ca027SAl Viro 		want_root = 1;
42631e6b01fSNick Piggin 		spin_lock(&fs->lock);
42731e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
42831e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
42931e6b01fSNick Piggin 			goto err_root;
43031e6b01fSNick Piggin 	}
43131e6b01fSNick Piggin 	spin_lock(&parent->d_lock);
43219660af7SAl Viro 	if (!dentry) {
43319660af7SAl Viro 		if (!__d_rcu_to_refcount(parent, nd->seq))
43419660af7SAl Viro 			goto err_parent;
43519660af7SAl Viro 		BUG_ON(nd->inode != parent->d_inode);
43619660af7SAl Viro 	} else {
43794c0d4ecSAl Viro 		if (dentry->d_parent != parent)
43894c0d4ecSAl Viro 			goto err_parent;
43931e6b01fSNick Piggin 		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
44031e6b01fSNick Piggin 		if (!__d_rcu_to_refcount(dentry, nd->seq))
44119660af7SAl Viro 			goto err_child;
44231e6b01fSNick Piggin 		/*
44319660af7SAl Viro 		 * If the sequence check on the child dentry passed, then
44419660af7SAl Viro 		 * the child has not been removed from its parent. This
44519660af7SAl Viro 		 * means the parent dentry must be valid and able to take
44619660af7SAl Viro 		 * a reference at this point.
44731e6b01fSNick Piggin 		 */
44831e6b01fSNick Piggin 		BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
44931e6b01fSNick Piggin 		BUG_ON(!parent->d_count);
45031e6b01fSNick Piggin 		parent->d_count++;
45131e6b01fSNick Piggin 		spin_unlock(&dentry->d_lock);
45219660af7SAl Viro 	}
45331e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
4545b6ca027SAl Viro 	if (want_root) {
45531e6b01fSNick Piggin 		path_get(&nd->root);
45631e6b01fSNick Piggin 		spin_unlock(&fs->lock);
45731e6b01fSNick Piggin 	}
45831e6b01fSNick Piggin 	mntget(nd->path.mnt);
45931e6b01fSNick Piggin 
46031e6b01fSNick Piggin 	rcu_read_unlock();
46131e6b01fSNick Piggin 	br_read_unlock(vfsmount_lock);
46231e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
46331e6b01fSNick Piggin 	return 0;
46419660af7SAl Viro 
46519660af7SAl Viro err_child:
46631e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
46719660af7SAl Viro err_parent:
46831e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
46931e6b01fSNick Piggin err_root:
4705b6ca027SAl Viro 	if (want_root)
47131e6b01fSNick Piggin 		spin_unlock(&fs->lock);
47231e6b01fSNick Piggin 	return -ECHILD;
47331e6b01fSNick Piggin }
47431e6b01fSNick Piggin 
47531e6b01fSNick Piggin /**
476834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
477834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
478834f2a4aSTrond Myklebust  */
479834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
480834f2a4aSTrond Myklebust {
4812dab5974SLinus Torvalds 	struct file *file = nd->intent.open.file;
4822dab5974SLinus Torvalds 
4832dab5974SLinus Torvalds 	if (file && !IS_ERR(file)) {
4842dab5974SLinus Torvalds 		if (file->f_path.dentry == NULL)
4852dab5974SLinus Torvalds 			put_filp(file);
486834f2a4aSTrond Myklebust 		else
4872dab5974SLinus Torvalds 			fput(file);
4882dab5974SLinus Torvalds 	}
489834f2a4aSTrond Myklebust }
490834f2a4aSTrond Myklebust 
491f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
49234286d66SNick Piggin {
493f60aef7eSAl Viro 	return dentry->d_op->d_revalidate(dentry, nd);
49434286d66SNick Piggin }
49534286d66SNick Piggin 
4969f1fafeeSAl Viro /**
4979f1fafeeSAl Viro  * complete_walk - successful completion of path walk
4989f1fafeeSAl Viro  * @nd:  pointer nameidata
49939159de2SJeff Layton  *
5009f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
5019f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
5029f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
5039f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
5049f1fafeeSAl Viro  * need to drop nd->path.
50539159de2SJeff Layton  */
5069f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
50739159de2SJeff Layton {
50816c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
50939159de2SJeff Layton 	int status;
51039159de2SJeff Layton 
5119f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
5129f1fafeeSAl Viro 		nd->flags &= ~LOOKUP_RCU;
5139f1fafeeSAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
5149f1fafeeSAl Viro 			nd->root.mnt = NULL;
5159f1fafeeSAl Viro 		spin_lock(&dentry->d_lock);
5169f1fafeeSAl Viro 		if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
5179f1fafeeSAl Viro 			spin_unlock(&dentry->d_lock);
5189f1fafeeSAl Viro 			rcu_read_unlock();
5199f1fafeeSAl Viro 			br_read_unlock(vfsmount_lock);
5209f1fafeeSAl Viro 			return -ECHILD;
5219f1fafeeSAl Viro 		}
5229f1fafeeSAl Viro 		BUG_ON(nd->inode != dentry->d_inode);
5239f1fafeeSAl Viro 		spin_unlock(&dentry->d_lock);
5249f1fafeeSAl Viro 		mntget(nd->path.mnt);
5259f1fafeeSAl Viro 		rcu_read_unlock();
5269f1fafeeSAl Viro 		br_read_unlock(vfsmount_lock);
5279f1fafeeSAl Viro 	}
5289f1fafeeSAl Viro 
52916c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
53039159de2SJeff Layton 		return 0;
53139159de2SJeff Layton 
53216c2cd71SAl Viro 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
53316c2cd71SAl Viro 		return 0;
53416c2cd71SAl Viro 
53516c2cd71SAl Viro 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
53616c2cd71SAl Viro 		return 0;
53716c2cd71SAl Viro 
53816c2cd71SAl Viro 	/* Note: we do not d_invalidate() */
53934286d66SNick Piggin 	status = d_revalidate(dentry, nd);
54039159de2SJeff Layton 	if (status > 0)
54139159de2SJeff Layton 		return 0;
54239159de2SJeff Layton 
54316c2cd71SAl Viro 	if (!status)
54439159de2SJeff Layton 		status = -ESTALE;
54516c2cd71SAl Viro 
5469f1fafeeSAl Viro 	path_put(&nd->path);
54739159de2SJeff Layton 	return status;
54839159de2SJeff Layton }
54939159de2SJeff Layton 
5502a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
5512a737871SAl Viro {
552f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
553f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
5542a737871SAl Viro }
5552a737871SAl Viro 
5566de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
5576de88d72SAl Viro 
55831e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
55931e6b01fSNick Piggin {
56031e6b01fSNick Piggin 	if (!nd->root.mnt) {
56131e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
562c28cc364SNick Piggin 		unsigned seq;
563c28cc364SNick Piggin 
564c28cc364SNick Piggin 		do {
565c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
56631e6b01fSNick Piggin 			nd->root = fs->root;
567c1530019STim Chen 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
568c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
56931e6b01fSNick Piggin 	}
57031e6b01fSNick Piggin }
57131e6b01fSNick Piggin 
572f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
5731da177e4SLinus Torvalds {
57431e6b01fSNick Piggin 	int ret;
57531e6b01fSNick Piggin 
5761da177e4SLinus Torvalds 	if (IS_ERR(link))
5771da177e4SLinus Torvalds 		goto fail;
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	if (*link == '/') {
5802a737871SAl Viro 		set_root(nd);
5811d957f9bSJan Blunck 		path_put(&nd->path);
5822a737871SAl Viro 		nd->path = nd->root;
5832a737871SAl Viro 		path_get(&nd->root);
58416c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
5851da177e4SLinus Torvalds 	}
58631e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
587b4091d5fSChristoph Hellwig 
58831e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
58931e6b01fSNick Piggin 	return ret;
5901da177e4SLinus Torvalds fail:
5911d957f9bSJan Blunck 	path_put(&nd->path);
5921da177e4SLinus Torvalds 	return PTR_ERR(link);
5931da177e4SLinus Torvalds }
5941da177e4SLinus Torvalds 
5951d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
596051d3812SIan Kent {
597051d3812SIan Kent 	dput(path->dentry);
5984ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
599051d3812SIan Kent 		mntput(path->mnt);
600051d3812SIan Kent }
601051d3812SIan Kent 
6027b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
6037b9337aaSNick Piggin 					struct nameidata *nd)
604051d3812SIan Kent {
60531e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
6064ac91378SJan Blunck 		dput(nd->path.dentry);
60731e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
6084ac91378SJan Blunck 			mntput(nd->path.mnt);
6099a229683SHuang Shijie 	}
61031e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
6114ac91378SJan Blunck 	nd->path.dentry = path->dentry;
612051d3812SIan Kent }
613051d3812SIan Kent 
614574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
615574197e0SAl Viro {
616574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
617574197e0SAl Viro 	if (!IS_ERR(cookie) && inode->i_op->put_link)
618574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
619574197e0SAl Viro 	path_put(link);
620574197e0SAl Viro }
621574197e0SAl Viro 
622def4af30SAl Viro static __always_inline int
623574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
6241da177e4SLinus Torvalds {
6251da177e4SLinus Torvalds 	int error;
6267b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
6271da177e4SLinus Torvalds 
628844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
629844a3917SAl Viro 
6300e794589SAl Viro 	if (link->mnt == nd->path.mnt)
6310e794589SAl Viro 		mntget(link->mnt);
6320e794589SAl Viro 
633574197e0SAl Viro 	if (unlikely(current->total_link_count >= 40)) {
634574197e0SAl Viro 		*p = ERR_PTR(-ELOOP); /* no ->put_link(), please */
635574197e0SAl Viro 		path_put(&nd->path);
636574197e0SAl Viro 		return -ELOOP;
637574197e0SAl Viro 	}
638574197e0SAl Viro 	cond_resched();
639574197e0SAl Viro 	current->total_link_count++;
640574197e0SAl Viro 
64168ac1234SAl Viro 	touch_atime(link);
6421da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
643cd4e91d3SAl Viro 
64436f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
64536f3b4f6SAl Viro 	if (error) {
64636f3b4f6SAl Viro 		*p = ERR_PTR(error); /* no ->put_link(), please */
64736f3b4f6SAl Viro 		path_put(&nd->path);
64836f3b4f6SAl Viro 		return error;
64936f3b4f6SAl Viro 	}
65036f3b4f6SAl Viro 
65186acdca1SAl Viro 	nd->last_type = LAST_BIND;
652def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
653def4af30SAl Viro 	error = PTR_ERR(*p);
654def4af30SAl Viro 	if (!IS_ERR(*p)) {
6551da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
656cc314eefSLinus Torvalds 		error = 0;
6571da177e4SLinus Torvalds 		if (s)
6581da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
659bcda7652SAl Viro 		else if (nd->last_type == LAST_BIND) {
66016c2cd71SAl Viro 			nd->flags |= LOOKUP_JUMPED;
661b21041d0SAl Viro 			nd->inode = nd->path.dentry->d_inode;
662b21041d0SAl Viro 			if (nd->inode->i_op->follow_link) {
663bcda7652SAl Viro 				/* stepped on a _really_ weird one */
664bcda7652SAl Viro 				path_put(&nd->path);
665bcda7652SAl Viro 				error = -ELOOP;
666bcda7652SAl Viro 			}
667bcda7652SAl Viro 		}
6681da177e4SLinus Torvalds 	}
6691da177e4SLinus Torvalds 	return error;
6701da177e4SLinus Torvalds }
6711da177e4SLinus Torvalds 
67231e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
67331e6b01fSNick Piggin {
6740714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
6750714a533SAl Viro 	struct mount *parent;
67631e6b01fSNick Piggin 	struct dentry *mountpoint;
67731e6b01fSNick Piggin 
6780714a533SAl Viro 	parent = mnt->mnt_parent;
6790714a533SAl Viro 	if (&parent->mnt == path->mnt)
68031e6b01fSNick Piggin 		return 0;
681a73324daSAl Viro 	mountpoint = mnt->mnt_mountpoint;
68231e6b01fSNick Piggin 	path->dentry = mountpoint;
6830714a533SAl Viro 	path->mnt = &parent->mnt;
68431e6b01fSNick Piggin 	return 1;
68531e6b01fSNick Piggin }
68631e6b01fSNick Piggin 
687bab77ebfSAl Viro int follow_up(struct path *path)
6881da177e4SLinus Torvalds {
6890714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
6900714a533SAl Viro 	struct mount *parent;
6911da177e4SLinus Torvalds 	struct dentry *mountpoint;
69299b7db7bSNick Piggin 
69399b7db7bSNick Piggin 	br_read_lock(vfsmount_lock);
6940714a533SAl Viro 	parent = mnt->mnt_parent;
6950714a533SAl Viro 	if (&parent->mnt == path->mnt) {
69699b7db7bSNick Piggin 		br_read_unlock(vfsmount_lock);
6971da177e4SLinus Torvalds 		return 0;
6981da177e4SLinus Torvalds 	}
6990714a533SAl Viro 	mntget(&parent->mnt);
700a73324daSAl Viro 	mountpoint = dget(mnt->mnt_mountpoint);
70199b7db7bSNick Piggin 	br_read_unlock(vfsmount_lock);
702bab77ebfSAl Viro 	dput(path->dentry);
703bab77ebfSAl Viro 	path->dentry = mountpoint;
704bab77ebfSAl Viro 	mntput(path->mnt);
7050714a533SAl Viro 	path->mnt = &parent->mnt;
7061da177e4SLinus Torvalds 	return 1;
7071da177e4SLinus Torvalds }
7081da177e4SLinus Torvalds 
709b5c84bf6SNick Piggin /*
7109875cf80SDavid Howells  * Perform an automount
7119875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
7129875cf80SDavid Howells  *   were called with.
7131da177e4SLinus Torvalds  */
7149875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
7159875cf80SDavid Howells 			    bool *need_mntput)
71631e6b01fSNick Piggin {
7179875cf80SDavid Howells 	struct vfsmount *mnt;
718ea5b778aSDavid Howells 	int err;
7199875cf80SDavid Howells 
7209875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
7219875cf80SDavid Howells 		return -EREMOTE;
7229875cf80SDavid Howells 
7230ec26fd0SMiklos Szeredi 	/* We don't want to mount if someone's just doing a stat -
7240ec26fd0SMiklos Szeredi 	 * unless they're stat'ing a directory and appended a '/' to
7250ec26fd0SMiklos Szeredi 	 * the name.
7260ec26fd0SMiklos Szeredi 	 *
7270ec26fd0SMiklos Szeredi 	 * We do, however, want to mount if someone wants to open or
7285a30d8a2SDavid Howells 	 * create a file of any type under the mountpoint, wants to
7290ec26fd0SMiklos Szeredi 	 * traverse through the mountpoint or wants to open the
7300ec26fd0SMiklos Szeredi 	 * mounted directory.  Also, autofs may mark negative dentries
7310ec26fd0SMiklos Szeredi 	 * as being automount points.  These will need the attentions
7320ec26fd0SMiklos Szeredi 	 * of the daemon to instantiate them before they can be used.
7335a30d8a2SDavid Howells 	 */
7345a30d8a2SDavid Howells 	if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
735d94c177bSLinus Torvalds 		     LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
7365a30d8a2SDavid Howells 	    path->dentry->d_inode)
7379875cf80SDavid Howells 		return -EISDIR;
7380ec26fd0SMiklos Szeredi 
7399875cf80SDavid Howells 	current->total_link_count++;
7409875cf80SDavid Howells 	if (current->total_link_count >= 40)
7419875cf80SDavid Howells 		return -ELOOP;
7429875cf80SDavid Howells 
7439875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
7449875cf80SDavid Howells 	if (IS_ERR(mnt)) {
7459875cf80SDavid Howells 		/*
7469875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
7479875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
7489875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
7499875cf80SDavid Howells 		 *
7509875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
7519875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
7529875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
7539875cf80SDavid Howells 		 */
75449084c3bSAl Viro 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
7559875cf80SDavid Howells 			return -EREMOTE;
7569875cf80SDavid Howells 		return PTR_ERR(mnt);
75731e6b01fSNick Piggin 	}
758ea5b778aSDavid Howells 
7599875cf80SDavid Howells 	if (!mnt) /* mount collision */
7609875cf80SDavid Howells 		return 0;
7619875cf80SDavid Howells 
7628aef1884SAl Viro 	if (!*need_mntput) {
7638aef1884SAl Viro 		/* lock_mount() may release path->mnt on error */
7648aef1884SAl Viro 		mntget(path->mnt);
7658aef1884SAl Viro 		*need_mntput = true;
7668aef1884SAl Viro 	}
76719a167afSAl Viro 	err = finish_automount(mnt, path);
768ea5b778aSDavid Howells 
769ea5b778aSDavid Howells 	switch (err) {
770ea5b778aSDavid Howells 	case -EBUSY:
771ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
77219a167afSAl Viro 		return 0;
773ea5b778aSDavid Howells 	case 0:
7748aef1884SAl Viro 		path_put(path);
7759875cf80SDavid Howells 		path->mnt = mnt;
7769875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
7779875cf80SDavid Howells 		return 0;
77819a167afSAl Viro 	default:
77919a167afSAl Viro 		return err;
7809875cf80SDavid Howells 	}
78119a167afSAl Viro 
782ea5b778aSDavid Howells }
7839875cf80SDavid Howells 
7849875cf80SDavid Howells /*
7859875cf80SDavid Howells  * Handle a dentry that is managed in some way.
786cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
7879875cf80SDavid Howells  * - Flagged as mountpoint
7889875cf80SDavid Howells  * - Flagged as automount point
7899875cf80SDavid Howells  *
7909875cf80SDavid Howells  * This may only be called in refwalk mode.
7919875cf80SDavid Howells  *
7929875cf80SDavid Howells  * Serialization is taken care of in namespace.c
7939875cf80SDavid Howells  */
7949875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
7959875cf80SDavid Howells {
7968aef1884SAl Viro 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
7979875cf80SDavid Howells 	unsigned managed;
7989875cf80SDavid Howells 	bool need_mntput = false;
7998aef1884SAl Viro 	int ret = 0;
8009875cf80SDavid Howells 
8019875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
8029875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
8039875cf80SDavid Howells 	 * the components of that value change under us */
8049875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
8059875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
8069875cf80SDavid Howells 	       unlikely(managed != 0)) {
807cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
808cc53ce53SDavid Howells 		 * being held. */
809cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
810cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
811cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
8121aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
813cc53ce53SDavid Howells 			if (ret < 0)
8148aef1884SAl Viro 				break;
815cc53ce53SDavid Howells 		}
816cc53ce53SDavid Howells 
8179875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
8189875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
8199875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
8209875cf80SDavid Howells 			if (mounted) {
8219875cf80SDavid Howells 				dput(path->dentry);
8229875cf80SDavid Howells 				if (need_mntput)
823463ffb2eSAl Viro 					mntput(path->mnt);
824463ffb2eSAl Viro 				path->mnt = mounted;
825463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
8269875cf80SDavid Howells 				need_mntput = true;
8279875cf80SDavid Howells 				continue;
828463ffb2eSAl Viro 			}
829463ffb2eSAl Viro 
8309875cf80SDavid Howells 			/* Something is mounted on this dentry in another
8319875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
8329875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
8339875cf80SDavid Howells 			 * vfsmount_lock */
8341da177e4SLinus Torvalds 		}
8359875cf80SDavid Howells 
8369875cf80SDavid Howells 		/* Handle an automount point */
8379875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
8389875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
8399875cf80SDavid Howells 			if (ret < 0)
8408aef1884SAl Viro 				break;
8419875cf80SDavid Howells 			continue;
8429875cf80SDavid Howells 		}
8439875cf80SDavid Howells 
8449875cf80SDavid Howells 		/* We didn't change the current path point */
8459875cf80SDavid Howells 		break;
8469875cf80SDavid Howells 	}
8478aef1884SAl Viro 
8488aef1884SAl Viro 	if (need_mntput && path->mnt == mnt)
8498aef1884SAl Viro 		mntput(path->mnt);
8508aef1884SAl Viro 	if (ret == -EISDIR)
8518aef1884SAl Viro 		ret = 0;
852a3fbbde7SAl Viro 	return ret < 0 ? ret : need_mntput;
8531da177e4SLinus Torvalds }
8541da177e4SLinus Torvalds 
855cc53ce53SDavid Howells int follow_down_one(struct path *path)
8561da177e4SLinus Torvalds {
8571da177e4SLinus Torvalds 	struct vfsmount *mounted;
8581da177e4SLinus Torvalds 
8591c755af4SAl Viro 	mounted = lookup_mnt(path);
8601da177e4SLinus Torvalds 	if (mounted) {
8619393bd07SAl Viro 		dput(path->dentry);
8629393bd07SAl Viro 		mntput(path->mnt);
8639393bd07SAl Viro 		path->mnt = mounted;
8649393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
8651da177e4SLinus Torvalds 		return 1;
8661da177e4SLinus Torvalds 	}
8671da177e4SLinus Torvalds 	return 0;
8681da177e4SLinus Torvalds }
8691da177e4SLinus Torvalds 
87062a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
87162a7375eSIan Kent {
87262a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
87362a7375eSIan Kent 		dentry->d_op->d_manage(dentry, true) < 0);
87462a7375eSIan Kent }
87562a7375eSIan Kent 
8769875cf80SDavid Howells /*
877287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
878287548e4SAl Viro  * we meet a managed dentry that would need blocking.
8799875cf80SDavid Howells  */
8809875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
881287548e4SAl Viro 			       struct inode **inode)
8829875cf80SDavid Howells {
88362a7375eSIan Kent 	for (;;) {
884c7105365SAl Viro 		struct mount *mounted;
88562a7375eSIan Kent 		/*
88662a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
88762a7375eSIan Kent 		 * that wants to block transit.
88862a7375eSIan Kent 		 */
889287548e4SAl Viro 		if (unlikely(managed_dentry_might_block(path->dentry)))
890ab90911fSDavid Howells 			return false;
89162a7375eSIan Kent 
89262a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
89362a7375eSIan Kent 			break;
89462a7375eSIan Kent 
8959875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
8969875cf80SDavid Howells 		if (!mounted)
8979875cf80SDavid Howells 			break;
898c7105365SAl Viro 		path->mnt = &mounted->mnt;
899c7105365SAl Viro 		path->dentry = mounted->mnt.mnt_root;
900a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
9019875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
90259430262SLinus Torvalds 		/*
90359430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
90459430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
90559430262SLinus Torvalds 		 * because a mount-point is always pinned.
90659430262SLinus Torvalds 		 */
90759430262SLinus Torvalds 		*inode = path->dentry->d_inode;
9089875cf80SDavid Howells 	}
9099875cf80SDavid Howells 	return true;
9109875cf80SDavid Howells }
9119875cf80SDavid Howells 
912dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd)
913287548e4SAl Viro {
914dea39376SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
915c7105365SAl Viro 		struct mount *mounted;
916dea39376SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
917287548e4SAl Viro 		if (!mounted)
918287548e4SAl Viro 			break;
919c7105365SAl Viro 		nd->path.mnt = &mounted->mnt;
920c7105365SAl Viro 		nd->path.dentry = mounted->mnt.mnt_root;
921dea39376SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
922287548e4SAl Viro 	}
923287548e4SAl Viro }
924287548e4SAl Viro 
92531e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
92631e6b01fSNick Piggin {
92731e6b01fSNick Piggin 	set_root_rcu(nd);
92831e6b01fSNick Piggin 
92931e6b01fSNick Piggin 	while (1) {
93031e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
93131e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
93231e6b01fSNick Piggin 			break;
93331e6b01fSNick Piggin 		}
93431e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
93531e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
93631e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
93731e6b01fSNick Piggin 			unsigned seq;
93831e6b01fSNick Piggin 
93931e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
94031e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
941ef7562d5SAl Viro 				goto failed;
94231e6b01fSNick Piggin 			nd->path.dentry = parent;
94331e6b01fSNick Piggin 			nd->seq = seq;
94431e6b01fSNick Piggin 			break;
94531e6b01fSNick Piggin 		}
94631e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
94731e6b01fSNick Piggin 			break;
94831e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
94931e6b01fSNick Piggin 	}
950dea39376SAl Viro 	follow_mount_rcu(nd);
951dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
95231e6b01fSNick Piggin 	return 0;
953ef7562d5SAl Viro 
954ef7562d5SAl Viro failed:
955ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
9565b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
957ef7562d5SAl Viro 		nd->root.mnt = NULL;
958ef7562d5SAl Viro 	rcu_read_unlock();
959ef7562d5SAl Viro 	br_read_unlock(vfsmount_lock);
960ef7562d5SAl Viro 	return -ECHILD;
96131e6b01fSNick Piggin }
96231e6b01fSNick Piggin 
9639875cf80SDavid Howells /*
964cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
965cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
966cc53ce53SDavid Howells  * caller is permitted to proceed or not.
967cc53ce53SDavid Howells  */
9687cc90cc3SAl Viro int follow_down(struct path *path)
969cc53ce53SDavid Howells {
970cc53ce53SDavid Howells 	unsigned managed;
971cc53ce53SDavid Howells 	int ret;
972cc53ce53SDavid Howells 
973cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
974cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
975cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
976cc53ce53SDavid Howells 		 * being held.
977cc53ce53SDavid Howells 		 *
978cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
979cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
980cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
981cc53ce53SDavid Howells 		 * superstructure.
982cc53ce53SDavid Howells 		 *
983cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
984cc53ce53SDavid Howells 		 */
985cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
986cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
987cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
988ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
9891aed3e42SAl Viro 				path->dentry, false);
990cc53ce53SDavid Howells 			if (ret < 0)
991cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
992cc53ce53SDavid Howells 		}
993cc53ce53SDavid Howells 
994cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
995cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
996cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
997cc53ce53SDavid Howells 			if (!mounted)
998cc53ce53SDavid Howells 				break;
999cc53ce53SDavid Howells 			dput(path->dentry);
1000cc53ce53SDavid Howells 			mntput(path->mnt);
1001cc53ce53SDavid Howells 			path->mnt = mounted;
1002cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1003cc53ce53SDavid Howells 			continue;
1004cc53ce53SDavid Howells 		}
1005cc53ce53SDavid Howells 
1006cc53ce53SDavid Howells 		/* Don't handle automount points here */
1007cc53ce53SDavid Howells 		break;
1008cc53ce53SDavid Howells 	}
1009cc53ce53SDavid Howells 	return 0;
1010cc53ce53SDavid Howells }
1011cc53ce53SDavid Howells 
1012cc53ce53SDavid Howells /*
10139875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
10149875cf80SDavid Howells  */
10159875cf80SDavid Howells static void follow_mount(struct path *path)
10169875cf80SDavid Howells {
10179875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
10189875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
10199875cf80SDavid Howells 		if (!mounted)
10209875cf80SDavid Howells 			break;
10219875cf80SDavid Howells 		dput(path->dentry);
10229875cf80SDavid Howells 		mntput(path->mnt);
10239875cf80SDavid Howells 		path->mnt = mounted;
10249875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
10259875cf80SDavid Howells 	}
10269875cf80SDavid Howells }
10279875cf80SDavid Howells 
102831e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
10291da177e4SLinus Torvalds {
10302a737871SAl Viro 	set_root(nd);
1031e518ddb7SAndreas Mohr 
10321da177e4SLinus Torvalds 	while(1) {
10334ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
10341da177e4SLinus Torvalds 
10352a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
10362a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
10371da177e4SLinus Torvalds 			break;
10381da177e4SLinus Torvalds 		}
10394ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
10403088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
10413088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
10421da177e4SLinus Torvalds 			dput(old);
10431da177e4SLinus Torvalds 			break;
10441da177e4SLinus Torvalds 		}
10453088dd70SAl Viro 		if (!follow_up(&nd->path))
10461da177e4SLinus Torvalds 			break;
10471da177e4SLinus Torvalds 	}
104879ed0226SAl Viro 	follow_mount(&nd->path);
104931e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
10501da177e4SLinus Torvalds }
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds /*
1053bad61189SMiklos Szeredi  * This looks up the name in dcache, possibly revalidates the old dentry and
1054bad61189SMiklos Szeredi  * allocates a new one if not found or not valid.  In the need_lookup argument
1055bad61189SMiklos Szeredi  * returns whether i_op->lookup is necessary.
1056bad61189SMiklos Szeredi  *
1057bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
1058baa03890SNick Piggin  */
1059bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1060bad61189SMiklos Szeredi 				    struct nameidata *nd, bool *need_lookup)
1061baa03890SNick Piggin {
1062baa03890SNick Piggin 	struct dentry *dentry;
1063bad61189SMiklos Szeredi 	int error;
1064baa03890SNick Piggin 
1065bad61189SMiklos Szeredi 	*need_lookup = false;
1066bad61189SMiklos Szeredi 	dentry = d_lookup(dir, name);
1067bad61189SMiklos Szeredi 	if (dentry) {
1068bad61189SMiklos Szeredi 		if (d_need_lookup(dentry)) {
1069bad61189SMiklos Szeredi 			*need_lookup = true;
1070bad61189SMiklos Szeredi 		} else if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1071bad61189SMiklos Szeredi 			error = d_revalidate(dentry, nd);
1072bad61189SMiklos Szeredi 			if (unlikely(error <= 0)) {
1073bad61189SMiklos Szeredi 				if (error < 0) {
1074bad61189SMiklos Szeredi 					dput(dentry);
1075bad61189SMiklos Szeredi 					return ERR_PTR(error);
1076bad61189SMiklos Szeredi 				} else if (!d_invalidate(dentry)) {
1077bad61189SMiklos Szeredi 					dput(dentry);
1078bad61189SMiklos Szeredi 					dentry = NULL;
1079bad61189SMiklos Szeredi 				}
1080bad61189SMiklos Szeredi 			}
1081bad61189SMiklos Szeredi 		}
1082bad61189SMiklos Szeredi 	}
1083baa03890SNick Piggin 
1084bad61189SMiklos Szeredi 	if (!dentry) {
1085bad61189SMiklos Szeredi 		dentry = d_alloc(dir, name);
1086baa03890SNick Piggin 		if (unlikely(!dentry))
1087baa03890SNick Piggin 			return ERR_PTR(-ENOMEM);
1088baa03890SNick Piggin 
1089bad61189SMiklos Szeredi 		*need_lookup = true;
1090baa03890SNick Piggin 	}
1091baa03890SNick Piggin 	return dentry;
1092baa03890SNick Piggin }
1093baa03890SNick Piggin 
1094baa03890SNick Piggin /*
1095bad61189SMiklos Szeredi  * Call i_op->lookup on the dentry.  The dentry must be negative but may be
1096bad61189SMiklos Szeredi  * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1097bad61189SMiklos Szeredi  *
1098bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
109944396f4bSJosef Bacik  */
1100bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
110144396f4bSJosef Bacik 				  struct nameidata *nd)
110244396f4bSJosef Bacik {
110344396f4bSJosef Bacik 	struct dentry *old;
110444396f4bSJosef Bacik 
110544396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
1106bad61189SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
1107e188dc02SMiklos Szeredi 		dput(dentry);
110844396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
1109e188dc02SMiklos Szeredi 	}
111044396f4bSJosef Bacik 
1111bad61189SMiklos Szeredi 	old = dir->i_op->lookup(dir, dentry, nd);
111244396f4bSJosef Bacik 	if (unlikely(old)) {
111344396f4bSJosef Bacik 		dput(dentry);
111444396f4bSJosef Bacik 		dentry = old;
111544396f4bSJosef Bacik 	}
111644396f4bSJosef Bacik 	return dentry;
111744396f4bSJosef Bacik }
111844396f4bSJosef Bacik 
1119a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name,
1120a3255546SAl Viro 		struct dentry *base, struct nameidata *nd)
1121a3255546SAl Viro {
1122bad61189SMiklos Szeredi 	bool need_lookup;
1123a3255546SAl Viro 	struct dentry *dentry;
1124a3255546SAl Viro 
1125bad61189SMiklos Szeredi 	dentry = lookup_dcache(name, base, nd, &need_lookup);
1126bad61189SMiklos Szeredi 	if (!need_lookup)
1127a3255546SAl Viro 		return dentry;
1128bad61189SMiklos Szeredi 
1129bad61189SMiklos Szeredi 	return lookup_real(base->d_inode, dentry, nd);
1130a3255546SAl Viro }
1131a3255546SAl Viro 
113244396f4bSJosef Bacik /*
11331da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
11341da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
11351da177e4SLinus Torvalds  *  It _is_ time-critical.
11361da177e4SLinus Torvalds  */
11371da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
113831e6b01fSNick Piggin 			struct path *path, struct inode **inode)
11391da177e4SLinus Torvalds {
11404ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
114131e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
11425a18fff2SAl Viro 	int need_reval = 1;
11435a18fff2SAl Viro 	int status = 1;
11449875cf80SDavid Howells 	int err;
11459875cf80SDavid Howells 
11463cac260aSAl Viro 	/*
1147b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1148b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1149b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1150b04f784eSNick Piggin 	 */
115131e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
115231e6b01fSNick Piggin 		unsigned seq;
115331e6b01fSNick Piggin 		*inode = nd->inode;
115431e6b01fSNick Piggin 		dentry = __d_lookup_rcu(parent, name, &seq, inode);
11555a18fff2SAl Viro 		if (!dentry)
11565a18fff2SAl Viro 			goto unlazy;
11575a18fff2SAl Viro 
115831e6b01fSNick Piggin 		/* Memory barrier in read_seqcount_begin of child is enough */
115931e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
116031e6b01fSNick Piggin 			return -ECHILD;
116131e6b01fSNick Piggin 		nd->seq = seq;
11625a18fff2SAl Viro 
1163fa4ee159SMiklos Szeredi 		if (unlikely(d_need_lookup(dentry)))
1164fa4ee159SMiklos Szeredi 			goto unlazy;
116524643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
11665a18fff2SAl Viro 			status = d_revalidate(dentry, nd);
11675a18fff2SAl Viro 			if (unlikely(status <= 0)) {
11685a18fff2SAl Viro 				if (status != -ECHILD)
11695a18fff2SAl Viro 					need_reval = 0;
11705a18fff2SAl Viro 				goto unlazy;
11715a18fff2SAl Viro 			}
117224643087SAl Viro 		}
117331e6b01fSNick Piggin 		path->mnt = mnt;
117431e6b01fSNick Piggin 		path->dentry = dentry;
1175d6e9bd25SAl Viro 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1176d6e9bd25SAl Viro 			goto unlazy;
1177d6e9bd25SAl Viro 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1178d6e9bd25SAl Viro 			goto unlazy;
11799875cf80SDavid Howells 		return 0;
11805a18fff2SAl Viro unlazy:
118119660af7SAl Viro 		if (unlazy_walk(nd, dentry))
11825a18fff2SAl Viro 			return -ECHILD;
11835a18fff2SAl Viro 	} else {
118431e6b01fSNick Piggin 		dentry = __d_lookup(parent, name);
118524643087SAl Viro 	}
11865a18fff2SAl Viro 
118781e6f520SAl Viro 	if (unlikely(!dentry))
118881e6f520SAl Viro 		goto need_lookup;
11895a18fff2SAl Viro 
119081e6f520SAl Viro 	if (unlikely(d_need_lookup(dentry))) {
119181e6f520SAl Viro 		dput(dentry);
119281e6f520SAl Viro 		goto need_lookup;
11935a18fff2SAl Viro 	}
119481e6f520SAl Viro 
11955a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
11965a18fff2SAl Viro 		status = d_revalidate(dentry, nd);
11975a18fff2SAl Viro 	if (unlikely(status <= 0)) {
11985a18fff2SAl Viro 		if (status < 0) {
11995a18fff2SAl Viro 			dput(dentry);
12005a18fff2SAl Viro 			return status;
12015a18fff2SAl Viro 		}
12025a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
12035a18fff2SAl Viro 			dput(dentry);
120481e6f520SAl Viro 			goto need_lookup;
12055a18fff2SAl Viro 		}
12065a18fff2SAl Viro 	}
12073f6c7c71SAl Viro done:
12081da177e4SLinus Torvalds 	path->mnt = mnt;
12091da177e4SLinus Torvalds 	path->dentry = dentry;
12109875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
121189312214SIan Kent 	if (unlikely(err < 0)) {
121289312214SIan Kent 		path_put_conditional(path, nd);
12139875cf80SDavid Howells 		return err;
121489312214SIan Kent 	}
1215a3fbbde7SAl Viro 	if (err)
1216a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
121731e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
12181da177e4SLinus Torvalds 	return 0;
121981e6f520SAl Viro 
122081e6f520SAl Viro need_lookup:
122181e6f520SAl Viro 	BUG_ON(nd->inode != parent->d_inode);
122281e6f520SAl Viro 
122381e6f520SAl Viro 	mutex_lock(&parent->d_inode->i_mutex);
122481e6f520SAl Viro 	dentry = __lookup_hash(name, parent, nd);
122581e6f520SAl Viro 	mutex_unlock(&parent->d_inode->i_mutex);
122681e6f520SAl Viro 	if (IS_ERR(dentry))
122781e6f520SAl Viro 		return PTR_ERR(dentry);
122881e6f520SAl Viro 	goto done;
12291da177e4SLinus Torvalds }
12301da177e4SLinus Torvalds 
123152094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
123252094c8aSAl Viro {
123352094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
12344ad5abb3SAl Viro 		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
123552094c8aSAl Viro 		if (err != -ECHILD)
123652094c8aSAl Viro 			return err;
123719660af7SAl Viro 		if (unlazy_walk(nd, NULL))
123852094c8aSAl Viro 			return -ECHILD;
123952094c8aSAl Viro 	}
12404ad5abb3SAl Viro 	return inode_permission(nd->inode, MAY_EXEC);
124152094c8aSAl Viro }
124252094c8aSAl Viro 
12439856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
12449856fa1bSAl Viro {
12459856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
12469856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
12479856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
12489856fa1bSAl Viro 				return -ECHILD;
12499856fa1bSAl Viro 		} else
12509856fa1bSAl Viro 			follow_dotdot(nd);
12519856fa1bSAl Viro 	}
12529856fa1bSAl Viro 	return 0;
12539856fa1bSAl Viro }
12549856fa1bSAl Viro 
1255951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1256951361f9SAl Viro {
1257951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1258951361f9SAl Viro 		path_put(&nd->path);
1259951361f9SAl Viro 	} else {
1260951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
12615b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1262951361f9SAl Viro 			nd->root.mnt = NULL;
1263951361f9SAl Viro 		rcu_read_unlock();
1264951361f9SAl Viro 		br_read_unlock(vfsmount_lock);
1265951361f9SAl Viro 	}
1266951361f9SAl Viro }
1267951361f9SAl Viro 
12683ddcd056SLinus Torvalds /*
12693ddcd056SLinus Torvalds  * Do we need to follow links? We _really_ want to be able
12703ddcd056SLinus Torvalds  * to do this check without having to look at inode->i_op,
12713ddcd056SLinus Torvalds  * so we keep a cache of "no, this doesn't need follow_link"
12723ddcd056SLinus Torvalds  * for the common case.
12733ddcd056SLinus Torvalds  */
12747813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow)
12753ddcd056SLinus Torvalds {
12763ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
12773ddcd056SLinus Torvalds 		if (likely(inode->i_op->follow_link))
12783ddcd056SLinus Torvalds 			return follow;
12793ddcd056SLinus Torvalds 
12803ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
12813ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
12823ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_NOFOLLOW;
12833ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
12843ddcd056SLinus Torvalds 	}
12853ddcd056SLinus Torvalds 	return 0;
12863ddcd056SLinus Torvalds }
12873ddcd056SLinus Torvalds 
1288ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
1289ce57dfc1SAl Viro 		struct qstr *name, int type, int follow)
1290ce57dfc1SAl Viro {
1291ce57dfc1SAl Viro 	struct inode *inode;
1292ce57dfc1SAl Viro 	int err;
1293ce57dfc1SAl Viro 	/*
1294ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1295ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1296ce57dfc1SAl Viro 	 * parent relationships.
1297ce57dfc1SAl Viro 	 */
1298ce57dfc1SAl Viro 	if (unlikely(type != LAST_NORM))
1299ce57dfc1SAl Viro 		return handle_dots(nd, type);
1300ce57dfc1SAl Viro 	err = do_lookup(nd, name, path, &inode);
1301ce57dfc1SAl Viro 	if (unlikely(err)) {
1302ce57dfc1SAl Viro 		terminate_walk(nd);
1303ce57dfc1SAl Viro 		return err;
1304ce57dfc1SAl Viro 	}
1305ce57dfc1SAl Viro 	if (!inode) {
1306ce57dfc1SAl Viro 		path_to_nameidata(path, nd);
1307ce57dfc1SAl Viro 		terminate_walk(nd);
1308ce57dfc1SAl Viro 		return -ENOENT;
1309ce57dfc1SAl Viro 	}
13107813b94aSLinus Torvalds 	if (should_follow_link(inode, follow)) {
131119660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
131219660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
131319660af7SAl Viro 				terminate_walk(nd);
1314ce57dfc1SAl Viro 				return -ECHILD;
131519660af7SAl Viro 			}
131619660af7SAl Viro 		}
1317ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1318ce57dfc1SAl Viro 		return 1;
1319ce57dfc1SAl Viro 	}
1320ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1321ce57dfc1SAl Viro 	nd->inode = inode;
1322ce57dfc1SAl Viro 	return 0;
1323ce57dfc1SAl Viro }
1324ce57dfc1SAl Viro 
13251da177e4SLinus Torvalds /*
1326b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1327b356379aSAl Viro  * limiting consecutive symlinks to 40.
1328b356379aSAl Viro  *
1329b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1330b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1331b356379aSAl Viro  */
1332b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1333b356379aSAl Viro {
1334b356379aSAl Viro 	int res;
1335b356379aSAl Viro 
1336b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1337b356379aSAl Viro 		path_put_conditional(path, nd);
1338b356379aSAl Viro 		path_put(&nd->path);
1339b356379aSAl Viro 		return -ELOOP;
1340b356379aSAl Viro 	}
13411a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1342b356379aSAl Viro 
1343b356379aSAl Viro 	nd->depth++;
1344b356379aSAl Viro 	current->link_count++;
1345b356379aSAl Viro 
1346b356379aSAl Viro 	do {
1347b356379aSAl Viro 		struct path link = *path;
1348b356379aSAl Viro 		void *cookie;
1349574197e0SAl Viro 
1350574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
1351b356379aSAl Viro 		if (!res)
1352b356379aSAl Viro 			res = walk_component(nd, path, &nd->last,
1353b356379aSAl Viro 					     nd->last_type, LOOKUP_FOLLOW);
1354574197e0SAl Viro 		put_link(nd, &link, cookie);
1355b356379aSAl Viro 	} while (res > 0);
1356b356379aSAl Viro 
1357b356379aSAl Viro 	current->link_count--;
1358b356379aSAl Viro 	nd->depth--;
1359b356379aSAl Viro 	return res;
1360b356379aSAl Viro }
1361b356379aSAl Viro 
1362b356379aSAl Viro /*
13633ddcd056SLinus Torvalds  * We really don't want to look at inode->i_op->lookup
13643ddcd056SLinus Torvalds  * when we don't have to. So we keep a cache bit in
13653ddcd056SLinus Torvalds  * the inode ->i_opflags field that says "yes, we can
13663ddcd056SLinus Torvalds  * do lookup on this inode".
13673ddcd056SLinus Torvalds  */
13683ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode)
13693ddcd056SLinus Torvalds {
13703ddcd056SLinus Torvalds 	if (likely(inode->i_opflags & IOP_LOOKUP))
13713ddcd056SLinus Torvalds 		return 1;
13723ddcd056SLinus Torvalds 	if (likely(!inode->i_op->lookup))
13733ddcd056SLinus Torvalds 		return 0;
13743ddcd056SLinus Torvalds 
13753ddcd056SLinus Torvalds 	/* We do this once for the lifetime of the inode */
13763ddcd056SLinus Torvalds 	spin_lock(&inode->i_lock);
13773ddcd056SLinus Torvalds 	inode->i_opflags |= IOP_LOOKUP;
13783ddcd056SLinus Torvalds 	spin_unlock(&inode->i_lock);
13793ddcd056SLinus Torvalds 	return 1;
13803ddcd056SLinus Torvalds }
13813ddcd056SLinus Torvalds 
1382bfcfaa77SLinus Torvalds /*
1383bfcfaa77SLinus Torvalds  * We can do the critical dentry name comparison and hashing
1384bfcfaa77SLinus Torvalds  * operations one word at a time, but we are limited to:
1385bfcfaa77SLinus Torvalds  *
1386bfcfaa77SLinus Torvalds  * - Architectures with fast unaligned word accesses. We could
1387bfcfaa77SLinus Torvalds  *   do a "get_unaligned()" if this helps and is sufficiently
1388bfcfaa77SLinus Torvalds  *   fast.
1389bfcfaa77SLinus Torvalds  *
1390bfcfaa77SLinus Torvalds  * - Little-endian machines (so that we can generate the mask
1391bfcfaa77SLinus Torvalds  *   of low bytes efficiently). Again, we *could* do a byte
1392bfcfaa77SLinus Torvalds  *   swapping load on big-endian architectures if that is not
1393bfcfaa77SLinus Torvalds  *   expensive enough to make the optimization worthless.
1394bfcfaa77SLinus Torvalds  *
1395bfcfaa77SLinus Torvalds  * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1396bfcfaa77SLinus Torvalds  *   do not trap on the (extremely unlikely) case of a page
1397bfcfaa77SLinus Torvalds  *   crossing operation.
1398bfcfaa77SLinus Torvalds  *
1399bfcfaa77SLinus Torvalds  * - Furthermore, we need an efficient 64-bit compile for the
1400bfcfaa77SLinus Torvalds  *   64-bit case in order to generate the "number of bytes in
1401bfcfaa77SLinus Torvalds  *   the final mask". Again, that could be replaced with a
1402bfcfaa77SLinus Torvalds  *   efficient population count instruction or similar.
1403bfcfaa77SLinus Torvalds  */
1404bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS
1405bfcfaa77SLinus Torvalds 
1406bfcfaa77SLinus Torvalds #ifdef CONFIG_64BIT
1407bfcfaa77SLinus Torvalds 
1408bfcfaa77SLinus Torvalds /*
1409bfcfaa77SLinus Torvalds  * Jan Achrenius on G+: microoptimized version of
1410bfcfaa77SLinus Torvalds  * the simpler "(mask & ONEBYTES) * ONEBYTES >> 56"
1411bfcfaa77SLinus Torvalds  * that works for the bytemasks without having to
1412bfcfaa77SLinus Torvalds  * mask them first.
1413bfcfaa77SLinus Torvalds  */
1414bfcfaa77SLinus Torvalds static inline long count_masked_bytes(unsigned long mask)
1415bfcfaa77SLinus Torvalds {
1416f7493e5dSLinus Torvalds 	return mask*0x0001020304050608ul >> 56;
1417bfcfaa77SLinus Torvalds }
1418bfcfaa77SLinus Torvalds 
1419bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash)
1420bfcfaa77SLinus Torvalds {
1421bfcfaa77SLinus Torvalds 	hash += hash >> (8*sizeof(int));
1422bfcfaa77SLinus Torvalds 	return hash;
1423bfcfaa77SLinus Torvalds }
1424bfcfaa77SLinus Torvalds 
1425bfcfaa77SLinus Torvalds #else	/* 32-bit case */
1426bfcfaa77SLinus Torvalds 
1427bfcfaa77SLinus Torvalds /* Carl Chatfield / Jan Achrenius G+ version for 32-bit */
1428bfcfaa77SLinus Torvalds static inline long count_masked_bytes(long mask)
1429bfcfaa77SLinus Torvalds {
1430bfcfaa77SLinus Torvalds 	/* (000000 0000ff 00ffff ffffff) -> ( 1 1 2 3 ) */
1431bfcfaa77SLinus Torvalds 	long a = (0x0ff0001+mask) >> 23;
1432bfcfaa77SLinus Torvalds 	/* Fix the 1 for 00 case */
1433bfcfaa77SLinus Torvalds 	return a & mask;
1434bfcfaa77SLinus Torvalds }
1435bfcfaa77SLinus Torvalds 
1436bfcfaa77SLinus Torvalds #define fold_hash(x) (x)
1437bfcfaa77SLinus Torvalds 
1438bfcfaa77SLinus Torvalds #endif
1439bfcfaa77SLinus Torvalds 
1440bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1441bfcfaa77SLinus Torvalds {
1442bfcfaa77SLinus Torvalds 	unsigned long a, mask;
1443bfcfaa77SLinus Torvalds 	unsigned long hash = 0;
1444bfcfaa77SLinus Torvalds 
1445bfcfaa77SLinus Torvalds 	for (;;) {
1446bfcfaa77SLinus Torvalds 		a = *(unsigned long *)name;
1447bfcfaa77SLinus Torvalds 		if (len < sizeof(unsigned long))
1448bfcfaa77SLinus Torvalds 			break;
1449bfcfaa77SLinus Torvalds 		hash += a;
1450f132c5beSAl Viro 		hash *= 9;
1451bfcfaa77SLinus Torvalds 		name += sizeof(unsigned long);
1452bfcfaa77SLinus Torvalds 		len -= sizeof(unsigned long);
1453bfcfaa77SLinus Torvalds 		if (!len)
1454bfcfaa77SLinus Torvalds 			goto done;
1455bfcfaa77SLinus Torvalds 	}
1456bfcfaa77SLinus Torvalds 	mask = ~(~0ul << len*8);
1457bfcfaa77SLinus Torvalds 	hash += mask & a;
1458bfcfaa77SLinus Torvalds done:
1459bfcfaa77SLinus Torvalds 	return fold_hash(hash);
1460bfcfaa77SLinus Torvalds }
1461bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
1462bfcfaa77SLinus Torvalds 
1463989412bbSLinus Torvalds #define REPEAT_BYTE(x)	((~0ul / 0xff) * (x))
1464989412bbSLinus Torvalds #define ONEBYTES	REPEAT_BYTE(0x01)
1465989412bbSLinus Torvalds #define SLASHBYTES	REPEAT_BYTE('/')
1466989412bbSLinus Torvalds #define HIGHBITS	REPEAT_BYTE(0x80)
1467bfcfaa77SLinus Torvalds 
1468bfcfaa77SLinus Torvalds /* Return the high bit set in the first byte that is a zero */
1469bfcfaa77SLinus Torvalds static inline unsigned long has_zero(unsigned long a)
1470bfcfaa77SLinus Torvalds {
1471bfcfaa77SLinus Torvalds 	return ((a - ONEBYTES) & ~a) & HIGHBITS;
1472bfcfaa77SLinus Torvalds }
1473bfcfaa77SLinus Torvalds 
1474bfcfaa77SLinus Torvalds /*
1475bfcfaa77SLinus Torvalds  * Calculate the length and hash of the path component, and
1476bfcfaa77SLinus Torvalds  * return the length of the component;
1477bfcfaa77SLinus Torvalds  */
1478bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1479bfcfaa77SLinus Torvalds {
1480bfcfaa77SLinus Torvalds 	unsigned long a, mask, hash, len;
1481bfcfaa77SLinus Torvalds 
1482bfcfaa77SLinus Torvalds 	hash = a = 0;
1483bfcfaa77SLinus Torvalds 	len = -sizeof(unsigned long);
1484bfcfaa77SLinus Torvalds 	do {
1485bfcfaa77SLinus Torvalds 		hash = (hash + a) * 9;
1486bfcfaa77SLinus Torvalds 		len += sizeof(unsigned long);
1487bfcfaa77SLinus Torvalds 		a = *(unsigned long *)(name+len);
1488bfcfaa77SLinus Torvalds 		/* Do we have any NUL or '/' bytes in this word? */
1489bfcfaa77SLinus Torvalds 		mask = has_zero(a) | has_zero(a ^ SLASHBYTES);
1490bfcfaa77SLinus Torvalds 	} while (!mask);
1491bfcfaa77SLinus Torvalds 
1492bfcfaa77SLinus Torvalds 	/* The mask *below* the first high bit set */
1493bfcfaa77SLinus Torvalds 	mask = (mask - 1) & ~mask;
1494bfcfaa77SLinus Torvalds 	mask >>= 7;
1495bfcfaa77SLinus Torvalds 	hash += a & mask;
1496bfcfaa77SLinus Torvalds 	*hashp = fold_hash(hash);
1497bfcfaa77SLinus Torvalds 
1498bfcfaa77SLinus Torvalds 	return len + count_masked_bytes(mask);
1499bfcfaa77SLinus Torvalds }
1500bfcfaa77SLinus Torvalds 
1501bfcfaa77SLinus Torvalds #else
1502bfcfaa77SLinus Torvalds 
15030145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
15040145acc2SLinus Torvalds {
15050145acc2SLinus Torvalds 	unsigned long hash = init_name_hash();
15060145acc2SLinus Torvalds 	while (len--)
15070145acc2SLinus Torvalds 		hash = partial_name_hash(*name++, hash);
15080145acc2SLinus Torvalds 	return end_name_hash(hash);
15090145acc2SLinus Torvalds }
1510ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
15110145acc2SLinus Torvalds 
15123ddcd056SLinus Torvalds /*
1513200e9ef7SLinus Torvalds  * We know there's a real path component here of at least
1514200e9ef7SLinus Torvalds  * one character.
1515200e9ef7SLinus Torvalds  */
1516200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1517200e9ef7SLinus Torvalds {
1518200e9ef7SLinus Torvalds 	unsigned long hash = init_name_hash();
1519200e9ef7SLinus Torvalds 	unsigned long len = 0, c;
1520200e9ef7SLinus Torvalds 
1521200e9ef7SLinus Torvalds 	c = (unsigned char)*name;
1522200e9ef7SLinus Torvalds 	do {
1523200e9ef7SLinus Torvalds 		len++;
1524200e9ef7SLinus Torvalds 		hash = partial_name_hash(c, hash);
1525200e9ef7SLinus Torvalds 		c = (unsigned char)name[len];
1526200e9ef7SLinus Torvalds 	} while (c && c != '/');
1527200e9ef7SLinus Torvalds 	*hashp = end_name_hash(hash);
1528200e9ef7SLinus Torvalds 	return len;
1529200e9ef7SLinus Torvalds }
1530200e9ef7SLinus Torvalds 
1531bfcfaa77SLinus Torvalds #endif
1532bfcfaa77SLinus Torvalds 
1533200e9ef7SLinus Torvalds /*
15341da177e4SLinus Torvalds  * Name resolution.
1535ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1536ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
15371da177e4SLinus Torvalds  *
1538ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1539ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
15401da177e4SLinus Torvalds  */
15416de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
15421da177e4SLinus Torvalds {
15431da177e4SLinus Torvalds 	struct path next;
15441da177e4SLinus Torvalds 	int err;
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds 	while (*name=='/')
15471da177e4SLinus Torvalds 		name++;
15481da177e4SLinus Torvalds 	if (!*name)
1549086e183aSAl Viro 		return 0;
15501da177e4SLinus Torvalds 
15511da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
15521da177e4SLinus Torvalds 	for(;;) {
15531da177e4SLinus Torvalds 		struct qstr this;
1554200e9ef7SLinus Torvalds 		long len;
1555fe479a58SAl Viro 		int type;
15561da177e4SLinus Torvalds 
155752094c8aSAl Viro 		err = may_lookup(nd);
15581da177e4SLinus Torvalds  		if (err)
15591da177e4SLinus Torvalds 			break;
15601da177e4SLinus Torvalds 
1561200e9ef7SLinus Torvalds 		len = hash_name(name, &this.hash);
15621da177e4SLinus Torvalds 		this.name = name;
1563200e9ef7SLinus Torvalds 		this.len = len;
15641da177e4SLinus Torvalds 
1565fe479a58SAl Viro 		type = LAST_NORM;
1566200e9ef7SLinus Torvalds 		if (name[0] == '.') switch (len) {
1567fe479a58SAl Viro 			case 2:
1568200e9ef7SLinus Torvalds 				if (name[1] == '.') {
1569fe479a58SAl Viro 					type = LAST_DOTDOT;
157016c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
157116c2cd71SAl Viro 				}
1572fe479a58SAl Viro 				break;
1573fe479a58SAl Viro 			case 1:
1574fe479a58SAl Viro 				type = LAST_DOT;
1575fe479a58SAl Viro 		}
15765a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
15775a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
157816c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
15795a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
15805a202bcdSAl Viro 				err = parent->d_op->d_hash(parent, nd->inode,
15815a202bcdSAl Viro 							   &this);
15825a202bcdSAl Viro 				if (err < 0)
15835a202bcdSAl Viro 					break;
15845a202bcdSAl Viro 			}
15855a202bcdSAl Viro 		}
1586fe479a58SAl Viro 
1587200e9ef7SLinus Torvalds 		if (!name[len])
15881da177e4SLinus Torvalds 			goto last_component;
1589200e9ef7SLinus Torvalds 		/*
1590200e9ef7SLinus Torvalds 		 * If it wasn't NUL, we know it was '/'. Skip that
1591200e9ef7SLinus Torvalds 		 * slash, and continue until no more slashes.
1592200e9ef7SLinus Torvalds 		 */
1593200e9ef7SLinus Torvalds 		do {
1594200e9ef7SLinus Torvalds 			len++;
1595200e9ef7SLinus Torvalds 		} while (unlikely(name[len] == '/'));
1596200e9ef7SLinus Torvalds 		if (!name[len])
1597b356379aSAl Viro 			goto last_component;
1598200e9ef7SLinus Torvalds 		name += len;
15991da177e4SLinus Torvalds 
1600ce57dfc1SAl Viro 		err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1601ce57dfc1SAl Viro 		if (err < 0)
1602ce57dfc1SAl Viro 			return err;
1603fe479a58SAl Viro 
1604ce57dfc1SAl Viro 		if (err) {
1605b356379aSAl Viro 			err = nested_symlink(&next, nd);
16061da177e4SLinus Torvalds 			if (err)
1607a7472babSAl Viro 				return err;
160831e6b01fSNick Piggin 		}
16093ddcd056SLinus Torvalds 		if (can_lookup(nd->inode))
16101da177e4SLinus Torvalds 			continue;
16113ddcd056SLinus Torvalds 		err = -ENOTDIR;
16123ddcd056SLinus Torvalds 		break;
16131da177e4SLinus Torvalds 		/* here ends the main loop */
16141da177e4SLinus Torvalds 
16151da177e4SLinus Torvalds last_component:
1616ce57dfc1SAl Viro 		nd->last = this;
1617ce57dfc1SAl Viro 		nd->last_type = type;
1618ce57dfc1SAl Viro 		return 0;
1619ce57dfc1SAl Viro 	}
1620951361f9SAl Viro 	terminate_walk(nd);
16211da177e4SLinus Torvalds 	return err;
16221da177e4SLinus Torvalds }
16231da177e4SLinus Torvalds 
162470e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
162570e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
162631e6b01fSNick Piggin {
162731e6b01fSNick Piggin 	int retval = 0;
162831e6b01fSNick Piggin 	int fput_needed;
162931e6b01fSNick Piggin 	struct file *file;
163031e6b01fSNick Piggin 
163131e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
163216c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
163331e6b01fSNick Piggin 	nd->depth = 0;
16345b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
16355b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
163673d049a4SAl Viro 		if (*name) {
16375b6ca027SAl Viro 			if (!inode->i_op->lookup)
16385b6ca027SAl Viro 				return -ENOTDIR;
16395b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
16405b6ca027SAl Viro 			if (retval)
16415b6ca027SAl Viro 				return retval;
164273d049a4SAl Viro 		}
16435b6ca027SAl Viro 		nd->path = nd->root;
16445b6ca027SAl Viro 		nd->inode = inode;
16455b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
16465b6ca027SAl Viro 			br_read_lock(vfsmount_lock);
16475b6ca027SAl Viro 			rcu_read_lock();
16485b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
16495b6ca027SAl Viro 		} else {
16505b6ca027SAl Viro 			path_get(&nd->path);
16515b6ca027SAl Viro 		}
16525b6ca027SAl Viro 		return 0;
16535b6ca027SAl Viro 	}
16545b6ca027SAl Viro 
165531e6b01fSNick Piggin 	nd->root.mnt = NULL;
165631e6b01fSNick Piggin 
165731e6b01fSNick Piggin 	if (*name=='/') {
1658e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
165931e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
166031e6b01fSNick Piggin 			rcu_read_lock();
1661e41f7d4eSAl Viro 			set_root_rcu(nd);
1662e41f7d4eSAl Viro 		} else {
1663e41f7d4eSAl Viro 			set_root(nd);
1664e41f7d4eSAl Viro 			path_get(&nd->root);
1665e41f7d4eSAl Viro 		}
166631e6b01fSNick Piggin 		nd->path = nd->root;
166731e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1668e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
166931e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1670c28cc364SNick Piggin 			unsigned seq;
167131e6b01fSNick Piggin 
167231e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
167331e6b01fSNick Piggin 			rcu_read_lock();
167431e6b01fSNick Piggin 
1675c28cc364SNick Piggin 			do {
1676c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
167731e6b01fSNick Piggin 				nd->path = fs->pwd;
1678c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1679c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1680e41f7d4eSAl Viro 		} else {
1681e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1682e41f7d4eSAl Viro 		}
168331e6b01fSNick Piggin 	} else {
168431e6b01fSNick Piggin 		struct dentry *dentry;
168531e6b01fSNick Piggin 
16861abf0c71SAl Viro 		file = fget_raw_light(dfd, &fput_needed);
168731e6b01fSNick Piggin 		retval = -EBADF;
168831e6b01fSNick Piggin 		if (!file)
168931e6b01fSNick Piggin 			goto out_fail;
169031e6b01fSNick Piggin 
169131e6b01fSNick Piggin 		dentry = file->f_path.dentry;
169231e6b01fSNick Piggin 
1693f52e0c11SAl Viro 		if (*name) {
169431e6b01fSNick Piggin 			retval = -ENOTDIR;
169531e6b01fSNick Piggin 			if (!S_ISDIR(dentry->d_inode->i_mode))
169631e6b01fSNick Piggin 				goto fput_fail;
169731e6b01fSNick Piggin 
16984ad5abb3SAl Viro 			retval = inode_permission(dentry->d_inode, MAY_EXEC);
169931e6b01fSNick Piggin 			if (retval)
170031e6b01fSNick Piggin 				goto fput_fail;
1701f52e0c11SAl Viro 		}
170231e6b01fSNick Piggin 
170331e6b01fSNick Piggin 		nd->path = file->f_path;
1704e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
170531e6b01fSNick Piggin 			if (fput_needed)
170670e9b357SAl Viro 				*fp = file;
1707c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
170831e6b01fSNick Piggin 			br_read_lock(vfsmount_lock);
170931e6b01fSNick Piggin 			rcu_read_lock();
17105590ff0dSUlrich Drepper 		} else {
17115dd784d0SJan Blunck 			path_get(&file->f_path);
17125590ff0dSUlrich Drepper 			fput_light(file, fput_needed);
17131da177e4SLinus Torvalds 		}
1714e41f7d4eSAl Viro 	}
1715e41f7d4eSAl Viro 
171631e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
17179b4a9b14SAl Viro 	return 0;
17182dfdd266SJosef 'Jeff' Sipek 
17199b4a9b14SAl Viro fput_fail:
17209b4a9b14SAl Viro 	fput_light(file, fput_needed);
17219b4a9b14SAl Viro out_fail:
17229b4a9b14SAl Viro 	return retval;
17239b4a9b14SAl Viro }
17249b4a9b14SAl Viro 
1725bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1726bd92d7feSAl Viro {
1727bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1728bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1729bd92d7feSAl Viro 
1730bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
1731bd92d7feSAl Viro 	return walk_component(nd, path, &nd->last, nd->last_type,
1732bd92d7feSAl Viro 					nd->flags & LOOKUP_FOLLOW);
1733bd92d7feSAl Viro }
1734bd92d7feSAl Viro 
17359b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1736ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
17379b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
17389b4a9b14SAl Viro {
173970e9b357SAl Viro 	struct file *base = NULL;
1740bd92d7feSAl Viro 	struct path path;
1741bd92d7feSAl Viro 	int err;
174231e6b01fSNick Piggin 
174331e6b01fSNick Piggin 	/*
174431e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
174531e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
174631e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
174731e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
174831e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
174931e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
175031e6b01fSNick Piggin 	 * analogue, foo_rcu().
175131e6b01fSNick Piggin 	 *
175231e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
175331e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
175431e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
175531e6b01fSNick Piggin 	 * be able to complete).
175631e6b01fSNick Piggin 	 */
1757bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1758ee0827cdSAl Viro 
1759bd92d7feSAl Viro 	if (unlikely(err))
1760bd92d7feSAl Viro 		return err;
1761ee0827cdSAl Viro 
1762ee0827cdSAl Viro 	current->total_link_count = 0;
1763bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1764bd92d7feSAl Viro 
1765bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1766bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1767bd92d7feSAl Viro 		while (err > 0) {
1768bd92d7feSAl Viro 			void *cookie;
1769bd92d7feSAl Viro 			struct path link = path;
1770bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1771574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
1772bd92d7feSAl Viro 			if (!err)
1773bd92d7feSAl Viro 				err = lookup_last(nd, &path);
1774574197e0SAl Viro 			put_link(nd, &link, cookie);
1775bd92d7feSAl Viro 		}
1776bd92d7feSAl Viro 	}
1777ee0827cdSAl Viro 
17789f1fafeeSAl Viro 	if (!err)
17799f1fafeeSAl Viro 		err = complete_walk(nd);
1780bd92d7feSAl Viro 
1781bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
1782bd92d7feSAl Viro 		if (!nd->inode->i_op->lookup) {
1783bd92d7feSAl Viro 			path_put(&nd->path);
1784bd23a539SAl Viro 			err = -ENOTDIR;
1785bd92d7feSAl Viro 		}
1786bd92d7feSAl Viro 	}
178716c2cd71SAl Viro 
178870e9b357SAl Viro 	if (base)
178970e9b357SAl Viro 		fput(base);
1790ee0827cdSAl Viro 
17915b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
179231e6b01fSNick Piggin 		path_put(&nd->root);
179331e6b01fSNick Piggin 		nd->root.mnt = NULL;
179431e6b01fSNick Piggin 	}
1795bd92d7feSAl Viro 	return err;
179631e6b01fSNick Piggin }
179731e6b01fSNick Piggin 
1798ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1799ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1800ee0827cdSAl Viro {
1801ee0827cdSAl Viro 	int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
1802ee0827cdSAl Viro 	if (unlikely(retval == -ECHILD))
1803ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags, nd);
1804ee0827cdSAl Viro 	if (unlikely(retval == -ESTALE))
1805ee0827cdSAl Viro 		retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
1806ee0827cdSAl Viro 
180731e6b01fSNick Piggin 	if (likely(!retval)) {
180831e6b01fSNick Piggin 		if (unlikely(!audit_dummy_context())) {
180931e6b01fSNick Piggin 			if (nd->path.dentry && nd->inode)
181031e6b01fSNick Piggin 				audit_inode(name, nd->path.dentry);
181131e6b01fSNick Piggin 		}
181231e6b01fSNick Piggin 	}
1813170aa3d0SUlrich Drepper 	return retval;
18141da177e4SLinus Torvalds }
18151da177e4SLinus Torvalds 
1816c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd)
18175590ff0dSUlrich Drepper {
1818c9c6cac0SAl Viro 	return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
18195590ff0dSUlrich Drepper }
18205590ff0dSUlrich Drepper 
1821d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1822d1811465SAl Viro {
1823d1811465SAl Viro 	struct nameidata nd;
1824d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1825d1811465SAl Viro 	if (!res)
1826d1811465SAl Viro 		*path = nd.path;
1827d1811465SAl Viro 	return res;
1828d1811465SAl Viro }
1829d1811465SAl Viro 
183016f18200SJosef 'Jeff' Sipek /**
183116f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
183216f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
183316f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
183416f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
183516f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
1836e0a01249SAl Viro  * @path: pointer to struct path to fill
183716f18200SJosef 'Jeff' Sipek  */
183816f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
183916f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
1840e0a01249SAl Viro 		    struct path *path)
184116f18200SJosef 'Jeff' Sipek {
1842e0a01249SAl Viro 	struct nameidata nd;
1843e0a01249SAl Viro 	int err;
1844e0a01249SAl Viro 	nd.root.dentry = dentry;
1845e0a01249SAl Viro 	nd.root.mnt = mnt;
1846e0a01249SAl Viro 	BUG_ON(flags & LOOKUP_PARENT);
18475b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
1848e0a01249SAl Viro 	err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
1849e0a01249SAl Viro 	if (!err)
1850e0a01249SAl Viro 		*path = nd.path;
1851e0a01249SAl Viro 	return err;
185216f18200SJosef 'Jeff' Sipek }
185316f18200SJosef 'Jeff' Sipek 
1854057f6c01SJames Morris /*
1855057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1856057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1857057f6c01SJames Morris  * SMP-safe.
1858057f6c01SJames Morris  */
1859a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
18601da177e4SLinus Torvalds {
18614ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
18621da177e4SLinus Torvalds }
18631da177e4SLinus Torvalds 
1864eead1911SChristoph Hellwig /**
1865a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1866eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1867eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1868eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1869eead1911SChristoph Hellwig  *
1870a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1871a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1872eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1873eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1874eead1911SChristoph Hellwig  */
1875057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1876057f6c01SJames Morris {
1877057f6c01SJames Morris 	struct qstr this;
18786a96ba54SAl Viro 	unsigned int c;
1879cda309deSMiklos Szeredi 	int err;
1880057f6c01SJames Morris 
18812f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
18822f9092e1SDavid Woodhouse 
18836a96ba54SAl Viro 	this.name = name;
18846a96ba54SAl Viro 	this.len = len;
18850145acc2SLinus Torvalds 	this.hash = full_name_hash(name, len);
18866a96ba54SAl Viro 	if (!len)
18876a96ba54SAl Viro 		return ERR_PTR(-EACCES);
18886a96ba54SAl Viro 
18896a96ba54SAl Viro 	while (len--) {
18906a96ba54SAl Viro 		c = *(const unsigned char *)name++;
18916a96ba54SAl Viro 		if (c == '/' || c == '\0')
18926a96ba54SAl Viro 			return ERR_PTR(-EACCES);
18936a96ba54SAl Viro 	}
18945a202bcdSAl Viro 	/*
18955a202bcdSAl Viro 	 * See if the low-level filesystem might want
18965a202bcdSAl Viro 	 * to use its own hash..
18975a202bcdSAl Viro 	 */
18985a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
18995a202bcdSAl Viro 		int err = base->d_op->d_hash(base, base->d_inode, &this);
19005a202bcdSAl Viro 		if (err < 0)
19015a202bcdSAl Viro 			return ERR_PTR(err);
19025a202bcdSAl Viro 	}
1903eead1911SChristoph Hellwig 
1904cda309deSMiklos Szeredi 	err = inode_permission(base->d_inode, MAY_EXEC);
1905cda309deSMiklos Szeredi 	if (err)
1906cda309deSMiklos Szeredi 		return ERR_PTR(err);
1907cda309deSMiklos Szeredi 
190849705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1909057f6c01SJames Morris }
1910057f6c01SJames Morris 
19111fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
19121fa1e7f6SAndy Whitcroft 		 struct path *path, int *empty)
19131da177e4SLinus Torvalds {
19142d8f3038SAl Viro 	struct nameidata nd;
19151fa1e7f6SAndy Whitcroft 	char *tmp = getname_flags(name, flags, empty);
19161da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
19171da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
19182d8f3038SAl Viro 
19192d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
19202d8f3038SAl Viro 
19212d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
19221da177e4SLinus Torvalds 		putname(tmp);
19232d8f3038SAl Viro 		if (!err)
19242d8f3038SAl Viro 			*path = nd.path;
19251da177e4SLinus Torvalds 	}
19261da177e4SLinus Torvalds 	return err;
19271da177e4SLinus Torvalds }
19281da177e4SLinus Torvalds 
19291fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags,
19301fa1e7f6SAndy Whitcroft 		 struct path *path)
19311fa1e7f6SAndy Whitcroft {
1932f7493e5dSLinus Torvalds 	return user_path_at_empty(dfd, name, flags, path, NULL);
19331fa1e7f6SAndy Whitcroft }
19341fa1e7f6SAndy Whitcroft 
19352ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
19362ad94ae6SAl Viro 			struct nameidata *nd, char **name)
19372ad94ae6SAl Viro {
19382ad94ae6SAl Viro 	char *s = getname(path);
19392ad94ae6SAl Viro 	int error;
19402ad94ae6SAl Viro 
19412ad94ae6SAl Viro 	if (IS_ERR(s))
19422ad94ae6SAl Viro 		return PTR_ERR(s);
19432ad94ae6SAl Viro 
19442ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
19452ad94ae6SAl Viro 	if (error)
19462ad94ae6SAl Viro 		putname(s);
19472ad94ae6SAl Viro 	else
19482ad94ae6SAl Viro 		*name = s;
19492ad94ae6SAl Viro 
19502ad94ae6SAl Viro 	return error;
19512ad94ae6SAl Viro }
19522ad94ae6SAl Viro 
19531da177e4SLinus Torvalds /*
19541da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
19551da177e4SLinus Torvalds  * minimal.
19561da177e4SLinus Torvalds  */
19571da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
19581da177e4SLinus Torvalds {
19598e96e3b7SEric W. Biederman 	kuid_t fsuid = current_fsuid();
1960da9592edSDavid Howells 
19611da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
19621da177e4SLinus Torvalds 		return 0;
19638e96e3b7SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
19641da177e4SLinus Torvalds 		return 0;
19658e96e3b7SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
19661da177e4SLinus Torvalds 		return 0;
19671a48e2acSEric W. Biederman 	return !inode_capable(inode, CAP_FOWNER);
19681da177e4SLinus Torvalds }
19691da177e4SLinus Torvalds 
19701da177e4SLinus Torvalds /*
19711da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
19721da177e4SLinus Torvalds  *  whether the type of victim is right.
19731da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
19741da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
19751da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
19761da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
19771da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
19781da177e4SLinus Torvalds  *	a. be owner of dir, or
19791da177e4SLinus Torvalds  *	b. be owner of victim, or
19801da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
19811da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
19821da177e4SLinus Torvalds  *     links pointing to it.
19831da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
19841da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
19851da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
19861da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
19871da177e4SLinus Torvalds  *     nfs_async_unlink().
19881da177e4SLinus Torvalds  */
1989858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
19901da177e4SLinus Torvalds {
19911da177e4SLinus Torvalds 	int error;
19921da177e4SLinus Torvalds 
19931da177e4SLinus Torvalds 	if (!victim->d_inode)
19941da177e4SLinus Torvalds 		return -ENOENT;
19951da177e4SLinus Torvalds 
19961da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
1997cccc6bbaSAl Viro 	audit_inode_child(victim, dir);
19981da177e4SLinus Torvalds 
1999f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
20001da177e4SLinus Torvalds 	if (error)
20011da177e4SLinus Torvalds 		return error;
20021da177e4SLinus Torvalds 	if (IS_APPEND(dir))
20031da177e4SLinus Torvalds 		return -EPERM;
20041da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
2005f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
20061da177e4SLinus Torvalds 		return -EPERM;
20071da177e4SLinus Torvalds 	if (isdir) {
20081da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
20091da177e4SLinus Torvalds 			return -ENOTDIR;
20101da177e4SLinus Torvalds 		if (IS_ROOT(victim))
20111da177e4SLinus Torvalds 			return -EBUSY;
20121da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
20131da177e4SLinus Torvalds 		return -EISDIR;
20141da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
20151da177e4SLinus Torvalds 		return -ENOENT;
20161da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
20171da177e4SLinus Torvalds 		return -EBUSY;
20181da177e4SLinus Torvalds 	return 0;
20191da177e4SLinus Torvalds }
20201da177e4SLinus Torvalds 
20211da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
20221da177e4SLinus Torvalds  *  dir.
20231da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
20241da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
20251da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
20261da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
20271da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
20281da177e4SLinus Torvalds  */
2029a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
20301da177e4SLinus Torvalds {
20311da177e4SLinus Torvalds 	if (child->d_inode)
20321da177e4SLinus Torvalds 		return -EEXIST;
20331da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
20341da177e4SLinus Torvalds 		return -ENOENT;
2035f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
20361da177e4SLinus Torvalds }
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds /*
20391da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
20401da177e4SLinus Torvalds  */
20411da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
20421da177e4SLinus Torvalds {
20431da177e4SLinus Torvalds 	struct dentry *p;
20441da177e4SLinus Torvalds 
20451da177e4SLinus Torvalds 	if (p1 == p2) {
2046f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
20471da177e4SLinus Torvalds 		return NULL;
20481da177e4SLinus Torvalds 	}
20491da177e4SLinus Torvalds 
2050a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
20511da177e4SLinus Torvalds 
2052e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
2053e2761a11SOGAWA Hirofumi 	if (p) {
2054f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2055f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
20561da177e4SLinus Torvalds 		return p;
20571da177e4SLinus Torvalds 	}
20581da177e4SLinus Torvalds 
2059e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
2060e2761a11SOGAWA Hirofumi 	if (p) {
2061f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2062f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
20631da177e4SLinus Torvalds 		return p;
20641da177e4SLinus Torvalds 	}
20651da177e4SLinus Torvalds 
2066f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2067f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
20681da177e4SLinus Torvalds 	return NULL;
20691da177e4SLinus Torvalds }
20701da177e4SLinus Torvalds 
20711da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
20721da177e4SLinus Torvalds {
20731b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
20741da177e4SLinus Torvalds 	if (p1 != p2) {
20751b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
2076a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
20771da177e4SLinus Torvalds 	}
20781da177e4SLinus Torvalds }
20791da177e4SLinus Torvalds 
20804acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
20811da177e4SLinus Torvalds 		struct nameidata *nd)
20821da177e4SLinus Torvalds {
2083a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
20841da177e4SLinus Torvalds 
20851da177e4SLinus Torvalds 	if (error)
20861da177e4SLinus Torvalds 		return error;
20871da177e4SLinus Torvalds 
2088acfa4380SAl Viro 	if (!dir->i_op->create)
20891da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
20901da177e4SLinus Torvalds 	mode &= S_IALLUGO;
20911da177e4SLinus Torvalds 	mode |= S_IFREG;
20921da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
20931da177e4SLinus Torvalds 	if (error)
20941da177e4SLinus Torvalds 		return error;
20951da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
2096a74574aaSStephen Smalley 	if (!error)
2097f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
20981da177e4SLinus Torvalds 	return error;
20991da177e4SLinus Torvalds }
21001da177e4SLinus Torvalds 
210173d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
21021da177e4SLinus Torvalds {
21033fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
21041da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
21051da177e4SLinus Torvalds 	int error;
21061da177e4SLinus Torvalds 
2107bcda7652SAl Viro 	/* O_PATH? */
2108bcda7652SAl Viro 	if (!acc_mode)
2109bcda7652SAl Viro 		return 0;
2110bcda7652SAl Viro 
21111da177e4SLinus Torvalds 	if (!inode)
21121da177e4SLinus Torvalds 		return -ENOENT;
21131da177e4SLinus Torvalds 
2114c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2115c8fe8f30SChristoph Hellwig 	case S_IFLNK:
21161da177e4SLinus Torvalds 		return -ELOOP;
2117c8fe8f30SChristoph Hellwig 	case S_IFDIR:
2118c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
21191da177e4SLinus Torvalds 			return -EISDIR;
2120c8fe8f30SChristoph Hellwig 		break;
2121c8fe8f30SChristoph Hellwig 	case S_IFBLK:
2122c8fe8f30SChristoph Hellwig 	case S_IFCHR:
21233fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
21241da177e4SLinus Torvalds 			return -EACCES;
2125c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
2126c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2127c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
21281da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2129c8fe8f30SChristoph Hellwig 		break;
21304a3fd211SDave Hansen 	}
2131b41572e9SDave Hansen 
21323fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
2133b41572e9SDave Hansen 	if (error)
2134b41572e9SDave Hansen 		return error;
21356146f0d5SMimi Zohar 
21361da177e4SLinus Torvalds 	/*
21371da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
21381da177e4SLinus Torvalds 	 */
21391da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
21408737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
21417715b521SAl Viro 			return -EPERM;
21421da177e4SLinus Torvalds 		if (flag & O_TRUNC)
21437715b521SAl Viro 			return -EPERM;
21441da177e4SLinus Torvalds 	}
21451da177e4SLinus Torvalds 
21461da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
21472e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
21487715b521SAl Viro 		return -EPERM;
21491da177e4SLinus Torvalds 
2150f3c7691eSJ. Bruce Fields 	return 0;
21517715b521SAl Viro }
21527715b521SAl Viro 
2153e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
21547715b521SAl Viro {
2155e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
21567715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
21577715b521SAl Viro 	int error = get_write_access(inode);
21581da177e4SLinus Torvalds 	if (error)
21597715b521SAl Viro 		return error;
21601da177e4SLinus Torvalds 	/*
21611da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
21621da177e4SLinus Torvalds 	 */
21631da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2164be6d3e56SKentaro Takeda 	if (!error)
2165ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
21661da177e4SLinus Torvalds 	if (!error) {
21677715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2168d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2169e1181ee6SJeff Layton 				    filp);
21701da177e4SLinus Torvalds 	}
21711da177e4SLinus Torvalds 	put_write_access(inode);
2172acd0c935SMimi Zohar 	return error;
21731da177e4SLinus Torvalds }
21741da177e4SLinus Torvalds 
2175d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2176d57999e1SDave Hansen {
21778a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
21788a5e929dSAl Viro 		flag--;
2179d57999e1SDave Hansen 	return flag;
2180d57999e1SDave Hansen }
2181d57999e1SDave Hansen 
218231e6b01fSNick Piggin /*
2183fe2d35ffSAl Viro  * Handle the last step of open()
218431e6b01fSNick Piggin  */
2185fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path,
2186c3e380b0SAl Viro 			    const struct open_flags *op, const char *pathname)
2187fb1cc555SAl Viro {
2188a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
21896c0d46c4SAl Viro 	struct dentry *dentry;
2190ca344a89SAl Viro 	int open_flag = op->open_flag;
21916c0d46c4SAl Viro 	int will_truncate = open_flag & O_TRUNC;
2192ca344a89SAl Viro 	int want_write = 0;
2193bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2194fb1cc555SAl Viro 	struct file *filp;
219516c2cd71SAl Viro 	int error;
2196fb1cc555SAl Viro 
2197c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2198c3e380b0SAl Viro 	nd->flags |= op->intent;
2199c3e380b0SAl Viro 
22001f36f774SAl Viro 	switch (nd->last_type) {
22011f36f774SAl Viro 	case LAST_DOTDOT:
2202176306f5SNeil Brown 	case LAST_DOT:
2203fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2204fe2d35ffSAl Viro 		if (error)
2205fe2d35ffSAl Viro 			return ERR_PTR(error);
22061f36f774SAl Viro 		/* fallthrough */
22071f36f774SAl Viro 	case LAST_ROOT:
22089f1fafeeSAl Viro 		error = complete_walk(nd);
220916c2cd71SAl Viro 		if (error)
22109f1fafeeSAl Viro 			return ERR_PTR(error);
2211fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2212ca344a89SAl Viro 		if (open_flag & O_CREAT) {
221316c2cd71SAl Viro 			error = -EISDIR;
22141f36f774SAl Viro 			goto exit;
2215fe2d35ffSAl Viro 		}
2216fe2d35ffSAl Viro 		goto ok;
22171f36f774SAl Viro 	case LAST_BIND:
22189f1fafeeSAl Viro 		error = complete_walk(nd);
221916c2cd71SAl Viro 		if (error)
22209f1fafeeSAl Viro 			return ERR_PTR(error);
22211f36f774SAl Viro 		audit_inode(pathname, dir);
22221f36f774SAl Viro 		goto ok;
22231f36f774SAl Viro 	}
2224a2c36b45SAl Viro 
2225ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2226bcda7652SAl Viro 		int symlink_ok = 0;
2227fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2228fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2229bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
2230bcda7652SAl Viro 			symlink_ok = 1;
2231fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2232ce57dfc1SAl Viro 		error = walk_component(nd, path, &nd->last, LAST_NORM,
2233ce57dfc1SAl Viro 					!symlink_ok);
2234ce57dfc1SAl Viro 		if (error < 0)
2235fe2d35ffSAl Viro 			return ERR_PTR(error);
2236ce57dfc1SAl Viro 		if (error) /* symlink */
2237fe2d35ffSAl Viro 			return NULL;
2238fe2d35ffSAl Viro 		/* sayonara */
22399f1fafeeSAl Viro 		error = complete_walk(nd);
22409f1fafeeSAl Viro 		if (error)
22417f6c7e62SMiklos Szeredi 			return ERR_PTR(error);
2242fe2d35ffSAl Viro 
2243fe2d35ffSAl Viro 		error = -ENOTDIR;
2244fe2d35ffSAl Viro 		if (nd->flags & LOOKUP_DIRECTORY) {
2245ce57dfc1SAl Viro 			if (!nd->inode->i_op->lookup)
2246fe2d35ffSAl Viro 				goto exit;
2247fe2d35ffSAl Viro 		}
2248fe2d35ffSAl Viro 		audit_inode(pathname, nd->path.dentry);
2249fe2d35ffSAl Viro 		goto ok;
2250fe2d35ffSAl Viro 	}
2251fe2d35ffSAl Viro 
2252fe2d35ffSAl Viro 	/* create side of things */
2253a3fbbde7SAl Viro 	/*
2254a3fbbde7SAl Viro 	 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED has been
2255a3fbbde7SAl Viro 	 * cleared when we got to the last component we are about to look up
2256a3fbbde7SAl Viro 	 */
22579f1fafeeSAl Viro 	error = complete_walk(nd);
22589f1fafeeSAl Viro 	if (error)
22599f1fafeeSAl Viro 		return ERR_PTR(error);
2260fe2d35ffSAl Viro 
2261fe2d35ffSAl Viro 	audit_inode(pathname, dir);
226216c2cd71SAl Viro 	error = -EISDIR;
22631f36f774SAl Viro 	/* trailing slashes? */
226431e6b01fSNick Piggin 	if (nd->last.name[nd->last.len])
22651f36f774SAl Viro 		goto exit;
22661f36f774SAl Viro 
2267a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
2268a1e28038SAl Viro 
22696c0d46c4SAl Viro 	dentry = lookup_hash(nd);
22706c0d46c4SAl Viro 	error = PTR_ERR(dentry);
22716c0d46c4SAl Viro 	if (IS_ERR(dentry)) {
2272fb1cc555SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
2273fb1cc555SAl Viro 		goto exit;
2274fb1cc555SAl Viro 	}
2275fb1cc555SAl Viro 
22766c0d46c4SAl Viro 	path->dentry = dentry;
22776c0d46c4SAl Viro 	path->mnt = nd->path.mnt;
22786c0d46c4SAl Viro 
2279fb1cc555SAl Viro 	/* Negative dentry, just create the file */
22806c0d46c4SAl Viro 	if (!dentry->d_inode) {
2281a218d0fdSAl Viro 		umode_t mode = op->mode;
22826c0d46c4SAl Viro 		if (!IS_POSIXACL(dir->d_inode))
22836c0d46c4SAl Viro 			mode &= ~current_umask();
2284fb1cc555SAl Viro 		/*
2285fb1cc555SAl Viro 		 * This write is needed to ensure that a
22866c0d46c4SAl Viro 		 * rw->ro transition does not occur between
2287fb1cc555SAl Viro 		 * the time when the file is created and when
2288fb1cc555SAl Viro 		 * a permanent write count is taken through
2289fb1cc555SAl Viro 		 * the 'struct file' in nameidata_to_filp().
2290fb1cc555SAl Viro 		 */
2291fb1cc555SAl Viro 		error = mnt_want_write(nd->path.mnt);
2292fb1cc555SAl Viro 		if (error)
2293fb1cc555SAl Viro 			goto exit_mutex_unlock;
2294ca344a89SAl Viro 		want_write = 1;
22959b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2296ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
22976c0d46c4SAl Viro 		will_truncate = 0;
2298bcda7652SAl Viro 		acc_mode = MAY_OPEN;
22996c0d46c4SAl Viro 		error = security_path_mknod(&nd->path, dentry, mode, 0);
23006c0d46c4SAl Viro 		if (error)
23016c0d46c4SAl Viro 			goto exit_mutex_unlock;
23026c0d46c4SAl Viro 		error = vfs_create(dir->d_inode, dentry, mode, nd);
23036c0d46c4SAl Viro 		if (error)
23046c0d46c4SAl Viro 			goto exit_mutex_unlock;
23056c0d46c4SAl Viro 		mutex_unlock(&dir->d_inode->i_mutex);
23066c0d46c4SAl Viro 		dput(nd->path.dentry);
23076c0d46c4SAl Viro 		nd->path.dentry = dentry;
2308ca344a89SAl Viro 		goto common;
2309fb1cc555SAl Viro 	}
2310fb1cc555SAl Viro 
2311fb1cc555SAl Viro 	/*
2312fb1cc555SAl Viro 	 * It already exists.
2313fb1cc555SAl Viro 	 */
2314fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2315fb1cc555SAl Viro 	audit_inode(pathname, path->dentry);
2316fb1cc555SAl Viro 
2317fb1cc555SAl Viro 	error = -EEXIST;
2318ca344a89SAl Viro 	if (open_flag & O_EXCL)
2319fb1cc555SAl Viro 		goto exit_dput;
2320fb1cc555SAl Viro 
23219875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
23229875cf80SDavid Howells 	if (error < 0)
2323fb1cc555SAl Viro 		goto exit_dput;
2324fb1cc555SAl Viro 
2325a3fbbde7SAl Viro 	if (error)
2326a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
2327a3fbbde7SAl Viro 
2328fb1cc555SAl Viro 	error = -ENOENT;
2329fb1cc555SAl Viro 	if (!path->dentry->d_inode)
2330fb1cc555SAl Viro 		goto exit_dput;
23319e67f361SAl Viro 
23329e67f361SAl Viro 	if (path->dentry->d_inode->i_op->follow_link)
2333fb1cc555SAl Viro 		return NULL;
2334fb1cc555SAl Viro 
2335fb1cc555SAl Viro 	path_to_nameidata(path, nd);
233631e6b01fSNick Piggin 	nd->inode = path->dentry->d_inode;
2337a3fbbde7SAl Viro 	/* Why this, you ask?  _Now_ we might have grown LOOKUP_JUMPED... */
2338a3fbbde7SAl Viro 	error = complete_walk(nd);
2339a3fbbde7SAl Viro 	if (error)
2340097b180cSMiklos Szeredi 		return ERR_PTR(error);
2341fb1cc555SAl Viro 	error = -EISDIR;
234231e6b01fSNick Piggin 	if (S_ISDIR(nd->inode->i_mode))
2343fb1cc555SAl Viro 		goto exit;
234467ee3ad2SAl Viro ok:
23456c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
23466c0d46c4SAl Viro 		will_truncate = 0;
23476c0d46c4SAl Viro 
23480f9d1a10SAl Viro 	if (will_truncate) {
23490f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
23500f9d1a10SAl Viro 		if (error)
23510f9d1a10SAl Viro 			goto exit;
2352ca344a89SAl Viro 		want_write = 1;
23530f9d1a10SAl Viro 	}
2354ca344a89SAl Viro common:
2355bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2356ca344a89SAl Viro 	if (error)
23570f9d1a10SAl Viro 		goto exit;
23580f9d1a10SAl Viro 	filp = nameidata_to_filp(nd);
23590f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
23600f9d1a10SAl Viro 		error = ima_file_check(filp, op->acc_mode);
23610f9d1a10SAl Viro 		if (error) {
23620f9d1a10SAl Viro 			fput(filp);
23630f9d1a10SAl Viro 			filp = ERR_PTR(error);
23640f9d1a10SAl Viro 		}
23650f9d1a10SAl Viro 	}
23660f9d1a10SAl Viro 	if (!IS_ERR(filp)) {
23670f9d1a10SAl Viro 		if (will_truncate) {
23680f9d1a10SAl Viro 			error = handle_truncate(filp);
23690f9d1a10SAl Viro 			if (error) {
23700f9d1a10SAl Viro 				fput(filp);
23710f9d1a10SAl Viro 				filp = ERR_PTR(error);
23720f9d1a10SAl Viro 			}
23730f9d1a10SAl Viro 		}
23740f9d1a10SAl Viro 	}
2375ca344a89SAl Viro out:
2376ca344a89SAl Viro 	if (want_write)
23770f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
23780f9d1a10SAl Viro 	path_put(&nd->path);
2379fb1cc555SAl Viro 	return filp;
2380fb1cc555SAl Viro 
2381fb1cc555SAl Viro exit_mutex_unlock:
2382fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2383fb1cc555SAl Viro exit_dput:
2384fb1cc555SAl Viro 	path_put_conditional(path, nd);
2385fb1cc555SAl Viro exit:
2386ca344a89SAl Viro 	filp = ERR_PTR(error);
2387ca344a89SAl Viro 	goto out;
2388fb1cc555SAl Viro }
2389fb1cc555SAl Viro 
239013aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname,
239173d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
23921da177e4SLinus Torvalds {
2393fe2d35ffSAl Viro 	struct file *base = NULL;
23944a3fd211SDave Hansen 	struct file *filp;
23959850c056SAl Viro 	struct path path;
239613aab428SAl Viro 	int error;
239731e6b01fSNick Piggin 
239831e6b01fSNick Piggin 	filp = get_empty_filp();
239931e6b01fSNick Piggin 	if (!filp)
240031e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
240131e6b01fSNick Piggin 
240247c805dcSAl Viro 	filp->f_flags = op->open_flag;
240373d049a4SAl Viro 	nd->intent.open.file = filp;
240473d049a4SAl Viro 	nd->intent.open.flags = open_to_namei_flags(op->open_flag);
240573d049a4SAl Viro 	nd->intent.open.create_mode = op->mode;
240631e6b01fSNick Piggin 
240773d049a4SAl Viro 	error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
240831e6b01fSNick Piggin 	if (unlikely(error))
240913aab428SAl Viro 		goto out_filp;
241031e6b01fSNick Piggin 
2411fe2d35ffSAl Viro 	current->total_link_count = 0;
241273d049a4SAl Viro 	error = link_path_walk(pathname, nd);
241331e6b01fSNick Piggin 	if (unlikely(error))
241431e6b01fSNick Piggin 		goto out_filp;
24151da177e4SLinus Torvalds 
241673d049a4SAl Viro 	filp = do_last(nd, &path, op, pathname);
2417806b681cSAl Viro 	while (unlikely(!filp)) { /* trailing symlink */
24187b9337aaSNick Piggin 		struct path link = path;
2419def4af30SAl Viro 		void *cookie;
2420574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
242173d049a4SAl Viro 			path_put_conditional(&path, nd);
242273d049a4SAl Viro 			path_put(&nd->path);
242340b39136SAl Viro 			filp = ERR_PTR(-ELOOP);
242440b39136SAl Viro 			break;
242540b39136SAl Viro 		}
242673d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
242773d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2428574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
2429c3e380b0SAl Viro 		if (unlikely(error))
2430f1afe9efSAl Viro 			filp = ERR_PTR(error);
2431c3e380b0SAl Viro 		else
243273d049a4SAl Viro 			filp = do_last(nd, &path, op, pathname);
2433574197e0SAl Viro 		put_link(nd, &link, cookie);
2434806b681cSAl Viro 	}
243510fa8e62SAl Viro out:
243673d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
243773d049a4SAl Viro 		path_put(&nd->root);
2438fe2d35ffSAl Viro 	if (base)
2439fe2d35ffSAl Viro 		fput(base);
244073d049a4SAl Viro 	release_open_intent(nd);
244110fa8e62SAl Viro 	return filp;
24421da177e4SLinus Torvalds 
244331e6b01fSNick Piggin out_filp:
244410fa8e62SAl Viro 	filp = ERR_PTR(error);
244510fa8e62SAl Viro 	goto out;
2446de459215SKirill Korotaev }
24471da177e4SLinus Torvalds 
244813aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname,
244913aab428SAl Viro 		const struct open_flags *op, int flags)
245013aab428SAl Viro {
245173d049a4SAl Viro 	struct nameidata nd;
245213aab428SAl Viro 	struct file *filp;
245313aab428SAl Viro 
245473d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
245513aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
245673d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
245713aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
245873d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
245913aab428SAl Viro 	return filp;
246013aab428SAl Viro }
246113aab428SAl Viro 
246273d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
246373d049a4SAl Viro 		const char *name, const struct open_flags *op, int flags)
246473d049a4SAl Viro {
246573d049a4SAl Viro 	struct nameidata nd;
246673d049a4SAl Viro 	struct file *file;
246773d049a4SAl Viro 
246873d049a4SAl Viro 	nd.root.mnt = mnt;
246973d049a4SAl Viro 	nd.root.dentry = dentry;
247073d049a4SAl Viro 
247173d049a4SAl Viro 	flags |= LOOKUP_ROOT;
247273d049a4SAl Viro 
2473bcda7652SAl Viro 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
247473d049a4SAl Viro 		return ERR_PTR(-ELOOP);
247573d049a4SAl Viro 
247673d049a4SAl Viro 	file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
247773d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
247873d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags);
247973d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
248073d049a4SAl Viro 		file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
248173d049a4SAl Viro 	return file;
248273d049a4SAl Viro }
248373d049a4SAl Viro 
2484ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
24851da177e4SLinus Torvalds {
2486c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
2487ed75e95dSAl Viro 	struct nameidata nd;
2488ed75e95dSAl Viro 	int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
2489ed75e95dSAl Viro 	if (error)
2490ed75e95dSAl Viro 		return ERR_PTR(error);
24911da177e4SLinus Torvalds 
2492c663e5d8SChristoph Hellwig 	/*
2493c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
2494c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
2495c663e5d8SChristoph Hellwig 	 */
2496ed75e95dSAl Viro 	if (nd.last_type != LAST_NORM)
2497ed75e95dSAl Viro 		goto out;
2498ed75e95dSAl Viro 	nd.flags &= ~LOOKUP_PARENT;
2499ed75e95dSAl Viro 	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
2500ed75e95dSAl Viro 	nd.intent.open.flags = O_EXCL;
2501c663e5d8SChristoph Hellwig 
2502c663e5d8SChristoph Hellwig 	/*
2503c663e5d8SChristoph Hellwig 	 * Do the final lookup.
2504c663e5d8SChristoph Hellwig 	 */
2505ed75e95dSAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
2506ed75e95dSAl Viro 	dentry = lookup_hash(&nd);
25071da177e4SLinus Torvalds 	if (IS_ERR(dentry))
25081da177e4SLinus Torvalds 		goto fail;
2509c663e5d8SChristoph Hellwig 
2510e9baf6e5SAl Viro 	if (dentry->d_inode)
2511e9baf6e5SAl Viro 		goto eexist;
2512c663e5d8SChristoph Hellwig 	/*
2513c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
2514c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
2515c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
2516c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
2517c663e5d8SChristoph Hellwig 	 */
2518ed75e95dSAl Viro 	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
25191da177e4SLinus Torvalds 		dput(dentry);
25201da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
2521ed75e95dSAl Viro 		goto fail;
2522e9baf6e5SAl Viro 	}
2523ed75e95dSAl Viro 	*path = nd.path;
2524e9baf6e5SAl Viro 	return dentry;
2525e9baf6e5SAl Viro eexist:
2526e9baf6e5SAl Viro 	dput(dentry);
2527e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
25281da177e4SLinus Torvalds fail:
2529dae6ad8fSAl Viro 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
2530ed75e95dSAl Viro out:
2531dae6ad8fSAl Viro 	path_put(&nd.path);
2532ed75e95dSAl Viro 	return dentry;
2533dae6ad8fSAl Viro }
2534dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create);
2535dae6ad8fSAl Viro 
2536dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
2537dae6ad8fSAl Viro {
2538dae6ad8fSAl Viro 	char *tmp = getname(pathname);
2539dae6ad8fSAl Viro 	struct dentry *res;
2540dae6ad8fSAl Viro 	if (IS_ERR(tmp))
2541dae6ad8fSAl Viro 		return ERR_CAST(tmp);
2542dae6ad8fSAl Viro 	res = kern_path_create(dfd, tmp, path, is_dir);
2543dae6ad8fSAl Viro 	putname(tmp);
2544dae6ad8fSAl Viro 	return res;
2545dae6ad8fSAl Viro }
2546dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create);
2547dae6ad8fSAl Viro 
25481a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
25491da177e4SLinus Torvalds {
2550a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds 	if (error)
25531da177e4SLinus Torvalds 		return error;
25541da177e4SLinus Torvalds 
2555975d6b39SEric W. Biederman 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
25561da177e4SLinus Torvalds 		return -EPERM;
25571da177e4SLinus Torvalds 
2558acfa4380SAl Viro 	if (!dir->i_op->mknod)
25591da177e4SLinus Torvalds 		return -EPERM;
25601da177e4SLinus Torvalds 
256108ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
256208ce5f16SSerge E. Hallyn 	if (error)
256308ce5f16SSerge E. Hallyn 		return error;
256408ce5f16SSerge E. Hallyn 
25651da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
25661da177e4SLinus Torvalds 	if (error)
25671da177e4SLinus Torvalds 		return error;
25681da177e4SLinus Torvalds 
25691da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
2570a74574aaSStephen Smalley 	if (!error)
2571f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
25721da177e4SLinus Torvalds 	return error;
25731da177e4SLinus Torvalds }
25741da177e4SLinus Torvalds 
2575f69aac00SAl Viro static int may_mknod(umode_t mode)
2576463c3197SDave Hansen {
2577463c3197SDave Hansen 	switch (mode & S_IFMT) {
2578463c3197SDave Hansen 	case S_IFREG:
2579463c3197SDave Hansen 	case S_IFCHR:
2580463c3197SDave Hansen 	case S_IFBLK:
2581463c3197SDave Hansen 	case S_IFIFO:
2582463c3197SDave Hansen 	case S_IFSOCK:
2583463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
2584463c3197SDave Hansen 		return 0;
2585463c3197SDave Hansen 	case S_IFDIR:
2586463c3197SDave Hansen 		return -EPERM;
2587463c3197SDave Hansen 	default:
2588463c3197SDave Hansen 		return -EINVAL;
2589463c3197SDave Hansen 	}
2590463c3197SDave Hansen }
2591463c3197SDave Hansen 
25928208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
25932e4d0924SHeiko Carstens 		unsigned, dev)
25941da177e4SLinus Torvalds {
25951da177e4SLinus Torvalds 	struct dentry *dentry;
2596dae6ad8fSAl Viro 	struct path path;
2597dae6ad8fSAl Viro 	int error;
25981da177e4SLinus Torvalds 
25991da177e4SLinus Torvalds 	if (S_ISDIR(mode))
26001da177e4SLinus Torvalds 		return -EPERM;
26011da177e4SLinus Torvalds 
2602dae6ad8fSAl Viro 	dentry = user_path_create(dfd, filename, &path, 0);
2603dae6ad8fSAl Viro 	if (IS_ERR(dentry))
2604dae6ad8fSAl Viro 		return PTR_ERR(dentry);
26052ad94ae6SAl Viro 
2606dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
2607ce3b0f8dSAl Viro 		mode &= ~current_umask();
2608463c3197SDave Hansen 	error = may_mknod(mode);
2609463c3197SDave Hansen 	if (error)
2610463c3197SDave Hansen 		goto out_dput;
2611dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
2612463c3197SDave Hansen 	if (error)
2613463c3197SDave Hansen 		goto out_dput;
2614dae6ad8fSAl Viro 	error = security_path_mknod(&path, dentry, mode, dev);
2615be6d3e56SKentaro Takeda 	if (error)
2616be6d3e56SKentaro Takeda 		goto out_drop_write;
26171da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
26181da177e4SLinus Torvalds 		case 0: case S_IFREG:
2619dae6ad8fSAl Viro 			error = vfs_create(path.dentry->d_inode,dentry,mode,NULL);
26201da177e4SLinus Torvalds 			break;
26211da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
2622dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
26231da177e4SLinus Torvalds 					new_decode_dev(dev));
26241da177e4SLinus Torvalds 			break;
26251da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
2626dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
26271da177e4SLinus Torvalds 			break;
26281da177e4SLinus Torvalds 	}
2629be6d3e56SKentaro Takeda out_drop_write:
2630dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
2631463c3197SDave Hansen out_dput:
26321da177e4SLinus Torvalds 	dput(dentry);
2633dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2634dae6ad8fSAl Viro 	path_put(&path);
26351da177e4SLinus Torvalds 
26361da177e4SLinus Torvalds 	return error;
26371da177e4SLinus Torvalds }
26381da177e4SLinus Torvalds 
26398208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
26405590ff0dSUlrich Drepper {
26415590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
26425590ff0dSUlrich Drepper }
26435590ff0dSUlrich Drepper 
264418bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
26451da177e4SLinus Torvalds {
2646a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
26478de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
26481da177e4SLinus Torvalds 
26491da177e4SLinus Torvalds 	if (error)
26501da177e4SLinus Torvalds 		return error;
26511da177e4SLinus Torvalds 
2652acfa4380SAl Viro 	if (!dir->i_op->mkdir)
26531da177e4SLinus Torvalds 		return -EPERM;
26541da177e4SLinus Torvalds 
26551da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
26561da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
26571da177e4SLinus Torvalds 	if (error)
26581da177e4SLinus Torvalds 		return error;
26591da177e4SLinus Torvalds 
26608de52778SAl Viro 	if (max_links && dir->i_nlink >= max_links)
26618de52778SAl Viro 		return -EMLINK;
26628de52778SAl Viro 
26631da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2664a74574aaSStephen Smalley 	if (!error)
2665f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
26661da177e4SLinus Torvalds 	return error;
26671da177e4SLinus Torvalds }
26681da177e4SLinus Torvalds 
2669a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
26701da177e4SLinus Torvalds {
26716902d925SDave Hansen 	struct dentry *dentry;
2672dae6ad8fSAl Viro 	struct path path;
2673dae6ad8fSAl Viro 	int error;
26741da177e4SLinus Torvalds 
2675dae6ad8fSAl Viro 	dentry = user_path_create(dfd, pathname, &path, 1);
26766902d925SDave Hansen 	if (IS_ERR(dentry))
2677dae6ad8fSAl Viro 		return PTR_ERR(dentry);
26786902d925SDave Hansen 
2679dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
2680ce3b0f8dSAl Viro 		mode &= ~current_umask();
2681dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
2682463c3197SDave Hansen 	if (error)
2683463c3197SDave Hansen 		goto out_dput;
2684dae6ad8fSAl Viro 	error = security_path_mkdir(&path, dentry, mode);
2685be6d3e56SKentaro Takeda 	if (error)
2686be6d3e56SKentaro Takeda 		goto out_drop_write;
2687dae6ad8fSAl Viro 	error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
2688be6d3e56SKentaro Takeda out_drop_write:
2689dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
2690463c3197SDave Hansen out_dput:
26911da177e4SLinus Torvalds 	dput(dentry);
2692dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2693dae6ad8fSAl Viro 	path_put(&path);
26941da177e4SLinus Torvalds 	return error;
26951da177e4SLinus Torvalds }
26961da177e4SLinus Torvalds 
2697a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
26985590ff0dSUlrich Drepper {
26995590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
27005590ff0dSUlrich Drepper }
27015590ff0dSUlrich Drepper 
27021da177e4SLinus Torvalds /*
2703a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
2704c0d02594SJ. Bruce Fields  * should have a usage count of 1 if we're the only user of this
2705a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
2706a71905f0SSage Weil  * then we drop the dentry now.
27071da177e4SLinus Torvalds  *
27081da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
27091da177e4SLinus Torvalds  * do a
27101da177e4SLinus Torvalds  *
27111da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
27121da177e4SLinus Torvalds  *		return -EBUSY;
27131da177e4SLinus Torvalds  *
27141da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
27151da177e4SLinus Torvalds  * that is still in use by something else..
27161da177e4SLinus Torvalds  */
27171da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
27181da177e4SLinus Torvalds {
27191da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
27201da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
272164252c75SSage Weil 	if (dentry->d_count == 1)
27221da177e4SLinus Torvalds 		__d_drop(dentry);
27231da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
27241da177e4SLinus Torvalds }
27251da177e4SLinus Torvalds 
27261da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
27271da177e4SLinus Torvalds {
27281da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
27291da177e4SLinus Torvalds 
27301da177e4SLinus Torvalds 	if (error)
27311da177e4SLinus Torvalds 		return error;
27321da177e4SLinus Torvalds 
2733acfa4380SAl Viro 	if (!dir->i_op->rmdir)
27341da177e4SLinus Torvalds 		return -EPERM;
27351da177e4SLinus Torvalds 
27361d2ef590SAl Viro 	dget(dentry);
27371b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
2738912dbc15SSage Weil 
27391da177e4SLinus Torvalds 	error = -EBUSY;
2740912dbc15SSage Weil 	if (d_mountpoint(dentry))
2741912dbc15SSage Weil 		goto out;
2742912dbc15SSage Weil 
27431da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
2744912dbc15SSage Weil 	if (error)
2745912dbc15SSage Weil 		goto out;
2746912dbc15SSage Weil 
27473cebde24SSage Weil 	shrink_dcache_parent(dentry);
27481da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
2749912dbc15SSage Weil 	if (error)
2750912dbc15SSage Weil 		goto out;
2751912dbc15SSage Weil 
27521da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
2753d83c49f3SAl Viro 	dont_mount(dentry);
27541da177e4SLinus Torvalds 
2755912dbc15SSage Weil out:
2756912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
27571d2ef590SAl Viro 	dput(dentry);
2758912dbc15SSage Weil 	if (!error)
2759912dbc15SSage Weil 		d_delete(dentry);
27601da177e4SLinus Torvalds 	return error;
27611da177e4SLinus Torvalds }
27621da177e4SLinus Torvalds 
27635590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
27641da177e4SLinus Torvalds {
27651da177e4SLinus Torvalds 	int error = 0;
27661da177e4SLinus Torvalds 	char * name;
27671da177e4SLinus Torvalds 	struct dentry *dentry;
27681da177e4SLinus Torvalds 	struct nameidata nd;
27691da177e4SLinus Torvalds 
27702ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
27711da177e4SLinus Torvalds 	if (error)
27722ad94ae6SAl Viro 		return error;
27731da177e4SLinus Torvalds 
27741da177e4SLinus Torvalds 	switch(nd.last_type) {
27751da177e4SLinus Torvalds 	case LAST_DOTDOT:
27761da177e4SLinus Torvalds 		error = -ENOTEMPTY;
27771da177e4SLinus Torvalds 		goto exit1;
27781da177e4SLinus Torvalds 	case LAST_DOT:
27791da177e4SLinus Torvalds 		error = -EINVAL;
27801da177e4SLinus Torvalds 		goto exit1;
27811da177e4SLinus Torvalds 	case LAST_ROOT:
27821da177e4SLinus Torvalds 		error = -EBUSY;
27831da177e4SLinus Torvalds 		goto exit1;
27841da177e4SLinus Torvalds 	}
27850612d9fbSOGAWA Hirofumi 
27860612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
27870612d9fbSOGAWA Hirofumi 
27884ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
278949705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
27901da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
27916902d925SDave Hansen 	if (IS_ERR(dentry))
27926902d925SDave Hansen 		goto exit2;
2793e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
2794e6bc45d6STheodore Ts'o 		error = -ENOENT;
2795e6bc45d6STheodore Ts'o 		goto exit3;
2796e6bc45d6STheodore Ts'o 	}
27970622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
27980622753bSDave Hansen 	if (error)
27990622753bSDave Hansen 		goto exit3;
2800be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2801be6d3e56SKentaro Takeda 	if (error)
2802be6d3e56SKentaro Takeda 		goto exit4;
28034ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2804be6d3e56SKentaro Takeda exit4:
28050622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
28060622753bSDave Hansen exit3:
28071da177e4SLinus Torvalds 	dput(dentry);
28086902d925SDave Hansen exit2:
28094ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
28101da177e4SLinus Torvalds exit1:
28111d957f9bSJan Blunck 	path_put(&nd.path);
28121da177e4SLinus Torvalds 	putname(name);
28131da177e4SLinus Torvalds 	return error;
28141da177e4SLinus Torvalds }
28151da177e4SLinus Torvalds 
28163cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
28175590ff0dSUlrich Drepper {
28185590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
28195590ff0dSUlrich Drepper }
28205590ff0dSUlrich Drepper 
28211da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
28221da177e4SLinus Torvalds {
28231da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
28241da177e4SLinus Torvalds 
28251da177e4SLinus Torvalds 	if (error)
28261da177e4SLinus Torvalds 		return error;
28271da177e4SLinus Torvalds 
2828acfa4380SAl Viro 	if (!dir->i_op->unlink)
28291da177e4SLinus Torvalds 		return -EPERM;
28301da177e4SLinus Torvalds 
28311b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
28321da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
28331da177e4SLinus Torvalds 		error = -EBUSY;
28341da177e4SLinus Torvalds 	else {
28351da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
2836bec1052eSAl Viro 		if (!error) {
28371da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
2838bec1052eSAl Viro 			if (!error)
2839d83c49f3SAl Viro 				dont_mount(dentry);
2840bec1052eSAl Viro 		}
28411da177e4SLinus Torvalds 	}
28421b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
28431da177e4SLinus Torvalds 
28441da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
28451da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2846ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
28471da177e4SLinus Torvalds 		d_delete(dentry);
28481da177e4SLinus Torvalds 	}
28490eeca283SRobert Love 
28501da177e4SLinus Torvalds 	return error;
28511da177e4SLinus Torvalds }
28521da177e4SLinus Torvalds 
28531da177e4SLinus Torvalds /*
28541da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
28551b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
28561da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
28571da177e4SLinus Torvalds  * while waiting on the I/O.
28581da177e4SLinus Torvalds  */
28595590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
28601da177e4SLinus Torvalds {
28612ad94ae6SAl Viro 	int error;
28621da177e4SLinus Torvalds 	char *name;
28631da177e4SLinus Torvalds 	struct dentry *dentry;
28641da177e4SLinus Torvalds 	struct nameidata nd;
28651da177e4SLinus Torvalds 	struct inode *inode = NULL;
28661da177e4SLinus Torvalds 
28672ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
28681da177e4SLinus Torvalds 	if (error)
28692ad94ae6SAl Viro 		return error;
28702ad94ae6SAl Viro 
28711da177e4SLinus Torvalds 	error = -EISDIR;
28721da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
28731da177e4SLinus Torvalds 		goto exit1;
28740612d9fbSOGAWA Hirofumi 
28750612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
28760612d9fbSOGAWA Hirofumi 
28774ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
287849705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
28791da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
28801da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
28811da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
288250338b88STörök Edwin 		if (nd.last.name[nd.last.len])
288350338b88STörök Edwin 			goto slashes;
28841da177e4SLinus Torvalds 		inode = dentry->d_inode;
288550338b88STörök Edwin 		if (!inode)
2886e6bc45d6STheodore Ts'o 			goto slashes;
28877de9c6eeSAl Viro 		ihold(inode);
28880622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
28890622753bSDave Hansen 		if (error)
28900622753bSDave Hansen 			goto exit2;
2891be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2892be6d3e56SKentaro Takeda 		if (error)
2893be6d3e56SKentaro Takeda 			goto exit3;
28944ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2895be6d3e56SKentaro Takeda exit3:
28960622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
28971da177e4SLinus Torvalds 	exit2:
28981da177e4SLinus Torvalds 		dput(dentry);
28991da177e4SLinus Torvalds 	}
29004ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
29011da177e4SLinus Torvalds 	if (inode)
29021da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
29031da177e4SLinus Torvalds exit1:
29041d957f9bSJan Blunck 	path_put(&nd.path);
29051da177e4SLinus Torvalds 	putname(name);
29061da177e4SLinus Torvalds 	return error;
29071da177e4SLinus Torvalds 
29081da177e4SLinus Torvalds slashes:
29091da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
29101da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
29111da177e4SLinus Torvalds 	goto exit2;
29121da177e4SLinus Torvalds }
29131da177e4SLinus Torvalds 
29142e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
29155590ff0dSUlrich Drepper {
29165590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
29175590ff0dSUlrich Drepper 		return -EINVAL;
29185590ff0dSUlrich Drepper 
29195590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
29205590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
29215590ff0dSUlrich Drepper 
29225590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
29235590ff0dSUlrich Drepper }
29245590ff0dSUlrich Drepper 
29253480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
29265590ff0dSUlrich Drepper {
29275590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
29285590ff0dSUlrich Drepper }
29295590ff0dSUlrich Drepper 
2930db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
29311da177e4SLinus Torvalds {
2932a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
29331da177e4SLinus Torvalds 
29341da177e4SLinus Torvalds 	if (error)
29351da177e4SLinus Torvalds 		return error;
29361da177e4SLinus Torvalds 
2937acfa4380SAl Viro 	if (!dir->i_op->symlink)
29381da177e4SLinus Torvalds 		return -EPERM;
29391da177e4SLinus Torvalds 
29401da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
29411da177e4SLinus Torvalds 	if (error)
29421da177e4SLinus Torvalds 		return error;
29431da177e4SLinus Torvalds 
29441da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2945a74574aaSStephen Smalley 	if (!error)
2946f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
29471da177e4SLinus Torvalds 	return error;
29481da177e4SLinus Torvalds }
29491da177e4SLinus Torvalds 
29502e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
29512e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
29521da177e4SLinus Torvalds {
29532ad94ae6SAl Viro 	int error;
29541da177e4SLinus Torvalds 	char *from;
29556902d925SDave Hansen 	struct dentry *dentry;
2956dae6ad8fSAl Viro 	struct path path;
29571da177e4SLinus Torvalds 
29581da177e4SLinus Torvalds 	from = getname(oldname);
29591da177e4SLinus Torvalds 	if (IS_ERR(from))
29601da177e4SLinus Torvalds 		return PTR_ERR(from);
29612ad94ae6SAl Viro 
2962dae6ad8fSAl Viro 	dentry = user_path_create(newdfd, newname, &path, 0);
29631da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
29646902d925SDave Hansen 	if (IS_ERR(dentry))
2965dae6ad8fSAl Viro 		goto out_putname;
29666902d925SDave Hansen 
2967dae6ad8fSAl Viro 	error = mnt_want_write(path.mnt);
296875c3f29dSDave Hansen 	if (error)
296975c3f29dSDave Hansen 		goto out_dput;
2970dae6ad8fSAl Viro 	error = security_path_symlink(&path, dentry, from);
2971be6d3e56SKentaro Takeda 	if (error)
2972be6d3e56SKentaro Takeda 		goto out_drop_write;
2973dae6ad8fSAl Viro 	error = vfs_symlink(path.dentry->d_inode, dentry, from);
2974be6d3e56SKentaro Takeda out_drop_write:
2975dae6ad8fSAl Viro 	mnt_drop_write(path.mnt);
297675c3f29dSDave Hansen out_dput:
29771da177e4SLinus Torvalds 	dput(dentry);
2978dae6ad8fSAl Viro 	mutex_unlock(&path.dentry->d_inode->i_mutex);
2979dae6ad8fSAl Viro 	path_put(&path);
29806902d925SDave Hansen out_putname:
29811da177e4SLinus Torvalds 	putname(from);
29821da177e4SLinus Torvalds 	return error;
29831da177e4SLinus Torvalds }
29841da177e4SLinus Torvalds 
29853480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
29865590ff0dSUlrich Drepper {
29875590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
29885590ff0dSUlrich Drepper }
29895590ff0dSUlrich Drepper 
29901da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
29911da177e4SLinus Torvalds {
29921da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
29938de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
29941da177e4SLinus Torvalds 	int error;
29951da177e4SLinus Torvalds 
29961da177e4SLinus Torvalds 	if (!inode)
29971da177e4SLinus Torvalds 		return -ENOENT;
29981da177e4SLinus Torvalds 
2999a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
30001da177e4SLinus Torvalds 	if (error)
30011da177e4SLinus Torvalds 		return error;
30021da177e4SLinus Torvalds 
30031da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
30041da177e4SLinus Torvalds 		return -EXDEV;
30051da177e4SLinus Torvalds 
30061da177e4SLinus Torvalds 	/*
30071da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
30081da177e4SLinus Torvalds 	 */
30091da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
30101da177e4SLinus Torvalds 		return -EPERM;
3011acfa4380SAl Viro 	if (!dir->i_op->link)
30121da177e4SLinus Torvalds 		return -EPERM;
30137e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
30141da177e4SLinus Torvalds 		return -EPERM;
30151da177e4SLinus Torvalds 
30161da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
30171da177e4SLinus Torvalds 	if (error)
30181da177e4SLinus Torvalds 		return error;
30191da177e4SLinus Torvalds 
30207e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
3021aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
3022aae8a97dSAneesh Kumar K.V 	if (inode->i_nlink == 0)
3023aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
30248de52778SAl Viro 	else if (max_links && inode->i_nlink >= max_links)
30258de52778SAl Viro 		error = -EMLINK;
3026aae8a97dSAneesh Kumar K.V 	else
30271da177e4SLinus Torvalds 		error = dir->i_op->link(old_dentry, dir, new_dentry);
30287e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
3029e31e14ecSStephen Smalley 	if (!error)
30307e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
30311da177e4SLinus Torvalds 	return error;
30321da177e4SLinus Torvalds }
30331da177e4SLinus Torvalds 
30341da177e4SLinus Torvalds /*
30351da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
30361da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
30371da177e4SLinus Torvalds  * newname.  --KAB
30381da177e4SLinus Torvalds  *
30391da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
30401da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
30411da177e4SLinus Torvalds  * and other special files.  --ADM
30421da177e4SLinus Torvalds  */
30432e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
30442e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
30451da177e4SLinus Torvalds {
30461da177e4SLinus Torvalds 	struct dentry *new_dentry;
3047dae6ad8fSAl Viro 	struct path old_path, new_path;
304811a7b371SAneesh Kumar K.V 	int how = 0;
30491da177e4SLinus Torvalds 	int error;
30501da177e4SLinus Torvalds 
305111a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3052c04030e1SUlrich Drepper 		return -EINVAL;
305311a7b371SAneesh Kumar K.V 	/*
305411a7b371SAneesh Kumar K.V 	 * To use null names we require CAP_DAC_READ_SEARCH
305511a7b371SAneesh Kumar K.V 	 * This ensures that not everyone will be able to create
305611a7b371SAneesh Kumar K.V 	 * handlink using the passed filedescriptor.
305711a7b371SAneesh Kumar K.V 	 */
305811a7b371SAneesh Kumar K.V 	if (flags & AT_EMPTY_PATH) {
305911a7b371SAneesh Kumar K.V 		if (!capable(CAP_DAC_READ_SEARCH))
306011a7b371SAneesh Kumar K.V 			return -ENOENT;
306111a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
306211a7b371SAneesh Kumar K.V 	}
3063c04030e1SUlrich Drepper 
306411a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
306511a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
306611a7b371SAneesh Kumar K.V 
306711a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
30681da177e4SLinus Torvalds 	if (error)
30692ad94ae6SAl Viro 		return error;
30702ad94ae6SAl Viro 
3071dae6ad8fSAl Viro 	new_dentry = user_path_create(newdfd, newname, &new_path, 0);
30721da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
30736902d925SDave Hansen 	if (IS_ERR(new_dentry))
3074dae6ad8fSAl Viro 		goto out;
3075dae6ad8fSAl Viro 
3076dae6ad8fSAl Viro 	error = -EXDEV;
3077dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt)
3078dae6ad8fSAl Viro 		goto out_dput;
3079dae6ad8fSAl Viro 	error = mnt_want_write(new_path.mnt);
308075c3f29dSDave Hansen 	if (error)
308175c3f29dSDave Hansen 		goto out_dput;
3082dae6ad8fSAl Viro 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
3083be6d3e56SKentaro Takeda 	if (error)
3084be6d3e56SKentaro Takeda 		goto out_drop_write;
3085dae6ad8fSAl Viro 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
3086be6d3e56SKentaro Takeda out_drop_write:
3087dae6ad8fSAl Viro 	mnt_drop_write(new_path.mnt);
308875c3f29dSDave Hansen out_dput:
30891da177e4SLinus Torvalds 	dput(new_dentry);
3090dae6ad8fSAl Viro 	mutex_unlock(&new_path.dentry->d_inode->i_mutex);
3091dae6ad8fSAl Viro 	path_put(&new_path);
30921da177e4SLinus Torvalds out:
30932d8f3038SAl Viro 	path_put(&old_path);
30941da177e4SLinus Torvalds 
30951da177e4SLinus Torvalds 	return error;
30961da177e4SLinus Torvalds }
30971da177e4SLinus Torvalds 
30983480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
30995590ff0dSUlrich Drepper {
3100c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
31015590ff0dSUlrich Drepper }
31025590ff0dSUlrich Drepper 
31031da177e4SLinus Torvalds /*
31041da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
31051da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
31061da177e4SLinus Torvalds  * Problems:
31071da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
31081da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
31091da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
3110a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
31111da177e4SLinus Torvalds  *	   story.
31121da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
31131b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
31141da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
31151da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
3116a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
31171da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
31181da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
31191da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
3120a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
31211da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
31221da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
31231da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
3124e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
31251b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
31261da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3127c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
31281da177e4SLinus Torvalds  *	   locking].
31291da177e4SLinus Torvalds  */
313075c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
31311da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
31321da177e4SLinus Torvalds {
31331da177e4SLinus Torvalds 	int error = 0;
31349055cba7SSage Weil 	struct inode *target = new_dentry->d_inode;
31358de52778SAl Viro 	unsigned max_links = new_dir->i_sb->s_max_links;
31361da177e4SLinus Torvalds 
31371da177e4SLinus Torvalds 	/*
31381da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
31391da177e4SLinus Torvalds 	 * we'll need to flip '..'.
31401da177e4SLinus Torvalds 	 */
31411da177e4SLinus Torvalds 	if (new_dir != old_dir) {
3142f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
31431da177e4SLinus Torvalds 		if (error)
31441da177e4SLinus Torvalds 			return error;
31451da177e4SLinus Torvalds 	}
31461da177e4SLinus Torvalds 
31471da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
31481da177e4SLinus Torvalds 	if (error)
31491da177e4SLinus Torvalds 		return error;
31501da177e4SLinus Torvalds 
31511d2ef590SAl Viro 	dget(new_dentry);
3152d83c49f3SAl Viro 	if (target)
31531b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
31549055cba7SSage Weil 
31551da177e4SLinus Torvalds 	error = -EBUSY;
31569055cba7SSage Weil 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
31579055cba7SSage Weil 		goto out;
31589055cba7SSage Weil 
31598de52778SAl Viro 	error = -EMLINK;
31608de52778SAl Viro 	if (max_links && !target && new_dir != old_dir &&
31618de52778SAl Viro 	    new_dir->i_nlink >= max_links)
31628de52778SAl Viro 		goto out;
31638de52778SAl Viro 
31643cebde24SSage Weil 	if (target)
31653cebde24SSage Weil 		shrink_dcache_parent(new_dentry);
31661da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
31679055cba7SSage Weil 	if (error)
31689055cba7SSage Weil 		goto out;
31699055cba7SSage Weil 
31701da177e4SLinus Torvalds 	if (target) {
31711da177e4SLinus Torvalds 		target->i_flags |= S_DEAD;
3172d83c49f3SAl Viro 		dont_mount(new_dentry);
3173d83c49f3SAl Viro 	}
31749055cba7SSage Weil out:
31759055cba7SSage Weil 	if (target)
31761b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
31771d2ef590SAl Viro 	dput(new_dentry);
3178e31e14ecSStephen Smalley 	if (!error)
3179349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
31801da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
31811da177e4SLinus Torvalds 	return error;
31821da177e4SLinus Torvalds }
31831da177e4SLinus Torvalds 
318475c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
31851da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
31861da177e4SLinus Torvalds {
318751892bbbSSage Weil 	struct inode *target = new_dentry->d_inode;
31881da177e4SLinus Torvalds 	int error;
31891da177e4SLinus Torvalds 
31901da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
31911da177e4SLinus Torvalds 	if (error)
31921da177e4SLinus Torvalds 		return error;
31931da177e4SLinus Torvalds 
31941da177e4SLinus Torvalds 	dget(new_dentry);
31951da177e4SLinus Torvalds 	if (target)
31961b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
319751892bbbSSage Weil 
31981da177e4SLinus Torvalds 	error = -EBUSY;
319951892bbbSSage Weil 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
320051892bbbSSage Weil 		goto out;
320151892bbbSSage Weil 
32021da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
320351892bbbSSage Weil 	if (error)
320451892bbbSSage Weil 		goto out;
320551892bbbSSage Weil 
3206bec1052eSAl Viro 	if (target)
3207d83c49f3SAl Viro 		dont_mount(new_dentry);
3208349457ccSMark Fasheh 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
32091da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
321051892bbbSSage Weil out:
32111da177e4SLinus Torvalds 	if (target)
32121b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
32131da177e4SLinus Torvalds 	dput(new_dentry);
32141da177e4SLinus Torvalds 	return error;
32151da177e4SLinus Torvalds }
32161da177e4SLinus Torvalds 
32171da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
32181da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
32191da177e4SLinus Torvalds {
32201da177e4SLinus Torvalds 	int error;
32211da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
322259b0df21SEric Paris 	const unsigned char *old_name;
32231da177e4SLinus Torvalds 
32241da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
32251da177e4SLinus Torvalds  		return 0;
32261da177e4SLinus Torvalds 
32271da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
32281da177e4SLinus Torvalds 	if (error)
32291da177e4SLinus Torvalds 		return error;
32301da177e4SLinus Torvalds 
32311da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3232a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
32331da177e4SLinus Torvalds 	else
32341da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
32351da177e4SLinus Torvalds 	if (error)
32361da177e4SLinus Torvalds 		return error;
32371da177e4SLinus Torvalds 
3238acfa4380SAl Viro 	if (!old_dir->i_op->rename)
32391da177e4SLinus Torvalds 		return -EPERM;
32401da177e4SLinus Torvalds 
32410eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
32420eeca283SRobert Love 
32431da177e4SLinus Torvalds 	if (is_dir)
32441da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
32451da177e4SLinus Torvalds 	else
32461da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3247123df294SAl Viro 	if (!error)
3248123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
32495a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
32500eeca283SRobert Love 	fsnotify_oldname_free(old_name);
32510eeca283SRobert Love 
32521da177e4SLinus Torvalds 	return error;
32531da177e4SLinus Torvalds }
32541da177e4SLinus Torvalds 
32552e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
32562e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
32571da177e4SLinus Torvalds {
32581da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
32591da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
32601da177e4SLinus Torvalds 	struct dentry *trap;
32611da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
32622ad94ae6SAl Viro 	char *from;
32632ad94ae6SAl Viro 	char *to;
32642ad94ae6SAl Viro 	int error;
32651da177e4SLinus Torvalds 
32662ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
32671da177e4SLinus Torvalds 	if (error)
32681da177e4SLinus Torvalds 		goto exit;
32691da177e4SLinus Torvalds 
32702ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
32711da177e4SLinus Torvalds 	if (error)
32721da177e4SLinus Torvalds 		goto exit1;
32731da177e4SLinus Torvalds 
32741da177e4SLinus Torvalds 	error = -EXDEV;
32754ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
32761da177e4SLinus Torvalds 		goto exit2;
32771da177e4SLinus Torvalds 
32784ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
32791da177e4SLinus Torvalds 	error = -EBUSY;
32801da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
32811da177e4SLinus Torvalds 		goto exit2;
32821da177e4SLinus Torvalds 
32834ac91378SJan Blunck 	new_dir = newnd.path.dentry;
32841da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
32851da177e4SLinus Torvalds 		goto exit2;
32861da177e4SLinus Torvalds 
32870612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
32880612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
32894e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
32900612d9fbSOGAWA Hirofumi 
32911da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
32921da177e4SLinus Torvalds 
329349705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
32941da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
32951da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
32961da177e4SLinus Torvalds 		goto exit3;
32971da177e4SLinus Torvalds 	/* source must exist */
32981da177e4SLinus Torvalds 	error = -ENOENT;
32991da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
33001da177e4SLinus Torvalds 		goto exit4;
33011da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
33021da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
33031da177e4SLinus Torvalds 		error = -ENOTDIR;
33041da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
33051da177e4SLinus Torvalds 			goto exit4;
33061da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
33071da177e4SLinus Torvalds 			goto exit4;
33081da177e4SLinus Torvalds 	}
33091da177e4SLinus Torvalds 	/* source should not be ancestor of target */
33101da177e4SLinus Torvalds 	error = -EINVAL;
33111da177e4SLinus Torvalds 	if (old_dentry == trap)
33121da177e4SLinus Torvalds 		goto exit4;
331349705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
33141da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
33151da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
33161da177e4SLinus Torvalds 		goto exit4;
33171da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
33181da177e4SLinus Torvalds 	error = -ENOTEMPTY;
33191da177e4SLinus Torvalds 	if (new_dentry == trap)
33201da177e4SLinus Torvalds 		goto exit5;
33211da177e4SLinus Torvalds 
33229079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
33239079b1ebSDave Hansen 	if (error)
33249079b1ebSDave Hansen 		goto exit5;
3325be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3326be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3327be6d3e56SKentaro Takeda 	if (error)
3328be6d3e56SKentaro Takeda 		goto exit6;
33291da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
33301da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
3331be6d3e56SKentaro Takeda exit6:
33329079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
33331da177e4SLinus Torvalds exit5:
33341da177e4SLinus Torvalds 	dput(new_dentry);
33351da177e4SLinus Torvalds exit4:
33361da177e4SLinus Torvalds 	dput(old_dentry);
33371da177e4SLinus Torvalds exit3:
33381da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
33391da177e4SLinus Torvalds exit2:
33401d957f9bSJan Blunck 	path_put(&newnd.path);
33412ad94ae6SAl Viro 	putname(to);
33421da177e4SLinus Torvalds exit1:
33431d957f9bSJan Blunck 	path_put(&oldnd.path);
33441da177e4SLinus Torvalds 	putname(from);
33452ad94ae6SAl Viro exit:
33461da177e4SLinus Torvalds 	return error;
33471da177e4SLinus Torvalds }
33481da177e4SLinus Torvalds 
3349a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
33505590ff0dSUlrich Drepper {
33515590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
33525590ff0dSUlrich Drepper }
33535590ff0dSUlrich Drepper 
33541da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
33551da177e4SLinus Torvalds {
33561da177e4SLinus Torvalds 	int len;
33571da177e4SLinus Torvalds 
33581da177e4SLinus Torvalds 	len = PTR_ERR(link);
33591da177e4SLinus Torvalds 	if (IS_ERR(link))
33601da177e4SLinus Torvalds 		goto out;
33611da177e4SLinus Torvalds 
33621da177e4SLinus Torvalds 	len = strlen(link);
33631da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
33641da177e4SLinus Torvalds 		len = buflen;
33651da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
33661da177e4SLinus Torvalds 		len = -EFAULT;
33671da177e4SLinus Torvalds out:
33681da177e4SLinus Torvalds 	return len;
33691da177e4SLinus Torvalds }
33701da177e4SLinus Torvalds 
33711da177e4SLinus Torvalds /*
33721da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
33731da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
33741da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
33751da177e4SLinus Torvalds  */
33761da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
33771da177e4SLinus Torvalds {
33781da177e4SLinus Torvalds 	struct nameidata nd;
3379cc314eefSLinus Torvalds 	void *cookie;
3380694a1764SMarcin Slusarz 	int res;
3381cc314eefSLinus Torvalds 
33821da177e4SLinus Torvalds 	nd.depth = 0;
3383cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3384694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3385694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3386694a1764SMarcin Slusarz 
3387694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
33881da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3389cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3390694a1764SMarcin Slusarz 	return res;
33911da177e4SLinus Torvalds }
33921da177e4SLinus Torvalds 
33931da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
33941da177e4SLinus Torvalds {
33951da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
33961da177e4SLinus Torvalds }
33971da177e4SLinus Torvalds 
33981da177e4SLinus Torvalds /* get the link contents into pagecache */
33991da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
34001da177e4SLinus Torvalds {
3401ebd09abbSDuane Griffin 	char *kaddr;
34021da177e4SLinus Torvalds 	struct page *page;
34031da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3404090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
34051da177e4SLinus Torvalds 	if (IS_ERR(page))
34066fe6900eSNick Piggin 		return (char*)page;
34071da177e4SLinus Torvalds 	*ppage = page;
3408ebd09abbSDuane Griffin 	kaddr = kmap(page);
3409ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3410ebd09abbSDuane Griffin 	return kaddr;
34111da177e4SLinus Torvalds }
34121da177e4SLinus Torvalds 
34131da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
34141da177e4SLinus Torvalds {
34151da177e4SLinus Torvalds 	struct page *page = NULL;
34161da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
34171da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
34181da177e4SLinus Torvalds 	if (page) {
34191da177e4SLinus Torvalds 		kunmap(page);
34201da177e4SLinus Torvalds 		page_cache_release(page);
34211da177e4SLinus Torvalds 	}
34221da177e4SLinus Torvalds 	return res;
34231da177e4SLinus Torvalds }
34241da177e4SLinus Torvalds 
3425cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
34261da177e4SLinus Torvalds {
3427cc314eefSLinus Torvalds 	struct page *page = NULL;
34281da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3429cc314eefSLinus Torvalds 	return page;
34301da177e4SLinus Torvalds }
34311da177e4SLinus Torvalds 
3432cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
34331da177e4SLinus Torvalds {
3434cc314eefSLinus Torvalds 	struct page *page = cookie;
3435cc314eefSLinus Torvalds 
3436cc314eefSLinus Torvalds 	if (page) {
34371da177e4SLinus Torvalds 		kunmap(page);
34381da177e4SLinus Torvalds 		page_cache_release(page);
34391da177e4SLinus Torvalds 	}
34401da177e4SLinus Torvalds }
34411da177e4SLinus Torvalds 
344254566b2cSNick Piggin /*
344354566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
344454566b2cSNick Piggin  */
344554566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
34461da177e4SLinus Torvalds {
34471da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
34480adb25d2SKirill Korotaev 	struct page *page;
3449afddba49SNick Piggin 	void *fsdata;
3450beb497abSDmitriy Monakhov 	int err;
34511da177e4SLinus Torvalds 	char *kaddr;
345254566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
345354566b2cSNick Piggin 	if (nofs)
345454566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
34551da177e4SLinus Torvalds 
34567e53cac4SNeilBrown retry:
3457afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
345854566b2cSNick Piggin 				flags, &page, &fsdata);
34591da177e4SLinus Torvalds 	if (err)
3460afddba49SNick Piggin 		goto fail;
3461afddba49SNick Piggin 
3462e8e3c3d6SCong Wang 	kaddr = kmap_atomic(page);
34631da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
3464e8e3c3d6SCong Wang 	kunmap_atomic(kaddr);
3465afddba49SNick Piggin 
3466afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3467afddba49SNick Piggin 							page, fsdata);
34681da177e4SLinus Torvalds 	if (err < 0)
34691da177e4SLinus Torvalds 		goto fail;
3470afddba49SNick Piggin 	if (err < len-1)
3471afddba49SNick Piggin 		goto retry;
3472afddba49SNick Piggin 
34731da177e4SLinus Torvalds 	mark_inode_dirty(inode);
34741da177e4SLinus Torvalds 	return 0;
34751da177e4SLinus Torvalds fail:
34761da177e4SLinus Torvalds 	return err;
34771da177e4SLinus Torvalds }
34781da177e4SLinus Torvalds 
34790adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
34800adb25d2SKirill Korotaev {
34810adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
348254566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
34830adb25d2SKirill Korotaev }
34840adb25d2SKirill Korotaev 
348592e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
34861da177e4SLinus Torvalds 	.readlink	= generic_readlink,
34871da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
34881da177e4SLinus Torvalds 	.put_link	= page_put_link,
34891da177e4SLinus Torvalds };
34901da177e4SLinus Torvalds 
34912d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
3492cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
34931da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
34941da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
34951da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
34961da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
34971da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
34981da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
34991da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
35001da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
35011da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
35020adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
35031da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
35041da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
3505d1811465SAl Viro EXPORT_SYMBOL(kern_path);
350616f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
3507f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
35081da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
35091da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
35101da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
35111da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
35121da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
35131da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
35141da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
35151da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
35161da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
35171da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
35181da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
35191da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
35201da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
35211da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
3522