xref: /openbmc/linux/fs/namei.c (revision 23adbe12)
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 
109462a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry)
109562a7375eSIan Kent {
109662a7375eSIan Kent 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
109762a7375eSIan Kent 		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 		 */
1113287548e4SAl Viro 		if (unlikely(managed_dentry_might_block(path->dentry)))
1114ab90911fSDavid Howells 			return false;
111562a7375eSIan Kent 
111662a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
1117b37199e6SAl Viro 			return true;
111862a7375eSIan Kent 
1119474279dcSAl Viro 		mounted = __lookup_mnt(path->mnt, path->dentry);
11209875cf80SDavid Howells 		if (!mounted)
11219875cf80SDavid Howells 			break;
1122c7105365SAl Viro 		path->mnt = &mounted->mnt;
1123c7105365SAl Viro 		path->dentry = mounted->mnt.mnt_root;
1124a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
11259875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
112659430262SLinus Torvalds 		/*
112759430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
112859430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
112959430262SLinus Torvalds 		 * because a mount-point is always pinned.
113059430262SLinus Torvalds 		 */
113159430262SLinus Torvalds 		*inode = path->dentry->d_inode;
11329875cf80SDavid Howells 	}
1133b37199e6SAl Viro 	return read_seqretry(&mount_lock, nd->m_seq);
1134287548e4SAl Viro }
1135287548e4SAl Viro 
113631e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
113731e6b01fSNick Piggin {
113831e6b01fSNick Piggin 	set_root_rcu(nd);
113931e6b01fSNick Piggin 
114031e6b01fSNick Piggin 	while (1) {
114131e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
114231e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
114331e6b01fSNick Piggin 			break;
114431e6b01fSNick Piggin 		}
114531e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
114631e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
114731e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
114831e6b01fSNick Piggin 			unsigned seq;
114931e6b01fSNick Piggin 
115031e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
115131e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
1152ef7562d5SAl Viro 				goto failed;
115331e6b01fSNick Piggin 			nd->path.dentry = parent;
115431e6b01fSNick Piggin 			nd->seq = seq;
115531e6b01fSNick Piggin 			break;
115631e6b01fSNick Piggin 		}
115731e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
115831e6b01fSNick Piggin 			break;
115931e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
116031e6b01fSNick Piggin 	}
1161b37199e6SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
1162b37199e6SAl Viro 		struct mount *mounted;
1163b37199e6SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
1164b37199e6SAl Viro 		if (!mounted)
1165b37199e6SAl Viro 			break;
1166b37199e6SAl Viro 		nd->path.mnt = &mounted->mnt;
1167b37199e6SAl Viro 		nd->path.dentry = mounted->mnt.mnt_root;
1168b37199e6SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1169b37199e6SAl Viro 		if (!read_seqretry(&mount_lock, nd->m_seq))
1170b37199e6SAl Viro 			goto failed;
1171b37199e6SAl Viro 	}
1172dea39376SAl Viro 	nd->inode = nd->path.dentry->d_inode;
117331e6b01fSNick Piggin 	return 0;
1174ef7562d5SAl Viro 
1175ef7562d5SAl Viro failed:
1176ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
11775b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
1178ef7562d5SAl Viro 		nd->root.mnt = NULL;
11798b61e74fSAl Viro 	rcu_read_unlock();
1180ef7562d5SAl Viro 	return -ECHILD;
118131e6b01fSNick Piggin }
118231e6b01fSNick Piggin 
11839875cf80SDavid Howells /*
1184cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
1185cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
1186cc53ce53SDavid Howells  * caller is permitted to proceed or not.
1187cc53ce53SDavid Howells  */
11887cc90cc3SAl Viro int follow_down(struct path *path)
1189cc53ce53SDavid Howells {
1190cc53ce53SDavid Howells 	unsigned managed;
1191cc53ce53SDavid Howells 	int ret;
1192cc53ce53SDavid Howells 
1193cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
1194cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1195cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1196cc53ce53SDavid Howells 		 * being held.
1197cc53ce53SDavid Howells 		 *
1198cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
1199cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
1200cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
1201cc53ce53SDavid Howells 		 * superstructure.
1202cc53ce53SDavid Howells 		 *
1203cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
1204cc53ce53SDavid Howells 		 */
1205cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
1206cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1207cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
1208ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
12091aed3e42SAl Viro 				path->dentry, false);
1210cc53ce53SDavid Howells 			if (ret < 0)
1211cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
1212cc53ce53SDavid Howells 		}
1213cc53ce53SDavid Howells 
1214cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
1215cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
1216cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
1217cc53ce53SDavid Howells 			if (!mounted)
1218cc53ce53SDavid Howells 				break;
1219cc53ce53SDavid Howells 			dput(path->dentry);
1220cc53ce53SDavid Howells 			mntput(path->mnt);
1221cc53ce53SDavid Howells 			path->mnt = mounted;
1222cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1223cc53ce53SDavid Howells 			continue;
1224cc53ce53SDavid Howells 		}
1225cc53ce53SDavid Howells 
1226cc53ce53SDavid Howells 		/* Don't handle automount points here */
1227cc53ce53SDavid Howells 		break;
1228cc53ce53SDavid Howells 	}
1229cc53ce53SDavid Howells 	return 0;
1230cc53ce53SDavid Howells }
12314d359507SAl Viro EXPORT_SYMBOL(follow_down);
1232cc53ce53SDavid Howells 
1233cc53ce53SDavid Howells /*
12349875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
12359875cf80SDavid Howells  */
12369875cf80SDavid Howells static void follow_mount(struct path *path)
12379875cf80SDavid Howells {
12389875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
12399875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
12409875cf80SDavid Howells 		if (!mounted)
12419875cf80SDavid Howells 			break;
12429875cf80SDavid Howells 		dput(path->dentry);
12439875cf80SDavid Howells 		mntput(path->mnt);
12449875cf80SDavid Howells 		path->mnt = mounted;
12459875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
12469875cf80SDavid Howells 	}
12479875cf80SDavid Howells }
12489875cf80SDavid Howells 
124931e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
12501da177e4SLinus Torvalds {
12512a737871SAl Viro 	set_root(nd);
1252e518ddb7SAndreas Mohr 
12531da177e4SLinus Torvalds 	while(1) {
12544ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
12551da177e4SLinus Torvalds 
12562a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
12572a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
12581da177e4SLinus Torvalds 			break;
12591da177e4SLinus Torvalds 		}
12604ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
12613088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
12623088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
12631da177e4SLinus Torvalds 			dput(old);
12641da177e4SLinus Torvalds 			break;
12651da177e4SLinus Torvalds 		}
12663088dd70SAl Viro 		if (!follow_up(&nd->path))
12671da177e4SLinus Torvalds 			break;
12681da177e4SLinus Torvalds 	}
126979ed0226SAl Viro 	follow_mount(&nd->path);
127031e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
12711da177e4SLinus Torvalds }
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds /*
1274bad61189SMiklos Szeredi  * This looks up the name in dcache, possibly revalidates the old dentry and
1275bad61189SMiklos Szeredi  * allocates a new one if not found or not valid.  In the need_lookup argument
1276bad61189SMiklos Szeredi  * returns whether i_op->lookup is necessary.
1277bad61189SMiklos Szeredi  *
1278bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
1279baa03890SNick Piggin  */
1280bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1281201f956eSAl Viro 				    unsigned int flags, bool *need_lookup)
1282baa03890SNick Piggin {
1283baa03890SNick Piggin 	struct dentry *dentry;
1284bad61189SMiklos Szeredi 	int error;
1285baa03890SNick Piggin 
1286bad61189SMiklos Szeredi 	*need_lookup = false;
1287bad61189SMiklos Szeredi 	dentry = d_lookup(dir, name);
1288bad61189SMiklos Szeredi 	if (dentry) {
128939e3c955SJeff Layton 		if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1290201f956eSAl Viro 			error = d_revalidate(dentry, flags);
1291bad61189SMiklos Szeredi 			if (unlikely(error <= 0)) {
1292bad61189SMiklos Szeredi 				if (error < 0) {
1293bad61189SMiklos Szeredi 					dput(dentry);
1294bad61189SMiklos Szeredi 					return ERR_PTR(error);
1295bad61189SMiklos Szeredi 				} else if (!d_invalidate(dentry)) {
1296bad61189SMiklos Szeredi 					dput(dentry);
1297bad61189SMiklos Szeredi 					dentry = NULL;
1298bad61189SMiklos Szeredi 				}
1299bad61189SMiklos Szeredi 			}
1300bad61189SMiklos Szeredi 		}
1301bad61189SMiklos Szeredi 	}
1302baa03890SNick Piggin 
1303bad61189SMiklos Szeredi 	if (!dentry) {
1304bad61189SMiklos Szeredi 		dentry = d_alloc(dir, name);
1305baa03890SNick Piggin 		if (unlikely(!dentry))
1306baa03890SNick Piggin 			return ERR_PTR(-ENOMEM);
1307baa03890SNick Piggin 
1308bad61189SMiklos Szeredi 		*need_lookup = true;
1309baa03890SNick Piggin 	}
1310baa03890SNick Piggin 	return dentry;
1311baa03890SNick Piggin }
1312baa03890SNick Piggin 
1313baa03890SNick Piggin /*
131413a2c3beSJ. Bruce Fields  * Call i_op->lookup on the dentry.  The dentry must be negative and
131513a2c3beSJ. Bruce Fields  * unhashed.
1316bad61189SMiklos Szeredi  *
1317bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
131844396f4bSJosef Bacik  */
1319bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
132072bd866aSAl Viro 				  unsigned int flags)
132144396f4bSJosef Bacik {
132244396f4bSJosef Bacik 	struct dentry *old;
132344396f4bSJosef Bacik 
132444396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
1325bad61189SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
1326e188dc02SMiklos Szeredi 		dput(dentry);
132744396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
1328e188dc02SMiklos Szeredi 	}
132944396f4bSJosef Bacik 
133072bd866aSAl Viro 	old = dir->i_op->lookup(dir, dentry, flags);
133144396f4bSJosef Bacik 	if (unlikely(old)) {
133244396f4bSJosef Bacik 		dput(dentry);
133344396f4bSJosef Bacik 		dentry = old;
133444396f4bSJosef Bacik 	}
133544396f4bSJosef Bacik 	return dentry;
133644396f4bSJosef Bacik }
133744396f4bSJosef Bacik 
1338a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name,
133972bd866aSAl Viro 		struct dentry *base, unsigned int flags)
1340a3255546SAl Viro {
1341bad61189SMiklos Szeredi 	bool need_lookup;
1342a3255546SAl Viro 	struct dentry *dentry;
1343a3255546SAl Viro 
134472bd866aSAl Viro 	dentry = lookup_dcache(name, base, flags, &need_lookup);
1345bad61189SMiklos Szeredi 	if (!need_lookup)
1346a3255546SAl Viro 		return dentry;
1347bad61189SMiklos Szeredi 
134872bd866aSAl Viro 	return lookup_real(base->d_inode, dentry, flags);
1349a3255546SAl Viro }
1350a3255546SAl Viro 
135144396f4bSJosef Bacik /*
13521da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
13531da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
13541da177e4SLinus Torvalds  *  It _is_ time-critical.
13551da177e4SLinus Torvalds  */
1356e97cdc87SAl Viro static int lookup_fast(struct nameidata *nd,
135731e6b01fSNick Piggin 		       struct path *path, struct inode **inode)
13581da177e4SLinus Torvalds {
13594ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
136031e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
13615a18fff2SAl Viro 	int need_reval = 1;
13625a18fff2SAl Viro 	int status = 1;
13639875cf80SDavid Howells 	int err;
13649875cf80SDavid Howells 
13653cac260aSAl Viro 	/*
1366b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1367b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1368b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1369b04f784eSNick Piggin 	 */
137031e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
137131e6b01fSNick Piggin 		unsigned seq;
1372da53be12SLinus Torvalds 		dentry = __d_lookup_rcu(parent, &nd->last, &seq);
13735a18fff2SAl Viro 		if (!dentry)
13745a18fff2SAl Viro 			goto unlazy;
13755a18fff2SAl Viro 
137612f8ad4bSLinus Torvalds 		/*
137712f8ad4bSLinus Torvalds 		 * This sequence count validates that the inode matches
137812f8ad4bSLinus Torvalds 		 * the dentry name information from lookup.
137912f8ad4bSLinus Torvalds 		 */
138012f8ad4bSLinus Torvalds 		*inode = dentry->d_inode;
138112f8ad4bSLinus Torvalds 		if (read_seqcount_retry(&dentry->d_seq, seq))
138212f8ad4bSLinus Torvalds 			return -ECHILD;
138312f8ad4bSLinus Torvalds 
138412f8ad4bSLinus Torvalds 		/*
138512f8ad4bSLinus Torvalds 		 * This sequence count validates that the parent had no
138612f8ad4bSLinus Torvalds 		 * changes while we did the lookup of the dentry above.
138712f8ad4bSLinus Torvalds 		 *
138812f8ad4bSLinus Torvalds 		 * The memory barrier in read_seqcount_begin of child is
138912f8ad4bSLinus Torvalds 		 *  enough, we can use __read_seqcount_retry here.
139012f8ad4bSLinus Torvalds 		 */
139131e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
139231e6b01fSNick Piggin 			return -ECHILD;
139331e6b01fSNick Piggin 		nd->seq = seq;
13945a18fff2SAl Viro 
139524643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
13964ce16ef3SAl Viro 			status = d_revalidate(dentry, nd->flags);
13975a18fff2SAl Viro 			if (unlikely(status <= 0)) {
13985a18fff2SAl Viro 				if (status != -ECHILD)
13995a18fff2SAl Viro 					need_reval = 0;
14005a18fff2SAl Viro 				goto unlazy;
14015a18fff2SAl Viro 			}
140224643087SAl Viro 		}
140331e6b01fSNick Piggin 		path->mnt = mnt;
140431e6b01fSNick Piggin 		path->dentry = dentry;
1405d6e9bd25SAl Viro 		if (unlikely(!__follow_mount_rcu(nd, path, inode)))
1406d6e9bd25SAl Viro 			goto unlazy;
1407d6e9bd25SAl Viro 		if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
1408d6e9bd25SAl Viro 			goto unlazy;
14099875cf80SDavid Howells 		return 0;
14105a18fff2SAl Viro unlazy:
141119660af7SAl Viro 		if (unlazy_walk(nd, dentry))
14125a18fff2SAl Viro 			return -ECHILD;
14135a18fff2SAl Viro 	} else {
1414e97cdc87SAl Viro 		dentry = __d_lookup(parent, &nd->last);
141524643087SAl Viro 	}
14165a18fff2SAl Viro 
141781e6f520SAl Viro 	if (unlikely(!dentry))
141881e6f520SAl Viro 		goto need_lookup;
14195a18fff2SAl Viro 
14205a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
14214ce16ef3SAl Viro 		status = d_revalidate(dentry, nd->flags);
14225a18fff2SAl Viro 	if (unlikely(status <= 0)) {
14235a18fff2SAl Viro 		if (status < 0) {
14245a18fff2SAl Viro 			dput(dentry);
14255a18fff2SAl Viro 			return status;
14265a18fff2SAl Viro 		}
14275a18fff2SAl Viro 		if (!d_invalidate(dentry)) {
14285a18fff2SAl Viro 			dput(dentry);
142981e6f520SAl Viro 			goto need_lookup;
14305a18fff2SAl Viro 		}
14315a18fff2SAl Viro 	}
1432697f514dSMiklos Szeredi 
14331da177e4SLinus Torvalds 	path->mnt = mnt;
14341da177e4SLinus Torvalds 	path->dentry = dentry;
14359875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
143689312214SIan Kent 	if (unlikely(err < 0)) {
143789312214SIan Kent 		path_put_conditional(path, nd);
14389875cf80SDavid Howells 		return err;
143989312214SIan Kent 	}
1440a3fbbde7SAl Viro 	if (err)
1441a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
144231e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
14431da177e4SLinus Torvalds 	return 0;
144481e6f520SAl Viro 
144581e6f520SAl Viro need_lookup:
1446697f514dSMiklos Szeredi 	return 1;
1447697f514dSMiklos Szeredi }
1448697f514dSMiklos Szeredi 
1449697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */
1450cc2a5271SAl Viro static int lookup_slow(struct nameidata *nd, struct path *path)
1451697f514dSMiklos Szeredi {
1452697f514dSMiklos Szeredi 	struct dentry *dentry, *parent;
1453697f514dSMiklos Szeredi 	int err;
1454697f514dSMiklos Szeredi 
1455697f514dSMiklos Szeredi 	parent = nd->path.dentry;
145681e6f520SAl Viro 	BUG_ON(nd->inode != parent->d_inode);
145781e6f520SAl Viro 
145881e6f520SAl Viro 	mutex_lock(&parent->d_inode->i_mutex);
1459cc2a5271SAl Viro 	dentry = __lookup_hash(&nd->last, parent, nd->flags);
146081e6f520SAl Viro 	mutex_unlock(&parent->d_inode->i_mutex);
146181e6f520SAl Viro 	if (IS_ERR(dentry))
146281e6f520SAl Viro 		return PTR_ERR(dentry);
1463697f514dSMiklos Szeredi 	path->mnt = nd->path.mnt;
1464697f514dSMiklos Szeredi 	path->dentry = dentry;
1465697f514dSMiklos Szeredi 	err = follow_managed(path, nd->flags);
1466697f514dSMiklos Szeredi 	if (unlikely(err < 0)) {
1467697f514dSMiklos Szeredi 		path_put_conditional(path, nd);
1468697f514dSMiklos Szeredi 		return err;
1469697f514dSMiklos Szeredi 	}
1470697f514dSMiklos Szeredi 	if (err)
1471697f514dSMiklos Szeredi 		nd->flags |= LOOKUP_JUMPED;
1472697f514dSMiklos Szeredi 	return 0;
14731da177e4SLinus Torvalds }
14741da177e4SLinus Torvalds 
147552094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
147652094c8aSAl Viro {
147752094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
14784ad5abb3SAl Viro 		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
147952094c8aSAl Viro 		if (err != -ECHILD)
148052094c8aSAl Viro 			return err;
148119660af7SAl Viro 		if (unlazy_walk(nd, NULL))
148252094c8aSAl Viro 			return -ECHILD;
148352094c8aSAl Viro 	}
14844ad5abb3SAl Viro 	return inode_permission(nd->inode, MAY_EXEC);
148552094c8aSAl Viro }
148652094c8aSAl Viro 
14879856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
14889856fa1bSAl Viro {
14899856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
14909856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
14919856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
14929856fa1bSAl Viro 				return -ECHILD;
14939856fa1bSAl Viro 		} else
14949856fa1bSAl Viro 			follow_dotdot(nd);
14959856fa1bSAl Viro 	}
14969856fa1bSAl Viro 	return 0;
14979856fa1bSAl Viro }
14989856fa1bSAl Viro 
1499951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1500951361f9SAl Viro {
1501951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1502951361f9SAl Viro 		path_put(&nd->path);
1503951361f9SAl Viro 	} else {
1504951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
15055b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1506951361f9SAl Viro 			nd->root.mnt = NULL;
15078b61e74fSAl Viro 		rcu_read_unlock();
1508951361f9SAl Viro 	}
1509951361f9SAl Viro }
1510951361f9SAl Viro 
15113ddcd056SLinus Torvalds /*
15123ddcd056SLinus Torvalds  * Do we need to follow links? We _really_ want to be able
15133ddcd056SLinus Torvalds  * to do this check without having to look at inode->i_op,
15143ddcd056SLinus Torvalds  * so we keep a cache of "no, this doesn't need follow_link"
15153ddcd056SLinus Torvalds  * for the common case.
15163ddcd056SLinus Torvalds  */
1517b18825a7SDavid Howells static inline int should_follow_link(struct dentry *dentry, int follow)
15183ddcd056SLinus Torvalds {
1519b18825a7SDavid Howells 	return unlikely(d_is_symlink(dentry)) ? follow : 0;
15203ddcd056SLinus Torvalds }
15213ddcd056SLinus Torvalds 
1522ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
152321b9b073SAl Viro 		int follow)
1524ce57dfc1SAl Viro {
1525ce57dfc1SAl Viro 	struct inode *inode;
1526ce57dfc1SAl Viro 	int err;
1527ce57dfc1SAl Viro 	/*
1528ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1529ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1530ce57dfc1SAl Viro 	 * parent relationships.
1531ce57dfc1SAl Viro 	 */
153221b9b073SAl Viro 	if (unlikely(nd->last_type != LAST_NORM))
153321b9b073SAl Viro 		return handle_dots(nd, nd->last_type);
1534e97cdc87SAl Viro 	err = lookup_fast(nd, path, &inode);
1535ce57dfc1SAl Viro 	if (unlikely(err)) {
1536697f514dSMiklos Szeredi 		if (err < 0)
1537697f514dSMiklos Szeredi 			goto out_err;
1538697f514dSMiklos Szeredi 
1539cc2a5271SAl Viro 		err = lookup_slow(nd, path);
1540697f514dSMiklos Szeredi 		if (err < 0)
1541697f514dSMiklos Szeredi 			goto out_err;
1542697f514dSMiklos Szeredi 
1543697f514dSMiklos Szeredi 		inode = path->dentry->d_inode;
1544ce57dfc1SAl Viro 	}
1545697f514dSMiklos Szeredi 	err = -ENOENT;
154622213318SAl Viro 	if (!inode || d_is_negative(path->dentry))
1547697f514dSMiklos Szeredi 		goto out_path_put;
1548697f514dSMiklos Szeredi 
1549b18825a7SDavid Howells 	if (should_follow_link(path->dentry, follow)) {
155019660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
155119660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
1552697f514dSMiklos Szeredi 				err = -ECHILD;
1553697f514dSMiklos Szeredi 				goto out_err;
155419660af7SAl Viro 			}
155519660af7SAl Viro 		}
1556ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1557ce57dfc1SAl Viro 		return 1;
1558ce57dfc1SAl Viro 	}
1559ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1560ce57dfc1SAl Viro 	nd->inode = inode;
1561ce57dfc1SAl Viro 	return 0;
1562697f514dSMiklos Szeredi 
1563697f514dSMiklos Szeredi out_path_put:
1564697f514dSMiklos Szeredi 	path_to_nameidata(path, nd);
1565697f514dSMiklos Szeredi out_err:
1566697f514dSMiklos Szeredi 	terminate_walk(nd);
1567697f514dSMiklos Szeredi 	return err;
1568ce57dfc1SAl Viro }
1569ce57dfc1SAl Viro 
15701da177e4SLinus Torvalds /*
1571b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1572b356379aSAl Viro  * limiting consecutive symlinks to 40.
1573b356379aSAl Viro  *
1574b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1575b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1576b356379aSAl Viro  */
1577b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1578b356379aSAl Viro {
1579b356379aSAl Viro 	int res;
1580b356379aSAl Viro 
1581b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1582b356379aSAl Viro 		path_put_conditional(path, nd);
1583b356379aSAl Viro 		path_put(&nd->path);
1584b356379aSAl Viro 		return -ELOOP;
1585b356379aSAl Viro 	}
15861a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1587b356379aSAl Viro 
1588b356379aSAl Viro 	nd->depth++;
1589b356379aSAl Viro 	current->link_count++;
1590b356379aSAl Viro 
1591b356379aSAl Viro 	do {
1592b356379aSAl Viro 		struct path link = *path;
1593b356379aSAl Viro 		void *cookie;
1594574197e0SAl Viro 
1595574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
15966d7b5aaeSAl Viro 		if (res)
15976d7b5aaeSAl Viro 			break;
159821b9b073SAl Viro 		res = walk_component(nd, path, LOOKUP_FOLLOW);
1599574197e0SAl Viro 		put_link(nd, &link, cookie);
1600b356379aSAl Viro 	} while (res > 0);
1601b356379aSAl Viro 
1602b356379aSAl Viro 	current->link_count--;
1603b356379aSAl Viro 	nd->depth--;
1604b356379aSAl Viro 	return res;
1605b356379aSAl Viro }
1606b356379aSAl Viro 
1607b356379aSAl Viro /*
1608bfcfaa77SLinus Torvalds  * We can do the critical dentry name comparison and hashing
1609bfcfaa77SLinus Torvalds  * operations one word at a time, but we are limited to:
1610bfcfaa77SLinus Torvalds  *
1611bfcfaa77SLinus Torvalds  * - Architectures with fast unaligned word accesses. We could
1612bfcfaa77SLinus Torvalds  *   do a "get_unaligned()" if this helps and is sufficiently
1613bfcfaa77SLinus Torvalds  *   fast.
1614bfcfaa77SLinus Torvalds  *
1615bfcfaa77SLinus Torvalds  * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1616bfcfaa77SLinus Torvalds  *   do not trap on the (extremely unlikely) case of a page
1617bfcfaa77SLinus Torvalds  *   crossing operation.
1618bfcfaa77SLinus Torvalds  *
1619bfcfaa77SLinus Torvalds  * - Furthermore, we need an efficient 64-bit compile for the
1620bfcfaa77SLinus Torvalds  *   64-bit case in order to generate the "number of bytes in
1621bfcfaa77SLinus Torvalds  *   the final mask". Again, that could be replaced with a
1622bfcfaa77SLinus Torvalds  *   efficient population count instruction or similar.
1623bfcfaa77SLinus Torvalds  */
1624bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS
1625bfcfaa77SLinus Torvalds 
1626f68e556eSLinus Torvalds #include <asm/word-at-a-time.h>
1627bfcfaa77SLinus Torvalds 
1628f68e556eSLinus Torvalds #ifdef CONFIG_64BIT
1629bfcfaa77SLinus Torvalds 
1630bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash)
1631bfcfaa77SLinus Torvalds {
1632bfcfaa77SLinus Torvalds 	hash += hash >> (8*sizeof(int));
1633bfcfaa77SLinus Torvalds 	return hash;
1634bfcfaa77SLinus Torvalds }
1635bfcfaa77SLinus Torvalds 
1636bfcfaa77SLinus Torvalds #else	/* 32-bit case */
1637bfcfaa77SLinus Torvalds 
1638bfcfaa77SLinus Torvalds #define fold_hash(x) (x)
1639bfcfaa77SLinus Torvalds 
1640bfcfaa77SLinus Torvalds #endif
1641bfcfaa77SLinus Torvalds 
1642bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1643bfcfaa77SLinus Torvalds {
1644bfcfaa77SLinus Torvalds 	unsigned long a, mask;
1645bfcfaa77SLinus Torvalds 	unsigned long hash = 0;
1646bfcfaa77SLinus Torvalds 
1647bfcfaa77SLinus Torvalds 	for (;;) {
1648e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name);
1649bfcfaa77SLinus Torvalds 		if (len < sizeof(unsigned long))
1650bfcfaa77SLinus Torvalds 			break;
1651bfcfaa77SLinus Torvalds 		hash += a;
1652f132c5beSAl Viro 		hash *= 9;
1653bfcfaa77SLinus Torvalds 		name += sizeof(unsigned long);
1654bfcfaa77SLinus Torvalds 		len -= sizeof(unsigned long);
1655bfcfaa77SLinus Torvalds 		if (!len)
1656bfcfaa77SLinus Torvalds 			goto done;
1657bfcfaa77SLinus Torvalds 	}
1658a5c21dceSWill Deacon 	mask = bytemask_from_count(len);
1659bfcfaa77SLinus Torvalds 	hash += mask & a;
1660bfcfaa77SLinus Torvalds done:
1661bfcfaa77SLinus Torvalds 	return fold_hash(hash);
1662bfcfaa77SLinus Torvalds }
1663bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
1664bfcfaa77SLinus Torvalds 
1665bfcfaa77SLinus Torvalds /*
1666bfcfaa77SLinus Torvalds  * Calculate the length and hash of the path component, and
1667bfcfaa77SLinus Torvalds  * return the length of the component;
1668bfcfaa77SLinus Torvalds  */
1669bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1670bfcfaa77SLinus Torvalds {
167136126f8fSLinus Torvalds 	unsigned long a, b, adata, bdata, mask, hash, len;
167236126f8fSLinus Torvalds 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1673bfcfaa77SLinus Torvalds 
1674bfcfaa77SLinus Torvalds 	hash = a = 0;
1675bfcfaa77SLinus Torvalds 	len = -sizeof(unsigned long);
1676bfcfaa77SLinus Torvalds 	do {
1677bfcfaa77SLinus Torvalds 		hash = (hash + a) * 9;
1678bfcfaa77SLinus Torvalds 		len += sizeof(unsigned long);
1679e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name+len);
168036126f8fSLinus Torvalds 		b = a ^ REPEAT_BYTE('/');
168136126f8fSLinus Torvalds 	} while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1682bfcfaa77SLinus Torvalds 
168336126f8fSLinus Torvalds 	adata = prep_zero_mask(a, adata, &constants);
168436126f8fSLinus Torvalds 	bdata = prep_zero_mask(b, bdata, &constants);
168536126f8fSLinus Torvalds 
168636126f8fSLinus Torvalds 	mask = create_zero_mask(adata | bdata);
168736126f8fSLinus Torvalds 
168836126f8fSLinus Torvalds 	hash += a & zero_bytemask(mask);
1689bfcfaa77SLinus Torvalds 	*hashp = fold_hash(hash);
1690bfcfaa77SLinus Torvalds 
169136126f8fSLinus Torvalds 	return len + find_zero(mask);
1692bfcfaa77SLinus Torvalds }
1693bfcfaa77SLinus Torvalds 
1694bfcfaa77SLinus Torvalds #else
1695bfcfaa77SLinus Torvalds 
16960145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
16970145acc2SLinus Torvalds {
16980145acc2SLinus Torvalds 	unsigned long hash = init_name_hash();
16990145acc2SLinus Torvalds 	while (len--)
17000145acc2SLinus Torvalds 		hash = partial_name_hash(*name++, hash);
17010145acc2SLinus Torvalds 	return end_name_hash(hash);
17020145acc2SLinus Torvalds }
1703ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
17040145acc2SLinus Torvalds 
17053ddcd056SLinus Torvalds /*
1706200e9ef7SLinus Torvalds  * We know there's a real path component here of at least
1707200e9ef7SLinus Torvalds  * one character.
1708200e9ef7SLinus Torvalds  */
1709200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1710200e9ef7SLinus Torvalds {
1711200e9ef7SLinus Torvalds 	unsigned long hash = init_name_hash();
1712200e9ef7SLinus Torvalds 	unsigned long len = 0, c;
1713200e9ef7SLinus Torvalds 
1714200e9ef7SLinus Torvalds 	c = (unsigned char)*name;
1715200e9ef7SLinus Torvalds 	do {
1716200e9ef7SLinus Torvalds 		len++;
1717200e9ef7SLinus Torvalds 		hash = partial_name_hash(c, hash);
1718200e9ef7SLinus Torvalds 		c = (unsigned char)name[len];
1719200e9ef7SLinus Torvalds 	} while (c && c != '/');
1720200e9ef7SLinus Torvalds 	*hashp = end_name_hash(hash);
1721200e9ef7SLinus Torvalds 	return len;
1722200e9ef7SLinus Torvalds }
1723200e9ef7SLinus Torvalds 
1724bfcfaa77SLinus Torvalds #endif
1725bfcfaa77SLinus Torvalds 
1726200e9ef7SLinus Torvalds /*
17271da177e4SLinus Torvalds  * Name resolution.
1728ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1729ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
17301da177e4SLinus Torvalds  *
1731ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1732ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
17331da177e4SLinus Torvalds  */
17346de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
17351da177e4SLinus Torvalds {
17361da177e4SLinus Torvalds 	struct path next;
17371da177e4SLinus Torvalds 	int err;
17381da177e4SLinus Torvalds 
17391da177e4SLinus Torvalds 	while (*name=='/')
17401da177e4SLinus Torvalds 		name++;
17411da177e4SLinus Torvalds 	if (!*name)
1742086e183aSAl Viro 		return 0;
17431da177e4SLinus Torvalds 
17441da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
17451da177e4SLinus Torvalds 	for(;;) {
17461da177e4SLinus Torvalds 		struct qstr this;
1747200e9ef7SLinus Torvalds 		long len;
1748fe479a58SAl Viro 		int type;
17491da177e4SLinus Torvalds 
175052094c8aSAl Viro 		err = may_lookup(nd);
17511da177e4SLinus Torvalds  		if (err)
17521da177e4SLinus Torvalds 			break;
17531da177e4SLinus Torvalds 
1754200e9ef7SLinus Torvalds 		len = hash_name(name, &this.hash);
17551da177e4SLinus Torvalds 		this.name = name;
1756200e9ef7SLinus Torvalds 		this.len = len;
17571da177e4SLinus Torvalds 
1758fe479a58SAl Viro 		type = LAST_NORM;
1759200e9ef7SLinus Torvalds 		if (name[0] == '.') switch (len) {
1760fe479a58SAl Viro 			case 2:
1761200e9ef7SLinus Torvalds 				if (name[1] == '.') {
1762fe479a58SAl Viro 					type = LAST_DOTDOT;
176316c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
176416c2cd71SAl Viro 				}
1765fe479a58SAl Viro 				break;
1766fe479a58SAl Viro 			case 1:
1767fe479a58SAl Viro 				type = LAST_DOT;
1768fe479a58SAl Viro 		}
17695a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
17705a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
177116c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
17725a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1773da53be12SLinus Torvalds 				err = parent->d_op->d_hash(parent, &this);
17745a202bcdSAl Viro 				if (err < 0)
17755a202bcdSAl Viro 					break;
17765a202bcdSAl Viro 			}
17775a202bcdSAl Viro 		}
1778fe479a58SAl Viro 
17795f4a6a69SAl Viro 		nd->last = this;
17805f4a6a69SAl Viro 		nd->last_type = type;
17815f4a6a69SAl Viro 
1782200e9ef7SLinus Torvalds 		if (!name[len])
17835f4a6a69SAl Viro 			return 0;
1784200e9ef7SLinus Torvalds 		/*
1785200e9ef7SLinus Torvalds 		 * If it wasn't NUL, we know it was '/'. Skip that
1786200e9ef7SLinus Torvalds 		 * slash, and continue until no more slashes.
1787200e9ef7SLinus Torvalds 		 */
1788200e9ef7SLinus Torvalds 		do {
1789200e9ef7SLinus Torvalds 			len++;
1790200e9ef7SLinus Torvalds 		} while (unlikely(name[len] == '/'));
1791200e9ef7SLinus Torvalds 		if (!name[len])
17925f4a6a69SAl Viro 			return 0;
17935f4a6a69SAl Viro 
1794200e9ef7SLinus Torvalds 		name += len;
17951da177e4SLinus Torvalds 
179621b9b073SAl Viro 		err = walk_component(nd, &next, LOOKUP_FOLLOW);
1797ce57dfc1SAl Viro 		if (err < 0)
1798ce57dfc1SAl Viro 			return err;
1799fe479a58SAl Viro 
1800ce57dfc1SAl Viro 		if (err) {
1801b356379aSAl Viro 			err = nested_symlink(&next, nd);
18021da177e4SLinus Torvalds 			if (err)
1803a7472babSAl Viro 				return err;
180431e6b01fSNick Piggin 		}
180544b1d530SMiklos Szeredi 		if (!d_can_lookup(nd->path.dentry)) {
18063ddcd056SLinus Torvalds 			err = -ENOTDIR;
18073ddcd056SLinus Torvalds 			break;
18085f4a6a69SAl Viro 		}
1809ce57dfc1SAl Viro 	}
1810951361f9SAl Viro 	terminate_walk(nd);
18111da177e4SLinus Torvalds 	return err;
18121da177e4SLinus Torvalds }
18131da177e4SLinus Torvalds 
181470e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
181570e9b357SAl Viro 		     struct nameidata *nd, struct file **fp)
181631e6b01fSNick Piggin {
181731e6b01fSNick Piggin 	int retval = 0;
181831e6b01fSNick Piggin 
181931e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
182016c2cd71SAl Viro 	nd->flags = flags | LOOKUP_JUMPED;
182131e6b01fSNick Piggin 	nd->depth = 0;
18225b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
1823b18825a7SDavid Howells 		struct dentry *root = nd->root.dentry;
1824b18825a7SDavid Howells 		struct inode *inode = root->d_inode;
182573d049a4SAl Viro 		if (*name) {
182644b1d530SMiklos Szeredi 			if (!d_can_lookup(root))
18275b6ca027SAl Viro 				return -ENOTDIR;
18285b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
18295b6ca027SAl Viro 			if (retval)
18305b6ca027SAl Viro 				return retval;
183173d049a4SAl Viro 		}
18325b6ca027SAl Viro 		nd->path = nd->root;
18335b6ca027SAl Viro 		nd->inode = inode;
18345b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
18358b61e74fSAl Viro 			rcu_read_lock();
18365b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
183748a066e7SAl Viro 			nd->m_seq = read_seqbegin(&mount_lock);
18385b6ca027SAl Viro 		} else {
18395b6ca027SAl Viro 			path_get(&nd->path);
18405b6ca027SAl Viro 		}
18415b6ca027SAl Viro 		return 0;
18425b6ca027SAl Viro 	}
18435b6ca027SAl Viro 
184431e6b01fSNick Piggin 	nd->root.mnt = NULL;
184531e6b01fSNick Piggin 
184648a066e7SAl Viro 	nd->m_seq = read_seqbegin(&mount_lock);
184731e6b01fSNick Piggin 	if (*name=='/') {
1848e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
18498b61e74fSAl Viro 			rcu_read_lock();
1850e41f7d4eSAl Viro 			set_root_rcu(nd);
1851e41f7d4eSAl Viro 		} else {
1852e41f7d4eSAl Viro 			set_root(nd);
1853e41f7d4eSAl Viro 			path_get(&nd->root);
1854e41f7d4eSAl Viro 		}
185531e6b01fSNick Piggin 		nd->path = nd->root;
185631e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1857e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
185831e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1859c28cc364SNick Piggin 			unsigned seq;
186031e6b01fSNick Piggin 
18618b61e74fSAl Viro 			rcu_read_lock();
186231e6b01fSNick Piggin 
1863c28cc364SNick Piggin 			do {
1864c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
186531e6b01fSNick Piggin 				nd->path = fs->pwd;
1866c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1867c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1868e41f7d4eSAl Viro 		} else {
1869e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1870e41f7d4eSAl Viro 		}
187131e6b01fSNick Piggin 	} else {
1872582aa64aSJeff Layton 		/* Caller must check execute permissions on the starting path component */
18732903ff01SAl Viro 		struct fd f = fdget_raw(dfd);
187431e6b01fSNick Piggin 		struct dentry *dentry;
187531e6b01fSNick Piggin 
18762903ff01SAl Viro 		if (!f.file)
18772903ff01SAl Viro 			return -EBADF;
187831e6b01fSNick Piggin 
18792903ff01SAl Viro 		dentry = f.file->f_path.dentry;
188031e6b01fSNick Piggin 
1881f52e0c11SAl Viro 		if (*name) {
188244b1d530SMiklos Szeredi 			if (!d_can_lookup(dentry)) {
18832903ff01SAl Viro 				fdput(f);
18842903ff01SAl Viro 				return -ENOTDIR;
1885f52e0c11SAl Viro 			}
18862903ff01SAl Viro 		}
18872903ff01SAl Viro 
18882903ff01SAl Viro 		nd->path = f.file->f_path;
1889e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
18909c225f26SLinus Torvalds 			if (f.flags & FDPUT_FPUT)
18912903ff01SAl Viro 				*fp = f.file;
1892c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
18938b61e74fSAl Viro 			rcu_read_lock();
18945590ff0dSUlrich Drepper 		} else {
18952903ff01SAl Viro 			path_get(&nd->path);
18962903ff01SAl Viro 			fdput(f);
18971da177e4SLinus Torvalds 		}
1898e41f7d4eSAl Viro 	}
1899e41f7d4eSAl Viro 
190031e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
19019b4a9b14SAl Viro 	return 0;
19029b4a9b14SAl Viro }
19039b4a9b14SAl Viro 
1904bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1905bd92d7feSAl Viro {
1906bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1907bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1908bd92d7feSAl Viro 
1909bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
191021b9b073SAl Viro 	return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
1911bd92d7feSAl Viro }
1912bd92d7feSAl Viro 
19139b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1914ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
19159b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
19169b4a9b14SAl Viro {
191770e9b357SAl Viro 	struct file *base = NULL;
1918bd92d7feSAl Viro 	struct path path;
1919bd92d7feSAl Viro 	int err;
192031e6b01fSNick Piggin 
192131e6b01fSNick Piggin 	/*
192231e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
192331e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
192431e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
192531e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
192631e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
192731e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
192831e6b01fSNick Piggin 	 * analogue, foo_rcu().
192931e6b01fSNick Piggin 	 *
193031e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
193131e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
193231e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
193331e6b01fSNick Piggin 	 * be able to complete).
193431e6b01fSNick Piggin 	 */
1935bd92d7feSAl Viro 	err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
1936ee0827cdSAl Viro 
1937bd92d7feSAl Viro 	if (unlikely(err))
1938bd92d7feSAl Viro 		return err;
1939ee0827cdSAl Viro 
1940ee0827cdSAl Viro 	current->total_link_count = 0;
1941bd92d7feSAl Viro 	err = link_path_walk(name, nd);
1942bd92d7feSAl Viro 
1943bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1944bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1945bd92d7feSAl Viro 		while (err > 0) {
1946bd92d7feSAl Viro 			void *cookie;
1947bd92d7feSAl Viro 			struct path link = path;
1948800179c9SKees Cook 			err = may_follow_link(&link, nd);
1949800179c9SKees Cook 			if (unlikely(err))
1950800179c9SKees Cook 				break;
1951bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
1952574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
19536d7b5aaeSAl Viro 			if (err)
19546d7b5aaeSAl Viro 				break;
1955bd92d7feSAl Viro 			err = lookup_last(nd, &path);
1956574197e0SAl Viro 			put_link(nd, &link, cookie);
1957bd92d7feSAl Viro 		}
1958bd92d7feSAl Viro 	}
1959ee0827cdSAl Viro 
19609f1fafeeSAl Viro 	if (!err)
19619f1fafeeSAl Viro 		err = complete_walk(nd);
1962bd92d7feSAl Viro 
1963bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
196444b1d530SMiklos Szeredi 		if (!d_can_lookup(nd->path.dentry)) {
1965bd92d7feSAl Viro 			path_put(&nd->path);
1966bd23a539SAl Viro 			err = -ENOTDIR;
1967bd92d7feSAl Viro 		}
1968bd92d7feSAl Viro 	}
196916c2cd71SAl Viro 
197070e9b357SAl Viro 	if (base)
197170e9b357SAl Viro 		fput(base);
1972ee0827cdSAl Viro 
19735b6ca027SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
197431e6b01fSNick Piggin 		path_put(&nd->root);
197531e6b01fSNick Piggin 		nd->root.mnt = NULL;
197631e6b01fSNick Piggin 	}
1977bd92d7feSAl Viro 	return err;
197831e6b01fSNick Piggin }
197931e6b01fSNick Piggin 
1980873f1eedSJeff Layton static int filename_lookup(int dfd, struct filename *name,
1981873f1eedSJeff Layton 				unsigned int flags, struct nameidata *nd)
1982873f1eedSJeff Layton {
1983873f1eedSJeff Layton 	int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
1984873f1eedSJeff Layton 	if (unlikely(retval == -ECHILD))
1985873f1eedSJeff Layton 		retval = path_lookupat(dfd, name->name, flags, nd);
1986873f1eedSJeff Layton 	if (unlikely(retval == -ESTALE))
1987873f1eedSJeff Layton 		retval = path_lookupat(dfd, name->name,
1988873f1eedSJeff Layton 						flags | LOOKUP_REVAL, nd);
1989873f1eedSJeff Layton 
1990873f1eedSJeff Layton 	if (likely(!retval))
1991adb5c247SJeff Layton 		audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
1992873f1eedSJeff Layton 	return retval;
1993873f1eedSJeff Layton }
1994873f1eedSJeff Layton 
1995ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
1996ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
1997ee0827cdSAl Viro {
1998873f1eedSJeff Layton 	struct filename filename = { .name = name };
1999ee0827cdSAl Viro 
2000873f1eedSJeff Layton 	return filename_lookup(dfd, &filename, flags, nd);
20011da177e4SLinus Torvalds }
20021da177e4SLinus Torvalds 
200379714f72SAl Viro /* does lookup, returns the object with parent locked */
200479714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path)
20055590ff0dSUlrich Drepper {
200679714f72SAl Viro 	struct nameidata nd;
200779714f72SAl Viro 	struct dentry *d;
200879714f72SAl Viro 	int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
200979714f72SAl Viro 	if (err)
201079714f72SAl Viro 		return ERR_PTR(err);
201179714f72SAl Viro 	if (nd.last_type != LAST_NORM) {
201279714f72SAl Viro 		path_put(&nd.path);
201379714f72SAl Viro 		return ERR_PTR(-EINVAL);
201479714f72SAl Viro 	}
201579714f72SAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
20161e0ea001SAl Viro 	d = __lookup_hash(&nd.last, nd.path.dentry, 0);
201779714f72SAl Viro 	if (IS_ERR(d)) {
201879714f72SAl Viro 		mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
201979714f72SAl Viro 		path_put(&nd.path);
202079714f72SAl Viro 		return d;
202179714f72SAl Viro 	}
202279714f72SAl Viro 	*path = nd.path;
202379714f72SAl Viro 	return d;
20245590ff0dSUlrich Drepper }
20255590ff0dSUlrich Drepper 
2026d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
2027d1811465SAl Viro {
2028d1811465SAl Viro 	struct nameidata nd;
2029d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2030d1811465SAl Viro 	if (!res)
2031d1811465SAl Viro 		*path = nd.path;
2032d1811465SAl Viro 	return res;
2033d1811465SAl Viro }
20344d359507SAl Viro EXPORT_SYMBOL(kern_path);
2035d1811465SAl Viro 
203616f18200SJosef 'Jeff' Sipek /**
203716f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
203816f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
203916f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
204016f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
204116f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
2042e0a01249SAl Viro  * @path: pointer to struct path to fill
204316f18200SJosef 'Jeff' Sipek  */
204416f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
204516f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
2046e0a01249SAl Viro 		    struct path *path)
204716f18200SJosef 'Jeff' Sipek {
2048e0a01249SAl Viro 	struct nameidata nd;
2049e0a01249SAl Viro 	int err;
2050e0a01249SAl Viro 	nd.root.dentry = dentry;
2051e0a01249SAl Viro 	nd.root.mnt = mnt;
2052e0a01249SAl Viro 	BUG_ON(flags & LOOKUP_PARENT);
20535b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
2054e0a01249SAl Viro 	err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2055e0a01249SAl Viro 	if (!err)
2056e0a01249SAl Viro 		*path = nd.path;
2057e0a01249SAl Viro 	return err;
205816f18200SJosef 'Jeff' Sipek }
20594d359507SAl Viro EXPORT_SYMBOL(vfs_path_lookup);
206016f18200SJosef 'Jeff' Sipek 
2061057f6c01SJames Morris /*
2062057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
2063057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
2064057f6c01SJames Morris  * SMP-safe.
2065057f6c01SJames Morris  */
2066a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
20671da177e4SLinus Torvalds {
206872bd866aSAl Viro 	return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
20691da177e4SLinus Torvalds }
20701da177e4SLinus Torvalds 
2071eead1911SChristoph Hellwig /**
2072a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
2073eead1911SChristoph Hellwig  * @name:	pathname component to lookup
2074eead1911SChristoph Hellwig  * @base:	base directory to lookup from
2075eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
2076eead1911SChristoph Hellwig  *
2077a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
2078a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
2079eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
2080eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
2081eead1911SChristoph Hellwig  */
2082057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2083057f6c01SJames Morris {
2084057f6c01SJames Morris 	struct qstr this;
20856a96ba54SAl Viro 	unsigned int c;
2086cda309deSMiklos Szeredi 	int err;
2087057f6c01SJames Morris 
20882f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
20892f9092e1SDavid Woodhouse 
20906a96ba54SAl Viro 	this.name = name;
20916a96ba54SAl Viro 	this.len = len;
20920145acc2SLinus Torvalds 	this.hash = full_name_hash(name, len);
20936a96ba54SAl Viro 	if (!len)
20946a96ba54SAl Viro 		return ERR_PTR(-EACCES);
20956a96ba54SAl Viro 
209621d8a15aSAl Viro 	if (unlikely(name[0] == '.')) {
209721d8a15aSAl Viro 		if (len < 2 || (len == 2 && name[1] == '.'))
209821d8a15aSAl Viro 			return ERR_PTR(-EACCES);
209921d8a15aSAl Viro 	}
210021d8a15aSAl Viro 
21016a96ba54SAl Viro 	while (len--) {
21026a96ba54SAl Viro 		c = *(const unsigned char *)name++;
21036a96ba54SAl Viro 		if (c == '/' || c == '\0')
21046a96ba54SAl Viro 			return ERR_PTR(-EACCES);
21056a96ba54SAl Viro 	}
21065a202bcdSAl Viro 	/*
21075a202bcdSAl Viro 	 * See if the low-level filesystem might want
21085a202bcdSAl Viro 	 * to use its own hash..
21095a202bcdSAl Viro 	 */
21105a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
2111da53be12SLinus Torvalds 		int err = base->d_op->d_hash(base, &this);
21125a202bcdSAl Viro 		if (err < 0)
21135a202bcdSAl Viro 			return ERR_PTR(err);
21145a202bcdSAl Viro 	}
2115eead1911SChristoph Hellwig 
2116cda309deSMiklos Szeredi 	err = inode_permission(base->d_inode, MAY_EXEC);
2117cda309deSMiklos Szeredi 	if (err)
2118cda309deSMiklos Szeredi 		return ERR_PTR(err);
2119cda309deSMiklos Szeredi 
212072bd866aSAl Viro 	return __lookup_hash(&this, base, 0);
2121057f6c01SJames Morris }
21224d359507SAl Viro EXPORT_SYMBOL(lookup_one_len);
2123057f6c01SJames Morris 
21241fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
21251fa1e7f6SAndy Whitcroft 		 struct path *path, int *empty)
21261da177e4SLinus Torvalds {
21272d8f3038SAl Viro 	struct nameidata nd;
212891a27b2aSJeff Layton 	struct filename *tmp = getname_flags(name, flags, empty);
21291da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
21301da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
21312d8f3038SAl Viro 
21322d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
21332d8f3038SAl Viro 
2134873f1eedSJeff Layton 		err = filename_lookup(dfd, tmp, flags, &nd);
21351da177e4SLinus Torvalds 		putname(tmp);
21362d8f3038SAl Viro 		if (!err)
21372d8f3038SAl Viro 			*path = nd.path;
21381da177e4SLinus Torvalds 	}
21391da177e4SLinus Torvalds 	return err;
21401da177e4SLinus Torvalds }
21411da177e4SLinus Torvalds 
21421fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags,
21431fa1e7f6SAndy Whitcroft 		 struct path *path)
21441fa1e7f6SAndy Whitcroft {
2145f7493e5dSLinus Torvalds 	return user_path_at_empty(dfd, name, flags, path, NULL);
21461fa1e7f6SAndy Whitcroft }
21474d359507SAl Viro EXPORT_SYMBOL(user_path_at);
21481fa1e7f6SAndy Whitcroft 
2149873f1eedSJeff Layton /*
2150873f1eedSJeff Layton  * NB: most callers don't do anything directly with the reference to the
2151873f1eedSJeff Layton  *     to struct filename, but the nd->last pointer points into the name string
2152873f1eedSJeff Layton  *     allocated by getname. So we must hold the reference to it until all
2153873f1eedSJeff Layton  *     path-walking is complete.
2154873f1eedSJeff Layton  */
215591a27b2aSJeff Layton static struct filename *
21569e790bd6SJeff Layton user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
21579e790bd6SJeff Layton 		 unsigned int flags)
21582ad94ae6SAl Viro {
215991a27b2aSJeff Layton 	struct filename *s = getname(path);
21602ad94ae6SAl Viro 	int error;
21612ad94ae6SAl Viro 
21629e790bd6SJeff Layton 	/* only LOOKUP_REVAL is allowed in extra flags */
21639e790bd6SJeff Layton 	flags &= LOOKUP_REVAL;
21649e790bd6SJeff Layton 
21652ad94ae6SAl Viro 	if (IS_ERR(s))
216691a27b2aSJeff Layton 		return s;
21672ad94ae6SAl Viro 
21689e790bd6SJeff Layton 	error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
216991a27b2aSJeff Layton 	if (error) {
21702ad94ae6SAl Viro 		putname(s);
217191a27b2aSJeff Layton 		return ERR_PTR(error);
217291a27b2aSJeff Layton 	}
21732ad94ae6SAl Viro 
217491a27b2aSJeff Layton 	return s;
21752ad94ae6SAl Viro }
21762ad94ae6SAl Viro 
21778033426eSJeff Layton /**
2178197df04cSAl Viro  * mountpoint_last - look up last component for umount
21798033426eSJeff Layton  * @nd:   pathwalk nameidata - currently pointing at parent directory of "last"
21808033426eSJeff Layton  * @path: pointer to container for result
21818033426eSJeff Layton  *
21828033426eSJeff Layton  * This is a special lookup_last function just for umount. In this case, we
21838033426eSJeff Layton  * need to resolve the path without doing any revalidation.
21848033426eSJeff Layton  *
21858033426eSJeff Layton  * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
21868033426eSJeff Layton  * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
21878033426eSJeff Layton  * in almost all cases, this lookup will be served out of the dcache. The only
21888033426eSJeff Layton  * cases where it won't are if nd->last refers to a symlink or the path is
21898033426eSJeff Layton  * bogus and it doesn't exist.
21908033426eSJeff Layton  *
21918033426eSJeff Layton  * Returns:
21928033426eSJeff Layton  * -error: if there was an error during lookup. This includes -ENOENT if the
21938033426eSJeff Layton  *         lookup found a negative dentry. The nd->path reference will also be
21948033426eSJeff Layton  *         put in this case.
21958033426eSJeff Layton  *
21968033426eSJeff Layton  * 0:      if we successfully resolved nd->path and found it to not to be a
21978033426eSJeff Layton  *         symlink that needs to be followed. "path" will also be populated.
21988033426eSJeff Layton  *         The nd->path reference will also be put.
21998033426eSJeff Layton  *
22008033426eSJeff Layton  * 1:      if we successfully resolved nd->last and found it to be a symlink
22018033426eSJeff Layton  *         that needs to be followed. "path" will be populated with the path
22028033426eSJeff Layton  *         to the link, and nd->path will *not* be put.
22038033426eSJeff Layton  */
22048033426eSJeff Layton static int
2205197df04cSAl Viro mountpoint_last(struct nameidata *nd, struct path *path)
22068033426eSJeff Layton {
22078033426eSJeff Layton 	int error = 0;
22088033426eSJeff Layton 	struct dentry *dentry;
22098033426eSJeff Layton 	struct dentry *dir = nd->path.dentry;
22108033426eSJeff Layton 
221135759521SAl Viro 	/* If we're in rcuwalk, drop out of it to handle last component */
221235759521SAl Viro 	if (nd->flags & LOOKUP_RCU) {
221335759521SAl Viro 		if (unlazy_walk(nd, NULL)) {
22148033426eSJeff Layton 			error = -ECHILD;
221535759521SAl Viro 			goto out;
221635759521SAl Viro 		}
22178033426eSJeff Layton 	}
22188033426eSJeff Layton 
22198033426eSJeff Layton 	nd->flags &= ~LOOKUP_PARENT;
22208033426eSJeff Layton 
22218033426eSJeff Layton 	if (unlikely(nd->last_type != LAST_NORM)) {
22228033426eSJeff Layton 		error = handle_dots(nd, nd->last_type);
222335759521SAl Viro 		if (error)
222435759521SAl Viro 			goto out;
22258033426eSJeff Layton 		dentry = dget(nd->path.dentry);
222635759521SAl Viro 		goto done;
22278033426eSJeff Layton 	}
22288033426eSJeff Layton 
22298033426eSJeff Layton 	mutex_lock(&dir->d_inode->i_mutex);
22308033426eSJeff Layton 	dentry = d_lookup(dir, &nd->last);
22318033426eSJeff Layton 	if (!dentry) {
22328033426eSJeff Layton 		/*
22338033426eSJeff Layton 		 * No cached dentry. Mounted dentries are pinned in the cache,
22348033426eSJeff Layton 		 * so that means that this dentry is probably a symlink or the
22358033426eSJeff Layton 		 * path doesn't actually point to a mounted dentry.
22368033426eSJeff Layton 		 */
22378033426eSJeff Layton 		dentry = d_alloc(dir, &nd->last);
22388033426eSJeff Layton 		if (!dentry) {
22398033426eSJeff Layton 			error = -ENOMEM;
2240bcceeebaSDave Jones 			mutex_unlock(&dir->d_inode->i_mutex);
224135759521SAl Viro 			goto out;
22428033426eSJeff Layton 		}
224335759521SAl Viro 		dentry = lookup_real(dir->d_inode, dentry, nd->flags);
224435759521SAl Viro 		error = PTR_ERR(dentry);
2245bcceeebaSDave Jones 		if (IS_ERR(dentry)) {
2246bcceeebaSDave Jones 			mutex_unlock(&dir->d_inode->i_mutex);
224735759521SAl Viro 			goto out;
22488033426eSJeff Layton 		}
2249bcceeebaSDave Jones 	}
22508033426eSJeff Layton 	mutex_unlock(&dir->d_inode->i_mutex);
22518033426eSJeff Layton 
225235759521SAl Viro done:
225322213318SAl Viro 	if (!dentry->d_inode || d_is_negative(dentry)) {
22548033426eSJeff Layton 		error = -ENOENT;
22558033426eSJeff Layton 		dput(dentry);
225635759521SAl Viro 		goto out;
225735759521SAl Viro 	}
22588033426eSJeff Layton 	path->dentry = dentry;
22598033426eSJeff Layton 	path->mnt = mntget(nd->path.mnt);
2260b18825a7SDavid Howells 	if (should_follow_link(dentry, nd->flags & LOOKUP_FOLLOW))
22618033426eSJeff Layton 		return 1;
22628033426eSJeff Layton 	follow_mount(path);
226335759521SAl Viro 	error = 0;
226435759521SAl Viro out:
22658033426eSJeff Layton 	terminate_walk(nd);
22668033426eSJeff Layton 	return error;
22678033426eSJeff Layton }
22688033426eSJeff Layton 
22698033426eSJeff Layton /**
2270197df04cSAl Viro  * path_mountpoint - look up a path to be umounted
22718033426eSJeff Layton  * @dfd:	directory file descriptor to start walk from
22728033426eSJeff Layton  * @name:	full pathname to walk
2273606d6fe3SRandy Dunlap  * @path:	pointer to container for result
22748033426eSJeff Layton  * @flags:	lookup flags
22758033426eSJeff Layton  *
22768033426eSJeff Layton  * Look up the given name, but don't attempt to revalidate the last component.
2277606d6fe3SRandy Dunlap  * Returns 0 and "path" will be valid on success; Returns error otherwise.
22788033426eSJeff Layton  */
22798033426eSJeff Layton static int
2280197df04cSAl Viro path_mountpoint(int dfd, const char *name, struct path *path, unsigned int flags)
22818033426eSJeff Layton {
22828033426eSJeff Layton 	struct file *base = NULL;
22838033426eSJeff Layton 	struct nameidata nd;
22848033426eSJeff Layton 	int err;
22858033426eSJeff Layton 
22868033426eSJeff Layton 	err = path_init(dfd, name, flags | LOOKUP_PARENT, &nd, &base);
22878033426eSJeff Layton 	if (unlikely(err))
22888033426eSJeff Layton 		return err;
22898033426eSJeff Layton 
22908033426eSJeff Layton 	current->total_link_count = 0;
22918033426eSJeff Layton 	err = link_path_walk(name, &nd);
22928033426eSJeff Layton 	if (err)
22938033426eSJeff Layton 		goto out;
22948033426eSJeff Layton 
2295197df04cSAl Viro 	err = mountpoint_last(&nd, path);
22968033426eSJeff Layton 	while (err > 0) {
22978033426eSJeff Layton 		void *cookie;
22988033426eSJeff Layton 		struct path link = *path;
22998033426eSJeff Layton 		err = may_follow_link(&link, &nd);
23008033426eSJeff Layton 		if (unlikely(err))
23018033426eSJeff Layton 			break;
23028033426eSJeff Layton 		nd.flags |= LOOKUP_PARENT;
23038033426eSJeff Layton 		err = follow_link(&link, &nd, &cookie);
23048033426eSJeff Layton 		if (err)
23058033426eSJeff Layton 			break;
2306197df04cSAl Viro 		err = mountpoint_last(&nd, path);
23078033426eSJeff Layton 		put_link(&nd, &link, cookie);
23088033426eSJeff Layton 	}
23098033426eSJeff Layton out:
23108033426eSJeff Layton 	if (base)
23118033426eSJeff Layton 		fput(base);
23128033426eSJeff Layton 
23138033426eSJeff Layton 	if (nd.root.mnt && !(nd.flags & LOOKUP_ROOT))
23148033426eSJeff Layton 		path_put(&nd.root);
23158033426eSJeff Layton 
23168033426eSJeff Layton 	return err;
23178033426eSJeff Layton }
23188033426eSJeff Layton 
23192d864651SAl Viro static int
23202d864651SAl Viro filename_mountpoint(int dfd, struct filename *s, struct path *path,
23212d864651SAl Viro 			unsigned int flags)
23222d864651SAl Viro {
23232d864651SAl Viro 	int error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_RCU);
23242d864651SAl Viro 	if (unlikely(error == -ECHILD))
23252d864651SAl Viro 		error = path_mountpoint(dfd, s->name, path, flags);
23262d864651SAl Viro 	if (unlikely(error == -ESTALE))
23272d864651SAl Viro 		error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_REVAL);
23282d864651SAl Viro 	if (likely(!error))
23292d864651SAl Viro 		audit_inode(s, path->dentry, 0);
23302d864651SAl Viro 	return error;
23312d864651SAl Viro }
23322d864651SAl Viro 
23338033426eSJeff Layton /**
2334197df04cSAl Viro  * user_path_mountpoint_at - lookup a path from userland in order to umount it
23358033426eSJeff Layton  * @dfd:	directory file descriptor
23368033426eSJeff Layton  * @name:	pathname from userland
23378033426eSJeff Layton  * @flags:	lookup flags
23388033426eSJeff Layton  * @path:	pointer to container to hold result
23398033426eSJeff Layton  *
23408033426eSJeff Layton  * A umount is a special case for path walking. We're not actually interested
23418033426eSJeff Layton  * in the inode in this situation, and ESTALE errors can be a problem. We
23428033426eSJeff Layton  * simply want track down the dentry and vfsmount attached at the mountpoint
23438033426eSJeff Layton  * and avoid revalidating the last component.
23448033426eSJeff Layton  *
23458033426eSJeff Layton  * Returns 0 and populates "path" on success.
23468033426eSJeff Layton  */
23478033426eSJeff Layton int
2348197df04cSAl Viro user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
23498033426eSJeff Layton 			struct path *path)
23508033426eSJeff Layton {
23518033426eSJeff Layton 	struct filename *s = getname(name);
23528033426eSJeff Layton 	int error;
23538033426eSJeff Layton 	if (IS_ERR(s))
23548033426eSJeff Layton 		return PTR_ERR(s);
23552d864651SAl Viro 	error = filename_mountpoint(dfd, s, path, flags);
23568033426eSJeff Layton 	putname(s);
23578033426eSJeff Layton 	return error;
23588033426eSJeff Layton }
23598033426eSJeff Layton 
23602d864651SAl Viro int
23612d864651SAl Viro kern_path_mountpoint(int dfd, const char *name, struct path *path,
23622d864651SAl Viro 			unsigned int flags)
23632d864651SAl Viro {
23642d864651SAl Viro 	struct filename s = {.name = name};
23652d864651SAl Viro 	return filename_mountpoint(dfd, &s, path, flags);
23662d864651SAl Viro }
23672d864651SAl Viro EXPORT_SYMBOL(kern_path_mountpoint);
23682d864651SAl Viro 
23691da177e4SLinus Torvalds /*
23701da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
23711da177e4SLinus Torvalds  * minimal.
23721da177e4SLinus Torvalds  */
23731da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
23741da177e4SLinus Torvalds {
23758e96e3b7SEric W. Biederman 	kuid_t fsuid = current_fsuid();
2376da9592edSDavid Howells 
23771da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
23781da177e4SLinus Torvalds 		return 0;
23798e96e3b7SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
23801da177e4SLinus Torvalds 		return 0;
23818e96e3b7SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
23821da177e4SLinus Torvalds 		return 0;
238323adbe12SAndy Lutomirski 	return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
23841da177e4SLinus Torvalds }
23851da177e4SLinus Torvalds 
23861da177e4SLinus Torvalds /*
23871da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
23881da177e4SLinus Torvalds  *  whether the type of victim is right.
23891da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
23901da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
23911da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
23921da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
23931da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
23941da177e4SLinus Torvalds  *	a. be owner of dir, or
23951da177e4SLinus Torvalds  *	b. be owner of victim, or
23961da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
23971da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
23981da177e4SLinus Torvalds  *     links pointing to it.
23991da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
24001da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
24011da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
24021da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
24031da177e4SLinus Torvalds  *     nfs_async_unlink().
24041da177e4SLinus Torvalds  */
2405b18825a7SDavid Howells static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
24061da177e4SLinus Torvalds {
2407b18825a7SDavid Howells 	struct inode *inode = victim->d_inode;
24081da177e4SLinus Torvalds 	int error;
24091da177e4SLinus Torvalds 
2410b18825a7SDavid Howells 	if (d_is_negative(victim))
24111da177e4SLinus Torvalds 		return -ENOENT;
2412b18825a7SDavid Howells 	BUG_ON(!inode);
24131da177e4SLinus Torvalds 
24141da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
24154fa6b5ecSJeff Layton 	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
24161da177e4SLinus Torvalds 
2417f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
24181da177e4SLinus Torvalds 	if (error)
24191da177e4SLinus Torvalds 		return error;
24201da177e4SLinus Torvalds 	if (IS_APPEND(dir))
24211da177e4SLinus Torvalds 		return -EPERM;
2422b18825a7SDavid Howells 
2423b18825a7SDavid Howells 	if (check_sticky(dir, inode) || IS_APPEND(inode) ||
2424b18825a7SDavid Howells 	    IS_IMMUTABLE(inode) || IS_SWAPFILE(inode))
24251da177e4SLinus Torvalds 		return -EPERM;
24261da177e4SLinus Torvalds 	if (isdir) {
242744b1d530SMiklos Szeredi 		if (!d_is_dir(victim))
24281da177e4SLinus Torvalds 			return -ENOTDIR;
24291da177e4SLinus Torvalds 		if (IS_ROOT(victim))
24301da177e4SLinus Torvalds 			return -EBUSY;
243144b1d530SMiklos Szeredi 	} else if (d_is_dir(victim))
24321da177e4SLinus Torvalds 		return -EISDIR;
24331da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
24341da177e4SLinus Torvalds 		return -ENOENT;
24351da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
24361da177e4SLinus Torvalds 		return -EBUSY;
24371da177e4SLinus Torvalds 	return 0;
24381da177e4SLinus Torvalds }
24391da177e4SLinus Torvalds 
24401da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
24411da177e4SLinus Torvalds  *  dir.
24421da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
24431da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
24441da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
24451da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
24461da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
24471da177e4SLinus Torvalds  */
2448a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
24491da177e4SLinus Torvalds {
245014e972b4SJeff Layton 	audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
24511da177e4SLinus Torvalds 	if (child->d_inode)
24521da177e4SLinus Torvalds 		return -EEXIST;
24531da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
24541da177e4SLinus Torvalds 		return -ENOENT;
2455f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
24561da177e4SLinus Torvalds }
24571da177e4SLinus Torvalds 
24581da177e4SLinus Torvalds /*
24591da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
24601da177e4SLinus Torvalds  */
24611da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
24621da177e4SLinus Torvalds {
24631da177e4SLinus Torvalds 	struct dentry *p;
24641da177e4SLinus Torvalds 
24651da177e4SLinus Torvalds 	if (p1 == p2) {
2466f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
24671da177e4SLinus Torvalds 		return NULL;
24681da177e4SLinus Torvalds 	}
24691da177e4SLinus Torvalds 
2470a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
24711da177e4SLinus Torvalds 
2472e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
2473e2761a11SOGAWA Hirofumi 	if (p) {
2474f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2475f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
24761da177e4SLinus Torvalds 		return p;
24771da177e4SLinus Torvalds 	}
24781da177e4SLinus Torvalds 
2479e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
2480e2761a11SOGAWA Hirofumi 	if (p) {
2481f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2482f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
24831da177e4SLinus Torvalds 		return p;
24841da177e4SLinus Torvalds 	}
24851da177e4SLinus Torvalds 
2486f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2487f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
24881da177e4SLinus Torvalds 	return NULL;
24891da177e4SLinus Torvalds }
24904d359507SAl Viro EXPORT_SYMBOL(lock_rename);
24911da177e4SLinus Torvalds 
24921da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
24931da177e4SLinus Torvalds {
24941b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
24951da177e4SLinus Torvalds 	if (p1 != p2) {
24961b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
2497a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
24981da177e4SLinus Torvalds 	}
24991da177e4SLinus Torvalds }
25004d359507SAl Viro EXPORT_SYMBOL(unlock_rename);
25011da177e4SLinus Torvalds 
25024acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2503312b63fbSAl Viro 		bool want_excl)
25041da177e4SLinus Torvalds {
2505a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
25061da177e4SLinus Torvalds 	if (error)
25071da177e4SLinus Torvalds 		return error;
25081da177e4SLinus Torvalds 
2509acfa4380SAl Viro 	if (!dir->i_op->create)
25101da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
25111da177e4SLinus Torvalds 	mode &= S_IALLUGO;
25121da177e4SLinus Torvalds 	mode |= S_IFREG;
25131da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
25141da177e4SLinus Torvalds 	if (error)
25151da177e4SLinus Torvalds 		return error;
2516312b63fbSAl Viro 	error = dir->i_op->create(dir, dentry, mode, want_excl);
2517a74574aaSStephen Smalley 	if (!error)
2518f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
25191da177e4SLinus Torvalds 	return error;
25201da177e4SLinus Torvalds }
25214d359507SAl Viro EXPORT_SYMBOL(vfs_create);
25221da177e4SLinus Torvalds 
252373d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
25241da177e4SLinus Torvalds {
25253fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
25261da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
25271da177e4SLinus Torvalds 	int error;
25281da177e4SLinus Torvalds 
2529bcda7652SAl Viro 	/* O_PATH? */
2530bcda7652SAl Viro 	if (!acc_mode)
2531bcda7652SAl Viro 		return 0;
2532bcda7652SAl Viro 
25331da177e4SLinus Torvalds 	if (!inode)
25341da177e4SLinus Torvalds 		return -ENOENT;
25351da177e4SLinus Torvalds 
2536c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2537c8fe8f30SChristoph Hellwig 	case S_IFLNK:
25381da177e4SLinus Torvalds 		return -ELOOP;
2539c8fe8f30SChristoph Hellwig 	case S_IFDIR:
2540c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
25411da177e4SLinus Torvalds 			return -EISDIR;
2542c8fe8f30SChristoph Hellwig 		break;
2543c8fe8f30SChristoph Hellwig 	case S_IFBLK:
2544c8fe8f30SChristoph Hellwig 	case S_IFCHR:
25453fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
25461da177e4SLinus Torvalds 			return -EACCES;
2547c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
2548c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2549c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
25501da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2551c8fe8f30SChristoph Hellwig 		break;
25524a3fd211SDave Hansen 	}
2553b41572e9SDave Hansen 
25543fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
2555b41572e9SDave Hansen 	if (error)
2556b41572e9SDave Hansen 		return error;
25576146f0d5SMimi Zohar 
25581da177e4SLinus Torvalds 	/*
25591da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
25601da177e4SLinus Torvalds 	 */
25611da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
25628737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
25637715b521SAl Viro 			return -EPERM;
25641da177e4SLinus Torvalds 		if (flag & O_TRUNC)
25657715b521SAl Viro 			return -EPERM;
25661da177e4SLinus Torvalds 	}
25671da177e4SLinus Torvalds 
25681da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
25692e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
25707715b521SAl Viro 		return -EPERM;
25711da177e4SLinus Torvalds 
2572f3c7691eSJ. Bruce Fields 	return 0;
25737715b521SAl Viro }
25747715b521SAl Viro 
2575e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
25767715b521SAl Viro {
2577e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
25787715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
25797715b521SAl Viro 	int error = get_write_access(inode);
25801da177e4SLinus Torvalds 	if (error)
25817715b521SAl Viro 		return error;
25821da177e4SLinus Torvalds 	/*
25831da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
25841da177e4SLinus Torvalds 	 */
2585d7a06983SJeff Layton 	error = locks_verify_locked(filp);
2586be6d3e56SKentaro Takeda 	if (!error)
2587ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
25881da177e4SLinus Torvalds 	if (!error) {
25897715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2590d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2591e1181ee6SJeff Layton 				    filp);
25921da177e4SLinus Torvalds 	}
25931da177e4SLinus Torvalds 	put_write_access(inode);
2594acd0c935SMimi Zohar 	return error;
25951da177e4SLinus Torvalds }
25961da177e4SLinus Torvalds 
2597d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2598d57999e1SDave Hansen {
25998a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
26008a5e929dSAl Viro 		flag--;
2601d57999e1SDave Hansen 	return flag;
2602d57999e1SDave Hansen }
2603d57999e1SDave Hansen 
2604d18e9008SMiklos Szeredi static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2605d18e9008SMiklos Szeredi {
2606d18e9008SMiklos Szeredi 	int error = security_path_mknod(dir, dentry, mode, 0);
2607d18e9008SMiklos Szeredi 	if (error)
2608d18e9008SMiklos Szeredi 		return error;
2609d18e9008SMiklos Szeredi 
2610d18e9008SMiklos Szeredi 	error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2611d18e9008SMiklos Szeredi 	if (error)
2612d18e9008SMiklos Szeredi 		return error;
2613d18e9008SMiklos Szeredi 
2614d18e9008SMiklos Szeredi 	return security_inode_create(dir->dentry->d_inode, dentry, mode);
2615d18e9008SMiklos Szeredi }
2616d18e9008SMiklos Szeredi 
26171acf0af9SDavid Howells /*
26181acf0af9SDavid Howells  * Attempt to atomically look up, create and open a file from a negative
26191acf0af9SDavid Howells  * dentry.
26201acf0af9SDavid Howells  *
26211acf0af9SDavid Howells  * Returns 0 if successful.  The file will have been created and attached to
26221acf0af9SDavid Howells  * @file by the filesystem calling finish_open().
26231acf0af9SDavid Howells  *
26241acf0af9SDavid Howells  * Returns 1 if the file was looked up only or didn't need creating.  The
26251acf0af9SDavid Howells  * caller will need to perform the open themselves.  @path will have been
26261acf0af9SDavid Howells  * updated to point to the new dentry.  This may be negative.
26271acf0af9SDavid Howells  *
26281acf0af9SDavid Howells  * Returns an error code otherwise.
26291acf0af9SDavid Howells  */
26302675a4ebSAl Viro static int atomic_open(struct nameidata *nd, struct dentry *dentry,
263130d90494SAl Viro 			struct path *path, struct file *file,
2632015c3bbcSMiklos Szeredi 			const struct open_flags *op,
263364894cf8SAl Viro 			bool got_write, bool need_lookup,
263447237687SAl Viro 			int *opened)
2635d18e9008SMiklos Szeredi {
2636d18e9008SMiklos Szeredi 	struct inode *dir =  nd->path.dentry->d_inode;
2637d18e9008SMiklos Szeredi 	unsigned open_flag = open_to_namei_flags(op->open_flag);
2638d18e9008SMiklos Szeredi 	umode_t mode;
2639d18e9008SMiklos Szeredi 	int error;
2640d18e9008SMiklos Szeredi 	int acc_mode;
2641d18e9008SMiklos Szeredi 	int create_error = 0;
2642d18e9008SMiklos Szeredi 	struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2643116cc022SMiklos Szeredi 	bool excl;
2644d18e9008SMiklos Szeredi 
2645d18e9008SMiklos Szeredi 	BUG_ON(dentry->d_inode);
2646d18e9008SMiklos Szeredi 
2647d18e9008SMiklos Szeredi 	/* Don't create child dentry for a dead directory. */
2648d18e9008SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
26492675a4ebSAl Viro 		error = -ENOENT;
2650d18e9008SMiklos Szeredi 		goto out;
2651d18e9008SMiklos Szeredi 	}
2652d18e9008SMiklos Szeredi 
265362b259d8SMiklos Szeredi 	mode = op->mode;
2654d18e9008SMiklos Szeredi 	if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2655d18e9008SMiklos Szeredi 		mode &= ~current_umask();
2656d18e9008SMiklos Szeredi 
2657116cc022SMiklos Szeredi 	excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
2658116cc022SMiklos Szeredi 	if (excl)
2659d18e9008SMiklos Szeredi 		open_flag &= ~O_TRUNC;
2660d18e9008SMiklos Szeredi 
2661d18e9008SMiklos Szeredi 	/*
2662d18e9008SMiklos Szeredi 	 * Checking write permission is tricky, bacuse we don't know if we are
2663d18e9008SMiklos Szeredi 	 * going to actually need it: O_CREAT opens should work as long as the
2664d18e9008SMiklos Szeredi 	 * file exists.  But checking existence breaks atomicity.  The trick is
2665d18e9008SMiklos Szeredi 	 * to check access and if not granted clear O_CREAT from the flags.
2666d18e9008SMiklos Szeredi 	 *
2667d18e9008SMiklos Szeredi 	 * Another problem is returing the "right" error value (e.g. for an
2668d18e9008SMiklos Szeredi 	 * O_EXCL open we want to return EEXIST not EROFS).
2669d18e9008SMiklos Szeredi 	 */
267064894cf8SAl Viro 	if (((open_flag & (O_CREAT | O_TRUNC)) ||
267164894cf8SAl Viro 	    (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
267264894cf8SAl Viro 		if (!(open_flag & O_CREAT)) {
2673d18e9008SMiklos Szeredi 			/*
2674d18e9008SMiklos Szeredi 			 * No O_CREATE -> atomicity not a requirement -> fall
2675d18e9008SMiklos Szeredi 			 * back to lookup + open
2676d18e9008SMiklos Szeredi 			 */
2677d18e9008SMiklos Szeredi 			goto no_open;
2678d18e9008SMiklos Szeredi 		} else if (open_flag & (O_EXCL | O_TRUNC)) {
2679d18e9008SMiklos Szeredi 			/* Fall back and fail with the right error */
268064894cf8SAl Viro 			create_error = -EROFS;
2681d18e9008SMiklos Szeredi 			goto no_open;
2682d18e9008SMiklos Szeredi 		} else {
2683d18e9008SMiklos Szeredi 			/* No side effects, safe to clear O_CREAT */
268464894cf8SAl Viro 			create_error = -EROFS;
2685d18e9008SMiklos Szeredi 			open_flag &= ~O_CREAT;
2686d18e9008SMiklos Szeredi 		}
2687d18e9008SMiklos Szeredi 	}
2688d18e9008SMiklos Szeredi 
2689d18e9008SMiklos Szeredi 	if (open_flag & O_CREAT) {
269038227f78SMiklos Szeredi 		error = may_o_create(&nd->path, dentry, mode);
2691d18e9008SMiklos Szeredi 		if (error) {
2692d18e9008SMiklos Szeredi 			create_error = error;
2693d18e9008SMiklos Szeredi 			if (open_flag & O_EXCL)
2694d18e9008SMiklos Szeredi 				goto no_open;
2695d18e9008SMiklos Szeredi 			open_flag &= ~O_CREAT;
2696d18e9008SMiklos Szeredi 		}
2697d18e9008SMiklos Szeredi 	}
2698d18e9008SMiklos Szeredi 
2699d18e9008SMiklos Szeredi 	if (nd->flags & LOOKUP_DIRECTORY)
2700d18e9008SMiklos Szeredi 		open_flag |= O_DIRECTORY;
2701d18e9008SMiklos Szeredi 
270230d90494SAl Viro 	file->f_path.dentry = DENTRY_NOT_SET;
270330d90494SAl Viro 	file->f_path.mnt = nd->path.mnt;
270430d90494SAl Viro 	error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
270547237687SAl Viro 				      opened);
2706d9585277SAl Viro 	if (error < 0) {
2707d9585277SAl Viro 		if (create_error && error == -ENOENT)
2708d9585277SAl Viro 			error = create_error;
2709d18e9008SMiklos Szeredi 		goto out;
2710d18e9008SMiklos Szeredi 	}
2711d18e9008SMiklos Szeredi 
2712d9585277SAl Viro 	if (error) {	/* returned 1, that is */
271330d90494SAl Viro 		if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
27142675a4ebSAl Viro 			error = -EIO;
2715d18e9008SMiklos Szeredi 			goto out;
2716d18e9008SMiklos Szeredi 		}
271730d90494SAl Viro 		if (file->f_path.dentry) {
2718d18e9008SMiklos Szeredi 			dput(dentry);
271930d90494SAl Viro 			dentry = file->f_path.dentry;
2720d18e9008SMiklos Szeredi 		}
272103da633aSAl Viro 		if (*opened & FILE_CREATED)
272203da633aSAl Viro 			fsnotify_create(dir, dentry);
272303da633aSAl Viro 		if (!dentry->d_inode) {
272403da633aSAl Viro 			WARN_ON(*opened & FILE_CREATED);
272503da633aSAl Viro 			if (create_error) {
272662b2ce96SSage Weil 				error = create_error;
272762b2ce96SSage Weil 				goto out;
272862b2ce96SSage Weil 			}
272903da633aSAl Viro 		} else {
273003da633aSAl Viro 			if (excl && !(*opened & FILE_CREATED)) {
273103da633aSAl Viro 				error = -EEXIST;
273203da633aSAl Viro 				goto out;
273303da633aSAl Viro 			}
273403da633aSAl Viro 		}
2735d18e9008SMiklos Szeredi 		goto looked_up;
2736d18e9008SMiklos Szeredi 	}
2737d18e9008SMiklos Szeredi 
2738d18e9008SMiklos Szeredi 	/*
2739d18e9008SMiklos Szeredi 	 * We didn't have the inode before the open, so check open permission
2740d18e9008SMiklos Szeredi 	 * here.
2741d18e9008SMiklos Szeredi 	 */
274203da633aSAl Viro 	acc_mode = op->acc_mode;
274303da633aSAl Viro 	if (*opened & FILE_CREATED) {
274403da633aSAl Viro 		WARN_ON(!(open_flag & O_CREAT));
274503da633aSAl Viro 		fsnotify_create(dir, dentry);
274603da633aSAl Viro 		acc_mode = MAY_OPEN;
274703da633aSAl Viro 	}
27482675a4ebSAl Viro 	error = may_open(&file->f_path, acc_mode, open_flag);
27492675a4ebSAl Viro 	if (error)
27502675a4ebSAl Viro 		fput(file);
2751d18e9008SMiklos Szeredi 
2752d18e9008SMiklos Szeredi out:
2753d18e9008SMiklos Szeredi 	dput(dentry);
27542675a4ebSAl Viro 	return error;
2755d18e9008SMiklos Szeredi 
2756d18e9008SMiklos Szeredi no_open:
2757d18e9008SMiklos Szeredi 	if (need_lookup) {
275872bd866aSAl Viro 		dentry = lookup_real(dir, dentry, nd->flags);
2759d18e9008SMiklos Szeredi 		if (IS_ERR(dentry))
27602675a4ebSAl Viro 			return PTR_ERR(dentry);
2761d18e9008SMiklos Szeredi 
2762d18e9008SMiklos Szeredi 		if (create_error) {
2763d18e9008SMiklos Szeredi 			int open_flag = op->open_flag;
2764d18e9008SMiklos Szeredi 
27652675a4ebSAl Viro 			error = create_error;
2766d18e9008SMiklos Szeredi 			if ((open_flag & O_EXCL)) {
2767d18e9008SMiklos Szeredi 				if (!dentry->d_inode)
2768d18e9008SMiklos Szeredi 					goto out;
2769d18e9008SMiklos Szeredi 			} else if (!dentry->d_inode) {
2770d18e9008SMiklos Szeredi 				goto out;
2771d18e9008SMiklos Szeredi 			} else if ((open_flag & O_TRUNC) &&
2772d18e9008SMiklos Szeredi 				   S_ISREG(dentry->d_inode->i_mode)) {
2773d18e9008SMiklos Szeredi 				goto out;
2774d18e9008SMiklos Szeredi 			}
2775d18e9008SMiklos Szeredi 			/* will fail later, go on to get the right error */
2776d18e9008SMiklos Szeredi 		}
2777d18e9008SMiklos Szeredi 	}
2778d18e9008SMiklos Szeredi looked_up:
2779d18e9008SMiklos Szeredi 	path->dentry = dentry;
2780d18e9008SMiklos Szeredi 	path->mnt = nd->path.mnt;
27812675a4ebSAl Viro 	return 1;
2782d18e9008SMiklos Szeredi }
2783d18e9008SMiklos Szeredi 
278431e6b01fSNick Piggin /*
27851acf0af9SDavid Howells  * Look up and maybe create and open the last component.
2786d58ffd35SMiklos Szeredi  *
2787d58ffd35SMiklos Szeredi  * Must be called with i_mutex held on parent.
2788d58ffd35SMiklos Szeredi  *
27891acf0af9SDavid Howells  * Returns 0 if the file was successfully atomically created (if necessary) and
27901acf0af9SDavid Howells  * opened.  In this case the file will be returned attached to @file.
27911acf0af9SDavid Howells  *
27921acf0af9SDavid Howells  * Returns 1 if the file was not completely opened at this time, though lookups
27931acf0af9SDavid Howells  * and creations will have been performed and the dentry returned in @path will
27941acf0af9SDavid Howells  * be positive upon return if O_CREAT was specified.  If O_CREAT wasn't
27951acf0af9SDavid Howells  * specified then a negative dentry may be returned.
27961acf0af9SDavid Howells  *
27971acf0af9SDavid Howells  * An error code is returned otherwise.
27981acf0af9SDavid Howells  *
27991acf0af9SDavid Howells  * FILE_CREATE will be set in @*opened if the dentry was created and will be
28001acf0af9SDavid Howells  * cleared otherwise prior to returning.
2801d58ffd35SMiklos Szeredi  */
28022675a4ebSAl Viro static int lookup_open(struct nameidata *nd, struct path *path,
280330d90494SAl Viro 			struct file *file,
2804d58ffd35SMiklos Szeredi 			const struct open_flags *op,
280564894cf8SAl Viro 			bool got_write, int *opened)
2806d58ffd35SMiklos Szeredi {
2807d58ffd35SMiklos Szeredi 	struct dentry *dir = nd->path.dentry;
280854ef4872SMiklos Szeredi 	struct inode *dir_inode = dir->d_inode;
2809d58ffd35SMiklos Szeredi 	struct dentry *dentry;
2810d58ffd35SMiklos Szeredi 	int error;
281154ef4872SMiklos Szeredi 	bool need_lookup;
2812d58ffd35SMiklos Szeredi 
281347237687SAl Viro 	*opened &= ~FILE_CREATED;
2814201f956eSAl Viro 	dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
2815d58ffd35SMiklos Szeredi 	if (IS_ERR(dentry))
28162675a4ebSAl Viro 		return PTR_ERR(dentry);
2817d58ffd35SMiklos Szeredi 
2818d18e9008SMiklos Szeredi 	/* Cached positive dentry: will open in f_op->open */
2819d18e9008SMiklos Szeredi 	if (!need_lookup && dentry->d_inode)
2820d18e9008SMiklos Szeredi 		goto out_no_open;
2821d18e9008SMiklos Szeredi 
2822d18e9008SMiklos Szeredi 	if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
282364894cf8SAl Viro 		return atomic_open(nd, dentry, path, file, op, got_write,
282447237687SAl Viro 				   need_lookup, opened);
2825d18e9008SMiklos Szeredi 	}
2826d18e9008SMiklos Szeredi 
282754ef4872SMiklos Szeredi 	if (need_lookup) {
282854ef4872SMiklos Szeredi 		BUG_ON(dentry->d_inode);
282954ef4872SMiklos Szeredi 
283072bd866aSAl Viro 		dentry = lookup_real(dir_inode, dentry, nd->flags);
283154ef4872SMiklos Szeredi 		if (IS_ERR(dentry))
28322675a4ebSAl Viro 			return PTR_ERR(dentry);
283354ef4872SMiklos Szeredi 	}
283454ef4872SMiklos Szeredi 
2835d58ffd35SMiklos Szeredi 	/* Negative dentry, just create the file */
2836d58ffd35SMiklos Szeredi 	if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2837d58ffd35SMiklos Szeredi 		umode_t mode = op->mode;
2838d58ffd35SMiklos Szeredi 		if (!IS_POSIXACL(dir->d_inode))
2839d58ffd35SMiklos Szeredi 			mode &= ~current_umask();
2840d58ffd35SMiklos Szeredi 		/*
2841d58ffd35SMiklos Szeredi 		 * This write is needed to ensure that a
2842d58ffd35SMiklos Szeredi 		 * rw->ro transition does not occur between
2843d58ffd35SMiklos Szeredi 		 * the time when the file is created and when
2844d58ffd35SMiklos Szeredi 		 * a permanent write count is taken through
2845015c3bbcSMiklos Szeredi 		 * the 'struct file' in finish_open().
2846d58ffd35SMiklos Szeredi 		 */
284764894cf8SAl Viro 		if (!got_write) {
284864894cf8SAl Viro 			error = -EROFS;
2849d58ffd35SMiklos Szeredi 			goto out_dput;
285064894cf8SAl Viro 		}
285147237687SAl Viro 		*opened |= FILE_CREATED;
2852d58ffd35SMiklos Szeredi 		error = security_path_mknod(&nd->path, dentry, mode, 0);
2853d58ffd35SMiklos Szeredi 		if (error)
2854d58ffd35SMiklos Szeredi 			goto out_dput;
2855312b63fbSAl Viro 		error = vfs_create(dir->d_inode, dentry, mode,
2856312b63fbSAl Viro 				   nd->flags & LOOKUP_EXCL);
2857d58ffd35SMiklos Szeredi 		if (error)
2858d58ffd35SMiklos Szeredi 			goto out_dput;
2859d58ffd35SMiklos Szeredi 	}
2860d18e9008SMiklos Szeredi out_no_open:
2861d58ffd35SMiklos Szeredi 	path->dentry = dentry;
2862d58ffd35SMiklos Szeredi 	path->mnt = nd->path.mnt;
28632675a4ebSAl Viro 	return 1;
2864d58ffd35SMiklos Szeredi 
2865d58ffd35SMiklos Szeredi out_dput:
2866d58ffd35SMiklos Szeredi 	dput(dentry);
28672675a4ebSAl Viro 	return error;
2868d58ffd35SMiklos Szeredi }
2869d58ffd35SMiklos Szeredi 
2870d58ffd35SMiklos Szeredi /*
2871fe2d35ffSAl Viro  * Handle the last step of open()
287231e6b01fSNick Piggin  */
28732675a4ebSAl Viro static int do_last(struct nameidata *nd, struct path *path,
287430d90494SAl Viro 		   struct file *file, const struct open_flags *op,
2875669abf4eSJeff Layton 		   int *opened, struct filename *name)
2876fb1cc555SAl Viro {
2877a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
2878ca344a89SAl Viro 	int open_flag = op->open_flag;
287977d660a8SMiklos Szeredi 	bool will_truncate = (open_flag & O_TRUNC) != 0;
288064894cf8SAl Viro 	bool got_write = false;
2881bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2882a1eb3315SMiklos Szeredi 	struct inode *inode;
288377d660a8SMiklos Szeredi 	bool symlink_ok = false;
288416b1c1cdSMiklos Szeredi 	struct path save_parent = { .dentry = NULL, .mnt = NULL };
288516b1c1cdSMiklos Szeredi 	bool retried = false;
288616c2cd71SAl Viro 	int error;
2887fb1cc555SAl Viro 
2888c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2889c3e380b0SAl Viro 	nd->flags |= op->intent;
2890c3e380b0SAl Viro 
2891bc77daa7SAl Viro 	if (nd->last_type != LAST_NORM) {
2892fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2893fe2d35ffSAl Viro 		if (error)
28942675a4ebSAl Viro 			return error;
2895e83db167SMiklos Szeredi 		goto finish_open;
28961f36f774SAl Viro 	}
2897a2c36b45SAl Viro 
2898ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2899fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2900fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2901bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
290277d660a8SMiklos Szeredi 			symlink_ok = true;
2903fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2904e97cdc87SAl Viro 		error = lookup_fast(nd, path, &inode);
290571574865SMiklos Szeredi 		if (likely(!error))
290671574865SMiklos Szeredi 			goto finish_lookup;
290771574865SMiklos Szeredi 
2908ce57dfc1SAl Viro 		if (error < 0)
29092675a4ebSAl Viro 			goto out;
2910a1eb3315SMiklos Szeredi 
291137d7fffcSMiklos Szeredi 		BUG_ON(nd->inode != dir->d_inode);
2912b6183df7SMiklos Szeredi 	} else {
2913fe2d35ffSAl Viro 		/* create side of things */
2914a3fbbde7SAl Viro 		/*
2915b6183df7SMiklos Szeredi 		 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2916b6183df7SMiklos Szeredi 		 * has been cleared when we got to the last component we are
2917b6183df7SMiklos Szeredi 		 * about to look up
2918a3fbbde7SAl Viro 		 */
29199f1fafeeSAl Viro 		error = complete_walk(nd);
29209f1fafeeSAl Viro 		if (error)
29212675a4ebSAl Viro 			return error;
2922fe2d35ffSAl Viro 
292333e2208aSJeff Layton 		audit_inode(name, dir, LOOKUP_PARENT);
292416c2cd71SAl Viro 		error = -EISDIR;
29251f36f774SAl Viro 		/* trailing slashes? */
292631e6b01fSNick Piggin 		if (nd->last.name[nd->last.len])
29272675a4ebSAl Viro 			goto out;
2928b6183df7SMiklos Szeredi 	}
29291f36f774SAl Viro 
293016b1c1cdSMiklos Szeredi retry_lookup:
293164894cf8SAl Viro 	if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
293264894cf8SAl Viro 		error = mnt_want_write(nd->path.mnt);
293364894cf8SAl Viro 		if (!error)
293464894cf8SAl Viro 			got_write = true;
293564894cf8SAl Viro 		/*
293664894cf8SAl Viro 		 * do _not_ fail yet - we might not need that or fail with
293764894cf8SAl Viro 		 * a different error; let lookup_open() decide; we'll be
293864894cf8SAl Viro 		 * dropping this one anyway.
293964894cf8SAl Viro 		 */
294064894cf8SAl Viro 	}
2941a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
294264894cf8SAl Viro 	error = lookup_open(nd, path, file, op, got_write, opened);
2943fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2944fb1cc555SAl Viro 
29452675a4ebSAl Viro 	if (error <= 0) {
29462675a4ebSAl Viro 		if (error)
2947d58ffd35SMiklos Szeredi 			goto out;
29486c0d46c4SAl Viro 
294947237687SAl Viro 		if ((*opened & FILE_CREATED) ||
2950496ad9aaSAl Viro 		    !S_ISREG(file_inode(file)->i_mode))
295177d660a8SMiklos Szeredi 			will_truncate = false;
2952d18e9008SMiklos Szeredi 
2953adb5c247SJeff Layton 		audit_inode(name, file->f_path.dentry, 0);
2954d18e9008SMiklos Szeredi 		goto opened;
2955d18e9008SMiklos Szeredi 	}
2956d18e9008SMiklos Szeredi 
295747237687SAl Viro 	if (*opened & FILE_CREATED) {
29589b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
2959ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
296077d660a8SMiklos Szeredi 		will_truncate = false;
2961bcda7652SAl Viro 		acc_mode = MAY_OPEN;
2962d58ffd35SMiklos Szeredi 		path_to_nameidata(path, nd);
2963e83db167SMiklos Szeredi 		goto finish_open_created;
2964fb1cc555SAl Viro 	}
2965fb1cc555SAl Viro 
2966fb1cc555SAl Viro 	/*
29673134f37eSJeff Layton 	 * create/update audit record if it already exists.
2968fb1cc555SAl Viro 	 */
2969b18825a7SDavid Howells 	if (d_is_positive(path->dentry))
2970adb5c247SJeff Layton 		audit_inode(name, path->dentry, 0);
2971fb1cc555SAl Viro 
2972d18e9008SMiklos Szeredi 	/*
2973d18e9008SMiklos Szeredi 	 * If atomic_open() acquired write access it is dropped now due to
2974d18e9008SMiklos Szeredi 	 * possible mount and symlink following (this might be optimized away if
2975d18e9008SMiklos Szeredi 	 * necessary...)
2976d18e9008SMiklos Szeredi 	 */
297764894cf8SAl Viro 	if (got_write) {
2978d18e9008SMiklos Szeredi 		mnt_drop_write(nd->path.mnt);
297964894cf8SAl Viro 		got_write = false;
2980d18e9008SMiklos Szeredi 	}
2981d18e9008SMiklos Szeredi 
2982fb1cc555SAl Viro 	error = -EEXIST;
2983f8310c59SAl Viro 	if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
2984fb1cc555SAl Viro 		goto exit_dput;
2985fb1cc555SAl Viro 
29869875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
29879875cf80SDavid Howells 	if (error < 0)
2988fb1cc555SAl Viro 		goto exit_dput;
2989fb1cc555SAl Viro 
2990a3fbbde7SAl Viro 	if (error)
2991a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
2992a3fbbde7SAl Viro 
2993decf3400SMiklos Szeredi 	BUG_ON(nd->flags & LOOKUP_RCU);
2994decf3400SMiklos Szeredi 	inode = path->dentry->d_inode;
29955f5daac1SMiklos Szeredi finish_lookup:
29965f5daac1SMiklos Szeredi 	/* we _can_ be in RCU mode here */
2997fb1cc555SAl Viro 	error = -ENOENT;
299822213318SAl Viro 	if (!inode || d_is_negative(path->dentry)) {
299954c33e7fSMiklos Szeredi 		path_to_nameidata(path, nd);
30002675a4ebSAl Viro 		goto out;
300154c33e7fSMiklos Szeredi 	}
30029e67f361SAl Viro 
3003b18825a7SDavid Howells 	if (should_follow_link(path->dentry, !symlink_ok)) {
3004d45ea867SMiklos Szeredi 		if (nd->flags & LOOKUP_RCU) {
3005d45ea867SMiklos Szeredi 			if (unlikely(unlazy_walk(nd, path->dentry))) {
3006d45ea867SMiklos Szeredi 				error = -ECHILD;
30072675a4ebSAl Viro 				goto out;
3008d45ea867SMiklos Szeredi 			}
3009d45ea867SMiklos Szeredi 		}
3010d45ea867SMiklos Szeredi 		BUG_ON(inode != path->dentry->d_inode);
30112675a4ebSAl Viro 		return 1;
3012d45ea867SMiklos Szeredi 	}
3013fb1cc555SAl Viro 
301416b1c1cdSMiklos Szeredi 	if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
3015fb1cc555SAl Viro 		path_to_nameidata(path, nd);
301616b1c1cdSMiklos Szeredi 	} else {
301716b1c1cdSMiklos Szeredi 		save_parent.dentry = nd->path.dentry;
301816b1c1cdSMiklos Szeredi 		save_parent.mnt = mntget(path->mnt);
301916b1c1cdSMiklos Szeredi 		nd->path.dentry = path->dentry;
302016b1c1cdSMiklos Szeredi 
302116b1c1cdSMiklos Szeredi 	}
3022decf3400SMiklos Szeredi 	nd->inode = inode;
3023a3fbbde7SAl Viro 	/* Why this, you ask?  _Now_ we might have grown LOOKUP_JUMPED... */
3024bc77daa7SAl Viro finish_open:
3025a3fbbde7SAl Viro 	error = complete_walk(nd);
302616b1c1cdSMiklos Szeredi 	if (error) {
302716b1c1cdSMiklos Szeredi 		path_put(&save_parent);
30282675a4ebSAl Viro 		return error;
302916b1c1cdSMiklos Szeredi 	}
3030bc77daa7SAl Viro 	audit_inode(name, nd->path.dentry, 0);
3031fb1cc555SAl Viro 	error = -EISDIR;
303244b1d530SMiklos Szeredi 	if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
30332675a4ebSAl Viro 		goto out;
3034af2f5542SMiklos Szeredi 	error = -ENOTDIR;
303544b1d530SMiklos Szeredi 	if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
30362675a4ebSAl Viro 		goto out;
30376c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
303877d660a8SMiklos Szeredi 		will_truncate = false;
30396c0d46c4SAl Viro 
30400f9d1a10SAl Viro 	if (will_truncate) {
30410f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
30420f9d1a10SAl Viro 		if (error)
30432675a4ebSAl Viro 			goto out;
304464894cf8SAl Viro 		got_write = true;
30450f9d1a10SAl Viro 	}
3046e83db167SMiklos Szeredi finish_open_created:
3047bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
3048ca344a89SAl Viro 	if (error)
30492675a4ebSAl Viro 		goto out;
305030d90494SAl Viro 	file->f_path.mnt = nd->path.mnt;
305130d90494SAl Viro 	error = finish_open(file, nd->path.dentry, NULL, opened);
305230d90494SAl Viro 	if (error) {
305330d90494SAl Viro 		if (error == -EOPENSTALE)
3054f60dc3dbSMiklos Szeredi 			goto stale_open;
3055015c3bbcSMiklos Szeredi 		goto out;
3056f60dc3dbSMiklos Szeredi 	}
3057a8277b9bSMiklos Szeredi opened:
30582675a4ebSAl Viro 	error = open_check_o_direct(file);
3059015c3bbcSMiklos Szeredi 	if (error)
3060015c3bbcSMiklos Szeredi 		goto exit_fput;
30612675a4ebSAl Viro 	error = ima_file_check(file, op->acc_mode);
3062aa4caadbSMiklos Szeredi 	if (error)
3063aa4caadbSMiklos Szeredi 		goto exit_fput;
3064aa4caadbSMiklos Szeredi 
30650f9d1a10SAl Viro 	if (will_truncate) {
30662675a4ebSAl Viro 		error = handle_truncate(file);
3067aa4caadbSMiklos Szeredi 		if (error)
3068aa4caadbSMiklos Szeredi 			goto exit_fput;
30690f9d1a10SAl Viro 	}
3070ca344a89SAl Viro out:
307164894cf8SAl Viro 	if (got_write)
30720f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
307316b1c1cdSMiklos Szeredi 	path_put(&save_parent);
3074e276ae67SMiklos Szeredi 	terminate_walk(nd);
30752675a4ebSAl Viro 	return error;
3076fb1cc555SAl Viro 
3077fb1cc555SAl Viro exit_dput:
3078fb1cc555SAl Viro 	path_put_conditional(path, nd);
3079ca344a89SAl Viro 	goto out;
3080015c3bbcSMiklos Szeredi exit_fput:
30812675a4ebSAl Viro 	fput(file);
30822675a4ebSAl Viro 	goto out;
3083015c3bbcSMiklos Szeredi 
3084f60dc3dbSMiklos Szeredi stale_open:
3085f60dc3dbSMiklos Szeredi 	/* If no saved parent or already retried then can't retry */
3086f60dc3dbSMiklos Szeredi 	if (!save_parent.dentry || retried)
3087f60dc3dbSMiklos Szeredi 		goto out;
3088f60dc3dbSMiklos Szeredi 
3089f60dc3dbSMiklos Szeredi 	BUG_ON(save_parent.dentry != dir);
3090f60dc3dbSMiklos Szeredi 	path_put(&nd->path);
3091f60dc3dbSMiklos Szeredi 	nd->path = save_parent;
3092f60dc3dbSMiklos Szeredi 	nd->inode = dir->d_inode;
3093f60dc3dbSMiklos Szeredi 	save_parent.mnt = NULL;
3094f60dc3dbSMiklos Szeredi 	save_parent.dentry = NULL;
309564894cf8SAl Viro 	if (got_write) {
3096f60dc3dbSMiklos Szeredi 		mnt_drop_write(nd->path.mnt);
309764894cf8SAl Viro 		got_write = false;
3098f60dc3dbSMiklos Szeredi 	}
3099f60dc3dbSMiklos Szeredi 	retried = true;
3100f60dc3dbSMiklos Szeredi 	goto retry_lookup;
3101fb1cc555SAl Viro }
3102fb1cc555SAl Viro 
310360545d0dSAl Viro static int do_tmpfile(int dfd, struct filename *pathname,
310460545d0dSAl Viro 		struct nameidata *nd, int flags,
310560545d0dSAl Viro 		const struct open_flags *op,
310660545d0dSAl Viro 		struct file *file, int *opened)
310760545d0dSAl Viro {
310860545d0dSAl Viro 	static const struct qstr name = QSTR_INIT("/", 1);
310960545d0dSAl Viro 	struct dentry *dentry, *child;
311060545d0dSAl Viro 	struct inode *dir;
311160545d0dSAl Viro 	int error = path_lookupat(dfd, pathname->name,
311260545d0dSAl Viro 				  flags | LOOKUP_DIRECTORY, nd);
311360545d0dSAl Viro 	if (unlikely(error))
311460545d0dSAl Viro 		return error;
311560545d0dSAl Viro 	error = mnt_want_write(nd->path.mnt);
311660545d0dSAl Viro 	if (unlikely(error))
311760545d0dSAl Viro 		goto out;
311860545d0dSAl Viro 	/* we want directory to be writable */
311960545d0dSAl Viro 	error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC);
312060545d0dSAl Viro 	if (error)
312160545d0dSAl Viro 		goto out2;
312260545d0dSAl Viro 	dentry = nd->path.dentry;
312360545d0dSAl Viro 	dir = dentry->d_inode;
312460545d0dSAl Viro 	if (!dir->i_op->tmpfile) {
312560545d0dSAl Viro 		error = -EOPNOTSUPP;
312660545d0dSAl Viro 		goto out2;
312760545d0dSAl Viro 	}
312860545d0dSAl Viro 	child = d_alloc(dentry, &name);
312960545d0dSAl Viro 	if (unlikely(!child)) {
313060545d0dSAl Viro 		error = -ENOMEM;
313160545d0dSAl Viro 		goto out2;
313260545d0dSAl Viro 	}
313360545d0dSAl Viro 	nd->flags &= ~LOOKUP_DIRECTORY;
313460545d0dSAl Viro 	nd->flags |= op->intent;
313560545d0dSAl Viro 	dput(nd->path.dentry);
313660545d0dSAl Viro 	nd->path.dentry = child;
313760545d0dSAl Viro 	error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode);
313860545d0dSAl Viro 	if (error)
313960545d0dSAl Viro 		goto out2;
314060545d0dSAl Viro 	audit_inode(pathname, nd->path.dentry, 0);
314160545d0dSAl Viro 	error = may_open(&nd->path, op->acc_mode, op->open_flag);
314260545d0dSAl Viro 	if (error)
314360545d0dSAl Viro 		goto out2;
314460545d0dSAl Viro 	file->f_path.mnt = nd->path.mnt;
314560545d0dSAl Viro 	error = finish_open(file, nd->path.dentry, NULL, opened);
314660545d0dSAl Viro 	if (error)
314760545d0dSAl Viro 		goto out2;
314860545d0dSAl Viro 	error = open_check_o_direct(file);
3149f4e0c30cSAl Viro 	if (error) {
315060545d0dSAl Viro 		fput(file);
3151f4e0c30cSAl Viro 	} else if (!(op->open_flag & O_EXCL)) {
3152f4e0c30cSAl Viro 		struct inode *inode = file_inode(file);
3153f4e0c30cSAl Viro 		spin_lock(&inode->i_lock);
3154f4e0c30cSAl Viro 		inode->i_state |= I_LINKABLE;
3155f4e0c30cSAl Viro 		spin_unlock(&inode->i_lock);
3156f4e0c30cSAl Viro 	}
315760545d0dSAl Viro out2:
315860545d0dSAl Viro 	mnt_drop_write(nd->path.mnt);
315960545d0dSAl Viro out:
316060545d0dSAl Viro 	path_put(&nd->path);
316160545d0dSAl Viro 	return error;
316260545d0dSAl Viro }
316360545d0dSAl Viro 
3164669abf4eSJeff Layton static struct file *path_openat(int dfd, struct filename *pathname,
316573d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
31661da177e4SLinus Torvalds {
3167fe2d35ffSAl Viro 	struct file *base = NULL;
316830d90494SAl Viro 	struct file *file;
31699850c056SAl Viro 	struct path path;
317047237687SAl Viro 	int opened = 0;
317113aab428SAl Viro 	int error;
317231e6b01fSNick Piggin 
317330d90494SAl Viro 	file = get_empty_filp();
31741afc99beSAl Viro 	if (IS_ERR(file))
31751afc99beSAl Viro 		return file;
317631e6b01fSNick Piggin 
317730d90494SAl Viro 	file->f_flags = op->open_flag;
317831e6b01fSNick Piggin 
3179bb458c64SAl Viro 	if (unlikely(file->f_flags & __O_TMPFILE)) {
318060545d0dSAl Viro 		error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened);
318160545d0dSAl Viro 		goto out;
318260545d0dSAl Viro 	}
318360545d0dSAl Viro 
3184669abf4eSJeff Layton 	error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
318531e6b01fSNick Piggin 	if (unlikely(error))
31862675a4ebSAl Viro 		goto out;
318731e6b01fSNick Piggin 
3188fe2d35ffSAl Viro 	current->total_link_count = 0;
3189669abf4eSJeff Layton 	error = link_path_walk(pathname->name, nd);
319031e6b01fSNick Piggin 	if (unlikely(error))
31912675a4ebSAl Viro 		goto out;
31921da177e4SLinus Torvalds 
31932675a4ebSAl Viro 	error = do_last(nd, &path, file, op, &opened, pathname);
31942675a4ebSAl Viro 	while (unlikely(error > 0)) { /* trailing symlink */
31957b9337aaSNick Piggin 		struct path link = path;
3196def4af30SAl Viro 		void *cookie;
3197574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
319873d049a4SAl Viro 			path_put_conditional(&path, nd);
319973d049a4SAl Viro 			path_put(&nd->path);
32002675a4ebSAl Viro 			error = -ELOOP;
320140b39136SAl Viro 			break;
320240b39136SAl Viro 		}
3203800179c9SKees Cook 		error = may_follow_link(&link, nd);
3204800179c9SKees Cook 		if (unlikely(error))
3205800179c9SKees Cook 			break;
320673d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
320773d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
3208574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
3209c3e380b0SAl Viro 		if (unlikely(error))
32102675a4ebSAl Viro 			break;
32112675a4ebSAl Viro 		error = do_last(nd, &path, file, op, &opened, pathname);
3212574197e0SAl Viro 		put_link(nd, &link, cookie);
3213806b681cSAl Viro 	}
321410fa8e62SAl Viro out:
321573d049a4SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
321673d049a4SAl Viro 		path_put(&nd->root);
3217fe2d35ffSAl Viro 	if (base)
3218fe2d35ffSAl Viro 		fput(base);
32192675a4ebSAl Viro 	if (!(opened & FILE_OPENED)) {
32202675a4ebSAl Viro 		BUG_ON(!error);
322130d90494SAl Viro 		put_filp(file);
3222015c3bbcSMiklos Szeredi 	}
32232675a4ebSAl Viro 	if (unlikely(error)) {
32242675a4ebSAl Viro 		if (error == -EOPENSTALE) {
32252675a4ebSAl Viro 			if (flags & LOOKUP_RCU)
32262675a4ebSAl Viro 				error = -ECHILD;
32272675a4ebSAl Viro 			else
32282675a4ebSAl Viro 				error = -ESTALE;
32292675a4ebSAl Viro 		}
32302675a4ebSAl Viro 		file = ERR_PTR(error);
32312675a4ebSAl Viro 	}
32322675a4ebSAl Viro 	return file;
3233de459215SKirill Korotaev }
32341da177e4SLinus Torvalds 
3235669abf4eSJeff Layton struct file *do_filp_open(int dfd, struct filename *pathname,
3236f9652e10SAl Viro 		const struct open_flags *op)
323713aab428SAl Viro {
323873d049a4SAl Viro 	struct nameidata nd;
3239f9652e10SAl Viro 	int flags = op->lookup_flags;
324013aab428SAl Viro 	struct file *filp;
324113aab428SAl Viro 
324273d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
324313aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
324473d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
324513aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
324673d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
324713aab428SAl Viro 	return filp;
324813aab428SAl Viro }
324913aab428SAl Viro 
325073d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
3251f9652e10SAl Viro 		const char *name, const struct open_flags *op)
325273d049a4SAl Viro {
325373d049a4SAl Viro 	struct nameidata nd;
325473d049a4SAl Viro 	struct file *file;
3255669abf4eSJeff Layton 	struct filename filename = { .name = name };
3256f9652e10SAl Viro 	int flags = op->lookup_flags | LOOKUP_ROOT;
325773d049a4SAl Viro 
325873d049a4SAl Viro 	nd.root.mnt = mnt;
325973d049a4SAl Viro 	nd.root.dentry = dentry;
326073d049a4SAl Viro 
3261b18825a7SDavid Howells 	if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
326273d049a4SAl Viro 		return ERR_PTR(-ELOOP);
326373d049a4SAl Viro 
3264669abf4eSJeff Layton 	file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
326573d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
3266669abf4eSJeff Layton 		file = path_openat(-1, &filename, &nd, op, flags);
326773d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
3268669abf4eSJeff Layton 		file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
326973d049a4SAl Viro 	return file;
327073d049a4SAl Viro }
327173d049a4SAl Viro 
32721ac12b4bSJeff Layton struct dentry *kern_path_create(int dfd, const char *pathname,
32731ac12b4bSJeff Layton 				struct path *path, unsigned int lookup_flags)
32741da177e4SLinus Torvalds {
3275c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
3276ed75e95dSAl Viro 	struct nameidata nd;
3277c30dabfeSJan Kara 	int err2;
32781ac12b4bSJeff Layton 	int error;
32791ac12b4bSJeff Layton 	bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
32801ac12b4bSJeff Layton 
32811ac12b4bSJeff Layton 	/*
32821ac12b4bSJeff Layton 	 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
32831ac12b4bSJeff Layton 	 * other flags passed in are ignored!
32841ac12b4bSJeff Layton 	 */
32851ac12b4bSJeff Layton 	lookup_flags &= LOOKUP_REVAL;
32861ac12b4bSJeff Layton 
32871ac12b4bSJeff Layton 	error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd);
3288ed75e95dSAl Viro 	if (error)
3289ed75e95dSAl Viro 		return ERR_PTR(error);
32901da177e4SLinus Torvalds 
3291c663e5d8SChristoph Hellwig 	/*
3292c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
3293c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
3294c663e5d8SChristoph Hellwig 	 */
3295ed75e95dSAl Viro 	if (nd.last_type != LAST_NORM)
3296ed75e95dSAl Viro 		goto out;
3297ed75e95dSAl Viro 	nd.flags &= ~LOOKUP_PARENT;
3298ed75e95dSAl Viro 	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3299c663e5d8SChristoph Hellwig 
3300c30dabfeSJan Kara 	/* don't fail immediately if it's r/o, at least try to report other errors */
3301c30dabfeSJan Kara 	err2 = mnt_want_write(nd.path.mnt);
3302c663e5d8SChristoph Hellwig 	/*
3303c663e5d8SChristoph Hellwig 	 * Do the final lookup.
3304c663e5d8SChristoph Hellwig 	 */
3305ed75e95dSAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3306ed75e95dSAl Viro 	dentry = lookup_hash(&nd);
33071da177e4SLinus Torvalds 	if (IS_ERR(dentry))
3308a8104a9fSAl Viro 		goto unlock;
3309c663e5d8SChristoph Hellwig 
3310a8104a9fSAl Viro 	error = -EEXIST;
3311b18825a7SDavid Howells 	if (d_is_positive(dentry))
3312a8104a9fSAl Viro 		goto fail;
3313b18825a7SDavid Howells 
3314c663e5d8SChristoph Hellwig 	/*
3315c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
3316c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
3317c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
3318c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
3319c663e5d8SChristoph Hellwig 	 */
3320ed75e95dSAl Viro 	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
3321a8104a9fSAl Viro 		error = -ENOENT;
3322ed75e95dSAl Viro 		goto fail;
3323e9baf6e5SAl Viro 	}
3324c30dabfeSJan Kara 	if (unlikely(err2)) {
3325c30dabfeSJan Kara 		error = err2;
3326a8104a9fSAl Viro 		goto fail;
3327c30dabfeSJan Kara 	}
3328ed75e95dSAl Viro 	*path = nd.path;
3329e9baf6e5SAl Viro 	return dentry;
33301da177e4SLinus Torvalds fail:
3331a8104a9fSAl Viro 	dput(dentry);
3332a8104a9fSAl Viro 	dentry = ERR_PTR(error);
3333a8104a9fSAl Viro unlock:
3334dae6ad8fSAl Viro 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3335c30dabfeSJan Kara 	if (!err2)
3336c30dabfeSJan Kara 		mnt_drop_write(nd.path.mnt);
3337ed75e95dSAl Viro out:
3338dae6ad8fSAl Viro 	path_put(&nd.path);
3339ed75e95dSAl Viro 	return dentry;
3340dae6ad8fSAl Viro }
3341dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create);
3342dae6ad8fSAl Viro 
3343921a1650SAl Viro void done_path_create(struct path *path, struct dentry *dentry)
3344921a1650SAl Viro {
3345921a1650SAl Viro 	dput(dentry);
3346921a1650SAl Viro 	mutex_unlock(&path->dentry->d_inode->i_mutex);
3347a8104a9fSAl Viro 	mnt_drop_write(path->mnt);
3348921a1650SAl Viro 	path_put(path);
3349921a1650SAl Viro }
3350921a1650SAl Viro EXPORT_SYMBOL(done_path_create);
3351921a1650SAl Viro 
33521ac12b4bSJeff Layton struct dentry *user_path_create(int dfd, const char __user *pathname,
33531ac12b4bSJeff Layton 				struct path *path, unsigned int lookup_flags)
3354dae6ad8fSAl Viro {
335591a27b2aSJeff Layton 	struct filename *tmp = getname(pathname);
3356dae6ad8fSAl Viro 	struct dentry *res;
3357dae6ad8fSAl Viro 	if (IS_ERR(tmp))
3358dae6ad8fSAl Viro 		return ERR_CAST(tmp);
33591ac12b4bSJeff Layton 	res = kern_path_create(dfd, tmp->name, path, lookup_flags);
3360dae6ad8fSAl Viro 	putname(tmp);
3361dae6ad8fSAl Viro 	return res;
3362dae6ad8fSAl Viro }
3363dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create);
3364dae6ad8fSAl Viro 
33651a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
33661da177e4SLinus Torvalds {
3367a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
33681da177e4SLinus Torvalds 
33691da177e4SLinus Torvalds 	if (error)
33701da177e4SLinus Torvalds 		return error;
33711da177e4SLinus Torvalds 
3372975d6b39SEric W. Biederman 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
33731da177e4SLinus Torvalds 		return -EPERM;
33741da177e4SLinus Torvalds 
3375acfa4380SAl Viro 	if (!dir->i_op->mknod)
33761da177e4SLinus Torvalds 		return -EPERM;
33771da177e4SLinus Torvalds 
337808ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
337908ce5f16SSerge E. Hallyn 	if (error)
338008ce5f16SSerge E. Hallyn 		return error;
338108ce5f16SSerge E. Hallyn 
33821da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
33831da177e4SLinus Torvalds 	if (error)
33841da177e4SLinus Torvalds 		return error;
33851da177e4SLinus Torvalds 
33861da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
3387a74574aaSStephen Smalley 	if (!error)
3388f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
33891da177e4SLinus Torvalds 	return error;
33901da177e4SLinus Torvalds }
33914d359507SAl Viro EXPORT_SYMBOL(vfs_mknod);
33921da177e4SLinus Torvalds 
3393f69aac00SAl Viro static int may_mknod(umode_t mode)
3394463c3197SDave Hansen {
3395463c3197SDave Hansen 	switch (mode & S_IFMT) {
3396463c3197SDave Hansen 	case S_IFREG:
3397463c3197SDave Hansen 	case S_IFCHR:
3398463c3197SDave Hansen 	case S_IFBLK:
3399463c3197SDave Hansen 	case S_IFIFO:
3400463c3197SDave Hansen 	case S_IFSOCK:
3401463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
3402463c3197SDave Hansen 		return 0;
3403463c3197SDave Hansen 	case S_IFDIR:
3404463c3197SDave Hansen 		return -EPERM;
3405463c3197SDave Hansen 	default:
3406463c3197SDave Hansen 		return -EINVAL;
3407463c3197SDave Hansen 	}
3408463c3197SDave Hansen }
3409463c3197SDave Hansen 
34108208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
34112e4d0924SHeiko Carstens 		unsigned, dev)
34121da177e4SLinus Torvalds {
34131da177e4SLinus Torvalds 	struct dentry *dentry;
3414dae6ad8fSAl Viro 	struct path path;
3415dae6ad8fSAl Viro 	int error;
3416972567f1SJeff Layton 	unsigned int lookup_flags = 0;
34171da177e4SLinus Torvalds 
34188e4bfca1SAl Viro 	error = may_mknod(mode);
34198e4bfca1SAl Viro 	if (error)
34208e4bfca1SAl Viro 		return error;
3421972567f1SJeff Layton retry:
3422972567f1SJeff Layton 	dentry = user_path_create(dfd, filename, &path, lookup_flags);
3423dae6ad8fSAl Viro 	if (IS_ERR(dentry))
3424dae6ad8fSAl Viro 		return PTR_ERR(dentry);
34252ad94ae6SAl Viro 
3426dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3427ce3b0f8dSAl Viro 		mode &= ~current_umask();
3428dae6ad8fSAl Viro 	error = security_path_mknod(&path, dentry, mode, dev);
3429be6d3e56SKentaro Takeda 	if (error)
3430a8104a9fSAl Viro 		goto out;
34311da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
34321da177e4SLinus Torvalds 		case 0: case S_IFREG:
3433312b63fbSAl Viro 			error = vfs_create(path.dentry->d_inode,dentry,mode,true);
34341da177e4SLinus Torvalds 			break;
34351da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
3436dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
34371da177e4SLinus Torvalds 					new_decode_dev(dev));
34381da177e4SLinus Torvalds 			break;
34391da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
3440dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
34411da177e4SLinus Torvalds 			break;
34421da177e4SLinus Torvalds 	}
3443a8104a9fSAl Viro out:
3444921a1650SAl Viro 	done_path_create(&path, dentry);
3445972567f1SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3446972567f1SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3447972567f1SJeff Layton 		goto retry;
3448972567f1SJeff Layton 	}
34491da177e4SLinus Torvalds 	return error;
34501da177e4SLinus Torvalds }
34511da177e4SLinus Torvalds 
34528208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
34535590ff0dSUlrich Drepper {
34545590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
34555590ff0dSUlrich Drepper }
34565590ff0dSUlrich Drepper 
345718bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
34581da177e4SLinus Torvalds {
3459a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
34608de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
34611da177e4SLinus Torvalds 
34621da177e4SLinus Torvalds 	if (error)
34631da177e4SLinus Torvalds 		return error;
34641da177e4SLinus Torvalds 
3465acfa4380SAl Viro 	if (!dir->i_op->mkdir)
34661da177e4SLinus Torvalds 		return -EPERM;
34671da177e4SLinus Torvalds 
34681da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
34691da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
34701da177e4SLinus Torvalds 	if (error)
34711da177e4SLinus Torvalds 		return error;
34721da177e4SLinus Torvalds 
34738de52778SAl Viro 	if (max_links && dir->i_nlink >= max_links)
34748de52778SAl Viro 		return -EMLINK;
34758de52778SAl Viro 
34761da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
3477a74574aaSStephen Smalley 	if (!error)
3478f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
34791da177e4SLinus Torvalds 	return error;
34801da177e4SLinus Torvalds }
34814d359507SAl Viro EXPORT_SYMBOL(vfs_mkdir);
34821da177e4SLinus Torvalds 
3483a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
34841da177e4SLinus Torvalds {
34856902d925SDave Hansen 	struct dentry *dentry;
3486dae6ad8fSAl Viro 	struct path path;
3487dae6ad8fSAl Viro 	int error;
3488b76d8b82SJeff Layton 	unsigned int lookup_flags = LOOKUP_DIRECTORY;
34891da177e4SLinus Torvalds 
3490b76d8b82SJeff Layton retry:
3491b76d8b82SJeff Layton 	dentry = user_path_create(dfd, pathname, &path, lookup_flags);
34926902d925SDave Hansen 	if (IS_ERR(dentry))
3493dae6ad8fSAl Viro 		return PTR_ERR(dentry);
34946902d925SDave Hansen 
3495dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3496ce3b0f8dSAl Viro 		mode &= ~current_umask();
3497dae6ad8fSAl Viro 	error = security_path_mkdir(&path, dentry, mode);
3498a8104a9fSAl Viro 	if (!error)
3499dae6ad8fSAl Viro 		error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3500921a1650SAl Viro 	done_path_create(&path, dentry);
3501b76d8b82SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3502b76d8b82SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3503b76d8b82SJeff Layton 		goto retry;
3504b76d8b82SJeff Layton 	}
35051da177e4SLinus Torvalds 	return error;
35061da177e4SLinus Torvalds }
35071da177e4SLinus Torvalds 
3508a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
35095590ff0dSUlrich Drepper {
35105590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
35115590ff0dSUlrich Drepper }
35125590ff0dSUlrich Drepper 
35131da177e4SLinus Torvalds /*
3514a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
3515c0d02594SJ. Bruce Fields  * should have a usage count of 1 if we're the only user of this
3516a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
3517a71905f0SSage Weil  * then we drop the dentry now.
35181da177e4SLinus Torvalds  *
35191da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
35201da177e4SLinus Torvalds  * do a
35211da177e4SLinus Torvalds  *
35221da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
35231da177e4SLinus Torvalds  *		return -EBUSY;
35241da177e4SLinus Torvalds  *
35251da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
35261da177e4SLinus Torvalds  * that is still in use by something else..
35271da177e4SLinus Torvalds  */
35281da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
35291da177e4SLinus Torvalds {
35301da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
35311da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
353298474236SWaiman Long 	if (dentry->d_lockref.count == 1)
35331da177e4SLinus Torvalds 		__d_drop(dentry);
35341da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
35351da177e4SLinus Torvalds }
35364d359507SAl Viro EXPORT_SYMBOL(dentry_unhash);
35371da177e4SLinus Torvalds 
35381da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
35391da177e4SLinus Torvalds {
35401da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
35411da177e4SLinus Torvalds 
35421da177e4SLinus Torvalds 	if (error)
35431da177e4SLinus Torvalds 		return error;
35441da177e4SLinus Torvalds 
3545acfa4380SAl Viro 	if (!dir->i_op->rmdir)
35461da177e4SLinus Torvalds 		return -EPERM;
35471da177e4SLinus Torvalds 
35481d2ef590SAl Viro 	dget(dentry);
35491b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
3550912dbc15SSage Weil 
35511da177e4SLinus Torvalds 	error = -EBUSY;
3552912dbc15SSage Weil 	if (d_mountpoint(dentry))
3553912dbc15SSage Weil 		goto out;
3554912dbc15SSage Weil 
35551da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
3556912dbc15SSage Weil 	if (error)
3557912dbc15SSage Weil 		goto out;
3558912dbc15SSage Weil 
35593cebde24SSage Weil 	shrink_dcache_parent(dentry);
35601da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
3561912dbc15SSage Weil 	if (error)
3562912dbc15SSage Weil 		goto out;
3563912dbc15SSage Weil 
35641da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
3565d83c49f3SAl Viro 	dont_mount(dentry);
35661da177e4SLinus Torvalds 
3567912dbc15SSage Weil out:
3568912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
35691d2ef590SAl Viro 	dput(dentry);
3570912dbc15SSage Weil 	if (!error)
3571912dbc15SSage Weil 		d_delete(dentry);
35721da177e4SLinus Torvalds 	return error;
35731da177e4SLinus Torvalds }
35744d359507SAl Viro EXPORT_SYMBOL(vfs_rmdir);
35751da177e4SLinus Torvalds 
35765590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
35771da177e4SLinus Torvalds {
35781da177e4SLinus Torvalds 	int error = 0;
357991a27b2aSJeff Layton 	struct filename *name;
35801da177e4SLinus Torvalds 	struct dentry *dentry;
35811da177e4SLinus Torvalds 	struct nameidata nd;
3582c6ee9206SJeff Layton 	unsigned int lookup_flags = 0;
3583c6ee9206SJeff Layton retry:
3584c6ee9206SJeff Layton 	name = user_path_parent(dfd, pathname, &nd, lookup_flags);
358591a27b2aSJeff Layton 	if (IS_ERR(name))
358691a27b2aSJeff Layton 		return PTR_ERR(name);
35871da177e4SLinus Torvalds 
35881da177e4SLinus Torvalds 	switch(nd.last_type) {
35891da177e4SLinus Torvalds 	case LAST_DOTDOT:
35901da177e4SLinus Torvalds 		error = -ENOTEMPTY;
35911da177e4SLinus Torvalds 		goto exit1;
35921da177e4SLinus Torvalds 	case LAST_DOT:
35931da177e4SLinus Torvalds 		error = -EINVAL;
35941da177e4SLinus Torvalds 		goto exit1;
35951da177e4SLinus Torvalds 	case LAST_ROOT:
35961da177e4SLinus Torvalds 		error = -EBUSY;
35971da177e4SLinus Torvalds 		goto exit1;
35981da177e4SLinus Torvalds 	}
35990612d9fbSOGAWA Hirofumi 
36000612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
3601c30dabfeSJan Kara 	error = mnt_want_write(nd.path.mnt);
3602c30dabfeSJan Kara 	if (error)
3603c30dabfeSJan Kara 		goto exit1;
36040612d9fbSOGAWA Hirofumi 
36054ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
360649705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
36071da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
36086902d925SDave Hansen 	if (IS_ERR(dentry))
36096902d925SDave Hansen 		goto exit2;
3610e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
3611e6bc45d6STheodore Ts'o 		error = -ENOENT;
3612e6bc45d6STheodore Ts'o 		goto exit3;
3613e6bc45d6STheodore Ts'o 	}
3614be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
3615be6d3e56SKentaro Takeda 	if (error)
3616c30dabfeSJan Kara 		goto exit3;
36174ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
36180622753bSDave Hansen exit3:
36191da177e4SLinus Torvalds 	dput(dentry);
36206902d925SDave Hansen exit2:
36214ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3622c30dabfeSJan Kara 	mnt_drop_write(nd.path.mnt);
36231da177e4SLinus Torvalds exit1:
36241d957f9bSJan Blunck 	path_put(&nd.path);
36251da177e4SLinus Torvalds 	putname(name);
3626c6ee9206SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3627c6ee9206SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3628c6ee9206SJeff Layton 		goto retry;
3629c6ee9206SJeff Layton 	}
36301da177e4SLinus Torvalds 	return error;
36311da177e4SLinus Torvalds }
36321da177e4SLinus Torvalds 
36333cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
36345590ff0dSUlrich Drepper {
36355590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
36365590ff0dSUlrich Drepper }
36375590ff0dSUlrich Drepper 
3638b21996e3SJ. Bruce Fields /**
3639b21996e3SJ. Bruce Fields  * vfs_unlink - unlink a filesystem object
3640b21996e3SJ. Bruce Fields  * @dir:	parent directory
3641b21996e3SJ. Bruce Fields  * @dentry:	victim
3642b21996e3SJ. Bruce Fields  * @delegated_inode: returns victim inode, if the inode is delegated.
3643b21996e3SJ. Bruce Fields  *
3644b21996e3SJ. Bruce Fields  * The caller must hold dir->i_mutex.
3645b21996e3SJ. Bruce Fields  *
3646b21996e3SJ. Bruce Fields  * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3647b21996e3SJ. Bruce Fields  * return a reference to the inode in delegated_inode.  The caller
3648b21996e3SJ. Bruce Fields  * should then break the delegation on that inode and retry.  Because
3649b21996e3SJ. Bruce Fields  * breaking a delegation may take a long time, the caller should drop
3650b21996e3SJ. Bruce Fields  * dir->i_mutex before doing so.
3651b21996e3SJ. Bruce Fields  *
3652b21996e3SJ. Bruce Fields  * Alternatively, a caller may pass NULL for delegated_inode.  This may
3653b21996e3SJ. Bruce Fields  * be appropriate for callers that expect the underlying filesystem not
3654b21996e3SJ. Bruce Fields  * to be NFS exported.
3655b21996e3SJ. Bruce Fields  */
3656b21996e3SJ. Bruce Fields int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
36571da177e4SLinus Torvalds {
36589accbb97SJ. Bruce Fields 	struct inode *target = dentry->d_inode;
36591da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
36601da177e4SLinus Torvalds 
36611da177e4SLinus Torvalds 	if (error)
36621da177e4SLinus Torvalds 		return error;
36631da177e4SLinus Torvalds 
3664acfa4380SAl Viro 	if (!dir->i_op->unlink)
36651da177e4SLinus Torvalds 		return -EPERM;
36661da177e4SLinus Torvalds 
36679accbb97SJ. Bruce Fields 	mutex_lock(&target->i_mutex);
36681da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
36691da177e4SLinus Torvalds 		error = -EBUSY;
36701da177e4SLinus Torvalds 	else {
36711da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
3672bec1052eSAl Viro 		if (!error) {
36735a14696cSJ. Bruce Fields 			error = try_break_deleg(target, delegated_inode);
36745a14696cSJ. Bruce Fields 			if (error)
3675b21996e3SJ. Bruce Fields 				goto out;
36761da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
3677bec1052eSAl Viro 			if (!error)
3678d83c49f3SAl Viro 				dont_mount(dentry);
3679bec1052eSAl Viro 		}
36801da177e4SLinus Torvalds 	}
3681b21996e3SJ. Bruce Fields out:
36829accbb97SJ. Bruce Fields 	mutex_unlock(&target->i_mutex);
36831da177e4SLinus Torvalds 
36841da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
36851da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
36869accbb97SJ. Bruce Fields 		fsnotify_link_count(target);
36871da177e4SLinus Torvalds 		d_delete(dentry);
36881da177e4SLinus Torvalds 	}
36890eeca283SRobert Love 
36901da177e4SLinus Torvalds 	return error;
36911da177e4SLinus Torvalds }
36924d359507SAl Viro EXPORT_SYMBOL(vfs_unlink);
36931da177e4SLinus Torvalds 
36941da177e4SLinus Torvalds /*
36951da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
36961b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
36971da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
36981da177e4SLinus Torvalds  * while waiting on the I/O.
36991da177e4SLinus Torvalds  */
37005590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
37011da177e4SLinus Torvalds {
37022ad94ae6SAl Viro 	int error;
370391a27b2aSJeff Layton 	struct filename *name;
37041da177e4SLinus Torvalds 	struct dentry *dentry;
37051da177e4SLinus Torvalds 	struct nameidata nd;
37061da177e4SLinus Torvalds 	struct inode *inode = NULL;
3707b21996e3SJ. Bruce Fields 	struct inode *delegated_inode = NULL;
37085d18f813SJeff Layton 	unsigned int lookup_flags = 0;
37095d18f813SJeff Layton retry:
37105d18f813SJeff Layton 	name = user_path_parent(dfd, pathname, &nd, lookup_flags);
371191a27b2aSJeff Layton 	if (IS_ERR(name))
371291a27b2aSJeff Layton 		return PTR_ERR(name);
37132ad94ae6SAl Viro 
37141da177e4SLinus Torvalds 	error = -EISDIR;
37151da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
37161da177e4SLinus Torvalds 		goto exit1;
37170612d9fbSOGAWA Hirofumi 
37180612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
3719c30dabfeSJan Kara 	error = mnt_want_write(nd.path.mnt);
3720c30dabfeSJan Kara 	if (error)
3721c30dabfeSJan Kara 		goto exit1;
3722b21996e3SJ. Bruce Fields retry_deleg:
37234ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
372449705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
37251da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
37261da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
37271da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
372850338b88STörök Edwin 		if (nd.last.name[nd.last.len])
372950338b88STörök Edwin 			goto slashes;
37301da177e4SLinus Torvalds 		inode = dentry->d_inode;
3731b18825a7SDavid Howells 		if (d_is_negative(dentry))
3732e6bc45d6STheodore Ts'o 			goto slashes;
37337de9c6eeSAl Viro 		ihold(inode);
3734be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
3735be6d3e56SKentaro Takeda 		if (error)
3736c30dabfeSJan Kara 			goto exit2;
3737b21996e3SJ. Bruce Fields 		error = vfs_unlink(nd.path.dentry->d_inode, dentry, &delegated_inode);
37381da177e4SLinus Torvalds exit2:
37391da177e4SLinus Torvalds 		dput(dentry);
37401da177e4SLinus Torvalds 	}
37414ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
37421da177e4SLinus Torvalds 	if (inode)
37431da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
3744b21996e3SJ. Bruce Fields 	inode = NULL;
3745b21996e3SJ. Bruce Fields 	if (delegated_inode) {
37465a14696cSJ. Bruce Fields 		error = break_deleg_wait(&delegated_inode);
3747b21996e3SJ. Bruce Fields 		if (!error)
3748b21996e3SJ. Bruce Fields 			goto retry_deleg;
3749b21996e3SJ. Bruce Fields 	}
3750c30dabfeSJan Kara 	mnt_drop_write(nd.path.mnt);
37511da177e4SLinus Torvalds exit1:
37521d957f9bSJan Blunck 	path_put(&nd.path);
37531da177e4SLinus Torvalds 	putname(name);
37545d18f813SJeff Layton 	if (retry_estale(error, lookup_flags)) {
37555d18f813SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
37565d18f813SJeff Layton 		inode = NULL;
37575d18f813SJeff Layton 		goto retry;
37585d18f813SJeff Layton 	}
37591da177e4SLinus Torvalds 	return error;
37601da177e4SLinus Torvalds 
37611da177e4SLinus Torvalds slashes:
3762b18825a7SDavid Howells 	if (d_is_negative(dentry))
3763b18825a7SDavid Howells 		error = -ENOENT;
376444b1d530SMiklos Szeredi 	else if (d_is_dir(dentry))
3765b18825a7SDavid Howells 		error = -EISDIR;
3766b18825a7SDavid Howells 	else
3767b18825a7SDavid Howells 		error = -ENOTDIR;
37681da177e4SLinus Torvalds 	goto exit2;
37691da177e4SLinus Torvalds }
37701da177e4SLinus Torvalds 
37712e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
37725590ff0dSUlrich Drepper {
37735590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
37745590ff0dSUlrich Drepper 		return -EINVAL;
37755590ff0dSUlrich Drepper 
37765590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
37775590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
37785590ff0dSUlrich Drepper 
37795590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
37805590ff0dSUlrich Drepper }
37815590ff0dSUlrich Drepper 
37823480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
37835590ff0dSUlrich Drepper {
37845590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
37855590ff0dSUlrich Drepper }
37865590ff0dSUlrich Drepper 
3787db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
37881da177e4SLinus Torvalds {
3789a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
37901da177e4SLinus Torvalds 
37911da177e4SLinus Torvalds 	if (error)
37921da177e4SLinus Torvalds 		return error;
37931da177e4SLinus Torvalds 
3794acfa4380SAl Viro 	if (!dir->i_op->symlink)
37951da177e4SLinus Torvalds 		return -EPERM;
37961da177e4SLinus Torvalds 
37971da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
37981da177e4SLinus Torvalds 	if (error)
37991da177e4SLinus Torvalds 		return error;
38001da177e4SLinus Torvalds 
38011da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
3802a74574aaSStephen Smalley 	if (!error)
3803f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
38041da177e4SLinus Torvalds 	return error;
38051da177e4SLinus Torvalds }
38064d359507SAl Viro EXPORT_SYMBOL(vfs_symlink);
38071da177e4SLinus Torvalds 
38082e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
38092e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
38101da177e4SLinus Torvalds {
38112ad94ae6SAl Viro 	int error;
381291a27b2aSJeff Layton 	struct filename *from;
38136902d925SDave Hansen 	struct dentry *dentry;
3814dae6ad8fSAl Viro 	struct path path;
3815f46d3567SJeff Layton 	unsigned int lookup_flags = 0;
38161da177e4SLinus Torvalds 
38171da177e4SLinus Torvalds 	from = getname(oldname);
38181da177e4SLinus Torvalds 	if (IS_ERR(from))
38191da177e4SLinus Torvalds 		return PTR_ERR(from);
3820f46d3567SJeff Layton retry:
3821f46d3567SJeff Layton 	dentry = user_path_create(newdfd, newname, &path, lookup_flags);
38221da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
38236902d925SDave Hansen 	if (IS_ERR(dentry))
3824dae6ad8fSAl Viro 		goto out_putname;
38256902d925SDave Hansen 
382691a27b2aSJeff Layton 	error = security_path_symlink(&path, dentry, from->name);
3827a8104a9fSAl Viro 	if (!error)
382891a27b2aSJeff Layton 		error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
3829921a1650SAl Viro 	done_path_create(&path, dentry);
3830f46d3567SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3831f46d3567SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3832f46d3567SJeff Layton 		goto retry;
3833f46d3567SJeff Layton 	}
38346902d925SDave Hansen out_putname:
38351da177e4SLinus Torvalds 	putname(from);
38361da177e4SLinus Torvalds 	return error;
38371da177e4SLinus Torvalds }
38381da177e4SLinus Torvalds 
38393480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
38405590ff0dSUlrich Drepper {
38415590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
38425590ff0dSUlrich Drepper }
38435590ff0dSUlrich Drepper 
3844146a8595SJ. Bruce Fields /**
3845146a8595SJ. Bruce Fields  * vfs_link - create a new link
3846146a8595SJ. Bruce Fields  * @old_dentry:	object to be linked
3847146a8595SJ. Bruce Fields  * @dir:	new parent
3848146a8595SJ. Bruce Fields  * @new_dentry:	where to create the new link
3849146a8595SJ. Bruce Fields  * @delegated_inode: returns inode needing a delegation break
3850146a8595SJ. Bruce Fields  *
3851146a8595SJ. Bruce Fields  * The caller must hold dir->i_mutex
3852146a8595SJ. Bruce Fields  *
3853146a8595SJ. Bruce Fields  * If vfs_link discovers a delegation on the to-be-linked file in need
3854146a8595SJ. Bruce Fields  * of breaking, it will return -EWOULDBLOCK and return a reference to the
3855146a8595SJ. Bruce Fields  * inode in delegated_inode.  The caller should then break the delegation
3856146a8595SJ. Bruce Fields  * and retry.  Because breaking a delegation may take a long time, the
3857146a8595SJ. Bruce Fields  * caller should drop the i_mutex before doing so.
3858146a8595SJ. Bruce Fields  *
3859146a8595SJ. Bruce Fields  * Alternatively, a caller may pass NULL for delegated_inode.  This may
3860146a8595SJ. Bruce Fields  * be appropriate for callers that expect the underlying filesystem not
3861146a8595SJ. Bruce Fields  * to be NFS exported.
3862146a8595SJ. Bruce Fields  */
3863146a8595SJ. Bruce Fields int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
38641da177e4SLinus Torvalds {
38651da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
38668de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
38671da177e4SLinus Torvalds 	int error;
38681da177e4SLinus Torvalds 
38691da177e4SLinus Torvalds 	if (!inode)
38701da177e4SLinus Torvalds 		return -ENOENT;
38711da177e4SLinus Torvalds 
3872a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
38731da177e4SLinus Torvalds 	if (error)
38741da177e4SLinus Torvalds 		return error;
38751da177e4SLinus Torvalds 
38761da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
38771da177e4SLinus Torvalds 		return -EXDEV;
38781da177e4SLinus Torvalds 
38791da177e4SLinus Torvalds 	/*
38801da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
38811da177e4SLinus Torvalds 	 */
38821da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
38831da177e4SLinus Torvalds 		return -EPERM;
3884acfa4380SAl Viro 	if (!dir->i_op->link)
38851da177e4SLinus Torvalds 		return -EPERM;
38867e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
38871da177e4SLinus Torvalds 		return -EPERM;
38881da177e4SLinus Torvalds 
38891da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
38901da177e4SLinus Torvalds 	if (error)
38911da177e4SLinus Torvalds 		return error;
38921da177e4SLinus Torvalds 
38937e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
3894aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
3895f4e0c30cSAl Viro 	if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
3896aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
38978de52778SAl Viro 	else if (max_links && inode->i_nlink >= max_links)
38988de52778SAl Viro 		error = -EMLINK;
3899146a8595SJ. Bruce Fields 	else {
3900146a8595SJ. Bruce Fields 		error = try_break_deleg(inode, delegated_inode);
3901146a8595SJ. Bruce Fields 		if (!error)
39021da177e4SLinus Torvalds 			error = dir->i_op->link(old_dentry, dir, new_dentry);
3903146a8595SJ. Bruce Fields 	}
3904f4e0c30cSAl Viro 
3905f4e0c30cSAl Viro 	if (!error && (inode->i_state & I_LINKABLE)) {
3906f4e0c30cSAl Viro 		spin_lock(&inode->i_lock);
3907f4e0c30cSAl Viro 		inode->i_state &= ~I_LINKABLE;
3908f4e0c30cSAl Viro 		spin_unlock(&inode->i_lock);
3909f4e0c30cSAl Viro 	}
39107e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
3911e31e14ecSStephen Smalley 	if (!error)
39127e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
39131da177e4SLinus Torvalds 	return error;
39141da177e4SLinus Torvalds }
39154d359507SAl Viro EXPORT_SYMBOL(vfs_link);
39161da177e4SLinus Torvalds 
39171da177e4SLinus Torvalds /*
39181da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
39191da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
39201da177e4SLinus Torvalds  * newname.  --KAB
39211da177e4SLinus Torvalds  *
39221da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
39231da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
39241da177e4SLinus Torvalds  * and other special files.  --ADM
39251da177e4SLinus Torvalds  */
39262e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
39272e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
39281da177e4SLinus Torvalds {
39291da177e4SLinus Torvalds 	struct dentry *new_dentry;
3930dae6ad8fSAl Viro 	struct path old_path, new_path;
3931146a8595SJ. Bruce Fields 	struct inode *delegated_inode = NULL;
393211a7b371SAneesh Kumar K.V 	int how = 0;
39331da177e4SLinus Torvalds 	int error;
39341da177e4SLinus Torvalds 
393511a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3936c04030e1SUlrich Drepper 		return -EINVAL;
393711a7b371SAneesh Kumar K.V 	/*
3938f0cc6ffbSLinus Torvalds 	 * To use null names we require CAP_DAC_READ_SEARCH
3939f0cc6ffbSLinus Torvalds 	 * This ensures that not everyone will be able to create
3940f0cc6ffbSLinus Torvalds 	 * handlink using the passed filedescriptor.
394111a7b371SAneesh Kumar K.V 	 */
3942f0cc6ffbSLinus Torvalds 	if (flags & AT_EMPTY_PATH) {
3943f0cc6ffbSLinus Torvalds 		if (!capable(CAP_DAC_READ_SEARCH))
3944f0cc6ffbSLinus Torvalds 			return -ENOENT;
394511a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
3946f0cc6ffbSLinus Torvalds 	}
3947c04030e1SUlrich Drepper 
394811a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
394911a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
3950442e31caSJeff Layton retry:
395111a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
39521da177e4SLinus Torvalds 	if (error)
39532ad94ae6SAl Viro 		return error;
39542ad94ae6SAl Viro 
3955442e31caSJeff Layton 	new_dentry = user_path_create(newdfd, newname, &new_path,
3956442e31caSJeff Layton 					(how & LOOKUP_REVAL));
39571da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
39586902d925SDave Hansen 	if (IS_ERR(new_dentry))
3959dae6ad8fSAl Viro 		goto out;
3960dae6ad8fSAl Viro 
3961dae6ad8fSAl Viro 	error = -EXDEV;
3962dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt)
3963dae6ad8fSAl Viro 		goto out_dput;
3964800179c9SKees Cook 	error = may_linkat(&old_path);
3965800179c9SKees Cook 	if (unlikely(error))
3966800179c9SKees Cook 		goto out_dput;
3967dae6ad8fSAl Viro 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
3968be6d3e56SKentaro Takeda 	if (error)
3969a8104a9fSAl Viro 		goto out_dput;
3970146a8595SJ. Bruce Fields 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
397175c3f29dSDave Hansen out_dput:
3972921a1650SAl Viro 	done_path_create(&new_path, new_dentry);
3973146a8595SJ. Bruce Fields 	if (delegated_inode) {
3974146a8595SJ. Bruce Fields 		error = break_deleg_wait(&delegated_inode);
3975d22e6338SOleg Drokin 		if (!error) {
3976d22e6338SOleg Drokin 			path_put(&old_path);
3977146a8595SJ. Bruce Fields 			goto retry;
3978146a8595SJ. Bruce Fields 		}
3979d22e6338SOleg Drokin 	}
3980442e31caSJeff Layton 	if (retry_estale(error, how)) {
3981d22e6338SOleg Drokin 		path_put(&old_path);
3982442e31caSJeff Layton 		how |= LOOKUP_REVAL;
3983442e31caSJeff Layton 		goto retry;
3984442e31caSJeff Layton 	}
39851da177e4SLinus Torvalds out:
39862d8f3038SAl Viro 	path_put(&old_path);
39871da177e4SLinus Torvalds 
39881da177e4SLinus Torvalds 	return error;
39891da177e4SLinus Torvalds }
39901da177e4SLinus Torvalds 
39913480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
39925590ff0dSUlrich Drepper {
3993c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
39945590ff0dSUlrich Drepper }
39955590ff0dSUlrich Drepper 
3996bc27027aSMiklos Szeredi /**
3997bc27027aSMiklos Szeredi  * vfs_rename - rename a filesystem object
3998bc27027aSMiklos Szeredi  * @old_dir:	parent of source
3999bc27027aSMiklos Szeredi  * @old_dentry:	source
4000bc27027aSMiklos Szeredi  * @new_dir:	parent of destination
4001bc27027aSMiklos Szeredi  * @new_dentry:	destination
4002bc27027aSMiklos Szeredi  * @delegated_inode: returns an inode needing a delegation break
4003520c8b16SMiklos Szeredi  * @flags:	rename flags
4004bc27027aSMiklos Szeredi  *
4005bc27027aSMiklos Szeredi  * The caller must hold multiple mutexes--see lock_rename()).
4006bc27027aSMiklos Szeredi  *
4007bc27027aSMiklos Szeredi  * If vfs_rename discovers a delegation in need of breaking at either
4008bc27027aSMiklos Szeredi  * the source or destination, it will return -EWOULDBLOCK and return a
4009bc27027aSMiklos Szeredi  * reference to the inode in delegated_inode.  The caller should then
4010bc27027aSMiklos Szeredi  * break the delegation and retry.  Because breaking a delegation may
4011bc27027aSMiklos Szeredi  * take a long time, the caller should drop all locks before doing
4012bc27027aSMiklos Szeredi  * so.
4013bc27027aSMiklos Szeredi  *
4014bc27027aSMiklos Szeredi  * Alternatively, a caller may pass NULL for delegated_inode.  This may
4015bc27027aSMiklos Szeredi  * be appropriate for callers that expect the underlying filesystem not
4016bc27027aSMiklos Szeredi  * to be NFS exported.
4017bc27027aSMiklos Szeredi  *
40181da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
40191da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
40201da177e4SLinus Torvalds  * Problems:
40211da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
40221da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
40231da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
4024a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
40251da177e4SLinus Torvalds  *	   story.
40266cedba89SJ. Bruce Fields  *	c) we have to lock _four_ objects - parents and victim (if it exists),
40276cedba89SJ. Bruce Fields  *	   and source (if it is not a directory).
40281b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
40291da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
40301da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
4031a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
40321da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
40331da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
40341da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
4035a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
40361da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
40371da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
40381da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
4039e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
40401b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
40411da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
4042c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
40431da177e4SLinus Torvalds  *	   locking].
40441da177e4SLinus Torvalds  */
40451da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
40468e6d782cSJ. Bruce Fields 	       struct inode *new_dir, struct dentry *new_dentry,
4047520c8b16SMiklos Szeredi 	       struct inode **delegated_inode, unsigned int flags)
40481da177e4SLinus Torvalds {
40491da177e4SLinus Torvalds 	int error;
4050bc27027aSMiklos Szeredi 	bool is_dir = d_is_dir(old_dentry);
405159b0df21SEric Paris 	const unsigned char *old_name;
4052bc27027aSMiklos Szeredi 	struct inode *source = old_dentry->d_inode;
4053bc27027aSMiklos Szeredi 	struct inode *target = new_dentry->d_inode;
4054da1ce067SMiklos Szeredi 	bool new_is_dir = false;
4055da1ce067SMiklos Szeredi 	unsigned max_links = new_dir->i_sb->s_max_links;
40561da177e4SLinus Torvalds 
4057bc27027aSMiklos Szeredi 	if (source == target)
40581da177e4SLinus Torvalds 		return 0;
40591da177e4SLinus Torvalds 
40601da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
40611da177e4SLinus Torvalds 	if (error)
40621da177e4SLinus Torvalds 		return error;
40631da177e4SLinus Torvalds 
4064da1ce067SMiklos Szeredi 	if (!target) {
4065a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
4066da1ce067SMiklos Szeredi 	} else {
4067da1ce067SMiklos Szeredi 		new_is_dir = d_is_dir(new_dentry);
4068da1ce067SMiklos Szeredi 
4069da1ce067SMiklos Szeredi 		if (!(flags & RENAME_EXCHANGE))
40701da177e4SLinus Torvalds 			error = may_delete(new_dir, new_dentry, is_dir);
4071da1ce067SMiklos Szeredi 		else
4072da1ce067SMiklos Szeredi 			error = may_delete(new_dir, new_dentry, new_is_dir);
4073da1ce067SMiklos Szeredi 	}
40741da177e4SLinus Torvalds 	if (error)
40751da177e4SLinus Torvalds 		return error;
40761da177e4SLinus Torvalds 
4077acfa4380SAl Viro 	if (!old_dir->i_op->rename)
40781da177e4SLinus Torvalds 		return -EPERM;
40791da177e4SLinus Torvalds 
4080520c8b16SMiklos Szeredi 	if (flags && !old_dir->i_op->rename2)
4081520c8b16SMiklos Szeredi 		return -EINVAL;
4082520c8b16SMiklos Szeredi 
4083bc27027aSMiklos Szeredi 	/*
4084bc27027aSMiklos Szeredi 	 * If we are going to change the parent - check write permissions,
4085bc27027aSMiklos Szeredi 	 * we'll need to flip '..'.
4086bc27027aSMiklos Szeredi 	 */
4087da1ce067SMiklos Szeredi 	if (new_dir != old_dir) {
4088da1ce067SMiklos Szeredi 		if (is_dir) {
4089bc27027aSMiklos Szeredi 			error = inode_permission(source, MAY_WRITE);
4090bc27027aSMiklos Szeredi 			if (error)
4091bc27027aSMiklos Szeredi 				return error;
4092bc27027aSMiklos Szeredi 		}
4093da1ce067SMiklos Szeredi 		if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4094da1ce067SMiklos Szeredi 			error = inode_permission(target, MAY_WRITE);
4095da1ce067SMiklos Szeredi 			if (error)
4096da1ce067SMiklos Szeredi 				return error;
4097da1ce067SMiklos Szeredi 		}
4098da1ce067SMiklos Szeredi 	}
40990eeca283SRobert Love 
41000b3974ebSMiklos Szeredi 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
41010b3974ebSMiklos Szeredi 				      flags);
4102bc27027aSMiklos Szeredi 	if (error)
4103bc27027aSMiklos Szeredi 		return error;
4104bc27027aSMiklos Szeredi 
4105bc27027aSMiklos Szeredi 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
4106bc27027aSMiklos Szeredi 	dget(new_dentry);
4107da1ce067SMiklos Szeredi 	if (!is_dir || (flags & RENAME_EXCHANGE))
4108bc27027aSMiklos Szeredi 		lock_two_nondirectories(source, target);
4109bc27027aSMiklos Szeredi 	else if (target)
4110bc27027aSMiklos Szeredi 		mutex_lock(&target->i_mutex);
4111bc27027aSMiklos Szeredi 
4112bc27027aSMiklos Szeredi 	error = -EBUSY;
4113bc27027aSMiklos Szeredi 	if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
4114bc27027aSMiklos Szeredi 		goto out;
4115bc27027aSMiklos Szeredi 
4116da1ce067SMiklos Szeredi 	if (max_links && new_dir != old_dir) {
4117bc27027aSMiklos Szeredi 		error = -EMLINK;
4118da1ce067SMiklos Szeredi 		if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
4119bc27027aSMiklos Szeredi 			goto out;
4120da1ce067SMiklos Szeredi 		if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4121da1ce067SMiklos Szeredi 		    old_dir->i_nlink >= max_links)
4122da1ce067SMiklos Szeredi 			goto out;
4123da1ce067SMiklos Szeredi 	}
4124da1ce067SMiklos Szeredi 	if (is_dir && !(flags & RENAME_EXCHANGE) && target)
4125bc27027aSMiklos Szeredi 		shrink_dcache_parent(new_dentry);
4126da1ce067SMiklos Szeredi 	if (!is_dir) {
4127bc27027aSMiklos Szeredi 		error = try_break_deleg(source, delegated_inode);
4128bc27027aSMiklos Szeredi 		if (error)
4129bc27027aSMiklos Szeredi 			goto out;
4130da1ce067SMiklos Szeredi 	}
4131da1ce067SMiklos Szeredi 	if (target && !new_is_dir) {
4132bc27027aSMiklos Szeredi 		error = try_break_deleg(target, delegated_inode);
4133bc27027aSMiklos Szeredi 		if (error)
4134bc27027aSMiklos Szeredi 			goto out;
4135bc27027aSMiklos Szeredi 	}
4136520c8b16SMiklos Szeredi 	if (!flags) {
4137520c8b16SMiklos Szeredi 		error = old_dir->i_op->rename(old_dir, old_dentry,
4138520c8b16SMiklos Szeredi 					      new_dir, new_dentry);
4139520c8b16SMiklos Szeredi 	} else {
4140520c8b16SMiklos Szeredi 		error = old_dir->i_op->rename2(old_dir, old_dentry,
4141520c8b16SMiklos Szeredi 					       new_dir, new_dentry, flags);
4142520c8b16SMiklos Szeredi 	}
4143bc27027aSMiklos Szeredi 	if (error)
4144bc27027aSMiklos Szeredi 		goto out;
4145bc27027aSMiklos Szeredi 
4146da1ce067SMiklos Szeredi 	if (!(flags & RENAME_EXCHANGE) && target) {
41471da177e4SLinus Torvalds 		if (is_dir)
4148bc27027aSMiklos Szeredi 			target->i_flags |= S_DEAD;
4149bc27027aSMiklos Szeredi 		dont_mount(new_dentry);
4150bc27027aSMiklos Szeredi 	}
4151da1ce067SMiklos Szeredi 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4152da1ce067SMiklos Szeredi 		if (!(flags & RENAME_EXCHANGE))
4153bc27027aSMiklos Szeredi 			d_move(old_dentry, new_dentry);
4154da1ce067SMiklos Szeredi 		else
4155da1ce067SMiklos Szeredi 			d_exchange(old_dentry, new_dentry);
4156da1ce067SMiklos Szeredi 	}
4157bc27027aSMiklos Szeredi out:
4158da1ce067SMiklos Szeredi 	if (!is_dir || (flags & RENAME_EXCHANGE))
4159bc27027aSMiklos Szeredi 		unlock_two_nondirectories(source, target);
4160bc27027aSMiklos Szeredi 	else if (target)
4161bc27027aSMiklos Szeredi 		mutex_unlock(&target->i_mutex);
4162bc27027aSMiklos Szeredi 	dput(new_dentry);
4163da1ce067SMiklos Szeredi 	if (!error) {
4164123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
4165da1ce067SMiklos Szeredi 			      !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4166da1ce067SMiklos Szeredi 		if (flags & RENAME_EXCHANGE) {
4167da1ce067SMiklos Szeredi 			fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
4168da1ce067SMiklos Szeredi 				      new_is_dir, NULL, new_dentry);
4169da1ce067SMiklos Szeredi 		}
4170da1ce067SMiklos Szeredi 	}
41710eeca283SRobert Love 	fsnotify_oldname_free(old_name);
41720eeca283SRobert Love 
41731da177e4SLinus Torvalds 	return error;
41741da177e4SLinus Torvalds }
41754d359507SAl Viro EXPORT_SYMBOL(vfs_rename);
41761da177e4SLinus Torvalds 
4177520c8b16SMiklos Szeredi SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4178520c8b16SMiklos Szeredi 		int, newdfd, const char __user *, newname, unsigned int, flags)
41791da177e4SLinus Torvalds {
41801da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
41811da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
41821da177e4SLinus Torvalds 	struct dentry *trap;
41831da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
41848e6d782cSJ. Bruce Fields 	struct inode *delegated_inode = NULL;
418591a27b2aSJeff Layton 	struct filename *from;
418691a27b2aSJeff Layton 	struct filename *to;
4187c6a94284SJeff Layton 	unsigned int lookup_flags = 0;
4188c6a94284SJeff Layton 	bool should_retry = false;
41892ad94ae6SAl Viro 	int error;
4190520c8b16SMiklos Szeredi 
4191da1ce067SMiklos Szeredi 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
4192da1ce067SMiklos Szeredi 		return -EINVAL;
4193da1ce067SMiklos Szeredi 
4194da1ce067SMiklos Szeredi 	if ((flags & RENAME_NOREPLACE) && (flags & RENAME_EXCHANGE))
4195520c8b16SMiklos Szeredi 		return -EINVAL;
4196520c8b16SMiklos Szeredi 
4197c6a94284SJeff Layton retry:
4198c6a94284SJeff Layton 	from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
419991a27b2aSJeff Layton 	if (IS_ERR(from)) {
420091a27b2aSJeff Layton 		error = PTR_ERR(from);
42011da177e4SLinus Torvalds 		goto exit;
420291a27b2aSJeff Layton 	}
42031da177e4SLinus Torvalds 
4204c6a94284SJeff Layton 	to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
420591a27b2aSJeff Layton 	if (IS_ERR(to)) {
420691a27b2aSJeff Layton 		error = PTR_ERR(to);
42071da177e4SLinus Torvalds 		goto exit1;
420891a27b2aSJeff Layton 	}
42091da177e4SLinus Torvalds 
42101da177e4SLinus Torvalds 	error = -EXDEV;
42114ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
42121da177e4SLinus Torvalds 		goto exit2;
42131da177e4SLinus Torvalds 
42144ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
42151da177e4SLinus Torvalds 	error = -EBUSY;
42161da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
42171da177e4SLinus Torvalds 		goto exit2;
42181da177e4SLinus Torvalds 
42194ac91378SJan Blunck 	new_dir = newnd.path.dentry;
42200a7c3937SMiklos Szeredi 	if (flags & RENAME_NOREPLACE)
42210a7c3937SMiklos Szeredi 		error = -EEXIST;
42221da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
42231da177e4SLinus Torvalds 		goto exit2;
42241da177e4SLinus Torvalds 
4225c30dabfeSJan Kara 	error = mnt_want_write(oldnd.path.mnt);
4226c30dabfeSJan Kara 	if (error)
4227c30dabfeSJan Kara 		goto exit2;
4228c30dabfeSJan Kara 
42290612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
42300612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
4231da1ce067SMiklos Szeredi 	if (!(flags & RENAME_EXCHANGE))
42324e9ed2f8SOGAWA Hirofumi 		newnd.flags |= LOOKUP_RENAME_TARGET;
42330612d9fbSOGAWA Hirofumi 
42348e6d782cSJ. Bruce Fields retry_deleg:
42351da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
42361da177e4SLinus Torvalds 
423749705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
42381da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
42391da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
42401da177e4SLinus Torvalds 		goto exit3;
42411da177e4SLinus Torvalds 	/* source must exist */
42421da177e4SLinus Torvalds 	error = -ENOENT;
4243b18825a7SDavid Howells 	if (d_is_negative(old_dentry))
42441da177e4SLinus Torvalds 		goto exit4;
424549705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
42461da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
42471da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
42481da177e4SLinus Torvalds 		goto exit4;
42490a7c3937SMiklos Szeredi 	error = -EEXIST;
42500a7c3937SMiklos Szeredi 	if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
42510a7c3937SMiklos Szeredi 		goto exit5;
4252da1ce067SMiklos Szeredi 	if (flags & RENAME_EXCHANGE) {
4253da1ce067SMiklos Szeredi 		error = -ENOENT;
4254da1ce067SMiklos Szeredi 		if (d_is_negative(new_dentry))
4255da1ce067SMiklos Szeredi 			goto exit5;
4256da1ce067SMiklos Szeredi 
4257da1ce067SMiklos Szeredi 		if (!d_is_dir(new_dentry)) {
4258da1ce067SMiklos Szeredi 			error = -ENOTDIR;
4259da1ce067SMiklos Szeredi 			if (newnd.last.name[newnd.last.len])
4260da1ce067SMiklos Szeredi 				goto exit5;
4261da1ce067SMiklos Szeredi 		}
4262da1ce067SMiklos Szeredi 	}
42630a7c3937SMiklos Szeredi 	/* unless the source is a directory trailing slashes give -ENOTDIR */
42640a7c3937SMiklos Szeredi 	if (!d_is_dir(old_dentry)) {
42650a7c3937SMiklos Szeredi 		error = -ENOTDIR;
42660a7c3937SMiklos Szeredi 		if (oldnd.last.name[oldnd.last.len])
42670a7c3937SMiklos Szeredi 			goto exit5;
4268da1ce067SMiklos Szeredi 		if (!(flags & RENAME_EXCHANGE) && newnd.last.name[newnd.last.len])
42690a7c3937SMiklos Szeredi 			goto exit5;
42700a7c3937SMiklos Szeredi 	}
42710a7c3937SMiklos Szeredi 	/* source should not be ancestor of target */
42720a7c3937SMiklos Szeredi 	error = -EINVAL;
42730a7c3937SMiklos Szeredi 	if (old_dentry == trap)
42740a7c3937SMiklos Szeredi 		goto exit5;
42751da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
4276da1ce067SMiklos Szeredi 	if (!(flags & RENAME_EXCHANGE))
42771da177e4SLinus Torvalds 		error = -ENOTEMPTY;
42781da177e4SLinus Torvalds 	if (new_dentry == trap)
42791da177e4SLinus Torvalds 		goto exit5;
42801da177e4SLinus Torvalds 
4281be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
42820b3974ebSMiklos Szeredi 				     &newnd.path, new_dentry, flags);
4283be6d3e56SKentaro Takeda 	if (error)
4284c30dabfeSJan Kara 		goto exit5;
42851da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
42868e6d782cSJ. Bruce Fields 			   new_dir->d_inode, new_dentry,
4287520c8b16SMiklos Szeredi 			   &delegated_inode, flags);
42881da177e4SLinus Torvalds exit5:
42891da177e4SLinus Torvalds 	dput(new_dentry);
42901da177e4SLinus Torvalds exit4:
42911da177e4SLinus Torvalds 	dput(old_dentry);
42921da177e4SLinus Torvalds exit3:
42931da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
42948e6d782cSJ. Bruce Fields 	if (delegated_inode) {
42958e6d782cSJ. Bruce Fields 		error = break_deleg_wait(&delegated_inode);
42968e6d782cSJ. Bruce Fields 		if (!error)
42978e6d782cSJ. Bruce Fields 			goto retry_deleg;
42988e6d782cSJ. Bruce Fields 	}
4299c30dabfeSJan Kara 	mnt_drop_write(oldnd.path.mnt);
43001da177e4SLinus Torvalds exit2:
4301c6a94284SJeff Layton 	if (retry_estale(error, lookup_flags))
4302c6a94284SJeff Layton 		should_retry = true;
43031d957f9bSJan Blunck 	path_put(&newnd.path);
43042ad94ae6SAl Viro 	putname(to);
43051da177e4SLinus Torvalds exit1:
43061d957f9bSJan Blunck 	path_put(&oldnd.path);
43071da177e4SLinus Torvalds 	putname(from);
4308c6a94284SJeff Layton 	if (should_retry) {
4309c6a94284SJeff Layton 		should_retry = false;
4310c6a94284SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
4311c6a94284SJeff Layton 		goto retry;
4312c6a94284SJeff Layton 	}
43132ad94ae6SAl Viro exit:
43141da177e4SLinus Torvalds 	return error;
43151da177e4SLinus Torvalds }
43161da177e4SLinus Torvalds 
4317520c8b16SMiklos Szeredi SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4318520c8b16SMiklos Szeredi 		int, newdfd, const char __user *, newname)
4319520c8b16SMiklos Szeredi {
4320520c8b16SMiklos Szeredi 	return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
4321520c8b16SMiklos Szeredi }
4322520c8b16SMiklos Szeredi 
4323a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
43245590ff0dSUlrich Drepper {
4325520c8b16SMiklos Szeredi 	return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
43265590ff0dSUlrich Drepper }
43275590ff0dSUlrich Drepper 
43285d826c84SAl Viro int readlink_copy(char __user *buffer, int buflen, const char *link)
43291da177e4SLinus Torvalds {
43305d826c84SAl Viro 	int len = PTR_ERR(link);
43311da177e4SLinus Torvalds 	if (IS_ERR(link))
43321da177e4SLinus Torvalds 		goto out;
43331da177e4SLinus Torvalds 
43341da177e4SLinus Torvalds 	len = strlen(link);
43351da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
43361da177e4SLinus Torvalds 		len = buflen;
43371da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
43381da177e4SLinus Torvalds 		len = -EFAULT;
43391da177e4SLinus Torvalds out:
43401da177e4SLinus Torvalds 	return len;
43411da177e4SLinus Torvalds }
43425d826c84SAl Viro EXPORT_SYMBOL(readlink_copy);
43431da177e4SLinus Torvalds 
43441da177e4SLinus Torvalds /*
43451da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
43461da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
43471da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
43481da177e4SLinus Torvalds  */
43491da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
43501da177e4SLinus Torvalds {
43511da177e4SLinus Torvalds 	struct nameidata nd;
4352cc314eefSLinus Torvalds 	void *cookie;
4353694a1764SMarcin Slusarz 	int res;
4354cc314eefSLinus Torvalds 
43551da177e4SLinus Torvalds 	nd.depth = 0;
4356cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
4357694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
4358694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
4359694a1764SMarcin Slusarz 
43605d826c84SAl Viro 	res = readlink_copy(buffer, buflen, nd_get_link(&nd));
43611da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
4362cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
4363694a1764SMarcin Slusarz 	return res;
43641da177e4SLinus Torvalds }
43654d359507SAl Viro EXPORT_SYMBOL(generic_readlink);
43661da177e4SLinus Torvalds 
43671da177e4SLinus Torvalds /* get the link contents into pagecache */
43681da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
43691da177e4SLinus Torvalds {
4370ebd09abbSDuane Griffin 	char *kaddr;
43711da177e4SLinus Torvalds 	struct page *page;
43721da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
4373090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
43741da177e4SLinus Torvalds 	if (IS_ERR(page))
43756fe6900eSNick Piggin 		return (char*)page;
43761da177e4SLinus Torvalds 	*ppage = page;
4377ebd09abbSDuane Griffin 	kaddr = kmap(page);
4378ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4379ebd09abbSDuane Griffin 	return kaddr;
43801da177e4SLinus Torvalds }
43811da177e4SLinus Torvalds 
43821da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
43831da177e4SLinus Torvalds {
43841da177e4SLinus Torvalds 	struct page *page = NULL;
43855d826c84SAl Viro 	int res = readlink_copy(buffer, buflen, page_getlink(dentry, &page));
43861da177e4SLinus Torvalds 	if (page) {
43871da177e4SLinus Torvalds 		kunmap(page);
43881da177e4SLinus Torvalds 		page_cache_release(page);
43891da177e4SLinus Torvalds 	}
43901da177e4SLinus Torvalds 	return res;
43911da177e4SLinus Torvalds }
43924d359507SAl Viro EXPORT_SYMBOL(page_readlink);
43931da177e4SLinus Torvalds 
4394cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
43951da177e4SLinus Torvalds {
4396cc314eefSLinus Torvalds 	struct page *page = NULL;
43971da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
4398cc314eefSLinus Torvalds 	return page;
43991da177e4SLinus Torvalds }
44004d359507SAl Viro EXPORT_SYMBOL(page_follow_link_light);
44011da177e4SLinus Torvalds 
4402cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
44031da177e4SLinus Torvalds {
4404cc314eefSLinus Torvalds 	struct page *page = cookie;
4405cc314eefSLinus Torvalds 
4406cc314eefSLinus Torvalds 	if (page) {
44071da177e4SLinus Torvalds 		kunmap(page);
44081da177e4SLinus Torvalds 		page_cache_release(page);
44091da177e4SLinus Torvalds 	}
44101da177e4SLinus Torvalds }
44114d359507SAl Viro EXPORT_SYMBOL(page_put_link);
44121da177e4SLinus Torvalds 
441354566b2cSNick Piggin /*
441454566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
441554566b2cSNick Piggin  */
441654566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
44171da177e4SLinus Torvalds {
44181da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
44190adb25d2SKirill Korotaev 	struct page *page;
4420afddba49SNick Piggin 	void *fsdata;
4421beb497abSDmitriy Monakhov 	int err;
44221da177e4SLinus Torvalds 	char *kaddr;
442354566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
442454566b2cSNick Piggin 	if (nofs)
442554566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
44261da177e4SLinus Torvalds 
44277e53cac4SNeilBrown retry:
4428afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
442954566b2cSNick Piggin 				flags, &page, &fsdata);
44301da177e4SLinus Torvalds 	if (err)
4431afddba49SNick Piggin 		goto fail;
4432afddba49SNick Piggin 
4433e8e3c3d6SCong Wang 	kaddr = kmap_atomic(page);
44341da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
4435e8e3c3d6SCong Wang 	kunmap_atomic(kaddr);
4436afddba49SNick Piggin 
4437afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4438afddba49SNick Piggin 							page, fsdata);
44391da177e4SLinus Torvalds 	if (err < 0)
44401da177e4SLinus Torvalds 		goto fail;
4441afddba49SNick Piggin 	if (err < len-1)
4442afddba49SNick Piggin 		goto retry;
4443afddba49SNick Piggin 
44441da177e4SLinus Torvalds 	mark_inode_dirty(inode);
44451da177e4SLinus Torvalds 	return 0;
44461da177e4SLinus Torvalds fail:
44471da177e4SLinus Torvalds 	return err;
44481da177e4SLinus Torvalds }
44494d359507SAl Viro EXPORT_SYMBOL(__page_symlink);
44501da177e4SLinus Torvalds 
44510adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
44520adb25d2SKirill Korotaev {
44530adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
445454566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
44550adb25d2SKirill Korotaev }
44564d359507SAl Viro EXPORT_SYMBOL(page_symlink);
44570adb25d2SKirill Korotaev 
445892e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
44591da177e4SLinus Torvalds 	.readlink	= generic_readlink,
44601da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
44611da177e4SLinus Torvalds 	.put_link	= page_put_link,
44621da177e4SLinus Torvalds };
44631da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
4464