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/pagemap.h> 230eeca283SRobert Love #include <linux/fsnotify.h> 241da177e4SLinus Torvalds #include <linux/personality.h> 251da177e4SLinus Torvalds #include <linux/security.h> 266146f0d5SMimi Zohar #include <linux/ima.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> 345ad4e53bSAl Viro #include <linux/fs_struct.h> 351da177e4SLinus Torvalds #include <asm/uaccess.h> 361da177e4SLinus Torvalds 37e81e3f4dSEric Paris #include "internal.h" 38e81e3f4dSEric Paris 391da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer] 401da177e4SLinus Torvalds * Fundamental changes in the pathname lookup mechanisms (namei) 411da177e4SLinus Torvalds * were necessary because of omirr. The reason is that omirr needs 421da177e4SLinus Torvalds * to know the _real_ pathname, not the user-supplied one, in case 431da177e4SLinus Torvalds * of symlinks (and also when transname replacements occur). 441da177e4SLinus Torvalds * 451da177e4SLinus Torvalds * The new code replaces the old recursive symlink resolution with 461da177e4SLinus Torvalds * an iterative one (in case of non-nested symlink chains). It does 471da177e4SLinus Torvalds * this with calls to <fs>_follow_link(). 481da177e4SLinus Torvalds * As a side effect, dir_namei(), _namei() and follow_link() are now 491da177e4SLinus Torvalds * replaced with a single function lookup_dentry() that can handle all 501da177e4SLinus Torvalds * the special cases of the former code. 511da177e4SLinus Torvalds * 521da177e4SLinus Torvalds * With the new dcache, the pathname is stored at each inode, at least as 531da177e4SLinus Torvalds * long as the refcount of the inode is positive. As a side effect, the 541da177e4SLinus Torvalds * size of the dcache depends on the inode cache and thus is dynamic. 551da177e4SLinus Torvalds * 561da177e4SLinus Torvalds * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink 571da177e4SLinus Torvalds * resolution to correspond with current state of the code. 581da177e4SLinus Torvalds * 591da177e4SLinus Torvalds * Note that the symlink resolution is not *completely* iterative. 601da177e4SLinus Torvalds * There is still a significant amount of tail- and mid- recursion in 611da177e4SLinus Torvalds * the algorithm. Also, note that <fs>_readlink() is not used in 621da177e4SLinus Torvalds * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() 631da177e4SLinus Torvalds * may return different results than <fs>_follow_link(). Many virtual 641da177e4SLinus Torvalds * filesystems (including /proc) exhibit this behavior. 651da177e4SLinus Torvalds */ 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: 681da177e4SLinus Torvalds * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL 691da177e4SLinus Torvalds * and the name already exists in form of a symlink, try to create the new 701da177e4SLinus Torvalds * name indicated by the symlink. The old code always complained that the 711da177e4SLinus Torvalds * name already exists, due to not following the symlink even if its target 721da177e4SLinus Torvalds * is nonexistent. The new semantics affects also mknod() and link() when 731da177e4SLinus Torvalds * the name is a symlink pointing to a non-existant name. 741da177e4SLinus Torvalds * 751da177e4SLinus Torvalds * I don't know which semantics is the right one, since I have no access 761da177e4SLinus Torvalds * to standards. But I found by trial that HP-UX 9.0 has the full "new" 771da177e4SLinus Torvalds * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the 781da177e4SLinus Torvalds * "old" one. Personally, I think the new semantics is much more logical. 791da177e4SLinus Torvalds * Note that "ln old new" where "new" is a symlink pointing to a non-existing 801da177e4SLinus Torvalds * file does succeed in both HP-UX and SunOs, but not in Solaris 811da177e4SLinus Torvalds * and in the old Linux semantics. 821da177e4SLinus Torvalds */ 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink 851da177e4SLinus Torvalds * semantics. See the comments in "open_namei" and "do_link" below. 861da177e4SLinus Torvalds * 871da177e4SLinus Torvalds * [10-Sep-98 Alan Modra] Another symlink change. 881da177e4SLinus Torvalds */ 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: 911da177e4SLinus Torvalds * inside the path - always follow. 921da177e4SLinus Torvalds * in the last component in creation/removal/renaming - never follow. 931da177e4SLinus Torvalds * if LOOKUP_FOLLOW passed - follow. 941da177e4SLinus Torvalds * if the pathname has trailing slashes - follow. 951da177e4SLinus Torvalds * otherwise - don't follow. 961da177e4SLinus Torvalds * (applied in that order). 971da177e4SLinus Torvalds * 981da177e4SLinus Torvalds * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT 991da177e4SLinus Torvalds * restored for 2.4. This is the last surviving part of old 4.2BSD bug. 1001da177e4SLinus Torvalds * During the 2.4 we need to fix the userland stuff depending on it - 1011da177e4SLinus Torvalds * hopefully we will be able to get rid of that wart in 2.5. So far only 1021da177e4SLinus Torvalds * XEmacs seems to be relying on it... 1031da177e4SLinus Torvalds */ 1041da177e4SLinus Torvalds /* 1051da177e4SLinus Torvalds * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) 106a11f3a05SArjan van de Ven * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives 1071da177e4SLinus Torvalds * any extra contention... 1081da177e4SLinus Torvalds */ 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional 1111da177e4SLinus Torvalds * checking and hopefully speeding things up, we copy filenames to the 1121da177e4SLinus Torvalds * kernel data space before using them.. 1131da177e4SLinus Torvalds * 1141da177e4SLinus Torvalds * POSIX.1 2.4: an empty pathname is invalid (ENOENT). 1151da177e4SLinus Torvalds * PATH_MAX includes the nul terminator --RR. 1161da177e4SLinus Torvalds */ 117858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page) 1181da177e4SLinus Torvalds { 1191da177e4SLinus Torvalds int retval; 1201da177e4SLinus Torvalds unsigned long len = PATH_MAX; 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds if (!segment_eq(get_fs(), KERNEL_DS)) { 1231da177e4SLinus Torvalds if ((unsigned long) filename >= TASK_SIZE) 1241da177e4SLinus Torvalds return -EFAULT; 1251da177e4SLinus Torvalds if (TASK_SIZE - (unsigned long) filename < PATH_MAX) 1261da177e4SLinus Torvalds len = TASK_SIZE - (unsigned long) filename; 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds retval = strncpy_from_user(page, filename, len); 1301da177e4SLinus Torvalds if (retval > 0) { 1311da177e4SLinus Torvalds if (retval < len) 1321da177e4SLinus Torvalds return 0; 1331da177e4SLinus Torvalds return -ENAMETOOLONG; 1341da177e4SLinus Torvalds } else if (!retval) 1351da177e4SLinus Torvalds retval = -ENOENT; 1361da177e4SLinus Torvalds return retval; 1371da177e4SLinus Torvalds } 1381da177e4SLinus Torvalds 139f52e0c11SAl Viro static char *getname_flags(const char __user * filename, int flags) 1401da177e4SLinus Torvalds { 1411da177e4SLinus Torvalds char *tmp, *result; 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds result = ERR_PTR(-ENOMEM); 1441da177e4SLinus Torvalds tmp = __getname(); 1451da177e4SLinus Torvalds if (tmp) { 1461da177e4SLinus Torvalds int retval = do_getname(filename, tmp); 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds result = tmp; 1491da177e4SLinus Torvalds if (retval < 0) { 150f52e0c11SAl Viro if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) { 1511da177e4SLinus Torvalds __putname(tmp); 1521da177e4SLinus Torvalds result = ERR_PTR(retval); 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds } 155f52e0c11SAl Viro } 1561da177e4SLinus Torvalds audit_getname(result); 1571da177e4SLinus Torvalds return result; 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds 160f52e0c11SAl Viro char *getname(const char __user * filename) 161f52e0c11SAl Viro { 162f52e0c11SAl Viro return getname_flags(filename, 0); 163f52e0c11SAl Viro } 164f52e0c11SAl Viro 1651da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 1661da177e4SLinus Torvalds void putname(const char *name) 1671da177e4SLinus Torvalds { 1685ac3a9c2SAl Viro if (unlikely(!audit_dummy_context())) 1691da177e4SLinus Torvalds audit_putname(name); 1701da177e4SLinus Torvalds else 1711da177e4SLinus Torvalds __putname(name); 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds EXPORT_SYMBOL(putname); 1741da177e4SLinus Torvalds #endif 1751da177e4SLinus Torvalds 1765909ccaaSLinus Torvalds /* 1775909ccaaSLinus Torvalds * This does basic POSIX ACL permission checking 1785909ccaaSLinus Torvalds */ 179b74c79e9SNick Piggin static int acl_permission_check(struct inode *inode, int mask, unsigned int flags, 180b74c79e9SNick Piggin int (*check_acl)(struct inode *inode, int mask, unsigned int flags)) 1815909ccaaSLinus Torvalds { 1825909ccaaSLinus Torvalds umode_t mode = inode->i_mode; 1835909ccaaSLinus Torvalds 1845909ccaaSLinus Torvalds mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 1855909ccaaSLinus Torvalds 1865909ccaaSLinus Torvalds if (current_fsuid() == inode->i_uid) 1875909ccaaSLinus Torvalds mode >>= 6; 1885909ccaaSLinus Torvalds else { 1895909ccaaSLinus Torvalds if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) { 190b74c79e9SNick Piggin int error = check_acl(inode, mask, flags); 1915909ccaaSLinus Torvalds if (error != -EAGAIN) 1925909ccaaSLinus Torvalds return error; 1935909ccaaSLinus Torvalds } 1945909ccaaSLinus Torvalds 1955909ccaaSLinus Torvalds if (in_group_p(inode->i_gid)) 1965909ccaaSLinus Torvalds mode >>= 3; 1975909ccaaSLinus Torvalds } 1985909ccaaSLinus Torvalds 1995909ccaaSLinus Torvalds /* 2005909ccaaSLinus Torvalds * If the DACs are ok we don't need any capability check. 2015909ccaaSLinus Torvalds */ 2025909ccaaSLinus Torvalds if ((mask & ~mode) == 0) 2035909ccaaSLinus Torvalds return 0; 2045909ccaaSLinus Torvalds return -EACCES; 2055909ccaaSLinus Torvalds } 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds /** 2081da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 2091da177e4SLinus Torvalds * @inode: inode to check access rights for 2101da177e4SLinus Torvalds * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 2111da177e4SLinus Torvalds * @check_acl: optional callback to check for Posix ACLs 21239191628SRandy Dunlap * @flags: IPERM_FLAG_ flags. 2131da177e4SLinus Torvalds * 2141da177e4SLinus Torvalds * Used to check for read/write/execute permissions on a file. 2151da177e4SLinus Torvalds * We use "fsuid" for this, letting us set arbitrary permissions 2161da177e4SLinus Torvalds * for filesystem access without changing the "normal" uids which 217b74c79e9SNick Piggin * are used for other things. 218b74c79e9SNick Piggin * 219b74c79e9SNick Piggin * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk 220b74c79e9SNick Piggin * request cannot be satisfied (eg. requires blocking or too much complexity). 221b74c79e9SNick Piggin * It would then be called again in ref-walk mode. 2221da177e4SLinus Torvalds */ 223b74c79e9SNick Piggin int generic_permission(struct inode *inode, int mask, unsigned int flags, 224b74c79e9SNick Piggin int (*check_acl)(struct inode *inode, int mask, unsigned int flags)) 2251da177e4SLinus Torvalds { 2265909ccaaSLinus Torvalds int ret; 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds /* 2295909ccaaSLinus Torvalds * Do the basic POSIX ACL permission checks. 2301da177e4SLinus Torvalds */ 231b74c79e9SNick Piggin ret = acl_permission_check(inode, mask, flags, check_acl); 2325909ccaaSLinus Torvalds if (ret != -EACCES) 2335909ccaaSLinus Torvalds return ret; 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds /* 2361da177e4SLinus Torvalds * Read/write DACs are always overridable. 2371da177e4SLinus Torvalds * Executable DACs are overridable if at least one exec bit is set. 2381da177e4SLinus Torvalds */ 239f696a365SMiklos Szeredi if (!(mask & MAY_EXEC) || execute_ok(inode)) 2401da177e4SLinus Torvalds if (capable(CAP_DAC_OVERRIDE)) 2411da177e4SLinus Torvalds return 0; 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds /* 2441da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 2451da177e4SLinus Torvalds */ 2467ea66001SSerge E. Hallyn mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 2471da177e4SLinus Torvalds if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE))) 2481da177e4SLinus Torvalds if (capable(CAP_DAC_READ_SEARCH)) 2491da177e4SLinus Torvalds return 0; 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds return -EACCES; 2521da177e4SLinus Torvalds } 2531da177e4SLinus Torvalds 254cb23beb5SChristoph Hellwig /** 255cb23beb5SChristoph Hellwig * inode_permission - check for access rights to a given inode 256cb23beb5SChristoph Hellwig * @inode: inode to check permission on 257cb23beb5SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 258cb23beb5SChristoph Hellwig * 259cb23beb5SChristoph Hellwig * Used to check for read/write/execute permissions on an inode. 260cb23beb5SChristoph Hellwig * We use "fsuid" for this, letting us set arbitrary permissions 261cb23beb5SChristoph Hellwig * for filesystem access without changing the "normal" uids which 262cb23beb5SChristoph Hellwig * are used for other things. 263cb23beb5SChristoph Hellwig */ 264f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask) 2651da177e4SLinus Torvalds { 266e6305c43SAl Viro int retval; 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds if (mask & MAY_WRITE) { 26922590e41SMiklos Szeredi umode_t mode = inode->i_mode; 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds /* 2721da177e4SLinus Torvalds * Nobody gets write access to a read-only fs. 2731da177e4SLinus Torvalds */ 2741da177e4SLinus Torvalds if (IS_RDONLY(inode) && 2751da177e4SLinus Torvalds (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 2761da177e4SLinus Torvalds return -EROFS; 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds /* 2791da177e4SLinus Torvalds * Nobody gets write access to an immutable file. 2801da177e4SLinus Torvalds */ 2811da177e4SLinus Torvalds if (IS_IMMUTABLE(inode)) 2821da177e4SLinus Torvalds return -EACCES; 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds 285acfa4380SAl Viro if (inode->i_op->permission) 286b74c79e9SNick Piggin retval = inode->i_op->permission(inode, mask, 0); 287f696a365SMiklos Szeredi else 288b74c79e9SNick Piggin retval = generic_permission(inode, mask, 0, 289b74c79e9SNick Piggin inode->i_op->check_acl); 290f696a365SMiklos Szeredi 2911da177e4SLinus Torvalds if (retval) 2921da177e4SLinus Torvalds return retval; 2931da177e4SLinus Torvalds 29408ce5f16SSerge E. Hallyn retval = devcgroup_inode_permission(inode, mask); 29508ce5f16SSerge E. Hallyn if (retval) 29608ce5f16SSerge E. Hallyn return retval; 29708ce5f16SSerge E. Hallyn 298d09ca739SEric Paris return security_inode_permission(inode, mask); 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds 301e4543eddSChristoph Hellwig /** 3028c744fb8SChristoph Hellwig * file_permission - check for additional access rights to a given file 3038c744fb8SChristoph Hellwig * @file: file to check access rights for 3048c744fb8SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 3058c744fb8SChristoph Hellwig * 3068c744fb8SChristoph Hellwig * Used to check for read/write/execute permissions on an already opened 3078c744fb8SChristoph Hellwig * file. 3088c744fb8SChristoph Hellwig * 3098c744fb8SChristoph Hellwig * Note: 3108c744fb8SChristoph Hellwig * Do not use this function in new code. All access checks should 311cb23beb5SChristoph Hellwig * be done using inode_permission(). 3128c744fb8SChristoph Hellwig */ 3138c744fb8SChristoph Hellwig int file_permission(struct file *file, int mask) 3148c744fb8SChristoph Hellwig { 315f419a2e3SAl Viro return inode_permission(file->f_path.dentry->d_inode, mask); 3168c744fb8SChristoph Hellwig } 3178c744fb8SChristoph Hellwig 3181da177e4SLinus Torvalds /* 3191da177e4SLinus Torvalds * get_write_access() gets write permission for a file. 3201da177e4SLinus Torvalds * put_write_access() releases this write permission. 3211da177e4SLinus Torvalds * This is used for regular files. 3221da177e4SLinus Torvalds * We cannot support write (and maybe mmap read-write shared) accesses and 3231da177e4SLinus Torvalds * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode 3241da177e4SLinus Torvalds * can have the following values: 3251da177e4SLinus Torvalds * 0: no writers, no VM_DENYWRITE mappings 3261da177e4SLinus Torvalds * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist 3271da177e4SLinus Torvalds * > 0: (i_writecount) users are writing to the file. 3281da177e4SLinus Torvalds * 3291da177e4SLinus Torvalds * Normally we operate on that counter with atomic_{inc,dec} and it's safe 3301da177e4SLinus Torvalds * except for the cases where we don't hold i_writecount yet. Then we need to 3311da177e4SLinus Torvalds * use {get,deny}_write_access() - these functions check the sign and refuse 3321da177e4SLinus Torvalds * to do the change if sign is wrong. Exclusion between them is provided by 3331da177e4SLinus Torvalds * the inode->i_lock spinlock. 3341da177e4SLinus Torvalds */ 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds int get_write_access(struct inode * inode) 3371da177e4SLinus Torvalds { 3381da177e4SLinus Torvalds spin_lock(&inode->i_lock); 3391da177e4SLinus Torvalds if (atomic_read(&inode->i_writecount) < 0) { 3401da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3411da177e4SLinus Torvalds return -ETXTBSY; 3421da177e4SLinus Torvalds } 3431da177e4SLinus Torvalds atomic_inc(&inode->i_writecount); 3441da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds return 0; 3471da177e4SLinus Torvalds } 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds int deny_write_access(struct file * file) 3501da177e4SLinus Torvalds { 3510f7fc9e4SJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds spin_lock(&inode->i_lock); 3541da177e4SLinus Torvalds if (atomic_read(&inode->i_writecount) > 0) { 3551da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3561da177e4SLinus Torvalds return -ETXTBSY; 3571da177e4SLinus Torvalds } 3581da177e4SLinus Torvalds atomic_dec(&inode->i_writecount); 3591da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3601da177e4SLinus Torvalds 3611da177e4SLinus Torvalds return 0; 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds 3641d957f9bSJan Blunck /** 3655dd784d0SJan Blunck * path_get - get a reference to a path 3665dd784d0SJan Blunck * @path: path to get the reference to 3675dd784d0SJan Blunck * 3685dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 3695dd784d0SJan Blunck */ 3705dd784d0SJan Blunck void path_get(struct path *path) 3715dd784d0SJan Blunck { 3725dd784d0SJan Blunck mntget(path->mnt); 3735dd784d0SJan Blunck dget(path->dentry); 3745dd784d0SJan Blunck } 3755dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 3765dd784d0SJan Blunck 3775dd784d0SJan Blunck /** 3781d957f9bSJan Blunck * path_put - put a reference to a path 3791d957f9bSJan Blunck * @path: path to put the reference to 3801d957f9bSJan Blunck * 3811d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 3821d957f9bSJan Blunck */ 3831d957f9bSJan Blunck void path_put(struct path *path) 3841da177e4SLinus Torvalds { 3851d957f9bSJan Blunck dput(path->dentry); 3861d957f9bSJan Blunck mntput(path->mnt); 3871da177e4SLinus Torvalds } 3881d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 3891da177e4SLinus Torvalds 390834f2a4aSTrond Myklebust /** 39131e6b01fSNick Piggin * nameidata_drop_rcu - drop this nameidata out of rcu-walk 39231e6b01fSNick Piggin * @nd: nameidata pathwalk data to drop 39339191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 39431e6b01fSNick Piggin * 39531e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 39631e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). __drop_rcu* functions attempt 39731e6b01fSNick Piggin * to drop out of rcu-walk mode and take normal reference counts on dentries 39831e6b01fSNick Piggin * and vfsmounts to transition to rcu-walk mode. __drop_rcu* functions take 39931e6b01fSNick Piggin * refcounts at the last known good point before rcu-walk got stuck, so 40031e6b01fSNick Piggin * ref-walk may continue from there. If this is not successful (eg. a seqcount 40131e6b01fSNick Piggin * has changed), then failure is returned and path walk restarts from the 40231e6b01fSNick Piggin * beginning in ref-walk mode. 40331e6b01fSNick Piggin * 40431e6b01fSNick Piggin * nameidata_drop_rcu attempts to drop the current nd->path and nd->root into 40531e6b01fSNick Piggin * ref-walk. Must be called from rcu-walk context. 40631e6b01fSNick Piggin */ 40731e6b01fSNick Piggin static int nameidata_drop_rcu(struct nameidata *nd) 40831e6b01fSNick Piggin { 40931e6b01fSNick Piggin struct fs_struct *fs = current->fs; 41031e6b01fSNick Piggin struct dentry *dentry = nd->path.dentry; 4115b6ca027SAl Viro int want_root = 0; 41231e6b01fSNick Piggin 41331e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 4145b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 4155b6ca027SAl Viro want_root = 1; 41631e6b01fSNick Piggin spin_lock(&fs->lock); 41731e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 41831e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 41931e6b01fSNick Piggin goto err_root; 42031e6b01fSNick Piggin } 42131e6b01fSNick Piggin spin_lock(&dentry->d_lock); 42231e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 42331e6b01fSNick Piggin goto err; 42431e6b01fSNick Piggin BUG_ON(nd->inode != dentry->d_inode); 42531e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 4265b6ca027SAl Viro if (want_root) { 42731e6b01fSNick Piggin path_get(&nd->root); 42831e6b01fSNick Piggin spin_unlock(&fs->lock); 42931e6b01fSNick Piggin } 43031e6b01fSNick Piggin mntget(nd->path.mnt); 43131e6b01fSNick Piggin 43231e6b01fSNick Piggin rcu_read_unlock(); 43331e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 43431e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 43531e6b01fSNick Piggin return 0; 43631e6b01fSNick Piggin err: 43731e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 43831e6b01fSNick Piggin err_root: 4395b6ca027SAl Viro if (want_root) 44031e6b01fSNick Piggin spin_unlock(&fs->lock); 44131e6b01fSNick Piggin return -ECHILD; 44231e6b01fSNick Piggin } 44331e6b01fSNick Piggin 44431e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */ 44531e6b01fSNick Piggin static inline int nameidata_drop_rcu_maybe(struct nameidata *nd) 44631e6b01fSNick Piggin { 44731e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) 44831e6b01fSNick Piggin return nameidata_drop_rcu(nd); 44931e6b01fSNick Piggin return 0; 45031e6b01fSNick Piggin } 45131e6b01fSNick Piggin 45231e6b01fSNick Piggin /** 45331e6b01fSNick Piggin * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk 45431e6b01fSNick Piggin * @nd: nameidata pathwalk data to drop 45531e6b01fSNick Piggin * @dentry: dentry to drop 45639191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 45731e6b01fSNick Piggin * 45831e6b01fSNick Piggin * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root, 45931e6b01fSNick Piggin * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on 46031e6b01fSNick Piggin * @nd. Must be called from rcu-walk context. 46131e6b01fSNick Piggin */ 46231e6b01fSNick Piggin static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry) 46331e6b01fSNick Piggin { 46431e6b01fSNick Piggin struct fs_struct *fs = current->fs; 46531e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 4665b6ca027SAl Viro int want_root = 0; 46731e6b01fSNick Piggin 46831e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 4695b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 4705b6ca027SAl Viro want_root = 1; 47131e6b01fSNick Piggin spin_lock(&fs->lock); 47231e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 47331e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 47431e6b01fSNick Piggin goto err_root; 47531e6b01fSNick Piggin } 47631e6b01fSNick Piggin spin_lock(&parent->d_lock); 47731e6b01fSNick Piggin spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); 47831e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 47931e6b01fSNick Piggin goto err; 48031e6b01fSNick Piggin /* 48131e6b01fSNick Piggin * If the sequence check on the child dentry passed, then the child has 48231e6b01fSNick Piggin * not been removed from its parent. This means the parent dentry must 48331e6b01fSNick Piggin * be valid and able to take a reference at this point. 48431e6b01fSNick Piggin */ 48531e6b01fSNick Piggin BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); 48631e6b01fSNick Piggin BUG_ON(!parent->d_count); 48731e6b01fSNick Piggin parent->d_count++; 48831e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 48931e6b01fSNick Piggin spin_unlock(&parent->d_lock); 4905b6ca027SAl Viro if (want_root) { 49131e6b01fSNick Piggin path_get(&nd->root); 49231e6b01fSNick Piggin spin_unlock(&fs->lock); 49331e6b01fSNick Piggin } 49431e6b01fSNick Piggin mntget(nd->path.mnt); 49531e6b01fSNick Piggin 49631e6b01fSNick Piggin rcu_read_unlock(); 49731e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 49831e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 49931e6b01fSNick Piggin return 0; 50031e6b01fSNick Piggin err: 50131e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 50231e6b01fSNick Piggin spin_unlock(&parent->d_lock); 50331e6b01fSNick Piggin err_root: 5045b6ca027SAl Viro if (want_root) 50531e6b01fSNick Piggin spin_unlock(&fs->lock); 50631e6b01fSNick Piggin return -ECHILD; 50731e6b01fSNick Piggin } 50831e6b01fSNick Piggin 50931e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */ 51031e6b01fSNick Piggin static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry) 51131e6b01fSNick Piggin { 512a7472babSAl Viro if (nd->flags & LOOKUP_RCU) { 513a7472babSAl Viro if (unlikely(nameidata_dentry_drop_rcu(nd, dentry))) { 514a7472babSAl Viro nd->flags &= ~LOOKUP_RCU; 5155b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 516a7472babSAl Viro nd->root.mnt = NULL; 517a7472babSAl Viro rcu_read_unlock(); 518a7472babSAl Viro br_read_unlock(vfsmount_lock); 519a7472babSAl Viro return -ECHILD; 520a7472babSAl Viro } 521a7472babSAl Viro } 52231e6b01fSNick Piggin return 0; 52331e6b01fSNick Piggin } 52431e6b01fSNick Piggin 52531e6b01fSNick Piggin /** 52631e6b01fSNick Piggin * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk 52731e6b01fSNick Piggin * @nd: nameidata pathwalk data to drop 52839191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 52931e6b01fSNick Piggin * 53031e6b01fSNick Piggin * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk. 53131e6b01fSNick Piggin * nd->path should be the final element of the lookup, so nd->root is discarded. 53231e6b01fSNick Piggin * Must be called from rcu-walk context. 53331e6b01fSNick Piggin */ 53431e6b01fSNick Piggin static int nameidata_drop_rcu_last(struct nameidata *nd) 53531e6b01fSNick Piggin { 53631e6b01fSNick Piggin struct dentry *dentry = nd->path.dentry; 53731e6b01fSNick Piggin 53831e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 53931e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 5405b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 54131e6b01fSNick Piggin nd->root.mnt = NULL; 54231e6b01fSNick Piggin spin_lock(&dentry->d_lock); 54331e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 54431e6b01fSNick Piggin goto err_unlock; 54531e6b01fSNick Piggin BUG_ON(nd->inode != dentry->d_inode); 54631e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 54731e6b01fSNick Piggin 54831e6b01fSNick Piggin mntget(nd->path.mnt); 54931e6b01fSNick Piggin 55031e6b01fSNick Piggin rcu_read_unlock(); 55131e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 55231e6b01fSNick Piggin 55331e6b01fSNick Piggin return 0; 55431e6b01fSNick Piggin 55531e6b01fSNick Piggin err_unlock: 55631e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 55731e6b01fSNick Piggin rcu_read_unlock(); 55831e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 55931e6b01fSNick Piggin return -ECHILD; 56031e6b01fSNick Piggin } 56131e6b01fSNick Piggin 56231e6b01fSNick Piggin /** 563834f2a4aSTrond Myklebust * release_open_intent - free up open intent resources 564834f2a4aSTrond Myklebust * @nd: pointer to nameidata 565834f2a4aSTrond Myklebust */ 566834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd) 567834f2a4aSTrond Myklebust { 5682dab5974SLinus Torvalds struct file *file = nd->intent.open.file; 5692dab5974SLinus Torvalds 5702dab5974SLinus Torvalds if (file && !IS_ERR(file)) { 5712dab5974SLinus Torvalds if (file->f_path.dentry == NULL) 5722dab5974SLinus Torvalds put_filp(file); 573834f2a4aSTrond Myklebust else 5742dab5974SLinus Torvalds fput(file); 5752dab5974SLinus Torvalds } 576834f2a4aSTrond Myklebust } 577834f2a4aSTrond Myklebust 578f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd) 57934286d66SNick Piggin { 580f60aef7eSAl Viro return dentry->d_op->d_revalidate(dentry, nd); 58134286d66SNick Piggin } 58234286d66SNick Piggin 583f5e1c1c1SAl Viro static struct dentry * 584bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd) 585bcdc5e01SIan Kent { 586f5e1c1c1SAl Viro int status = d_revalidate(dentry, nd); 587bcdc5e01SIan Kent if (unlikely(status <= 0)) { 588bcdc5e01SIan Kent /* 589bcdc5e01SIan Kent * The dentry failed validation. 590bcdc5e01SIan Kent * If d_revalidate returned 0 attempt to invalidate 591bcdc5e01SIan Kent * the dentry otherwise d_revalidate is asking us 592bcdc5e01SIan Kent * to return a fail status. 593bcdc5e01SIan Kent */ 59434286d66SNick Piggin if (status < 0) { 59534286d66SNick Piggin dput(dentry); 59634286d66SNick Piggin dentry = ERR_PTR(status); 597f5e1c1c1SAl Viro } else if (!d_invalidate(dentry)) { 598bcdc5e01SIan Kent dput(dentry); 599bcdc5e01SIan Kent dentry = NULL; 600bcdc5e01SIan Kent } 601bcdc5e01SIan Kent } 602f5e1c1c1SAl Viro return dentry; 603f5e1c1c1SAl Viro } 604f5e1c1c1SAl Viro 6051da177e4SLinus Torvalds /* 60616c2cd71SAl Viro * handle_reval_path - force revalidation of a dentry 60739159de2SJeff Layton * 60839159de2SJeff Layton * In some situations the path walking code will trust dentries without 60939159de2SJeff Layton * revalidating them. This causes problems for filesystems that depend on 61039159de2SJeff Layton * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set 61139159de2SJeff Layton * (which indicates that it's possible for the dentry to go stale), force 61239159de2SJeff Layton * a d_revalidate call before proceeding. 61339159de2SJeff Layton * 61439159de2SJeff Layton * Returns 0 if the revalidation was successful. If the revalidation fails, 61539159de2SJeff Layton * either return the error returned by d_revalidate or -ESTALE if the 61639159de2SJeff Layton * revalidation it just returned 0. If d_revalidate returns 0, we attempt to 61739159de2SJeff Layton * invalidate the dentry. It's up to the caller to handle putting references 61839159de2SJeff Layton * to the path if necessary. 61939159de2SJeff Layton */ 62016c2cd71SAl Viro static inline int handle_reval_path(struct nameidata *nd) 62139159de2SJeff Layton { 62216c2cd71SAl Viro struct dentry *dentry = nd->path.dentry; 62339159de2SJeff Layton int status; 62439159de2SJeff Layton 62516c2cd71SAl Viro if (likely(!(nd->flags & LOOKUP_JUMPED))) 62639159de2SJeff Layton return 0; 62739159de2SJeff Layton 62816c2cd71SAl Viro if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE))) 62916c2cd71SAl Viro return 0; 63016c2cd71SAl Viro 63116c2cd71SAl Viro if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))) 63216c2cd71SAl Viro return 0; 63316c2cd71SAl Viro 63416c2cd71SAl Viro /* Note: we do not d_invalidate() */ 63534286d66SNick Piggin status = d_revalidate(dentry, nd); 63639159de2SJeff Layton if (status > 0) 63739159de2SJeff Layton return 0; 63839159de2SJeff Layton 63916c2cd71SAl Viro if (!status) 64039159de2SJeff Layton status = -ESTALE; 64116c2cd71SAl Viro 64239159de2SJeff Layton return status; 64339159de2SJeff Layton } 64439159de2SJeff Layton 64539159de2SJeff Layton /* 646b75b5086SAl Viro * Short-cut version of permission(), for calling on directories 647b75b5086SAl Viro * during pathname resolution. Combines parts of permission() 648b75b5086SAl Viro * and generic_permission(), and tests ONLY for MAY_EXEC permission. 6491da177e4SLinus Torvalds * 6501da177e4SLinus Torvalds * If appropriate, check DAC only. If not appropriate, or 651b75b5086SAl Viro * short-cut DAC fails, then call ->permission() to do more 6521da177e4SLinus Torvalds * complete permission check. 6531da177e4SLinus Torvalds */ 654b74c79e9SNick Piggin static inline int exec_permission(struct inode *inode, unsigned int flags) 6551da177e4SLinus Torvalds { 6565909ccaaSLinus Torvalds int ret; 6571da177e4SLinus Torvalds 658cb9179eaSLinus Torvalds if (inode->i_op->permission) { 659b74c79e9SNick Piggin ret = inode->i_op->permission(inode, MAY_EXEC, flags); 660b74c79e9SNick Piggin } else { 661b74c79e9SNick Piggin ret = acl_permission_check(inode, MAY_EXEC, flags, 662b74c79e9SNick Piggin inode->i_op->check_acl); 663cb9179eaSLinus Torvalds } 664b74c79e9SNick Piggin if (likely(!ret)) 6651da177e4SLinus Torvalds goto ok; 666b74c79e9SNick Piggin if (ret == -ECHILD) 66731e6b01fSNick Piggin return ret; 6681da177e4SLinus Torvalds 669f1ac9f6bSLinus Torvalds if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH)) 6701da177e4SLinus Torvalds goto ok; 6711da177e4SLinus Torvalds 6725909ccaaSLinus Torvalds return ret; 6731da177e4SLinus Torvalds ok: 674b74c79e9SNick Piggin return security_inode_exec_permission(inode, flags); 6751da177e4SLinus Torvalds } 6761da177e4SLinus Torvalds 6772a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 6782a737871SAl Viro { 679f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 680f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 6812a737871SAl Viro } 6822a737871SAl Viro 6836de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 6846de88d72SAl Viro 68531e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 68631e6b01fSNick Piggin { 68731e6b01fSNick Piggin if (!nd->root.mnt) { 68831e6b01fSNick Piggin struct fs_struct *fs = current->fs; 689c28cc364SNick Piggin unsigned seq; 690c28cc364SNick Piggin 691c28cc364SNick Piggin do { 692c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 69331e6b01fSNick Piggin nd->root = fs->root; 694c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 69531e6b01fSNick Piggin } 69631e6b01fSNick Piggin } 69731e6b01fSNick Piggin 698f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 6991da177e4SLinus Torvalds { 70031e6b01fSNick Piggin int ret; 70131e6b01fSNick Piggin 7021da177e4SLinus Torvalds if (IS_ERR(link)) 7031da177e4SLinus Torvalds goto fail; 7041da177e4SLinus Torvalds 7051da177e4SLinus Torvalds if (*link == '/') { 7062a737871SAl Viro set_root(nd); 7071d957f9bSJan Blunck path_put(&nd->path); 7082a737871SAl Viro nd->path = nd->root; 7092a737871SAl Viro path_get(&nd->root); 71016c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 7111da177e4SLinus Torvalds } 71231e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 713b4091d5fSChristoph Hellwig 71431e6b01fSNick Piggin ret = link_path_walk(link, nd); 71531e6b01fSNick Piggin return ret; 7161da177e4SLinus Torvalds fail: 7171d957f9bSJan Blunck path_put(&nd->path); 7181da177e4SLinus Torvalds return PTR_ERR(link); 7191da177e4SLinus Torvalds } 7201da177e4SLinus Torvalds 7211d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 722051d3812SIan Kent { 723051d3812SIan Kent dput(path->dentry); 7244ac91378SJan Blunck if (path->mnt != nd->path.mnt) 725051d3812SIan Kent mntput(path->mnt); 726051d3812SIan Kent } 727051d3812SIan Kent 7287b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path, 7297b9337aaSNick Piggin struct nameidata *nd) 730051d3812SIan Kent { 73131e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 7324ac91378SJan Blunck dput(nd->path.dentry); 73331e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 7344ac91378SJan Blunck mntput(nd->path.mnt); 7359a229683SHuang Shijie } 73631e6b01fSNick Piggin nd->path.mnt = path->mnt; 7374ac91378SJan Blunck nd->path.dentry = path->dentry; 738051d3812SIan Kent } 739051d3812SIan Kent 740574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie) 741574197e0SAl Viro { 742574197e0SAl Viro struct inode *inode = link->dentry->d_inode; 743574197e0SAl Viro if (!IS_ERR(cookie) && inode->i_op->put_link) 744574197e0SAl Viro inode->i_op->put_link(link->dentry, nd, cookie); 745574197e0SAl Viro path_put(link); 746574197e0SAl Viro } 747574197e0SAl Viro 748def4af30SAl Viro static __always_inline int 749574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p) 7501da177e4SLinus Torvalds { 7511da177e4SLinus Torvalds int error; 7527b9337aaSNick Piggin struct dentry *dentry = link->dentry; 7531da177e4SLinus Torvalds 754844a3917SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 755844a3917SAl Viro 756574197e0SAl Viro if (unlikely(current->total_link_count >= 40)) { 757574197e0SAl Viro *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */ 758574197e0SAl Viro path_put_conditional(link, nd); 759574197e0SAl Viro path_put(&nd->path); 760574197e0SAl Viro return -ELOOP; 761574197e0SAl Viro } 762574197e0SAl Viro cond_resched(); 763574197e0SAl Viro current->total_link_count++; 764574197e0SAl Viro 7657b9337aaSNick Piggin touch_atime(link->mnt, dentry); 7661da177e4SLinus Torvalds nd_set_link(nd, NULL); 767cd4e91d3SAl Viro 76887556ef1SDavid Howells if (link->mnt == nd->path.mnt) 7697b9337aaSNick Piggin mntget(link->mnt); 77031e6b01fSNick Piggin 77136f3b4f6SAl Viro error = security_inode_follow_link(link->dentry, nd); 77236f3b4f6SAl Viro if (error) { 77336f3b4f6SAl Viro *p = ERR_PTR(error); /* no ->put_link(), please */ 77436f3b4f6SAl Viro path_put(&nd->path); 77536f3b4f6SAl Viro return error; 77636f3b4f6SAl Viro } 77736f3b4f6SAl Viro 77886acdca1SAl Viro nd->last_type = LAST_BIND; 779def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 780def4af30SAl Viro error = PTR_ERR(*p); 781def4af30SAl Viro if (!IS_ERR(*p)) { 7821da177e4SLinus Torvalds char *s = nd_get_link(nd); 783cc314eefSLinus Torvalds error = 0; 7841da177e4SLinus Torvalds if (s) 7851da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 786bcda7652SAl Viro else if (nd->last_type == LAST_BIND) { 78716c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 788b21041d0SAl Viro nd->inode = nd->path.dentry->d_inode; 789b21041d0SAl Viro if (nd->inode->i_op->follow_link) { 790bcda7652SAl Viro /* stepped on a _really_ weird one */ 791bcda7652SAl Viro path_put(&nd->path); 792bcda7652SAl Viro error = -ELOOP; 793bcda7652SAl Viro } 794bcda7652SAl Viro } 7951da177e4SLinus Torvalds } 7961da177e4SLinus Torvalds return error; 7971da177e4SLinus Torvalds } 7981da177e4SLinus Torvalds 79931e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 80031e6b01fSNick Piggin { 80131e6b01fSNick Piggin struct vfsmount *parent; 80231e6b01fSNick Piggin struct dentry *mountpoint; 80331e6b01fSNick Piggin 80431e6b01fSNick Piggin parent = path->mnt->mnt_parent; 80531e6b01fSNick Piggin if (parent == path->mnt) 80631e6b01fSNick Piggin return 0; 80731e6b01fSNick Piggin mountpoint = path->mnt->mnt_mountpoint; 80831e6b01fSNick Piggin path->dentry = mountpoint; 80931e6b01fSNick Piggin path->mnt = parent; 81031e6b01fSNick Piggin return 1; 81131e6b01fSNick Piggin } 81231e6b01fSNick Piggin 813bab77ebfSAl Viro int follow_up(struct path *path) 8141da177e4SLinus Torvalds { 8151da177e4SLinus Torvalds struct vfsmount *parent; 8161da177e4SLinus Torvalds struct dentry *mountpoint; 81799b7db7bSNick Piggin 81899b7db7bSNick Piggin br_read_lock(vfsmount_lock); 819bab77ebfSAl Viro parent = path->mnt->mnt_parent; 820bab77ebfSAl Viro if (parent == path->mnt) { 82199b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 8221da177e4SLinus Torvalds return 0; 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds mntget(parent); 825bab77ebfSAl Viro mountpoint = dget(path->mnt->mnt_mountpoint); 82699b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 827bab77ebfSAl Viro dput(path->dentry); 828bab77ebfSAl Viro path->dentry = mountpoint; 829bab77ebfSAl Viro mntput(path->mnt); 830bab77ebfSAl Viro path->mnt = parent; 8311da177e4SLinus Torvalds return 1; 8321da177e4SLinus Torvalds } 8331da177e4SLinus Torvalds 834b5c84bf6SNick Piggin /* 8359875cf80SDavid Howells * Perform an automount 8369875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 8379875cf80SDavid Howells * were called with. 8381da177e4SLinus Torvalds */ 8399875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags, 8409875cf80SDavid Howells bool *need_mntput) 84131e6b01fSNick Piggin { 8429875cf80SDavid Howells struct vfsmount *mnt; 843ea5b778aSDavid Howells int err; 8449875cf80SDavid Howells 8459875cf80SDavid Howells if (!path->dentry->d_op || !path->dentry->d_op->d_automount) 8469875cf80SDavid Howells return -EREMOTE; 8479875cf80SDavid Howells 8486f45b656SDavid Howells /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT 8496f45b656SDavid Howells * and this is the terminal part of the path. 8506f45b656SDavid Howells */ 8516f45b656SDavid Howells if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE)) 8526f45b656SDavid Howells return -EISDIR; /* we actually want to stop here */ 8536f45b656SDavid Howells 8549875cf80SDavid Howells /* We want to mount if someone is trying to open/create a file of any 8559875cf80SDavid Howells * type under the mountpoint, wants to traverse through the mountpoint 8569875cf80SDavid Howells * or wants to open the mounted directory. 8579875cf80SDavid Howells * 8589875cf80SDavid Howells * We don't want to mount if someone's just doing a stat and they've 8599875cf80SDavid Howells * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and 8609875cf80SDavid Howells * appended a '/' to the name. 8619875cf80SDavid Howells */ 8629875cf80SDavid Howells if (!(flags & LOOKUP_FOLLOW) && 8639875cf80SDavid Howells !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY | 8649875cf80SDavid Howells LOOKUP_OPEN | LOOKUP_CREATE))) 8659875cf80SDavid Howells return -EISDIR; 8669875cf80SDavid Howells 8679875cf80SDavid Howells current->total_link_count++; 8689875cf80SDavid Howells if (current->total_link_count >= 40) 8699875cf80SDavid Howells return -ELOOP; 8709875cf80SDavid Howells 8719875cf80SDavid Howells mnt = path->dentry->d_op->d_automount(path); 8729875cf80SDavid Howells if (IS_ERR(mnt)) { 8739875cf80SDavid Howells /* 8749875cf80SDavid Howells * The filesystem is allowed to return -EISDIR here to indicate 8759875cf80SDavid Howells * it doesn't want to automount. For instance, autofs would do 8769875cf80SDavid Howells * this so that its userspace daemon can mount on this dentry. 8779875cf80SDavid Howells * 8789875cf80SDavid Howells * However, we can only permit this if it's a terminal point in 8799875cf80SDavid Howells * the path being looked up; if it wasn't then the remainder of 8809875cf80SDavid Howells * the path is inaccessible and we should say so. 8819875cf80SDavid Howells */ 8829875cf80SDavid Howells if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE)) 8839875cf80SDavid Howells return -EREMOTE; 8849875cf80SDavid Howells return PTR_ERR(mnt); 88531e6b01fSNick Piggin } 886ea5b778aSDavid Howells 8879875cf80SDavid Howells if (!mnt) /* mount collision */ 8889875cf80SDavid Howells return 0; 8899875cf80SDavid Howells 89019a167afSAl Viro err = finish_automount(mnt, path); 891ea5b778aSDavid Howells 892ea5b778aSDavid Howells switch (err) { 893ea5b778aSDavid Howells case -EBUSY: 894ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 89519a167afSAl Viro return 0; 896ea5b778aSDavid Howells case 0: 897463ffb2eSAl Viro dput(path->dentry); 8989875cf80SDavid Howells if (*need_mntput) 8999875cf80SDavid Howells mntput(path->mnt); 9009875cf80SDavid Howells path->mnt = mnt; 9019875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 9029875cf80SDavid Howells *need_mntput = true; 9039875cf80SDavid Howells return 0; 90419a167afSAl Viro default: 90519a167afSAl Viro return err; 9069875cf80SDavid Howells } 90719a167afSAl Viro 908ea5b778aSDavid Howells } 9099875cf80SDavid Howells 9109875cf80SDavid Howells /* 9119875cf80SDavid Howells * Handle a dentry that is managed in some way. 912cc53ce53SDavid Howells * - Flagged for transit management (autofs) 9139875cf80SDavid Howells * - Flagged as mountpoint 9149875cf80SDavid Howells * - Flagged as automount point 9159875cf80SDavid Howells * 9169875cf80SDavid Howells * This may only be called in refwalk mode. 9179875cf80SDavid Howells * 9189875cf80SDavid Howells * Serialization is taken care of in namespace.c 9199875cf80SDavid Howells */ 9209875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 9219875cf80SDavid Howells { 9229875cf80SDavid Howells unsigned managed; 9239875cf80SDavid Howells bool need_mntput = false; 9249875cf80SDavid Howells int ret; 9259875cf80SDavid Howells 9269875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 9279875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 9289875cf80SDavid Howells * the components of that value change under us */ 9299875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 9309875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 9319875cf80SDavid Howells unlikely(managed != 0)) { 932cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 933cc53ce53SDavid Howells * being held. */ 934cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 935cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 936cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 937ab90911fSDavid Howells ret = path->dentry->d_op->d_manage(path->dentry, 938ab90911fSDavid Howells false, false); 939cc53ce53SDavid Howells if (ret < 0) 940cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 941cc53ce53SDavid Howells } 942cc53ce53SDavid Howells 9439875cf80SDavid Howells /* Transit to a mounted filesystem. */ 9449875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 9459875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 9469875cf80SDavid Howells if (mounted) { 9479875cf80SDavid Howells dput(path->dentry); 9489875cf80SDavid Howells if (need_mntput) 949463ffb2eSAl Viro mntput(path->mnt); 950463ffb2eSAl Viro path->mnt = mounted; 951463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 9529875cf80SDavid Howells need_mntput = true; 9539875cf80SDavid Howells continue; 954463ffb2eSAl Viro } 955463ffb2eSAl Viro 9569875cf80SDavid Howells /* Something is mounted on this dentry in another 9579875cf80SDavid Howells * namespace and/or whatever was mounted there in this 9589875cf80SDavid Howells * namespace got unmounted before we managed to get the 9599875cf80SDavid Howells * vfsmount_lock */ 9601da177e4SLinus Torvalds } 9619875cf80SDavid Howells 9629875cf80SDavid Howells /* Handle an automount point */ 9639875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 9649875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 9659875cf80SDavid Howells if (ret < 0) 9669875cf80SDavid Howells return ret == -EISDIR ? 0 : ret; 9679875cf80SDavid Howells continue; 9689875cf80SDavid Howells } 9699875cf80SDavid Howells 9709875cf80SDavid Howells /* We didn't change the current path point */ 9719875cf80SDavid Howells break; 9729875cf80SDavid Howells } 9739875cf80SDavid Howells return 0; 9741da177e4SLinus Torvalds } 9751da177e4SLinus Torvalds 976cc53ce53SDavid Howells int follow_down_one(struct path *path) 9771da177e4SLinus Torvalds { 9781da177e4SLinus Torvalds struct vfsmount *mounted; 9791da177e4SLinus Torvalds 9801c755af4SAl Viro mounted = lookup_mnt(path); 9811da177e4SLinus Torvalds if (mounted) { 9829393bd07SAl Viro dput(path->dentry); 9839393bd07SAl Viro mntput(path->mnt); 9849393bd07SAl Viro path->mnt = mounted; 9859393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 9861da177e4SLinus Torvalds return 1; 9871da177e4SLinus Torvalds } 9881da177e4SLinus Torvalds return 0; 9891da177e4SLinus Torvalds } 9901da177e4SLinus Torvalds 9919875cf80SDavid Howells /* 9929875cf80SDavid Howells * Skip to top of mountpoint pile in rcuwalk mode. We abort the rcu-walk if we 993cc53ce53SDavid Howells * meet a managed dentry and we're not walking to "..". True is returned to 9949875cf80SDavid Howells * continue, false to abort. 9959875cf80SDavid Howells */ 9969875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 9979875cf80SDavid Howells struct inode **inode, bool reverse_transit) 9989875cf80SDavid Howells { 9999875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 10009875cf80SDavid Howells struct vfsmount *mounted; 1001ab90911fSDavid Howells if (unlikely(path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) && 1002ab90911fSDavid Howells !reverse_transit && 1003ab90911fSDavid Howells path->dentry->d_op->d_manage(path->dentry, false, true) < 0) 1004ab90911fSDavid Howells return false; 10059875cf80SDavid Howells mounted = __lookup_mnt(path->mnt, path->dentry, 1); 10069875cf80SDavid Howells if (!mounted) 10079875cf80SDavid Howells break; 10089875cf80SDavid Howells path->mnt = mounted; 10099875cf80SDavid Howells path->dentry = mounted->mnt_root; 10109875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 10119875cf80SDavid Howells *inode = path->dentry->d_inode; 10129875cf80SDavid Howells } 10139875cf80SDavid Howells 10149875cf80SDavid Howells if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 10159875cf80SDavid Howells return reverse_transit; 10169875cf80SDavid Howells return true; 10179875cf80SDavid Howells } 10189875cf80SDavid Howells 101931e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 102031e6b01fSNick Piggin { 102131e6b01fSNick Piggin struct inode *inode = nd->inode; 102231e6b01fSNick Piggin 102331e6b01fSNick Piggin set_root_rcu(nd); 102431e6b01fSNick Piggin 102531e6b01fSNick Piggin while (1) { 102631e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 102731e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 102831e6b01fSNick Piggin break; 102931e6b01fSNick Piggin } 103031e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 103131e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 103231e6b01fSNick Piggin struct dentry *parent = old->d_parent; 103331e6b01fSNick Piggin unsigned seq; 103431e6b01fSNick Piggin 103531e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 103631e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 1037ef7562d5SAl Viro goto failed; 103831e6b01fSNick Piggin inode = parent->d_inode; 103931e6b01fSNick Piggin nd->path.dentry = parent; 104031e6b01fSNick Piggin nd->seq = seq; 104131e6b01fSNick Piggin break; 104231e6b01fSNick Piggin } 104331e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 104431e6b01fSNick Piggin break; 104531e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 104631e6b01fSNick Piggin inode = nd->path.dentry->d_inode; 104731e6b01fSNick Piggin } 10489875cf80SDavid Howells __follow_mount_rcu(nd, &nd->path, &inode, true); 104931e6b01fSNick Piggin nd->inode = inode; 105031e6b01fSNick Piggin return 0; 1051ef7562d5SAl Viro 1052ef7562d5SAl Viro failed: 1053ef7562d5SAl Viro nd->flags &= ~LOOKUP_RCU; 10545b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1055ef7562d5SAl Viro nd->root.mnt = NULL; 1056ef7562d5SAl Viro rcu_read_unlock(); 1057ef7562d5SAl Viro br_read_unlock(vfsmount_lock); 1058ef7562d5SAl Viro return -ECHILD; 105931e6b01fSNick Piggin } 106031e6b01fSNick Piggin 10619875cf80SDavid Howells /* 1062cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 1063cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 1064cc53ce53SDavid Howells * caller is permitted to proceed or not. 1065cc53ce53SDavid Howells * 1066cc53ce53SDavid Howells * Care must be taken as namespace_sem may be held (indicated by mounting_here 1067cc53ce53SDavid Howells * being true). 1068cc53ce53SDavid Howells */ 1069cc53ce53SDavid Howells int follow_down(struct path *path, bool mounting_here) 1070cc53ce53SDavid Howells { 1071cc53ce53SDavid Howells unsigned managed; 1072cc53ce53SDavid Howells int ret; 1073cc53ce53SDavid Howells 1074cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 1075cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 1076cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 1077cc53ce53SDavid Howells * being held. 1078cc53ce53SDavid Howells * 1079cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 1080cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 1081cc53ce53SDavid Howells * other than its daemon the right to mount on its 1082cc53ce53SDavid Howells * superstructure. 1083cc53ce53SDavid Howells * 1084cc53ce53SDavid Howells * The filesystem may sleep at this point. 1085cc53ce53SDavid Howells */ 1086cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 1087cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 1088cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 1089ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 1090ab90911fSDavid Howells path->dentry, mounting_here, false); 1091cc53ce53SDavid Howells if (ret < 0) 1092cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 1093cc53ce53SDavid Howells } 1094cc53ce53SDavid Howells 1095cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 1096cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 1097cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 1098cc53ce53SDavid Howells if (!mounted) 1099cc53ce53SDavid Howells break; 1100cc53ce53SDavid Howells dput(path->dentry); 1101cc53ce53SDavid Howells mntput(path->mnt); 1102cc53ce53SDavid Howells path->mnt = mounted; 1103cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 1104cc53ce53SDavid Howells continue; 1105cc53ce53SDavid Howells } 1106cc53ce53SDavid Howells 1107cc53ce53SDavid Howells /* Don't handle automount points here */ 1108cc53ce53SDavid Howells break; 1109cc53ce53SDavid Howells } 1110cc53ce53SDavid Howells return 0; 1111cc53ce53SDavid Howells } 1112cc53ce53SDavid Howells 1113cc53ce53SDavid Howells /* 11149875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 11159875cf80SDavid Howells */ 11169875cf80SDavid Howells static void follow_mount(struct path *path) 11179875cf80SDavid Howells { 11189875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 11199875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 11209875cf80SDavid Howells if (!mounted) 11219875cf80SDavid Howells break; 11229875cf80SDavid Howells dput(path->dentry); 11239875cf80SDavid Howells mntput(path->mnt); 11249875cf80SDavid Howells path->mnt = mounted; 11259875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 11269875cf80SDavid Howells } 11279875cf80SDavid Howells } 11289875cf80SDavid Howells 112931e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 11301da177e4SLinus Torvalds { 11312a737871SAl Viro set_root(nd); 1132e518ddb7SAndreas Mohr 11331da177e4SLinus Torvalds while(1) { 11344ac91378SJan Blunck struct dentry *old = nd->path.dentry; 11351da177e4SLinus Torvalds 11362a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 11372a737871SAl Viro nd->path.mnt == nd->root.mnt) { 11381da177e4SLinus Torvalds break; 11391da177e4SLinus Torvalds } 11404ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 11413088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 11423088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 11431da177e4SLinus Torvalds dput(old); 11441da177e4SLinus Torvalds break; 11451da177e4SLinus Torvalds } 11463088dd70SAl Viro if (!follow_up(&nd->path)) 11471da177e4SLinus Torvalds break; 11481da177e4SLinus Torvalds } 114979ed0226SAl Viro follow_mount(&nd->path); 115031e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 11511da177e4SLinus Torvalds } 11521da177e4SLinus Torvalds 11531da177e4SLinus Torvalds /* 1154baa03890SNick Piggin * Allocate a dentry with name and parent, and perform a parent 1155baa03890SNick Piggin * directory ->lookup on it. Returns the new dentry, or ERR_PTR 1156baa03890SNick Piggin * on error. parent->d_inode->i_mutex must be held. d_lookup must 1157baa03890SNick Piggin * have verified that no child exists while under i_mutex. 1158baa03890SNick Piggin */ 1159baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent, 1160baa03890SNick Piggin struct qstr *name, struct nameidata *nd) 1161baa03890SNick Piggin { 1162baa03890SNick Piggin struct inode *inode = parent->d_inode; 1163baa03890SNick Piggin struct dentry *dentry; 1164baa03890SNick Piggin struct dentry *old; 1165baa03890SNick Piggin 1166baa03890SNick Piggin /* Don't create child dentry for a dead directory. */ 1167baa03890SNick Piggin if (unlikely(IS_DEADDIR(inode))) 1168baa03890SNick Piggin return ERR_PTR(-ENOENT); 1169baa03890SNick Piggin 1170baa03890SNick Piggin dentry = d_alloc(parent, name); 1171baa03890SNick Piggin if (unlikely(!dentry)) 1172baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1173baa03890SNick Piggin 1174baa03890SNick Piggin old = inode->i_op->lookup(inode, dentry, nd); 1175baa03890SNick Piggin if (unlikely(old)) { 1176baa03890SNick Piggin dput(dentry); 1177baa03890SNick Piggin dentry = old; 1178baa03890SNick Piggin } 1179baa03890SNick Piggin return dentry; 1180baa03890SNick Piggin } 1181baa03890SNick Piggin 1182baa03890SNick Piggin /* 11831da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 11841da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 11851da177e4SLinus Torvalds * It _is_ time-critical. 11861da177e4SLinus Torvalds */ 11871da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name, 118831e6b01fSNick Piggin struct path *path, struct inode **inode) 11891da177e4SLinus Torvalds { 11904ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 119131e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 11925a18fff2SAl Viro int need_reval = 1; 11935a18fff2SAl Viro int status = 1; 11949875cf80SDavid Howells int err; 11959875cf80SDavid Howells 11963cac260aSAl Viro /* 1197b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1198b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1199b04f784eSNick Piggin * do the non-racy lookup, below. 1200b04f784eSNick Piggin */ 120131e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 120231e6b01fSNick Piggin unsigned seq; 120331e6b01fSNick Piggin *inode = nd->inode; 120431e6b01fSNick Piggin dentry = __d_lookup_rcu(parent, name, &seq, inode); 12055a18fff2SAl Viro if (!dentry) 12065a18fff2SAl Viro goto unlazy; 12075a18fff2SAl Viro 120831e6b01fSNick Piggin /* Memory barrier in read_seqcount_begin of child is enough */ 120931e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 121031e6b01fSNick Piggin return -ECHILD; 121131e6b01fSNick Piggin nd->seq = seq; 12125a18fff2SAl Viro 121324643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 12145a18fff2SAl Viro status = d_revalidate(dentry, nd); 12155a18fff2SAl Viro if (unlikely(status <= 0)) { 12165a18fff2SAl Viro if (status != -ECHILD) 12175a18fff2SAl Viro need_reval = 0; 12185a18fff2SAl Viro goto unlazy; 12195a18fff2SAl Viro } 122024643087SAl Viro } 122131e6b01fSNick Piggin path->mnt = mnt; 122231e6b01fSNick Piggin path->dentry = dentry; 12239875cf80SDavid Howells if (likely(__follow_mount_rcu(nd, path, inode, false))) 12249875cf80SDavid Howells return 0; 12255a18fff2SAl Viro unlazy: 12265a18fff2SAl Viro if (dentry) { 12275a18fff2SAl Viro if (nameidata_dentry_drop_rcu(nd, dentry)) 12285a18fff2SAl Viro return -ECHILD; 12295a18fff2SAl Viro } else { 12309875cf80SDavid Howells if (nameidata_drop_rcu(nd)) 12319875cf80SDavid Howells return -ECHILD; 12329875cf80SDavid Howells } 12335a18fff2SAl Viro } else { 123431e6b01fSNick Piggin dentry = __d_lookup(parent, name); 123524643087SAl Viro } 12365a18fff2SAl Viro 12375a18fff2SAl Viro retry: 12385a18fff2SAl Viro if (unlikely(!dentry)) { 12395a18fff2SAl Viro struct inode *dir = parent->d_inode; 12405a18fff2SAl Viro BUG_ON(nd->inode != dir); 12415a18fff2SAl Viro 12425a18fff2SAl Viro mutex_lock(&dir->i_mutex); 12435a18fff2SAl Viro dentry = d_lookup(parent, name); 12445a18fff2SAl Viro if (likely(!dentry)) { 12455a18fff2SAl Viro dentry = d_alloc_and_lookup(parent, name, nd); 12465a18fff2SAl Viro if (IS_ERR(dentry)) { 12475a18fff2SAl Viro mutex_unlock(&dir->i_mutex); 12485a18fff2SAl Viro return PTR_ERR(dentry); 12495a18fff2SAl Viro } 12505a18fff2SAl Viro /* known good */ 12515a18fff2SAl Viro need_reval = 0; 12525a18fff2SAl Viro status = 1; 12535a18fff2SAl Viro } 12545a18fff2SAl Viro mutex_unlock(&dir->i_mutex); 12555a18fff2SAl Viro } 12565a18fff2SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) 12575a18fff2SAl Viro status = d_revalidate(dentry, nd); 12585a18fff2SAl Viro if (unlikely(status <= 0)) { 12595a18fff2SAl Viro if (status < 0) { 12605a18fff2SAl Viro dput(dentry); 12615a18fff2SAl Viro return status; 12625a18fff2SAl Viro } 12635a18fff2SAl Viro if (!d_invalidate(dentry)) { 12645a18fff2SAl Viro dput(dentry); 12655a18fff2SAl Viro dentry = NULL; 12665a18fff2SAl Viro need_reval = 1; 12675a18fff2SAl Viro goto retry; 12685a18fff2SAl Viro } 12695a18fff2SAl Viro } 12705a18fff2SAl Viro 12711da177e4SLinus Torvalds path->mnt = mnt; 12721da177e4SLinus Torvalds path->dentry = dentry; 12739875cf80SDavid Howells err = follow_managed(path, nd->flags); 127489312214SIan Kent if (unlikely(err < 0)) { 127589312214SIan Kent path_put_conditional(path, nd); 12769875cf80SDavid Howells return err; 127789312214SIan Kent } 127831e6b01fSNick Piggin *inode = path->dentry->d_inode; 12791da177e4SLinus Torvalds return 0; 12801da177e4SLinus Torvalds } 12811da177e4SLinus Torvalds 128252094c8aSAl Viro static inline int may_lookup(struct nameidata *nd) 128352094c8aSAl Viro { 128452094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 128552094c8aSAl Viro int err = exec_permission(nd->inode, IPERM_FLAG_RCU); 128652094c8aSAl Viro if (err != -ECHILD) 128752094c8aSAl Viro return err; 128852094c8aSAl Viro if (nameidata_drop_rcu(nd)) 128952094c8aSAl Viro return -ECHILD; 129052094c8aSAl Viro } 129152094c8aSAl Viro return exec_permission(nd->inode, 0); 129252094c8aSAl Viro } 129352094c8aSAl Viro 12949856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type) 12959856fa1bSAl Viro { 12969856fa1bSAl Viro if (type == LAST_DOTDOT) { 12979856fa1bSAl Viro if (nd->flags & LOOKUP_RCU) { 12989856fa1bSAl Viro if (follow_dotdot_rcu(nd)) 12999856fa1bSAl Viro return -ECHILD; 13009856fa1bSAl Viro } else 13019856fa1bSAl Viro follow_dotdot(nd); 13029856fa1bSAl Viro } 13039856fa1bSAl Viro return 0; 13049856fa1bSAl Viro } 13059856fa1bSAl Viro 1306951361f9SAl Viro static void terminate_walk(struct nameidata *nd) 1307951361f9SAl Viro { 1308951361f9SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1309951361f9SAl Viro path_put(&nd->path); 1310951361f9SAl Viro } else { 1311951361f9SAl Viro nd->flags &= ~LOOKUP_RCU; 13125b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1313951361f9SAl Viro nd->root.mnt = NULL; 1314951361f9SAl Viro rcu_read_unlock(); 1315951361f9SAl Viro br_read_unlock(vfsmount_lock); 1316951361f9SAl Viro } 1317951361f9SAl Viro } 1318951361f9SAl Viro 1319ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path, 1320ce57dfc1SAl Viro struct qstr *name, int type, int follow) 1321ce57dfc1SAl Viro { 1322ce57dfc1SAl Viro struct inode *inode; 1323ce57dfc1SAl Viro int err; 1324ce57dfc1SAl Viro /* 1325ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1326ce57dfc1SAl Viro * to be able to know about the current root directory and 1327ce57dfc1SAl Viro * parent relationships. 1328ce57dfc1SAl Viro */ 1329ce57dfc1SAl Viro if (unlikely(type != LAST_NORM)) 1330ce57dfc1SAl Viro return handle_dots(nd, type); 1331ce57dfc1SAl Viro err = do_lookup(nd, name, path, &inode); 1332ce57dfc1SAl Viro if (unlikely(err)) { 1333ce57dfc1SAl Viro terminate_walk(nd); 1334ce57dfc1SAl Viro return err; 1335ce57dfc1SAl Viro } 1336ce57dfc1SAl Viro if (!inode) { 1337ce57dfc1SAl Viro path_to_nameidata(path, nd); 1338ce57dfc1SAl Viro terminate_walk(nd); 1339ce57dfc1SAl Viro return -ENOENT; 1340ce57dfc1SAl Viro } 1341ce57dfc1SAl Viro if (unlikely(inode->i_op->follow_link) && follow) { 1342ce57dfc1SAl Viro if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry)) 1343ce57dfc1SAl Viro return -ECHILD; 1344ce57dfc1SAl Viro BUG_ON(inode != path->dentry->d_inode); 1345ce57dfc1SAl Viro return 1; 1346ce57dfc1SAl Viro } 1347ce57dfc1SAl Viro path_to_nameidata(path, nd); 1348ce57dfc1SAl Viro nd->inode = inode; 1349ce57dfc1SAl Viro return 0; 1350ce57dfc1SAl Viro } 1351ce57dfc1SAl Viro 13521da177e4SLinus Torvalds /* 1353b356379aSAl Viro * This limits recursive symlink follows to 8, while 1354b356379aSAl Viro * limiting consecutive symlinks to 40. 1355b356379aSAl Viro * 1356b356379aSAl Viro * Without that kind of total limit, nasty chains of consecutive 1357b356379aSAl Viro * symlinks can cause almost arbitrarily long lookups. 1358b356379aSAl Viro */ 1359b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd) 1360b356379aSAl Viro { 1361b356379aSAl Viro int res; 1362b356379aSAl Viro 1363b356379aSAl Viro BUG_ON(nd->depth >= MAX_NESTED_LINKS); 1364b356379aSAl Viro if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { 1365b356379aSAl Viro path_put_conditional(path, nd); 1366b356379aSAl Viro path_put(&nd->path); 1367b356379aSAl Viro return -ELOOP; 1368b356379aSAl Viro } 1369b356379aSAl Viro 1370b356379aSAl Viro nd->depth++; 1371b356379aSAl Viro current->link_count++; 1372b356379aSAl Viro 1373b356379aSAl Viro do { 1374b356379aSAl Viro struct path link = *path; 1375b356379aSAl Viro void *cookie; 1376574197e0SAl Viro 1377574197e0SAl Viro res = follow_link(&link, nd, &cookie); 1378b356379aSAl Viro if (!res) 1379b356379aSAl Viro res = walk_component(nd, path, &nd->last, 1380b356379aSAl Viro nd->last_type, LOOKUP_FOLLOW); 1381574197e0SAl Viro put_link(nd, &link, cookie); 1382b356379aSAl Viro } while (res > 0); 1383b356379aSAl Viro 1384b356379aSAl Viro current->link_count--; 1385b356379aSAl Viro nd->depth--; 1386b356379aSAl Viro return res; 1387b356379aSAl Viro } 1388b356379aSAl Viro 1389b356379aSAl Viro /* 13901da177e4SLinus Torvalds * Name resolution. 1391ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1392ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 13931da177e4SLinus Torvalds * 1394ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1395ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 13961da177e4SLinus Torvalds */ 13976de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 13981da177e4SLinus Torvalds { 13991da177e4SLinus Torvalds struct path next; 14001da177e4SLinus Torvalds int err; 14011da177e4SLinus Torvalds unsigned int lookup_flags = nd->flags; 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds while (*name=='/') 14041da177e4SLinus Torvalds name++; 14051da177e4SLinus Torvalds if (!*name) 1406086e183aSAl Viro return 0; 14071da177e4SLinus Torvalds 14081da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 14091da177e4SLinus Torvalds for(;;) { 14101da177e4SLinus Torvalds unsigned long hash; 14111da177e4SLinus Torvalds struct qstr this; 14121da177e4SLinus Torvalds unsigned int c; 1413fe479a58SAl Viro int type; 14141da177e4SLinus Torvalds 1415cdce5d6bSTrond Myklebust nd->flags |= LOOKUP_CONTINUE; 141652094c8aSAl Viro 141752094c8aSAl Viro err = may_lookup(nd); 14181da177e4SLinus Torvalds if (err) 14191da177e4SLinus Torvalds break; 14201da177e4SLinus Torvalds 14211da177e4SLinus Torvalds this.name = name; 14221da177e4SLinus Torvalds c = *(const unsigned char *)name; 14231da177e4SLinus Torvalds 14241da177e4SLinus Torvalds hash = init_name_hash(); 14251da177e4SLinus Torvalds do { 14261da177e4SLinus Torvalds name++; 14271da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 14281da177e4SLinus Torvalds c = *(const unsigned char *)name; 14291da177e4SLinus Torvalds } while (c && (c != '/')); 14301da177e4SLinus Torvalds this.len = name - (const char *) this.name; 14311da177e4SLinus Torvalds this.hash = end_name_hash(hash); 14321da177e4SLinus Torvalds 1433fe479a58SAl Viro type = LAST_NORM; 1434fe479a58SAl Viro if (this.name[0] == '.') switch (this.len) { 1435fe479a58SAl Viro case 2: 143616c2cd71SAl Viro if (this.name[1] == '.') { 1437fe479a58SAl Viro type = LAST_DOTDOT; 143816c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 143916c2cd71SAl Viro } 1440fe479a58SAl Viro break; 1441fe479a58SAl Viro case 1: 1442fe479a58SAl Viro type = LAST_DOT; 1443fe479a58SAl Viro } 14445a202bcdSAl Viro if (likely(type == LAST_NORM)) { 14455a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 144616c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 14475a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 14485a202bcdSAl Viro err = parent->d_op->d_hash(parent, nd->inode, 14495a202bcdSAl Viro &this); 14505a202bcdSAl Viro if (err < 0) 14515a202bcdSAl Viro break; 14525a202bcdSAl Viro } 14535a202bcdSAl Viro } 1454fe479a58SAl Viro 14551da177e4SLinus Torvalds /* remove trailing slashes? */ 14561da177e4SLinus Torvalds if (!c) 14571da177e4SLinus Torvalds goto last_component; 14581da177e4SLinus Torvalds while (*++name == '/'); 14591da177e4SLinus Torvalds if (!*name) 1460b356379aSAl Viro goto last_component; 14611da177e4SLinus Torvalds 1462ce57dfc1SAl Viro err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW); 1463ce57dfc1SAl Viro if (err < 0) 1464ce57dfc1SAl Viro return err; 1465fe479a58SAl Viro 1466ce57dfc1SAl Viro if (err) { 1467b356379aSAl Viro err = nested_symlink(&next, nd); 14681da177e4SLinus Torvalds if (err) 1469a7472babSAl Viro return err; 147031e6b01fSNick Piggin } 14711da177e4SLinus Torvalds err = -ENOTDIR; 147231e6b01fSNick Piggin if (!nd->inode->i_op->lookup) 14731da177e4SLinus Torvalds break; 14741da177e4SLinus Torvalds continue; 14751da177e4SLinus Torvalds /* here ends the main loop */ 14761da177e4SLinus Torvalds 14771da177e4SLinus Torvalds last_component: 1478f55eab82STrond Myklebust /* Clear LOOKUP_CONTINUE iff it was previously unset */ 1479f55eab82STrond Myklebust nd->flags &= lookup_flags | ~LOOKUP_CONTINUE; 1480ce57dfc1SAl Viro nd->last = this; 1481ce57dfc1SAl Viro nd->last_type = type; 1482ce57dfc1SAl Viro return 0; 1483ce57dfc1SAl Viro } 1484951361f9SAl Viro terminate_walk(nd); 14851da177e4SLinus Torvalds return err; 14861da177e4SLinus Torvalds } 14871da177e4SLinus Torvalds 148870e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, 148970e9b357SAl Viro struct nameidata *nd, struct file **fp) 149031e6b01fSNick Piggin { 149131e6b01fSNick Piggin int retval = 0; 149231e6b01fSNick Piggin int fput_needed; 149331e6b01fSNick Piggin struct file *file; 149431e6b01fSNick Piggin 149531e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 149616c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 149731e6b01fSNick Piggin nd->depth = 0; 14985b6ca027SAl Viro if (flags & LOOKUP_ROOT) { 14995b6ca027SAl Viro struct inode *inode = nd->root.dentry->d_inode; 150073d049a4SAl Viro if (*name) { 15015b6ca027SAl Viro if (!inode->i_op->lookup) 15025b6ca027SAl Viro return -ENOTDIR; 15035b6ca027SAl Viro retval = inode_permission(inode, MAY_EXEC); 15045b6ca027SAl Viro if (retval) 15055b6ca027SAl Viro return retval; 150673d049a4SAl Viro } 15075b6ca027SAl Viro nd->path = nd->root; 15085b6ca027SAl Viro nd->inode = inode; 15095b6ca027SAl Viro if (flags & LOOKUP_RCU) { 15105b6ca027SAl Viro br_read_lock(vfsmount_lock); 15115b6ca027SAl Viro rcu_read_lock(); 15125b6ca027SAl Viro nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 15135b6ca027SAl Viro } else { 15145b6ca027SAl Viro path_get(&nd->path); 15155b6ca027SAl Viro } 15165b6ca027SAl Viro return 0; 15175b6ca027SAl Viro } 15185b6ca027SAl Viro 151931e6b01fSNick Piggin nd->root.mnt = NULL; 152031e6b01fSNick Piggin 152131e6b01fSNick Piggin if (*name=='/') { 1522e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 152331e6b01fSNick Piggin br_read_lock(vfsmount_lock); 152431e6b01fSNick Piggin rcu_read_lock(); 1525e41f7d4eSAl Viro set_root_rcu(nd); 1526e41f7d4eSAl Viro } else { 1527e41f7d4eSAl Viro set_root(nd); 1528e41f7d4eSAl Viro path_get(&nd->root); 1529e41f7d4eSAl Viro } 153031e6b01fSNick Piggin nd->path = nd->root; 153131e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 1532e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 153331e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1534c28cc364SNick Piggin unsigned seq; 153531e6b01fSNick Piggin 153631e6b01fSNick Piggin br_read_lock(vfsmount_lock); 153731e6b01fSNick Piggin rcu_read_lock(); 153831e6b01fSNick Piggin 1539c28cc364SNick Piggin do { 1540c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 154131e6b01fSNick Piggin nd->path = fs->pwd; 1542c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1543c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1544e41f7d4eSAl Viro } else { 1545e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 1546e41f7d4eSAl Viro } 154731e6b01fSNick Piggin } else { 154831e6b01fSNick Piggin struct dentry *dentry; 154931e6b01fSNick Piggin 15501abf0c71SAl Viro file = fget_raw_light(dfd, &fput_needed); 155131e6b01fSNick Piggin retval = -EBADF; 155231e6b01fSNick Piggin if (!file) 155331e6b01fSNick Piggin goto out_fail; 155431e6b01fSNick Piggin 155531e6b01fSNick Piggin dentry = file->f_path.dentry; 155631e6b01fSNick Piggin 1557f52e0c11SAl Viro if (*name) { 155831e6b01fSNick Piggin retval = -ENOTDIR; 155931e6b01fSNick Piggin if (!S_ISDIR(dentry->d_inode->i_mode)) 156031e6b01fSNick Piggin goto fput_fail; 156131e6b01fSNick Piggin 156231e6b01fSNick Piggin retval = file_permission(file, MAY_EXEC); 156331e6b01fSNick Piggin if (retval) 156431e6b01fSNick Piggin goto fput_fail; 1565f52e0c11SAl Viro } 156631e6b01fSNick Piggin 156731e6b01fSNick Piggin nd->path = file->f_path; 1568e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 156931e6b01fSNick Piggin if (fput_needed) 157070e9b357SAl Viro *fp = file; 1571c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 157231e6b01fSNick Piggin br_read_lock(vfsmount_lock); 157331e6b01fSNick Piggin rcu_read_lock(); 15745590ff0dSUlrich Drepper } else { 15755dd784d0SJan Blunck path_get(&file->f_path); 15765590ff0dSUlrich Drepper fput_light(file, fput_needed); 15771da177e4SLinus Torvalds } 1578e41f7d4eSAl Viro } 1579e41f7d4eSAl Viro 158031e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 15819b4a9b14SAl Viro return 0; 15822dfdd266SJosef 'Jeff' Sipek 15839b4a9b14SAl Viro fput_fail: 15849b4a9b14SAl Viro fput_light(file, fput_needed); 15859b4a9b14SAl Viro out_fail: 15869b4a9b14SAl Viro return retval; 15879b4a9b14SAl Viro } 15889b4a9b14SAl Viro 1589bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path) 1590bd92d7feSAl Viro { 1591bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 1592bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 1593bd92d7feSAl Viro 1594bd92d7feSAl Viro nd->flags &= ~LOOKUP_PARENT; 1595bd92d7feSAl Viro return walk_component(nd, path, &nd->last, nd->last_type, 1596bd92d7feSAl Viro nd->flags & LOOKUP_FOLLOW); 1597bd92d7feSAl Viro } 1598bd92d7feSAl Viro 15999b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1600ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name, 16019b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 16029b4a9b14SAl Viro { 160370e9b357SAl Viro struct file *base = NULL; 1604bd92d7feSAl Viro struct path path; 1605bd92d7feSAl Viro int err; 160631e6b01fSNick Piggin 160731e6b01fSNick Piggin /* 160831e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 160931e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 161031e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 161131e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 161231e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 161331e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 161431e6b01fSNick Piggin * analogue, foo_rcu(). 161531e6b01fSNick Piggin * 161631e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 161731e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 161831e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 161931e6b01fSNick Piggin * be able to complete). 162031e6b01fSNick Piggin */ 1621bd92d7feSAl Viro err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); 1622ee0827cdSAl Viro 1623bd92d7feSAl Viro if (unlikely(err)) 1624bd92d7feSAl Viro return err; 1625ee0827cdSAl Viro 1626ee0827cdSAl Viro current->total_link_count = 0; 1627bd92d7feSAl Viro err = link_path_walk(name, nd); 1628bd92d7feSAl Viro 1629bd92d7feSAl Viro if (!err && !(flags & LOOKUP_PARENT)) { 1630bd92d7feSAl Viro err = lookup_last(nd, &path); 1631bd92d7feSAl Viro while (err > 0) { 1632bd92d7feSAl Viro void *cookie; 1633bd92d7feSAl Viro struct path link = path; 1634bd92d7feSAl Viro nd->flags |= LOOKUP_PARENT; 1635574197e0SAl Viro err = follow_link(&link, nd, &cookie); 1636bd92d7feSAl Viro if (!err) 1637bd92d7feSAl Viro err = lookup_last(nd, &path); 1638574197e0SAl Viro put_link(nd, &link, cookie); 1639bd92d7feSAl Viro } 1640bd92d7feSAl Viro } 1641ee0827cdSAl Viro 1642ee0827cdSAl Viro if (nd->flags & LOOKUP_RCU) { 16434455ca62SAl Viro /* went all way through without dropping RCU */ 1644bd92d7feSAl Viro BUG_ON(err); 1645086e183aSAl Viro if (nameidata_drop_rcu_last(nd)) 1646bd92d7feSAl Viro err = -ECHILD; 1647086e183aSAl Viro } 164831e6b01fSNick Piggin 1649bd92d7feSAl Viro if (!err) 1650bd92d7feSAl Viro err = handle_reval_path(nd); 1651bd92d7feSAl Viro 1652bd92d7feSAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) { 1653bd92d7feSAl Viro if (!nd->inode->i_op->lookup) { 1654bd92d7feSAl Viro path_put(&nd->path); 1655bd92d7feSAl Viro return -ENOTDIR; 1656bd92d7feSAl Viro } 1657bd92d7feSAl Viro } 165816c2cd71SAl Viro 165970e9b357SAl Viro if (base) 166070e9b357SAl Viro fput(base); 1661ee0827cdSAl Viro 16625b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 166331e6b01fSNick Piggin path_put(&nd->root); 166431e6b01fSNick Piggin nd->root.mnt = NULL; 166531e6b01fSNick Piggin } 1666bd92d7feSAl Viro return err; 166731e6b01fSNick Piggin } 166831e6b01fSNick Piggin 1669ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name, 1670ee0827cdSAl Viro unsigned int flags, struct nameidata *nd) 1671ee0827cdSAl Viro { 1672ee0827cdSAl Viro int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd); 1673ee0827cdSAl Viro if (unlikely(retval == -ECHILD)) 1674ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags, nd); 1675ee0827cdSAl Viro if (unlikely(retval == -ESTALE)) 1676ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd); 1677ee0827cdSAl Viro 167831e6b01fSNick Piggin if (likely(!retval)) { 167931e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) { 168031e6b01fSNick Piggin if (nd->path.dentry && nd->inode) 168131e6b01fSNick Piggin audit_inode(name, nd->path.dentry); 168231e6b01fSNick Piggin } 168331e6b01fSNick Piggin } 1684170aa3d0SUlrich Drepper return retval; 16851da177e4SLinus Torvalds } 16861da177e4SLinus Torvalds 1687c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd) 16885590ff0dSUlrich Drepper { 1689c9c6cac0SAl Viro return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd); 16905590ff0dSUlrich Drepper } 16915590ff0dSUlrich Drepper 1692d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1693d1811465SAl Viro { 1694d1811465SAl Viro struct nameidata nd; 1695d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1696d1811465SAl Viro if (!res) 1697d1811465SAl Viro *path = nd.path; 1698d1811465SAl Viro return res; 1699d1811465SAl Viro } 1700d1811465SAl Viro 170116f18200SJosef 'Jeff' Sipek /** 170216f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 170316f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 170416f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 170516f18200SJosef 'Jeff' Sipek * @name: pointer to file name 170616f18200SJosef 'Jeff' Sipek * @flags: lookup flags 170716f18200SJosef 'Jeff' Sipek * @nd: pointer to nameidata 170816f18200SJosef 'Jeff' Sipek */ 170916f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 171016f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 171116f18200SJosef 'Jeff' Sipek struct nameidata *nd) 171216f18200SJosef 'Jeff' Sipek { 17135b6ca027SAl Viro nd->root.dentry = dentry; 17145b6ca027SAl Viro nd->root.mnt = mnt; 17155b6ca027SAl Viro /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ 17165b6ca027SAl Viro return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd); 171716f18200SJosef 'Jeff' Sipek } 171816f18200SJosef 'Jeff' Sipek 1719eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name, 1720eead1911SChristoph Hellwig struct dentry *base, struct nameidata *nd) 17211da177e4SLinus Torvalds { 172281fca444SChristoph Hellwig struct inode *inode = base->d_inode; 17231da177e4SLinus Torvalds struct dentry *dentry; 17241da177e4SLinus Torvalds int err; 17251da177e4SLinus Torvalds 1726b74c79e9SNick Piggin err = exec_permission(inode, 0); 172781fca444SChristoph Hellwig if (err) 172881fca444SChristoph Hellwig return ERR_PTR(err); 17291da177e4SLinus Torvalds 17301da177e4SLinus Torvalds /* 1731b04f784eSNick Piggin * Don't bother with __d_lookup: callers are for creat as 1732b04f784eSNick Piggin * well as unlink, so a lot of the time it would cost 1733b04f784eSNick Piggin * a double lookup. 17346e6b1bd1SAl Viro */ 17356e6b1bd1SAl Viro dentry = d_lookup(base, name); 17366e6b1bd1SAl Viro 1737fb045adbSNick Piggin if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) 17386e6b1bd1SAl Viro dentry = do_revalidate(dentry, nd); 17396e6b1bd1SAl Viro 17401da177e4SLinus Torvalds if (!dentry) 1741baa03890SNick Piggin dentry = d_alloc_and_lookup(base, name, nd); 17425a202bcdSAl Viro 17431da177e4SLinus Torvalds return dentry; 17441da177e4SLinus Torvalds } 17451da177e4SLinus Torvalds 1746057f6c01SJames Morris /* 1747057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1748057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1749057f6c01SJames Morris * SMP-safe. 1750057f6c01SJames Morris */ 1751a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 17521da177e4SLinus Torvalds { 17534ac91378SJan Blunck return __lookup_hash(&nd->last, nd->path.dentry, nd); 17541da177e4SLinus Torvalds } 17551da177e4SLinus Torvalds 1756eead1911SChristoph Hellwig /** 1757a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1758eead1911SChristoph Hellwig * @name: pathname component to lookup 1759eead1911SChristoph Hellwig * @base: base directory to lookup from 1760eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1761eead1911SChristoph Hellwig * 1762a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1763a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1764eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1765eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1766eead1911SChristoph Hellwig */ 1767057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1768057f6c01SJames Morris { 1769057f6c01SJames Morris struct qstr this; 17706a96ba54SAl Viro unsigned long hash; 17716a96ba54SAl Viro unsigned int c; 1772057f6c01SJames Morris 17732f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 17742f9092e1SDavid Woodhouse 17756a96ba54SAl Viro this.name = name; 17766a96ba54SAl Viro this.len = len; 17776a96ba54SAl Viro if (!len) 17786a96ba54SAl Viro return ERR_PTR(-EACCES); 17796a96ba54SAl Viro 17806a96ba54SAl Viro hash = init_name_hash(); 17816a96ba54SAl Viro while (len--) { 17826a96ba54SAl Viro c = *(const unsigned char *)name++; 17836a96ba54SAl Viro if (c == '/' || c == '\0') 17846a96ba54SAl Viro return ERR_PTR(-EACCES); 17856a96ba54SAl Viro hash = partial_name_hash(c, hash); 17866a96ba54SAl Viro } 17876a96ba54SAl Viro this.hash = end_name_hash(hash); 17885a202bcdSAl Viro /* 17895a202bcdSAl Viro * See if the low-level filesystem might want 17905a202bcdSAl Viro * to use its own hash.. 17915a202bcdSAl Viro */ 17925a202bcdSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 17935a202bcdSAl Viro int err = base->d_op->d_hash(base, base->d_inode, &this); 17945a202bcdSAl Viro if (err < 0) 17955a202bcdSAl Viro return ERR_PTR(err); 17965a202bcdSAl Viro } 1797eead1911SChristoph Hellwig 179849705b77SChristoph Hellwig return __lookup_hash(&this, base, NULL); 1799057f6c01SJames Morris } 1800057f6c01SJames Morris 18012d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags, 18022d8f3038SAl Viro struct path *path) 18031da177e4SLinus Torvalds { 18042d8f3038SAl Viro struct nameidata nd; 1805f52e0c11SAl Viro char *tmp = getname_flags(name, flags); 18061da177e4SLinus Torvalds int err = PTR_ERR(tmp); 18071da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 18082d8f3038SAl Viro 18092d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 18102d8f3038SAl Viro 18112d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 18121da177e4SLinus Torvalds putname(tmp); 18132d8f3038SAl Viro if (!err) 18142d8f3038SAl Viro *path = nd.path; 18151da177e4SLinus Torvalds } 18161da177e4SLinus Torvalds return err; 18171da177e4SLinus Torvalds } 18181da177e4SLinus Torvalds 18192ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 18202ad94ae6SAl Viro struct nameidata *nd, char **name) 18212ad94ae6SAl Viro { 18222ad94ae6SAl Viro char *s = getname(path); 18232ad94ae6SAl Viro int error; 18242ad94ae6SAl Viro 18252ad94ae6SAl Viro if (IS_ERR(s)) 18262ad94ae6SAl Viro return PTR_ERR(s); 18272ad94ae6SAl Viro 18282ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 18292ad94ae6SAl Viro if (error) 18302ad94ae6SAl Viro putname(s); 18312ad94ae6SAl Viro else 18322ad94ae6SAl Viro *name = s; 18332ad94ae6SAl Viro 18342ad94ae6SAl Viro return error; 18352ad94ae6SAl Viro } 18362ad94ae6SAl Viro 18371da177e4SLinus Torvalds /* 18381da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 18391da177e4SLinus Torvalds * minimal. 18401da177e4SLinus Torvalds */ 18411da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 18421da177e4SLinus Torvalds { 1843da9592edSDavid Howells uid_t fsuid = current_fsuid(); 1844da9592edSDavid Howells 18451da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 18461da177e4SLinus Torvalds return 0; 1847da9592edSDavid Howells if (inode->i_uid == fsuid) 18481da177e4SLinus Torvalds return 0; 1849da9592edSDavid Howells if (dir->i_uid == fsuid) 18501da177e4SLinus Torvalds return 0; 18511da177e4SLinus Torvalds return !capable(CAP_FOWNER); 18521da177e4SLinus Torvalds } 18531da177e4SLinus Torvalds 18541da177e4SLinus Torvalds /* 18551da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 18561da177e4SLinus Torvalds * whether the type of victim is right. 18571da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 18581da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 18591da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 18601da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 18611da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 18621da177e4SLinus Torvalds * a. be owner of dir, or 18631da177e4SLinus Torvalds * b. be owner of victim, or 18641da177e4SLinus Torvalds * c. have CAP_FOWNER capability 18651da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 18661da177e4SLinus Torvalds * links pointing to it. 18671da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 18681da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 18691da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 18701da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 18711da177e4SLinus Torvalds * nfs_async_unlink(). 18721da177e4SLinus Torvalds */ 1873858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 18741da177e4SLinus Torvalds { 18751da177e4SLinus Torvalds int error; 18761da177e4SLinus Torvalds 18771da177e4SLinus Torvalds if (!victim->d_inode) 18781da177e4SLinus Torvalds return -ENOENT; 18791da177e4SLinus Torvalds 18801da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 1881cccc6bbaSAl Viro audit_inode_child(victim, dir); 18821da177e4SLinus Torvalds 1883f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 18841da177e4SLinus Torvalds if (error) 18851da177e4SLinus Torvalds return error; 18861da177e4SLinus Torvalds if (IS_APPEND(dir)) 18871da177e4SLinus Torvalds return -EPERM; 18881da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 1889f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 18901da177e4SLinus Torvalds return -EPERM; 18911da177e4SLinus Torvalds if (isdir) { 18921da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 18931da177e4SLinus Torvalds return -ENOTDIR; 18941da177e4SLinus Torvalds if (IS_ROOT(victim)) 18951da177e4SLinus Torvalds return -EBUSY; 18961da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 18971da177e4SLinus Torvalds return -EISDIR; 18981da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 18991da177e4SLinus Torvalds return -ENOENT; 19001da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 19011da177e4SLinus Torvalds return -EBUSY; 19021da177e4SLinus Torvalds return 0; 19031da177e4SLinus Torvalds } 19041da177e4SLinus Torvalds 19051da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 19061da177e4SLinus Torvalds * dir. 19071da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 19081da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 19091da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 19101da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 19111da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 19121da177e4SLinus Torvalds */ 1913a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 19141da177e4SLinus Torvalds { 19151da177e4SLinus Torvalds if (child->d_inode) 19161da177e4SLinus Torvalds return -EEXIST; 19171da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 19181da177e4SLinus Torvalds return -ENOENT; 1919f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 19201da177e4SLinus Torvalds } 19211da177e4SLinus Torvalds 19221da177e4SLinus Torvalds /* 19231da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 19241da177e4SLinus Torvalds */ 19251da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 19261da177e4SLinus Torvalds { 19271da177e4SLinus Torvalds struct dentry *p; 19281da177e4SLinus Torvalds 19291da177e4SLinus Torvalds if (p1 == p2) { 1930f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 19311da177e4SLinus Torvalds return NULL; 19321da177e4SLinus Torvalds } 19331da177e4SLinus Torvalds 1934a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 19351da177e4SLinus Torvalds 1936e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 1937e2761a11SOGAWA Hirofumi if (p) { 1938f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 1939f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 19401da177e4SLinus Torvalds return p; 19411da177e4SLinus Torvalds } 19421da177e4SLinus Torvalds 1943e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 1944e2761a11SOGAWA Hirofumi if (p) { 1945f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1946f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 19471da177e4SLinus Torvalds return p; 19481da177e4SLinus Torvalds } 19491da177e4SLinus Torvalds 1950f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1951f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 19521da177e4SLinus Torvalds return NULL; 19531da177e4SLinus Torvalds } 19541da177e4SLinus Torvalds 19551da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 19561da177e4SLinus Torvalds { 19571b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 19581da177e4SLinus Torvalds if (p1 != p2) { 19591b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 1960a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 19611da177e4SLinus Torvalds } 19621da177e4SLinus Torvalds } 19631da177e4SLinus Torvalds 19641da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode, 19651da177e4SLinus Torvalds struct nameidata *nd) 19661da177e4SLinus Torvalds { 1967a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 19681da177e4SLinus Torvalds 19691da177e4SLinus Torvalds if (error) 19701da177e4SLinus Torvalds return error; 19711da177e4SLinus Torvalds 1972acfa4380SAl Viro if (!dir->i_op->create) 19731da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 19741da177e4SLinus Torvalds mode &= S_IALLUGO; 19751da177e4SLinus Torvalds mode |= S_IFREG; 19761da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 19771da177e4SLinus Torvalds if (error) 19781da177e4SLinus Torvalds return error; 19791da177e4SLinus Torvalds error = dir->i_op->create(dir, dentry, mode, nd); 1980a74574aaSStephen Smalley if (!error) 1981f38aa942SAmy Griffis fsnotify_create(dir, dentry); 19821da177e4SLinus Torvalds return error; 19831da177e4SLinus Torvalds } 19841da177e4SLinus Torvalds 198573d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag) 19861da177e4SLinus Torvalds { 19873fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 19881da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 19891da177e4SLinus Torvalds int error; 19901da177e4SLinus Torvalds 1991bcda7652SAl Viro /* O_PATH? */ 1992bcda7652SAl Viro if (!acc_mode) 1993bcda7652SAl Viro return 0; 1994bcda7652SAl Viro 19951da177e4SLinus Torvalds if (!inode) 19961da177e4SLinus Torvalds return -ENOENT; 19971da177e4SLinus Torvalds 1998c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 1999c8fe8f30SChristoph Hellwig case S_IFLNK: 20001da177e4SLinus Torvalds return -ELOOP; 2001c8fe8f30SChristoph Hellwig case S_IFDIR: 2002c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 20031da177e4SLinus Torvalds return -EISDIR; 2004c8fe8f30SChristoph Hellwig break; 2005c8fe8f30SChristoph Hellwig case S_IFBLK: 2006c8fe8f30SChristoph Hellwig case S_IFCHR: 20073fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 20081da177e4SLinus Torvalds return -EACCES; 2009c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 2010c8fe8f30SChristoph Hellwig case S_IFIFO: 2011c8fe8f30SChristoph Hellwig case S_IFSOCK: 20121da177e4SLinus Torvalds flag &= ~O_TRUNC; 2013c8fe8f30SChristoph Hellwig break; 20144a3fd211SDave Hansen } 2015b41572e9SDave Hansen 20163fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 2017b41572e9SDave Hansen if (error) 2018b41572e9SDave Hansen return error; 20196146f0d5SMimi Zohar 20201da177e4SLinus Torvalds /* 20211da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 20221da177e4SLinus Torvalds */ 20231da177e4SLinus Torvalds if (IS_APPEND(inode)) { 20248737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 20257715b521SAl Viro return -EPERM; 20261da177e4SLinus Torvalds if (flag & O_TRUNC) 20277715b521SAl Viro return -EPERM; 20281da177e4SLinus Torvalds } 20291da177e4SLinus Torvalds 20301da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 20317715b521SAl Viro if (flag & O_NOATIME && !is_owner_or_cap(inode)) 20327715b521SAl Viro return -EPERM; 20331da177e4SLinus Torvalds 20341da177e4SLinus Torvalds /* 20351da177e4SLinus Torvalds * Ensure there are no outstanding leases on the file. 20361da177e4SLinus Torvalds */ 2037b65a9cfcSAl Viro return break_lease(inode, flag); 20387715b521SAl Viro } 20397715b521SAl Viro 2040e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 20417715b521SAl Viro { 2042e1181ee6SJeff Layton struct path *path = &filp->f_path; 20437715b521SAl Viro struct inode *inode = path->dentry->d_inode; 20447715b521SAl Viro int error = get_write_access(inode); 20451da177e4SLinus Torvalds if (error) 20467715b521SAl Viro return error; 20471da177e4SLinus Torvalds /* 20481da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 20491da177e4SLinus Torvalds */ 20501da177e4SLinus Torvalds error = locks_verify_locked(inode); 2051be6d3e56SKentaro Takeda if (!error) 2052ea0d3ab2STetsuo Handa error = security_path_truncate(path); 20531da177e4SLinus Torvalds if (!error) { 20547715b521SAl Viro error = do_truncate(path->dentry, 0, 2055d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 2056e1181ee6SJeff Layton filp); 20571da177e4SLinus Torvalds } 20581da177e4SLinus Torvalds put_write_access(inode); 2059acd0c935SMimi Zohar return error; 20601da177e4SLinus Torvalds } 20611da177e4SLinus Torvalds 2062d57999e1SDave Hansen /* 2063d57999e1SDave Hansen * Note that while the flag value (low two bits) for sys_open means: 2064d57999e1SDave Hansen * 00 - read-only 2065d57999e1SDave Hansen * 01 - write-only 2066d57999e1SDave Hansen * 10 - read-write 2067d57999e1SDave Hansen * 11 - special 2068d57999e1SDave Hansen * it is changed into 2069d57999e1SDave Hansen * 00 - no permissions needed 2070d57999e1SDave Hansen * 01 - read-permission 2071d57999e1SDave Hansen * 10 - write-permission 2072d57999e1SDave Hansen * 11 - read-write 2073d57999e1SDave Hansen * for the internal routines (ie open_namei()/follow_link() etc) 2074d57999e1SDave Hansen * This is more logical, and also allows the 00 "no perm needed" 2075d57999e1SDave Hansen * to be used for symlinks (where the permissions are checked 2076d57999e1SDave Hansen * later). 2077d57999e1SDave Hansen * 2078d57999e1SDave Hansen */ 2079d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2080d57999e1SDave Hansen { 2081d57999e1SDave Hansen if ((flag+1) & O_ACCMODE) 2082d57999e1SDave Hansen flag++; 2083d57999e1SDave Hansen return flag; 2084d57999e1SDave Hansen } 2085d57999e1SDave Hansen 208631e6b01fSNick Piggin /* 2087fe2d35ffSAl Viro * Handle the last step of open() 208831e6b01fSNick Piggin */ 2089fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path, 2090c3e380b0SAl Viro const struct open_flags *op, const char *pathname) 2091fb1cc555SAl Viro { 2092a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 20936c0d46c4SAl Viro struct dentry *dentry; 2094ca344a89SAl Viro int open_flag = op->open_flag; 20956c0d46c4SAl Viro int will_truncate = open_flag & O_TRUNC; 2096ca344a89SAl Viro int want_write = 0; 2097bcda7652SAl Viro int acc_mode = op->acc_mode; 2098fb1cc555SAl Viro struct file *filp; 209916c2cd71SAl Viro int error; 2100fb1cc555SAl Viro 2101c3e380b0SAl Viro nd->flags &= ~LOOKUP_PARENT; 2102c3e380b0SAl Viro nd->flags |= op->intent; 2103c3e380b0SAl Viro 21041f36f774SAl Viro switch (nd->last_type) { 21051f36f774SAl Viro case LAST_DOTDOT: 2106176306f5SNeil Brown case LAST_DOT: 2107fe2d35ffSAl Viro error = handle_dots(nd, nd->last_type); 2108fe2d35ffSAl Viro if (error) 2109fe2d35ffSAl Viro return ERR_PTR(error); 21101f36f774SAl Viro /* fallthrough */ 21111f36f774SAl Viro case LAST_ROOT: 2112fe2d35ffSAl Viro if (nd->flags & LOOKUP_RCU) { 2113fe2d35ffSAl Viro if (nameidata_drop_rcu_last(nd)) 2114fe2d35ffSAl Viro return ERR_PTR(-ECHILD); 2115fe2d35ffSAl Viro } 211616c2cd71SAl Viro error = handle_reval_path(nd); 211716c2cd71SAl Viro if (error) 211816c2cd71SAl Viro goto exit; 2119fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2120ca344a89SAl Viro if (open_flag & O_CREAT) { 212116c2cd71SAl Viro error = -EISDIR; 21221f36f774SAl Viro goto exit; 2123fe2d35ffSAl Viro } 2124fe2d35ffSAl Viro goto ok; 21251f36f774SAl Viro case LAST_BIND: 2126fe2d35ffSAl Viro /* can't be RCU mode here */ 212716c2cd71SAl Viro error = handle_reval_path(nd); 212816c2cd71SAl Viro if (error) 212916c2cd71SAl Viro goto exit; 21301f36f774SAl Viro audit_inode(pathname, dir); 21311f36f774SAl Viro goto ok; 21321f36f774SAl Viro } 2133a2c36b45SAl Viro 2134ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 2135bcda7652SAl Viro int symlink_ok = 0; 2136fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 2137fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2138bcda7652SAl Viro if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) 2139bcda7652SAl Viro symlink_ok = 1; 2140fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 2141ce57dfc1SAl Viro error = walk_component(nd, path, &nd->last, LAST_NORM, 2142ce57dfc1SAl Viro !symlink_ok); 2143ce57dfc1SAl Viro if (error < 0) 2144fe2d35ffSAl Viro return ERR_PTR(error); 2145ce57dfc1SAl Viro if (error) /* symlink */ 2146fe2d35ffSAl Viro return NULL; 2147fe2d35ffSAl Viro /* sayonara */ 2148fe2d35ffSAl Viro if (nd->flags & LOOKUP_RCU) { 2149fe2d35ffSAl Viro if (nameidata_drop_rcu_last(nd)) 2150fe2d35ffSAl Viro return ERR_PTR(-ECHILD); 2151fe2d35ffSAl Viro } 2152fe2d35ffSAl Viro 2153fe2d35ffSAl Viro error = -ENOTDIR; 2154fe2d35ffSAl Viro if (nd->flags & LOOKUP_DIRECTORY) { 2155ce57dfc1SAl Viro if (!nd->inode->i_op->lookup) 2156fe2d35ffSAl Viro goto exit; 2157fe2d35ffSAl Viro } 2158fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2159fe2d35ffSAl Viro goto ok; 2160fe2d35ffSAl Viro } 2161fe2d35ffSAl Viro 2162fe2d35ffSAl Viro /* create side of things */ 2163fe2d35ffSAl Viro 2164fe2d35ffSAl Viro if (nd->flags & LOOKUP_RCU) { 2165fe2d35ffSAl Viro if (nameidata_drop_rcu_last(nd)) 2166fe2d35ffSAl Viro return ERR_PTR(-ECHILD); 2167fe2d35ffSAl Viro } 2168fe2d35ffSAl Viro 2169fe2d35ffSAl Viro audit_inode(pathname, dir); 217016c2cd71SAl Viro error = -EISDIR; 21711f36f774SAl Viro /* trailing slashes? */ 217231e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 21731f36f774SAl Viro goto exit; 21741f36f774SAl Viro 2175a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 2176a1e28038SAl Viro 21776c0d46c4SAl Viro dentry = lookup_hash(nd); 21786c0d46c4SAl Viro error = PTR_ERR(dentry); 21796c0d46c4SAl Viro if (IS_ERR(dentry)) { 2180fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2181fb1cc555SAl Viro goto exit; 2182fb1cc555SAl Viro } 2183fb1cc555SAl Viro 21846c0d46c4SAl Viro path->dentry = dentry; 21856c0d46c4SAl Viro path->mnt = nd->path.mnt; 21866c0d46c4SAl Viro 2187fb1cc555SAl Viro /* Negative dentry, just create the file */ 21886c0d46c4SAl Viro if (!dentry->d_inode) { 21896c0d46c4SAl Viro int mode = op->mode; 21906c0d46c4SAl Viro if (!IS_POSIXACL(dir->d_inode)) 21916c0d46c4SAl Viro mode &= ~current_umask(); 2192fb1cc555SAl Viro /* 2193fb1cc555SAl Viro * This write is needed to ensure that a 21946c0d46c4SAl Viro * rw->ro transition does not occur between 2195fb1cc555SAl Viro * the time when the file is created and when 2196fb1cc555SAl Viro * a permanent write count is taken through 2197fb1cc555SAl Viro * the 'struct file' in nameidata_to_filp(). 2198fb1cc555SAl Viro */ 2199fb1cc555SAl Viro error = mnt_want_write(nd->path.mnt); 2200fb1cc555SAl Viro if (error) 2201fb1cc555SAl Viro goto exit_mutex_unlock; 2202ca344a89SAl Viro want_write = 1; 22039b44f1b3SAl Viro /* Don't check for write permission, don't truncate */ 2204ca344a89SAl Viro open_flag &= ~O_TRUNC; 22056c0d46c4SAl Viro will_truncate = 0; 2206bcda7652SAl Viro acc_mode = MAY_OPEN; 22076c0d46c4SAl Viro error = security_path_mknod(&nd->path, dentry, mode, 0); 22086c0d46c4SAl Viro if (error) 22096c0d46c4SAl Viro goto exit_mutex_unlock; 22106c0d46c4SAl Viro error = vfs_create(dir->d_inode, dentry, mode, nd); 22116c0d46c4SAl Viro if (error) 22126c0d46c4SAl Viro goto exit_mutex_unlock; 22136c0d46c4SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 22146c0d46c4SAl Viro dput(nd->path.dentry); 22156c0d46c4SAl Viro nd->path.dentry = dentry; 2216ca344a89SAl Viro goto common; 2217fb1cc555SAl Viro } 2218fb1cc555SAl Viro 2219fb1cc555SAl Viro /* 2220fb1cc555SAl Viro * It already exists. 2221fb1cc555SAl Viro */ 2222fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2223fb1cc555SAl Viro audit_inode(pathname, path->dentry); 2224fb1cc555SAl Viro 2225fb1cc555SAl Viro error = -EEXIST; 2226ca344a89SAl Viro if (open_flag & O_EXCL) 2227fb1cc555SAl Viro goto exit_dput; 2228fb1cc555SAl Viro 22299875cf80SDavid Howells error = follow_managed(path, nd->flags); 22309875cf80SDavid Howells if (error < 0) 2231fb1cc555SAl Viro goto exit_dput; 2232fb1cc555SAl Viro 2233fb1cc555SAl Viro error = -ENOENT; 2234fb1cc555SAl Viro if (!path->dentry->d_inode) 2235fb1cc555SAl Viro goto exit_dput; 22369e67f361SAl Viro 22379e67f361SAl Viro if (path->dentry->d_inode->i_op->follow_link) 2238fb1cc555SAl Viro return NULL; 2239fb1cc555SAl Viro 2240fb1cc555SAl Viro path_to_nameidata(path, nd); 224131e6b01fSNick Piggin nd->inode = path->dentry->d_inode; 2242fb1cc555SAl Viro error = -EISDIR; 224331e6b01fSNick Piggin if (S_ISDIR(nd->inode->i_mode)) 2244fb1cc555SAl Viro goto exit; 224567ee3ad2SAl Viro ok: 22466c0d46c4SAl Viro if (!S_ISREG(nd->inode->i_mode)) 22476c0d46c4SAl Viro will_truncate = 0; 22486c0d46c4SAl Viro 22490f9d1a10SAl Viro if (will_truncate) { 22500f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 22510f9d1a10SAl Viro if (error) 22520f9d1a10SAl Viro goto exit; 2253ca344a89SAl Viro want_write = 1; 22540f9d1a10SAl Viro } 2255ca344a89SAl Viro common: 2256bcda7652SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2257ca344a89SAl Viro if (error) 22580f9d1a10SAl Viro goto exit; 22590f9d1a10SAl Viro filp = nameidata_to_filp(nd); 22600f9d1a10SAl Viro if (!IS_ERR(filp)) { 22610f9d1a10SAl Viro error = ima_file_check(filp, op->acc_mode); 22620f9d1a10SAl Viro if (error) { 22630f9d1a10SAl Viro fput(filp); 22640f9d1a10SAl Viro filp = ERR_PTR(error); 22650f9d1a10SAl Viro } 22660f9d1a10SAl Viro } 22670f9d1a10SAl Viro if (!IS_ERR(filp)) { 22680f9d1a10SAl Viro if (will_truncate) { 22690f9d1a10SAl Viro error = handle_truncate(filp); 22700f9d1a10SAl Viro if (error) { 22710f9d1a10SAl Viro fput(filp); 22720f9d1a10SAl Viro filp = ERR_PTR(error); 22730f9d1a10SAl Viro } 22740f9d1a10SAl Viro } 22750f9d1a10SAl Viro } 2276ca344a89SAl Viro out: 2277ca344a89SAl Viro if (want_write) 22780f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 22790f9d1a10SAl Viro path_put(&nd->path); 2280fb1cc555SAl Viro return filp; 2281fb1cc555SAl Viro 2282fb1cc555SAl Viro exit_mutex_unlock: 2283fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2284fb1cc555SAl Viro exit_dput: 2285fb1cc555SAl Viro path_put_conditional(path, nd); 2286fb1cc555SAl Viro exit: 2287ca344a89SAl Viro filp = ERR_PTR(error); 2288ca344a89SAl Viro goto out; 2289fb1cc555SAl Viro } 2290fb1cc555SAl Viro 229113aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname, 229273d049a4SAl Viro struct nameidata *nd, const struct open_flags *op, int flags) 22931da177e4SLinus Torvalds { 2294fe2d35ffSAl Viro struct file *base = NULL; 22954a3fd211SDave Hansen struct file *filp; 22969850c056SAl Viro struct path path; 229713aab428SAl Viro int error; 229831e6b01fSNick Piggin 229931e6b01fSNick Piggin filp = get_empty_filp(); 230031e6b01fSNick Piggin if (!filp) 230131e6b01fSNick Piggin return ERR_PTR(-ENFILE); 230231e6b01fSNick Piggin 230347c805dcSAl Viro filp->f_flags = op->open_flag; 230473d049a4SAl Viro nd->intent.open.file = filp; 230573d049a4SAl Viro nd->intent.open.flags = open_to_namei_flags(op->open_flag); 230673d049a4SAl Viro nd->intent.open.create_mode = op->mode; 230731e6b01fSNick Piggin 230873d049a4SAl Viro error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base); 230931e6b01fSNick Piggin if (unlikely(error)) 231013aab428SAl Viro goto out_filp; 231131e6b01fSNick Piggin 2312fe2d35ffSAl Viro current->total_link_count = 0; 231373d049a4SAl Viro error = link_path_walk(pathname, nd); 231431e6b01fSNick Piggin if (unlikely(error)) 231531e6b01fSNick Piggin goto out_filp; 23161da177e4SLinus Torvalds 231773d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2318806b681cSAl Viro while (unlikely(!filp)) { /* trailing symlink */ 23197b9337aaSNick Piggin struct path link = path; 2320def4af30SAl Viro void *cookie; 2321574197e0SAl Viro if (!(nd->flags & LOOKUP_FOLLOW)) { 232273d049a4SAl Viro path_put_conditional(&path, nd); 232373d049a4SAl Viro path_put(&nd->path); 232440b39136SAl Viro filp = ERR_PTR(-ELOOP); 232540b39136SAl Viro break; 232640b39136SAl Viro } 232773d049a4SAl Viro nd->flags |= LOOKUP_PARENT; 232873d049a4SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 2329574197e0SAl Viro error = follow_link(&link, nd, &cookie); 2330c3e380b0SAl Viro if (unlikely(error)) 2331f1afe9efSAl Viro filp = ERR_PTR(error); 2332c3e380b0SAl Viro else 233373d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2334574197e0SAl Viro put_link(nd, &link, cookie); 2335806b681cSAl Viro } 233610fa8e62SAl Viro out: 233773d049a4SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) 233873d049a4SAl Viro path_put(&nd->root); 2339fe2d35ffSAl Viro if (base) 2340fe2d35ffSAl Viro fput(base); 234173d049a4SAl Viro release_open_intent(nd); 234210fa8e62SAl Viro return filp; 23431da177e4SLinus Torvalds 234431e6b01fSNick Piggin out_filp: 234510fa8e62SAl Viro filp = ERR_PTR(error); 234610fa8e62SAl Viro goto out; 2347de459215SKirill Korotaev } 23481da177e4SLinus Torvalds 234913aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname, 235013aab428SAl Viro const struct open_flags *op, int flags) 235113aab428SAl Viro { 235273d049a4SAl Viro struct nameidata nd; 235313aab428SAl Viro struct file *filp; 235413aab428SAl Viro 235573d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); 235613aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 235773d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags); 235813aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 235973d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); 236013aab428SAl Viro return filp; 236113aab428SAl Viro } 236213aab428SAl Viro 236373d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, 236473d049a4SAl Viro const char *name, const struct open_flags *op, int flags) 236573d049a4SAl Viro { 236673d049a4SAl Viro struct nameidata nd; 236773d049a4SAl Viro struct file *file; 236873d049a4SAl Viro 236973d049a4SAl Viro nd.root.mnt = mnt; 237073d049a4SAl Viro nd.root.dentry = dentry; 237173d049a4SAl Viro 237273d049a4SAl Viro flags |= LOOKUP_ROOT; 237373d049a4SAl Viro 2374bcda7652SAl Viro if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN) 237573d049a4SAl Viro return ERR_PTR(-ELOOP); 237673d049a4SAl Viro 237773d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU); 237873d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 237973d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags); 238073d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 238173d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL); 238273d049a4SAl Viro return file; 238373d049a4SAl Viro } 238473d049a4SAl Viro 23851da177e4SLinus Torvalds /** 23861da177e4SLinus Torvalds * lookup_create - lookup a dentry, creating it if it doesn't exist 23871da177e4SLinus Torvalds * @nd: nameidata info 23881da177e4SLinus Torvalds * @is_dir: directory flag 23891da177e4SLinus Torvalds * 23901da177e4SLinus Torvalds * Simple function to lookup and return a dentry and create it 23911da177e4SLinus Torvalds * if it doesn't exist. Is SMP-safe. 2392c663e5d8SChristoph Hellwig * 23934ac91378SJan Blunck * Returns with nd->path.dentry->d_inode->i_mutex locked. 23941da177e4SLinus Torvalds */ 23951da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir) 23961da177e4SLinus Torvalds { 2397c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 23981da177e4SLinus Torvalds 23994ac91378SJan Blunck mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 2400c663e5d8SChristoph Hellwig /* 2401c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 2402c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 2403c663e5d8SChristoph Hellwig */ 24041da177e4SLinus Torvalds if (nd->last_type != LAST_NORM) 24051da177e4SLinus Torvalds goto fail; 24061da177e4SLinus Torvalds nd->flags &= ~LOOKUP_PARENT; 24073516586aSAl Viro nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL; 2408a634904aSASANO Masahiro nd->intent.open.flags = O_EXCL; 2409c663e5d8SChristoph Hellwig 2410c663e5d8SChristoph Hellwig /* 2411c663e5d8SChristoph Hellwig * Do the final lookup. 2412c663e5d8SChristoph Hellwig */ 241349705b77SChristoph Hellwig dentry = lookup_hash(nd); 24141da177e4SLinus Torvalds if (IS_ERR(dentry)) 24151da177e4SLinus Torvalds goto fail; 2416c663e5d8SChristoph Hellwig 2417e9baf6e5SAl Viro if (dentry->d_inode) 2418e9baf6e5SAl Viro goto eexist; 2419c663e5d8SChristoph Hellwig /* 2420c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 2421c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 2422c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 2423c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 2424c663e5d8SChristoph Hellwig */ 2425e9baf6e5SAl Viro if (unlikely(!is_dir && nd->last.name[nd->last.len])) { 24261da177e4SLinus Torvalds dput(dentry); 24271da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 2428e9baf6e5SAl Viro } 2429e9baf6e5SAl Viro return dentry; 2430e9baf6e5SAl Viro eexist: 2431e9baf6e5SAl Viro dput(dentry); 2432e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 24331da177e4SLinus Torvalds fail: 24341da177e4SLinus Torvalds return dentry; 24351da177e4SLinus Torvalds } 2436f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create); 24371da177e4SLinus Torvalds 24381da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 24391da177e4SLinus Torvalds { 2440a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 24411da177e4SLinus Torvalds 24421da177e4SLinus Torvalds if (error) 24431da177e4SLinus Torvalds return error; 24441da177e4SLinus Torvalds 24451da177e4SLinus Torvalds if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) 24461da177e4SLinus Torvalds return -EPERM; 24471da177e4SLinus Torvalds 2448acfa4380SAl Viro if (!dir->i_op->mknod) 24491da177e4SLinus Torvalds return -EPERM; 24501da177e4SLinus Torvalds 245108ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 245208ce5f16SSerge E. Hallyn if (error) 245308ce5f16SSerge E. Hallyn return error; 245408ce5f16SSerge E. Hallyn 24551da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 24561da177e4SLinus Torvalds if (error) 24571da177e4SLinus Torvalds return error; 24581da177e4SLinus Torvalds 24591da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 2460a74574aaSStephen Smalley if (!error) 2461f38aa942SAmy Griffis fsnotify_create(dir, dentry); 24621da177e4SLinus Torvalds return error; 24631da177e4SLinus Torvalds } 24641da177e4SLinus Torvalds 2465463c3197SDave Hansen static int may_mknod(mode_t mode) 2466463c3197SDave Hansen { 2467463c3197SDave Hansen switch (mode & S_IFMT) { 2468463c3197SDave Hansen case S_IFREG: 2469463c3197SDave Hansen case S_IFCHR: 2470463c3197SDave Hansen case S_IFBLK: 2471463c3197SDave Hansen case S_IFIFO: 2472463c3197SDave Hansen case S_IFSOCK: 2473463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2474463c3197SDave Hansen return 0; 2475463c3197SDave Hansen case S_IFDIR: 2476463c3197SDave Hansen return -EPERM; 2477463c3197SDave Hansen default: 2478463c3197SDave Hansen return -EINVAL; 2479463c3197SDave Hansen } 2480463c3197SDave Hansen } 2481463c3197SDave Hansen 24822e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode, 24832e4d0924SHeiko Carstens unsigned, dev) 24841da177e4SLinus Torvalds { 24852ad94ae6SAl Viro int error; 24861da177e4SLinus Torvalds char *tmp; 24871da177e4SLinus Torvalds struct dentry *dentry; 24881da177e4SLinus Torvalds struct nameidata nd; 24891da177e4SLinus Torvalds 24901da177e4SLinus Torvalds if (S_ISDIR(mode)) 24911da177e4SLinus Torvalds return -EPERM; 24921da177e4SLinus Torvalds 24932ad94ae6SAl Viro error = user_path_parent(dfd, filename, &nd, &tmp); 24941da177e4SLinus Torvalds if (error) 24952ad94ae6SAl Viro return error; 24962ad94ae6SAl Viro 24971da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 2498463c3197SDave Hansen if (IS_ERR(dentry)) { 24991da177e4SLinus Torvalds error = PTR_ERR(dentry); 2500463c3197SDave Hansen goto out_unlock; 2501463c3197SDave Hansen } 25024ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2503ce3b0f8dSAl Viro mode &= ~current_umask(); 2504463c3197SDave Hansen error = may_mknod(mode); 2505463c3197SDave Hansen if (error) 2506463c3197SDave Hansen goto out_dput; 2507463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2508463c3197SDave Hansen if (error) 2509463c3197SDave Hansen goto out_dput; 2510be6d3e56SKentaro Takeda error = security_path_mknod(&nd.path, dentry, mode, dev); 2511be6d3e56SKentaro Takeda if (error) 2512be6d3e56SKentaro Takeda goto out_drop_write; 25131da177e4SLinus Torvalds switch (mode & S_IFMT) { 25141da177e4SLinus Torvalds case 0: case S_IFREG: 25154ac91378SJan Blunck error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd); 25161da177e4SLinus Torvalds break; 25171da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 25184ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode, 25191da177e4SLinus Torvalds new_decode_dev(dev)); 25201da177e4SLinus Torvalds break; 25211da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 25224ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0); 25231da177e4SLinus Torvalds break; 25241da177e4SLinus Torvalds } 2525be6d3e56SKentaro Takeda out_drop_write: 2526463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2527463c3197SDave Hansen out_dput: 25281da177e4SLinus Torvalds dput(dentry); 2529463c3197SDave Hansen out_unlock: 25304ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 25311d957f9bSJan Blunck path_put(&nd.path); 25321da177e4SLinus Torvalds putname(tmp); 25331da177e4SLinus Torvalds 25341da177e4SLinus Torvalds return error; 25351da177e4SLinus Torvalds } 25361da177e4SLinus Torvalds 25373480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev) 25385590ff0dSUlrich Drepper { 25395590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 25405590ff0dSUlrich Drepper } 25415590ff0dSUlrich Drepper 25421da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 25431da177e4SLinus Torvalds { 2544a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 25451da177e4SLinus Torvalds 25461da177e4SLinus Torvalds if (error) 25471da177e4SLinus Torvalds return error; 25481da177e4SLinus Torvalds 2549acfa4380SAl Viro if (!dir->i_op->mkdir) 25501da177e4SLinus Torvalds return -EPERM; 25511da177e4SLinus Torvalds 25521da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 25531da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 25541da177e4SLinus Torvalds if (error) 25551da177e4SLinus Torvalds return error; 25561da177e4SLinus Torvalds 25571da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2558a74574aaSStephen Smalley if (!error) 2559f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 25601da177e4SLinus Torvalds return error; 25611da177e4SLinus Torvalds } 25621da177e4SLinus Torvalds 25632e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode) 25641da177e4SLinus Torvalds { 25651da177e4SLinus Torvalds int error = 0; 25661da177e4SLinus Torvalds char * tmp; 25676902d925SDave Hansen struct dentry *dentry; 25686902d925SDave Hansen struct nameidata nd; 25691da177e4SLinus Torvalds 25702ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &tmp); 25712ad94ae6SAl Viro if (error) 25726902d925SDave Hansen goto out_err; 25731da177e4SLinus Torvalds 25741da177e4SLinus Torvalds dentry = lookup_create(&nd, 1); 25751da177e4SLinus Torvalds error = PTR_ERR(dentry); 25766902d925SDave Hansen if (IS_ERR(dentry)) 25776902d925SDave Hansen goto out_unlock; 25786902d925SDave Hansen 25794ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2580ce3b0f8dSAl Viro mode &= ~current_umask(); 2581463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2582463c3197SDave Hansen if (error) 2583463c3197SDave Hansen goto out_dput; 2584be6d3e56SKentaro Takeda error = security_path_mkdir(&nd.path, dentry, mode); 2585be6d3e56SKentaro Takeda if (error) 2586be6d3e56SKentaro Takeda goto out_drop_write; 25874ac91378SJan Blunck error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode); 2588be6d3e56SKentaro Takeda out_drop_write: 2589463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2590463c3197SDave Hansen out_dput: 25911da177e4SLinus Torvalds dput(dentry); 25926902d925SDave Hansen out_unlock: 25934ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 25941d957f9bSJan Blunck path_put(&nd.path); 25951da177e4SLinus Torvalds putname(tmp); 25966902d925SDave Hansen out_err: 25971da177e4SLinus Torvalds return error; 25981da177e4SLinus Torvalds } 25991da177e4SLinus Torvalds 26003cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode) 26015590ff0dSUlrich Drepper { 26025590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 26035590ff0dSUlrich Drepper } 26045590ff0dSUlrich Drepper 26051da177e4SLinus Torvalds /* 26061da177e4SLinus Torvalds * We try to drop the dentry early: we should have 26071da177e4SLinus Torvalds * a usage count of 2 if we're the only user of this 26081da177e4SLinus Torvalds * dentry, and if that is true (possibly after pruning 26091da177e4SLinus Torvalds * the dcache), then we drop the dentry now. 26101da177e4SLinus Torvalds * 26111da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 26121da177e4SLinus Torvalds * do a 26131da177e4SLinus Torvalds * 26141da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 26151da177e4SLinus Torvalds * return -EBUSY; 26161da177e4SLinus Torvalds * 26171da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 26181da177e4SLinus Torvalds * that is still in use by something else.. 26191da177e4SLinus Torvalds */ 26201da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 26211da177e4SLinus Torvalds { 26221da177e4SLinus Torvalds dget(dentry); 26231da177e4SLinus Torvalds shrink_dcache_parent(dentry); 26241da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 2625b7ab39f6SNick Piggin if (dentry->d_count == 2) 26261da177e4SLinus Torvalds __d_drop(dentry); 26271da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 26281da177e4SLinus Torvalds } 26291da177e4SLinus Torvalds 26301da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 26311da177e4SLinus Torvalds { 26321da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 26331da177e4SLinus Torvalds 26341da177e4SLinus Torvalds if (error) 26351da177e4SLinus Torvalds return error; 26361da177e4SLinus Torvalds 2637acfa4380SAl Viro if (!dir->i_op->rmdir) 26381da177e4SLinus Torvalds return -EPERM; 26391da177e4SLinus Torvalds 26401b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 26411da177e4SLinus Torvalds dentry_unhash(dentry); 26421da177e4SLinus Torvalds if (d_mountpoint(dentry)) 26431da177e4SLinus Torvalds error = -EBUSY; 26441da177e4SLinus Torvalds else { 26451da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 26461da177e4SLinus Torvalds if (!error) { 26471da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 2648d83c49f3SAl Viro if (!error) { 26491da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 2650d83c49f3SAl Viro dont_mount(dentry); 2651d83c49f3SAl Viro } 26521da177e4SLinus Torvalds } 26531da177e4SLinus Torvalds } 26541b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 26551da177e4SLinus Torvalds if (!error) { 26561da177e4SLinus Torvalds d_delete(dentry); 26571da177e4SLinus Torvalds } 26581da177e4SLinus Torvalds dput(dentry); 26591da177e4SLinus Torvalds 26601da177e4SLinus Torvalds return error; 26611da177e4SLinus Torvalds } 26621da177e4SLinus Torvalds 26635590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 26641da177e4SLinus Torvalds { 26651da177e4SLinus Torvalds int error = 0; 26661da177e4SLinus Torvalds char * name; 26671da177e4SLinus Torvalds struct dentry *dentry; 26681da177e4SLinus Torvalds struct nameidata nd; 26691da177e4SLinus Torvalds 26702ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 26711da177e4SLinus Torvalds if (error) 26722ad94ae6SAl Viro return error; 26731da177e4SLinus Torvalds 26741da177e4SLinus Torvalds switch(nd.last_type) { 26751da177e4SLinus Torvalds case LAST_DOTDOT: 26761da177e4SLinus Torvalds error = -ENOTEMPTY; 26771da177e4SLinus Torvalds goto exit1; 26781da177e4SLinus Torvalds case LAST_DOT: 26791da177e4SLinus Torvalds error = -EINVAL; 26801da177e4SLinus Torvalds goto exit1; 26811da177e4SLinus Torvalds case LAST_ROOT: 26821da177e4SLinus Torvalds error = -EBUSY; 26831da177e4SLinus Torvalds goto exit1; 26841da177e4SLinus Torvalds } 26850612d9fbSOGAWA Hirofumi 26860612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 26870612d9fbSOGAWA Hirofumi 26884ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 268949705b77SChristoph Hellwig dentry = lookup_hash(&nd); 26901da177e4SLinus Torvalds error = PTR_ERR(dentry); 26916902d925SDave Hansen if (IS_ERR(dentry)) 26926902d925SDave Hansen goto exit2; 26930622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 26940622753bSDave Hansen if (error) 26950622753bSDave Hansen goto exit3; 2696be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 2697be6d3e56SKentaro Takeda if (error) 2698be6d3e56SKentaro Takeda goto exit4; 26994ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 2700be6d3e56SKentaro Takeda exit4: 27010622753bSDave Hansen mnt_drop_write(nd.path.mnt); 27020622753bSDave Hansen exit3: 27031da177e4SLinus Torvalds dput(dentry); 27046902d925SDave Hansen exit2: 27054ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 27061da177e4SLinus Torvalds exit1: 27071d957f9bSJan Blunck path_put(&nd.path); 27081da177e4SLinus Torvalds putname(name); 27091da177e4SLinus Torvalds return error; 27101da177e4SLinus Torvalds } 27111da177e4SLinus Torvalds 27123cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 27135590ff0dSUlrich Drepper { 27145590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 27155590ff0dSUlrich Drepper } 27165590ff0dSUlrich Drepper 27171da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 27181da177e4SLinus Torvalds { 27191da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 27201da177e4SLinus Torvalds 27211da177e4SLinus Torvalds if (error) 27221da177e4SLinus Torvalds return error; 27231da177e4SLinus Torvalds 2724acfa4380SAl Viro if (!dir->i_op->unlink) 27251da177e4SLinus Torvalds return -EPERM; 27261da177e4SLinus Torvalds 27271b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 27281da177e4SLinus Torvalds if (d_mountpoint(dentry)) 27291da177e4SLinus Torvalds error = -EBUSY; 27301da177e4SLinus Torvalds else { 27311da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 2732bec1052eSAl Viro if (!error) { 27331da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 2734bec1052eSAl Viro if (!error) 2735d83c49f3SAl Viro dont_mount(dentry); 2736bec1052eSAl Viro } 27371da177e4SLinus Torvalds } 27381b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 27391da177e4SLinus Torvalds 27401da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 27411da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 2742ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 27431da177e4SLinus Torvalds d_delete(dentry); 27441da177e4SLinus Torvalds } 27450eeca283SRobert Love 27461da177e4SLinus Torvalds return error; 27471da177e4SLinus Torvalds } 27481da177e4SLinus Torvalds 27491da177e4SLinus Torvalds /* 27501da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 27511b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 27521da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 27531da177e4SLinus Torvalds * while waiting on the I/O. 27541da177e4SLinus Torvalds */ 27555590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 27561da177e4SLinus Torvalds { 27572ad94ae6SAl Viro int error; 27581da177e4SLinus Torvalds char *name; 27591da177e4SLinus Torvalds struct dentry *dentry; 27601da177e4SLinus Torvalds struct nameidata nd; 27611da177e4SLinus Torvalds struct inode *inode = NULL; 27621da177e4SLinus Torvalds 27632ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 27641da177e4SLinus Torvalds if (error) 27652ad94ae6SAl Viro return error; 27662ad94ae6SAl Viro 27671da177e4SLinus Torvalds error = -EISDIR; 27681da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 27691da177e4SLinus Torvalds goto exit1; 27700612d9fbSOGAWA Hirofumi 27710612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 27720612d9fbSOGAWA Hirofumi 27734ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 277449705b77SChristoph Hellwig dentry = lookup_hash(&nd); 27751da177e4SLinus Torvalds error = PTR_ERR(dentry); 27761da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 27771da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 27781da177e4SLinus Torvalds if (nd.last.name[nd.last.len]) 27791da177e4SLinus Torvalds goto slashes; 27801da177e4SLinus Torvalds inode = dentry->d_inode; 27811da177e4SLinus Torvalds if (inode) 27827de9c6eeSAl Viro ihold(inode); 27830622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 27840622753bSDave Hansen if (error) 27850622753bSDave Hansen goto exit2; 2786be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 2787be6d3e56SKentaro Takeda if (error) 2788be6d3e56SKentaro Takeda goto exit3; 27894ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 2790be6d3e56SKentaro Takeda exit3: 27910622753bSDave Hansen mnt_drop_write(nd.path.mnt); 27921da177e4SLinus Torvalds exit2: 27931da177e4SLinus Torvalds dput(dentry); 27941da177e4SLinus Torvalds } 27954ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 27961da177e4SLinus Torvalds if (inode) 27971da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 27981da177e4SLinus Torvalds exit1: 27991d957f9bSJan Blunck path_put(&nd.path); 28001da177e4SLinus Torvalds putname(name); 28011da177e4SLinus Torvalds return error; 28021da177e4SLinus Torvalds 28031da177e4SLinus Torvalds slashes: 28041da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 28051da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 28061da177e4SLinus Torvalds goto exit2; 28071da177e4SLinus Torvalds } 28081da177e4SLinus Torvalds 28092e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 28105590ff0dSUlrich Drepper { 28115590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 28125590ff0dSUlrich Drepper return -EINVAL; 28135590ff0dSUlrich Drepper 28145590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 28155590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 28165590ff0dSUlrich Drepper 28175590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 28185590ff0dSUlrich Drepper } 28195590ff0dSUlrich Drepper 28203480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 28215590ff0dSUlrich Drepper { 28225590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 28235590ff0dSUlrich Drepper } 28245590ff0dSUlrich Drepper 2825db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 28261da177e4SLinus Torvalds { 2827a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 28281da177e4SLinus Torvalds 28291da177e4SLinus Torvalds if (error) 28301da177e4SLinus Torvalds return error; 28311da177e4SLinus Torvalds 2832acfa4380SAl Viro if (!dir->i_op->symlink) 28331da177e4SLinus Torvalds return -EPERM; 28341da177e4SLinus Torvalds 28351da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 28361da177e4SLinus Torvalds if (error) 28371da177e4SLinus Torvalds return error; 28381da177e4SLinus Torvalds 28391da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 2840a74574aaSStephen Smalley if (!error) 2841f38aa942SAmy Griffis fsnotify_create(dir, dentry); 28421da177e4SLinus Torvalds return error; 28431da177e4SLinus Torvalds } 28441da177e4SLinus Torvalds 28452e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 28462e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 28471da177e4SLinus Torvalds { 28482ad94ae6SAl Viro int error; 28491da177e4SLinus Torvalds char *from; 28501da177e4SLinus Torvalds char *to; 28516902d925SDave Hansen struct dentry *dentry; 28526902d925SDave Hansen struct nameidata nd; 28531da177e4SLinus Torvalds 28541da177e4SLinus Torvalds from = getname(oldname); 28551da177e4SLinus Torvalds if (IS_ERR(from)) 28561da177e4SLinus Torvalds return PTR_ERR(from); 28572ad94ae6SAl Viro 28582ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 28592ad94ae6SAl Viro if (error) 28606902d925SDave Hansen goto out_putname; 28611da177e4SLinus Torvalds 28621da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 28631da177e4SLinus Torvalds error = PTR_ERR(dentry); 28646902d925SDave Hansen if (IS_ERR(dentry)) 28656902d925SDave Hansen goto out_unlock; 28666902d925SDave Hansen 286775c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 286875c3f29dSDave Hansen if (error) 286975c3f29dSDave Hansen goto out_dput; 2870be6d3e56SKentaro Takeda error = security_path_symlink(&nd.path, dentry, from); 2871be6d3e56SKentaro Takeda if (error) 2872be6d3e56SKentaro Takeda goto out_drop_write; 2873db2e747bSMiklos Szeredi error = vfs_symlink(nd.path.dentry->d_inode, dentry, from); 2874be6d3e56SKentaro Takeda out_drop_write: 287575c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 287675c3f29dSDave Hansen out_dput: 28771da177e4SLinus Torvalds dput(dentry); 28786902d925SDave Hansen out_unlock: 28794ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 28801d957f9bSJan Blunck path_put(&nd.path); 28811da177e4SLinus Torvalds putname(to); 28826902d925SDave Hansen out_putname: 28831da177e4SLinus Torvalds putname(from); 28841da177e4SLinus Torvalds return error; 28851da177e4SLinus Torvalds } 28861da177e4SLinus Torvalds 28873480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 28885590ff0dSUlrich Drepper { 28895590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 28905590ff0dSUlrich Drepper } 28915590ff0dSUlrich Drepper 28921da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 28931da177e4SLinus Torvalds { 28941da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 28951da177e4SLinus Torvalds int error; 28961da177e4SLinus Torvalds 28971da177e4SLinus Torvalds if (!inode) 28981da177e4SLinus Torvalds return -ENOENT; 28991da177e4SLinus Torvalds 2900a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 29011da177e4SLinus Torvalds if (error) 29021da177e4SLinus Torvalds return error; 29031da177e4SLinus Torvalds 29041da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 29051da177e4SLinus Torvalds return -EXDEV; 29061da177e4SLinus Torvalds 29071da177e4SLinus Torvalds /* 29081da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 29091da177e4SLinus Torvalds */ 29101da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 29111da177e4SLinus Torvalds return -EPERM; 2912acfa4380SAl Viro if (!dir->i_op->link) 29131da177e4SLinus Torvalds return -EPERM; 29147e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 29151da177e4SLinus Torvalds return -EPERM; 29161da177e4SLinus Torvalds 29171da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 29181da177e4SLinus Torvalds if (error) 29191da177e4SLinus Torvalds return error; 29201da177e4SLinus Torvalds 29217e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 2922aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 2923aae8a97dSAneesh Kumar K.V if (inode->i_nlink == 0) 2924aae8a97dSAneesh Kumar K.V error = -ENOENT; 2925aae8a97dSAneesh Kumar K.V else 29261da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 29277e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 2928e31e14ecSStephen Smalley if (!error) 29297e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 29301da177e4SLinus Torvalds return error; 29311da177e4SLinus Torvalds } 29321da177e4SLinus Torvalds 29331da177e4SLinus Torvalds /* 29341da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 29351da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 29361da177e4SLinus Torvalds * newname. --KAB 29371da177e4SLinus Torvalds * 29381da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 29391da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 29401da177e4SLinus Torvalds * and other special files. --ADM 29411da177e4SLinus Torvalds */ 29422e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 29432e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 29441da177e4SLinus Torvalds { 29451da177e4SLinus Torvalds struct dentry *new_dentry; 29462d8f3038SAl Viro struct nameidata nd; 29472d8f3038SAl Viro struct path old_path; 294811a7b371SAneesh Kumar K.V int how = 0; 29491da177e4SLinus Torvalds int error; 29501da177e4SLinus Torvalds char *to; 29511da177e4SLinus Torvalds 295211a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 2953c04030e1SUlrich Drepper return -EINVAL; 295411a7b371SAneesh Kumar K.V /* 295511a7b371SAneesh Kumar K.V * To use null names we require CAP_DAC_READ_SEARCH 295611a7b371SAneesh Kumar K.V * This ensures that not everyone will be able to create 295711a7b371SAneesh Kumar K.V * handlink using the passed filedescriptor. 295811a7b371SAneesh Kumar K.V */ 295911a7b371SAneesh Kumar K.V if (flags & AT_EMPTY_PATH) { 296011a7b371SAneesh Kumar K.V if (!capable(CAP_DAC_READ_SEARCH)) 296111a7b371SAneesh Kumar K.V return -ENOENT; 296211a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 296311a7b371SAneesh Kumar K.V } 2964c04030e1SUlrich Drepper 296511a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 296611a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 296711a7b371SAneesh Kumar K.V 296811a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 29691da177e4SLinus Torvalds if (error) 29702ad94ae6SAl Viro return error; 29712ad94ae6SAl Viro 29722ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 29731da177e4SLinus Torvalds if (error) 29741da177e4SLinus Torvalds goto out; 29751da177e4SLinus Torvalds error = -EXDEV; 29762d8f3038SAl Viro if (old_path.mnt != nd.path.mnt) 29771da177e4SLinus Torvalds goto out_release; 29781da177e4SLinus Torvalds new_dentry = lookup_create(&nd, 0); 29791da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 29806902d925SDave Hansen if (IS_ERR(new_dentry)) 29816902d925SDave Hansen goto out_unlock; 298275c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 298375c3f29dSDave Hansen if (error) 298475c3f29dSDave Hansen goto out_dput; 2985be6d3e56SKentaro Takeda error = security_path_link(old_path.dentry, &nd.path, new_dentry); 2986be6d3e56SKentaro Takeda if (error) 2987be6d3e56SKentaro Takeda goto out_drop_write; 29882d8f3038SAl Viro error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry); 2989be6d3e56SKentaro Takeda out_drop_write: 299075c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 299175c3f29dSDave Hansen out_dput: 29921da177e4SLinus Torvalds dput(new_dentry); 29936902d925SDave Hansen out_unlock: 29944ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 29951da177e4SLinus Torvalds out_release: 29961d957f9bSJan Blunck path_put(&nd.path); 29972ad94ae6SAl Viro putname(to); 29981da177e4SLinus Torvalds out: 29992d8f3038SAl Viro path_put(&old_path); 30001da177e4SLinus Torvalds 30011da177e4SLinus Torvalds return error; 30021da177e4SLinus Torvalds } 30031da177e4SLinus Torvalds 30043480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 30055590ff0dSUlrich Drepper { 3006c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 30075590ff0dSUlrich Drepper } 30085590ff0dSUlrich Drepper 30091da177e4SLinus Torvalds /* 30101da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 30111da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 30121da177e4SLinus Torvalds * Problems: 30131da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 30141da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 30151da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 3016a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 30171da177e4SLinus Torvalds * story. 30181da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 30191b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 30201da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 30211da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 3022a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 30231da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 30241da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 30251da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 3026a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 30271da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 30281da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 30291da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 30301da177e4SLinus Torvalds * d) some filesystems don't support opened-but-unlinked directories, 30311da177e4SLinus Torvalds * either because of layout or because they are not ready to deal with 30321da177e4SLinus Torvalds * all cases correctly. The latter will be fixed (taking this sort of 30331da177e4SLinus Torvalds * stuff into VFS), but the former is not going away. Solution: the same 30341da177e4SLinus Torvalds * trick as in rmdir(). 30351da177e4SLinus Torvalds * e) conversion from fhandle to dentry may come in the wrong moment - when 30361b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 30371da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 3038c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 30391da177e4SLinus Torvalds * locking]. 30401da177e4SLinus Torvalds */ 304175c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 30421da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30431da177e4SLinus Torvalds { 30441da177e4SLinus Torvalds int error = 0; 30451da177e4SLinus Torvalds struct inode *target; 30461da177e4SLinus Torvalds 30471da177e4SLinus Torvalds /* 30481da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 30491da177e4SLinus Torvalds * we'll need to flip '..'. 30501da177e4SLinus Torvalds */ 30511da177e4SLinus Torvalds if (new_dir != old_dir) { 3052f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 30531da177e4SLinus Torvalds if (error) 30541da177e4SLinus Torvalds return error; 30551da177e4SLinus Torvalds } 30561da177e4SLinus Torvalds 30571da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 30581da177e4SLinus Torvalds if (error) 30591da177e4SLinus Torvalds return error; 30601da177e4SLinus Torvalds 30611da177e4SLinus Torvalds target = new_dentry->d_inode; 3062d83c49f3SAl Viro if (target) 30631b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 30641da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 30651da177e4SLinus Torvalds error = -EBUSY; 3066d83c49f3SAl Viro else { 3067d83c49f3SAl Viro if (target) 3068d83c49f3SAl Viro dentry_unhash(new_dentry); 30691da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 3070d83c49f3SAl Viro } 30711da177e4SLinus Torvalds if (target) { 3072d83c49f3SAl Viro if (!error) { 30731da177e4SLinus Torvalds target->i_flags |= S_DEAD; 3074d83c49f3SAl Viro dont_mount(new_dentry); 3075d83c49f3SAl Viro } 30761b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 30771da177e4SLinus Torvalds if (d_unhashed(new_dentry)) 30781da177e4SLinus Torvalds d_rehash(new_dentry); 30791da177e4SLinus Torvalds dput(new_dentry); 30801da177e4SLinus Torvalds } 3081e31e14ecSStephen Smalley if (!error) 3082349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 30831da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 30841da177e4SLinus Torvalds return error; 30851da177e4SLinus Torvalds } 30861da177e4SLinus Torvalds 308775c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 30881da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30891da177e4SLinus Torvalds { 30901da177e4SLinus Torvalds struct inode *target; 30911da177e4SLinus Torvalds int error; 30921da177e4SLinus Torvalds 30931da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 30941da177e4SLinus Torvalds if (error) 30951da177e4SLinus Torvalds return error; 30961da177e4SLinus Torvalds 30971da177e4SLinus Torvalds dget(new_dentry); 30981da177e4SLinus Torvalds target = new_dentry->d_inode; 30991da177e4SLinus Torvalds if (target) 31001b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 31011da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 31021da177e4SLinus Torvalds error = -EBUSY; 31031da177e4SLinus Torvalds else 31041da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 31051da177e4SLinus Torvalds if (!error) { 3106bec1052eSAl Viro if (target) 3107d83c49f3SAl Viro dont_mount(new_dentry); 3108349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 31091da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 31101da177e4SLinus Torvalds } 31111da177e4SLinus Torvalds if (target) 31121b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 31131da177e4SLinus Torvalds dput(new_dentry); 31141da177e4SLinus Torvalds return error; 31151da177e4SLinus Torvalds } 31161da177e4SLinus Torvalds 31171da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 31181da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 31191da177e4SLinus Torvalds { 31201da177e4SLinus Torvalds int error; 31211da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 312259b0df21SEric Paris const unsigned char *old_name; 31231da177e4SLinus Torvalds 31241da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 31251da177e4SLinus Torvalds return 0; 31261da177e4SLinus Torvalds 31271da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 31281da177e4SLinus Torvalds if (error) 31291da177e4SLinus Torvalds return error; 31301da177e4SLinus Torvalds 31311da177e4SLinus Torvalds if (!new_dentry->d_inode) 3132a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 31331da177e4SLinus Torvalds else 31341da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 31351da177e4SLinus Torvalds if (error) 31361da177e4SLinus Torvalds return error; 31371da177e4SLinus Torvalds 3138acfa4380SAl Viro if (!old_dir->i_op->rename) 31391da177e4SLinus Torvalds return -EPERM; 31401da177e4SLinus Torvalds 31410eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 31420eeca283SRobert Love 31431da177e4SLinus Torvalds if (is_dir) 31441da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 31451da177e4SLinus Torvalds else 31461da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3147123df294SAl Viro if (!error) 3148123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 31495a190ae6SAl Viro new_dentry->d_inode, old_dentry); 31500eeca283SRobert Love fsnotify_oldname_free(old_name); 31510eeca283SRobert Love 31521da177e4SLinus Torvalds return error; 31531da177e4SLinus Torvalds } 31541da177e4SLinus Torvalds 31552e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 31562e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 31571da177e4SLinus Torvalds { 31581da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 31591da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 31601da177e4SLinus Torvalds struct dentry *trap; 31611da177e4SLinus Torvalds struct nameidata oldnd, newnd; 31622ad94ae6SAl Viro char *from; 31632ad94ae6SAl Viro char *to; 31642ad94ae6SAl Viro int error; 31651da177e4SLinus Torvalds 31662ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 31671da177e4SLinus Torvalds if (error) 31681da177e4SLinus Torvalds goto exit; 31691da177e4SLinus Torvalds 31702ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 31711da177e4SLinus Torvalds if (error) 31721da177e4SLinus Torvalds goto exit1; 31731da177e4SLinus Torvalds 31741da177e4SLinus Torvalds error = -EXDEV; 31754ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 31761da177e4SLinus Torvalds goto exit2; 31771da177e4SLinus Torvalds 31784ac91378SJan Blunck old_dir = oldnd.path.dentry; 31791da177e4SLinus Torvalds error = -EBUSY; 31801da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 31811da177e4SLinus Torvalds goto exit2; 31821da177e4SLinus Torvalds 31834ac91378SJan Blunck new_dir = newnd.path.dentry; 31841da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 31851da177e4SLinus Torvalds goto exit2; 31861da177e4SLinus Torvalds 31870612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 31880612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 31894e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 31900612d9fbSOGAWA Hirofumi 31911da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 31921da177e4SLinus Torvalds 319349705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 31941da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 31951da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 31961da177e4SLinus Torvalds goto exit3; 31971da177e4SLinus Torvalds /* source must exist */ 31981da177e4SLinus Torvalds error = -ENOENT; 31991da177e4SLinus Torvalds if (!old_dentry->d_inode) 32001da177e4SLinus Torvalds goto exit4; 32011da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 32021da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 32031da177e4SLinus Torvalds error = -ENOTDIR; 32041da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 32051da177e4SLinus Torvalds goto exit4; 32061da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 32071da177e4SLinus Torvalds goto exit4; 32081da177e4SLinus Torvalds } 32091da177e4SLinus Torvalds /* source should not be ancestor of target */ 32101da177e4SLinus Torvalds error = -EINVAL; 32111da177e4SLinus Torvalds if (old_dentry == trap) 32121da177e4SLinus Torvalds goto exit4; 321349705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 32141da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 32151da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 32161da177e4SLinus Torvalds goto exit4; 32171da177e4SLinus Torvalds /* target should not be an ancestor of source */ 32181da177e4SLinus Torvalds error = -ENOTEMPTY; 32191da177e4SLinus Torvalds if (new_dentry == trap) 32201da177e4SLinus Torvalds goto exit5; 32211da177e4SLinus Torvalds 32229079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 32239079b1ebSDave Hansen if (error) 32249079b1ebSDave Hansen goto exit5; 3225be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3226be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3227be6d3e56SKentaro Takeda if (error) 3228be6d3e56SKentaro Takeda goto exit6; 32291da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 32301da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 3231be6d3e56SKentaro Takeda exit6: 32329079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 32331da177e4SLinus Torvalds exit5: 32341da177e4SLinus Torvalds dput(new_dentry); 32351da177e4SLinus Torvalds exit4: 32361da177e4SLinus Torvalds dput(old_dentry); 32371da177e4SLinus Torvalds exit3: 32381da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 32391da177e4SLinus Torvalds exit2: 32401d957f9bSJan Blunck path_put(&newnd.path); 32412ad94ae6SAl Viro putname(to); 32421da177e4SLinus Torvalds exit1: 32431d957f9bSJan Blunck path_put(&oldnd.path); 32441da177e4SLinus Torvalds putname(from); 32452ad94ae6SAl Viro exit: 32461da177e4SLinus Torvalds return error; 32471da177e4SLinus Torvalds } 32481da177e4SLinus Torvalds 3249a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 32505590ff0dSUlrich Drepper { 32515590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 32525590ff0dSUlrich Drepper } 32535590ff0dSUlrich Drepper 32541da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 32551da177e4SLinus Torvalds { 32561da177e4SLinus Torvalds int len; 32571da177e4SLinus Torvalds 32581da177e4SLinus Torvalds len = PTR_ERR(link); 32591da177e4SLinus Torvalds if (IS_ERR(link)) 32601da177e4SLinus Torvalds goto out; 32611da177e4SLinus Torvalds 32621da177e4SLinus Torvalds len = strlen(link); 32631da177e4SLinus Torvalds if (len > (unsigned) buflen) 32641da177e4SLinus Torvalds len = buflen; 32651da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 32661da177e4SLinus Torvalds len = -EFAULT; 32671da177e4SLinus Torvalds out: 32681da177e4SLinus Torvalds return len; 32691da177e4SLinus Torvalds } 32701da177e4SLinus Torvalds 32711da177e4SLinus Torvalds /* 32721da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 32731da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 32741da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 32751da177e4SLinus Torvalds */ 32761da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 32771da177e4SLinus Torvalds { 32781da177e4SLinus Torvalds struct nameidata nd; 3279cc314eefSLinus Torvalds void *cookie; 3280694a1764SMarcin Slusarz int res; 3281cc314eefSLinus Torvalds 32821da177e4SLinus Torvalds nd.depth = 0; 3283cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3284694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3285694a1764SMarcin Slusarz return PTR_ERR(cookie); 3286694a1764SMarcin Slusarz 3287694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 32881da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3289cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3290694a1764SMarcin Slusarz return res; 32911da177e4SLinus Torvalds } 32921da177e4SLinus Torvalds 32931da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 32941da177e4SLinus Torvalds { 32951da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 32961da177e4SLinus Torvalds } 32971da177e4SLinus Torvalds 32981da177e4SLinus Torvalds /* get the link contents into pagecache */ 32991da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 33001da177e4SLinus Torvalds { 3301ebd09abbSDuane Griffin char *kaddr; 33021da177e4SLinus Torvalds struct page *page; 33031da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3304090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 33051da177e4SLinus Torvalds if (IS_ERR(page)) 33066fe6900eSNick Piggin return (char*)page; 33071da177e4SLinus Torvalds *ppage = page; 3308ebd09abbSDuane Griffin kaddr = kmap(page); 3309ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3310ebd09abbSDuane Griffin return kaddr; 33111da177e4SLinus Torvalds } 33121da177e4SLinus Torvalds 33131da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 33141da177e4SLinus Torvalds { 33151da177e4SLinus Torvalds struct page *page = NULL; 33161da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 33171da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 33181da177e4SLinus Torvalds if (page) { 33191da177e4SLinus Torvalds kunmap(page); 33201da177e4SLinus Torvalds page_cache_release(page); 33211da177e4SLinus Torvalds } 33221da177e4SLinus Torvalds return res; 33231da177e4SLinus Torvalds } 33241da177e4SLinus Torvalds 3325cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 33261da177e4SLinus Torvalds { 3327cc314eefSLinus Torvalds struct page *page = NULL; 33281da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 3329cc314eefSLinus Torvalds return page; 33301da177e4SLinus Torvalds } 33311da177e4SLinus Torvalds 3332cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 33331da177e4SLinus Torvalds { 3334cc314eefSLinus Torvalds struct page *page = cookie; 3335cc314eefSLinus Torvalds 3336cc314eefSLinus Torvalds if (page) { 33371da177e4SLinus Torvalds kunmap(page); 33381da177e4SLinus Torvalds page_cache_release(page); 33391da177e4SLinus Torvalds } 33401da177e4SLinus Torvalds } 33411da177e4SLinus Torvalds 334254566b2cSNick Piggin /* 334354566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 334454566b2cSNick Piggin */ 334554566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 33461da177e4SLinus Torvalds { 33471da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 33480adb25d2SKirill Korotaev struct page *page; 3349afddba49SNick Piggin void *fsdata; 3350beb497abSDmitriy Monakhov int err; 33511da177e4SLinus Torvalds char *kaddr; 335254566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 335354566b2cSNick Piggin if (nofs) 335454566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 33551da177e4SLinus Torvalds 33567e53cac4SNeilBrown retry: 3357afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 335854566b2cSNick Piggin flags, &page, &fsdata); 33591da177e4SLinus Torvalds if (err) 3360afddba49SNick Piggin goto fail; 3361afddba49SNick Piggin 33621da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 33631da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 33641da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 3365afddba49SNick Piggin 3366afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 3367afddba49SNick Piggin page, fsdata); 33681da177e4SLinus Torvalds if (err < 0) 33691da177e4SLinus Torvalds goto fail; 3370afddba49SNick Piggin if (err < len-1) 3371afddba49SNick Piggin goto retry; 3372afddba49SNick Piggin 33731da177e4SLinus Torvalds mark_inode_dirty(inode); 33741da177e4SLinus Torvalds return 0; 33751da177e4SLinus Torvalds fail: 33761da177e4SLinus Torvalds return err; 33771da177e4SLinus Torvalds } 33781da177e4SLinus Torvalds 33790adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 33800adb25d2SKirill Korotaev { 33810adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 338254566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 33830adb25d2SKirill Korotaev } 33840adb25d2SKirill Korotaev 338592e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 33861da177e4SLinus Torvalds .readlink = generic_readlink, 33871da177e4SLinus Torvalds .follow_link = page_follow_link_light, 33881da177e4SLinus Torvalds .put_link = page_put_link, 33891da177e4SLinus Torvalds }; 33901da177e4SLinus Torvalds 33912d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 3392cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one); 33931da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 33941da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 33951da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 33961da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 33971da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 33981da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 33991da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 34001da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 34011da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 34020adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 34031da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 34041da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 3405c9c6cac0SAl Viro EXPORT_SYMBOL(kern_path_parent); 3406d1811465SAl Viro EXPORT_SYMBOL(kern_path); 340716f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 3408f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 34098c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission); 34101da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 34111da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 34121da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 34131da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 34141da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 34151da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 34161da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 34171da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 34181da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 34191da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 34201da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 34211da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 34221da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 34231da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 3424