xref: /openbmc/linux/fs/namei.c (revision d03b29a2)
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 {
1227950e385SJeff Layton 	if (name->separate) {
12391a27b2aSJeff Layton 		__putname(name->name);
12491a27b2aSJeff Layton 		kfree(name);
1257950e385SJeff Layton 	} else {
1267950e385SJeff Layton 		__putname(name);
12791a27b2aSJeff Layton 	}
1287950e385SJeff Layton }
1297950e385SJeff Layton 
1307950e385SJeff Layton #define EMBEDDED_NAME_MAX	(PATH_MAX - sizeof(struct filename))
13191a27b2aSJeff Layton 
13291a27b2aSJeff Layton static struct filename *
13391a27b2aSJeff Layton getname_flags(const char __user *filename, int flags, int *empty)
13491a27b2aSJeff Layton {
13591a27b2aSJeff Layton 	struct filename *result, *err;
1363f9f0aa6SLinus Torvalds 	int len;
1377950e385SJeff Layton 	long max;
1387950e385SJeff Layton 	char *kname;
1391da177e4SLinus Torvalds 
1407ac86265SJeff Layton 	result = audit_reusename(filename);
1417ac86265SJeff Layton 	if (result)
1427ac86265SJeff Layton 		return result;
1437ac86265SJeff Layton 
1447950e385SJeff Layton 	result = __getname();
1453f9f0aa6SLinus Torvalds 	if (unlikely(!result))
1464043cde8SEric Paris 		return ERR_PTR(-ENOMEM);
1471da177e4SLinus Torvalds 
1487950e385SJeff Layton 	/*
1497950e385SJeff Layton 	 * First, try to embed the struct filename inside the names_cache
1507950e385SJeff Layton 	 * allocation
1517950e385SJeff Layton 	 */
1527950e385SJeff Layton 	kname = (char *)result + sizeof(*result);
15391a27b2aSJeff Layton 	result->name = kname;
1547950e385SJeff Layton 	result->separate = false;
1557950e385SJeff Layton 	max = EMBEDDED_NAME_MAX;
1567950e385SJeff Layton 
1577950e385SJeff Layton recopy:
1587950e385SJeff Layton 	len = strncpy_from_user(kname, filename, max);
15991a27b2aSJeff Layton 	if (unlikely(len < 0)) {
1603f9f0aa6SLinus Torvalds 		err = ERR_PTR(len);
1613f9f0aa6SLinus Torvalds 		goto error;
16291a27b2aSJeff Layton 	}
1633f9f0aa6SLinus Torvalds 
1647950e385SJeff Layton 	/*
1657950e385SJeff Layton 	 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
1667950e385SJeff Layton 	 * separate struct filename so we can dedicate the entire
1677950e385SJeff Layton 	 * names_cache allocation for the pathname, and re-do the copy from
1687950e385SJeff Layton 	 * userland.
1697950e385SJeff Layton 	 */
1707950e385SJeff Layton 	if (len == EMBEDDED_NAME_MAX && max == EMBEDDED_NAME_MAX) {
1717950e385SJeff Layton 		kname = (char *)result;
1727950e385SJeff Layton 
1737950e385SJeff Layton 		result = kzalloc(sizeof(*result), GFP_KERNEL);
1747950e385SJeff Layton 		if (!result) {
1757950e385SJeff Layton 			err = ERR_PTR(-ENOMEM);
1767950e385SJeff Layton 			result = (struct filename *)kname;
1777950e385SJeff Layton 			goto error;
1787950e385SJeff Layton 		}
1797950e385SJeff Layton 		result->name = kname;
1807950e385SJeff Layton 		result->separate = true;
1817950e385SJeff Layton 		max = PATH_MAX;
1827950e385SJeff Layton 		goto recopy;
1837950e385SJeff Layton 	}
1847950e385SJeff Layton 
1853f9f0aa6SLinus Torvalds 	/* The empty path is special. */
1863f9f0aa6SLinus Torvalds 	if (unlikely(!len)) {
1873f9f0aa6SLinus Torvalds 		if (empty)
1881fa1e7f6SAndy Whitcroft 			*empty = 1;
1893f9f0aa6SLinus Torvalds 		err = ERR_PTR(-ENOENT);
1903f9f0aa6SLinus Torvalds 		if (!(flags & LOOKUP_EMPTY))
1913f9f0aa6SLinus Torvalds 			goto error;
1921da177e4SLinus Torvalds 	}
1933f9f0aa6SLinus Torvalds 
1943f9f0aa6SLinus Torvalds 	err = ERR_PTR(-ENAMETOOLONG);
1957950e385SJeff Layton 	if (unlikely(len >= PATH_MAX))
1967950e385SJeff Layton 		goto error;
1977950e385SJeff Layton 
1987950e385SJeff Layton 	result->uptr = filename;
199c4ad8f98SLinus Torvalds 	result->aname = NULL;
2001da177e4SLinus Torvalds 	audit_getname(result);
2011da177e4SLinus Torvalds 	return result;
2021da177e4SLinus Torvalds 
2033f9f0aa6SLinus Torvalds error:
2047950e385SJeff Layton 	final_putname(result);
2053f9f0aa6SLinus Torvalds 	return err;
2063f9f0aa6SLinus Torvalds }
2073f9f0aa6SLinus Torvalds 
20891a27b2aSJeff Layton struct filename *
20991a27b2aSJeff Layton getname(const char __user * filename)
210f52e0c11SAl Viro {
211f7493e5dSLinus Torvalds 	return getname_flags(filename, 0, NULL);
212f52e0c11SAl Viro }
213f52e0c11SAl Viro 
214c4ad8f98SLinus Torvalds /*
215c4ad8f98SLinus Torvalds  * The "getname_kernel()" interface doesn't do pathnames longer
216c4ad8f98SLinus Torvalds  * than EMBEDDED_NAME_MAX. Deal with it - you're a kernel user.
217c4ad8f98SLinus Torvalds  */
218c4ad8f98SLinus Torvalds struct filename *
219c4ad8f98SLinus Torvalds getname_kernel(const char * filename)
220c4ad8f98SLinus Torvalds {
221c4ad8f98SLinus Torvalds 	struct filename *result;
222c4ad8f98SLinus Torvalds 	char *kname;
223c4ad8f98SLinus Torvalds 	int len;
224c4ad8f98SLinus Torvalds 
225c4ad8f98SLinus Torvalds 	len = strlen(filename);
226c4ad8f98SLinus Torvalds 	if (len >= EMBEDDED_NAME_MAX)
227c4ad8f98SLinus Torvalds 		return ERR_PTR(-ENAMETOOLONG);
228c4ad8f98SLinus Torvalds 
229c4ad8f98SLinus Torvalds 	result = __getname();
230c4ad8f98SLinus Torvalds 	if (unlikely(!result))
231c4ad8f98SLinus Torvalds 		return ERR_PTR(-ENOMEM);
232c4ad8f98SLinus Torvalds 
233c4ad8f98SLinus Torvalds 	kname = (char *)result + sizeof(*result);
234c4ad8f98SLinus Torvalds 	result->name = kname;
235c4ad8f98SLinus Torvalds 	result->uptr = NULL;
236c4ad8f98SLinus Torvalds 	result->aname = NULL;
237c4ad8f98SLinus Torvalds 	result->separate = false;
238c4ad8f98SLinus Torvalds 
239c4ad8f98SLinus Torvalds 	strlcpy(kname, filename, EMBEDDED_NAME_MAX);
240c4ad8f98SLinus Torvalds 	return result;
241c4ad8f98SLinus Torvalds }
242c4ad8f98SLinus Torvalds 
2431da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
24491a27b2aSJeff Layton void putname(struct filename *name)
2451da177e4SLinus Torvalds {
2465ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
24791a27b2aSJeff Layton 		return audit_putname(name);
24891a27b2aSJeff Layton 	final_putname(name);
2491da177e4SLinus Torvalds }
2501da177e4SLinus Torvalds #endif
2511da177e4SLinus Torvalds 
252e77819e5SLinus Torvalds static int check_acl(struct inode *inode, int mask)
253e77819e5SLinus Torvalds {
25484635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL
255e77819e5SLinus Torvalds 	struct posix_acl *acl;
256e77819e5SLinus Torvalds 
257e77819e5SLinus Torvalds 	if (mask & MAY_NOT_BLOCK) {
2583567866bSAl Viro 		acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
2593567866bSAl Viro 	        if (!acl)
260e77819e5SLinus Torvalds 	                return -EAGAIN;
2613567866bSAl Viro 		/* no ->get_acl() calls in RCU mode... */
2623567866bSAl Viro 		if (acl == ACL_NOT_CACHED)
263e77819e5SLinus Torvalds 			return -ECHILD;
264206b1d09SAri Savolainen 	        return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
265e77819e5SLinus Torvalds 	}
266e77819e5SLinus Torvalds 
2672982baa2SChristoph Hellwig 	acl = get_acl(inode, ACL_TYPE_ACCESS);
2684e34e719SChristoph Hellwig 	if (IS_ERR(acl))
2694e34e719SChristoph Hellwig 		return PTR_ERR(acl);
270e77819e5SLinus Torvalds 	if (acl) {
271e77819e5SLinus Torvalds 	        int error = posix_acl_permission(inode, acl, mask);
272e77819e5SLinus Torvalds 	        posix_acl_release(acl);
273e77819e5SLinus Torvalds 	        return error;
274e77819e5SLinus Torvalds 	}
27584635d68SLinus Torvalds #endif
276e77819e5SLinus Torvalds 
277e77819e5SLinus Torvalds 	return -EAGAIN;
278e77819e5SLinus Torvalds }
279e77819e5SLinus Torvalds 
2805909ccaaSLinus Torvalds /*
281948409c7SAndreas Gruenbacher  * This does the basic permission checking
2825909ccaaSLinus Torvalds  */
2837e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask)
2845909ccaaSLinus Torvalds {
28526cf46beSLinus Torvalds 	unsigned int mode = inode->i_mode;
2865909ccaaSLinus Torvalds 
2878e96e3b7SEric W. Biederman 	if (likely(uid_eq(current_fsuid(), inode->i_uid)))
2885909ccaaSLinus Torvalds 		mode >>= 6;
2895909ccaaSLinus Torvalds 	else {
290e77819e5SLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
2917e40145eSAl Viro 			int error = check_acl(inode, mask);
2925909ccaaSLinus Torvalds 			if (error != -EAGAIN)
2935909ccaaSLinus Torvalds 				return error;
2945909ccaaSLinus Torvalds 		}
2955909ccaaSLinus Torvalds 
2965909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
2975909ccaaSLinus Torvalds 			mode >>= 3;
2985909ccaaSLinus Torvalds 	}
2995909ccaaSLinus Torvalds 
3005909ccaaSLinus Torvalds 	/*
3015909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
3025909ccaaSLinus Torvalds 	 */
3039c2c7039SAl Viro 	if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
3045909ccaaSLinus Torvalds 		return 0;
3055909ccaaSLinus Torvalds 	return -EACCES;
3065909ccaaSLinus Torvalds }
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds /**
3091da177e4SLinus Torvalds  * generic_permission -  check for access rights on a Posix-like filesystem
3101da177e4SLinus Torvalds  * @inode:	inode to check access rights for
3118fd90c8dSAndreas Gruenbacher  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
3121da177e4SLinus Torvalds  *
3131da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
3141da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
3151da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
316b74c79e9SNick Piggin  * are used for other things.
317b74c79e9SNick Piggin  *
318b74c79e9SNick Piggin  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
319b74c79e9SNick Piggin  * request cannot be satisfied (eg. requires blocking or too much complexity).
320b74c79e9SNick Piggin  * It would then be called again in ref-walk mode.
3211da177e4SLinus Torvalds  */
3222830ba7fSAl Viro int generic_permission(struct inode *inode, int mask)
3231da177e4SLinus Torvalds {
3245909ccaaSLinus Torvalds 	int ret;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	/*
327948409c7SAndreas Gruenbacher 	 * Do the basic permission checks.
3281da177e4SLinus Torvalds 	 */
3297e40145eSAl Viro 	ret = acl_permission_check(inode, mask);
3305909ccaaSLinus Torvalds 	if (ret != -EACCES)
3315909ccaaSLinus Torvalds 		return ret;
3321da177e4SLinus Torvalds 
333d594e7ecSAl Viro 	if (S_ISDIR(inode->i_mode)) {
334d594e7ecSAl Viro 		/* DACs are overridable for directories */
33523adbe12SAndy Lutomirski 		if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
336d594e7ecSAl Viro 			return 0;
337d594e7ecSAl Viro 		if (!(mask & MAY_WRITE))
33823adbe12SAndy Lutomirski 			if (capable_wrt_inode_uidgid(inode,
33923adbe12SAndy Lutomirski 						     CAP_DAC_READ_SEARCH))
340d594e7ecSAl Viro 				return 0;
341d594e7ecSAl Viro 		return -EACCES;
342d594e7ecSAl Viro 	}
3431da177e4SLinus Torvalds 	/*
3441da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
345d594e7ecSAl Viro 	 * Executable DACs are overridable when there is
346d594e7ecSAl Viro 	 * at least one exec bit set.
3471da177e4SLinus Torvalds 	 */
348d594e7ecSAl Viro 	if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
34923adbe12SAndy Lutomirski 		if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
3501da177e4SLinus Torvalds 			return 0;
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 	/*
3531da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
3541da177e4SLinus Torvalds 	 */
3557ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
356d594e7ecSAl Viro 	if (mask == MAY_READ)
35723adbe12SAndy Lutomirski 		if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
3581da177e4SLinus Torvalds 			return 0;
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	return -EACCES;
3611da177e4SLinus Torvalds }
3624d359507SAl Viro EXPORT_SYMBOL(generic_permission);
3631da177e4SLinus Torvalds 
3643ddcd056SLinus Torvalds /*
3653ddcd056SLinus Torvalds  * We _really_ want to just do "generic_permission()" without
3663ddcd056SLinus Torvalds  * even looking at the inode->i_op values. So we keep a cache
3673ddcd056SLinus Torvalds  * flag in inode->i_opflags, that says "this has not special
3683ddcd056SLinus Torvalds  * permission function, use the fast case".
3693ddcd056SLinus Torvalds  */
3703ddcd056SLinus Torvalds static inline int do_inode_permission(struct inode *inode, int mask)
3713ddcd056SLinus Torvalds {
3723ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
3733ddcd056SLinus Torvalds 		if (likely(inode->i_op->permission))
3743ddcd056SLinus Torvalds 			return inode->i_op->permission(inode, mask);
3753ddcd056SLinus Torvalds 
3763ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
3773ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
3783ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_FASTPERM;
3793ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
3803ddcd056SLinus Torvalds 	}
3813ddcd056SLinus Torvalds 	return generic_permission(inode, mask);
3823ddcd056SLinus Torvalds }
3833ddcd056SLinus Torvalds 
384cb23beb5SChristoph Hellwig /**
3850bdaea90SDavid Howells  * __inode_permission - Check for access rights to a given inode
3860bdaea90SDavid Howells  * @inode: Inode to check permission on
3870bdaea90SDavid Howells  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
388cb23beb5SChristoph Hellwig  *
3890bdaea90SDavid Howells  * Check for read/write/execute permissions on an inode.
390948409c7SAndreas Gruenbacher  *
391948409c7SAndreas Gruenbacher  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
3920bdaea90SDavid Howells  *
3930bdaea90SDavid Howells  * This does not check for a read-only file system.  You probably want
3940bdaea90SDavid Howells  * inode_permission().
395cb23beb5SChristoph Hellwig  */
3960bdaea90SDavid Howells int __inode_permission(struct inode *inode, int mask)
3971da177e4SLinus Torvalds {
398e6305c43SAl Viro 	int retval;
3991da177e4SLinus Torvalds 
4003ddcd056SLinus Torvalds 	if (unlikely(mask & MAY_WRITE)) {
4011da177e4SLinus Torvalds 		/*
4021da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
4031da177e4SLinus Torvalds 		 */
4041da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
4051da177e4SLinus Torvalds 			return -EACCES;
4061da177e4SLinus Torvalds 	}
4071da177e4SLinus Torvalds 
4083ddcd056SLinus Torvalds 	retval = do_inode_permission(inode, mask);
4091da177e4SLinus Torvalds 	if (retval)
4101da177e4SLinus Torvalds 		return retval;
4111da177e4SLinus Torvalds 
41208ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
41308ce5f16SSerge E. Hallyn 	if (retval)
41408ce5f16SSerge E. Hallyn 		return retval;
41508ce5f16SSerge E. Hallyn 
416d09ca739SEric Paris 	return security_inode_permission(inode, mask);
4171da177e4SLinus Torvalds }
4181da177e4SLinus Torvalds 
419f4d6ff89SAl Viro /**
4200bdaea90SDavid Howells  * sb_permission - Check superblock-level permissions
4210bdaea90SDavid Howells  * @sb: Superblock of inode to check permission on
42255852635SRandy Dunlap  * @inode: Inode to check permission on
4230bdaea90SDavid Howells  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
4240bdaea90SDavid Howells  *
4250bdaea90SDavid Howells  * Separate out file-system wide checks from inode-specific permission checks.
4260bdaea90SDavid Howells  */
4270bdaea90SDavid Howells static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
4280bdaea90SDavid Howells {
4290bdaea90SDavid Howells 	if (unlikely(mask & MAY_WRITE)) {
4300bdaea90SDavid Howells 		umode_t mode = inode->i_mode;
4310bdaea90SDavid Howells 
4320bdaea90SDavid Howells 		/* Nobody gets write access to a read-only fs. */
4330bdaea90SDavid Howells 		if ((sb->s_flags & MS_RDONLY) &&
4340bdaea90SDavid Howells 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
4350bdaea90SDavid Howells 			return -EROFS;
4360bdaea90SDavid Howells 	}
4370bdaea90SDavid Howells 	return 0;
4380bdaea90SDavid Howells }
4390bdaea90SDavid Howells 
4400bdaea90SDavid Howells /**
4410bdaea90SDavid Howells  * inode_permission - Check for access rights to a given inode
4420bdaea90SDavid Howells  * @inode: Inode to check permission on
4430bdaea90SDavid Howells  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
4440bdaea90SDavid Howells  *
4450bdaea90SDavid Howells  * Check for read/write/execute permissions on an inode.  We use fs[ug]id for
4460bdaea90SDavid Howells  * this, letting us set arbitrary permissions for filesystem access without
4470bdaea90SDavid Howells  * changing the "normal" UIDs which are used for other things.
4480bdaea90SDavid Howells  *
4490bdaea90SDavid Howells  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
4500bdaea90SDavid Howells  */
4510bdaea90SDavid Howells int inode_permission(struct inode *inode, int mask)
4520bdaea90SDavid Howells {
4530bdaea90SDavid Howells 	int retval;
4540bdaea90SDavid Howells 
4550bdaea90SDavid Howells 	retval = sb_permission(inode->i_sb, inode, mask);
4560bdaea90SDavid Howells 	if (retval)
4570bdaea90SDavid Howells 		return retval;
4580bdaea90SDavid Howells 	return __inode_permission(inode, mask);
4590bdaea90SDavid Howells }
4604d359507SAl Viro EXPORT_SYMBOL(inode_permission);
4610bdaea90SDavid Howells 
4620bdaea90SDavid Howells /**
4635dd784d0SJan Blunck  * path_get - get a reference to a path
4645dd784d0SJan Blunck  * @path: path to get the reference to
4655dd784d0SJan Blunck  *
4665dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
4675dd784d0SJan Blunck  */
468dcf787f3SAl Viro void path_get(const struct path *path)
4695dd784d0SJan Blunck {
4705dd784d0SJan Blunck 	mntget(path->mnt);
4715dd784d0SJan Blunck 	dget(path->dentry);
4725dd784d0SJan Blunck }
4735dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
4745dd784d0SJan Blunck 
4755dd784d0SJan Blunck /**
4761d957f9bSJan Blunck  * path_put - put a reference to a path
4771d957f9bSJan Blunck  * @path: path to put the reference to
4781d957f9bSJan Blunck  *
4791d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
4801d957f9bSJan Blunck  */
481dcf787f3SAl Viro void path_put(const struct path *path)
4821da177e4SLinus Torvalds {
4831d957f9bSJan Blunck 	dput(path->dentry);
4841d957f9bSJan Blunck 	mntput(path->mnt);
4851da177e4SLinus Torvalds }
4861d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
4871da177e4SLinus Torvalds 
48819660af7SAl Viro /*
48931e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
49019660af7SAl Viro  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
49119660af7SAl Viro  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
49219660af7SAl Viro  * normal reference counts on dentries and vfsmounts to transition to rcu-walk
49319660af7SAl Viro  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
49419660af7SAl Viro  * got stuck, so ref-walk may continue from there. If this is not successful
49519660af7SAl Viro  * (eg. a seqcount has changed), then failure is returned and it's up to caller
49619660af7SAl Viro  * to restart the path walk from the beginning in ref-walk mode.
49731e6b01fSNick Piggin  */
49831e6b01fSNick Piggin 
49931e6b01fSNick Piggin /**
50019660af7SAl Viro  * unlazy_walk - try to switch to ref-walk mode.
50119660af7SAl Viro  * @nd: nameidata pathwalk data
50219660af7SAl Viro  * @dentry: child of nd->path.dentry or NULL
50339191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
50431e6b01fSNick Piggin  *
50519660af7SAl Viro  * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
50619660af7SAl Viro  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
50719660af7SAl Viro  * @nd or NULL.  Must be called from rcu-walk context.
50831e6b01fSNick Piggin  */
50919660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
51031e6b01fSNick Piggin {
51131e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
51231e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
51331e6b01fSNick Piggin 
51431e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
515e5c832d5SLinus Torvalds 
516e5c832d5SLinus Torvalds 	/*
51748a066e7SAl Viro 	 * After legitimizing the bastards, terminate_walk()
51848a066e7SAl Viro 	 * will do the right thing for non-RCU mode, and all our
51948a066e7SAl Viro 	 * subsequent exit cases should rcu_read_unlock()
52048a066e7SAl Viro 	 * before returning.  Do vfsmount first; if dentry
52148a066e7SAl Viro 	 * can't be legitimized, just set nd->path.dentry to NULL
52248a066e7SAl Viro 	 * and rely on dput(NULL) being a no-op.
523e5c832d5SLinus Torvalds 	 */
52448a066e7SAl Viro 	if (!legitimize_mnt(nd->path.mnt, nd->m_seq))
525e5c832d5SLinus Torvalds 		return -ECHILD;
526e5c832d5SLinus Torvalds 	nd->flags &= ~LOOKUP_RCU;
52715570086SLinus Torvalds 
52848a066e7SAl Viro 	if (!lockref_get_not_dead(&parent->d_lockref)) {
52948a066e7SAl Viro 		nd->path.dentry = NULL;
530d870b4a1SAl Viro 		goto out;
53148a066e7SAl Viro 	}
53248a066e7SAl Viro 
53315570086SLinus Torvalds 	/*
53415570086SLinus Torvalds 	 * For a negative lookup, the lookup sequence point is the parents
53515570086SLinus Torvalds 	 * sequence point, and it only needs to revalidate the parent dentry.
53615570086SLinus Torvalds 	 *
53715570086SLinus Torvalds 	 * For a positive lookup, we need to move both the parent and the
53815570086SLinus Torvalds 	 * dentry from the RCU domain to be properly refcounted. And the
53915570086SLinus Torvalds 	 * sequence number in the dentry validates *both* dentry counters,
54015570086SLinus Torvalds 	 * since we checked the sequence number of the parent after we got
54115570086SLinus Torvalds 	 * the child sequence number. So we know the parent must still
54215570086SLinus Torvalds 	 * be valid if the child sequence number is still valid.
54315570086SLinus Torvalds 	 */
54419660af7SAl Viro 	if (!dentry) {
545e5c832d5SLinus Torvalds 		if (read_seqcount_retry(&parent->d_seq, nd->seq))
546e5c832d5SLinus Torvalds 			goto out;
54719660af7SAl Viro 		BUG_ON(nd->inode != parent->d_inode);
54819660af7SAl Viro 	} else {
549e5c832d5SLinus Torvalds 		if (!lockref_get_not_dead(&dentry->d_lockref))
550e5c832d5SLinus Torvalds 			goto out;
551e5c832d5SLinus Torvalds 		if (read_seqcount_retry(&dentry->d_seq, nd->seq))
552e5c832d5SLinus Torvalds 			goto drop_dentry;
55319660af7SAl Viro 	}
554e5c832d5SLinus Torvalds 
555e5c832d5SLinus Torvalds 	/*
556e5c832d5SLinus Torvalds 	 * Sequence counts matched. Now make sure that the root is
557e5c832d5SLinus Torvalds 	 * still valid and get it if required.
558e5c832d5SLinus Torvalds 	 */
559e5c832d5SLinus Torvalds 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
560e5c832d5SLinus Torvalds 		spin_lock(&fs->lock);
561e5c832d5SLinus Torvalds 		if (nd->root.mnt != fs->root.mnt || nd->root.dentry != fs->root.dentry)
562e5c832d5SLinus Torvalds 			goto unlock_and_drop_dentry;
56331e6b01fSNick Piggin 		path_get(&nd->root);
56431e6b01fSNick Piggin 		spin_unlock(&fs->lock);
56531e6b01fSNick Piggin 	}
56631e6b01fSNick Piggin 
5678b61e74fSAl Viro 	rcu_read_unlock();
56831e6b01fSNick Piggin 	return 0;
56919660af7SAl Viro 
570e5c832d5SLinus Torvalds unlock_and_drop_dentry:
57131e6b01fSNick Piggin 	spin_unlock(&fs->lock);
572e5c832d5SLinus Torvalds drop_dentry:
5738b61e74fSAl Viro 	rcu_read_unlock();
574e5c832d5SLinus Torvalds 	dput(dentry);
575d0d27277SLinus Torvalds 	goto drop_root_mnt;
576e5c832d5SLinus Torvalds out:
5778b61e74fSAl Viro 	rcu_read_unlock();
578d0d27277SLinus Torvalds drop_root_mnt:
579d0d27277SLinus Torvalds 	if (!(nd->flags & LOOKUP_ROOT))
580d0d27277SLinus Torvalds 		nd->root.mnt = NULL;
58131e6b01fSNick Piggin 	return -ECHILD;
58231e6b01fSNick Piggin }
58331e6b01fSNick Piggin 
5844ce16ef3SAl Viro static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
58534286d66SNick Piggin {
5864ce16ef3SAl Viro 	return dentry->d_op->d_revalidate(dentry, flags);
58734286d66SNick Piggin }
58834286d66SNick Piggin 
5899f1fafeeSAl Viro /**
5909f1fafeeSAl Viro  * complete_walk - successful completion of path walk
5919f1fafeeSAl Viro  * @nd:  pointer nameidata
59239159de2SJeff Layton  *
5939f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
5949f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
5959f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
5969f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
5979f1fafeeSAl Viro  * need to drop nd->path.
59839159de2SJeff Layton  */
5999f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
60039159de2SJeff Layton {
60116c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
60239159de2SJeff Layton 	int status;
60339159de2SJeff Layton 
6049f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
6059f1fafeeSAl Viro 		nd->flags &= ~LOOKUP_RCU;
6069f1fafeeSAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
6079f1fafeeSAl Viro 			nd->root.mnt = NULL;
60815570086SLinus Torvalds 
60948a066e7SAl Viro 		if (!legitimize_mnt(nd->path.mnt, nd->m_seq)) {
6108b61e74fSAl Viro 			rcu_read_unlock();
61148a066e7SAl Viro 			return -ECHILD;
61248a066e7SAl Viro 		}
613e5c832d5SLinus Torvalds 		if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) {
6148b61e74fSAl Viro 			rcu_read_unlock();
61548a066e7SAl Viro 			mntput(nd->path.mnt);
6169f1fafeeSAl Viro 			return -ECHILD;
6179f1fafeeSAl Viro 		}
618e5c832d5SLinus Torvalds 		if (read_seqcount_retry(&dentry->d_seq, nd->seq)) {
6198b61e74fSAl Viro 			rcu_read_unlock();
620e5c832d5SLinus Torvalds 			dput(dentry);
62148a066e7SAl Viro 			mntput(nd->path.mnt);
622e5c832d5SLinus Torvalds 			return -ECHILD;
623e5c832d5SLinus Torvalds 		}
6248b61e74fSAl Viro 		rcu_read_unlock();
6259f1fafeeSAl Viro 	}
6269f1fafeeSAl Viro 
62716c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
62839159de2SJeff Layton 		return 0;
62939159de2SJeff Layton 
630ecf3d1f1SJeff Layton 	if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
63116c2cd71SAl Viro 		return 0;
63216c2cd71SAl Viro 
633ecf3d1f1SJeff Layton 	status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
63439159de2SJeff Layton 	if (status > 0)
63539159de2SJeff Layton 		return 0;
63639159de2SJeff Layton 
63716c2cd71SAl Viro 	if (!status)
63839159de2SJeff Layton 		status = -ESTALE;
63916c2cd71SAl Viro 
6409f1fafeeSAl Viro 	path_put(&nd->path);
64139159de2SJeff Layton 	return status;
64239159de2SJeff Layton }
64339159de2SJeff Layton 
6442a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
6452a737871SAl Viro {
646f7ad3c6bSMiklos Szeredi 	if (!nd->root.mnt)
647f7ad3c6bSMiklos Szeredi 		get_fs_root(current->fs, &nd->root);
6482a737871SAl Viro }
6492a737871SAl Viro 
6506de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
6516de88d72SAl Viro 
65231e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd)
65331e6b01fSNick Piggin {
65431e6b01fSNick Piggin 	if (!nd->root.mnt) {
65531e6b01fSNick Piggin 		struct fs_struct *fs = current->fs;
656c28cc364SNick Piggin 		unsigned seq;
657c28cc364SNick Piggin 
658c28cc364SNick Piggin 		do {
659c28cc364SNick Piggin 			seq = read_seqcount_begin(&fs->seq);
66031e6b01fSNick Piggin 			nd->root = fs->root;
661c1530019STim Chen 			nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
662c28cc364SNick Piggin 		} while (read_seqcount_retry(&fs->seq, seq));
66331e6b01fSNick Piggin 	}
66431e6b01fSNick Piggin }
66531e6b01fSNick Piggin 
6661d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
667051d3812SIan Kent {
668051d3812SIan Kent 	dput(path->dentry);
6694ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
670051d3812SIan Kent 		mntput(path->mnt);
671051d3812SIan Kent }
672051d3812SIan Kent 
6737b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
6747b9337aaSNick Piggin 					struct nameidata *nd)
675051d3812SIan Kent {
67631e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
6774ac91378SJan Blunck 		dput(nd->path.dentry);
67831e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
6794ac91378SJan Blunck 			mntput(nd->path.mnt);
6809a229683SHuang Shijie 	}
68131e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
6824ac91378SJan Blunck 	nd->path.dentry = path->dentry;
683051d3812SIan Kent }
684051d3812SIan Kent 
685b5fb63c1SChristoph Hellwig /*
686b5fb63c1SChristoph Hellwig  * Helper to directly jump to a known parsed path from ->follow_link,
687b5fb63c1SChristoph Hellwig  * caller must have taken a reference to path beforehand.
688b5fb63c1SChristoph Hellwig  */
689b5fb63c1SChristoph Hellwig void nd_jump_link(struct nameidata *nd, struct path *path)
690b5fb63c1SChristoph Hellwig {
691b5fb63c1SChristoph Hellwig 	path_put(&nd->path);
692b5fb63c1SChristoph Hellwig 
693b5fb63c1SChristoph Hellwig 	nd->path = *path;
694b5fb63c1SChristoph Hellwig 	nd->inode = nd->path.dentry->d_inode;
695b5fb63c1SChristoph Hellwig 	nd->flags |= LOOKUP_JUMPED;
696b5fb63c1SChristoph Hellwig }
697b5fb63c1SChristoph Hellwig 
698574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
699574197e0SAl Viro {
700574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
7016d7b5aaeSAl Viro 	if (inode->i_op->put_link)
702574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
703574197e0SAl Viro 	path_put(link);
704574197e0SAl Viro }
705574197e0SAl Viro 
706561ec64aSLinus Torvalds int sysctl_protected_symlinks __read_mostly = 0;
707561ec64aSLinus Torvalds int sysctl_protected_hardlinks __read_mostly = 0;
708800179c9SKees Cook 
709800179c9SKees Cook /**
710800179c9SKees Cook  * may_follow_link - Check symlink following for unsafe situations
711800179c9SKees Cook  * @link: The path of the symlink
71255852635SRandy Dunlap  * @nd: nameidata pathwalk data
713800179c9SKees Cook  *
714800179c9SKees Cook  * In the case of the sysctl_protected_symlinks sysctl being enabled,
715800179c9SKees Cook  * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
716800179c9SKees Cook  * in a sticky world-writable directory. This is to protect privileged
717800179c9SKees Cook  * processes from failing races against path names that may change out
718800179c9SKees Cook  * from under them by way of other users creating malicious symlinks.
719800179c9SKees Cook  * It will permit symlinks to be followed only when outside a sticky
720800179c9SKees Cook  * world-writable directory, or when the uid of the symlink and follower
721800179c9SKees Cook  * match, or when the directory owner matches the symlink's owner.
722800179c9SKees Cook  *
723800179c9SKees Cook  * Returns 0 if following the symlink is allowed, -ve on error.
724800179c9SKees Cook  */
725800179c9SKees Cook static inline int may_follow_link(struct path *link, struct nameidata *nd)
726800179c9SKees Cook {
727800179c9SKees Cook 	const struct inode *inode;
728800179c9SKees Cook 	const struct inode *parent;
729800179c9SKees Cook 
730800179c9SKees Cook 	if (!sysctl_protected_symlinks)
731800179c9SKees Cook 		return 0;
732800179c9SKees Cook 
733800179c9SKees Cook 	/* Allowed if owner and follower match. */
734800179c9SKees Cook 	inode = link->dentry->d_inode;
73581abe27bSEric W. Biederman 	if (uid_eq(current_cred()->fsuid, inode->i_uid))
736800179c9SKees Cook 		return 0;
737800179c9SKees Cook 
738800179c9SKees Cook 	/* Allowed if parent directory not sticky and world-writable. */
739800179c9SKees Cook 	parent = nd->path.dentry->d_inode;
740800179c9SKees Cook 	if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
741800179c9SKees Cook 		return 0;
742800179c9SKees Cook 
743800179c9SKees Cook 	/* Allowed if parent directory and link owner match. */
74481abe27bSEric W. Biederman 	if (uid_eq(parent->i_uid, inode->i_uid))
745800179c9SKees Cook 		return 0;
746800179c9SKees Cook 
747ffd8d101SSasha Levin 	audit_log_link_denied("follow_link", link);
748800179c9SKees Cook 	path_put_conditional(link, nd);
749800179c9SKees Cook 	path_put(&nd->path);
750800179c9SKees Cook 	return -EACCES;
751800179c9SKees Cook }
752800179c9SKees Cook 
753800179c9SKees Cook /**
754800179c9SKees Cook  * safe_hardlink_source - Check for safe hardlink conditions
755800179c9SKees Cook  * @inode: the source inode to hardlink from
756800179c9SKees Cook  *
757800179c9SKees Cook  * Return false if at least one of the following conditions:
758800179c9SKees Cook  *    - inode is not a regular file
759800179c9SKees Cook  *    - inode is setuid
760800179c9SKees Cook  *    - inode is setgid and group-exec
761800179c9SKees Cook  *    - access failure for read and write
762800179c9SKees Cook  *
763800179c9SKees Cook  * Otherwise returns true.
764800179c9SKees Cook  */
765800179c9SKees Cook static bool safe_hardlink_source(struct inode *inode)
766800179c9SKees Cook {
767800179c9SKees Cook 	umode_t mode = inode->i_mode;
768800179c9SKees Cook 
769800179c9SKees Cook 	/* Special files should not get pinned to the filesystem. */
770800179c9SKees Cook 	if (!S_ISREG(mode))
771800179c9SKees Cook 		return false;
772800179c9SKees Cook 
773800179c9SKees Cook 	/* Setuid files should not get pinned to the filesystem. */
774800179c9SKees Cook 	if (mode & S_ISUID)
775800179c9SKees Cook 		return false;
776800179c9SKees Cook 
777800179c9SKees Cook 	/* Executable setgid files should not get pinned to the filesystem. */
778800179c9SKees Cook 	if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
779800179c9SKees Cook 		return false;
780800179c9SKees Cook 
781800179c9SKees Cook 	/* Hardlinking to unreadable or unwritable sources is dangerous. */
782800179c9SKees Cook 	if (inode_permission(inode, MAY_READ | MAY_WRITE))
783800179c9SKees Cook 		return false;
784800179c9SKees Cook 
785800179c9SKees Cook 	return true;
786800179c9SKees Cook }
787800179c9SKees Cook 
788800179c9SKees Cook /**
789800179c9SKees Cook  * may_linkat - Check permissions for creating a hardlink
790800179c9SKees Cook  * @link: the source to hardlink from
791800179c9SKees Cook  *
792800179c9SKees Cook  * Block hardlink when all of:
793800179c9SKees Cook  *  - sysctl_protected_hardlinks enabled
794800179c9SKees Cook  *  - fsuid does not match inode
795800179c9SKees Cook  *  - hardlink source is unsafe (see safe_hardlink_source() above)
796800179c9SKees Cook  *  - not CAP_FOWNER
797800179c9SKees Cook  *
798800179c9SKees Cook  * Returns 0 if successful, -ve on error.
799800179c9SKees Cook  */
800800179c9SKees Cook static int may_linkat(struct path *link)
801800179c9SKees Cook {
802800179c9SKees Cook 	const struct cred *cred;
803800179c9SKees Cook 	struct inode *inode;
804800179c9SKees Cook 
805800179c9SKees Cook 	if (!sysctl_protected_hardlinks)
806800179c9SKees Cook 		return 0;
807800179c9SKees Cook 
808800179c9SKees Cook 	cred = current_cred();
809800179c9SKees Cook 	inode = link->dentry->d_inode;
810800179c9SKees Cook 
811800179c9SKees Cook 	/* Source inode owner (or CAP_FOWNER) can hardlink all they like,
812800179c9SKees Cook 	 * otherwise, it must be a safe source.
813800179c9SKees Cook 	 */
81481abe27bSEric W. Biederman 	if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
815800179c9SKees Cook 	    capable(CAP_FOWNER))
816800179c9SKees Cook 		return 0;
817800179c9SKees Cook 
818a51d9eaaSKees Cook 	audit_log_link_denied("linkat", link);
819800179c9SKees Cook 	return -EPERM;
820800179c9SKees Cook }
821800179c9SKees Cook 
822def4af30SAl Viro static __always_inline int
823574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
8241da177e4SLinus Torvalds {
8257b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
8266d7b5aaeSAl Viro 	int error;
8276d7b5aaeSAl Viro 	char *s;
8281da177e4SLinus Torvalds 
829844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
830844a3917SAl Viro 
8310e794589SAl Viro 	if (link->mnt == nd->path.mnt)
8320e794589SAl Viro 		mntget(link->mnt);
8330e794589SAl Viro 
8346d7b5aaeSAl Viro 	error = -ELOOP;
8356d7b5aaeSAl Viro 	if (unlikely(current->total_link_count >= 40))
8366d7b5aaeSAl Viro 		goto out_put_nd_path;
8376d7b5aaeSAl Viro 
838574197e0SAl Viro 	cond_resched();
839574197e0SAl Viro 	current->total_link_count++;
840574197e0SAl Viro 
84168ac1234SAl Viro 	touch_atime(link);
8421da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
843cd4e91d3SAl Viro 
84436f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
8456d7b5aaeSAl Viro 	if (error)
8466d7b5aaeSAl Viro 		goto out_put_nd_path;
84736f3b4f6SAl Viro 
84886acdca1SAl Viro 	nd->last_type = LAST_BIND;
849def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
850def4af30SAl Viro 	error = PTR_ERR(*p);
8516d7b5aaeSAl Viro 	if (IS_ERR(*p))
852408ef013SChristoph Hellwig 		goto out_put_nd_path;
8536d7b5aaeSAl Viro 
854cc314eefSLinus Torvalds 	error = 0;
8556d7b5aaeSAl Viro 	s = nd_get_link(nd);
8566d7b5aaeSAl Viro 	if (s) {
857443ed254SAl Viro 		if (unlikely(IS_ERR(s))) {
858443ed254SAl Viro 			path_put(&nd->path);
859443ed254SAl Viro 			put_link(nd, link, *p);
860443ed254SAl Viro 			return PTR_ERR(s);
861443ed254SAl Viro 		}
862443ed254SAl Viro 		if (*s == '/') {
863443ed254SAl Viro 			set_root(nd);
864443ed254SAl Viro 			path_put(&nd->path);
865443ed254SAl Viro 			nd->path = nd->root;
866443ed254SAl Viro 			path_get(&nd->root);
867443ed254SAl Viro 			nd->flags |= LOOKUP_JUMPED;
868443ed254SAl Viro 		}
869443ed254SAl Viro 		nd->inode = nd->path.dentry->d_inode;
870443ed254SAl Viro 		error = link_path_walk(s, nd);
8716d7b5aaeSAl Viro 		if (unlikely(error))
8726d7b5aaeSAl Viro 			put_link(nd, link, *p);
873b5fb63c1SChristoph Hellwig 	}
8746d7b5aaeSAl Viro 
8756d7b5aaeSAl Viro 	return error;
8766d7b5aaeSAl Viro 
8776d7b5aaeSAl Viro out_put_nd_path:
87898f6ef64SArnd Bergmann 	*p = NULL;
8796d7b5aaeSAl Viro 	path_put(&nd->path);
8806d7b5aaeSAl Viro 	path_put(link);
8811da177e4SLinus Torvalds 	return error;
8821da177e4SLinus Torvalds }
8831da177e4SLinus Torvalds 
88431e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
88531e6b01fSNick Piggin {
8860714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
8870714a533SAl Viro 	struct mount *parent;
88831e6b01fSNick Piggin 	struct dentry *mountpoint;
88931e6b01fSNick Piggin 
8900714a533SAl Viro 	parent = mnt->mnt_parent;
8910714a533SAl Viro 	if (&parent->mnt == path->mnt)
89231e6b01fSNick Piggin 		return 0;
893a73324daSAl Viro 	mountpoint = mnt->mnt_mountpoint;
89431e6b01fSNick Piggin 	path->dentry = mountpoint;
8950714a533SAl Viro 	path->mnt = &parent->mnt;
89631e6b01fSNick Piggin 	return 1;
89731e6b01fSNick Piggin }
89831e6b01fSNick Piggin 
899f015f126SDavid Howells /*
900f015f126SDavid Howells  * follow_up - Find the mountpoint of path's vfsmount
901f015f126SDavid Howells  *
902f015f126SDavid Howells  * Given a path, find the mountpoint of its source file system.
903f015f126SDavid Howells  * Replace @path with the path of the mountpoint in the parent mount.
904f015f126SDavid Howells  * Up is towards /.
905f015f126SDavid Howells  *
906f015f126SDavid Howells  * Return 1 if we went up a level and 0 if we were already at the
907f015f126SDavid Howells  * root.
908f015f126SDavid Howells  */
909bab77ebfSAl Viro int follow_up(struct path *path)
9101da177e4SLinus Torvalds {
9110714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
9120714a533SAl Viro 	struct mount *parent;
9131da177e4SLinus Torvalds 	struct dentry *mountpoint;
91499b7db7bSNick Piggin 
91548a066e7SAl Viro 	read_seqlock_excl(&mount_lock);
9160714a533SAl Viro 	parent = mnt->mnt_parent;
9173c0a6163SAl Viro 	if (parent == mnt) {
91848a066e7SAl Viro 		read_sequnlock_excl(&mount_lock);
9191da177e4SLinus Torvalds 		return 0;
9201da177e4SLinus Torvalds 	}
9210714a533SAl Viro 	mntget(&parent->mnt);
922a73324daSAl Viro 	mountpoint = dget(mnt->mnt_mountpoint);
92348a066e7SAl Viro 	read_sequnlock_excl(&mount_lock);
924bab77ebfSAl Viro 	dput(path->dentry);
925bab77ebfSAl Viro 	path->dentry = mountpoint;
926bab77ebfSAl Viro 	mntput(path->mnt);
9270714a533SAl Viro 	path->mnt = &parent->mnt;
9281da177e4SLinus Torvalds 	return 1;
9291da177e4SLinus Torvalds }
9304d359507SAl Viro EXPORT_SYMBOL(follow_up);
9311da177e4SLinus Torvalds 
932b5c84bf6SNick Piggin /*
9339875cf80SDavid Howells  * Perform an automount
9349875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
9359875cf80SDavid Howells  *   were called with.
9361da177e4SLinus Torvalds  */
9379875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
9389875cf80SDavid Howells 			    bool *need_mntput)
93931e6b01fSNick Piggin {
9409875cf80SDavid Howells 	struct vfsmount *mnt;
941ea5b778aSDavid Howells 	int err;
9429875cf80SDavid Howells 
9439875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
9449875cf80SDavid Howells 		return -EREMOTE;
9459875cf80SDavid Howells 
9460ec26fd0SMiklos Szeredi 	/* We don't want to mount if someone's just doing a stat -
9470ec26fd0SMiklos Szeredi 	 * unless they're stat'ing a directory and appended a '/' to
9480ec26fd0SMiklos Szeredi 	 * the name.
9490ec26fd0SMiklos Szeredi 	 *
9500ec26fd0SMiklos Szeredi 	 * We do, however, want to mount if someone wants to open or
9515a30d8a2SDavid Howells 	 * create a file of any type under the mountpoint, wants to
9520ec26fd0SMiklos Szeredi 	 * traverse through the mountpoint or wants to open the
9530ec26fd0SMiklos Szeredi 	 * mounted directory.  Also, autofs may mark negative dentries
9540ec26fd0SMiklos Szeredi 	 * as being automount points.  These will need the attentions
9550ec26fd0SMiklos Szeredi 	 * of the daemon to instantiate them before they can be used.
9565a30d8a2SDavid Howells 	 */
9575a30d8a2SDavid Howells 	if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
958d94c177bSLinus Torvalds 		     LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
9595a30d8a2SDavid Howells 	    path->dentry->d_inode)
9609875cf80SDavid Howells 		return -EISDIR;
9610ec26fd0SMiklos Szeredi 
9629875cf80SDavid Howells 	current->total_link_count++;
9639875cf80SDavid Howells 	if (current->total_link_count >= 40)
9649875cf80SDavid Howells 		return -ELOOP;
9659875cf80SDavid Howells 
9669875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
9679875cf80SDavid Howells 	if (IS_ERR(mnt)) {
9689875cf80SDavid Howells 		/*
9699875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
9709875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
9719875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
9729875cf80SDavid Howells 		 *
9739875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
9749875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
9759875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
9769875cf80SDavid Howells 		 */
97749084c3bSAl Viro 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
9789875cf80SDavid Howells 			return -EREMOTE;
9799875cf80SDavid Howells 		return PTR_ERR(mnt);
98031e6b01fSNick Piggin 	}
981ea5b778aSDavid Howells 
9829875cf80SDavid Howells 	if (!mnt) /* mount collision */
9839875cf80SDavid Howells 		return 0;
9849875cf80SDavid Howells 
9858aef1884SAl Viro 	if (!*need_mntput) {
9868aef1884SAl Viro 		/* lock_mount() may release path->mnt on error */
9878aef1884SAl Viro 		mntget(path->mnt);
9888aef1884SAl Viro 		*need_mntput = true;
9898aef1884SAl Viro 	}
99019a167afSAl Viro 	err = finish_automount(mnt, path);
991ea5b778aSDavid Howells 
992ea5b778aSDavid Howells 	switch (err) {
993ea5b778aSDavid Howells 	case -EBUSY:
994ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
99519a167afSAl Viro 		return 0;
996ea5b778aSDavid Howells 	case 0:
9978aef1884SAl Viro 		path_put(path);
9989875cf80SDavid Howells 		path->mnt = mnt;
9999875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
10009875cf80SDavid Howells 		return 0;
100119a167afSAl Viro 	default:
100219a167afSAl Viro 		return err;
10039875cf80SDavid Howells 	}
100419a167afSAl Viro 
1005ea5b778aSDavid Howells }
10069875cf80SDavid Howells 
10079875cf80SDavid Howells /*
10089875cf80SDavid Howells  * Handle a dentry that is managed in some way.
1009cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
10109875cf80SDavid Howells  * - Flagged as mountpoint
10119875cf80SDavid Howells  * - Flagged as automount point
10129875cf80SDavid Howells  *
10139875cf80SDavid Howells  * This may only be called in refwalk mode.
10149875cf80SDavid Howells  *
10159875cf80SDavid Howells  * Serialization is taken care of in namespace.c
10169875cf80SDavid Howells  */
10179875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
10189875cf80SDavid Howells {
10198aef1884SAl Viro 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
10209875cf80SDavid Howells 	unsigned managed;
10219875cf80SDavid Howells 	bool need_mntput = false;
10228aef1884SAl Viro 	int ret = 0;
10239875cf80SDavid Howells 
10249875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
10259875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
10269875cf80SDavid Howells 	 * the components of that value change under us */
10279875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
10289875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
10299875cf80SDavid Howells 	       unlikely(managed != 0)) {
1030cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1031cc53ce53SDavid Howells 		 * being held. */
1032cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
1033cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1034cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
10351aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
1036cc53ce53SDavid Howells 			if (ret < 0)
10378aef1884SAl Viro 				break;
1038cc53ce53SDavid Howells 		}
1039cc53ce53SDavid Howells 
10409875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
10419875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
10429875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
10439875cf80SDavid Howells 			if (mounted) {
10449875cf80SDavid Howells 				dput(path->dentry);
10459875cf80SDavid Howells 				if (need_mntput)
1046463ffb2eSAl Viro 					mntput(path->mnt);
1047463ffb2eSAl Viro 				path->mnt = mounted;
1048463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
10499875cf80SDavid Howells 				need_mntput = true;
10509875cf80SDavid Howells 				continue;
1051463ffb2eSAl Viro 			}
1052463ffb2eSAl Viro 
10539875cf80SDavid Howells 			/* Something is mounted on this dentry in another
10549875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
105548a066e7SAl Viro 			 * namespace got unmounted before lookup_mnt() could
105648a066e7SAl Viro 			 * get it */
10571da177e4SLinus Torvalds 		}
10589875cf80SDavid Howells 
10599875cf80SDavid Howells 		/* Handle an automount point */
10609875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
10619875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
10629875cf80SDavid Howells 			if (ret < 0)
10638aef1884SAl Viro 				break;
10649875cf80SDavid Howells 			continue;
10659875cf80SDavid Howells 		}
10669875cf80SDavid Howells 
10679875cf80SDavid Howells 		/* We didn't change the current path point */
10689875cf80SDavid Howells 		break;
10699875cf80SDavid Howells 	}
10708aef1884SAl Viro 
10718aef1884SAl Viro 	if (need_mntput && path->mnt == mnt)
10728aef1884SAl Viro 		mntput(path->mnt);
10738aef1884SAl Viro 	if (ret == -EISDIR)
10748aef1884SAl Viro 		ret = 0;
1075a3fbbde7SAl Viro 	return ret < 0 ? ret : need_mntput;
10761da177e4SLinus Torvalds }
10771da177e4SLinus Torvalds 
1078cc53ce53SDavid Howells int follow_down_one(struct path *path)
10791da177e4SLinus Torvalds {
10801da177e4SLinus Torvalds 	struct vfsmount *mounted;
10811da177e4SLinus Torvalds 
10821c755af4SAl Viro 	mounted = lookup_mnt(path);
10831da177e4SLinus Torvalds 	if (mounted) {
10849393bd07SAl Viro 		dput(path->dentry);
10859393bd07SAl Viro 		mntput(path->mnt);
10869393bd07SAl Viro 		path->mnt = mounted;
10879393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
10881da177e4SLinus Torvalds 		return 1;
10891da177e4SLinus Torvalds 	}
10901da177e4SLinus Torvalds 	return 0;
10911da177e4SLinus Torvalds }
10924d359507SAl Viro EXPORT_SYMBOL(follow_down_one);
10931da177e4SLinus Torvalds 
1094b8faf035SNeilBrown static inline int managed_dentry_rcu(struct dentry *dentry)
109562a7375eSIan Kent {
1096b8faf035SNeilBrown 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
1097b8faf035SNeilBrown 		dentry->d_op->d_manage(dentry, true) : 0;
109862a7375eSIan Kent }
109962a7375eSIan Kent 
11009875cf80SDavid Howells /*
1101287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
1102287548e4SAl Viro  * we meet a managed dentry that would need blocking.
11039875cf80SDavid Howells  */
11049875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1105287548e4SAl Viro 			       struct inode **inode)
11069875cf80SDavid Howells {
110762a7375eSIan Kent 	for (;;) {
1108c7105365SAl Viro 		struct mount *mounted;
110962a7375eSIan Kent 		/*
111062a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
111162a7375eSIan Kent 		 * that wants to block transit.
111262a7375eSIan Kent 		 */
1113b8faf035SNeilBrown 		switch (managed_dentry_rcu(path->dentry)) {
1114b8faf035SNeilBrown 		case -ECHILD:
1115b8faf035SNeilBrown 		default:
1116ab90911fSDavid Howells 			return false;
1117b8faf035SNeilBrown 		case -EISDIR:
1118b8faf035SNeilBrown 			return true;
1119b8faf035SNeilBrown 		case 0:
1120b8faf035SNeilBrown 			break;
1121b8faf035SNeilBrown 		}
112262a7375eSIan Kent 
112362a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
1124b8faf035SNeilBrown 			return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
112562a7375eSIan Kent 
1126474279dcSAl Viro 		mounted = __lookup_mnt(path->mnt, path->dentry);
11279875cf80SDavid Howells 		if (!mounted)
11289875cf80SDavid Howells 			break;
1129c7105365SAl Viro 		path->mnt = &mounted->mnt;
1130c7105365SAl Viro 		path->dentry = mounted->mnt.mnt_root;
1131a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
11329875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
113359430262SLinus Torvalds 		/*
113459430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
113559430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
113659430262SLinus Torvalds 		 * because a mount-point is always pinned.
113759430262SLinus Torvalds 		 */
113859430262SLinus Torvalds 		*inode = path->dentry->d_inode;
11399875cf80SDavid Howells 	}
1140b8faf035SNeilBrown 	return read_seqretry(&mount_lock, nd->m_seq) &&
1141b8faf035SNeilBrown 		!(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
1142287548e4SAl Viro }
1143287548e4SAl Viro 
114431e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
114531e6b01fSNick Piggin {
114631e6b01fSNick Piggin 	set_root_rcu(nd);
114731e6b01fSNick Piggin 
114831e6b01fSNick Piggin 	while (1) {
114931e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
115031e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
115131e6b01fSNick Piggin 			break;
115231e6b01fSNick Piggin 		}
115331e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
115431e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
115531e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
115631e6b01fSNick Piggin 			unsigned seq;
115731e6b01fSNick Piggin 
115831e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
115931e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
1160ef7562d5SAl Viro 				goto failed;
116131e6b01fSNick Piggin 			nd->path.dentry = parent;
116231e6b01fSNick Piggin 			nd->seq = seq;
116331e6b01fSNick Piggin 			break;
116431e6b01fSNick Piggin 		}
116531e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
116631e6b01fSNick Piggin 			break;
116731e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
116831e6b01fSNick Piggin 	}
1169b37199e6SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
1170b37199e6SAl Viro 		struct mount *mounted;
1171b37199e6SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
1172b37199e6SAl Viro 		if (!mounted)
1173b37199e6SAl Viro 			break;
1174b37199e6SAl Viro 		nd->path.mnt = &mounted->mnt;
1175b37199e6SAl Viro 		nd->path.dentry = mounted->mnt.mnt_root;
1176b37199e6SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1177b37199e6SAl Viro 		if (!read_seqretry(&mount_lock, nd->m_seq))
1178b37199e6SAl Viro 			goto failed;
1179b37199e6SAl Viro 	}
1180dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
118131e6b01fSNick Piggin 	return 0;
1182ef7562d5SAl Viro 
1183ef7562d5SAl Viro failed:
1184ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
11855b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
1186ef7562d5SAl Viro 		nd->root.mnt = NULL;
11878b61e74fSAl Viro 	rcu_read_unlock();
1188ef7562d5SAl Viro 	return -ECHILD;
118931e6b01fSNick Piggin }
119031e6b01fSNick Piggin 
11919875cf80SDavid Howells /*
1192cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
1193cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
1194cc53ce53SDavid Howells  * caller is permitted to proceed or not.
1195cc53ce53SDavid Howells  */
11967cc90cc3SAl Viro int follow_down(struct path *path)
1197cc53ce53SDavid Howells {
1198cc53ce53SDavid Howells 	unsigned managed;
1199cc53ce53SDavid Howells 	int ret;
1200cc53ce53SDavid Howells 
1201cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
1202cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1203cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1204cc53ce53SDavid Howells 		 * being held.
1205cc53ce53SDavid Howells 		 *
1206cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
1207cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
1208cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
1209cc53ce53SDavid Howells 		 * superstructure.
1210cc53ce53SDavid Howells 		 *
1211cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
1212cc53ce53SDavid Howells 		 */
1213cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
1214cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1215cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
1216ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
12171aed3e42SAl Viro 				path->dentry, false);
1218cc53ce53SDavid Howells 			if (ret < 0)
1219cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
1220cc53ce53SDavid Howells 		}
1221cc53ce53SDavid Howells 
1222cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
1223cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
1224cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
1225cc53ce53SDavid Howells 			if (!mounted)
1226cc53ce53SDavid Howells 				break;
1227cc53ce53SDavid Howells 			dput(path->dentry);
1228cc53ce53SDavid Howells 			mntput(path->mnt);
1229cc53ce53SDavid Howells 			path->mnt = mounted;
1230cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1231cc53ce53SDavid Howells 			continue;
1232cc53ce53SDavid Howells 		}
1233cc53ce53SDavid Howells 
1234cc53ce53SDavid Howells 		/* Don't handle automount points here */
1235cc53ce53SDavid Howells 		break;
1236cc53ce53SDavid Howells 	}
1237cc53ce53SDavid Howells 	return 0;
1238cc53ce53SDavid Howells }
12394d359507SAl Viro EXPORT_SYMBOL(follow_down);
1240cc53ce53SDavid Howells 
1241cc53ce53SDavid Howells /*
12429875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
12439875cf80SDavid Howells  */
12449875cf80SDavid Howells static void follow_mount(struct path *path)
12459875cf80SDavid Howells {
12469875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
12479875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
12489875cf80SDavid Howells 		if (!mounted)
12499875cf80SDavid Howells 			break;
12509875cf80SDavid Howells 		dput(path->dentry);
12519875cf80SDavid Howells 		mntput(path->mnt);
12529875cf80SDavid Howells 		path->mnt = mounted;
12539875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
12549875cf80SDavid Howells 	}
12559875cf80SDavid Howells }
12569875cf80SDavid Howells 
125731e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
12581da177e4SLinus Torvalds {
12592a737871SAl Viro 	set_root(nd);
1260e518ddb7SAndreas Mohr 
12611da177e4SLinus Torvalds 	while(1) {
12624ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
12631da177e4SLinus Torvalds 
12642a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
12652a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
12661da177e4SLinus Torvalds 			break;
12671da177e4SLinus Torvalds 		}
12684ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
12693088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
12703088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
12711da177e4SLinus Torvalds 			dput(old);
12721da177e4SLinus Torvalds 			break;
12731da177e4SLinus Torvalds 		}
12743088dd70SAl Viro 		if (!follow_up(&nd->path))
12751da177e4SLinus Torvalds 			break;
12761da177e4SLinus Torvalds 	}
127779ed0226SAl Viro 	follow_mount(&nd->path);
127831e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
12791da177e4SLinus Torvalds }
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds /*
1282bad61189SMiklos Szeredi  * This looks up the name in dcache, possibly revalidates the old dentry and
1283bad61189SMiklos Szeredi  * allocates a new one if not found or not valid.  In the need_lookup argument
1284bad61189SMiklos Szeredi  * returns whether i_op->lookup is necessary.
1285bad61189SMiklos Szeredi  *
1286bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
1287baa03890SNick Piggin  */
1288bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1289201f956eSAl Viro 				    unsigned int flags, bool *need_lookup)
1290baa03890SNick Piggin {
1291baa03890SNick Piggin 	struct dentry *dentry;
1292bad61189SMiklos Szeredi 	int error;
1293baa03890SNick Piggin 
1294bad61189SMiklos Szeredi 	*need_lookup = false;
1295bad61189SMiklos Szeredi 	dentry = d_lookup(dir, name);
1296bad61189SMiklos Szeredi 	if (dentry) {
129739e3c955SJeff Layton 		if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1298201f956eSAl Viro 			error = d_revalidate(dentry, flags);
1299bad61189SMiklos Szeredi 			if (unlikely(error <= 0)) {
1300bad61189SMiklos Szeredi 				if (error < 0) {
1301bad61189SMiklos Szeredi 					dput(dentry);
1302bad61189SMiklos Szeredi 					return ERR_PTR(error);
1303bad61189SMiklos Szeredi 				} else if (!d_invalidate(dentry)) {
1304bad61189SMiklos Szeredi 					dput(dentry);
1305bad61189SMiklos Szeredi 					dentry = NULL;
1306bad61189SMiklos Szeredi 				}
1307bad61189SMiklos Szeredi 			}
1308bad61189SMiklos Szeredi 		}
1309bad61189SMiklos Szeredi 	}
1310baa03890SNick Piggin 
1311bad61189SMiklos Szeredi 	if (!dentry) {
1312bad61189SMiklos Szeredi 		dentry = d_alloc(dir, name);
1313baa03890SNick Piggin 		if (unlikely(!dentry))
1314baa03890SNick Piggin 			return ERR_PTR(-ENOMEM);
1315baa03890SNick Piggin 
1316bad61189SMiklos Szeredi 		*need_lookup = true;
1317baa03890SNick Piggin 	}
1318baa03890SNick Piggin 	return dentry;
1319baa03890SNick Piggin }
1320baa03890SNick Piggin 
1321baa03890SNick Piggin /*
132213a2c3beSJ. Bruce Fields  * Call i_op->lookup on the dentry.  The dentry must be negative and
132313a2c3beSJ. Bruce Fields  * unhashed.
1324bad61189SMiklos Szeredi  *
1325bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
132644396f4bSJosef Bacik  */
1327bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
132872bd866aSAl Viro 				  unsigned int flags)
132944396f4bSJosef Bacik {
133044396f4bSJosef Bacik 	struct dentry *old;
133144396f4bSJosef Bacik 
133244396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
1333bad61189SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
1334e188dc02SMiklos Szeredi 		dput(dentry);
133544396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
1336e188dc02SMiklos Szeredi 	}
133744396f4bSJosef Bacik 
133872bd866aSAl Viro 	old = dir->i_op->lookup(dir, dentry, flags);
133944396f4bSJosef Bacik 	if (unlikely(old)) {
134044396f4bSJosef Bacik 		dput(dentry);
134144396f4bSJosef Bacik 		dentry = old;
134244396f4bSJosef Bacik 	}
134344396f4bSJosef Bacik 	return dentry;
134444396f4bSJosef Bacik }
134544396f4bSJosef Bacik 
1346a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name,
134772bd866aSAl Viro 		struct dentry *base, unsigned int flags)
1348a3255546SAl Viro {
1349bad61189SMiklos Szeredi 	bool need_lookup;
1350a3255546SAl Viro 	struct dentry *dentry;
1351a3255546SAl Viro 
135272bd866aSAl Viro 	dentry = lookup_dcache(name, base, flags, &need_lookup);
1353bad61189SMiklos Szeredi 	if (!need_lookup)
1354a3255546SAl Viro 		return dentry;
1355bad61189SMiklos Szeredi 
135672bd866aSAl Viro 	return lookup_real(base->d_inode, dentry, flags);
1357a3255546SAl Viro }
1358a3255546SAl Viro 
135944396f4bSJosef Bacik /*
13601da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
13611da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
13621da177e4SLinus Torvalds  *  It _is_ time-critical.
13631da177e4SLinus Torvalds  */
1364e97cdc87SAl Viro static int lookup_fast(struct nameidata *nd,
136531e6b01fSNick Piggin 		       struct path *path, struct inode **inode)
13661da177e4SLinus Torvalds {
13674ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
136831e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
13695a18fff2SAl Viro 	int need_reval = 1;
13705a18fff2SAl Viro 	int status = 1;
13719875cf80SDavid Howells 	int err;
13729875cf80SDavid Howells 
13733cac260aSAl Viro 	/*
1374b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1375b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1376b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1377b04f784eSNick Piggin 	 */
137831e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
137931e6b01fSNick Piggin 		unsigned seq;
1380da53be12SLinus Torvalds 		dentry = __d_lookup_rcu(parent, &nd->last, &seq);
13815a18fff2SAl Viro 		if (!dentry)
13825a18fff2SAl Viro 			goto unlazy;
13835a18fff2SAl Viro 
138412f8ad4bSLinus Torvalds 		/*
138512f8ad4bSLinus Torvalds 		 * This sequence count validates that the inode matches
138612f8ad4bSLinus Torvalds 		 * the dentry name information from lookup.
138712f8ad4bSLinus Torvalds 		 */
138812f8ad4bSLinus Torvalds 		*inode = dentry->d_inode;
138912f8ad4bSLinus Torvalds 		if (read_seqcount_retry(&dentry->d_seq, seq))
139012f8ad4bSLinus Torvalds 			return -ECHILD;
139112f8ad4bSLinus Torvalds 
139212f8ad4bSLinus Torvalds 		/*
139312f8ad4bSLinus Torvalds 		 * This sequence count validates that the parent had no
139412f8ad4bSLinus Torvalds 		 * changes while we did the lookup of the dentry above.
139512f8ad4bSLinus Torvalds 		 *
139612f8ad4bSLinus Torvalds 		 * The memory barrier in read_seqcount_begin of child is
139712f8ad4bSLinus Torvalds 		 *  enough, we can use __read_seqcount_retry here.
139812f8ad4bSLinus Torvalds 		 */
139931e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
140031e6b01fSNick Piggin 			return -ECHILD;
140131e6b01fSNick Piggin 		nd->seq = seq;
14025a18fff2SAl Viro 
140324643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
14044ce16ef3SAl Viro 			status = d_revalidate(dentry, nd->flags);
14055a18fff2SAl Viro 			if (unlikely(status <= 0)) {
14065a18fff2SAl Viro 				if (status != -ECHILD)
14075a18fff2SAl Viro 					need_reval = 0;
14085a18fff2SAl Viro 				goto unlazy;
14095a18fff2SAl Viro 			}
141024643087SAl Viro 		}
141131e6b01fSNick Piggin 		path->mnt = mnt;
141231e6b01fSNick Piggin 		path->dentry = dentry;
1413b8faf035SNeilBrown 		if (likely(__follow_mount_rcu(nd, path, inode)))
14149875cf80SDavid Howells 			return 0;
14155a18fff2SAl Viro unlazy:
141619660af7SAl Viro 		if (unlazy_walk(nd, dentry))
14175a18fff2SAl Viro 			return -ECHILD;
14185a18fff2SAl Viro 	} else {
1419e97cdc87SAl Viro 		dentry = __d_lookup(parent, &nd->last);
142024643087SAl Viro 	}
14215a18fff2SAl Viro 
142281e6f520SAl Viro 	if (unlikely(!dentry))
142381e6f520SAl Viro 		goto need_lookup;
14245a18fff2SAl Viro 
14255a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
14264ce16ef3SAl Viro 		status = d_revalidate(dentry, nd->flags);
14275a18fff2SAl Viro 	if (unlikely(status <= 0)) {
14285a18fff2SAl Viro 		if (status < 0) {
14295a18fff2SAl Viro 			dput(dentry);
14305a18fff2SAl Viro 			return status;
14315a18fff2SAl Viro 		}
14325a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
14335a18fff2SAl Viro 			dput(dentry);
143481e6f520SAl Viro 			goto need_lookup;
14355a18fff2SAl Viro 		}
14365a18fff2SAl Viro 	}
1437697f514dSMiklos Szeredi 
14381da177e4SLinus Torvalds 	path->mnt = mnt;
14391da177e4SLinus Torvalds 	path->dentry = dentry;
14409875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
144189312214SIan Kent 	if (unlikely(err < 0)) {
144289312214SIan Kent 		path_put_conditional(path, nd);
14439875cf80SDavid Howells 		return err;
144489312214SIan Kent 	}
1445a3fbbde7SAl Viro 	if (err)
1446a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
144731e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
14481da177e4SLinus Torvalds 	return 0;
144981e6f520SAl Viro 
145081e6f520SAl Viro need_lookup:
1451697f514dSMiklos Szeredi 	return 1;
1452697f514dSMiklos Szeredi }
1453697f514dSMiklos Szeredi 
1454697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */
1455cc2a5271SAl Viro static int lookup_slow(struct nameidata *nd, struct path *path)
1456697f514dSMiklos Szeredi {
1457697f514dSMiklos Szeredi 	struct dentry *dentry, *parent;
1458697f514dSMiklos Szeredi 	int err;
1459697f514dSMiklos Szeredi 
1460697f514dSMiklos Szeredi 	parent = nd->path.dentry;
146181e6f520SAl Viro 	BUG_ON(nd->inode != parent->d_inode);
146281e6f520SAl Viro 
146381e6f520SAl Viro 	mutex_lock(&parent->d_inode->i_mutex);
1464cc2a5271SAl Viro 	dentry = __lookup_hash(&nd->last, parent, nd->flags);
146581e6f520SAl Viro 	mutex_unlock(&parent->d_inode->i_mutex);
146681e6f520SAl Viro 	if (IS_ERR(dentry))
146781e6f520SAl Viro 		return PTR_ERR(dentry);
1468697f514dSMiklos Szeredi 	path->mnt = nd->path.mnt;
1469697f514dSMiklos Szeredi 	path->dentry = dentry;
1470697f514dSMiklos Szeredi 	err = follow_managed(path, nd->flags);
1471697f514dSMiklos Szeredi 	if (unlikely(err < 0)) {
1472697f514dSMiklos Szeredi 		path_put_conditional(path, nd);
1473697f514dSMiklos Szeredi 		return err;
1474697f514dSMiklos Szeredi 	}
1475697f514dSMiklos Szeredi 	if (err)
1476697f514dSMiklos Szeredi 		nd->flags |= LOOKUP_JUMPED;
1477697f514dSMiklos Szeredi 	return 0;
14781da177e4SLinus Torvalds }
14791da177e4SLinus Torvalds 
148052094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
148152094c8aSAl Viro {
148252094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
14834ad5abb3SAl Viro 		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
148452094c8aSAl Viro 		if (err != -ECHILD)
148552094c8aSAl Viro 			return err;
148619660af7SAl Viro 		if (unlazy_walk(nd, NULL))
148752094c8aSAl Viro 			return -ECHILD;
148852094c8aSAl Viro 	}
14894ad5abb3SAl Viro 	return inode_permission(nd->inode, MAY_EXEC);
149052094c8aSAl Viro }
149152094c8aSAl Viro 
14929856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
14939856fa1bSAl Viro {
14949856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
14959856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
14969856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
14979856fa1bSAl Viro 				return -ECHILD;
14989856fa1bSAl Viro 		} else
14999856fa1bSAl Viro 			follow_dotdot(nd);
15009856fa1bSAl Viro 	}
15019856fa1bSAl Viro 	return 0;
15029856fa1bSAl Viro }
15039856fa1bSAl Viro 
1504951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1505951361f9SAl Viro {
1506951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1507951361f9SAl Viro 		path_put(&nd->path);
1508951361f9SAl Viro 	} else {
1509951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
15105b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1511951361f9SAl Viro 			nd->root.mnt = NULL;
15128b61e74fSAl Viro 		rcu_read_unlock();
1513951361f9SAl Viro 	}
1514951361f9SAl Viro }
1515951361f9SAl Viro 
15163ddcd056SLinus Torvalds /*
15173ddcd056SLinus Torvalds  * Do we need to follow links? We _really_ want to be able
15183ddcd056SLinus Torvalds  * to do this check without having to look at inode->i_op,
15193ddcd056SLinus Torvalds  * so we keep a cache of "no, this doesn't need follow_link"
15203ddcd056SLinus Torvalds  * for the common case.
15213ddcd056SLinus Torvalds  */
1522b18825a7SDavid Howells static inline int should_follow_link(struct dentry *dentry, int follow)
15233ddcd056SLinus Torvalds {
1524b18825a7SDavid Howells 	return unlikely(d_is_symlink(dentry)) ? follow : 0;
15253ddcd056SLinus Torvalds }
15263ddcd056SLinus Torvalds 
1527ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
152821b9b073SAl Viro 		int follow)
1529ce57dfc1SAl Viro {
1530ce57dfc1SAl Viro 	struct inode *inode;
1531ce57dfc1SAl Viro 	int err;
1532ce57dfc1SAl Viro 	/*
1533ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1534ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1535ce57dfc1SAl Viro 	 * parent relationships.
1536ce57dfc1SAl Viro 	 */
153721b9b073SAl Viro 	if (unlikely(nd->last_type != LAST_NORM))
153821b9b073SAl Viro 		return handle_dots(nd, nd->last_type);
1539e97cdc87SAl Viro 	err = lookup_fast(nd, path, &inode);
1540ce57dfc1SAl Viro 	if (unlikely(err)) {
1541697f514dSMiklos Szeredi 		if (err < 0)
1542697f514dSMiklos Szeredi 			goto out_err;
1543697f514dSMiklos Szeredi 
1544cc2a5271SAl Viro 		err = lookup_slow(nd, path);
1545697f514dSMiklos Szeredi 		if (err < 0)
1546697f514dSMiklos Szeredi 			goto out_err;
1547697f514dSMiklos Szeredi 
1548697f514dSMiklos Szeredi 		inode = path->dentry->d_inode;
1549ce57dfc1SAl Viro 	}
1550697f514dSMiklos Szeredi 	err = -ENOENT;
155122213318SAl Viro 	if (!inode || d_is_negative(path->dentry))
1552697f514dSMiklos Szeredi 		goto out_path_put;
1553697f514dSMiklos Szeredi 
1554b18825a7SDavid Howells 	if (should_follow_link(path->dentry, follow)) {
155519660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
155619660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
1557697f514dSMiklos Szeredi 				err = -ECHILD;
1558697f514dSMiklos Szeredi 				goto out_err;
155919660af7SAl Viro 			}
156019660af7SAl Viro 		}
1561ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1562ce57dfc1SAl Viro 		return 1;
1563ce57dfc1SAl Viro 	}
1564ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1565ce57dfc1SAl Viro 	nd->inode = inode;
1566ce57dfc1SAl Viro 	return 0;
1567697f514dSMiklos Szeredi 
1568697f514dSMiklos Szeredi out_path_put:
1569697f514dSMiklos Szeredi 	path_to_nameidata(path, nd);
1570697f514dSMiklos Szeredi out_err:
1571697f514dSMiklos Szeredi 	terminate_walk(nd);
1572697f514dSMiklos Szeredi 	return err;
1573ce57dfc1SAl Viro }
1574ce57dfc1SAl Viro 
15751da177e4SLinus Torvalds /*
1576b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1577b356379aSAl Viro  * limiting consecutive symlinks to 40.
1578b356379aSAl Viro  *
1579b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1580b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1581b356379aSAl Viro  */
1582b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1583b356379aSAl Viro {
1584b356379aSAl Viro 	int res;
1585b356379aSAl Viro 
1586b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1587b356379aSAl Viro 		path_put_conditional(path, nd);
1588b356379aSAl Viro 		path_put(&nd->path);
1589b356379aSAl Viro 		return -ELOOP;
1590b356379aSAl Viro 	}
15911a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1592b356379aSAl Viro 
1593b356379aSAl Viro 	nd->depth++;
1594b356379aSAl Viro 	current->link_count++;
1595b356379aSAl Viro 
1596b356379aSAl Viro 	do {
1597b356379aSAl Viro 		struct path link = *path;
1598b356379aSAl Viro 		void *cookie;
1599574197e0SAl Viro 
1600574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
16016d7b5aaeSAl Viro 		if (res)
16026d7b5aaeSAl Viro 			break;
160321b9b073SAl Viro 		res = walk_component(nd, path, LOOKUP_FOLLOW);
1604574197e0SAl Viro 		put_link(nd, &link, cookie);
1605b356379aSAl Viro 	} while (res > 0);
1606b356379aSAl Viro 
1607b356379aSAl Viro 	current->link_count--;
1608b356379aSAl Viro 	nd->depth--;
1609b356379aSAl Viro 	return res;
1610b356379aSAl Viro }
1611b356379aSAl Viro 
1612b356379aSAl Viro /*
1613bfcfaa77SLinus Torvalds  * We can do the critical dentry name comparison and hashing
1614bfcfaa77SLinus Torvalds  * operations one word at a time, but we are limited to:
1615bfcfaa77SLinus Torvalds  *
1616bfcfaa77SLinus Torvalds  * - Architectures with fast unaligned word accesses. We could
1617bfcfaa77SLinus Torvalds  *   do a "get_unaligned()" if this helps and is sufficiently
1618bfcfaa77SLinus Torvalds  *   fast.
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 	}
1663a5c21dceSWill Deacon 	mask = bytemask_from_count(len);
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)) {
1778da53be12SLinus Torvalds 				err = parent->d_op->d_hash(parent, &this);
17795a202bcdSAl Viro 				if (err < 0)
17805a202bcdSAl Viro 					break;
17815a202bcdSAl Viro 			}
17825a202bcdSAl Viro 		}
1783fe479a58SAl Viro 
17845f4a6a69SAl Viro 		nd->last = this;
17855f4a6a69SAl Viro 		nd->last_type = type;
17865f4a6a69SAl Viro 
1787200e9ef7SLinus Torvalds 		if (!name[len])
17885f4a6a69SAl Viro 			return 0;
1789200e9ef7SLinus Torvalds 		/*
1790200e9ef7SLinus Torvalds 		 * If it wasn't NUL, we know it was '/'. Skip that
1791200e9ef7SLinus Torvalds 		 * slash, and continue until no more slashes.
1792200e9ef7SLinus Torvalds 		 */
1793200e9ef7SLinus Torvalds 		do {
1794200e9ef7SLinus Torvalds 			len++;
1795200e9ef7SLinus Torvalds 		} while (unlikely(name[len] == '/'));
1796200e9ef7SLinus Torvalds 		if (!name[len])
17975f4a6a69SAl Viro 			return 0;
17985f4a6a69SAl Viro 
1799200e9ef7SLinus Torvalds 		name += len;
18001da177e4SLinus Torvalds 
180121b9b073SAl Viro 		err = walk_component(nd, &next, LOOKUP_FOLLOW);
1802ce57dfc1SAl Viro 		if (err < 0)
1803ce57dfc1SAl Viro 			return err;
1804fe479a58SAl Viro 
1805ce57dfc1SAl Viro 		if (err) {
1806b356379aSAl Viro 			err = nested_symlink(&next, nd);
18071da177e4SLinus Torvalds 			if (err)
1808a7472babSAl Viro 				return err;
180931e6b01fSNick Piggin 		}
181044b1d530SMiklos Szeredi 		if (!d_can_lookup(nd->path.dentry)) {
18113ddcd056SLinus Torvalds 			err = -ENOTDIR;
18123ddcd056SLinus Torvalds 			break;
18135f4a6a69SAl Viro 		}
1814ce57dfc1SAl Viro 	}
1815951361f9SAl Viro 	terminate_walk(nd);
18161da177e4SLinus Torvalds 	return err;
18171da177e4SLinus Torvalds }
18181da177e4SLinus Torvalds 
181970e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
182070e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
182131e6b01fSNick Piggin {
182231e6b01fSNick Piggin 	int retval = 0;
182331e6b01fSNick Piggin 
182431e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
182516c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
182631e6b01fSNick Piggin 	nd->depth = 0;
18275b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
1828b18825a7SDavid Howells 		struct dentry *root = nd->root.dentry;
1829b18825a7SDavid Howells 		struct inode *inode = root->d_inode;
183073d049a4SAl Viro 		if (*name) {
183144b1d530SMiklos Szeredi 			if (!d_can_lookup(root))
18325b6ca027SAl Viro 				return -ENOTDIR;
18335b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
18345b6ca027SAl Viro 			if (retval)
18355b6ca027SAl Viro 				return retval;
183673d049a4SAl Viro 		}
18375b6ca027SAl Viro 		nd->path = nd->root;
18385b6ca027SAl Viro 		nd->inode = inode;
18395b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
18408b61e74fSAl Viro 			rcu_read_lock();
18415b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
184248a066e7SAl Viro 			nd->m_seq = read_seqbegin(&mount_lock);
18435b6ca027SAl Viro 		} else {
18445b6ca027SAl Viro 			path_get(&nd->path);
18455b6ca027SAl Viro 		}
18465b6ca027SAl Viro 		return 0;
18475b6ca027SAl Viro 	}
18485b6ca027SAl Viro 
184931e6b01fSNick Piggin 	nd->root.mnt = NULL;
185031e6b01fSNick Piggin 
185148a066e7SAl Viro 	nd->m_seq = read_seqbegin(&mount_lock);
185231e6b01fSNick Piggin 	if (*name=='/') {
1853e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
18548b61e74fSAl Viro 			rcu_read_lock();
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 
18668b61e74fSAl Viro 			rcu_read_lock();
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 {
1877582aa64aSJeff Layton 		/* Caller must check execute permissions on the starting path component */
18782903ff01SAl Viro 		struct fd f = fdget_raw(dfd);
187931e6b01fSNick Piggin 		struct dentry *dentry;
188031e6b01fSNick Piggin 
18812903ff01SAl Viro 		if (!f.file)
18822903ff01SAl Viro 			return -EBADF;
188331e6b01fSNick Piggin 
18842903ff01SAl Viro 		dentry = f.file->f_path.dentry;
188531e6b01fSNick Piggin 
1886f52e0c11SAl Viro 		if (*name) {
188744b1d530SMiklos Szeredi 			if (!d_can_lookup(dentry)) {
18882903ff01SAl Viro 				fdput(f);
18892903ff01SAl Viro 				return -ENOTDIR;
1890f52e0c11SAl Viro 			}
18912903ff01SAl Viro 		}
18922903ff01SAl Viro 
18932903ff01SAl Viro 		nd->path = f.file->f_path;
1894e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
18959c225f26SLinus Torvalds 			if (f.flags & FDPUT_FPUT)
18962903ff01SAl Viro 				*fp = f.file;
1897c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
18988b61e74fSAl Viro 			rcu_read_lock();
18995590ff0dSUlrich Drepper 		} else {
19002903ff01SAl Viro 			path_get(&nd->path);
19012903ff01SAl Viro 			fdput(f);
19021da177e4SLinus Torvalds 		}
1903e41f7d4eSAl Viro 	}
1904e41f7d4eSAl Viro 
190531e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
19069b4a9b14SAl Viro 	return 0;
19079b4a9b14SAl Viro }
19089b4a9b14SAl Viro 
1909bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1910bd92d7feSAl Viro {
1911bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1912bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1913bd92d7feSAl Viro 
1914bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
191521b9b073SAl Viro 	return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
1916bd92d7feSAl Viro }
1917bd92d7feSAl Viro 
19189b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1919ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
19209b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
19219b4a9b14SAl Viro {
192270e9b357SAl Viro 	struct file *base = NULL;
1923bd92d7feSAl Viro 	struct path path;
1924bd92d7feSAl Viro 	int err;
192531e6b01fSNick Piggin 
192631e6b01fSNick Piggin 	/*
192731e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
192831e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
192931e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
193031e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
193131e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
193231e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
193331e6b01fSNick Piggin 	 * analogue, foo_rcu().
193431e6b01fSNick Piggin 	 *
193531e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
193631e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
193731e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
193831e6b01fSNick Piggin 	 * be able to complete).
193931e6b01fSNick Piggin 	 */
1940bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1941ee0827cdSAl Viro 
1942bd92d7feSAl Viro 	if (unlikely(err))
1943bd92d7feSAl Viro 		return err;
1944ee0827cdSAl Viro 
1945ee0827cdSAl Viro 	current->total_link_count = 0;
1946bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1947bd92d7feSAl Viro 
1948bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1949bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1950bd92d7feSAl Viro 		while (err > 0) {
1951bd92d7feSAl Viro 			void *cookie;
1952bd92d7feSAl Viro 			struct path link = path;
1953800179c9SKees Cook 			err = may_follow_link(&link, nd);
1954800179c9SKees Cook 			if (unlikely(err))
1955800179c9SKees Cook 				break;
1956bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1957574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
19586d7b5aaeSAl Viro 			if (err)
19596d7b5aaeSAl Viro 				break;
1960bd92d7feSAl Viro 			err = lookup_last(nd, &path);
1961574197e0SAl Viro 			put_link(nd, &link, cookie);
1962bd92d7feSAl Viro 		}
1963bd92d7feSAl Viro 	}
1964ee0827cdSAl Viro 
19659f1fafeeSAl Viro 	if (!err)
19669f1fafeeSAl Viro 		err = complete_walk(nd);
1967bd92d7feSAl Viro 
1968bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
196944b1d530SMiklos Szeredi 		if (!d_can_lookup(nd->path.dentry)) {
1970bd92d7feSAl Viro 			path_put(&nd->path);
1971bd23a539SAl Viro 			err = -ENOTDIR;
1972bd92d7feSAl Viro 		}
1973bd92d7feSAl Viro 	}
197416c2cd71SAl Viro 
197570e9b357SAl Viro 	if (base)
197670e9b357SAl Viro 		fput(base);
1977ee0827cdSAl Viro 
19785b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
197931e6b01fSNick Piggin 		path_put(&nd->root);
198031e6b01fSNick Piggin 		nd->root.mnt = NULL;
198131e6b01fSNick Piggin 	}
1982bd92d7feSAl Viro 	return err;
198331e6b01fSNick Piggin }
198431e6b01fSNick Piggin 
1985873f1eedSJeff Layton static int filename_lookup(int dfd, struct filename *name,
1986873f1eedSJeff Layton 				unsigned int flags, struct nameidata *nd)
1987873f1eedSJeff Layton {
1988873f1eedSJeff Layton 	int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
1989873f1eedSJeff Layton 	if (unlikely(retval == -ECHILD))
1990873f1eedSJeff Layton 		retval = path_lookupat(dfd, name->name, flags, nd);
1991873f1eedSJeff Layton 	if (unlikely(retval == -ESTALE))
1992873f1eedSJeff Layton 		retval = path_lookupat(dfd, name->name,
1993873f1eedSJeff Layton 						flags | LOOKUP_REVAL, nd);
1994873f1eedSJeff Layton 
1995873f1eedSJeff Layton 	if (likely(!retval))
1996adb5c247SJeff Layton 		audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
1997873f1eedSJeff Layton 	return retval;
1998873f1eedSJeff Layton }
1999873f1eedSJeff Layton 
2000ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
2001ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
2002ee0827cdSAl Viro {
2003873f1eedSJeff Layton 	struct filename filename = { .name = name };
2004ee0827cdSAl Viro 
2005873f1eedSJeff Layton 	return filename_lookup(dfd, &filename, flags, nd);
20061da177e4SLinus Torvalds }
20071da177e4SLinus Torvalds 
200879714f72SAl Viro /* does lookup, returns the object with parent locked */
200979714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path)
20105590ff0dSUlrich Drepper {
201179714f72SAl Viro 	struct nameidata nd;
201279714f72SAl Viro 	struct dentry *d;
201379714f72SAl Viro 	int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
201479714f72SAl Viro 	if (err)
201579714f72SAl Viro 		return ERR_PTR(err);
201679714f72SAl Viro 	if (nd.last_type != LAST_NORM) {
201779714f72SAl Viro 		path_put(&nd.path);
201879714f72SAl Viro 		return ERR_PTR(-EINVAL);
201979714f72SAl Viro 	}
202079714f72SAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
20211e0ea001SAl Viro 	d = __lookup_hash(&nd.last, nd.path.dentry, 0);
202279714f72SAl Viro 	if (IS_ERR(d)) {
202379714f72SAl Viro 		mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
202479714f72SAl Viro 		path_put(&nd.path);
202579714f72SAl Viro 		return d;
202679714f72SAl Viro 	}
202779714f72SAl Viro 	*path = nd.path;
202879714f72SAl Viro 	return d;
20295590ff0dSUlrich Drepper }
20305590ff0dSUlrich Drepper 
2031d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
2032d1811465SAl Viro {
2033d1811465SAl Viro 	struct nameidata nd;
2034d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2035d1811465SAl Viro 	if (!res)
2036d1811465SAl Viro 		*path = nd.path;
2037d1811465SAl Viro 	return res;
2038d1811465SAl Viro }
20394d359507SAl Viro EXPORT_SYMBOL(kern_path);
2040d1811465SAl Viro 
204116f18200SJosef 'Jeff' Sipek /**
204216f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
204316f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
204416f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
204516f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
204616f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
2047e0a01249SAl Viro  * @path: pointer to struct path to fill
204816f18200SJosef 'Jeff' Sipek  */
204916f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
205016f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
2051e0a01249SAl Viro 		    struct path *path)
205216f18200SJosef 'Jeff' Sipek {
2053e0a01249SAl Viro 	struct nameidata nd;
2054e0a01249SAl Viro 	int err;
2055e0a01249SAl Viro 	nd.root.dentry = dentry;
2056e0a01249SAl Viro 	nd.root.mnt = mnt;
2057e0a01249SAl Viro 	BUG_ON(flags & LOOKUP_PARENT);
20585b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
2059e0a01249SAl Viro 	err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2060e0a01249SAl Viro 	if (!err)
2061e0a01249SAl Viro 		*path = nd.path;
2062e0a01249SAl Viro 	return err;
206316f18200SJosef 'Jeff' Sipek }
20644d359507SAl Viro EXPORT_SYMBOL(vfs_path_lookup);
206516f18200SJosef 'Jeff' Sipek 
2066057f6c01SJames Morris /*
2067057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
2068057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
2069057f6c01SJames Morris  * SMP-safe.
2070057f6c01SJames Morris  */
2071a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
20721da177e4SLinus Torvalds {
207372bd866aSAl Viro 	return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
20741da177e4SLinus Torvalds }
20751da177e4SLinus Torvalds 
2076eead1911SChristoph Hellwig /**
2077a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
2078eead1911SChristoph Hellwig  * @name:	pathname component to lookup
2079eead1911SChristoph Hellwig  * @base:	base directory to lookup from
2080eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
2081eead1911SChristoph Hellwig  *
2082a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
2083a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
2084eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
2085eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
2086eead1911SChristoph Hellwig  */
2087057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2088057f6c01SJames Morris {
2089057f6c01SJames Morris 	struct qstr this;
20906a96ba54SAl Viro 	unsigned int c;
2091cda309deSMiklos Szeredi 	int err;
2092057f6c01SJames Morris 
20932f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
20942f9092e1SDavid Woodhouse 
20956a96ba54SAl Viro 	this.name = name;
20966a96ba54SAl Viro 	this.len = len;
20970145acc2SLinus Torvalds 	this.hash = full_name_hash(name, len);
20986a96ba54SAl Viro 	if (!len)
20996a96ba54SAl Viro 		return ERR_PTR(-EACCES);
21006a96ba54SAl Viro 
210121d8a15aSAl Viro 	if (unlikely(name[0] == '.')) {
210221d8a15aSAl Viro 		if (len < 2 || (len == 2 && name[1] == '.'))
210321d8a15aSAl Viro 			return ERR_PTR(-EACCES);
210421d8a15aSAl Viro 	}
210521d8a15aSAl 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) {
2116da53be12SLinus Torvalds 		int err = base->d_op->d_hash(base, &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 }
21274d359507SAl Viro EXPORT_SYMBOL(lookup_one_len);
2128057f6c01SJames Morris 
21291fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
21301fa1e7f6SAndy Whitcroft 		 struct path *path, int *empty)
21311da177e4SLinus Torvalds {
21322d8f3038SAl Viro 	struct nameidata nd;
213391a27b2aSJeff Layton 	struct filename *tmp = getname_flags(name, flags, empty);
21341da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
21351da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
21362d8f3038SAl Viro 
21372d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
21382d8f3038SAl Viro 
2139873f1eedSJeff Layton 		err = filename_lookup(dfd, tmp, flags, &nd);
21401da177e4SLinus Torvalds 		putname(tmp);
21412d8f3038SAl Viro 		if (!err)
21422d8f3038SAl Viro 			*path = nd.path;
21431da177e4SLinus Torvalds 	}
21441da177e4SLinus Torvalds 	return err;
21451da177e4SLinus Torvalds }
21461da177e4SLinus Torvalds 
21471fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags,
21481fa1e7f6SAndy Whitcroft 		 struct path *path)
21491fa1e7f6SAndy Whitcroft {
2150f7493e5dSLinus Torvalds 	return user_path_at_empty(dfd, name, flags, path, NULL);
21511fa1e7f6SAndy Whitcroft }
21524d359507SAl Viro EXPORT_SYMBOL(user_path_at);
21531fa1e7f6SAndy Whitcroft 
2154873f1eedSJeff Layton /*
2155873f1eedSJeff Layton  * NB: most callers don't do anything directly with the reference to the
2156873f1eedSJeff Layton  *     to struct filename, but the nd->last pointer points into the name string
2157873f1eedSJeff Layton  *     allocated by getname. So we must hold the reference to it until all
2158873f1eedSJeff Layton  *     path-walking is complete.
2159873f1eedSJeff Layton  */
216091a27b2aSJeff Layton static struct filename *
21619e790bd6SJeff Layton user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
21629e790bd6SJeff Layton 		 unsigned int flags)
21632ad94ae6SAl Viro {
216491a27b2aSJeff Layton 	struct filename *s = getname(path);
21652ad94ae6SAl Viro 	int error;
21662ad94ae6SAl Viro 
21679e790bd6SJeff Layton 	/* only LOOKUP_REVAL is allowed in extra flags */
21689e790bd6SJeff Layton 	flags &= LOOKUP_REVAL;
21699e790bd6SJeff Layton 
21702ad94ae6SAl Viro 	if (IS_ERR(s))
217191a27b2aSJeff Layton 		return s;
21722ad94ae6SAl Viro 
21739e790bd6SJeff Layton 	error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
217491a27b2aSJeff Layton 	if (error) {
21752ad94ae6SAl Viro 		putname(s);
217691a27b2aSJeff Layton 		return ERR_PTR(error);
217791a27b2aSJeff Layton 	}
21782ad94ae6SAl Viro 
217991a27b2aSJeff Layton 	return s;
21802ad94ae6SAl Viro }
21812ad94ae6SAl Viro 
21828033426eSJeff Layton /**
2183197df04cSAl Viro  * mountpoint_last - look up last component for umount
21848033426eSJeff Layton  * @nd:   pathwalk nameidata - currently pointing at parent directory of "last"
21858033426eSJeff Layton  * @path: pointer to container for result
21868033426eSJeff Layton  *
21878033426eSJeff Layton  * This is a special lookup_last function just for umount. In this case, we
21888033426eSJeff Layton  * need to resolve the path without doing any revalidation.
21898033426eSJeff Layton  *
21908033426eSJeff Layton  * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
21918033426eSJeff Layton  * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
21928033426eSJeff Layton  * in almost all cases, this lookup will be served out of the dcache. The only
21938033426eSJeff Layton  * cases where it won't are if nd->last refers to a symlink or the path is
21948033426eSJeff Layton  * bogus and it doesn't exist.
21958033426eSJeff Layton  *
21968033426eSJeff Layton  * Returns:
21978033426eSJeff Layton  * -error: if there was an error during lookup. This includes -ENOENT if the
21988033426eSJeff Layton  *         lookup found a negative dentry. The nd->path reference will also be
21998033426eSJeff Layton  *         put in this case.
22008033426eSJeff Layton  *
22018033426eSJeff Layton  * 0:      if we successfully resolved nd->path and found it to not to be a
22028033426eSJeff Layton  *         symlink that needs to be followed. "path" will also be populated.
22038033426eSJeff Layton  *         The nd->path reference will also be put.
22048033426eSJeff Layton  *
22058033426eSJeff Layton  * 1:      if we successfully resolved nd->last and found it to be a symlink
22068033426eSJeff Layton  *         that needs to be followed. "path" will be populated with the path
22078033426eSJeff Layton  *         to the link, and nd->path will *not* be put.
22088033426eSJeff Layton  */
22098033426eSJeff Layton static int
2210197df04cSAl Viro mountpoint_last(struct nameidata *nd, struct path *path)
22118033426eSJeff Layton {
22128033426eSJeff Layton 	int error = 0;
22138033426eSJeff Layton 	struct dentry *dentry;
22148033426eSJeff Layton 	struct dentry *dir = nd->path.dentry;
22158033426eSJeff Layton 
221635759521SAl Viro 	/* If we're in rcuwalk, drop out of it to handle last component */
221735759521SAl Viro 	if (nd->flags & LOOKUP_RCU) {
221835759521SAl Viro 		if (unlazy_walk(nd, NULL)) {
22198033426eSJeff Layton 			error = -ECHILD;
222035759521SAl Viro 			goto out;
222135759521SAl Viro 		}
22228033426eSJeff Layton 	}
22238033426eSJeff Layton 
22248033426eSJeff Layton 	nd->flags &= ~LOOKUP_PARENT;
22258033426eSJeff Layton 
22268033426eSJeff Layton 	if (unlikely(nd->last_type != LAST_NORM)) {
22278033426eSJeff Layton 		error = handle_dots(nd, nd->last_type);
222835759521SAl Viro 		if (error)
222935759521SAl Viro 			goto out;
22308033426eSJeff Layton 		dentry = dget(nd->path.dentry);
223135759521SAl Viro 		goto done;
22328033426eSJeff Layton 	}
22338033426eSJeff Layton 
22348033426eSJeff Layton 	mutex_lock(&dir->d_inode->i_mutex);
22358033426eSJeff Layton 	dentry = d_lookup(dir, &nd->last);
22368033426eSJeff Layton 	if (!dentry) {
22378033426eSJeff Layton 		/*
22388033426eSJeff Layton 		 * No cached dentry. Mounted dentries are pinned in the cache,
22398033426eSJeff Layton 		 * so that means that this dentry is probably a symlink or the
22408033426eSJeff Layton 		 * path doesn't actually point to a mounted dentry.
22418033426eSJeff Layton 		 */
22428033426eSJeff Layton 		dentry = d_alloc(dir, &nd->last);
22438033426eSJeff Layton 		if (!dentry) {
22448033426eSJeff Layton 			error = -ENOMEM;
2245bcceeebaSDave Jones 			mutex_unlock(&dir->d_inode->i_mutex);
224635759521SAl Viro 			goto out;
22478033426eSJeff Layton 		}
224835759521SAl Viro 		dentry = lookup_real(dir->d_inode, dentry, nd->flags);
224935759521SAl Viro 		error = PTR_ERR(dentry);
2250bcceeebaSDave Jones 		if (IS_ERR(dentry)) {
2251bcceeebaSDave Jones 			mutex_unlock(&dir->d_inode->i_mutex);
225235759521SAl Viro 			goto out;
22538033426eSJeff Layton 		}
2254bcceeebaSDave Jones 	}
22558033426eSJeff Layton 	mutex_unlock(&dir->d_inode->i_mutex);
22568033426eSJeff Layton 
225735759521SAl Viro done:
225822213318SAl Viro 	if (!dentry->d_inode || d_is_negative(dentry)) {
22598033426eSJeff Layton 		error = -ENOENT;
22608033426eSJeff Layton 		dput(dentry);
226135759521SAl Viro 		goto out;
226235759521SAl Viro 	}
22638033426eSJeff Layton 	path->dentry = dentry;
2264295dc39dSVasily Averin 	path->mnt = nd->path.mnt;
2265b18825a7SDavid Howells 	if (should_follow_link(dentry, nd->flags & LOOKUP_FOLLOW))
22668033426eSJeff Layton 		return 1;
2267295dc39dSVasily Averin 	mntget(path->mnt);
22688033426eSJeff Layton 	follow_mount(path);
226935759521SAl Viro 	error = 0;
227035759521SAl Viro out:
22718033426eSJeff Layton 	terminate_walk(nd);
22728033426eSJeff Layton 	return error;
22738033426eSJeff Layton }
22748033426eSJeff Layton 
22758033426eSJeff Layton /**
2276197df04cSAl Viro  * path_mountpoint - look up a path to be umounted
22778033426eSJeff Layton  * @dfd:	directory file descriptor to start walk from
22788033426eSJeff Layton  * @name:	full pathname to walk
2279606d6fe3SRandy Dunlap  * @path:	pointer to container for result
22808033426eSJeff Layton  * @flags:	lookup flags
22818033426eSJeff Layton  *
22828033426eSJeff Layton  * Look up the given name, but don't attempt to revalidate the last component.
2283606d6fe3SRandy Dunlap  * Returns 0 and "path" will be valid on success; Returns error otherwise.
22848033426eSJeff Layton  */
22858033426eSJeff Layton static int
2286197df04cSAl Viro path_mountpoint(int dfd, const char *name, struct path *path, unsigned int flags)
22878033426eSJeff Layton {
22888033426eSJeff Layton 	struct file *base = NULL;
22898033426eSJeff Layton 	struct nameidata nd;
22908033426eSJeff Layton 	int err;
22918033426eSJeff Layton 
22928033426eSJeff Layton 	err = path_init(dfd, name, flags | LOOKUP_PARENT, &nd, &base);
22938033426eSJeff Layton 	if (unlikely(err))
22948033426eSJeff Layton 		return err;
22958033426eSJeff Layton 
22968033426eSJeff Layton 	current->total_link_count = 0;
22978033426eSJeff Layton 	err = link_path_walk(name, &nd);
22988033426eSJeff Layton 	if (err)
22998033426eSJeff Layton 		goto out;
23008033426eSJeff Layton 
2301197df04cSAl Viro 	err = mountpoint_last(&nd, path);
23028033426eSJeff Layton 	while (err > 0) {
23038033426eSJeff Layton 		void *cookie;
23048033426eSJeff Layton 		struct path link = *path;
23058033426eSJeff Layton 		err = may_follow_link(&link, &nd);
23068033426eSJeff Layton 		if (unlikely(err))
23078033426eSJeff Layton 			break;
23088033426eSJeff Layton 		nd.flags |= LOOKUP_PARENT;
23098033426eSJeff Layton 		err = follow_link(&link, &nd, &cookie);
23108033426eSJeff Layton 		if (err)
23118033426eSJeff Layton 			break;
2312197df04cSAl Viro 		err = mountpoint_last(&nd, path);
23138033426eSJeff Layton 		put_link(&nd, &link, cookie);
23148033426eSJeff Layton 	}
23158033426eSJeff Layton out:
23168033426eSJeff Layton 	if (base)
23178033426eSJeff Layton 		fput(base);
23188033426eSJeff Layton 
23198033426eSJeff Layton 	if (nd.root.mnt && !(nd.flags & LOOKUP_ROOT))
23208033426eSJeff Layton 		path_put(&nd.root);
23218033426eSJeff Layton 
23228033426eSJeff Layton 	return err;
23238033426eSJeff Layton }
23248033426eSJeff Layton 
23252d864651SAl Viro static int
23262d864651SAl Viro filename_mountpoint(int dfd, struct filename *s, struct path *path,
23272d864651SAl Viro 			unsigned int flags)
23282d864651SAl Viro {
23292d864651SAl Viro 	int error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_RCU);
23302d864651SAl Viro 	if (unlikely(error == -ECHILD))
23312d864651SAl Viro 		error = path_mountpoint(dfd, s->name, path, flags);
23322d864651SAl Viro 	if (unlikely(error == -ESTALE))
23332d864651SAl Viro 		error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_REVAL);
23342d864651SAl Viro 	if (likely(!error))
23352d864651SAl Viro 		audit_inode(s, path->dentry, 0);
23362d864651SAl Viro 	return error;
23372d864651SAl Viro }
23382d864651SAl Viro 
23398033426eSJeff Layton /**
2340197df04cSAl Viro  * user_path_mountpoint_at - lookup a path from userland in order to umount it
23418033426eSJeff Layton  * @dfd:	directory file descriptor
23428033426eSJeff Layton  * @name:	pathname from userland
23438033426eSJeff Layton  * @flags:	lookup flags
23448033426eSJeff Layton  * @path:	pointer to container to hold result
23458033426eSJeff Layton  *
23468033426eSJeff Layton  * A umount is a special case for path walking. We're not actually interested
23478033426eSJeff Layton  * in the inode in this situation, and ESTALE errors can be a problem. We
23488033426eSJeff Layton  * simply want track down the dentry and vfsmount attached at the mountpoint
23498033426eSJeff Layton  * and avoid revalidating the last component.
23508033426eSJeff Layton  *
23518033426eSJeff Layton  * Returns 0 and populates "path" on success.
23528033426eSJeff Layton  */
23538033426eSJeff Layton int
2354197df04cSAl Viro user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
23558033426eSJeff Layton 			struct path *path)
23568033426eSJeff Layton {
23578033426eSJeff Layton 	struct filename *s = getname(name);
23588033426eSJeff Layton 	int error;
23598033426eSJeff Layton 	if (IS_ERR(s))
23608033426eSJeff Layton 		return PTR_ERR(s);
23612d864651SAl Viro 	error = filename_mountpoint(dfd, s, path, flags);
23628033426eSJeff Layton 	putname(s);
23638033426eSJeff Layton 	return error;
23648033426eSJeff Layton }
23658033426eSJeff Layton 
23662d864651SAl Viro int
23672d864651SAl Viro kern_path_mountpoint(int dfd, const char *name, struct path *path,
23682d864651SAl Viro 			unsigned int flags)
23692d864651SAl Viro {
23702d864651SAl Viro 	struct filename s = {.name = name};
23712d864651SAl Viro 	return filename_mountpoint(dfd, &s, path, flags);
23722d864651SAl Viro }
23732d864651SAl Viro EXPORT_SYMBOL(kern_path_mountpoint);
23742d864651SAl Viro 
23751da177e4SLinus Torvalds /*
23761da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
23771da177e4SLinus Torvalds  * minimal.
23781da177e4SLinus Torvalds  */
23791da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
23801da177e4SLinus Torvalds {
23818e96e3b7SEric W. Biederman 	kuid_t fsuid = current_fsuid();
2382da9592edSDavid Howells 
23831da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
23841da177e4SLinus Torvalds 		return 0;
23858e96e3b7SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
23861da177e4SLinus Torvalds 		return 0;
23878e96e3b7SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
23881da177e4SLinus Torvalds 		return 0;
238923adbe12SAndy Lutomirski 	return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
23901da177e4SLinus Torvalds }
23911da177e4SLinus Torvalds 
23921da177e4SLinus Torvalds /*
23931da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
23941da177e4SLinus Torvalds  *  whether the type of victim is right.
23951da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
23961da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
23971da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
23981da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
23991da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
24001da177e4SLinus Torvalds  *	a. be owner of dir, or
24011da177e4SLinus Torvalds  *	b. be owner of victim, or
24021da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
24031da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
24041da177e4SLinus Torvalds  *     links pointing to it.
24051da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
24061da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
24071da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
24081da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
24091da177e4SLinus Torvalds  *     nfs_async_unlink().
24101da177e4SLinus Torvalds  */
2411b18825a7SDavid Howells static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
24121da177e4SLinus Torvalds {
2413b18825a7SDavid Howells 	struct inode *inode = victim->d_inode;
24141da177e4SLinus Torvalds 	int error;
24151da177e4SLinus Torvalds 
2416b18825a7SDavid Howells 	if (d_is_negative(victim))
24171da177e4SLinus Torvalds 		return -ENOENT;
2418b18825a7SDavid Howells 	BUG_ON(!inode);
24191da177e4SLinus Torvalds 
24201da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
24214fa6b5ecSJeff Layton 	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
24221da177e4SLinus Torvalds 
2423f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
24241da177e4SLinus Torvalds 	if (error)
24251da177e4SLinus Torvalds 		return error;
24261da177e4SLinus Torvalds 	if (IS_APPEND(dir))
24271da177e4SLinus Torvalds 		return -EPERM;
2428b18825a7SDavid Howells 
2429b18825a7SDavid Howells 	if (check_sticky(dir, inode) || IS_APPEND(inode) ||
2430b18825a7SDavid Howells 	    IS_IMMUTABLE(inode) || IS_SWAPFILE(inode))
24311da177e4SLinus Torvalds 		return -EPERM;
24321da177e4SLinus Torvalds 	if (isdir) {
243344b1d530SMiklos Szeredi 		if (!d_is_dir(victim))
24341da177e4SLinus Torvalds 			return -ENOTDIR;
24351da177e4SLinus Torvalds 		if (IS_ROOT(victim))
24361da177e4SLinus Torvalds 			return -EBUSY;
243744b1d530SMiklos Szeredi 	} else if (d_is_dir(victim))
24381da177e4SLinus Torvalds 		return -EISDIR;
24391da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
24401da177e4SLinus Torvalds 		return -ENOENT;
24411da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
24421da177e4SLinus Torvalds 		return -EBUSY;
24431da177e4SLinus Torvalds 	return 0;
24441da177e4SLinus Torvalds }
24451da177e4SLinus Torvalds 
24461da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
24471da177e4SLinus Torvalds  *  dir.
24481da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
24491da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
24501da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
24511da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
24521da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
24531da177e4SLinus Torvalds  */
2454a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
24551da177e4SLinus Torvalds {
245614e972b4SJeff Layton 	audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
24571da177e4SLinus Torvalds 	if (child->d_inode)
24581da177e4SLinus Torvalds 		return -EEXIST;
24591da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
24601da177e4SLinus Torvalds 		return -ENOENT;
2461f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
24621da177e4SLinus Torvalds }
24631da177e4SLinus Torvalds 
24641da177e4SLinus Torvalds /*
24651da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
24661da177e4SLinus Torvalds  */
24671da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
24681da177e4SLinus Torvalds {
24691da177e4SLinus Torvalds 	struct dentry *p;
24701da177e4SLinus Torvalds 
24711da177e4SLinus Torvalds 	if (p1 == p2) {
2472f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
24731da177e4SLinus Torvalds 		return NULL;
24741da177e4SLinus Torvalds 	}
24751da177e4SLinus Torvalds 
2476a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
24771da177e4SLinus Torvalds 
2478e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
2479e2761a11SOGAWA Hirofumi 	if (p) {
2480f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2481f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
24821da177e4SLinus Torvalds 		return p;
24831da177e4SLinus Torvalds 	}
24841da177e4SLinus Torvalds 
2485e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
2486e2761a11SOGAWA Hirofumi 	if (p) {
2487f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2488f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
24891da177e4SLinus Torvalds 		return p;
24901da177e4SLinus Torvalds 	}
24911da177e4SLinus Torvalds 
2492f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2493f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
24941da177e4SLinus Torvalds 	return NULL;
24951da177e4SLinus Torvalds }
24964d359507SAl Viro EXPORT_SYMBOL(lock_rename);
24971da177e4SLinus Torvalds 
24981da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
24991da177e4SLinus Torvalds {
25001b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
25011da177e4SLinus Torvalds 	if (p1 != p2) {
25021b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
2503a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
25041da177e4SLinus Torvalds 	}
25051da177e4SLinus Torvalds }
25064d359507SAl Viro EXPORT_SYMBOL(unlock_rename);
25071da177e4SLinus Torvalds 
25084acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2509312b63fbSAl Viro 		bool want_excl)
25101da177e4SLinus Torvalds {
2511a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
25121da177e4SLinus Torvalds 	if (error)
25131da177e4SLinus Torvalds 		return error;
25141da177e4SLinus Torvalds 
2515acfa4380SAl Viro 	if (!dir->i_op->create)
25161da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
25171da177e4SLinus Torvalds 	mode &= S_IALLUGO;
25181da177e4SLinus Torvalds 	mode |= S_IFREG;
25191da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
25201da177e4SLinus Torvalds 	if (error)
25211da177e4SLinus Torvalds 		return error;
2522312b63fbSAl Viro 	error = dir->i_op->create(dir, dentry, mode, want_excl);
2523a74574aaSStephen Smalley 	if (!error)
2524f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
25251da177e4SLinus Torvalds 	return error;
25261da177e4SLinus Torvalds }
25274d359507SAl Viro EXPORT_SYMBOL(vfs_create);
25281da177e4SLinus Torvalds 
252973d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
25301da177e4SLinus Torvalds {
25313fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
25321da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
25331da177e4SLinus Torvalds 	int error;
25341da177e4SLinus Torvalds 
2535bcda7652SAl Viro 	/* O_PATH? */
2536bcda7652SAl Viro 	if (!acc_mode)
2537bcda7652SAl Viro 		return 0;
2538bcda7652SAl Viro 
25391da177e4SLinus Torvalds 	if (!inode)
25401da177e4SLinus Torvalds 		return -ENOENT;
25411da177e4SLinus Torvalds 
2542c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2543c8fe8f30SChristoph Hellwig 	case S_IFLNK:
25441da177e4SLinus Torvalds 		return -ELOOP;
2545c8fe8f30SChristoph Hellwig 	case S_IFDIR:
2546c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
25471da177e4SLinus Torvalds 			return -EISDIR;
2548c8fe8f30SChristoph Hellwig 		break;
2549c8fe8f30SChristoph Hellwig 	case S_IFBLK:
2550c8fe8f30SChristoph Hellwig 	case S_IFCHR:
25513fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
25521da177e4SLinus Torvalds 			return -EACCES;
2553c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
2554c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2555c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
25561da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2557c8fe8f30SChristoph Hellwig 		break;
25584a3fd211SDave Hansen 	}
2559b41572e9SDave Hansen 
25603fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
2561b41572e9SDave Hansen 	if (error)
2562b41572e9SDave Hansen 		return error;
25636146f0d5SMimi Zohar 
25641da177e4SLinus Torvalds 	/*
25651da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
25661da177e4SLinus Torvalds 	 */
25671da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
25688737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
25697715b521SAl Viro 			return -EPERM;
25701da177e4SLinus Torvalds 		if (flag & O_TRUNC)
25717715b521SAl Viro 			return -EPERM;
25721da177e4SLinus Torvalds 	}
25731da177e4SLinus Torvalds 
25741da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
25752e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
25767715b521SAl Viro 		return -EPERM;
25771da177e4SLinus Torvalds 
2578f3c7691eSJ. Bruce Fields 	return 0;
25797715b521SAl Viro }
25807715b521SAl Viro 
2581e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
25827715b521SAl Viro {
2583e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
25847715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
25857715b521SAl Viro 	int error = get_write_access(inode);
25861da177e4SLinus Torvalds 	if (error)
25877715b521SAl Viro 		return error;
25881da177e4SLinus Torvalds 	/*
25891da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
25901da177e4SLinus Torvalds 	 */
2591d7a06983SJeff Layton 	error = locks_verify_locked(filp);
2592be6d3e56SKentaro Takeda 	if (!error)
2593ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
25941da177e4SLinus Torvalds 	if (!error) {
25957715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2596d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2597e1181ee6SJeff Layton 				    filp);
25981da177e4SLinus Torvalds 	}
25991da177e4SLinus Torvalds 	put_write_access(inode);
2600acd0c935SMimi Zohar 	return error;
26011da177e4SLinus Torvalds }
26021da177e4SLinus Torvalds 
2603d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2604d57999e1SDave Hansen {
26058a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
26068a5e929dSAl Viro 		flag--;
2607d57999e1SDave Hansen 	return flag;
2608d57999e1SDave Hansen }
2609d57999e1SDave Hansen 
2610d18e9008SMiklos Szeredi static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2611d18e9008SMiklos Szeredi {
2612d18e9008SMiklos Szeredi 	int error = security_path_mknod(dir, dentry, mode, 0);
2613d18e9008SMiklos Szeredi 	if (error)
2614d18e9008SMiklos Szeredi 		return error;
2615d18e9008SMiklos Szeredi 
2616d18e9008SMiklos Szeredi 	error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2617d18e9008SMiklos Szeredi 	if (error)
2618d18e9008SMiklos Szeredi 		return error;
2619d18e9008SMiklos Szeredi 
2620d18e9008SMiklos Szeredi 	return security_inode_create(dir->dentry->d_inode, dentry, mode);
2621d18e9008SMiklos Szeredi }
2622d18e9008SMiklos Szeredi 
26231acf0af9SDavid Howells /*
26241acf0af9SDavid Howells  * Attempt to atomically look up, create and open a file from a negative
26251acf0af9SDavid Howells  * dentry.
26261acf0af9SDavid Howells  *
26271acf0af9SDavid Howells  * Returns 0 if successful.  The file will have been created and attached to
26281acf0af9SDavid Howells  * @file by the filesystem calling finish_open().
26291acf0af9SDavid Howells  *
26301acf0af9SDavid Howells  * Returns 1 if the file was looked up only or didn't need creating.  The
26311acf0af9SDavid Howells  * caller will need to perform the open themselves.  @path will have been
26321acf0af9SDavid Howells  * updated to point to the new dentry.  This may be negative.
26331acf0af9SDavid Howells  *
26341acf0af9SDavid Howells  * Returns an error code otherwise.
26351acf0af9SDavid Howells  */
26362675a4ebSAl Viro static int atomic_open(struct nameidata *nd, struct dentry *dentry,
263730d90494SAl Viro 			struct path *path, struct file *file,
2638015c3bbcSMiklos Szeredi 			const struct open_flags *op,
263964894cf8SAl Viro 			bool got_write, bool need_lookup,
264047237687SAl Viro 			int *opened)
2641d18e9008SMiklos Szeredi {
2642d18e9008SMiklos Szeredi 	struct inode *dir =  nd->path.dentry->d_inode;
2643d18e9008SMiklos Szeredi 	unsigned open_flag = open_to_namei_flags(op->open_flag);
2644d18e9008SMiklos Szeredi 	umode_t mode;
2645d18e9008SMiklos Szeredi 	int error;
2646d18e9008SMiklos Szeredi 	int acc_mode;
2647d18e9008SMiklos Szeredi 	int create_error = 0;
2648d18e9008SMiklos Szeredi 	struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2649116cc022SMiklos Szeredi 	bool excl;
2650d18e9008SMiklos Szeredi 
2651d18e9008SMiklos Szeredi 	BUG_ON(dentry->d_inode);
2652d18e9008SMiklos Szeredi 
2653d18e9008SMiklos Szeredi 	/* Don't create child dentry for a dead directory. */
2654d18e9008SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
26552675a4ebSAl Viro 		error = -ENOENT;
2656d18e9008SMiklos Szeredi 		goto out;
2657d18e9008SMiklos Szeredi 	}
2658d18e9008SMiklos Szeredi 
265962b259d8SMiklos Szeredi 	mode = op->mode;
2660d18e9008SMiklos Szeredi 	if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2661d18e9008SMiklos Szeredi 		mode &= ~current_umask();
2662d18e9008SMiklos Szeredi 
2663116cc022SMiklos Szeredi 	excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
2664116cc022SMiklos Szeredi 	if (excl)
2665d18e9008SMiklos Szeredi 		open_flag &= ~O_TRUNC;
2666d18e9008SMiklos Szeredi 
2667d18e9008SMiklos Szeredi 	/*
2668d18e9008SMiklos Szeredi 	 * Checking write permission is tricky, bacuse we don't know if we are
2669d18e9008SMiklos Szeredi 	 * going to actually need it: O_CREAT opens should work as long as the
2670d18e9008SMiklos Szeredi 	 * file exists.  But checking existence breaks atomicity.  The trick is
2671d18e9008SMiklos Szeredi 	 * to check access and if not granted clear O_CREAT from the flags.
2672d18e9008SMiklos Szeredi 	 *
2673d18e9008SMiklos Szeredi 	 * Another problem is returing the "right" error value (e.g. for an
2674d18e9008SMiklos Szeredi 	 * O_EXCL open we want to return EEXIST not EROFS).
2675d18e9008SMiklos Szeredi 	 */
267664894cf8SAl Viro 	if (((open_flag & (O_CREAT | O_TRUNC)) ||
267764894cf8SAl Viro 	    (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
267864894cf8SAl Viro 		if (!(open_flag & O_CREAT)) {
2679d18e9008SMiklos Szeredi 			/*
2680d18e9008SMiklos Szeredi 			 * No O_CREATE -> atomicity not a requirement -> fall
2681d18e9008SMiklos Szeredi 			 * back to lookup + open
2682d18e9008SMiklos Szeredi 			 */
2683d18e9008SMiklos Szeredi 			goto no_open;
2684d18e9008SMiklos Szeredi 		} else if (open_flag & (O_EXCL | O_TRUNC)) {
2685d18e9008SMiklos Szeredi 			/* Fall back and fail with the right error */
268664894cf8SAl Viro 			create_error = -EROFS;
2687d18e9008SMiklos Szeredi 			goto no_open;
2688d18e9008SMiklos Szeredi 		} else {
2689d18e9008SMiklos Szeredi 			/* No side effects, safe to clear O_CREAT */
269064894cf8SAl Viro 			create_error = -EROFS;
2691d18e9008SMiklos Szeredi 			open_flag &= ~O_CREAT;
2692d18e9008SMiklos Szeredi 		}
2693d18e9008SMiklos Szeredi 	}
2694d18e9008SMiklos Szeredi 
2695d18e9008SMiklos Szeredi 	if (open_flag & O_CREAT) {
269638227f78SMiklos Szeredi 		error = may_o_create(&nd->path, dentry, mode);
2697d18e9008SMiklos Szeredi 		if (error) {
2698d18e9008SMiklos Szeredi 			create_error = error;
2699d18e9008SMiklos Szeredi 			if (open_flag & O_EXCL)
2700d18e9008SMiklos Szeredi 				goto no_open;
2701d18e9008SMiklos Szeredi 			open_flag &= ~O_CREAT;
2702d18e9008SMiklos Szeredi 		}
2703d18e9008SMiklos Szeredi 	}
2704d18e9008SMiklos Szeredi 
2705d18e9008SMiklos Szeredi 	if (nd->flags & LOOKUP_DIRECTORY)
2706d18e9008SMiklos Szeredi 		open_flag |= O_DIRECTORY;
2707d18e9008SMiklos Szeredi 
270830d90494SAl Viro 	file->f_path.dentry = DENTRY_NOT_SET;
270930d90494SAl Viro 	file->f_path.mnt = nd->path.mnt;
271030d90494SAl Viro 	error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
271147237687SAl Viro 				      opened);
2712d9585277SAl Viro 	if (error < 0) {
2713d9585277SAl Viro 		if (create_error && error == -ENOENT)
2714d9585277SAl Viro 			error = create_error;
2715d18e9008SMiklos Szeredi 		goto out;
2716d18e9008SMiklos Szeredi 	}
2717d18e9008SMiklos Szeredi 
2718d9585277SAl Viro 	if (error) {	/* returned 1, that is */
271930d90494SAl Viro 		if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
27202675a4ebSAl Viro 			error = -EIO;
2721d18e9008SMiklos Szeredi 			goto out;
2722d18e9008SMiklos Szeredi 		}
272330d90494SAl Viro 		if (file->f_path.dentry) {
2724d18e9008SMiklos Szeredi 			dput(dentry);
272530d90494SAl Viro 			dentry = file->f_path.dentry;
2726d18e9008SMiklos Szeredi 		}
272703da633aSAl Viro 		if (*opened & FILE_CREATED)
272803da633aSAl Viro 			fsnotify_create(dir, dentry);
272903da633aSAl Viro 		if (!dentry->d_inode) {
273003da633aSAl Viro 			WARN_ON(*opened & FILE_CREATED);
273103da633aSAl Viro 			if (create_error) {
273262b2ce96SSage Weil 				error = create_error;
273362b2ce96SSage Weil 				goto out;
273462b2ce96SSage Weil 			}
273503da633aSAl Viro 		} else {
273603da633aSAl Viro 			if (excl && !(*opened & FILE_CREATED)) {
273703da633aSAl Viro 				error = -EEXIST;
273803da633aSAl Viro 				goto out;
273903da633aSAl Viro 			}
274003da633aSAl Viro 		}
2741d18e9008SMiklos Szeredi 		goto looked_up;
2742d18e9008SMiklos Szeredi 	}
2743d18e9008SMiklos Szeredi 
2744d18e9008SMiklos Szeredi 	/*
2745d18e9008SMiklos Szeredi 	 * We didn't have the inode before the open, so check open permission
2746d18e9008SMiklos Szeredi 	 * here.
2747d18e9008SMiklos Szeredi 	 */
274803da633aSAl Viro 	acc_mode = op->acc_mode;
274903da633aSAl Viro 	if (*opened & FILE_CREATED) {
275003da633aSAl Viro 		WARN_ON(!(open_flag & O_CREAT));
275103da633aSAl Viro 		fsnotify_create(dir, dentry);
275203da633aSAl Viro 		acc_mode = MAY_OPEN;
275303da633aSAl Viro 	}
27542675a4ebSAl Viro 	error = may_open(&file->f_path, acc_mode, open_flag);
27552675a4ebSAl Viro 	if (error)
27562675a4ebSAl Viro 		fput(file);
2757d18e9008SMiklos Szeredi 
2758d18e9008SMiklos Szeredi out:
2759d18e9008SMiklos Szeredi 	dput(dentry);
27602675a4ebSAl Viro 	return error;
2761d18e9008SMiklos Szeredi 
2762d18e9008SMiklos Szeredi no_open:
2763d18e9008SMiklos Szeredi 	if (need_lookup) {
276472bd866aSAl Viro 		dentry = lookup_real(dir, dentry, nd->flags);
2765d18e9008SMiklos Szeredi 		if (IS_ERR(dentry))
27662675a4ebSAl Viro 			return PTR_ERR(dentry);
2767d18e9008SMiklos Szeredi 
2768d18e9008SMiklos Szeredi 		if (create_error) {
2769d18e9008SMiklos Szeredi 			int open_flag = op->open_flag;
2770d18e9008SMiklos Szeredi 
27712675a4ebSAl Viro 			error = create_error;
2772d18e9008SMiklos Szeredi 			if ((open_flag & O_EXCL)) {
2773d18e9008SMiklos Szeredi 				if (!dentry->d_inode)
2774d18e9008SMiklos Szeredi 					goto out;
2775d18e9008SMiklos Szeredi 			} else if (!dentry->d_inode) {
2776d18e9008SMiklos Szeredi 				goto out;
2777d18e9008SMiklos Szeredi 			} else if ((open_flag & O_TRUNC) &&
2778d18e9008SMiklos Szeredi 				   S_ISREG(dentry->d_inode->i_mode)) {
2779d18e9008SMiklos Szeredi 				goto out;
2780d18e9008SMiklos Szeredi 			}
2781d18e9008SMiklos Szeredi 			/* will fail later, go on to get the right error */
2782d18e9008SMiklos Szeredi 		}
2783d18e9008SMiklos Szeredi 	}
2784d18e9008SMiklos Szeredi looked_up:
2785d18e9008SMiklos Szeredi 	path->dentry = dentry;
2786d18e9008SMiklos Szeredi 	path->mnt = nd->path.mnt;
27872675a4ebSAl Viro 	return 1;
2788d18e9008SMiklos Szeredi }
2789d18e9008SMiklos Szeredi 
279031e6b01fSNick Piggin /*
27911acf0af9SDavid Howells  * Look up and maybe create and open the last component.
2792d58ffd35SMiklos Szeredi  *
2793d58ffd35SMiklos Szeredi  * Must be called with i_mutex held on parent.
2794d58ffd35SMiklos Szeredi  *
27951acf0af9SDavid Howells  * Returns 0 if the file was successfully atomically created (if necessary) and
27961acf0af9SDavid Howells  * opened.  In this case the file will be returned attached to @file.
27971acf0af9SDavid Howells  *
27981acf0af9SDavid Howells  * Returns 1 if the file was not completely opened at this time, though lookups
27991acf0af9SDavid Howells  * and creations will have been performed and the dentry returned in @path will
28001acf0af9SDavid Howells  * be positive upon return if O_CREAT was specified.  If O_CREAT wasn't
28011acf0af9SDavid Howells  * specified then a negative dentry may be returned.
28021acf0af9SDavid Howells  *
28031acf0af9SDavid Howells  * An error code is returned otherwise.
28041acf0af9SDavid Howells  *
28051acf0af9SDavid Howells  * FILE_CREATE will be set in @*opened if the dentry was created and will be
28061acf0af9SDavid Howells  * cleared otherwise prior to returning.
2807d58ffd35SMiklos Szeredi  */
28082675a4ebSAl Viro static int lookup_open(struct nameidata *nd, struct path *path,
280930d90494SAl Viro 			struct file *file,
2810d58ffd35SMiklos Szeredi 			const struct open_flags *op,
281164894cf8SAl Viro 			bool got_write, int *opened)
2812d58ffd35SMiklos Szeredi {
2813d58ffd35SMiklos Szeredi 	struct dentry *dir = nd->path.dentry;
281454ef4872SMiklos Szeredi 	struct inode *dir_inode = dir->d_inode;
2815d58ffd35SMiklos Szeredi 	struct dentry *dentry;
2816d58ffd35SMiklos Szeredi 	int error;
281754ef4872SMiklos Szeredi 	bool need_lookup;
2818d58ffd35SMiklos Szeredi 
281947237687SAl Viro 	*opened &= ~FILE_CREATED;
2820201f956eSAl Viro 	dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
2821d58ffd35SMiklos Szeredi 	if (IS_ERR(dentry))
28222675a4ebSAl Viro 		return PTR_ERR(dentry);
2823d58ffd35SMiklos Szeredi 
2824d18e9008SMiklos Szeredi 	/* Cached positive dentry: will open in f_op->open */
2825d18e9008SMiklos Szeredi 	if (!need_lookup && dentry->d_inode)
2826d18e9008SMiklos Szeredi 		goto out_no_open;
2827d18e9008SMiklos Szeredi 
2828d18e9008SMiklos Szeredi 	if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
282964894cf8SAl Viro 		return atomic_open(nd, dentry, path, file, op, got_write,
283047237687SAl Viro 				   need_lookup, opened);
2831d18e9008SMiklos Szeredi 	}
2832d18e9008SMiklos Szeredi 
283354ef4872SMiklos Szeredi 	if (need_lookup) {
283454ef4872SMiklos Szeredi 		BUG_ON(dentry->d_inode);
283554ef4872SMiklos Szeredi 
283672bd866aSAl Viro 		dentry = lookup_real(dir_inode, dentry, nd->flags);
283754ef4872SMiklos Szeredi 		if (IS_ERR(dentry))
28382675a4ebSAl Viro 			return PTR_ERR(dentry);
283954ef4872SMiklos Szeredi 	}
284054ef4872SMiklos Szeredi 
2841d58ffd35SMiklos Szeredi 	/* Negative dentry, just create the file */
2842d58ffd35SMiklos Szeredi 	if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2843d58ffd35SMiklos Szeredi 		umode_t mode = op->mode;
2844d58ffd35SMiklos Szeredi 		if (!IS_POSIXACL(dir->d_inode))
2845d58ffd35SMiklos Szeredi 			mode &= ~current_umask();
2846d58ffd35SMiklos Szeredi 		/*
2847d58ffd35SMiklos Szeredi 		 * This write is needed to ensure that a
2848d58ffd35SMiklos Szeredi 		 * rw->ro transition does not occur between
2849d58ffd35SMiklos Szeredi 		 * the time when the file is created and when
2850d58ffd35SMiklos Szeredi 		 * a permanent write count is taken through
2851015c3bbcSMiklos Szeredi 		 * the 'struct file' in finish_open().
2852d58ffd35SMiklos Szeredi 		 */
285364894cf8SAl Viro 		if (!got_write) {
285464894cf8SAl Viro 			error = -EROFS;
2855d58ffd35SMiklos Szeredi 			goto out_dput;
285664894cf8SAl Viro 		}
285747237687SAl Viro 		*opened |= FILE_CREATED;
2858d58ffd35SMiklos Szeredi 		error = security_path_mknod(&nd->path, dentry, mode, 0);
2859d58ffd35SMiklos Szeredi 		if (error)
2860d58ffd35SMiklos Szeredi 			goto out_dput;
2861312b63fbSAl Viro 		error = vfs_create(dir->d_inode, dentry, mode,
2862312b63fbSAl Viro 				   nd->flags & LOOKUP_EXCL);
2863d58ffd35SMiklos Szeredi 		if (error)
2864d58ffd35SMiklos Szeredi 			goto out_dput;
2865d58ffd35SMiklos Szeredi 	}
2866d18e9008SMiklos Szeredi out_no_open:
2867d58ffd35SMiklos Szeredi 	path->dentry = dentry;
2868d58ffd35SMiklos Szeredi 	path->mnt = nd->path.mnt;
28692675a4ebSAl Viro 	return 1;
2870d58ffd35SMiklos Szeredi 
2871d58ffd35SMiklos Szeredi out_dput:
2872d58ffd35SMiklos Szeredi 	dput(dentry);
28732675a4ebSAl Viro 	return error;
2874d58ffd35SMiklos Szeredi }
2875d58ffd35SMiklos Szeredi 
2876d58ffd35SMiklos Szeredi /*
2877fe2d35ffSAl Viro  * Handle the last step of open()
287831e6b01fSNick Piggin  */
28792675a4ebSAl Viro static int do_last(struct nameidata *nd, struct path *path,
288030d90494SAl Viro 		   struct file *file, const struct open_flags *op,
2881669abf4eSJeff Layton 		   int *opened, struct filename *name)
2882fb1cc555SAl Viro {
2883a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
2884ca344a89SAl Viro 	int open_flag = op->open_flag;
288577d660a8SMiklos Szeredi 	bool will_truncate = (open_flag & O_TRUNC) != 0;
288664894cf8SAl Viro 	bool got_write = false;
2887bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2888a1eb3315SMiklos Szeredi 	struct inode *inode;
288977d660a8SMiklos Szeredi 	bool symlink_ok = false;
289016b1c1cdSMiklos Szeredi 	struct path save_parent = { .dentry = NULL, .mnt = NULL };
289116b1c1cdSMiklos Szeredi 	bool retried = false;
289216c2cd71SAl Viro 	int error;
2893fb1cc555SAl Viro 
2894c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2895c3e380b0SAl Viro 	nd->flags |= op->intent;
2896c3e380b0SAl Viro 
2897bc77daa7SAl Viro 	if (nd->last_type != LAST_NORM) {
2898fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2899fe2d35ffSAl Viro 		if (error)
29002675a4ebSAl Viro 			return error;
2901e83db167SMiklos Szeredi 		goto finish_open;
29021f36f774SAl Viro 	}
2903a2c36b45SAl Viro 
2904ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2905fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2906fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2907bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
290877d660a8SMiklos Szeredi 			symlink_ok = true;
2909fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2910e97cdc87SAl Viro 		error = lookup_fast(nd, path, &inode);
291171574865SMiklos Szeredi 		if (likely(!error))
291271574865SMiklos Szeredi 			goto finish_lookup;
291371574865SMiklos Szeredi 
2914ce57dfc1SAl Viro 		if (error < 0)
29152675a4ebSAl Viro 			goto out;
2916a1eb3315SMiklos Szeredi 
291737d7fffcSMiklos Szeredi 		BUG_ON(nd->inode != dir->d_inode);
2918b6183df7SMiklos Szeredi 	} else {
2919fe2d35ffSAl Viro 		/* create side of things */
2920a3fbbde7SAl Viro 		/*
2921b6183df7SMiklos Szeredi 		 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2922b6183df7SMiklos Szeredi 		 * has been cleared when we got to the last component we are
2923b6183df7SMiklos Szeredi 		 * about to look up
2924a3fbbde7SAl Viro 		 */
29259f1fafeeSAl Viro 		error = complete_walk(nd);
29269f1fafeeSAl Viro 		if (error)
29272675a4ebSAl Viro 			return error;
2928fe2d35ffSAl Viro 
292933e2208aSJeff Layton 		audit_inode(name, dir, LOOKUP_PARENT);
293016c2cd71SAl Viro 		error = -EISDIR;
29311f36f774SAl Viro 		/* trailing slashes? */
293231e6b01fSNick Piggin 		if (nd->last.name[nd->last.len])
29332675a4ebSAl Viro 			goto out;
2934b6183df7SMiklos Szeredi 	}
29351f36f774SAl Viro 
293616b1c1cdSMiklos Szeredi retry_lookup:
293764894cf8SAl Viro 	if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
293864894cf8SAl Viro 		error = mnt_want_write(nd->path.mnt);
293964894cf8SAl Viro 		if (!error)
294064894cf8SAl Viro 			got_write = true;
294164894cf8SAl Viro 		/*
294264894cf8SAl Viro 		 * do _not_ fail yet - we might not need that or fail with
294364894cf8SAl Viro 		 * a different error; let lookup_open() decide; we'll be
294464894cf8SAl Viro 		 * dropping this one anyway.
294564894cf8SAl Viro 		 */
294664894cf8SAl Viro 	}
2947a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
294864894cf8SAl Viro 	error = lookup_open(nd, path, file, op, got_write, opened);
2949fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2950fb1cc555SAl Viro 
29512675a4ebSAl Viro 	if (error <= 0) {
29522675a4ebSAl Viro 		if (error)
2953d58ffd35SMiklos Szeredi 			goto out;
29546c0d46c4SAl Viro 
295547237687SAl Viro 		if ((*opened & FILE_CREATED) ||
2956496ad9aaSAl Viro 		    !S_ISREG(file_inode(file)->i_mode))
295777d660a8SMiklos Szeredi 			will_truncate = false;
2958d18e9008SMiklos Szeredi 
2959adb5c247SJeff Layton 		audit_inode(name, file->f_path.dentry, 0);
2960d18e9008SMiklos Szeredi 		goto opened;
2961d18e9008SMiklos Szeredi 	}
2962d18e9008SMiklos Szeredi 
296347237687SAl Viro 	if (*opened & FILE_CREATED) {
29649b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2965ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
296677d660a8SMiklos Szeredi 		will_truncate = false;
2967bcda7652SAl Viro 		acc_mode = MAY_OPEN;
2968d58ffd35SMiklos Szeredi 		path_to_nameidata(path, nd);
2969e83db167SMiklos Szeredi 		goto finish_open_created;
2970fb1cc555SAl Viro 	}
2971fb1cc555SAl Viro 
2972fb1cc555SAl Viro 	/*
29733134f37eSJeff Layton 	 * create/update audit record if it already exists.
2974fb1cc555SAl Viro 	 */
2975b18825a7SDavid Howells 	if (d_is_positive(path->dentry))
2976adb5c247SJeff Layton 		audit_inode(name, path->dentry, 0);
2977fb1cc555SAl Viro 
2978d18e9008SMiklos Szeredi 	/*
2979d18e9008SMiklos Szeredi 	 * If atomic_open() acquired write access it is dropped now due to
2980d18e9008SMiklos Szeredi 	 * possible mount and symlink following (this might be optimized away if
2981d18e9008SMiklos Szeredi 	 * necessary...)
2982d18e9008SMiklos Szeredi 	 */
298364894cf8SAl Viro 	if (got_write) {
2984d18e9008SMiklos Szeredi 		mnt_drop_write(nd->path.mnt);
298564894cf8SAl Viro 		got_write = false;
2986d18e9008SMiklos Szeredi 	}
2987d18e9008SMiklos Szeredi 
2988fb1cc555SAl Viro 	error = -EEXIST;
2989f8310c59SAl Viro 	if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
2990fb1cc555SAl Viro 		goto exit_dput;
2991fb1cc555SAl Viro 
29929875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
29939875cf80SDavid Howells 	if (error < 0)
2994fb1cc555SAl Viro 		goto exit_dput;
2995fb1cc555SAl Viro 
2996a3fbbde7SAl Viro 	if (error)
2997a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
2998a3fbbde7SAl Viro 
2999decf3400SMiklos Szeredi 	BUG_ON(nd->flags & LOOKUP_RCU);
3000decf3400SMiklos Szeredi 	inode = path->dentry->d_inode;
30015f5daac1SMiklos Szeredi finish_lookup:
30025f5daac1SMiklos Szeredi 	/* we _can_ be in RCU mode here */
3003fb1cc555SAl Viro 	error = -ENOENT;
300422213318SAl Viro 	if (!inode || d_is_negative(path->dentry)) {
300554c33e7fSMiklos Szeredi 		path_to_nameidata(path, nd);
30062675a4ebSAl Viro 		goto out;
300754c33e7fSMiklos Szeredi 	}
30089e67f361SAl Viro 
3009b18825a7SDavid Howells 	if (should_follow_link(path->dentry, !symlink_ok)) {
3010d45ea867SMiklos Szeredi 		if (nd->flags & LOOKUP_RCU) {
3011d45ea867SMiklos Szeredi 			if (unlikely(unlazy_walk(nd, path->dentry))) {
3012d45ea867SMiklos Szeredi 				error = -ECHILD;
30132675a4ebSAl Viro 				goto out;
3014d45ea867SMiklos Szeredi 			}
3015d45ea867SMiklos Szeredi 		}
3016d45ea867SMiklos Szeredi 		BUG_ON(inode != path->dentry->d_inode);
30172675a4ebSAl Viro 		return 1;
3018d45ea867SMiklos Szeredi 	}
3019fb1cc555SAl Viro 
302016b1c1cdSMiklos Szeredi 	if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
3021fb1cc555SAl Viro 		path_to_nameidata(path, nd);
302216b1c1cdSMiklos Szeredi 	} else {
302316b1c1cdSMiklos Szeredi 		save_parent.dentry = nd->path.dentry;
302416b1c1cdSMiklos Szeredi 		save_parent.mnt = mntget(path->mnt);
302516b1c1cdSMiklos Szeredi 		nd->path.dentry = path->dentry;
302616b1c1cdSMiklos Szeredi 
302716b1c1cdSMiklos Szeredi 	}
3028decf3400SMiklos Szeredi 	nd->inode = inode;
3029a3fbbde7SAl Viro 	/* Why this, you ask?  _Now_ we might have grown LOOKUP_JUMPED... */
3030bc77daa7SAl Viro finish_open:
3031a3fbbde7SAl Viro 	error = complete_walk(nd);
303216b1c1cdSMiklos Szeredi 	if (error) {
303316b1c1cdSMiklos Szeredi 		path_put(&save_parent);
30342675a4ebSAl Viro 		return error;
303516b1c1cdSMiklos Szeredi 	}
3036bc77daa7SAl Viro 	audit_inode(name, nd->path.dentry, 0);
3037fb1cc555SAl Viro 	error = -EISDIR;
303844b1d530SMiklos Szeredi 	if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
30392675a4ebSAl Viro 		goto out;
3040af2f5542SMiklos Szeredi 	error = -ENOTDIR;
304144b1d530SMiklos Szeredi 	if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
30422675a4ebSAl Viro 		goto out;
30436c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
304477d660a8SMiklos Szeredi 		will_truncate = false;
30456c0d46c4SAl Viro 
30460f9d1a10SAl Viro 	if (will_truncate) {
30470f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
30480f9d1a10SAl Viro 		if (error)
30492675a4ebSAl Viro 			goto out;
305064894cf8SAl Viro 		got_write = true;
30510f9d1a10SAl Viro 	}
3052e83db167SMiklos Szeredi finish_open_created:
3053bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
3054ca344a89SAl Viro 	if (error)
30552675a4ebSAl Viro 		goto out;
305630d90494SAl Viro 	file->f_path.mnt = nd->path.mnt;
305730d90494SAl Viro 	error = finish_open(file, nd->path.dentry, NULL, opened);
305830d90494SAl Viro 	if (error) {
305930d90494SAl Viro 		if (error == -EOPENSTALE)
3060f60dc3dbSMiklos Szeredi 			goto stale_open;
3061015c3bbcSMiklos Szeredi 		goto out;
3062f60dc3dbSMiklos Szeredi 	}
3063a8277b9bSMiklos Szeredi opened:
30642675a4ebSAl Viro 	error = open_check_o_direct(file);
3065015c3bbcSMiklos Szeredi 	if (error)
3066015c3bbcSMiklos Szeredi 		goto exit_fput;
30672675a4ebSAl Viro 	error = ima_file_check(file, op->acc_mode);
3068aa4caadbSMiklos Szeredi 	if (error)
3069aa4caadbSMiklos Szeredi 		goto exit_fput;
3070aa4caadbSMiklos Szeredi 
30710f9d1a10SAl Viro 	if (will_truncate) {
30722675a4ebSAl Viro 		error = handle_truncate(file);
3073aa4caadbSMiklos Szeredi 		if (error)
3074aa4caadbSMiklos Szeredi 			goto exit_fput;
30750f9d1a10SAl Viro 	}
3076ca344a89SAl Viro out:
307764894cf8SAl Viro 	if (got_write)
30780f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
307916b1c1cdSMiklos Szeredi 	path_put(&save_parent);
3080e276ae67SMiklos Szeredi 	terminate_walk(nd);
30812675a4ebSAl Viro 	return error;
3082fb1cc555SAl Viro 
3083fb1cc555SAl Viro exit_dput:
3084fb1cc555SAl Viro 	path_put_conditional(path, nd);
3085ca344a89SAl Viro 	goto out;
3086015c3bbcSMiklos Szeredi exit_fput:
30872675a4ebSAl Viro 	fput(file);
30882675a4ebSAl Viro 	goto out;
3089015c3bbcSMiklos Szeredi 
3090f60dc3dbSMiklos Szeredi stale_open:
3091f60dc3dbSMiklos Szeredi 	/* If no saved parent or already retried then can't retry */
3092f60dc3dbSMiklos Szeredi 	if (!save_parent.dentry || retried)
3093f60dc3dbSMiklos Szeredi 		goto out;
3094f60dc3dbSMiklos Szeredi 
3095f60dc3dbSMiklos Szeredi 	BUG_ON(save_parent.dentry != dir);
3096f60dc3dbSMiklos Szeredi 	path_put(&nd->path);
3097f60dc3dbSMiklos Szeredi 	nd->path = save_parent;
3098f60dc3dbSMiklos Szeredi 	nd->inode = dir->d_inode;
3099f60dc3dbSMiklos Szeredi 	save_parent.mnt = NULL;
3100f60dc3dbSMiklos Szeredi 	save_parent.dentry = NULL;
310164894cf8SAl Viro 	if (got_write) {
3102f60dc3dbSMiklos Szeredi 		mnt_drop_write(nd->path.mnt);
310364894cf8SAl Viro 		got_write = false;
3104f60dc3dbSMiklos Szeredi 	}
3105f60dc3dbSMiklos Szeredi 	retried = true;
3106f60dc3dbSMiklos Szeredi 	goto retry_lookup;
3107fb1cc555SAl Viro }
3108fb1cc555SAl Viro 
310960545d0dSAl Viro static int do_tmpfile(int dfd, struct filename *pathname,
311060545d0dSAl Viro 		struct nameidata *nd, int flags,
311160545d0dSAl Viro 		const struct open_flags *op,
311260545d0dSAl Viro 		struct file *file, int *opened)
311360545d0dSAl Viro {
311460545d0dSAl Viro 	static const struct qstr name = QSTR_INIT("/", 1);
311560545d0dSAl Viro 	struct dentry *dentry, *child;
311660545d0dSAl Viro 	struct inode *dir;
311760545d0dSAl Viro 	int error = path_lookupat(dfd, pathname->name,
311860545d0dSAl Viro 				  flags | LOOKUP_DIRECTORY, nd);
311960545d0dSAl Viro 	if (unlikely(error))
312060545d0dSAl Viro 		return error;
312160545d0dSAl Viro 	error = mnt_want_write(nd->path.mnt);
312260545d0dSAl Viro 	if (unlikely(error))
312360545d0dSAl Viro 		goto out;
312460545d0dSAl Viro 	/* we want directory to be writable */
312560545d0dSAl Viro 	error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC);
312660545d0dSAl Viro 	if (error)
312760545d0dSAl Viro 		goto out2;
312860545d0dSAl Viro 	dentry = nd->path.dentry;
312960545d0dSAl Viro 	dir = dentry->d_inode;
313060545d0dSAl Viro 	if (!dir->i_op->tmpfile) {
313160545d0dSAl Viro 		error = -EOPNOTSUPP;
313260545d0dSAl Viro 		goto out2;
313360545d0dSAl Viro 	}
313460545d0dSAl Viro 	child = d_alloc(dentry, &name);
313560545d0dSAl Viro 	if (unlikely(!child)) {
313660545d0dSAl Viro 		error = -ENOMEM;
313760545d0dSAl Viro 		goto out2;
313860545d0dSAl Viro 	}
313960545d0dSAl Viro 	nd->flags &= ~LOOKUP_DIRECTORY;
314060545d0dSAl Viro 	nd->flags |= op->intent;
314160545d0dSAl Viro 	dput(nd->path.dentry);
314260545d0dSAl Viro 	nd->path.dentry = child;
314360545d0dSAl Viro 	error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode);
314460545d0dSAl Viro 	if (error)
314560545d0dSAl Viro 		goto out2;
314660545d0dSAl Viro 	audit_inode(pathname, nd->path.dentry, 0);
314760545d0dSAl Viro 	error = may_open(&nd->path, op->acc_mode, op->open_flag);
314860545d0dSAl Viro 	if (error)
314960545d0dSAl Viro 		goto out2;
315060545d0dSAl Viro 	file->f_path.mnt = nd->path.mnt;
315160545d0dSAl Viro 	error = finish_open(file, nd->path.dentry, NULL, opened);
315260545d0dSAl Viro 	if (error)
315360545d0dSAl Viro 		goto out2;
315460545d0dSAl Viro 	error = open_check_o_direct(file);
3155f4e0c30cSAl Viro 	if (error) {
315660545d0dSAl Viro 		fput(file);
3157f4e0c30cSAl Viro 	} else if (!(op->open_flag & O_EXCL)) {
3158f4e0c30cSAl Viro 		struct inode *inode = file_inode(file);
3159f4e0c30cSAl Viro 		spin_lock(&inode->i_lock);
3160f4e0c30cSAl Viro 		inode->i_state |= I_LINKABLE;
3161f4e0c30cSAl Viro 		spin_unlock(&inode->i_lock);
3162f4e0c30cSAl Viro 	}
316360545d0dSAl Viro out2:
316460545d0dSAl Viro 	mnt_drop_write(nd->path.mnt);
316560545d0dSAl Viro out:
316660545d0dSAl Viro 	path_put(&nd->path);
316760545d0dSAl Viro 	return error;
316860545d0dSAl Viro }
316960545d0dSAl Viro 
3170669abf4eSJeff Layton static struct file *path_openat(int dfd, struct filename *pathname,
317173d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
31721da177e4SLinus Torvalds {
3173fe2d35ffSAl Viro 	struct file *base = NULL;
317430d90494SAl Viro 	struct file *file;
31759850c056SAl Viro 	struct path path;
317647237687SAl Viro 	int opened = 0;
317713aab428SAl Viro 	int error;
317831e6b01fSNick Piggin 
317930d90494SAl Viro 	file = get_empty_filp();
31801afc99beSAl Viro 	if (IS_ERR(file))
31811afc99beSAl Viro 		return file;
318231e6b01fSNick Piggin 
318330d90494SAl Viro 	file->f_flags = op->open_flag;
318431e6b01fSNick Piggin 
3185bb458c64SAl Viro 	if (unlikely(file->f_flags & __O_TMPFILE)) {
318660545d0dSAl Viro 		error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened);
318760545d0dSAl Viro 		goto out;
318860545d0dSAl Viro 	}
318960545d0dSAl Viro 
3190669abf4eSJeff Layton 	error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
319131e6b01fSNick Piggin 	if (unlikely(error))
31922675a4ebSAl Viro 		goto out;
319331e6b01fSNick Piggin 
3194fe2d35ffSAl Viro 	current->total_link_count = 0;
3195669abf4eSJeff Layton 	error = link_path_walk(pathname->name, nd);
319631e6b01fSNick Piggin 	if (unlikely(error))
31972675a4ebSAl Viro 		goto out;
31981da177e4SLinus Torvalds 
31992675a4ebSAl Viro 	error = do_last(nd, &path, file, op, &opened, pathname);
32002675a4ebSAl Viro 	while (unlikely(error > 0)) { /* trailing symlink */
32017b9337aaSNick Piggin 		struct path link = path;
3202def4af30SAl Viro 		void *cookie;
3203574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
320473d049a4SAl Viro 			path_put_conditional(&path, nd);
320573d049a4SAl Viro 			path_put(&nd->path);
32062675a4ebSAl Viro 			error = -ELOOP;
320740b39136SAl Viro 			break;
320840b39136SAl Viro 		}
3209800179c9SKees Cook 		error = may_follow_link(&link, nd);
3210800179c9SKees Cook 		if (unlikely(error))
3211800179c9SKees Cook 			break;
321273d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
321373d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
3214574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
3215c3e380b0SAl Viro 		if (unlikely(error))
32162675a4ebSAl Viro 			break;
32172675a4ebSAl Viro 		error = do_last(nd, &path, file, op, &opened, pathname);
3218574197e0SAl Viro 		put_link(nd, &link, cookie);
3219806b681cSAl Viro 	}
322010fa8e62SAl Viro out:
322173d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
322273d049a4SAl Viro 		path_put(&nd->root);
3223fe2d35ffSAl Viro 	if (base)
3224fe2d35ffSAl Viro 		fput(base);
32252675a4ebSAl Viro 	if (!(opened & FILE_OPENED)) {
32262675a4ebSAl Viro 		BUG_ON(!error);
322730d90494SAl Viro 		put_filp(file);
3228015c3bbcSMiklos Szeredi 	}
32292675a4ebSAl Viro 	if (unlikely(error)) {
32302675a4ebSAl Viro 		if (error == -EOPENSTALE) {
32312675a4ebSAl Viro 			if (flags & LOOKUP_RCU)
32322675a4ebSAl Viro 				error = -ECHILD;
32332675a4ebSAl Viro 			else
32342675a4ebSAl Viro 				error = -ESTALE;
32352675a4ebSAl Viro 		}
32362675a4ebSAl Viro 		file = ERR_PTR(error);
32372675a4ebSAl Viro 	}
32382675a4ebSAl Viro 	return file;
3239de459215SKirill Korotaev }
32401da177e4SLinus Torvalds 
3241669abf4eSJeff Layton struct file *do_filp_open(int dfd, struct filename *pathname,
3242f9652e10SAl Viro 		const struct open_flags *op)
324313aab428SAl Viro {
324473d049a4SAl Viro 	struct nameidata nd;
3245f9652e10SAl Viro 	int flags = op->lookup_flags;
324613aab428SAl Viro 	struct file *filp;
324713aab428SAl Viro 
324873d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
324913aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
325073d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
325113aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
325273d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
325313aab428SAl Viro 	return filp;
325413aab428SAl Viro }
325513aab428SAl Viro 
325673d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
3257f9652e10SAl Viro 		const char *name, const struct open_flags *op)
325873d049a4SAl Viro {
325973d049a4SAl Viro 	struct nameidata nd;
326073d049a4SAl Viro 	struct file *file;
3261669abf4eSJeff Layton 	struct filename filename = { .name = name };
3262f9652e10SAl Viro 	int flags = op->lookup_flags | LOOKUP_ROOT;
326373d049a4SAl Viro 
326473d049a4SAl Viro 	nd.root.mnt = mnt;
326573d049a4SAl Viro 	nd.root.dentry = dentry;
326673d049a4SAl Viro 
3267b18825a7SDavid Howells 	if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
326873d049a4SAl Viro 		return ERR_PTR(-ELOOP);
326973d049a4SAl Viro 
3270669abf4eSJeff Layton 	file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
327173d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
3272669abf4eSJeff Layton 		file = path_openat(-1, &filename, &nd, op, flags);
327373d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
3274669abf4eSJeff Layton 		file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
327573d049a4SAl Viro 	return file;
327673d049a4SAl Viro }
327773d049a4SAl Viro 
32781ac12b4bSJeff Layton struct dentry *kern_path_create(int dfd, const char *pathname,
32791ac12b4bSJeff Layton 				struct path *path, unsigned int lookup_flags)
32801da177e4SLinus Torvalds {
3281c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
3282ed75e95dSAl Viro 	struct nameidata nd;
3283c30dabfeSJan Kara 	int err2;
32841ac12b4bSJeff Layton 	int error;
32851ac12b4bSJeff Layton 	bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
32861ac12b4bSJeff Layton 
32871ac12b4bSJeff Layton 	/*
32881ac12b4bSJeff Layton 	 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
32891ac12b4bSJeff Layton 	 * other flags passed in are ignored!
32901ac12b4bSJeff Layton 	 */
32911ac12b4bSJeff Layton 	lookup_flags &= LOOKUP_REVAL;
32921ac12b4bSJeff Layton 
32931ac12b4bSJeff Layton 	error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd);
3294ed75e95dSAl Viro 	if (error)
3295ed75e95dSAl Viro 		return ERR_PTR(error);
32961da177e4SLinus Torvalds 
3297c663e5d8SChristoph Hellwig 	/*
3298c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
3299c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
3300c663e5d8SChristoph Hellwig 	 */
3301ed75e95dSAl Viro 	if (nd.last_type != LAST_NORM)
3302ed75e95dSAl Viro 		goto out;
3303ed75e95dSAl Viro 	nd.flags &= ~LOOKUP_PARENT;
3304ed75e95dSAl Viro 	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3305c663e5d8SChristoph Hellwig 
3306c30dabfeSJan Kara 	/* don't fail immediately if it's r/o, at least try to report other errors */
3307c30dabfeSJan Kara 	err2 = mnt_want_write(nd.path.mnt);
3308c663e5d8SChristoph Hellwig 	/*
3309c663e5d8SChristoph Hellwig 	 * Do the final lookup.
3310c663e5d8SChristoph Hellwig 	 */
3311ed75e95dSAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3312ed75e95dSAl Viro 	dentry = lookup_hash(&nd);
33131da177e4SLinus Torvalds 	if (IS_ERR(dentry))
3314a8104a9fSAl Viro 		goto unlock;
3315c663e5d8SChristoph Hellwig 
3316a8104a9fSAl Viro 	error = -EEXIST;
3317b18825a7SDavid Howells 	if (d_is_positive(dentry))
3318a8104a9fSAl Viro 		goto fail;
3319b18825a7SDavid Howells 
3320c663e5d8SChristoph Hellwig 	/*
3321c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
3322c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
3323c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
3324c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
3325c663e5d8SChristoph Hellwig 	 */
3326ed75e95dSAl Viro 	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
3327a8104a9fSAl Viro 		error = -ENOENT;
3328ed75e95dSAl Viro 		goto fail;
3329e9baf6e5SAl Viro 	}
3330c30dabfeSJan Kara 	if (unlikely(err2)) {
3331c30dabfeSJan Kara 		error = err2;
3332a8104a9fSAl Viro 		goto fail;
3333c30dabfeSJan Kara 	}
3334ed75e95dSAl Viro 	*path = nd.path;
3335e9baf6e5SAl Viro 	return dentry;
33361da177e4SLinus Torvalds fail:
3337a8104a9fSAl Viro 	dput(dentry);
3338a8104a9fSAl Viro 	dentry = ERR_PTR(error);
3339a8104a9fSAl Viro unlock:
3340dae6ad8fSAl Viro 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3341c30dabfeSJan Kara 	if (!err2)
3342c30dabfeSJan Kara 		mnt_drop_write(nd.path.mnt);
3343ed75e95dSAl Viro out:
3344dae6ad8fSAl Viro 	path_put(&nd.path);
3345ed75e95dSAl Viro 	return dentry;
3346dae6ad8fSAl Viro }
3347dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create);
3348dae6ad8fSAl Viro 
3349921a1650SAl Viro void done_path_create(struct path *path, struct dentry *dentry)
3350921a1650SAl Viro {
3351921a1650SAl Viro 	dput(dentry);
3352921a1650SAl Viro 	mutex_unlock(&path->dentry->d_inode->i_mutex);
3353a8104a9fSAl Viro 	mnt_drop_write(path->mnt);
3354921a1650SAl Viro 	path_put(path);
3355921a1650SAl Viro }
3356921a1650SAl Viro EXPORT_SYMBOL(done_path_create);
3357921a1650SAl Viro 
33581ac12b4bSJeff Layton struct dentry *user_path_create(int dfd, const char __user *pathname,
33591ac12b4bSJeff Layton 				struct path *path, unsigned int lookup_flags)
3360dae6ad8fSAl Viro {
336191a27b2aSJeff Layton 	struct filename *tmp = getname(pathname);
3362dae6ad8fSAl Viro 	struct dentry *res;
3363dae6ad8fSAl Viro 	if (IS_ERR(tmp))
3364dae6ad8fSAl Viro 		return ERR_CAST(tmp);
33651ac12b4bSJeff Layton 	res = kern_path_create(dfd, tmp->name, path, lookup_flags);
3366dae6ad8fSAl Viro 	putname(tmp);
3367dae6ad8fSAl Viro 	return res;
3368dae6ad8fSAl Viro }
3369dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create);
3370dae6ad8fSAl Viro 
33711a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
33721da177e4SLinus Torvalds {
3373a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
33741da177e4SLinus Torvalds 
33751da177e4SLinus Torvalds 	if (error)
33761da177e4SLinus Torvalds 		return error;
33771da177e4SLinus Torvalds 
3378975d6b39SEric W. Biederman 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
33791da177e4SLinus Torvalds 		return -EPERM;
33801da177e4SLinus Torvalds 
3381acfa4380SAl Viro 	if (!dir->i_op->mknod)
33821da177e4SLinus Torvalds 		return -EPERM;
33831da177e4SLinus Torvalds 
338408ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
338508ce5f16SSerge E. Hallyn 	if (error)
338608ce5f16SSerge E. Hallyn 		return error;
338708ce5f16SSerge E. Hallyn 
33881da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
33891da177e4SLinus Torvalds 	if (error)
33901da177e4SLinus Torvalds 		return error;
33911da177e4SLinus Torvalds 
33921da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
3393a74574aaSStephen Smalley 	if (!error)
3394f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
33951da177e4SLinus Torvalds 	return error;
33961da177e4SLinus Torvalds }
33974d359507SAl Viro EXPORT_SYMBOL(vfs_mknod);
33981da177e4SLinus Torvalds 
3399f69aac00SAl Viro static int may_mknod(umode_t mode)
3400463c3197SDave Hansen {
3401463c3197SDave Hansen 	switch (mode & S_IFMT) {
3402463c3197SDave Hansen 	case S_IFREG:
3403463c3197SDave Hansen 	case S_IFCHR:
3404463c3197SDave Hansen 	case S_IFBLK:
3405463c3197SDave Hansen 	case S_IFIFO:
3406463c3197SDave Hansen 	case S_IFSOCK:
3407463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
3408463c3197SDave Hansen 		return 0;
3409463c3197SDave Hansen 	case S_IFDIR:
3410463c3197SDave Hansen 		return -EPERM;
3411463c3197SDave Hansen 	default:
3412463c3197SDave Hansen 		return -EINVAL;
3413463c3197SDave Hansen 	}
3414463c3197SDave Hansen }
3415463c3197SDave Hansen 
34168208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
34172e4d0924SHeiko Carstens 		unsigned, dev)
34181da177e4SLinus Torvalds {
34191da177e4SLinus Torvalds 	struct dentry *dentry;
3420dae6ad8fSAl Viro 	struct path path;
3421dae6ad8fSAl Viro 	int error;
3422972567f1SJeff Layton 	unsigned int lookup_flags = 0;
34231da177e4SLinus Torvalds 
34248e4bfca1SAl Viro 	error = may_mknod(mode);
34258e4bfca1SAl Viro 	if (error)
34268e4bfca1SAl Viro 		return error;
3427972567f1SJeff Layton retry:
3428972567f1SJeff Layton 	dentry = user_path_create(dfd, filename, &path, lookup_flags);
3429dae6ad8fSAl Viro 	if (IS_ERR(dentry))
3430dae6ad8fSAl Viro 		return PTR_ERR(dentry);
34312ad94ae6SAl Viro 
3432dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3433ce3b0f8dSAl Viro 		mode &= ~current_umask();
3434dae6ad8fSAl Viro 	error = security_path_mknod(&path, dentry, mode, dev);
3435be6d3e56SKentaro Takeda 	if (error)
3436a8104a9fSAl Viro 		goto out;
34371da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
34381da177e4SLinus Torvalds 		case 0: case S_IFREG:
3439312b63fbSAl Viro 			error = vfs_create(path.dentry->d_inode,dentry,mode,true);
34401da177e4SLinus Torvalds 			break;
34411da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
3442dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
34431da177e4SLinus Torvalds 					new_decode_dev(dev));
34441da177e4SLinus Torvalds 			break;
34451da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
3446dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
34471da177e4SLinus Torvalds 			break;
34481da177e4SLinus Torvalds 	}
3449a8104a9fSAl Viro out:
3450921a1650SAl Viro 	done_path_create(&path, dentry);
3451972567f1SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3452972567f1SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3453972567f1SJeff Layton 		goto retry;
3454972567f1SJeff Layton 	}
34551da177e4SLinus Torvalds 	return error;
34561da177e4SLinus Torvalds }
34571da177e4SLinus Torvalds 
34588208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
34595590ff0dSUlrich Drepper {
34605590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
34615590ff0dSUlrich Drepper }
34625590ff0dSUlrich Drepper 
346318bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
34641da177e4SLinus Torvalds {
3465a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
34668de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
34671da177e4SLinus Torvalds 
34681da177e4SLinus Torvalds 	if (error)
34691da177e4SLinus Torvalds 		return error;
34701da177e4SLinus Torvalds 
3471acfa4380SAl Viro 	if (!dir->i_op->mkdir)
34721da177e4SLinus Torvalds 		return -EPERM;
34731da177e4SLinus Torvalds 
34741da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
34751da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
34761da177e4SLinus Torvalds 	if (error)
34771da177e4SLinus Torvalds 		return error;
34781da177e4SLinus Torvalds 
34798de52778SAl Viro 	if (max_links && dir->i_nlink >= max_links)
34808de52778SAl Viro 		return -EMLINK;
34818de52778SAl Viro 
34821da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
3483a74574aaSStephen Smalley 	if (!error)
3484f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
34851da177e4SLinus Torvalds 	return error;
34861da177e4SLinus Torvalds }
34874d359507SAl Viro EXPORT_SYMBOL(vfs_mkdir);
34881da177e4SLinus Torvalds 
3489a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
34901da177e4SLinus Torvalds {
34916902d925SDave Hansen 	struct dentry *dentry;
3492dae6ad8fSAl Viro 	struct path path;
3493dae6ad8fSAl Viro 	int error;
3494b76d8b82SJeff Layton 	unsigned int lookup_flags = LOOKUP_DIRECTORY;
34951da177e4SLinus Torvalds 
3496b76d8b82SJeff Layton retry:
3497b76d8b82SJeff Layton 	dentry = user_path_create(dfd, pathname, &path, lookup_flags);
34986902d925SDave Hansen 	if (IS_ERR(dentry))
3499dae6ad8fSAl Viro 		return PTR_ERR(dentry);
35006902d925SDave Hansen 
3501dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3502ce3b0f8dSAl Viro 		mode &= ~current_umask();
3503dae6ad8fSAl Viro 	error = security_path_mkdir(&path, dentry, mode);
3504a8104a9fSAl Viro 	if (!error)
3505dae6ad8fSAl Viro 		error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3506921a1650SAl Viro 	done_path_create(&path, dentry);
3507b76d8b82SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3508b76d8b82SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3509b76d8b82SJeff Layton 		goto retry;
3510b76d8b82SJeff Layton 	}
35111da177e4SLinus Torvalds 	return error;
35121da177e4SLinus Torvalds }
35131da177e4SLinus Torvalds 
3514a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
35155590ff0dSUlrich Drepper {
35165590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
35175590ff0dSUlrich Drepper }
35185590ff0dSUlrich Drepper 
35191da177e4SLinus Torvalds /*
3520a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
3521c0d02594SJ. Bruce Fields  * should have a usage count of 1 if we're the only user of this
3522a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
3523a71905f0SSage Weil  * then we drop the dentry now.
35241da177e4SLinus Torvalds  *
35251da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
35261da177e4SLinus Torvalds  * do a
35271da177e4SLinus Torvalds  *
35281da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
35291da177e4SLinus Torvalds  *		return -EBUSY;
35301da177e4SLinus Torvalds  *
35311da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
35321da177e4SLinus Torvalds  * that is still in use by something else..
35331da177e4SLinus Torvalds  */
35341da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
35351da177e4SLinus Torvalds {
35361da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
35371da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
353898474236SWaiman Long 	if (dentry->d_lockref.count == 1)
35391da177e4SLinus Torvalds 		__d_drop(dentry);
35401da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
35411da177e4SLinus Torvalds }
35424d359507SAl Viro EXPORT_SYMBOL(dentry_unhash);
35431da177e4SLinus Torvalds 
35441da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
35451da177e4SLinus Torvalds {
35461da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
35471da177e4SLinus Torvalds 
35481da177e4SLinus Torvalds 	if (error)
35491da177e4SLinus Torvalds 		return error;
35501da177e4SLinus Torvalds 
3551acfa4380SAl Viro 	if (!dir->i_op->rmdir)
35521da177e4SLinus Torvalds 		return -EPERM;
35531da177e4SLinus Torvalds 
35541d2ef590SAl Viro 	dget(dentry);
35551b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
3556912dbc15SSage Weil 
35571da177e4SLinus Torvalds 	error = -EBUSY;
3558912dbc15SSage Weil 	if (d_mountpoint(dentry))
3559912dbc15SSage Weil 		goto out;
3560912dbc15SSage Weil 
35611da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
3562912dbc15SSage Weil 	if (error)
3563912dbc15SSage Weil 		goto out;
3564912dbc15SSage Weil 
35653cebde24SSage Weil 	shrink_dcache_parent(dentry);
35661da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
3567912dbc15SSage Weil 	if (error)
3568912dbc15SSage Weil 		goto out;
3569912dbc15SSage Weil 
35701da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
3571d83c49f3SAl Viro 	dont_mount(dentry);
35721da177e4SLinus Torvalds 
3573912dbc15SSage Weil out:
3574912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
35751d2ef590SAl Viro 	dput(dentry);
3576912dbc15SSage Weil 	if (!error)
3577912dbc15SSage Weil 		d_delete(dentry);
35781da177e4SLinus Torvalds 	return error;
35791da177e4SLinus Torvalds }
35804d359507SAl Viro EXPORT_SYMBOL(vfs_rmdir);
35811da177e4SLinus Torvalds 
35825590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
35831da177e4SLinus Torvalds {
35841da177e4SLinus Torvalds 	int error = 0;
358591a27b2aSJeff Layton 	struct filename *name;
35861da177e4SLinus Torvalds 	struct dentry *dentry;
35871da177e4SLinus Torvalds 	struct nameidata nd;
3588c6ee9206SJeff Layton 	unsigned int lookup_flags = 0;
3589c6ee9206SJeff Layton retry:
3590c6ee9206SJeff Layton 	name = user_path_parent(dfd, pathname, &nd, lookup_flags);
359191a27b2aSJeff Layton 	if (IS_ERR(name))
359291a27b2aSJeff Layton 		return PTR_ERR(name);
35931da177e4SLinus Torvalds 
35941da177e4SLinus Torvalds 	switch(nd.last_type) {
35951da177e4SLinus Torvalds 	case LAST_DOTDOT:
35961da177e4SLinus Torvalds 		error = -ENOTEMPTY;
35971da177e4SLinus Torvalds 		goto exit1;
35981da177e4SLinus Torvalds 	case LAST_DOT:
35991da177e4SLinus Torvalds 		error = -EINVAL;
36001da177e4SLinus Torvalds 		goto exit1;
36011da177e4SLinus Torvalds 	case LAST_ROOT:
36021da177e4SLinus Torvalds 		error = -EBUSY;
36031da177e4SLinus Torvalds 		goto exit1;
36041da177e4SLinus Torvalds 	}
36050612d9fbSOGAWA Hirofumi 
36060612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
3607c30dabfeSJan Kara 	error = mnt_want_write(nd.path.mnt);
3608c30dabfeSJan Kara 	if (error)
3609c30dabfeSJan Kara 		goto exit1;
36100612d9fbSOGAWA Hirofumi 
36114ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
361249705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
36131da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
36146902d925SDave Hansen 	if (IS_ERR(dentry))
36156902d925SDave Hansen 		goto exit2;
3616e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
3617e6bc45d6STheodore Ts'o 		error = -ENOENT;
3618e6bc45d6STheodore Ts'o 		goto exit3;
3619e6bc45d6STheodore Ts'o 	}
3620be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
3621be6d3e56SKentaro Takeda 	if (error)
3622c30dabfeSJan Kara 		goto exit3;
36234ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
36240622753bSDave Hansen exit3:
36251da177e4SLinus Torvalds 	dput(dentry);
36266902d925SDave Hansen exit2:
36274ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3628c30dabfeSJan Kara 	mnt_drop_write(nd.path.mnt);
36291da177e4SLinus Torvalds exit1:
36301d957f9bSJan Blunck 	path_put(&nd.path);
36311da177e4SLinus Torvalds 	putname(name);
3632c6ee9206SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3633c6ee9206SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3634c6ee9206SJeff Layton 		goto retry;
3635c6ee9206SJeff Layton 	}
36361da177e4SLinus Torvalds 	return error;
36371da177e4SLinus Torvalds }
36381da177e4SLinus Torvalds 
36393cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
36405590ff0dSUlrich Drepper {
36415590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
36425590ff0dSUlrich Drepper }
36435590ff0dSUlrich Drepper 
3644b21996e3SJ. Bruce Fields /**
3645b21996e3SJ. Bruce Fields  * vfs_unlink - unlink a filesystem object
3646b21996e3SJ. Bruce Fields  * @dir:	parent directory
3647b21996e3SJ. Bruce Fields  * @dentry:	victim
3648b21996e3SJ. Bruce Fields  * @delegated_inode: returns victim inode, if the inode is delegated.
3649b21996e3SJ. Bruce Fields  *
3650b21996e3SJ. Bruce Fields  * The caller must hold dir->i_mutex.
3651b21996e3SJ. Bruce Fields  *
3652b21996e3SJ. Bruce Fields  * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3653b21996e3SJ. Bruce Fields  * return a reference to the inode in delegated_inode.  The caller
3654b21996e3SJ. Bruce Fields  * should then break the delegation on that inode and retry.  Because
3655b21996e3SJ. Bruce Fields  * breaking a delegation may take a long time, the caller should drop
3656b21996e3SJ. Bruce Fields  * dir->i_mutex before doing so.
3657b21996e3SJ. Bruce Fields  *
3658b21996e3SJ. Bruce Fields  * Alternatively, a caller may pass NULL for delegated_inode.  This may
3659b21996e3SJ. Bruce Fields  * be appropriate for callers that expect the underlying filesystem not
3660b21996e3SJ. Bruce Fields  * to be NFS exported.
3661b21996e3SJ. Bruce Fields  */
3662b21996e3SJ. Bruce Fields int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
36631da177e4SLinus Torvalds {
36649accbb97SJ. Bruce Fields 	struct inode *target = dentry->d_inode;
36651da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
36661da177e4SLinus Torvalds 
36671da177e4SLinus Torvalds 	if (error)
36681da177e4SLinus Torvalds 		return error;
36691da177e4SLinus Torvalds 
3670acfa4380SAl Viro 	if (!dir->i_op->unlink)
36711da177e4SLinus Torvalds 		return -EPERM;
36721da177e4SLinus Torvalds 
36739accbb97SJ. Bruce Fields 	mutex_lock(&target->i_mutex);
36741da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
36751da177e4SLinus Torvalds 		error = -EBUSY;
36761da177e4SLinus Torvalds 	else {
36771da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
3678bec1052eSAl Viro 		if (!error) {
36795a14696cSJ. Bruce Fields 			error = try_break_deleg(target, delegated_inode);
36805a14696cSJ. Bruce Fields 			if (error)
3681b21996e3SJ. Bruce Fields 				goto out;
36821da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
3683bec1052eSAl Viro 			if (!error)
3684d83c49f3SAl Viro 				dont_mount(dentry);
3685bec1052eSAl Viro 		}
36861da177e4SLinus Torvalds 	}
3687b21996e3SJ. Bruce Fields out:
36889accbb97SJ. Bruce Fields 	mutex_unlock(&target->i_mutex);
36891da177e4SLinus Torvalds 
36901da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
36911da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
36929accbb97SJ. Bruce Fields 		fsnotify_link_count(target);
36931da177e4SLinus Torvalds 		d_delete(dentry);
36941da177e4SLinus Torvalds 	}
36950eeca283SRobert Love 
36961da177e4SLinus Torvalds 	return error;
36971da177e4SLinus Torvalds }
36984d359507SAl Viro EXPORT_SYMBOL(vfs_unlink);
36991da177e4SLinus Torvalds 
37001da177e4SLinus Torvalds /*
37011da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
37021b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
37031da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
37041da177e4SLinus Torvalds  * while waiting on the I/O.
37051da177e4SLinus Torvalds  */
37065590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
37071da177e4SLinus Torvalds {
37082ad94ae6SAl Viro 	int error;
370991a27b2aSJeff Layton 	struct filename *name;
37101da177e4SLinus Torvalds 	struct dentry *dentry;
37111da177e4SLinus Torvalds 	struct nameidata nd;
37121da177e4SLinus Torvalds 	struct inode *inode = NULL;
3713b21996e3SJ. Bruce Fields 	struct inode *delegated_inode = NULL;
37145d18f813SJeff Layton 	unsigned int lookup_flags = 0;
37155d18f813SJeff Layton retry:
37165d18f813SJeff Layton 	name = user_path_parent(dfd, pathname, &nd, lookup_flags);
371791a27b2aSJeff Layton 	if (IS_ERR(name))
371891a27b2aSJeff Layton 		return PTR_ERR(name);
37192ad94ae6SAl Viro 
37201da177e4SLinus Torvalds 	error = -EISDIR;
37211da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
37221da177e4SLinus Torvalds 		goto exit1;
37230612d9fbSOGAWA Hirofumi 
37240612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
3725c30dabfeSJan Kara 	error = mnt_want_write(nd.path.mnt);
3726c30dabfeSJan Kara 	if (error)
3727c30dabfeSJan Kara 		goto exit1;
3728b21996e3SJ. Bruce Fields retry_deleg:
37294ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
373049705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
37311da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
37321da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
37331da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
373450338b88STörök Edwin 		if (nd.last.name[nd.last.len])
373550338b88STörök Edwin 			goto slashes;
37361da177e4SLinus Torvalds 		inode = dentry->d_inode;
3737b18825a7SDavid Howells 		if (d_is_negative(dentry))
3738e6bc45d6STheodore Ts'o 			goto slashes;
37397de9c6eeSAl Viro 		ihold(inode);
3740be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
3741be6d3e56SKentaro Takeda 		if (error)
3742c30dabfeSJan Kara 			goto exit2;
3743b21996e3SJ. Bruce Fields 		error = vfs_unlink(nd.path.dentry->d_inode, dentry, &delegated_inode);
37441da177e4SLinus Torvalds exit2:
37451da177e4SLinus Torvalds 		dput(dentry);
37461da177e4SLinus Torvalds 	}
37474ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
37481da177e4SLinus Torvalds 	if (inode)
37491da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
3750b21996e3SJ. Bruce Fields 	inode = NULL;
3751b21996e3SJ. Bruce Fields 	if (delegated_inode) {
37525a14696cSJ. Bruce Fields 		error = break_deleg_wait(&delegated_inode);
3753b21996e3SJ. Bruce Fields 		if (!error)
3754b21996e3SJ. Bruce Fields 			goto retry_deleg;
3755b21996e3SJ. Bruce Fields 	}
3756c30dabfeSJan Kara 	mnt_drop_write(nd.path.mnt);
37571da177e4SLinus Torvalds exit1:
37581d957f9bSJan Blunck 	path_put(&nd.path);
37591da177e4SLinus Torvalds 	putname(name);
37605d18f813SJeff Layton 	if (retry_estale(error, lookup_flags)) {
37615d18f813SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
37625d18f813SJeff Layton 		inode = NULL;
37635d18f813SJeff Layton 		goto retry;
37645d18f813SJeff Layton 	}
37651da177e4SLinus Torvalds 	return error;
37661da177e4SLinus Torvalds 
37671da177e4SLinus Torvalds slashes:
3768b18825a7SDavid Howells 	if (d_is_negative(dentry))
3769b18825a7SDavid Howells 		error = -ENOENT;
377044b1d530SMiklos Szeredi 	else if (d_is_dir(dentry))
3771b18825a7SDavid Howells 		error = -EISDIR;
3772b18825a7SDavid Howells 	else
3773b18825a7SDavid Howells 		error = -ENOTDIR;
37741da177e4SLinus Torvalds 	goto exit2;
37751da177e4SLinus Torvalds }
37761da177e4SLinus Torvalds 
37772e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
37785590ff0dSUlrich Drepper {
37795590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
37805590ff0dSUlrich Drepper 		return -EINVAL;
37815590ff0dSUlrich Drepper 
37825590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
37835590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
37845590ff0dSUlrich Drepper 
37855590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
37865590ff0dSUlrich Drepper }
37875590ff0dSUlrich Drepper 
37883480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
37895590ff0dSUlrich Drepper {
37905590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
37915590ff0dSUlrich Drepper }
37925590ff0dSUlrich Drepper 
3793db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
37941da177e4SLinus Torvalds {
3795a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
37961da177e4SLinus Torvalds 
37971da177e4SLinus Torvalds 	if (error)
37981da177e4SLinus Torvalds 		return error;
37991da177e4SLinus Torvalds 
3800acfa4380SAl Viro 	if (!dir->i_op->symlink)
38011da177e4SLinus Torvalds 		return -EPERM;
38021da177e4SLinus Torvalds 
38031da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
38041da177e4SLinus Torvalds 	if (error)
38051da177e4SLinus Torvalds 		return error;
38061da177e4SLinus Torvalds 
38071da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
3808a74574aaSStephen Smalley 	if (!error)
3809f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
38101da177e4SLinus Torvalds 	return error;
38111da177e4SLinus Torvalds }
38124d359507SAl Viro EXPORT_SYMBOL(vfs_symlink);
38131da177e4SLinus Torvalds 
38142e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
38152e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
38161da177e4SLinus Torvalds {
38172ad94ae6SAl Viro 	int error;
381891a27b2aSJeff Layton 	struct filename *from;
38196902d925SDave Hansen 	struct dentry *dentry;
3820dae6ad8fSAl Viro 	struct path path;
3821f46d3567SJeff Layton 	unsigned int lookup_flags = 0;
38221da177e4SLinus Torvalds 
38231da177e4SLinus Torvalds 	from = getname(oldname);
38241da177e4SLinus Torvalds 	if (IS_ERR(from))
38251da177e4SLinus Torvalds 		return PTR_ERR(from);
3826f46d3567SJeff Layton retry:
3827f46d3567SJeff Layton 	dentry = user_path_create(newdfd, newname, &path, lookup_flags);
38281da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
38296902d925SDave Hansen 	if (IS_ERR(dentry))
3830dae6ad8fSAl Viro 		goto out_putname;
38316902d925SDave Hansen 
383291a27b2aSJeff Layton 	error = security_path_symlink(&path, dentry, from->name);
3833a8104a9fSAl Viro 	if (!error)
383491a27b2aSJeff Layton 		error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
3835921a1650SAl Viro 	done_path_create(&path, dentry);
3836f46d3567SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3837f46d3567SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3838f46d3567SJeff Layton 		goto retry;
3839f46d3567SJeff Layton 	}
38406902d925SDave Hansen out_putname:
38411da177e4SLinus Torvalds 	putname(from);
38421da177e4SLinus Torvalds 	return error;
38431da177e4SLinus Torvalds }
38441da177e4SLinus Torvalds 
38453480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
38465590ff0dSUlrich Drepper {
38475590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
38485590ff0dSUlrich Drepper }
38495590ff0dSUlrich Drepper 
3850146a8595SJ. Bruce Fields /**
3851146a8595SJ. Bruce Fields  * vfs_link - create a new link
3852146a8595SJ. Bruce Fields  * @old_dentry:	object to be linked
3853146a8595SJ. Bruce Fields  * @dir:	new parent
3854146a8595SJ. Bruce Fields  * @new_dentry:	where to create the new link
3855146a8595SJ. Bruce Fields  * @delegated_inode: returns inode needing a delegation break
3856146a8595SJ. Bruce Fields  *
3857146a8595SJ. Bruce Fields  * The caller must hold dir->i_mutex
3858146a8595SJ. Bruce Fields  *
3859146a8595SJ. Bruce Fields  * If vfs_link discovers a delegation on the to-be-linked file in need
3860146a8595SJ. Bruce Fields  * of breaking, it will return -EWOULDBLOCK and return a reference to the
3861146a8595SJ. Bruce Fields  * inode in delegated_inode.  The caller should then break the delegation
3862146a8595SJ. Bruce Fields  * and retry.  Because breaking a delegation may take a long time, the
3863146a8595SJ. Bruce Fields  * caller should drop the i_mutex before doing so.
3864146a8595SJ. Bruce Fields  *
3865146a8595SJ. Bruce Fields  * Alternatively, a caller may pass NULL for delegated_inode.  This may
3866146a8595SJ. Bruce Fields  * be appropriate for callers that expect the underlying filesystem not
3867146a8595SJ. Bruce Fields  * to be NFS exported.
3868146a8595SJ. Bruce Fields  */
3869146a8595SJ. Bruce Fields int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
38701da177e4SLinus Torvalds {
38711da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
38728de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
38731da177e4SLinus Torvalds 	int error;
38741da177e4SLinus Torvalds 
38751da177e4SLinus Torvalds 	if (!inode)
38761da177e4SLinus Torvalds 		return -ENOENT;
38771da177e4SLinus Torvalds 
3878a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
38791da177e4SLinus Torvalds 	if (error)
38801da177e4SLinus Torvalds 		return error;
38811da177e4SLinus Torvalds 
38821da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
38831da177e4SLinus Torvalds 		return -EXDEV;
38841da177e4SLinus Torvalds 
38851da177e4SLinus Torvalds 	/*
38861da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
38871da177e4SLinus Torvalds 	 */
38881da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
38891da177e4SLinus Torvalds 		return -EPERM;
3890acfa4380SAl Viro 	if (!dir->i_op->link)
38911da177e4SLinus Torvalds 		return -EPERM;
38927e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
38931da177e4SLinus Torvalds 		return -EPERM;
38941da177e4SLinus Torvalds 
38951da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
38961da177e4SLinus Torvalds 	if (error)
38971da177e4SLinus Torvalds 		return error;
38981da177e4SLinus Torvalds 
38997e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
3900aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
3901f4e0c30cSAl Viro 	if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
3902aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
39038de52778SAl Viro 	else if (max_links && inode->i_nlink >= max_links)
39048de52778SAl Viro 		error = -EMLINK;
3905146a8595SJ. Bruce Fields 	else {
3906146a8595SJ. Bruce Fields 		error = try_break_deleg(inode, delegated_inode);
3907146a8595SJ. Bruce Fields 		if (!error)
39081da177e4SLinus Torvalds 			error = dir->i_op->link(old_dentry, dir, new_dentry);
3909146a8595SJ. Bruce Fields 	}
3910f4e0c30cSAl Viro 
3911f4e0c30cSAl Viro 	if (!error && (inode->i_state & I_LINKABLE)) {
3912f4e0c30cSAl Viro 		spin_lock(&inode->i_lock);
3913f4e0c30cSAl Viro 		inode->i_state &= ~I_LINKABLE;
3914f4e0c30cSAl Viro 		spin_unlock(&inode->i_lock);
3915f4e0c30cSAl Viro 	}
39167e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
3917e31e14ecSStephen Smalley 	if (!error)
39187e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
39191da177e4SLinus Torvalds 	return error;
39201da177e4SLinus Torvalds }
39214d359507SAl Viro EXPORT_SYMBOL(vfs_link);
39221da177e4SLinus Torvalds 
39231da177e4SLinus Torvalds /*
39241da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
39251da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
39261da177e4SLinus Torvalds  * newname.  --KAB
39271da177e4SLinus Torvalds  *
39281da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
39291da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
39301da177e4SLinus Torvalds  * and other special files.  --ADM
39311da177e4SLinus Torvalds  */
39322e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
39332e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
39341da177e4SLinus Torvalds {
39351da177e4SLinus Torvalds 	struct dentry *new_dentry;
3936dae6ad8fSAl Viro 	struct path old_path, new_path;
3937146a8595SJ. Bruce Fields 	struct inode *delegated_inode = NULL;
393811a7b371SAneesh Kumar K.V 	int how = 0;
39391da177e4SLinus Torvalds 	int error;
39401da177e4SLinus Torvalds 
394111a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3942c04030e1SUlrich Drepper 		return -EINVAL;
394311a7b371SAneesh Kumar K.V 	/*
3944f0cc6ffbSLinus Torvalds 	 * To use null names we require CAP_DAC_READ_SEARCH
3945f0cc6ffbSLinus Torvalds 	 * This ensures that not everyone will be able to create
3946f0cc6ffbSLinus Torvalds 	 * handlink using the passed filedescriptor.
394711a7b371SAneesh Kumar K.V 	 */
3948f0cc6ffbSLinus Torvalds 	if (flags & AT_EMPTY_PATH) {
3949f0cc6ffbSLinus Torvalds 		if (!capable(CAP_DAC_READ_SEARCH))
3950f0cc6ffbSLinus Torvalds 			return -ENOENT;
395111a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
3952f0cc6ffbSLinus Torvalds 	}
3953c04030e1SUlrich Drepper 
395411a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
395511a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
3956442e31caSJeff Layton retry:
395711a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
39581da177e4SLinus Torvalds 	if (error)
39592ad94ae6SAl Viro 		return error;
39602ad94ae6SAl Viro 
3961442e31caSJeff Layton 	new_dentry = user_path_create(newdfd, newname, &new_path,
3962442e31caSJeff Layton 					(how & LOOKUP_REVAL));
39631da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
39646902d925SDave Hansen 	if (IS_ERR(new_dentry))
3965dae6ad8fSAl Viro 		goto out;
3966dae6ad8fSAl Viro 
3967dae6ad8fSAl Viro 	error = -EXDEV;
3968dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt)
3969dae6ad8fSAl Viro 		goto out_dput;
3970800179c9SKees Cook 	error = may_linkat(&old_path);
3971800179c9SKees Cook 	if (unlikely(error))
3972800179c9SKees Cook 		goto out_dput;
3973dae6ad8fSAl Viro 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
3974be6d3e56SKentaro Takeda 	if (error)
3975a8104a9fSAl Viro 		goto out_dput;
3976146a8595SJ. Bruce Fields 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
397775c3f29dSDave Hansen out_dput:
3978921a1650SAl Viro 	done_path_create(&new_path, new_dentry);
3979146a8595SJ. Bruce Fields 	if (delegated_inode) {
3980146a8595SJ. Bruce Fields 		error = break_deleg_wait(&delegated_inode);
3981d22e6338SOleg Drokin 		if (!error) {
3982d22e6338SOleg Drokin 			path_put(&old_path);
3983146a8595SJ. Bruce Fields 			goto retry;
3984146a8595SJ. Bruce Fields 		}
3985d22e6338SOleg Drokin 	}
3986442e31caSJeff Layton 	if (retry_estale(error, how)) {
3987d22e6338SOleg Drokin 		path_put(&old_path);
3988442e31caSJeff Layton 		how |= LOOKUP_REVAL;
3989442e31caSJeff Layton 		goto retry;
3990442e31caSJeff Layton 	}
39911da177e4SLinus Torvalds out:
39922d8f3038SAl Viro 	path_put(&old_path);
39931da177e4SLinus Torvalds 
39941da177e4SLinus Torvalds 	return error;
39951da177e4SLinus Torvalds }
39961da177e4SLinus Torvalds 
39973480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
39985590ff0dSUlrich Drepper {
3999c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
40005590ff0dSUlrich Drepper }
40015590ff0dSUlrich Drepper 
4002bc27027aSMiklos Szeredi /**
4003bc27027aSMiklos Szeredi  * vfs_rename - rename a filesystem object
4004bc27027aSMiklos Szeredi  * @old_dir:	parent of source
4005bc27027aSMiklos Szeredi  * @old_dentry:	source
4006bc27027aSMiklos Szeredi  * @new_dir:	parent of destination
4007bc27027aSMiklos Szeredi  * @new_dentry:	destination
4008bc27027aSMiklos Szeredi  * @delegated_inode: returns an inode needing a delegation break
4009520c8b16SMiklos Szeredi  * @flags:	rename flags
4010bc27027aSMiklos Szeredi  *
4011bc27027aSMiklos Szeredi  * The caller must hold multiple mutexes--see lock_rename()).
4012bc27027aSMiklos Szeredi  *
4013bc27027aSMiklos Szeredi  * If vfs_rename discovers a delegation in need of breaking at either
4014bc27027aSMiklos Szeredi  * the source or destination, it will return -EWOULDBLOCK and return a
4015bc27027aSMiklos Szeredi  * reference to the inode in delegated_inode.  The caller should then
4016bc27027aSMiklos Szeredi  * break the delegation and retry.  Because breaking a delegation may
4017bc27027aSMiklos Szeredi  * take a long time, the caller should drop all locks before doing
4018bc27027aSMiklos Szeredi  * so.
4019bc27027aSMiklos Szeredi  *
4020bc27027aSMiklos Szeredi  * Alternatively, a caller may pass NULL for delegated_inode.  This may
4021bc27027aSMiklos Szeredi  * be appropriate for callers that expect the underlying filesystem not
4022bc27027aSMiklos Szeredi  * to be NFS exported.
4023bc27027aSMiklos Szeredi  *
40241da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
40251da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
40261da177e4SLinus Torvalds  * Problems:
4027d03b29a2SJ. Bruce Fields  *	a) we can get into loop creation.
40281da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
40291da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
4030a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
40311da177e4SLinus Torvalds  *	   story.
40326cedba89SJ. Bruce Fields  *	c) we have to lock _four_ objects - parents and victim (if it exists),
40336cedba89SJ. Bruce Fields  *	   and source (if it is not a directory).
40341b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
40351da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
40361da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
4037a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
40381da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
40391da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
40401da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
4041a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
40421da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
40431da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
40441da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
4045e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
40461b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
40471da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
4048c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
40491da177e4SLinus Torvalds  *	   locking].
40501da177e4SLinus Torvalds  */
40511da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
40528e6d782cSJ. Bruce Fields 	       struct inode *new_dir, struct dentry *new_dentry,
4053520c8b16SMiklos Szeredi 	       struct inode **delegated_inode, unsigned int flags)
40541da177e4SLinus Torvalds {
40551da177e4SLinus Torvalds 	int error;
4056bc27027aSMiklos Szeredi 	bool is_dir = d_is_dir(old_dentry);
405759b0df21SEric Paris 	const unsigned char *old_name;
4058bc27027aSMiklos Szeredi 	struct inode *source = old_dentry->d_inode;
4059bc27027aSMiklos Szeredi 	struct inode *target = new_dentry->d_inode;
4060da1ce067SMiklos Szeredi 	bool new_is_dir = false;
4061da1ce067SMiklos Szeredi 	unsigned max_links = new_dir->i_sb->s_max_links;
40621da177e4SLinus Torvalds 
4063bc27027aSMiklos Szeredi 	if (source == target)
40641da177e4SLinus Torvalds 		return 0;
40651da177e4SLinus Torvalds 
40661da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
40671da177e4SLinus Torvalds 	if (error)
40681da177e4SLinus Torvalds 		return error;
40691da177e4SLinus Torvalds 
4070da1ce067SMiklos Szeredi 	if (!target) {
4071a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
4072da1ce067SMiklos Szeredi 	} else {
4073da1ce067SMiklos Szeredi 		new_is_dir = d_is_dir(new_dentry);
4074da1ce067SMiklos Szeredi 
4075da1ce067SMiklos Szeredi 		if (!(flags & RENAME_EXCHANGE))
40761da177e4SLinus Torvalds 			error = may_delete(new_dir, new_dentry, is_dir);
4077da1ce067SMiklos Szeredi 		else
4078da1ce067SMiklos Szeredi 			error = may_delete(new_dir, new_dentry, new_is_dir);
4079da1ce067SMiklos Szeredi 	}
40801da177e4SLinus Torvalds 	if (error)
40811da177e4SLinus Torvalds 		return error;
40821da177e4SLinus Torvalds 
40837177a9c4SMiklos Szeredi 	if (!old_dir->i_op->rename && !old_dir->i_op->rename2)
40841da177e4SLinus Torvalds 		return -EPERM;
40851da177e4SLinus Torvalds 
4086520c8b16SMiklos Szeredi 	if (flags && !old_dir->i_op->rename2)
4087520c8b16SMiklos Szeredi 		return -EINVAL;
4088520c8b16SMiklos Szeredi 
4089bc27027aSMiklos Szeredi 	/*
4090bc27027aSMiklos Szeredi 	 * If we are going to change the parent - check write permissions,
4091bc27027aSMiklos Szeredi 	 * we'll need to flip '..'.
4092bc27027aSMiklos Szeredi 	 */
4093da1ce067SMiklos Szeredi 	if (new_dir != old_dir) {
4094da1ce067SMiklos Szeredi 		if (is_dir) {
4095bc27027aSMiklos Szeredi 			error = inode_permission(source, MAY_WRITE);
4096bc27027aSMiklos Szeredi 			if (error)
4097bc27027aSMiklos Szeredi 				return error;
4098bc27027aSMiklos Szeredi 		}
4099da1ce067SMiklos Szeredi 		if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4100da1ce067SMiklos Szeredi 			error = inode_permission(target, MAY_WRITE);
4101da1ce067SMiklos Szeredi 			if (error)
4102da1ce067SMiklos Szeredi 				return error;
4103da1ce067SMiklos Szeredi 		}
4104da1ce067SMiklos Szeredi 	}
41050eeca283SRobert Love 
41060b3974ebSMiklos Szeredi 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
41070b3974ebSMiklos Szeredi 				      flags);
4108bc27027aSMiklos Szeredi 	if (error)
4109bc27027aSMiklos Szeredi 		return error;
4110bc27027aSMiklos Szeredi 
4111bc27027aSMiklos Szeredi 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
4112bc27027aSMiklos Szeredi 	dget(new_dentry);
4113da1ce067SMiklos Szeredi 	if (!is_dir || (flags & RENAME_EXCHANGE))
4114bc27027aSMiklos Szeredi 		lock_two_nondirectories(source, target);
4115bc27027aSMiklos Szeredi 	else if (target)
4116bc27027aSMiklos Szeredi 		mutex_lock(&target->i_mutex);
4117bc27027aSMiklos Szeredi 
4118bc27027aSMiklos Szeredi 	error = -EBUSY;
4119bc27027aSMiklos Szeredi 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
4120bc27027aSMiklos Szeredi 		goto out;
4121bc27027aSMiklos Szeredi 
4122da1ce067SMiklos Szeredi 	if (max_links && new_dir != old_dir) {
4123bc27027aSMiklos Szeredi 		error = -EMLINK;
4124da1ce067SMiklos Szeredi 		if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
4125bc27027aSMiklos Szeredi 			goto out;
4126da1ce067SMiklos Szeredi 		if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4127da1ce067SMiklos Szeredi 		    old_dir->i_nlink >= max_links)
4128da1ce067SMiklos Szeredi 			goto out;
4129da1ce067SMiklos Szeredi 	}
4130da1ce067SMiklos Szeredi 	if (is_dir && !(flags & RENAME_EXCHANGE) && target)
4131bc27027aSMiklos Szeredi 		shrink_dcache_parent(new_dentry);
4132da1ce067SMiklos Szeredi 	if (!is_dir) {
4133bc27027aSMiklos Szeredi 		error = try_break_deleg(source, delegated_inode);
4134bc27027aSMiklos Szeredi 		if (error)
4135bc27027aSMiklos Szeredi 			goto out;
4136da1ce067SMiklos Szeredi 	}
4137da1ce067SMiklos Szeredi 	if (target && !new_is_dir) {
4138bc27027aSMiklos Szeredi 		error = try_break_deleg(target, delegated_inode);
4139bc27027aSMiklos Szeredi 		if (error)
4140bc27027aSMiklos Szeredi 			goto out;
4141bc27027aSMiklos Szeredi 	}
41427177a9c4SMiklos Szeredi 	if (!old_dir->i_op->rename2) {
4143520c8b16SMiklos Szeredi 		error = old_dir->i_op->rename(old_dir, old_dentry,
4144520c8b16SMiklos Szeredi 					      new_dir, new_dentry);
4145520c8b16SMiklos Szeredi 	} else {
41467177a9c4SMiklos Szeredi 		WARN_ON(old_dir->i_op->rename != NULL);
4147520c8b16SMiklos Szeredi 		error = old_dir->i_op->rename2(old_dir, old_dentry,
4148520c8b16SMiklos Szeredi 					       new_dir, new_dentry, flags);
4149520c8b16SMiklos Szeredi 	}
4150bc27027aSMiklos Szeredi 	if (error)
4151bc27027aSMiklos Szeredi 		goto out;
4152bc27027aSMiklos Szeredi 
4153da1ce067SMiklos Szeredi 	if (!(flags & RENAME_EXCHANGE) && target) {
41541da177e4SLinus Torvalds 		if (is_dir)
4155bc27027aSMiklos Szeredi 			target->i_flags |= S_DEAD;
4156bc27027aSMiklos Szeredi 		dont_mount(new_dentry);
4157bc27027aSMiklos Szeredi 	}
4158da1ce067SMiklos Szeredi 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4159da1ce067SMiklos Szeredi 		if (!(flags & RENAME_EXCHANGE))
4160bc27027aSMiklos Szeredi 			d_move(old_dentry, new_dentry);
4161da1ce067SMiklos Szeredi 		else
4162da1ce067SMiklos Szeredi 			d_exchange(old_dentry, new_dentry);
4163da1ce067SMiklos Szeredi 	}
4164bc27027aSMiklos Szeredi out:
4165da1ce067SMiklos Szeredi 	if (!is_dir || (flags & RENAME_EXCHANGE))
4166bc27027aSMiklos Szeredi 		unlock_two_nondirectories(source, target);
4167bc27027aSMiklos Szeredi 	else if (target)
4168bc27027aSMiklos Szeredi 		mutex_unlock(&target->i_mutex);
4169bc27027aSMiklos Szeredi 	dput(new_dentry);
4170da1ce067SMiklos Szeredi 	if (!error) {
4171123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
4172da1ce067SMiklos Szeredi 			      !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4173da1ce067SMiklos Szeredi 		if (flags & RENAME_EXCHANGE) {
4174da1ce067SMiklos Szeredi 			fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
4175da1ce067SMiklos Szeredi 				      new_is_dir, NULL, new_dentry);
4176da1ce067SMiklos Szeredi 		}
4177da1ce067SMiklos Szeredi 	}
41780eeca283SRobert Love 	fsnotify_oldname_free(old_name);
41790eeca283SRobert Love 
41801da177e4SLinus Torvalds 	return error;
41811da177e4SLinus Torvalds }
41824d359507SAl Viro EXPORT_SYMBOL(vfs_rename);
41831da177e4SLinus Torvalds 
4184520c8b16SMiklos Szeredi SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4185520c8b16SMiklos Szeredi 		int, newdfd, const char __user *, newname, unsigned int, flags)
41861da177e4SLinus Torvalds {
41871da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
41881da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
41891da177e4SLinus Torvalds 	struct dentry *trap;
41901da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
41918e6d782cSJ. Bruce Fields 	struct inode *delegated_inode = NULL;
419291a27b2aSJeff Layton 	struct filename *from;
419391a27b2aSJeff Layton 	struct filename *to;
4194c6a94284SJeff Layton 	unsigned int lookup_flags = 0;
4195c6a94284SJeff Layton 	bool should_retry = false;
41962ad94ae6SAl Viro 	int error;
4197520c8b16SMiklos Szeredi 
4198da1ce067SMiklos Szeredi 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
4199da1ce067SMiklos Szeredi 		return -EINVAL;
4200da1ce067SMiklos Szeredi 
4201da1ce067SMiklos Szeredi 	if ((flags & RENAME_NOREPLACE) && (flags & RENAME_EXCHANGE))
4202520c8b16SMiklos Szeredi 		return -EINVAL;
4203520c8b16SMiklos Szeredi 
4204c6a94284SJeff Layton retry:
4205c6a94284SJeff Layton 	from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
420691a27b2aSJeff Layton 	if (IS_ERR(from)) {
420791a27b2aSJeff Layton 		error = PTR_ERR(from);
42081da177e4SLinus Torvalds 		goto exit;
420991a27b2aSJeff Layton 	}
42101da177e4SLinus Torvalds 
4211c6a94284SJeff Layton 	to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
421291a27b2aSJeff Layton 	if (IS_ERR(to)) {
421391a27b2aSJeff Layton 		error = PTR_ERR(to);
42141da177e4SLinus Torvalds 		goto exit1;
421591a27b2aSJeff Layton 	}
42161da177e4SLinus Torvalds 
42171da177e4SLinus Torvalds 	error = -EXDEV;
42184ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
42191da177e4SLinus Torvalds 		goto exit2;
42201da177e4SLinus Torvalds 
42214ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
42221da177e4SLinus Torvalds 	error = -EBUSY;
42231da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
42241da177e4SLinus Torvalds 		goto exit2;
42251da177e4SLinus Torvalds 
42264ac91378SJan Blunck 	new_dir = newnd.path.dentry;
42270a7c3937SMiklos Szeredi 	if (flags & RENAME_NOREPLACE)
42280a7c3937SMiklos Szeredi 		error = -EEXIST;
42291da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
42301da177e4SLinus Torvalds 		goto exit2;
42311da177e4SLinus Torvalds 
4232c30dabfeSJan Kara 	error = mnt_want_write(oldnd.path.mnt);
4233c30dabfeSJan Kara 	if (error)
4234c30dabfeSJan Kara 		goto exit2;
4235c30dabfeSJan Kara 
42360612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
42370612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
4238da1ce067SMiklos Szeredi 	if (!(flags & RENAME_EXCHANGE))
42394e9ed2f8SOGAWA Hirofumi 		newnd.flags |= LOOKUP_RENAME_TARGET;
42400612d9fbSOGAWA Hirofumi 
42418e6d782cSJ. Bruce Fields retry_deleg:
42421da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
42431da177e4SLinus Torvalds 
424449705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
42451da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
42461da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
42471da177e4SLinus Torvalds 		goto exit3;
42481da177e4SLinus Torvalds 	/* source must exist */
42491da177e4SLinus Torvalds 	error = -ENOENT;
4250b18825a7SDavid Howells 	if (d_is_negative(old_dentry))
42511da177e4SLinus Torvalds 		goto exit4;
425249705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
42531da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
42541da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
42551da177e4SLinus Torvalds 		goto exit4;
42560a7c3937SMiklos Szeredi 	error = -EEXIST;
42570a7c3937SMiklos Szeredi 	if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
42580a7c3937SMiklos Szeredi 		goto exit5;
4259da1ce067SMiklos Szeredi 	if (flags & RENAME_EXCHANGE) {
4260da1ce067SMiklos Szeredi 		error = -ENOENT;
4261da1ce067SMiklos Szeredi 		if (d_is_negative(new_dentry))
4262da1ce067SMiklos Szeredi 			goto exit5;
4263da1ce067SMiklos Szeredi 
4264da1ce067SMiklos Szeredi 		if (!d_is_dir(new_dentry)) {
4265da1ce067SMiklos Szeredi 			error = -ENOTDIR;
4266da1ce067SMiklos Szeredi 			if (newnd.last.name[newnd.last.len])
4267da1ce067SMiklos Szeredi 				goto exit5;
4268da1ce067SMiklos Szeredi 		}
4269da1ce067SMiklos Szeredi 	}
42700a7c3937SMiklos Szeredi 	/* unless the source is a directory trailing slashes give -ENOTDIR */
42710a7c3937SMiklos Szeredi 	if (!d_is_dir(old_dentry)) {
42720a7c3937SMiklos Szeredi 		error = -ENOTDIR;
42730a7c3937SMiklos Szeredi 		if (oldnd.last.name[oldnd.last.len])
42740a7c3937SMiklos Szeredi 			goto exit5;
4275da1ce067SMiklos Szeredi 		if (!(flags & RENAME_EXCHANGE) && newnd.last.name[newnd.last.len])
42760a7c3937SMiklos Szeredi 			goto exit5;
42770a7c3937SMiklos Szeredi 	}
42780a7c3937SMiklos Szeredi 	/* source should not be ancestor of target */
42790a7c3937SMiklos Szeredi 	error = -EINVAL;
42800a7c3937SMiklos Szeredi 	if (old_dentry == trap)
42810a7c3937SMiklos Szeredi 		goto exit5;
42821da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
4283da1ce067SMiklos Szeredi 	if (!(flags & RENAME_EXCHANGE))
42841da177e4SLinus Torvalds 		error = -ENOTEMPTY;
42851da177e4SLinus Torvalds 	if (new_dentry == trap)
42861da177e4SLinus Torvalds 		goto exit5;
42871da177e4SLinus Torvalds 
4288be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
42890b3974ebSMiklos Szeredi 				     &newnd.path, new_dentry, flags);
4290be6d3e56SKentaro Takeda 	if (error)
4291c30dabfeSJan Kara 		goto exit5;
42921da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
42938e6d782cSJ. Bruce Fields 			   new_dir->d_inode, new_dentry,
4294520c8b16SMiklos Szeredi 			   &delegated_inode, flags);
42951da177e4SLinus Torvalds exit5:
42961da177e4SLinus Torvalds 	dput(new_dentry);
42971da177e4SLinus Torvalds exit4:
42981da177e4SLinus Torvalds 	dput(old_dentry);
42991da177e4SLinus Torvalds exit3:
43001da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
43018e6d782cSJ. Bruce Fields 	if (delegated_inode) {
43028e6d782cSJ. Bruce Fields 		error = break_deleg_wait(&delegated_inode);
43038e6d782cSJ. Bruce Fields 		if (!error)
43048e6d782cSJ. Bruce Fields 			goto retry_deleg;
43058e6d782cSJ. Bruce Fields 	}
4306c30dabfeSJan Kara 	mnt_drop_write(oldnd.path.mnt);
43071da177e4SLinus Torvalds exit2:
4308c6a94284SJeff Layton 	if (retry_estale(error, lookup_flags))
4309c6a94284SJeff Layton 		should_retry = true;
43101d957f9bSJan Blunck 	path_put(&newnd.path);
43112ad94ae6SAl Viro 	putname(to);
43121da177e4SLinus Torvalds exit1:
43131d957f9bSJan Blunck 	path_put(&oldnd.path);
43141da177e4SLinus Torvalds 	putname(from);
4315c6a94284SJeff Layton 	if (should_retry) {
4316c6a94284SJeff Layton 		should_retry = false;
4317c6a94284SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
4318c6a94284SJeff Layton 		goto retry;
4319c6a94284SJeff Layton 	}
43202ad94ae6SAl Viro exit:
43211da177e4SLinus Torvalds 	return error;
43221da177e4SLinus Torvalds }
43231da177e4SLinus Torvalds 
4324520c8b16SMiklos Szeredi SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4325520c8b16SMiklos Szeredi 		int, newdfd, const char __user *, newname)
4326520c8b16SMiklos Szeredi {
4327520c8b16SMiklos Szeredi 	return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
4328520c8b16SMiklos Szeredi }
4329520c8b16SMiklos Szeredi 
4330a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
43315590ff0dSUlrich Drepper {
4332520c8b16SMiklos Szeredi 	return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
43335590ff0dSUlrich Drepper }
43345590ff0dSUlrich Drepper 
43355d826c84SAl Viro int readlink_copy(char __user *buffer, int buflen, const char *link)
43361da177e4SLinus Torvalds {
43375d826c84SAl Viro 	int len = PTR_ERR(link);
43381da177e4SLinus Torvalds 	if (IS_ERR(link))
43391da177e4SLinus Torvalds 		goto out;
43401da177e4SLinus Torvalds 
43411da177e4SLinus Torvalds 	len = strlen(link);
43421da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
43431da177e4SLinus Torvalds 		len = buflen;
43441da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
43451da177e4SLinus Torvalds 		len = -EFAULT;
43461da177e4SLinus Torvalds out:
43471da177e4SLinus Torvalds 	return len;
43481da177e4SLinus Torvalds }
43495d826c84SAl Viro EXPORT_SYMBOL(readlink_copy);
43501da177e4SLinus Torvalds 
43511da177e4SLinus Torvalds /*
43521da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
43531da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
43541da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
43551da177e4SLinus Torvalds  */
43561da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
43571da177e4SLinus Torvalds {
43581da177e4SLinus Torvalds 	struct nameidata nd;
4359cc314eefSLinus Torvalds 	void *cookie;
4360694a1764SMarcin Slusarz 	int res;
4361cc314eefSLinus Torvalds 
43621da177e4SLinus Torvalds 	nd.depth = 0;
4363cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
4364694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
4365694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
4366694a1764SMarcin Slusarz 
43675d826c84SAl Viro 	res = readlink_copy(buffer, buflen, nd_get_link(&nd));
43681da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
4369cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
4370694a1764SMarcin Slusarz 	return res;
43711da177e4SLinus Torvalds }
43724d359507SAl Viro EXPORT_SYMBOL(generic_readlink);
43731da177e4SLinus Torvalds 
43741da177e4SLinus Torvalds /* get the link contents into pagecache */
43751da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
43761da177e4SLinus Torvalds {
4377ebd09abbSDuane Griffin 	char *kaddr;
43781da177e4SLinus Torvalds 	struct page *page;
43791da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
4380090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
43811da177e4SLinus Torvalds 	if (IS_ERR(page))
43826fe6900eSNick Piggin 		return (char*)page;
43831da177e4SLinus Torvalds 	*ppage = page;
4384ebd09abbSDuane Griffin 	kaddr = kmap(page);
4385ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4386ebd09abbSDuane Griffin 	return kaddr;
43871da177e4SLinus Torvalds }
43881da177e4SLinus Torvalds 
43891da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
43901da177e4SLinus Torvalds {
43911da177e4SLinus Torvalds 	struct page *page = NULL;
43925d826c84SAl Viro 	int res = readlink_copy(buffer, buflen, page_getlink(dentry, &page));
43931da177e4SLinus Torvalds 	if (page) {
43941da177e4SLinus Torvalds 		kunmap(page);
43951da177e4SLinus Torvalds 		page_cache_release(page);
43961da177e4SLinus Torvalds 	}
43971da177e4SLinus Torvalds 	return res;
43981da177e4SLinus Torvalds }
43994d359507SAl Viro EXPORT_SYMBOL(page_readlink);
44001da177e4SLinus Torvalds 
4401cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
44021da177e4SLinus Torvalds {
4403cc314eefSLinus Torvalds 	struct page *page = NULL;
44041da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
4405cc314eefSLinus Torvalds 	return page;
44061da177e4SLinus Torvalds }
44074d359507SAl Viro EXPORT_SYMBOL(page_follow_link_light);
44081da177e4SLinus Torvalds 
4409cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
44101da177e4SLinus Torvalds {
4411cc314eefSLinus Torvalds 	struct page *page = cookie;
4412cc314eefSLinus Torvalds 
4413cc314eefSLinus Torvalds 	if (page) {
44141da177e4SLinus Torvalds 		kunmap(page);
44151da177e4SLinus Torvalds 		page_cache_release(page);
44161da177e4SLinus Torvalds 	}
44171da177e4SLinus Torvalds }
44184d359507SAl Viro EXPORT_SYMBOL(page_put_link);
44191da177e4SLinus Torvalds 
442054566b2cSNick Piggin /*
442154566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
442254566b2cSNick Piggin  */
442354566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
44241da177e4SLinus Torvalds {
44251da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
44260adb25d2SKirill Korotaev 	struct page *page;
4427afddba49SNick Piggin 	void *fsdata;
4428beb497abSDmitriy Monakhov 	int err;
44291da177e4SLinus Torvalds 	char *kaddr;
443054566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
443154566b2cSNick Piggin 	if (nofs)
443254566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
44331da177e4SLinus Torvalds 
44347e53cac4SNeilBrown retry:
4435afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
443654566b2cSNick Piggin 				flags, &page, &fsdata);
44371da177e4SLinus Torvalds 	if (err)
4438afddba49SNick Piggin 		goto fail;
4439afddba49SNick Piggin 
4440e8e3c3d6SCong Wang 	kaddr = kmap_atomic(page);
44411da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
4442e8e3c3d6SCong Wang 	kunmap_atomic(kaddr);
4443afddba49SNick Piggin 
4444afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4445afddba49SNick Piggin 							page, fsdata);
44461da177e4SLinus Torvalds 	if (err < 0)
44471da177e4SLinus Torvalds 		goto fail;
4448afddba49SNick Piggin 	if (err < len-1)
4449afddba49SNick Piggin 		goto retry;
4450afddba49SNick Piggin 
44511da177e4SLinus Torvalds 	mark_inode_dirty(inode);
44521da177e4SLinus Torvalds 	return 0;
44531da177e4SLinus Torvalds fail:
44541da177e4SLinus Torvalds 	return err;
44551da177e4SLinus Torvalds }
44564d359507SAl Viro EXPORT_SYMBOL(__page_symlink);
44571da177e4SLinus Torvalds 
44580adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
44590adb25d2SKirill Korotaev {
44600adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
446154566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
44620adb25d2SKirill Korotaev }
44634d359507SAl Viro EXPORT_SYMBOL(page_symlink);
44640adb25d2SKirill Korotaev 
446592e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
44661da177e4SLinus Torvalds 	.readlink	= generic_readlink,
44671da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
44681da177e4SLinus Torvalds 	.put_link	= page_put_link,
44691da177e4SLinus Torvalds };
44701da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
4471