xref: /openbmc/linux/fs/namei.c (revision fd3522fd)
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>
3799d263d4SLinus Torvalds #include <linux/hash.h>
381da177e4SLinus Torvalds #include <asm/uaccess.h>
391da177e4SLinus Torvalds 
40e81e3f4dSEric Paris #include "internal.h"
41c7105365SAl Viro #include "mount.h"
42e81e3f4dSEric Paris 
431da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer]
441da177e4SLinus Torvalds  * Fundamental changes in the pathname lookup mechanisms (namei)
451da177e4SLinus Torvalds  * were necessary because of omirr.  The reason is that omirr needs
461da177e4SLinus Torvalds  * to know the _real_ pathname, not the user-supplied one, in case
471da177e4SLinus Torvalds  * of symlinks (and also when transname replacements occur).
481da177e4SLinus Torvalds  *
491da177e4SLinus Torvalds  * The new code replaces the old recursive symlink resolution with
501da177e4SLinus Torvalds  * an iterative one (in case of non-nested symlink chains).  It does
511da177e4SLinus Torvalds  * this with calls to <fs>_follow_link().
521da177e4SLinus Torvalds  * As a side effect, dir_namei(), _namei() and follow_link() are now
531da177e4SLinus Torvalds  * replaced with a single function lookup_dentry() that can handle all
541da177e4SLinus Torvalds  * the special cases of the former code.
551da177e4SLinus Torvalds  *
561da177e4SLinus Torvalds  * With the new dcache, the pathname is stored at each inode, at least as
571da177e4SLinus Torvalds  * long as the refcount of the inode is positive.  As a side effect, the
581da177e4SLinus Torvalds  * size of the dcache depends on the inode cache and thus is dynamic.
591da177e4SLinus Torvalds  *
601da177e4SLinus Torvalds  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
611da177e4SLinus Torvalds  * resolution to correspond with current state of the code.
621da177e4SLinus Torvalds  *
631da177e4SLinus Torvalds  * Note that the symlink resolution is not *completely* iterative.
641da177e4SLinus Torvalds  * There is still a significant amount of tail- and mid- recursion in
651da177e4SLinus Torvalds  * the algorithm.  Also, note that <fs>_readlink() is not used in
661da177e4SLinus Torvalds  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
671da177e4SLinus Torvalds  * may return different results than <fs>_follow_link().  Many virtual
681da177e4SLinus Torvalds  * filesystems (including /proc) exhibit this behavior.
691da177e4SLinus Torvalds  */
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
721da177e4SLinus Torvalds  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
731da177e4SLinus Torvalds  * and the name already exists in form of a symlink, try to create the new
741da177e4SLinus Torvalds  * name indicated by the symlink. The old code always complained that the
751da177e4SLinus Torvalds  * name already exists, due to not following the symlink even if its target
761da177e4SLinus Torvalds  * is nonexistent.  The new semantics affects also mknod() and link() when
7725985edcSLucas De Marchi  * the name is a symlink pointing to a non-existent name.
781da177e4SLinus Torvalds  *
791da177e4SLinus Torvalds  * I don't know which semantics is the right one, since I have no access
801da177e4SLinus Torvalds  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
811da177e4SLinus Torvalds  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
821da177e4SLinus Torvalds  * "old" one. Personally, I think the new semantics is much more logical.
831da177e4SLinus Torvalds  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
841da177e4SLinus Torvalds  * file does succeed in both HP-UX and SunOs, but not in Solaris
851da177e4SLinus Torvalds  * and in the old Linux semantics.
861da177e4SLinus Torvalds  */
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
891da177e4SLinus Torvalds  * semantics.  See the comments in "open_namei" and "do_link" below.
901da177e4SLinus Torvalds  *
911da177e4SLinus Torvalds  * [10-Sep-98 Alan Modra] Another symlink change.
921da177e4SLinus Torvalds  */
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
951da177e4SLinus Torvalds  *	inside the path - always follow.
961da177e4SLinus Torvalds  *	in the last component in creation/removal/renaming - never follow.
971da177e4SLinus Torvalds  *	if LOOKUP_FOLLOW passed - follow.
981da177e4SLinus Torvalds  *	if the pathname has trailing slashes - follow.
991da177e4SLinus Torvalds  *	otherwise - don't follow.
1001da177e4SLinus Torvalds  * (applied in that order).
1011da177e4SLinus Torvalds  *
1021da177e4SLinus Torvalds  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
1031da177e4SLinus Torvalds  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
1041da177e4SLinus Torvalds  * During the 2.4 we need to fix the userland stuff depending on it -
1051da177e4SLinus Torvalds  * hopefully we will be able to get rid of that wart in 2.5. So far only
1061da177e4SLinus Torvalds  * XEmacs seems to be relying on it...
1071da177e4SLinus Torvalds  */
1081da177e4SLinus Torvalds /*
1091da177e4SLinus Torvalds  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
110a11f3a05SArjan van de Ven  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
1111da177e4SLinus Torvalds  * any extra contention...
1121da177e4SLinus Torvalds  */
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional
1151da177e4SLinus Torvalds  * checking and hopefully speeding things up, we copy filenames to the
1161da177e4SLinus Torvalds  * kernel data space before using them..
1171da177e4SLinus Torvalds  *
1181da177e4SLinus Torvalds  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
1191da177e4SLinus Torvalds  * PATH_MAX includes the nul terminator --RR.
1201da177e4SLinus Torvalds  */
12191a27b2aSJeff Layton void final_putname(struct filename *name)
1221da177e4SLinus Torvalds {
1237950e385SJeff Layton 	if (name->separate) {
12491a27b2aSJeff Layton 		__putname(name->name);
12591a27b2aSJeff Layton 		kfree(name);
1267950e385SJeff Layton 	} else {
1277950e385SJeff Layton 		__putname(name);
12891a27b2aSJeff Layton 	}
1297950e385SJeff Layton }
1307950e385SJeff Layton 
1317950e385SJeff Layton #define EMBEDDED_NAME_MAX	(PATH_MAX - sizeof(struct filename))
13291a27b2aSJeff Layton 
13351f39a1fSDavid Drysdale struct filename *
13491a27b2aSJeff Layton getname_flags(const char __user *filename, int flags, int *empty)
13591a27b2aSJeff Layton {
13691a27b2aSJeff Layton 	struct filename *result, *err;
1373f9f0aa6SLinus Torvalds 	int len;
1387950e385SJeff Layton 	long max;
1397950e385SJeff Layton 	char *kname;
1401da177e4SLinus Torvalds 
1417ac86265SJeff Layton 	result = audit_reusename(filename);
1427ac86265SJeff Layton 	if (result)
1437ac86265SJeff Layton 		return result;
1447ac86265SJeff Layton 
1457950e385SJeff Layton 	result = __getname();
1463f9f0aa6SLinus Torvalds 	if (unlikely(!result))
1474043cde8SEric Paris 		return ERR_PTR(-ENOMEM);
1481da177e4SLinus Torvalds 
1497950e385SJeff Layton 	/*
1507950e385SJeff Layton 	 * First, try to embed the struct filename inside the names_cache
1517950e385SJeff Layton 	 * allocation
1527950e385SJeff Layton 	 */
1537950e385SJeff Layton 	kname = (char *)result + sizeof(*result);
15491a27b2aSJeff Layton 	result->name = kname;
1557950e385SJeff Layton 	result->separate = false;
1567950e385SJeff Layton 	max = EMBEDDED_NAME_MAX;
1577950e385SJeff Layton 
1587950e385SJeff Layton recopy:
1597950e385SJeff Layton 	len = strncpy_from_user(kname, filename, max);
16091a27b2aSJeff Layton 	if (unlikely(len < 0)) {
1613f9f0aa6SLinus Torvalds 		err = ERR_PTR(len);
1623f9f0aa6SLinus Torvalds 		goto error;
16391a27b2aSJeff Layton 	}
1643f9f0aa6SLinus Torvalds 
1657950e385SJeff Layton 	/*
1667950e385SJeff Layton 	 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
1677950e385SJeff Layton 	 * separate struct filename so we can dedicate the entire
1687950e385SJeff Layton 	 * names_cache allocation for the pathname, and re-do the copy from
1697950e385SJeff Layton 	 * userland.
1707950e385SJeff Layton 	 */
1717950e385SJeff Layton 	if (len == EMBEDDED_NAME_MAX && max == EMBEDDED_NAME_MAX) {
1727950e385SJeff Layton 		kname = (char *)result;
1737950e385SJeff Layton 
1747950e385SJeff Layton 		result = kzalloc(sizeof(*result), GFP_KERNEL);
1757950e385SJeff Layton 		if (!result) {
1767950e385SJeff Layton 			err = ERR_PTR(-ENOMEM);
1777950e385SJeff Layton 			result = (struct filename *)kname;
1787950e385SJeff Layton 			goto error;
1797950e385SJeff Layton 		}
1807950e385SJeff Layton 		result->name = kname;
1817950e385SJeff Layton 		result->separate = true;
1827950e385SJeff Layton 		max = PATH_MAX;
1837950e385SJeff Layton 		goto recopy;
1847950e385SJeff Layton 	}
1857950e385SJeff Layton 
1863f9f0aa6SLinus Torvalds 	/* The empty path is special. */
1873f9f0aa6SLinus Torvalds 	if (unlikely(!len)) {
1883f9f0aa6SLinus Torvalds 		if (empty)
1891fa1e7f6SAndy Whitcroft 			*empty = 1;
1903f9f0aa6SLinus Torvalds 		err = ERR_PTR(-ENOENT);
1913f9f0aa6SLinus Torvalds 		if (!(flags & LOOKUP_EMPTY))
1923f9f0aa6SLinus Torvalds 			goto error;
1931da177e4SLinus Torvalds 	}
1943f9f0aa6SLinus Torvalds 
1953f9f0aa6SLinus Torvalds 	err = ERR_PTR(-ENAMETOOLONG);
1967950e385SJeff Layton 	if (unlikely(len >= PATH_MAX))
1977950e385SJeff Layton 		goto error;
1987950e385SJeff Layton 
1997950e385SJeff Layton 	result->uptr = filename;
200c4ad8f98SLinus Torvalds 	result->aname = NULL;
2011da177e4SLinus Torvalds 	audit_getname(result);
2021da177e4SLinus Torvalds 	return result;
2031da177e4SLinus Torvalds 
2043f9f0aa6SLinus Torvalds error:
2057950e385SJeff Layton 	final_putname(result);
2063f9f0aa6SLinus Torvalds 	return err;
2073f9f0aa6SLinus Torvalds }
2083f9f0aa6SLinus Torvalds 
20991a27b2aSJeff Layton struct filename *
21091a27b2aSJeff Layton getname(const char __user * filename)
211f52e0c11SAl Viro {
212f7493e5dSLinus Torvalds 	return getname_flags(filename, 0, NULL);
213f52e0c11SAl Viro }
214f52e0c11SAl Viro 
215c4ad8f98SLinus Torvalds struct filename *
216c4ad8f98SLinus Torvalds getname_kernel(const char * filename)
217c4ad8f98SLinus Torvalds {
218c4ad8f98SLinus Torvalds 	struct filename *result;
21908518549SPaul Moore 	int len = strlen(filename) + 1;
220c4ad8f98SLinus Torvalds 
221c4ad8f98SLinus Torvalds 	result = __getname();
222c4ad8f98SLinus Torvalds 	if (unlikely(!result))
223c4ad8f98SLinus Torvalds 		return ERR_PTR(-ENOMEM);
224c4ad8f98SLinus Torvalds 
22508518549SPaul Moore 	if (len <= EMBEDDED_NAME_MAX) {
22608518549SPaul Moore 		result->name = (char *)(result) + sizeof(*result);
22708518549SPaul Moore 		result->separate = false;
22808518549SPaul Moore 	} else if (len <= PATH_MAX) {
22908518549SPaul Moore 		struct filename *tmp;
23008518549SPaul Moore 
23108518549SPaul Moore 		tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
23208518549SPaul Moore 		if (unlikely(!tmp)) {
23308518549SPaul Moore 			__putname(result);
23408518549SPaul Moore 			return ERR_PTR(-ENOMEM);
23508518549SPaul Moore 		}
23608518549SPaul Moore 		tmp->name = (char *)result;
23708518549SPaul Moore 		tmp->separate = true;
23808518549SPaul Moore 		result = tmp;
23908518549SPaul Moore 	} else {
24008518549SPaul Moore 		__putname(result);
24108518549SPaul Moore 		return ERR_PTR(-ENAMETOOLONG);
24208518549SPaul Moore 	}
24308518549SPaul Moore 	memcpy((char *)result->name, filename, len);
244c4ad8f98SLinus Torvalds 	result->uptr = NULL;
245c4ad8f98SLinus Torvalds 	result->aname = NULL;
246fd3522fdSPaul Moore 	audit_getname(result);
247c4ad8f98SLinus Torvalds 
248c4ad8f98SLinus Torvalds 	return result;
249c4ad8f98SLinus Torvalds }
250c4ad8f98SLinus Torvalds 
2511da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
25291a27b2aSJeff Layton void putname(struct filename *name)
2531da177e4SLinus Torvalds {
2545ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
25591a27b2aSJeff Layton 		return audit_putname(name);
25691a27b2aSJeff Layton 	final_putname(name);
2571da177e4SLinus Torvalds }
2581da177e4SLinus Torvalds #endif
2591da177e4SLinus Torvalds 
260e77819e5SLinus Torvalds static int check_acl(struct inode *inode, int mask)
261e77819e5SLinus Torvalds {
26284635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL
263e77819e5SLinus Torvalds 	struct posix_acl *acl;
264e77819e5SLinus Torvalds 
265e77819e5SLinus Torvalds 	if (mask & MAY_NOT_BLOCK) {
2663567866bSAl Viro 		acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
2673567866bSAl Viro 	        if (!acl)
268e77819e5SLinus Torvalds 	                return -EAGAIN;
2693567866bSAl Viro 		/* no ->get_acl() calls in RCU mode... */
2703567866bSAl Viro 		if (acl == ACL_NOT_CACHED)
271e77819e5SLinus Torvalds 			return -ECHILD;
272206b1d09SAri Savolainen 	        return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
273e77819e5SLinus Torvalds 	}
274e77819e5SLinus Torvalds 
2752982baa2SChristoph Hellwig 	acl = get_acl(inode, ACL_TYPE_ACCESS);
2764e34e719SChristoph Hellwig 	if (IS_ERR(acl))
2774e34e719SChristoph Hellwig 		return PTR_ERR(acl);
278e77819e5SLinus Torvalds 	if (acl) {
279e77819e5SLinus Torvalds 	        int error = posix_acl_permission(inode, acl, mask);
280e77819e5SLinus Torvalds 	        posix_acl_release(acl);
281e77819e5SLinus Torvalds 	        return error;
282e77819e5SLinus Torvalds 	}
28384635d68SLinus Torvalds #endif
284e77819e5SLinus Torvalds 
285e77819e5SLinus Torvalds 	return -EAGAIN;
286e77819e5SLinus Torvalds }
287e77819e5SLinus Torvalds 
2885909ccaaSLinus Torvalds /*
289948409c7SAndreas Gruenbacher  * This does the basic permission checking
2905909ccaaSLinus Torvalds  */
2917e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask)
2925909ccaaSLinus Torvalds {
29326cf46beSLinus Torvalds 	unsigned int mode = inode->i_mode;
2945909ccaaSLinus Torvalds 
2958e96e3b7SEric W. Biederman 	if (likely(uid_eq(current_fsuid(), inode->i_uid)))
2965909ccaaSLinus Torvalds 		mode >>= 6;
2975909ccaaSLinus Torvalds 	else {
298e77819e5SLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
2997e40145eSAl Viro 			int error = check_acl(inode, mask);
3005909ccaaSLinus Torvalds 			if (error != -EAGAIN)
3015909ccaaSLinus Torvalds 				return error;
3025909ccaaSLinus Torvalds 		}
3035909ccaaSLinus Torvalds 
3045909ccaaSLinus Torvalds 		if (in_group_p(inode->i_gid))
3055909ccaaSLinus Torvalds 			mode >>= 3;
3065909ccaaSLinus Torvalds 	}
3075909ccaaSLinus Torvalds 
3085909ccaaSLinus Torvalds 	/*
3095909ccaaSLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
3105909ccaaSLinus Torvalds 	 */
3119c2c7039SAl Viro 	if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
3125909ccaaSLinus Torvalds 		return 0;
3135909ccaaSLinus Torvalds 	return -EACCES;
3145909ccaaSLinus Torvalds }
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds /**
3171da177e4SLinus Torvalds  * generic_permission -  check for access rights on a Posix-like filesystem
3181da177e4SLinus Torvalds  * @inode:	inode to check access rights for
3198fd90c8dSAndreas Gruenbacher  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
3201da177e4SLinus Torvalds  *
3211da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
3221da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
3231da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
324b74c79e9SNick Piggin  * are used for other things.
325b74c79e9SNick Piggin  *
326b74c79e9SNick Piggin  * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
327b74c79e9SNick Piggin  * request cannot be satisfied (eg. requires blocking or too much complexity).
328b74c79e9SNick Piggin  * It would then be called again in ref-walk mode.
3291da177e4SLinus Torvalds  */
3302830ba7fSAl Viro int generic_permission(struct inode *inode, int mask)
3311da177e4SLinus Torvalds {
3325909ccaaSLinus Torvalds 	int ret;
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	/*
335948409c7SAndreas Gruenbacher 	 * Do the basic permission checks.
3361da177e4SLinus Torvalds 	 */
3377e40145eSAl Viro 	ret = acl_permission_check(inode, mask);
3385909ccaaSLinus Torvalds 	if (ret != -EACCES)
3395909ccaaSLinus Torvalds 		return ret;
3401da177e4SLinus Torvalds 
341d594e7ecSAl Viro 	if (S_ISDIR(inode->i_mode)) {
342d594e7ecSAl Viro 		/* DACs are overridable for directories */
34323adbe12SAndy Lutomirski 		if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
344d594e7ecSAl Viro 			return 0;
345d594e7ecSAl Viro 		if (!(mask & MAY_WRITE))
34623adbe12SAndy Lutomirski 			if (capable_wrt_inode_uidgid(inode,
34723adbe12SAndy Lutomirski 						     CAP_DAC_READ_SEARCH))
348d594e7ecSAl Viro 				return 0;
349d594e7ecSAl Viro 		return -EACCES;
350d594e7ecSAl Viro 	}
3511da177e4SLinus Torvalds 	/*
3521da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
353d594e7ecSAl Viro 	 * Executable DACs are overridable when there is
354d594e7ecSAl Viro 	 * at least one exec bit set.
3551da177e4SLinus Torvalds 	 */
356d594e7ecSAl Viro 	if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
35723adbe12SAndy Lutomirski 		if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
3581da177e4SLinus Torvalds 			return 0;
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	/*
3611da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
3621da177e4SLinus Torvalds 	 */
3637ea66001SSerge E. Hallyn 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
364d594e7ecSAl Viro 	if (mask == MAY_READ)
36523adbe12SAndy Lutomirski 		if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
3661da177e4SLinus Torvalds 			return 0;
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	return -EACCES;
3691da177e4SLinus Torvalds }
3704d359507SAl Viro EXPORT_SYMBOL(generic_permission);
3711da177e4SLinus Torvalds 
3723ddcd056SLinus Torvalds /*
3733ddcd056SLinus Torvalds  * We _really_ want to just do "generic_permission()" without
3743ddcd056SLinus Torvalds  * even looking at the inode->i_op values. So we keep a cache
3753ddcd056SLinus Torvalds  * flag in inode->i_opflags, that says "this has not special
3763ddcd056SLinus Torvalds  * permission function, use the fast case".
3773ddcd056SLinus Torvalds  */
3783ddcd056SLinus Torvalds static inline int do_inode_permission(struct inode *inode, int mask)
3793ddcd056SLinus Torvalds {
3803ddcd056SLinus Torvalds 	if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
3813ddcd056SLinus Torvalds 		if (likely(inode->i_op->permission))
3823ddcd056SLinus Torvalds 			return inode->i_op->permission(inode, mask);
3833ddcd056SLinus Torvalds 
3843ddcd056SLinus Torvalds 		/* This gets set once for the inode lifetime */
3853ddcd056SLinus Torvalds 		spin_lock(&inode->i_lock);
3863ddcd056SLinus Torvalds 		inode->i_opflags |= IOP_FASTPERM;
3873ddcd056SLinus Torvalds 		spin_unlock(&inode->i_lock);
3883ddcd056SLinus Torvalds 	}
3893ddcd056SLinus Torvalds 	return generic_permission(inode, mask);
3903ddcd056SLinus Torvalds }
3913ddcd056SLinus Torvalds 
392cb23beb5SChristoph Hellwig /**
3930bdaea90SDavid Howells  * __inode_permission - Check for access rights to a given inode
3940bdaea90SDavid Howells  * @inode: Inode to check permission on
3950bdaea90SDavid Howells  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
396cb23beb5SChristoph Hellwig  *
3970bdaea90SDavid Howells  * Check for read/write/execute permissions on an inode.
398948409c7SAndreas Gruenbacher  *
399948409c7SAndreas Gruenbacher  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
4000bdaea90SDavid Howells  *
4010bdaea90SDavid Howells  * This does not check for a read-only file system.  You probably want
4020bdaea90SDavid Howells  * inode_permission().
403cb23beb5SChristoph Hellwig  */
4040bdaea90SDavid Howells int __inode_permission(struct inode *inode, int mask)
4051da177e4SLinus Torvalds {
406e6305c43SAl Viro 	int retval;
4071da177e4SLinus Torvalds 
4083ddcd056SLinus Torvalds 	if (unlikely(mask & MAY_WRITE)) {
4091da177e4SLinus Torvalds 		/*
4101da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
4111da177e4SLinus Torvalds 		 */
4121da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
4131da177e4SLinus Torvalds 			return -EACCES;
4141da177e4SLinus Torvalds 	}
4151da177e4SLinus Torvalds 
4163ddcd056SLinus Torvalds 	retval = do_inode_permission(inode, mask);
4171da177e4SLinus Torvalds 	if (retval)
4181da177e4SLinus Torvalds 		return retval;
4191da177e4SLinus Torvalds 
42008ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
42108ce5f16SSerge E. Hallyn 	if (retval)
42208ce5f16SSerge E. Hallyn 		return retval;
42308ce5f16SSerge E. Hallyn 
424d09ca739SEric Paris 	return security_inode_permission(inode, mask);
4251da177e4SLinus Torvalds }
426bd5d0856SMiklos Szeredi EXPORT_SYMBOL(__inode_permission);
4271da177e4SLinus Torvalds 
428f4d6ff89SAl Viro /**
4290bdaea90SDavid Howells  * sb_permission - Check superblock-level permissions
4300bdaea90SDavid Howells  * @sb: Superblock of inode to check permission on
43155852635SRandy Dunlap  * @inode: Inode to check permission on
4320bdaea90SDavid Howells  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
4330bdaea90SDavid Howells  *
4340bdaea90SDavid Howells  * Separate out file-system wide checks from inode-specific permission checks.
4350bdaea90SDavid Howells  */
4360bdaea90SDavid Howells static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
4370bdaea90SDavid Howells {
4380bdaea90SDavid Howells 	if (unlikely(mask & MAY_WRITE)) {
4390bdaea90SDavid Howells 		umode_t mode = inode->i_mode;
4400bdaea90SDavid Howells 
4410bdaea90SDavid Howells 		/* Nobody gets write access to a read-only fs. */
4420bdaea90SDavid Howells 		if ((sb->s_flags & MS_RDONLY) &&
4430bdaea90SDavid Howells 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
4440bdaea90SDavid Howells 			return -EROFS;
4450bdaea90SDavid Howells 	}
4460bdaea90SDavid Howells 	return 0;
4470bdaea90SDavid Howells }
4480bdaea90SDavid Howells 
4490bdaea90SDavid Howells /**
4500bdaea90SDavid Howells  * inode_permission - Check for access rights to a given inode
4510bdaea90SDavid Howells  * @inode: Inode to check permission on
4520bdaea90SDavid Howells  * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
4530bdaea90SDavid Howells  *
4540bdaea90SDavid Howells  * Check for read/write/execute permissions on an inode.  We use fs[ug]id for
4550bdaea90SDavid Howells  * this, letting us set arbitrary permissions for filesystem access without
4560bdaea90SDavid Howells  * changing the "normal" UIDs which are used for other things.
4570bdaea90SDavid Howells  *
4580bdaea90SDavid Howells  * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
4590bdaea90SDavid Howells  */
4600bdaea90SDavid Howells int inode_permission(struct inode *inode, int mask)
4610bdaea90SDavid Howells {
4620bdaea90SDavid Howells 	int retval;
4630bdaea90SDavid Howells 
4640bdaea90SDavid Howells 	retval = sb_permission(inode->i_sb, inode, mask);
4650bdaea90SDavid Howells 	if (retval)
4660bdaea90SDavid Howells 		return retval;
4670bdaea90SDavid Howells 	return __inode_permission(inode, mask);
4680bdaea90SDavid Howells }
4694d359507SAl Viro EXPORT_SYMBOL(inode_permission);
4700bdaea90SDavid Howells 
4710bdaea90SDavid Howells /**
4725dd784d0SJan Blunck  * path_get - get a reference to a path
4735dd784d0SJan Blunck  * @path: path to get the reference to
4745dd784d0SJan Blunck  *
4755dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
4765dd784d0SJan Blunck  */
477dcf787f3SAl Viro void path_get(const struct path *path)
4785dd784d0SJan Blunck {
4795dd784d0SJan Blunck 	mntget(path->mnt);
4805dd784d0SJan Blunck 	dget(path->dentry);
4815dd784d0SJan Blunck }
4825dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
4835dd784d0SJan Blunck 
4845dd784d0SJan Blunck /**
4851d957f9bSJan Blunck  * path_put - put a reference to a path
4861d957f9bSJan Blunck  * @path: path to put the reference to
4871d957f9bSJan Blunck  *
4881d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
4891d957f9bSJan Blunck  */
490dcf787f3SAl Viro void path_put(const struct path *path)
4911da177e4SLinus Torvalds {
4921d957f9bSJan Blunck 	dput(path->dentry);
4931d957f9bSJan Blunck 	mntput(path->mnt);
4941da177e4SLinus Torvalds }
4951d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
4961da177e4SLinus Torvalds 
4971f55a6ecSAl Viro struct nameidata {
4981f55a6ecSAl Viro 	struct path	path;
4991f55a6ecSAl Viro 	struct qstr	last;
5001f55a6ecSAl Viro 	struct path	root;
5011f55a6ecSAl Viro 	struct inode	*inode; /* path.dentry.d_inode */
5021f55a6ecSAl Viro 	unsigned int	flags;
5031f55a6ecSAl Viro 	unsigned	seq, m_seq;
5041f55a6ecSAl Viro 	int		last_type;
5051f55a6ecSAl Viro 	unsigned	depth;
5065e53084dSAl Viro 	struct file	*base;
5071f55a6ecSAl Viro 	char *saved_names[MAX_NESTED_LINKS + 1];
5081f55a6ecSAl Viro };
5091f55a6ecSAl Viro 
51019660af7SAl Viro /*
51131e6b01fSNick Piggin  * Path walking has 2 modes, rcu-walk and ref-walk (see
51219660af7SAl Viro  * Documentation/filesystems/path-lookup.txt).  In situations when we can't
51319660af7SAl Viro  * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
51419660af7SAl Viro  * normal reference counts on dentries and vfsmounts to transition to rcu-walk
51519660af7SAl Viro  * mode.  Refcounts are grabbed at the last known good point before rcu-walk
51619660af7SAl Viro  * got stuck, so ref-walk may continue from there. If this is not successful
51719660af7SAl Viro  * (eg. a seqcount has changed), then failure is returned and it's up to caller
51819660af7SAl Viro  * to restart the path walk from the beginning in ref-walk mode.
51931e6b01fSNick Piggin  */
52031e6b01fSNick Piggin 
52131e6b01fSNick Piggin /**
52219660af7SAl Viro  * unlazy_walk - try to switch to ref-walk mode.
52319660af7SAl Viro  * @nd: nameidata pathwalk data
52419660af7SAl Viro  * @dentry: child of nd->path.dentry or NULL
52539191628SRandy Dunlap  * Returns: 0 on success, -ECHILD on failure
52631e6b01fSNick Piggin  *
52719660af7SAl Viro  * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
52819660af7SAl Viro  * for ref-walk mode.  @dentry must be a path found by a do_lookup call on
52919660af7SAl Viro  * @nd or NULL.  Must be called from rcu-walk context.
53031e6b01fSNick Piggin  */
53119660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
53231e6b01fSNick Piggin {
53331e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
53431e6b01fSNick Piggin 	struct dentry *parent = nd->path.dentry;
53531e6b01fSNick Piggin 
53631e6b01fSNick Piggin 	BUG_ON(!(nd->flags & LOOKUP_RCU));
537e5c832d5SLinus Torvalds 
538e5c832d5SLinus Torvalds 	/*
53948a066e7SAl Viro 	 * After legitimizing the bastards, terminate_walk()
54048a066e7SAl Viro 	 * will do the right thing for non-RCU mode, and all our
54148a066e7SAl Viro 	 * subsequent exit cases should rcu_read_unlock()
54248a066e7SAl Viro 	 * before returning.  Do vfsmount first; if dentry
54348a066e7SAl Viro 	 * can't be legitimized, just set nd->path.dentry to NULL
54448a066e7SAl Viro 	 * and rely on dput(NULL) being a no-op.
545e5c832d5SLinus Torvalds 	 */
54648a066e7SAl Viro 	if (!legitimize_mnt(nd->path.mnt, nd->m_seq))
547e5c832d5SLinus Torvalds 		return -ECHILD;
548e5c832d5SLinus Torvalds 	nd->flags &= ~LOOKUP_RCU;
54915570086SLinus Torvalds 
55048a066e7SAl Viro 	if (!lockref_get_not_dead(&parent->d_lockref)) {
55148a066e7SAl Viro 		nd->path.dentry = NULL;
552d870b4a1SAl Viro 		goto out;
55348a066e7SAl Viro 	}
55448a066e7SAl Viro 
55515570086SLinus Torvalds 	/*
55615570086SLinus Torvalds 	 * For a negative lookup, the lookup sequence point is the parents
55715570086SLinus Torvalds 	 * sequence point, and it only needs to revalidate the parent dentry.
55815570086SLinus Torvalds 	 *
55915570086SLinus Torvalds 	 * For a positive lookup, we need to move both the parent and the
56015570086SLinus Torvalds 	 * dentry from the RCU domain to be properly refcounted. And the
56115570086SLinus Torvalds 	 * sequence number in the dentry validates *both* dentry counters,
56215570086SLinus Torvalds 	 * since we checked the sequence number of the parent after we got
56315570086SLinus Torvalds 	 * the child sequence number. So we know the parent must still
56415570086SLinus Torvalds 	 * be valid if the child sequence number is still valid.
56515570086SLinus Torvalds 	 */
56619660af7SAl Viro 	if (!dentry) {
567e5c832d5SLinus Torvalds 		if (read_seqcount_retry(&parent->d_seq, nd->seq))
568e5c832d5SLinus Torvalds 			goto out;
56919660af7SAl Viro 		BUG_ON(nd->inode != parent->d_inode);
57019660af7SAl Viro 	} else {
571e5c832d5SLinus Torvalds 		if (!lockref_get_not_dead(&dentry->d_lockref))
572e5c832d5SLinus Torvalds 			goto out;
573e5c832d5SLinus Torvalds 		if (read_seqcount_retry(&dentry->d_seq, nd->seq))
574e5c832d5SLinus Torvalds 			goto drop_dentry;
57519660af7SAl Viro 	}
576e5c832d5SLinus Torvalds 
577e5c832d5SLinus Torvalds 	/*
578e5c832d5SLinus Torvalds 	 * Sequence counts matched. Now make sure that the root is
579e5c832d5SLinus Torvalds 	 * still valid and get it if required.
580e5c832d5SLinus Torvalds 	 */
581e5c832d5SLinus Torvalds 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
582e5c832d5SLinus Torvalds 		spin_lock(&fs->lock);
583e5c832d5SLinus Torvalds 		if (nd->root.mnt != fs->root.mnt || nd->root.dentry != fs->root.dentry)
584e5c832d5SLinus Torvalds 			goto unlock_and_drop_dentry;
58531e6b01fSNick Piggin 		path_get(&nd->root);
58631e6b01fSNick Piggin 		spin_unlock(&fs->lock);
58731e6b01fSNick Piggin 	}
58831e6b01fSNick Piggin 
5898b61e74fSAl Viro 	rcu_read_unlock();
59031e6b01fSNick Piggin 	return 0;
59119660af7SAl Viro 
592e5c832d5SLinus Torvalds unlock_and_drop_dentry:
59331e6b01fSNick Piggin 	spin_unlock(&fs->lock);
594e5c832d5SLinus Torvalds drop_dentry:
5958b61e74fSAl Viro 	rcu_read_unlock();
596e5c832d5SLinus Torvalds 	dput(dentry);
597d0d27277SLinus Torvalds 	goto drop_root_mnt;
598e5c832d5SLinus Torvalds out:
5998b61e74fSAl Viro 	rcu_read_unlock();
600d0d27277SLinus Torvalds drop_root_mnt:
601d0d27277SLinus Torvalds 	if (!(nd->flags & LOOKUP_ROOT))
602d0d27277SLinus Torvalds 		nd->root.mnt = NULL;
60331e6b01fSNick Piggin 	return -ECHILD;
60431e6b01fSNick Piggin }
60531e6b01fSNick Piggin 
6064ce16ef3SAl Viro static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
60734286d66SNick Piggin {
6084ce16ef3SAl Viro 	return dentry->d_op->d_revalidate(dentry, flags);
60934286d66SNick Piggin }
61034286d66SNick Piggin 
6119f1fafeeSAl Viro /**
6129f1fafeeSAl Viro  * complete_walk - successful completion of path walk
6139f1fafeeSAl Viro  * @nd:  pointer nameidata
61439159de2SJeff Layton  *
6159f1fafeeSAl Viro  * If we had been in RCU mode, drop out of it and legitimize nd->path.
6169f1fafeeSAl Viro  * Revalidate the final result, unless we'd already done that during
6179f1fafeeSAl Viro  * the path walk or the filesystem doesn't ask for it.  Return 0 on
6189f1fafeeSAl Viro  * success, -error on failure.  In case of failure caller does not
6199f1fafeeSAl Viro  * need to drop nd->path.
62039159de2SJeff Layton  */
6219f1fafeeSAl Viro static int complete_walk(struct nameidata *nd)
62239159de2SJeff Layton {
62316c2cd71SAl Viro 	struct dentry *dentry = nd->path.dentry;
62439159de2SJeff Layton 	int status;
62539159de2SJeff Layton 
6269f1fafeeSAl Viro 	if (nd->flags & LOOKUP_RCU) {
6279f1fafeeSAl Viro 		nd->flags &= ~LOOKUP_RCU;
6289f1fafeeSAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
6299f1fafeeSAl Viro 			nd->root.mnt = NULL;
63015570086SLinus Torvalds 
63148a066e7SAl Viro 		if (!legitimize_mnt(nd->path.mnt, nd->m_seq)) {
6328b61e74fSAl Viro 			rcu_read_unlock();
63348a066e7SAl Viro 			return -ECHILD;
63448a066e7SAl Viro 		}
635e5c832d5SLinus Torvalds 		if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) {
6368b61e74fSAl Viro 			rcu_read_unlock();
63748a066e7SAl Viro 			mntput(nd->path.mnt);
6389f1fafeeSAl Viro 			return -ECHILD;
6399f1fafeeSAl Viro 		}
640e5c832d5SLinus Torvalds 		if (read_seqcount_retry(&dentry->d_seq, nd->seq)) {
6418b61e74fSAl Viro 			rcu_read_unlock();
642e5c832d5SLinus Torvalds 			dput(dentry);
64348a066e7SAl Viro 			mntput(nd->path.mnt);
644e5c832d5SLinus Torvalds 			return -ECHILD;
645e5c832d5SLinus Torvalds 		}
6468b61e74fSAl Viro 		rcu_read_unlock();
6479f1fafeeSAl Viro 	}
6489f1fafeeSAl Viro 
64916c2cd71SAl Viro 	if (likely(!(nd->flags & LOOKUP_JUMPED)))
65039159de2SJeff Layton 		return 0;
65139159de2SJeff Layton 
652ecf3d1f1SJeff Layton 	if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
65316c2cd71SAl Viro 		return 0;
65416c2cd71SAl Viro 
655ecf3d1f1SJeff Layton 	status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
65639159de2SJeff Layton 	if (status > 0)
65739159de2SJeff Layton 		return 0;
65839159de2SJeff Layton 
65916c2cd71SAl Viro 	if (!status)
66039159de2SJeff Layton 		status = -ESTALE;
66116c2cd71SAl Viro 
6629f1fafeeSAl Viro 	path_put(&nd->path);
66339159de2SJeff Layton 	return status;
66439159de2SJeff Layton }
66539159de2SJeff Layton 
6662a737871SAl Viro static __always_inline void set_root(struct nameidata *nd)
6672a737871SAl Viro {
668f7ad3c6bSMiklos Szeredi 	get_fs_root(current->fs, &nd->root);
6692a737871SAl Viro }
6702a737871SAl Viro 
6716de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *);
6726de88d72SAl Viro 
6737bd88377SAl Viro static __always_inline unsigned set_root_rcu(struct nameidata *nd)
67431e6b01fSNick Piggin {
67531e6b01fSNick Piggin 	struct fs_struct *fs = current->fs;
6767bd88377SAl Viro 	unsigned seq, res;
677c28cc364SNick Piggin 
678c28cc364SNick Piggin 	do {
679c28cc364SNick Piggin 		seq = read_seqcount_begin(&fs->seq);
68031e6b01fSNick Piggin 		nd->root = fs->root;
6817bd88377SAl Viro 		res = __read_seqcount_begin(&nd->root.dentry->d_seq);
682c28cc364SNick Piggin 	} while (read_seqcount_retry(&fs->seq, seq));
6837bd88377SAl Viro 	return res;
68431e6b01fSNick Piggin }
68531e6b01fSNick Piggin 
6861d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
687051d3812SIan Kent {
688051d3812SIan Kent 	dput(path->dentry);
6894ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
690051d3812SIan Kent 		mntput(path->mnt);
691051d3812SIan Kent }
692051d3812SIan Kent 
6937b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path,
6947b9337aaSNick Piggin 					struct nameidata *nd)
695051d3812SIan Kent {
69631e6b01fSNick Piggin 	if (!(nd->flags & LOOKUP_RCU)) {
6974ac91378SJan Blunck 		dput(nd->path.dentry);
69831e6b01fSNick Piggin 		if (nd->path.mnt != path->mnt)
6994ac91378SJan Blunck 			mntput(nd->path.mnt);
7009a229683SHuang Shijie 	}
70131e6b01fSNick Piggin 	nd->path.mnt = path->mnt;
7024ac91378SJan Blunck 	nd->path.dentry = path->dentry;
703051d3812SIan Kent }
704051d3812SIan Kent 
705b5fb63c1SChristoph Hellwig /*
706b5fb63c1SChristoph Hellwig  * Helper to directly jump to a known parsed path from ->follow_link,
707b5fb63c1SChristoph Hellwig  * caller must have taken a reference to path beforehand.
708b5fb63c1SChristoph Hellwig  */
709b5fb63c1SChristoph Hellwig void nd_jump_link(struct nameidata *nd, struct path *path)
710b5fb63c1SChristoph Hellwig {
711b5fb63c1SChristoph Hellwig 	path_put(&nd->path);
712b5fb63c1SChristoph Hellwig 
713b5fb63c1SChristoph Hellwig 	nd->path = *path;
714b5fb63c1SChristoph Hellwig 	nd->inode = nd->path.dentry->d_inode;
715b5fb63c1SChristoph Hellwig 	nd->flags |= LOOKUP_JUMPED;
716b5fb63c1SChristoph Hellwig }
717b5fb63c1SChristoph Hellwig 
7181f55a6ecSAl Viro void nd_set_link(struct nameidata *nd, char *path)
7191f55a6ecSAl Viro {
7201f55a6ecSAl Viro 	nd->saved_names[nd->depth] = path;
7211f55a6ecSAl Viro }
7221f55a6ecSAl Viro EXPORT_SYMBOL(nd_set_link);
7231f55a6ecSAl Viro 
7241f55a6ecSAl Viro char *nd_get_link(struct nameidata *nd)
7251f55a6ecSAl Viro {
7261f55a6ecSAl Viro 	return nd->saved_names[nd->depth];
7271f55a6ecSAl Viro }
7281f55a6ecSAl Viro EXPORT_SYMBOL(nd_get_link);
7291f55a6ecSAl Viro 
730574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
731574197e0SAl Viro {
732574197e0SAl Viro 	struct inode *inode = link->dentry->d_inode;
7336d7b5aaeSAl Viro 	if (inode->i_op->put_link)
734574197e0SAl Viro 		inode->i_op->put_link(link->dentry, nd, cookie);
735574197e0SAl Viro 	path_put(link);
736574197e0SAl Viro }
737574197e0SAl Viro 
738561ec64aSLinus Torvalds int sysctl_protected_symlinks __read_mostly = 0;
739561ec64aSLinus Torvalds int sysctl_protected_hardlinks __read_mostly = 0;
740800179c9SKees Cook 
741800179c9SKees Cook /**
742800179c9SKees Cook  * may_follow_link - Check symlink following for unsafe situations
743800179c9SKees Cook  * @link: The path of the symlink
74455852635SRandy Dunlap  * @nd: nameidata pathwalk data
745800179c9SKees Cook  *
746800179c9SKees Cook  * In the case of the sysctl_protected_symlinks sysctl being enabled,
747800179c9SKees Cook  * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
748800179c9SKees Cook  * in a sticky world-writable directory. This is to protect privileged
749800179c9SKees Cook  * processes from failing races against path names that may change out
750800179c9SKees Cook  * from under them by way of other users creating malicious symlinks.
751800179c9SKees Cook  * It will permit symlinks to be followed only when outside a sticky
752800179c9SKees Cook  * world-writable directory, or when the uid of the symlink and follower
753800179c9SKees Cook  * match, or when the directory owner matches the symlink's owner.
754800179c9SKees Cook  *
755800179c9SKees Cook  * Returns 0 if following the symlink is allowed, -ve on error.
756800179c9SKees Cook  */
757800179c9SKees Cook static inline int may_follow_link(struct path *link, struct nameidata *nd)
758800179c9SKees Cook {
759800179c9SKees Cook 	const struct inode *inode;
760800179c9SKees Cook 	const struct inode *parent;
761800179c9SKees Cook 
762800179c9SKees Cook 	if (!sysctl_protected_symlinks)
763800179c9SKees Cook 		return 0;
764800179c9SKees Cook 
765800179c9SKees Cook 	/* Allowed if owner and follower match. */
766800179c9SKees Cook 	inode = link->dentry->d_inode;
76781abe27bSEric W. Biederman 	if (uid_eq(current_cred()->fsuid, inode->i_uid))
768800179c9SKees Cook 		return 0;
769800179c9SKees Cook 
770800179c9SKees Cook 	/* Allowed if parent directory not sticky and world-writable. */
771800179c9SKees Cook 	parent = nd->path.dentry->d_inode;
772800179c9SKees Cook 	if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
773800179c9SKees Cook 		return 0;
774800179c9SKees Cook 
775800179c9SKees Cook 	/* Allowed if parent directory and link owner match. */
77681abe27bSEric W. Biederman 	if (uid_eq(parent->i_uid, inode->i_uid))
777800179c9SKees Cook 		return 0;
778800179c9SKees Cook 
779ffd8d101SSasha Levin 	audit_log_link_denied("follow_link", link);
780800179c9SKees Cook 	path_put_conditional(link, nd);
781800179c9SKees Cook 	path_put(&nd->path);
782800179c9SKees Cook 	return -EACCES;
783800179c9SKees Cook }
784800179c9SKees Cook 
785800179c9SKees Cook /**
786800179c9SKees Cook  * safe_hardlink_source - Check for safe hardlink conditions
787800179c9SKees Cook  * @inode: the source inode to hardlink from
788800179c9SKees Cook  *
789800179c9SKees Cook  * Return false if at least one of the following conditions:
790800179c9SKees Cook  *    - inode is not a regular file
791800179c9SKees Cook  *    - inode is setuid
792800179c9SKees Cook  *    - inode is setgid and group-exec
793800179c9SKees Cook  *    - access failure for read and write
794800179c9SKees Cook  *
795800179c9SKees Cook  * Otherwise returns true.
796800179c9SKees Cook  */
797800179c9SKees Cook static bool safe_hardlink_source(struct inode *inode)
798800179c9SKees Cook {
799800179c9SKees Cook 	umode_t mode = inode->i_mode;
800800179c9SKees Cook 
801800179c9SKees Cook 	/* Special files should not get pinned to the filesystem. */
802800179c9SKees Cook 	if (!S_ISREG(mode))
803800179c9SKees Cook 		return false;
804800179c9SKees Cook 
805800179c9SKees Cook 	/* Setuid files should not get pinned to the filesystem. */
806800179c9SKees Cook 	if (mode & S_ISUID)
807800179c9SKees Cook 		return false;
808800179c9SKees Cook 
809800179c9SKees Cook 	/* Executable setgid files should not get pinned to the filesystem. */
810800179c9SKees Cook 	if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
811800179c9SKees Cook 		return false;
812800179c9SKees Cook 
813800179c9SKees Cook 	/* Hardlinking to unreadable or unwritable sources is dangerous. */
814800179c9SKees Cook 	if (inode_permission(inode, MAY_READ | MAY_WRITE))
815800179c9SKees Cook 		return false;
816800179c9SKees Cook 
817800179c9SKees Cook 	return true;
818800179c9SKees Cook }
819800179c9SKees Cook 
820800179c9SKees Cook /**
821800179c9SKees Cook  * may_linkat - Check permissions for creating a hardlink
822800179c9SKees Cook  * @link: the source to hardlink from
823800179c9SKees Cook  *
824800179c9SKees Cook  * Block hardlink when all of:
825800179c9SKees Cook  *  - sysctl_protected_hardlinks enabled
826800179c9SKees Cook  *  - fsuid does not match inode
827800179c9SKees Cook  *  - hardlink source is unsafe (see safe_hardlink_source() above)
828800179c9SKees Cook  *  - not CAP_FOWNER
829800179c9SKees Cook  *
830800179c9SKees Cook  * Returns 0 if successful, -ve on error.
831800179c9SKees Cook  */
832800179c9SKees Cook static int may_linkat(struct path *link)
833800179c9SKees Cook {
834800179c9SKees Cook 	const struct cred *cred;
835800179c9SKees Cook 	struct inode *inode;
836800179c9SKees Cook 
837800179c9SKees Cook 	if (!sysctl_protected_hardlinks)
838800179c9SKees Cook 		return 0;
839800179c9SKees Cook 
840800179c9SKees Cook 	cred = current_cred();
841800179c9SKees Cook 	inode = link->dentry->d_inode;
842800179c9SKees Cook 
843800179c9SKees Cook 	/* Source inode owner (or CAP_FOWNER) can hardlink all they like,
844800179c9SKees Cook 	 * otherwise, it must be a safe source.
845800179c9SKees Cook 	 */
84681abe27bSEric W. Biederman 	if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
847800179c9SKees Cook 	    capable(CAP_FOWNER))
848800179c9SKees Cook 		return 0;
849800179c9SKees Cook 
850a51d9eaaSKees Cook 	audit_log_link_denied("linkat", link);
851800179c9SKees Cook 	return -EPERM;
852800179c9SKees Cook }
853800179c9SKees Cook 
854def4af30SAl Viro static __always_inline int
855574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p)
8561da177e4SLinus Torvalds {
8577b9337aaSNick Piggin 	struct dentry *dentry = link->dentry;
8586d7b5aaeSAl Viro 	int error;
8596d7b5aaeSAl Viro 	char *s;
8601da177e4SLinus Torvalds 
861844a3917SAl Viro 	BUG_ON(nd->flags & LOOKUP_RCU);
862844a3917SAl Viro 
8630e794589SAl Viro 	if (link->mnt == nd->path.mnt)
8640e794589SAl Viro 		mntget(link->mnt);
8650e794589SAl Viro 
8666d7b5aaeSAl Viro 	error = -ELOOP;
8676d7b5aaeSAl Viro 	if (unlikely(current->total_link_count >= 40))
8686d7b5aaeSAl Viro 		goto out_put_nd_path;
8696d7b5aaeSAl Viro 
870574197e0SAl Viro 	cond_resched();
871574197e0SAl Viro 	current->total_link_count++;
872574197e0SAl Viro 
87368ac1234SAl Viro 	touch_atime(link);
8741da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
875cd4e91d3SAl Viro 
87636f3b4f6SAl Viro 	error = security_inode_follow_link(link->dentry, nd);
8776d7b5aaeSAl Viro 	if (error)
8786d7b5aaeSAl Viro 		goto out_put_nd_path;
87936f3b4f6SAl Viro 
88086acdca1SAl Viro 	nd->last_type = LAST_BIND;
881def4af30SAl Viro 	*p = dentry->d_inode->i_op->follow_link(dentry, nd);
882def4af30SAl Viro 	error = PTR_ERR(*p);
8836d7b5aaeSAl Viro 	if (IS_ERR(*p))
884408ef013SChristoph Hellwig 		goto out_put_nd_path;
8856d7b5aaeSAl Viro 
886cc314eefSLinus Torvalds 	error = 0;
8876d7b5aaeSAl Viro 	s = nd_get_link(nd);
8886d7b5aaeSAl Viro 	if (s) {
889443ed254SAl Viro 		if (unlikely(IS_ERR(s))) {
890443ed254SAl Viro 			path_put(&nd->path);
891443ed254SAl Viro 			put_link(nd, link, *p);
892443ed254SAl Viro 			return PTR_ERR(s);
893443ed254SAl Viro 		}
894443ed254SAl Viro 		if (*s == '/') {
8957bd88377SAl Viro 			if (!nd->root.mnt)
896443ed254SAl Viro 				set_root(nd);
897443ed254SAl Viro 			path_put(&nd->path);
898443ed254SAl Viro 			nd->path = nd->root;
899443ed254SAl Viro 			path_get(&nd->root);
900443ed254SAl Viro 			nd->flags |= LOOKUP_JUMPED;
901443ed254SAl Viro 		}
902443ed254SAl Viro 		nd->inode = nd->path.dentry->d_inode;
903443ed254SAl Viro 		error = link_path_walk(s, nd);
9046d7b5aaeSAl Viro 		if (unlikely(error))
9056d7b5aaeSAl Viro 			put_link(nd, link, *p);
906b5fb63c1SChristoph Hellwig 	}
9076d7b5aaeSAl Viro 
9086d7b5aaeSAl Viro 	return error;
9096d7b5aaeSAl Viro 
9106d7b5aaeSAl Viro out_put_nd_path:
91198f6ef64SArnd Bergmann 	*p = NULL;
9126d7b5aaeSAl Viro 	path_put(&nd->path);
9136d7b5aaeSAl Viro 	path_put(link);
9141da177e4SLinus Torvalds 	return error;
9151da177e4SLinus Torvalds }
9161da177e4SLinus Torvalds 
91731e6b01fSNick Piggin static int follow_up_rcu(struct path *path)
91831e6b01fSNick Piggin {
9190714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
9200714a533SAl Viro 	struct mount *parent;
92131e6b01fSNick Piggin 	struct dentry *mountpoint;
92231e6b01fSNick Piggin 
9230714a533SAl Viro 	parent = mnt->mnt_parent;
9240714a533SAl Viro 	if (&parent->mnt == path->mnt)
92531e6b01fSNick Piggin 		return 0;
926a73324daSAl Viro 	mountpoint = mnt->mnt_mountpoint;
92731e6b01fSNick Piggin 	path->dentry = mountpoint;
9280714a533SAl Viro 	path->mnt = &parent->mnt;
92931e6b01fSNick Piggin 	return 1;
93031e6b01fSNick Piggin }
93131e6b01fSNick Piggin 
932f015f126SDavid Howells /*
933f015f126SDavid Howells  * follow_up - Find the mountpoint of path's vfsmount
934f015f126SDavid Howells  *
935f015f126SDavid Howells  * Given a path, find the mountpoint of its source file system.
936f015f126SDavid Howells  * Replace @path with the path of the mountpoint in the parent mount.
937f015f126SDavid Howells  * Up is towards /.
938f015f126SDavid Howells  *
939f015f126SDavid Howells  * Return 1 if we went up a level and 0 if we were already at the
940f015f126SDavid Howells  * root.
941f015f126SDavid Howells  */
942bab77ebfSAl Viro int follow_up(struct path *path)
9431da177e4SLinus Torvalds {
9440714a533SAl Viro 	struct mount *mnt = real_mount(path->mnt);
9450714a533SAl Viro 	struct mount *parent;
9461da177e4SLinus Torvalds 	struct dentry *mountpoint;
94799b7db7bSNick Piggin 
94848a066e7SAl Viro 	read_seqlock_excl(&mount_lock);
9490714a533SAl Viro 	parent = mnt->mnt_parent;
9503c0a6163SAl Viro 	if (parent == mnt) {
95148a066e7SAl Viro 		read_sequnlock_excl(&mount_lock);
9521da177e4SLinus Torvalds 		return 0;
9531da177e4SLinus Torvalds 	}
9540714a533SAl Viro 	mntget(&parent->mnt);
955a73324daSAl Viro 	mountpoint = dget(mnt->mnt_mountpoint);
95648a066e7SAl Viro 	read_sequnlock_excl(&mount_lock);
957bab77ebfSAl Viro 	dput(path->dentry);
958bab77ebfSAl Viro 	path->dentry = mountpoint;
959bab77ebfSAl Viro 	mntput(path->mnt);
9600714a533SAl Viro 	path->mnt = &parent->mnt;
9611da177e4SLinus Torvalds 	return 1;
9621da177e4SLinus Torvalds }
9634d359507SAl Viro EXPORT_SYMBOL(follow_up);
9641da177e4SLinus Torvalds 
965b5c84bf6SNick Piggin /*
9669875cf80SDavid Howells  * Perform an automount
9679875cf80SDavid Howells  * - return -EISDIR to tell follow_managed() to stop and return the path we
9689875cf80SDavid Howells  *   were called with.
9691da177e4SLinus Torvalds  */
9709875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags,
9719875cf80SDavid Howells 			    bool *need_mntput)
97231e6b01fSNick Piggin {
9739875cf80SDavid Howells 	struct vfsmount *mnt;
974ea5b778aSDavid Howells 	int err;
9759875cf80SDavid Howells 
9769875cf80SDavid Howells 	if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
9779875cf80SDavid Howells 		return -EREMOTE;
9789875cf80SDavid Howells 
9790ec26fd0SMiklos Szeredi 	/* We don't want to mount if someone's just doing a stat -
9800ec26fd0SMiklos Szeredi 	 * unless they're stat'ing a directory and appended a '/' to
9810ec26fd0SMiklos Szeredi 	 * the name.
9820ec26fd0SMiklos Szeredi 	 *
9830ec26fd0SMiklos Szeredi 	 * We do, however, want to mount if someone wants to open or
9845a30d8a2SDavid Howells 	 * create a file of any type under the mountpoint, wants to
9850ec26fd0SMiklos Szeredi 	 * traverse through the mountpoint or wants to open the
9860ec26fd0SMiklos Szeredi 	 * mounted directory.  Also, autofs may mark negative dentries
9870ec26fd0SMiklos Szeredi 	 * as being automount points.  These will need the attentions
9880ec26fd0SMiklos Szeredi 	 * of the daemon to instantiate them before they can be used.
9895a30d8a2SDavid Howells 	 */
9905a30d8a2SDavid Howells 	if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
991d94c177bSLinus Torvalds 		     LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
9925a30d8a2SDavid Howells 	    path->dentry->d_inode)
9939875cf80SDavid Howells 		return -EISDIR;
9940ec26fd0SMiklos Szeredi 
9959875cf80SDavid Howells 	current->total_link_count++;
9969875cf80SDavid Howells 	if (current->total_link_count >= 40)
9979875cf80SDavid Howells 		return -ELOOP;
9989875cf80SDavid Howells 
9999875cf80SDavid Howells 	mnt = path->dentry->d_op->d_automount(path);
10009875cf80SDavid Howells 	if (IS_ERR(mnt)) {
10019875cf80SDavid Howells 		/*
10029875cf80SDavid Howells 		 * The filesystem is allowed to return -EISDIR here to indicate
10039875cf80SDavid Howells 		 * it doesn't want to automount.  For instance, autofs would do
10049875cf80SDavid Howells 		 * this so that its userspace daemon can mount on this dentry.
10059875cf80SDavid Howells 		 *
10069875cf80SDavid Howells 		 * However, we can only permit this if it's a terminal point in
10079875cf80SDavid Howells 		 * the path being looked up; if it wasn't then the remainder of
10089875cf80SDavid Howells 		 * the path is inaccessible and we should say so.
10099875cf80SDavid Howells 		 */
101049084c3bSAl Viro 		if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
10119875cf80SDavid Howells 			return -EREMOTE;
10129875cf80SDavid Howells 		return PTR_ERR(mnt);
101331e6b01fSNick Piggin 	}
1014ea5b778aSDavid Howells 
10159875cf80SDavid Howells 	if (!mnt) /* mount collision */
10169875cf80SDavid Howells 		return 0;
10179875cf80SDavid Howells 
10188aef1884SAl Viro 	if (!*need_mntput) {
10198aef1884SAl Viro 		/* lock_mount() may release path->mnt on error */
10208aef1884SAl Viro 		mntget(path->mnt);
10218aef1884SAl Viro 		*need_mntput = true;
10228aef1884SAl Viro 	}
102319a167afSAl Viro 	err = finish_automount(mnt, path);
1024ea5b778aSDavid Howells 
1025ea5b778aSDavid Howells 	switch (err) {
1026ea5b778aSDavid Howells 	case -EBUSY:
1027ea5b778aSDavid Howells 		/* Someone else made a mount here whilst we were busy */
102819a167afSAl Viro 		return 0;
1029ea5b778aSDavid Howells 	case 0:
10308aef1884SAl Viro 		path_put(path);
10319875cf80SDavid Howells 		path->mnt = mnt;
10329875cf80SDavid Howells 		path->dentry = dget(mnt->mnt_root);
10339875cf80SDavid Howells 		return 0;
103419a167afSAl Viro 	default:
103519a167afSAl Viro 		return err;
10369875cf80SDavid Howells 	}
103719a167afSAl Viro 
1038ea5b778aSDavid Howells }
10399875cf80SDavid Howells 
10409875cf80SDavid Howells /*
10419875cf80SDavid Howells  * Handle a dentry that is managed in some way.
1042cc53ce53SDavid Howells  * - Flagged for transit management (autofs)
10439875cf80SDavid Howells  * - Flagged as mountpoint
10449875cf80SDavid Howells  * - Flagged as automount point
10459875cf80SDavid Howells  *
10469875cf80SDavid Howells  * This may only be called in refwalk mode.
10479875cf80SDavid Howells  *
10489875cf80SDavid Howells  * Serialization is taken care of in namespace.c
10499875cf80SDavid Howells  */
10509875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags)
10519875cf80SDavid Howells {
10528aef1884SAl Viro 	struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
10539875cf80SDavid Howells 	unsigned managed;
10549875cf80SDavid Howells 	bool need_mntput = false;
10558aef1884SAl Viro 	int ret = 0;
10569875cf80SDavid Howells 
10579875cf80SDavid Howells 	/* Given that we're not holding a lock here, we retain the value in a
10589875cf80SDavid Howells 	 * local variable for each dentry as we look at it so that we don't see
10599875cf80SDavid Howells 	 * the components of that value change under us */
10609875cf80SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
10619875cf80SDavid Howells 	       managed &= DCACHE_MANAGED_DENTRY,
10629875cf80SDavid Howells 	       unlikely(managed != 0)) {
1063cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1064cc53ce53SDavid Howells 		 * being held. */
1065cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
1066cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1067cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
10681aed3e42SAl Viro 			ret = path->dentry->d_op->d_manage(path->dentry, false);
1069cc53ce53SDavid Howells 			if (ret < 0)
10708aef1884SAl Viro 				break;
1071cc53ce53SDavid Howells 		}
1072cc53ce53SDavid Howells 
10739875cf80SDavid Howells 		/* Transit to a mounted filesystem. */
10749875cf80SDavid Howells 		if (managed & DCACHE_MOUNTED) {
10759875cf80SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
10769875cf80SDavid Howells 			if (mounted) {
10779875cf80SDavid Howells 				dput(path->dentry);
10789875cf80SDavid Howells 				if (need_mntput)
1079463ffb2eSAl Viro 					mntput(path->mnt);
1080463ffb2eSAl Viro 				path->mnt = mounted;
1081463ffb2eSAl Viro 				path->dentry = dget(mounted->mnt_root);
10829875cf80SDavid Howells 				need_mntput = true;
10839875cf80SDavid Howells 				continue;
1084463ffb2eSAl Viro 			}
1085463ffb2eSAl Viro 
10869875cf80SDavid Howells 			/* Something is mounted on this dentry in another
10879875cf80SDavid Howells 			 * namespace and/or whatever was mounted there in this
108848a066e7SAl Viro 			 * namespace got unmounted before lookup_mnt() could
108948a066e7SAl Viro 			 * get it */
10901da177e4SLinus Torvalds 		}
10919875cf80SDavid Howells 
10929875cf80SDavid Howells 		/* Handle an automount point */
10939875cf80SDavid Howells 		if (managed & DCACHE_NEED_AUTOMOUNT) {
10949875cf80SDavid Howells 			ret = follow_automount(path, flags, &need_mntput);
10959875cf80SDavid Howells 			if (ret < 0)
10968aef1884SAl Viro 				break;
10979875cf80SDavid Howells 			continue;
10989875cf80SDavid Howells 		}
10999875cf80SDavid Howells 
11009875cf80SDavid Howells 		/* We didn't change the current path point */
11019875cf80SDavid Howells 		break;
11029875cf80SDavid Howells 	}
11038aef1884SAl Viro 
11048aef1884SAl Viro 	if (need_mntput && path->mnt == mnt)
11058aef1884SAl Viro 		mntput(path->mnt);
11068aef1884SAl Viro 	if (ret == -EISDIR)
11078aef1884SAl Viro 		ret = 0;
1108a3fbbde7SAl Viro 	return ret < 0 ? ret : need_mntput;
11091da177e4SLinus Torvalds }
11101da177e4SLinus Torvalds 
1111cc53ce53SDavid Howells int follow_down_one(struct path *path)
11121da177e4SLinus Torvalds {
11131da177e4SLinus Torvalds 	struct vfsmount *mounted;
11141da177e4SLinus Torvalds 
11151c755af4SAl Viro 	mounted = lookup_mnt(path);
11161da177e4SLinus Torvalds 	if (mounted) {
11179393bd07SAl Viro 		dput(path->dentry);
11189393bd07SAl Viro 		mntput(path->mnt);
11199393bd07SAl Viro 		path->mnt = mounted;
11209393bd07SAl Viro 		path->dentry = dget(mounted->mnt_root);
11211da177e4SLinus Torvalds 		return 1;
11221da177e4SLinus Torvalds 	}
11231da177e4SLinus Torvalds 	return 0;
11241da177e4SLinus Torvalds }
11254d359507SAl Viro EXPORT_SYMBOL(follow_down_one);
11261da177e4SLinus Torvalds 
1127b8faf035SNeilBrown static inline int managed_dentry_rcu(struct dentry *dentry)
112862a7375eSIan Kent {
1129b8faf035SNeilBrown 	return (dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
1130b8faf035SNeilBrown 		dentry->d_op->d_manage(dentry, true) : 0;
113162a7375eSIan Kent }
113262a7375eSIan Kent 
11339875cf80SDavid Howells /*
1134287548e4SAl Viro  * Try to skip to top of mountpoint pile in rcuwalk mode.  Fail if
1135287548e4SAl Viro  * we meet a managed dentry that would need blocking.
11369875cf80SDavid Howells  */
11379875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1138287548e4SAl Viro 			       struct inode **inode)
11399875cf80SDavid Howells {
114062a7375eSIan Kent 	for (;;) {
1141c7105365SAl Viro 		struct mount *mounted;
114262a7375eSIan Kent 		/*
114362a7375eSIan Kent 		 * Don't forget we might have a non-mountpoint managed dentry
114462a7375eSIan Kent 		 * that wants to block transit.
114562a7375eSIan Kent 		 */
1146b8faf035SNeilBrown 		switch (managed_dentry_rcu(path->dentry)) {
1147b8faf035SNeilBrown 		case -ECHILD:
1148b8faf035SNeilBrown 		default:
1149ab90911fSDavid Howells 			return false;
1150b8faf035SNeilBrown 		case -EISDIR:
1151b8faf035SNeilBrown 			return true;
1152b8faf035SNeilBrown 		case 0:
1153b8faf035SNeilBrown 			break;
1154b8faf035SNeilBrown 		}
115562a7375eSIan Kent 
115662a7375eSIan Kent 		if (!d_mountpoint(path->dentry))
1157b8faf035SNeilBrown 			return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
115862a7375eSIan Kent 
1159474279dcSAl Viro 		mounted = __lookup_mnt(path->mnt, path->dentry);
11609875cf80SDavid Howells 		if (!mounted)
11619875cf80SDavid Howells 			break;
1162c7105365SAl Viro 		path->mnt = &mounted->mnt;
1163c7105365SAl Viro 		path->dentry = mounted->mnt.mnt_root;
1164a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
11659875cf80SDavid Howells 		nd->seq = read_seqcount_begin(&path->dentry->d_seq);
116659430262SLinus Torvalds 		/*
116759430262SLinus Torvalds 		 * Update the inode too. We don't need to re-check the
116859430262SLinus Torvalds 		 * dentry sequence number here after this d_inode read,
116959430262SLinus Torvalds 		 * because a mount-point is always pinned.
117059430262SLinus Torvalds 		 */
117159430262SLinus Torvalds 		*inode = path->dentry->d_inode;
11729875cf80SDavid Howells 	}
1173f5be3e29SAl Viro 	return !read_seqretry(&mount_lock, nd->m_seq) &&
1174b8faf035SNeilBrown 		!(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
1175287548e4SAl Viro }
1176287548e4SAl Viro 
117731e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd)
117831e6b01fSNick Piggin {
11794023bfc9SAl Viro 	struct inode *inode = nd->inode;
11807bd88377SAl Viro 	if (!nd->root.mnt)
118131e6b01fSNick Piggin 		set_root_rcu(nd);
118231e6b01fSNick Piggin 
118331e6b01fSNick Piggin 	while (1) {
118431e6b01fSNick Piggin 		if (nd->path.dentry == nd->root.dentry &&
118531e6b01fSNick Piggin 		    nd->path.mnt == nd->root.mnt) {
118631e6b01fSNick Piggin 			break;
118731e6b01fSNick Piggin 		}
118831e6b01fSNick Piggin 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
118931e6b01fSNick Piggin 			struct dentry *old = nd->path.dentry;
119031e6b01fSNick Piggin 			struct dentry *parent = old->d_parent;
119131e6b01fSNick Piggin 			unsigned seq;
119231e6b01fSNick Piggin 
11934023bfc9SAl Viro 			inode = parent->d_inode;
119431e6b01fSNick Piggin 			seq = read_seqcount_begin(&parent->d_seq);
119531e6b01fSNick Piggin 			if (read_seqcount_retry(&old->d_seq, nd->seq))
1196ef7562d5SAl Viro 				goto failed;
119731e6b01fSNick Piggin 			nd->path.dentry = parent;
119831e6b01fSNick Piggin 			nd->seq = seq;
119931e6b01fSNick Piggin 			break;
120031e6b01fSNick Piggin 		}
120131e6b01fSNick Piggin 		if (!follow_up_rcu(&nd->path))
120231e6b01fSNick Piggin 			break;
12034023bfc9SAl Viro 		inode = nd->path.dentry->d_inode;
120431e6b01fSNick Piggin 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
120531e6b01fSNick Piggin 	}
1206b37199e6SAl Viro 	while (d_mountpoint(nd->path.dentry)) {
1207b37199e6SAl Viro 		struct mount *mounted;
1208b37199e6SAl Viro 		mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
1209b37199e6SAl Viro 		if (!mounted)
1210b37199e6SAl Viro 			break;
1211b37199e6SAl Viro 		nd->path.mnt = &mounted->mnt;
1212b37199e6SAl Viro 		nd->path.dentry = mounted->mnt.mnt_root;
12134023bfc9SAl Viro 		inode = nd->path.dentry->d_inode;
1214b37199e6SAl Viro 		nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1215f5be3e29SAl Viro 		if (read_seqretry(&mount_lock, nd->m_seq))
1216b37199e6SAl Viro 			goto failed;
1217b37199e6SAl Viro 	}
12184023bfc9SAl Viro 	nd->inode = inode;
121931e6b01fSNick Piggin 	return 0;
1220ef7562d5SAl Viro 
1221ef7562d5SAl Viro failed:
1222ef7562d5SAl Viro 	nd->flags &= ~LOOKUP_RCU;
12235b6ca027SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
1224ef7562d5SAl Viro 		nd->root.mnt = NULL;
12258b61e74fSAl Viro 	rcu_read_unlock();
1226ef7562d5SAl Viro 	return -ECHILD;
122731e6b01fSNick Piggin }
122831e6b01fSNick Piggin 
12299875cf80SDavid Howells /*
1230cc53ce53SDavid Howells  * Follow down to the covering mount currently visible to userspace.  At each
1231cc53ce53SDavid Howells  * point, the filesystem owning that dentry may be queried as to whether the
1232cc53ce53SDavid Howells  * caller is permitted to proceed or not.
1233cc53ce53SDavid Howells  */
12347cc90cc3SAl Viro int follow_down(struct path *path)
1235cc53ce53SDavid Howells {
1236cc53ce53SDavid Howells 	unsigned managed;
1237cc53ce53SDavid Howells 	int ret;
1238cc53ce53SDavid Howells 
1239cc53ce53SDavid Howells 	while (managed = ACCESS_ONCE(path->dentry->d_flags),
1240cc53ce53SDavid Howells 	       unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1241cc53ce53SDavid Howells 		/* Allow the filesystem to manage the transit without i_mutex
1242cc53ce53SDavid Howells 		 * being held.
1243cc53ce53SDavid Howells 		 *
1244cc53ce53SDavid Howells 		 * We indicate to the filesystem if someone is trying to mount
1245cc53ce53SDavid Howells 		 * something here.  This gives autofs the chance to deny anyone
1246cc53ce53SDavid Howells 		 * other than its daemon the right to mount on its
1247cc53ce53SDavid Howells 		 * superstructure.
1248cc53ce53SDavid Howells 		 *
1249cc53ce53SDavid Howells 		 * The filesystem may sleep at this point.
1250cc53ce53SDavid Howells 		 */
1251cc53ce53SDavid Howells 		if (managed & DCACHE_MANAGE_TRANSIT) {
1252cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op);
1253cc53ce53SDavid Howells 			BUG_ON(!path->dentry->d_op->d_manage);
1254ab90911fSDavid Howells 			ret = path->dentry->d_op->d_manage(
12551aed3e42SAl Viro 				path->dentry, false);
1256cc53ce53SDavid Howells 			if (ret < 0)
1257cc53ce53SDavid Howells 				return ret == -EISDIR ? 0 : ret;
1258cc53ce53SDavid Howells 		}
1259cc53ce53SDavid Howells 
1260cc53ce53SDavid Howells 		/* Transit to a mounted filesystem. */
1261cc53ce53SDavid Howells 		if (managed & DCACHE_MOUNTED) {
1262cc53ce53SDavid Howells 			struct vfsmount *mounted = lookup_mnt(path);
1263cc53ce53SDavid Howells 			if (!mounted)
1264cc53ce53SDavid Howells 				break;
1265cc53ce53SDavid Howells 			dput(path->dentry);
1266cc53ce53SDavid Howells 			mntput(path->mnt);
1267cc53ce53SDavid Howells 			path->mnt = mounted;
1268cc53ce53SDavid Howells 			path->dentry = dget(mounted->mnt_root);
1269cc53ce53SDavid Howells 			continue;
1270cc53ce53SDavid Howells 		}
1271cc53ce53SDavid Howells 
1272cc53ce53SDavid Howells 		/* Don't handle automount points here */
1273cc53ce53SDavid Howells 		break;
1274cc53ce53SDavid Howells 	}
1275cc53ce53SDavid Howells 	return 0;
1276cc53ce53SDavid Howells }
12774d359507SAl Viro EXPORT_SYMBOL(follow_down);
1278cc53ce53SDavid Howells 
1279cc53ce53SDavid Howells /*
12809875cf80SDavid Howells  * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
12819875cf80SDavid Howells  */
12829875cf80SDavid Howells static void follow_mount(struct path *path)
12839875cf80SDavid Howells {
12849875cf80SDavid Howells 	while (d_mountpoint(path->dentry)) {
12859875cf80SDavid Howells 		struct vfsmount *mounted = lookup_mnt(path);
12869875cf80SDavid Howells 		if (!mounted)
12879875cf80SDavid Howells 			break;
12889875cf80SDavid Howells 		dput(path->dentry);
12899875cf80SDavid Howells 		mntput(path->mnt);
12909875cf80SDavid Howells 		path->mnt = mounted;
12919875cf80SDavid Howells 		path->dentry = dget(mounted->mnt_root);
12929875cf80SDavid Howells 	}
12939875cf80SDavid Howells }
12949875cf80SDavid Howells 
129531e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd)
12961da177e4SLinus Torvalds {
12977bd88377SAl Viro 	if (!nd->root.mnt)
12982a737871SAl Viro 		set_root(nd);
1299e518ddb7SAndreas Mohr 
13001da177e4SLinus Torvalds 	while(1) {
13014ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
13021da177e4SLinus Torvalds 
13032a737871SAl Viro 		if (nd->path.dentry == nd->root.dentry &&
13042a737871SAl Viro 		    nd->path.mnt == nd->root.mnt) {
13051da177e4SLinus Torvalds 			break;
13061da177e4SLinus Torvalds 		}
13074ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
13083088dd70SAl Viro 			/* rare case of legitimate dget_parent()... */
13093088dd70SAl Viro 			nd->path.dentry = dget_parent(nd->path.dentry);
13101da177e4SLinus Torvalds 			dput(old);
13111da177e4SLinus Torvalds 			break;
13121da177e4SLinus Torvalds 		}
13133088dd70SAl Viro 		if (!follow_up(&nd->path))
13141da177e4SLinus Torvalds 			break;
13151da177e4SLinus Torvalds 	}
131679ed0226SAl Viro 	follow_mount(&nd->path);
131731e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
13181da177e4SLinus Torvalds }
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds /*
1321bad61189SMiklos Szeredi  * This looks up the name in dcache, possibly revalidates the old dentry and
1322bad61189SMiklos Szeredi  * allocates a new one if not found or not valid.  In the need_lookup argument
1323bad61189SMiklos Szeredi  * returns whether i_op->lookup is necessary.
1324bad61189SMiklos Szeredi  *
1325bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
1326baa03890SNick Piggin  */
1327bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
1328201f956eSAl Viro 				    unsigned int flags, bool *need_lookup)
1329baa03890SNick Piggin {
1330baa03890SNick Piggin 	struct dentry *dentry;
1331bad61189SMiklos Szeredi 	int error;
1332baa03890SNick Piggin 
1333bad61189SMiklos Szeredi 	*need_lookup = false;
1334bad61189SMiklos Szeredi 	dentry = d_lookup(dir, name);
1335bad61189SMiklos Szeredi 	if (dentry) {
133639e3c955SJeff Layton 		if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1337201f956eSAl Viro 			error = d_revalidate(dentry, flags);
1338bad61189SMiklos Szeredi 			if (unlikely(error <= 0)) {
1339bad61189SMiklos Szeredi 				if (error < 0) {
1340bad61189SMiklos Szeredi 					dput(dentry);
1341bad61189SMiklos Szeredi 					return ERR_PTR(error);
13425542aa2fSEric W. Biederman 				} else {
13435542aa2fSEric W. Biederman 					d_invalidate(dentry);
1344bad61189SMiklos Szeredi 					dput(dentry);
1345bad61189SMiklos Szeredi 					dentry = NULL;
1346bad61189SMiklos Szeredi 				}
1347bad61189SMiklos Szeredi 			}
1348bad61189SMiklos Szeredi 		}
1349bad61189SMiklos Szeredi 	}
1350baa03890SNick Piggin 
1351bad61189SMiklos Szeredi 	if (!dentry) {
1352bad61189SMiklos Szeredi 		dentry = d_alloc(dir, name);
1353baa03890SNick Piggin 		if (unlikely(!dentry))
1354baa03890SNick Piggin 			return ERR_PTR(-ENOMEM);
1355baa03890SNick Piggin 
1356bad61189SMiklos Szeredi 		*need_lookup = true;
1357baa03890SNick Piggin 	}
1358baa03890SNick Piggin 	return dentry;
1359baa03890SNick Piggin }
1360baa03890SNick Piggin 
1361baa03890SNick Piggin /*
136213a2c3beSJ. Bruce Fields  * Call i_op->lookup on the dentry.  The dentry must be negative and
136313a2c3beSJ. Bruce Fields  * unhashed.
1364bad61189SMiklos Szeredi  *
1365bad61189SMiklos Szeredi  * dir->d_inode->i_mutex must be held
136644396f4bSJosef Bacik  */
1367bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
136872bd866aSAl Viro 				  unsigned int flags)
136944396f4bSJosef Bacik {
137044396f4bSJosef Bacik 	struct dentry *old;
137144396f4bSJosef Bacik 
137244396f4bSJosef Bacik 	/* Don't create child dentry for a dead directory. */
1373bad61189SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
1374e188dc02SMiklos Szeredi 		dput(dentry);
137544396f4bSJosef Bacik 		return ERR_PTR(-ENOENT);
1376e188dc02SMiklos Szeredi 	}
137744396f4bSJosef Bacik 
137872bd866aSAl Viro 	old = dir->i_op->lookup(dir, dentry, flags);
137944396f4bSJosef Bacik 	if (unlikely(old)) {
138044396f4bSJosef Bacik 		dput(dentry);
138144396f4bSJosef Bacik 		dentry = old;
138244396f4bSJosef Bacik 	}
138344396f4bSJosef Bacik 	return dentry;
138444396f4bSJosef Bacik }
138544396f4bSJosef Bacik 
1386a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name,
138772bd866aSAl Viro 		struct dentry *base, unsigned int flags)
1388a3255546SAl Viro {
1389bad61189SMiklos Szeredi 	bool need_lookup;
1390a3255546SAl Viro 	struct dentry *dentry;
1391a3255546SAl Viro 
139272bd866aSAl Viro 	dentry = lookup_dcache(name, base, flags, &need_lookup);
1393bad61189SMiklos Szeredi 	if (!need_lookup)
1394a3255546SAl Viro 		return dentry;
1395bad61189SMiklos Szeredi 
139672bd866aSAl Viro 	return lookup_real(base->d_inode, dentry, flags);
1397a3255546SAl Viro }
1398a3255546SAl Viro 
139944396f4bSJosef Bacik /*
14001da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
14011da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
14021da177e4SLinus Torvalds  *  It _is_ time-critical.
14031da177e4SLinus Torvalds  */
1404e97cdc87SAl Viro static int lookup_fast(struct nameidata *nd,
140531e6b01fSNick Piggin 		       struct path *path, struct inode **inode)
14061da177e4SLinus Torvalds {
14074ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
140831e6b01fSNick Piggin 	struct dentry *dentry, *parent = nd->path.dentry;
14095a18fff2SAl Viro 	int need_reval = 1;
14105a18fff2SAl Viro 	int status = 1;
14119875cf80SDavid Howells 	int err;
14129875cf80SDavid Howells 
14133cac260aSAl Viro 	/*
1414b04f784eSNick Piggin 	 * Rename seqlock is not required here because in the off chance
1415b04f784eSNick Piggin 	 * of a false negative due to a concurrent rename, we're going to
1416b04f784eSNick Piggin 	 * do the non-racy lookup, below.
1417b04f784eSNick Piggin 	 */
141831e6b01fSNick Piggin 	if (nd->flags & LOOKUP_RCU) {
141931e6b01fSNick Piggin 		unsigned seq;
1420da53be12SLinus Torvalds 		dentry = __d_lookup_rcu(parent, &nd->last, &seq);
14215a18fff2SAl Viro 		if (!dentry)
14225a18fff2SAl Viro 			goto unlazy;
14235a18fff2SAl Viro 
142412f8ad4bSLinus Torvalds 		/*
142512f8ad4bSLinus Torvalds 		 * This sequence count validates that the inode matches
142612f8ad4bSLinus Torvalds 		 * the dentry name information from lookup.
142712f8ad4bSLinus Torvalds 		 */
142812f8ad4bSLinus Torvalds 		*inode = dentry->d_inode;
142912f8ad4bSLinus Torvalds 		if (read_seqcount_retry(&dentry->d_seq, seq))
143012f8ad4bSLinus Torvalds 			return -ECHILD;
143112f8ad4bSLinus Torvalds 
143212f8ad4bSLinus Torvalds 		/*
143312f8ad4bSLinus Torvalds 		 * This sequence count validates that the parent had no
143412f8ad4bSLinus Torvalds 		 * changes while we did the lookup of the dentry above.
143512f8ad4bSLinus Torvalds 		 *
143612f8ad4bSLinus Torvalds 		 * The memory barrier in read_seqcount_begin of child is
143712f8ad4bSLinus Torvalds 		 *  enough, we can use __read_seqcount_retry here.
143812f8ad4bSLinus Torvalds 		 */
143931e6b01fSNick Piggin 		if (__read_seqcount_retry(&parent->d_seq, nd->seq))
144031e6b01fSNick Piggin 			return -ECHILD;
144131e6b01fSNick Piggin 		nd->seq = seq;
14425a18fff2SAl Viro 
144324643087SAl Viro 		if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
14444ce16ef3SAl Viro 			status = d_revalidate(dentry, nd->flags);
14455a18fff2SAl Viro 			if (unlikely(status <= 0)) {
14465a18fff2SAl Viro 				if (status != -ECHILD)
14475a18fff2SAl Viro 					need_reval = 0;
14485a18fff2SAl Viro 				goto unlazy;
14495a18fff2SAl Viro 			}
145024643087SAl Viro 		}
145131e6b01fSNick Piggin 		path->mnt = mnt;
145231e6b01fSNick Piggin 		path->dentry = dentry;
1453b8faf035SNeilBrown 		if (likely(__follow_mount_rcu(nd, path, inode)))
14549875cf80SDavid Howells 			return 0;
14555a18fff2SAl Viro unlazy:
145619660af7SAl Viro 		if (unlazy_walk(nd, dentry))
14575a18fff2SAl Viro 			return -ECHILD;
14585a18fff2SAl Viro 	} else {
1459e97cdc87SAl Viro 		dentry = __d_lookup(parent, &nd->last);
146024643087SAl Viro 	}
14615a18fff2SAl Viro 
146281e6f520SAl Viro 	if (unlikely(!dentry))
146381e6f520SAl Viro 		goto need_lookup;
14645a18fff2SAl Viro 
14655a18fff2SAl Viro 	if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
14664ce16ef3SAl Viro 		status = d_revalidate(dentry, nd->flags);
14675a18fff2SAl Viro 	if (unlikely(status <= 0)) {
14685a18fff2SAl Viro 		if (status < 0) {
14695a18fff2SAl Viro 			dput(dentry);
14705a18fff2SAl Viro 			return status;
14715a18fff2SAl Viro 		}
14725542aa2fSEric W. Biederman 		d_invalidate(dentry);
14735a18fff2SAl Viro 		dput(dentry);
147481e6f520SAl Viro 		goto need_lookup;
14755a18fff2SAl Viro 	}
1476697f514dSMiklos Szeredi 
14771da177e4SLinus Torvalds 	path->mnt = mnt;
14781da177e4SLinus Torvalds 	path->dentry = dentry;
14799875cf80SDavid Howells 	err = follow_managed(path, nd->flags);
148089312214SIan Kent 	if (unlikely(err < 0)) {
148189312214SIan Kent 		path_put_conditional(path, nd);
14829875cf80SDavid Howells 		return err;
148389312214SIan Kent 	}
1484a3fbbde7SAl Viro 	if (err)
1485a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
148631e6b01fSNick Piggin 	*inode = path->dentry->d_inode;
14871da177e4SLinus Torvalds 	return 0;
148881e6f520SAl Viro 
148981e6f520SAl Viro need_lookup:
1490697f514dSMiklos Szeredi 	return 1;
1491697f514dSMiklos Szeredi }
1492697f514dSMiklos Szeredi 
1493697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */
1494cc2a5271SAl Viro static int lookup_slow(struct nameidata *nd, struct path *path)
1495697f514dSMiklos Szeredi {
1496697f514dSMiklos Szeredi 	struct dentry *dentry, *parent;
1497697f514dSMiklos Szeredi 	int err;
1498697f514dSMiklos Szeredi 
1499697f514dSMiklos Szeredi 	parent = nd->path.dentry;
150081e6f520SAl Viro 	BUG_ON(nd->inode != parent->d_inode);
150181e6f520SAl Viro 
150281e6f520SAl Viro 	mutex_lock(&parent->d_inode->i_mutex);
1503cc2a5271SAl Viro 	dentry = __lookup_hash(&nd->last, parent, nd->flags);
150481e6f520SAl Viro 	mutex_unlock(&parent->d_inode->i_mutex);
150581e6f520SAl Viro 	if (IS_ERR(dentry))
150681e6f520SAl Viro 		return PTR_ERR(dentry);
1507697f514dSMiklos Szeredi 	path->mnt = nd->path.mnt;
1508697f514dSMiklos Szeredi 	path->dentry = dentry;
1509697f514dSMiklos Szeredi 	err = follow_managed(path, nd->flags);
1510697f514dSMiklos Szeredi 	if (unlikely(err < 0)) {
1511697f514dSMiklos Szeredi 		path_put_conditional(path, nd);
1512697f514dSMiklos Szeredi 		return err;
1513697f514dSMiklos Szeredi 	}
1514697f514dSMiklos Szeredi 	if (err)
1515697f514dSMiklos Szeredi 		nd->flags |= LOOKUP_JUMPED;
1516697f514dSMiklos Szeredi 	return 0;
15171da177e4SLinus Torvalds }
15181da177e4SLinus Torvalds 
151952094c8aSAl Viro static inline int may_lookup(struct nameidata *nd)
152052094c8aSAl Viro {
152152094c8aSAl Viro 	if (nd->flags & LOOKUP_RCU) {
15224ad5abb3SAl Viro 		int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
152352094c8aSAl Viro 		if (err != -ECHILD)
152452094c8aSAl Viro 			return err;
152519660af7SAl Viro 		if (unlazy_walk(nd, NULL))
152652094c8aSAl Viro 			return -ECHILD;
152752094c8aSAl Viro 	}
15284ad5abb3SAl Viro 	return inode_permission(nd->inode, MAY_EXEC);
152952094c8aSAl Viro }
153052094c8aSAl Viro 
15319856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type)
15329856fa1bSAl Viro {
15339856fa1bSAl Viro 	if (type == LAST_DOTDOT) {
15349856fa1bSAl Viro 		if (nd->flags & LOOKUP_RCU) {
15359856fa1bSAl Viro 			if (follow_dotdot_rcu(nd))
15369856fa1bSAl Viro 				return -ECHILD;
15379856fa1bSAl Viro 		} else
15389856fa1bSAl Viro 			follow_dotdot(nd);
15399856fa1bSAl Viro 	}
15409856fa1bSAl Viro 	return 0;
15419856fa1bSAl Viro }
15429856fa1bSAl Viro 
1543951361f9SAl Viro static void terminate_walk(struct nameidata *nd)
1544951361f9SAl Viro {
1545951361f9SAl Viro 	if (!(nd->flags & LOOKUP_RCU)) {
1546951361f9SAl Viro 		path_put(&nd->path);
1547951361f9SAl Viro 	} else {
1548951361f9SAl Viro 		nd->flags &= ~LOOKUP_RCU;
15495b6ca027SAl Viro 		if (!(nd->flags & LOOKUP_ROOT))
1550951361f9SAl Viro 			nd->root.mnt = NULL;
15518b61e74fSAl Viro 		rcu_read_unlock();
1552951361f9SAl Viro 	}
1553951361f9SAl Viro }
1554951361f9SAl Viro 
15553ddcd056SLinus Torvalds /*
15563ddcd056SLinus Torvalds  * Do we need to follow links? We _really_ want to be able
15573ddcd056SLinus Torvalds  * to do this check without having to look at inode->i_op,
15583ddcd056SLinus Torvalds  * so we keep a cache of "no, this doesn't need follow_link"
15593ddcd056SLinus Torvalds  * for the common case.
15603ddcd056SLinus Torvalds  */
1561b18825a7SDavid Howells static inline int should_follow_link(struct dentry *dentry, int follow)
15623ddcd056SLinus Torvalds {
1563b18825a7SDavid Howells 	return unlikely(d_is_symlink(dentry)) ? follow : 0;
15643ddcd056SLinus Torvalds }
15653ddcd056SLinus Torvalds 
1566ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path,
156721b9b073SAl Viro 		int follow)
1568ce57dfc1SAl Viro {
1569ce57dfc1SAl Viro 	struct inode *inode;
1570ce57dfc1SAl Viro 	int err;
1571ce57dfc1SAl Viro 	/*
1572ce57dfc1SAl Viro 	 * "." and ".." are special - ".." especially so because it has
1573ce57dfc1SAl Viro 	 * to be able to know about the current root directory and
1574ce57dfc1SAl Viro 	 * parent relationships.
1575ce57dfc1SAl Viro 	 */
157621b9b073SAl Viro 	if (unlikely(nd->last_type != LAST_NORM))
157721b9b073SAl Viro 		return handle_dots(nd, nd->last_type);
1578e97cdc87SAl Viro 	err = lookup_fast(nd, path, &inode);
1579ce57dfc1SAl Viro 	if (unlikely(err)) {
1580697f514dSMiklos Szeredi 		if (err < 0)
1581697f514dSMiklos Szeredi 			goto out_err;
1582697f514dSMiklos Szeredi 
1583cc2a5271SAl Viro 		err = lookup_slow(nd, path);
1584697f514dSMiklos Szeredi 		if (err < 0)
1585697f514dSMiklos Szeredi 			goto out_err;
1586697f514dSMiklos Szeredi 
1587697f514dSMiklos Szeredi 		inode = path->dentry->d_inode;
1588ce57dfc1SAl Viro 	}
1589697f514dSMiklos Szeredi 	err = -ENOENT;
159022213318SAl Viro 	if (!inode || d_is_negative(path->dentry))
1591697f514dSMiklos Szeredi 		goto out_path_put;
1592697f514dSMiklos Szeredi 
1593b18825a7SDavid Howells 	if (should_follow_link(path->dentry, follow)) {
159419660af7SAl Viro 		if (nd->flags & LOOKUP_RCU) {
159519660af7SAl Viro 			if (unlikely(unlazy_walk(nd, path->dentry))) {
1596697f514dSMiklos Szeredi 				err = -ECHILD;
1597697f514dSMiklos Szeredi 				goto out_err;
159819660af7SAl Viro 			}
159919660af7SAl Viro 		}
1600ce57dfc1SAl Viro 		BUG_ON(inode != path->dentry->d_inode);
1601ce57dfc1SAl Viro 		return 1;
1602ce57dfc1SAl Viro 	}
1603ce57dfc1SAl Viro 	path_to_nameidata(path, nd);
1604ce57dfc1SAl Viro 	nd->inode = inode;
1605ce57dfc1SAl Viro 	return 0;
1606697f514dSMiklos Szeredi 
1607697f514dSMiklos Szeredi out_path_put:
1608697f514dSMiklos Szeredi 	path_to_nameidata(path, nd);
1609697f514dSMiklos Szeredi out_err:
1610697f514dSMiklos Szeredi 	terminate_walk(nd);
1611697f514dSMiklos Szeredi 	return err;
1612ce57dfc1SAl Viro }
1613ce57dfc1SAl Viro 
16141da177e4SLinus Torvalds /*
1615b356379aSAl Viro  * This limits recursive symlink follows to 8, while
1616b356379aSAl Viro  * limiting consecutive symlinks to 40.
1617b356379aSAl Viro  *
1618b356379aSAl Viro  * Without that kind of total limit, nasty chains of consecutive
1619b356379aSAl Viro  * symlinks can cause almost arbitrarily long lookups.
1620b356379aSAl Viro  */
1621b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd)
1622b356379aSAl Viro {
1623b356379aSAl Viro 	int res;
1624b356379aSAl Viro 
1625b356379aSAl Viro 	if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
1626b356379aSAl Viro 		path_put_conditional(path, nd);
1627b356379aSAl Viro 		path_put(&nd->path);
1628b356379aSAl Viro 		return -ELOOP;
1629b356379aSAl Viro 	}
16301a4022f8SErez Zadok 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
1631b356379aSAl Viro 
1632b356379aSAl Viro 	nd->depth++;
1633b356379aSAl Viro 	current->link_count++;
1634b356379aSAl Viro 
1635b356379aSAl Viro 	do {
1636b356379aSAl Viro 		struct path link = *path;
1637b356379aSAl Viro 		void *cookie;
1638574197e0SAl Viro 
1639574197e0SAl Viro 		res = follow_link(&link, nd, &cookie);
16406d7b5aaeSAl Viro 		if (res)
16416d7b5aaeSAl Viro 			break;
164221b9b073SAl Viro 		res = walk_component(nd, path, LOOKUP_FOLLOW);
1643574197e0SAl Viro 		put_link(nd, &link, cookie);
1644b356379aSAl Viro 	} while (res > 0);
1645b356379aSAl Viro 
1646b356379aSAl Viro 	current->link_count--;
1647b356379aSAl Viro 	nd->depth--;
1648b356379aSAl Viro 	return res;
1649b356379aSAl Viro }
1650b356379aSAl Viro 
1651b356379aSAl Viro /*
1652bfcfaa77SLinus Torvalds  * We can do the critical dentry name comparison and hashing
1653bfcfaa77SLinus Torvalds  * operations one word at a time, but we are limited to:
1654bfcfaa77SLinus Torvalds  *
1655bfcfaa77SLinus Torvalds  * - Architectures with fast unaligned word accesses. We could
1656bfcfaa77SLinus Torvalds  *   do a "get_unaligned()" if this helps and is sufficiently
1657bfcfaa77SLinus Torvalds  *   fast.
1658bfcfaa77SLinus Torvalds  *
1659bfcfaa77SLinus Torvalds  * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1660bfcfaa77SLinus Torvalds  *   do not trap on the (extremely unlikely) case of a page
1661bfcfaa77SLinus Torvalds  *   crossing operation.
1662bfcfaa77SLinus Torvalds  *
1663bfcfaa77SLinus Torvalds  * - Furthermore, we need an efficient 64-bit compile for the
1664bfcfaa77SLinus Torvalds  *   64-bit case in order to generate the "number of bytes in
1665bfcfaa77SLinus Torvalds  *   the final mask". Again, that could be replaced with a
1666bfcfaa77SLinus Torvalds  *   efficient population count instruction or similar.
1667bfcfaa77SLinus Torvalds  */
1668bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS
1669bfcfaa77SLinus Torvalds 
1670f68e556eSLinus Torvalds #include <asm/word-at-a-time.h>
1671bfcfaa77SLinus Torvalds 
1672f68e556eSLinus Torvalds #ifdef CONFIG_64BIT
1673bfcfaa77SLinus Torvalds 
1674bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash)
1675bfcfaa77SLinus Torvalds {
167699d263d4SLinus Torvalds 	return hash_64(hash, 32);
1677bfcfaa77SLinus Torvalds }
1678bfcfaa77SLinus Torvalds 
1679bfcfaa77SLinus Torvalds #else	/* 32-bit case */
1680bfcfaa77SLinus Torvalds 
1681bfcfaa77SLinus Torvalds #define fold_hash(x) (x)
1682bfcfaa77SLinus Torvalds 
1683bfcfaa77SLinus Torvalds #endif
1684bfcfaa77SLinus Torvalds 
1685bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1686bfcfaa77SLinus Torvalds {
1687bfcfaa77SLinus Torvalds 	unsigned long a, mask;
1688bfcfaa77SLinus Torvalds 	unsigned long hash = 0;
1689bfcfaa77SLinus Torvalds 
1690bfcfaa77SLinus Torvalds 	for (;;) {
1691e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name);
1692bfcfaa77SLinus Torvalds 		if (len < sizeof(unsigned long))
1693bfcfaa77SLinus Torvalds 			break;
1694bfcfaa77SLinus Torvalds 		hash += a;
1695f132c5beSAl Viro 		hash *= 9;
1696bfcfaa77SLinus Torvalds 		name += sizeof(unsigned long);
1697bfcfaa77SLinus Torvalds 		len -= sizeof(unsigned long);
1698bfcfaa77SLinus Torvalds 		if (!len)
1699bfcfaa77SLinus Torvalds 			goto done;
1700bfcfaa77SLinus Torvalds 	}
1701a5c21dceSWill Deacon 	mask = bytemask_from_count(len);
1702bfcfaa77SLinus Torvalds 	hash += mask & a;
1703bfcfaa77SLinus Torvalds done:
1704bfcfaa77SLinus Torvalds 	return fold_hash(hash);
1705bfcfaa77SLinus Torvalds }
1706bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
1707bfcfaa77SLinus Torvalds 
1708bfcfaa77SLinus Torvalds /*
1709bfcfaa77SLinus Torvalds  * Calculate the length and hash of the path component, and
1710d6bb3e90SLinus Torvalds  * return the "hash_len" as the result.
1711bfcfaa77SLinus Torvalds  */
1712d6bb3e90SLinus Torvalds static inline u64 hash_name(const char *name)
1713bfcfaa77SLinus Torvalds {
171436126f8fSLinus Torvalds 	unsigned long a, b, adata, bdata, mask, hash, len;
171536126f8fSLinus Torvalds 	const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1716bfcfaa77SLinus Torvalds 
1717bfcfaa77SLinus Torvalds 	hash = a = 0;
1718bfcfaa77SLinus Torvalds 	len = -sizeof(unsigned long);
1719bfcfaa77SLinus Torvalds 	do {
1720bfcfaa77SLinus Torvalds 		hash = (hash + a) * 9;
1721bfcfaa77SLinus Torvalds 		len += sizeof(unsigned long);
1722e419b4ccSLinus Torvalds 		a = load_unaligned_zeropad(name+len);
172336126f8fSLinus Torvalds 		b = a ^ REPEAT_BYTE('/');
172436126f8fSLinus Torvalds 	} while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1725bfcfaa77SLinus Torvalds 
172636126f8fSLinus Torvalds 	adata = prep_zero_mask(a, adata, &constants);
172736126f8fSLinus Torvalds 	bdata = prep_zero_mask(b, bdata, &constants);
172836126f8fSLinus Torvalds 
172936126f8fSLinus Torvalds 	mask = create_zero_mask(adata | bdata);
173036126f8fSLinus Torvalds 
173136126f8fSLinus Torvalds 	hash += a & zero_bytemask(mask);
17329226b5b4SLinus Torvalds 	len += find_zero(mask);
1733d6bb3e90SLinus Torvalds 	return hashlen_create(fold_hash(hash), len);
1734bfcfaa77SLinus Torvalds }
1735bfcfaa77SLinus Torvalds 
1736bfcfaa77SLinus Torvalds #else
1737bfcfaa77SLinus Torvalds 
17380145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len)
17390145acc2SLinus Torvalds {
17400145acc2SLinus Torvalds 	unsigned long hash = init_name_hash();
17410145acc2SLinus Torvalds 	while (len--)
17420145acc2SLinus Torvalds 		hash = partial_name_hash(*name++, hash);
17430145acc2SLinus Torvalds 	return end_name_hash(hash);
17440145acc2SLinus Torvalds }
1745ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash);
17460145acc2SLinus Torvalds 
17473ddcd056SLinus Torvalds /*
1748200e9ef7SLinus Torvalds  * We know there's a real path component here of at least
1749200e9ef7SLinus Torvalds  * one character.
1750200e9ef7SLinus Torvalds  */
1751d6bb3e90SLinus Torvalds static inline u64 hash_name(const char *name)
1752200e9ef7SLinus Torvalds {
1753200e9ef7SLinus Torvalds 	unsigned long hash = init_name_hash();
1754200e9ef7SLinus Torvalds 	unsigned long len = 0, c;
1755200e9ef7SLinus Torvalds 
1756200e9ef7SLinus Torvalds 	c = (unsigned char)*name;
1757200e9ef7SLinus Torvalds 	do {
1758200e9ef7SLinus Torvalds 		len++;
1759200e9ef7SLinus Torvalds 		hash = partial_name_hash(c, hash);
1760200e9ef7SLinus Torvalds 		c = (unsigned char)name[len];
1761200e9ef7SLinus Torvalds 	} while (c && c != '/');
1762d6bb3e90SLinus Torvalds 	return hashlen_create(end_name_hash(hash), len);
1763200e9ef7SLinus Torvalds }
1764200e9ef7SLinus Torvalds 
1765bfcfaa77SLinus Torvalds #endif
1766bfcfaa77SLinus Torvalds 
1767200e9ef7SLinus Torvalds /*
17681da177e4SLinus Torvalds  * Name resolution.
1769ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
1770ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
17711da177e4SLinus Torvalds  *
1772ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
1773ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
17741da177e4SLinus Torvalds  */
17756de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd)
17761da177e4SLinus Torvalds {
17771da177e4SLinus Torvalds 	struct path next;
17781da177e4SLinus Torvalds 	int err;
17791da177e4SLinus Torvalds 
17801da177e4SLinus Torvalds 	while (*name=='/')
17811da177e4SLinus Torvalds 		name++;
17821da177e4SLinus Torvalds 	if (!*name)
1783086e183aSAl Viro 		return 0;
17841da177e4SLinus Torvalds 
17851da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
17861da177e4SLinus Torvalds 	for(;;) {
1787d6bb3e90SLinus Torvalds 		u64 hash_len;
1788fe479a58SAl Viro 		int type;
17891da177e4SLinus Torvalds 
179052094c8aSAl Viro 		err = may_lookup(nd);
17911da177e4SLinus Torvalds  		if (err)
17921da177e4SLinus Torvalds 			break;
17931da177e4SLinus Torvalds 
1794d6bb3e90SLinus Torvalds 		hash_len = hash_name(name);
17951da177e4SLinus Torvalds 
1796fe479a58SAl Viro 		type = LAST_NORM;
1797d6bb3e90SLinus Torvalds 		if (name[0] == '.') switch (hashlen_len(hash_len)) {
1798fe479a58SAl Viro 			case 2:
1799200e9ef7SLinus Torvalds 				if (name[1] == '.') {
1800fe479a58SAl Viro 					type = LAST_DOTDOT;
180116c2cd71SAl Viro 					nd->flags |= LOOKUP_JUMPED;
180216c2cd71SAl Viro 				}
1803fe479a58SAl Viro 				break;
1804fe479a58SAl Viro 			case 1:
1805fe479a58SAl Viro 				type = LAST_DOT;
1806fe479a58SAl Viro 		}
18075a202bcdSAl Viro 		if (likely(type == LAST_NORM)) {
18085a202bcdSAl Viro 			struct dentry *parent = nd->path.dentry;
180916c2cd71SAl Viro 			nd->flags &= ~LOOKUP_JUMPED;
18105a202bcdSAl Viro 			if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
1811a060dc50SJames Hogan 				struct qstr this = { { .hash_len = hash_len }, .name = name };
1812da53be12SLinus Torvalds 				err = parent->d_op->d_hash(parent, &this);
18135a202bcdSAl Viro 				if (err < 0)
18145a202bcdSAl Viro 					break;
1815d6bb3e90SLinus Torvalds 				hash_len = this.hash_len;
1816d6bb3e90SLinus Torvalds 				name = this.name;
18175a202bcdSAl Viro 			}
18185a202bcdSAl Viro 		}
1819fe479a58SAl Viro 
1820d6bb3e90SLinus Torvalds 		nd->last.hash_len = hash_len;
1821d6bb3e90SLinus Torvalds 		nd->last.name = name;
18225f4a6a69SAl Viro 		nd->last_type = type;
18235f4a6a69SAl Viro 
1824d6bb3e90SLinus Torvalds 		name += hashlen_len(hash_len);
1825d6bb3e90SLinus Torvalds 		if (!*name)
18265f4a6a69SAl Viro 			return 0;
1827200e9ef7SLinus Torvalds 		/*
1828200e9ef7SLinus Torvalds 		 * If it wasn't NUL, we know it was '/'. Skip that
1829200e9ef7SLinus Torvalds 		 * slash, and continue until no more slashes.
1830200e9ef7SLinus Torvalds 		 */
1831200e9ef7SLinus Torvalds 		do {
1832d6bb3e90SLinus Torvalds 			name++;
1833d6bb3e90SLinus Torvalds 		} while (unlikely(*name == '/'));
1834d6bb3e90SLinus Torvalds 		if (!*name)
18355f4a6a69SAl Viro 			return 0;
18365f4a6a69SAl Viro 
183721b9b073SAl Viro 		err = walk_component(nd, &next, LOOKUP_FOLLOW);
1838ce57dfc1SAl Viro 		if (err < 0)
1839ce57dfc1SAl Viro 			return err;
1840fe479a58SAl Viro 
1841ce57dfc1SAl Viro 		if (err) {
1842b356379aSAl Viro 			err = nested_symlink(&next, nd);
18431da177e4SLinus Torvalds 			if (err)
1844a7472babSAl Viro 				return err;
184531e6b01fSNick Piggin 		}
184644b1d530SMiklos Szeredi 		if (!d_can_lookup(nd->path.dentry)) {
18473ddcd056SLinus Torvalds 			err = -ENOTDIR;
18483ddcd056SLinus Torvalds 			break;
18495f4a6a69SAl Viro 		}
1850ce57dfc1SAl Viro 	}
1851951361f9SAl Viro 	terminate_walk(nd);
18521da177e4SLinus Torvalds 	return err;
18531da177e4SLinus Torvalds }
18541da177e4SLinus Torvalds 
185570e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags,
18565e53084dSAl Viro 		     struct nameidata *nd)
185731e6b01fSNick Piggin {
185831e6b01fSNick Piggin 	int retval = 0;
185931e6b01fSNick Piggin 
186031e6b01fSNick Piggin 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
1861980f3ea2SAl Viro 	nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
186231e6b01fSNick Piggin 	nd->depth = 0;
18635e53084dSAl Viro 	nd->base = NULL;
18645b6ca027SAl Viro 	if (flags & LOOKUP_ROOT) {
1865b18825a7SDavid Howells 		struct dentry *root = nd->root.dentry;
1866b18825a7SDavid Howells 		struct inode *inode = root->d_inode;
186773d049a4SAl Viro 		if (*name) {
186844b1d530SMiklos Szeredi 			if (!d_can_lookup(root))
18695b6ca027SAl Viro 				return -ENOTDIR;
18705b6ca027SAl Viro 			retval = inode_permission(inode, MAY_EXEC);
18715b6ca027SAl Viro 			if (retval)
18725b6ca027SAl Viro 				return retval;
187373d049a4SAl Viro 		}
18745b6ca027SAl Viro 		nd->path = nd->root;
18755b6ca027SAl Viro 		nd->inode = inode;
18765b6ca027SAl Viro 		if (flags & LOOKUP_RCU) {
18778b61e74fSAl Viro 			rcu_read_lock();
18785b6ca027SAl Viro 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
187948a066e7SAl Viro 			nd->m_seq = read_seqbegin(&mount_lock);
18805b6ca027SAl Viro 		} else {
18815b6ca027SAl Viro 			path_get(&nd->path);
18825b6ca027SAl Viro 		}
1883d465887fSAl Viro 		goto done;
18845b6ca027SAl Viro 	}
18855b6ca027SAl Viro 
188631e6b01fSNick Piggin 	nd->root.mnt = NULL;
188731e6b01fSNick Piggin 
188848a066e7SAl Viro 	nd->m_seq = read_seqbegin(&mount_lock);
188931e6b01fSNick Piggin 	if (*name=='/') {
1890e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
18918b61e74fSAl Viro 			rcu_read_lock();
18927bd88377SAl Viro 			nd->seq = set_root_rcu(nd);
1893e41f7d4eSAl Viro 		} else {
1894e41f7d4eSAl Viro 			set_root(nd);
1895e41f7d4eSAl Viro 			path_get(&nd->root);
1896e41f7d4eSAl Viro 		}
189731e6b01fSNick Piggin 		nd->path = nd->root;
189831e6b01fSNick Piggin 	} else if (dfd == AT_FDCWD) {
1899e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
190031e6b01fSNick Piggin 			struct fs_struct *fs = current->fs;
1901c28cc364SNick Piggin 			unsigned seq;
190231e6b01fSNick Piggin 
19038b61e74fSAl Viro 			rcu_read_lock();
190431e6b01fSNick Piggin 
1905c28cc364SNick Piggin 			do {
1906c28cc364SNick Piggin 				seq = read_seqcount_begin(&fs->seq);
190731e6b01fSNick Piggin 				nd->path = fs->pwd;
1908c28cc364SNick Piggin 				nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
1909c28cc364SNick Piggin 			} while (read_seqcount_retry(&fs->seq, seq));
1910e41f7d4eSAl Viro 		} else {
1911e41f7d4eSAl Viro 			get_fs_pwd(current->fs, &nd->path);
1912e41f7d4eSAl Viro 		}
191331e6b01fSNick Piggin 	} else {
1914582aa64aSJeff Layton 		/* Caller must check execute permissions on the starting path component */
19152903ff01SAl Viro 		struct fd f = fdget_raw(dfd);
191631e6b01fSNick Piggin 		struct dentry *dentry;
191731e6b01fSNick Piggin 
19182903ff01SAl Viro 		if (!f.file)
19192903ff01SAl Viro 			return -EBADF;
192031e6b01fSNick Piggin 
19212903ff01SAl Viro 		dentry = f.file->f_path.dentry;
192231e6b01fSNick Piggin 
1923f52e0c11SAl Viro 		if (*name) {
192444b1d530SMiklos Szeredi 			if (!d_can_lookup(dentry)) {
19252903ff01SAl Viro 				fdput(f);
19262903ff01SAl Viro 				return -ENOTDIR;
1927f52e0c11SAl Viro 			}
19282903ff01SAl Viro 		}
19292903ff01SAl Viro 
19302903ff01SAl Viro 		nd->path = f.file->f_path;
1931e41f7d4eSAl Viro 		if (flags & LOOKUP_RCU) {
19329c225f26SLinus Torvalds 			if (f.flags & FDPUT_FPUT)
19335e53084dSAl Viro 				nd->base = f.file;
1934c28cc364SNick Piggin 			nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
19358b61e74fSAl Viro 			rcu_read_lock();
19365590ff0dSUlrich Drepper 		} else {
19372903ff01SAl Viro 			path_get(&nd->path);
19382903ff01SAl Viro 			fdput(f);
19391da177e4SLinus Torvalds 		}
1940e41f7d4eSAl Viro 	}
1941e41f7d4eSAl Viro 
194231e6b01fSNick Piggin 	nd->inode = nd->path.dentry->d_inode;
19434023bfc9SAl Viro 	if (!(flags & LOOKUP_RCU))
1944d465887fSAl Viro 		goto done;
19454023bfc9SAl Viro 	if (likely(!read_seqcount_retry(&nd->path.dentry->d_seq, nd->seq)))
1946d465887fSAl Viro 		goto done;
19474023bfc9SAl Viro 	if (!(nd->flags & LOOKUP_ROOT))
19484023bfc9SAl Viro 		nd->root.mnt = NULL;
19494023bfc9SAl Viro 	rcu_read_unlock();
19504023bfc9SAl Viro 	return -ECHILD;
1951d465887fSAl Viro done:
1952d465887fSAl Viro 	current->total_link_count = 0;
1953d465887fSAl Viro 	return link_path_walk(name, nd);
19549b4a9b14SAl Viro }
19559b4a9b14SAl Viro 
1956893b7775SAl Viro static void path_cleanup(struct nameidata *nd)
1957893b7775SAl Viro {
1958893b7775SAl Viro 	if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
1959893b7775SAl Viro 		path_put(&nd->root);
1960893b7775SAl Viro 		nd->root.mnt = NULL;
1961893b7775SAl Viro 	}
1962893b7775SAl Viro 	if (unlikely(nd->base))
1963893b7775SAl Viro 		fput(nd->base);
19649b4a9b14SAl Viro }
19659b4a9b14SAl Viro 
1966bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path)
1967bd92d7feSAl Viro {
1968bd92d7feSAl Viro 	if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
1969bd92d7feSAl Viro 		nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
1970bd92d7feSAl Viro 
1971bd92d7feSAl Viro 	nd->flags &= ~LOOKUP_PARENT;
197221b9b073SAl Viro 	return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
1973bd92d7feSAl Viro }
1974bd92d7feSAl Viro 
19759b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1976ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name,
19779b4a9b14SAl Viro 				unsigned int flags, struct nameidata *nd)
19789b4a9b14SAl Viro {
1979bd92d7feSAl Viro 	struct path path;
1980bd92d7feSAl Viro 	int err;
198131e6b01fSNick Piggin 
198231e6b01fSNick Piggin 	/*
198331e6b01fSNick Piggin 	 * Path walking is largely split up into 2 different synchronisation
198431e6b01fSNick Piggin 	 * schemes, rcu-walk and ref-walk (explained in
198531e6b01fSNick Piggin 	 * Documentation/filesystems/path-lookup.txt). These share much of the
198631e6b01fSNick Piggin 	 * path walk code, but some things particularly setup, cleanup, and
198731e6b01fSNick Piggin 	 * following mounts are sufficiently divergent that functions are
198831e6b01fSNick Piggin 	 * duplicated. Typically there is a function foo(), and its RCU
198931e6b01fSNick Piggin 	 * analogue, foo_rcu().
199031e6b01fSNick Piggin 	 *
199131e6b01fSNick Piggin 	 * -ECHILD is the error number of choice (just to avoid clashes) that
199231e6b01fSNick Piggin 	 * is returned if some aspect of an rcu-walk fails. Such an error must
199331e6b01fSNick Piggin 	 * be handled by restarting a traditional ref-walk (which will always
199431e6b01fSNick Piggin 	 * be able to complete).
199531e6b01fSNick Piggin 	 */
1996980f3ea2SAl Viro 	err = path_init(dfd, name, flags, nd);
1997bd92d7feSAl Viro 	if (!err && !(flags & LOOKUP_PARENT)) {
1998bd92d7feSAl Viro 		err = lookup_last(nd, &path);
1999bd92d7feSAl Viro 		while (err > 0) {
2000bd92d7feSAl Viro 			void *cookie;
2001bd92d7feSAl Viro 			struct path link = path;
2002800179c9SKees Cook 			err = may_follow_link(&link, nd);
2003800179c9SKees Cook 			if (unlikely(err))
2004800179c9SKees Cook 				break;
2005bd92d7feSAl Viro 			nd->flags |= LOOKUP_PARENT;
2006574197e0SAl Viro 			err = follow_link(&link, nd, &cookie);
20076d7b5aaeSAl Viro 			if (err)
20086d7b5aaeSAl Viro 				break;
2009bd92d7feSAl Viro 			err = lookup_last(nd, &path);
2010574197e0SAl Viro 			put_link(nd, &link, cookie);
2011bd92d7feSAl Viro 		}
2012bd92d7feSAl Viro 	}
2013ee0827cdSAl Viro 
20149f1fafeeSAl Viro 	if (!err)
20159f1fafeeSAl Viro 		err = complete_walk(nd);
2016bd92d7feSAl Viro 
2017bd92d7feSAl Viro 	if (!err && nd->flags & LOOKUP_DIRECTORY) {
201844b1d530SMiklos Szeredi 		if (!d_can_lookup(nd->path.dentry)) {
2019bd92d7feSAl Viro 			path_put(&nd->path);
2020bd23a539SAl Viro 			err = -ENOTDIR;
2021bd92d7feSAl Viro 		}
2022bd92d7feSAl Viro 	}
202316c2cd71SAl Viro 
2024893b7775SAl Viro 	path_cleanup(nd);
2025bd92d7feSAl Viro 	return err;
202631e6b01fSNick Piggin }
202731e6b01fSNick Piggin 
2028873f1eedSJeff Layton static int filename_lookup(int dfd, struct filename *name,
2029873f1eedSJeff Layton 				unsigned int flags, struct nameidata *nd)
2030873f1eedSJeff Layton {
2031873f1eedSJeff Layton 	int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
2032873f1eedSJeff Layton 	if (unlikely(retval == -ECHILD))
2033873f1eedSJeff Layton 		retval = path_lookupat(dfd, name->name, flags, nd);
2034873f1eedSJeff Layton 	if (unlikely(retval == -ESTALE))
2035873f1eedSJeff Layton 		retval = path_lookupat(dfd, name->name,
2036873f1eedSJeff Layton 						flags | LOOKUP_REVAL, nd);
2037873f1eedSJeff Layton 
2038873f1eedSJeff Layton 	if (likely(!retval))
2039adb5c247SJeff Layton 		audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
2040873f1eedSJeff Layton 	return retval;
2041873f1eedSJeff Layton }
2042873f1eedSJeff Layton 
2043ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name,
2044ee0827cdSAl Viro 				unsigned int flags, struct nameidata *nd)
2045ee0827cdSAl Viro {
204651689104SPaul Moore 	struct filename *filename = getname_kernel(name);
204751689104SPaul Moore 	int retval = PTR_ERR(filename);
2048ee0827cdSAl Viro 
204951689104SPaul Moore 	if (!IS_ERR(filename)) {
205051689104SPaul Moore 		retval = filename_lookup(dfd, filename, flags, nd);
205151689104SPaul Moore 		putname(filename);
205251689104SPaul Moore 	}
205351689104SPaul Moore 	return retval;
20541da177e4SLinus Torvalds }
20551da177e4SLinus Torvalds 
205679714f72SAl Viro /* does lookup, returns the object with parent locked */
205779714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path)
20585590ff0dSUlrich Drepper {
205951689104SPaul Moore 	struct filename *filename = getname_kernel(name);
206079714f72SAl Viro 	struct nameidata nd;
206179714f72SAl Viro 	struct dentry *d;
206251689104SPaul Moore 	int err;
206351689104SPaul Moore 
206451689104SPaul Moore 	if (IS_ERR(filename))
206551689104SPaul Moore 		return ERR_CAST(filename);
206651689104SPaul Moore 
206751689104SPaul Moore 	err = filename_lookup(AT_FDCWD, filename, LOOKUP_PARENT, &nd);
206851689104SPaul Moore 	if (err) {
206951689104SPaul Moore 		d = ERR_PTR(err);
207051689104SPaul Moore 		goto out;
207151689104SPaul Moore 	}
207279714f72SAl Viro 	if (nd.last_type != LAST_NORM) {
207379714f72SAl Viro 		path_put(&nd.path);
207451689104SPaul Moore 		d = ERR_PTR(-EINVAL);
207551689104SPaul Moore 		goto out;
207679714f72SAl Viro 	}
207779714f72SAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
20781e0ea001SAl Viro 	d = __lookup_hash(&nd.last, nd.path.dentry, 0);
207979714f72SAl Viro 	if (IS_ERR(d)) {
208079714f72SAl Viro 		mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
208179714f72SAl Viro 		path_put(&nd.path);
208251689104SPaul Moore 		goto out;
208379714f72SAl Viro 	}
208479714f72SAl Viro 	*path = nd.path;
208551689104SPaul Moore out:
208651689104SPaul Moore 	putname(filename);
208779714f72SAl Viro 	return d;
20885590ff0dSUlrich Drepper }
20895590ff0dSUlrich Drepper 
2090d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
2091d1811465SAl Viro {
2092d1811465SAl Viro 	struct nameidata nd;
2093d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
2094d1811465SAl Viro 	if (!res)
2095d1811465SAl Viro 		*path = nd.path;
2096d1811465SAl Viro 	return res;
2097d1811465SAl Viro }
20984d359507SAl Viro EXPORT_SYMBOL(kern_path);
2099d1811465SAl Viro 
210016f18200SJosef 'Jeff' Sipek /**
210116f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
210216f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
210316f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
210416f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
210516f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
2106e0a01249SAl Viro  * @path: pointer to struct path to fill
210716f18200SJosef 'Jeff' Sipek  */
210816f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
210916f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
2110e0a01249SAl Viro 		    struct path *path)
211116f18200SJosef 'Jeff' Sipek {
2112e0a01249SAl Viro 	struct nameidata nd;
2113e0a01249SAl Viro 	int err;
2114e0a01249SAl Viro 	nd.root.dentry = dentry;
2115e0a01249SAl Viro 	nd.root.mnt = mnt;
2116e0a01249SAl Viro 	BUG_ON(flags & LOOKUP_PARENT);
21175b6ca027SAl Viro 	/* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
2118e0a01249SAl Viro 	err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
2119e0a01249SAl Viro 	if (!err)
2120e0a01249SAl Viro 		*path = nd.path;
2121e0a01249SAl Viro 	return err;
212216f18200SJosef 'Jeff' Sipek }
21234d359507SAl Viro EXPORT_SYMBOL(vfs_path_lookup);
212416f18200SJosef 'Jeff' Sipek 
2125057f6c01SJames Morris /*
2126057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
2127057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
2128057f6c01SJames Morris  * SMP-safe.
2129057f6c01SJames Morris  */
2130a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
21311da177e4SLinus Torvalds {
213272bd866aSAl Viro 	return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
21331da177e4SLinus Torvalds }
21341da177e4SLinus Torvalds 
2135eead1911SChristoph Hellwig /**
2136a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
2137eead1911SChristoph Hellwig  * @name:	pathname component to lookup
2138eead1911SChristoph Hellwig  * @base:	base directory to lookup from
2139eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
2140eead1911SChristoph Hellwig  *
2141a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
2142a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
2143eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
2144eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
2145eead1911SChristoph Hellwig  */
2146057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2147057f6c01SJames Morris {
2148057f6c01SJames Morris 	struct qstr this;
21496a96ba54SAl Viro 	unsigned int c;
2150cda309deSMiklos Szeredi 	int err;
2151057f6c01SJames Morris 
21522f9092e1SDavid Woodhouse 	WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
21532f9092e1SDavid Woodhouse 
21546a96ba54SAl Viro 	this.name = name;
21556a96ba54SAl Viro 	this.len = len;
21560145acc2SLinus Torvalds 	this.hash = full_name_hash(name, len);
21576a96ba54SAl Viro 	if (!len)
21586a96ba54SAl Viro 		return ERR_PTR(-EACCES);
21596a96ba54SAl Viro 
216021d8a15aSAl Viro 	if (unlikely(name[0] == '.')) {
216121d8a15aSAl Viro 		if (len < 2 || (len == 2 && name[1] == '.'))
216221d8a15aSAl Viro 			return ERR_PTR(-EACCES);
216321d8a15aSAl Viro 	}
216421d8a15aSAl Viro 
21656a96ba54SAl Viro 	while (len--) {
21666a96ba54SAl Viro 		c = *(const unsigned char *)name++;
21676a96ba54SAl Viro 		if (c == '/' || c == '\0')
21686a96ba54SAl Viro 			return ERR_PTR(-EACCES);
21696a96ba54SAl Viro 	}
21705a202bcdSAl Viro 	/*
21715a202bcdSAl Viro 	 * See if the low-level filesystem might want
21725a202bcdSAl Viro 	 * to use its own hash..
21735a202bcdSAl Viro 	 */
21745a202bcdSAl Viro 	if (base->d_flags & DCACHE_OP_HASH) {
2175da53be12SLinus Torvalds 		int err = base->d_op->d_hash(base, &this);
21765a202bcdSAl Viro 		if (err < 0)
21775a202bcdSAl Viro 			return ERR_PTR(err);
21785a202bcdSAl Viro 	}
2179eead1911SChristoph Hellwig 
2180cda309deSMiklos Szeredi 	err = inode_permission(base->d_inode, MAY_EXEC);
2181cda309deSMiklos Szeredi 	if (err)
2182cda309deSMiklos Szeredi 		return ERR_PTR(err);
2183cda309deSMiklos Szeredi 
218472bd866aSAl Viro 	return __lookup_hash(&this, base, 0);
2185057f6c01SJames Morris }
21864d359507SAl Viro EXPORT_SYMBOL(lookup_one_len);
2187057f6c01SJames Morris 
21881fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
21891fa1e7f6SAndy Whitcroft 		 struct path *path, int *empty)
21901da177e4SLinus Torvalds {
21912d8f3038SAl Viro 	struct nameidata nd;
219291a27b2aSJeff Layton 	struct filename *tmp = getname_flags(name, flags, empty);
21931da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
21941da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
21952d8f3038SAl Viro 
21962d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
21972d8f3038SAl Viro 
2198873f1eedSJeff Layton 		err = filename_lookup(dfd, tmp, flags, &nd);
21991da177e4SLinus Torvalds 		putname(tmp);
22002d8f3038SAl Viro 		if (!err)
22012d8f3038SAl Viro 			*path = nd.path;
22021da177e4SLinus Torvalds 	}
22031da177e4SLinus Torvalds 	return err;
22041da177e4SLinus Torvalds }
22051da177e4SLinus Torvalds 
22061fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags,
22071fa1e7f6SAndy Whitcroft 		 struct path *path)
22081fa1e7f6SAndy Whitcroft {
2209f7493e5dSLinus Torvalds 	return user_path_at_empty(dfd, name, flags, path, NULL);
22101fa1e7f6SAndy Whitcroft }
22114d359507SAl Viro EXPORT_SYMBOL(user_path_at);
22121fa1e7f6SAndy Whitcroft 
2213873f1eedSJeff Layton /*
2214873f1eedSJeff Layton  * NB: most callers don't do anything directly with the reference to the
2215873f1eedSJeff Layton  *     to struct filename, but the nd->last pointer points into the name string
2216873f1eedSJeff Layton  *     allocated by getname. So we must hold the reference to it until all
2217873f1eedSJeff Layton  *     path-walking is complete.
2218873f1eedSJeff Layton  */
221991a27b2aSJeff Layton static struct filename *
22209e790bd6SJeff Layton user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
22219e790bd6SJeff Layton 		 unsigned int flags)
22222ad94ae6SAl Viro {
222391a27b2aSJeff Layton 	struct filename *s = getname(path);
22242ad94ae6SAl Viro 	int error;
22252ad94ae6SAl Viro 
22269e790bd6SJeff Layton 	/* only LOOKUP_REVAL is allowed in extra flags */
22279e790bd6SJeff Layton 	flags &= LOOKUP_REVAL;
22289e790bd6SJeff Layton 
22292ad94ae6SAl Viro 	if (IS_ERR(s))
223091a27b2aSJeff Layton 		return s;
22312ad94ae6SAl Viro 
22329e790bd6SJeff Layton 	error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
223391a27b2aSJeff Layton 	if (error) {
22342ad94ae6SAl Viro 		putname(s);
223591a27b2aSJeff Layton 		return ERR_PTR(error);
223691a27b2aSJeff Layton 	}
22372ad94ae6SAl Viro 
223891a27b2aSJeff Layton 	return s;
22392ad94ae6SAl Viro }
22402ad94ae6SAl Viro 
22418033426eSJeff Layton /**
2242197df04cSAl Viro  * mountpoint_last - look up last component for umount
22438033426eSJeff Layton  * @nd:   pathwalk nameidata - currently pointing at parent directory of "last"
22448033426eSJeff Layton  * @path: pointer to container for result
22458033426eSJeff Layton  *
22468033426eSJeff Layton  * This is a special lookup_last function just for umount. In this case, we
22478033426eSJeff Layton  * need to resolve the path without doing any revalidation.
22488033426eSJeff Layton  *
22498033426eSJeff Layton  * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
22508033426eSJeff Layton  * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
22518033426eSJeff Layton  * in almost all cases, this lookup will be served out of the dcache. The only
22528033426eSJeff Layton  * cases where it won't are if nd->last refers to a symlink or the path is
22538033426eSJeff Layton  * bogus and it doesn't exist.
22548033426eSJeff Layton  *
22558033426eSJeff Layton  * Returns:
22568033426eSJeff Layton  * -error: if there was an error during lookup. This includes -ENOENT if the
22578033426eSJeff Layton  *         lookup found a negative dentry. The nd->path reference will also be
22588033426eSJeff Layton  *         put in this case.
22598033426eSJeff Layton  *
22608033426eSJeff Layton  * 0:      if we successfully resolved nd->path and found it to not to be a
22618033426eSJeff Layton  *         symlink that needs to be followed. "path" will also be populated.
22628033426eSJeff Layton  *         The nd->path reference will also be put.
22638033426eSJeff Layton  *
22648033426eSJeff Layton  * 1:      if we successfully resolved nd->last and found it to be a symlink
22658033426eSJeff Layton  *         that needs to be followed. "path" will be populated with the path
22668033426eSJeff Layton  *         to the link, and nd->path will *not* be put.
22678033426eSJeff Layton  */
22688033426eSJeff Layton static int
2269197df04cSAl Viro mountpoint_last(struct nameidata *nd, struct path *path)
22708033426eSJeff Layton {
22718033426eSJeff Layton 	int error = 0;
22728033426eSJeff Layton 	struct dentry *dentry;
22738033426eSJeff Layton 	struct dentry *dir = nd->path.dentry;
22748033426eSJeff Layton 
227535759521SAl Viro 	/* If we're in rcuwalk, drop out of it to handle last component */
227635759521SAl Viro 	if (nd->flags & LOOKUP_RCU) {
227735759521SAl Viro 		if (unlazy_walk(nd, NULL)) {
22788033426eSJeff Layton 			error = -ECHILD;
227935759521SAl Viro 			goto out;
228035759521SAl Viro 		}
22818033426eSJeff Layton 	}
22828033426eSJeff Layton 
22838033426eSJeff Layton 	nd->flags &= ~LOOKUP_PARENT;
22848033426eSJeff Layton 
22858033426eSJeff Layton 	if (unlikely(nd->last_type != LAST_NORM)) {
22868033426eSJeff Layton 		error = handle_dots(nd, nd->last_type);
228735759521SAl Viro 		if (error)
228835759521SAl Viro 			goto out;
22898033426eSJeff Layton 		dentry = dget(nd->path.dentry);
229035759521SAl Viro 		goto done;
22918033426eSJeff Layton 	}
22928033426eSJeff Layton 
22938033426eSJeff Layton 	mutex_lock(&dir->d_inode->i_mutex);
22948033426eSJeff Layton 	dentry = d_lookup(dir, &nd->last);
22958033426eSJeff Layton 	if (!dentry) {
22968033426eSJeff Layton 		/*
22978033426eSJeff Layton 		 * No cached dentry. Mounted dentries are pinned in the cache,
22988033426eSJeff Layton 		 * so that means that this dentry is probably a symlink or the
22998033426eSJeff Layton 		 * path doesn't actually point to a mounted dentry.
23008033426eSJeff Layton 		 */
23018033426eSJeff Layton 		dentry = d_alloc(dir, &nd->last);
23028033426eSJeff Layton 		if (!dentry) {
23038033426eSJeff Layton 			error = -ENOMEM;
2304bcceeebaSDave Jones 			mutex_unlock(&dir->d_inode->i_mutex);
230535759521SAl Viro 			goto out;
23068033426eSJeff Layton 		}
230735759521SAl Viro 		dentry = lookup_real(dir->d_inode, dentry, nd->flags);
230835759521SAl Viro 		error = PTR_ERR(dentry);
2309bcceeebaSDave Jones 		if (IS_ERR(dentry)) {
2310bcceeebaSDave Jones 			mutex_unlock(&dir->d_inode->i_mutex);
231135759521SAl Viro 			goto out;
23128033426eSJeff Layton 		}
2313bcceeebaSDave Jones 	}
23148033426eSJeff Layton 	mutex_unlock(&dir->d_inode->i_mutex);
23158033426eSJeff Layton 
231635759521SAl Viro done:
231722213318SAl Viro 	if (!dentry->d_inode || d_is_negative(dentry)) {
23188033426eSJeff Layton 		error = -ENOENT;
23198033426eSJeff Layton 		dput(dentry);
232035759521SAl Viro 		goto out;
232135759521SAl Viro 	}
23228033426eSJeff Layton 	path->dentry = dentry;
2323295dc39dSVasily Averin 	path->mnt = nd->path.mnt;
2324b18825a7SDavid Howells 	if (should_follow_link(dentry, nd->flags & LOOKUP_FOLLOW))
23258033426eSJeff Layton 		return 1;
2326295dc39dSVasily Averin 	mntget(path->mnt);
23278033426eSJeff Layton 	follow_mount(path);
232835759521SAl Viro 	error = 0;
232935759521SAl Viro out:
23308033426eSJeff Layton 	terminate_walk(nd);
23318033426eSJeff Layton 	return error;
23328033426eSJeff Layton }
23338033426eSJeff Layton 
23348033426eSJeff Layton /**
2335197df04cSAl Viro  * path_mountpoint - look up a path to be umounted
23368033426eSJeff Layton  * @dfd:	directory file descriptor to start walk from
23378033426eSJeff Layton  * @name:	full pathname to walk
2338606d6fe3SRandy Dunlap  * @path:	pointer to container for result
23398033426eSJeff Layton  * @flags:	lookup flags
23408033426eSJeff Layton  *
23418033426eSJeff Layton  * Look up the given name, but don't attempt to revalidate the last component.
2342606d6fe3SRandy Dunlap  * Returns 0 and "path" will be valid on success; Returns error otherwise.
23438033426eSJeff Layton  */
23448033426eSJeff Layton static int
2345197df04cSAl Viro path_mountpoint(int dfd, const char *name, struct path *path, unsigned int flags)
23468033426eSJeff Layton {
23478033426eSJeff Layton 	struct nameidata nd;
23488033426eSJeff Layton 	int err;
23498033426eSJeff Layton 
2350980f3ea2SAl Viro 	err = path_init(dfd, name, flags, &nd);
23518033426eSJeff Layton 	if (unlikely(err))
2352115cbfdcSAl Viro 		goto out;
23538033426eSJeff Layton 
2354197df04cSAl Viro 	err = mountpoint_last(&nd, path);
23558033426eSJeff Layton 	while (err > 0) {
23568033426eSJeff Layton 		void *cookie;
23578033426eSJeff Layton 		struct path link = *path;
23588033426eSJeff Layton 		err = may_follow_link(&link, &nd);
23598033426eSJeff Layton 		if (unlikely(err))
23608033426eSJeff Layton 			break;
23618033426eSJeff Layton 		nd.flags |= LOOKUP_PARENT;
23628033426eSJeff Layton 		err = follow_link(&link, &nd, &cookie);
23638033426eSJeff Layton 		if (err)
23648033426eSJeff Layton 			break;
2365197df04cSAl Viro 		err = mountpoint_last(&nd, path);
23668033426eSJeff Layton 		put_link(&nd, &link, cookie);
23678033426eSJeff Layton 	}
23688033426eSJeff Layton out:
2369893b7775SAl Viro 	path_cleanup(&nd);
23708033426eSJeff Layton 	return err;
23718033426eSJeff Layton }
23728033426eSJeff Layton 
23732d864651SAl Viro static int
23742d864651SAl Viro filename_mountpoint(int dfd, struct filename *s, struct path *path,
23752d864651SAl Viro 			unsigned int flags)
23762d864651SAl Viro {
2377cbaab2dbSAl Viro 	int error;
2378cbaab2dbSAl Viro 	if (IS_ERR(s))
2379cbaab2dbSAl Viro 		return PTR_ERR(s);
2380cbaab2dbSAl Viro 	error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_RCU);
23812d864651SAl Viro 	if (unlikely(error == -ECHILD))
23822d864651SAl Viro 		error = path_mountpoint(dfd, s->name, path, flags);
23832d864651SAl Viro 	if (unlikely(error == -ESTALE))
23842d864651SAl Viro 		error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_REVAL);
23852d864651SAl Viro 	if (likely(!error))
23862d864651SAl Viro 		audit_inode(s, path->dentry, 0);
2387cbaab2dbSAl Viro 	putname(s);
23882d864651SAl Viro 	return error;
23892d864651SAl Viro }
23902d864651SAl Viro 
23918033426eSJeff Layton /**
2392197df04cSAl Viro  * user_path_mountpoint_at - lookup a path from userland in order to umount it
23938033426eSJeff Layton  * @dfd:	directory file descriptor
23948033426eSJeff Layton  * @name:	pathname from userland
23958033426eSJeff Layton  * @flags:	lookup flags
23968033426eSJeff Layton  * @path:	pointer to container to hold result
23978033426eSJeff Layton  *
23988033426eSJeff Layton  * A umount is a special case for path walking. We're not actually interested
23998033426eSJeff Layton  * in the inode in this situation, and ESTALE errors can be a problem. We
24008033426eSJeff Layton  * simply want track down the dentry and vfsmount attached at the mountpoint
24018033426eSJeff Layton  * and avoid revalidating the last component.
24028033426eSJeff Layton  *
24038033426eSJeff Layton  * Returns 0 and populates "path" on success.
24048033426eSJeff Layton  */
24058033426eSJeff Layton int
2406197df04cSAl Viro user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
24078033426eSJeff Layton 			struct path *path)
24088033426eSJeff Layton {
2409cbaab2dbSAl Viro 	return filename_mountpoint(dfd, getname(name), path, flags);
24108033426eSJeff Layton }
24118033426eSJeff Layton 
24122d864651SAl Viro int
24132d864651SAl Viro kern_path_mountpoint(int dfd, const char *name, struct path *path,
24142d864651SAl Viro 			unsigned int flags)
24152d864651SAl Viro {
2416cbaab2dbSAl Viro 	return filename_mountpoint(dfd, getname_kernel(name), path, flags);
24172d864651SAl Viro }
24182d864651SAl Viro EXPORT_SYMBOL(kern_path_mountpoint);
24192d864651SAl Viro 
2420cbdf35bcSMiklos Szeredi int __check_sticky(struct inode *dir, struct inode *inode)
24211da177e4SLinus Torvalds {
24228e96e3b7SEric W. Biederman 	kuid_t fsuid = current_fsuid();
2423da9592edSDavid Howells 
24248e96e3b7SEric W. Biederman 	if (uid_eq(inode->i_uid, fsuid))
24251da177e4SLinus Torvalds 		return 0;
24268e96e3b7SEric W. Biederman 	if (uid_eq(dir->i_uid, fsuid))
24271da177e4SLinus Torvalds 		return 0;
242823adbe12SAndy Lutomirski 	return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
24291da177e4SLinus Torvalds }
2430cbdf35bcSMiklos Szeredi EXPORT_SYMBOL(__check_sticky);
24311da177e4SLinus Torvalds 
24321da177e4SLinus Torvalds /*
24331da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
24341da177e4SLinus Torvalds  *  whether the type of victim is right.
24351da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
24361da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
24371da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
24381da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
24391da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
24401da177e4SLinus Torvalds  *	a. be owner of dir, or
24411da177e4SLinus Torvalds  *	b. be owner of victim, or
24421da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
24431da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
24441da177e4SLinus Torvalds  *     links pointing to it.
24451da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
24461da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
24471da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
24481da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
24491da177e4SLinus Torvalds  *     nfs_async_unlink().
24501da177e4SLinus Torvalds  */
2451b18825a7SDavid Howells static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
24521da177e4SLinus Torvalds {
2453b18825a7SDavid Howells 	struct inode *inode = victim->d_inode;
24541da177e4SLinus Torvalds 	int error;
24551da177e4SLinus Torvalds 
2456b18825a7SDavid Howells 	if (d_is_negative(victim))
24571da177e4SLinus Torvalds 		return -ENOENT;
2458b18825a7SDavid Howells 	BUG_ON(!inode);
24591da177e4SLinus Torvalds 
24601da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
24614fa6b5ecSJeff Layton 	audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
24621da177e4SLinus Torvalds 
2463f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
24641da177e4SLinus Torvalds 	if (error)
24651da177e4SLinus Torvalds 		return error;
24661da177e4SLinus Torvalds 	if (IS_APPEND(dir))
24671da177e4SLinus Torvalds 		return -EPERM;
2468b18825a7SDavid Howells 
2469b18825a7SDavid Howells 	if (check_sticky(dir, inode) || IS_APPEND(inode) ||
2470b18825a7SDavid Howells 	    IS_IMMUTABLE(inode) || IS_SWAPFILE(inode))
24711da177e4SLinus Torvalds 		return -EPERM;
24721da177e4SLinus Torvalds 	if (isdir) {
247344b1d530SMiklos Szeredi 		if (!d_is_dir(victim))
24741da177e4SLinus Torvalds 			return -ENOTDIR;
24751da177e4SLinus Torvalds 		if (IS_ROOT(victim))
24761da177e4SLinus Torvalds 			return -EBUSY;
247744b1d530SMiklos Szeredi 	} else if (d_is_dir(victim))
24781da177e4SLinus Torvalds 		return -EISDIR;
24791da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
24801da177e4SLinus Torvalds 		return -ENOENT;
24811da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
24821da177e4SLinus Torvalds 		return -EBUSY;
24831da177e4SLinus Torvalds 	return 0;
24841da177e4SLinus Torvalds }
24851da177e4SLinus Torvalds 
24861da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
24871da177e4SLinus Torvalds  *  dir.
24881da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
24891da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
24901da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
24911da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
24921da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
24931da177e4SLinus Torvalds  */
2494a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
24951da177e4SLinus Torvalds {
249614e972b4SJeff Layton 	audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
24971da177e4SLinus Torvalds 	if (child->d_inode)
24981da177e4SLinus Torvalds 		return -EEXIST;
24991da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
25001da177e4SLinus Torvalds 		return -ENOENT;
2501f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
25021da177e4SLinus Torvalds }
25031da177e4SLinus Torvalds 
25041da177e4SLinus Torvalds /*
25051da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
25061da177e4SLinus Torvalds  */
25071da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
25081da177e4SLinus Torvalds {
25091da177e4SLinus Torvalds 	struct dentry *p;
25101da177e4SLinus Torvalds 
25111da177e4SLinus Torvalds 	if (p1 == p2) {
2512f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
25131da177e4SLinus Torvalds 		return NULL;
25141da177e4SLinus Torvalds 	}
25151da177e4SLinus Torvalds 
2516a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
25171da177e4SLinus Torvalds 
2518e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
2519e2761a11SOGAWA Hirofumi 	if (p) {
2520f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
2521f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
25221da177e4SLinus Torvalds 		return p;
25231da177e4SLinus Torvalds 	}
25241da177e4SLinus Torvalds 
2525e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
2526e2761a11SOGAWA Hirofumi 	if (p) {
2527f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2528f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
25291da177e4SLinus Torvalds 		return p;
25301da177e4SLinus Torvalds 	}
25311da177e4SLinus Torvalds 
2532f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
2533d1b72cc6SMiklos Szeredi 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT2);
25341da177e4SLinus Torvalds 	return NULL;
25351da177e4SLinus Torvalds }
25364d359507SAl Viro EXPORT_SYMBOL(lock_rename);
25371da177e4SLinus Torvalds 
25381da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
25391da177e4SLinus Torvalds {
25401b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
25411da177e4SLinus Torvalds 	if (p1 != p2) {
25421b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
2543a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
25441da177e4SLinus Torvalds 	}
25451da177e4SLinus Torvalds }
25464d359507SAl Viro EXPORT_SYMBOL(unlock_rename);
25471da177e4SLinus Torvalds 
25484acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2549312b63fbSAl Viro 		bool want_excl)
25501da177e4SLinus Torvalds {
2551a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
25521da177e4SLinus Torvalds 	if (error)
25531da177e4SLinus Torvalds 		return error;
25541da177e4SLinus Torvalds 
2555acfa4380SAl Viro 	if (!dir->i_op->create)
25561da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
25571da177e4SLinus Torvalds 	mode &= S_IALLUGO;
25581da177e4SLinus Torvalds 	mode |= S_IFREG;
25591da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
25601da177e4SLinus Torvalds 	if (error)
25611da177e4SLinus Torvalds 		return error;
2562312b63fbSAl Viro 	error = dir->i_op->create(dir, dentry, mode, want_excl);
2563a74574aaSStephen Smalley 	if (!error)
2564f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
25651da177e4SLinus Torvalds 	return error;
25661da177e4SLinus Torvalds }
25674d359507SAl Viro EXPORT_SYMBOL(vfs_create);
25681da177e4SLinus Torvalds 
256973d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag)
25701da177e4SLinus Torvalds {
25713fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
25721da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
25731da177e4SLinus Torvalds 	int error;
25741da177e4SLinus Torvalds 
2575bcda7652SAl Viro 	/* O_PATH? */
2576bcda7652SAl Viro 	if (!acc_mode)
2577bcda7652SAl Viro 		return 0;
2578bcda7652SAl Viro 
25791da177e4SLinus Torvalds 	if (!inode)
25801da177e4SLinus Torvalds 		return -ENOENT;
25811da177e4SLinus Torvalds 
2582c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
2583c8fe8f30SChristoph Hellwig 	case S_IFLNK:
25841da177e4SLinus Torvalds 		return -ELOOP;
2585c8fe8f30SChristoph Hellwig 	case S_IFDIR:
2586c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
25871da177e4SLinus Torvalds 			return -EISDIR;
2588c8fe8f30SChristoph Hellwig 		break;
2589c8fe8f30SChristoph Hellwig 	case S_IFBLK:
2590c8fe8f30SChristoph Hellwig 	case S_IFCHR:
25913fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
25921da177e4SLinus Torvalds 			return -EACCES;
2593c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
2594c8fe8f30SChristoph Hellwig 	case S_IFIFO:
2595c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
25961da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
2597c8fe8f30SChristoph Hellwig 		break;
25984a3fd211SDave Hansen 	}
2599b41572e9SDave Hansen 
26003fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
2601b41572e9SDave Hansen 	if (error)
2602b41572e9SDave Hansen 		return error;
26036146f0d5SMimi Zohar 
26041da177e4SLinus Torvalds 	/*
26051da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
26061da177e4SLinus Torvalds 	 */
26071da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
26088737c930SAl Viro 		if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
26097715b521SAl Viro 			return -EPERM;
26101da177e4SLinus Torvalds 		if (flag & O_TRUNC)
26117715b521SAl Viro 			return -EPERM;
26121da177e4SLinus Torvalds 	}
26131da177e4SLinus Torvalds 
26141da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
26152e149670SSerge E. Hallyn 	if (flag & O_NOATIME && !inode_owner_or_capable(inode))
26167715b521SAl Viro 		return -EPERM;
26171da177e4SLinus Torvalds 
2618f3c7691eSJ. Bruce Fields 	return 0;
26197715b521SAl Viro }
26207715b521SAl Viro 
2621e1181ee6SJeff Layton static int handle_truncate(struct file *filp)
26227715b521SAl Viro {
2623e1181ee6SJeff Layton 	struct path *path = &filp->f_path;
26247715b521SAl Viro 	struct inode *inode = path->dentry->d_inode;
26257715b521SAl Viro 	int error = get_write_access(inode);
26261da177e4SLinus Torvalds 	if (error)
26277715b521SAl Viro 		return error;
26281da177e4SLinus Torvalds 	/*
26291da177e4SLinus Torvalds 	 * Refuse to truncate files with mandatory locks held on them.
26301da177e4SLinus Torvalds 	 */
2631d7a06983SJeff Layton 	error = locks_verify_locked(filp);
2632be6d3e56SKentaro Takeda 	if (!error)
2633ea0d3ab2STetsuo Handa 		error = security_path_truncate(path);
26341da177e4SLinus Torvalds 	if (!error) {
26357715b521SAl Viro 		error = do_truncate(path->dentry, 0,
2636d139d7ffSMiklos Szeredi 				    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2637e1181ee6SJeff Layton 				    filp);
26381da177e4SLinus Torvalds 	}
26391da177e4SLinus Torvalds 	put_write_access(inode);
2640acd0c935SMimi Zohar 	return error;
26411da177e4SLinus Torvalds }
26421da177e4SLinus Torvalds 
2643d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
2644d57999e1SDave Hansen {
26458a5e929dSAl Viro 	if ((flag & O_ACCMODE) == 3)
26468a5e929dSAl Viro 		flag--;
2647d57999e1SDave Hansen 	return flag;
2648d57999e1SDave Hansen }
2649d57999e1SDave Hansen 
2650d18e9008SMiklos Szeredi static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
2651d18e9008SMiklos Szeredi {
2652d18e9008SMiklos Szeredi 	int error = security_path_mknod(dir, dentry, mode, 0);
2653d18e9008SMiklos Szeredi 	if (error)
2654d18e9008SMiklos Szeredi 		return error;
2655d18e9008SMiklos Szeredi 
2656d18e9008SMiklos Szeredi 	error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2657d18e9008SMiklos Szeredi 	if (error)
2658d18e9008SMiklos Szeredi 		return error;
2659d18e9008SMiklos Szeredi 
2660d18e9008SMiklos Szeredi 	return security_inode_create(dir->dentry->d_inode, dentry, mode);
2661d18e9008SMiklos Szeredi }
2662d18e9008SMiklos Szeredi 
26631acf0af9SDavid Howells /*
26641acf0af9SDavid Howells  * Attempt to atomically look up, create and open a file from a negative
26651acf0af9SDavid Howells  * dentry.
26661acf0af9SDavid Howells  *
26671acf0af9SDavid Howells  * Returns 0 if successful.  The file will have been created and attached to
26681acf0af9SDavid Howells  * @file by the filesystem calling finish_open().
26691acf0af9SDavid Howells  *
26701acf0af9SDavid Howells  * Returns 1 if the file was looked up only or didn't need creating.  The
26711acf0af9SDavid Howells  * caller will need to perform the open themselves.  @path will have been
26721acf0af9SDavid Howells  * updated to point to the new dentry.  This may be negative.
26731acf0af9SDavid Howells  *
26741acf0af9SDavid Howells  * Returns an error code otherwise.
26751acf0af9SDavid Howells  */
26762675a4ebSAl Viro static int atomic_open(struct nameidata *nd, struct dentry *dentry,
267730d90494SAl Viro 			struct path *path, struct file *file,
2678015c3bbcSMiklos Szeredi 			const struct open_flags *op,
267964894cf8SAl Viro 			bool got_write, bool need_lookup,
268047237687SAl Viro 			int *opened)
2681d18e9008SMiklos Szeredi {
2682d18e9008SMiklos Szeredi 	struct inode *dir =  nd->path.dentry->d_inode;
2683d18e9008SMiklos Szeredi 	unsigned open_flag = open_to_namei_flags(op->open_flag);
2684d18e9008SMiklos Szeredi 	umode_t mode;
2685d18e9008SMiklos Szeredi 	int error;
2686d18e9008SMiklos Szeredi 	int acc_mode;
2687d18e9008SMiklos Szeredi 	int create_error = 0;
2688d18e9008SMiklos Szeredi 	struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
2689116cc022SMiklos Szeredi 	bool excl;
2690d18e9008SMiklos Szeredi 
2691d18e9008SMiklos Szeredi 	BUG_ON(dentry->d_inode);
2692d18e9008SMiklos Szeredi 
2693d18e9008SMiklos Szeredi 	/* Don't create child dentry for a dead directory. */
2694d18e9008SMiklos Szeredi 	if (unlikely(IS_DEADDIR(dir))) {
26952675a4ebSAl Viro 		error = -ENOENT;
2696d18e9008SMiklos Szeredi 		goto out;
2697d18e9008SMiklos Szeredi 	}
2698d18e9008SMiklos Szeredi 
269962b259d8SMiklos Szeredi 	mode = op->mode;
2700d18e9008SMiklos Szeredi 	if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
2701d18e9008SMiklos Szeredi 		mode &= ~current_umask();
2702d18e9008SMiklos Szeredi 
2703116cc022SMiklos Szeredi 	excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
2704116cc022SMiklos Szeredi 	if (excl)
2705d18e9008SMiklos Szeredi 		open_flag &= ~O_TRUNC;
2706d18e9008SMiklos Szeredi 
2707d18e9008SMiklos Szeredi 	/*
2708d18e9008SMiklos Szeredi 	 * Checking write permission is tricky, bacuse we don't know if we are
2709d18e9008SMiklos Szeredi 	 * going to actually need it: O_CREAT opens should work as long as the
2710d18e9008SMiklos Szeredi 	 * file exists.  But checking existence breaks atomicity.  The trick is
2711d18e9008SMiklos Szeredi 	 * to check access and if not granted clear O_CREAT from the flags.
2712d18e9008SMiklos Szeredi 	 *
2713d18e9008SMiklos Szeredi 	 * Another problem is returing the "right" error value (e.g. for an
2714d18e9008SMiklos Szeredi 	 * O_EXCL open we want to return EEXIST not EROFS).
2715d18e9008SMiklos Szeredi 	 */
271664894cf8SAl Viro 	if (((open_flag & (O_CREAT | O_TRUNC)) ||
271764894cf8SAl Viro 	    (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
271864894cf8SAl Viro 		if (!(open_flag & O_CREAT)) {
2719d18e9008SMiklos Szeredi 			/*
2720d18e9008SMiklos Szeredi 			 * No O_CREATE -> atomicity not a requirement -> fall
2721d18e9008SMiklos Szeredi 			 * back to lookup + open
2722d18e9008SMiklos Szeredi 			 */
2723d18e9008SMiklos Szeredi 			goto no_open;
2724d18e9008SMiklos Szeredi 		} else if (open_flag & (O_EXCL | O_TRUNC)) {
2725d18e9008SMiklos Szeredi 			/* Fall back and fail with the right error */
272664894cf8SAl Viro 			create_error = -EROFS;
2727d18e9008SMiklos Szeredi 			goto no_open;
2728d18e9008SMiklos Szeredi 		} else {
2729d18e9008SMiklos Szeredi 			/* No side effects, safe to clear O_CREAT */
273064894cf8SAl Viro 			create_error = -EROFS;
2731d18e9008SMiklos Szeredi 			open_flag &= ~O_CREAT;
2732d18e9008SMiklos Szeredi 		}
2733d18e9008SMiklos Szeredi 	}
2734d18e9008SMiklos Szeredi 
2735d18e9008SMiklos Szeredi 	if (open_flag & O_CREAT) {
273638227f78SMiklos Szeredi 		error = may_o_create(&nd->path, dentry, mode);
2737d18e9008SMiklos Szeredi 		if (error) {
2738d18e9008SMiklos Szeredi 			create_error = error;
2739d18e9008SMiklos Szeredi 			if (open_flag & O_EXCL)
2740d18e9008SMiklos Szeredi 				goto no_open;
2741d18e9008SMiklos Szeredi 			open_flag &= ~O_CREAT;
2742d18e9008SMiklos Szeredi 		}
2743d18e9008SMiklos Szeredi 	}
2744d18e9008SMiklos Szeredi 
2745d18e9008SMiklos Szeredi 	if (nd->flags & LOOKUP_DIRECTORY)
2746d18e9008SMiklos Szeredi 		open_flag |= O_DIRECTORY;
2747d18e9008SMiklos Szeredi 
274830d90494SAl Viro 	file->f_path.dentry = DENTRY_NOT_SET;
274930d90494SAl Viro 	file->f_path.mnt = nd->path.mnt;
275030d90494SAl Viro 	error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
275147237687SAl Viro 				      opened);
2752d9585277SAl Viro 	if (error < 0) {
2753d9585277SAl Viro 		if (create_error && error == -ENOENT)
2754d9585277SAl Viro 			error = create_error;
2755d18e9008SMiklos Szeredi 		goto out;
2756d18e9008SMiklos Szeredi 	}
2757d18e9008SMiklos Szeredi 
2758d9585277SAl Viro 	if (error) {	/* returned 1, that is */
275930d90494SAl Viro 		if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
27602675a4ebSAl Viro 			error = -EIO;
2761d18e9008SMiklos Szeredi 			goto out;
2762d18e9008SMiklos Szeredi 		}
276330d90494SAl Viro 		if (file->f_path.dentry) {
2764d18e9008SMiklos Szeredi 			dput(dentry);
276530d90494SAl Viro 			dentry = file->f_path.dentry;
2766d18e9008SMiklos Szeredi 		}
276703da633aSAl Viro 		if (*opened & FILE_CREATED)
276803da633aSAl Viro 			fsnotify_create(dir, dentry);
276903da633aSAl Viro 		if (!dentry->d_inode) {
277003da633aSAl Viro 			WARN_ON(*opened & FILE_CREATED);
277103da633aSAl Viro 			if (create_error) {
277262b2ce96SSage Weil 				error = create_error;
277362b2ce96SSage Weil 				goto out;
277462b2ce96SSage Weil 			}
277503da633aSAl Viro 		} else {
277603da633aSAl Viro 			if (excl && !(*opened & FILE_CREATED)) {
277703da633aSAl Viro 				error = -EEXIST;
277803da633aSAl Viro 				goto out;
277903da633aSAl Viro 			}
278003da633aSAl Viro 		}
2781d18e9008SMiklos Szeredi 		goto looked_up;
2782d18e9008SMiklos Szeredi 	}
2783d18e9008SMiklos Szeredi 
2784d18e9008SMiklos Szeredi 	/*
2785d18e9008SMiklos Szeredi 	 * We didn't have the inode before the open, so check open permission
2786d18e9008SMiklos Szeredi 	 * here.
2787d18e9008SMiklos Szeredi 	 */
278803da633aSAl Viro 	acc_mode = op->acc_mode;
278903da633aSAl Viro 	if (*opened & FILE_CREATED) {
279003da633aSAl Viro 		WARN_ON(!(open_flag & O_CREAT));
279103da633aSAl Viro 		fsnotify_create(dir, dentry);
279203da633aSAl Viro 		acc_mode = MAY_OPEN;
279303da633aSAl Viro 	}
27942675a4ebSAl Viro 	error = may_open(&file->f_path, acc_mode, open_flag);
27952675a4ebSAl Viro 	if (error)
27962675a4ebSAl Viro 		fput(file);
2797d18e9008SMiklos Szeredi 
2798d18e9008SMiklos Szeredi out:
2799d18e9008SMiklos Szeredi 	dput(dentry);
28002675a4ebSAl Viro 	return error;
2801d18e9008SMiklos Szeredi 
2802d18e9008SMiklos Szeredi no_open:
2803d18e9008SMiklos Szeredi 	if (need_lookup) {
280472bd866aSAl Viro 		dentry = lookup_real(dir, dentry, nd->flags);
2805d18e9008SMiklos Szeredi 		if (IS_ERR(dentry))
28062675a4ebSAl Viro 			return PTR_ERR(dentry);
2807d18e9008SMiklos Szeredi 
2808d18e9008SMiklos Szeredi 		if (create_error) {
2809d18e9008SMiklos Szeredi 			int open_flag = op->open_flag;
2810d18e9008SMiklos Szeredi 
28112675a4ebSAl Viro 			error = create_error;
2812d18e9008SMiklos Szeredi 			if ((open_flag & O_EXCL)) {
2813d18e9008SMiklos Szeredi 				if (!dentry->d_inode)
2814d18e9008SMiklos Szeredi 					goto out;
2815d18e9008SMiklos Szeredi 			} else if (!dentry->d_inode) {
2816d18e9008SMiklos Szeredi 				goto out;
2817d18e9008SMiklos Szeredi 			} else if ((open_flag & O_TRUNC) &&
2818d18e9008SMiklos Szeredi 				   S_ISREG(dentry->d_inode->i_mode)) {
2819d18e9008SMiklos Szeredi 				goto out;
2820d18e9008SMiklos Szeredi 			}
2821d18e9008SMiklos Szeredi 			/* will fail later, go on to get the right error */
2822d18e9008SMiklos Szeredi 		}
2823d18e9008SMiklos Szeredi 	}
2824d18e9008SMiklos Szeredi looked_up:
2825d18e9008SMiklos Szeredi 	path->dentry = dentry;
2826d18e9008SMiklos Szeredi 	path->mnt = nd->path.mnt;
28272675a4ebSAl Viro 	return 1;
2828d18e9008SMiklos Szeredi }
2829d18e9008SMiklos Szeredi 
283031e6b01fSNick Piggin /*
28311acf0af9SDavid Howells  * Look up and maybe create and open the last component.
2832d58ffd35SMiklos Szeredi  *
2833d58ffd35SMiklos Szeredi  * Must be called with i_mutex held on parent.
2834d58ffd35SMiklos Szeredi  *
28351acf0af9SDavid Howells  * Returns 0 if the file was successfully atomically created (if necessary) and
28361acf0af9SDavid Howells  * opened.  In this case the file will be returned attached to @file.
28371acf0af9SDavid Howells  *
28381acf0af9SDavid Howells  * Returns 1 if the file was not completely opened at this time, though lookups
28391acf0af9SDavid Howells  * and creations will have been performed and the dentry returned in @path will
28401acf0af9SDavid Howells  * be positive upon return if O_CREAT was specified.  If O_CREAT wasn't
28411acf0af9SDavid Howells  * specified then a negative dentry may be returned.
28421acf0af9SDavid Howells  *
28431acf0af9SDavid Howells  * An error code is returned otherwise.
28441acf0af9SDavid Howells  *
28451acf0af9SDavid Howells  * FILE_CREATE will be set in @*opened if the dentry was created and will be
28461acf0af9SDavid Howells  * cleared otherwise prior to returning.
2847d58ffd35SMiklos Szeredi  */
28482675a4ebSAl Viro static int lookup_open(struct nameidata *nd, struct path *path,
284930d90494SAl Viro 			struct file *file,
2850d58ffd35SMiklos Szeredi 			const struct open_flags *op,
285164894cf8SAl Viro 			bool got_write, int *opened)
2852d58ffd35SMiklos Szeredi {
2853d58ffd35SMiklos Szeredi 	struct dentry *dir = nd->path.dentry;
285454ef4872SMiklos Szeredi 	struct inode *dir_inode = dir->d_inode;
2855d58ffd35SMiklos Szeredi 	struct dentry *dentry;
2856d58ffd35SMiklos Szeredi 	int error;
285754ef4872SMiklos Szeredi 	bool need_lookup;
2858d58ffd35SMiklos Szeredi 
285947237687SAl Viro 	*opened &= ~FILE_CREATED;
2860201f956eSAl Viro 	dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
2861d58ffd35SMiklos Szeredi 	if (IS_ERR(dentry))
28622675a4ebSAl Viro 		return PTR_ERR(dentry);
2863d58ffd35SMiklos Szeredi 
2864d18e9008SMiklos Szeredi 	/* Cached positive dentry: will open in f_op->open */
2865d18e9008SMiklos Szeredi 	if (!need_lookup && dentry->d_inode)
2866d18e9008SMiklos Szeredi 		goto out_no_open;
2867d18e9008SMiklos Szeredi 
2868d18e9008SMiklos Szeredi 	if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
286964894cf8SAl Viro 		return atomic_open(nd, dentry, path, file, op, got_write,
287047237687SAl Viro 				   need_lookup, opened);
2871d18e9008SMiklos Szeredi 	}
2872d18e9008SMiklos Szeredi 
287354ef4872SMiklos Szeredi 	if (need_lookup) {
287454ef4872SMiklos Szeredi 		BUG_ON(dentry->d_inode);
287554ef4872SMiklos Szeredi 
287672bd866aSAl Viro 		dentry = lookup_real(dir_inode, dentry, nd->flags);
287754ef4872SMiklos Szeredi 		if (IS_ERR(dentry))
28782675a4ebSAl Viro 			return PTR_ERR(dentry);
287954ef4872SMiklos Szeredi 	}
288054ef4872SMiklos Szeredi 
2881d58ffd35SMiklos Szeredi 	/* Negative dentry, just create the file */
2882d58ffd35SMiklos Szeredi 	if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
2883d58ffd35SMiklos Szeredi 		umode_t mode = op->mode;
2884d58ffd35SMiklos Szeredi 		if (!IS_POSIXACL(dir->d_inode))
2885d58ffd35SMiklos Szeredi 			mode &= ~current_umask();
2886d58ffd35SMiklos Szeredi 		/*
2887d58ffd35SMiklos Szeredi 		 * This write is needed to ensure that a
2888d58ffd35SMiklos Szeredi 		 * rw->ro transition does not occur between
2889d58ffd35SMiklos Szeredi 		 * the time when the file is created and when
2890d58ffd35SMiklos Szeredi 		 * a permanent write count is taken through
2891015c3bbcSMiklos Szeredi 		 * the 'struct file' in finish_open().
2892d58ffd35SMiklos Szeredi 		 */
289364894cf8SAl Viro 		if (!got_write) {
289464894cf8SAl Viro 			error = -EROFS;
2895d58ffd35SMiklos Szeredi 			goto out_dput;
289664894cf8SAl Viro 		}
289747237687SAl Viro 		*opened |= FILE_CREATED;
2898d58ffd35SMiklos Szeredi 		error = security_path_mknod(&nd->path, dentry, mode, 0);
2899d58ffd35SMiklos Szeredi 		if (error)
2900d58ffd35SMiklos Szeredi 			goto out_dput;
2901312b63fbSAl Viro 		error = vfs_create(dir->d_inode, dentry, mode,
2902312b63fbSAl Viro 				   nd->flags & LOOKUP_EXCL);
2903d58ffd35SMiklos Szeredi 		if (error)
2904d58ffd35SMiklos Szeredi 			goto out_dput;
2905d58ffd35SMiklos Szeredi 	}
2906d18e9008SMiklos Szeredi out_no_open:
2907d58ffd35SMiklos Szeredi 	path->dentry = dentry;
2908d58ffd35SMiklos Szeredi 	path->mnt = nd->path.mnt;
29092675a4ebSAl Viro 	return 1;
2910d58ffd35SMiklos Szeredi 
2911d58ffd35SMiklos Szeredi out_dput:
2912d58ffd35SMiklos Szeredi 	dput(dentry);
29132675a4ebSAl Viro 	return error;
2914d58ffd35SMiklos Szeredi }
2915d58ffd35SMiklos Szeredi 
2916d58ffd35SMiklos Szeredi /*
2917fe2d35ffSAl Viro  * Handle the last step of open()
291831e6b01fSNick Piggin  */
29192675a4ebSAl Viro static int do_last(struct nameidata *nd, struct path *path,
292030d90494SAl Viro 		   struct file *file, const struct open_flags *op,
2921669abf4eSJeff Layton 		   int *opened, struct filename *name)
2922fb1cc555SAl Viro {
2923a1e28038SAl Viro 	struct dentry *dir = nd->path.dentry;
2924ca344a89SAl Viro 	int open_flag = op->open_flag;
292577d660a8SMiklos Szeredi 	bool will_truncate = (open_flag & O_TRUNC) != 0;
292664894cf8SAl Viro 	bool got_write = false;
2927bcda7652SAl Viro 	int acc_mode = op->acc_mode;
2928a1eb3315SMiklos Szeredi 	struct inode *inode;
292977d660a8SMiklos Szeredi 	bool symlink_ok = false;
293016b1c1cdSMiklos Szeredi 	struct path save_parent = { .dentry = NULL, .mnt = NULL };
293116b1c1cdSMiklos Szeredi 	bool retried = false;
293216c2cd71SAl Viro 	int error;
2933fb1cc555SAl Viro 
2934c3e380b0SAl Viro 	nd->flags &= ~LOOKUP_PARENT;
2935c3e380b0SAl Viro 	nd->flags |= op->intent;
2936c3e380b0SAl Viro 
2937bc77daa7SAl Viro 	if (nd->last_type != LAST_NORM) {
2938fe2d35ffSAl Viro 		error = handle_dots(nd, nd->last_type);
2939fe2d35ffSAl Viro 		if (error)
29402675a4ebSAl Viro 			return error;
2941e83db167SMiklos Szeredi 		goto finish_open;
29421f36f774SAl Viro 	}
2943a2c36b45SAl Viro 
2944ca344a89SAl Viro 	if (!(open_flag & O_CREAT)) {
2945fe2d35ffSAl Viro 		if (nd->last.name[nd->last.len])
2946fe2d35ffSAl Viro 			nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2947bcda7652SAl Viro 		if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
294877d660a8SMiklos Szeredi 			symlink_ok = true;
2949fe2d35ffSAl Viro 		/* we _can_ be in RCU mode here */
2950e97cdc87SAl Viro 		error = lookup_fast(nd, path, &inode);
295171574865SMiklos Szeredi 		if (likely(!error))
295271574865SMiklos Szeredi 			goto finish_lookup;
295371574865SMiklos Szeredi 
2954ce57dfc1SAl Viro 		if (error < 0)
29552675a4ebSAl Viro 			goto out;
2956a1eb3315SMiklos Szeredi 
295737d7fffcSMiklos Szeredi 		BUG_ON(nd->inode != dir->d_inode);
2958b6183df7SMiklos Szeredi 	} else {
2959fe2d35ffSAl Viro 		/* create side of things */
2960a3fbbde7SAl Viro 		/*
2961b6183df7SMiklos Szeredi 		 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
2962b6183df7SMiklos Szeredi 		 * has been cleared when we got to the last component we are
2963b6183df7SMiklos Szeredi 		 * about to look up
2964a3fbbde7SAl Viro 		 */
29659f1fafeeSAl Viro 		error = complete_walk(nd);
29669f1fafeeSAl Viro 		if (error)
29672675a4ebSAl Viro 			return error;
2968fe2d35ffSAl Viro 
296933e2208aSJeff Layton 		audit_inode(name, dir, LOOKUP_PARENT);
297016c2cd71SAl Viro 		error = -EISDIR;
29711f36f774SAl Viro 		/* trailing slashes? */
297231e6b01fSNick Piggin 		if (nd->last.name[nd->last.len])
29732675a4ebSAl Viro 			goto out;
2974b6183df7SMiklos Szeredi 	}
29751f36f774SAl Viro 
297616b1c1cdSMiklos Szeredi retry_lookup:
297764894cf8SAl Viro 	if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
297864894cf8SAl Viro 		error = mnt_want_write(nd->path.mnt);
297964894cf8SAl Viro 		if (!error)
298064894cf8SAl Viro 			got_write = true;
298164894cf8SAl Viro 		/*
298264894cf8SAl Viro 		 * do _not_ fail yet - we might not need that or fail with
298364894cf8SAl Viro 		 * a different error; let lookup_open() decide; we'll be
298464894cf8SAl Viro 		 * dropping this one anyway.
298564894cf8SAl Viro 		 */
298664894cf8SAl Viro 	}
2987a1e28038SAl Viro 	mutex_lock(&dir->d_inode->i_mutex);
298864894cf8SAl Viro 	error = lookup_open(nd, path, file, op, got_write, opened);
2989fb1cc555SAl Viro 	mutex_unlock(&dir->d_inode->i_mutex);
2990fb1cc555SAl Viro 
29912675a4ebSAl Viro 	if (error <= 0) {
29922675a4ebSAl Viro 		if (error)
2993d58ffd35SMiklos Szeredi 			goto out;
29946c0d46c4SAl Viro 
299547237687SAl Viro 		if ((*opened & FILE_CREATED) ||
2996496ad9aaSAl Viro 		    !S_ISREG(file_inode(file)->i_mode))
299777d660a8SMiklos Szeredi 			will_truncate = false;
2998d18e9008SMiklos Szeredi 
2999adb5c247SJeff Layton 		audit_inode(name, file->f_path.dentry, 0);
3000d18e9008SMiklos Szeredi 		goto opened;
3001d18e9008SMiklos Szeredi 	}
3002d18e9008SMiklos Szeredi 
300347237687SAl Viro 	if (*opened & FILE_CREATED) {
30049b44f1b3SAl Viro 		/* Don't check for write permission, don't truncate */
3005ca344a89SAl Viro 		open_flag &= ~O_TRUNC;
300677d660a8SMiklos Szeredi 		will_truncate = false;
3007bcda7652SAl Viro 		acc_mode = MAY_OPEN;
3008d58ffd35SMiklos Szeredi 		path_to_nameidata(path, nd);
3009e83db167SMiklos Szeredi 		goto finish_open_created;
3010fb1cc555SAl Viro 	}
3011fb1cc555SAl Viro 
3012fb1cc555SAl Viro 	/*
30133134f37eSJeff Layton 	 * create/update audit record if it already exists.
3014fb1cc555SAl Viro 	 */
3015b18825a7SDavid Howells 	if (d_is_positive(path->dentry))
3016adb5c247SJeff Layton 		audit_inode(name, path->dentry, 0);
3017fb1cc555SAl Viro 
3018d18e9008SMiklos Szeredi 	/*
3019d18e9008SMiklos Szeredi 	 * If atomic_open() acquired write access it is dropped now due to
3020d18e9008SMiklos Szeredi 	 * possible mount and symlink following (this might be optimized away if
3021d18e9008SMiklos Szeredi 	 * necessary...)
3022d18e9008SMiklos Szeredi 	 */
302364894cf8SAl Viro 	if (got_write) {
3024d18e9008SMiklos Szeredi 		mnt_drop_write(nd->path.mnt);
302564894cf8SAl Viro 		got_write = false;
3026d18e9008SMiklos Szeredi 	}
3027d18e9008SMiklos Szeredi 
3028fb1cc555SAl Viro 	error = -EEXIST;
3029f8310c59SAl Viro 	if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
3030fb1cc555SAl Viro 		goto exit_dput;
3031fb1cc555SAl Viro 
30329875cf80SDavid Howells 	error = follow_managed(path, nd->flags);
30339875cf80SDavid Howells 	if (error < 0)
3034fb1cc555SAl Viro 		goto exit_dput;
3035fb1cc555SAl Viro 
3036a3fbbde7SAl Viro 	if (error)
3037a3fbbde7SAl Viro 		nd->flags |= LOOKUP_JUMPED;
3038a3fbbde7SAl Viro 
3039decf3400SMiklos Szeredi 	BUG_ON(nd->flags & LOOKUP_RCU);
3040decf3400SMiklos Szeredi 	inode = path->dentry->d_inode;
30415f5daac1SMiklos Szeredi finish_lookup:
30425f5daac1SMiklos Szeredi 	/* we _can_ be in RCU mode here */
3043fb1cc555SAl Viro 	error = -ENOENT;
304422213318SAl Viro 	if (!inode || d_is_negative(path->dentry)) {
304554c33e7fSMiklos Szeredi 		path_to_nameidata(path, nd);
30462675a4ebSAl Viro 		goto out;
304754c33e7fSMiklos Szeredi 	}
30489e67f361SAl Viro 
3049b18825a7SDavid Howells 	if (should_follow_link(path->dentry, !symlink_ok)) {
3050d45ea867SMiklos Szeredi 		if (nd->flags & LOOKUP_RCU) {
3051d45ea867SMiklos Szeredi 			if (unlikely(unlazy_walk(nd, path->dentry))) {
3052d45ea867SMiklos Szeredi 				error = -ECHILD;
30532675a4ebSAl Viro 				goto out;
3054d45ea867SMiklos Szeredi 			}
3055d45ea867SMiklos Szeredi 		}
3056d45ea867SMiklos Szeredi 		BUG_ON(inode != path->dentry->d_inode);
30572675a4ebSAl Viro 		return 1;
3058d45ea867SMiklos Szeredi 	}
3059fb1cc555SAl Viro 
306016b1c1cdSMiklos Szeredi 	if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
3061fb1cc555SAl Viro 		path_to_nameidata(path, nd);
306216b1c1cdSMiklos Szeredi 	} else {
306316b1c1cdSMiklos Szeredi 		save_parent.dentry = nd->path.dentry;
306416b1c1cdSMiklos Szeredi 		save_parent.mnt = mntget(path->mnt);
306516b1c1cdSMiklos Szeredi 		nd->path.dentry = path->dentry;
306616b1c1cdSMiklos Szeredi 
306716b1c1cdSMiklos Szeredi 	}
3068decf3400SMiklos Szeredi 	nd->inode = inode;
3069a3fbbde7SAl Viro 	/* Why this, you ask?  _Now_ we might have grown LOOKUP_JUMPED... */
3070bc77daa7SAl Viro finish_open:
3071a3fbbde7SAl Viro 	error = complete_walk(nd);
307216b1c1cdSMiklos Szeredi 	if (error) {
307316b1c1cdSMiklos Szeredi 		path_put(&save_parent);
30742675a4ebSAl Viro 		return error;
307516b1c1cdSMiklos Szeredi 	}
3076bc77daa7SAl Viro 	audit_inode(name, nd->path.dentry, 0);
3077fb1cc555SAl Viro 	error = -EISDIR;
307844b1d530SMiklos Szeredi 	if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
30792675a4ebSAl Viro 		goto out;
3080af2f5542SMiklos Szeredi 	error = -ENOTDIR;
308144b1d530SMiklos Szeredi 	if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
30822675a4ebSAl Viro 		goto out;
30836c0d46c4SAl Viro 	if (!S_ISREG(nd->inode->i_mode))
308477d660a8SMiklos Szeredi 		will_truncate = false;
30856c0d46c4SAl Viro 
30860f9d1a10SAl Viro 	if (will_truncate) {
30870f9d1a10SAl Viro 		error = mnt_want_write(nd->path.mnt);
30880f9d1a10SAl Viro 		if (error)
30892675a4ebSAl Viro 			goto out;
309064894cf8SAl Viro 		got_write = true;
30910f9d1a10SAl Viro 	}
3092e83db167SMiklos Szeredi finish_open_created:
3093bcda7652SAl Viro 	error = may_open(&nd->path, acc_mode, open_flag);
3094ca344a89SAl Viro 	if (error)
30952675a4ebSAl Viro 		goto out;
30964aa7c634SMiklos Szeredi 
30974aa7c634SMiklos Szeredi 	BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
30984aa7c634SMiklos Szeredi 	error = vfs_open(&nd->path, file, current_cred());
30994aa7c634SMiklos Szeredi 	if (!error) {
31004aa7c634SMiklos Szeredi 		*opened |= FILE_OPENED;
31014aa7c634SMiklos Szeredi 	} else {
310230d90494SAl Viro 		if (error == -EOPENSTALE)
3103f60dc3dbSMiklos Szeredi 			goto stale_open;
3104015c3bbcSMiklos Szeredi 		goto out;
3105f60dc3dbSMiklos Szeredi 	}
3106a8277b9bSMiklos Szeredi opened:
31072675a4ebSAl Viro 	error = open_check_o_direct(file);
3108015c3bbcSMiklos Szeredi 	if (error)
3109015c3bbcSMiklos Szeredi 		goto exit_fput;
31103034a146SDmitry Kasatkin 	error = ima_file_check(file, op->acc_mode, *opened);
3111aa4caadbSMiklos Szeredi 	if (error)
3112aa4caadbSMiklos Szeredi 		goto exit_fput;
3113aa4caadbSMiklos Szeredi 
31140f9d1a10SAl Viro 	if (will_truncate) {
31152675a4ebSAl Viro 		error = handle_truncate(file);
3116aa4caadbSMiklos Szeredi 		if (error)
3117aa4caadbSMiklos Szeredi 			goto exit_fput;
31180f9d1a10SAl Viro 	}
3119ca344a89SAl Viro out:
312064894cf8SAl Viro 	if (got_write)
31210f9d1a10SAl Viro 		mnt_drop_write(nd->path.mnt);
312216b1c1cdSMiklos Szeredi 	path_put(&save_parent);
3123e276ae67SMiklos Szeredi 	terminate_walk(nd);
31242675a4ebSAl Viro 	return error;
3125fb1cc555SAl Viro 
3126fb1cc555SAl Viro exit_dput:
3127fb1cc555SAl Viro 	path_put_conditional(path, nd);
3128ca344a89SAl Viro 	goto out;
3129015c3bbcSMiklos Szeredi exit_fput:
31302675a4ebSAl Viro 	fput(file);
31312675a4ebSAl Viro 	goto out;
3132015c3bbcSMiklos Szeredi 
3133f60dc3dbSMiklos Szeredi stale_open:
3134f60dc3dbSMiklos Szeredi 	/* If no saved parent or already retried then can't retry */
3135f60dc3dbSMiklos Szeredi 	if (!save_parent.dentry || retried)
3136f60dc3dbSMiklos Szeredi 		goto out;
3137f60dc3dbSMiklos Szeredi 
3138f60dc3dbSMiklos Szeredi 	BUG_ON(save_parent.dentry != dir);
3139f60dc3dbSMiklos Szeredi 	path_put(&nd->path);
3140f60dc3dbSMiklos Szeredi 	nd->path = save_parent;
3141f60dc3dbSMiklos Szeredi 	nd->inode = dir->d_inode;
3142f60dc3dbSMiklos Szeredi 	save_parent.mnt = NULL;
3143f60dc3dbSMiklos Szeredi 	save_parent.dentry = NULL;
314464894cf8SAl Viro 	if (got_write) {
3145f60dc3dbSMiklos Szeredi 		mnt_drop_write(nd->path.mnt);
314664894cf8SAl Viro 		got_write = false;
3147f60dc3dbSMiklos Szeredi 	}
3148f60dc3dbSMiklos Szeredi 	retried = true;
3149f60dc3dbSMiklos Szeredi 	goto retry_lookup;
3150fb1cc555SAl Viro }
3151fb1cc555SAl Viro 
315260545d0dSAl Viro static int do_tmpfile(int dfd, struct filename *pathname,
315360545d0dSAl Viro 		struct nameidata *nd, int flags,
315460545d0dSAl Viro 		const struct open_flags *op,
315560545d0dSAl Viro 		struct file *file, int *opened)
315660545d0dSAl Viro {
315760545d0dSAl Viro 	static const struct qstr name = QSTR_INIT("/", 1);
315860545d0dSAl Viro 	struct dentry *dentry, *child;
315960545d0dSAl Viro 	struct inode *dir;
316060545d0dSAl Viro 	int error = path_lookupat(dfd, pathname->name,
316160545d0dSAl Viro 				  flags | LOOKUP_DIRECTORY, nd);
316260545d0dSAl Viro 	if (unlikely(error))
316360545d0dSAl Viro 		return error;
316460545d0dSAl Viro 	error = mnt_want_write(nd->path.mnt);
316560545d0dSAl Viro 	if (unlikely(error))
316660545d0dSAl Viro 		goto out;
316760545d0dSAl Viro 	/* we want directory to be writable */
316860545d0dSAl Viro 	error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC);
316960545d0dSAl Viro 	if (error)
317060545d0dSAl Viro 		goto out2;
317160545d0dSAl Viro 	dentry = nd->path.dentry;
317260545d0dSAl Viro 	dir = dentry->d_inode;
317360545d0dSAl Viro 	if (!dir->i_op->tmpfile) {
317460545d0dSAl Viro 		error = -EOPNOTSUPP;
317560545d0dSAl Viro 		goto out2;
317660545d0dSAl Viro 	}
317760545d0dSAl Viro 	child = d_alloc(dentry, &name);
317860545d0dSAl Viro 	if (unlikely(!child)) {
317960545d0dSAl Viro 		error = -ENOMEM;
318060545d0dSAl Viro 		goto out2;
318160545d0dSAl Viro 	}
318260545d0dSAl Viro 	nd->flags &= ~LOOKUP_DIRECTORY;
318360545d0dSAl Viro 	nd->flags |= op->intent;
318460545d0dSAl Viro 	dput(nd->path.dentry);
318560545d0dSAl Viro 	nd->path.dentry = child;
318660545d0dSAl Viro 	error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode);
318760545d0dSAl Viro 	if (error)
318860545d0dSAl Viro 		goto out2;
318960545d0dSAl Viro 	audit_inode(pathname, nd->path.dentry, 0);
319069a91c23SEric Rannaud 	/* Don't check for other permissions, the inode was just created */
319169a91c23SEric Rannaud 	error = may_open(&nd->path, MAY_OPEN, op->open_flag);
319260545d0dSAl Viro 	if (error)
319360545d0dSAl Viro 		goto out2;
319460545d0dSAl Viro 	file->f_path.mnt = nd->path.mnt;
319560545d0dSAl Viro 	error = finish_open(file, nd->path.dentry, NULL, opened);
319660545d0dSAl Viro 	if (error)
319760545d0dSAl Viro 		goto out2;
319860545d0dSAl Viro 	error = open_check_o_direct(file);
3199f4e0c30cSAl Viro 	if (error) {
320060545d0dSAl Viro 		fput(file);
3201f4e0c30cSAl Viro 	} else if (!(op->open_flag & O_EXCL)) {
3202f4e0c30cSAl Viro 		struct inode *inode = file_inode(file);
3203f4e0c30cSAl Viro 		spin_lock(&inode->i_lock);
3204f4e0c30cSAl Viro 		inode->i_state |= I_LINKABLE;
3205f4e0c30cSAl Viro 		spin_unlock(&inode->i_lock);
3206f4e0c30cSAl Viro 	}
320760545d0dSAl Viro out2:
320860545d0dSAl Viro 	mnt_drop_write(nd->path.mnt);
320960545d0dSAl Viro out:
321060545d0dSAl Viro 	path_put(&nd->path);
321160545d0dSAl Viro 	return error;
321260545d0dSAl Viro }
321360545d0dSAl Viro 
3214669abf4eSJeff Layton static struct file *path_openat(int dfd, struct filename *pathname,
321573d049a4SAl Viro 		struct nameidata *nd, const struct open_flags *op, int flags)
32161da177e4SLinus Torvalds {
321730d90494SAl Viro 	struct file *file;
32189850c056SAl Viro 	struct path path;
321947237687SAl Viro 	int opened = 0;
322013aab428SAl Viro 	int error;
322131e6b01fSNick Piggin 
322230d90494SAl Viro 	file = get_empty_filp();
32231afc99beSAl Viro 	if (IS_ERR(file))
32241afc99beSAl Viro 		return file;
322531e6b01fSNick Piggin 
322630d90494SAl Viro 	file->f_flags = op->open_flag;
322731e6b01fSNick Piggin 
3228bb458c64SAl Viro 	if (unlikely(file->f_flags & __O_TMPFILE)) {
322960545d0dSAl Viro 		error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened);
323060545d0dSAl Viro 		goto out;
323160545d0dSAl Viro 	}
323260545d0dSAl Viro 
3233980f3ea2SAl Viro 	error = path_init(dfd, pathname->name, flags, nd);
323431e6b01fSNick Piggin 	if (unlikely(error))
32352675a4ebSAl Viro 		goto out;
32361da177e4SLinus Torvalds 
32372675a4ebSAl Viro 	error = do_last(nd, &path, file, op, &opened, pathname);
32382675a4ebSAl Viro 	while (unlikely(error > 0)) { /* trailing symlink */
32397b9337aaSNick Piggin 		struct path link = path;
3240def4af30SAl Viro 		void *cookie;
3241574197e0SAl Viro 		if (!(nd->flags & LOOKUP_FOLLOW)) {
324273d049a4SAl Viro 			path_put_conditional(&path, nd);
324373d049a4SAl Viro 			path_put(&nd->path);
32442675a4ebSAl Viro 			error = -ELOOP;
324540b39136SAl Viro 			break;
324640b39136SAl Viro 		}
3247800179c9SKees Cook 		error = may_follow_link(&link, nd);
3248800179c9SKees Cook 		if (unlikely(error))
3249800179c9SKees Cook 			break;
325073d049a4SAl Viro 		nd->flags |= LOOKUP_PARENT;
325173d049a4SAl Viro 		nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
3252574197e0SAl Viro 		error = follow_link(&link, nd, &cookie);
3253c3e380b0SAl Viro 		if (unlikely(error))
32542675a4ebSAl Viro 			break;
32552675a4ebSAl Viro 		error = do_last(nd, &path, file, op, &opened, pathname);
3256574197e0SAl Viro 		put_link(nd, &link, cookie);
3257806b681cSAl Viro 	}
325810fa8e62SAl Viro out:
3259893b7775SAl Viro 	path_cleanup(nd);
32602675a4ebSAl Viro 	if (!(opened & FILE_OPENED)) {
32612675a4ebSAl Viro 		BUG_ON(!error);
326230d90494SAl Viro 		put_filp(file);
3263015c3bbcSMiklos Szeredi 	}
32642675a4ebSAl Viro 	if (unlikely(error)) {
32652675a4ebSAl Viro 		if (error == -EOPENSTALE) {
32662675a4ebSAl Viro 			if (flags & LOOKUP_RCU)
32672675a4ebSAl Viro 				error = -ECHILD;
32682675a4ebSAl Viro 			else
32692675a4ebSAl Viro 				error = -ESTALE;
32702675a4ebSAl Viro 		}
32712675a4ebSAl Viro 		file = ERR_PTR(error);
32722675a4ebSAl Viro 	}
32732675a4ebSAl Viro 	return file;
3274de459215SKirill Korotaev }
32751da177e4SLinus Torvalds 
3276669abf4eSJeff Layton struct file *do_filp_open(int dfd, struct filename *pathname,
3277f9652e10SAl Viro 		const struct open_flags *op)
327813aab428SAl Viro {
327973d049a4SAl Viro 	struct nameidata nd;
3280f9652e10SAl Viro 	int flags = op->lookup_flags;
328113aab428SAl Viro 	struct file *filp;
328213aab428SAl Viro 
328373d049a4SAl Viro 	filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
328413aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ECHILD)))
328573d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags);
328613aab428SAl Viro 	if (unlikely(filp == ERR_PTR(-ESTALE)))
328773d049a4SAl Viro 		filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
328813aab428SAl Viro 	return filp;
328913aab428SAl Viro }
329013aab428SAl Viro 
329173d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
3292f9652e10SAl Viro 		const char *name, const struct open_flags *op)
329373d049a4SAl Viro {
329473d049a4SAl Viro 	struct nameidata nd;
329573d049a4SAl Viro 	struct file *file;
329651689104SPaul Moore 	struct filename *filename;
3297f9652e10SAl Viro 	int flags = op->lookup_flags | LOOKUP_ROOT;
329873d049a4SAl Viro 
329973d049a4SAl Viro 	nd.root.mnt = mnt;
330073d049a4SAl Viro 	nd.root.dentry = dentry;
330173d049a4SAl Viro 
3302b18825a7SDavid Howells 	if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
330373d049a4SAl Viro 		return ERR_PTR(-ELOOP);
330473d049a4SAl Viro 
330551689104SPaul Moore 	filename = getname_kernel(name);
330651689104SPaul Moore 	if (unlikely(IS_ERR(filename)))
330751689104SPaul Moore 		return ERR_CAST(filename);
330851689104SPaul Moore 
330951689104SPaul Moore 	file = path_openat(-1, filename, &nd, op, flags | LOOKUP_RCU);
331073d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ECHILD)))
331151689104SPaul Moore 		file = path_openat(-1, filename, &nd, op, flags);
331273d049a4SAl Viro 	if (unlikely(file == ERR_PTR(-ESTALE)))
331351689104SPaul Moore 		file = path_openat(-1, filename, &nd, op, flags | LOOKUP_REVAL);
331451689104SPaul Moore 	putname(filename);
331573d049a4SAl Viro 	return file;
331673d049a4SAl Viro }
331773d049a4SAl Viro 
3318fa14a0b8SAl Viro static struct dentry *filename_create(int dfd, struct filename *name,
33191ac12b4bSJeff Layton 				struct path *path, unsigned int lookup_flags)
33201da177e4SLinus Torvalds {
3321c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
3322ed75e95dSAl Viro 	struct nameidata nd;
3323c30dabfeSJan Kara 	int err2;
33241ac12b4bSJeff Layton 	int error;
33251ac12b4bSJeff Layton 	bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
33261ac12b4bSJeff Layton 
33271ac12b4bSJeff Layton 	/*
33281ac12b4bSJeff Layton 	 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
33291ac12b4bSJeff Layton 	 * other flags passed in are ignored!
33301ac12b4bSJeff Layton 	 */
33311ac12b4bSJeff Layton 	lookup_flags &= LOOKUP_REVAL;
33321ac12b4bSJeff Layton 
3333fa14a0b8SAl Viro 	error = filename_lookup(dfd, name, LOOKUP_PARENT|lookup_flags, &nd);
3334ed75e95dSAl Viro 	if (error)
3335ed75e95dSAl Viro 		return ERR_PTR(error);
33361da177e4SLinus Torvalds 
3337c663e5d8SChristoph Hellwig 	/*
3338c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
3339c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
3340c663e5d8SChristoph Hellwig 	 */
3341ed75e95dSAl Viro 	if (nd.last_type != LAST_NORM)
3342ed75e95dSAl Viro 		goto out;
3343ed75e95dSAl Viro 	nd.flags &= ~LOOKUP_PARENT;
3344ed75e95dSAl Viro 	nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3345c663e5d8SChristoph Hellwig 
3346c30dabfeSJan Kara 	/* don't fail immediately if it's r/o, at least try to report other errors */
3347c30dabfeSJan Kara 	err2 = mnt_want_write(nd.path.mnt);
3348c663e5d8SChristoph Hellwig 	/*
3349c663e5d8SChristoph Hellwig 	 * Do the final lookup.
3350c663e5d8SChristoph Hellwig 	 */
3351ed75e95dSAl Viro 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
3352ed75e95dSAl Viro 	dentry = lookup_hash(&nd);
33531da177e4SLinus Torvalds 	if (IS_ERR(dentry))
3354a8104a9fSAl Viro 		goto unlock;
3355c663e5d8SChristoph Hellwig 
3356a8104a9fSAl Viro 	error = -EEXIST;
3357b18825a7SDavid Howells 	if (d_is_positive(dentry))
3358a8104a9fSAl Viro 		goto fail;
3359b18825a7SDavid Howells 
3360c663e5d8SChristoph Hellwig 	/*
3361c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
3362c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
3363c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
3364c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
3365c663e5d8SChristoph Hellwig 	 */
3366ed75e95dSAl Viro 	if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
3367a8104a9fSAl Viro 		error = -ENOENT;
3368ed75e95dSAl Viro 		goto fail;
3369e9baf6e5SAl Viro 	}
3370c30dabfeSJan Kara 	if (unlikely(err2)) {
3371c30dabfeSJan Kara 		error = err2;
3372a8104a9fSAl Viro 		goto fail;
3373c30dabfeSJan Kara 	}
3374ed75e95dSAl Viro 	*path = nd.path;
3375e9baf6e5SAl Viro 	return dentry;
33761da177e4SLinus Torvalds fail:
3377a8104a9fSAl Viro 	dput(dentry);
3378a8104a9fSAl Viro 	dentry = ERR_PTR(error);
3379a8104a9fSAl Viro unlock:
3380dae6ad8fSAl Viro 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3381c30dabfeSJan Kara 	if (!err2)
3382c30dabfeSJan Kara 		mnt_drop_write(nd.path.mnt);
3383ed75e95dSAl Viro out:
3384dae6ad8fSAl Viro 	path_put(&nd.path);
3385ed75e95dSAl Viro 	return dentry;
3386dae6ad8fSAl Viro }
3387fa14a0b8SAl Viro 
3388fa14a0b8SAl Viro struct dentry *kern_path_create(int dfd, const char *pathname,
3389fa14a0b8SAl Viro 				struct path *path, unsigned int lookup_flags)
3390fa14a0b8SAl Viro {
339151689104SPaul Moore 	struct filename *filename = getname_kernel(pathname);
339251689104SPaul Moore 	struct dentry *res;
339351689104SPaul Moore 
339451689104SPaul Moore 	if (IS_ERR(filename))
339551689104SPaul Moore 		return ERR_CAST(filename);
339651689104SPaul Moore 	res = filename_create(dfd, filename, path, lookup_flags);
339751689104SPaul Moore 	putname(filename);
339851689104SPaul Moore 	return res;
3399fa14a0b8SAl Viro }
3400dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create);
3401dae6ad8fSAl Viro 
3402921a1650SAl Viro void done_path_create(struct path *path, struct dentry *dentry)
3403921a1650SAl Viro {
3404921a1650SAl Viro 	dput(dentry);
3405921a1650SAl Viro 	mutex_unlock(&path->dentry->d_inode->i_mutex);
3406a8104a9fSAl Viro 	mnt_drop_write(path->mnt);
3407921a1650SAl Viro 	path_put(path);
3408921a1650SAl Viro }
3409921a1650SAl Viro EXPORT_SYMBOL(done_path_create);
3410921a1650SAl Viro 
34111ac12b4bSJeff Layton struct dentry *user_path_create(int dfd, const char __user *pathname,
34121ac12b4bSJeff Layton 				struct path *path, unsigned int lookup_flags)
3413dae6ad8fSAl Viro {
341491a27b2aSJeff Layton 	struct filename *tmp = getname(pathname);
3415dae6ad8fSAl Viro 	struct dentry *res;
3416dae6ad8fSAl Viro 	if (IS_ERR(tmp))
3417dae6ad8fSAl Viro 		return ERR_CAST(tmp);
3418fa14a0b8SAl Viro 	res = filename_create(dfd, tmp, path, lookup_flags);
3419dae6ad8fSAl Viro 	putname(tmp);
3420dae6ad8fSAl Viro 	return res;
3421dae6ad8fSAl Viro }
3422dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create);
3423dae6ad8fSAl Viro 
34241a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
34251da177e4SLinus Torvalds {
3426a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
34271da177e4SLinus Torvalds 
34281da177e4SLinus Torvalds 	if (error)
34291da177e4SLinus Torvalds 		return error;
34301da177e4SLinus Torvalds 
3431975d6b39SEric W. Biederman 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
34321da177e4SLinus Torvalds 		return -EPERM;
34331da177e4SLinus Torvalds 
3434acfa4380SAl Viro 	if (!dir->i_op->mknod)
34351da177e4SLinus Torvalds 		return -EPERM;
34361da177e4SLinus Torvalds 
343708ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
343808ce5f16SSerge E. Hallyn 	if (error)
343908ce5f16SSerge E. Hallyn 		return error;
344008ce5f16SSerge E. Hallyn 
34411da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
34421da177e4SLinus Torvalds 	if (error)
34431da177e4SLinus Torvalds 		return error;
34441da177e4SLinus Torvalds 
34451da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
3446a74574aaSStephen Smalley 	if (!error)
3447f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
34481da177e4SLinus Torvalds 	return error;
34491da177e4SLinus Torvalds }
34504d359507SAl Viro EXPORT_SYMBOL(vfs_mknod);
34511da177e4SLinus Torvalds 
3452f69aac00SAl Viro static int may_mknod(umode_t mode)
3453463c3197SDave Hansen {
3454463c3197SDave Hansen 	switch (mode & S_IFMT) {
3455463c3197SDave Hansen 	case S_IFREG:
3456463c3197SDave Hansen 	case S_IFCHR:
3457463c3197SDave Hansen 	case S_IFBLK:
3458463c3197SDave Hansen 	case S_IFIFO:
3459463c3197SDave Hansen 	case S_IFSOCK:
3460463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
3461463c3197SDave Hansen 		return 0;
3462463c3197SDave Hansen 	case S_IFDIR:
3463463c3197SDave Hansen 		return -EPERM;
3464463c3197SDave Hansen 	default:
3465463c3197SDave Hansen 		return -EINVAL;
3466463c3197SDave Hansen 	}
3467463c3197SDave Hansen }
3468463c3197SDave Hansen 
34698208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
34702e4d0924SHeiko Carstens 		unsigned, dev)
34711da177e4SLinus Torvalds {
34721da177e4SLinus Torvalds 	struct dentry *dentry;
3473dae6ad8fSAl Viro 	struct path path;
3474dae6ad8fSAl Viro 	int error;
3475972567f1SJeff Layton 	unsigned int lookup_flags = 0;
34761da177e4SLinus Torvalds 
34778e4bfca1SAl Viro 	error = may_mknod(mode);
34788e4bfca1SAl Viro 	if (error)
34798e4bfca1SAl Viro 		return error;
3480972567f1SJeff Layton retry:
3481972567f1SJeff Layton 	dentry = user_path_create(dfd, filename, &path, lookup_flags);
3482dae6ad8fSAl Viro 	if (IS_ERR(dentry))
3483dae6ad8fSAl Viro 		return PTR_ERR(dentry);
34842ad94ae6SAl Viro 
3485dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3486ce3b0f8dSAl Viro 		mode &= ~current_umask();
3487dae6ad8fSAl Viro 	error = security_path_mknod(&path, dentry, mode, dev);
3488be6d3e56SKentaro Takeda 	if (error)
3489a8104a9fSAl Viro 		goto out;
34901da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
34911da177e4SLinus Torvalds 		case 0: case S_IFREG:
3492312b63fbSAl Viro 			error = vfs_create(path.dentry->d_inode,dentry,mode,true);
34931da177e4SLinus Torvalds 			break;
34941da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
3495dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,
34961da177e4SLinus Torvalds 					new_decode_dev(dev));
34971da177e4SLinus Torvalds 			break;
34981da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
3499dae6ad8fSAl Viro 			error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
35001da177e4SLinus Torvalds 			break;
35011da177e4SLinus Torvalds 	}
3502a8104a9fSAl Viro out:
3503921a1650SAl Viro 	done_path_create(&path, dentry);
3504972567f1SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3505972567f1SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3506972567f1SJeff Layton 		goto retry;
3507972567f1SJeff Layton 	}
35081da177e4SLinus Torvalds 	return error;
35091da177e4SLinus Torvalds }
35101da177e4SLinus Torvalds 
35118208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
35125590ff0dSUlrich Drepper {
35135590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
35145590ff0dSUlrich Drepper }
35155590ff0dSUlrich Drepper 
351618bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
35171da177e4SLinus Torvalds {
3518a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
35198de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
35201da177e4SLinus Torvalds 
35211da177e4SLinus Torvalds 	if (error)
35221da177e4SLinus Torvalds 		return error;
35231da177e4SLinus Torvalds 
3524acfa4380SAl Viro 	if (!dir->i_op->mkdir)
35251da177e4SLinus Torvalds 		return -EPERM;
35261da177e4SLinus Torvalds 
35271da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
35281da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
35291da177e4SLinus Torvalds 	if (error)
35301da177e4SLinus Torvalds 		return error;
35311da177e4SLinus Torvalds 
35328de52778SAl Viro 	if (max_links && dir->i_nlink >= max_links)
35338de52778SAl Viro 		return -EMLINK;
35348de52778SAl Viro 
35351da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
3536a74574aaSStephen Smalley 	if (!error)
3537f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
35381da177e4SLinus Torvalds 	return error;
35391da177e4SLinus Torvalds }
35404d359507SAl Viro EXPORT_SYMBOL(vfs_mkdir);
35411da177e4SLinus Torvalds 
3542a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
35431da177e4SLinus Torvalds {
35446902d925SDave Hansen 	struct dentry *dentry;
3545dae6ad8fSAl Viro 	struct path path;
3546dae6ad8fSAl Viro 	int error;
3547b76d8b82SJeff Layton 	unsigned int lookup_flags = LOOKUP_DIRECTORY;
35481da177e4SLinus Torvalds 
3549b76d8b82SJeff Layton retry:
3550b76d8b82SJeff Layton 	dentry = user_path_create(dfd, pathname, &path, lookup_flags);
35516902d925SDave Hansen 	if (IS_ERR(dentry))
3552dae6ad8fSAl Viro 		return PTR_ERR(dentry);
35536902d925SDave Hansen 
3554dae6ad8fSAl Viro 	if (!IS_POSIXACL(path.dentry->d_inode))
3555ce3b0f8dSAl Viro 		mode &= ~current_umask();
3556dae6ad8fSAl Viro 	error = security_path_mkdir(&path, dentry, mode);
3557a8104a9fSAl Viro 	if (!error)
3558dae6ad8fSAl Viro 		error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
3559921a1650SAl Viro 	done_path_create(&path, dentry);
3560b76d8b82SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3561b76d8b82SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3562b76d8b82SJeff Layton 		goto retry;
3563b76d8b82SJeff Layton 	}
35641da177e4SLinus Torvalds 	return error;
35651da177e4SLinus Torvalds }
35661da177e4SLinus Torvalds 
3567a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
35685590ff0dSUlrich Drepper {
35695590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
35705590ff0dSUlrich Drepper }
35715590ff0dSUlrich Drepper 
35721da177e4SLinus Torvalds /*
3573a71905f0SSage Weil  * The dentry_unhash() helper will try to drop the dentry early: we
3574c0d02594SJ. Bruce Fields  * should have a usage count of 1 if we're the only user of this
3575a71905f0SSage Weil  * dentry, and if that is true (possibly after pruning the dcache),
3576a71905f0SSage Weil  * then we drop the dentry now.
35771da177e4SLinus Torvalds  *
35781da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
35791da177e4SLinus Torvalds  * do a
35801da177e4SLinus Torvalds  *
35811da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
35821da177e4SLinus Torvalds  *		return -EBUSY;
35831da177e4SLinus Torvalds  *
35841da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
35851da177e4SLinus Torvalds  * that is still in use by something else..
35861da177e4SLinus Torvalds  */
35871da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
35881da177e4SLinus Torvalds {
35891da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
35901da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
359198474236SWaiman Long 	if (dentry->d_lockref.count == 1)
35921da177e4SLinus Torvalds 		__d_drop(dentry);
35931da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
35941da177e4SLinus Torvalds }
35954d359507SAl Viro EXPORT_SYMBOL(dentry_unhash);
35961da177e4SLinus Torvalds 
35971da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
35981da177e4SLinus Torvalds {
35991da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
36001da177e4SLinus Torvalds 
36011da177e4SLinus Torvalds 	if (error)
36021da177e4SLinus Torvalds 		return error;
36031da177e4SLinus Torvalds 
3604acfa4380SAl Viro 	if (!dir->i_op->rmdir)
36051da177e4SLinus Torvalds 		return -EPERM;
36061da177e4SLinus Torvalds 
36071d2ef590SAl Viro 	dget(dentry);
36081b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
3609912dbc15SSage Weil 
36101da177e4SLinus Torvalds 	error = -EBUSY;
36117af1364fSEric W. Biederman 	if (is_local_mountpoint(dentry))
3612912dbc15SSage Weil 		goto out;
3613912dbc15SSage Weil 
36141da177e4SLinus Torvalds 	error = security_inode_rmdir(dir, dentry);
3615912dbc15SSage Weil 	if (error)
3616912dbc15SSage Weil 		goto out;
3617912dbc15SSage Weil 
36183cebde24SSage Weil 	shrink_dcache_parent(dentry);
36191da177e4SLinus Torvalds 	error = dir->i_op->rmdir(dir, dentry);
3620912dbc15SSage Weil 	if (error)
3621912dbc15SSage Weil 		goto out;
3622912dbc15SSage Weil 
36231da177e4SLinus Torvalds 	dentry->d_inode->i_flags |= S_DEAD;
3624d83c49f3SAl Viro 	dont_mount(dentry);
36258ed936b5SEric W. Biederman 	detach_mounts(dentry);
36261da177e4SLinus Torvalds 
3627912dbc15SSage Weil out:
3628912dbc15SSage Weil 	mutex_unlock(&dentry->d_inode->i_mutex);
36291d2ef590SAl Viro 	dput(dentry);
3630912dbc15SSage Weil 	if (!error)
3631912dbc15SSage Weil 		d_delete(dentry);
36321da177e4SLinus Torvalds 	return error;
36331da177e4SLinus Torvalds }
36344d359507SAl Viro EXPORT_SYMBOL(vfs_rmdir);
36351da177e4SLinus Torvalds 
36365590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
36371da177e4SLinus Torvalds {
36381da177e4SLinus Torvalds 	int error = 0;
363991a27b2aSJeff Layton 	struct filename *name;
36401da177e4SLinus Torvalds 	struct dentry *dentry;
36411da177e4SLinus Torvalds 	struct nameidata nd;
3642c6ee9206SJeff Layton 	unsigned int lookup_flags = 0;
3643c6ee9206SJeff Layton retry:
3644c6ee9206SJeff Layton 	name = user_path_parent(dfd, pathname, &nd, lookup_flags);
364591a27b2aSJeff Layton 	if (IS_ERR(name))
364691a27b2aSJeff Layton 		return PTR_ERR(name);
36471da177e4SLinus Torvalds 
36481da177e4SLinus Torvalds 	switch(nd.last_type) {
36491da177e4SLinus Torvalds 	case LAST_DOTDOT:
36501da177e4SLinus Torvalds 		error = -ENOTEMPTY;
36511da177e4SLinus Torvalds 		goto exit1;
36521da177e4SLinus Torvalds 	case LAST_DOT:
36531da177e4SLinus Torvalds 		error = -EINVAL;
36541da177e4SLinus Torvalds 		goto exit1;
36551da177e4SLinus Torvalds 	case LAST_ROOT:
36561da177e4SLinus Torvalds 		error = -EBUSY;
36571da177e4SLinus Torvalds 		goto exit1;
36581da177e4SLinus Torvalds 	}
36590612d9fbSOGAWA Hirofumi 
36600612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
3661c30dabfeSJan Kara 	error = mnt_want_write(nd.path.mnt);
3662c30dabfeSJan Kara 	if (error)
3663c30dabfeSJan Kara 		goto exit1;
36640612d9fbSOGAWA Hirofumi 
36654ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
366649705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
36671da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
36686902d925SDave Hansen 	if (IS_ERR(dentry))
36696902d925SDave Hansen 		goto exit2;
3670e6bc45d6STheodore Ts'o 	if (!dentry->d_inode) {
3671e6bc45d6STheodore Ts'o 		error = -ENOENT;
3672e6bc45d6STheodore Ts'o 		goto exit3;
3673e6bc45d6STheodore Ts'o 	}
3674be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
3675be6d3e56SKentaro Takeda 	if (error)
3676c30dabfeSJan Kara 		goto exit3;
36774ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
36780622753bSDave Hansen exit3:
36791da177e4SLinus Torvalds 	dput(dentry);
36806902d925SDave Hansen exit2:
36814ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
3682c30dabfeSJan Kara 	mnt_drop_write(nd.path.mnt);
36831da177e4SLinus Torvalds exit1:
36841d957f9bSJan Blunck 	path_put(&nd.path);
36851da177e4SLinus Torvalds 	putname(name);
3686c6ee9206SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3687c6ee9206SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3688c6ee9206SJeff Layton 		goto retry;
3689c6ee9206SJeff Layton 	}
36901da177e4SLinus Torvalds 	return error;
36911da177e4SLinus Torvalds }
36921da177e4SLinus Torvalds 
36933cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
36945590ff0dSUlrich Drepper {
36955590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
36965590ff0dSUlrich Drepper }
36975590ff0dSUlrich Drepper 
3698b21996e3SJ. Bruce Fields /**
3699b21996e3SJ. Bruce Fields  * vfs_unlink - unlink a filesystem object
3700b21996e3SJ. Bruce Fields  * @dir:	parent directory
3701b21996e3SJ. Bruce Fields  * @dentry:	victim
3702b21996e3SJ. Bruce Fields  * @delegated_inode: returns victim inode, if the inode is delegated.
3703b21996e3SJ. Bruce Fields  *
3704b21996e3SJ. Bruce Fields  * The caller must hold dir->i_mutex.
3705b21996e3SJ. Bruce Fields  *
3706b21996e3SJ. Bruce Fields  * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3707b21996e3SJ. Bruce Fields  * return a reference to the inode in delegated_inode.  The caller
3708b21996e3SJ. Bruce Fields  * should then break the delegation on that inode and retry.  Because
3709b21996e3SJ. Bruce Fields  * breaking a delegation may take a long time, the caller should drop
3710b21996e3SJ. Bruce Fields  * dir->i_mutex before doing so.
3711b21996e3SJ. Bruce Fields  *
3712b21996e3SJ. Bruce Fields  * Alternatively, a caller may pass NULL for delegated_inode.  This may
3713b21996e3SJ. Bruce Fields  * be appropriate for callers that expect the underlying filesystem not
3714b21996e3SJ. Bruce Fields  * to be NFS exported.
3715b21996e3SJ. Bruce Fields  */
3716b21996e3SJ. Bruce Fields int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
37171da177e4SLinus Torvalds {
37189accbb97SJ. Bruce Fields 	struct inode *target = dentry->d_inode;
37191da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
37201da177e4SLinus Torvalds 
37211da177e4SLinus Torvalds 	if (error)
37221da177e4SLinus Torvalds 		return error;
37231da177e4SLinus Torvalds 
3724acfa4380SAl Viro 	if (!dir->i_op->unlink)
37251da177e4SLinus Torvalds 		return -EPERM;
37261da177e4SLinus Torvalds 
37279accbb97SJ. Bruce Fields 	mutex_lock(&target->i_mutex);
37288ed936b5SEric W. Biederman 	if (is_local_mountpoint(dentry))
37291da177e4SLinus Torvalds 		error = -EBUSY;
37301da177e4SLinus Torvalds 	else {
37311da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
3732bec1052eSAl Viro 		if (!error) {
37335a14696cSJ. Bruce Fields 			error = try_break_deleg(target, delegated_inode);
37345a14696cSJ. Bruce Fields 			if (error)
3735b21996e3SJ. Bruce Fields 				goto out;
37361da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
37378ed936b5SEric W. Biederman 			if (!error) {
3738d83c49f3SAl Viro 				dont_mount(dentry);
37398ed936b5SEric W. Biederman 				detach_mounts(dentry);
37408ed936b5SEric W. Biederman 			}
3741bec1052eSAl Viro 		}
37421da177e4SLinus Torvalds 	}
3743b21996e3SJ. Bruce Fields out:
37449accbb97SJ. Bruce Fields 	mutex_unlock(&target->i_mutex);
37451da177e4SLinus Torvalds 
37461da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
37471da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
37489accbb97SJ. Bruce Fields 		fsnotify_link_count(target);
37491da177e4SLinus Torvalds 		d_delete(dentry);
37501da177e4SLinus Torvalds 	}
37510eeca283SRobert Love 
37521da177e4SLinus Torvalds 	return error;
37531da177e4SLinus Torvalds }
37544d359507SAl Viro EXPORT_SYMBOL(vfs_unlink);
37551da177e4SLinus Torvalds 
37561da177e4SLinus Torvalds /*
37571da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
37581b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
37591da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
37601da177e4SLinus Torvalds  * while waiting on the I/O.
37611da177e4SLinus Torvalds  */
37625590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
37631da177e4SLinus Torvalds {
37642ad94ae6SAl Viro 	int error;
376591a27b2aSJeff Layton 	struct filename *name;
37661da177e4SLinus Torvalds 	struct dentry *dentry;
37671da177e4SLinus Torvalds 	struct nameidata nd;
37681da177e4SLinus Torvalds 	struct inode *inode = NULL;
3769b21996e3SJ. Bruce Fields 	struct inode *delegated_inode = NULL;
37705d18f813SJeff Layton 	unsigned int lookup_flags = 0;
37715d18f813SJeff Layton retry:
37725d18f813SJeff Layton 	name = user_path_parent(dfd, pathname, &nd, lookup_flags);
377391a27b2aSJeff Layton 	if (IS_ERR(name))
377491a27b2aSJeff Layton 		return PTR_ERR(name);
37752ad94ae6SAl Viro 
37761da177e4SLinus Torvalds 	error = -EISDIR;
37771da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
37781da177e4SLinus Torvalds 		goto exit1;
37790612d9fbSOGAWA Hirofumi 
37800612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
3781c30dabfeSJan Kara 	error = mnt_want_write(nd.path.mnt);
3782c30dabfeSJan Kara 	if (error)
3783c30dabfeSJan Kara 		goto exit1;
3784b21996e3SJ. Bruce Fields retry_deleg:
37854ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
378649705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
37871da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
37881da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
37891da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
379050338b88STörök Edwin 		if (nd.last.name[nd.last.len])
379150338b88STörök Edwin 			goto slashes;
37921da177e4SLinus Torvalds 		inode = dentry->d_inode;
3793b18825a7SDavid Howells 		if (d_is_negative(dentry))
3794e6bc45d6STheodore Ts'o 			goto slashes;
37957de9c6eeSAl Viro 		ihold(inode);
3796be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
3797be6d3e56SKentaro Takeda 		if (error)
3798c30dabfeSJan Kara 			goto exit2;
3799b21996e3SJ. Bruce Fields 		error = vfs_unlink(nd.path.dentry->d_inode, dentry, &delegated_inode);
38001da177e4SLinus Torvalds exit2:
38011da177e4SLinus Torvalds 		dput(dentry);
38021da177e4SLinus Torvalds 	}
38034ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
38041da177e4SLinus Torvalds 	if (inode)
38051da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
3806b21996e3SJ. Bruce Fields 	inode = NULL;
3807b21996e3SJ. Bruce Fields 	if (delegated_inode) {
38085a14696cSJ. Bruce Fields 		error = break_deleg_wait(&delegated_inode);
3809b21996e3SJ. Bruce Fields 		if (!error)
3810b21996e3SJ. Bruce Fields 			goto retry_deleg;
3811b21996e3SJ. Bruce Fields 	}
3812c30dabfeSJan Kara 	mnt_drop_write(nd.path.mnt);
38131da177e4SLinus Torvalds exit1:
38141d957f9bSJan Blunck 	path_put(&nd.path);
38151da177e4SLinus Torvalds 	putname(name);
38165d18f813SJeff Layton 	if (retry_estale(error, lookup_flags)) {
38175d18f813SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
38185d18f813SJeff Layton 		inode = NULL;
38195d18f813SJeff Layton 		goto retry;
38205d18f813SJeff Layton 	}
38211da177e4SLinus Torvalds 	return error;
38221da177e4SLinus Torvalds 
38231da177e4SLinus Torvalds slashes:
3824b18825a7SDavid Howells 	if (d_is_negative(dentry))
3825b18825a7SDavid Howells 		error = -ENOENT;
382644b1d530SMiklos Szeredi 	else if (d_is_dir(dentry))
3827b18825a7SDavid Howells 		error = -EISDIR;
3828b18825a7SDavid Howells 	else
3829b18825a7SDavid Howells 		error = -ENOTDIR;
38301da177e4SLinus Torvalds 	goto exit2;
38311da177e4SLinus Torvalds }
38321da177e4SLinus Torvalds 
38332e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
38345590ff0dSUlrich Drepper {
38355590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
38365590ff0dSUlrich Drepper 		return -EINVAL;
38375590ff0dSUlrich Drepper 
38385590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
38395590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
38405590ff0dSUlrich Drepper 
38415590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
38425590ff0dSUlrich Drepper }
38435590ff0dSUlrich Drepper 
38443480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
38455590ff0dSUlrich Drepper {
38465590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
38475590ff0dSUlrich Drepper }
38485590ff0dSUlrich Drepper 
3849db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
38501da177e4SLinus Torvalds {
3851a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
38521da177e4SLinus Torvalds 
38531da177e4SLinus Torvalds 	if (error)
38541da177e4SLinus Torvalds 		return error;
38551da177e4SLinus Torvalds 
3856acfa4380SAl Viro 	if (!dir->i_op->symlink)
38571da177e4SLinus Torvalds 		return -EPERM;
38581da177e4SLinus Torvalds 
38591da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
38601da177e4SLinus Torvalds 	if (error)
38611da177e4SLinus Torvalds 		return error;
38621da177e4SLinus Torvalds 
38631da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
3864a74574aaSStephen Smalley 	if (!error)
3865f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
38661da177e4SLinus Torvalds 	return error;
38671da177e4SLinus Torvalds }
38684d359507SAl Viro EXPORT_SYMBOL(vfs_symlink);
38691da177e4SLinus Torvalds 
38702e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
38712e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
38721da177e4SLinus Torvalds {
38732ad94ae6SAl Viro 	int error;
387491a27b2aSJeff Layton 	struct filename *from;
38756902d925SDave Hansen 	struct dentry *dentry;
3876dae6ad8fSAl Viro 	struct path path;
3877f46d3567SJeff Layton 	unsigned int lookup_flags = 0;
38781da177e4SLinus Torvalds 
38791da177e4SLinus Torvalds 	from = getname(oldname);
38801da177e4SLinus Torvalds 	if (IS_ERR(from))
38811da177e4SLinus Torvalds 		return PTR_ERR(from);
3882f46d3567SJeff Layton retry:
3883f46d3567SJeff Layton 	dentry = user_path_create(newdfd, newname, &path, lookup_flags);
38841da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
38856902d925SDave Hansen 	if (IS_ERR(dentry))
3886dae6ad8fSAl Viro 		goto out_putname;
38876902d925SDave Hansen 
388891a27b2aSJeff Layton 	error = security_path_symlink(&path, dentry, from->name);
3889a8104a9fSAl Viro 	if (!error)
389091a27b2aSJeff Layton 		error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
3891921a1650SAl Viro 	done_path_create(&path, dentry);
3892f46d3567SJeff Layton 	if (retry_estale(error, lookup_flags)) {
3893f46d3567SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
3894f46d3567SJeff Layton 		goto retry;
3895f46d3567SJeff Layton 	}
38966902d925SDave Hansen out_putname:
38971da177e4SLinus Torvalds 	putname(from);
38981da177e4SLinus Torvalds 	return error;
38991da177e4SLinus Torvalds }
39001da177e4SLinus Torvalds 
39013480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
39025590ff0dSUlrich Drepper {
39035590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
39045590ff0dSUlrich Drepper }
39055590ff0dSUlrich Drepper 
3906146a8595SJ. Bruce Fields /**
3907146a8595SJ. Bruce Fields  * vfs_link - create a new link
3908146a8595SJ. Bruce Fields  * @old_dentry:	object to be linked
3909146a8595SJ. Bruce Fields  * @dir:	new parent
3910146a8595SJ. Bruce Fields  * @new_dentry:	where to create the new link
3911146a8595SJ. Bruce Fields  * @delegated_inode: returns inode needing a delegation break
3912146a8595SJ. Bruce Fields  *
3913146a8595SJ. Bruce Fields  * The caller must hold dir->i_mutex
3914146a8595SJ. Bruce Fields  *
3915146a8595SJ. Bruce Fields  * If vfs_link discovers a delegation on the to-be-linked file in need
3916146a8595SJ. Bruce Fields  * of breaking, it will return -EWOULDBLOCK and return a reference to the
3917146a8595SJ. Bruce Fields  * inode in delegated_inode.  The caller should then break the delegation
3918146a8595SJ. Bruce Fields  * and retry.  Because breaking a delegation may take a long time, the
3919146a8595SJ. Bruce Fields  * caller should drop the i_mutex before doing so.
3920146a8595SJ. Bruce Fields  *
3921146a8595SJ. Bruce Fields  * Alternatively, a caller may pass NULL for delegated_inode.  This may
3922146a8595SJ. Bruce Fields  * be appropriate for callers that expect the underlying filesystem not
3923146a8595SJ. Bruce Fields  * to be NFS exported.
3924146a8595SJ. Bruce Fields  */
3925146a8595SJ. Bruce Fields int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
39261da177e4SLinus Torvalds {
39271da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
39288de52778SAl Viro 	unsigned max_links = dir->i_sb->s_max_links;
39291da177e4SLinus Torvalds 	int error;
39301da177e4SLinus Torvalds 
39311da177e4SLinus Torvalds 	if (!inode)
39321da177e4SLinus Torvalds 		return -ENOENT;
39331da177e4SLinus Torvalds 
3934a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
39351da177e4SLinus Torvalds 	if (error)
39361da177e4SLinus Torvalds 		return error;
39371da177e4SLinus Torvalds 
39381da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
39391da177e4SLinus Torvalds 		return -EXDEV;
39401da177e4SLinus Torvalds 
39411da177e4SLinus Torvalds 	/*
39421da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
39431da177e4SLinus Torvalds 	 */
39441da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
39451da177e4SLinus Torvalds 		return -EPERM;
3946acfa4380SAl Viro 	if (!dir->i_op->link)
39471da177e4SLinus Torvalds 		return -EPERM;
39487e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
39491da177e4SLinus Torvalds 		return -EPERM;
39501da177e4SLinus Torvalds 
39511da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
39521da177e4SLinus Torvalds 	if (error)
39531da177e4SLinus Torvalds 		return error;
39541da177e4SLinus Torvalds 
39557e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
3956aae8a97dSAneesh Kumar K.V 	/* Make sure we don't allow creating hardlink to an unlinked file */
3957f4e0c30cSAl Viro 	if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
3958aae8a97dSAneesh Kumar K.V 		error =  -ENOENT;
39598de52778SAl Viro 	else if (max_links && inode->i_nlink >= max_links)
39608de52778SAl Viro 		error = -EMLINK;
3961146a8595SJ. Bruce Fields 	else {
3962146a8595SJ. Bruce Fields 		error = try_break_deleg(inode, delegated_inode);
3963146a8595SJ. Bruce Fields 		if (!error)
39641da177e4SLinus Torvalds 			error = dir->i_op->link(old_dentry, dir, new_dentry);
3965146a8595SJ. Bruce Fields 	}
3966f4e0c30cSAl Viro 
3967f4e0c30cSAl Viro 	if (!error && (inode->i_state & I_LINKABLE)) {
3968f4e0c30cSAl Viro 		spin_lock(&inode->i_lock);
3969f4e0c30cSAl Viro 		inode->i_state &= ~I_LINKABLE;
3970f4e0c30cSAl Viro 		spin_unlock(&inode->i_lock);
3971f4e0c30cSAl Viro 	}
39727e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
3973e31e14ecSStephen Smalley 	if (!error)
39747e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
39751da177e4SLinus Torvalds 	return error;
39761da177e4SLinus Torvalds }
39774d359507SAl Viro EXPORT_SYMBOL(vfs_link);
39781da177e4SLinus Torvalds 
39791da177e4SLinus Torvalds /*
39801da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
39811da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
39821da177e4SLinus Torvalds  * newname.  --KAB
39831da177e4SLinus Torvalds  *
39841da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
39851da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
39861da177e4SLinus Torvalds  * and other special files.  --ADM
39871da177e4SLinus Torvalds  */
39882e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
39892e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
39901da177e4SLinus Torvalds {
39911da177e4SLinus Torvalds 	struct dentry *new_dentry;
3992dae6ad8fSAl Viro 	struct path old_path, new_path;
3993146a8595SJ. Bruce Fields 	struct inode *delegated_inode = NULL;
399411a7b371SAneesh Kumar K.V 	int how = 0;
39951da177e4SLinus Torvalds 	int error;
39961da177e4SLinus Torvalds 
399711a7b371SAneesh Kumar K.V 	if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
3998c04030e1SUlrich Drepper 		return -EINVAL;
399911a7b371SAneesh Kumar K.V 	/*
4000f0cc6ffbSLinus Torvalds 	 * To use null names we require CAP_DAC_READ_SEARCH
4001f0cc6ffbSLinus Torvalds 	 * This ensures that not everyone will be able to create
4002f0cc6ffbSLinus Torvalds 	 * handlink using the passed filedescriptor.
400311a7b371SAneesh Kumar K.V 	 */
4004f0cc6ffbSLinus Torvalds 	if (flags & AT_EMPTY_PATH) {
4005f0cc6ffbSLinus Torvalds 		if (!capable(CAP_DAC_READ_SEARCH))
4006f0cc6ffbSLinus Torvalds 			return -ENOENT;
400711a7b371SAneesh Kumar K.V 		how = LOOKUP_EMPTY;
4008f0cc6ffbSLinus Torvalds 	}
4009c04030e1SUlrich Drepper 
401011a7b371SAneesh Kumar K.V 	if (flags & AT_SYMLINK_FOLLOW)
401111a7b371SAneesh Kumar K.V 		how |= LOOKUP_FOLLOW;
4012442e31caSJeff Layton retry:
401311a7b371SAneesh Kumar K.V 	error = user_path_at(olddfd, oldname, how, &old_path);
40141da177e4SLinus Torvalds 	if (error)
40152ad94ae6SAl Viro 		return error;
40162ad94ae6SAl Viro 
4017442e31caSJeff Layton 	new_dentry = user_path_create(newdfd, newname, &new_path,
4018442e31caSJeff Layton 					(how & LOOKUP_REVAL));
40191da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
40206902d925SDave Hansen 	if (IS_ERR(new_dentry))
4021dae6ad8fSAl Viro 		goto out;
4022dae6ad8fSAl Viro 
4023dae6ad8fSAl Viro 	error = -EXDEV;
4024dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt)
4025dae6ad8fSAl Viro 		goto out_dput;
4026800179c9SKees Cook 	error = may_linkat(&old_path);
4027800179c9SKees Cook 	if (unlikely(error))
4028800179c9SKees Cook 		goto out_dput;
4029dae6ad8fSAl Viro 	error = security_path_link(old_path.dentry, &new_path, new_dentry);
4030be6d3e56SKentaro Takeda 	if (error)
4031a8104a9fSAl Viro 		goto out_dput;
4032146a8595SJ. Bruce Fields 	error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
403375c3f29dSDave Hansen out_dput:
4034921a1650SAl Viro 	done_path_create(&new_path, new_dentry);
4035146a8595SJ. Bruce Fields 	if (delegated_inode) {
4036146a8595SJ. Bruce Fields 		error = break_deleg_wait(&delegated_inode);
4037d22e6338SOleg Drokin 		if (!error) {
4038d22e6338SOleg Drokin 			path_put(&old_path);
4039146a8595SJ. Bruce Fields 			goto retry;
4040146a8595SJ. Bruce Fields 		}
4041d22e6338SOleg Drokin 	}
4042442e31caSJeff Layton 	if (retry_estale(error, how)) {
4043d22e6338SOleg Drokin 		path_put(&old_path);
4044442e31caSJeff Layton 		how |= LOOKUP_REVAL;
4045442e31caSJeff Layton 		goto retry;
4046442e31caSJeff Layton 	}
40471da177e4SLinus Torvalds out:
40482d8f3038SAl Viro 	path_put(&old_path);
40491da177e4SLinus Torvalds 
40501da177e4SLinus Torvalds 	return error;
40511da177e4SLinus Torvalds }
40521da177e4SLinus Torvalds 
40533480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
40545590ff0dSUlrich Drepper {
4055c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
40565590ff0dSUlrich Drepper }
40575590ff0dSUlrich Drepper 
4058bc27027aSMiklos Szeredi /**
4059bc27027aSMiklos Szeredi  * vfs_rename - rename a filesystem object
4060bc27027aSMiklos Szeredi  * @old_dir:	parent of source
4061bc27027aSMiklos Szeredi  * @old_dentry:	source
4062bc27027aSMiklos Szeredi  * @new_dir:	parent of destination
4063bc27027aSMiklos Szeredi  * @new_dentry:	destination
4064bc27027aSMiklos Szeredi  * @delegated_inode: returns an inode needing a delegation break
4065520c8b16SMiklos Szeredi  * @flags:	rename flags
4066bc27027aSMiklos Szeredi  *
4067bc27027aSMiklos Szeredi  * The caller must hold multiple mutexes--see lock_rename()).
4068bc27027aSMiklos Szeredi  *
4069bc27027aSMiklos Szeredi  * If vfs_rename discovers a delegation in need of breaking at either
4070bc27027aSMiklos Szeredi  * the source or destination, it will return -EWOULDBLOCK and return a
4071bc27027aSMiklos Szeredi  * reference to the inode in delegated_inode.  The caller should then
4072bc27027aSMiklos Szeredi  * break the delegation and retry.  Because breaking a delegation may
4073bc27027aSMiklos Szeredi  * take a long time, the caller should drop all locks before doing
4074bc27027aSMiklos Szeredi  * so.
4075bc27027aSMiklos Szeredi  *
4076bc27027aSMiklos Szeredi  * Alternatively, a caller may pass NULL for delegated_inode.  This may
4077bc27027aSMiklos Szeredi  * be appropriate for callers that expect the underlying filesystem not
4078bc27027aSMiklos Szeredi  * to be NFS exported.
4079bc27027aSMiklos Szeredi  *
40801da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
40811da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
40821da177e4SLinus Torvalds  * Problems:
4083d03b29a2SJ. Bruce Fields  *	a) we can get into loop creation.
40841da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
40851da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
4086a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
40871da177e4SLinus Torvalds  *	   story.
40886cedba89SJ. Bruce Fields  *	c) we have to lock _four_ objects - parents and victim (if it exists),
40896cedba89SJ. Bruce Fields  *	   and source (if it is not a directory).
40901b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
40911da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
40921da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
4093a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
40941da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
40951da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
40961da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
4097a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
40981da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
40991da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
41001da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
4101e4eaac06SSage Weil  *	d) conversion from fhandle to dentry may come in the wrong moment - when
41021b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
41031da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
4104c41b20e7SAdam Buchbinder  *	   ->i_mutex on parents, which works but leads to some truly excessive
41051da177e4SLinus Torvalds  *	   locking].
41061da177e4SLinus Torvalds  */
41071da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
41088e6d782cSJ. Bruce Fields 	       struct inode *new_dir, struct dentry *new_dentry,
4109520c8b16SMiklos Szeredi 	       struct inode **delegated_inode, unsigned int flags)
41101da177e4SLinus Torvalds {
41111da177e4SLinus Torvalds 	int error;
4112bc27027aSMiklos Szeredi 	bool is_dir = d_is_dir(old_dentry);
411359b0df21SEric Paris 	const unsigned char *old_name;
4114bc27027aSMiklos Szeredi 	struct inode *source = old_dentry->d_inode;
4115bc27027aSMiklos Szeredi 	struct inode *target = new_dentry->d_inode;
4116da1ce067SMiklos Szeredi 	bool new_is_dir = false;
4117da1ce067SMiklos Szeredi 	unsigned max_links = new_dir->i_sb->s_max_links;
41181da177e4SLinus Torvalds 
4119bc27027aSMiklos Szeredi 	if (source == target)
41201da177e4SLinus Torvalds 		return 0;
41211da177e4SLinus Torvalds 
41221da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
41231da177e4SLinus Torvalds 	if (error)
41241da177e4SLinus Torvalds 		return error;
41251da177e4SLinus Torvalds 
4126da1ce067SMiklos Szeredi 	if (!target) {
4127a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
4128da1ce067SMiklos Szeredi 	} else {
4129da1ce067SMiklos Szeredi 		new_is_dir = d_is_dir(new_dentry);
4130da1ce067SMiklos Szeredi 
4131da1ce067SMiklos Szeredi 		if (!(flags & RENAME_EXCHANGE))
41321da177e4SLinus Torvalds 			error = may_delete(new_dir, new_dentry, is_dir);
4133da1ce067SMiklos Szeredi 		else
4134da1ce067SMiklos Szeredi 			error = may_delete(new_dir, new_dentry, new_is_dir);
4135da1ce067SMiklos Szeredi 	}
41361da177e4SLinus Torvalds 	if (error)
41371da177e4SLinus Torvalds 		return error;
41381da177e4SLinus Torvalds 
41397177a9c4SMiklos Szeredi 	if (!old_dir->i_op->rename && !old_dir->i_op->rename2)
41401da177e4SLinus Torvalds 		return -EPERM;
41411da177e4SLinus Torvalds 
4142520c8b16SMiklos Szeredi 	if (flags && !old_dir->i_op->rename2)
4143520c8b16SMiklos Szeredi 		return -EINVAL;
4144520c8b16SMiklos Szeredi 
4145bc27027aSMiklos Szeredi 	/*
4146bc27027aSMiklos Szeredi 	 * If we are going to change the parent - check write permissions,
4147bc27027aSMiklos Szeredi 	 * we'll need to flip '..'.
4148bc27027aSMiklos Szeredi 	 */
4149da1ce067SMiklos Szeredi 	if (new_dir != old_dir) {
4150da1ce067SMiklos Szeredi 		if (is_dir) {
4151bc27027aSMiklos Szeredi 			error = inode_permission(source, MAY_WRITE);
4152bc27027aSMiklos Szeredi 			if (error)
4153bc27027aSMiklos Szeredi 				return error;
4154bc27027aSMiklos Szeredi 		}
4155da1ce067SMiklos Szeredi 		if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4156da1ce067SMiklos Szeredi 			error = inode_permission(target, MAY_WRITE);
4157da1ce067SMiklos Szeredi 			if (error)
4158da1ce067SMiklos Szeredi 				return error;
4159da1ce067SMiklos Szeredi 		}
4160da1ce067SMiklos Szeredi 	}
41610eeca283SRobert Love 
41620b3974ebSMiklos Szeredi 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
41630b3974ebSMiklos Szeredi 				      flags);
4164bc27027aSMiklos Szeredi 	if (error)
4165bc27027aSMiklos Szeredi 		return error;
4166bc27027aSMiklos Szeredi 
4167bc27027aSMiklos Szeredi 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
4168bc27027aSMiklos Szeredi 	dget(new_dentry);
4169da1ce067SMiklos Szeredi 	if (!is_dir || (flags & RENAME_EXCHANGE))
4170bc27027aSMiklos Szeredi 		lock_two_nondirectories(source, target);
4171bc27027aSMiklos Szeredi 	else if (target)
4172bc27027aSMiklos Szeredi 		mutex_lock(&target->i_mutex);
4173bc27027aSMiklos Szeredi 
4174bc27027aSMiklos Szeredi 	error = -EBUSY;
41757af1364fSEric W. Biederman 	if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
4176bc27027aSMiklos Szeredi 		goto out;
4177bc27027aSMiklos Szeredi 
4178da1ce067SMiklos Szeredi 	if (max_links && new_dir != old_dir) {
4179bc27027aSMiklos Szeredi 		error = -EMLINK;
4180da1ce067SMiklos Szeredi 		if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
4181bc27027aSMiklos Szeredi 			goto out;
4182da1ce067SMiklos Szeredi 		if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4183da1ce067SMiklos Szeredi 		    old_dir->i_nlink >= max_links)
4184da1ce067SMiklos Szeredi 			goto out;
4185da1ce067SMiklos Szeredi 	}
4186da1ce067SMiklos Szeredi 	if (is_dir && !(flags & RENAME_EXCHANGE) && target)
4187bc27027aSMiklos Szeredi 		shrink_dcache_parent(new_dentry);
4188da1ce067SMiklos Szeredi 	if (!is_dir) {
4189bc27027aSMiklos Szeredi 		error = try_break_deleg(source, delegated_inode);
4190bc27027aSMiklos Szeredi 		if (error)
4191bc27027aSMiklos Szeredi 			goto out;
4192da1ce067SMiklos Szeredi 	}
4193da1ce067SMiklos Szeredi 	if (target && !new_is_dir) {
4194bc27027aSMiklos Szeredi 		error = try_break_deleg(target, delegated_inode);
4195bc27027aSMiklos Szeredi 		if (error)
4196bc27027aSMiklos Szeredi 			goto out;
4197bc27027aSMiklos Szeredi 	}
41987177a9c4SMiklos Szeredi 	if (!old_dir->i_op->rename2) {
4199520c8b16SMiklos Szeredi 		error = old_dir->i_op->rename(old_dir, old_dentry,
4200520c8b16SMiklos Szeredi 					      new_dir, new_dentry);
4201520c8b16SMiklos Szeredi 	} else {
42027177a9c4SMiklos Szeredi 		WARN_ON(old_dir->i_op->rename != NULL);
4203520c8b16SMiklos Szeredi 		error = old_dir->i_op->rename2(old_dir, old_dentry,
4204520c8b16SMiklos Szeredi 					       new_dir, new_dentry, flags);
4205520c8b16SMiklos Szeredi 	}
4206bc27027aSMiklos Szeredi 	if (error)
4207bc27027aSMiklos Szeredi 		goto out;
4208bc27027aSMiklos Szeredi 
4209da1ce067SMiklos Szeredi 	if (!(flags & RENAME_EXCHANGE) && target) {
42101da177e4SLinus Torvalds 		if (is_dir)
4211bc27027aSMiklos Szeredi 			target->i_flags |= S_DEAD;
4212bc27027aSMiklos Szeredi 		dont_mount(new_dentry);
42138ed936b5SEric W. Biederman 		detach_mounts(new_dentry);
4214bc27027aSMiklos Szeredi 	}
4215da1ce067SMiklos Szeredi 	if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4216da1ce067SMiklos Szeredi 		if (!(flags & RENAME_EXCHANGE))
4217bc27027aSMiklos Szeredi 			d_move(old_dentry, new_dentry);
4218da1ce067SMiklos Szeredi 		else
4219da1ce067SMiklos Szeredi 			d_exchange(old_dentry, new_dentry);
4220da1ce067SMiklos Szeredi 	}
4221bc27027aSMiklos Szeredi out:
4222da1ce067SMiklos Szeredi 	if (!is_dir || (flags & RENAME_EXCHANGE))
4223bc27027aSMiklos Szeredi 		unlock_two_nondirectories(source, target);
4224bc27027aSMiklos Szeredi 	else if (target)
4225bc27027aSMiklos Szeredi 		mutex_unlock(&target->i_mutex);
4226bc27027aSMiklos Szeredi 	dput(new_dentry);
4227da1ce067SMiklos Szeredi 	if (!error) {
4228123df294SAl Viro 		fsnotify_move(old_dir, new_dir, old_name, is_dir,
4229da1ce067SMiklos Szeredi 			      !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4230da1ce067SMiklos Szeredi 		if (flags & RENAME_EXCHANGE) {
4231da1ce067SMiklos Szeredi 			fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
4232da1ce067SMiklos Szeredi 				      new_is_dir, NULL, new_dentry);
4233da1ce067SMiklos Szeredi 		}
4234da1ce067SMiklos Szeredi 	}
42350eeca283SRobert Love 	fsnotify_oldname_free(old_name);
42360eeca283SRobert Love 
42371da177e4SLinus Torvalds 	return error;
42381da177e4SLinus Torvalds }
42394d359507SAl Viro EXPORT_SYMBOL(vfs_rename);
42401da177e4SLinus Torvalds 
4241520c8b16SMiklos Szeredi SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4242520c8b16SMiklos Szeredi 		int, newdfd, const char __user *, newname, unsigned int, flags)
42431da177e4SLinus Torvalds {
42441da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
42451da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
42461da177e4SLinus Torvalds 	struct dentry *trap;
42471da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
42488e6d782cSJ. Bruce Fields 	struct inode *delegated_inode = NULL;
424991a27b2aSJeff Layton 	struct filename *from;
425091a27b2aSJeff Layton 	struct filename *to;
4251c6a94284SJeff Layton 	unsigned int lookup_flags = 0;
4252c6a94284SJeff Layton 	bool should_retry = false;
42532ad94ae6SAl Viro 	int error;
4254520c8b16SMiklos Szeredi 
42550d7a8555SMiklos Szeredi 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
4256da1ce067SMiklos Szeredi 		return -EINVAL;
4257da1ce067SMiklos Szeredi 
42580d7a8555SMiklos Szeredi 	if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
42590d7a8555SMiklos Szeredi 	    (flags & RENAME_EXCHANGE))
4260520c8b16SMiklos Szeredi 		return -EINVAL;
4261520c8b16SMiklos Szeredi 
42620d7a8555SMiklos Szeredi 	if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
42630d7a8555SMiklos Szeredi 		return -EPERM;
42640d7a8555SMiklos Szeredi 
4265c6a94284SJeff Layton retry:
4266c6a94284SJeff Layton 	from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
426791a27b2aSJeff Layton 	if (IS_ERR(from)) {
426891a27b2aSJeff Layton 		error = PTR_ERR(from);
42691da177e4SLinus Torvalds 		goto exit;
427091a27b2aSJeff Layton 	}
42711da177e4SLinus Torvalds 
4272c6a94284SJeff Layton 	to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
427391a27b2aSJeff Layton 	if (IS_ERR(to)) {
427491a27b2aSJeff Layton 		error = PTR_ERR(to);
42751da177e4SLinus Torvalds 		goto exit1;
427691a27b2aSJeff Layton 	}
42771da177e4SLinus Torvalds 
42781da177e4SLinus Torvalds 	error = -EXDEV;
42794ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
42801da177e4SLinus Torvalds 		goto exit2;
42811da177e4SLinus Torvalds 
42824ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
42831da177e4SLinus Torvalds 	error = -EBUSY;
42841da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
42851da177e4SLinus Torvalds 		goto exit2;
42861da177e4SLinus Torvalds 
42874ac91378SJan Blunck 	new_dir = newnd.path.dentry;
42880a7c3937SMiklos Szeredi 	if (flags & RENAME_NOREPLACE)
42890a7c3937SMiklos Szeredi 		error = -EEXIST;
42901da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
42911da177e4SLinus Torvalds 		goto exit2;
42921da177e4SLinus Torvalds 
4293c30dabfeSJan Kara 	error = mnt_want_write(oldnd.path.mnt);
4294c30dabfeSJan Kara 	if (error)
4295c30dabfeSJan Kara 		goto exit2;
4296c30dabfeSJan Kara 
42970612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
42980612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
4299da1ce067SMiklos Szeredi 	if (!(flags & RENAME_EXCHANGE))
43004e9ed2f8SOGAWA Hirofumi 		newnd.flags |= LOOKUP_RENAME_TARGET;
43010612d9fbSOGAWA Hirofumi 
43028e6d782cSJ. Bruce Fields retry_deleg:
43031da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
43041da177e4SLinus Torvalds 
430549705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
43061da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
43071da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
43081da177e4SLinus Torvalds 		goto exit3;
43091da177e4SLinus Torvalds 	/* source must exist */
43101da177e4SLinus Torvalds 	error = -ENOENT;
4311b18825a7SDavid Howells 	if (d_is_negative(old_dentry))
43121da177e4SLinus Torvalds 		goto exit4;
431349705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
43141da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
43151da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
43161da177e4SLinus Torvalds 		goto exit4;
43170a7c3937SMiklos Szeredi 	error = -EEXIST;
43180a7c3937SMiklos Szeredi 	if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
43190a7c3937SMiklos Szeredi 		goto exit5;
4320da1ce067SMiklos Szeredi 	if (flags & RENAME_EXCHANGE) {
4321da1ce067SMiklos Szeredi 		error = -ENOENT;
4322da1ce067SMiklos Szeredi 		if (d_is_negative(new_dentry))
4323da1ce067SMiklos Szeredi 			goto exit5;
4324da1ce067SMiklos Szeredi 
4325da1ce067SMiklos Szeredi 		if (!d_is_dir(new_dentry)) {
4326da1ce067SMiklos Szeredi 			error = -ENOTDIR;
4327da1ce067SMiklos Szeredi 			if (newnd.last.name[newnd.last.len])
4328da1ce067SMiklos Szeredi 				goto exit5;
4329da1ce067SMiklos Szeredi 		}
4330da1ce067SMiklos Szeredi 	}
43310a7c3937SMiklos Szeredi 	/* unless the source is a directory trailing slashes give -ENOTDIR */
43320a7c3937SMiklos Szeredi 	if (!d_is_dir(old_dentry)) {
43330a7c3937SMiklos Szeredi 		error = -ENOTDIR;
43340a7c3937SMiklos Szeredi 		if (oldnd.last.name[oldnd.last.len])
43350a7c3937SMiklos Szeredi 			goto exit5;
4336da1ce067SMiklos Szeredi 		if (!(flags & RENAME_EXCHANGE) && newnd.last.name[newnd.last.len])
43370a7c3937SMiklos Szeredi 			goto exit5;
43380a7c3937SMiklos Szeredi 	}
43390a7c3937SMiklos Szeredi 	/* source should not be ancestor of target */
43400a7c3937SMiklos Szeredi 	error = -EINVAL;
43410a7c3937SMiklos Szeredi 	if (old_dentry == trap)
43420a7c3937SMiklos Szeredi 		goto exit5;
43431da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
4344da1ce067SMiklos Szeredi 	if (!(flags & RENAME_EXCHANGE))
43451da177e4SLinus Torvalds 		error = -ENOTEMPTY;
43461da177e4SLinus Torvalds 	if (new_dentry == trap)
43471da177e4SLinus Torvalds 		goto exit5;
43481da177e4SLinus Torvalds 
4349be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
43500b3974ebSMiklos Szeredi 				     &newnd.path, new_dentry, flags);
4351be6d3e56SKentaro Takeda 	if (error)
4352c30dabfeSJan Kara 		goto exit5;
43531da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
43548e6d782cSJ. Bruce Fields 			   new_dir->d_inode, new_dentry,
4355520c8b16SMiklos Szeredi 			   &delegated_inode, flags);
43561da177e4SLinus Torvalds exit5:
43571da177e4SLinus Torvalds 	dput(new_dentry);
43581da177e4SLinus Torvalds exit4:
43591da177e4SLinus Torvalds 	dput(old_dentry);
43601da177e4SLinus Torvalds exit3:
43611da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
43628e6d782cSJ. Bruce Fields 	if (delegated_inode) {
43638e6d782cSJ. Bruce Fields 		error = break_deleg_wait(&delegated_inode);
43648e6d782cSJ. Bruce Fields 		if (!error)
43658e6d782cSJ. Bruce Fields 			goto retry_deleg;
43668e6d782cSJ. Bruce Fields 	}
4367c30dabfeSJan Kara 	mnt_drop_write(oldnd.path.mnt);
43681da177e4SLinus Torvalds exit2:
4369c6a94284SJeff Layton 	if (retry_estale(error, lookup_flags))
4370c6a94284SJeff Layton 		should_retry = true;
43711d957f9bSJan Blunck 	path_put(&newnd.path);
43722ad94ae6SAl Viro 	putname(to);
43731da177e4SLinus Torvalds exit1:
43741d957f9bSJan Blunck 	path_put(&oldnd.path);
43751da177e4SLinus Torvalds 	putname(from);
4376c6a94284SJeff Layton 	if (should_retry) {
4377c6a94284SJeff Layton 		should_retry = false;
4378c6a94284SJeff Layton 		lookup_flags |= LOOKUP_REVAL;
4379c6a94284SJeff Layton 		goto retry;
4380c6a94284SJeff Layton 	}
43812ad94ae6SAl Viro exit:
43821da177e4SLinus Torvalds 	return error;
43831da177e4SLinus Torvalds }
43841da177e4SLinus Torvalds 
4385520c8b16SMiklos Szeredi SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4386520c8b16SMiklos Szeredi 		int, newdfd, const char __user *, newname)
4387520c8b16SMiklos Szeredi {
4388520c8b16SMiklos Szeredi 	return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
4389520c8b16SMiklos Szeredi }
4390520c8b16SMiklos Szeredi 
4391a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
43925590ff0dSUlrich Drepper {
4393520c8b16SMiklos Szeredi 	return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
43945590ff0dSUlrich Drepper }
43955590ff0dSUlrich Drepper 
4396787fb6bcSMiklos Szeredi int vfs_whiteout(struct inode *dir, struct dentry *dentry)
4397787fb6bcSMiklos Szeredi {
4398787fb6bcSMiklos Szeredi 	int error = may_create(dir, dentry);
4399787fb6bcSMiklos Szeredi 	if (error)
4400787fb6bcSMiklos Szeredi 		return error;
4401787fb6bcSMiklos Szeredi 
4402787fb6bcSMiklos Szeredi 	if (!dir->i_op->mknod)
4403787fb6bcSMiklos Szeredi 		return -EPERM;
4404787fb6bcSMiklos Szeredi 
4405787fb6bcSMiklos Szeredi 	return dir->i_op->mknod(dir, dentry,
4406787fb6bcSMiklos Szeredi 				S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
4407787fb6bcSMiklos Szeredi }
4408787fb6bcSMiklos Szeredi EXPORT_SYMBOL(vfs_whiteout);
4409787fb6bcSMiklos Szeredi 
44105d826c84SAl Viro int readlink_copy(char __user *buffer, int buflen, const char *link)
44111da177e4SLinus Torvalds {
44125d826c84SAl Viro 	int len = PTR_ERR(link);
44131da177e4SLinus Torvalds 	if (IS_ERR(link))
44141da177e4SLinus Torvalds 		goto out;
44151da177e4SLinus Torvalds 
44161da177e4SLinus Torvalds 	len = strlen(link);
44171da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
44181da177e4SLinus Torvalds 		len = buflen;
44191da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
44201da177e4SLinus Torvalds 		len = -EFAULT;
44211da177e4SLinus Torvalds out:
44221da177e4SLinus Torvalds 	return len;
44231da177e4SLinus Torvalds }
44245d826c84SAl Viro EXPORT_SYMBOL(readlink_copy);
44251da177e4SLinus Torvalds 
44261da177e4SLinus Torvalds /*
44271da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
44281da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
44291da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
44301da177e4SLinus Torvalds  */
44311da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
44321da177e4SLinus Torvalds {
44331da177e4SLinus Torvalds 	struct nameidata nd;
4434cc314eefSLinus Torvalds 	void *cookie;
4435694a1764SMarcin Slusarz 	int res;
4436cc314eefSLinus Torvalds 
44371da177e4SLinus Torvalds 	nd.depth = 0;
4438cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
4439694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
4440694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
4441694a1764SMarcin Slusarz 
44425d826c84SAl Viro 	res = readlink_copy(buffer, buflen, nd_get_link(&nd));
44431da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
4444cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
4445694a1764SMarcin Slusarz 	return res;
44461da177e4SLinus Torvalds }
44474d359507SAl Viro EXPORT_SYMBOL(generic_readlink);
44481da177e4SLinus Torvalds 
44491da177e4SLinus Torvalds /* get the link contents into pagecache */
44501da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
44511da177e4SLinus Torvalds {
4452ebd09abbSDuane Griffin 	char *kaddr;
44531da177e4SLinus Torvalds 	struct page *page;
44541da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
4455090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
44561da177e4SLinus Torvalds 	if (IS_ERR(page))
44576fe6900eSNick Piggin 		return (char*)page;
44581da177e4SLinus Torvalds 	*ppage = page;
4459ebd09abbSDuane Griffin 	kaddr = kmap(page);
4460ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
4461ebd09abbSDuane Griffin 	return kaddr;
44621da177e4SLinus Torvalds }
44631da177e4SLinus Torvalds 
44641da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
44651da177e4SLinus Torvalds {
44661da177e4SLinus Torvalds 	struct page *page = NULL;
44675d826c84SAl Viro 	int res = readlink_copy(buffer, buflen, page_getlink(dentry, &page));
44681da177e4SLinus Torvalds 	if (page) {
44691da177e4SLinus Torvalds 		kunmap(page);
44701da177e4SLinus Torvalds 		page_cache_release(page);
44711da177e4SLinus Torvalds 	}
44721da177e4SLinus Torvalds 	return res;
44731da177e4SLinus Torvalds }
44744d359507SAl Viro EXPORT_SYMBOL(page_readlink);
44751da177e4SLinus Torvalds 
4476cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
44771da177e4SLinus Torvalds {
4478cc314eefSLinus Torvalds 	struct page *page = NULL;
44791da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
4480cc314eefSLinus Torvalds 	return page;
44811da177e4SLinus Torvalds }
44824d359507SAl Viro EXPORT_SYMBOL(page_follow_link_light);
44831da177e4SLinus Torvalds 
4484cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
44851da177e4SLinus Torvalds {
4486cc314eefSLinus Torvalds 	struct page *page = cookie;
4487cc314eefSLinus Torvalds 
4488cc314eefSLinus Torvalds 	if (page) {
44891da177e4SLinus Torvalds 		kunmap(page);
44901da177e4SLinus Torvalds 		page_cache_release(page);
44911da177e4SLinus Torvalds 	}
44921da177e4SLinus Torvalds }
44934d359507SAl Viro EXPORT_SYMBOL(page_put_link);
44941da177e4SLinus Torvalds 
449554566b2cSNick Piggin /*
449654566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
449754566b2cSNick Piggin  */
449854566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
44991da177e4SLinus Torvalds {
45001da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
45010adb25d2SKirill Korotaev 	struct page *page;
4502afddba49SNick Piggin 	void *fsdata;
4503beb497abSDmitriy Monakhov 	int err;
45041da177e4SLinus Torvalds 	char *kaddr;
450554566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
450654566b2cSNick Piggin 	if (nofs)
450754566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
45081da177e4SLinus Torvalds 
45097e53cac4SNeilBrown retry:
4510afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
451154566b2cSNick Piggin 				flags, &page, &fsdata);
45121da177e4SLinus Torvalds 	if (err)
4513afddba49SNick Piggin 		goto fail;
4514afddba49SNick Piggin 
4515e8e3c3d6SCong Wang 	kaddr = kmap_atomic(page);
45161da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
4517e8e3c3d6SCong Wang 	kunmap_atomic(kaddr);
4518afddba49SNick Piggin 
4519afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4520afddba49SNick Piggin 							page, fsdata);
45211da177e4SLinus Torvalds 	if (err < 0)
45221da177e4SLinus Torvalds 		goto fail;
4523afddba49SNick Piggin 	if (err < len-1)
4524afddba49SNick Piggin 		goto retry;
4525afddba49SNick Piggin 
45261da177e4SLinus Torvalds 	mark_inode_dirty(inode);
45271da177e4SLinus Torvalds 	return 0;
45281da177e4SLinus Torvalds fail:
45291da177e4SLinus Torvalds 	return err;
45301da177e4SLinus Torvalds }
45314d359507SAl Viro EXPORT_SYMBOL(__page_symlink);
45321da177e4SLinus Torvalds 
45330adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
45340adb25d2SKirill Korotaev {
45350adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
453654566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
45370adb25d2SKirill Korotaev }
45384d359507SAl Viro EXPORT_SYMBOL(page_symlink);
45390adb25d2SKirill Korotaev 
454092e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
45411da177e4SLinus Torvalds 	.readlink	= generic_readlink,
45421da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
45431da177e4SLinus Torvalds 	.put_link	= page_put_link,
45441da177e4SLinus Torvalds };
45451da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
4546