xref: /openbmc/linux/fs/namei.c (revision c8fe8f30)
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>
181da177e4SLinus Torvalds #include <linux/module.h>
191da177e4SLinus Torvalds #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/fs.h>
211da177e4SLinus Torvalds #include <linux/namei.h>
221da177e4SLinus Torvalds #include <linux/quotaops.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>
271da177e4SLinus Torvalds #include <linux/syscalls.h>
281da177e4SLinus Torvalds #include <linux/mount.h>
291da177e4SLinus Torvalds #include <linux/audit.h>
3016f7e0feSRandy Dunlap #include <linux/capability.h>
31834f2a4aSTrond Myklebust #include <linux/file.h>
325590ff0dSUlrich Drepper #include <linux/fcntl.h>
3308ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h>
341da177e4SLinus Torvalds #include <asm/uaccess.h>
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer]
391da177e4SLinus Torvalds  * Fundamental changes in the pathname lookup mechanisms (namei)
401da177e4SLinus Torvalds  * were necessary because of omirr.  The reason is that omirr needs
411da177e4SLinus Torvalds  * to know the _real_ pathname, not the user-supplied one, in case
421da177e4SLinus Torvalds  * of symlinks (and also when transname replacements occur).
431da177e4SLinus Torvalds  *
441da177e4SLinus Torvalds  * The new code replaces the old recursive symlink resolution with
451da177e4SLinus Torvalds  * an iterative one (in case of non-nested symlink chains).  It does
461da177e4SLinus Torvalds  * this with calls to <fs>_follow_link().
471da177e4SLinus Torvalds  * As a side effect, dir_namei(), _namei() and follow_link() are now
481da177e4SLinus Torvalds  * replaced with a single function lookup_dentry() that can handle all
491da177e4SLinus Torvalds  * the special cases of the former code.
501da177e4SLinus Torvalds  *
511da177e4SLinus Torvalds  * With the new dcache, the pathname is stored at each inode, at least as
521da177e4SLinus Torvalds  * long as the refcount of the inode is positive.  As a side effect, the
531da177e4SLinus Torvalds  * size of the dcache depends on the inode cache and thus is dynamic.
541da177e4SLinus Torvalds  *
551da177e4SLinus Torvalds  * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
561da177e4SLinus Torvalds  * resolution to correspond with current state of the code.
571da177e4SLinus Torvalds  *
581da177e4SLinus Torvalds  * Note that the symlink resolution is not *completely* iterative.
591da177e4SLinus Torvalds  * There is still a significant amount of tail- and mid- recursion in
601da177e4SLinus Torvalds  * the algorithm.  Also, note that <fs>_readlink() is not used in
611da177e4SLinus Torvalds  * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
621da177e4SLinus Torvalds  * may return different results than <fs>_follow_link().  Many virtual
631da177e4SLinus Torvalds  * filesystems (including /proc) exhibit this behavior.
641da177e4SLinus Torvalds  */
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
671da177e4SLinus Torvalds  * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
681da177e4SLinus Torvalds  * and the name already exists in form of a symlink, try to create the new
691da177e4SLinus Torvalds  * name indicated by the symlink. The old code always complained that the
701da177e4SLinus Torvalds  * name already exists, due to not following the symlink even if its target
711da177e4SLinus Torvalds  * is nonexistent.  The new semantics affects also mknod() and link() when
721da177e4SLinus Torvalds  * the name is a symlink pointing to a non-existant name.
731da177e4SLinus Torvalds  *
741da177e4SLinus Torvalds  * I don't know which semantics is the right one, since I have no access
751da177e4SLinus Torvalds  * to standards. But I found by trial that HP-UX 9.0 has the full "new"
761da177e4SLinus Torvalds  * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
771da177e4SLinus Torvalds  * "old" one. Personally, I think the new semantics is much more logical.
781da177e4SLinus Torvalds  * Note that "ln old new" where "new" is a symlink pointing to a non-existing
791da177e4SLinus Torvalds  * file does succeed in both HP-UX and SunOs, but not in Solaris
801da177e4SLinus Torvalds  * and in the old Linux semantics.
811da177e4SLinus Torvalds  */
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
841da177e4SLinus Torvalds  * semantics.  See the comments in "open_namei" and "do_link" below.
851da177e4SLinus Torvalds  *
861da177e4SLinus Torvalds  * [10-Sep-98 Alan Modra] Another symlink change.
871da177e4SLinus Torvalds  */
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
901da177e4SLinus Torvalds  *	inside the path - always follow.
911da177e4SLinus Torvalds  *	in the last component in creation/removal/renaming - never follow.
921da177e4SLinus Torvalds  *	if LOOKUP_FOLLOW passed - follow.
931da177e4SLinus Torvalds  *	if the pathname has trailing slashes - follow.
941da177e4SLinus Torvalds  *	otherwise - don't follow.
951da177e4SLinus Torvalds  * (applied in that order).
961da177e4SLinus Torvalds  *
971da177e4SLinus Torvalds  * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
981da177e4SLinus Torvalds  * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
991da177e4SLinus Torvalds  * During the 2.4 we need to fix the userland stuff depending on it -
1001da177e4SLinus Torvalds  * hopefully we will be able to get rid of that wart in 2.5. So far only
1011da177e4SLinus Torvalds  * XEmacs seems to be relying on it...
1021da177e4SLinus Torvalds  */
1031da177e4SLinus Torvalds /*
1041da177e4SLinus Torvalds  * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
105a11f3a05SArjan van de Ven  * implemented.  Let's see if raised priority of ->s_vfs_rename_mutex gives
1061da177e4SLinus Torvalds  * any extra contention...
1071da177e4SLinus Torvalds  */
1081da177e4SLinus Torvalds 
109a02f76c3SAl Viro static int __link_path_walk(const char *name, struct nameidata *nd);
110c4a7808fSJosef 'Jeff' Sipek 
1111da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional
1121da177e4SLinus Torvalds  * checking and hopefully speeding things up, we copy filenames to the
1131da177e4SLinus Torvalds  * kernel data space before using them..
1141da177e4SLinus Torvalds  *
1151da177e4SLinus Torvalds  * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
1161da177e4SLinus Torvalds  * PATH_MAX includes the nul terminator --RR.
1171da177e4SLinus Torvalds  */
118858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page)
1191da177e4SLinus Torvalds {
1201da177e4SLinus Torvalds 	int retval;
1211da177e4SLinus Torvalds 	unsigned long len = PATH_MAX;
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	if (!segment_eq(get_fs(), KERNEL_DS)) {
1241da177e4SLinus Torvalds 		if ((unsigned long) filename >= TASK_SIZE)
1251da177e4SLinus Torvalds 			return -EFAULT;
1261da177e4SLinus Torvalds 		if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
1271da177e4SLinus Torvalds 			len = TASK_SIZE - (unsigned long) filename;
1281da177e4SLinus Torvalds 	}
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	retval = strncpy_from_user(page, filename, len);
1311da177e4SLinus Torvalds 	if (retval > 0) {
1321da177e4SLinus Torvalds 		if (retval < len)
1331da177e4SLinus Torvalds 			return 0;
1341da177e4SLinus Torvalds 		return -ENAMETOOLONG;
1351da177e4SLinus Torvalds 	} else if (!retval)
1361da177e4SLinus Torvalds 		retval = -ENOENT;
1371da177e4SLinus Torvalds 	return retval;
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds char * getname(const char __user * filename)
1411da177e4SLinus Torvalds {
1421da177e4SLinus Torvalds 	char *tmp, *result;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	result = ERR_PTR(-ENOMEM);
1451da177e4SLinus Torvalds 	tmp = __getname();
1461da177e4SLinus Torvalds 	if (tmp)  {
1471da177e4SLinus Torvalds 		int retval = do_getname(filename, tmp);
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 		result = tmp;
1501da177e4SLinus Torvalds 		if (retval < 0) {
1511da177e4SLinus Torvalds 			__putname(tmp);
1521da177e4SLinus Torvalds 			result = ERR_PTR(retval);
1531da177e4SLinus Torvalds 		}
1541da177e4SLinus Torvalds 	}
1551da177e4SLinus Torvalds 	audit_getname(result);
1561da177e4SLinus Torvalds 	return result;
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL
1601da177e4SLinus Torvalds void putname(const char *name)
1611da177e4SLinus Torvalds {
1625ac3a9c2SAl Viro 	if (unlikely(!audit_dummy_context()))
1631da177e4SLinus Torvalds 		audit_putname(name);
1641da177e4SLinus Torvalds 	else
1651da177e4SLinus Torvalds 		__putname(name);
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds EXPORT_SYMBOL(putname);
1681da177e4SLinus Torvalds #endif
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds /**
1721da177e4SLinus Torvalds  * generic_permission  -  check for access rights on a Posix-like filesystem
1731da177e4SLinus Torvalds  * @inode:	inode to check access rights for
1741da177e4SLinus Torvalds  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
1751da177e4SLinus Torvalds  * @check_acl:	optional callback to check for Posix ACLs
1761da177e4SLinus Torvalds  *
1771da177e4SLinus Torvalds  * Used to check for read/write/execute permissions on a file.
1781da177e4SLinus Torvalds  * We use "fsuid" for this, letting us set arbitrary permissions
1791da177e4SLinus Torvalds  * for filesystem access without changing the "normal" uids which
1801da177e4SLinus Torvalds  * are used for other things..
1811da177e4SLinus Torvalds  */
1821da177e4SLinus Torvalds int generic_permission(struct inode *inode, int mask,
1831da177e4SLinus Torvalds 		int (*check_acl)(struct inode *inode, int mask))
1841da177e4SLinus Torvalds {
1851da177e4SLinus Torvalds 	umode_t			mode = inode->i_mode;
1861da177e4SLinus Torvalds 
187e6305c43SAl Viro 	mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
188e6305c43SAl Viro 
189da9592edSDavid Howells 	if (current_fsuid() == inode->i_uid)
1901da177e4SLinus Torvalds 		mode >>= 6;
1911da177e4SLinus Torvalds 	else {
1921da177e4SLinus Torvalds 		if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
1931da177e4SLinus Torvalds 			int error = check_acl(inode, mask);
1941da177e4SLinus Torvalds 			if (error == -EACCES)
1951da177e4SLinus Torvalds 				goto check_capabilities;
1961da177e4SLinus Torvalds 			else if (error != -EAGAIN)
1971da177e4SLinus Torvalds 				return error;
1981da177e4SLinus Torvalds 		}
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 		if (in_group_p(inode->i_gid))
2011da177e4SLinus Torvalds 			mode >>= 3;
2021da177e4SLinus Torvalds 	}
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	/*
2051da177e4SLinus Torvalds 	 * If the DACs are ok we don't need any capability check.
2061da177e4SLinus Torvalds 	 */
207e6305c43SAl Viro 	if ((mask & ~mode) == 0)
2081da177e4SLinus Torvalds 		return 0;
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds  check_capabilities:
2111da177e4SLinus Torvalds 	/*
2121da177e4SLinus Torvalds 	 * Read/write DACs are always overridable.
2131da177e4SLinus Torvalds 	 * Executable DACs are overridable if at least one exec bit is set.
2141da177e4SLinus Torvalds 	 */
215f696a365SMiklos Szeredi 	if (!(mask & MAY_EXEC) || execute_ok(inode))
2161da177e4SLinus Torvalds 		if (capable(CAP_DAC_OVERRIDE))
2171da177e4SLinus Torvalds 			return 0;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	/*
2201da177e4SLinus Torvalds 	 * Searching includes executable on directories, else just read.
2211da177e4SLinus Torvalds 	 */
2221da177e4SLinus Torvalds 	if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
2231da177e4SLinus Torvalds 		if (capable(CAP_DAC_READ_SEARCH))
2241da177e4SLinus Torvalds 			return 0;
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	return -EACCES;
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds 
229cb23beb5SChristoph Hellwig /**
230cb23beb5SChristoph Hellwig  * inode_permission  -  check for access rights to a given inode
231cb23beb5SChristoph Hellwig  * @inode:	inode to check permission on
232cb23beb5SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
233cb23beb5SChristoph Hellwig  *
234cb23beb5SChristoph Hellwig  * Used to check for read/write/execute permissions on an inode.
235cb23beb5SChristoph Hellwig  * We use "fsuid" for this, letting us set arbitrary permissions
236cb23beb5SChristoph Hellwig  * for filesystem access without changing the "normal" uids which
237cb23beb5SChristoph Hellwig  * are used for other things.
238cb23beb5SChristoph Hellwig  */
239f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask)
2401da177e4SLinus Torvalds {
241e6305c43SAl Viro 	int retval;
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	if (mask & MAY_WRITE) {
24422590e41SMiklos Szeredi 		umode_t mode = inode->i_mode;
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 		/*
2471da177e4SLinus Torvalds 		 * Nobody gets write access to a read-only fs.
2481da177e4SLinus Torvalds 		 */
2491da177e4SLinus Torvalds 		if (IS_RDONLY(inode) &&
2501da177e4SLinus Torvalds 		    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
2511da177e4SLinus Torvalds 			return -EROFS;
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 		/*
2541da177e4SLinus Torvalds 		 * Nobody gets write access to an immutable file.
2551da177e4SLinus Torvalds 		 */
2561da177e4SLinus Torvalds 		if (IS_IMMUTABLE(inode))
2571da177e4SLinus Torvalds 			return -EACCES;
2581da177e4SLinus Torvalds 	}
2591da177e4SLinus Torvalds 
260acfa4380SAl Viro 	if (inode->i_op->permission)
261b77b0646SAl Viro 		retval = inode->i_op->permission(inode, mask);
262f696a365SMiklos Szeredi 	else
263e6305c43SAl Viro 		retval = generic_permission(inode, mask, NULL);
264f696a365SMiklos Szeredi 
2651da177e4SLinus Torvalds 	if (retval)
2661da177e4SLinus Torvalds 		return retval;
2671da177e4SLinus Torvalds 
26808ce5f16SSerge E. Hallyn 	retval = devcgroup_inode_permission(inode, mask);
26908ce5f16SSerge E. Hallyn 	if (retval)
27008ce5f16SSerge E. Hallyn 		return retval;
27108ce5f16SSerge E. Hallyn 
272e6305c43SAl Viro 	return security_inode_permission(inode,
273f418b006SStephen Smalley 			mask & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND));
2741da177e4SLinus Torvalds }
2751da177e4SLinus Torvalds 
276e4543eddSChristoph Hellwig /**
2778c744fb8SChristoph Hellwig  * file_permission  -  check for additional access rights to a given file
2788c744fb8SChristoph Hellwig  * @file:	file to check access rights for
2798c744fb8SChristoph Hellwig  * @mask:	right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
2808c744fb8SChristoph Hellwig  *
2818c744fb8SChristoph Hellwig  * Used to check for read/write/execute permissions on an already opened
2828c744fb8SChristoph Hellwig  * file.
2838c744fb8SChristoph Hellwig  *
2848c744fb8SChristoph Hellwig  * Note:
2858c744fb8SChristoph Hellwig  *	Do not use this function in new code.  All access checks should
286cb23beb5SChristoph Hellwig  *	be done using inode_permission().
2878c744fb8SChristoph Hellwig  */
2888c744fb8SChristoph Hellwig int file_permission(struct file *file, int mask)
2898c744fb8SChristoph Hellwig {
290f419a2e3SAl Viro 	return inode_permission(file->f_path.dentry->d_inode, mask);
2918c744fb8SChristoph Hellwig }
2928c744fb8SChristoph Hellwig 
2931da177e4SLinus Torvalds /*
2941da177e4SLinus Torvalds  * get_write_access() gets write permission for a file.
2951da177e4SLinus Torvalds  * put_write_access() releases this write permission.
2961da177e4SLinus Torvalds  * This is used for regular files.
2971da177e4SLinus Torvalds  * We cannot support write (and maybe mmap read-write shared) accesses and
2981da177e4SLinus Torvalds  * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
2991da177e4SLinus Torvalds  * can have the following values:
3001da177e4SLinus Torvalds  * 0: no writers, no VM_DENYWRITE mappings
3011da177e4SLinus Torvalds  * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
3021da177e4SLinus Torvalds  * > 0: (i_writecount) users are writing to the file.
3031da177e4SLinus Torvalds  *
3041da177e4SLinus Torvalds  * Normally we operate on that counter with atomic_{inc,dec} and it's safe
3051da177e4SLinus Torvalds  * except for the cases where we don't hold i_writecount yet. Then we need to
3061da177e4SLinus Torvalds  * use {get,deny}_write_access() - these functions check the sign and refuse
3071da177e4SLinus Torvalds  * to do the change if sign is wrong. Exclusion between them is provided by
3081da177e4SLinus Torvalds  * the inode->i_lock spinlock.
3091da177e4SLinus Torvalds  */
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds int get_write_access(struct inode * inode)
3121da177e4SLinus Torvalds {
3131da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
3141da177e4SLinus Torvalds 	if (atomic_read(&inode->i_writecount) < 0) {
3151da177e4SLinus Torvalds 		spin_unlock(&inode->i_lock);
3161da177e4SLinus Torvalds 		return -ETXTBSY;
3171da177e4SLinus Torvalds 	}
3181da177e4SLinus Torvalds 	atomic_inc(&inode->i_writecount);
3191da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	return 0;
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds int deny_write_access(struct file * file)
3251da177e4SLinus Torvalds {
3260f7fc9e4SJosef "Jeff" Sipek 	struct inode *inode = file->f_path.dentry->d_inode;
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	spin_lock(&inode->i_lock);
3291da177e4SLinus Torvalds 	if (atomic_read(&inode->i_writecount) > 0) {
3301da177e4SLinus Torvalds 		spin_unlock(&inode->i_lock);
3311da177e4SLinus Torvalds 		return -ETXTBSY;
3321da177e4SLinus Torvalds 	}
3331da177e4SLinus Torvalds 	atomic_dec(&inode->i_writecount);
3341da177e4SLinus Torvalds 	spin_unlock(&inode->i_lock);
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	return 0;
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds 
3391d957f9bSJan Blunck /**
3405dd784d0SJan Blunck  * path_get - get a reference to a path
3415dd784d0SJan Blunck  * @path: path to get the reference to
3425dd784d0SJan Blunck  *
3435dd784d0SJan Blunck  * Given a path increment the reference count to the dentry and the vfsmount.
3445dd784d0SJan Blunck  */
3455dd784d0SJan Blunck void path_get(struct path *path)
3465dd784d0SJan Blunck {
3475dd784d0SJan Blunck 	mntget(path->mnt);
3485dd784d0SJan Blunck 	dget(path->dentry);
3495dd784d0SJan Blunck }
3505dd784d0SJan Blunck EXPORT_SYMBOL(path_get);
3515dd784d0SJan Blunck 
3525dd784d0SJan Blunck /**
3531d957f9bSJan Blunck  * path_put - put a reference to a path
3541d957f9bSJan Blunck  * @path: path to put the reference to
3551d957f9bSJan Blunck  *
3561d957f9bSJan Blunck  * Given a path decrement the reference count to the dentry and the vfsmount.
3571d957f9bSJan Blunck  */
3581d957f9bSJan Blunck void path_put(struct path *path)
3591da177e4SLinus Torvalds {
3601d957f9bSJan Blunck 	dput(path->dentry);
3611d957f9bSJan Blunck 	mntput(path->mnt);
3621da177e4SLinus Torvalds }
3631d957f9bSJan Blunck EXPORT_SYMBOL(path_put);
3641da177e4SLinus Torvalds 
365834f2a4aSTrond Myklebust /**
366834f2a4aSTrond Myklebust  * release_open_intent - free up open intent resources
367834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
368834f2a4aSTrond Myklebust  */
369834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd)
370834f2a4aSTrond Myklebust {
3710f7fc9e4SJosef "Jeff" Sipek 	if (nd->intent.open.file->f_path.dentry == NULL)
372834f2a4aSTrond Myklebust 		put_filp(nd->intent.open.file);
373834f2a4aSTrond Myklebust 	else
374834f2a4aSTrond Myklebust 		fput(nd->intent.open.file);
375834f2a4aSTrond Myklebust }
376834f2a4aSTrond Myklebust 
377bcdc5e01SIan Kent static inline struct dentry *
378bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd)
379bcdc5e01SIan Kent {
380bcdc5e01SIan Kent 	int status = dentry->d_op->d_revalidate(dentry, nd);
381bcdc5e01SIan Kent 	if (unlikely(status <= 0)) {
382bcdc5e01SIan Kent 		/*
383bcdc5e01SIan Kent 		 * The dentry failed validation.
384bcdc5e01SIan Kent 		 * If d_revalidate returned 0 attempt to invalidate
385bcdc5e01SIan Kent 		 * the dentry otherwise d_revalidate is asking us
386bcdc5e01SIan Kent 		 * to return a fail status.
387bcdc5e01SIan Kent 		 */
388bcdc5e01SIan Kent 		if (!status) {
389bcdc5e01SIan Kent 			if (!d_invalidate(dentry)) {
390bcdc5e01SIan Kent 				dput(dentry);
391bcdc5e01SIan Kent 				dentry = NULL;
392bcdc5e01SIan Kent 			}
393bcdc5e01SIan Kent 		} else {
394bcdc5e01SIan Kent 			dput(dentry);
395bcdc5e01SIan Kent 			dentry = ERR_PTR(status);
396bcdc5e01SIan Kent 		}
397bcdc5e01SIan Kent 	}
398bcdc5e01SIan Kent 	return dentry;
399bcdc5e01SIan Kent }
400bcdc5e01SIan Kent 
4011da177e4SLinus Torvalds /*
4021da177e4SLinus Torvalds  * Internal lookup() using the new generic dcache.
4031da177e4SLinus Torvalds  * SMP-safe
4041da177e4SLinus Torvalds  */
4051da177e4SLinus Torvalds static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
4061da177e4SLinus Torvalds {
4071da177e4SLinus Torvalds 	struct dentry * dentry = __d_lookup(parent, name);
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 	/* lockess __d_lookup may fail due to concurrent d_move()
4101da177e4SLinus Torvalds 	 * in some unrelated directory, so try with d_lookup
4111da177e4SLinus Torvalds 	 */
4121da177e4SLinus Torvalds 	if (!dentry)
4131da177e4SLinus Torvalds 		dentry = d_lookup(parent, name);
4141da177e4SLinus Torvalds 
415bcdc5e01SIan Kent 	if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
416bcdc5e01SIan Kent 		dentry = do_revalidate(dentry, nd);
417bcdc5e01SIan Kent 
4181da177e4SLinus Torvalds 	return dentry;
4191da177e4SLinus Torvalds }
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds /*
4221da177e4SLinus Torvalds  * Short-cut version of permission(), for calling by
4231da177e4SLinus Torvalds  * path_walk(), when dcache lock is held.  Combines parts
4241da177e4SLinus Torvalds  * of permission() and generic_permission(), and tests ONLY for
4251da177e4SLinus Torvalds  * MAY_EXEC permission.
4261da177e4SLinus Torvalds  *
4271da177e4SLinus Torvalds  * If appropriate, check DAC only.  If not appropriate, or
4281da177e4SLinus Torvalds  * short-cut DAC fails, then call permission() to do more
4291da177e4SLinus Torvalds  * complete permission check.
4301da177e4SLinus Torvalds  */
431672b16b2SAl Viro static int exec_permission_lite(struct inode *inode)
4321da177e4SLinus Torvalds {
4331da177e4SLinus Torvalds 	umode_t	mode = inode->i_mode;
4341da177e4SLinus Torvalds 
435acfa4380SAl Viro 	if (inode->i_op->permission)
4361da177e4SLinus Torvalds 		return -EAGAIN;
4371da177e4SLinus Torvalds 
438da9592edSDavid Howells 	if (current_fsuid() == inode->i_uid)
4391da177e4SLinus Torvalds 		mode >>= 6;
4401da177e4SLinus Torvalds 	else if (in_group_p(inode->i_gid))
4411da177e4SLinus Torvalds 		mode >>= 3;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	if (mode & MAY_EXEC)
4441da177e4SLinus Torvalds 		goto ok;
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
4471da177e4SLinus Torvalds 		goto ok;
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
4501da177e4SLinus Torvalds 		goto ok;
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
4531da177e4SLinus Torvalds 		goto ok;
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	return -EACCES;
4561da177e4SLinus Torvalds ok:
457b77b0646SAl Viro 	return security_inode_permission(inode, MAY_EXEC);
4581da177e4SLinus Torvalds }
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds /*
4611da177e4SLinus Torvalds  * This is called when everything else fails, and we actually have
4621da177e4SLinus Torvalds  * to go to the low-level filesystem to find out what we should do..
4631da177e4SLinus Torvalds  *
4641da177e4SLinus Torvalds  * We get the directory semaphore, and after getting that we also
4651da177e4SLinus Torvalds  * make sure that nobody added the entry to the dcache in the meantime..
4661da177e4SLinus Torvalds  * SMP-safe
4671da177e4SLinus Torvalds  */
4681da177e4SLinus Torvalds static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
4691da177e4SLinus Torvalds {
4701da177e4SLinus Torvalds 	struct dentry * result;
4711da177e4SLinus Torvalds 	struct inode *dir = parent->d_inode;
4721da177e4SLinus Torvalds 
4731b1dcc1bSJes Sorensen 	mutex_lock(&dir->i_mutex);
4741da177e4SLinus Torvalds 	/*
4751da177e4SLinus Torvalds 	 * First re-do the cached lookup just in case it was created
4761da177e4SLinus Torvalds 	 * while we waited for the directory semaphore..
4771da177e4SLinus Torvalds 	 *
4781da177e4SLinus Torvalds 	 * FIXME! This could use version numbering or similar to
4791da177e4SLinus Torvalds 	 * avoid unnecessary cache lookups.
4801da177e4SLinus Torvalds 	 *
4811da177e4SLinus Torvalds 	 * The "dcache_lock" is purely to protect the RCU list walker
4821da177e4SLinus Torvalds 	 * from concurrent renames at this point (we mustn't get false
4831da177e4SLinus Torvalds 	 * negatives from the RCU list walk here, unlike the optimistic
4841da177e4SLinus Torvalds 	 * fast walk).
4851da177e4SLinus Torvalds 	 *
4861da177e4SLinus Torvalds 	 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
4871da177e4SLinus Torvalds 	 */
4881da177e4SLinus Torvalds 	result = d_lookup(parent, name);
4891da177e4SLinus Torvalds 	if (!result) {
490d70b67c8SMiklos Szeredi 		struct dentry *dentry;
491d70b67c8SMiklos Szeredi 
492d70b67c8SMiklos Szeredi 		/* Don't create child dentry for a dead directory. */
493d70b67c8SMiklos Szeredi 		result = ERR_PTR(-ENOENT);
494d70b67c8SMiklos Szeredi 		if (IS_DEADDIR(dir))
495d70b67c8SMiklos Szeredi 			goto out_unlock;
496d70b67c8SMiklos Szeredi 
497d70b67c8SMiklos Szeredi 		dentry = d_alloc(parent, name);
4981da177e4SLinus Torvalds 		result = ERR_PTR(-ENOMEM);
4991da177e4SLinus Torvalds 		if (dentry) {
5001da177e4SLinus Torvalds 			result = dir->i_op->lookup(dir, dentry, nd);
5011da177e4SLinus Torvalds 			if (result)
5021da177e4SLinus Torvalds 				dput(dentry);
5031da177e4SLinus Torvalds 			else
5041da177e4SLinus Torvalds 				result = dentry;
5051da177e4SLinus Torvalds 		}
506d70b67c8SMiklos Szeredi out_unlock:
5071b1dcc1bSJes Sorensen 		mutex_unlock(&dir->i_mutex);
5081da177e4SLinus Torvalds 		return result;
5091da177e4SLinus Torvalds 	}
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	/*
5121da177e4SLinus Torvalds 	 * Uhhuh! Nasty case: the cache was re-populated while
5131da177e4SLinus Torvalds 	 * we waited on the semaphore. Need to revalidate.
5141da177e4SLinus Torvalds 	 */
5151b1dcc1bSJes Sorensen 	mutex_unlock(&dir->i_mutex);
5161da177e4SLinus Torvalds 	if (result->d_op && result->d_op->d_revalidate) {
517bcdc5e01SIan Kent 		result = do_revalidate(result, nd);
518bcdc5e01SIan Kent 		if (!result)
5191da177e4SLinus Torvalds 			result = ERR_PTR(-ENOENT);
5201da177e4SLinus Torvalds 	}
5211da177e4SLinus Torvalds 	return result;
5221da177e4SLinus Torvalds }
5231da177e4SLinus Torvalds 
524a02f76c3SAl Viro /*
525a02f76c3SAl Viro  * Wrapper to retry pathname resolution whenever the underlying
526a02f76c3SAl Viro  * file system returns an ESTALE.
527a02f76c3SAl Viro  *
528a02f76c3SAl Viro  * Retry the whole path once, forcing real lookup requests
529a02f76c3SAl Viro  * instead of relying on the dcache.
530a02f76c3SAl Viro  */
531a02f76c3SAl Viro static __always_inline int link_path_walk(const char *name, struct nameidata *nd)
532a02f76c3SAl Viro {
533a02f76c3SAl Viro 	struct path save = nd->path;
534a02f76c3SAl Viro 	int result;
535a02f76c3SAl Viro 
536a02f76c3SAl Viro 	/* make sure the stuff we saved doesn't go away */
537c8e7f449SJan Blunck 	path_get(&save);
538a02f76c3SAl Viro 
539a02f76c3SAl Viro 	result = __link_path_walk(name, nd);
540a02f76c3SAl Viro 	if (result == -ESTALE) {
541a02f76c3SAl Viro 		/* nd->path had been dropped */
542a02f76c3SAl Viro 		nd->path = save;
543c8e7f449SJan Blunck 		path_get(&nd->path);
544a02f76c3SAl Viro 		nd->flags |= LOOKUP_REVAL;
545a02f76c3SAl Viro 		result = __link_path_walk(name, nd);
546a02f76c3SAl Viro 	}
547a02f76c3SAl Viro 
548a02f76c3SAl Viro 	path_put(&save);
549a02f76c3SAl Viro 
550a02f76c3SAl Viro 	return result;
551a02f76c3SAl Viro }
552a02f76c3SAl Viro 
553f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
5541da177e4SLinus Torvalds {
5551da177e4SLinus Torvalds 	int res = 0;
5561da177e4SLinus Torvalds 	char *name;
5571da177e4SLinus Torvalds 	if (IS_ERR(link))
5581da177e4SLinus Torvalds 		goto fail;
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	if (*link == '/') {
561b4091d5fSChristoph Hellwig 		struct fs_struct *fs = current->fs;
562b4091d5fSChristoph Hellwig 
5631d957f9bSJan Blunck 		path_put(&nd->path);
564b4091d5fSChristoph Hellwig 
565b4091d5fSChristoph Hellwig 		read_lock(&fs->lock);
566b4091d5fSChristoph Hellwig 		nd->path = fs->root;
567b4091d5fSChristoph Hellwig 		path_get(&fs->root);
568b4091d5fSChristoph Hellwig 		read_unlock(&fs->lock);
5691da177e4SLinus Torvalds 	}
570b4091d5fSChristoph Hellwig 
5711da177e4SLinus Torvalds 	res = link_path_walk(link, nd);
5721da177e4SLinus Torvalds 	if (nd->depth || res || nd->last_type!=LAST_NORM)
5731da177e4SLinus Torvalds 		return res;
5741da177e4SLinus Torvalds 	/*
5751da177e4SLinus Torvalds 	 * If it is an iterative symlinks resolution in open_namei() we
5761da177e4SLinus Torvalds 	 * have to copy the last component. And all that crap because of
5771da177e4SLinus Torvalds 	 * bloody create() on broken symlinks. Furrfu...
5781da177e4SLinus Torvalds 	 */
5791da177e4SLinus Torvalds 	name = __getname();
5801da177e4SLinus Torvalds 	if (unlikely(!name)) {
5811d957f9bSJan Blunck 		path_put(&nd->path);
5821da177e4SLinus Torvalds 		return -ENOMEM;
5831da177e4SLinus Torvalds 	}
5841da177e4SLinus Torvalds 	strcpy(name, nd->last.name);
5851da177e4SLinus Torvalds 	nd->last.name = name;
5861da177e4SLinus Torvalds 	return 0;
5871da177e4SLinus Torvalds fail:
5881d957f9bSJan Blunck 	path_put(&nd->path);
5891da177e4SLinus Torvalds 	return PTR_ERR(link);
5901da177e4SLinus Torvalds }
5911da177e4SLinus Torvalds 
5921d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd)
593051d3812SIan Kent {
594051d3812SIan Kent 	dput(path->dentry);
5954ac91378SJan Blunck 	if (path->mnt != nd->path.mnt)
596051d3812SIan Kent 		mntput(path->mnt);
597051d3812SIan Kent }
598051d3812SIan Kent 
599051d3812SIan Kent static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
600051d3812SIan Kent {
6014ac91378SJan Blunck 	dput(nd->path.dentry);
6024ac91378SJan Blunck 	if (nd->path.mnt != path->mnt)
6034ac91378SJan Blunck 		mntput(nd->path.mnt);
6044ac91378SJan Blunck 	nd->path.mnt = path->mnt;
6054ac91378SJan Blunck 	nd->path.dentry = path->dentry;
606051d3812SIan Kent }
607051d3812SIan Kent 
608f1662356SArjan van de Ven static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
6091da177e4SLinus Torvalds {
6101da177e4SLinus Torvalds 	int error;
611cc314eefSLinus Torvalds 	void *cookie;
612cd4e91d3SAl Viro 	struct dentry *dentry = path->dentry;
6131da177e4SLinus Torvalds 
614d671a1cbSAl Viro 	touch_atime(path->mnt, dentry);
6151da177e4SLinus Torvalds 	nd_set_link(nd, NULL);
616cd4e91d3SAl Viro 
6174ac91378SJan Blunck 	if (path->mnt != nd->path.mnt) {
618051d3812SIan Kent 		path_to_nameidata(path, nd);
619051d3812SIan Kent 		dget(dentry);
620051d3812SIan Kent 	}
621cd4e91d3SAl Viro 	mntget(path->mnt);
622cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
623cc314eefSLinus Torvalds 	error = PTR_ERR(cookie);
624cc314eefSLinus Torvalds 	if (!IS_ERR(cookie)) {
6251da177e4SLinus Torvalds 		char *s = nd_get_link(nd);
626cc314eefSLinus Torvalds 		error = 0;
6271da177e4SLinus Torvalds 		if (s)
6281da177e4SLinus Torvalds 			error = __vfs_follow_link(nd, s);
6291da177e4SLinus Torvalds 		if (dentry->d_inode->i_op->put_link)
630cc314eefSLinus Torvalds 			dentry->d_inode->i_op->put_link(dentry, nd, cookie);
6311da177e4SLinus Torvalds 	}
63209da5916SJan Blunck 	path_put(path);
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	return error;
6351da177e4SLinus Torvalds }
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds /*
6381da177e4SLinus Torvalds  * This limits recursive symlink follows to 8, while
6391da177e4SLinus Torvalds  * limiting consecutive symlinks to 40.
6401da177e4SLinus Torvalds  *
6411da177e4SLinus Torvalds  * Without that kind of total limit, nasty chains of consecutive
6421da177e4SLinus Torvalds  * symlinks can cause almost arbitrarily long lookups.
6431da177e4SLinus Torvalds  */
64490ebe565SAl Viro static inline int do_follow_link(struct path *path, struct nameidata *nd)
6451da177e4SLinus Torvalds {
6461da177e4SLinus Torvalds 	int err = -ELOOP;
6471da177e4SLinus Torvalds 	if (current->link_count >= MAX_NESTED_LINKS)
6481da177e4SLinus Torvalds 		goto loop;
6491da177e4SLinus Torvalds 	if (current->total_link_count >= 40)
6501da177e4SLinus Torvalds 		goto loop;
6511da177e4SLinus Torvalds 	BUG_ON(nd->depth >= MAX_NESTED_LINKS);
6521da177e4SLinus Torvalds 	cond_resched();
65390ebe565SAl Viro 	err = security_inode_follow_link(path->dentry, nd);
6541da177e4SLinus Torvalds 	if (err)
6551da177e4SLinus Torvalds 		goto loop;
6561da177e4SLinus Torvalds 	current->link_count++;
6571da177e4SLinus Torvalds 	current->total_link_count++;
6581da177e4SLinus Torvalds 	nd->depth++;
659cd4e91d3SAl Viro 	err = __do_follow_link(path, nd);
6601da177e4SLinus Torvalds 	current->link_count--;
6611da177e4SLinus Torvalds 	nd->depth--;
6621da177e4SLinus Torvalds 	return err;
6631da177e4SLinus Torvalds loop:
6641d957f9bSJan Blunck 	path_put_conditional(path, nd);
6651d957f9bSJan Blunck 	path_put(&nd->path);
6661da177e4SLinus Torvalds 	return err;
6671da177e4SLinus Torvalds }
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds int follow_up(struct vfsmount **mnt, struct dentry **dentry)
6701da177e4SLinus Torvalds {
6711da177e4SLinus Torvalds 	struct vfsmount *parent;
6721da177e4SLinus Torvalds 	struct dentry *mountpoint;
6731da177e4SLinus Torvalds 	spin_lock(&vfsmount_lock);
6741da177e4SLinus Torvalds 	parent=(*mnt)->mnt_parent;
6751da177e4SLinus Torvalds 	if (parent == *mnt) {
6761da177e4SLinus Torvalds 		spin_unlock(&vfsmount_lock);
6771da177e4SLinus Torvalds 		return 0;
6781da177e4SLinus Torvalds 	}
6791da177e4SLinus Torvalds 	mntget(parent);
6801da177e4SLinus Torvalds 	mountpoint=dget((*mnt)->mnt_mountpoint);
6811da177e4SLinus Torvalds 	spin_unlock(&vfsmount_lock);
6821da177e4SLinus Torvalds 	dput(*dentry);
6831da177e4SLinus Torvalds 	*dentry = mountpoint;
6841da177e4SLinus Torvalds 	mntput(*mnt);
6851da177e4SLinus Torvalds 	*mnt = parent;
6861da177e4SLinus Torvalds 	return 1;
6871da177e4SLinus Torvalds }
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds /* no need for dcache_lock, as serialization is taken care in
6901da177e4SLinus Torvalds  * namespace.c
6911da177e4SLinus Torvalds  */
692463ffb2eSAl Viro static int __follow_mount(struct path *path)
693463ffb2eSAl Viro {
694463ffb2eSAl Viro 	int res = 0;
695463ffb2eSAl Viro 	while (d_mountpoint(path->dentry)) {
696463ffb2eSAl Viro 		struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
697463ffb2eSAl Viro 		if (!mounted)
698463ffb2eSAl Viro 			break;
699463ffb2eSAl Viro 		dput(path->dentry);
700463ffb2eSAl Viro 		if (res)
701463ffb2eSAl Viro 			mntput(path->mnt);
702463ffb2eSAl Viro 		path->mnt = mounted;
703463ffb2eSAl Viro 		path->dentry = dget(mounted->mnt_root);
704463ffb2eSAl Viro 		res = 1;
705463ffb2eSAl Viro 	}
706463ffb2eSAl Viro 	return res;
707463ffb2eSAl Viro }
708463ffb2eSAl Viro 
70958c465ebSAl Viro static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
7101da177e4SLinus Torvalds {
7111da177e4SLinus Torvalds 	while (d_mountpoint(*dentry)) {
7121da177e4SLinus Torvalds 		struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
7131da177e4SLinus Torvalds 		if (!mounted)
7141da177e4SLinus Torvalds 			break;
71558c465ebSAl Viro 		dput(*dentry);
7161da177e4SLinus Torvalds 		mntput(*mnt);
7171da177e4SLinus Torvalds 		*mnt = mounted;
7181da177e4SLinus Torvalds 		*dentry = dget(mounted->mnt_root);
7191da177e4SLinus Torvalds 	}
7201da177e4SLinus Torvalds }
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds /* no need for dcache_lock, as serialization is taken care in
7231da177e4SLinus Torvalds  * namespace.c
7241da177e4SLinus Torvalds  */
725e13b210fSAl Viro int follow_down(struct vfsmount **mnt, struct dentry **dentry)
7261da177e4SLinus Torvalds {
7271da177e4SLinus Torvalds 	struct vfsmount *mounted;
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 	mounted = lookup_mnt(*mnt, *dentry);
7301da177e4SLinus Torvalds 	if (mounted) {
731e13b210fSAl Viro 		dput(*dentry);
7321da177e4SLinus Torvalds 		mntput(*mnt);
7331da177e4SLinus Torvalds 		*mnt = mounted;
7341da177e4SLinus Torvalds 		*dentry = dget(mounted->mnt_root);
7351da177e4SLinus Torvalds 		return 1;
7361da177e4SLinus Torvalds 	}
7371da177e4SLinus Torvalds 	return 0;
7381da177e4SLinus Torvalds }
7391da177e4SLinus Torvalds 
740f1662356SArjan van de Ven static __always_inline void follow_dotdot(struct nameidata *nd)
7411da177e4SLinus Torvalds {
742e518ddb7SAndreas Mohr 	struct fs_struct *fs = current->fs;
743e518ddb7SAndreas Mohr 
7441da177e4SLinus Torvalds 	while(1) {
7451da177e4SLinus Torvalds 		struct vfsmount *parent;
7464ac91378SJan Blunck 		struct dentry *old = nd->path.dentry;
7471da177e4SLinus Torvalds 
748e518ddb7SAndreas Mohr                 read_lock(&fs->lock);
7496ac08c39SJan Blunck 		if (nd->path.dentry == fs->root.dentry &&
7506ac08c39SJan Blunck 		    nd->path.mnt == fs->root.mnt) {
751e518ddb7SAndreas Mohr                         read_unlock(&fs->lock);
7521da177e4SLinus Torvalds 			break;
7531da177e4SLinus Torvalds 		}
754e518ddb7SAndreas Mohr                 read_unlock(&fs->lock);
7551da177e4SLinus Torvalds 		spin_lock(&dcache_lock);
7564ac91378SJan Blunck 		if (nd->path.dentry != nd->path.mnt->mnt_root) {
7574ac91378SJan Blunck 			nd->path.dentry = dget(nd->path.dentry->d_parent);
7581da177e4SLinus Torvalds 			spin_unlock(&dcache_lock);
7591da177e4SLinus Torvalds 			dput(old);
7601da177e4SLinus Torvalds 			break;
7611da177e4SLinus Torvalds 		}
7621da177e4SLinus Torvalds 		spin_unlock(&dcache_lock);
7631da177e4SLinus Torvalds 		spin_lock(&vfsmount_lock);
7644ac91378SJan Blunck 		parent = nd->path.mnt->mnt_parent;
7654ac91378SJan Blunck 		if (parent == nd->path.mnt) {
7661da177e4SLinus Torvalds 			spin_unlock(&vfsmount_lock);
7671da177e4SLinus Torvalds 			break;
7681da177e4SLinus Torvalds 		}
7691da177e4SLinus Torvalds 		mntget(parent);
7704ac91378SJan Blunck 		nd->path.dentry = dget(nd->path.mnt->mnt_mountpoint);
7711da177e4SLinus Torvalds 		spin_unlock(&vfsmount_lock);
7721da177e4SLinus Torvalds 		dput(old);
7734ac91378SJan Blunck 		mntput(nd->path.mnt);
7744ac91378SJan Blunck 		nd->path.mnt = parent;
7751da177e4SLinus Torvalds 	}
7764ac91378SJan Blunck 	follow_mount(&nd->path.mnt, &nd->path.dentry);
7771da177e4SLinus Torvalds }
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds /*
7801da177e4SLinus Torvalds  *  It's more convoluted than I'd like it to be, but... it's still fairly
7811da177e4SLinus Torvalds  *  small and for now I'd prefer to have fast path as straight as possible.
7821da177e4SLinus Torvalds  *  It _is_ time-critical.
7831da177e4SLinus Torvalds  */
7841da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name,
7851da177e4SLinus Torvalds 		     struct path *path)
7861da177e4SLinus Torvalds {
7874ac91378SJan Blunck 	struct vfsmount *mnt = nd->path.mnt;
7884ac91378SJan Blunck 	struct dentry *dentry = __d_lookup(nd->path.dentry, name);
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	if (!dentry)
7911da177e4SLinus Torvalds 		goto need_lookup;
7921da177e4SLinus Torvalds 	if (dentry->d_op && dentry->d_op->d_revalidate)
7931da177e4SLinus Torvalds 		goto need_revalidate;
7941da177e4SLinus Torvalds done:
7951da177e4SLinus Torvalds 	path->mnt = mnt;
7961da177e4SLinus Torvalds 	path->dentry = dentry;
797634ee701SAl Viro 	__follow_mount(path);
7981da177e4SLinus Torvalds 	return 0;
7991da177e4SLinus Torvalds 
8001da177e4SLinus Torvalds need_lookup:
8014ac91378SJan Blunck 	dentry = real_lookup(nd->path.dentry, name, nd);
8021da177e4SLinus Torvalds 	if (IS_ERR(dentry))
8031da177e4SLinus Torvalds 		goto fail;
8041da177e4SLinus Torvalds 	goto done;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds need_revalidate:
807bcdc5e01SIan Kent 	dentry = do_revalidate(dentry, nd);
808bcdc5e01SIan Kent 	if (!dentry)
8091da177e4SLinus Torvalds 		goto need_lookup;
810bcdc5e01SIan Kent 	if (IS_ERR(dentry))
811bcdc5e01SIan Kent 		goto fail;
812bcdc5e01SIan Kent 	goto done;
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds fail:
8151da177e4SLinus Torvalds 	return PTR_ERR(dentry);
8161da177e4SLinus Torvalds }
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds /*
8191da177e4SLinus Torvalds  * Name resolution.
820ea3834d9SPrasanna Meda  * This is the basic name resolution function, turning a pathname into
821ea3834d9SPrasanna Meda  * the final dentry. We expect 'base' to be positive and a directory.
8221da177e4SLinus Torvalds  *
823ea3834d9SPrasanna Meda  * Returns 0 and nd will have valid dentry and mnt on success.
824ea3834d9SPrasanna Meda  * Returns error and drops reference to input namei data on failure.
8251da177e4SLinus Torvalds  */
826fc9b52cdSHarvey Harrison static int __link_path_walk(const char *name, struct nameidata *nd)
8271da177e4SLinus Torvalds {
8281da177e4SLinus Torvalds 	struct path next;
8291da177e4SLinus Torvalds 	struct inode *inode;
8301da177e4SLinus Torvalds 	int err;
8311da177e4SLinus Torvalds 	unsigned int lookup_flags = nd->flags;
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	while (*name=='/')
8341da177e4SLinus Torvalds 		name++;
8351da177e4SLinus Torvalds 	if (!*name)
8361da177e4SLinus Torvalds 		goto return_reval;
8371da177e4SLinus Torvalds 
8384ac91378SJan Blunck 	inode = nd->path.dentry->d_inode;
8391da177e4SLinus Torvalds 	if (nd->depth)
840f55eab82STrond Myklebust 		lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	/* At this point we know we have a real path component. */
8431da177e4SLinus Torvalds 	for(;;) {
8441da177e4SLinus Torvalds 		unsigned long hash;
8451da177e4SLinus Torvalds 		struct qstr this;
8461da177e4SLinus Torvalds 		unsigned int c;
8471da177e4SLinus Torvalds 
848cdce5d6bSTrond Myklebust 		nd->flags |= LOOKUP_CONTINUE;
849672b16b2SAl Viro 		err = exec_permission_lite(inode);
850e4543eddSChristoph Hellwig 		if (err == -EAGAIN)
851cb23beb5SChristoph Hellwig 			err = inode_permission(nd->path.dentry->d_inode,
852cb23beb5SChristoph Hellwig 					       MAY_EXEC);
8531da177e4SLinus Torvalds  		if (err)
8541da177e4SLinus Torvalds 			break;
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds 		this.name = name;
8571da177e4SLinus Torvalds 		c = *(const unsigned char *)name;
8581da177e4SLinus Torvalds 
8591da177e4SLinus Torvalds 		hash = init_name_hash();
8601da177e4SLinus Torvalds 		do {
8611da177e4SLinus Torvalds 			name++;
8621da177e4SLinus Torvalds 			hash = partial_name_hash(c, hash);
8631da177e4SLinus Torvalds 			c = *(const unsigned char *)name;
8641da177e4SLinus Torvalds 		} while (c && (c != '/'));
8651da177e4SLinus Torvalds 		this.len = name - (const char *) this.name;
8661da177e4SLinus Torvalds 		this.hash = end_name_hash(hash);
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 		/* remove trailing slashes? */
8691da177e4SLinus Torvalds 		if (!c)
8701da177e4SLinus Torvalds 			goto last_component;
8711da177e4SLinus Torvalds 		while (*++name == '/');
8721da177e4SLinus Torvalds 		if (!*name)
8731da177e4SLinus Torvalds 			goto last_with_slashes;
8741da177e4SLinus Torvalds 
8751da177e4SLinus Torvalds 		/*
8761da177e4SLinus Torvalds 		 * "." and ".." are special - ".." especially so because it has
8771da177e4SLinus Torvalds 		 * to be able to know about the current root directory and
8781da177e4SLinus Torvalds 		 * parent relationships.
8791da177e4SLinus Torvalds 		 */
8801da177e4SLinus Torvalds 		if (this.name[0] == '.') switch (this.len) {
8811da177e4SLinus Torvalds 			default:
8821da177e4SLinus Torvalds 				break;
8831da177e4SLinus Torvalds 			case 2:
8841da177e4SLinus Torvalds 				if (this.name[1] != '.')
8851da177e4SLinus Torvalds 					break;
88658c465ebSAl Viro 				follow_dotdot(nd);
8874ac91378SJan Blunck 				inode = nd->path.dentry->d_inode;
8881da177e4SLinus Torvalds 				/* fallthrough */
8891da177e4SLinus Torvalds 			case 1:
8901da177e4SLinus Torvalds 				continue;
8911da177e4SLinus Torvalds 		}
8921da177e4SLinus Torvalds 		/*
8931da177e4SLinus Torvalds 		 * See if the low-level filesystem might want
8941da177e4SLinus Torvalds 		 * to use its own hash..
8951da177e4SLinus Torvalds 		 */
8964ac91378SJan Blunck 		if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
8974ac91378SJan Blunck 			err = nd->path.dentry->d_op->d_hash(nd->path.dentry,
8984ac91378SJan Blunck 							    &this);
8991da177e4SLinus Torvalds 			if (err < 0)
9001da177e4SLinus Torvalds 				break;
9011da177e4SLinus Torvalds 		}
9021da177e4SLinus Torvalds 		/* This does the actual lookups.. */
9031da177e4SLinus Torvalds 		err = do_lookup(nd, &this, &next);
9041da177e4SLinus Torvalds 		if (err)
9051da177e4SLinus Torvalds 			break;
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 		err = -ENOENT;
9081da177e4SLinus Torvalds 		inode = next.dentry->d_inode;
9091da177e4SLinus Torvalds 		if (!inode)
9101da177e4SLinus Torvalds 			goto out_dput;
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 		if (inode->i_op->follow_link) {
91390ebe565SAl Viro 			err = do_follow_link(&next, nd);
9141da177e4SLinus Torvalds 			if (err)
9151da177e4SLinus Torvalds 				goto return_err;
9161da177e4SLinus Torvalds 			err = -ENOENT;
9174ac91378SJan Blunck 			inode = nd->path.dentry->d_inode;
9181da177e4SLinus Torvalds 			if (!inode)
9191da177e4SLinus Torvalds 				break;
92009dd17d3SMiklos Szeredi 		} else
92109dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
9221da177e4SLinus Torvalds 		err = -ENOTDIR;
9231da177e4SLinus Torvalds 		if (!inode->i_op->lookup)
9241da177e4SLinus Torvalds 			break;
9251da177e4SLinus Torvalds 		continue;
9261da177e4SLinus Torvalds 		/* here ends the main loop */
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds last_with_slashes:
9291da177e4SLinus Torvalds 		lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
9301da177e4SLinus Torvalds last_component:
931f55eab82STrond Myklebust 		/* Clear LOOKUP_CONTINUE iff it was previously unset */
932f55eab82STrond Myklebust 		nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
9331da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_PARENT)
9341da177e4SLinus Torvalds 			goto lookup_parent;
9351da177e4SLinus Torvalds 		if (this.name[0] == '.') switch (this.len) {
9361da177e4SLinus Torvalds 			default:
9371da177e4SLinus Torvalds 				break;
9381da177e4SLinus Torvalds 			case 2:
9391da177e4SLinus Torvalds 				if (this.name[1] != '.')
9401da177e4SLinus Torvalds 					break;
94158c465ebSAl Viro 				follow_dotdot(nd);
9424ac91378SJan Blunck 				inode = nd->path.dentry->d_inode;
9431da177e4SLinus Torvalds 				/* fallthrough */
9441da177e4SLinus Torvalds 			case 1:
9451da177e4SLinus Torvalds 				goto return_reval;
9461da177e4SLinus Torvalds 		}
9474ac91378SJan Blunck 		if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
9484ac91378SJan Blunck 			err = nd->path.dentry->d_op->d_hash(nd->path.dentry,
9494ac91378SJan Blunck 							    &this);
9501da177e4SLinus Torvalds 			if (err < 0)
9511da177e4SLinus Torvalds 				break;
9521da177e4SLinus Torvalds 		}
9531da177e4SLinus Torvalds 		err = do_lookup(nd, &this, &next);
9541da177e4SLinus Torvalds 		if (err)
9551da177e4SLinus Torvalds 			break;
9561da177e4SLinus Torvalds 		inode = next.dentry->d_inode;
9571da177e4SLinus Torvalds 		if ((lookup_flags & LOOKUP_FOLLOW)
958acfa4380SAl Viro 		    && inode && inode->i_op->follow_link) {
95990ebe565SAl Viro 			err = do_follow_link(&next, nd);
9601da177e4SLinus Torvalds 			if (err)
9611da177e4SLinus Torvalds 				goto return_err;
9624ac91378SJan Blunck 			inode = nd->path.dentry->d_inode;
96309dd17d3SMiklos Szeredi 		} else
96409dd17d3SMiklos Szeredi 			path_to_nameidata(&next, nd);
9651da177e4SLinus Torvalds 		err = -ENOENT;
9661da177e4SLinus Torvalds 		if (!inode)
9671da177e4SLinus Torvalds 			break;
9681da177e4SLinus Torvalds 		if (lookup_flags & LOOKUP_DIRECTORY) {
9691da177e4SLinus Torvalds 			err = -ENOTDIR;
970acfa4380SAl Viro 			if (!inode->i_op->lookup)
9711da177e4SLinus Torvalds 				break;
9721da177e4SLinus Torvalds 		}
9731da177e4SLinus Torvalds 		goto return_base;
9741da177e4SLinus Torvalds lookup_parent:
9751da177e4SLinus Torvalds 		nd->last = this;
9761da177e4SLinus Torvalds 		nd->last_type = LAST_NORM;
9771da177e4SLinus Torvalds 		if (this.name[0] != '.')
9781da177e4SLinus Torvalds 			goto return_base;
9791da177e4SLinus Torvalds 		if (this.len == 1)
9801da177e4SLinus Torvalds 			nd->last_type = LAST_DOT;
9811da177e4SLinus Torvalds 		else if (this.len == 2 && this.name[1] == '.')
9821da177e4SLinus Torvalds 			nd->last_type = LAST_DOTDOT;
9831da177e4SLinus Torvalds 		else
9841da177e4SLinus Torvalds 			goto return_base;
9851da177e4SLinus Torvalds return_reval:
9861da177e4SLinus Torvalds 		/*
9871da177e4SLinus Torvalds 		 * We bypassed the ordinary revalidation routines.
9881da177e4SLinus Torvalds 		 * We may need to check the cached dentry for staleness.
9891da177e4SLinus Torvalds 		 */
9904ac91378SJan Blunck 		if (nd->path.dentry && nd->path.dentry->d_sb &&
9914ac91378SJan Blunck 		    (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
9921da177e4SLinus Torvalds 			err = -ESTALE;
9931da177e4SLinus Torvalds 			/* Note: we do not d_invalidate() */
9944ac91378SJan Blunck 			if (!nd->path.dentry->d_op->d_revalidate(
9954ac91378SJan Blunck 					nd->path.dentry, nd))
9961da177e4SLinus Torvalds 				break;
9971da177e4SLinus Torvalds 		}
9981da177e4SLinus Torvalds return_base:
9991da177e4SLinus Torvalds 		return 0;
10001da177e4SLinus Torvalds out_dput:
10011d957f9bSJan Blunck 		path_put_conditional(&next, nd);
10021da177e4SLinus Torvalds 		break;
10031da177e4SLinus Torvalds 	}
10041d957f9bSJan Blunck 	path_put(&nd->path);
10051da177e4SLinus Torvalds return_err:
10061da177e4SLinus Torvalds 	return err;
10071da177e4SLinus Torvalds }
10081da177e4SLinus Torvalds 
1009fc9b52cdSHarvey Harrison static int path_walk(const char *name, struct nameidata *nd)
10101da177e4SLinus Torvalds {
10111da177e4SLinus Torvalds 	current->total_link_count = 0;
10121da177e4SLinus Torvalds 	return link_path_walk(name, nd);
10131da177e4SLinus Torvalds }
10141da177e4SLinus Torvalds 
1015ea3834d9SPrasanna Meda /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
1016fc9b52cdSHarvey Harrison static int do_path_lookup(int dfd, const char *name,
10175590ff0dSUlrich Drepper 				unsigned int flags, struct nameidata *nd)
10181da177e4SLinus Torvalds {
1019ea3834d9SPrasanna Meda 	int retval = 0;
1020170aa3d0SUlrich Drepper 	int fput_needed;
1021170aa3d0SUlrich Drepper 	struct file *file;
1022e518ddb7SAndreas Mohr 	struct fs_struct *fs = current->fs;
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
10251da177e4SLinus Torvalds 	nd->flags = flags;
10261da177e4SLinus Torvalds 	nd->depth = 0;
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds 	if (*name=='/') {
1029e518ddb7SAndreas Mohr 		read_lock(&fs->lock);
10306ac08c39SJan Blunck 		nd->path = fs->root;
10316ac08c39SJan Blunck 		path_get(&fs->root);
1032e518ddb7SAndreas Mohr 		read_unlock(&fs->lock);
10335590ff0dSUlrich Drepper 	} else if (dfd == AT_FDCWD) {
1034e518ddb7SAndreas Mohr 		read_lock(&fs->lock);
10356ac08c39SJan Blunck 		nd->path = fs->pwd;
10366ac08c39SJan Blunck 		path_get(&fs->pwd);
1037e518ddb7SAndreas Mohr 		read_unlock(&fs->lock);
10385590ff0dSUlrich Drepper 	} else {
10395590ff0dSUlrich Drepper 		struct dentry *dentry;
10405590ff0dSUlrich Drepper 
10415590ff0dSUlrich Drepper 		file = fget_light(dfd, &fput_needed);
10425590ff0dSUlrich Drepper 		retval = -EBADF;
1043170aa3d0SUlrich Drepper 		if (!file)
10446d09bb62STrond Myklebust 			goto out_fail;
10455590ff0dSUlrich Drepper 
10460f7fc9e4SJosef "Jeff" Sipek 		dentry = file->f_path.dentry;
10475590ff0dSUlrich Drepper 
10485590ff0dSUlrich Drepper 		retval = -ENOTDIR;
1049170aa3d0SUlrich Drepper 		if (!S_ISDIR(dentry->d_inode->i_mode))
10506d09bb62STrond Myklebust 			goto fput_fail;
10515590ff0dSUlrich Drepper 
10525590ff0dSUlrich Drepper 		retval = file_permission(file, MAY_EXEC);
1053170aa3d0SUlrich Drepper 		if (retval)
10546d09bb62STrond Myklebust 			goto fput_fail;
10555590ff0dSUlrich Drepper 
10565dd784d0SJan Blunck 		nd->path = file->f_path;
10575dd784d0SJan Blunck 		path_get(&file->f_path);
10585590ff0dSUlrich Drepper 
10595590ff0dSUlrich Drepper 		fput_light(file, fput_needed);
10601da177e4SLinus Torvalds 	}
10612dfdd266SJosef 'Jeff' Sipek 
10622dfdd266SJosef 'Jeff' Sipek 	retval = path_walk(name, nd);
10634ac91378SJan Blunck 	if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
10644ac91378SJan Blunck 				nd->path.dentry->d_inode))
10654ac91378SJan Blunck 		audit_inode(name, nd->path.dentry);
10666d09bb62STrond Myklebust out_fail:
1067170aa3d0SUlrich Drepper 	return retval;
1068170aa3d0SUlrich Drepper 
10696d09bb62STrond Myklebust fput_fail:
1070170aa3d0SUlrich Drepper 	fput_light(file, fput_needed);
10716d09bb62STrond Myklebust 	goto out_fail;
10721da177e4SLinus Torvalds }
10731da177e4SLinus Torvalds 
1074fc9b52cdSHarvey Harrison int path_lookup(const char *name, unsigned int flags,
10755590ff0dSUlrich Drepper 			struct nameidata *nd)
10765590ff0dSUlrich Drepper {
10775590ff0dSUlrich Drepper 	return do_path_lookup(AT_FDCWD, name, flags, nd);
10785590ff0dSUlrich Drepper }
10795590ff0dSUlrich Drepper 
1080d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path)
1081d1811465SAl Viro {
1082d1811465SAl Viro 	struct nameidata nd;
1083d1811465SAl Viro 	int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
1084d1811465SAl Viro 	if (!res)
1085d1811465SAl Viro 		*path = nd.path;
1086d1811465SAl Viro 	return res;
1087d1811465SAl Viro }
1088d1811465SAl Viro 
108916f18200SJosef 'Jeff' Sipek /**
109016f18200SJosef 'Jeff' Sipek  * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
109116f18200SJosef 'Jeff' Sipek  * @dentry:  pointer to dentry of the base directory
109216f18200SJosef 'Jeff' Sipek  * @mnt: pointer to vfs mount of the base directory
109316f18200SJosef 'Jeff' Sipek  * @name: pointer to file name
109416f18200SJosef 'Jeff' Sipek  * @flags: lookup flags
109516f18200SJosef 'Jeff' Sipek  * @nd: pointer to nameidata
109616f18200SJosef 'Jeff' Sipek  */
109716f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
109816f18200SJosef 'Jeff' Sipek 		    const char *name, unsigned int flags,
109916f18200SJosef 'Jeff' Sipek 		    struct nameidata *nd)
110016f18200SJosef 'Jeff' Sipek {
110116f18200SJosef 'Jeff' Sipek 	int retval;
110216f18200SJosef 'Jeff' Sipek 
110316f18200SJosef 'Jeff' Sipek 	/* same as do_path_lookup */
110416f18200SJosef 'Jeff' Sipek 	nd->last_type = LAST_ROOT;
110516f18200SJosef 'Jeff' Sipek 	nd->flags = flags;
110616f18200SJosef 'Jeff' Sipek 	nd->depth = 0;
110716f18200SJosef 'Jeff' Sipek 
1108c8e7f449SJan Blunck 	nd->path.dentry = dentry;
1109c8e7f449SJan Blunck 	nd->path.mnt = mnt;
1110c8e7f449SJan Blunck 	path_get(&nd->path);
111116f18200SJosef 'Jeff' Sipek 
111216f18200SJosef 'Jeff' Sipek 	retval = path_walk(name, nd);
11134ac91378SJan Blunck 	if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
11144ac91378SJan Blunck 				nd->path.dentry->d_inode))
11154ac91378SJan Blunck 		audit_inode(name, nd->path.dentry);
111616f18200SJosef 'Jeff' Sipek 
111716f18200SJosef 'Jeff' Sipek 	return retval;
111816f18200SJosef 'Jeff' Sipek 
111916f18200SJosef 'Jeff' Sipek }
112016f18200SJosef 'Jeff' Sipek 
1121834f2a4aSTrond Myklebust /**
1122834f2a4aSTrond Myklebust  * path_lookup_open - lookup a file path with open intent
11237045f37bSMartin Waitz  * @dfd: the directory to use as base, or AT_FDCWD
1124834f2a4aSTrond Myklebust  * @name: pointer to file name
1125834f2a4aSTrond Myklebust  * @lookup_flags: lookup intent flags
1126834f2a4aSTrond Myklebust  * @nd: pointer to nameidata
1127834f2a4aSTrond Myklebust  * @open_flags: open intent flags
1128834f2a4aSTrond Myklebust  */
11295590ff0dSUlrich Drepper int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags,
1130834f2a4aSTrond Myklebust 		struct nameidata *nd, int open_flags)
1131834f2a4aSTrond Myklebust {
11328737f3a1SAl Viro 	struct file *filp = get_empty_filp();
11338737f3a1SAl Viro 	int err;
1134834f2a4aSTrond Myklebust 
11358737f3a1SAl Viro 	if (filp == NULL)
11368737f3a1SAl Viro 		return -ENFILE;
11378737f3a1SAl Viro 	nd->intent.open.file = filp;
11388737f3a1SAl Viro 	nd->intent.open.flags = open_flags;
11398737f3a1SAl Viro 	nd->intent.open.create_mode = 0;
11408737f3a1SAl Viro 	err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd);
11418737f3a1SAl Viro 	if (IS_ERR(nd->intent.open.file)) {
11428737f3a1SAl Viro 		if (err == 0) {
11438737f3a1SAl Viro 			err = PTR_ERR(nd->intent.open.file);
11448737f3a1SAl Viro 			path_put(&nd->path);
11458737f3a1SAl Viro 		}
11468737f3a1SAl Viro 	} else if (err != 0)
11478737f3a1SAl Viro 		release_open_intent(nd);
11488737f3a1SAl Viro 	return err;
1149834f2a4aSTrond Myklebust }
1150834f2a4aSTrond Myklebust 
1151eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name,
1152eead1911SChristoph Hellwig 		struct dentry *base, struct nameidata *nd)
11531da177e4SLinus Torvalds {
11541da177e4SLinus Torvalds 	struct dentry *dentry;
11551da177e4SLinus Torvalds 	struct inode *inode;
11561da177e4SLinus Torvalds 	int err;
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 	inode = base->d_inode;
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 	/*
11611da177e4SLinus Torvalds 	 * See if the low-level filesystem might want
11621da177e4SLinus Torvalds 	 * to use its own hash..
11631da177e4SLinus Torvalds 	 */
11641da177e4SLinus Torvalds 	if (base->d_op && base->d_op->d_hash) {
11651da177e4SLinus Torvalds 		err = base->d_op->d_hash(base, name);
11661da177e4SLinus Torvalds 		dentry = ERR_PTR(err);
11671da177e4SLinus Torvalds 		if (err < 0)
11681da177e4SLinus Torvalds 			goto out;
11691da177e4SLinus Torvalds 	}
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 	dentry = cached_lookup(base, name, nd);
11721da177e4SLinus Torvalds 	if (!dentry) {
1173d70b67c8SMiklos Szeredi 		struct dentry *new;
1174d70b67c8SMiklos Szeredi 
1175d70b67c8SMiklos Szeredi 		/* Don't create child dentry for a dead directory. */
1176d70b67c8SMiklos Szeredi 		dentry = ERR_PTR(-ENOENT);
1177d70b67c8SMiklos Szeredi 		if (IS_DEADDIR(inode))
1178d70b67c8SMiklos Szeredi 			goto out;
1179d70b67c8SMiklos Szeredi 
1180d70b67c8SMiklos Szeredi 		new = d_alloc(base, name);
11811da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOMEM);
11821da177e4SLinus Torvalds 		if (!new)
11831da177e4SLinus Torvalds 			goto out;
11841da177e4SLinus Torvalds 		dentry = inode->i_op->lookup(inode, new, nd);
11851da177e4SLinus Torvalds 		if (!dentry)
11861da177e4SLinus Torvalds 			dentry = new;
11871da177e4SLinus Torvalds 		else
11881da177e4SLinus Torvalds 			dput(new);
11891da177e4SLinus Torvalds 	}
11901da177e4SLinus Torvalds out:
11911da177e4SLinus Torvalds 	return dentry;
11921da177e4SLinus Torvalds }
11931da177e4SLinus Torvalds 
1194057f6c01SJames Morris /*
1195057f6c01SJames Morris  * Restricted form of lookup. Doesn't follow links, single-component only,
1196057f6c01SJames Morris  * needs parent already locked. Doesn't follow mounts.
1197057f6c01SJames Morris  * SMP-safe.
1198057f6c01SJames Morris  */
1199a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd)
12001da177e4SLinus Torvalds {
1201eead1911SChristoph Hellwig 	int err;
1202eead1911SChristoph Hellwig 
1203f419a2e3SAl Viro 	err = inode_permission(nd->path.dentry->d_inode, MAY_EXEC);
1204eead1911SChristoph Hellwig 	if (err)
1205eead1911SChristoph Hellwig 		return ERR_PTR(err);
12064ac91378SJan Blunck 	return __lookup_hash(&nd->last, nd->path.dentry, nd);
12071da177e4SLinus Torvalds }
12081da177e4SLinus Torvalds 
1209eead1911SChristoph Hellwig static int __lookup_one_len(const char *name, struct qstr *this,
1210eead1911SChristoph Hellwig 		struct dentry *base, int len)
12111da177e4SLinus Torvalds {
12121da177e4SLinus Torvalds 	unsigned long hash;
12131da177e4SLinus Torvalds 	unsigned int c;
12141da177e4SLinus Torvalds 
1215057f6c01SJames Morris 	this->name = name;
1216057f6c01SJames Morris 	this->len = len;
12171da177e4SLinus Torvalds 	if (!len)
1218057f6c01SJames Morris 		return -EACCES;
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds 	hash = init_name_hash();
12211da177e4SLinus Torvalds 	while (len--) {
12221da177e4SLinus Torvalds 		c = *(const unsigned char *)name++;
12231da177e4SLinus Torvalds 		if (c == '/' || c == '\0')
1224057f6c01SJames Morris 			return -EACCES;
12251da177e4SLinus Torvalds 		hash = partial_name_hash(c, hash);
12261da177e4SLinus Torvalds 	}
1227057f6c01SJames Morris 	this->hash = end_name_hash(hash);
1228057f6c01SJames Morris 	return 0;
1229057f6c01SJames Morris }
12301da177e4SLinus Torvalds 
1231eead1911SChristoph Hellwig /**
1232a6b91919SRandy Dunlap  * lookup_one_len - filesystem helper to lookup single pathname component
1233eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1234eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1235eead1911SChristoph Hellwig  * @len:	maximum length @len should be interpreted to
1236eead1911SChristoph Hellwig  *
1237a6b91919SRandy Dunlap  * Note that this routine is purely a helper for filesystem usage and should
1238a6b91919SRandy Dunlap  * not be called by generic code.  Also note that by using this function the
1239eead1911SChristoph Hellwig  * nameidata argument is passed to the filesystem methods and a filesystem
1240eead1911SChristoph Hellwig  * using this helper needs to be prepared for that.
1241eead1911SChristoph Hellwig  */
1242057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1243057f6c01SJames Morris {
1244057f6c01SJames Morris 	int err;
1245057f6c01SJames Morris 	struct qstr this;
1246057f6c01SJames Morris 
1247057f6c01SJames Morris 	err = __lookup_one_len(name, &this, base, len);
1248057f6c01SJames Morris 	if (err)
1249057f6c01SJames Morris 		return ERR_PTR(err);
1250eead1911SChristoph Hellwig 
1251f419a2e3SAl Viro 	err = inode_permission(base->d_inode, MAY_EXEC);
1252eead1911SChristoph Hellwig 	if (err)
1253eead1911SChristoph Hellwig 		return ERR_PTR(err);
125449705b77SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
1255057f6c01SJames Morris }
1256057f6c01SJames Morris 
1257eead1911SChristoph Hellwig /**
1258eead1911SChristoph Hellwig  * lookup_one_noperm - bad hack for sysfs
1259eead1911SChristoph Hellwig  * @name:	pathname component to lookup
1260eead1911SChristoph Hellwig  * @base:	base directory to lookup from
1261eead1911SChristoph Hellwig  *
1262eead1911SChristoph Hellwig  * This is a variant of lookup_one_len that doesn't perform any permission
1263eead1911SChristoph Hellwig  * checks.   It's a horrible hack to work around the braindead sysfs
1264eead1911SChristoph Hellwig  * architecture and should not be used anywhere else.
1265eead1911SChristoph Hellwig  *
1266eead1911SChristoph Hellwig  * DON'T USE THIS FUNCTION EVER, thanks.
1267eead1911SChristoph Hellwig  */
1268eead1911SChristoph Hellwig struct dentry *lookup_one_noperm(const char *name, struct dentry *base)
1269057f6c01SJames Morris {
1270057f6c01SJames Morris 	int err;
1271057f6c01SJames Morris 	struct qstr this;
1272057f6c01SJames Morris 
1273eead1911SChristoph Hellwig 	err = __lookup_one_len(name, &this, base, strlen(name));
1274057f6c01SJames Morris 	if (err)
1275057f6c01SJames Morris 		return ERR_PTR(err);
1276eead1911SChristoph Hellwig 	return __lookup_hash(&this, base, NULL);
12771da177e4SLinus Torvalds }
12781da177e4SLinus Torvalds 
12792d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags,
12802d8f3038SAl Viro 		 struct path *path)
12811da177e4SLinus Torvalds {
12822d8f3038SAl Viro 	struct nameidata nd;
12831da177e4SLinus Torvalds 	char *tmp = getname(name);
12841da177e4SLinus Torvalds 	int err = PTR_ERR(tmp);
12851da177e4SLinus Torvalds 	if (!IS_ERR(tmp)) {
12862d8f3038SAl Viro 
12872d8f3038SAl Viro 		BUG_ON(flags & LOOKUP_PARENT);
12882d8f3038SAl Viro 
12892d8f3038SAl Viro 		err = do_path_lookup(dfd, tmp, flags, &nd);
12901da177e4SLinus Torvalds 		putname(tmp);
12912d8f3038SAl Viro 		if (!err)
12922d8f3038SAl Viro 			*path = nd.path;
12931da177e4SLinus Torvalds 	}
12941da177e4SLinus Torvalds 	return err;
12951da177e4SLinus Torvalds }
12961da177e4SLinus Torvalds 
12972ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path,
12982ad94ae6SAl Viro 			struct nameidata *nd, char **name)
12992ad94ae6SAl Viro {
13002ad94ae6SAl Viro 	char *s = getname(path);
13012ad94ae6SAl Viro 	int error;
13022ad94ae6SAl Viro 
13032ad94ae6SAl Viro 	if (IS_ERR(s))
13042ad94ae6SAl Viro 		return PTR_ERR(s);
13052ad94ae6SAl Viro 
13062ad94ae6SAl Viro 	error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
13072ad94ae6SAl Viro 	if (error)
13082ad94ae6SAl Viro 		putname(s);
13092ad94ae6SAl Viro 	else
13102ad94ae6SAl Viro 		*name = s;
13112ad94ae6SAl Viro 
13122ad94ae6SAl Viro 	return error;
13132ad94ae6SAl Viro }
13142ad94ae6SAl Viro 
13151da177e4SLinus Torvalds /*
13161da177e4SLinus Torvalds  * It's inline, so penalty for filesystems that don't use sticky bit is
13171da177e4SLinus Torvalds  * minimal.
13181da177e4SLinus Torvalds  */
13191da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode)
13201da177e4SLinus Torvalds {
1321da9592edSDavid Howells 	uid_t fsuid = current_fsuid();
1322da9592edSDavid Howells 
13231da177e4SLinus Torvalds 	if (!(dir->i_mode & S_ISVTX))
13241da177e4SLinus Torvalds 		return 0;
1325da9592edSDavid Howells 	if (inode->i_uid == fsuid)
13261da177e4SLinus Torvalds 		return 0;
1327da9592edSDavid Howells 	if (dir->i_uid == fsuid)
13281da177e4SLinus Torvalds 		return 0;
13291da177e4SLinus Torvalds 	return !capable(CAP_FOWNER);
13301da177e4SLinus Torvalds }
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds /*
13331da177e4SLinus Torvalds  *	Check whether we can remove a link victim from directory dir, check
13341da177e4SLinus Torvalds  *  whether the type of victim is right.
13351da177e4SLinus Torvalds  *  1. We can't do it if dir is read-only (done in permission())
13361da177e4SLinus Torvalds  *  2. We should have write and exec permissions on dir
13371da177e4SLinus Torvalds  *  3. We can't remove anything from append-only dir
13381da177e4SLinus Torvalds  *  4. We can't do anything with immutable dir (done in permission())
13391da177e4SLinus Torvalds  *  5. If the sticky bit on dir is set we should either
13401da177e4SLinus Torvalds  *	a. be owner of dir, or
13411da177e4SLinus Torvalds  *	b. be owner of victim, or
13421da177e4SLinus Torvalds  *	c. have CAP_FOWNER capability
13431da177e4SLinus Torvalds  *  6. If the victim is append-only or immutable we can't do antyhing with
13441da177e4SLinus Torvalds  *     links pointing to it.
13451da177e4SLinus Torvalds  *  7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
13461da177e4SLinus Torvalds  *  8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
13471da177e4SLinus Torvalds  *  9. We can't remove a root or mountpoint.
13481da177e4SLinus Torvalds  * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
13491da177e4SLinus Torvalds  *     nfs_async_unlink().
13501da177e4SLinus Torvalds  */
1351858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
13521da177e4SLinus Torvalds {
13531da177e4SLinus Torvalds 	int error;
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 	if (!victim->d_inode)
13561da177e4SLinus Torvalds 		return -ENOENT;
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds 	BUG_ON(victim->d_parent->d_inode != dir);
13595a190ae6SAl Viro 	audit_inode_child(victim->d_name.name, victim, dir);
13601da177e4SLinus Torvalds 
1361f419a2e3SAl Viro 	error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
13621da177e4SLinus Torvalds 	if (error)
13631da177e4SLinus Torvalds 		return error;
13641da177e4SLinus Torvalds 	if (IS_APPEND(dir))
13651da177e4SLinus Torvalds 		return -EPERM;
13661da177e4SLinus Torvalds 	if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1367f9454548SHugh Dickins 	    IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
13681da177e4SLinus Torvalds 		return -EPERM;
13691da177e4SLinus Torvalds 	if (isdir) {
13701da177e4SLinus Torvalds 		if (!S_ISDIR(victim->d_inode->i_mode))
13711da177e4SLinus Torvalds 			return -ENOTDIR;
13721da177e4SLinus Torvalds 		if (IS_ROOT(victim))
13731da177e4SLinus Torvalds 			return -EBUSY;
13741da177e4SLinus Torvalds 	} else if (S_ISDIR(victim->d_inode->i_mode))
13751da177e4SLinus Torvalds 		return -EISDIR;
13761da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
13771da177e4SLinus Torvalds 		return -ENOENT;
13781da177e4SLinus Torvalds 	if (victim->d_flags & DCACHE_NFSFS_RENAMED)
13791da177e4SLinus Torvalds 		return -EBUSY;
13801da177e4SLinus Torvalds 	return 0;
13811da177e4SLinus Torvalds }
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds /*	Check whether we can create an object with dentry child in directory
13841da177e4SLinus Torvalds  *  dir.
13851da177e4SLinus Torvalds  *  1. We can't do it if child already exists (open has special treatment for
13861da177e4SLinus Torvalds  *     this case, but since we are inlined it's OK)
13871da177e4SLinus Torvalds  *  2. We can't do it if dir is read-only (done in permission())
13881da177e4SLinus Torvalds  *  3. We should have write and exec permissions on dir
13891da177e4SLinus Torvalds  *  4. We can't do it if dir is immutable (done in permission())
13901da177e4SLinus Torvalds  */
1391a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child)
13921da177e4SLinus Torvalds {
13931da177e4SLinus Torvalds 	if (child->d_inode)
13941da177e4SLinus Torvalds 		return -EEXIST;
13951da177e4SLinus Torvalds 	if (IS_DEADDIR(dir))
13961da177e4SLinus Torvalds 		return -ENOENT;
1397f419a2e3SAl Viro 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
13981da177e4SLinus Torvalds }
13991da177e4SLinus Torvalds 
14001da177e4SLinus Torvalds /*
14011da177e4SLinus Torvalds  * O_DIRECTORY translates into forcing a directory lookup.
14021da177e4SLinus Torvalds  */
14031da177e4SLinus Torvalds static inline int lookup_flags(unsigned int f)
14041da177e4SLinus Torvalds {
14051da177e4SLinus Torvalds 	unsigned long retval = LOOKUP_FOLLOW;
14061da177e4SLinus Torvalds 
14071da177e4SLinus Torvalds 	if (f & O_NOFOLLOW)
14081da177e4SLinus Torvalds 		retval &= ~LOOKUP_FOLLOW;
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds 	if (f & O_DIRECTORY)
14111da177e4SLinus Torvalds 		retval |= LOOKUP_DIRECTORY;
14121da177e4SLinus Torvalds 
14131da177e4SLinus Torvalds 	return retval;
14141da177e4SLinus Torvalds }
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds /*
14171da177e4SLinus Torvalds  * p1 and p2 should be directories on the same fs.
14181da177e4SLinus Torvalds  */
14191da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
14201da177e4SLinus Torvalds {
14211da177e4SLinus Torvalds 	struct dentry *p;
14221da177e4SLinus Torvalds 
14231da177e4SLinus Torvalds 	if (p1 == p2) {
1424f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
14251da177e4SLinus Torvalds 		return NULL;
14261da177e4SLinus Torvalds 	}
14271da177e4SLinus Torvalds 
1428a11f3a05SArjan van de Ven 	mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
14291da177e4SLinus Torvalds 
1430e2761a11SOGAWA Hirofumi 	p = d_ancestor(p2, p1);
1431e2761a11SOGAWA Hirofumi 	if (p) {
1432f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1433f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
14341da177e4SLinus Torvalds 		return p;
14351da177e4SLinus Torvalds 	}
14361da177e4SLinus Torvalds 
1437e2761a11SOGAWA Hirofumi 	p = d_ancestor(p1, p2);
1438e2761a11SOGAWA Hirofumi 	if (p) {
1439f2eace23SIngo Molnar 		mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1440f2eace23SIngo Molnar 		mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
14411da177e4SLinus Torvalds 		return p;
14421da177e4SLinus Torvalds 	}
14431da177e4SLinus Torvalds 
1444f2eace23SIngo Molnar 	mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1445f2eace23SIngo Molnar 	mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
14461da177e4SLinus Torvalds 	return NULL;
14471da177e4SLinus Torvalds }
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2)
14501da177e4SLinus Torvalds {
14511b1dcc1bSJes Sorensen 	mutex_unlock(&p1->d_inode->i_mutex);
14521da177e4SLinus Torvalds 	if (p1 != p2) {
14531b1dcc1bSJes Sorensen 		mutex_unlock(&p2->d_inode->i_mutex);
1454a11f3a05SArjan van de Ven 		mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
14551da177e4SLinus Torvalds 	}
14561da177e4SLinus Torvalds }
14571da177e4SLinus Torvalds 
14581da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
14591da177e4SLinus Torvalds 		struct nameidata *nd)
14601da177e4SLinus Torvalds {
1461a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
14621da177e4SLinus Torvalds 
14631da177e4SLinus Torvalds 	if (error)
14641da177e4SLinus Torvalds 		return error;
14651da177e4SLinus Torvalds 
1466acfa4380SAl Viro 	if (!dir->i_op->create)
14671da177e4SLinus Torvalds 		return -EACCES;	/* shouldn't it be ENOSYS? */
14681da177e4SLinus Torvalds 	mode &= S_IALLUGO;
14691da177e4SLinus Torvalds 	mode |= S_IFREG;
14701da177e4SLinus Torvalds 	error = security_inode_create(dir, dentry, mode);
14711da177e4SLinus Torvalds 	if (error)
14721da177e4SLinus Torvalds 		return error;
14731da177e4SLinus Torvalds 	DQUOT_INIT(dir);
14741da177e4SLinus Torvalds 	error = dir->i_op->create(dir, dentry, mode, nd);
1475a74574aaSStephen Smalley 	if (!error)
1476f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
14771da177e4SLinus Torvalds 	return error;
14781da177e4SLinus Torvalds }
14791da177e4SLinus Torvalds 
14803fb64190SChristoph Hellwig int may_open(struct path *path, int acc_mode, int flag)
14811da177e4SLinus Torvalds {
14823fb64190SChristoph Hellwig 	struct dentry *dentry = path->dentry;
14831da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
14841da177e4SLinus Torvalds 	int error;
14851da177e4SLinus Torvalds 
14861da177e4SLinus Torvalds 	if (!inode)
14871da177e4SLinus Torvalds 		return -ENOENT;
14881da177e4SLinus Torvalds 
1489c8fe8f30SChristoph Hellwig 	switch (inode->i_mode & S_IFMT) {
1490c8fe8f30SChristoph Hellwig 	case S_IFLNK:
14911da177e4SLinus Torvalds 		return -ELOOP;
1492c8fe8f30SChristoph Hellwig 	case S_IFDIR:
1493c8fe8f30SChristoph Hellwig 		if (acc_mode & MAY_WRITE)
14941da177e4SLinus Torvalds 			return -EISDIR;
1495c8fe8f30SChristoph Hellwig 		break;
1496c8fe8f30SChristoph Hellwig 	case S_IFBLK:
1497c8fe8f30SChristoph Hellwig 	case S_IFCHR:
14983fb64190SChristoph Hellwig 		if (path->mnt->mnt_flags & MNT_NODEV)
14991da177e4SLinus Torvalds 			return -EACCES;
1500c8fe8f30SChristoph Hellwig 		/*FALLTHRU*/
1501c8fe8f30SChristoph Hellwig 	case S_IFIFO:
1502c8fe8f30SChristoph Hellwig 	case S_IFSOCK:
15031da177e4SLinus Torvalds 		flag &= ~O_TRUNC;
1504c8fe8f30SChristoph Hellwig 		break;
15054a3fd211SDave Hansen 	}
1506b41572e9SDave Hansen 
15073fb64190SChristoph Hellwig 	error = inode_permission(inode, acc_mode);
1508b41572e9SDave Hansen 	if (error)
1509b41572e9SDave Hansen 		return error;
15101da177e4SLinus Torvalds 	/*
15111da177e4SLinus Torvalds 	 * An append-only file must be opened in append mode for writing.
15121da177e4SLinus Torvalds 	 */
15131da177e4SLinus Torvalds 	if (IS_APPEND(inode)) {
15141da177e4SLinus Torvalds 		if  ((flag & FMODE_WRITE) && !(flag & O_APPEND))
15151da177e4SLinus Torvalds 			return -EPERM;
15161da177e4SLinus Torvalds 		if (flag & O_TRUNC)
15171da177e4SLinus Torvalds 			return -EPERM;
15181da177e4SLinus Torvalds 	}
15191da177e4SLinus Torvalds 
15201da177e4SLinus Torvalds 	/* O_NOATIME can only be set by the owner or superuser */
15211da177e4SLinus Torvalds 	if (flag & O_NOATIME)
15223bd858abSSatyam Sharma 		if (!is_owner_or_cap(inode))
15231da177e4SLinus Torvalds 			return -EPERM;
15241da177e4SLinus Torvalds 
15251da177e4SLinus Torvalds 	/*
15261da177e4SLinus Torvalds 	 * Ensure there are no outstanding leases on the file.
15271da177e4SLinus Torvalds 	 */
15281da177e4SLinus Torvalds 	error = break_lease(inode, flag);
15291da177e4SLinus Torvalds 	if (error)
15301da177e4SLinus Torvalds 		return error;
15311da177e4SLinus Torvalds 
15321da177e4SLinus Torvalds 	if (flag & O_TRUNC) {
15331da177e4SLinus Torvalds 		error = get_write_access(inode);
15341da177e4SLinus Torvalds 		if (error)
15351da177e4SLinus Torvalds 			return error;
15361da177e4SLinus Torvalds 
15371da177e4SLinus Torvalds 		/*
15381da177e4SLinus Torvalds 		 * Refuse to truncate files with mandatory locks held on them.
15391da177e4SLinus Torvalds 		 */
15401da177e4SLinus Torvalds 		error = locks_verify_locked(inode);
1541be6d3e56SKentaro Takeda 		if (!error)
15423fb64190SChristoph Hellwig 			error = security_path_truncate(path, 0,
1543be6d3e56SKentaro Takeda 					       ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
15441da177e4SLinus Torvalds 		if (!error) {
15451da177e4SLinus Torvalds 			DQUOT_INIT(inode);
15461da177e4SLinus Torvalds 
1547d139d7ffSMiklos Szeredi 			error = do_truncate(dentry, 0,
1548d139d7ffSMiklos Szeredi 					    ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
1549d139d7ffSMiklos Szeredi 					    NULL);
15501da177e4SLinus Torvalds 		}
15511da177e4SLinus Torvalds 		put_write_access(inode);
15521da177e4SLinus Torvalds 		if (error)
15531da177e4SLinus Torvalds 			return error;
15541da177e4SLinus Torvalds 	} else
15551da177e4SLinus Torvalds 		if (flag & FMODE_WRITE)
15561da177e4SLinus Torvalds 			DQUOT_INIT(inode);
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds 	return 0;
15591da177e4SLinus Torvalds }
15601da177e4SLinus Torvalds 
1561d57999e1SDave Hansen /*
1562d57999e1SDave Hansen  * Be careful about ever adding any more callers of this
1563d57999e1SDave Hansen  * function.  Its flags must be in the namei format, not
1564d57999e1SDave Hansen  * what get passed to sys_open().
1565d57999e1SDave Hansen  */
1566d57999e1SDave Hansen static int __open_namei_create(struct nameidata *nd, struct path *path,
1567aab520e2SDave Hansen 				int flag, int mode)
1568aab520e2SDave Hansen {
1569aab520e2SDave Hansen 	int error;
15704ac91378SJan Blunck 	struct dentry *dir = nd->path.dentry;
1571aab520e2SDave Hansen 
1572aab520e2SDave Hansen 	if (!IS_POSIXACL(dir->d_inode))
1573aab520e2SDave Hansen 		mode &= ~current->fs->umask;
1574be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd->path, path->dentry, mode, 0);
1575be6d3e56SKentaro Takeda 	if (error)
1576be6d3e56SKentaro Takeda 		goto out_unlock;
1577aab520e2SDave Hansen 	error = vfs_create(dir->d_inode, path->dentry, mode, nd);
1578be6d3e56SKentaro Takeda out_unlock:
1579aab520e2SDave Hansen 	mutex_unlock(&dir->d_inode->i_mutex);
15804ac91378SJan Blunck 	dput(nd->path.dentry);
15814ac91378SJan Blunck 	nd->path.dentry = path->dentry;
1582aab520e2SDave Hansen 	if (error)
1583aab520e2SDave Hansen 		return error;
1584aab520e2SDave Hansen 	/* Don't check for write permission, don't truncate */
15853fb64190SChristoph Hellwig 	return may_open(&nd->path, 0, flag & ~O_TRUNC);
1586aab520e2SDave Hansen }
1587aab520e2SDave Hansen 
15881da177e4SLinus Torvalds /*
1589d57999e1SDave Hansen  * Note that while the flag value (low two bits) for sys_open means:
1590d57999e1SDave Hansen  *	00 - read-only
1591d57999e1SDave Hansen  *	01 - write-only
1592d57999e1SDave Hansen  *	10 - read-write
1593d57999e1SDave Hansen  *	11 - special
1594d57999e1SDave Hansen  * it is changed into
1595d57999e1SDave Hansen  *	00 - no permissions needed
1596d57999e1SDave Hansen  *	01 - read-permission
1597d57999e1SDave Hansen  *	10 - write-permission
1598d57999e1SDave Hansen  *	11 - read-write
1599d57999e1SDave Hansen  * for the internal routines (ie open_namei()/follow_link() etc)
1600d57999e1SDave Hansen  * This is more logical, and also allows the 00 "no perm needed"
1601d57999e1SDave Hansen  * to be used for symlinks (where the permissions are checked
1602d57999e1SDave Hansen  * later).
1603d57999e1SDave Hansen  *
1604d57999e1SDave Hansen */
1605d57999e1SDave Hansen static inline int open_to_namei_flags(int flag)
1606d57999e1SDave Hansen {
1607d57999e1SDave Hansen 	if ((flag+1) & O_ACCMODE)
1608d57999e1SDave Hansen 		flag++;
1609d57999e1SDave Hansen 	return flag;
1610d57999e1SDave Hansen }
1611d57999e1SDave Hansen 
16124a3fd211SDave Hansen static int open_will_write_to_fs(int flag, struct inode *inode)
16134a3fd211SDave Hansen {
1614d57999e1SDave Hansen 	/*
16154a3fd211SDave Hansen 	 * We'll never write to the fs underlying
16164a3fd211SDave Hansen 	 * a device file.
16174a3fd211SDave Hansen 	 */
16184a3fd211SDave Hansen 	if (special_file(inode->i_mode))
16194a3fd211SDave Hansen 		return 0;
16204a3fd211SDave Hansen 	return (flag & O_TRUNC);
16214a3fd211SDave Hansen }
16224a3fd211SDave Hansen 
16234a3fd211SDave Hansen /*
16244a3fd211SDave Hansen  * Note that the low bits of the passed in "open_flag"
16254a3fd211SDave Hansen  * are not the same as in the local variable "flag". See
16264a3fd211SDave Hansen  * open_to_namei_flags() for more details.
16271da177e4SLinus Torvalds  */
1628a70e65dfSChristoph Hellwig struct file *do_filp_open(int dfd, const char *pathname,
1629a70e65dfSChristoph Hellwig 		int open_flag, int mode)
16301da177e4SLinus Torvalds {
16314a3fd211SDave Hansen 	struct file *filp;
1632a70e65dfSChristoph Hellwig 	struct nameidata nd;
1633834f2a4aSTrond Myklebust 	int acc_mode, error;
16344e7506e4SAl Viro 	struct path path;
16351da177e4SLinus Torvalds 	struct dentry *dir;
16361da177e4SLinus Torvalds 	int count = 0;
16374a3fd211SDave Hansen 	int will_write;
1638d57999e1SDave Hansen 	int flag = open_to_namei_flags(open_flag);
16391da177e4SLinus Torvalds 
1640b77b0646SAl Viro 	acc_mode = MAY_OPEN | ACC_MODE(flag);
16411da177e4SLinus Torvalds 
1642834f2a4aSTrond Myklebust 	/* O_TRUNC implies we need access checks for write permissions */
1643834f2a4aSTrond Myklebust 	if (flag & O_TRUNC)
1644834f2a4aSTrond Myklebust 		acc_mode |= MAY_WRITE;
1645834f2a4aSTrond Myklebust 
16461da177e4SLinus Torvalds 	/* Allow the LSM permission hook to distinguish append
16471da177e4SLinus Torvalds 	   access from general write access. */
16481da177e4SLinus Torvalds 	if (flag & O_APPEND)
16491da177e4SLinus Torvalds 		acc_mode |= MAY_APPEND;
16501da177e4SLinus Torvalds 
16511da177e4SLinus Torvalds 	/*
16521da177e4SLinus Torvalds 	 * The simplest case - just a plain lookup.
16531da177e4SLinus Torvalds 	 */
16541da177e4SLinus Torvalds 	if (!(flag & O_CREAT)) {
16555590ff0dSUlrich Drepper 		error = path_lookup_open(dfd, pathname, lookup_flags(flag),
1656a70e65dfSChristoph Hellwig 					 &nd, flag);
16571da177e4SLinus Torvalds 		if (error)
1658a70e65dfSChristoph Hellwig 			return ERR_PTR(error);
16591da177e4SLinus Torvalds 		goto ok;
16601da177e4SLinus Torvalds 	}
16611da177e4SLinus Torvalds 
16621da177e4SLinus Torvalds 	/*
16631da177e4SLinus Torvalds 	 * Create - we need to know the parent.
16641da177e4SLinus Torvalds 	 */
16658737f3a1SAl Viro 	error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd);
16661da177e4SLinus Torvalds 	if (error)
1667a70e65dfSChristoph Hellwig 		return ERR_PTR(error);
16681da177e4SLinus Torvalds 
16691da177e4SLinus Torvalds 	/*
16701da177e4SLinus Torvalds 	 * We have the parent and last component. First of all, check
16711da177e4SLinus Torvalds 	 * that we are not asked to creat(2) an obvious directory - that
16721da177e4SLinus Torvalds 	 * will not do.
16731da177e4SLinus Torvalds 	 */
16741da177e4SLinus Torvalds 	error = -EISDIR;
1675a70e65dfSChristoph Hellwig 	if (nd.last_type != LAST_NORM || nd.last.name[nd.last.len])
16768737f3a1SAl Viro 		goto exit_parent;
16771da177e4SLinus Torvalds 
16788737f3a1SAl Viro 	error = -ENFILE;
16798737f3a1SAl Viro 	filp = get_empty_filp();
16808737f3a1SAl Viro 	if (filp == NULL)
16818737f3a1SAl Viro 		goto exit_parent;
16828737f3a1SAl Viro 	nd.intent.open.file = filp;
16838737f3a1SAl Viro 	nd.intent.open.flags = flag;
16848737f3a1SAl Viro 	nd.intent.open.create_mode = mode;
1685a70e65dfSChristoph Hellwig 	dir = nd.path.dentry;
1686a70e65dfSChristoph Hellwig 	nd.flags &= ~LOOKUP_PARENT;
16878737f3a1SAl Viro 	nd.flags |= LOOKUP_CREATE | LOOKUP_OPEN;
16883516586aSAl Viro 	if (flag & O_EXCL)
16893516586aSAl Viro 		nd.flags |= LOOKUP_EXCL;
16901b1dcc1bSJes Sorensen 	mutex_lock(&dir->d_inode->i_mutex);
1691a70e65dfSChristoph Hellwig 	path.dentry = lookup_hash(&nd);
1692a70e65dfSChristoph Hellwig 	path.mnt = nd.path.mnt;
16931da177e4SLinus Torvalds 
16941da177e4SLinus Torvalds do_last:
16954e7506e4SAl Viro 	error = PTR_ERR(path.dentry);
16964e7506e4SAl Viro 	if (IS_ERR(path.dentry)) {
16971b1dcc1bSJes Sorensen 		mutex_unlock(&dir->d_inode->i_mutex);
16981da177e4SLinus Torvalds 		goto exit;
16991da177e4SLinus Torvalds 	}
17001da177e4SLinus Torvalds 
1701a70e65dfSChristoph Hellwig 	if (IS_ERR(nd.intent.open.file)) {
1702a70e65dfSChristoph Hellwig 		error = PTR_ERR(nd.intent.open.file);
17034a3fd211SDave Hansen 		goto exit_mutex_unlock;
17044af4c52fSOleg Drokin 	}
17054af4c52fSOleg Drokin 
17061da177e4SLinus Torvalds 	/* Negative dentry, just create the file */
17074e7506e4SAl Viro 	if (!path.dentry->d_inode) {
17084a3fd211SDave Hansen 		/*
17094a3fd211SDave Hansen 		 * This write is needed to ensure that a
17104a3fd211SDave Hansen 		 * ro->rw transition does not occur between
17114a3fd211SDave Hansen 		 * the time when the file is created and when
17124a3fd211SDave Hansen 		 * a permanent write count is taken through
17134a3fd211SDave Hansen 		 * the 'struct file' in nameidata_to_filp().
17144a3fd211SDave Hansen 		 */
17154a3fd211SDave Hansen 		error = mnt_want_write(nd.path.mnt);
17161da177e4SLinus Torvalds 		if (error)
17174a3fd211SDave Hansen 			goto exit_mutex_unlock;
17184a3fd211SDave Hansen 		error = __open_namei_create(&nd, &path, flag, mode);
17194a3fd211SDave Hansen 		if (error) {
17204a3fd211SDave Hansen 			mnt_drop_write(nd.path.mnt);
17211da177e4SLinus Torvalds 			goto exit;
17224a3fd211SDave Hansen 		}
17234a3fd211SDave Hansen 		filp = nameidata_to_filp(&nd, open_flag);
17244a3fd211SDave Hansen 		mnt_drop_write(nd.path.mnt);
17254a3fd211SDave Hansen 		return filp;
17261da177e4SLinus Torvalds 	}
17271da177e4SLinus Torvalds 
17281da177e4SLinus Torvalds 	/*
17291da177e4SLinus Torvalds 	 * It already exists.
17301da177e4SLinus Torvalds 	 */
17311b1dcc1bSJes Sorensen 	mutex_unlock(&dir->d_inode->i_mutex);
17325a190ae6SAl Viro 	audit_inode(pathname, path.dentry);
17331da177e4SLinus Torvalds 
17341da177e4SLinus Torvalds 	error = -EEXIST;
17351da177e4SLinus Torvalds 	if (flag & O_EXCL)
17361da177e4SLinus Torvalds 		goto exit_dput;
17371da177e4SLinus Torvalds 
1738e13b210fSAl Viro 	if (__follow_mount(&path)) {
17391da177e4SLinus Torvalds 		error = -ELOOP;
1740ba7a4c1aSAl Viro 		if (flag & O_NOFOLLOW)
1741ba7a4c1aSAl Viro 			goto exit_dput;
17421da177e4SLinus Torvalds 	}
17433e2efce0SAmy Griffis 
17441da177e4SLinus Torvalds 	error = -ENOENT;
17454e7506e4SAl Viro 	if (!path.dentry->d_inode)
17461da177e4SLinus Torvalds 		goto exit_dput;
1747acfa4380SAl Viro 	if (path.dentry->d_inode->i_op->follow_link)
17481da177e4SLinus Torvalds 		goto do_link;
17491da177e4SLinus Torvalds 
1750a70e65dfSChristoph Hellwig 	path_to_nameidata(&path, &nd);
17511da177e4SLinus Torvalds 	error = -EISDIR;
17524e7506e4SAl Viro 	if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
17531da177e4SLinus Torvalds 		goto exit;
17541da177e4SLinus Torvalds ok:
17554a3fd211SDave Hansen 	/*
17564a3fd211SDave Hansen 	 * Consider:
17574a3fd211SDave Hansen 	 * 1. may_open() truncates a file
17584a3fd211SDave Hansen 	 * 2. a rw->ro mount transition occurs
17594a3fd211SDave Hansen 	 * 3. nameidata_to_filp() fails due to
17604a3fd211SDave Hansen 	 *    the ro mount.
17614a3fd211SDave Hansen 	 * That would be inconsistent, and should
17624a3fd211SDave Hansen 	 * be avoided. Taking this mnt write here
17634a3fd211SDave Hansen 	 * ensures that (2) can not occur.
17644a3fd211SDave Hansen 	 */
17654a3fd211SDave Hansen 	will_write = open_will_write_to_fs(flag, nd.path.dentry->d_inode);
17664a3fd211SDave Hansen 	if (will_write) {
17674a3fd211SDave Hansen 		error = mnt_want_write(nd.path.mnt);
17681da177e4SLinus Torvalds 		if (error)
17691da177e4SLinus Torvalds 			goto exit;
17704a3fd211SDave Hansen 	}
17713fb64190SChristoph Hellwig 	error = may_open(&nd.path, acc_mode, flag);
17724a3fd211SDave Hansen 	if (error) {
17734a3fd211SDave Hansen 		if (will_write)
17744a3fd211SDave Hansen 			mnt_drop_write(nd.path.mnt);
17754a3fd211SDave Hansen 		goto exit;
17764a3fd211SDave Hansen 	}
17774a3fd211SDave Hansen 	filp = nameidata_to_filp(&nd, open_flag);
17784a3fd211SDave Hansen 	/*
17794a3fd211SDave Hansen 	 * It is now safe to drop the mnt write
17804a3fd211SDave Hansen 	 * because the filp has had a write taken
17814a3fd211SDave Hansen 	 * on its behalf.
17824a3fd211SDave Hansen 	 */
17834a3fd211SDave Hansen 	if (will_write)
17844a3fd211SDave Hansen 		mnt_drop_write(nd.path.mnt);
17854a3fd211SDave Hansen 	return filp;
17861da177e4SLinus Torvalds 
17874a3fd211SDave Hansen exit_mutex_unlock:
17884a3fd211SDave Hansen 	mutex_unlock(&dir->d_inode->i_mutex);
17891da177e4SLinus Torvalds exit_dput:
1790a70e65dfSChristoph Hellwig 	path_put_conditional(&path, &nd);
17911da177e4SLinus Torvalds exit:
1792a70e65dfSChristoph Hellwig 	if (!IS_ERR(nd.intent.open.file))
1793a70e65dfSChristoph Hellwig 		release_open_intent(&nd);
17948737f3a1SAl Viro exit_parent:
1795a70e65dfSChristoph Hellwig 	path_put(&nd.path);
1796a70e65dfSChristoph Hellwig 	return ERR_PTR(error);
17971da177e4SLinus Torvalds 
17981da177e4SLinus Torvalds do_link:
17991da177e4SLinus Torvalds 	error = -ELOOP;
18001da177e4SLinus Torvalds 	if (flag & O_NOFOLLOW)
18011da177e4SLinus Torvalds 		goto exit_dput;
18021da177e4SLinus Torvalds 	/*
18031da177e4SLinus Torvalds 	 * This is subtle. Instead of calling do_follow_link() we do the
18041da177e4SLinus Torvalds 	 * thing by hands. The reason is that this way we have zero link_count
18051da177e4SLinus Torvalds 	 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
18061da177e4SLinus Torvalds 	 * After that we have the parent and last component, i.e.
18071da177e4SLinus Torvalds 	 * we are in the same situation as after the first path_walk().
18081da177e4SLinus Torvalds 	 * Well, almost - if the last component is normal we get its copy
18091da177e4SLinus Torvalds 	 * stored in nd->last.name and we will have to putname() it when we
18101da177e4SLinus Torvalds 	 * are done. Procfs-like symlinks just set LAST_BIND.
18111da177e4SLinus Torvalds 	 */
1812a70e65dfSChristoph Hellwig 	nd.flags |= LOOKUP_PARENT;
1813a70e65dfSChristoph Hellwig 	error = security_inode_follow_link(path.dentry, &nd);
18141da177e4SLinus Torvalds 	if (error)
18151da177e4SLinus Torvalds 		goto exit_dput;
1816a70e65dfSChristoph Hellwig 	error = __do_follow_link(&path, &nd);
1817de459215SKirill Korotaev 	if (error) {
1818de459215SKirill Korotaev 		/* Does someone understand code flow here? Or it is only
1819de459215SKirill Korotaev 		 * me so stupid? Anathema to whoever designed this non-sense
1820de459215SKirill Korotaev 		 * with "intent.open".
1821de459215SKirill Korotaev 		 */
1822a70e65dfSChristoph Hellwig 		release_open_intent(&nd);
1823a70e65dfSChristoph Hellwig 		return ERR_PTR(error);
1824de459215SKirill Korotaev 	}
1825a70e65dfSChristoph Hellwig 	nd.flags &= ~LOOKUP_PARENT;
1826a70e65dfSChristoph Hellwig 	if (nd.last_type == LAST_BIND)
18271da177e4SLinus Torvalds 		goto ok;
18281da177e4SLinus Torvalds 	error = -EISDIR;
1829a70e65dfSChristoph Hellwig 	if (nd.last_type != LAST_NORM)
18301da177e4SLinus Torvalds 		goto exit;
1831a70e65dfSChristoph Hellwig 	if (nd.last.name[nd.last.len]) {
1832a70e65dfSChristoph Hellwig 		__putname(nd.last.name);
18331da177e4SLinus Torvalds 		goto exit;
18341da177e4SLinus Torvalds 	}
18351da177e4SLinus Torvalds 	error = -ELOOP;
18361da177e4SLinus Torvalds 	if (count++==32) {
1837a70e65dfSChristoph Hellwig 		__putname(nd.last.name);
18381da177e4SLinus Torvalds 		goto exit;
18391da177e4SLinus Torvalds 	}
1840a70e65dfSChristoph Hellwig 	dir = nd.path.dentry;
18411b1dcc1bSJes Sorensen 	mutex_lock(&dir->d_inode->i_mutex);
1842a70e65dfSChristoph Hellwig 	path.dentry = lookup_hash(&nd);
1843a70e65dfSChristoph Hellwig 	path.mnt = nd.path.mnt;
1844a70e65dfSChristoph Hellwig 	__putname(nd.last.name);
18451da177e4SLinus Torvalds 	goto do_last;
18461da177e4SLinus Torvalds }
18471da177e4SLinus Torvalds 
18481da177e4SLinus Torvalds /**
1849a70e65dfSChristoph Hellwig  * filp_open - open file and return file pointer
1850a70e65dfSChristoph Hellwig  *
1851a70e65dfSChristoph Hellwig  * @filename:	path to open
1852a70e65dfSChristoph Hellwig  * @flags:	open flags as per the open(2) second argument
1853a70e65dfSChristoph Hellwig  * @mode:	mode for the new file if O_CREAT is set, else ignored
1854a70e65dfSChristoph Hellwig  *
1855a70e65dfSChristoph Hellwig  * This is the helper to open a file from kernelspace if you really
1856a70e65dfSChristoph Hellwig  * have to.  But in generally you should not do this, so please move
1857a70e65dfSChristoph Hellwig  * along, nothing to see here..
1858a70e65dfSChristoph Hellwig  */
1859a70e65dfSChristoph Hellwig struct file *filp_open(const char *filename, int flags, int mode)
1860a70e65dfSChristoph Hellwig {
1861a70e65dfSChristoph Hellwig 	return do_filp_open(AT_FDCWD, filename, flags, mode);
1862a70e65dfSChristoph Hellwig }
1863a70e65dfSChristoph Hellwig EXPORT_SYMBOL(filp_open);
1864a70e65dfSChristoph Hellwig 
1865a70e65dfSChristoph Hellwig /**
18661da177e4SLinus Torvalds  * lookup_create - lookup a dentry, creating it if it doesn't exist
18671da177e4SLinus Torvalds  * @nd: nameidata info
18681da177e4SLinus Torvalds  * @is_dir: directory flag
18691da177e4SLinus Torvalds  *
18701da177e4SLinus Torvalds  * Simple function to lookup and return a dentry and create it
18711da177e4SLinus Torvalds  * if it doesn't exist.  Is SMP-safe.
1872c663e5d8SChristoph Hellwig  *
18734ac91378SJan Blunck  * Returns with nd->path.dentry->d_inode->i_mutex locked.
18741da177e4SLinus Torvalds  */
18751da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir)
18761da177e4SLinus Torvalds {
1877c663e5d8SChristoph Hellwig 	struct dentry *dentry = ERR_PTR(-EEXIST);
18781da177e4SLinus Torvalds 
18794ac91378SJan Blunck 	mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
1880c663e5d8SChristoph Hellwig 	/*
1881c663e5d8SChristoph Hellwig 	 * Yucky last component or no last component at all?
1882c663e5d8SChristoph Hellwig 	 * (foo/., foo/.., /////)
1883c663e5d8SChristoph Hellwig 	 */
18841da177e4SLinus Torvalds 	if (nd->last_type != LAST_NORM)
18851da177e4SLinus Torvalds 		goto fail;
18861da177e4SLinus Torvalds 	nd->flags &= ~LOOKUP_PARENT;
18873516586aSAl Viro 	nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
1888a634904aSASANO Masahiro 	nd->intent.open.flags = O_EXCL;
1889c663e5d8SChristoph Hellwig 
1890c663e5d8SChristoph Hellwig 	/*
1891c663e5d8SChristoph Hellwig 	 * Do the final lookup.
1892c663e5d8SChristoph Hellwig 	 */
189349705b77SChristoph Hellwig 	dentry = lookup_hash(nd);
18941da177e4SLinus Torvalds 	if (IS_ERR(dentry))
18951da177e4SLinus Torvalds 		goto fail;
1896c663e5d8SChristoph Hellwig 
1897e9baf6e5SAl Viro 	if (dentry->d_inode)
1898e9baf6e5SAl Viro 		goto eexist;
1899c663e5d8SChristoph Hellwig 	/*
1900c663e5d8SChristoph Hellwig 	 * Special case - lookup gave negative, but... we had foo/bar/
1901c663e5d8SChristoph Hellwig 	 * From the vfs_mknod() POV we just have a negative dentry -
1902c663e5d8SChristoph Hellwig 	 * all is fine. Let's be bastards - you had / on the end, you've
1903c663e5d8SChristoph Hellwig 	 * been asking for (non-existent) directory. -ENOENT for you.
1904c663e5d8SChristoph Hellwig 	 */
1905e9baf6e5SAl Viro 	if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
19061da177e4SLinus Torvalds 		dput(dentry);
19071da177e4SLinus Torvalds 		dentry = ERR_PTR(-ENOENT);
1908e9baf6e5SAl Viro 	}
1909e9baf6e5SAl Viro 	return dentry;
1910e9baf6e5SAl Viro eexist:
1911e9baf6e5SAl Viro 	dput(dentry);
1912e9baf6e5SAl Viro 	dentry = ERR_PTR(-EEXIST);
19131da177e4SLinus Torvalds fail:
19141da177e4SLinus Torvalds 	return dentry;
19151da177e4SLinus Torvalds }
1916f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create);
19171da177e4SLinus Torvalds 
19181da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
19191da177e4SLinus Torvalds {
1920a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
19211da177e4SLinus Torvalds 
19221da177e4SLinus Torvalds 	if (error)
19231da177e4SLinus Torvalds 		return error;
19241da177e4SLinus Torvalds 
19251da177e4SLinus Torvalds 	if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
19261da177e4SLinus Torvalds 		return -EPERM;
19271da177e4SLinus Torvalds 
1928acfa4380SAl Viro 	if (!dir->i_op->mknod)
19291da177e4SLinus Torvalds 		return -EPERM;
19301da177e4SLinus Torvalds 
193108ce5f16SSerge E. Hallyn 	error = devcgroup_inode_mknod(mode, dev);
193208ce5f16SSerge E. Hallyn 	if (error)
193308ce5f16SSerge E. Hallyn 		return error;
193408ce5f16SSerge E. Hallyn 
19351da177e4SLinus Torvalds 	error = security_inode_mknod(dir, dentry, mode, dev);
19361da177e4SLinus Torvalds 	if (error)
19371da177e4SLinus Torvalds 		return error;
19381da177e4SLinus Torvalds 
19391da177e4SLinus Torvalds 	DQUOT_INIT(dir);
19401da177e4SLinus Torvalds 	error = dir->i_op->mknod(dir, dentry, mode, dev);
1941a74574aaSStephen Smalley 	if (!error)
1942f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
19431da177e4SLinus Torvalds 	return error;
19441da177e4SLinus Torvalds }
19451da177e4SLinus Torvalds 
1946463c3197SDave Hansen static int may_mknod(mode_t mode)
1947463c3197SDave Hansen {
1948463c3197SDave Hansen 	switch (mode & S_IFMT) {
1949463c3197SDave Hansen 	case S_IFREG:
1950463c3197SDave Hansen 	case S_IFCHR:
1951463c3197SDave Hansen 	case S_IFBLK:
1952463c3197SDave Hansen 	case S_IFIFO:
1953463c3197SDave Hansen 	case S_IFSOCK:
1954463c3197SDave Hansen 	case 0: /* zero mode translates to S_IFREG */
1955463c3197SDave Hansen 		return 0;
1956463c3197SDave Hansen 	case S_IFDIR:
1957463c3197SDave Hansen 		return -EPERM;
1958463c3197SDave Hansen 	default:
1959463c3197SDave Hansen 		return -EINVAL;
1960463c3197SDave Hansen 	}
1961463c3197SDave Hansen }
1962463c3197SDave Hansen 
19632e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
19642e4d0924SHeiko Carstens 		unsigned, dev)
19651da177e4SLinus Torvalds {
19662ad94ae6SAl Viro 	int error;
19671da177e4SLinus Torvalds 	char *tmp;
19681da177e4SLinus Torvalds 	struct dentry *dentry;
19691da177e4SLinus Torvalds 	struct nameidata nd;
19701da177e4SLinus Torvalds 
19711da177e4SLinus Torvalds 	if (S_ISDIR(mode))
19721da177e4SLinus Torvalds 		return -EPERM;
19731da177e4SLinus Torvalds 
19742ad94ae6SAl Viro 	error = user_path_parent(dfd, filename, &nd, &tmp);
19751da177e4SLinus Torvalds 	if (error)
19762ad94ae6SAl Viro 		return error;
19772ad94ae6SAl Viro 
19781da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
1979463c3197SDave Hansen 	if (IS_ERR(dentry)) {
19801da177e4SLinus Torvalds 		error = PTR_ERR(dentry);
1981463c3197SDave Hansen 		goto out_unlock;
1982463c3197SDave Hansen 	}
19834ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
19841da177e4SLinus Torvalds 		mode &= ~current->fs->umask;
1985463c3197SDave Hansen 	error = may_mknod(mode);
1986463c3197SDave Hansen 	if (error)
1987463c3197SDave Hansen 		goto out_dput;
1988463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
1989463c3197SDave Hansen 	if (error)
1990463c3197SDave Hansen 		goto out_dput;
1991be6d3e56SKentaro Takeda 	error = security_path_mknod(&nd.path, dentry, mode, dev);
1992be6d3e56SKentaro Takeda 	if (error)
1993be6d3e56SKentaro Takeda 		goto out_drop_write;
19941da177e4SLinus Torvalds 	switch (mode & S_IFMT) {
19951da177e4SLinus Torvalds 		case 0: case S_IFREG:
19964ac91378SJan Blunck 			error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
19971da177e4SLinus Torvalds 			break;
19981da177e4SLinus Torvalds 		case S_IFCHR: case S_IFBLK:
19994ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
20001da177e4SLinus Torvalds 					new_decode_dev(dev));
20011da177e4SLinus Torvalds 			break;
20021da177e4SLinus Torvalds 		case S_IFIFO: case S_IFSOCK:
20034ac91378SJan Blunck 			error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
20041da177e4SLinus Torvalds 			break;
20051da177e4SLinus Torvalds 	}
2006be6d3e56SKentaro Takeda out_drop_write:
2007463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2008463c3197SDave Hansen out_dput:
20091da177e4SLinus Torvalds 	dput(dentry);
2010463c3197SDave Hansen out_unlock:
20114ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
20121d957f9bSJan Blunck 	path_put(&nd.path);
20131da177e4SLinus Torvalds 	putname(tmp);
20141da177e4SLinus Torvalds 
20151da177e4SLinus Torvalds 	return error;
20161da177e4SLinus Torvalds }
20171da177e4SLinus Torvalds 
20183480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
20195590ff0dSUlrich Drepper {
20205590ff0dSUlrich Drepper 	return sys_mknodat(AT_FDCWD, filename, mode, dev);
20215590ff0dSUlrich Drepper }
20225590ff0dSUlrich Drepper 
20231da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
20241da177e4SLinus Torvalds {
2025a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
20261da177e4SLinus Torvalds 
20271da177e4SLinus Torvalds 	if (error)
20281da177e4SLinus Torvalds 		return error;
20291da177e4SLinus Torvalds 
2030acfa4380SAl Viro 	if (!dir->i_op->mkdir)
20311da177e4SLinus Torvalds 		return -EPERM;
20321da177e4SLinus Torvalds 
20331da177e4SLinus Torvalds 	mode &= (S_IRWXUGO|S_ISVTX);
20341da177e4SLinus Torvalds 	error = security_inode_mkdir(dir, dentry, mode);
20351da177e4SLinus Torvalds 	if (error)
20361da177e4SLinus Torvalds 		return error;
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds 	DQUOT_INIT(dir);
20391da177e4SLinus Torvalds 	error = dir->i_op->mkdir(dir, dentry, mode);
2040a74574aaSStephen Smalley 	if (!error)
2041f38aa942SAmy Griffis 		fsnotify_mkdir(dir, dentry);
20421da177e4SLinus Torvalds 	return error;
20431da177e4SLinus Torvalds }
20441da177e4SLinus Torvalds 
20452e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
20461da177e4SLinus Torvalds {
20471da177e4SLinus Torvalds 	int error = 0;
20481da177e4SLinus Torvalds 	char * tmp;
20496902d925SDave Hansen 	struct dentry *dentry;
20506902d925SDave Hansen 	struct nameidata nd;
20511da177e4SLinus Torvalds 
20522ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &tmp);
20532ad94ae6SAl Viro 	if (error)
20546902d925SDave Hansen 		goto out_err;
20551da177e4SLinus Torvalds 
20561da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 1);
20571da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
20586902d925SDave Hansen 	if (IS_ERR(dentry))
20596902d925SDave Hansen 		goto out_unlock;
20606902d925SDave Hansen 
20614ac91378SJan Blunck 	if (!IS_POSIXACL(nd.path.dentry->d_inode))
20621da177e4SLinus Torvalds 		mode &= ~current->fs->umask;
2063463c3197SDave Hansen 	error = mnt_want_write(nd.path.mnt);
2064463c3197SDave Hansen 	if (error)
2065463c3197SDave Hansen 		goto out_dput;
2066be6d3e56SKentaro Takeda 	error = security_path_mkdir(&nd.path, dentry, mode);
2067be6d3e56SKentaro Takeda 	if (error)
2068be6d3e56SKentaro Takeda 		goto out_drop_write;
20694ac91378SJan Blunck 	error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
2070be6d3e56SKentaro Takeda out_drop_write:
2071463c3197SDave Hansen 	mnt_drop_write(nd.path.mnt);
2072463c3197SDave Hansen out_dput:
20731da177e4SLinus Torvalds 	dput(dentry);
20746902d925SDave Hansen out_unlock:
20754ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
20761d957f9bSJan Blunck 	path_put(&nd.path);
20771da177e4SLinus Torvalds 	putname(tmp);
20786902d925SDave Hansen out_err:
20791da177e4SLinus Torvalds 	return error;
20801da177e4SLinus Torvalds }
20811da177e4SLinus Torvalds 
20823cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
20835590ff0dSUlrich Drepper {
20845590ff0dSUlrich Drepper 	return sys_mkdirat(AT_FDCWD, pathname, mode);
20855590ff0dSUlrich Drepper }
20865590ff0dSUlrich Drepper 
20871da177e4SLinus Torvalds /*
20881da177e4SLinus Torvalds  * We try to drop the dentry early: we should have
20891da177e4SLinus Torvalds  * a usage count of 2 if we're the only user of this
20901da177e4SLinus Torvalds  * dentry, and if that is true (possibly after pruning
20911da177e4SLinus Torvalds  * the dcache), then we drop the dentry now.
20921da177e4SLinus Torvalds  *
20931da177e4SLinus Torvalds  * A low-level filesystem can, if it choses, legally
20941da177e4SLinus Torvalds  * do a
20951da177e4SLinus Torvalds  *
20961da177e4SLinus Torvalds  *	if (!d_unhashed(dentry))
20971da177e4SLinus Torvalds  *		return -EBUSY;
20981da177e4SLinus Torvalds  *
20991da177e4SLinus Torvalds  * if it cannot handle the case of removing a directory
21001da177e4SLinus Torvalds  * that is still in use by something else..
21011da177e4SLinus Torvalds  */
21021da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry)
21031da177e4SLinus Torvalds {
21041da177e4SLinus Torvalds 	dget(dentry);
21051da177e4SLinus Torvalds 	shrink_dcache_parent(dentry);
21061da177e4SLinus Torvalds 	spin_lock(&dcache_lock);
21071da177e4SLinus Torvalds 	spin_lock(&dentry->d_lock);
21081da177e4SLinus Torvalds 	if (atomic_read(&dentry->d_count) == 2)
21091da177e4SLinus Torvalds 		__d_drop(dentry);
21101da177e4SLinus Torvalds 	spin_unlock(&dentry->d_lock);
21111da177e4SLinus Torvalds 	spin_unlock(&dcache_lock);
21121da177e4SLinus Torvalds }
21131da177e4SLinus Torvalds 
21141da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry)
21151da177e4SLinus Torvalds {
21161da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 1);
21171da177e4SLinus Torvalds 
21181da177e4SLinus Torvalds 	if (error)
21191da177e4SLinus Torvalds 		return error;
21201da177e4SLinus Torvalds 
2121acfa4380SAl Viro 	if (!dir->i_op->rmdir)
21221da177e4SLinus Torvalds 		return -EPERM;
21231da177e4SLinus Torvalds 
21241da177e4SLinus Torvalds 	DQUOT_INIT(dir);
21251da177e4SLinus Torvalds 
21261b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
21271da177e4SLinus Torvalds 	dentry_unhash(dentry);
21281da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
21291da177e4SLinus Torvalds 		error = -EBUSY;
21301da177e4SLinus Torvalds 	else {
21311da177e4SLinus Torvalds 		error = security_inode_rmdir(dir, dentry);
21321da177e4SLinus Torvalds 		if (!error) {
21331da177e4SLinus Torvalds 			error = dir->i_op->rmdir(dir, dentry);
21341da177e4SLinus Torvalds 			if (!error)
21351da177e4SLinus Torvalds 				dentry->d_inode->i_flags |= S_DEAD;
21361da177e4SLinus Torvalds 		}
21371da177e4SLinus Torvalds 	}
21381b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
21391da177e4SLinus Torvalds 	if (!error) {
21401da177e4SLinus Torvalds 		d_delete(dentry);
21411da177e4SLinus Torvalds 	}
21421da177e4SLinus Torvalds 	dput(dentry);
21431da177e4SLinus Torvalds 
21441da177e4SLinus Torvalds 	return error;
21451da177e4SLinus Torvalds }
21461da177e4SLinus Torvalds 
21475590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname)
21481da177e4SLinus Torvalds {
21491da177e4SLinus Torvalds 	int error = 0;
21501da177e4SLinus Torvalds 	char * name;
21511da177e4SLinus Torvalds 	struct dentry *dentry;
21521da177e4SLinus Torvalds 	struct nameidata nd;
21531da177e4SLinus Torvalds 
21542ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
21551da177e4SLinus Torvalds 	if (error)
21562ad94ae6SAl Viro 		return error;
21571da177e4SLinus Torvalds 
21581da177e4SLinus Torvalds 	switch(nd.last_type) {
21591da177e4SLinus Torvalds 	case LAST_DOTDOT:
21601da177e4SLinus Torvalds 		error = -ENOTEMPTY;
21611da177e4SLinus Torvalds 		goto exit1;
21621da177e4SLinus Torvalds 	case LAST_DOT:
21631da177e4SLinus Torvalds 		error = -EINVAL;
21641da177e4SLinus Torvalds 		goto exit1;
21651da177e4SLinus Torvalds 	case LAST_ROOT:
21661da177e4SLinus Torvalds 		error = -EBUSY;
21671da177e4SLinus Torvalds 		goto exit1;
21681da177e4SLinus Torvalds 	}
21690612d9fbSOGAWA Hirofumi 
21700612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
21710612d9fbSOGAWA Hirofumi 
21724ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
217349705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
21741da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
21756902d925SDave Hansen 	if (IS_ERR(dentry))
21766902d925SDave Hansen 		goto exit2;
21770622753bSDave Hansen 	error = mnt_want_write(nd.path.mnt);
21780622753bSDave Hansen 	if (error)
21790622753bSDave Hansen 		goto exit3;
2180be6d3e56SKentaro Takeda 	error = security_path_rmdir(&nd.path, dentry);
2181be6d3e56SKentaro Takeda 	if (error)
2182be6d3e56SKentaro Takeda 		goto exit4;
21834ac91378SJan Blunck 	error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
2184be6d3e56SKentaro Takeda exit4:
21850622753bSDave Hansen 	mnt_drop_write(nd.path.mnt);
21860622753bSDave Hansen exit3:
21871da177e4SLinus Torvalds 	dput(dentry);
21886902d925SDave Hansen exit2:
21894ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
21901da177e4SLinus Torvalds exit1:
21911d957f9bSJan Blunck 	path_put(&nd.path);
21921da177e4SLinus Torvalds 	putname(name);
21931da177e4SLinus Torvalds 	return error;
21941da177e4SLinus Torvalds }
21951da177e4SLinus Torvalds 
21963cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
21975590ff0dSUlrich Drepper {
21985590ff0dSUlrich Drepper 	return do_rmdir(AT_FDCWD, pathname);
21995590ff0dSUlrich Drepper }
22005590ff0dSUlrich Drepper 
22011da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry)
22021da177e4SLinus Torvalds {
22031da177e4SLinus Torvalds 	int error = may_delete(dir, dentry, 0);
22041da177e4SLinus Torvalds 
22051da177e4SLinus Torvalds 	if (error)
22061da177e4SLinus Torvalds 		return error;
22071da177e4SLinus Torvalds 
2208acfa4380SAl Viro 	if (!dir->i_op->unlink)
22091da177e4SLinus Torvalds 		return -EPERM;
22101da177e4SLinus Torvalds 
22111da177e4SLinus Torvalds 	DQUOT_INIT(dir);
22121da177e4SLinus Torvalds 
22131b1dcc1bSJes Sorensen 	mutex_lock(&dentry->d_inode->i_mutex);
22141da177e4SLinus Torvalds 	if (d_mountpoint(dentry))
22151da177e4SLinus Torvalds 		error = -EBUSY;
22161da177e4SLinus Torvalds 	else {
22171da177e4SLinus Torvalds 		error = security_inode_unlink(dir, dentry);
22181da177e4SLinus Torvalds 		if (!error)
22191da177e4SLinus Torvalds 			error = dir->i_op->unlink(dir, dentry);
22201da177e4SLinus Torvalds 	}
22211b1dcc1bSJes Sorensen 	mutex_unlock(&dentry->d_inode->i_mutex);
22221da177e4SLinus Torvalds 
22231da177e4SLinus Torvalds 	/* We don't d_delete() NFS sillyrenamed files--they still exist. */
22241da177e4SLinus Torvalds 	if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
2225ece95912SJan Kara 		fsnotify_link_count(dentry->d_inode);
22261da177e4SLinus Torvalds 		d_delete(dentry);
22271da177e4SLinus Torvalds 	}
22280eeca283SRobert Love 
22291da177e4SLinus Torvalds 	return error;
22301da177e4SLinus Torvalds }
22311da177e4SLinus Torvalds 
22321da177e4SLinus Torvalds /*
22331da177e4SLinus Torvalds  * Make sure that the actual truncation of the file will occur outside its
22341b1dcc1bSJes Sorensen  * directory's i_mutex.  Truncate can take a long time if there is a lot of
22351da177e4SLinus Torvalds  * writeout happening, and we don't want to prevent access to the directory
22361da177e4SLinus Torvalds  * while waiting on the I/O.
22371da177e4SLinus Torvalds  */
22385590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname)
22391da177e4SLinus Torvalds {
22402ad94ae6SAl Viro 	int error;
22411da177e4SLinus Torvalds 	char *name;
22421da177e4SLinus Torvalds 	struct dentry *dentry;
22431da177e4SLinus Torvalds 	struct nameidata nd;
22441da177e4SLinus Torvalds 	struct inode *inode = NULL;
22451da177e4SLinus Torvalds 
22462ad94ae6SAl Viro 	error = user_path_parent(dfd, pathname, &nd, &name);
22471da177e4SLinus Torvalds 	if (error)
22482ad94ae6SAl Viro 		return error;
22492ad94ae6SAl Viro 
22501da177e4SLinus Torvalds 	error = -EISDIR;
22511da177e4SLinus Torvalds 	if (nd.last_type != LAST_NORM)
22521da177e4SLinus Torvalds 		goto exit1;
22530612d9fbSOGAWA Hirofumi 
22540612d9fbSOGAWA Hirofumi 	nd.flags &= ~LOOKUP_PARENT;
22550612d9fbSOGAWA Hirofumi 
22564ac91378SJan Blunck 	mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
225749705b77SChristoph Hellwig 	dentry = lookup_hash(&nd);
22581da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
22591da177e4SLinus Torvalds 	if (!IS_ERR(dentry)) {
22601da177e4SLinus Torvalds 		/* Why not before? Because we want correct error value */
22611da177e4SLinus Torvalds 		if (nd.last.name[nd.last.len])
22621da177e4SLinus Torvalds 			goto slashes;
22631da177e4SLinus Torvalds 		inode = dentry->d_inode;
22641da177e4SLinus Torvalds 		if (inode)
22651da177e4SLinus Torvalds 			atomic_inc(&inode->i_count);
22660622753bSDave Hansen 		error = mnt_want_write(nd.path.mnt);
22670622753bSDave Hansen 		if (error)
22680622753bSDave Hansen 			goto exit2;
2269be6d3e56SKentaro Takeda 		error = security_path_unlink(&nd.path, dentry);
2270be6d3e56SKentaro Takeda 		if (error)
2271be6d3e56SKentaro Takeda 			goto exit3;
22724ac91378SJan Blunck 		error = vfs_unlink(nd.path.dentry->d_inode, dentry);
2273be6d3e56SKentaro Takeda exit3:
22740622753bSDave Hansen 		mnt_drop_write(nd.path.mnt);
22751da177e4SLinus Torvalds 	exit2:
22761da177e4SLinus Torvalds 		dput(dentry);
22771da177e4SLinus Torvalds 	}
22784ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
22791da177e4SLinus Torvalds 	if (inode)
22801da177e4SLinus Torvalds 		iput(inode);	/* truncate the inode here */
22811da177e4SLinus Torvalds exit1:
22821d957f9bSJan Blunck 	path_put(&nd.path);
22831da177e4SLinus Torvalds 	putname(name);
22841da177e4SLinus Torvalds 	return error;
22851da177e4SLinus Torvalds 
22861da177e4SLinus Torvalds slashes:
22871da177e4SLinus Torvalds 	error = !dentry->d_inode ? -ENOENT :
22881da177e4SLinus Torvalds 		S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
22891da177e4SLinus Torvalds 	goto exit2;
22901da177e4SLinus Torvalds }
22911da177e4SLinus Torvalds 
22922e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
22935590ff0dSUlrich Drepper {
22945590ff0dSUlrich Drepper 	if ((flag & ~AT_REMOVEDIR) != 0)
22955590ff0dSUlrich Drepper 		return -EINVAL;
22965590ff0dSUlrich Drepper 
22975590ff0dSUlrich Drepper 	if (flag & AT_REMOVEDIR)
22985590ff0dSUlrich Drepper 		return do_rmdir(dfd, pathname);
22995590ff0dSUlrich Drepper 
23005590ff0dSUlrich Drepper 	return do_unlinkat(dfd, pathname);
23015590ff0dSUlrich Drepper }
23025590ff0dSUlrich Drepper 
23033480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname)
23045590ff0dSUlrich Drepper {
23055590ff0dSUlrich Drepper 	return do_unlinkat(AT_FDCWD, pathname);
23065590ff0dSUlrich Drepper }
23075590ff0dSUlrich Drepper 
2308db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
23091da177e4SLinus Torvalds {
2310a95164d9SMiklos Szeredi 	int error = may_create(dir, dentry);
23111da177e4SLinus Torvalds 
23121da177e4SLinus Torvalds 	if (error)
23131da177e4SLinus Torvalds 		return error;
23141da177e4SLinus Torvalds 
2315acfa4380SAl Viro 	if (!dir->i_op->symlink)
23161da177e4SLinus Torvalds 		return -EPERM;
23171da177e4SLinus Torvalds 
23181da177e4SLinus Torvalds 	error = security_inode_symlink(dir, dentry, oldname);
23191da177e4SLinus Torvalds 	if (error)
23201da177e4SLinus Torvalds 		return error;
23211da177e4SLinus Torvalds 
23221da177e4SLinus Torvalds 	DQUOT_INIT(dir);
23231da177e4SLinus Torvalds 	error = dir->i_op->symlink(dir, dentry, oldname);
2324a74574aaSStephen Smalley 	if (!error)
2325f38aa942SAmy Griffis 		fsnotify_create(dir, dentry);
23261da177e4SLinus Torvalds 	return error;
23271da177e4SLinus Torvalds }
23281da177e4SLinus Torvalds 
23292e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
23302e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
23311da177e4SLinus Torvalds {
23322ad94ae6SAl Viro 	int error;
23331da177e4SLinus Torvalds 	char *from;
23341da177e4SLinus Torvalds 	char *to;
23356902d925SDave Hansen 	struct dentry *dentry;
23366902d925SDave Hansen 	struct nameidata nd;
23371da177e4SLinus Torvalds 
23381da177e4SLinus Torvalds 	from = getname(oldname);
23391da177e4SLinus Torvalds 	if (IS_ERR(from))
23401da177e4SLinus Torvalds 		return PTR_ERR(from);
23412ad94ae6SAl Viro 
23422ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
23432ad94ae6SAl Viro 	if (error)
23446902d925SDave Hansen 		goto out_putname;
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds 	dentry = lookup_create(&nd, 0);
23471da177e4SLinus Torvalds 	error = PTR_ERR(dentry);
23486902d925SDave Hansen 	if (IS_ERR(dentry))
23496902d925SDave Hansen 		goto out_unlock;
23506902d925SDave Hansen 
235175c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
235275c3f29dSDave Hansen 	if (error)
235375c3f29dSDave Hansen 		goto out_dput;
2354be6d3e56SKentaro Takeda 	error = security_path_symlink(&nd.path, dentry, from);
2355be6d3e56SKentaro Takeda 	if (error)
2356be6d3e56SKentaro Takeda 		goto out_drop_write;
2357db2e747bSMiklos Szeredi 	error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
2358be6d3e56SKentaro Takeda out_drop_write:
235975c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
236075c3f29dSDave Hansen out_dput:
23611da177e4SLinus Torvalds 	dput(dentry);
23626902d925SDave Hansen out_unlock:
23634ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
23641d957f9bSJan Blunck 	path_put(&nd.path);
23651da177e4SLinus Torvalds 	putname(to);
23666902d925SDave Hansen out_putname:
23671da177e4SLinus Torvalds 	putname(from);
23681da177e4SLinus Torvalds 	return error;
23691da177e4SLinus Torvalds }
23701da177e4SLinus Torvalds 
23713480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
23725590ff0dSUlrich Drepper {
23735590ff0dSUlrich Drepper 	return sys_symlinkat(oldname, AT_FDCWD, newname);
23745590ff0dSUlrich Drepper }
23755590ff0dSUlrich Drepper 
23761da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
23771da177e4SLinus Torvalds {
23781da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
23791da177e4SLinus Torvalds 	int error;
23801da177e4SLinus Torvalds 
23811da177e4SLinus Torvalds 	if (!inode)
23821da177e4SLinus Torvalds 		return -ENOENT;
23831da177e4SLinus Torvalds 
2384a95164d9SMiklos Szeredi 	error = may_create(dir, new_dentry);
23851da177e4SLinus Torvalds 	if (error)
23861da177e4SLinus Torvalds 		return error;
23871da177e4SLinus Torvalds 
23881da177e4SLinus Torvalds 	if (dir->i_sb != inode->i_sb)
23891da177e4SLinus Torvalds 		return -EXDEV;
23901da177e4SLinus Torvalds 
23911da177e4SLinus Torvalds 	/*
23921da177e4SLinus Torvalds 	 * A link to an append-only or immutable file cannot be created.
23931da177e4SLinus Torvalds 	 */
23941da177e4SLinus Torvalds 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
23951da177e4SLinus Torvalds 		return -EPERM;
2396acfa4380SAl Viro 	if (!dir->i_op->link)
23971da177e4SLinus Torvalds 		return -EPERM;
23987e79eedbSTetsuo Handa 	if (S_ISDIR(inode->i_mode))
23991da177e4SLinus Torvalds 		return -EPERM;
24001da177e4SLinus Torvalds 
24011da177e4SLinus Torvalds 	error = security_inode_link(old_dentry, dir, new_dentry);
24021da177e4SLinus Torvalds 	if (error)
24031da177e4SLinus Torvalds 		return error;
24041da177e4SLinus Torvalds 
24057e79eedbSTetsuo Handa 	mutex_lock(&inode->i_mutex);
24061da177e4SLinus Torvalds 	DQUOT_INIT(dir);
24071da177e4SLinus Torvalds 	error = dir->i_op->link(old_dentry, dir, new_dentry);
24087e79eedbSTetsuo Handa 	mutex_unlock(&inode->i_mutex);
2409e31e14ecSStephen Smalley 	if (!error)
24107e79eedbSTetsuo Handa 		fsnotify_link(dir, inode, new_dentry);
24111da177e4SLinus Torvalds 	return error;
24121da177e4SLinus Torvalds }
24131da177e4SLinus Torvalds 
24141da177e4SLinus Torvalds /*
24151da177e4SLinus Torvalds  * Hardlinks are often used in delicate situations.  We avoid
24161da177e4SLinus Torvalds  * security-related surprises by not following symlinks on the
24171da177e4SLinus Torvalds  * newname.  --KAB
24181da177e4SLinus Torvalds  *
24191da177e4SLinus Torvalds  * We don't follow them on the oldname either to be compatible
24201da177e4SLinus Torvalds  * with linux 2.0, and to avoid hard-linking to directories
24211da177e4SLinus Torvalds  * and other special files.  --ADM
24221da177e4SLinus Torvalds  */
24232e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
24242e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname, int, flags)
24251da177e4SLinus Torvalds {
24261da177e4SLinus Torvalds 	struct dentry *new_dentry;
24272d8f3038SAl Viro 	struct nameidata nd;
24282d8f3038SAl Viro 	struct path old_path;
24291da177e4SLinus Torvalds 	int error;
24301da177e4SLinus Torvalds 	char *to;
24311da177e4SLinus Torvalds 
243245c9b11aSUlrich Drepper 	if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
2433c04030e1SUlrich Drepper 		return -EINVAL;
2434c04030e1SUlrich Drepper 
24352d8f3038SAl Viro 	error = user_path_at(olddfd, oldname,
243645c9b11aSUlrich Drepper 			     flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
24372d8f3038SAl Viro 			     &old_path);
24381da177e4SLinus Torvalds 	if (error)
24392ad94ae6SAl Viro 		return error;
24402ad94ae6SAl Viro 
24412ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &nd, &to);
24421da177e4SLinus Torvalds 	if (error)
24431da177e4SLinus Torvalds 		goto out;
24441da177e4SLinus Torvalds 	error = -EXDEV;
24452d8f3038SAl Viro 	if (old_path.mnt != nd.path.mnt)
24461da177e4SLinus Torvalds 		goto out_release;
24471da177e4SLinus Torvalds 	new_dentry = lookup_create(&nd, 0);
24481da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
24496902d925SDave Hansen 	if (IS_ERR(new_dentry))
24506902d925SDave Hansen 		goto out_unlock;
245175c3f29dSDave Hansen 	error = mnt_want_write(nd.path.mnt);
245275c3f29dSDave Hansen 	if (error)
245375c3f29dSDave Hansen 		goto out_dput;
2454be6d3e56SKentaro Takeda 	error = security_path_link(old_path.dentry, &nd.path, new_dentry);
2455be6d3e56SKentaro Takeda 	if (error)
2456be6d3e56SKentaro Takeda 		goto out_drop_write;
24572d8f3038SAl Viro 	error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
2458be6d3e56SKentaro Takeda out_drop_write:
245975c3f29dSDave Hansen 	mnt_drop_write(nd.path.mnt);
246075c3f29dSDave Hansen out_dput:
24611da177e4SLinus Torvalds 	dput(new_dentry);
24626902d925SDave Hansen out_unlock:
24634ac91378SJan Blunck 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
24641da177e4SLinus Torvalds out_release:
24651d957f9bSJan Blunck 	path_put(&nd.path);
24662ad94ae6SAl Viro 	putname(to);
24671da177e4SLinus Torvalds out:
24682d8f3038SAl Viro 	path_put(&old_path);
24691da177e4SLinus Torvalds 
24701da177e4SLinus Torvalds 	return error;
24711da177e4SLinus Torvalds }
24721da177e4SLinus Torvalds 
24733480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
24745590ff0dSUlrich Drepper {
2475c04030e1SUlrich Drepper 	return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
24765590ff0dSUlrich Drepper }
24775590ff0dSUlrich Drepper 
24781da177e4SLinus Torvalds /*
24791da177e4SLinus Torvalds  * The worst of all namespace operations - renaming directory. "Perverted"
24801da177e4SLinus Torvalds  * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
24811da177e4SLinus Torvalds  * Problems:
24821da177e4SLinus Torvalds  *	a) we can get into loop creation. Check is done in is_subdir().
24831da177e4SLinus Torvalds  *	b) race potential - two innocent renames can create a loop together.
24841da177e4SLinus Torvalds  *	   That's where 4.4 screws up. Current fix: serialization on
2485a11f3a05SArjan van de Ven  *	   sb->s_vfs_rename_mutex. We might be more accurate, but that's another
24861da177e4SLinus Torvalds  *	   story.
24871da177e4SLinus Torvalds  *	c) we have to lock _three_ objects - parents and victim (if it exists).
24881b1dcc1bSJes Sorensen  *	   And that - after we got ->i_mutex on parents (until then we don't know
24891da177e4SLinus Torvalds  *	   whether the target exists).  Solution: try to be smart with locking
24901da177e4SLinus Torvalds  *	   order for inodes.  We rely on the fact that tree topology may change
2491a11f3a05SArjan van de Ven  *	   only under ->s_vfs_rename_mutex _and_ that parent of the object we
24921da177e4SLinus Torvalds  *	   move will be locked.  Thus we can rank directories by the tree
24931da177e4SLinus Torvalds  *	   (ancestors first) and rank all non-directories after them.
24941da177e4SLinus Torvalds  *	   That works since everybody except rename does "lock parent, lookup,
2495a11f3a05SArjan van de Ven  *	   lock child" and rename is under ->s_vfs_rename_mutex.
24961da177e4SLinus Torvalds  *	   HOWEVER, it relies on the assumption that any object with ->lookup()
24971da177e4SLinus Torvalds  *	   has no more than 1 dentry.  If "hybrid" objects will ever appear,
24981da177e4SLinus Torvalds  *	   we'd better make sure that there's no link(2) for them.
24991da177e4SLinus Torvalds  *	d) some filesystems don't support opened-but-unlinked directories,
25001da177e4SLinus Torvalds  *	   either because of layout or because they are not ready to deal with
25011da177e4SLinus Torvalds  *	   all cases correctly. The latter will be fixed (taking this sort of
25021da177e4SLinus Torvalds  *	   stuff into VFS), but the former is not going away. Solution: the same
25031da177e4SLinus Torvalds  *	   trick as in rmdir().
25041da177e4SLinus Torvalds  *	e) conversion from fhandle to dentry may come in the wrong moment - when
25051b1dcc1bSJes Sorensen  *	   we are removing the target. Solution: we will have to grab ->i_mutex
25061da177e4SLinus Torvalds  *	   in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
25071b1dcc1bSJes Sorensen  *	   ->i_mutex on parents, which works but leads to some truely excessive
25081da177e4SLinus Torvalds  *	   locking].
25091da177e4SLinus Torvalds  */
251075c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
25111da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
25121da177e4SLinus Torvalds {
25131da177e4SLinus Torvalds 	int error = 0;
25141da177e4SLinus Torvalds 	struct inode *target;
25151da177e4SLinus Torvalds 
25161da177e4SLinus Torvalds 	/*
25171da177e4SLinus Torvalds 	 * If we are going to change the parent - check write permissions,
25181da177e4SLinus Torvalds 	 * we'll need to flip '..'.
25191da177e4SLinus Torvalds 	 */
25201da177e4SLinus Torvalds 	if (new_dir != old_dir) {
2521f419a2e3SAl Viro 		error = inode_permission(old_dentry->d_inode, MAY_WRITE);
25221da177e4SLinus Torvalds 		if (error)
25231da177e4SLinus Torvalds 			return error;
25241da177e4SLinus Torvalds 	}
25251da177e4SLinus Torvalds 
25261da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
25271da177e4SLinus Torvalds 	if (error)
25281da177e4SLinus Torvalds 		return error;
25291da177e4SLinus Torvalds 
25301da177e4SLinus Torvalds 	target = new_dentry->d_inode;
25311da177e4SLinus Torvalds 	if (target) {
25321b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
25331da177e4SLinus Torvalds 		dentry_unhash(new_dentry);
25341da177e4SLinus Torvalds 	}
25351da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
25361da177e4SLinus Torvalds 		error = -EBUSY;
25371da177e4SLinus Torvalds 	else
25381da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
25391da177e4SLinus Torvalds 	if (target) {
25401da177e4SLinus Torvalds 		if (!error)
25411da177e4SLinus Torvalds 			target->i_flags |= S_DEAD;
25421b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
25431da177e4SLinus Torvalds 		if (d_unhashed(new_dentry))
25441da177e4SLinus Torvalds 			d_rehash(new_dentry);
25451da177e4SLinus Torvalds 		dput(new_dentry);
25461da177e4SLinus Torvalds 	}
2547e31e14ecSStephen Smalley 	if (!error)
2548349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
25491da177e4SLinus Torvalds 			d_move(old_dentry,new_dentry);
25501da177e4SLinus Torvalds 	return error;
25511da177e4SLinus Torvalds }
25521da177e4SLinus Torvalds 
255375c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
25541da177e4SLinus Torvalds 			    struct inode *new_dir, struct dentry *new_dentry)
25551da177e4SLinus Torvalds {
25561da177e4SLinus Torvalds 	struct inode *target;
25571da177e4SLinus Torvalds 	int error;
25581da177e4SLinus Torvalds 
25591da177e4SLinus Torvalds 	error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
25601da177e4SLinus Torvalds 	if (error)
25611da177e4SLinus Torvalds 		return error;
25621da177e4SLinus Torvalds 
25631da177e4SLinus Torvalds 	dget(new_dentry);
25641da177e4SLinus Torvalds 	target = new_dentry->d_inode;
25651da177e4SLinus Torvalds 	if (target)
25661b1dcc1bSJes Sorensen 		mutex_lock(&target->i_mutex);
25671da177e4SLinus Torvalds 	if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
25681da177e4SLinus Torvalds 		error = -EBUSY;
25691da177e4SLinus Torvalds 	else
25701da177e4SLinus Torvalds 		error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
25711da177e4SLinus Torvalds 	if (!error) {
2572349457ccSMark Fasheh 		if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
25731da177e4SLinus Torvalds 			d_move(old_dentry, new_dentry);
25741da177e4SLinus Torvalds 	}
25751da177e4SLinus Torvalds 	if (target)
25761b1dcc1bSJes Sorensen 		mutex_unlock(&target->i_mutex);
25771da177e4SLinus Torvalds 	dput(new_dentry);
25781da177e4SLinus Torvalds 	return error;
25791da177e4SLinus Torvalds }
25801da177e4SLinus Torvalds 
25811da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
25821da177e4SLinus Torvalds 	       struct inode *new_dir, struct dentry *new_dentry)
25831da177e4SLinus Torvalds {
25841da177e4SLinus Torvalds 	int error;
25851da177e4SLinus Torvalds 	int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
25860eeca283SRobert Love 	const char *old_name;
25871da177e4SLinus Torvalds 
25881da177e4SLinus Torvalds 	if (old_dentry->d_inode == new_dentry->d_inode)
25891da177e4SLinus Torvalds  		return 0;
25901da177e4SLinus Torvalds 
25911da177e4SLinus Torvalds 	error = may_delete(old_dir, old_dentry, is_dir);
25921da177e4SLinus Torvalds 	if (error)
25931da177e4SLinus Torvalds 		return error;
25941da177e4SLinus Torvalds 
25951da177e4SLinus Torvalds 	if (!new_dentry->d_inode)
2596a95164d9SMiklos Szeredi 		error = may_create(new_dir, new_dentry);
25971da177e4SLinus Torvalds 	else
25981da177e4SLinus Torvalds 		error = may_delete(new_dir, new_dentry, is_dir);
25991da177e4SLinus Torvalds 	if (error)
26001da177e4SLinus Torvalds 		return error;
26011da177e4SLinus Torvalds 
2602acfa4380SAl Viro 	if (!old_dir->i_op->rename)
26031da177e4SLinus Torvalds 		return -EPERM;
26041da177e4SLinus Torvalds 
26051da177e4SLinus Torvalds 	DQUOT_INIT(old_dir);
26061da177e4SLinus Torvalds 	DQUOT_INIT(new_dir);
26071da177e4SLinus Torvalds 
26080eeca283SRobert Love 	old_name = fsnotify_oldname_init(old_dentry->d_name.name);
26090eeca283SRobert Love 
26101da177e4SLinus Torvalds 	if (is_dir)
26111da177e4SLinus Torvalds 		error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
26121da177e4SLinus Torvalds 	else
26131da177e4SLinus Torvalds 		error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
26141da177e4SLinus Torvalds 	if (!error) {
26150eeca283SRobert Love 		const char *new_name = old_dentry->d_name.name;
261689204c40SJohn McCutchan 		fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
26175a190ae6SAl Viro 			      new_dentry->d_inode, old_dentry);
26181da177e4SLinus Torvalds 	}
26190eeca283SRobert Love 	fsnotify_oldname_free(old_name);
26200eeca283SRobert Love 
26211da177e4SLinus Torvalds 	return error;
26221da177e4SLinus Torvalds }
26231da177e4SLinus Torvalds 
26242e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
26252e4d0924SHeiko Carstens 		int, newdfd, const char __user *, newname)
26261da177e4SLinus Torvalds {
26271da177e4SLinus Torvalds 	struct dentry *old_dir, *new_dir;
26281da177e4SLinus Torvalds 	struct dentry *old_dentry, *new_dentry;
26291da177e4SLinus Torvalds 	struct dentry *trap;
26301da177e4SLinus Torvalds 	struct nameidata oldnd, newnd;
26312ad94ae6SAl Viro 	char *from;
26322ad94ae6SAl Viro 	char *to;
26332ad94ae6SAl Viro 	int error;
26341da177e4SLinus Torvalds 
26352ad94ae6SAl Viro 	error = user_path_parent(olddfd, oldname, &oldnd, &from);
26361da177e4SLinus Torvalds 	if (error)
26371da177e4SLinus Torvalds 		goto exit;
26381da177e4SLinus Torvalds 
26392ad94ae6SAl Viro 	error = user_path_parent(newdfd, newname, &newnd, &to);
26401da177e4SLinus Torvalds 	if (error)
26411da177e4SLinus Torvalds 		goto exit1;
26421da177e4SLinus Torvalds 
26431da177e4SLinus Torvalds 	error = -EXDEV;
26444ac91378SJan Blunck 	if (oldnd.path.mnt != newnd.path.mnt)
26451da177e4SLinus Torvalds 		goto exit2;
26461da177e4SLinus Torvalds 
26474ac91378SJan Blunck 	old_dir = oldnd.path.dentry;
26481da177e4SLinus Torvalds 	error = -EBUSY;
26491da177e4SLinus Torvalds 	if (oldnd.last_type != LAST_NORM)
26501da177e4SLinus Torvalds 		goto exit2;
26511da177e4SLinus Torvalds 
26524ac91378SJan Blunck 	new_dir = newnd.path.dentry;
26531da177e4SLinus Torvalds 	if (newnd.last_type != LAST_NORM)
26541da177e4SLinus Torvalds 		goto exit2;
26551da177e4SLinus Torvalds 
26560612d9fbSOGAWA Hirofumi 	oldnd.flags &= ~LOOKUP_PARENT;
26570612d9fbSOGAWA Hirofumi 	newnd.flags &= ~LOOKUP_PARENT;
26584e9ed2f8SOGAWA Hirofumi 	newnd.flags |= LOOKUP_RENAME_TARGET;
26590612d9fbSOGAWA Hirofumi 
26601da177e4SLinus Torvalds 	trap = lock_rename(new_dir, old_dir);
26611da177e4SLinus Torvalds 
266249705b77SChristoph Hellwig 	old_dentry = lookup_hash(&oldnd);
26631da177e4SLinus Torvalds 	error = PTR_ERR(old_dentry);
26641da177e4SLinus Torvalds 	if (IS_ERR(old_dentry))
26651da177e4SLinus Torvalds 		goto exit3;
26661da177e4SLinus Torvalds 	/* source must exist */
26671da177e4SLinus Torvalds 	error = -ENOENT;
26681da177e4SLinus Torvalds 	if (!old_dentry->d_inode)
26691da177e4SLinus Torvalds 		goto exit4;
26701da177e4SLinus Torvalds 	/* unless the source is a directory trailing slashes give -ENOTDIR */
26711da177e4SLinus Torvalds 	if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
26721da177e4SLinus Torvalds 		error = -ENOTDIR;
26731da177e4SLinus Torvalds 		if (oldnd.last.name[oldnd.last.len])
26741da177e4SLinus Torvalds 			goto exit4;
26751da177e4SLinus Torvalds 		if (newnd.last.name[newnd.last.len])
26761da177e4SLinus Torvalds 			goto exit4;
26771da177e4SLinus Torvalds 	}
26781da177e4SLinus Torvalds 	/* source should not be ancestor of target */
26791da177e4SLinus Torvalds 	error = -EINVAL;
26801da177e4SLinus Torvalds 	if (old_dentry == trap)
26811da177e4SLinus Torvalds 		goto exit4;
268249705b77SChristoph Hellwig 	new_dentry = lookup_hash(&newnd);
26831da177e4SLinus Torvalds 	error = PTR_ERR(new_dentry);
26841da177e4SLinus Torvalds 	if (IS_ERR(new_dentry))
26851da177e4SLinus Torvalds 		goto exit4;
26861da177e4SLinus Torvalds 	/* target should not be an ancestor of source */
26871da177e4SLinus Torvalds 	error = -ENOTEMPTY;
26881da177e4SLinus Torvalds 	if (new_dentry == trap)
26891da177e4SLinus Torvalds 		goto exit5;
26901da177e4SLinus Torvalds 
26919079b1ebSDave Hansen 	error = mnt_want_write(oldnd.path.mnt);
26929079b1ebSDave Hansen 	if (error)
26939079b1ebSDave Hansen 		goto exit5;
2694be6d3e56SKentaro Takeda 	error = security_path_rename(&oldnd.path, old_dentry,
2695be6d3e56SKentaro Takeda 				     &newnd.path, new_dentry);
2696be6d3e56SKentaro Takeda 	if (error)
2697be6d3e56SKentaro Takeda 		goto exit6;
26981da177e4SLinus Torvalds 	error = vfs_rename(old_dir->d_inode, old_dentry,
26991da177e4SLinus Torvalds 				   new_dir->d_inode, new_dentry);
2700be6d3e56SKentaro Takeda exit6:
27019079b1ebSDave Hansen 	mnt_drop_write(oldnd.path.mnt);
27021da177e4SLinus Torvalds exit5:
27031da177e4SLinus Torvalds 	dput(new_dentry);
27041da177e4SLinus Torvalds exit4:
27051da177e4SLinus Torvalds 	dput(old_dentry);
27061da177e4SLinus Torvalds exit3:
27071da177e4SLinus Torvalds 	unlock_rename(new_dir, old_dir);
27081da177e4SLinus Torvalds exit2:
27091d957f9bSJan Blunck 	path_put(&newnd.path);
27102ad94ae6SAl Viro 	putname(to);
27111da177e4SLinus Torvalds exit1:
27121d957f9bSJan Blunck 	path_put(&oldnd.path);
27131da177e4SLinus Torvalds 	putname(from);
27142ad94ae6SAl Viro exit:
27151da177e4SLinus Torvalds 	return error;
27161da177e4SLinus Torvalds }
27171da177e4SLinus Torvalds 
2718a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
27195590ff0dSUlrich Drepper {
27205590ff0dSUlrich Drepper 	return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
27215590ff0dSUlrich Drepper }
27225590ff0dSUlrich Drepper 
27231da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
27241da177e4SLinus Torvalds {
27251da177e4SLinus Torvalds 	int len;
27261da177e4SLinus Torvalds 
27271da177e4SLinus Torvalds 	len = PTR_ERR(link);
27281da177e4SLinus Torvalds 	if (IS_ERR(link))
27291da177e4SLinus Torvalds 		goto out;
27301da177e4SLinus Torvalds 
27311da177e4SLinus Torvalds 	len = strlen(link);
27321da177e4SLinus Torvalds 	if (len > (unsigned) buflen)
27331da177e4SLinus Torvalds 		len = buflen;
27341da177e4SLinus Torvalds 	if (copy_to_user(buffer, link, len))
27351da177e4SLinus Torvalds 		len = -EFAULT;
27361da177e4SLinus Torvalds out:
27371da177e4SLinus Torvalds 	return len;
27381da177e4SLinus Torvalds }
27391da177e4SLinus Torvalds 
27401da177e4SLinus Torvalds /*
27411da177e4SLinus Torvalds  * A helper for ->readlink().  This should be used *ONLY* for symlinks that
27421da177e4SLinus Torvalds  * have ->follow_link() touching nd only in nd_set_link().  Using (or not
27431da177e4SLinus Torvalds  * using) it for any given inode is up to filesystem.
27441da177e4SLinus Torvalds  */
27451da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
27461da177e4SLinus Torvalds {
27471da177e4SLinus Torvalds 	struct nameidata nd;
2748cc314eefSLinus Torvalds 	void *cookie;
2749694a1764SMarcin Slusarz 	int res;
2750cc314eefSLinus Torvalds 
27511da177e4SLinus Torvalds 	nd.depth = 0;
2752cc314eefSLinus Torvalds 	cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
2753694a1764SMarcin Slusarz 	if (IS_ERR(cookie))
2754694a1764SMarcin Slusarz 		return PTR_ERR(cookie);
2755694a1764SMarcin Slusarz 
2756694a1764SMarcin Slusarz 	res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
27571da177e4SLinus Torvalds 	if (dentry->d_inode->i_op->put_link)
2758cc314eefSLinus Torvalds 		dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2759694a1764SMarcin Slusarz 	return res;
27601da177e4SLinus Torvalds }
27611da177e4SLinus Torvalds 
27621da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link)
27631da177e4SLinus Torvalds {
27641da177e4SLinus Torvalds 	return __vfs_follow_link(nd, link);
27651da177e4SLinus Torvalds }
27661da177e4SLinus Torvalds 
27671da177e4SLinus Torvalds /* get the link contents into pagecache */
27681da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage)
27691da177e4SLinus Torvalds {
2770ebd09abbSDuane Griffin 	char *kaddr;
27711da177e4SLinus Torvalds 	struct page *page;
27721da177e4SLinus Torvalds 	struct address_space *mapping = dentry->d_inode->i_mapping;
2773090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, NULL);
27741da177e4SLinus Torvalds 	if (IS_ERR(page))
27756fe6900eSNick Piggin 		return (char*)page;
27761da177e4SLinus Torvalds 	*ppage = page;
2777ebd09abbSDuane Griffin 	kaddr = kmap(page);
2778ebd09abbSDuane Griffin 	nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
2779ebd09abbSDuane Griffin 	return kaddr;
27801da177e4SLinus Torvalds }
27811da177e4SLinus Torvalds 
27821da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
27831da177e4SLinus Torvalds {
27841da177e4SLinus Torvalds 	struct page *page = NULL;
27851da177e4SLinus Torvalds 	char *s = page_getlink(dentry, &page);
27861da177e4SLinus Torvalds 	int res = vfs_readlink(dentry,buffer,buflen,s);
27871da177e4SLinus Torvalds 	if (page) {
27881da177e4SLinus Torvalds 		kunmap(page);
27891da177e4SLinus Torvalds 		page_cache_release(page);
27901da177e4SLinus Torvalds 	}
27911da177e4SLinus Torvalds 	return res;
27921da177e4SLinus Torvalds }
27931da177e4SLinus Torvalds 
2794cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
27951da177e4SLinus Torvalds {
2796cc314eefSLinus Torvalds 	struct page *page = NULL;
27971da177e4SLinus Torvalds 	nd_set_link(nd, page_getlink(dentry, &page));
2798cc314eefSLinus Torvalds 	return page;
27991da177e4SLinus Torvalds }
28001da177e4SLinus Torvalds 
2801cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
28021da177e4SLinus Torvalds {
2803cc314eefSLinus Torvalds 	struct page *page = cookie;
2804cc314eefSLinus Torvalds 
2805cc314eefSLinus Torvalds 	if (page) {
28061da177e4SLinus Torvalds 		kunmap(page);
28071da177e4SLinus Torvalds 		page_cache_release(page);
28081da177e4SLinus Torvalds 	}
28091da177e4SLinus Torvalds }
28101da177e4SLinus Torvalds 
281154566b2cSNick Piggin /*
281254566b2cSNick Piggin  * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
281354566b2cSNick Piggin  */
281454566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
28151da177e4SLinus Torvalds {
28161da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
28170adb25d2SKirill Korotaev 	struct page *page;
2818afddba49SNick Piggin 	void *fsdata;
2819beb497abSDmitriy Monakhov 	int err;
28201da177e4SLinus Torvalds 	char *kaddr;
282154566b2cSNick Piggin 	unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
282254566b2cSNick Piggin 	if (nofs)
282354566b2cSNick Piggin 		flags |= AOP_FLAG_NOFS;
28241da177e4SLinus Torvalds 
28257e53cac4SNeilBrown retry:
2826afddba49SNick Piggin 	err = pagecache_write_begin(NULL, mapping, 0, len-1,
282754566b2cSNick Piggin 				flags, &page, &fsdata);
28281da177e4SLinus Torvalds 	if (err)
2829afddba49SNick Piggin 		goto fail;
2830afddba49SNick Piggin 
28311da177e4SLinus Torvalds 	kaddr = kmap_atomic(page, KM_USER0);
28321da177e4SLinus Torvalds 	memcpy(kaddr, symname, len-1);
28331da177e4SLinus Torvalds 	kunmap_atomic(kaddr, KM_USER0);
2834afddba49SNick Piggin 
2835afddba49SNick Piggin 	err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
2836afddba49SNick Piggin 							page, fsdata);
28371da177e4SLinus Torvalds 	if (err < 0)
28381da177e4SLinus Torvalds 		goto fail;
2839afddba49SNick Piggin 	if (err < len-1)
2840afddba49SNick Piggin 		goto retry;
2841afddba49SNick Piggin 
28421da177e4SLinus Torvalds 	mark_inode_dirty(inode);
28431da177e4SLinus Torvalds 	return 0;
28441da177e4SLinus Torvalds fail:
28451da177e4SLinus Torvalds 	return err;
28461da177e4SLinus Torvalds }
28471da177e4SLinus Torvalds 
28480adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len)
28490adb25d2SKirill Korotaev {
28500adb25d2SKirill Korotaev 	return __page_symlink(inode, symname, len,
285154566b2cSNick Piggin 			!(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
28520adb25d2SKirill Korotaev }
28530adb25d2SKirill Korotaev 
285492e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = {
28551da177e4SLinus Torvalds 	.readlink	= generic_readlink,
28561da177e4SLinus Torvalds 	.follow_link	= page_follow_link_light,
28571da177e4SLinus Torvalds 	.put_link	= page_put_link,
28581da177e4SLinus Torvalds };
28591da177e4SLinus Torvalds 
28602d8f3038SAl Viro EXPORT_SYMBOL(user_path_at);
28611da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down);
28621da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up);
28631da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
28641da177e4SLinus Torvalds EXPORT_SYMBOL(getname);
28651da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename);
28661da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len);
28671da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light);
28681da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link);
28691da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink);
28700adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink);
28711da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink);
28721da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations);
28731da177e4SLinus Torvalds EXPORT_SYMBOL(path_lookup);
2874d1811465SAl Viro EXPORT_SYMBOL(kern_path);
287516f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup);
2876f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission);
28778c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission);
28781da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename);
28791da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create);
28801da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link);
28811da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link);
28821da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir);
28831da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod);
28841da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission);
28851da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink);
28861da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename);
28871da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir);
28881da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink);
28891da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink);
28901da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash);
28911da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink);
289218d8fda7SAl Viro 
289318d8fda7SAl Viro /* to be mentioned only in INIT_TASK */
289418d8fda7SAl Viro struct fs_struct init_fs = {
289518d8fda7SAl Viro 	.count		= ATOMIC_INIT(1),
28961239f26cSSteven Rostedt 	.lock		= __RW_LOCK_UNLOCKED(init_fs.lock),
289718d8fda7SAl Viro 	.umask		= 0022,
289818d8fda7SAl Viro };
2899