xref: /openbmc/linux/fs/namei.c (revision 669abf4e)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/namei.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  * Some corrections by tytso.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
121da177e4SLinus Torvalds  * lookup logic.
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/init.h>
18630d9c47SPaul Gortmaker #include <linux/export.h>
1944696908SDavid S. Miller #include <linux/kernel.h>
201da177e4SLinus Torvalds #include <linux/slab.h>
211da177e4SLinus Torvalds #include <linux/fs.h>
221da177e4SLinus Torvalds #include <linux/namei.h>
231da177e4SLinus Torvalds #include <linux/pagemap.h>
240eeca283SRobert Love #include <linux/fsnotify.h>
251da177e4SLinus Torvalds #include <linux/personality.h>
261da177e4SLinus Torvalds #include <linux/security.h>
276146f0d5SMimi Zohar #include <linux/ima.h>
281da177e4SLinus Torvalds #include <linux/syscalls.h>
291da177e4SLinus Torvalds #include <linux/mount.h>
301da177e4SLinus Torvalds #include <linux/audit.h>
3116f7e0feSRandy Dunlap #include <linux/capability.h>
32834f2a4aSTrond Myklebust #include <linux/file.h>
335590ff0dSUlrich Drepper #include <linux/fcntl.h>
3408ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h>
355ad4e53bSAl Viro #include <linux/fs_struct.h>
36e77819e5SLinus Torvalds #include <linux/posix_acl.h>
371da177e4SLinus Torvalds #include <asm/uaccess.h>
381da177e4SLinus Torvalds 
39e81e3f4dSEric Paris #include "internal.h"
40c7105365SAl Viro #include "mount.h"
41e81e3f4dSEric Paris 
421da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer]
431da177e4SLinus Torvalds  * Fundamental changes in the pathname lookup mechanisms (namei)
441da177e4SLinus Torvalds  * were necessary because of omirr.  The reason is that omirr needs
451da177e4SLinus Torvalds  * to know the _real_ pathname, not the user-supplied one, in case
461da177e4SLinus Torvalds  * of symlinks (and also when transname replacements occur).
471da177e4SLinus Torvalds  *
481da177e4SLinus Torvalds  * The new code replaces the old recursive symlink resolution with
491da177e4SLinus Torvalds  * an iterative one (in case of non-nested symlink chains).  It does
501da177e4SLinus Torvalds  * this with calls to <fs>_follow_link().
511da177e4SLinus Torvalds  * As a side effect, dir_namei(), _namei() and follow_link() are now
521da177e4SLinus Torvalds  * replaced with a single function lookup_dentry() that can handle all
531da177e4SLinus Torvalds  * the special cases of the former code.
541da177e4SLinus Torvalds  *
551da177e4SLinus Torvalds  * With the new dcache, the pathname is stored at each inode, at least as
561da177e4SLinus Torvalds  * long as the refcount of the inode is positive.  As a side effect, the
571da177e4SLinus Torvalds  * size of the dcache depends on the inode cache and thus is dynamic.
581da177e4SLinus Torvalds  *
591da177e4SLinus Torvalds  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
601da177e4SLinus Torvalds  * resolution to correspond with current state of the code.
611da177e4SLinus Torvalds  *
621da177e4SLinus Torvalds  * Note that the symlink resolution is not *completely* iterative.
631da177e4SLinus Torvalds  * There is still a significant amount of tail- and mid- recursion in
641da177e4SLinus Torvalds  * the algorithm.  Also, note that <fs>_readlink() is not used in
651da177e4SLinus Torvalds  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
661da177e4SLinus Torvalds  * may return different results than <fs>_follow_link().  Many virtual
671da177e4SLinus Torvalds  * filesystems (including /proc) exhibit this behavior.
681da177e4SLinus Torvalds  */
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
711da177e4SLinus Torvalds  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
721da177e4SLinus Torvalds  * and the name already exists in form of a symlink, try to create the new
731da177e4SLinus Torvalds  * name indicated by the symlink. The old code always complained that the
741da177e4SLinus Torvalds  * name already exists, due to not following the symlink even if its target
751da177e4SLinus Torvalds  * is nonexistent.  The new semantics affects also mknod() and link() when
7625985edcSLucas De Marchi  * the name is a symlink pointing to a non-existent name.
771da177e4SLinus Torvalds  *
781da177e4SLinus Torvalds  * I don't know which semantics is the right one, since I have no access
791da177e4SLinus Torvalds  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
801da177e4SLinus Torvalds  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
811da177e4SLinus Torvalds  * "old" one. Personally, I think the new semantics is much more logical.
821da177e4SLinus Torvalds  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
831da177e4SLinus Torvalds  * file does succeed in both HP-UX and SunOs, but not in Solaris
841da177e4SLinus Torvalds  * and in the old Linux semantics.
851da177e4SLinus Torvalds  */
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
881da177e4SLinus Torvalds  * semantics.  See the comments in "open_namei" and "do_link" below.
891da177e4SLinus Torvalds  *
901da177e4SLinus Torvalds  * [10-Sep-98 Alan Modra] Another symlink change.
911da177e4SLinus Torvalds  */
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
941da177e4SLinus Torvalds  *	inside the path - always follow.
951da177e4SLinus Torvalds  *	in the last component in creation/removal/renaming - never follow.
961da177e4SLinus Torvalds  *	if LOOKUP_FOLLOW passed - follow.
971da177e4SLinus Torvalds  *	if the pathname has trailing slashes - follow.
981da177e4SLinus Torvalds  *	otherwise - don't follow.
991da177e4SLinus Torvalds  * (applied in that order).
1001da177e4SLinus Torvalds  *
1011da177e4SLinus Torvalds  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
1021da177e4SLinus Torvalds  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
1031da177e4SLinus Torvalds  * During the 2.4 we need to fix the userland stuff depending on it -
1041da177e4SLinus Torvalds  * hopefully we will be able to get rid of that wart in 2.5. So far only
1051da177e4SLinus Torvalds  * XEmacs seems to be relying on it...
1061da177e4SLinus Torvalds  */
1071da177e4SLinus Torvalds /*
1081da177e4SLinus Torvalds  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
109a11f3a05SArjan van de Ven  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
1101da177e4SLinus Torvalds  * any extra contention...
1111da177e4SLinus Torvalds  */
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional
1141da177e4SLinus Torvalds  * checking and hopefully speeding things up, we copy filenames to the
1151da177e4SLinus Torvalds  * kernel data space before using them..
1161da177e4SLinus Torvalds  *
1171da177e4SLinus Torvalds  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
1181da177e4SLinus Torvalds  * PATH_MAX includes the nul terminator --RR.
1191da177e4SLinus Torvalds  */
12091a27b2aSJeff Layton void final_putname(struct filename *name)
1211da177e4SLinus Torvalds {
12291a27b2aSJeff Layton 	__putname(name->name);
12391a27b2aSJeff Layton 	kfree(name);
12491a27b2aSJeff Layton }
12591a27b2aSJeff Layton 
12691a27b2aSJeff Layton static struct filename *
12791a27b2aSJeff Layton getname_flags(const char __user *filename, int flags, int *empty)
12891a27b2aSJeff Layton {
12991a27b2aSJeff Layton 	struct filename *result, *err;
13091a27b2aSJeff Layton 	char *kname;
1313f9f0aa6SLinus Torvalds 	int len;
1321da177e4SLinus Torvalds 
1337ac86265SJeff Layton 	result = audit_reusename(filename);
1347ac86265SJeff Layton 	if (result)
1357ac86265SJeff Layton 		return result;
1367ac86265SJeff Layton 
13791a27b2aSJeff Layton 	/* FIXME: create dedicated slabcache? */
13891a27b2aSJeff Layton 	result = kzalloc(sizeof(*result), GFP_KERNEL);
1393f9f0aa6SLinus Torvalds 	if (unlikely(!result))
1404043cde8SEric Paris 		return ERR_PTR(-ENOMEM);
1411da177e4SLinus Torvalds 
14291a27b2aSJeff Layton 	kname = __getname();
14391a27b2aSJeff Layton 	if (unlikely(!kname)) {
14491a27b2aSJeff Layton 		err = ERR_PTR(-ENOMEM);
14591a27b2aSJeff Layton 		goto error_free_name;
14691a27b2aSJeff Layton 	}
14791a27b2aSJeff Layton 
14891a27b2aSJeff Layton 	result->name = kname;
14991a27b2aSJeff Layton 	result->uptr = filename;
15091a27b2aSJeff Layton 	len = strncpy_from_user(kname, filename, PATH_MAX);
15191a27b2aSJeff Layton 	if (unlikely(len < 0)) {
1523f9f0aa6SLinus Torvalds 		err = ERR_PTR(len);
1533f9f0aa6SLinus Torvalds 		goto error;
15491a27b2aSJeff Layton 	}
1553f9f0aa6SLinus Torvalds 
1563f9f0aa6SLinus Torvalds 	/* The empty path is special. */
1573f9f0aa6SLinus Torvalds 	if (unlikely(!len)) {
1583f9f0aa6SLinus Torvalds 		if (empty)
1591fa1e7f6SAndy Whitcroft 			*empty = 1;
1603f9f0aa6SLinus Torvalds 		err = ERR_PTR(-ENOENT);
1613f9f0aa6SLinus Torvalds 		if (!(flags & LOOKUP_EMPTY))
1623f9f0aa6SLinus Torvalds 			goto error;
1631da177e4SLinus Torvalds 	}
1643f9f0aa6SLinus Torvalds 
1653f9f0aa6SLinus Torvalds 	err = ERR_PTR(-ENAMETOOLONG);
1663f9f0aa6SLinus Torvalds 	if (likely(len < PATH_MAX)) {
1671da177e4SLinus Torvalds 		audit_getname(result);
1681da177e4SLinus Torvalds 		return result;
1691da177e4SLinus Torvalds 	}
1701da177e4SLinus Torvalds 
1713f9f0aa6SLinus Torvalds error:
17291a27b2aSJeff Layton 	__putname(kname);
17391a27b2aSJeff Layton error_free_name:
17491a27b2aSJeff Layton 	kfree(result);
1753f9f0aa6SLinus Torvalds 	return err;
1763f9f0aa6SLinus Torvalds }
1773f9f0aa6SLinus Torvalds 
17891a27b2aSJeff Layton struct filename *
17991a27b2aSJeff Layton getname(const char __user * filename)
180f52e0c11SAl Viro {
181f7493e5dSLinus Torvalds 	return getname_flags(filename, 0, NULL);
182f52e0c11SAl Viro }
18391a27b2aSJeff Layton EXPORT_SYMBOL(getname);
184f52e0c11SAl Viro 
1851da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
18691a27b2aSJeff Layton void putname(struct filename *name)
1871da177e4SLinus Torvalds {
1885ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
18991a27b2aSJeff Layton 		return audit_putname(name);
19091a27b2aSJeff Layton 	final_putname(name);
1911da177e4SLinus Torvalds }
1921da177e4SLinus Torvalds #endif
1931da177e4SLinus Torvalds 
194e77819e5SLinus Torvalds static int check_acl(struct inode *inode, int mask)
195e77819e5SLinus Torvalds {
19684635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL
197e77819e5SLinus Torvalds 	struct posix_acl *acl;
198e77819e5SLinus Torvalds 
199e77819e5SLinus Torvalds 	if (mask & MAY_NOT_BLOCK) {
2003567866bSAl Viro 		acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
2013567866bSAl Viro 	        if (!acl)
202e77819e5SLinus Torvalds 	                return -EAGAIN;
2033567866bSAl Viro 		/* no ->get_acl() calls in RCU mode... */
2043567866bSAl Viro 		if (acl == ACL_NOT_CACHED)
205e77819e5SLinus Torvalds 			return -ECHILD;
206206b1d09SAri Savolainen 	        return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
207e77819e5SLinus Torvalds 	}
208e77819e5SLinus Torvalds 
209e77819e5SLinus Torvalds 	acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
210e77819e5SLinus Torvalds 
211e77819e5SLinus Torvalds 	/*
2124e34e719SChristoph Hellwig 	 * A filesystem can force a ACL callback by just never filling the
2134e34e719SChristoph Hellwig 	 * ACL cache. But normally you'd fill the cache either at inode
2144e34e719SChristoph Hellwig 	 * instantiation time, or on the first ->get_acl call.
215e77819e5SLinus Torvalds 	 *
2164e34e719SChristoph Hellwig 	 * If the filesystem doesn't have a get_acl() function at all, we'll
2174e34e719SChristoph Hellwig 	 * just create the negative cache entry.
218e77819e5SLinus Torvalds 	 */
219e77819e5SLinus Torvalds 	if (acl == ACL_NOT_CACHED) {
2204e34e719SChristoph Hellwig 	        if (inode->i_op->get_acl) {
2214e34e719SChristoph Hellwig 			acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS);
2224e34e719SChristoph Hellwig 			if (IS_ERR(acl))
2234e34e719SChristoph Hellwig 				return PTR_ERR(acl);
2244e34e719SChristoph Hellwig 		} else {
225e77819e5SLinus Torvalds 		        set_cached_acl(inode, ACL_TYPE_ACCESS, NULL);
226e77819e5SLinus Torvalds 		        return -EAGAIN;
227e77819e5SLinus Torvalds 		}
2284e34e719SChristoph Hellwig 	}
229e77819e5SLinus Torvalds 
230e77819e5SLinus Torvalds 	if (acl) {
231e77819e5SLinus Torvalds 	        int error = posix_acl_permission(inode, acl, mask);
232e77819e5SLinus Torvalds 	        posix_acl_release(acl);
233e77819e5SLinus Torvalds 	        return error;
234e77819e5SLinus Torvalds 	}
23584635d68SLinus Torvalds #endif
236e77819e5SLinus Torvalds 
237e77819e5SLinus Torvalds 	return -EAGAIN;
238e77819e5SLinus Torvalds }
239e77819e5SLinus Torvalds 
2405909ccaaSLinus Torvalds /*
241948409c7SAndreas Gruenbacher  * This does the basic permission checking
2425909ccaaSLinus Torvalds  */
2437e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask)
2445909ccaaSLinus Torvalds {
24526cf46beSLinus Torvalds 	unsigned int mode = inode->i_mode;
2465909ccaaSLinus Torvalds 
2478e96e3b7SEric W. Biederman 	if (likely(uid_eq(current_fsuid(), inode->i_uid)))
2485909ccaaSLinus Torvalds 		mode >>= 6;
2495909ccaaSLinus Torvalds 	else {
250e77819e5SLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
2517e40145eSAl Viro 			int error = check_acl(inode, mask);
2525909ccaaSLinus Torvalds 			if (error != -EAGAIN)
2535909ccaaSLinus Torvalds 				return error;
2545909ccaaSLinus Torvalds 		}
2555909ccaaSLinus Torvalds 
2565909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
2575909ccaaSLinus Torvalds 			mode >>= 3;
2585909ccaaSLinus Torvalds 	}
2595909ccaaSLinus Torvalds 
2605909ccaaSLinus Torvalds 	/*
2615909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
2625909ccaaSLinus Torvalds 	 */
2639c2c7039SAl Viro 	if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
2645909ccaaSLinus Torvalds 		return 0;
2655909ccaaSLinus Torvalds 	return -EACCES;
2665909ccaaSLinus Torvalds }
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds /**
2691da177e4SLinus Torvalds  * generic_permission -  check for access rights on a Posix-like filesystem
2701da177e4SLinus Torvalds  * @inode:	inode to check access rights for
2718fd90c8dSAndreas Gruenbacher  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
2721da177e4SLinus Torvalds  *
2731da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
2741da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
2751da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
276b74c79e9SNick Piggin  * are used for other things.
277b74c79e9SNick Piggin  *
278b74c79e9SNick Piggin  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
279b74c79e9SNick Piggin  * request cannot be satisfied (eg. requires blocking or too much complexity).
280b74c79e9SNick Piggin  * It would then be called again in ref-walk mode.
2811da177e4SLinus Torvalds  */
2822830ba7fSAl Viro int generic_permission(struct inode *inode, int mask)
2831da177e4SLinus Torvalds {
2845909ccaaSLinus Torvalds 	int ret;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	/*
287948409c7SAndreas Gruenbacher 	 * Do the basic permission checks.
2881da177e4SLinus Torvalds 	 */
2897e40145eSAl Viro 	ret = acl_permission_check(inode, mask);
2905909ccaaSLinus Torvalds 	if (ret != -EACCES)
2915909ccaaSLinus Torvalds 		return ret;
2921da177e4SLinus Torvalds 
293d594e7ecSAl Viro 	if (S_ISDIR(inode->i_mode)) {
294d594e7ecSAl Viro 		/* DACs are overridable for directories */
2951a48e2acSEric W. Biederman 		if (inode_capable(inode, CAP_DAC_OVERRIDE))
296d594e7ecSAl Viro 			return 0;
297d594e7ecSAl Viro 		if (!(mask & MAY_WRITE))
2981a48e2acSEric W. Biederman 			if (inode_capable(inode, CAP_DAC_READ_SEARCH))
299d594e7ecSAl Viro 				return 0;
300d594e7ecSAl Viro 		return -EACCES;
301d594e7ecSAl Viro 	}
3021da177e4SLinus Torvalds 	/*
3031da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
304d594e7ecSAl Viro 	 * Executable DACs are overridable when there is
305d594e7ecSAl Viro 	 * at least one exec bit set.
3061da177e4SLinus Torvalds 	 */
307d594e7ecSAl Viro 	if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
3081a48e2acSEric W. Biederman 		if (inode_capable(inode, CAP_DAC_OVERRIDE))
3091da177e4SLinus Torvalds 			return 0;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	/*
3121da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
3131da177e4SLinus Torvalds 	 */
3147ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
315d594e7ecSAl Viro 	if (mask == MAY_READ)
3161a48e2acSEric W. Biederman 		if (inode_capable(inode, CAP_DAC_READ_SEARCH))
3171da177e4SLinus Torvalds 			return 0;
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	return -EACCES;
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds 
3223ddcd056SLinus Torvalds /*
3233ddcd056SLinus Torvalds  * We _really_ want to just do "generic_permission()" without
3243ddcd056SLinus Torvalds  * even looking at the inode->i_op values. So we keep a cache
3253ddcd056SLinus Torvalds  * flag in inode->i_opflags, that says "this has not special
3263ddcd056SLinus Torvalds  * permission function, use the fast case".
3273ddcd056SLinus Torvalds  */
3283ddcd056SLinus Torvalds static inline int do_inode_permission(struct inode *inode, int mask)
3293ddcd056SLinus Torvalds {
3303ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
3313ddcd056SLinus Torvalds 		if (likely(inode->i_op->permission))
3323ddcd056SLinus Torvalds 			return inode->i_op->permission(inode, mask);
3333ddcd056SLinus Torvalds 
3343ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
3353ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
3363ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_FASTPERM;
3373ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
3383ddcd056SLinus Torvalds 	}
3393ddcd056SLinus Torvalds 	return generic_permission(inode, mask);
3403ddcd056SLinus Torvalds }
3413ddcd056SLinus Torvalds 
342cb23beb5SChristoph Hellwig /**
3430bdaea90SDavid Howells  * __inode_permission - Check for access rights to a given inode
3440bdaea90SDavid Howells  * @inode: Inode to check permission on
3450bdaea90SDavid Howells  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
346cb23beb5SChristoph Hellwig  *
3470bdaea90SDavid Howells  * Check for read/write/execute permissions on an inode.
348948409c7SAndreas Gruenbacher  *
349948409c7SAndreas Gruenbacher  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
3500bdaea90SDavid Howells  *
3510bdaea90SDavid Howells  * This does not check for a read-only file system.  You probably want
3520bdaea90SDavid Howells  * inode_permission().
353cb23beb5SChristoph Hellwig  */
3540bdaea90SDavid Howells int __inode_permission(struct inode *inode, int mask)
3551da177e4SLinus Torvalds {
356e6305c43SAl Viro 	int retval;
3571da177e4SLinus Torvalds 
3583ddcd056SLinus Torvalds 	if (unlikely(mask & MAY_WRITE)) {
3591da177e4SLinus Torvalds 		/*
3601da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
3611da177e4SLinus Torvalds 		 */
3621da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
3631da177e4SLinus Torvalds 			return -EACCES;
3641da177e4SLinus Torvalds 	}
3651da177e4SLinus Torvalds 
3663ddcd056SLinus Torvalds 	retval = do_inode_permission(inode, mask);
3671da177e4SLinus Torvalds 	if (retval)
3681da177e4SLinus Torvalds 		return retval;
3691da177e4SLinus Torvalds 
37008ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
37108ce5f16SSerge E. Hallyn 	if (retval)
37208ce5f16SSerge E. Hallyn 		return retval;
37308ce5f16SSerge E. Hallyn 
374d09ca739SEric Paris 	return security_inode_permission(inode, mask);
3751da177e4SLinus Torvalds }
3761da177e4SLinus Torvalds 
377f4d6ff89SAl Viro /**
3780bdaea90SDavid Howells  * sb_permission - Check superblock-level permissions
3790bdaea90SDavid Howells  * @sb: Superblock of inode to check permission on
38055852635SRandy Dunlap  * @inode: Inode to check permission on
3810bdaea90SDavid Howells  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
3820bdaea90SDavid Howells  *
3830bdaea90SDavid Howells  * Separate out file-system wide checks from inode-specific permission checks.
3840bdaea90SDavid Howells  */
3850bdaea90SDavid Howells static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
3860bdaea90SDavid Howells {
3870bdaea90SDavid Howells 	if (unlikely(mask & MAY_WRITE)) {
3880bdaea90SDavid Howells 		umode_t mode = inode->i_mode;
3890bdaea90SDavid Howells 
3900bdaea90SDavid Howells 		/* Nobody gets write access to a read-only fs. */
3910bdaea90SDavid Howells 		if ((sb->s_flags & MS_RDONLY) &&
3920bdaea90SDavid Howells 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
3930bdaea90SDavid Howells 			return -EROFS;
3940bdaea90SDavid Howells 	}
3950bdaea90SDavid Howells 	return 0;
3960bdaea90SDavid Howells }
3970bdaea90SDavid Howells 
3980bdaea90SDavid Howells /**
3990bdaea90SDavid Howells  * inode_permission - Check for access rights to a given inode
4000bdaea90SDavid Howells  * @inode: Inode to check permission on
4010bdaea90SDavid Howells  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
4020bdaea90SDavid Howells  *
4030bdaea90SDavid Howells  * Check for read/write/execute permissions on an inode.  We use fs[ug]id for
4040bdaea90SDavid Howells  * this, letting us set arbitrary permissions for filesystem access without
4050bdaea90SDavid Howells  * changing the "normal" UIDs which are used for other things.
4060bdaea90SDavid Howells  *
4070bdaea90SDavid Howells  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
4080bdaea90SDavid Howells  */
4090bdaea90SDavid Howells int inode_permission(struct inode *inode, int mask)
4100bdaea90SDavid Howells {
4110bdaea90SDavid Howells 	int retval;
4120bdaea90SDavid Howells 
4130bdaea90SDavid Howells 	retval = sb_permission(inode->i_sb, inode, mask);
4140bdaea90SDavid Howells 	if (retval)
4150bdaea90SDavid Howells 		return retval;
4160bdaea90SDavid Howells 	return __inode_permission(inode, mask);
4170bdaea90SDavid Howells }
4180bdaea90SDavid Howells 
4190bdaea90SDavid Howells /**
4205dd784d0SJan Blunck  * path_get - get a reference to a path
4215dd784d0SJan Blunck  * @path: path to get the reference to
4225dd784d0SJan Blunck  *
4235dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
4245dd784d0SJan Blunck  */
4255dd784d0SJan Blunck void path_get(struct path *path)
4265dd784d0SJan Blunck {
4275dd784d0SJan Blunck 	mntget(path->mnt);
4285dd784d0SJan Blunck 	dget(path->dentry);
4295dd784d0SJan Blunck }
4305dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
4315dd784d0SJan Blunck 
4325dd784d0SJan Blunck /**
4331d957f9bSJan Blunck  * path_put - put a reference to a path
4341d957f9bSJan Blunck  * @path: path to put the reference to
4351d957f9bSJan Blunck  *
4361d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
4371d957f9bSJan Blunck  */
4381d957f9bSJan Blunck void path_put(struct path *path)
4391da177e4SLinus Torvalds {
4401d957f9bSJan Blunck 	dput(path->dentry);
4411d957f9bSJan Blunck 	mntput(path->mnt);
4421da177e4SLinus Torvalds }
4431d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
4441da177e4SLinus Torvalds 
44519660af7SAl Viro /*
44631e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
44719660af7SAl Viro  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
44819660af7SAl Viro  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
44919660af7SAl Viro  * normal reference counts on dentries and vfsmounts to transition to rcu-walk
45019660af7SAl Viro  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
45119660af7SAl Viro  * got stuck, so ref-walk may continue from there. If this is not successful
45219660af7SAl Viro  * (eg. a seqcount has changed), then failure is returned and it's up to caller
45319660af7SAl Viro  * to restart the path walk from the beginning in ref-walk mode.
45431e6b01fSNick Piggin  */
45531e6b01fSNick Piggin 
45632a7991bSAl Viro static inline void lock_rcu_walk(void)
45732a7991bSAl Viro {
45832a7991bSAl Viro 	br_read_lock(&vfsmount_lock);
45932a7991bSAl Viro 	rcu_read_lock();
46032a7991bSAl Viro }
46132a7991bSAl Viro 
46232a7991bSAl Viro static inline void unlock_rcu_walk(void)
46332a7991bSAl Viro {
46432a7991bSAl Viro 	rcu_read_unlock();
46532a7991bSAl Viro 	br_read_unlock(&vfsmount_lock);
46632a7991bSAl Viro }
46732a7991bSAl Viro 
46831e6b01fSNick Piggin /**
46919660af7SAl Viro  * unlazy_walk - try to switch to ref-walk mode.
47019660af7SAl Viro  * @nd: nameidata pathwalk data
47119660af7SAl Viro  * @dentry: child of nd->path.dentry or NULL
47239191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
47331e6b01fSNick Piggin  *
47419660af7SAl Viro  * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
47519660af7SAl Viro  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
47619660af7SAl Viro  * @nd or NULL.  Must be called from rcu-walk context.
47731e6b01fSNick Piggin  */
47819660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
47931e6b01fSNick Piggin {
48031e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
48131e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
4825b6ca027SAl Viro 	int want_root = 0;
48331e6b01fSNick Piggin 
48431e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
4855b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
4865b6ca027SAl Viro 		want_root = 1;
48731e6b01fSNick Piggin 		spin_lock(&fs->lock);
48831e6b01fSNick Piggin 		if (nd->root.mnt != fs->root.mnt ||
48931e6b01fSNick Piggin 				nd->root.dentry != fs->root.dentry)
49031e6b01fSNick Piggin 			goto err_root;
49131e6b01fSNick Piggin 	}
49231e6b01fSNick Piggin 	spin_lock(&parent->d_lock);
49319660af7SAl Viro 	if (!dentry) {
49419660af7SAl Viro 		if (!__d_rcu_to_refcount(parent, nd->seq))
49519660af7SAl Viro 			goto err_parent;
49619660af7SAl Viro 		BUG_ON(nd->inode != parent->d_inode);
49719660af7SAl Viro 	} else {
49894c0d4ecSAl Viro 		if (dentry->d_parent != parent)
49994c0d4ecSAl Viro 			goto err_parent;
50031e6b01fSNick Piggin 		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
50131e6b01fSNick Piggin 		if (!__d_rcu_to_refcount(dentry, nd->seq))
50219660af7SAl Viro 			goto err_child;
50331e6b01fSNick Piggin 		/*
50419660af7SAl Viro 		 * If the sequence check on the child dentry passed, then
50519660af7SAl Viro 		 * the child has not been removed from its parent. This
50619660af7SAl Viro 		 * means the parent dentry must be valid and able to take
50719660af7SAl Viro 		 * a reference at this point.
50831e6b01fSNick Piggin 		 */
50931e6b01fSNick Piggin 		BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
51031e6b01fSNick Piggin 		BUG_ON(!parent->d_count);
51131e6b01fSNick Piggin 		parent->d_count++;
51231e6b01fSNick Piggin 		spin_unlock(&dentry->d_lock);
51319660af7SAl Viro 	}
51431e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
5155b6ca027SAl Viro 	if (want_root) {
51631e6b01fSNick Piggin 		path_get(&nd->root);
51731e6b01fSNick Piggin 		spin_unlock(&fs->lock);
51831e6b01fSNick Piggin 	}
51931e6b01fSNick Piggin 	mntget(nd->path.mnt);
52031e6b01fSNick Piggin 
52132a7991bSAl Viro 	unlock_rcu_walk();
52231e6b01fSNick Piggin 	nd->flags &= ~LOOKUP_RCU;
52331e6b01fSNick Piggin 	return 0;
52419660af7SAl Viro 
52519660af7SAl Viro err_child:
52631e6b01fSNick Piggin 	spin_unlock(&dentry->d_lock);
52719660af7SAl Viro err_parent:
52831e6b01fSNick Piggin 	spin_unlock(&parent->d_lock);
52931e6b01fSNick Piggin err_root:
5305b6ca027SAl Viro 	if (want_root)
53131e6b01fSNick Piggin 		spin_unlock(&fs->lock);
53231e6b01fSNick Piggin 	return -ECHILD;
53331e6b01fSNick Piggin }
53431e6b01fSNick Piggin 
5354ce16ef3SAl Viro static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
53634286d66SNick Piggin {
5374ce16ef3SAl Viro 	return dentry->d_op->d_revalidate(dentry, flags);
53834286d66SNick Piggin }
53934286d66SNick Piggin 
5409f1fafeeSAl Viro /**
5419f1fafeeSAl Viro  * complete_walk - successful completion of path walk
5429f1fafeeSAl Viro  * @nd:  pointer nameidata
54339159de2SJeff Layton  *
5449f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
5459f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
5469f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
5479f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
5489f1fafeeSAl Viro  * need to drop nd->path.
54939159de2SJeff Layton  */
5509f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
55139159de2SJeff Layton {
55216c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
55339159de2SJeff Layton 	int status;
55439159de2SJeff Layton 
5559f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
5569f1fafeeSAl Viro 		nd->flags &= ~LOOKUP_RCU;
5579f1fafeeSAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
5589f1fafeeSAl Viro 			nd->root.mnt = NULL;
5599f1fafeeSAl Viro 		spin_lock(&dentry->d_lock);
5609f1fafeeSAl Viro 		if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) {
5619f1fafeeSAl Viro 			spin_unlock(&dentry->d_lock);
56232a7991bSAl Viro 			unlock_rcu_walk();
5639f1fafeeSAl Viro 			return -ECHILD;
5649f1fafeeSAl Viro 		}
5659f1fafeeSAl Viro 		BUG_ON(nd->inode != dentry->d_inode);
5669f1fafeeSAl Viro 		spin_unlock(&dentry->d_lock);
5679f1fafeeSAl Viro 		mntget(nd->path.mnt);
56832a7991bSAl Viro 		unlock_rcu_walk();
5699f1fafeeSAl Viro 	}
5709f1fafeeSAl Viro 
57116c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
57239159de2SJeff Layton 		return 0;
57339159de2SJeff Layton 
57416c2cd71SAl Viro 	if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
57516c2cd71SAl Viro 		return 0;
57616c2cd71SAl Viro 
57716c2cd71SAl Viro 	if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
57816c2cd71SAl Viro 		return 0;
57916c2cd71SAl Viro 
58016c2cd71SAl Viro 	/* Note: we do not d_invalidate() */
5814ce16ef3SAl Viro 	status = d_revalidate(dentry, nd->flags);
58239159de2SJeff Layton 	if (status > 0)
58339159de2SJeff Layton 		return 0;
58439159de2SJeff Layton 
58516c2cd71SAl Viro 	if (!status)
58639159de2SJeff Layton 		status = -ESTALE;
58716c2cd71SAl Viro 
5889f1fafeeSAl Viro 	path_put(&nd->path);
58939159de2SJeff Layton 	return status;
59039159de2SJeff Layton }
59139159de2SJeff Layton 
5922a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
5932a737871SAl Viro {
594f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
595f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
5962a737871SAl Viro }
5972a737871SAl Viro 
5986de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
5996de88d72SAl Viro 
60031e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
60131e6b01fSNick Piggin {
60231e6b01fSNick Piggin 	if (!nd->root.mnt) {
60331e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
604c28cc364SNick Piggin 		unsigned seq;
605c28cc364SNick Piggin 
606c28cc364SNick Piggin 		do {
607c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
60831e6b01fSNick Piggin 			nd->root = fs->root;
609c1530019STim Chen 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
610c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
61131e6b01fSNick Piggin 	}
61231e6b01fSNick Piggin }
61331e6b01fSNick Piggin 
614f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
6151da177e4SLinus Torvalds {
61631e6b01fSNick Piggin 	int ret;
61731e6b01fSNick Piggin 
6181da177e4SLinus Torvalds 	if (IS_ERR(link))
6191da177e4SLinus Torvalds 		goto fail;
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	if (*link == '/') {
6222a737871SAl Viro 		set_root(nd);
6231d957f9bSJan Blunck 		path_put(&nd->path);
6242a737871SAl Viro 		nd->path = nd->root;
6252a737871SAl Viro 		path_get(&nd->root);
62616c2cd71SAl Viro 		nd->flags |= LOOKUP_JUMPED;
6271da177e4SLinus Torvalds 	}
62831e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
629b4091d5fSChristoph Hellwig 
63031e6b01fSNick Piggin 	ret = link_path_walk(link, nd);
63131e6b01fSNick Piggin 	return ret;
6321da177e4SLinus Torvalds fail:
6331d957f9bSJan Blunck 	path_put(&nd->path);
6341da177e4SLinus Torvalds 	return PTR_ERR(link);
6351da177e4SLinus Torvalds }
6361da177e4SLinus Torvalds 
6371d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
638051d3812SIan Kent {
639051d3812SIan Kent 	dput(path->dentry);
6404ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
641051d3812SIan Kent 		mntput(path->mnt);
642051d3812SIan Kent }
643051d3812SIan Kent 
6447b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
6457b9337aaSNick Piggin 					struct nameidata *nd)
646051d3812SIan Kent {
64731e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
6484ac91378SJan Blunck 		dput(nd->path.dentry);
64931e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
6504ac91378SJan Blunck 			mntput(nd->path.mnt);
6519a229683SHuang Shijie 	}
65231e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
6534ac91378SJan Blunck 	nd->path.dentry = path->dentry;
654051d3812SIan Kent }
655051d3812SIan Kent 
656b5fb63c1SChristoph Hellwig /*
657b5fb63c1SChristoph Hellwig  * Helper to directly jump to a known parsed path from ->follow_link,
658b5fb63c1SChristoph Hellwig  * caller must have taken a reference to path beforehand.
659b5fb63c1SChristoph Hellwig  */
660b5fb63c1SChristoph Hellwig void nd_jump_link(struct nameidata *nd, struct path *path)
661b5fb63c1SChristoph Hellwig {
662b5fb63c1SChristoph Hellwig 	path_put(&nd->path);
663b5fb63c1SChristoph Hellwig 
664b5fb63c1SChristoph Hellwig 	nd->path = *path;
665b5fb63c1SChristoph Hellwig 	nd->inode = nd->path.dentry->d_inode;
666b5fb63c1SChristoph Hellwig 	nd->flags |= LOOKUP_JUMPED;
667b5fb63c1SChristoph Hellwig 
668b5fb63c1SChristoph Hellwig 	BUG_ON(nd->inode->i_op->follow_link);
669b5fb63c1SChristoph Hellwig }
670b5fb63c1SChristoph Hellwig 
671574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
672574197e0SAl Viro {
673574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
6746d7b5aaeSAl Viro 	if (inode->i_op->put_link)
675574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
676574197e0SAl Viro 	path_put(link);
677574197e0SAl Viro }
678574197e0SAl Viro 
679800179c9SKees Cook int sysctl_protected_symlinks __read_mostly = 1;
680800179c9SKees Cook int sysctl_protected_hardlinks __read_mostly = 1;
681800179c9SKees Cook 
682800179c9SKees Cook /**
683800179c9SKees Cook  * may_follow_link - Check symlink following for unsafe situations
684800179c9SKees Cook  * @link: The path of the symlink
68555852635SRandy Dunlap  * @nd: nameidata pathwalk data
686800179c9SKees Cook  *
687800179c9SKees Cook  * In the case of the sysctl_protected_symlinks sysctl being enabled,
688800179c9SKees Cook  * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
689800179c9SKees Cook  * in a sticky world-writable directory. This is to protect privileged
690800179c9SKees Cook  * processes from failing races against path names that may change out
691800179c9SKees Cook  * from under them by way of other users creating malicious symlinks.
692800179c9SKees Cook  * It will permit symlinks to be followed only when outside a sticky
693800179c9SKees Cook  * world-writable directory, or when the uid of the symlink and follower
694800179c9SKees Cook  * match, or when the directory owner matches the symlink's owner.
695800179c9SKees Cook  *
696800179c9SKees Cook  * Returns 0 if following the symlink is allowed, -ve on error.
697800179c9SKees Cook  */
698800179c9SKees Cook static inline int may_follow_link(struct path *link, struct nameidata *nd)
699800179c9SKees Cook {
700800179c9SKees Cook 	const struct inode *inode;
701800179c9SKees Cook 	const struct inode *parent;
702800179c9SKees Cook 
703800179c9SKees Cook 	if (!sysctl_protected_symlinks)
704800179c9SKees Cook 		return 0;
705800179c9SKees Cook 
706800179c9SKees Cook 	/* Allowed if owner and follower match. */
707800179c9SKees Cook 	inode = link->dentry->d_inode;
70881abe27bSEric W. Biederman 	if (uid_eq(current_cred()->fsuid, inode->i_uid))
709800179c9SKees Cook 		return 0;
710800179c9SKees Cook 
711800179c9SKees Cook 	/* Allowed if parent directory not sticky and world-writable. */
712800179c9SKees Cook 	parent = nd->path.dentry->d_inode;
713800179c9SKees Cook 	if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
714800179c9SKees Cook 		return 0;
715800179c9SKees Cook 
716800179c9SKees Cook 	/* Allowed if parent directory and link owner match. */
71781abe27bSEric W. Biederman 	if (uid_eq(parent->i_uid, inode->i_uid))
718800179c9SKees Cook 		return 0;
719800179c9SKees Cook 
720ffd8d101SSasha Levin 	audit_log_link_denied("follow_link", link);
721800179c9SKees Cook 	path_put_conditional(link, nd);
722800179c9SKees Cook 	path_put(&nd->path);
723800179c9SKees Cook 	return -EACCES;
724800179c9SKees Cook }
725800179c9SKees Cook 
726800179c9SKees Cook /**
727800179c9SKees Cook  * safe_hardlink_source - Check for safe hardlink conditions
728800179c9SKees Cook  * @inode: the source inode to hardlink from
729800179c9SKees Cook  *
730800179c9SKees Cook  * Return false if at least one of the following conditions:
731800179c9SKees Cook  *    - inode is not a regular file
732800179c9SKees Cook  *    - inode is setuid
733800179c9SKees Cook  *    - inode is setgid and group-exec
734800179c9SKees Cook  *    - access failure for read and write
735800179c9SKees Cook  *
736800179c9SKees Cook  * Otherwise returns true.
737800179c9SKees Cook  */
738800179c9SKees Cook static bool safe_hardlink_source(struct inode *inode)
739800179c9SKees Cook {
740800179c9SKees Cook 	umode_t mode = inode->i_mode;
741800179c9SKees Cook 
742800179c9SKees Cook 	/* Special files should not get pinned to the filesystem. */
743800179c9SKees Cook 	if (!S_ISREG(mode))
744800179c9SKees Cook 		return false;
745800179c9SKees Cook 
746800179c9SKees Cook 	/* Setuid files should not get pinned to the filesystem. */
747800179c9SKees Cook 	if (mode & S_ISUID)
748800179c9SKees Cook 		return false;
749800179c9SKees Cook 
750800179c9SKees Cook 	/* Executable setgid files should not get pinned to the filesystem. */
751800179c9SKees Cook 	if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
752800179c9SKees Cook 		return false;
753800179c9SKees Cook 
754800179c9SKees Cook 	/* Hardlinking to unreadable or unwritable sources is dangerous. */
755800179c9SKees Cook 	if (inode_permission(inode, MAY_READ | MAY_WRITE))
756800179c9SKees Cook 		return false;
757800179c9SKees Cook 
758800179c9SKees Cook 	return true;
759800179c9SKees Cook }
760800179c9SKees Cook 
761800179c9SKees Cook /**
762800179c9SKees Cook  * may_linkat - Check permissions for creating a hardlink
763800179c9SKees Cook  * @link: the source to hardlink from
764800179c9SKees Cook  *
765800179c9SKees Cook  * Block hardlink when all of:
766800179c9SKees Cook  *  - sysctl_protected_hardlinks enabled
767800179c9SKees Cook  *  - fsuid does not match inode
768800179c9SKees Cook  *  - hardlink source is unsafe (see safe_hardlink_source() above)
769800179c9SKees Cook  *  - not CAP_FOWNER
770800179c9SKees Cook  *
771800179c9SKees Cook  * Returns 0 if successful, -ve on error.
772800179c9SKees Cook  */
773800179c9SKees Cook static int may_linkat(struct path *link)
774800179c9SKees Cook {
775800179c9SKees Cook 	const struct cred *cred;
776800179c9SKees Cook 	struct inode *inode;
777800179c9SKees Cook 
778800179c9SKees Cook 	if (!sysctl_protected_hardlinks)
779800179c9SKees Cook 		return 0;
780800179c9SKees Cook 
781800179c9SKees Cook 	cred = current_cred();
782800179c9SKees Cook 	inode = link->dentry->d_inode;
783800179c9SKees Cook 
784800179c9SKees Cook 	/* Source inode owner (or CAP_FOWNER) can hardlink all they like,
785800179c9SKees Cook 	 * otherwise, it must be a safe source.
786800179c9SKees Cook 	 */
78781abe27bSEric W. Biederman 	if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
788800179c9SKees Cook 	    capable(CAP_FOWNER))
789800179c9SKees Cook 		return 0;
790800179c9SKees Cook 
791a51d9eaaSKees Cook 	audit_log_link_denied("linkat", link);
792800179c9SKees Cook 	return -EPERM;
793800179c9SKees Cook }
794800179c9SKees Cook 
795def4af30SAl Viro static __always_inline int
796574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
7971da177e4SLinus Torvalds {
7987b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
7996d7b5aaeSAl Viro 	int error;
8006d7b5aaeSAl Viro 	char *s;
8011da177e4SLinus Torvalds 
802844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
803844a3917SAl Viro 
8040e794589SAl Viro 	if (link->mnt == nd->path.mnt)
8050e794589SAl Viro 		mntget(link->mnt);
8060e794589SAl Viro 
8076d7b5aaeSAl Viro 	error = -ELOOP;
8086d7b5aaeSAl Viro 	if (unlikely(current->total_link_count >= 40))
8096d7b5aaeSAl Viro 		goto out_put_nd_path;
8106d7b5aaeSAl Viro 
811574197e0SAl Viro 	cond_resched();
812574197e0SAl Viro 	current->total_link_count++;
813574197e0SAl Viro 
81468ac1234SAl Viro 	touch_atime(link);
8151da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
816cd4e91d3SAl Viro 
81736f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
8186d7b5aaeSAl Viro 	if (error)
8196d7b5aaeSAl Viro 		goto out_put_nd_path;
82036f3b4f6SAl Viro 
82186acdca1SAl Viro 	nd->last_type = LAST_BIND;
822def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
823def4af30SAl Viro 	error = PTR_ERR(*p);
8246d7b5aaeSAl Viro 	if (IS_ERR(*p))
825408ef013SChristoph Hellwig 		goto out_put_nd_path;
8266d7b5aaeSAl Viro 
827cc314eefSLinus Torvalds 	error = 0;
8286d7b5aaeSAl Viro 	s = nd_get_link(nd);
8296d7b5aaeSAl Viro 	if (s) {
8301da177e4SLinus Torvalds 		error = __vfs_follow_link(nd, s);
8316d7b5aaeSAl Viro 		if (unlikely(error))
8326d7b5aaeSAl Viro 			put_link(nd, link, *p);
833b5fb63c1SChristoph Hellwig 	}
8346d7b5aaeSAl Viro 
8356d7b5aaeSAl Viro 	return error;
8366d7b5aaeSAl Viro 
8376d7b5aaeSAl Viro out_put_nd_path:
83898f6ef64SArnd Bergmann 	*p = NULL;
8396d7b5aaeSAl Viro 	path_put(&nd->path);
8406d7b5aaeSAl Viro 	path_put(link);
8411da177e4SLinus Torvalds 	return error;
8421da177e4SLinus Torvalds }
8431da177e4SLinus Torvalds 
84431e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
84531e6b01fSNick Piggin {
8460714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
8470714a533SAl Viro 	struct mount *parent;
84831e6b01fSNick Piggin 	struct dentry *mountpoint;
84931e6b01fSNick Piggin 
8500714a533SAl Viro 	parent = mnt->mnt_parent;
8510714a533SAl Viro 	if (&parent->mnt == path->mnt)
85231e6b01fSNick Piggin 		return 0;
853a73324daSAl Viro 	mountpoint = mnt->mnt_mountpoint;
85431e6b01fSNick Piggin 	path->dentry = mountpoint;
8550714a533SAl Viro 	path->mnt = &parent->mnt;
85631e6b01fSNick Piggin 	return 1;
85731e6b01fSNick Piggin }
85831e6b01fSNick Piggin 
859f015f126SDavid Howells /*
860f015f126SDavid Howells  * follow_up - Find the mountpoint of path's vfsmount
861f015f126SDavid Howells  *
862f015f126SDavid Howells  * Given a path, find the mountpoint of its source file system.
863f015f126SDavid Howells  * Replace @path with the path of the mountpoint in the parent mount.
864f015f126SDavid Howells  * Up is towards /.
865f015f126SDavid Howells  *
866f015f126SDavid Howells  * Return 1 if we went up a level and 0 if we were already at the
867f015f126SDavid Howells  * root.
868f015f126SDavid Howells  */
869bab77ebfSAl Viro int follow_up(struct path *path)
8701da177e4SLinus Torvalds {
8710714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
8720714a533SAl Viro 	struct mount *parent;
8731da177e4SLinus Torvalds 	struct dentry *mountpoint;
87499b7db7bSNick Piggin 
875962830dfSAndi Kleen 	br_read_lock(&vfsmount_lock);
8760714a533SAl Viro 	parent = mnt->mnt_parent;
8773c0a6163SAl Viro 	if (parent == mnt) {
878962830dfSAndi Kleen 		br_read_unlock(&vfsmount_lock);
8791da177e4SLinus Torvalds 		return 0;
8801da177e4SLinus Torvalds 	}
8810714a533SAl Viro 	mntget(&parent->mnt);
882a73324daSAl Viro 	mountpoint = dget(mnt->mnt_mountpoint);
883962830dfSAndi Kleen 	br_read_unlock(&vfsmount_lock);
884bab77ebfSAl Viro 	dput(path->dentry);
885bab77ebfSAl Viro 	path->dentry = mountpoint;
886bab77ebfSAl Viro 	mntput(path->mnt);
8870714a533SAl Viro 	path->mnt = &parent->mnt;
8881da177e4SLinus Torvalds 	return 1;
8891da177e4SLinus Torvalds }
8901da177e4SLinus Torvalds 
891b5c84bf6SNick Piggin /*
8929875cf80SDavid Howells  * Perform an automount
8939875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
8949875cf80SDavid Howells  *   were called with.
8951da177e4SLinus Torvalds  */
8969875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
8979875cf80SDavid Howells 			    bool *need_mntput)
89831e6b01fSNick Piggin {
8999875cf80SDavid Howells 	struct vfsmount *mnt;
900ea5b778aSDavid Howells 	int err;
9019875cf80SDavid Howells 
9029875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
9039875cf80SDavid Howells 		return -EREMOTE;
9049875cf80SDavid Howells 
9050ec26fd0SMiklos Szeredi 	/* We don't want to mount if someone's just doing a stat -
9060ec26fd0SMiklos Szeredi 	 * unless they're stat'ing a directory and appended a '/' to
9070ec26fd0SMiklos Szeredi 	 * the name.
9080ec26fd0SMiklos Szeredi 	 *
9090ec26fd0SMiklos Szeredi 	 * We do, however, want to mount if someone wants to open or
9105a30d8a2SDavid Howells 	 * create a file of any type under the mountpoint, wants to
9110ec26fd0SMiklos Szeredi 	 * traverse through the mountpoint or wants to open the
9120ec26fd0SMiklos Szeredi 	 * mounted directory.  Also, autofs may mark negative dentries
9130ec26fd0SMiklos Szeredi 	 * as being automount points.  These will need the attentions
9140ec26fd0SMiklos Szeredi 	 * of the daemon to instantiate them before they can be used.
9155a30d8a2SDavid Howells 	 */
9165a30d8a2SDavid Howells 	if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
917d94c177bSLinus Torvalds 		     LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
9185a30d8a2SDavid Howells 	    path->dentry->d_inode)
9199875cf80SDavid Howells 		return -EISDIR;
9200ec26fd0SMiklos Szeredi 
9219875cf80SDavid Howells 	current->total_link_count++;
9229875cf80SDavid Howells 	if (current->total_link_count >= 40)
9239875cf80SDavid Howells 		return -ELOOP;
9249875cf80SDavid Howells 
9259875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
9269875cf80SDavid Howells 	if (IS_ERR(mnt)) {
9279875cf80SDavid Howells 		/*
9289875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
9299875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
9309875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
9319875cf80SDavid Howells 		 *
9329875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
9339875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
9349875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
9359875cf80SDavid Howells 		 */
93649084c3bSAl Viro 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
9379875cf80SDavid Howells 			return -EREMOTE;
9389875cf80SDavid Howells 		return PTR_ERR(mnt);
93931e6b01fSNick Piggin 	}
940ea5b778aSDavid Howells 
9419875cf80SDavid Howells 	if (!mnt) /* mount collision */
9429875cf80SDavid Howells 		return 0;
9439875cf80SDavid Howells 
9448aef1884SAl Viro 	if (!*need_mntput) {
9458aef1884SAl Viro 		/* lock_mount() may release path->mnt on error */
9468aef1884SAl Viro 		mntget(path->mnt);
9478aef1884SAl Viro 		*need_mntput = true;
9488aef1884SAl Viro 	}
94919a167afSAl Viro 	err = finish_automount(mnt, path);
950ea5b778aSDavid Howells 
951ea5b778aSDavid Howells 	switch (err) {
952ea5b778aSDavid Howells 	case -EBUSY:
953ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
95419a167afSAl Viro 		return 0;
955ea5b778aSDavid Howells 	case 0:
9568aef1884SAl Viro 		path_put(path);
9579875cf80SDavid Howells 		path->mnt = mnt;
9589875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
9599875cf80SDavid Howells 		return 0;
96019a167afSAl Viro 	default:
96119a167afSAl Viro 		return err;
9629875cf80SDavid Howells 	}
96319a167afSAl Viro 
964ea5b778aSDavid Howells }
9659875cf80SDavid Howells 
9669875cf80SDavid Howells /*
9679875cf80SDavid Howells  * Handle a dentry that is managed in some way.
968cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
9699875cf80SDavid Howells  * - Flagged as mountpoint
9709875cf80SDavid Howells  * - Flagged as automount point
9719875cf80SDavid Howells  *
9729875cf80SDavid Howells  * This may only be called in refwalk mode.
9739875cf80SDavid Howells  *
9749875cf80SDavid Howells  * Serialization is taken care of in namespace.c
9759875cf80SDavid Howells  */
9769875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
9779875cf80SDavid Howells {
9788aef1884SAl Viro 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
9799875cf80SDavid Howells 	unsigned managed;
9809875cf80SDavid Howells 	bool need_mntput = false;
9818aef1884SAl Viro 	int ret = 0;
9829875cf80SDavid Howells 
9839875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
9849875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
9859875cf80SDavid Howells 	 * the components of that value change under us */
9869875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
9879875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
9889875cf80SDavid Howells 	       unlikely(managed != 0)) {
989cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
990cc53ce53SDavid Howells 		 * being held. */
991cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
992cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
993cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
9941aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
995cc53ce53SDavid Howells 			if (ret < 0)
9968aef1884SAl Viro 				break;
997cc53ce53SDavid Howells 		}
998cc53ce53SDavid Howells 
9999875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
10009875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
10019875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
10029875cf80SDavid Howells 			if (mounted) {
10039875cf80SDavid Howells 				dput(path->dentry);
10049875cf80SDavid Howells 				if (need_mntput)
1005463ffb2eSAl Viro 					mntput(path->mnt);
1006463ffb2eSAl Viro 				path->mnt = mounted;
1007463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
10089875cf80SDavid Howells 				need_mntput = true;
10099875cf80SDavid Howells 				continue;
1010463ffb2eSAl Viro 			}
1011463ffb2eSAl Viro 
10129875cf80SDavid Howells 			/* Something is mounted on this dentry in another
10139875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
10149875cf80SDavid Howells 			 * namespace got unmounted before we managed to get the
10159875cf80SDavid Howells 			 * vfsmount_lock */
10161da177e4SLinus Torvalds 		}
10179875cf80SDavid Howells 
10189875cf80SDavid Howells 		/* Handle an automount point */
10199875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
10209875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
10219875cf80SDavid Howells 			if (ret < 0)
10228aef1884SAl Viro 				break;
10239875cf80SDavid Howells 			continue;
10249875cf80SDavid Howells 		}
10259875cf80SDavid Howells 
10269875cf80SDavid Howells 		/* We didn't change the current path point */
10279875cf80SDavid Howells 		break;
10289875cf80SDavid Howells 	}
10298aef1884SAl Viro 
10308aef1884SAl Viro 	if (need_mntput && path->mnt == mnt)
10318aef1884SAl Viro 		mntput(path->mnt);
10328aef1884SAl Viro 	if (ret == -EISDIR)
10338aef1884SAl Viro 		ret = 0;
1034a3fbbde7SAl Viro 	return ret < 0 ? ret : need_mntput;
10351da177e4SLinus Torvalds }
10361da177e4SLinus Torvalds 
1037cc53ce53SDavid Howells int follow_down_one(struct path *path)
10381da177e4SLinus Torvalds {
10391da177e4SLinus Torvalds 	struct vfsmount *mounted;
10401da177e4SLinus Torvalds 
10411c755af4SAl Viro 	mounted = lookup_mnt(path);
10421da177e4SLinus Torvalds 	if (mounted) {
10439393bd07SAl Viro 		dput(path->dentry);
10449393bd07SAl Viro 		mntput(path->mnt);
10459393bd07SAl Viro 		path->mnt = mounted;
10469393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
10471da177e4SLinus Torvalds 		return 1;
10481da177e4SLinus Torvalds 	}
10491da177e4SLinus Torvalds 	return 0;
10501da177e4SLinus Torvalds }
10511da177e4SLinus Torvalds 
105262a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
105362a7375eSIan Kent {
105462a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
105562a7375eSIan Kent 		dentry->d_op->d_manage(dentry, true) < 0);
105662a7375eSIan Kent }
105762a7375eSIan Kent 
10589875cf80SDavid Howells /*
1059287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
1060287548e4SAl Viro  * we meet a managed dentry that would need blocking.
10619875cf80SDavid Howells  */
10629875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1063287548e4SAl Viro 			       struct inode **inode)
10649875cf80SDavid Howells {
106562a7375eSIan Kent 	for (;;) {
1066c7105365SAl Viro 		struct mount *mounted;
106762a7375eSIan Kent 		/*
106862a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
106962a7375eSIan Kent 		 * that wants to block transit.
107062a7375eSIan Kent 		 */
1071287548e4SAl Viro 		if (unlikely(managed_dentry_might_block(path->dentry)))
1072ab90911fSDavid Howells 			return false;
107362a7375eSIan Kent 
107462a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
107562a7375eSIan Kent 			break;
107662a7375eSIan Kent 
10779875cf80SDavid Howells 		mounted = __lookup_mnt(path->mnt, path->dentry, 1);
10789875cf80SDavid Howells 		if (!mounted)
10799875cf80SDavid Howells 			break;
1080c7105365SAl Viro 		path->mnt = &mounted->mnt;
1081c7105365SAl Viro 		path->dentry = mounted->mnt.mnt_root;
1082a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
10839875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
108459430262SLinus Torvalds 		/*
108559430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
108659430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
108759430262SLinus Torvalds 		 * because a mount-point is always pinned.
108859430262SLinus Torvalds 		 */
108959430262SLinus Torvalds 		*inode = path->dentry->d_inode;
10909875cf80SDavid Howells 	}
10919875cf80SDavid Howells 	return true;
10929875cf80SDavid Howells }
10939875cf80SDavid Howells 
1094dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd)
1095287548e4SAl Viro {
1096dea39376SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
1097c7105365SAl Viro 		struct mount *mounted;
1098dea39376SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1);
1099287548e4SAl Viro 		if (!mounted)
1100287548e4SAl Viro 			break;
1101c7105365SAl Viro 		nd->path.mnt = &mounted->mnt;
1102c7105365SAl Viro 		nd->path.dentry = mounted->mnt.mnt_root;
1103dea39376SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1104287548e4SAl Viro 	}
1105287548e4SAl Viro }
1106287548e4SAl Viro 
110731e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
110831e6b01fSNick Piggin {
110931e6b01fSNick Piggin 	set_root_rcu(nd);
111031e6b01fSNick Piggin 
111131e6b01fSNick Piggin 	while (1) {
111231e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
111331e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
111431e6b01fSNick Piggin 			break;
111531e6b01fSNick Piggin 		}
111631e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
111731e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
111831e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
111931e6b01fSNick Piggin 			unsigned seq;
112031e6b01fSNick Piggin 
112131e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
112231e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
1123ef7562d5SAl Viro 				goto failed;
112431e6b01fSNick Piggin 			nd->path.dentry = parent;
112531e6b01fSNick Piggin 			nd->seq = seq;
112631e6b01fSNick Piggin 			break;
112731e6b01fSNick Piggin 		}
112831e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
112931e6b01fSNick Piggin 			break;
113031e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
113131e6b01fSNick Piggin 	}
1132dea39376SAl Viro 	follow_mount_rcu(nd);
1133dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
113431e6b01fSNick Piggin 	return 0;
1135ef7562d5SAl Viro 
1136ef7562d5SAl Viro failed:
1137ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
11385b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
1139ef7562d5SAl Viro 		nd->root.mnt = NULL;
114032a7991bSAl Viro 	unlock_rcu_walk();
1141ef7562d5SAl Viro 	return -ECHILD;
114231e6b01fSNick Piggin }
114331e6b01fSNick Piggin 
11449875cf80SDavid Howells /*
1145cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
1146cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
1147cc53ce53SDavid Howells  * caller is permitted to proceed or not.
1148cc53ce53SDavid Howells  */
11497cc90cc3SAl Viro int follow_down(struct path *path)
1150cc53ce53SDavid Howells {
1151cc53ce53SDavid Howells 	unsigned managed;
1152cc53ce53SDavid Howells 	int ret;
1153cc53ce53SDavid Howells 
1154cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
1155cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1156cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1157cc53ce53SDavid Howells 		 * being held.
1158cc53ce53SDavid Howells 		 *
1159cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
1160cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
1161cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
1162cc53ce53SDavid Howells 		 * superstructure.
1163cc53ce53SDavid Howells 		 *
1164cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
1165cc53ce53SDavid Howells 		 */
1166cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
1167cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1168cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
1169ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
11701aed3e42SAl Viro 				path->dentry, false);
1171cc53ce53SDavid Howells 			if (ret < 0)
1172cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
1173cc53ce53SDavid Howells 		}
1174cc53ce53SDavid Howells 
1175cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
1176cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
1177cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
1178cc53ce53SDavid Howells 			if (!mounted)
1179cc53ce53SDavid Howells 				break;
1180cc53ce53SDavid Howells 			dput(path->dentry);
1181cc53ce53SDavid Howells 			mntput(path->mnt);
1182cc53ce53SDavid Howells 			path->mnt = mounted;
1183cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1184cc53ce53SDavid Howells 			continue;
1185cc53ce53SDavid Howells 		}
1186cc53ce53SDavid Howells 
1187cc53ce53SDavid Howells 		/* Don't handle automount points here */
1188cc53ce53SDavid Howells 		break;
1189cc53ce53SDavid Howells 	}
1190cc53ce53SDavid Howells 	return 0;
1191cc53ce53SDavid Howells }
1192cc53ce53SDavid Howells 
1193cc53ce53SDavid Howells /*
11949875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
11959875cf80SDavid Howells  */
11969875cf80SDavid Howells static void follow_mount(struct path *path)
11979875cf80SDavid Howells {
11989875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
11999875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
12009875cf80SDavid Howells 		if (!mounted)
12019875cf80SDavid Howells 			break;
12029875cf80SDavid Howells 		dput(path->dentry);
12039875cf80SDavid Howells 		mntput(path->mnt);
12049875cf80SDavid Howells 		path->mnt = mounted;
12059875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
12069875cf80SDavid Howells 	}
12079875cf80SDavid Howells }
12089875cf80SDavid Howells 
120931e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
12101da177e4SLinus Torvalds {
12112a737871SAl Viro 	set_root(nd);
1212e518ddb7SAndreas Mohr 
12131da177e4SLinus Torvalds 	while(1) {
12144ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
12151da177e4SLinus Torvalds 
12162a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
12172a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
12181da177e4SLinus Torvalds 			break;
12191da177e4SLinus Torvalds 		}
12204ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
12213088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
12223088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
12231da177e4SLinus Torvalds 			dput(old);
12241da177e4SLinus Torvalds 			break;
12251da177e4SLinus Torvalds 		}
12263088dd70SAl Viro 		if (!follow_up(&nd->path))
12271da177e4SLinus Torvalds 			break;
12281da177e4SLinus Torvalds 	}
122979ed0226SAl Viro 	follow_mount(&nd->path);
123031e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
12311da177e4SLinus Torvalds }
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds /*
1234bad61189SMiklos Szeredi  * This looks up the name in dcache, possibly revalidates the old dentry and
1235bad61189SMiklos Szeredi  * allocates a new one if not found or not valid.  In the need_lookup argument
1236bad61189SMiklos Szeredi  * returns whether i_op->lookup is necessary.
1237bad61189SMiklos Szeredi  *
1238bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
1239baa03890SNick Piggin  */
1240bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1241201f956eSAl Viro 				    unsigned int flags, bool *need_lookup)
1242baa03890SNick Piggin {
1243baa03890SNick Piggin 	struct dentry *dentry;
1244bad61189SMiklos Szeredi 	int error;
1245baa03890SNick Piggin 
1246bad61189SMiklos Szeredi 	*need_lookup = false;
1247bad61189SMiklos Szeredi 	dentry = d_lookup(dir, name);
1248bad61189SMiklos Szeredi 	if (dentry) {
1249bad61189SMiklos Szeredi 		if (d_need_lookup(dentry)) {
1250bad61189SMiklos Szeredi 			*need_lookup = true;
1251bad61189SMiklos Szeredi 		} else if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1252201f956eSAl Viro 			error = d_revalidate(dentry, flags);
1253bad61189SMiklos Szeredi 			if (unlikely(error <= 0)) {
1254bad61189SMiklos Szeredi 				if (error < 0) {
1255bad61189SMiklos Szeredi 					dput(dentry);
1256bad61189SMiklos Szeredi 					return ERR_PTR(error);
1257bad61189SMiklos Szeredi 				} else if (!d_invalidate(dentry)) {
1258bad61189SMiklos Szeredi 					dput(dentry);
1259bad61189SMiklos Szeredi 					dentry = NULL;
1260bad61189SMiklos Szeredi 				}
1261bad61189SMiklos Szeredi 			}
1262bad61189SMiklos Szeredi 		}
1263bad61189SMiklos Szeredi 	}
1264baa03890SNick Piggin 
1265bad61189SMiklos Szeredi 	if (!dentry) {
1266bad61189SMiklos Szeredi 		dentry = d_alloc(dir, name);
1267baa03890SNick Piggin 		if (unlikely(!dentry))
1268baa03890SNick Piggin 			return ERR_PTR(-ENOMEM);
1269baa03890SNick Piggin 
1270bad61189SMiklos Szeredi 		*need_lookup = true;
1271baa03890SNick Piggin 	}
1272baa03890SNick Piggin 	return dentry;
1273baa03890SNick Piggin }
1274baa03890SNick Piggin 
1275baa03890SNick Piggin /*
1276bad61189SMiklos Szeredi  * Call i_op->lookup on the dentry.  The dentry must be negative but may be
1277bad61189SMiklos Szeredi  * hashed if it was pouplated with DCACHE_NEED_LOOKUP.
1278bad61189SMiklos Szeredi  *
1279bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
128044396f4bSJosef Bacik  */
1281bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
128272bd866aSAl Viro 				  unsigned int flags)
128344396f4bSJosef Bacik {
128444396f4bSJosef Bacik 	struct dentry *old;
128544396f4bSJosef Bacik 
128644396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
1287bad61189SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
1288e188dc02SMiklos Szeredi 		dput(dentry);
128944396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
1290e188dc02SMiklos Szeredi 	}
129144396f4bSJosef Bacik 
129272bd866aSAl Viro 	old = dir->i_op->lookup(dir, dentry, flags);
129344396f4bSJosef Bacik 	if (unlikely(old)) {
129444396f4bSJosef Bacik 		dput(dentry);
129544396f4bSJosef Bacik 		dentry = old;
129644396f4bSJosef Bacik 	}
129744396f4bSJosef Bacik 	return dentry;
129844396f4bSJosef Bacik }
129944396f4bSJosef Bacik 
1300a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name,
130172bd866aSAl Viro 		struct dentry *base, unsigned int flags)
1302a3255546SAl Viro {
1303bad61189SMiklos Szeredi 	bool need_lookup;
1304a3255546SAl Viro 	struct dentry *dentry;
1305a3255546SAl Viro 
130672bd866aSAl Viro 	dentry = lookup_dcache(name, base, flags, &need_lookup);
1307bad61189SMiklos Szeredi 	if (!need_lookup)
1308a3255546SAl Viro 		return dentry;
1309bad61189SMiklos Szeredi 
131072bd866aSAl Viro 	return lookup_real(base->d_inode, dentry, flags);
1311a3255546SAl Viro }
1312a3255546SAl Viro 
131344396f4bSJosef Bacik /*
13141da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
13151da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
13161da177e4SLinus Torvalds  *  It _is_ time-critical.
13171da177e4SLinus Torvalds  */
1318697f514dSMiklos Szeredi static int lookup_fast(struct nameidata *nd, struct qstr *name,
131931e6b01fSNick Piggin 		       struct path *path, struct inode **inode)
13201da177e4SLinus Torvalds {
13214ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
132231e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
13235a18fff2SAl Viro 	int need_reval = 1;
13245a18fff2SAl Viro 	int status = 1;
13259875cf80SDavid Howells 	int err;
13269875cf80SDavid Howells 
13273cac260aSAl Viro 	/*
1328b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1329b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1330b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1331b04f784eSNick Piggin 	 */
133231e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
133331e6b01fSNick Piggin 		unsigned seq;
133412f8ad4bSLinus Torvalds 		dentry = __d_lookup_rcu(parent, name, &seq, nd->inode);
13355a18fff2SAl Viro 		if (!dentry)
13365a18fff2SAl Viro 			goto unlazy;
13375a18fff2SAl Viro 
133812f8ad4bSLinus Torvalds 		/*
133912f8ad4bSLinus Torvalds 		 * This sequence count validates that the inode matches
134012f8ad4bSLinus Torvalds 		 * the dentry name information from lookup.
134112f8ad4bSLinus Torvalds 		 */
134212f8ad4bSLinus Torvalds 		*inode = dentry->d_inode;
134312f8ad4bSLinus Torvalds 		if (read_seqcount_retry(&dentry->d_seq, seq))
134412f8ad4bSLinus Torvalds 			return -ECHILD;
134512f8ad4bSLinus Torvalds 
134612f8ad4bSLinus Torvalds 		/*
134712f8ad4bSLinus Torvalds 		 * This sequence count validates that the parent had no
134812f8ad4bSLinus Torvalds 		 * changes while we did the lookup of the dentry above.
134912f8ad4bSLinus Torvalds 		 *
135012f8ad4bSLinus Torvalds 		 * The memory barrier in read_seqcount_begin of child is
135112f8ad4bSLinus Torvalds 		 *  enough, we can use __read_seqcount_retry here.
135212f8ad4bSLinus Torvalds 		 */
135331e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
135431e6b01fSNick Piggin 			return -ECHILD;
135531e6b01fSNick Piggin 		nd->seq = seq;
13565a18fff2SAl Viro 
1357fa4ee159SMiklos Szeredi 		if (unlikely(d_need_lookup(dentry)))
1358fa4ee159SMiklos Szeredi 			goto unlazy;
135924643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
13604ce16ef3SAl Viro 			status = d_revalidate(dentry, nd->flags);
13615a18fff2SAl Viro 			if (unlikely(status <= 0)) {
13625a18fff2SAl Viro 				if (status != -ECHILD)
13635a18fff2SAl Viro 					need_reval = 0;
13645a18fff2SAl Viro 				goto unlazy;
13655a18fff2SAl Viro 			}
136624643087SAl Viro 		}
136731e6b01fSNick Piggin 		path->mnt = mnt;
136831e6b01fSNick Piggin 		path->dentry = dentry;
1369d6e9bd25SAl Viro 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1370d6e9bd25SAl Viro 			goto unlazy;
1371d6e9bd25SAl Viro 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1372d6e9bd25SAl Viro 			goto unlazy;
13739875cf80SDavid Howells 		return 0;
13745a18fff2SAl Viro unlazy:
137519660af7SAl Viro 		if (unlazy_walk(nd, dentry))
13765a18fff2SAl Viro 			return -ECHILD;
13775a18fff2SAl Viro 	} else {
137831e6b01fSNick Piggin 		dentry = __d_lookup(parent, name);
137924643087SAl Viro 	}
13805a18fff2SAl Viro 
138181e6f520SAl Viro 	if (unlikely(!dentry))
138281e6f520SAl Viro 		goto need_lookup;
13835a18fff2SAl Viro 
138481e6f520SAl Viro 	if (unlikely(d_need_lookup(dentry))) {
138581e6f520SAl Viro 		dput(dentry);
138681e6f520SAl Viro 		goto need_lookup;
13875a18fff2SAl Viro 	}
138881e6f520SAl Viro 
13895a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
13904ce16ef3SAl Viro 		status = d_revalidate(dentry, nd->flags);
13915a18fff2SAl Viro 	if (unlikely(status <= 0)) {
13925a18fff2SAl Viro 		if (status < 0) {
13935a18fff2SAl Viro 			dput(dentry);
13945a18fff2SAl Viro 			return status;
13955a18fff2SAl Viro 		}
13965a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
13975a18fff2SAl Viro 			dput(dentry);
139881e6f520SAl Viro 			goto need_lookup;
13995a18fff2SAl Viro 		}
14005a18fff2SAl Viro 	}
1401697f514dSMiklos Szeredi 
14021da177e4SLinus Torvalds 	path->mnt = mnt;
14031da177e4SLinus Torvalds 	path->dentry = dentry;
14049875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
140589312214SIan Kent 	if (unlikely(err < 0)) {
140689312214SIan Kent 		path_put_conditional(path, nd);
14079875cf80SDavid Howells 		return err;
140889312214SIan Kent 	}
1409a3fbbde7SAl Viro 	if (err)
1410a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
141131e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
14121da177e4SLinus Torvalds 	return 0;
141381e6f520SAl Viro 
141481e6f520SAl Viro need_lookup:
1415697f514dSMiklos Szeredi 	return 1;
1416697f514dSMiklos Szeredi }
1417697f514dSMiklos Szeredi 
1418697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */
1419697f514dSMiklos Szeredi static int lookup_slow(struct nameidata *nd, struct qstr *name,
1420697f514dSMiklos Szeredi 		       struct path *path)
1421697f514dSMiklos Szeredi {
1422697f514dSMiklos Szeredi 	struct dentry *dentry, *parent;
1423697f514dSMiklos Szeredi 	int err;
1424697f514dSMiklos Szeredi 
1425697f514dSMiklos Szeredi 	parent = nd->path.dentry;
142681e6f520SAl Viro 	BUG_ON(nd->inode != parent->d_inode);
142781e6f520SAl Viro 
142881e6f520SAl Viro 	mutex_lock(&parent->d_inode->i_mutex);
142972bd866aSAl Viro 	dentry = __lookup_hash(name, parent, nd->flags);
143081e6f520SAl Viro 	mutex_unlock(&parent->d_inode->i_mutex);
143181e6f520SAl Viro 	if (IS_ERR(dentry))
143281e6f520SAl Viro 		return PTR_ERR(dentry);
1433697f514dSMiklos Szeredi 	path->mnt = nd->path.mnt;
1434697f514dSMiklos Szeredi 	path->dentry = dentry;
1435697f514dSMiklos Szeredi 	err = follow_managed(path, nd->flags);
1436697f514dSMiklos Szeredi 	if (unlikely(err < 0)) {
1437697f514dSMiklos Szeredi 		path_put_conditional(path, nd);
1438697f514dSMiklos Szeredi 		return err;
1439697f514dSMiklos Szeredi 	}
1440697f514dSMiklos Szeredi 	if (err)
1441697f514dSMiklos Szeredi 		nd->flags |= LOOKUP_JUMPED;
1442697f514dSMiklos Szeredi 	return 0;
14431da177e4SLinus Torvalds }
14441da177e4SLinus Torvalds 
144552094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
144652094c8aSAl Viro {
144752094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
14484ad5abb3SAl Viro 		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
144952094c8aSAl Viro 		if (err != -ECHILD)
145052094c8aSAl Viro 			return err;
145119660af7SAl Viro 		if (unlazy_walk(nd, NULL))
145252094c8aSAl Viro 			return -ECHILD;
145352094c8aSAl Viro 	}
14544ad5abb3SAl Viro 	return inode_permission(nd->inode, MAY_EXEC);
145552094c8aSAl Viro }
145652094c8aSAl Viro 
14579856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
14589856fa1bSAl Viro {
14599856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
14609856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
14619856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
14629856fa1bSAl Viro 				return -ECHILD;
14639856fa1bSAl Viro 		} else
14649856fa1bSAl Viro 			follow_dotdot(nd);
14659856fa1bSAl Viro 	}
14669856fa1bSAl Viro 	return 0;
14679856fa1bSAl Viro }
14689856fa1bSAl Viro 
1469951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1470951361f9SAl Viro {
1471951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1472951361f9SAl Viro 		path_put(&nd->path);
1473951361f9SAl Viro 	} else {
1474951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
14755b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1476951361f9SAl Viro 			nd->root.mnt = NULL;
147732a7991bSAl Viro 		unlock_rcu_walk();
1478951361f9SAl Viro 	}
1479951361f9SAl Viro }
1480951361f9SAl Viro 
14813ddcd056SLinus Torvalds /*
14823ddcd056SLinus Torvalds  * Do we need to follow links? We _really_ want to be able
14833ddcd056SLinus Torvalds  * to do this check without having to look at inode->i_op,
14843ddcd056SLinus Torvalds  * so we keep a cache of "no, this doesn't need follow_link"
14853ddcd056SLinus Torvalds  * for the common case.
14863ddcd056SLinus Torvalds  */
14877813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow)
14883ddcd056SLinus Torvalds {
14893ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
14903ddcd056SLinus Torvalds 		if (likely(inode->i_op->follow_link))
14913ddcd056SLinus Torvalds 			return follow;
14923ddcd056SLinus Torvalds 
14933ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
14943ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
14953ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_NOFOLLOW;
14963ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
14973ddcd056SLinus Torvalds 	}
14983ddcd056SLinus Torvalds 	return 0;
14993ddcd056SLinus Torvalds }
15003ddcd056SLinus Torvalds 
1501ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
1502ce57dfc1SAl Viro 		struct qstr *name, int type, int follow)
1503ce57dfc1SAl Viro {
1504ce57dfc1SAl Viro 	struct inode *inode;
1505ce57dfc1SAl Viro 	int err;
1506ce57dfc1SAl Viro 	/*
1507ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1508ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1509ce57dfc1SAl Viro 	 * parent relationships.
1510ce57dfc1SAl Viro 	 */
1511ce57dfc1SAl Viro 	if (unlikely(type != LAST_NORM))
1512ce57dfc1SAl Viro 		return handle_dots(nd, type);
1513697f514dSMiklos Szeredi 	err = lookup_fast(nd, name, path, &inode);
1514ce57dfc1SAl Viro 	if (unlikely(err)) {
1515697f514dSMiklos Szeredi 		if (err < 0)
1516697f514dSMiklos Szeredi 			goto out_err;
1517697f514dSMiklos Szeredi 
1518697f514dSMiklos Szeredi 		err = lookup_slow(nd, name, path);
1519697f514dSMiklos Szeredi 		if (err < 0)
1520697f514dSMiklos Szeredi 			goto out_err;
1521697f514dSMiklos Szeredi 
1522697f514dSMiklos Szeredi 		inode = path->dentry->d_inode;
1523ce57dfc1SAl Viro 	}
1524697f514dSMiklos Szeredi 	err = -ENOENT;
1525697f514dSMiklos Szeredi 	if (!inode)
1526697f514dSMiklos Szeredi 		goto out_path_put;
1527697f514dSMiklos Szeredi 
15287813b94aSLinus Torvalds 	if (should_follow_link(inode, follow)) {
152919660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
153019660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
1531697f514dSMiklos Szeredi 				err = -ECHILD;
1532697f514dSMiklos Szeredi 				goto out_err;
153319660af7SAl Viro 			}
153419660af7SAl Viro 		}
1535ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1536ce57dfc1SAl Viro 		return 1;
1537ce57dfc1SAl Viro 	}
1538ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1539ce57dfc1SAl Viro 	nd->inode = inode;
1540ce57dfc1SAl Viro 	return 0;
1541697f514dSMiklos Szeredi 
1542697f514dSMiklos Szeredi out_path_put:
1543697f514dSMiklos Szeredi 	path_to_nameidata(path, nd);
1544697f514dSMiklos Szeredi out_err:
1545697f514dSMiklos Szeredi 	terminate_walk(nd);
1546697f514dSMiklos Szeredi 	return err;
1547ce57dfc1SAl Viro }
1548ce57dfc1SAl Viro 
15491da177e4SLinus Torvalds /*
1550b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1551b356379aSAl Viro  * limiting consecutive symlinks to 40.
1552b356379aSAl Viro  *
1553b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1554b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1555b356379aSAl Viro  */
1556b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1557b356379aSAl Viro {
1558b356379aSAl Viro 	int res;
1559b356379aSAl Viro 
1560b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1561b356379aSAl Viro 		path_put_conditional(path, nd);
1562b356379aSAl Viro 		path_put(&nd->path);
1563b356379aSAl Viro 		return -ELOOP;
1564b356379aSAl Viro 	}
15651a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1566b356379aSAl Viro 
1567b356379aSAl Viro 	nd->depth++;
1568b356379aSAl Viro 	current->link_count++;
1569b356379aSAl Viro 
1570b356379aSAl Viro 	do {
1571b356379aSAl Viro 		struct path link = *path;
1572b356379aSAl Viro 		void *cookie;
1573574197e0SAl Viro 
1574574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
15756d7b5aaeSAl Viro 		if (res)
15766d7b5aaeSAl Viro 			break;
1577b356379aSAl Viro 		res = walk_component(nd, path, &nd->last,
1578b356379aSAl Viro 				     nd->last_type, LOOKUP_FOLLOW);
1579574197e0SAl Viro 		put_link(nd, &link, cookie);
1580b356379aSAl Viro 	} while (res > 0);
1581b356379aSAl Viro 
1582b356379aSAl Viro 	current->link_count--;
1583b356379aSAl Viro 	nd->depth--;
1584b356379aSAl Viro 	return res;
1585b356379aSAl Viro }
1586b356379aSAl Viro 
1587b356379aSAl Viro /*
15883ddcd056SLinus Torvalds  * We really don't want to look at inode->i_op->lookup
15893ddcd056SLinus Torvalds  * when we don't have to. So we keep a cache bit in
15903ddcd056SLinus Torvalds  * the inode ->i_opflags field that says "yes, we can
15913ddcd056SLinus Torvalds  * do lookup on this inode".
15923ddcd056SLinus Torvalds  */
15933ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode)
15943ddcd056SLinus Torvalds {
15953ddcd056SLinus Torvalds 	if (likely(inode->i_opflags & IOP_LOOKUP))
15963ddcd056SLinus Torvalds 		return 1;
15973ddcd056SLinus Torvalds 	if (likely(!inode->i_op->lookup))
15983ddcd056SLinus Torvalds 		return 0;
15993ddcd056SLinus Torvalds 
16003ddcd056SLinus Torvalds 	/* We do this once for the lifetime of the inode */
16013ddcd056SLinus Torvalds 	spin_lock(&inode->i_lock);
16023ddcd056SLinus Torvalds 	inode->i_opflags |= IOP_LOOKUP;
16033ddcd056SLinus Torvalds 	spin_unlock(&inode->i_lock);
16043ddcd056SLinus Torvalds 	return 1;
16053ddcd056SLinus Torvalds }
16063ddcd056SLinus Torvalds 
1607bfcfaa77SLinus Torvalds /*
1608bfcfaa77SLinus Torvalds  * We can do the critical dentry name comparison and hashing
1609bfcfaa77SLinus Torvalds  * operations one word at a time, but we are limited to:
1610bfcfaa77SLinus Torvalds  *
1611bfcfaa77SLinus Torvalds  * - Architectures with fast unaligned word accesses. We could
1612bfcfaa77SLinus Torvalds  *   do a "get_unaligned()" if this helps and is sufficiently
1613bfcfaa77SLinus Torvalds  *   fast.
1614bfcfaa77SLinus Torvalds  *
1615bfcfaa77SLinus Torvalds  * - Little-endian machines (so that we can generate the mask
1616bfcfaa77SLinus Torvalds  *   of low bytes efficiently). Again, we *could* do a byte
1617bfcfaa77SLinus Torvalds  *   swapping load on big-endian architectures if that is not
1618bfcfaa77SLinus Torvalds  *   expensive enough to make the optimization worthless.
1619bfcfaa77SLinus Torvalds  *
1620bfcfaa77SLinus Torvalds  * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1621bfcfaa77SLinus Torvalds  *   do not trap on the (extremely unlikely) case of a page
1622bfcfaa77SLinus Torvalds  *   crossing operation.
1623bfcfaa77SLinus Torvalds  *
1624bfcfaa77SLinus Torvalds  * - Furthermore, we need an efficient 64-bit compile for the
1625bfcfaa77SLinus Torvalds  *   64-bit case in order to generate the "number of bytes in
1626bfcfaa77SLinus Torvalds  *   the final mask". Again, that could be replaced with a
1627bfcfaa77SLinus Torvalds  *   efficient population count instruction or similar.
1628bfcfaa77SLinus Torvalds  */
1629bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS
1630bfcfaa77SLinus Torvalds 
1631f68e556eSLinus Torvalds #include <asm/word-at-a-time.h>
1632bfcfaa77SLinus Torvalds 
1633f68e556eSLinus Torvalds #ifdef CONFIG_64BIT
1634bfcfaa77SLinus Torvalds 
1635bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash)
1636bfcfaa77SLinus Torvalds {
1637bfcfaa77SLinus Torvalds 	hash += hash >> (8*sizeof(int));
1638bfcfaa77SLinus Torvalds 	return hash;
1639bfcfaa77SLinus Torvalds }
1640bfcfaa77SLinus Torvalds 
1641bfcfaa77SLinus Torvalds #else	/* 32-bit case */
1642bfcfaa77SLinus Torvalds 
1643bfcfaa77SLinus Torvalds #define fold_hash(x) (x)
1644bfcfaa77SLinus Torvalds 
1645bfcfaa77SLinus Torvalds #endif
1646bfcfaa77SLinus Torvalds 
1647bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1648bfcfaa77SLinus Torvalds {
1649bfcfaa77SLinus Torvalds 	unsigned long a, mask;
1650bfcfaa77SLinus Torvalds 	unsigned long hash = 0;
1651bfcfaa77SLinus Torvalds 
1652bfcfaa77SLinus Torvalds 	for (;;) {
1653e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name);
1654bfcfaa77SLinus Torvalds 		if (len < sizeof(unsigned long))
1655bfcfaa77SLinus Torvalds 			break;
1656bfcfaa77SLinus Torvalds 		hash += a;
1657f132c5beSAl Viro 		hash *= 9;
1658bfcfaa77SLinus Torvalds 		name += sizeof(unsigned long);
1659bfcfaa77SLinus Torvalds 		len -= sizeof(unsigned long);
1660bfcfaa77SLinus Torvalds 		if (!len)
1661bfcfaa77SLinus Torvalds 			goto done;
1662bfcfaa77SLinus Torvalds 	}
1663bfcfaa77SLinus Torvalds 	mask = ~(~0ul << len*8);
1664bfcfaa77SLinus Torvalds 	hash += mask & a;
1665bfcfaa77SLinus Torvalds done:
1666bfcfaa77SLinus Torvalds 	return fold_hash(hash);
1667bfcfaa77SLinus Torvalds }
1668bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
1669bfcfaa77SLinus Torvalds 
1670bfcfaa77SLinus Torvalds /*
1671bfcfaa77SLinus Torvalds  * Calculate the length and hash of the path component, and
1672bfcfaa77SLinus Torvalds  * return the length of the component;
1673bfcfaa77SLinus Torvalds  */
1674bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1675bfcfaa77SLinus Torvalds {
167636126f8fSLinus Torvalds 	unsigned long a, b, adata, bdata, mask, hash, len;
167736126f8fSLinus Torvalds 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1678bfcfaa77SLinus Torvalds 
1679bfcfaa77SLinus Torvalds 	hash = a = 0;
1680bfcfaa77SLinus Torvalds 	len = -sizeof(unsigned long);
1681bfcfaa77SLinus Torvalds 	do {
1682bfcfaa77SLinus Torvalds 		hash = (hash + a) * 9;
1683bfcfaa77SLinus Torvalds 		len += sizeof(unsigned long);
1684e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name+len);
168536126f8fSLinus Torvalds 		b = a ^ REPEAT_BYTE('/');
168636126f8fSLinus Torvalds 	} while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1687bfcfaa77SLinus Torvalds 
168836126f8fSLinus Torvalds 	adata = prep_zero_mask(a, adata, &constants);
168936126f8fSLinus Torvalds 	bdata = prep_zero_mask(b, bdata, &constants);
169036126f8fSLinus Torvalds 
169136126f8fSLinus Torvalds 	mask = create_zero_mask(adata | bdata);
169236126f8fSLinus Torvalds 
169336126f8fSLinus Torvalds 	hash += a & zero_bytemask(mask);
1694bfcfaa77SLinus Torvalds 	*hashp = fold_hash(hash);
1695bfcfaa77SLinus Torvalds 
169636126f8fSLinus Torvalds 	return len + find_zero(mask);
1697bfcfaa77SLinus Torvalds }
1698bfcfaa77SLinus Torvalds 
1699bfcfaa77SLinus Torvalds #else
1700bfcfaa77SLinus Torvalds 
17010145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
17020145acc2SLinus Torvalds {
17030145acc2SLinus Torvalds 	unsigned long hash = init_name_hash();
17040145acc2SLinus Torvalds 	while (len--)
17050145acc2SLinus Torvalds 		hash = partial_name_hash(*name++, hash);
17060145acc2SLinus Torvalds 	return end_name_hash(hash);
17070145acc2SLinus Torvalds }
1708ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
17090145acc2SLinus Torvalds 
17103ddcd056SLinus Torvalds /*
1711200e9ef7SLinus Torvalds  * We know there's a real path component here of at least
1712200e9ef7SLinus Torvalds  * one character.
1713200e9ef7SLinus Torvalds  */
1714200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1715200e9ef7SLinus Torvalds {
1716200e9ef7SLinus Torvalds 	unsigned long hash = init_name_hash();
1717200e9ef7SLinus Torvalds 	unsigned long len = 0, c;
1718200e9ef7SLinus Torvalds 
1719200e9ef7SLinus Torvalds 	c = (unsigned char)*name;
1720200e9ef7SLinus Torvalds 	do {
1721200e9ef7SLinus Torvalds 		len++;
1722200e9ef7SLinus Torvalds 		hash = partial_name_hash(c, hash);
1723200e9ef7SLinus Torvalds 		c = (unsigned char)name[len];
1724200e9ef7SLinus Torvalds 	} while (c && c != '/');
1725200e9ef7SLinus Torvalds 	*hashp = end_name_hash(hash);
1726200e9ef7SLinus Torvalds 	return len;
1727200e9ef7SLinus Torvalds }
1728200e9ef7SLinus Torvalds 
1729bfcfaa77SLinus Torvalds #endif
1730bfcfaa77SLinus Torvalds 
1731200e9ef7SLinus Torvalds /*
17321da177e4SLinus Torvalds  * Name resolution.
1733ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1734ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
17351da177e4SLinus Torvalds  *
1736ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1737ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
17381da177e4SLinus Torvalds  */
17396de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
17401da177e4SLinus Torvalds {
17411da177e4SLinus Torvalds 	struct path next;
17421da177e4SLinus Torvalds 	int err;
17431da177e4SLinus Torvalds 
17441da177e4SLinus Torvalds 	while (*name=='/')
17451da177e4SLinus Torvalds 		name++;
17461da177e4SLinus Torvalds 	if (!*name)
1747086e183aSAl Viro 		return 0;
17481da177e4SLinus Torvalds 
17491da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
17501da177e4SLinus Torvalds 	for(;;) {
17511da177e4SLinus Torvalds 		struct qstr this;
1752200e9ef7SLinus Torvalds 		long len;
1753fe479a58SAl Viro 		int type;
17541da177e4SLinus Torvalds 
175552094c8aSAl Viro 		err = may_lookup(nd);
17561da177e4SLinus Torvalds  		if (err)
17571da177e4SLinus Torvalds 			break;
17581da177e4SLinus Torvalds 
1759200e9ef7SLinus Torvalds 		len = hash_name(name, &this.hash);
17601da177e4SLinus Torvalds 		this.name = name;
1761200e9ef7SLinus Torvalds 		this.len = len;
17621da177e4SLinus Torvalds 
1763fe479a58SAl Viro 		type = LAST_NORM;
1764200e9ef7SLinus Torvalds 		if (name[0] == '.') switch (len) {
1765fe479a58SAl Viro 			case 2:
1766200e9ef7SLinus Torvalds 				if (name[1] == '.') {
1767fe479a58SAl Viro 					type = LAST_DOTDOT;
176816c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
176916c2cd71SAl Viro 				}
1770fe479a58SAl Viro 				break;
1771fe479a58SAl Viro 			case 1:
1772fe479a58SAl Viro 				type = LAST_DOT;
1773fe479a58SAl Viro 		}
17745a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
17755a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
177616c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
17775a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
17785a202bcdSAl Viro 				err = parent->d_op->d_hash(parent, nd->inode,
17795a202bcdSAl Viro 							   &this);
17805a202bcdSAl Viro 				if (err < 0)
17815a202bcdSAl Viro 					break;
17825a202bcdSAl Viro 			}
17835a202bcdSAl Viro 		}
1784fe479a58SAl Viro 
1785200e9ef7SLinus Torvalds 		if (!name[len])
17861da177e4SLinus Torvalds 			goto last_component;
1787200e9ef7SLinus Torvalds 		/*
1788200e9ef7SLinus Torvalds 		 * If it wasn't NUL, we know it was '/'. Skip that
1789200e9ef7SLinus Torvalds 		 * slash, and continue until no more slashes.
1790200e9ef7SLinus Torvalds 		 */
1791200e9ef7SLinus Torvalds 		do {
1792200e9ef7SLinus Torvalds 			len++;
1793200e9ef7SLinus Torvalds 		} while (unlikely(name[len] == '/'));
1794200e9ef7SLinus Torvalds 		if (!name[len])
1795b356379aSAl Viro 			goto last_component;
1796200e9ef7SLinus Torvalds 		name += len;
17971da177e4SLinus Torvalds 
1798ce57dfc1SAl Viro 		err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
1799ce57dfc1SAl Viro 		if (err < 0)
1800ce57dfc1SAl Viro 			return err;
1801fe479a58SAl Viro 
1802ce57dfc1SAl Viro 		if (err) {
1803b356379aSAl Viro 			err = nested_symlink(&next, nd);
18041da177e4SLinus Torvalds 			if (err)
1805a7472babSAl Viro 				return err;
180631e6b01fSNick Piggin 		}
18073ddcd056SLinus Torvalds 		if (can_lookup(nd->inode))
18081da177e4SLinus Torvalds 			continue;
18093ddcd056SLinus Torvalds 		err = -ENOTDIR;
18103ddcd056SLinus Torvalds 		break;
18111da177e4SLinus Torvalds 		/* here ends the main loop */
18121da177e4SLinus Torvalds 
18131da177e4SLinus Torvalds last_component:
1814ce57dfc1SAl Viro 		nd->last = this;
1815ce57dfc1SAl Viro 		nd->last_type = type;
1816ce57dfc1SAl Viro 		return 0;
1817ce57dfc1SAl Viro 	}
1818951361f9SAl Viro 	terminate_walk(nd);
18191da177e4SLinus Torvalds 	return err;
18201da177e4SLinus Torvalds }
18211da177e4SLinus Torvalds 
182270e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
182370e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
182431e6b01fSNick Piggin {
182531e6b01fSNick Piggin 	int retval = 0;
182631e6b01fSNick Piggin 
182731e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
182816c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
182931e6b01fSNick Piggin 	nd->depth = 0;
18305b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
18315b6ca027SAl Viro 		struct inode *inode = nd->root.dentry->d_inode;
183273d049a4SAl Viro 		if (*name) {
18335b6ca027SAl Viro 			if (!inode->i_op->lookup)
18345b6ca027SAl Viro 				return -ENOTDIR;
18355b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
18365b6ca027SAl Viro 			if (retval)
18375b6ca027SAl Viro 				return retval;
183873d049a4SAl Viro 		}
18395b6ca027SAl Viro 		nd->path = nd->root;
18405b6ca027SAl Viro 		nd->inode = inode;
18415b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
184232a7991bSAl Viro 			lock_rcu_walk();
18435b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
18445b6ca027SAl Viro 		} else {
18455b6ca027SAl Viro 			path_get(&nd->path);
18465b6ca027SAl Viro 		}
18475b6ca027SAl Viro 		return 0;
18485b6ca027SAl Viro 	}
18495b6ca027SAl Viro 
185031e6b01fSNick Piggin 	nd->root.mnt = NULL;
185131e6b01fSNick Piggin 
185231e6b01fSNick Piggin 	if (*name=='/') {
1853e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
185432a7991bSAl Viro 			lock_rcu_walk();
1855e41f7d4eSAl Viro 			set_root_rcu(nd);
1856e41f7d4eSAl Viro 		} else {
1857e41f7d4eSAl Viro 			set_root(nd);
1858e41f7d4eSAl Viro 			path_get(&nd->root);
1859e41f7d4eSAl Viro 		}
186031e6b01fSNick Piggin 		nd->path = nd->root;
186131e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1862e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
186331e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1864c28cc364SNick Piggin 			unsigned seq;
186531e6b01fSNick Piggin 
186632a7991bSAl Viro 			lock_rcu_walk();
186731e6b01fSNick Piggin 
1868c28cc364SNick Piggin 			do {
1869c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
187031e6b01fSNick Piggin 				nd->path = fs->pwd;
1871c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1872c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1873e41f7d4eSAl Viro 		} else {
1874e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1875e41f7d4eSAl Viro 		}
187631e6b01fSNick Piggin 	} else {
18772903ff01SAl Viro 		struct fd f = fdget_raw(dfd);
187831e6b01fSNick Piggin 		struct dentry *dentry;
187931e6b01fSNick Piggin 
18802903ff01SAl Viro 		if (!f.file)
18812903ff01SAl Viro 			return -EBADF;
188231e6b01fSNick Piggin 
18832903ff01SAl Viro 		dentry = f.file->f_path.dentry;
188431e6b01fSNick Piggin 
1885f52e0c11SAl Viro 		if (*name) {
18862903ff01SAl Viro 			if (!S_ISDIR(dentry->d_inode->i_mode)) {
18872903ff01SAl Viro 				fdput(f);
18882903ff01SAl Viro 				return -ENOTDIR;
1889f52e0c11SAl Viro 			}
189031e6b01fSNick Piggin 
18912903ff01SAl Viro 			retval = inode_permission(dentry->d_inode, MAY_EXEC);
18922903ff01SAl Viro 			if (retval) {
18932903ff01SAl Viro 				fdput(f);
18942903ff01SAl Viro 				return retval;
18952903ff01SAl Viro 			}
18962903ff01SAl Viro 		}
18972903ff01SAl Viro 
18982903ff01SAl Viro 		nd->path = f.file->f_path;
1899e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
19002903ff01SAl Viro 			if (f.need_put)
19012903ff01SAl Viro 				*fp = f.file;
1902c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
190332a7991bSAl Viro 			lock_rcu_walk();
19045590ff0dSUlrich Drepper 		} else {
19052903ff01SAl Viro 			path_get(&nd->path);
19062903ff01SAl Viro 			fdput(f);
19071da177e4SLinus Torvalds 		}
1908e41f7d4eSAl Viro 	}
1909e41f7d4eSAl Viro 
191031e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
19119b4a9b14SAl Viro 	return 0;
19129b4a9b14SAl Viro }
19139b4a9b14SAl Viro 
1914bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1915bd92d7feSAl Viro {
1916bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1917bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1918bd92d7feSAl Viro 
1919bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
1920bd92d7feSAl Viro 	return walk_component(nd, path, &nd->last, nd->last_type,
1921bd92d7feSAl Viro 					nd->flags & LOOKUP_FOLLOW);
1922bd92d7feSAl Viro }
1923bd92d7feSAl Viro 
19249b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1925ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
19269b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
19279b4a9b14SAl Viro {
192870e9b357SAl Viro 	struct file *base = NULL;
1929bd92d7feSAl Viro 	struct path path;
1930bd92d7feSAl Viro 	int err;
193131e6b01fSNick Piggin 
193231e6b01fSNick Piggin 	/*
193331e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
193431e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
193531e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
193631e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
193731e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
193831e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
193931e6b01fSNick Piggin 	 * analogue, foo_rcu().
194031e6b01fSNick Piggin 	 *
194131e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
194231e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
194331e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
194431e6b01fSNick Piggin 	 * be able to complete).
194531e6b01fSNick Piggin 	 */
1946bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1947ee0827cdSAl Viro 
1948bd92d7feSAl Viro 	if (unlikely(err))
1949bd92d7feSAl Viro 		return err;
1950ee0827cdSAl Viro 
1951ee0827cdSAl Viro 	current->total_link_count = 0;
1952bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1953bd92d7feSAl Viro 
1954bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1955bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1956bd92d7feSAl Viro 		while (err > 0) {
1957bd92d7feSAl Viro 			void *cookie;
1958bd92d7feSAl Viro 			struct path link = path;
1959800179c9SKees Cook 			err = may_follow_link(&link, nd);
1960800179c9SKees Cook 			if (unlikely(err))
1961800179c9SKees Cook 				break;
1962bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1963574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
19646d7b5aaeSAl Viro 			if (err)
19656d7b5aaeSAl Viro 				break;
1966bd92d7feSAl Viro 			err = lookup_last(nd, &path);
1967574197e0SAl Viro 			put_link(nd, &link, cookie);
1968bd92d7feSAl Viro 		}
1969bd92d7feSAl Viro 	}
1970ee0827cdSAl Viro 
19719f1fafeeSAl Viro 	if (!err)
19729f1fafeeSAl Viro 		err = complete_walk(nd);
1973bd92d7feSAl Viro 
1974bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
1975bd92d7feSAl Viro 		if (!nd->inode->i_op->lookup) {
1976bd92d7feSAl Viro 			path_put(&nd->path);
1977bd23a539SAl Viro 			err = -ENOTDIR;
1978bd92d7feSAl Viro 		}
1979bd92d7feSAl Viro 	}
198016c2cd71SAl Viro 
198170e9b357SAl Viro 	if (base)
198270e9b357SAl Viro 		fput(base);
1983ee0827cdSAl Viro 
19845b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
198531e6b01fSNick Piggin 		path_put(&nd->root);
198631e6b01fSNick Piggin 		nd->root.mnt = NULL;
198731e6b01fSNick Piggin 	}
1988bd92d7feSAl Viro 	return err;
198931e6b01fSNick Piggin }
199031e6b01fSNick Piggin 
1991873f1eedSJeff Layton static int filename_lookup(int dfd, struct filename *name,
1992873f1eedSJeff Layton 				unsigned int flags, struct nameidata *nd)
1993873f1eedSJeff Layton {
1994873f1eedSJeff Layton 	int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
1995873f1eedSJeff Layton 	if (unlikely(retval == -ECHILD))
1996873f1eedSJeff Layton 		retval = path_lookupat(dfd, name->name, flags, nd);
1997873f1eedSJeff Layton 	if (unlikely(retval == -ESTALE))
1998873f1eedSJeff Layton 		retval = path_lookupat(dfd, name->name,
1999873f1eedSJeff Layton 						flags | LOOKUP_REVAL, nd);
2000873f1eedSJeff Layton 
2001873f1eedSJeff Layton 	if (likely(!retval))
2002873f1eedSJeff Layton 		audit_inode(name->name, nd->path.dentry,
2003873f1eedSJeff Layton 						flags & LOOKUP_PARENT);
2004873f1eedSJeff Layton 	return retval;
2005873f1eedSJeff Layton }
2006873f1eedSJeff Layton 
2007ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
2008ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
2009ee0827cdSAl Viro {
2010873f1eedSJeff Layton 	struct filename filename = { .name = name };
2011ee0827cdSAl Viro 
2012873f1eedSJeff Layton 	return filename_lookup(dfd, &filename, flags, nd);
20131da177e4SLinus Torvalds }
20141da177e4SLinus Torvalds 
201579714f72SAl Viro /* does lookup, returns the object with parent locked */
201679714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path)
20175590ff0dSUlrich Drepper {
201879714f72SAl Viro 	struct nameidata nd;
201979714f72SAl Viro 	struct dentry *d;
202079714f72SAl Viro 	int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
202179714f72SAl Viro 	if (err)
202279714f72SAl Viro 		return ERR_PTR(err);
202379714f72SAl Viro 	if (nd.last_type != LAST_NORM) {
202479714f72SAl Viro 		path_put(&nd.path);
202579714f72SAl Viro 		return ERR_PTR(-EINVAL);
202679714f72SAl Viro 	}
202779714f72SAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
20281e0ea001SAl Viro 	d = __lookup_hash(&nd.last, nd.path.dentry, 0);
202979714f72SAl Viro 	if (IS_ERR(d)) {
203079714f72SAl Viro 		mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
203179714f72SAl Viro 		path_put(&nd.path);
203279714f72SAl Viro 		return d;
203379714f72SAl Viro 	}
203479714f72SAl Viro 	*path = nd.path;
203579714f72SAl Viro 	return d;
20365590ff0dSUlrich Drepper }
20375590ff0dSUlrich Drepper 
2038d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
2039d1811465SAl Viro {
2040d1811465SAl Viro 	struct nameidata nd;
2041d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2042d1811465SAl Viro 	if (!res)
2043d1811465SAl Viro 		*path = nd.path;
2044d1811465SAl Viro 	return res;
2045d1811465SAl Viro }
2046d1811465SAl Viro 
204716f18200SJosef 'Jeff' Sipek /**
204816f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
204916f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
205016f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
205116f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
205216f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
2053e0a01249SAl Viro  * @path: pointer to struct path to fill
205416f18200SJosef 'Jeff' Sipek  */
205516f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
205616f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
2057e0a01249SAl Viro 		    struct path *path)
205816f18200SJosef 'Jeff' Sipek {
2059e0a01249SAl Viro 	struct nameidata nd;
2060e0a01249SAl Viro 	int err;
2061e0a01249SAl Viro 	nd.root.dentry = dentry;
2062e0a01249SAl Viro 	nd.root.mnt = mnt;
2063e0a01249SAl Viro 	BUG_ON(flags & LOOKUP_PARENT);
20645b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
2065e0a01249SAl Viro 	err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2066e0a01249SAl Viro 	if (!err)
2067e0a01249SAl Viro 		*path = nd.path;
2068e0a01249SAl Viro 	return err;
206916f18200SJosef 'Jeff' Sipek }
207016f18200SJosef 'Jeff' Sipek 
2071057f6c01SJames Morris /*
2072057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
2073057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
2074057f6c01SJames Morris  * SMP-safe.
2075057f6c01SJames Morris  */
2076a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
20771da177e4SLinus Torvalds {
207872bd866aSAl Viro 	return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
20791da177e4SLinus Torvalds }
20801da177e4SLinus Torvalds 
2081eead1911SChristoph Hellwig /**
2082a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
2083eead1911SChristoph Hellwig  * @name:	pathname component to lookup
2084eead1911SChristoph Hellwig  * @base:	base directory to lookup from
2085eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
2086eead1911SChristoph Hellwig  *
2087a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
2088a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
2089eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
2090eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
2091eead1911SChristoph Hellwig  */
2092057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2093057f6c01SJames Morris {
2094057f6c01SJames Morris 	struct qstr this;
20956a96ba54SAl Viro 	unsigned int c;
2096cda309deSMiklos Szeredi 	int err;
2097057f6c01SJames Morris 
20982f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
20992f9092e1SDavid Woodhouse 
21006a96ba54SAl Viro 	this.name = name;
21016a96ba54SAl Viro 	this.len = len;
21020145acc2SLinus Torvalds 	this.hash = full_name_hash(name, len);
21036a96ba54SAl Viro 	if (!len)
21046a96ba54SAl Viro 		return ERR_PTR(-EACCES);
21056a96ba54SAl Viro 
21066a96ba54SAl Viro 	while (len--) {
21076a96ba54SAl Viro 		c = *(const unsigned char *)name++;
21086a96ba54SAl Viro 		if (c == '/' || c == '\0')
21096a96ba54SAl Viro 			return ERR_PTR(-EACCES);
21106a96ba54SAl Viro 	}
21115a202bcdSAl Viro 	/*
21125a202bcdSAl Viro 	 * See if the low-level filesystem might want
21135a202bcdSAl Viro 	 * to use its own hash..
21145a202bcdSAl Viro 	 */
21155a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
21165a202bcdSAl Viro 		int err = base->d_op->d_hash(base, base->d_inode, &this);
21175a202bcdSAl Viro 		if (err < 0)
21185a202bcdSAl Viro 			return ERR_PTR(err);
21195a202bcdSAl Viro 	}
2120eead1911SChristoph Hellwig 
2121cda309deSMiklos Szeredi 	err = inode_permission(base->d_inode, MAY_EXEC);
2122cda309deSMiklos Szeredi 	if (err)
2123cda309deSMiklos Szeredi 		return ERR_PTR(err);
2124cda309deSMiklos Szeredi 
212572bd866aSAl Viro 	return __lookup_hash(&this, base, 0);
2126057f6c01SJames Morris }
2127057f6c01SJames Morris 
21281fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
21291fa1e7f6SAndy Whitcroft 		 struct path *path, int *empty)
21301da177e4SLinus Torvalds {
21312d8f3038SAl Viro 	struct nameidata nd;
213291a27b2aSJeff Layton 	struct filename *tmp = getname_flags(name, flags, empty);
21331da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
21341da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
21352d8f3038SAl Viro 
21362d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
21372d8f3038SAl Viro 
2138873f1eedSJeff Layton 		err = filename_lookup(dfd, tmp, flags, &nd);
21391da177e4SLinus Torvalds 		putname(tmp);
21402d8f3038SAl Viro 		if (!err)
21412d8f3038SAl Viro 			*path = nd.path;
21421da177e4SLinus Torvalds 	}
21431da177e4SLinus Torvalds 	return err;
21441da177e4SLinus Torvalds }
21451da177e4SLinus Torvalds 
21461fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags,
21471fa1e7f6SAndy Whitcroft 		 struct path *path)
21481fa1e7f6SAndy Whitcroft {
2149f7493e5dSLinus Torvalds 	return user_path_at_empty(dfd, name, flags, path, NULL);
21501fa1e7f6SAndy Whitcroft }
21511fa1e7f6SAndy Whitcroft 
2152873f1eedSJeff Layton /*
2153873f1eedSJeff Layton  * NB: most callers don't do anything directly with the reference to the
2154873f1eedSJeff Layton  *     to struct filename, but the nd->last pointer points into the name string
2155873f1eedSJeff Layton  *     allocated by getname. So we must hold the reference to it until all
2156873f1eedSJeff Layton  *     path-walking is complete.
2157873f1eedSJeff Layton  */
215891a27b2aSJeff Layton static struct filename *
215991a27b2aSJeff Layton user_path_parent(int dfd, const char __user *path, struct nameidata *nd)
21602ad94ae6SAl Viro {
216191a27b2aSJeff Layton 	struct filename *s = getname(path);
21622ad94ae6SAl Viro 	int error;
21632ad94ae6SAl Viro 
21642ad94ae6SAl Viro 	if (IS_ERR(s))
216591a27b2aSJeff Layton 		return s;
21662ad94ae6SAl Viro 
2167873f1eedSJeff Layton 	error = filename_lookup(dfd, s, LOOKUP_PARENT, nd);
216891a27b2aSJeff Layton 	if (error) {
21692ad94ae6SAl Viro 		putname(s);
217091a27b2aSJeff Layton 		return ERR_PTR(error);
217191a27b2aSJeff Layton 	}
21722ad94ae6SAl Viro 
217391a27b2aSJeff Layton 	return s;
21742ad94ae6SAl Viro }
21752ad94ae6SAl Viro 
21761da177e4SLinus Torvalds /*
21771da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
21781da177e4SLinus Torvalds  * minimal.
21791da177e4SLinus Torvalds  */
21801da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
21811da177e4SLinus Torvalds {
21828e96e3b7SEric W. Biederman 	kuid_t fsuid = current_fsuid();
2183da9592edSDavid Howells 
21841da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
21851da177e4SLinus Torvalds 		return 0;
21868e96e3b7SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
21871da177e4SLinus Torvalds 		return 0;
21888e96e3b7SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
21891da177e4SLinus Torvalds 		return 0;
21901a48e2acSEric W. Biederman 	return !inode_capable(inode, CAP_FOWNER);
21911da177e4SLinus Torvalds }
21921da177e4SLinus Torvalds 
21931da177e4SLinus Torvalds /*
21941da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
21951da177e4SLinus Torvalds  *  whether the type of victim is right.
21961da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
21971da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
21981da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
21991da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
22001da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
22011da177e4SLinus Torvalds  *	a. be owner of dir, or
22021da177e4SLinus Torvalds  *	b. be owner of victim, or
22031da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
22041da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
22051da177e4SLinus Torvalds  *     links pointing to it.
22061da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
22071da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
22081da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
22091da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
22101da177e4SLinus Torvalds  *     nfs_async_unlink().
22111da177e4SLinus Torvalds  */
2212858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
22131da177e4SLinus Torvalds {
22141da177e4SLinus Torvalds 	int error;
22151da177e4SLinus Torvalds 
22161da177e4SLinus Torvalds 	if (!victim->d_inode)
22171da177e4SLinus Torvalds 		return -ENOENT;
22181da177e4SLinus Torvalds 
22191da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
22204fa6b5ecSJeff Layton 	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
22211da177e4SLinus Torvalds 
2222f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
22231da177e4SLinus Torvalds 	if (error)
22241da177e4SLinus Torvalds 		return error;
22251da177e4SLinus Torvalds 	if (IS_APPEND(dir))
22261da177e4SLinus Torvalds 		return -EPERM;
22271da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
2228f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
22291da177e4SLinus Torvalds 		return -EPERM;
22301da177e4SLinus Torvalds 	if (isdir) {
22311da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
22321da177e4SLinus Torvalds 			return -ENOTDIR;
22331da177e4SLinus Torvalds 		if (IS_ROOT(victim))
22341da177e4SLinus Torvalds 			return -EBUSY;
22351da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
22361da177e4SLinus Torvalds 		return -EISDIR;
22371da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
22381da177e4SLinus Torvalds 		return -ENOENT;
22391da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
22401da177e4SLinus Torvalds 		return -EBUSY;
22411da177e4SLinus Torvalds 	return 0;
22421da177e4SLinus Torvalds }
22431da177e4SLinus Torvalds 
22441da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
22451da177e4SLinus Torvalds  *  dir.
22461da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
22471da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
22481da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
22491da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
22501da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
22511da177e4SLinus Torvalds  */
2252a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
22531da177e4SLinus Torvalds {
22541da177e4SLinus Torvalds 	if (child->d_inode)
22551da177e4SLinus Torvalds 		return -EEXIST;
22561da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
22571da177e4SLinus Torvalds 		return -ENOENT;
2258f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
22591da177e4SLinus Torvalds }
22601da177e4SLinus Torvalds 
22611da177e4SLinus Torvalds /*
22621da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
22631da177e4SLinus Torvalds  */
22641da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
22651da177e4SLinus Torvalds {
22661da177e4SLinus Torvalds 	struct dentry *p;
22671da177e4SLinus Torvalds 
22681da177e4SLinus Torvalds 	if (p1 == p2) {
2269f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
22701da177e4SLinus Torvalds 		return NULL;
22711da177e4SLinus Torvalds 	}
22721da177e4SLinus Torvalds 
2273a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
22741da177e4SLinus Torvalds 
2275e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
2276e2761a11SOGAWA Hirofumi 	if (p) {
2277f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2278f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
22791da177e4SLinus Torvalds 		return p;
22801da177e4SLinus Torvalds 	}
22811da177e4SLinus Torvalds 
2282e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
2283e2761a11SOGAWA Hirofumi 	if (p) {
2284f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2285f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
22861da177e4SLinus Torvalds 		return p;
22871da177e4SLinus Torvalds 	}
22881da177e4SLinus Torvalds 
2289f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2290f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
22911da177e4SLinus Torvalds 	return NULL;
22921da177e4SLinus Torvalds }
22931da177e4SLinus Torvalds 
22941da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
22951da177e4SLinus Torvalds {
22961b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
22971da177e4SLinus Torvalds 	if (p1 != p2) {
22981b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
2299a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
23001da177e4SLinus Torvalds 	}
23011da177e4SLinus Torvalds }
23021da177e4SLinus Torvalds 
23034acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2304312b63fbSAl Viro 		bool want_excl)
23051da177e4SLinus Torvalds {
2306a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
23071da177e4SLinus Torvalds 	if (error)
23081da177e4SLinus Torvalds 		return error;
23091da177e4SLinus Torvalds 
2310acfa4380SAl Viro 	if (!dir->i_op->create)
23111da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
23121da177e4SLinus Torvalds 	mode &= S_IALLUGO;
23131da177e4SLinus Torvalds 	mode |= S_IFREG;
23141da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
23151da177e4SLinus Torvalds 	if (error)
23161da177e4SLinus Torvalds 		return error;
2317312b63fbSAl Viro 	error = dir->i_op->create(dir, dentry, mode, want_excl);
2318a74574aaSStephen Smalley 	if (!error)
2319f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
23201da177e4SLinus Torvalds 	return error;
23211da177e4SLinus Torvalds }
23221da177e4SLinus Torvalds 
232373d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
23241da177e4SLinus Torvalds {
23253fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
23261da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
23271da177e4SLinus Torvalds 	int error;
23281da177e4SLinus Torvalds 
2329bcda7652SAl Viro 	/* O_PATH? */
2330bcda7652SAl Viro 	if (!acc_mode)
2331bcda7652SAl Viro 		return 0;
2332bcda7652SAl Viro 
23331da177e4SLinus Torvalds 	if (!inode)
23341da177e4SLinus Torvalds 		return -ENOENT;
23351da177e4SLinus Torvalds 
2336c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2337c8fe8f30SChristoph Hellwig 	case S_IFLNK:
23381da177e4SLinus Torvalds 		return -ELOOP;
2339c8fe8f30SChristoph Hellwig 	case S_IFDIR:
2340c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
23411da177e4SLinus Torvalds 			return -EISDIR;
2342c8fe8f30SChristoph Hellwig 		break;
2343c8fe8f30SChristoph Hellwig 	case S_IFBLK:
2344c8fe8f30SChristoph Hellwig 	case S_IFCHR:
23453fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
23461da177e4SLinus Torvalds 			return -EACCES;
2347c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
2348c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2349c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
23501da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2351c8fe8f30SChristoph Hellwig 		break;
23524a3fd211SDave Hansen 	}
2353b41572e9SDave Hansen 
23543fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
2355b41572e9SDave Hansen 	if (error)
2356b41572e9SDave Hansen 		return error;
23576146f0d5SMimi Zohar 
23581da177e4SLinus Torvalds 	/*
23591da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
23601da177e4SLinus Torvalds 	 */
23611da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
23628737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
23637715b521SAl Viro 			return -EPERM;
23641da177e4SLinus Torvalds 		if (flag & O_TRUNC)
23657715b521SAl Viro 			return -EPERM;
23661da177e4SLinus Torvalds 	}
23671da177e4SLinus Torvalds 
23681da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
23692e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
23707715b521SAl Viro 		return -EPERM;
23711da177e4SLinus Torvalds 
2372f3c7691eSJ. Bruce Fields 	return 0;
23737715b521SAl Viro }
23747715b521SAl Viro 
2375e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
23767715b521SAl Viro {
2377e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
23787715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
23797715b521SAl Viro 	int error = get_write_access(inode);
23801da177e4SLinus Torvalds 	if (error)
23817715b521SAl Viro 		return error;
23821da177e4SLinus Torvalds 	/*
23831da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
23841da177e4SLinus Torvalds 	 */
23851da177e4SLinus Torvalds 	error = locks_verify_locked(inode);
2386be6d3e56SKentaro Takeda 	if (!error)
2387ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
23881da177e4SLinus Torvalds 	if (!error) {
23897715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2390d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2391e1181ee6SJeff Layton 				    filp);
23921da177e4SLinus Torvalds 	}
23931da177e4SLinus Torvalds 	put_write_access(inode);
2394acd0c935SMimi Zohar 	return error;
23951da177e4SLinus Torvalds }
23961da177e4SLinus Torvalds 
2397d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2398d57999e1SDave Hansen {
23998a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
24008a5e929dSAl Viro 		flag--;
2401d57999e1SDave Hansen 	return flag;
2402d57999e1SDave Hansen }
2403d57999e1SDave Hansen 
2404d18e9008SMiklos Szeredi static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2405d18e9008SMiklos Szeredi {
2406d18e9008SMiklos Szeredi 	int error = security_path_mknod(dir, dentry, mode, 0);
2407d18e9008SMiklos Szeredi 	if (error)
2408d18e9008SMiklos Szeredi 		return error;
2409d18e9008SMiklos Szeredi 
2410d18e9008SMiklos Szeredi 	error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2411d18e9008SMiklos Szeredi 	if (error)
2412d18e9008SMiklos Szeredi 		return error;
2413d18e9008SMiklos Szeredi 
2414d18e9008SMiklos Szeredi 	return security_inode_create(dir->dentry->d_inode, dentry, mode);
2415d18e9008SMiklos Szeredi }
2416d18e9008SMiklos Szeredi 
24171acf0af9SDavid Howells /*
24181acf0af9SDavid Howells  * Attempt to atomically look up, create and open a file from a negative
24191acf0af9SDavid Howells  * dentry.
24201acf0af9SDavid Howells  *
24211acf0af9SDavid Howells  * Returns 0 if successful.  The file will have been created and attached to
24221acf0af9SDavid Howells  * @file by the filesystem calling finish_open().
24231acf0af9SDavid Howells  *
24241acf0af9SDavid Howells  * Returns 1 if the file was looked up only or didn't need creating.  The
24251acf0af9SDavid Howells  * caller will need to perform the open themselves.  @path will have been
24261acf0af9SDavid Howells  * updated to point to the new dentry.  This may be negative.
24271acf0af9SDavid Howells  *
24281acf0af9SDavid Howells  * Returns an error code otherwise.
24291acf0af9SDavid Howells  */
24302675a4ebSAl Viro static int atomic_open(struct nameidata *nd, struct dentry *dentry,
243130d90494SAl Viro 			struct path *path, struct file *file,
2432015c3bbcSMiklos Szeredi 			const struct open_flags *op,
243364894cf8SAl Viro 			bool got_write, bool need_lookup,
243447237687SAl Viro 			int *opened)
2435d18e9008SMiklos Szeredi {
2436d18e9008SMiklos Szeredi 	struct inode *dir =  nd->path.dentry->d_inode;
2437d18e9008SMiklos Szeredi 	unsigned open_flag = open_to_namei_flags(op->open_flag);
2438d18e9008SMiklos Szeredi 	umode_t mode;
2439d18e9008SMiklos Szeredi 	int error;
2440d18e9008SMiklos Szeredi 	int acc_mode;
2441d18e9008SMiklos Szeredi 	int create_error = 0;
2442d18e9008SMiklos Szeredi 	struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2443d18e9008SMiklos Szeredi 
2444d18e9008SMiklos Szeredi 	BUG_ON(dentry->d_inode);
2445d18e9008SMiklos Szeredi 
2446d18e9008SMiklos Szeredi 	/* Don't create child dentry for a dead directory. */
2447d18e9008SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
24482675a4ebSAl Viro 		error = -ENOENT;
2449d18e9008SMiklos Szeredi 		goto out;
2450d18e9008SMiklos Szeredi 	}
2451d18e9008SMiklos Szeredi 
245262b259d8SMiklos Szeredi 	mode = op->mode;
2453d18e9008SMiklos Szeredi 	if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2454d18e9008SMiklos Szeredi 		mode &= ~current_umask();
2455d18e9008SMiklos Szeredi 
2456f8310c59SAl Viro 	if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) {
2457d18e9008SMiklos Szeredi 		open_flag &= ~O_TRUNC;
245847237687SAl Viro 		*opened |= FILE_CREATED;
2459d18e9008SMiklos Szeredi 	}
2460d18e9008SMiklos Szeredi 
2461d18e9008SMiklos Szeredi 	/*
2462d18e9008SMiklos Szeredi 	 * Checking write permission is tricky, bacuse we don't know if we are
2463d18e9008SMiklos Szeredi 	 * going to actually need it: O_CREAT opens should work as long as the
2464d18e9008SMiklos Szeredi 	 * file exists.  But checking existence breaks atomicity.  The trick is
2465d18e9008SMiklos Szeredi 	 * to check access and if not granted clear O_CREAT from the flags.
2466d18e9008SMiklos Szeredi 	 *
2467d18e9008SMiklos Szeredi 	 * Another problem is returing the "right" error value (e.g. for an
2468d18e9008SMiklos Szeredi 	 * O_EXCL open we want to return EEXIST not EROFS).
2469d18e9008SMiklos Szeredi 	 */
247064894cf8SAl Viro 	if (((open_flag & (O_CREAT | O_TRUNC)) ||
247164894cf8SAl Viro 	    (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
247264894cf8SAl Viro 		if (!(open_flag & O_CREAT)) {
2473d18e9008SMiklos Szeredi 			/*
2474d18e9008SMiklos Szeredi 			 * No O_CREATE -> atomicity not a requirement -> fall
2475d18e9008SMiklos Szeredi 			 * back to lookup + open
2476d18e9008SMiklos Szeredi 			 */
2477d18e9008SMiklos Szeredi 			goto no_open;
2478d18e9008SMiklos Szeredi 		} else if (open_flag & (O_EXCL | O_TRUNC)) {
2479d18e9008SMiklos Szeredi 			/* Fall back and fail with the right error */
248064894cf8SAl Viro 			create_error = -EROFS;
2481d18e9008SMiklos Szeredi 			goto no_open;
2482d18e9008SMiklos Szeredi 		} else {
2483d18e9008SMiklos Szeredi 			/* No side effects, safe to clear O_CREAT */
248464894cf8SAl Viro 			create_error = -EROFS;
2485d18e9008SMiklos Szeredi 			open_flag &= ~O_CREAT;
2486d18e9008SMiklos Szeredi 		}
2487d18e9008SMiklos Szeredi 	}
2488d18e9008SMiklos Szeredi 
2489d18e9008SMiklos Szeredi 	if (open_flag & O_CREAT) {
249038227f78SMiklos Szeredi 		error = may_o_create(&nd->path, dentry, mode);
2491d18e9008SMiklos Szeredi 		if (error) {
2492d18e9008SMiklos Szeredi 			create_error = error;
2493d18e9008SMiklos Szeredi 			if (open_flag & O_EXCL)
2494d18e9008SMiklos Szeredi 				goto no_open;
2495d18e9008SMiklos Szeredi 			open_flag &= ~O_CREAT;
2496d18e9008SMiklos Szeredi 		}
2497d18e9008SMiklos Szeredi 	}
2498d18e9008SMiklos Szeredi 
2499d18e9008SMiklos Szeredi 	if (nd->flags & LOOKUP_DIRECTORY)
2500d18e9008SMiklos Szeredi 		open_flag |= O_DIRECTORY;
2501d18e9008SMiklos Szeredi 
250230d90494SAl Viro 	file->f_path.dentry = DENTRY_NOT_SET;
250330d90494SAl Viro 	file->f_path.mnt = nd->path.mnt;
250430d90494SAl Viro 	error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
250547237687SAl Viro 				      opened);
2506d9585277SAl Viro 	if (error < 0) {
2507d9585277SAl Viro 		if (create_error && error == -ENOENT)
2508d9585277SAl Viro 			error = create_error;
2509d18e9008SMiklos Szeredi 		goto out;
2510d18e9008SMiklos Szeredi 	}
2511d18e9008SMiklos Szeredi 
2512d18e9008SMiklos Szeredi 	acc_mode = op->acc_mode;
251347237687SAl Viro 	if (*opened & FILE_CREATED) {
2514d18e9008SMiklos Szeredi 		fsnotify_create(dir, dentry);
2515d18e9008SMiklos Szeredi 		acc_mode = MAY_OPEN;
2516d18e9008SMiklos Szeredi 	}
2517d18e9008SMiklos Szeredi 
2518d9585277SAl Viro 	if (error) {	/* returned 1, that is */
251930d90494SAl Viro 		if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
25202675a4ebSAl Viro 			error = -EIO;
2521d18e9008SMiklos Szeredi 			goto out;
2522d18e9008SMiklos Szeredi 		}
252330d90494SAl Viro 		if (file->f_path.dentry) {
2524d18e9008SMiklos Szeredi 			dput(dentry);
252530d90494SAl Viro 			dentry = file->f_path.dentry;
2526d18e9008SMiklos Szeredi 		}
252762b2ce96SSage Weil 		if (create_error && dentry->d_inode == NULL) {
252862b2ce96SSage Weil 			error = create_error;
252962b2ce96SSage Weil 			goto out;
253062b2ce96SSage Weil 		}
2531d18e9008SMiklos Szeredi 		goto looked_up;
2532d18e9008SMiklos Szeredi 	}
2533d18e9008SMiklos Szeredi 
2534d18e9008SMiklos Szeredi 	/*
2535d18e9008SMiklos Szeredi 	 * We didn't have the inode before the open, so check open permission
2536d18e9008SMiklos Szeredi 	 * here.
2537d18e9008SMiklos Szeredi 	 */
25382675a4ebSAl Viro 	error = may_open(&file->f_path, acc_mode, open_flag);
25392675a4ebSAl Viro 	if (error)
25402675a4ebSAl Viro 		fput(file);
2541d18e9008SMiklos Szeredi 
2542d18e9008SMiklos Szeredi out:
2543d18e9008SMiklos Szeredi 	dput(dentry);
25442675a4ebSAl Viro 	return error;
2545d18e9008SMiklos Szeredi 
2546d18e9008SMiklos Szeredi no_open:
2547d18e9008SMiklos Szeredi 	if (need_lookup) {
254872bd866aSAl Viro 		dentry = lookup_real(dir, dentry, nd->flags);
2549d18e9008SMiklos Szeredi 		if (IS_ERR(dentry))
25502675a4ebSAl Viro 			return PTR_ERR(dentry);
2551d18e9008SMiklos Szeredi 
2552d18e9008SMiklos Szeredi 		if (create_error) {
2553d18e9008SMiklos Szeredi 			int open_flag = op->open_flag;
2554d18e9008SMiklos Szeredi 
25552675a4ebSAl Viro 			error = create_error;
2556d18e9008SMiklos Szeredi 			if ((open_flag & O_EXCL)) {
2557d18e9008SMiklos Szeredi 				if (!dentry->d_inode)
2558d18e9008SMiklos Szeredi 					goto out;
2559d18e9008SMiklos Szeredi 			} else if (!dentry->d_inode) {
2560d18e9008SMiklos Szeredi 				goto out;
2561d18e9008SMiklos Szeredi 			} else if ((open_flag & O_TRUNC) &&
2562d18e9008SMiklos Szeredi 				   S_ISREG(dentry->d_inode->i_mode)) {
2563d18e9008SMiklos Szeredi 				goto out;
2564d18e9008SMiklos Szeredi 			}
2565d18e9008SMiklos Szeredi 			/* will fail later, go on to get the right error */
2566d18e9008SMiklos Szeredi 		}
2567d18e9008SMiklos Szeredi 	}
2568d18e9008SMiklos Szeredi looked_up:
2569d18e9008SMiklos Szeredi 	path->dentry = dentry;
2570d18e9008SMiklos Szeredi 	path->mnt = nd->path.mnt;
25712675a4ebSAl Viro 	return 1;
2572d18e9008SMiklos Szeredi }
2573d18e9008SMiklos Szeredi 
257431e6b01fSNick Piggin /*
25751acf0af9SDavid Howells  * Look up and maybe create and open the last component.
2576d58ffd35SMiklos Szeredi  *
2577d58ffd35SMiklos Szeredi  * Must be called with i_mutex held on parent.
2578d58ffd35SMiklos Szeredi  *
25791acf0af9SDavid Howells  * Returns 0 if the file was successfully atomically created (if necessary) and
25801acf0af9SDavid Howells  * opened.  In this case the file will be returned attached to @file.
25811acf0af9SDavid Howells  *
25821acf0af9SDavid Howells  * Returns 1 if the file was not completely opened at this time, though lookups
25831acf0af9SDavid Howells  * and creations will have been performed and the dentry returned in @path will
25841acf0af9SDavid Howells  * be positive upon return if O_CREAT was specified.  If O_CREAT wasn't
25851acf0af9SDavid Howells  * specified then a negative dentry may be returned.
25861acf0af9SDavid Howells  *
25871acf0af9SDavid Howells  * An error code is returned otherwise.
25881acf0af9SDavid Howells  *
25891acf0af9SDavid Howells  * FILE_CREATE will be set in @*opened if the dentry was created and will be
25901acf0af9SDavid Howells  * cleared otherwise prior to returning.
2591d58ffd35SMiklos Szeredi  */
25922675a4ebSAl Viro static int lookup_open(struct nameidata *nd, struct path *path,
259330d90494SAl Viro 			struct file *file,
2594d58ffd35SMiklos Szeredi 			const struct open_flags *op,
259564894cf8SAl Viro 			bool got_write, int *opened)
2596d58ffd35SMiklos Szeredi {
2597d58ffd35SMiklos Szeredi 	struct dentry *dir = nd->path.dentry;
259854ef4872SMiklos Szeredi 	struct inode *dir_inode = dir->d_inode;
2599d58ffd35SMiklos Szeredi 	struct dentry *dentry;
2600d58ffd35SMiklos Szeredi 	int error;
260154ef4872SMiklos Szeredi 	bool need_lookup;
2602d58ffd35SMiklos Szeredi 
260347237687SAl Viro 	*opened &= ~FILE_CREATED;
2604201f956eSAl Viro 	dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
2605d58ffd35SMiklos Szeredi 	if (IS_ERR(dentry))
26062675a4ebSAl Viro 		return PTR_ERR(dentry);
2607d58ffd35SMiklos Szeredi 
2608d18e9008SMiklos Szeredi 	/* Cached positive dentry: will open in f_op->open */
2609d18e9008SMiklos Szeredi 	if (!need_lookup && dentry->d_inode)
2610d18e9008SMiklos Szeredi 		goto out_no_open;
2611d18e9008SMiklos Szeredi 
2612d18e9008SMiklos Szeredi 	if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
261364894cf8SAl Viro 		return atomic_open(nd, dentry, path, file, op, got_write,
261447237687SAl Viro 				   need_lookup, opened);
2615d18e9008SMiklos Szeredi 	}
2616d18e9008SMiklos Szeredi 
261754ef4872SMiklos Szeredi 	if (need_lookup) {
261854ef4872SMiklos Szeredi 		BUG_ON(dentry->d_inode);
261954ef4872SMiklos Szeredi 
262072bd866aSAl Viro 		dentry = lookup_real(dir_inode, dentry, nd->flags);
262154ef4872SMiklos Szeredi 		if (IS_ERR(dentry))
26222675a4ebSAl Viro 			return PTR_ERR(dentry);
262354ef4872SMiklos Szeredi 	}
262454ef4872SMiklos Szeredi 
2625d58ffd35SMiklos Szeredi 	/* Negative dentry, just create the file */
2626d58ffd35SMiklos Szeredi 	if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2627d58ffd35SMiklos Szeredi 		umode_t mode = op->mode;
2628d58ffd35SMiklos Szeredi 		if (!IS_POSIXACL(dir->d_inode))
2629d58ffd35SMiklos Szeredi 			mode &= ~current_umask();
2630d58ffd35SMiklos Szeredi 		/*
2631d58ffd35SMiklos Szeredi 		 * This write is needed to ensure that a
2632d58ffd35SMiklos Szeredi 		 * rw->ro transition does not occur between
2633d58ffd35SMiklos Szeredi 		 * the time when the file is created and when
2634d58ffd35SMiklos Szeredi 		 * a permanent write count is taken through
2635015c3bbcSMiklos Szeredi 		 * the 'struct file' in finish_open().
2636d58ffd35SMiklos Szeredi 		 */
263764894cf8SAl Viro 		if (!got_write) {
263864894cf8SAl Viro 			error = -EROFS;
2639d58ffd35SMiklos Szeredi 			goto out_dput;
264064894cf8SAl Viro 		}
264147237687SAl Viro 		*opened |= FILE_CREATED;
2642d58ffd35SMiklos Szeredi 		error = security_path_mknod(&nd->path, dentry, mode, 0);
2643d58ffd35SMiklos Szeredi 		if (error)
2644d58ffd35SMiklos Szeredi 			goto out_dput;
2645312b63fbSAl Viro 		error = vfs_create(dir->d_inode, dentry, mode,
2646312b63fbSAl Viro 				   nd->flags & LOOKUP_EXCL);
2647d58ffd35SMiklos Szeredi 		if (error)
2648d58ffd35SMiklos Szeredi 			goto out_dput;
2649d58ffd35SMiklos Szeredi 	}
2650d18e9008SMiklos Szeredi out_no_open:
2651d58ffd35SMiklos Szeredi 	path->dentry = dentry;
2652d58ffd35SMiklos Szeredi 	path->mnt = nd->path.mnt;
26532675a4ebSAl Viro 	return 1;
2654d58ffd35SMiklos Szeredi 
2655d58ffd35SMiklos Szeredi out_dput:
2656d58ffd35SMiklos Szeredi 	dput(dentry);
26572675a4ebSAl Viro 	return error;
2658d58ffd35SMiklos Szeredi }
2659d58ffd35SMiklos Szeredi 
2660d58ffd35SMiklos Szeredi /*
2661fe2d35ffSAl Viro  * Handle the last step of open()
266231e6b01fSNick Piggin  */
26632675a4ebSAl Viro static int do_last(struct nameidata *nd, struct path *path,
266430d90494SAl Viro 		   struct file *file, const struct open_flags *op,
2665669abf4eSJeff Layton 		   int *opened, struct filename *name)
2666fb1cc555SAl Viro {
2667a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
2668ca344a89SAl Viro 	int open_flag = op->open_flag;
266977d660a8SMiklos Szeredi 	bool will_truncate = (open_flag & O_TRUNC) != 0;
267064894cf8SAl Viro 	bool got_write = false;
2671bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2672a1eb3315SMiklos Szeredi 	struct inode *inode;
267377d660a8SMiklos Szeredi 	bool symlink_ok = false;
267416b1c1cdSMiklos Szeredi 	struct path save_parent = { .dentry = NULL, .mnt = NULL };
267516b1c1cdSMiklos Szeredi 	bool retried = false;
267616c2cd71SAl Viro 	int error;
2677669abf4eSJeff Layton 	const char *pathname = name->name;
2678fb1cc555SAl Viro 
2679c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2680c3e380b0SAl Viro 	nd->flags |= op->intent;
2681c3e380b0SAl Viro 
26821f36f774SAl Viro 	switch (nd->last_type) {
26831f36f774SAl Viro 	case LAST_DOTDOT:
2684176306f5SNeil Brown 	case LAST_DOT:
2685fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2686fe2d35ffSAl Viro 		if (error)
26872675a4ebSAl Viro 			return error;
26881f36f774SAl Viro 		/* fallthrough */
26891f36f774SAl Viro 	case LAST_ROOT:
26909f1fafeeSAl Viro 		error = complete_walk(nd);
269116c2cd71SAl Viro 		if (error)
26922675a4ebSAl Viro 			return error;
2693bfcec708SJeff Layton 		audit_inode(pathname, nd->path.dentry, 0);
2694ca344a89SAl Viro 		if (open_flag & O_CREAT) {
269516c2cd71SAl Viro 			error = -EISDIR;
26962675a4ebSAl Viro 			goto out;
2697fe2d35ffSAl Viro 		}
2698e83db167SMiklos Szeredi 		goto finish_open;
26991f36f774SAl Viro 	case LAST_BIND:
27009f1fafeeSAl Viro 		error = complete_walk(nd);
270116c2cd71SAl Viro 		if (error)
27022675a4ebSAl Viro 			return error;
2703bfcec708SJeff Layton 		audit_inode(pathname, dir, 0);
2704e83db167SMiklos Szeredi 		goto finish_open;
27051f36f774SAl Viro 	}
2706a2c36b45SAl Viro 
2707ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2708fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2709fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2710bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
271177d660a8SMiklos Szeredi 			symlink_ok = true;
2712fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2713a1eb3315SMiklos Szeredi 		error = lookup_fast(nd, &nd->last, path, &inode);
271471574865SMiklos Szeredi 		if (likely(!error))
271571574865SMiklos Szeredi 			goto finish_lookup;
271671574865SMiklos Szeredi 
2717ce57dfc1SAl Viro 		if (error < 0)
27182675a4ebSAl Viro 			goto out;
2719a1eb3315SMiklos Szeredi 
272037d7fffcSMiklos Szeredi 		BUG_ON(nd->inode != dir->d_inode);
2721b6183df7SMiklos Szeredi 	} else {
2722fe2d35ffSAl Viro 		/* create side of things */
2723a3fbbde7SAl Viro 		/*
2724b6183df7SMiklos Szeredi 		 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2725b6183df7SMiklos Szeredi 		 * has been cleared when we got to the last component we are
2726b6183df7SMiklos Szeredi 		 * about to look up
2727a3fbbde7SAl Viro 		 */
27289f1fafeeSAl Viro 		error = complete_walk(nd);
27299f1fafeeSAl Viro 		if (error)
27302675a4ebSAl Viro 			return error;
2731fe2d35ffSAl Viro 
2732bfcec708SJeff Layton 		audit_inode(pathname, dir, 0);
273316c2cd71SAl Viro 		error = -EISDIR;
27341f36f774SAl Viro 		/* trailing slashes? */
273531e6b01fSNick Piggin 		if (nd->last.name[nd->last.len])
27362675a4ebSAl Viro 			goto out;
2737b6183df7SMiklos Szeredi 	}
27381f36f774SAl Viro 
273916b1c1cdSMiklos Szeredi retry_lookup:
274064894cf8SAl Viro 	if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
274164894cf8SAl Viro 		error = mnt_want_write(nd->path.mnt);
274264894cf8SAl Viro 		if (!error)
274364894cf8SAl Viro 			got_write = true;
274464894cf8SAl Viro 		/*
274564894cf8SAl Viro 		 * do _not_ fail yet - we might not need that or fail with
274664894cf8SAl Viro 		 * a different error; let lookup_open() decide; we'll be
274764894cf8SAl Viro 		 * dropping this one anyway.
274864894cf8SAl Viro 		 */
274964894cf8SAl Viro 	}
2750a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
275164894cf8SAl Viro 	error = lookup_open(nd, path, file, op, got_write, opened);
2752fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2753fb1cc555SAl Viro 
27542675a4ebSAl Viro 	if (error <= 0) {
27552675a4ebSAl Viro 		if (error)
2756d58ffd35SMiklos Szeredi 			goto out;
27576c0d46c4SAl Viro 
275847237687SAl Viro 		if ((*opened & FILE_CREATED) ||
27592675a4ebSAl Viro 		    !S_ISREG(file->f_path.dentry->d_inode->i_mode))
276077d660a8SMiklos Szeredi 			will_truncate = false;
2761d18e9008SMiklos Szeredi 
2762bfcec708SJeff Layton 		audit_inode(pathname, file->f_path.dentry, 0);
2763d18e9008SMiklos Szeredi 		goto opened;
2764d18e9008SMiklos Szeredi 	}
2765d18e9008SMiklos Szeredi 
276647237687SAl Viro 	if (*opened & FILE_CREATED) {
27679b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2768ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
276977d660a8SMiklos Szeredi 		will_truncate = false;
2770bcda7652SAl Viro 		acc_mode = MAY_OPEN;
2771d58ffd35SMiklos Szeredi 		path_to_nameidata(path, nd);
2772e83db167SMiklos Szeredi 		goto finish_open_created;
2773fb1cc555SAl Viro 	}
2774fb1cc555SAl Viro 
2775fb1cc555SAl Viro 	/*
27763134f37eSJeff Layton 	 * create/update audit record if it already exists.
2777fb1cc555SAl Viro 	 */
27783134f37eSJeff Layton 	if (path->dentry->d_inode)
2779bfcec708SJeff Layton 		audit_inode(pathname, path->dentry, 0);
2780fb1cc555SAl Viro 
2781d18e9008SMiklos Szeredi 	/*
2782d18e9008SMiklos Szeredi 	 * If atomic_open() acquired write access it is dropped now due to
2783d18e9008SMiklos Szeredi 	 * possible mount and symlink following (this might be optimized away if
2784d18e9008SMiklos Szeredi 	 * necessary...)
2785d18e9008SMiklos Szeredi 	 */
278664894cf8SAl Viro 	if (got_write) {
2787d18e9008SMiklos Szeredi 		mnt_drop_write(nd->path.mnt);
278864894cf8SAl Viro 		got_write = false;
2789d18e9008SMiklos Szeredi 	}
2790d18e9008SMiklos Szeredi 
2791fb1cc555SAl Viro 	error = -EEXIST;
2792f8310c59SAl Viro 	if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
2793fb1cc555SAl Viro 		goto exit_dput;
2794fb1cc555SAl Viro 
27959875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
27969875cf80SDavid Howells 	if (error < 0)
2797fb1cc555SAl Viro 		goto exit_dput;
2798fb1cc555SAl Viro 
2799a3fbbde7SAl Viro 	if (error)
2800a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
2801a3fbbde7SAl Viro 
2802decf3400SMiklos Szeredi 	BUG_ON(nd->flags & LOOKUP_RCU);
2803decf3400SMiklos Szeredi 	inode = path->dentry->d_inode;
28045f5daac1SMiklos Szeredi finish_lookup:
28055f5daac1SMiklos Szeredi 	/* we _can_ be in RCU mode here */
2806fb1cc555SAl Viro 	error = -ENOENT;
280754c33e7fSMiklos Szeredi 	if (!inode) {
280854c33e7fSMiklos Szeredi 		path_to_nameidata(path, nd);
28092675a4ebSAl Viro 		goto out;
281054c33e7fSMiklos Szeredi 	}
28119e67f361SAl Viro 
2812d45ea867SMiklos Szeredi 	if (should_follow_link(inode, !symlink_ok)) {
2813d45ea867SMiklos Szeredi 		if (nd->flags & LOOKUP_RCU) {
2814d45ea867SMiklos Szeredi 			if (unlikely(unlazy_walk(nd, path->dentry))) {
2815d45ea867SMiklos Szeredi 				error = -ECHILD;
28162675a4ebSAl Viro 				goto out;
2817d45ea867SMiklos Szeredi 			}
2818d45ea867SMiklos Szeredi 		}
2819d45ea867SMiklos Szeredi 		BUG_ON(inode != path->dentry->d_inode);
28202675a4ebSAl Viro 		return 1;
2821d45ea867SMiklos Szeredi 	}
2822fb1cc555SAl Viro 
282316b1c1cdSMiklos Szeredi 	if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
2824fb1cc555SAl Viro 		path_to_nameidata(path, nd);
282516b1c1cdSMiklos Szeredi 	} else {
282616b1c1cdSMiklos Szeredi 		save_parent.dentry = nd->path.dentry;
282716b1c1cdSMiklos Szeredi 		save_parent.mnt = mntget(path->mnt);
282816b1c1cdSMiklos Szeredi 		nd->path.dentry = path->dentry;
282916b1c1cdSMiklos Szeredi 
283016b1c1cdSMiklos Szeredi 	}
2831decf3400SMiklos Szeredi 	nd->inode = inode;
2832a3fbbde7SAl Viro 	/* Why this, you ask?  _Now_ we might have grown LOOKUP_JUMPED... */
2833a3fbbde7SAl Viro 	error = complete_walk(nd);
283416b1c1cdSMiklos Szeredi 	if (error) {
283516b1c1cdSMiklos Szeredi 		path_put(&save_parent);
28362675a4ebSAl Viro 		return error;
283716b1c1cdSMiklos Szeredi 	}
2838fb1cc555SAl Viro 	error = -EISDIR;
2839050ac841SMiklos Szeredi 	if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode))
28402675a4ebSAl Viro 		goto out;
2841af2f5542SMiklos Szeredi 	error = -ENOTDIR;
2842af2f5542SMiklos Szeredi 	if ((nd->flags & LOOKUP_DIRECTORY) && !nd->inode->i_op->lookup)
28432675a4ebSAl Viro 		goto out;
2844bfcec708SJeff Layton 	audit_inode(pathname, nd->path.dentry, 0);
2845e83db167SMiklos Szeredi finish_open:
28466c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
284777d660a8SMiklos Szeredi 		will_truncate = false;
28486c0d46c4SAl Viro 
28490f9d1a10SAl Viro 	if (will_truncate) {
28500f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
28510f9d1a10SAl Viro 		if (error)
28522675a4ebSAl Viro 			goto out;
285364894cf8SAl Viro 		got_write = true;
28540f9d1a10SAl Viro 	}
2855e83db167SMiklos Szeredi finish_open_created:
2856bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
2857ca344a89SAl Viro 	if (error)
28582675a4ebSAl Viro 		goto out;
285930d90494SAl Viro 	file->f_path.mnt = nd->path.mnt;
286030d90494SAl Viro 	error = finish_open(file, nd->path.dentry, NULL, opened);
286130d90494SAl Viro 	if (error) {
286230d90494SAl Viro 		if (error == -EOPENSTALE)
2863f60dc3dbSMiklos Szeredi 			goto stale_open;
2864015c3bbcSMiklos Szeredi 		goto out;
2865f60dc3dbSMiklos Szeredi 	}
2866a8277b9bSMiklos Szeredi opened:
28672675a4ebSAl Viro 	error = open_check_o_direct(file);
2868015c3bbcSMiklos Szeredi 	if (error)
2869015c3bbcSMiklos Szeredi 		goto exit_fput;
28702675a4ebSAl Viro 	error = ima_file_check(file, op->acc_mode);
2871aa4caadbSMiklos Szeredi 	if (error)
2872aa4caadbSMiklos Szeredi 		goto exit_fput;
2873aa4caadbSMiklos Szeredi 
28740f9d1a10SAl Viro 	if (will_truncate) {
28752675a4ebSAl Viro 		error = handle_truncate(file);
2876aa4caadbSMiklos Szeredi 		if (error)
2877aa4caadbSMiklos Szeredi 			goto exit_fput;
28780f9d1a10SAl Viro 	}
2879ca344a89SAl Viro out:
288064894cf8SAl Viro 	if (got_write)
28810f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
288216b1c1cdSMiklos Szeredi 	path_put(&save_parent);
2883e276ae67SMiklos Szeredi 	terminate_walk(nd);
28842675a4ebSAl Viro 	return error;
2885fb1cc555SAl Viro 
2886fb1cc555SAl Viro exit_dput:
2887fb1cc555SAl Viro 	path_put_conditional(path, nd);
2888ca344a89SAl Viro 	goto out;
2889015c3bbcSMiklos Szeredi exit_fput:
28902675a4ebSAl Viro 	fput(file);
28912675a4ebSAl Viro 	goto out;
2892015c3bbcSMiklos Szeredi 
2893f60dc3dbSMiklos Szeredi stale_open:
2894f60dc3dbSMiklos Szeredi 	/* If no saved parent or already retried then can't retry */
2895f60dc3dbSMiklos Szeredi 	if (!save_parent.dentry || retried)
2896f60dc3dbSMiklos Szeredi 		goto out;
2897f60dc3dbSMiklos Szeredi 
2898f60dc3dbSMiklos Szeredi 	BUG_ON(save_parent.dentry != dir);
2899f60dc3dbSMiklos Szeredi 	path_put(&nd->path);
2900f60dc3dbSMiklos Szeredi 	nd->path = save_parent;
2901f60dc3dbSMiklos Szeredi 	nd->inode = dir->d_inode;
2902f60dc3dbSMiklos Szeredi 	save_parent.mnt = NULL;
2903f60dc3dbSMiklos Szeredi 	save_parent.dentry = NULL;
290464894cf8SAl Viro 	if (got_write) {
2905f60dc3dbSMiklos Szeredi 		mnt_drop_write(nd->path.mnt);
290664894cf8SAl Viro 		got_write = false;
2907f60dc3dbSMiklos Szeredi 	}
2908f60dc3dbSMiklos Szeredi 	retried = true;
2909f60dc3dbSMiklos Szeredi 	goto retry_lookup;
2910fb1cc555SAl Viro }
2911fb1cc555SAl Viro 
2912669abf4eSJeff Layton static struct file *path_openat(int dfd, struct filename *pathname,
291373d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
29141da177e4SLinus Torvalds {
2915fe2d35ffSAl Viro 	struct file *base = NULL;
291630d90494SAl Viro 	struct file *file;
29179850c056SAl Viro 	struct path path;
291847237687SAl Viro 	int opened = 0;
291913aab428SAl Viro 	int error;
292031e6b01fSNick Piggin 
292130d90494SAl Viro 	file = get_empty_filp();
292230d90494SAl Viro 	if (!file)
292331e6b01fSNick Piggin 		return ERR_PTR(-ENFILE);
292431e6b01fSNick Piggin 
292530d90494SAl Viro 	file->f_flags = op->open_flag;
292631e6b01fSNick Piggin 
2927669abf4eSJeff Layton 	error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
292831e6b01fSNick Piggin 	if (unlikely(error))
29292675a4ebSAl Viro 		goto out;
293031e6b01fSNick Piggin 
2931fe2d35ffSAl Viro 	current->total_link_count = 0;
2932669abf4eSJeff Layton 	error = link_path_walk(pathname->name, nd);
293331e6b01fSNick Piggin 	if (unlikely(error))
29342675a4ebSAl Viro 		goto out;
29351da177e4SLinus Torvalds 
29362675a4ebSAl Viro 	error = do_last(nd, &path, file, op, &opened, pathname);
29372675a4ebSAl Viro 	while (unlikely(error > 0)) { /* trailing symlink */
29387b9337aaSNick Piggin 		struct path link = path;
2939def4af30SAl Viro 		void *cookie;
2940574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
294173d049a4SAl Viro 			path_put_conditional(&path, nd);
294273d049a4SAl Viro 			path_put(&nd->path);
29432675a4ebSAl Viro 			error = -ELOOP;
294440b39136SAl Viro 			break;
294540b39136SAl Viro 		}
2946800179c9SKees Cook 		error = may_follow_link(&link, nd);
2947800179c9SKees Cook 		if (unlikely(error))
2948800179c9SKees Cook 			break;
294973d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
295073d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
2951574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
2952c3e380b0SAl Viro 		if (unlikely(error))
29532675a4ebSAl Viro 			break;
29542675a4ebSAl Viro 		error = do_last(nd, &path, file, op, &opened, pathname);
2955574197e0SAl Viro 		put_link(nd, &link, cookie);
2956806b681cSAl Viro 	}
295710fa8e62SAl Viro out:
295873d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
295973d049a4SAl Viro 		path_put(&nd->root);
2960fe2d35ffSAl Viro 	if (base)
2961fe2d35ffSAl Viro 		fput(base);
29622675a4ebSAl Viro 	if (!(opened & FILE_OPENED)) {
29632675a4ebSAl Viro 		BUG_ON(!error);
296430d90494SAl Viro 		put_filp(file);
2965015c3bbcSMiklos Szeredi 	}
29662675a4ebSAl Viro 	if (unlikely(error)) {
29672675a4ebSAl Viro 		if (error == -EOPENSTALE) {
29682675a4ebSAl Viro 			if (flags & LOOKUP_RCU)
29692675a4ebSAl Viro 				error = -ECHILD;
29702675a4ebSAl Viro 			else
29712675a4ebSAl Viro 				error = -ESTALE;
29722675a4ebSAl Viro 		}
29732675a4ebSAl Viro 		file = ERR_PTR(error);
29742675a4ebSAl Viro 	}
29752675a4ebSAl Viro 	return file;
2976de459215SKirill Korotaev }
29771da177e4SLinus Torvalds 
2978669abf4eSJeff Layton struct file *do_filp_open(int dfd, struct filename *pathname,
297913aab428SAl Viro 		const struct open_flags *op, int flags)
298013aab428SAl Viro {
298173d049a4SAl Viro 	struct nameidata nd;
298213aab428SAl Viro 	struct file *filp;
298313aab428SAl Viro 
298473d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
298513aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
298673d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
298713aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
298873d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
298913aab428SAl Viro 	return filp;
299013aab428SAl Viro }
299113aab428SAl Viro 
299273d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
299373d049a4SAl Viro 		const char *name, const struct open_flags *op, int flags)
299473d049a4SAl Viro {
299573d049a4SAl Viro 	struct nameidata nd;
299673d049a4SAl Viro 	struct file *file;
2997669abf4eSJeff Layton 	struct filename filename = { .name = name };
299873d049a4SAl Viro 
299973d049a4SAl Viro 	nd.root.mnt = mnt;
300073d049a4SAl Viro 	nd.root.dentry = dentry;
300173d049a4SAl Viro 
300273d049a4SAl Viro 	flags |= LOOKUP_ROOT;
300373d049a4SAl Viro 
3004bcda7652SAl Viro 	if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
300573d049a4SAl Viro 		return ERR_PTR(-ELOOP);
300673d049a4SAl Viro 
3007669abf4eSJeff Layton 	file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
300873d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
3009669abf4eSJeff Layton 		file = path_openat(-1, &filename, &nd, op, flags);
301073d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
3011669abf4eSJeff Layton 		file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
301273d049a4SAl Viro 	return file;
301373d049a4SAl Viro }
301473d049a4SAl Viro 
3015ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir)
30161da177e4SLinus Torvalds {
3017c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
3018ed75e95dSAl Viro 	struct nameidata nd;
3019c30dabfeSJan Kara 	int err2;
3020ed75e95dSAl Viro 	int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
3021ed75e95dSAl Viro 	if (error)
3022ed75e95dSAl Viro 		return ERR_PTR(error);
30231da177e4SLinus Torvalds 
3024c663e5d8SChristoph Hellwig 	/*
3025c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
3026c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
3027c663e5d8SChristoph Hellwig 	 */
3028ed75e95dSAl Viro 	if (nd.last_type != LAST_NORM)
3029ed75e95dSAl Viro 		goto out;
3030ed75e95dSAl Viro 	nd.flags &= ~LOOKUP_PARENT;
3031ed75e95dSAl Viro 	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3032c663e5d8SChristoph Hellwig 
3033c30dabfeSJan Kara 	/* don't fail immediately if it's r/o, at least try to report other errors */
3034c30dabfeSJan Kara 	err2 = mnt_want_write(nd.path.mnt);
3035c663e5d8SChristoph Hellwig 	/*
3036c663e5d8SChristoph Hellwig 	 * Do the final lookup.
3037c663e5d8SChristoph Hellwig 	 */
3038ed75e95dSAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3039ed75e95dSAl Viro 	dentry = lookup_hash(&nd);
30401da177e4SLinus Torvalds 	if (IS_ERR(dentry))
3041a8104a9fSAl Viro 		goto unlock;
3042c663e5d8SChristoph Hellwig 
3043a8104a9fSAl Viro 	error = -EEXIST;
3044e9baf6e5SAl Viro 	if (dentry->d_inode)
3045a8104a9fSAl Viro 		goto fail;
3046c663e5d8SChristoph Hellwig 	/*
3047c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
3048c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
3049c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
3050c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
3051c663e5d8SChristoph Hellwig 	 */
3052ed75e95dSAl Viro 	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
3053a8104a9fSAl Viro 		error = -ENOENT;
3054ed75e95dSAl Viro 		goto fail;
3055e9baf6e5SAl Viro 	}
3056c30dabfeSJan Kara 	if (unlikely(err2)) {
3057c30dabfeSJan Kara 		error = err2;
3058a8104a9fSAl Viro 		goto fail;
3059c30dabfeSJan Kara 	}
3060ed75e95dSAl Viro 	*path = nd.path;
3061e9baf6e5SAl Viro 	return dentry;
30621da177e4SLinus Torvalds fail:
3063a8104a9fSAl Viro 	dput(dentry);
3064a8104a9fSAl Viro 	dentry = ERR_PTR(error);
3065a8104a9fSAl Viro unlock:
3066dae6ad8fSAl Viro 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3067c30dabfeSJan Kara 	if (!err2)
3068c30dabfeSJan Kara 		mnt_drop_write(nd.path.mnt);
3069ed75e95dSAl Viro out:
3070dae6ad8fSAl Viro 	path_put(&nd.path);
3071ed75e95dSAl Viro 	return dentry;
3072dae6ad8fSAl Viro }
3073dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create);
3074dae6ad8fSAl Viro 
3075921a1650SAl Viro void done_path_create(struct path *path, struct dentry *dentry)
3076921a1650SAl Viro {
3077921a1650SAl Viro 	dput(dentry);
3078921a1650SAl Viro 	mutex_unlock(&path->dentry->d_inode->i_mutex);
3079a8104a9fSAl Viro 	mnt_drop_write(path->mnt);
3080921a1650SAl Viro 	path_put(path);
3081921a1650SAl Viro }
3082921a1650SAl Viro EXPORT_SYMBOL(done_path_create);
3083921a1650SAl Viro 
3084dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir)
3085dae6ad8fSAl Viro {
308691a27b2aSJeff Layton 	struct filename *tmp = getname(pathname);
3087dae6ad8fSAl Viro 	struct dentry *res;
3088dae6ad8fSAl Viro 	if (IS_ERR(tmp))
3089dae6ad8fSAl Viro 		return ERR_CAST(tmp);
309091a27b2aSJeff Layton 	res = kern_path_create(dfd, tmp->name, path, is_dir);
3091dae6ad8fSAl Viro 	putname(tmp);
3092dae6ad8fSAl Viro 	return res;
3093dae6ad8fSAl Viro }
3094dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create);
3095dae6ad8fSAl Viro 
30961a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
30971da177e4SLinus Torvalds {
3098a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
30991da177e4SLinus Torvalds 
31001da177e4SLinus Torvalds 	if (error)
31011da177e4SLinus Torvalds 		return error;
31021da177e4SLinus Torvalds 
3103975d6b39SEric W. Biederman 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
31041da177e4SLinus Torvalds 		return -EPERM;
31051da177e4SLinus Torvalds 
3106acfa4380SAl Viro 	if (!dir->i_op->mknod)
31071da177e4SLinus Torvalds 		return -EPERM;
31081da177e4SLinus Torvalds 
310908ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
311008ce5f16SSerge E. Hallyn 	if (error)
311108ce5f16SSerge E. Hallyn 		return error;
311208ce5f16SSerge E. Hallyn 
31131da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
31141da177e4SLinus Torvalds 	if (error)
31151da177e4SLinus Torvalds 		return error;
31161da177e4SLinus Torvalds 
31171da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
3118a74574aaSStephen Smalley 	if (!error)
3119f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
31201da177e4SLinus Torvalds 	return error;
31211da177e4SLinus Torvalds }
31221da177e4SLinus Torvalds 
3123f69aac00SAl Viro static int may_mknod(umode_t mode)
3124463c3197SDave Hansen {
3125463c3197SDave Hansen 	switch (mode & S_IFMT) {
3126463c3197SDave Hansen 	case S_IFREG:
3127463c3197SDave Hansen 	case S_IFCHR:
3128463c3197SDave Hansen 	case S_IFBLK:
3129463c3197SDave Hansen 	case S_IFIFO:
3130463c3197SDave Hansen 	case S_IFSOCK:
3131463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
3132463c3197SDave Hansen 		return 0;
3133463c3197SDave Hansen 	case S_IFDIR:
3134463c3197SDave Hansen 		return -EPERM;
3135463c3197SDave Hansen 	default:
3136463c3197SDave Hansen 		return -EINVAL;
3137463c3197SDave Hansen 	}
3138463c3197SDave Hansen }
3139463c3197SDave Hansen 
31408208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
31412e4d0924SHeiko Carstens 		unsigned, dev)
31421da177e4SLinus Torvalds {
31431da177e4SLinus Torvalds 	struct dentry *dentry;
3144dae6ad8fSAl Viro 	struct path path;
3145dae6ad8fSAl Viro 	int error;
31461da177e4SLinus Torvalds 
31478e4bfca1SAl Viro 	error = may_mknod(mode);
31488e4bfca1SAl Viro 	if (error)
31498e4bfca1SAl Viro 		return error;
31501da177e4SLinus Torvalds 
3151dae6ad8fSAl Viro 	dentry = user_path_create(dfd, filename, &path, 0);
3152dae6ad8fSAl Viro 	if (IS_ERR(dentry))
3153dae6ad8fSAl Viro 		return PTR_ERR(dentry);
31542ad94ae6SAl Viro 
3155dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3156ce3b0f8dSAl Viro 		mode &= ~current_umask();
3157dae6ad8fSAl Viro 	error = security_path_mknod(&path, dentry, mode, dev);
3158be6d3e56SKentaro Takeda 	if (error)
3159a8104a9fSAl Viro 		goto out;
31601da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
31611da177e4SLinus Torvalds 		case 0: case S_IFREG:
3162312b63fbSAl Viro 			error = vfs_create(path.dentry->d_inode,dentry,mode,true);
31631da177e4SLinus Torvalds 			break;
31641da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
3165dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
31661da177e4SLinus Torvalds 					new_decode_dev(dev));
31671da177e4SLinus Torvalds 			break;
31681da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
3169dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
31701da177e4SLinus Torvalds 			break;
31711da177e4SLinus Torvalds 	}
3172a8104a9fSAl Viro out:
3173921a1650SAl Viro 	done_path_create(&path, dentry);
31741da177e4SLinus Torvalds 	return error;
31751da177e4SLinus Torvalds }
31761da177e4SLinus Torvalds 
31778208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
31785590ff0dSUlrich Drepper {
31795590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
31805590ff0dSUlrich Drepper }
31815590ff0dSUlrich Drepper 
318218bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
31831da177e4SLinus Torvalds {
3184a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
31858de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
31861da177e4SLinus Torvalds 
31871da177e4SLinus Torvalds 	if (error)
31881da177e4SLinus Torvalds 		return error;
31891da177e4SLinus Torvalds 
3190acfa4380SAl Viro 	if (!dir->i_op->mkdir)
31911da177e4SLinus Torvalds 		return -EPERM;
31921da177e4SLinus Torvalds 
31931da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
31941da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
31951da177e4SLinus Torvalds 	if (error)
31961da177e4SLinus Torvalds 		return error;
31971da177e4SLinus Torvalds 
31988de52778SAl Viro 	if (max_links && dir->i_nlink >= max_links)
31998de52778SAl Viro 		return -EMLINK;
32008de52778SAl Viro 
32011da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
3202a74574aaSStephen Smalley 	if (!error)
3203f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
32041da177e4SLinus Torvalds 	return error;
32051da177e4SLinus Torvalds }
32061da177e4SLinus Torvalds 
3207a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
32081da177e4SLinus Torvalds {
32096902d925SDave Hansen 	struct dentry *dentry;
3210dae6ad8fSAl Viro 	struct path path;
3211dae6ad8fSAl Viro 	int error;
32121da177e4SLinus Torvalds 
3213dae6ad8fSAl Viro 	dentry = user_path_create(dfd, pathname, &path, 1);
32146902d925SDave Hansen 	if (IS_ERR(dentry))
3215dae6ad8fSAl Viro 		return PTR_ERR(dentry);
32166902d925SDave Hansen 
3217dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3218ce3b0f8dSAl Viro 		mode &= ~current_umask();
3219dae6ad8fSAl Viro 	error = security_path_mkdir(&path, dentry, mode);
3220a8104a9fSAl Viro 	if (!error)
3221dae6ad8fSAl Viro 		error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3222921a1650SAl Viro 	done_path_create(&path, dentry);
32231da177e4SLinus Torvalds 	return error;
32241da177e4SLinus Torvalds }
32251da177e4SLinus Torvalds 
3226a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
32275590ff0dSUlrich Drepper {
32285590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
32295590ff0dSUlrich Drepper }
32305590ff0dSUlrich Drepper 
32311da177e4SLinus Torvalds /*
3232a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
3233c0d02594SJ. Bruce Fields  * should have a usage count of 1 if we're the only user of this
3234a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
3235a71905f0SSage Weil  * then we drop the dentry now.
32361da177e4SLinus Torvalds  *
32371da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
32381da177e4SLinus Torvalds  * do a
32391da177e4SLinus Torvalds  *
32401da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
32411da177e4SLinus Torvalds  *		return -EBUSY;
32421da177e4SLinus Torvalds  *
32431da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
32441da177e4SLinus Torvalds  * that is still in use by something else..
32451da177e4SLinus Torvalds  */
32461da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
32471da177e4SLinus Torvalds {
32481da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
32491da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
325064252c75SSage Weil 	if (dentry->d_count == 1)
32511da177e4SLinus Torvalds 		__d_drop(dentry);
32521da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
32531da177e4SLinus Torvalds }
32541da177e4SLinus Torvalds 
32551da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
32561da177e4SLinus Torvalds {
32571da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
32581da177e4SLinus Torvalds 
32591da177e4SLinus Torvalds 	if (error)
32601da177e4SLinus Torvalds 		return error;
32611da177e4SLinus Torvalds 
3262acfa4380SAl Viro 	if (!dir->i_op->rmdir)
32631da177e4SLinus Torvalds 		return -EPERM;
32641da177e4SLinus Torvalds 
32651d2ef590SAl Viro 	dget(dentry);
32661b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
3267912dbc15SSage Weil 
32681da177e4SLinus Torvalds 	error = -EBUSY;
3269912dbc15SSage Weil 	if (d_mountpoint(dentry))
3270912dbc15SSage Weil 		goto out;
3271912dbc15SSage Weil 
32721da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
3273912dbc15SSage Weil 	if (error)
3274912dbc15SSage Weil 		goto out;
3275912dbc15SSage Weil 
32763cebde24SSage Weil 	shrink_dcache_parent(dentry);
32771da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
3278912dbc15SSage Weil 	if (error)
3279912dbc15SSage Weil 		goto out;
3280912dbc15SSage Weil 
32811da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
3282d83c49f3SAl Viro 	dont_mount(dentry);
32831da177e4SLinus Torvalds 
3284912dbc15SSage Weil out:
3285912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
32861d2ef590SAl Viro 	dput(dentry);
3287912dbc15SSage Weil 	if (!error)
3288912dbc15SSage Weil 		d_delete(dentry);
32891da177e4SLinus Torvalds 	return error;
32901da177e4SLinus Torvalds }
32911da177e4SLinus Torvalds 
32925590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
32931da177e4SLinus Torvalds {
32941da177e4SLinus Torvalds 	int error = 0;
329591a27b2aSJeff Layton 	struct filename *name;
32961da177e4SLinus Torvalds 	struct dentry *dentry;
32971da177e4SLinus Torvalds 	struct nameidata nd;
32981da177e4SLinus Torvalds 
329991a27b2aSJeff Layton 	name = user_path_parent(dfd, pathname, &nd);
330091a27b2aSJeff Layton 	if (IS_ERR(name))
330191a27b2aSJeff Layton 		return PTR_ERR(name);
33021da177e4SLinus Torvalds 
33031da177e4SLinus Torvalds 	switch(nd.last_type) {
33041da177e4SLinus Torvalds 	case LAST_DOTDOT:
33051da177e4SLinus Torvalds 		error = -ENOTEMPTY;
33061da177e4SLinus Torvalds 		goto exit1;
33071da177e4SLinus Torvalds 	case LAST_DOT:
33081da177e4SLinus Torvalds 		error = -EINVAL;
33091da177e4SLinus Torvalds 		goto exit1;
33101da177e4SLinus Torvalds 	case LAST_ROOT:
33111da177e4SLinus Torvalds 		error = -EBUSY;
33121da177e4SLinus Torvalds 		goto exit1;
33131da177e4SLinus Torvalds 	}
33140612d9fbSOGAWA Hirofumi 
33150612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
3316c30dabfeSJan Kara 	error = mnt_want_write(nd.path.mnt);
3317c30dabfeSJan Kara 	if (error)
3318c30dabfeSJan Kara 		goto exit1;
33190612d9fbSOGAWA Hirofumi 
33204ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
332149705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
33221da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
33236902d925SDave Hansen 	if (IS_ERR(dentry))
33246902d925SDave Hansen 		goto exit2;
3325e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
3326e6bc45d6STheodore Ts'o 		error = -ENOENT;
3327e6bc45d6STheodore Ts'o 		goto exit3;
3328e6bc45d6STheodore Ts'o 	}
3329be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
3330be6d3e56SKentaro Takeda 	if (error)
3331c30dabfeSJan Kara 		goto exit3;
33324ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
33330622753bSDave Hansen exit3:
33341da177e4SLinus Torvalds 	dput(dentry);
33356902d925SDave Hansen exit2:
33364ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3337c30dabfeSJan Kara 	mnt_drop_write(nd.path.mnt);
33381da177e4SLinus Torvalds exit1:
33391d957f9bSJan Blunck 	path_put(&nd.path);
33401da177e4SLinus Torvalds 	putname(name);
33411da177e4SLinus Torvalds 	return error;
33421da177e4SLinus Torvalds }
33431da177e4SLinus Torvalds 
33443cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
33455590ff0dSUlrich Drepper {
33465590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
33475590ff0dSUlrich Drepper }
33485590ff0dSUlrich Drepper 
33491da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
33501da177e4SLinus Torvalds {
33511da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
33521da177e4SLinus Torvalds 
33531da177e4SLinus Torvalds 	if (error)
33541da177e4SLinus Torvalds 		return error;
33551da177e4SLinus Torvalds 
3356acfa4380SAl Viro 	if (!dir->i_op->unlink)
33571da177e4SLinus Torvalds 		return -EPERM;
33581da177e4SLinus Torvalds 
33591b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
33601da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
33611da177e4SLinus Torvalds 		error = -EBUSY;
33621da177e4SLinus Torvalds 	else {
33631da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
3364bec1052eSAl Viro 		if (!error) {
33651da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
3366bec1052eSAl Viro 			if (!error)
3367d83c49f3SAl Viro 				dont_mount(dentry);
3368bec1052eSAl Viro 		}
33691da177e4SLinus Torvalds 	}
33701b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
33711da177e4SLinus Torvalds 
33721da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
33731da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
3374ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
33751da177e4SLinus Torvalds 		d_delete(dentry);
33761da177e4SLinus Torvalds 	}
33770eeca283SRobert Love 
33781da177e4SLinus Torvalds 	return error;
33791da177e4SLinus Torvalds }
33801da177e4SLinus Torvalds 
33811da177e4SLinus Torvalds /*
33821da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
33831b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
33841da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
33851da177e4SLinus Torvalds  * while waiting on the I/O.
33861da177e4SLinus Torvalds  */
33875590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
33881da177e4SLinus Torvalds {
33892ad94ae6SAl Viro 	int error;
339091a27b2aSJeff Layton 	struct filename *name;
33911da177e4SLinus Torvalds 	struct dentry *dentry;
33921da177e4SLinus Torvalds 	struct nameidata nd;
33931da177e4SLinus Torvalds 	struct inode *inode = NULL;
33941da177e4SLinus Torvalds 
339591a27b2aSJeff Layton 	name = user_path_parent(dfd, pathname, &nd);
339691a27b2aSJeff Layton 	if (IS_ERR(name))
339791a27b2aSJeff Layton 		return PTR_ERR(name);
33982ad94ae6SAl Viro 
33991da177e4SLinus Torvalds 	error = -EISDIR;
34001da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
34011da177e4SLinus Torvalds 		goto exit1;
34020612d9fbSOGAWA Hirofumi 
34030612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
3404c30dabfeSJan Kara 	error = mnt_want_write(nd.path.mnt);
3405c30dabfeSJan Kara 	if (error)
3406c30dabfeSJan Kara 		goto exit1;
34070612d9fbSOGAWA Hirofumi 
34084ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
340949705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
34101da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
34111da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
34121da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
341350338b88STörök Edwin 		if (nd.last.name[nd.last.len])
341450338b88STörök Edwin 			goto slashes;
34151da177e4SLinus Torvalds 		inode = dentry->d_inode;
341650338b88STörök Edwin 		if (!inode)
3417e6bc45d6STheodore Ts'o 			goto slashes;
34187de9c6eeSAl Viro 		ihold(inode);
3419be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
3420be6d3e56SKentaro Takeda 		if (error)
3421c30dabfeSJan Kara 			goto exit2;
34224ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
34231da177e4SLinus Torvalds exit2:
34241da177e4SLinus Torvalds 		dput(dentry);
34251da177e4SLinus Torvalds 	}
34264ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
34271da177e4SLinus Torvalds 	if (inode)
34281da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
3429c30dabfeSJan Kara 	mnt_drop_write(nd.path.mnt);
34301da177e4SLinus Torvalds exit1:
34311d957f9bSJan Blunck 	path_put(&nd.path);
34321da177e4SLinus Torvalds 	putname(name);
34331da177e4SLinus Torvalds 	return error;
34341da177e4SLinus Torvalds 
34351da177e4SLinus Torvalds slashes:
34361da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
34371da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
34381da177e4SLinus Torvalds 	goto exit2;
34391da177e4SLinus Torvalds }
34401da177e4SLinus Torvalds 
34412e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
34425590ff0dSUlrich Drepper {
34435590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
34445590ff0dSUlrich Drepper 		return -EINVAL;
34455590ff0dSUlrich Drepper 
34465590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
34475590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
34485590ff0dSUlrich Drepper 
34495590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
34505590ff0dSUlrich Drepper }
34515590ff0dSUlrich Drepper 
34523480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
34535590ff0dSUlrich Drepper {
34545590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
34555590ff0dSUlrich Drepper }
34565590ff0dSUlrich Drepper 
3457db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
34581da177e4SLinus Torvalds {
3459a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
34601da177e4SLinus Torvalds 
34611da177e4SLinus Torvalds 	if (error)
34621da177e4SLinus Torvalds 		return error;
34631da177e4SLinus Torvalds 
3464acfa4380SAl Viro 	if (!dir->i_op->symlink)
34651da177e4SLinus Torvalds 		return -EPERM;
34661da177e4SLinus Torvalds 
34671da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
34681da177e4SLinus Torvalds 	if (error)
34691da177e4SLinus Torvalds 		return error;
34701da177e4SLinus Torvalds 
34711da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
3472a74574aaSStephen Smalley 	if (!error)
3473f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
34741da177e4SLinus Torvalds 	return error;
34751da177e4SLinus Torvalds }
34761da177e4SLinus Torvalds 
34772e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
34782e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
34791da177e4SLinus Torvalds {
34802ad94ae6SAl Viro 	int error;
348191a27b2aSJeff Layton 	struct filename *from;
34826902d925SDave Hansen 	struct dentry *dentry;
3483dae6ad8fSAl Viro 	struct path path;
34841da177e4SLinus Torvalds 
34851da177e4SLinus Torvalds 	from = getname(oldname);
34861da177e4SLinus Torvalds 	if (IS_ERR(from))
34871da177e4SLinus Torvalds 		return PTR_ERR(from);
34882ad94ae6SAl Viro 
3489dae6ad8fSAl Viro 	dentry = user_path_create(newdfd, newname, &path, 0);
34901da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
34916902d925SDave Hansen 	if (IS_ERR(dentry))
3492dae6ad8fSAl Viro 		goto out_putname;
34936902d925SDave Hansen 
349491a27b2aSJeff Layton 	error = security_path_symlink(&path, dentry, from->name);
3495a8104a9fSAl Viro 	if (!error)
349691a27b2aSJeff Layton 		error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
3497921a1650SAl Viro 	done_path_create(&path, dentry);
34986902d925SDave Hansen out_putname:
34991da177e4SLinus Torvalds 	putname(from);
35001da177e4SLinus Torvalds 	return error;
35011da177e4SLinus Torvalds }
35021da177e4SLinus Torvalds 
35033480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
35045590ff0dSUlrich Drepper {
35055590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
35065590ff0dSUlrich Drepper }
35075590ff0dSUlrich Drepper 
35081da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
35091da177e4SLinus Torvalds {
35101da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
35118de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
35121da177e4SLinus Torvalds 	int error;
35131da177e4SLinus Torvalds 
35141da177e4SLinus Torvalds 	if (!inode)
35151da177e4SLinus Torvalds 		return -ENOENT;
35161da177e4SLinus Torvalds 
3517a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
35181da177e4SLinus Torvalds 	if (error)
35191da177e4SLinus Torvalds 		return error;
35201da177e4SLinus Torvalds 
35211da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
35221da177e4SLinus Torvalds 		return -EXDEV;
35231da177e4SLinus Torvalds 
35241da177e4SLinus Torvalds 	/*
35251da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
35261da177e4SLinus Torvalds 	 */
35271da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
35281da177e4SLinus Torvalds 		return -EPERM;
3529acfa4380SAl Viro 	if (!dir->i_op->link)
35301da177e4SLinus Torvalds 		return -EPERM;
35317e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
35321da177e4SLinus Torvalds 		return -EPERM;
35331da177e4SLinus Torvalds 
35341da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
35351da177e4SLinus Torvalds 	if (error)
35361da177e4SLinus Torvalds 		return error;
35371da177e4SLinus Torvalds 
35387e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
3539aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
3540aae8a97dSAneesh Kumar K.V 	if (inode->i_nlink == 0)
3541aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
35428de52778SAl Viro 	else if (max_links && inode->i_nlink >= max_links)
35438de52778SAl Viro 		error = -EMLINK;
3544aae8a97dSAneesh Kumar K.V 	else
35451da177e4SLinus Torvalds 		error = dir->i_op->link(old_dentry, dir, new_dentry);
35467e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
3547e31e14ecSStephen Smalley 	if (!error)
35487e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
35491da177e4SLinus Torvalds 	return error;
35501da177e4SLinus Torvalds }
35511da177e4SLinus Torvalds 
35521da177e4SLinus Torvalds /*
35531da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
35541da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
35551da177e4SLinus Torvalds  * newname.  --KAB
35561da177e4SLinus Torvalds  *
35571da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
35581da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
35591da177e4SLinus Torvalds  * and other special files.  --ADM
35601da177e4SLinus Torvalds  */
35612e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
35622e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
35631da177e4SLinus Torvalds {
35641da177e4SLinus Torvalds 	struct dentry *new_dentry;
3565dae6ad8fSAl Viro 	struct path old_path, new_path;
356611a7b371SAneesh Kumar K.V 	int how = 0;
35671da177e4SLinus Torvalds 	int error;
35681da177e4SLinus Torvalds 
356911a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3570c04030e1SUlrich Drepper 		return -EINVAL;
357111a7b371SAneesh Kumar K.V 	/*
357211a7b371SAneesh Kumar K.V 	 * To use null names we require CAP_DAC_READ_SEARCH
357311a7b371SAneesh Kumar K.V 	 * This ensures that not everyone will be able to create
357411a7b371SAneesh Kumar K.V 	 * handlink using the passed filedescriptor.
357511a7b371SAneesh Kumar K.V 	 */
357611a7b371SAneesh Kumar K.V 	if (flags & AT_EMPTY_PATH) {
357711a7b371SAneesh Kumar K.V 		if (!capable(CAP_DAC_READ_SEARCH))
357811a7b371SAneesh Kumar K.V 			return -ENOENT;
357911a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
358011a7b371SAneesh Kumar K.V 	}
3581c04030e1SUlrich Drepper 
358211a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
358311a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
358411a7b371SAneesh Kumar K.V 
358511a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
35861da177e4SLinus Torvalds 	if (error)
35872ad94ae6SAl Viro 		return error;
35882ad94ae6SAl Viro 
3589dae6ad8fSAl Viro 	new_dentry = user_path_create(newdfd, newname, &new_path, 0);
35901da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
35916902d925SDave Hansen 	if (IS_ERR(new_dentry))
3592dae6ad8fSAl Viro 		goto out;
3593dae6ad8fSAl Viro 
3594dae6ad8fSAl Viro 	error = -EXDEV;
3595dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt)
3596dae6ad8fSAl Viro 		goto out_dput;
3597800179c9SKees Cook 	error = may_linkat(&old_path);
3598800179c9SKees Cook 	if (unlikely(error))
3599800179c9SKees Cook 		goto out_dput;
3600dae6ad8fSAl Viro 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
3601be6d3e56SKentaro Takeda 	if (error)
3602a8104a9fSAl Viro 		goto out_dput;
3603dae6ad8fSAl Viro 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry);
360475c3f29dSDave Hansen out_dput:
3605921a1650SAl Viro 	done_path_create(&new_path, new_dentry);
36061da177e4SLinus Torvalds out:
36072d8f3038SAl Viro 	path_put(&old_path);
36081da177e4SLinus Torvalds 
36091da177e4SLinus Torvalds 	return error;
36101da177e4SLinus Torvalds }
36111da177e4SLinus Torvalds 
36123480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
36135590ff0dSUlrich Drepper {
3614c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
36155590ff0dSUlrich Drepper }
36165590ff0dSUlrich Drepper 
36171da177e4SLinus Torvalds /*
36181da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
36191da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
36201da177e4SLinus Torvalds  * Problems:
36211da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
36221da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
36231da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
3624a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
36251da177e4SLinus Torvalds  *	   story.
36261da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
36271b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
36281da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
36291da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
3630a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
36311da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
36321da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
36331da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
3634a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
36351da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
36361da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
36371da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
3638e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
36391b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
36401da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
3641c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
36421da177e4SLinus Torvalds  *	   locking].
36431da177e4SLinus Torvalds  */
364475c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
36451da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
36461da177e4SLinus Torvalds {
36471da177e4SLinus Torvalds 	int error = 0;
36489055cba7SSage Weil 	struct inode *target = new_dentry->d_inode;
36498de52778SAl Viro 	unsigned max_links = new_dir->i_sb->s_max_links;
36501da177e4SLinus Torvalds 
36511da177e4SLinus Torvalds 	/*
36521da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
36531da177e4SLinus Torvalds 	 * we'll need to flip '..'.
36541da177e4SLinus Torvalds 	 */
36551da177e4SLinus Torvalds 	if (new_dir != old_dir) {
3656f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
36571da177e4SLinus Torvalds 		if (error)
36581da177e4SLinus Torvalds 			return error;
36591da177e4SLinus Torvalds 	}
36601da177e4SLinus Torvalds 
36611da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
36621da177e4SLinus Torvalds 	if (error)
36631da177e4SLinus Torvalds 		return error;
36641da177e4SLinus Torvalds 
36651d2ef590SAl Viro 	dget(new_dentry);
3666d83c49f3SAl Viro 	if (target)
36671b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
36689055cba7SSage Weil 
36691da177e4SLinus Torvalds 	error = -EBUSY;
36709055cba7SSage Weil 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
36719055cba7SSage Weil 		goto out;
36729055cba7SSage Weil 
36738de52778SAl Viro 	error = -EMLINK;
36748de52778SAl Viro 	if (max_links && !target && new_dir != old_dir &&
36758de52778SAl Viro 	    new_dir->i_nlink >= max_links)
36768de52778SAl Viro 		goto out;
36778de52778SAl Viro 
36783cebde24SSage Weil 	if (target)
36793cebde24SSage Weil 		shrink_dcache_parent(new_dentry);
36801da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
36819055cba7SSage Weil 	if (error)
36829055cba7SSage Weil 		goto out;
36839055cba7SSage Weil 
36841da177e4SLinus Torvalds 	if (target) {
36851da177e4SLinus Torvalds 		target->i_flags |= S_DEAD;
3686d83c49f3SAl Viro 		dont_mount(new_dentry);
3687d83c49f3SAl Viro 	}
36889055cba7SSage Weil out:
36899055cba7SSage Weil 	if (target)
36901b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
36911d2ef590SAl Viro 	dput(new_dentry);
3692e31e14ecSStephen Smalley 	if (!error)
3693349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
36941da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
36951da177e4SLinus Torvalds 	return error;
36961da177e4SLinus Torvalds }
36971da177e4SLinus Torvalds 
369875c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
36991da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
37001da177e4SLinus Torvalds {
370151892bbbSSage Weil 	struct inode *target = new_dentry->d_inode;
37021da177e4SLinus Torvalds 	int error;
37031da177e4SLinus Torvalds 
37041da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
37051da177e4SLinus Torvalds 	if (error)
37061da177e4SLinus Torvalds 		return error;
37071da177e4SLinus Torvalds 
37081da177e4SLinus Torvalds 	dget(new_dentry);
37091da177e4SLinus Torvalds 	if (target)
37101b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
371151892bbbSSage Weil 
37121da177e4SLinus Torvalds 	error = -EBUSY;
371351892bbbSSage Weil 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
371451892bbbSSage Weil 		goto out;
371551892bbbSSage Weil 
37161da177e4SLinus Torvalds 	error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
371751892bbbSSage Weil 	if (error)
371851892bbbSSage Weil 		goto out;
371951892bbbSSage Weil 
3720bec1052eSAl Viro 	if (target)
3721d83c49f3SAl Viro 		dont_mount(new_dentry);
3722349457ccSMark Fasheh 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
37231da177e4SLinus Torvalds 		d_move(old_dentry, new_dentry);
372451892bbbSSage Weil out:
37251da177e4SLinus Torvalds 	if (target)
37261b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
37271da177e4SLinus Torvalds 	dput(new_dentry);
37281da177e4SLinus Torvalds 	return error;
37291da177e4SLinus Torvalds }
37301da177e4SLinus Torvalds 
37311da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
37321da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
37331da177e4SLinus Torvalds {
37341da177e4SLinus Torvalds 	int error;
37351da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
373659b0df21SEric Paris 	const unsigned char *old_name;
37371da177e4SLinus Torvalds 
37381da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
37391da177e4SLinus Torvalds  		return 0;
37401da177e4SLinus Torvalds 
37411da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
37421da177e4SLinus Torvalds 	if (error)
37431da177e4SLinus Torvalds 		return error;
37441da177e4SLinus Torvalds 
37451da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
3746a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
37471da177e4SLinus Torvalds 	else
37481da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
37491da177e4SLinus Torvalds 	if (error)
37501da177e4SLinus Torvalds 		return error;
37511da177e4SLinus Torvalds 
3752acfa4380SAl Viro 	if (!old_dir->i_op->rename)
37531da177e4SLinus Torvalds 		return -EPERM;
37541da177e4SLinus Torvalds 
37550eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
37560eeca283SRobert Love 
37571da177e4SLinus Torvalds 	if (is_dir)
37581da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
37591da177e4SLinus Torvalds 	else
37601da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
3761123df294SAl Viro 	if (!error)
3762123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
37635a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
37640eeca283SRobert Love 	fsnotify_oldname_free(old_name);
37650eeca283SRobert Love 
37661da177e4SLinus Torvalds 	return error;
37671da177e4SLinus Torvalds }
37681da177e4SLinus Torvalds 
37692e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
37702e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
37711da177e4SLinus Torvalds {
37721da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
37731da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
37741da177e4SLinus Torvalds 	struct dentry *trap;
37751da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
377691a27b2aSJeff Layton 	struct filename *from;
377791a27b2aSJeff Layton 	struct filename *to;
37782ad94ae6SAl Viro 	int error;
37791da177e4SLinus Torvalds 
378091a27b2aSJeff Layton 	from = user_path_parent(olddfd, oldname, &oldnd);
378191a27b2aSJeff Layton 	if (IS_ERR(from)) {
378291a27b2aSJeff Layton 		error = PTR_ERR(from);
37831da177e4SLinus Torvalds 		goto exit;
378491a27b2aSJeff Layton 	}
37851da177e4SLinus Torvalds 
378691a27b2aSJeff Layton 	to = user_path_parent(newdfd, newname, &newnd);
378791a27b2aSJeff Layton 	if (IS_ERR(to)) {
378891a27b2aSJeff Layton 		error = PTR_ERR(to);
37891da177e4SLinus Torvalds 		goto exit1;
379091a27b2aSJeff Layton 	}
37911da177e4SLinus Torvalds 
37921da177e4SLinus Torvalds 	error = -EXDEV;
37934ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
37941da177e4SLinus Torvalds 		goto exit2;
37951da177e4SLinus Torvalds 
37964ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
37971da177e4SLinus Torvalds 	error = -EBUSY;
37981da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
37991da177e4SLinus Torvalds 		goto exit2;
38001da177e4SLinus Torvalds 
38014ac91378SJan Blunck 	new_dir = newnd.path.dentry;
38021da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
38031da177e4SLinus Torvalds 		goto exit2;
38041da177e4SLinus Torvalds 
3805c30dabfeSJan Kara 	error = mnt_want_write(oldnd.path.mnt);
3806c30dabfeSJan Kara 	if (error)
3807c30dabfeSJan Kara 		goto exit2;
3808c30dabfeSJan Kara 
38090612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
38100612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
38114e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
38120612d9fbSOGAWA Hirofumi 
38131da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
38141da177e4SLinus Torvalds 
381549705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
38161da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
38171da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
38181da177e4SLinus Torvalds 		goto exit3;
38191da177e4SLinus Torvalds 	/* source must exist */
38201da177e4SLinus Torvalds 	error = -ENOENT;
38211da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
38221da177e4SLinus Torvalds 		goto exit4;
38231da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
38241da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
38251da177e4SLinus Torvalds 		error = -ENOTDIR;
38261da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
38271da177e4SLinus Torvalds 			goto exit4;
38281da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
38291da177e4SLinus Torvalds 			goto exit4;
38301da177e4SLinus Torvalds 	}
38311da177e4SLinus Torvalds 	/* source should not be ancestor of target */
38321da177e4SLinus Torvalds 	error = -EINVAL;
38331da177e4SLinus Torvalds 	if (old_dentry == trap)
38341da177e4SLinus Torvalds 		goto exit4;
383549705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
38361da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
38371da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
38381da177e4SLinus Torvalds 		goto exit4;
38391da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
38401da177e4SLinus Torvalds 	error = -ENOTEMPTY;
38411da177e4SLinus Torvalds 	if (new_dentry == trap)
38421da177e4SLinus Torvalds 		goto exit5;
38431da177e4SLinus Torvalds 
3844be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
3845be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
3846be6d3e56SKentaro Takeda 	if (error)
3847c30dabfeSJan Kara 		goto exit5;
38481da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
38491da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
38501da177e4SLinus Torvalds exit5:
38511da177e4SLinus Torvalds 	dput(new_dentry);
38521da177e4SLinus Torvalds exit4:
38531da177e4SLinus Torvalds 	dput(old_dentry);
38541da177e4SLinus Torvalds exit3:
38551da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
3856c30dabfeSJan Kara 	mnt_drop_write(oldnd.path.mnt);
38571da177e4SLinus Torvalds exit2:
38581d957f9bSJan Blunck 	path_put(&newnd.path);
38592ad94ae6SAl Viro 	putname(to);
38601da177e4SLinus Torvalds exit1:
38611d957f9bSJan Blunck 	path_put(&oldnd.path);
38621da177e4SLinus Torvalds 	putname(from);
38632ad94ae6SAl Viro exit:
38641da177e4SLinus Torvalds 	return error;
38651da177e4SLinus Torvalds }
38661da177e4SLinus Torvalds 
3867a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
38685590ff0dSUlrich Drepper {
38695590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
38705590ff0dSUlrich Drepper }
38715590ff0dSUlrich Drepper 
38721da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
38731da177e4SLinus Torvalds {
38741da177e4SLinus Torvalds 	int len;
38751da177e4SLinus Torvalds 
38761da177e4SLinus Torvalds 	len = PTR_ERR(link);
38771da177e4SLinus Torvalds 	if (IS_ERR(link))
38781da177e4SLinus Torvalds 		goto out;
38791da177e4SLinus Torvalds 
38801da177e4SLinus Torvalds 	len = strlen(link);
38811da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
38821da177e4SLinus Torvalds 		len = buflen;
38831da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
38841da177e4SLinus Torvalds 		len = -EFAULT;
38851da177e4SLinus Torvalds out:
38861da177e4SLinus Torvalds 	return len;
38871da177e4SLinus Torvalds }
38881da177e4SLinus Torvalds 
38891da177e4SLinus Torvalds /*
38901da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
38911da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
38921da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
38931da177e4SLinus Torvalds  */
38941da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
38951da177e4SLinus Torvalds {
38961da177e4SLinus Torvalds 	struct nameidata nd;
3897cc314eefSLinus Torvalds 	void *cookie;
3898694a1764SMarcin Slusarz 	int res;
3899cc314eefSLinus Torvalds 
39001da177e4SLinus Torvalds 	nd.depth = 0;
3901cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
3902694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
3903694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
3904694a1764SMarcin Slusarz 
3905694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
39061da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
3907cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
3908694a1764SMarcin Slusarz 	return res;
39091da177e4SLinus Torvalds }
39101da177e4SLinus Torvalds 
39111da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
39121da177e4SLinus Torvalds {
39131da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
39141da177e4SLinus Torvalds }
39151da177e4SLinus Torvalds 
39161da177e4SLinus Torvalds /* get the link contents into pagecache */
39171da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
39181da177e4SLinus Torvalds {
3919ebd09abbSDuane Griffin 	char *kaddr;
39201da177e4SLinus Torvalds 	struct page *page;
39211da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
3922090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
39231da177e4SLinus Torvalds 	if (IS_ERR(page))
39246fe6900eSNick Piggin 		return (char*)page;
39251da177e4SLinus Torvalds 	*ppage = page;
3926ebd09abbSDuane Griffin 	kaddr = kmap(page);
3927ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
3928ebd09abbSDuane Griffin 	return kaddr;
39291da177e4SLinus Torvalds }
39301da177e4SLinus Torvalds 
39311da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
39321da177e4SLinus Torvalds {
39331da177e4SLinus Torvalds 	struct page *page = NULL;
39341da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
39351da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
39361da177e4SLinus Torvalds 	if (page) {
39371da177e4SLinus Torvalds 		kunmap(page);
39381da177e4SLinus Torvalds 		page_cache_release(page);
39391da177e4SLinus Torvalds 	}
39401da177e4SLinus Torvalds 	return res;
39411da177e4SLinus Torvalds }
39421da177e4SLinus Torvalds 
3943cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
39441da177e4SLinus Torvalds {
3945cc314eefSLinus Torvalds 	struct page *page = NULL;
39461da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
3947cc314eefSLinus Torvalds 	return page;
39481da177e4SLinus Torvalds }
39491da177e4SLinus Torvalds 
3950cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
39511da177e4SLinus Torvalds {
3952cc314eefSLinus Torvalds 	struct page *page = cookie;
3953cc314eefSLinus Torvalds 
3954cc314eefSLinus Torvalds 	if (page) {
39551da177e4SLinus Torvalds 		kunmap(page);
39561da177e4SLinus Torvalds 		page_cache_release(page);
39571da177e4SLinus Torvalds 	}
39581da177e4SLinus Torvalds }
39591da177e4SLinus Torvalds 
396054566b2cSNick Piggin /*
396154566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
396254566b2cSNick Piggin  */
396354566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
39641da177e4SLinus Torvalds {
39651da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
39660adb25d2SKirill Korotaev 	struct page *page;
3967afddba49SNick Piggin 	void *fsdata;
3968beb497abSDmitriy Monakhov 	int err;
39691da177e4SLinus Torvalds 	char *kaddr;
397054566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
397154566b2cSNick Piggin 	if (nofs)
397254566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
39731da177e4SLinus Torvalds 
39747e53cac4SNeilBrown retry:
3975afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
397654566b2cSNick Piggin 				flags, &page, &fsdata);
39771da177e4SLinus Torvalds 	if (err)
3978afddba49SNick Piggin 		goto fail;
3979afddba49SNick Piggin 
3980e8e3c3d6SCong Wang 	kaddr = kmap_atomic(page);
39811da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
3982e8e3c3d6SCong Wang 	kunmap_atomic(kaddr);
3983afddba49SNick Piggin 
3984afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
3985afddba49SNick Piggin 							page, fsdata);
39861da177e4SLinus Torvalds 	if (err < 0)
39871da177e4SLinus Torvalds 		goto fail;
3988afddba49SNick Piggin 	if (err < len-1)
3989afddba49SNick Piggin 		goto retry;
3990afddba49SNick Piggin 
39911da177e4SLinus Torvalds 	mark_inode_dirty(inode);
39921da177e4SLinus Torvalds 	return 0;
39931da177e4SLinus Torvalds fail:
39941da177e4SLinus Torvalds 	return err;
39951da177e4SLinus Torvalds }
39961da177e4SLinus Torvalds 
39970adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
39980adb25d2SKirill Korotaev {
39990adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
400054566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
40010adb25d2SKirill Korotaev }
40020adb25d2SKirill Korotaev 
400392e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
40041da177e4SLinus Torvalds 	.readlink	= generic_readlink,
40051da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
40061da177e4SLinus Torvalds 	.put_link	= page_put_link,
40071da177e4SLinus Torvalds };
40081da177e4SLinus Torvalds 
40092d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
4010cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one);
40111da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
40121da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
4013f6d2ac5cSAl Viro EXPORT_SYMBOL(get_write_access); /* nfsd */
40141da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
40151da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
40161da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
40171da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
40181da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
40190adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
40201da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
40211da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
4022d1811465SAl Viro EXPORT_SYMBOL(kern_path);
402316f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
4024f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
40251da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
40261da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
40271da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
40281da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
40291da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
40301da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
40311da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
40321da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
40331da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
40341da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
40351da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
40361da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
40371da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
40381da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
4039