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 7325985edcSLucas De Marchi * the name is a symlink pointing to a non-existent 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 { 18226cf46beSLinus Torvalds unsigned int mode = inode->i_mode; 1835909ccaaSLinus Torvalds 1845909ccaaSLinus Torvalds mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 1855909ccaaSLinus Torvalds 186e795b717SSerge E. Hallyn if (current_user_ns() != inode_userns(inode)) 187e795b717SSerge E. Hallyn goto other_perms; 188e795b717SSerge E. Hallyn 1895909ccaaSLinus Torvalds if (current_fsuid() == inode->i_uid) 1905909ccaaSLinus Torvalds mode >>= 6; 1915909ccaaSLinus Torvalds else { 1925909ccaaSLinus Torvalds if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) { 193b74c79e9SNick Piggin int error = check_acl(inode, mask, flags); 1945909ccaaSLinus Torvalds if (error != -EAGAIN) 1955909ccaaSLinus Torvalds return error; 1965909ccaaSLinus Torvalds } 1975909ccaaSLinus Torvalds 1985909ccaaSLinus Torvalds if (in_group_p(inode->i_gid)) 1995909ccaaSLinus Torvalds mode >>= 3; 2005909ccaaSLinus Torvalds } 2015909ccaaSLinus Torvalds 202e795b717SSerge E. Hallyn other_perms: 2035909ccaaSLinus Torvalds /* 2045909ccaaSLinus Torvalds * If the DACs are ok we don't need any capability check. 2055909ccaaSLinus Torvalds */ 2065909ccaaSLinus Torvalds if ((mask & ~mode) == 0) 2075909ccaaSLinus Torvalds return 0; 2085909ccaaSLinus Torvalds return -EACCES; 2095909ccaaSLinus Torvalds } 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds /** 2121da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 2131da177e4SLinus Torvalds * @inode: inode to check access rights for 2141da177e4SLinus Torvalds * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 2151da177e4SLinus Torvalds * @check_acl: optional callback to check for Posix ACLs 21639191628SRandy Dunlap * @flags: IPERM_FLAG_ flags. 2171da177e4SLinus Torvalds * 2181da177e4SLinus Torvalds * Used to check for read/write/execute permissions on a file. 2191da177e4SLinus Torvalds * We use "fsuid" for this, letting us set arbitrary permissions 2201da177e4SLinus Torvalds * for filesystem access without changing the "normal" uids which 221b74c79e9SNick Piggin * are used for other things. 222b74c79e9SNick Piggin * 223b74c79e9SNick Piggin * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk 224b74c79e9SNick Piggin * request cannot be satisfied (eg. requires blocking or too much complexity). 225b74c79e9SNick Piggin * It would then be called again in ref-walk mode. 2261da177e4SLinus Torvalds */ 227b74c79e9SNick Piggin int generic_permission(struct inode *inode, int mask, unsigned int flags, 228b74c79e9SNick Piggin int (*check_acl)(struct inode *inode, int mask, unsigned int flags)) 2291da177e4SLinus Torvalds { 2305909ccaaSLinus Torvalds int ret; 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds /* 2335909ccaaSLinus Torvalds * Do the basic POSIX ACL permission checks. 2341da177e4SLinus Torvalds */ 235b74c79e9SNick Piggin ret = acl_permission_check(inode, mask, flags, check_acl); 2365909ccaaSLinus Torvalds if (ret != -EACCES) 2375909ccaaSLinus Torvalds return ret; 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds /* 2401da177e4SLinus Torvalds * Read/write DACs are always overridable. 2411da177e4SLinus Torvalds * Executable DACs are overridable if at least one exec bit is set. 2421da177e4SLinus Torvalds */ 243f696a365SMiklos Szeredi if (!(mask & MAY_EXEC) || execute_ok(inode)) 244e795b717SSerge E. Hallyn if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE)) 2451da177e4SLinus Torvalds return 0; 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds /* 2481da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 2491da177e4SLinus Torvalds */ 2507ea66001SSerge E. Hallyn mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 2511da177e4SLinus Torvalds if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE))) 252e795b717SSerge E. Hallyn if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH)) 2531da177e4SLinus Torvalds return 0; 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds return -EACCES; 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds 258cb23beb5SChristoph Hellwig /** 259cb23beb5SChristoph Hellwig * inode_permission - check for access rights to a given inode 260cb23beb5SChristoph Hellwig * @inode: inode to check permission on 261cb23beb5SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 262cb23beb5SChristoph Hellwig * 263cb23beb5SChristoph Hellwig * Used to check for read/write/execute permissions on an inode. 264cb23beb5SChristoph Hellwig * We use "fsuid" for this, letting us set arbitrary permissions 265cb23beb5SChristoph Hellwig * for filesystem access without changing the "normal" uids which 266cb23beb5SChristoph Hellwig * are used for other things. 267cb23beb5SChristoph Hellwig */ 268f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask) 2691da177e4SLinus Torvalds { 270e6305c43SAl Viro int retval; 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds if (mask & MAY_WRITE) { 27322590e41SMiklos Szeredi umode_t mode = inode->i_mode; 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds /* 2761da177e4SLinus Torvalds * Nobody gets write access to a read-only fs. 2771da177e4SLinus Torvalds */ 2781da177e4SLinus Torvalds if (IS_RDONLY(inode) && 2791da177e4SLinus Torvalds (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 2801da177e4SLinus Torvalds return -EROFS; 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds /* 2831da177e4SLinus Torvalds * Nobody gets write access to an immutable file. 2841da177e4SLinus Torvalds */ 2851da177e4SLinus Torvalds if (IS_IMMUTABLE(inode)) 2861da177e4SLinus Torvalds return -EACCES; 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds 289acfa4380SAl Viro if (inode->i_op->permission) 290b74c79e9SNick Piggin retval = inode->i_op->permission(inode, mask, 0); 291f696a365SMiklos Szeredi else 292b74c79e9SNick Piggin retval = generic_permission(inode, mask, 0, 293b74c79e9SNick Piggin inode->i_op->check_acl); 294f696a365SMiklos Szeredi 2951da177e4SLinus Torvalds if (retval) 2961da177e4SLinus Torvalds return retval; 2971da177e4SLinus Torvalds 29808ce5f16SSerge E. Hallyn retval = devcgroup_inode_permission(inode, mask); 29908ce5f16SSerge E. Hallyn if (retval) 30008ce5f16SSerge E. Hallyn return retval; 30108ce5f16SSerge E. Hallyn 302d09ca739SEric Paris return security_inode_permission(inode, mask); 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds 305e4543eddSChristoph Hellwig /** 3068c744fb8SChristoph Hellwig * file_permission - check for additional access rights to a given file 3078c744fb8SChristoph Hellwig * @file: file to check access rights for 3088c744fb8SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 3098c744fb8SChristoph Hellwig * 3108c744fb8SChristoph Hellwig * Used to check for read/write/execute permissions on an already opened 3118c744fb8SChristoph Hellwig * file. 3128c744fb8SChristoph Hellwig * 3138c744fb8SChristoph Hellwig * Note: 3148c744fb8SChristoph Hellwig * Do not use this function in new code. All access checks should 315cb23beb5SChristoph Hellwig * be done using inode_permission(). 3168c744fb8SChristoph Hellwig */ 3178c744fb8SChristoph Hellwig int file_permission(struct file *file, int mask) 3188c744fb8SChristoph Hellwig { 319f419a2e3SAl Viro return inode_permission(file->f_path.dentry->d_inode, mask); 3208c744fb8SChristoph Hellwig } 3218c744fb8SChristoph Hellwig 3221da177e4SLinus Torvalds /* 3231da177e4SLinus Torvalds * get_write_access() gets write permission for a file. 3241da177e4SLinus Torvalds * put_write_access() releases this write permission. 3251da177e4SLinus Torvalds * This is used for regular files. 3261da177e4SLinus Torvalds * We cannot support write (and maybe mmap read-write shared) accesses and 3271da177e4SLinus Torvalds * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode 3281da177e4SLinus Torvalds * can have the following values: 3291da177e4SLinus Torvalds * 0: no writers, no VM_DENYWRITE mappings 3301da177e4SLinus Torvalds * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist 3311da177e4SLinus Torvalds * > 0: (i_writecount) users are writing to the file. 3321da177e4SLinus Torvalds * 3331da177e4SLinus Torvalds * Normally we operate on that counter with atomic_{inc,dec} and it's safe 3341da177e4SLinus Torvalds * except for the cases where we don't hold i_writecount yet. Then we need to 3351da177e4SLinus Torvalds * use {get,deny}_write_access() - these functions check the sign and refuse 3361da177e4SLinus Torvalds * to do the change if sign is wrong. Exclusion between them is provided by 3371da177e4SLinus Torvalds * the inode->i_lock spinlock. 3381da177e4SLinus Torvalds */ 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds int get_write_access(struct inode * inode) 3411da177e4SLinus Torvalds { 3421da177e4SLinus Torvalds spin_lock(&inode->i_lock); 3431da177e4SLinus Torvalds if (atomic_read(&inode->i_writecount) < 0) { 3441da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3451da177e4SLinus Torvalds return -ETXTBSY; 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds atomic_inc(&inode->i_writecount); 3481da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds return 0; 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds int deny_write_access(struct file * file) 3541da177e4SLinus Torvalds { 3550f7fc9e4SJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds spin_lock(&inode->i_lock); 3581da177e4SLinus Torvalds if (atomic_read(&inode->i_writecount) > 0) { 3591da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3601da177e4SLinus Torvalds return -ETXTBSY; 3611da177e4SLinus Torvalds } 3621da177e4SLinus Torvalds atomic_dec(&inode->i_writecount); 3631da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds return 0; 3661da177e4SLinus Torvalds } 3671da177e4SLinus Torvalds 3681d957f9bSJan Blunck /** 3695dd784d0SJan Blunck * path_get - get a reference to a path 3705dd784d0SJan Blunck * @path: path to get the reference to 3715dd784d0SJan Blunck * 3725dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 3735dd784d0SJan Blunck */ 3745dd784d0SJan Blunck void path_get(struct path *path) 3755dd784d0SJan Blunck { 3765dd784d0SJan Blunck mntget(path->mnt); 3775dd784d0SJan Blunck dget(path->dentry); 3785dd784d0SJan Blunck } 3795dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 3805dd784d0SJan Blunck 3815dd784d0SJan Blunck /** 3821d957f9bSJan Blunck * path_put - put a reference to a path 3831d957f9bSJan Blunck * @path: path to put the reference to 3841d957f9bSJan Blunck * 3851d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 3861d957f9bSJan Blunck */ 3871d957f9bSJan Blunck void path_put(struct path *path) 3881da177e4SLinus Torvalds { 3891d957f9bSJan Blunck dput(path->dentry); 3901d957f9bSJan Blunck mntput(path->mnt); 3911da177e4SLinus Torvalds } 3921d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 3931da177e4SLinus Torvalds 394834f2a4aSTrond Myklebust /** 39531e6b01fSNick Piggin * nameidata_drop_rcu - drop this nameidata out of rcu-walk 39631e6b01fSNick Piggin * @nd: nameidata pathwalk data to drop 39739191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 39831e6b01fSNick Piggin * 39931e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 40031e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). __drop_rcu* functions attempt 40131e6b01fSNick Piggin * to drop out of rcu-walk mode and take normal reference counts on dentries 40231e6b01fSNick Piggin * and vfsmounts to transition to rcu-walk mode. __drop_rcu* functions take 40331e6b01fSNick Piggin * refcounts at the last known good point before rcu-walk got stuck, so 40431e6b01fSNick Piggin * ref-walk may continue from there. If this is not successful (eg. a seqcount 40531e6b01fSNick Piggin * has changed), then failure is returned and path walk restarts from the 40631e6b01fSNick Piggin * beginning in ref-walk mode. 40731e6b01fSNick Piggin * 40831e6b01fSNick Piggin * nameidata_drop_rcu attempts to drop the current nd->path and nd->root into 40931e6b01fSNick Piggin * ref-walk. Must be called from rcu-walk context. 41031e6b01fSNick Piggin */ 41131e6b01fSNick Piggin static int nameidata_drop_rcu(struct nameidata *nd) 41231e6b01fSNick Piggin { 41331e6b01fSNick Piggin struct fs_struct *fs = current->fs; 41431e6b01fSNick Piggin struct dentry *dentry = nd->path.dentry; 4155b6ca027SAl Viro int want_root = 0; 41631e6b01fSNick Piggin 41731e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 4185b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 4195b6ca027SAl Viro want_root = 1; 42031e6b01fSNick Piggin spin_lock(&fs->lock); 42131e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 42231e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 42331e6b01fSNick Piggin goto err_root; 42431e6b01fSNick Piggin } 42531e6b01fSNick Piggin spin_lock(&dentry->d_lock); 42631e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 42731e6b01fSNick Piggin goto err; 42831e6b01fSNick Piggin BUG_ON(nd->inode != dentry->d_inode); 42931e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 4305b6ca027SAl Viro if (want_root) { 43131e6b01fSNick Piggin path_get(&nd->root); 43231e6b01fSNick Piggin spin_unlock(&fs->lock); 43331e6b01fSNick Piggin } 43431e6b01fSNick Piggin mntget(nd->path.mnt); 43531e6b01fSNick Piggin 43631e6b01fSNick Piggin rcu_read_unlock(); 43731e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 43831e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 43931e6b01fSNick Piggin return 0; 44031e6b01fSNick Piggin err: 44131e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 44231e6b01fSNick Piggin err_root: 4435b6ca027SAl Viro if (want_root) 44431e6b01fSNick Piggin spin_unlock(&fs->lock); 44531e6b01fSNick Piggin return -ECHILD; 44631e6b01fSNick Piggin } 44731e6b01fSNick Piggin 44831e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */ 44931e6b01fSNick Piggin static inline int nameidata_drop_rcu_maybe(struct nameidata *nd) 45031e6b01fSNick Piggin { 45131e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) 45231e6b01fSNick Piggin return nameidata_drop_rcu(nd); 45331e6b01fSNick Piggin return 0; 45431e6b01fSNick Piggin } 45531e6b01fSNick Piggin 45631e6b01fSNick Piggin /** 45731e6b01fSNick Piggin * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk 45831e6b01fSNick Piggin * @nd: nameidata pathwalk data to drop 45931e6b01fSNick Piggin * @dentry: dentry to drop 46039191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 46131e6b01fSNick Piggin * 46231e6b01fSNick Piggin * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root, 46331e6b01fSNick Piggin * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on 46431e6b01fSNick Piggin * @nd. Must be called from rcu-walk context. 46531e6b01fSNick Piggin */ 46631e6b01fSNick Piggin static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry) 46731e6b01fSNick Piggin { 46831e6b01fSNick Piggin struct fs_struct *fs = current->fs; 46931e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 4705b6ca027SAl Viro int want_root = 0; 47131e6b01fSNick Piggin 47231e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 4735b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 4745b6ca027SAl Viro want_root = 1; 47531e6b01fSNick Piggin spin_lock(&fs->lock); 47631e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 47731e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 47831e6b01fSNick Piggin goto err_root; 47931e6b01fSNick Piggin } 48031e6b01fSNick Piggin spin_lock(&parent->d_lock); 48131e6b01fSNick Piggin spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); 48231e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 48331e6b01fSNick Piggin goto err; 48431e6b01fSNick Piggin /* 48531e6b01fSNick Piggin * If the sequence check on the child dentry passed, then the child has 48631e6b01fSNick Piggin * not been removed from its parent. This means the parent dentry must 48731e6b01fSNick Piggin * be valid and able to take a reference at this point. 48831e6b01fSNick Piggin */ 48931e6b01fSNick Piggin BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); 49031e6b01fSNick Piggin BUG_ON(!parent->d_count); 49131e6b01fSNick Piggin parent->d_count++; 49231e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 49331e6b01fSNick Piggin spin_unlock(&parent->d_lock); 4945b6ca027SAl Viro if (want_root) { 49531e6b01fSNick Piggin path_get(&nd->root); 49631e6b01fSNick Piggin spin_unlock(&fs->lock); 49731e6b01fSNick Piggin } 49831e6b01fSNick Piggin mntget(nd->path.mnt); 49931e6b01fSNick Piggin 50031e6b01fSNick Piggin rcu_read_unlock(); 50131e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 50231e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 50331e6b01fSNick Piggin return 0; 50431e6b01fSNick Piggin err: 50531e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 50631e6b01fSNick Piggin spin_unlock(&parent->d_lock); 50731e6b01fSNick Piggin err_root: 5085b6ca027SAl Viro if (want_root) 50931e6b01fSNick Piggin spin_unlock(&fs->lock); 51031e6b01fSNick Piggin return -ECHILD; 51131e6b01fSNick Piggin } 51231e6b01fSNick Piggin 51331e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */ 51431e6b01fSNick Piggin static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry) 51531e6b01fSNick Piggin { 516a7472babSAl Viro if (nd->flags & LOOKUP_RCU) { 517a7472babSAl Viro if (unlikely(nameidata_dentry_drop_rcu(nd, dentry))) { 518a7472babSAl Viro nd->flags &= ~LOOKUP_RCU; 5195b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 520a7472babSAl Viro nd->root.mnt = NULL; 521a7472babSAl Viro rcu_read_unlock(); 522a7472babSAl Viro br_read_unlock(vfsmount_lock); 523a7472babSAl Viro return -ECHILD; 524a7472babSAl Viro } 525a7472babSAl Viro } 52631e6b01fSNick Piggin return 0; 52731e6b01fSNick Piggin } 52831e6b01fSNick Piggin 52931e6b01fSNick Piggin /** 53031e6b01fSNick Piggin * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk 53131e6b01fSNick Piggin * @nd: nameidata pathwalk data to drop 53239191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 53331e6b01fSNick Piggin * 53431e6b01fSNick Piggin * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk. 53531e6b01fSNick Piggin * nd->path should be the final element of the lookup, so nd->root is discarded. 53631e6b01fSNick Piggin * Must be called from rcu-walk context. 53731e6b01fSNick Piggin */ 53831e6b01fSNick Piggin static int nameidata_drop_rcu_last(struct nameidata *nd) 53931e6b01fSNick Piggin { 54031e6b01fSNick Piggin struct dentry *dentry = nd->path.dentry; 54131e6b01fSNick Piggin 54231e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 54331e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 5445b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 54531e6b01fSNick Piggin nd->root.mnt = NULL; 54631e6b01fSNick Piggin spin_lock(&dentry->d_lock); 54731e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 54831e6b01fSNick Piggin goto err_unlock; 54931e6b01fSNick Piggin BUG_ON(nd->inode != dentry->d_inode); 55031e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 55131e6b01fSNick Piggin 55231e6b01fSNick Piggin mntget(nd->path.mnt); 55331e6b01fSNick Piggin 55431e6b01fSNick Piggin rcu_read_unlock(); 55531e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 55631e6b01fSNick Piggin 55731e6b01fSNick Piggin return 0; 55831e6b01fSNick Piggin 55931e6b01fSNick Piggin err_unlock: 56031e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 56131e6b01fSNick Piggin rcu_read_unlock(); 56231e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 56331e6b01fSNick Piggin return -ECHILD; 56431e6b01fSNick Piggin } 56531e6b01fSNick Piggin 56631e6b01fSNick Piggin /** 567834f2a4aSTrond Myklebust * release_open_intent - free up open intent resources 568834f2a4aSTrond Myklebust * @nd: pointer to nameidata 569834f2a4aSTrond Myklebust */ 570834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd) 571834f2a4aSTrond Myklebust { 5722dab5974SLinus Torvalds struct file *file = nd->intent.open.file; 5732dab5974SLinus Torvalds 5742dab5974SLinus Torvalds if (file && !IS_ERR(file)) { 5752dab5974SLinus Torvalds if (file->f_path.dentry == NULL) 5762dab5974SLinus Torvalds put_filp(file); 577834f2a4aSTrond Myklebust else 5782dab5974SLinus Torvalds fput(file); 5792dab5974SLinus Torvalds } 580834f2a4aSTrond Myklebust } 581834f2a4aSTrond Myklebust 582f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd) 58334286d66SNick Piggin { 584f60aef7eSAl Viro return dentry->d_op->d_revalidate(dentry, nd); 58534286d66SNick Piggin } 58634286d66SNick Piggin 587f5e1c1c1SAl Viro static struct dentry * 588bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd) 589bcdc5e01SIan Kent { 590f5e1c1c1SAl Viro int status = d_revalidate(dentry, nd); 591bcdc5e01SIan Kent if (unlikely(status <= 0)) { 592bcdc5e01SIan Kent /* 593bcdc5e01SIan Kent * The dentry failed validation. 594bcdc5e01SIan Kent * If d_revalidate returned 0 attempt to invalidate 595bcdc5e01SIan Kent * the dentry otherwise d_revalidate is asking us 596bcdc5e01SIan Kent * to return a fail status. 597bcdc5e01SIan Kent */ 59834286d66SNick Piggin if (status < 0) { 59934286d66SNick Piggin dput(dentry); 60034286d66SNick Piggin dentry = ERR_PTR(status); 601f5e1c1c1SAl Viro } else if (!d_invalidate(dentry)) { 602bcdc5e01SIan Kent dput(dentry); 603bcdc5e01SIan Kent dentry = NULL; 604bcdc5e01SIan Kent } 605bcdc5e01SIan Kent } 606f5e1c1c1SAl Viro return dentry; 607f5e1c1c1SAl Viro } 608f5e1c1c1SAl Viro 6091da177e4SLinus Torvalds /* 61016c2cd71SAl Viro * handle_reval_path - force revalidation of a dentry 61139159de2SJeff Layton * 61239159de2SJeff Layton * In some situations the path walking code will trust dentries without 61339159de2SJeff Layton * revalidating them. This causes problems for filesystems that depend on 61439159de2SJeff Layton * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set 61539159de2SJeff Layton * (which indicates that it's possible for the dentry to go stale), force 61639159de2SJeff Layton * a d_revalidate call before proceeding. 61739159de2SJeff Layton * 61839159de2SJeff Layton * Returns 0 if the revalidation was successful. If the revalidation fails, 61939159de2SJeff Layton * either return the error returned by d_revalidate or -ESTALE if the 62039159de2SJeff Layton * revalidation it just returned 0. If d_revalidate returns 0, we attempt to 62139159de2SJeff Layton * invalidate the dentry. It's up to the caller to handle putting references 62239159de2SJeff Layton * to the path if necessary. 62339159de2SJeff Layton */ 62416c2cd71SAl Viro static inline int handle_reval_path(struct nameidata *nd) 62539159de2SJeff Layton { 62616c2cd71SAl Viro struct dentry *dentry = nd->path.dentry; 62739159de2SJeff Layton int status; 62839159de2SJeff Layton 62916c2cd71SAl Viro if (likely(!(nd->flags & LOOKUP_JUMPED))) 63039159de2SJeff Layton return 0; 63139159de2SJeff Layton 63216c2cd71SAl Viro if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE))) 63316c2cd71SAl Viro return 0; 63416c2cd71SAl Viro 63516c2cd71SAl Viro if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))) 63616c2cd71SAl Viro return 0; 63716c2cd71SAl Viro 63816c2cd71SAl Viro /* Note: we do not d_invalidate() */ 63934286d66SNick Piggin status = d_revalidate(dentry, nd); 64039159de2SJeff Layton if (status > 0) 64139159de2SJeff Layton return 0; 64239159de2SJeff Layton 64316c2cd71SAl Viro if (!status) 64439159de2SJeff Layton status = -ESTALE; 64516c2cd71SAl Viro 64639159de2SJeff Layton return status; 64739159de2SJeff Layton } 64839159de2SJeff Layton 64939159de2SJeff Layton /* 650b75b5086SAl Viro * Short-cut version of permission(), for calling on directories 651b75b5086SAl Viro * during pathname resolution. Combines parts of permission() 652b75b5086SAl Viro * and generic_permission(), and tests ONLY for MAY_EXEC permission. 6531da177e4SLinus Torvalds * 6541da177e4SLinus Torvalds * If appropriate, check DAC only. If not appropriate, or 655b75b5086SAl Viro * short-cut DAC fails, then call ->permission() to do more 6561da177e4SLinus Torvalds * complete permission check. 6571da177e4SLinus Torvalds */ 658b74c79e9SNick Piggin static inline int exec_permission(struct inode *inode, unsigned int flags) 6591da177e4SLinus Torvalds { 6605909ccaaSLinus Torvalds int ret; 661e795b717SSerge E. Hallyn struct user_namespace *ns = inode_userns(inode); 6621da177e4SLinus Torvalds 663cb9179eaSLinus Torvalds if (inode->i_op->permission) { 664b74c79e9SNick Piggin ret = inode->i_op->permission(inode, MAY_EXEC, flags); 665b74c79e9SNick Piggin } else { 666b74c79e9SNick Piggin ret = acl_permission_check(inode, MAY_EXEC, flags, 667b74c79e9SNick Piggin inode->i_op->check_acl); 668cb9179eaSLinus Torvalds } 669b74c79e9SNick Piggin if (likely(!ret)) 6701da177e4SLinus Torvalds goto ok; 671b74c79e9SNick Piggin if (ret == -ECHILD) 67231e6b01fSNick Piggin return ret; 6731da177e4SLinus Torvalds 674e795b717SSerge E. Hallyn if (ns_capable(ns, CAP_DAC_OVERRIDE) || 675e795b717SSerge E. Hallyn ns_capable(ns, CAP_DAC_READ_SEARCH)) 6761da177e4SLinus Torvalds goto ok; 6771da177e4SLinus Torvalds 6785909ccaaSLinus Torvalds return ret; 6791da177e4SLinus Torvalds ok: 680b74c79e9SNick Piggin return security_inode_exec_permission(inode, flags); 6811da177e4SLinus Torvalds } 6821da177e4SLinus Torvalds 6832a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 6842a737871SAl Viro { 685f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 686f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 6872a737871SAl Viro } 6882a737871SAl Viro 6896de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 6906de88d72SAl Viro 69131e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 69231e6b01fSNick Piggin { 69331e6b01fSNick Piggin if (!nd->root.mnt) { 69431e6b01fSNick Piggin struct fs_struct *fs = current->fs; 695c28cc364SNick Piggin unsigned seq; 696c28cc364SNick Piggin 697c28cc364SNick Piggin do { 698c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 69931e6b01fSNick Piggin nd->root = fs->root; 700c1530019STim Chen nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq); 701c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 70231e6b01fSNick Piggin } 70331e6b01fSNick Piggin } 70431e6b01fSNick Piggin 705f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 7061da177e4SLinus Torvalds { 70731e6b01fSNick Piggin int ret; 70831e6b01fSNick Piggin 7091da177e4SLinus Torvalds if (IS_ERR(link)) 7101da177e4SLinus Torvalds goto fail; 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds if (*link == '/') { 7132a737871SAl Viro set_root(nd); 7141d957f9bSJan Blunck path_put(&nd->path); 7152a737871SAl Viro nd->path = nd->root; 7162a737871SAl Viro path_get(&nd->root); 71716c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 7181da177e4SLinus Torvalds } 71931e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 720b4091d5fSChristoph Hellwig 72131e6b01fSNick Piggin ret = link_path_walk(link, nd); 72231e6b01fSNick Piggin return ret; 7231da177e4SLinus Torvalds fail: 7241d957f9bSJan Blunck path_put(&nd->path); 7251da177e4SLinus Torvalds return PTR_ERR(link); 7261da177e4SLinus Torvalds } 7271da177e4SLinus Torvalds 7281d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 729051d3812SIan Kent { 730051d3812SIan Kent dput(path->dentry); 7314ac91378SJan Blunck if (path->mnt != nd->path.mnt) 732051d3812SIan Kent mntput(path->mnt); 733051d3812SIan Kent } 734051d3812SIan Kent 7357b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path, 7367b9337aaSNick Piggin struct nameidata *nd) 737051d3812SIan Kent { 73831e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 7394ac91378SJan Blunck dput(nd->path.dentry); 74031e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 7414ac91378SJan Blunck mntput(nd->path.mnt); 7429a229683SHuang Shijie } 74331e6b01fSNick Piggin nd->path.mnt = path->mnt; 7444ac91378SJan Blunck nd->path.dentry = path->dentry; 745051d3812SIan Kent } 746051d3812SIan Kent 747574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie) 748574197e0SAl Viro { 749574197e0SAl Viro struct inode *inode = link->dentry->d_inode; 750574197e0SAl Viro if (!IS_ERR(cookie) && inode->i_op->put_link) 751574197e0SAl Viro inode->i_op->put_link(link->dentry, nd, cookie); 752574197e0SAl Viro path_put(link); 753574197e0SAl Viro } 754574197e0SAl Viro 755def4af30SAl Viro static __always_inline int 756574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p) 7571da177e4SLinus Torvalds { 7581da177e4SLinus Torvalds int error; 7597b9337aaSNick Piggin struct dentry *dentry = link->dentry; 7601da177e4SLinus Torvalds 761844a3917SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 762844a3917SAl Viro 7630e794589SAl Viro if (link->mnt == nd->path.mnt) 7640e794589SAl Viro mntget(link->mnt); 7650e794589SAl Viro 766574197e0SAl Viro if (unlikely(current->total_link_count >= 40)) { 767574197e0SAl Viro *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */ 768574197e0SAl Viro path_put(&nd->path); 769574197e0SAl Viro return -ELOOP; 770574197e0SAl Viro } 771574197e0SAl Viro cond_resched(); 772574197e0SAl Viro current->total_link_count++; 773574197e0SAl Viro 7747b9337aaSNick Piggin touch_atime(link->mnt, dentry); 7751da177e4SLinus Torvalds nd_set_link(nd, NULL); 776cd4e91d3SAl Viro 77736f3b4f6SAl Viro error = security_inode_follow_link(link->dentry, nd); 77836f3b4f6SAl Viro if (error) { 77936f3b4f6SAl Viro *p = ERR_PTR(error); /* no ->put_link(), please */ 78036f3b4f6SAl Viro path_put(&nd->path); 78136f3b4f6SAl Viro return error; 78236f3b4f6SAl Viro } 78336f3b4f6SAl Viro 78486acdca1SAl Viro nd->last_type = LAST_BIND; 785def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 786def4af30SAl Viro error = PTR_ERR(*p); 787def4af30SAl Viro if (!IS_ERR(*p)) { 7881da177e4SLinus Torvalds char *s = nd_get_link(nd); 789cc314eefSLinus Torvalds error = 0; 7901da177e4SLinus Torvalds if (s) 7911da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 792bcda7652SAl Viro else if (nd->last_type == LAST_BIND) { 79316c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 794b21041d0SAl Viro nd->inode = nd->path.dentry->d_inode; 795b21041d0SAl Viro if (nd->inode->i_op->follow_link) { 796bcda7652SAl Viro /* stepped on a _really_ weird one */ 797bcda7652SAl Viro path_put(&nd->path); 798bcda7652SAl Viro error = -ELOOP; 799bcda7652SAl Viro } 800bcda7652SAl Viro } 8011da177e4SLinus Torvalds } 8021da177e4SLinus Torvalds return error; 8031da177e4SLinus Torvalds } 8041da177e4SLinus Torvalds 80531e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 80631e6b01fSNick Piggin { 80731e6b01fSNick Piggin struct vfsmount *parent; 80831e6b01fSNick Piggin struct dentry *mountpoint; 80931e6b01fSNick Piggin 81031e6b01fSNick Piggin parent = path->mnt->mnt_parent; 81131e6b01fSNick Piggin if (parent == path->mnt) 81231e6b01fSNick Piggin return 0; 81331e6b01fSNick Piggin mountpoint = path->mnt->mnt_mountpoint; 81431e6b01fSNick Piggin path->dentry = mountpoint; 81531e6b01fSNick Piggin path->mnt = parent; 81631e6b01fSNick Piggin return 1; 81731e6b01fSNick Piggin } 81831e6b01fSNick Piggin 819bab77ebfSAl Viro int follow_up(struct path *path) 8201da177e4SLinus Torvalds { 8211da177e4SLinus Torvalds struct vfsmount *parent; 8221da177e4SLinus Torvalds struct dentry *mountpoint; 82399b7db7bSNick Piggin 82499b7db7bSNick Piggin br_read_lock(vfsmount_lock); 825bab77ebfSAl Viro parent = path->mnt->mnt_parent; 826bab77ebfSAl Viro if (parent == path->mnt) { 82799b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 8281da177e4SLinus Torvalds return 0; 8291da177e4SLinus Torvalds } 8301da177e4SLinus Torvalds mntget(parent); 831bab77ebfSAl Viro mountpoint = dget(path->mnt->mnt_mountpoint); 83299b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 833bab77ebfSAl Viro dput(path->dentry); 834bab77ebfSAl Viro path->dentry = mountpoint; 835bab77ebfSAl Viro mntput(path->mnt); 836bab77ebfSAl Viro path->mnt = parent; 8371da177e4SLinus Torvalds return 1; 8381da177e4SLinus Torvalds } 8391da177e4SLinus Torvalds 840b5c84bf6SNick Piggin /* 8419875cf80SDavid Howells * Perform an automount 8429875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 8439875cf80SDavid Howells * were called with. 8441da177e4SLinus Torvalds */ 8459875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags, 8469875cf80SDavid Howells bool *need_mntput) 84731e6b01fSNick Piggin { 8489875cf80SDavid Howells struct vfsmount *mnt; 849ea5b778aSDavid Howells int err; 8509875cf80SDavid Howells 8519875cf80SDavid Howells if (!path->dentry->d_op || !path->dentry->d_op->d_automount) 8529875cf80SDavid Howells return -EREMOTE; 8539875cf80SDavid Howells 8546f45b656SDavid Howells /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT 8556f45b656SDavid Howells * and this is the terminal part of the path. 8566f45b656SDavid Howells */ 8576f45b656SDavid Howells if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE)) 8586f45b656SDavid Howells return -EISDIR; /* we actually want to stop here */ 8596f45b656SDavid Howells 8609875cf80SDavid Howells /* We want to mount if someone is trying to open/create a file of any 8619875cf80SDavid Howells * type under the mountpoint, wants to traverse through the mountpoint 8629875cf80SDavid Howells * or wants to open the mounted directory. 8639875cf80SDavid Howells * 8649875cf80SDavid Howells * We don't want to mount if someone's just doing a stat and they've 8659875cf80SDavid Howells * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and 8669875cf80SDavid Howells * appended a '/' to the name. 8679875cf80SDavid Howells */ 8689875cf80SDavid Howells if (!(flags & LOOKUP_FOLLOW) && 8699875cf80SDavid Howells !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY | 8709875cf80SDavid Howells LOOKUP_OPEN | LOOKUP_CREATE))) 8719875cf80SDavid Howells return -EISDIR; 8729875cf80SDavid Howells 8739875cf80SDavid Howells current->total_link_count++; 8749875cf80SDavid Howells if (current->total_link_count >= 40) 8759875cf80SDavid Howells return -ELOOP; 8769875cf80SDavid Howells 8779875cf80SDavid Howells mnt = path->dentry->d_op->d_automount(path); 8789875cf80SDavid Howells if (IS_ERR(mnt)) { 8799875cf80SDavid Howells /* 8809875cf80SDavid Howells * The filesystem is allowed to return -EISDIR here to indicate 8819875cf80SDavid Howells * it doesn't want to automount. For instance, autofs would do 8829875cf80SDavid Howells * this so that its userspace daemon can mount on this dentry. 8839875cf80SDavid Howells * 8849875cf80SDavid Howells * However, we can only permit this if it's a terminal point in 8859875cf80SDavid Howells * the path being looked up; if it wasn't then the remainder of 8869875cf80SDavid Howells * the path is inaccessible and we should say so. 8879875cf80SDavid Howells */ 8889875cf80SDavid Howells if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE)) 8899875cf80SDavid Howells return -EREMOTE; 8909875cf80SDavid Howells return PTR_ERR(mnt); 89131e6b01fSNick Piggin } 892ea5b778aSDavid Howells 8939875cf80SDavid Howells if (!mnt) /* mount collision */ 8949875cf80SDavid Howells return 0; 8959875cf80SDavid Howells 89619a167afSAl Viro err = finish_automount(mnt, path); 897ea5b778aSDavid Howells 898ea5b778aSDavid Howells switch (err) { 899ea5b778aSDavid Howells case -EBUSY: 900ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 90119a167afSAl Viro return 0; 902ea5b778aSDavid Howells case 0: 903463ffb2eSAl Viro dput(path->dentry); 9049875cf80SDavid Howells if (*need_mntput) 9059875cf80SDavid Howells mntput(path->mnt); 9069875cf80SDavid Howells path->mnt = mnt; 9079875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 9089875cf80SDavid Howells *need_mntput = true; 9099875cf80SDavid Howells return 0; 91019a167afSAl Viro default: 91119a167afSAl Viro return err; 9129875cf80SDavid Howells } 91319a167afSAl Viro 914ea5b778aSDavid Howells } 9159875cf80SDavid Howells 9169875cf80SDavid Howells /* 9179875cf80SDavid Howells * Handle a dentry that is managed in some way. 918cc53ce53SDavid Howells * - Flagged for transit management (autofs) 9199875cf80SDavid Howells * - Flagged as mountpoint 9209875cf80SDavid Howells * - Flagged as automount point 9219875cf80SDavid Howells * 9229875cf80SDavid Howells * This may only be called in refwalk mode. 9239875cf80SDavid Howells * 9249875cf80SDavid Howells * Serialization is taken care of in namespace.c 9259875cf80SDavid Howells */ 9269875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 9279875cf80SDavid Howells { 9289875cf80SDavid Howells unsigned managed; 9299875cf80SDavid Howells bool need_mntput = false; 9309875cf80SDavid Howells int ret; 9319875cf80SDavid Howells 9329875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 9339875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 9349875cf80SDavid Howells * the components of that value change under us */ 9359875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 9369875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 9379875cf80SDavid Howells unlikely(managed != 0)) { 938cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 939cc53ce53SDavid Howells * being held. */ 940cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 941cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 942cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 9431aed3e42SAl Viro ret = path->dentry->d_op->d_manage(path->dentry, false); 944cc53ce53SDavid Howells if (ret < 0) 945cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 946cc53ce53SDavid Howells } 947cc53ce53SDavid Howells 9489875cf80SDavid Howells /* Transit to a mounted filesystem. */ 9499875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 9509875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 9519875cf80SDavid Howells if (mounted) { 9529875cf80SDavid Howells dput(path->dentry); 9539875cf80SDavid Howells if (need_mntput) 954463ffb2eSAl Viro mntput(path->mnt); 955463ffb2eSAl Viro path->mnt = mounted; 956463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 9579875cf80SDavid Howells need_mntput = true; 9589875cf80SDavid Howells continue; 959463ffb2eSAl Viro } 960463ffb2eSAl Viro 9619875cf80SDavid Howells /* Something is mounted on this dentry in another 9629875cf80SDavid Howells * namespace and/or whatever was mounted there in this 9639875cf80SDavid Howells * namespace got unmounted before we managed to get the 9649875cf80SDavid Howells * vfsmount_lock */ 9651da177e4SLinus Torvalds } 9669875cf80SDavid Howells 9679875cf80SDavid Howells /* Handle an automount point */ 9689875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 9699875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 9709875cf80SDavid Howells if (ret < 0) 9719875cf80SDavid Howells return ret == -EISDIR ? 0 : ret; 9729875cf80SDavid Howells continue; 9739875cf80SDavid Howells } 9749875cf80SDavid Howells 9759875cf80SDavid Howells /* We didn't change the current path point */ 9769875cf80SDavid Howells break; 9779875cf80SDavid Howells } 9789875cf80SDavid Howells return 0; 9791da177e4SLinus Torvalds } 9801da177e4SLinus Torvalds 981cc53ce53SDavid Howells int follow_down_one(struct path *path) 9821da177e4SLinus Torvalds { 9831da177e4SLinus Torvalds struct vfsmount *mounted; 9841da177e4SLinus Torvalds 9851c755af4SAl Viro mounted = lookup_mnt(path); 9861da177e4SLinus Torvalds if (mounted) { 9879393bd07SAl Viro dput(path->dentry); 9889393bd07SAl Viro mntput(path->mnt); 9899393bd07SAl Viro path->mnt = mounted; 9909393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 9911da177e4SLinus Torvalds return 1; 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds return 0; 9941da177e4SLinus Torvalds } 9951da177e4SLinus Torvalds 99662a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry) 99762a7375eSIan Kent { 99862a7375eSIan Kent return (dentry->d_flags & DCACHE_MANAGE_TRANSIT && 99962a7375eSIan Kent dentry->d_op->d_manage(dentry, true) < 0); 100062a7375eSIan Kent } 100162a7375eSIan Kent 10029875cf80SDavid Howells /* 10039875cf80SDavid Howells * Skip to top of mountpoint pile in rcuwalk mode. We abort the rcu-walk if we 1004cc53ce53SDavid Howells * meet a managed dentry and we're not walking to "..". True is returned to 10059875cf80SDavid Howells * continue, false to abort. 10069875cf80SDavid Howells */ 10079875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 10089875cf80SDavid Howells struct inode **inode, bool reverse_transit) 10099875cf80SDavid Howells { 101062a7375eSIan Kent for (;;) { 10119875cf80SDavid Howells struct vfsmount *mounted; 101262a7375eSIan Kent /* 101362a7375eSIan Kent * Don't forget we might have a non-mountpoint managed dentry 101462a7375eSIan Kent * that wants to block transit. 101562a7375eSIan Kent */ 101662a7375eSIan Kent *inode = path->dentry->d_inode; 101762a7375eSIan Kent if (!reverse_transit && 101862a7375eSIan Kent unlikely(managed_dentry_might_block(path->dentry))) 1019ab90911fSDavid Howells return false; 102062a7375eSIan Kent 102162a7375eSIan Kent if (!d_mountpoint(path->dentry)) 102262a7375eSIan Kent break; 102362a7375eSIan Kent 10249875cf80SDavid Howells mounted = __lookup_mnt(path->mnt, path->dentry, 1); 10259875cf80SDavid Howells if (!mounted) 10269875cf80SDavid Howells break; 10279875cf80SDavid Howells path->mnt = mounted; 10289875cf80SDavid Howells path->dentry = mounted->mnt_root; 10299875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 10309875cf80SDavid Howells } 10319875cf80SDavid Howells 10329875cf80SDavid Howells if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 10339875cf80SDavid Howells return reverse_transit; 10349875cf80SDavid Howells return true; 10359875cf80SDavid Howells } 10369875cf80SDavid Howells 103731e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 103831e6b01fSNick Piggin { 103931e6b01fSNick Piggin struct inode *inode = nd->inode; 104031e6b01fSNick Piggin 104131e6b01fSNick Piggin set_root_rcu(nd); 104231e6b01fSNick Piggin 104331e6b01fSNick Piggin while (1) { 104431e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 104531e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 104631e6b01fSNick Piggin break; 104731e6b01fSNick Piggin } 104831e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 104931e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 105031e6b01fSNick Piggin struct dentry *parent = old->d_parent; 105131e6b01fSNick Piggin unsigned seq; 105231e6b01fSNick Piggin 105331e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 105431e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 1055ef7562d5SAl Viro goto failed; 105631e6b01fSNick Piggin inode = parent->d_inode; 105731e6b01fSNick Piggin nd->path.dentry = parent; 105831e6b01fSNick Piggin nd->seq = seq; 105931e6b01fSNick Piggin break; 106031e6b01fSNick Piggin } 106131e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 106231e6b01fSNick Piggin break; 106331e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 106431e6b01fSNick Piggin inode = nd->path.dentry->d_inode; 106531e6b01fSNick Piggin } 10669875cf80SDavid Howells __follow_mount_rcu(nd, &nd->path, &inode, true); 106731e6b01fSNick Piggin nd->inode = inode; 106831e6b01fSNick Piggin return 0; 1069ef7562d5SAl Viro 1070ef7562d5SAl Viro failed: 1071ef7562d5SAl Viro nd->flags &= ~LOOKUP_RCU; 10725b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1073ef7562d5SAl Viro nd->root.mnt = NULL; 1074ef7562d5SAl Viro rcu_read_unlock(); 1075ef7562d5SAl Viro br_read_unlock(vfsmount_lock); 1076ef7562d5SAl Viro return -ECHILD; 107731e6b01fSNick Piggin } 107831e6b01fSNick Piggin 10799875cf80SDavid Howells /* 1080cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 1081cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 1082cc53ce53SDavid Howells * caller is permitted to proceed or not. 1083cc53ce53SDavid Howells * 1084cc53ce53SDavid Howells * Care must be taken as namespace_sem may be held (indicated by mounting_here 1085cc53ce53SDavid Howells * being true). 1086cc53ce53SDavid Howells */ 10877cc90cc3SAl Viro int follow_down(struct path *path) 1088cc53ce53SDavid Howells { 1089cc53ce53SDavid Howells unsigned managed; 1090cc53ce53SDavid Howells int ret; 1091cc53ce53SDavid Howells 1092cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 1093cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 1094cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 1095cc53ce53SDavid Howells * being held. 1096cc53ce53SDavid Howells * 1097cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 1098cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 1099cc53ce53SDavid Howells * other than its daemon the right to mount on its 1100cc53ce53SDavid Howells * superstructure. 1101cc53ce53SDavid Howells * 1102cc53ce53SDavid Howells * The filesystem may sleep at this point. 1103cc53ce53SDavid Howells */ 1104cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 1105cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 1106cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 1107ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 11081aed3e42SAl Viro path->dentry, false); 1109cc53ce53SDavid Howells if (ret < 0) 1110cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 1111cc53ce53SDavid Howells } 1112cc53ce53SDavid Howells 1113cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 1114cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 1115cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 1116cc53ce53SDavid Howells if (!mounted) 1117cc53ce53SDavid Howells break; 1118cc53ce53SDavid Howells dput(path->dentry); 1119cc53ce53SDavid Howells mntput(path->mnt); 1120cc53ce53SDavid Howells path->mnt = mounted; 1121cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 1122cc53ce53SDavid Howells continue; 1123cc53ce53SDavid Howells } 1124cc53ce53SDavid Howells 1125cc53ce53SDavid Howells /* Don't handle automount points here */ 1126cc53ce53SDavid Howells break; 1127cc53ce53SDavid Howells } 1128cc53ce53SDavid Howells return 0; 1129cc53ce53SDavid Howells } 1130cc53ce53SDavid Howells 1131cc53ce53SDavid Howells /* 11329875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 11339875cf80SDavid Howells */ 11349875cf80SDavid Howells static void follow_mount(struct path *path) 11359875cf80SDavid Howells { 11369875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 11379875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 11389875cf80SDavid Howells if (!mounted) 11399875cf80SDavid Howells break; 11409875cf80SDavid Howells dput(path->dentry); 11419875cf80SDavid Howells mntput(path->mnt); 11429875cf80SDavid Howells path->mnt = mounted; 11439875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 11449875cf80SDavid Howells } 11459875cf80SDavid Howells } 11469875cf80SDavid Howells 114731e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 11481da177e4SLinus Torvalds { 11492a737871SAl Viro set_root(nd); 1150e518ddb7SAndreas Mohr 11511da177e4SLinus Torvalds while(1) { 11524ac91378SJan Blunck struct dentry *old = nd->path.dentry; 11531da177e4SLinus Torvalds 11542a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 11552a737871SAl Viro nd->path.mnt == nd->root.mnt) { 11561da177e4SLinus Torvalds break; 11571da177e4SLinus Torvalds } 11584ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 11593088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 11603088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 11611da177e4SLinus Torvalds dput(old); 11621da177e4SLinus Torvalds break; 11631da177e4SLinus Torvalds } 11643088dd70SAl Viro if (!follow_up(&nd->path)) 11651da177e4SLinus Torvalds break; 11661da177e4SLinus Torvalds } 116779ed0226SAl Viro follow_mount(&nd->path); 116831e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 11691da177e4SLinus Torvalds } 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds /* 1172baa03890SNick Piggin * Allocate a dentry with name and parent, and perform a parent 1173baa03890SNick Piggin * directory ->lookup on it. Returns the new dentry, or ERR_PTR 1174baa03890SNick Piggin * on error. parent->d_inode->i_mutex must be held. d_lookup must 1175baa03890SNick Piggin * have verified that no child exists while under i_mutex. 1176baa03890SNick Piggin */ 1177baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent, 1178baa03890SNick Piggin struct qstr *name, struct nameidata *nd) 1179baa03890SNick Piggin { 1180baa03890SNick Piggin struct inode *inode = parent->d_inode; 1181baa03890SNick Piggin struct dentry *dentry; 1182baa03890SNick Piggin struct dentry *old; 1183baa03890SNick Piggin 1184baa03890SNick Piggin /* Don't create child dentry for a dead directory. */ 1185baa03890SNick Piggin if (unlikely(IS_DEADDIR(inode))) 1186baa03890SNick Piggin return ERR_PTR(-ENOENT); 1187baa03890SNick Piggin 1188baa03890SNick Piggin dentry = d_alloc(parent, name); 1189baa03890SNick Piggin if (unlikely(!dentry)) 1190baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1191baa03890SNick Piggin 1192baa03890SNick Piggin old = inode->i_op->lookup(inode, dentry, nd); 1193baa03890SNick Piggin if (unlikely(old)) { 1194baa03890SNick Piggin dput(dentry); 1195baa03890SNick Piggin dentry = old; 1196baa03890SNick Piggin } 1197baa03890SNick Piggin return dentry; 1198baa03890SNick Piggin } 1199baa03890SNick Piggin 1200baa03890SNick Piggin /* 12011da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 12021da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 12031da177e4SLinus Torvalds * It _is_ time-critical. 12041da177e4SLinus Torvalds */ 12051da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name, 120631e6b01fSNick Piggin struct path *path, struct inode **inode) 12071da177e4SLinus Torvalds { 12084ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 120931e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 12105a18fff2SAl Viro int need_reval = 1; 12115a18fff2SAl Viro int status = 1; 12129875cf80SDavid Howells int err; 12139875cf80SDavid Howells 12143cac260aSAl Viro /* 1215b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1216b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1217b04f784eSNick Piggin * do the non-racy lookup, below. 1218b04f784eSNick Piggin */ 121931e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 122031e6b01fSNick Piggin unsigned seq; 122131e6b01fSNick Piggin *inode = nd->inode; 122231e6b01fSNick Piggin dentry = __d_lookup_rcu(parent, name, &seq, inode); 12235a18fff2SAl Viro if (!dentry) 12245a18fff2SAl Viro goto unlazy; 12255a18fff2SAl Viro 122631e6b01fSNick Piggin /* Memory barrier in read_seqcount_begin of child is enough */ 122731e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 122831e6b01fSNick Piggin return -ECHILD; 122931e6b01fSNick Piggin nd->seq = seq; 12305a18fff2SAl Viro 123124643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 12325a18fff2SAl Viro status = d_revalidate(dentry, nd); 12335a18fff2SAl Viro if (unlikely(status <= 0)) { 12345a18fff2SAl Viro if (status != -ECHILD) 12355a18fff2SAl Viro need_reval = 0; 12365a18fff2SAl Viro goto unlazy; 12375a18fff2SAl Viro } 123824643087SAl Viro } 123931e6b01fSNick Piggin path->mnt = mnt; 124031e6b01fSNick Piggin path->dentry = dentry; 12419875cf80SDavid Howells if (likely(__follow_mount_rcu(nd, path, inode, false))) 12429875cf80SDavid Howells return 0; 12435a18fff2SAl Viro unlazy: 12445a18fff2SAl Viro if (dentry) { 12455a18fff2SAl Viro if (nameidata_dentry_drop_rcu(nd, dentry)) 12465a18fff2SAl Viro return -ECHILD; 12475a18fff2SAl Viro } else { 12489875cf80SDavid Howells if (nameidata_drop_rcu(nd)) 12499875cf80SDavid Howells return -ECHILD; 12509875cf80SDavid Howells } 12515a18fff2SAl Viro } else { 125231e6b01fSNick Piggin dentry = __d_lookup(parent, name); 125324643087SAl Viro } 12545a18fff2SAl Viro 12555a18fff2SAl Viro retry: 12565a18fff2SAl Viro if (unlikely(!dentry)) { 12575a18fff2SAl Viro struct inode *dir = parent->d_inode; 12585a18fff2SAl Viro BUG_ON(nd->inode != dir); 12595a18fff2SAl Viro 12605a18fff2SAl Viro mutex_lock(&dir->i_mutex); 12615a18fff2SAl Viro dentry = d_lookup(parent, name); 12625a18fff2SAl Viro if (likely(!dentry)) { 12635a18fff2SAl Viro dentry = d_alloc_and_lookup(parent, name, nd); 12645a18fff2SAl Viro if (IS_ERR(dentry)) { 12655a18fff2SAl Viro mutex_unlock(&dir->i_mutex); 12665a18fff2SAl Viro return PTR_ERR(dentry); 12675a18fff2SAl Viro } 12685a18fff2SAl Viro /* known good */ 12695a18fff2SAl Viro need_reval = 0; 12705a18fff2SAl Viro status = 1; 12715a18fff2SAl Viro } 12725a18fff2SAl Viro mutex_unlock(&dir->i_mutex); 12735a18fff2SAl Viro } 12745a18fff2SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) 12755a18fff2SAl Viro status = d_revalidate(dentry, nd); 12765a18fff2SAl Viro if (unlikely(status <= 0)) { 12775a18fff2SAl Viro if (status < 0) { 12785a18fff2SAl Viro dput(dentry); 12795a18fff2SAl Viro return status; 12805a18fff2SAl Viro } 12815a18fff2SAl Viro if (!d_invalidate(dentry)) { 12825a18fff2SAl Viro dput(dentry); 12835a18fff2SAl Viro dentry = NULL; 12845a18fff2SAl Viro need_reval = 1; 12855a18fff2SAl Viro goto retry; 12865a18fff2SAl Viro } 12875a18fff2SAl Viro } 12885a18fff2SAl Viro 12891da177e4SLinus Torvalds path->mnt = mnt; 12901da177e4SLinus Torvalds path->dentry = dentry; 12919875cf80SDavid Howells err = follow_managed(path, nd->flags); 129289312214SIan Kent if (unlikely(err < 0)) { 129389312214SIan Kent path_put_conditional(path, nd); 12949875cf80SDavid Howells return err; 129589312214SIan Kent } 129631e6b01fSNick Piggin *inode = path->dentry->d_inode; 12971da177e4SLinus Torvalds return 0; 12981da177e4SLinus Torvalds } 12991da177e4SLinus Torvalds 130052094c8aSAl Viro static inline int may_lookup(struct nameidata *nd) 130152094c8aSAl Viro { 130252094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 130352094c8aSAl Viro int err = exec_permission(nd->inode, IPERM_FLAG_RCU); 130452094c8aSAl Viro if (err != -ECHILD) 130552094c8aSAl Viro return err; 130652094c8aSAl Viro if (nameidata_drop_rcu(nd)) 130752094c8aSAl Viro return -ECHILD; 130852094c8aSAl Viro } 130952094c8aSAl Viro return exec_permission(nd->inode, 0); 131052094c8aSAl Viro } 131152094c8aSAl Viro 13129856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type) 13139856fa1bSAl Viro { 13149856fa1bSAl Viro if (type == LAST_DOTDOT) { 13159856fa1bSAl Viro if (nd->flags & LOOKUP_RCU) { 13169856fa1bSAl Viro if (follow_dotdot_rcu(nd)) 13179856fa1bSAl Viro return -ECHILD; 13189856fa1bSAl Viro } else 13199856fa1bSAl Viro follow_dotdot(nd); 13209856fa1bSAl Viro } 13219856fa1bSAl Viro return 0; 13229856fa1bSAl Viro } 13239856fa1bSAl Viro 1324951361f9SAl Viro static void terminate_walk(struct nameidata *nd) 1325951361f9SAl Viro { 1326951361f9SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1327951361f9SAl Viro path_put(&nd->path); 1328951361f9SAl Viro } else { 1329951361f9SAl Viro nd->flags &= ~LOOKUP_RCU; 13305b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1331951361f9SAl Viro nd->root.mnt = NULL; 1332951361f9SAl Viro rcu_read_unlock(); 1333951361f9SAl Viro br_read_unlock(vfsmount_lock); 1334951361f9SAl Viro } 1335951361f9SAl Viro } 1336951361f9SAl Viro 1337ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path, 1338ce57dfc1SAl Viro struct qstr *name, int type, int follow) 1339ce57dfc1SAl Viro { 1340ce57dfc1SAl Viro struct inode *inode; 1341ce57dfc1SAl Viro int err; 1342ce57dfc1SAl Viro /* 1343ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1344ce57dfc1SAl Viro * to be able to know about the current root directory and 1345ce57dfc1SAl Viro * parent relationships. 1346ce57dfc1SAl Viro */ 1347ce57dfc1SAl Viro if (unlikely(type != LAST_NORM)) 1348ce57dfc1SAl Viro return handle_dots(nd, type); 1349ce57dfc1SAl Viro err = do_lookup(nd, name, path, &inode); 1350ce57dfc1SAl Viro if (unlikely(err)) { 1351ce57dfc1SAl Viro terminate_walk(nd); 1352ce57dfc1SAl Viro return err; 1353ce57dfc1SAl Viro } 1354ce57dfc1SAl Viro if (!inode) { 1355ce57dfc1SAl Viro path_to_nameidata(path, nd); 1356ce57dfc1SAl Viro terminate_walk(nd); 1357ce57dfc1SAl Viro return -ENOENT; 1358ce57dfc1SAl Viro } 1359ce57dfc1SAl Viro if (unlikely(inode->i_op->follow_link) && follow) { 1360ce57dfc1SAl Viro if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry)) 1361ce57dfc1SAl Viro return -ECHILD; 1362ce57dfc1SAl Viro BUG_ON(inode != path->dentry->d_inode); 1363ce57dfc1SAl Viro return 1; 1364ce57dfc1SAl Viro } 1365ce57dfc1SAl Viro path_to_nameidata(path, nd); 1366ce57dfc1SAl Viro nd->inode = inode; 1367ce57dfc1SAl Viro return 0; 1368ce57dfc1SAl Viro } 1369ce57dfc1SAl Viro 13701da177e4SLinus Torvalds /* 1371b356379aSAl Viro * This limits recursive symlink follows to 8, while 1372b356379aSAl Viro * limiting consecutive symlinks to 40. 1373b356379aSAl Viro * 1374b356379aSAl Viro * Without that kind of total limit, nasty chains of consecutive 1375b356379aSAl Viro * symlinks can cause almost arbitrarily long lookups. 1376b356379aSAl Viro */ 1377b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd) 1378b356379aSAl Viro { 1379b356379aSAl Viro int res; 1380b356379aSAl Viro 1381b356379aSAl Viro BUG_ON(nd->depth >= MAX_NESTED_LINKS); 1382b356379aSAl Viro if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { 1383b356379aSAl Viro path_put_conditional(path, nd); 1384b356379aSAl Viro path_put(&nd->path); 1385b356379aSAl Viro return -ELOOP; 1386b356379aSAl Viro } 1387b356379aSAl Viro 1388b356379aSAl Viro nd->depth++; 1389b356379aSAl Viro current->link_count++; 1390b356379aSAl Viro 1391b356379aSAl Viro do { 1392b356379aSAl Viro struct path link = *path; 1393b356379aSAl Viro void *cookie; 1394574197e0SAl Viro 1395574197e0SAl Viro res = follow_link(&link, nd, &cookie); 1396b356379aSAl Viro if (!res) 1397b356379aSAl Viro res = walk_component(nd, path, &nd->last, 1398b356379aSAl Viro nd->last_type, LOOKUP_FOLLOW); 1399574197e0SAl Viro put_link(nd, &link, cookie); 1400b356379aSAl Viro } while (res > 0); 1401b356379aSAl Viro 1402b356379aSAl Viro current->link_count--; 1403b356379aSAl Viro nd->depth--; 1404b356379aSAl Viro return res; 1405b356379aSAl Viro } 1406b356379aSAl Viro 1407b356379aSAl Viro /* 14081da177e4SLinus Torvalds * Name resolution. 1409ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1410ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 14111da177e4SLinus Torvalds * 1412ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1413ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 14141da177e4SLinus Torvalds */ 14156de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 14161da177e4SLinus Torvalds { 14171da177e4SLinus Torvalds struct path next; 14181da177e4SLinus Torvalds int err; 14191da177e4SLinus Torvalds unsigned int lookup_flags = nd->flags; 14201da177e4SLinus Torvalds 14211da177e4SLinus Torvalds while (*name=='/') 14221da177e4SLinus Torvalds name++; 14231da177e4SLinus Torvalds if (!*name) 1424086e183aSAl Viro return 0; 14251da177e4SLinus Torvalds 14261da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 14271da177e4SLinus Torvalds for(;;) { 14281da177e4SLinus Torvalds unsigned long hash; 14291da177e4SLinus Torvalds struct qstr this; 14301da177e4SLinus Torvalds unsigned int c; 1431fe479a58SAl Viro int type; 14321da177e4SLinus Torvalds 1433cdce5d6bSTrond Myklebust nd->flags |= LOOKUP_CONTINUE; 143452094c8aSAl Viro 143552094c8aSAl Viro err = may_lookup(nd); 14361da177e4SLinus Torvalds if (err) 14371da177e4SLinus Torvalds break; 14381da177e4SLinus Torvalds 14391da177e4SLinus Torvalds this.name = name; 14401da177e4SLinus Torvalds c = *(const unsigned char *)name; 14411da177e4SLinus Torvalds 14421da177e4SLinus Torvalds hash = init_name_hash(); 14431da177e4SLinus Torvalds do { 14441da177e4SLinus Torvalds name++; 14451da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 14461da177e4SLinus Torvalds c = *(const unsigned char *)name; 14471da177e4SLinus Torvalds } while (c && (c != '/')); 14481da177e4SLinus Torvalds this.len = name - (const char *) this.name; 14491da177e4SLinus Torvalds this.hash = end_name_hash(hash); 14501da177e4SLinus Torvalds 1451fe479a58SAl Viro type = LAST_NORM; 1452fe479a58SAl Viro if (this.name[0] == '.') switch (this.len) { 1453fe479a58SAl Viro case 2: 145416c2cd71SAl Viro if (this.name[1] == '.') { 1455fe479a58SAl Viro type = LAST_DOTDOT; 145616c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 145716c2cd71SAl Viro } 1458fe479a58SAl Viro break; 1459fe479a58SAl Viro case 1: 1460fe479a58SAl Viro type = LAST_DOT; 1461fe479a58SAl Viro } 14625a202bcdSAl Viro if (likely(type == LAST_NORM)) { 14635a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 146416c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 14655a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 14665a202bcdSAl Viro err = parent->d_op->d_hash(parent, nd->inode, 14675a202bcdSAl Viro &this); 14685a202bcdSAl Viro if (err < 0) 14695a202bcdSAl Viro break; 14705a202bcdSAl Viro } 14715a202bcdSAl Viro } 1472fe479a58SAl Viro 14731da177e4SLinus Torvalds /* remove trailing slashes? */ 14741da177e4SLinus Torvalds if (!c) 14751da177e4SLinus Torvalds goto last_component; 14761da177e4SLinus Torvalds while (*++name == '/'); 14771da177e4SLinus Torvalds if (!*name) 1478b356379aSAl Viro goto last_component; 14791da177e4SLinus Torvalds 1480ce57dfc1SAl Viro err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW); 1481ce57dfc1SAl Viro if (err < 0) 1482ce57dfc1SAl Viro return err; 1483fe479a58SAl Viro 1484ce57dfc1SAl Viro if (err) { 1485b356379aSAl Viro err = nested_symlink(&next, nd); 14861da177e4SLinus Torvalds if (err) 1487a7472babSAl Viro return err; 148831e6b01fSNick Piggin } 14891da177e4SLinus Torvalds err = -ENOTDIR; 149031e6b01fSNick Piggin if (!nd->inode->i_op->lookup) 14911da177e4SLinus Torvalds break; 14921da177e4SLinus Torvalds continue; 14931da177e4SLinus Torvalds /* here ends the main loop */ 14941da177e4SLinus Torvalds 14951da177e4SLinus Torvalds last_component: 1496f55eab82STrond Myklebust /* Clear LOOKUP_CONTINUE iff it was previously unset */ 1497f55eab82STrond Myklebust nd->flags &= lookup_flags | ~LOOKUP_CONTINUE; 1498ce57dfc1SAl Viro nd->last = this; 1499ce57dfc1SAl Viro nd->last_type = type; 1500ce57dfc1SAl Viro return 0; 1501ce57dfc1SAl Viro } 1502951361f9SAl Viro terminate_walk(nd); 15031da177e4SLinus Torvalds return err; 15041da177e4SLinus Torvalds } 15051da177e4SLinus Torvalds 150670e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, 150770e9b357SAl Viro struct nameidata *nd, struct file **fp) 150831e6b01fSNick Piggin { 150931e6b01fSNick Piggin int retval = 0; 151031e6b01fSNick Piggin int fput_needed; 151131e6b01fSNick Piggin struct file *file; 151231e6b01fSNick Piggin 151331e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 151416c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 151531e6b01fSNick Piggin nd->depth = 0; 15165b6ca027SAl Viro if (flags & LOOKUP_ROOT) { 15175b6ca027SAl Viro struct inode *inode = nd->root.dentry->d_inode; 151873d049a4SAl Viro if (*name) { 15195b6ca027SAl Viro if (!inode->i_op->lookup) 15205b6ca027SAl Viro return -ENOTDIR; 15215b6ca027SAl Viro retval = inode_permission(inode, MAY_EXEC); 15225b6ca027SAl Viro if (retval) 15235b6ca027SAl Viro return retval; 152473d049a4SAl Viro } 15255b6ca027SAl Viro nd->path = nd->root; 15265b6ca027SAl Viro nd->inode = inode; 15275b6ca027SAl Viro if (flags & LOOKUP_RCU) { 15285b6ca027SAl Viro br_read_lock(vfsmount_lock); 15295b6ca027SAl Viro rcu_read_lock(); 15305b6ca027SAl Viro nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 15315b6ca027SAl Viro } else { 15325b6ca027SAl Viro path_get(&nd->path); 15335b6ca027SAl Viro } 15345b6ca027SAl Viro return 0; 15355b6ca027SAl Viro } 15365b6ca027SAl Viro 153731e6b01fSNick Piggin nd->root.mnt = NULL; 153831e6b01fSNick Piggin 153931e6b01fSNick Piggin if (*name=='/') { 1540e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 154131e6b01fSNick Piggin br_read_lock(vfsmount_lock); 154231e6b01fSNick Piggin rcu_read_lock(); 1543e41f7d4eSAl Viro set_root_rcu(nd); 1544e41f7d4eSAl Viro } else { 1545e41f7d4eSAl Viro set_root(nd); 1546e41f7d4eSAl Viro path_get(&nd->root); 1547e41f7d4eSAl Viro } 154831e6b01fSNick Piggin nd->path = nd->root; 154931e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 1550e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 155131e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1552c28cc364SNick Piggin unsigned seq; 155331e6b01fSNick Piggin 155431e6b01fSNick Piggin br_read_lock(vfsmount_lock); 155531e6b01fSNick Piggin rcu_read_lock(); 155631e6b01fSNick Piggin 1557c28cc364SNick Piggin do { 1558c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 155931e6b01fSNick Piggin nd->path = fs->pwd; 1560c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1561c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1562e41f7d4eSAl Viro } else { 1563e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 1564e41f7d4eSAl Viro } 156531e6b01fSNick Piggin } else { 156631e6b01fSNick Piggin struct dentry *dentry; 156731e6b01fSNick Piggin 15681abf0c71SAl Viro file = fget_raw_light(dfd, &fput_needed); 156931e6b01fSNick Piggin retval = -EBADF; 157031e6b01fSNick Piggin if (!file) 157131e6b01fSNick Piggin goto out_fail; 157231e6b01fSNick Piggin 157331e6b01fSNick Piggin dentry = file->f_path.dentry; 157431e6b01fSNick Piggin 1575f52e0c11SAl Viro if (*name) { 157631e6b01fSNick Piggin retval = -ENOTDIR; 157731e6b01fSNick Piggin if (!S_ISDIR(dentry->d_inode->i_mode)) 157831e6b01fSNick Piggin goto fput_fail; 157931e6b01fSNick Piggin 158031e6b01fSNick Piggin retval = file_permission(file, MAY_EXEC); 158131e6b01fSNick Piggin if (retval) 158231e6b01fSNick Piggin goto fput_fail; 1583f52e0c11SAl Viro } 158431e6b01fSNick Piggin 158531e6b01fSNick Piggin nd->path = file->f_path; 1586e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 158731e6b01fSNick Piggin if (fput_needed) 158870e9b357SAl Viro *fp = file; 1589c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 159031e6b01fSNick Piggin br_read_lock(vfsmount_lock); 159131e6b01fSNick Piggin rcu_read_lock(); 15925590ff0dSUlrich Drepper } else { 15935dd784d0SJan Blunck path_get(&file->f_path); 15945590ff0dSUlrich Drepper fput_light(file, fput_needed); 15951da177e4SLinus Torvalds } 1596e41f7d4eSAl Viro } 1597e41f7d4eSAl Viro 159831e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 15999b4a9b14SAl Viro return 0; 16002dfdd266SJosef 'Jeff' Sipek 16019b4a9b14SAl Viro fput_fail: 16029b4a9b14SAl Viro fput_light(file, fput_needed); 16039b4a9b14SAl Viro out_fail: 16049b4a9b14SAl Viro return retval; 16059b4a9b14SAl Viro } 16069b4a9b14SAl Viro 1607bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path) 1608bd92d7feSAl Viro { 1609bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 1610bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 1611bd92d7feSAl Viro 1612bd92d7feSAl Viro nd->flags &= ~LOOKUP_PARENT; 1613bd92d7feSAl Viro return walk_component(nd, path, &nd->last, nd->last_type, 1614bd92d7feSAl Viro nd->flags & LOOKUP_FOLLOW); 1615bd92d7feSAl Viro } 1616bd92d7feSAl Viro 16179b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1618ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name, 16199b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 16209b4a9b14SAl Viro { 162170e9b357SAl Viro struct file *base = NULL; 1622bd92d7feSAl Viro struct path path; 1623bd92d7feSAl Viro int err; 162431e6b01fSNick Piggin 162531e6b01fSNick Piggin /* 162631e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 162731e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 162831e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 162931e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 163031e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 163131e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 163231e6b01fSNick Piggin * analogue, foo_rcu(). 163331e6b01fSNick Piggin * 163431e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 163531e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 163631e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 163731e6b01fSNick Piggin * be able to complete). 163831e6b01fSNick Piggin */ 1639bd92d7feSAl Viro err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); 1640ee0827cdSAl Viro 1641bd92d7feSAl Viro if (unlikely(err)) 1642bd92d7feSAl Viro return err; 1643ee0827cdSAl Viro 1644ee0827cdSAl Viro current->total_link_count = 0; 1645bd92d7feSAl Viro err = link_path_walk(name, nd); 1646bd92d7feSAl Viro 1647bd92d7feSAl Viro if (!err && !(flags & LOOKUP_PARENT)) { 1648bd92d7feSAl Viro err = lookup_last(nd, &path); 1649bd92d7feSAl Viro while (err > 0) { 1650bd92d7feSAl Viro void *cookie; 1651bd92d7feSAl Viro struct path link = path; 1652bd92d7feSAl Viro nd->flags |= LOOKUP_PARENT; 1653574197e0SAl Viro err = follow_link(&link, nd, &cookie); 1654bd92d7feSAl Viro if (!err) 1655bd92d7feSAl Viro err = lookup_last(nd, &path); 1656574197e0SAl Viro put_link(nd, &link, cookie); 1657bd92d7feSAl Viro } 1658bd92d7feSAl Viro } 1659ee0827cdSAl Viro 1660ee0827cdSAl Viro if (nd->flags & LOOKUP_RCU) { 16614455ca62SAl Viro /* went all way through without dropping RCU */ 1662bd92d7feSAl Viro BUG_ON(err); 1663086e183aSAl Viro if (nameidata_drop_rcu_last(nd)) 1664bd92d7feSAl Viro err = -ECHILD; 1665086e183aSAl Viro } 166631e6b01fSNick Piggin 1667bd23a539SAl Viro if (!err) { 1668bd92d7feSAl Viro err = handle_reval_path(nd); 1669bd23a539SAl Viro if (err) 1670bd23a539SAl Viro path_put(&nd->path); 1671bd23a539SAl Viro } 1672bd92d7feSAl Viro 1673bd92d7feSAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) { 1674bd92d7feSAl Viro if (!nd->inode->i_op->lookup) { 1675bd92d7feSAl Viro path_put(&nd->path); 1676bd23a539SAl Viro err = -ENOTDIR; 1677bd92d7feSAl Viro } 1678bd92d7feSAl Viro } 167916c2cd71SAl Viro 168070e9b357SAl Viro if (base) 168170e9b357SAl Viro fput(base); 1682ee0827cdSAl Viro 16835b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 168431e6b01fSNick Piggin path_put(&nd->root); 168531e6b01fSNick Piggin nd->root.mnt = NULL; 168631e6b01fSNick Piggin } 1687bd92d7feSAl Viro return err; 168831e6b01fSNick Piggin } 168931e6b01fSNick Piggin 1690ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name, 1691ee0827cdSAl Viro unsigned int flags, struct nameidata *nd) 1692ee0827cdSAl Viro { 1693ee0827cdSAl Viro int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd); 1694ee0827cdSAl Viro if (unlikely(retval == -ECHILD)) 1695ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags, nd); 1696ee0827cdSAl Viro if (unlikely(retval == -ESTALE)) 1697ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd); 1698ee0827cdSAl Viro 169931e6b01fSNick Piggin if (likely(!retval)) { 170031e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) { 170131e6b01fSNick Piggin if (nd->path.dentry && nd->inode) 170231e6b01fSNick Piggin audit_inode(name, nd->path.dentry); 170331e6b01fSNick Piggin } 170431e6b01fSNick Piggin } 1705170aa3d0SUlrich Drepper return retval; 17061da177e4SLinus Torvalds } 17071da177e4SLinus Torvalds 1708c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd) 17095590ff0dSUlrich Drepper { 1710c9c6cac0SAl Viro return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd); 17115590ff0dSUlrich Drepper } 17125590ff0dSUlrich Drepper 1713d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1714d1811465SAl Viro { 1715d1811465SAl Viro struct nameidata nd; 1716d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1717d1811465SAl Viro if (!res) 1718d1811465SAl Viro *path = nd.path; 1719d1811465SAl Viro return res; 1720d1811465SAl Viro } 1721d1811465SAl Viro 172216f18200SJosef 'Jeff' Sipek /** 172316f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 172416f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 172516f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 172616f18200SJosef 'Jeff' Sipek * @name: pointer to file name 172716f18200SJosef 'Jeff' Sipek * @flags: lookup flags 172816f18200SJosef 'Jeff' Sipek * @nd: pointer to nameidata 172916f18200SJosef 'Jeff' Sipek */ 173016f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 173116f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 173216f18200SJosef 'Jeff' Sipek struct nameidata *nd) 173316f18200SJosef 'Jeff' Sipek { 17345b6ca027SAl Viro nd->root.dentry = dentry; 17355b6ca027SAl Viro nd->root.mnt = mnt; 17365b6ca027SAl Viro /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ 17375b6ca027SAl Viro return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd); 173816f18200SJosef 'Jeff' Sipek } 173916f18200SJosef 'Jeff' Sipek 1740eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name, 1741eead1911SChristoph Hellwig struct dentry *base, struct nameidata *nd) 17421da177e4SLinus Torvalds { 174381fca444SChristoph Hellwig struct inode *inode = base->d_inode; 17441da177e4SLinus Torvalds struct dentry *dentry; 17451da177e4SLinus Torvalds int err; 17461da177e4SLinus Torvalds 1747b74c79e9SNick Piggin err = exec_permission(inode, 0); 174881fca444SChristoph Hellwig if (err) 174981fca444SChristoph Hellwig return ERR_PTR(err); 17501da177e4SLinus Torvalds 17511da177e4SLinus Torvalds /* 1752b04f784eSNick Piggin * Don't bother with __d_lookup: callers are for creat as 1753b04f784eSNick Piggin * well as unlink, so a lot of the time it would cost 1754b04f784eSNick Piggin * a double lookup. 17556e6b1bd1SAl Viro */ 17566e6b1bd1SAl Viro dentry = d_lookup(base, name); 17576e6b1bd1SAl Viro 1758fb045adbSNick Piggin if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) 17596e6b1bd1SAl Viro dentry = do_revalidate(dentry, nd); 17606e6b1bd1SAl Viro 17611da177e4SLinus Torvalds if (!dentry) 1762baa03890SNick Piggin dentry = d_alloc_and_lookup(base, name, nd); 17635a202bcdSAl Viro 17641da177e4SLinus Torvalds return dentry; 17651da177e4SLinus Torvalds } 17661da177e4SLinus Torvalds 1767057f6c01SJames Morris /* 1768057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1769057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1770057f6c01SJames Morris * SMP-safe. 1771057f6c01SJames Morris */ 1772a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 17731da177e4SLinus Torvalds { 17744ac91378SJan Blunck return __lookup_hash(&nd->last, nd->path.dentry, nd); 17751da177e4SLinus Torvalds } 17761da177e4SLinus Torvalds 1777eead1911SChristoph Hellwig /** 1778a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1779eead1911SChristoph Hellwig * @name: pathname component to lookup 1780eead1911SChristoph Hellwig * @base: base directory to lookup from 1781eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1782eead1911SChristoph Hellwig * 1783a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1784a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1785eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1786eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1787eead1911SChristoph Hellwig */ 1788057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1789057f6c01SJames Morris { 1790057f6c01SJames Morris struct qstr this; 17916a96ba54SAl Viro unsigned long hash; 17926a96ba54SAl Viro unsigned int c; 1793057f6c01SJames Morris 17942f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 17952f9092e1SDavid Woodhouse 17966a96ba54SAl Viro this.name = name; 17976a96ba54SAl Viro this.len = len; 17986a96ba54SAl Viro if (!len) 17996a96ba54SAl Viro return ERR_PTR(-EACCES); 18006a96ba54SAl Viro 18016a96ba54SAl Viro hash = init_name_hash(); 18026a96ba54SAl Viro while (len--) { 18036a96ba54SAl Viro c = *(const unsigned char *)name++; 18046a96ba54SAl Viro if (c == '/' || c == '\0') 18056a96ba54SAl Viro return ERR_PTR(-EACCES); 18066a96ba54SAl Viro hash = partial_name_hash(c, hash); 18076a96ba54SAl Viro } 18086a96ba54SAl Viro this.hash = end_name_hash(hash); 18095a202bcdSAl Viro /* 18105a202bcdSAl Viro * See if the low-level filesystem might want 18115a202bcdSAl Viro * to use its own hash.. 18125a202bcdSAl Viro */ 18135a202bcdSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 18145a202bcdSAl Viro int err = base->d_op->d_hash(base, base->d_inode, &this); 18155a202bcdSAl Viro if (err < 0) 18165a202bcdSAl Viro return ERR_PTR(err); 18175a202bcdSAl Viro } 1818eead1911SChristoph Hellwig 181949705b77SChristoph Hellwig return __lookup_hash(&this, base, NULL); 1820057f6c01SJames Morris } 1821057f6c01SJames Morris 18222d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags, 18232d8f3038SAl Viro struct path *path) 18241da177e4SLinus Torvalds { 18252d8f3038SAl Viro struct nameidata nd; 1826f52e0c11SAl Viro char *tmp = getname_flags(name, flags); 18271da177e4SLinus Torvalds int err = PTR_ERR(tmp); 18281da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 18292d8f3038SAl Viro 18302d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 18312d8f3038SAl Viro 18322d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 18331da177e4SLinus Torvalds putname(tmp); 18342d8f3038SAl Viro if (!err) 18352d8f3038SAl Viro *path = nd.path; 18361da177e4SLinus Torvalds } 18371da177e4SLinus Torvalds return err; 18381da177e4SLinus Torvalds } 18391da177e4SLinus Torvalds 18402ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 18412ad94ae6SAl Viro struct nameidata *nd, char **name) 18422ad94ae6SAl Viro { 18432ad94ae6SAl Viro char *s = getname(path); 18442ad94ae6SAl Viro int error; 18452ad94ae6SAl Viro 18462ad94ae6SAl Viro if (IS_ERR(s)) 18472ad94ae6SAl Viro return PTR_ERR(s); 18482ad94ae6SAl Viro 18492ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 18502ad94ae6SAl Viro if (error) 18512ad94ae6SAl Viro putname(s); 18522ad94ae6SAl Viro else 18532ad94ae6SAl Viro *name = s; 18542ad94ae6SAl Viro 18552ad94ae6SAl Viro return error; 18562ad94ae6SAl Viro } 18572ad94ae6SAl Viro 18581da177e4SLinus Torvalds /* 18591da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 18601da177e4SLinus Torvalds * minimal. 18611da177e4SLinus Torvalds */ 18621da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 18631da177e4SLinus Torvalds { 1864da9592edSDavid Howells uid_t fsuid = current_fsuid(); 1865da9592edSDavid Howells 18661da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 18671da177e4SLinus Torvalds return 0; 1868e795b717SSerge E. Hallyn if (current_user_ns() != inode_userns(inode)) 1869e795b717SSerge E. Hallyn goto other_userns; 1870da9592edSDavid Howells if (inode->i_uid == fsuid) 18711da177e4SLinus Torvalds return 0; 1872da9592edSDavid Howells if (dir->i_uid == fsuid) 18731da177e4SLinus Torvalds return 0; 1874e795b717SSerge E. Hallyn 1875e795b717SSerge E. Hallyn other_userns: 1876e795b717SSerge E. Hallyn return !ns_capable(inode_userns(inode), CAP_FOWNER); 18771da177e4SLinus Torvalds } 18781da177e4SLinus Torvalds 18791da177e4SLinus Torvalds /* 18801da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 18811da177e4SLinus Torvalds * whether the type of victim is right. 18821da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 18831da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 18841da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 18851da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 18861da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 18871da177e4SLinus Torvalds * a. be owner of dir, or 18881da177e4SLinus Torvalds * b. be owner of victim, or 18891da177e4SLinus Torvalds * c. have CAP_FOWNER capability 18901da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 18911da177e4SLinus Torvalds * links pointing to it. 18921da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 18931da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 18941da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 18951da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 18961da177e4SLinus Torvalds * nfs_async_unlink(). 18971da177e4SLinus Torvalds */ 1898858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 18991da177e4SLinus Torvalds { 19001da177e4SLinus Torvalds int error; 19011da177e4SLinus Torvalds 19021da177e4SLinus Torvalds if (!victim->d_inode) 19031da177e4SLinus Torvalds return -ENOENT; 19041da177e4SLinus Torvalds 19051da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 1906cccc6bbaSAl Viro audit_inode_child(victim, dir); 19071da177e4SLinus Torvalds 1908f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 19091da177e4SLinus Torvalds if (error) 19101da177e4SLinus Torvalds return error; 19111da177e4SLinus Torvalds if (IS_APPEND(dir)) 19121da177e4SLinus Torvalds return -EPERM; 19131da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 1914f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 19151da177e4SLinus Torvalds return -EPERM; 19161da177e4SLinus Torvalds if (isdir) { 19171da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 19181da177e4SLinus Torvalds return -ENOTDIR; 19191da177e4SLinus Torvalds if (IS_ROOT(victim)) 19201da177e4SLinus Torvalds return -EBUSY; 19211da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 19221da177e4SLinus Torvalds return -EISDIR; 19231da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 19241da177e4SLinus Torvalds return -ENOENT; 19251da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 19261da177e4SLinus Torvalds return -EBUSY; 19271da177e4SLinus Torvalds return 0; 19281da177e4SLinus Torvalds } 19291da177e4SLinus Torvalds 19301da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 19311da177e4SLinus Torvalds * dir. 19321da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 19331da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 19341da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 19351da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 19361da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 19371da177e4SLinus Torvalds */ 1938a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 19391da177e4SLinus Torvalds { 19401da177e4SLinus Torvalds if (child->d_inode) 19411da177e4SLinus Torvalds return -EEXIST; 19421da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 19431da177e4SLinus Torvalds return -ENOENT; 1944f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 19451da177e4SLinus Torvalds } 19461da177e4SLinus Torvalds 19471da177e4SLinus Torvalds /* 19481da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 19491da177e4SLinus Torvalds */ 19501da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 19511da177e4SLinus Torvalds { 19521da177e4SLinus Torvalds struct dentry *p; 19531da177e4SLinus Torvalds 19541da177e4SLinus Torvalds if (p1 == p2) { 1955f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 19561da177e4SLinus Torvalds return NULL; 19571da177e4SLinus Torvalds } 19581da177e4SLinus Torvalds 1959a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 19601da177e4SLinus Torvalds 1961e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 1962e2761a11SOGAWA Hirofumi if (p) { 1963f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 1964f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 19651da177e4SLinus Torvalds return p; 19661da177e4SLinus Torvalds } 19671da177e4SLinus Torvalds 1968e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 1969e2761a11SOGAWA Hirofumi if (p) { 1970f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1971f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 19721da177e4SLinus Torvalds return p; 19731da177e4SLinus Torvalds } 19741da177e4SLinus Torvalds 1975f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1976f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 19771da177e4SLinus Torvalds return NULL; 19781da177e4SLinus Torvalds } 19791da177e4SLinus Torvalds 19801da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 19811da177e4SLinus Torvalds { 19821b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 19831da177e4SLinus Torvalds if (p1 != p2) { 19841b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 1985a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 19861da177e4SLinus Torvalds } 19871da177e4SLinus Torvalds } 19881da177e4SLinus Torvalds 19891da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode, 19901da177e4SLinus Torvalds struct nameidata *nd) 19911da177e4SLinus Torvalds { 1992a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 19931da177e4SLinus Torvalds 19941da177e4SLinus Torvalds if (error) 19951da177e4SLinus Torvalds return error; 19961da177e4SLinus Torvalds 1997acfa4380SAl Viro if (!dir->i_op->create) 19981da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 19991da177e4SLinus Torvalds mode &= S_IALLUGO; 20001da177e4SLinus Torvalds mode |= S_IFREG; 20011da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 20021da177e4SLinus Torvalds if (error) 20031da177e4SLinus Torvalds return error; 20041da177e4SLinus Torvalds error = dir->i_op->create(dir, dentry, mode, nd); 2005a74574aaSStephen Smalley if (!error) 2006f38aa942SAmy Griffis fsnotify_create(dir, dentry); 20071da177e4SLinus Torvalds return error; 20081da177e4SLinus Torvalds } 20091da177e4SLinus Torvalds 201073d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag) 20111da177e4SLinus Torvalds { 20123fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 20131da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 20141da177e4SLinus Torvalds int error; 20151da177e4SLinus Torvalds 2016bcda7652SAl Viro /* O_PATH? */ 2017bcda7652SAl Viro if (!acc_mode) 2018bcda7652SAl Viro return 0; 2019bcda7652SAl Viro 20201da177e4SLinus Torvalds if (!inode) 20211da177e4SLinus Torvalds return -ENOENT; 20221da177e4SLinus Torvalds 2023c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2024c8fe8f30SChristoph Hellwig case S_IFLNK: 20251da177e4SLinus Torvalds return -ELOOP; 2026c8fe8f30SChristoph Hellwig case S_IFDIR: 2027c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 20281da177e4SLinus Torvalds return -EISDIR; 2029c8fe8f30SChristoph Hellwig break; 2030c8fe8f30SChristoph Hellwig case S_IFBLK: 2031c8fe8f30SChristoph Hellwig case S_IFCHR: 20323fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 20331da177e4SLinus Torvalds return -EACCES; 2034c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 2035c8fe8f30SChristoph Hellwig case S_IFIFO: 2036c8fe8f30SChristoph Hellwig case S_IFSOCK: 20371da177e4SLinus Torvalds flag &= ~O_TRUNC; 2038c8fe8f30SChristoph Hellwig break; 20394a3fd211SDave Hansen } 2040b41572e9SDave Hansen 20413fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 2042b41572e9SDave Hansen if (error) 2043b41572e9SDave Hansen return error; 20446146f0d5SMimi Zohar 20451da177e4SLinus Torvalds /* 20461da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 20471da177e4SLinus Torvalds */ 20481da177e4SLinus Torvalds if (IS_APPEND(inode)) { 20498737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 20507715b521SAl Viro return -EPERM; 20511da177e4SLinus Torvalds if (flag & O_TRUNC) 20527715b521SAl Viro return -EPERM; 20531da177e4SLinus Torvalds } 20541da177e4SLinus Torvalds 20551da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 20562e149670SSerge E. Hallyn if (flag & O_NOATIME && !inode_owner_or_capable(inode)) 20577715b521SAl Viro return -EPERM; 20581da177e4SLinus Torvalds 20591da177e4SLinus Torvalds /* 20601da177e4SLinus Torvalds * Ensure there are no outstanding leases on the file. 20611da177e4SLinus Torvalds */ 2062b65a9cfcSAl Viro return break_lease(inode, flag); 20637715b521SAl Viro } 20647715b521SAl Viro 2065e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 20667715b521SAl Viro { 2067e1181ee6SJeff Layton struct path *path = &filp->f_path; 20687715b521SAl Viro struct inode *inode = path->dentry->d_inode; 20697715b521SAl Viro int error = get_write_access(inode); 20701da177e4SLinus Torvalds if (error) 20717715b521SAl Viro return error; 20721da177e4SLinus Torvalds /* 20731da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 20741da177e4SLinus Torvalds */ 20751da177e4SLinus Torvalds error = locks_verify_locked(inode); 2076be6d3e56SKentaro Takeda if (!error) 2077ea0d3ab2STetsuo Handa error = security_path_truncate(path); 20781da177e4SLinus Torvalds if (!error) { 20797715b521SAl Viro error = do_truncate(path->dentry, 0, 2080d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 2081e1181ee6SJeff Layton filp); 20821da177e4SLinus Torvalds } 20831da177e4SLinus Torvalds put_write_access(inode); 2084acd0c935SMimi Zohar return error; 20851da177e4SLinus Torvalds } 20861da177e4SLinus Torvalds 2087d57999e1SDave Hansen /* 2088d57999e1SDave Hansen * Note that while the flag value (low two bits) for sys_open means: 2089d57999e1SDave Hansen * 00 - read-only 2090d57999e1SDave Hansen * 01 - write-only 2091d57999e1SDave Hansen * 10 - read-write 2092d57999e1SDave Hansen * 11 - special 2093d57999e1SDave Hansen * it is changed into 2094d57999e1SDave Hansen * 00 - no permissions needed 2095d57999e1SDave Hansen * 01 - read-permission 2096d57999e1SDave Hansen * 10 - write-permission 2097d57999e1SDave Hansen * 11 - read-write 2098d57999e1SDave Hansen * for the internal routines (ie open_namei()/follow_link() etc) 2099d57999e1SDave Hansen * This is more logical, and also allows the 00 "no perm needed" 2100d57999e1SDave Hansen * to be used for symlinks (where the permissions are checked 2101d57999e1SDave Hansen * later). 2102d57999e1SDave Hansen * 2103d57999e1SDave Hansen */ 2104d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2105d57999e1SDave Hansen { 2106d57999e1SDave Hansen if ((flag+1) & O_ACCMODE) 2107d57999e1SDave Hansen flag++; 2108d57999e1SDave Hansen return flag; 2109d57999e1SDave Hansen } 2110d57999e1SDave Hansen 211131e6b01fSNick Piggin /* 2112fe2d35ffSAl Viro * Handle the last step of open() 211331e6b01fSNick Piggin */ 2114fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path, 2115c3e380b0SAl Viro const struct open_flags *op, const char *pathname) 2116fb1cc555SAl Viro { 2117a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 21186c0d46c4SAl Viro struct dentry *dentry; 2119ca344a89SAl Viro int open_flag = op->open_flag; 21206c0d46c4SAl Viro int will_truncate = open_flag & O_TRUNC; 2121ca344a89SAl Viro int want_write = 0; 2122bcda7652SAl Viro int acc_mode = op->acc_mode; 2123fb1cc555SAl Viro struct file *filp; 212416c2cd71SAl Viro int error; 2125fb1cc555SAl Viro 2126c3e380b0SAl Viro nd->flags &= ~LOOKUP_PARENT; 2127c3e380b0SAl Viro nd->flags |= op->intent; 2128c3e380b0SAl Viro 21291f36f774SAl Viro switch (nd->last_type) { 21301f36f774SAl Viro case LAST_DOTDOT: 2131176306f5SNeil Brown case LAST_DOT: 2132fe2d35ffSAl Viro error = handle_dots(nd, nd->last_type); 2133fe2d35ffSAl Viro if (error) 2134fe2d35ffSAl Viro return ERR_PTR(error); 21351f36f774SAl Viro /* fallthrough */ 21361f36f774SAl Viro case LAST_ROOT: 2137fe2d35ffSAl Viro if (nd->flags & LOOKUP_RCU) { 2138fe2d35ffSAl Viro if (nameidata_drop_rcu_last(nd)) 2139fe2d35ffSAl Viro return ERR_PTR(-ECHILD); 2140fe2d35ffSAl Viro } 214116c2cd71SAl Viro error = handle_reval_path(nd); 214216c2cd71SAl Viro if (error) 214316c2cd71SAl Viro goto exit; 2144fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2145ca344a89SAl Viro if (open_flag & O_CREAT) { 214616c2cd71SAl Viro error = -EISDIR; 21471f36f774SAl Viro goto exit; 2148fe2d35ffSAl Viro } 2149fe2d35ffSAl Viro goto ok; 21501f36f774SAl Viro case LAST_BIND: 2151fe2d35ffSAl Viro /* can't be RCU mode here */ 215216c2cd71SAl Viro error = handle_reval_path(nd); 215316c2cd71SAl Viro if (error) 215416c2cd71SAl Viro goto exit; 21551f36f774SAl Viro audit_inode(pathname, dir); 21561f36f774SAl Viro goto ok; 21571f36f774SAl Viro } 2158a2c36b45SAl Viro 2159ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 2160bcda7652SAl Viro int symlink_ok = 0; 2161fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 2162fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2163bcda7652SAl Viro if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) 2164bcda7652SAl Viro symlink_ok = 1; 2165fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 2166ce57dfc1SAl Viro error = walk_component(nd, path, &nd->last, LAST_NORM, 2167ce57dfc1SAl Viro !symlink_ok); 2168ce57dfc1SAl Viro if (error < 0) 2169fe2d35ffSAl Viro return ERR_PTR(error); 2170ce57dfc1SAl Viro if (error) /* symlink */ 2171fe2d35ffSAl Viro return NULL; 2172fe2d35ffSAl Viro /* sayonara */ 2173fe2d35ffSAl Viro if (nd->flags & LOOKUP_RCU) { 2174fe2d35ffSAl Viro if (nameidata_drop_rcu_last(nd)) 2175fe2d35ffSAl Viro return ERR_PTR(-ECHILD); 2176fe2d35ffSAl Viro } 2177fe2d35ffSAl Viro 2178fe2d35ffSAl Viro error = -ENOTDIR; 2179fe2d35ffSAl Viro if (nd->flags & LOOKUP_DIRECTORY) { 2180ce57dfc1SAl Viro if (!nd->inode->i_op->lookup) 2181fe2d35ffSAl Viro goto exit; 2182fe2d35ffSAl Viro } 2183fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2184fe2d35ffSAl Viro goto ok; 2185fe2d35ffSAl Viro } 2186fe2d35ffSAl Viro 2187fe2d35ffSAl Viro /* create side of things */ 2188fe2d35ffSAl Viro 2189fe2d35ffSAl Viro if (nd->flags & LOOKUP_RCU) { 2190fe2d35ffSAl Viro if (nameidata_drop_rcu_last(nd)) 2191fe2d35ffSAl Viro return ERR_PTR(-ECHILD); 2192fe2d35ffSAl Viro } 2193fe2d35ffSAl Viro 2194fe2d35ffSAl Viro audit_inode(pathname, dir); 219516c2cd71SAl Viro error = -EISDIR; 21961f36f774SAl Viro /* trailing slashes? */ 219731e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 21981f36f774SAl Viro goto exit; 21991f36f774SAl Viro 2200a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 2201a1e28038SAl Viro 22026c0d46c4SAl Viro dentry = lookup_hash(nd); 22036c0d46c4SAl Viro error = PTR_ERR(dentry); 22046c0d46c4SAl Viro if (IS_ERR(dentry)) { 2205fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2206fb1cc555SAl Viro goto exit; 2207fb1cc555SAl Viro } 2208fb1cc555SAl Viro 22096c0d46c4SAl Viro path->dentry = dentry; 22106c0d46c4SAl Viro path->mnt = nd->path.mnt; 22116c0d46c4SAl Viro 2212fb1cc555SAl Viro /* Negative dentry, just create the file */ 22136c0d46c4SAl Viro if (!dentry->d_inode) { 22146c0d46c4SAl Viro int mode = op->mode; 22156c0d46c4SAl Viro if (!IS_POSIXACL(dir->d_inode)) 22166c0d46c4SAl Viro mode &= ~current_umask(); 2217fb1cc555SAl Viro /* 2218fb1cc555SAl Viro * This write is needed to ensure that a 22196c0d46c4SAl Viro * rw->ro transition does not occur between 2220fb1cc555SAl Viro * the time when the file is created and when 2221fb1cc555SAl Viro * a permanent write count is taken through 2222fb1cc555SAl Viro * the 'struct file' in nameidata_to_filp(). 2223fb1cc555SAl Viro */ 2224fb1cc555SAl Viro error = mnt_want_write(nd->path.mnt); 2225fb1cc555SAl Viro if (error) 2226fb1cc555SAl Viro goto exit_mutex_unlock; 2227ca344a89SAl Viro want_write = 1; 22289b44f1b3SAl Viro /* Don't check for write permission, don't truncate */ 2229ca344a89SAl Viro open_flag &= ~O_TRUNC; 22306c0d46c4SAl Viro will_truncate = 0; 2231bcda7652SAl Viro acc_mode = MAY_OPEN; 22326c0d46c4SAl Viro error = security_path_mknod(&nd->path, dentry, mode, 0); 22336c0d46c4SAl Viro if (error) 22346c0d46c4SAl Viro goto exit_mutex_unlock; 22356c0d46c4SAl Viro error = vfs_create(dir->d_inode, dentry, mode, nd); 22366c0d46c4SAl Viro if (error) 22376c0d46c4SAl Viro goto exit_mutex_unlock; 22386c0d46c4SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 22396c0d46c4SAl Viro dput(nd->path.dentry); 22406c0d46c4SAl Viro nd->path.dentry = dentry; 2241ca344a89SAl Viro goto common; 2242fb1cc555SAl Viro } 2243fb1cc555SAl Viro 2244fb1cc555SAl Viro /* 2245fb1cc555SAl Viro * It already exists. 2246fb1cc555SAl Viro */ 2247fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2248fb1cc555SAl Viro audit_inode(pathname, path->dentry); 2249fb1cc555SAl Viro 2250fb1cc555SAl Viro error = -EEXIST; 2251ca344a89SAl Viro if (open_flag & O_EXCL) 2252fb1cc555SAl Viro goto exit_dput; 2253fb1cc555SAl Viro 22549875cf80SDavid Howells error = follow_managed(path, nd->flags); 22559875cf80SDavid Howells if (error < 0) 2256fb1cc555SAl Viro goto exit_dput; 2257fb1cc555SAl Viro 2258fb1cc555SAl Viro error = -ENOENT; 2259fb1cc555SAl Viro if (!path->dentry->d_inode) 2260fb1cc555SAl Viro goto exit_dput; 22619e67f361SAl Viro 22629e67f361SAl Viro if (path->dentry->d_inode->i_op->follow_link) 2263fb1cc555SAl Viro return NULL; 2264fb1cc555SAl Viro 2265fb1cc555SAl Viro path_to_nameidata(path, nd); 226631e6b01fSNick Piggin nd->inode = path->dentry->d_inode; 2267fb1cc555SAl Viro error = -EISDIR; 226831e6b01fSNick Piggin if (S_ISDIR(nd->inode->i_mode)) 2269fb1cc555SAl Viro goto exit; 227067ee3ad2SAl Viro ok: 22716c0d46c4SAl Viro if (!S_ISREG(nd->inode->i_mode)) 22726c0d46c4SAl Viro will_truncate = 0; 22736c0d46c4SAl Viro 22740f9d1a10SAl Viro if (will_truncate) { 22750f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 22760f9d1a10SAl Viro if (error) 22770f9d1a10SAl Viro goto exit; 2278ca344a89SAl Viro want_write = 1; 22790f9d1a10SAl Viro } 2280ca344a89SAl Viro common: 2281bcda7652SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2282ca344a89SAl Viro if (error) 22830f9d1a10SAl Viro goto exit; 22840f9d1a10SAl Viro filp = nameidata_to_filp(nd); 22850f9d1a10SAl Viro if (!IS_ERR(filp)) { 22860f9d1a10SAl Viro error = ima_file_check(filp, op->acc_mode); 22870f9d1a10SAl Viro if (error) { 22880f9d1a10SAl Viro fput(filp); 22890f9d1a10SAl Viro filp = ERR_PTR(error); 22900f9d1a10SAl Viro } 22910f9d1a10SAl Viro } 22920f9d1a10SAl Viro if (!IS_ERR(filp)) { 22930f9d1a10SAl Viro if (will_truncate) { 22940f9d1a10SAl Viro error = handle_truncate(filp); 22950f9d1a10SAl Viro if (error) { 22960f9d1a10SAl Viro fput(filp); 22970f9d1a10SAl Viro filp = ERR_PTR(error); 22980f9d1a10SAl Viro } 22990f9d1a10SAl Viro } 23000f9d1a10SAl Viro } 2301ca344a89SAl Viro out: 2302ca344a89SAl Viro if (want_write) 23030f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 23040f9d1a10SAl Viro path_put(&nd->path); 2305fb1cc555SAl Viro return filp; 2306fb1cc555SAl Viro 2307fb1cc555SAl Viro exit_mutex_unlock: 2308fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2309fb1cc555SAl Viro exit_dput: 2310fb1cc555SAl Viro path_put_conditional(path, nd); 2311fb1cc555SAl Viro exit: 2312ca344a89SAl Viro filp = ERR_PTR(error); 2313ca344a89SAl Viro goto out; 2314fb1cc555SAl Viro } 2315fb1cc555SAl Viro 231613aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname, 231773d049a4SAl Viro struct nameidata *nd, const struct open_flags *op, int flags) 23181da177e4SLinus Torvalds { 2319fe2d35ffSAl Viro struct file *base = NULL; 23204a3fd211SDave Hansen struct file *filp; 23219850c056SAl Viro struct path path; 232213aab428SAl Viro int error; 232331e6b01fSNick Piggin 232431e6b01fSNick Piggin filp = get_empty_filp(); 232531e6b01fSNick Piggin if (!filp) 232631e6b01fSNick Piggin return ERR_PTR(-ENFILE); 232731e6b01fSNick Piggin 232847c805dcSAl Viro filp->f_flags = op->open_flag; 232973d049a4SAl Viro nd->intent.open.file = filp; 233073d049a4SAl Viro nd->intent.open.flags = open_to_namei_flags(op->open_flag); 233173d049a4SAl Viro nd->intent.open.create_mode = op->mode; 233231e6b01fSNick Piggin 233373d049a4SAl Viro error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base); 233431e6b01fSNick Piggin if (unlikely(error)) 233513aab428SAl Viro goto out_filp; 233631e6b01fSNick Piggin 2337fe2d35ffSAl Viro current->total_link_count = 0; 233873d049a4SAl Viro error = link_path_walk(pathname, nd); 233931e6b01fSNick Piggin if (unlikely(error)) 234031e6b01fSNick Piggin goto out_filp; 23411da177e4SLinus Torvalds 234273d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2343806b681cSAl Viro while (unlikely(!filp)) { /* trailing symlink */ 23447b9337aaSNick Piggin struct path link = path; 2345def4af30SAl Viro void *cookie; 2346574197e0SAl Viro if (!(nd->flags & LOOKUP_FOLLOW)) { 234773d049a4SAl Viro path_put_conditional(&path, nd); 234873d049a4SAl Viro path_put(&nd->path); 234940b39136SAl Viro filp = ERR_PTR(-ELOOP); 235040b39136SAl Viro break; 235140b39136SAl Viro } 235273d049a4SAl Viro nd->flags |= LOOKUP_PARENT; 235373d049a4SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 2354574197e0SAl Viro error = follow_link(&link, nd, &cookie); 2355c3e380b0SAl Viro if (unlikely(error)) 2356f1afe9efSAl Viro filp = ERR_PTR(error); 2357c3e380b0SAl Viro else 235873d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2359574197e0SAl Viro put_link(nd, &link, cookie); 2360806b681cSAl Viro } 236110fa8e62SAl Viro out: 236273d049a4SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) 236373d049a4SAl Viro path_put(&nd->root); 2364fe2d35ffSAl Viro if (base) 2365fe2d35ffSAl Viro fput(base); 236673d049a4SAl Viro release_open_intent(nd); 236710fa8e62SAl Viro return filp; 23681da177e4SLinus Torvalds 236931e6b01fSNick Piggin out_filp: 237010fa8e62SAl Viro filp = ERR_PTR(error); 237110fa8e62SAl Viro goto out; 2372de459215SKirill Korotaev } 23731da177e4SLinus Torvalds 237413aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname, 237513aab428SAl Viro const struct open_flags *op, int flags) 237613aab428SAl Viro { 237773d049a4SAl Viro struct nameidata nd; 237813aab428SAl Viro struct file *filp; 237913aab428SAl Viro 238073d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); 238113aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 238273d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags); 238313aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 238473d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); 238513aab428SAl Viro return filp; 238613aab428SAl Viro } 238713aab428SAl Viro 238873d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, 238973d049a4SAl Viro const char *name, const struct open_flags *op, int flags) 239073d049a4SAl Viro { 239173d049a4SAl Viro struct nameidata nd; 239273d049a4SAl Viro struct file *file; 239373d049a4SAl Viro 239473d049a4SAl Viro nd.root.mnt = mnt; 239573d049a4SAl Viro nd.root.dentry = dentry; 239673d049a4SAl Viro 239773d049a4SAl Viro flags |= LOOKUP_ROOT; 239873d049a4SAl Viro 2399bcda7652SAl Viro if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN) 240073d049a4SAl Viro return ERR_PTR(-ELOOP); 240173d049a4SAl Viro 240273d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU); 240373d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 240473d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags); 240573d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 240673d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL); 240773d049a4SAl Viro return file; 240873d049a4SAl Viro } 240973d049a4SAl Viro 24101da177e4SLinus Torvalds /** 24111da177e4SLinus Torvalds * lookup_create - lookup a dentry, creating it if it doesn't exist 24121da177e4SLinus Torvalds * @nd: nameidata info 24131da177e4SLinus Torvalds * @is_dir: directory flag 24141da177e4SLinus Torvalds * 24151da177e4SLinus Torvalds * Simple function to lookup and return a dentry and create it 24161da177e4SLinus Torvalds * if it doesn't exist. Is SMP-safe. 2417c663e5d8SChristoph Hellwig * 24184ac91378SJan Blunck * Returns with nd->path.dentry->d_inode->i_mutex locked. 24191da177e4SLinus Torvalds */ 24201da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir) 24211da177e4SLinus Torvalds { 2422c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 24231da177e4SLinus Torvalds 24244ac91378SJan Blunck mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 2425c663e5d8SChristoph Hellwig /* 2426c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 2427c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 2428c663e5d8SChristoph Hellwig */ 24291da177e4SLinus Torvalds if (nd->last_type != LAST_NORM) 24301da177e4SLinus Torvalds goto fail; 24311da177e4SLinus Torvalds nd->flags &= ~LOOKUP_PARENT; 24323516586aSAl Viro nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL; 2433a634904aSASANO Masahiro nd->intent.open.flags = O_EXCL; 2434c663e5d8SChristoph Hellwig 2435c663e5d8SChristoph Hellwig /* 2436c663e5d8SChristoph Hellwig * Do the final lookup. 2437c663e5d8SChristoph Hellwig */ 243849705b77SChristoph Hellwig dentry = lookup_hash(nd); 24391da177e4SLinus Torvalds if (IS_ERR(dentry)) 24401da177e4SLinus Torvalds goto fail; 2441c663e5d8SChristoph Hellwig 2442e9baf6e5SAl Viro if (dentry->d_inode) 2443e9baf6e5SAl Viro goto eexist; 2444c663e5d8SChristoph Hellwig /* 2445c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 2446c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 2447c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 2448c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 2449c663e5d8SChristoph Hellwig */ 2450e9baf6e5SAl Viro if (unlikely(!is_dir && nd->last.name[nd->last.len])) { 24511da177e4SLinus Torvalds dput(dentry); 24521da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 2453e9baf6e5SAl Viro } 2454e9baf6e5SAl Viro return dentry; 2455e9baf6e5SAl Viro eexist: 2456e9baf6e5SAl Viro dput(dentry); 2457e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 24581da177e4SLinus Torvalds fail: 24591da177e4SLinus Torvalds return dentry; 24601da177e4SLinus Torvalds } 2461f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create); 24621da177e4SLinus Torvalds 24631da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 24641da177e4SLinus Torvalds { 2465a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 24661da177e4SLinus Torvalds 24671da177e4SLinus Torvalds if (error) 24681da177e4SLinus Torvalds return error; 24691da177e4SLinus Torvalds 2470e795b717SSerge E. Hallyn if ((S_ISCHR(mode) || S_ISBLK(mode)) && 2471e795b717SSerge E. Hallyn !ns_capable(inode_userns(dir), CAP_MKNOD)) 24721da177e4SLinus Torvalds return -EPERM; 24731da177e4SLinus Torvalds 2474acfa4380SAl Viro if (!dir->i_op->mknod) 24751da177e4SLinus Torvalds return -EPERM; 24761da177e4SLinus Torvalds 247708ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 247808ce5f16SSerge E. Hallyn if (error) 247908ce5f16SSerge E. Hallyn return error; 248008ce5f16SSerge E. Hallyn 24811da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 24821da177e4SLinus Torvalds if (error) 24831da177e4SLinus Torvalds return error; 24841da177e4SLinus Torvalds 24851da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 2486a74574aaSStephen Smalley if (!error) 2487f38aa942SAmy Griffis fsnotify_create(dir, dentry); 24881da177e4SLinus Torvalds return error; 24891da177e4SLinus Torvalds } 24901da177e4SLinus Torvalds 2491463c3197SDave Hansen static int may_mknod(mode_t mode) 2492463c3197SDave Hansen { 2493463c3197SDave Hansen switch (mode & S_IFMT) { 2494463c3197SDave Hansen case S_IFREG: 2495463c3197SDave Hansen case S_IFCHR: 2496463c3197SDave Hansen case S_IFBLK: 2497463c3197SDave Hansen case S_IFIFO: 2498463c3197SDave Hansen case S_IFSOCK: 2499463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2500463c3197SDave Hansen return 0; 2501463c3197SDave Hansen case S_IFDIR: 2502463c3197SDave Hansen return -EPERM; 2503463c3197SDave Hansen default: 2504463c3197SDave Hansen return -EINVAL; 2505463c3197SDave Hansen } 2506463c3197SDave Hansen } 2507463c3197SDave Hansen 25082e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode, 25092e4d0924SHeiko Carstens unsigned, dev) 25101da177e4SLinus Torvalds { 25112ad94ae6SAl Viro int error; 25121da177e4SLinus Torvalds char *tmp; 25131da177e4SLinus Torvalds struct dentry *dentry; 25141da177e4SLinus Torvalds struct nameidata nd; 25151da177e4SLinus Torvalds 25161da177e4SLinus Torvalds if (S_ISDIR(mode)) 25171da177e4SLinus Torvalds return -EPERM; 25181da177e4SLinus Torvalds 25192ad94ae6SAl Viro error = user_path_parent(dfd, filename, &nd, &tmp); 25201da177e4SLinus Torvalds if (error) 25212ad94ae6SAl Viro return error; 25222ad94ae6SAl Viro 25231da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 2524463c3197SDave Hansen if (IS_ERR(dentry)) { 25251da177e4SLinus Torvalds error = PTR_ERR(dentry); 2526463c3197SDave Hansen goto out_unlock; 2527463c3197SDave Hansen } 25284ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2529ce3b0f8dSAl Viro mode &= ~current_umask(); 2530463c3197SDave Hansen error = may_mknod(mode); 2531463c3197SDave Hansen if (error) 2532463c3197SDave Hansen goto out_dput; 2533463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2534463c3197SDave Hansen if (error) 2535463c3197SDave Hansen goto out_dput; 2536be6d3e56SKentaro Takeda error = security_path_mknod(&nd.path, dentry, mode, dev); 2537be6d3e56SKentaro Takeda if (error) 2538be6d3e56SKentaro Takeda goto out_drop_write; 25391da177e4SLinus Torvalds switch (mode & S_IFMT) { 25401da177e4SLinus Torvalds case 0: case S_IFREG: 25414ac91378SJan Blunck error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd); 25421da177e4SLinus Torvalds break; 25431da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 25444ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode, 25451da177e4SLinus Torvalds new_decode_dev(dev)); 25461da177e4SLinus Torvalds break; 25471da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 25484ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0); 25491da177e4SLinus Torvalds break; 25501da177e4SLinus Torvalds } 2551be6d3e56SKentaro Takeda out_drop_write: 2552463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2553463c3197SDave Hansen out_dput: 25541da177e4SLinus Torvalds dput(dentry); 2555463c3197SDave Hansen out_unlock: 25564ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 25571d957f9bSJan Blunck path_put(&nd.path); 25581da177e4SLinus Torvalds putname(tmp); 25591da177e4SLinus Torvalds 25601da177e4SLinus Torvalds return error; 25611da177e4SLinus Torvalds } 25621da177e4SLinus Torvalds 25633480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev) 25645590ff0dSUlrich Drepper { 25655590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 25665590ff0dSUlrich Drepper } 25675590ff0dSUlrich Drepper 25681da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 25691da177e4SLinus Torvalds { 2570a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 25711da177e4SLinus Torvalds 25721da177e4SLinus Torvalds if (error) 25731da177e4SLinus Torvalds return error; 25741da177e4SLinus Torvalds 2575acfa4380SAl Viro if (!dir->i_op->mkdir) 25761da177e4SLinus Torvalds return -EPERM; 25771da177e4SLinus Torvalds 25781da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 25791da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 25801da177e4SLinus Torvalds if (error) 25811da177e4SLinus Torvalds return error; 25821da177e4SLinus Torvalds 25831da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2584a74574aaSStephen Smalley if (!error) 2585f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 25861da177e4SLinus Torvalds return error; 25871da177e4SLinus Torvalds } 25881da177e4SLinus Torvalds 25892e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode) 25901da177e4SLinus Torvalds { 25911da177e4SLinus Torvalds int error = 0; 25921da177e4SLinus Torvalds char * tmp; 25936902d925SDave Hansen struct dentry *dentry; 25946902d925SDave Hansen struct nameidata nd; 25951da177e4SLinus Torvalds 25962ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &tmp); 25972ad94ae6SAl Viro if (error) 25986902d925SDave Hansen goto out_err; 25991da177e4SLinus Torvalds 26001da177e4SLinus Torvalds dentry = lookup_create(&nd, 1); 26011da177e4SLinus Torvalds error = PTR_ERR(dentry); 26026902d925SDave Hansen if (IS_ERR(dentry)) 26036902d925SDave Hansen goto out_unlock; 26046902d925SDave Hansen 26054ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2606ce3b0f8dSAl Viro mode &= ~current_umask(); 2607463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2608463c3197SDave Hansen if (error) 2609463c3197SDave Hansen goto out_dput; 2610be6d3e56SKentaro Takeda error = security_path_mkdir(&nd.path, dentry, mode); 2611be6d3e56SKentaro Takeda if (error) 2612be6d3e56SKentaro Takeda goto out_drop_write; 26134ac91378SJan Blunck error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode); 2614be6d3e56SKentaro Takeda out_drop_write: 2615463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2616463c3197SDave Hansen out_dput: 26171da177e4SLinus Torvalds dput(dentry); 26186902d925SDave Hansen out_unlock: 26194ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 26201d957f9bSJan Blunck path_put(&nd.path); 26211da177e4SLinus Torvalds putname(tmp); 26226902d925SDave Hansen out_err: 26231da177e4SLinus Torvalds return error; 26241da177e4SLinus Torvalds } 26251da177e4SLinus Torvalds 26263cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode) 26275590ff0dSUlrich Drepper { 26285590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 26295590ff0dSUlrich Drepper } 26305590ff0dSUlrich Drepper 26311da177e4SLinus Torvalds /* 26321da177e4SLinus Torvalds * We try to drop the dentry early: we should have 26331da177e4SLinus Torvalds * a usage count of 2 if we're the only user of this 26341da177e4SLinus Torvalds * dentry, and if that is true (possibly after pruning 26351da177e4SLinus Torvalds * the dcache), then we drop the dentry now. 26361da177e4SLinus Torvalds * 26371da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 26381da177e4SLinus Torvalds * do a 26391da177e4SLinus Torvalds * 26401da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 26411da177e4SLinus Torvalds * return -EBUSY; 26421da177e4SLinus Torvalds * 26431da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 26441da177e4SLinus Torvalds * that is still in use by something else.. 26451da177e4SLinus Torvalds */ 26461da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 26471da177e4SLinus Torvalds { 26481da177e4SLinus Torvalds dget(dentry); 26491da177e4SLinus Torvalds shrink_dcache_parent(dentry); 26501da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 2651b7ab39f6SNick Piggin if (dentry->d_count == 2) 26521da177e4SLinus Torvalds __d_drop(dentry); 26531da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 26541da177e4SLinus Torvalds } 26551da177e4SLinus Torvalds 26561da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 26571da177e4SLinus Torvalds { 26581da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 26591da177e4SLinus Torvalds 26601da177e4SLinus Torvalds if (error) 26611da177e4SLinus Torvalds return error; 26621da177e4SLinus Torvalds 2663acfa4380SAl Viro if (!dir->i_op->rmdir) 26641da177e4SLinus Torvalds return -EPERM; 26651da177e4SLinus Torvalds 26661b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 26671da177e4SLinus Torvalds dentry_unhash(dentry); 26681da177e4SLinus Torvalds if (d_mountpoint(dentry)) 26691da177e4SLinus Torvalds error = -EBUSY; 26701da177e4SLinus Torvalds else { 26711da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 26721da177e4SLinus Torvalds if (!error) { 26731da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 2674d83c49f3SAl Viro if (!error) { 26751da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 2676d83c49f3SAl Viro dont_mount(dentry); 2677d83c49f3SAl Viro } 26781da177e4SLinus Torvalds } 26791da177e4SLinus Torvalds } 26801b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 26811da177e4SLinus Torvalds if (!error) { 26821da177e4SLinus Torvalds d_delete(dentry); 26831da177e4SLinus Torvalds } 26841da177e4SLinus Torvalds dput(dentry); 26851da177e4SLinus Torvalds 26861da177e4SLinus Torvalds return error; 26871da177e4SLinus Torvalds } 26881da177e4SLinus Torvalds 26895590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 26901da177e4SLinus Torvalds { 26911da177e4SLinus Torvalds int error = 0; 26921da177e4SLinus Torvalds char * name; 26931da177e4SLinus Torvalds struct dentry *dentry; 26941da177e4SLinus Torvalds struct nameidata nd; 26951da177e4SLinus Torvalds 26962ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 26971da177e4SLinus Torvalds if (error) 26982ad94ae6SAl Viro return error; 26991da177e4SLinus Torvalds 27001da177e4SLinus Torvalds switch(nd.last_type) { 27011da177e4SLinus Torvalds case LAST_DOTDOT: 27021da177e4SLinus Torvalds error = -ENOTEMPTY; 27031da177e4SLinus Torvalds goto exit1; 27041da177e4SLinus Torvalds case LAST_DOT: 27051da177e4SLinus Torvalds error = -EINVAL; 27061da177e4SLinus Torvalds goto exit1; 27071da177e4SLinus Torvalds case LAST_ROOT: 27081da177e4SLinus Torvalds error = -EBUSY; 27091da177e4SLinus Torvalds goto exit1; 27101da177e4SLinus Torvalds } 27110612d9fbSOGAWA Hirofumi 27120612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 27130612d9fbSOGAWA Hirofumi 27144ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 271549705b77SChristoph Hellwig dentry = lookup_hash(&nd); 27161da177e4SLinus Torvalds error = PTR_ERR(dentry); 27176902d925SDave Hansen if (IS_ERR(dentry)) 27186902d925SDave Hansen goto exit2; 27190622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 27200622753bSDave Hansen if (error) 27210622753bSDave Hansen goto exit3; 2722be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 2723be6d3e56SKentaro Takeda if (error) 2724be6d3e56SKentaro Takeda goto exit4; 27254ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 2726be6d3e56SKentaro Takeda exit4: 27270622753bSDave Hansen mnt_drop_write(nd.path.mnt); 27280622753bSDave Hansen exit3: 27291da177e4SLinus Torvalds dput(dentry); 27306902d925SDave Hansen exit2: 27314ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 27321da177e4SLinus Torvalds exit1: 27331d957f9bSJan Blunck path_put(&nd.path); 27341da177e4SLinus Torvalds putname(name); 27351da177e4SLinus Torvalds return error; 27361da177e4SLinus Torvalds } 27371da177e4SLinus Torvalds 27383cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 27395590ff0dSUlrich Drepper { 27405590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 27415590ff0dSUlrich Drepper } 27425590ff0dSUlrich Drepper 27431da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 27441da177e4SLinus Torvalds { 27451da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 27461da177e4SLinus Torvalds 27471da177e4SLinus Torvalds if (error) 27481da177e4SLinus Torvalds return error; 27491da177e4SLinus Torvalds 2750acfa4380SAl Viro if (!dir->i_op->unlink) 27511da177e4SLinus Torvalds return -EPERM; 27521da177e4SLinus Torvalds 27531b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 27541da177e4SLinus Torvalds if (d_mountpoint(dentry)) 27551da177e4SLinus Torvalds error = -EBUSY; 27561da177e4SLinus Torvalds else { 27571da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 2758bec1052eSAl Viro if (!error) { 27591da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 2760bec1052eSAl Viro if (!error) 2761d83c49f3SAl Viro dont_mount(dentry); 2762bec1052eSAl Viro } 27631da177e4SLinus Torvalds } 27641b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 27651da177e4SLinus Torvalds 27661da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 27671da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 2768ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 27691da177e4SLinus Torvalds d_delete(dentry); 27701da177e4SLinus Torvalds } 27710eeca283SRobert Love 27721da177e4SLinus Torvalds return error; 27731da177e4SLinus Torvalds } 27741da177e4SLinus Torvalds 27751da177e4SLinus Torvalds /* 27761da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 27771b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 27781da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 27791da177e4SLinus Torvalds * while waiting on the I/O. 27801da177e4SLinus Torvalds */ 27815590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 27821da177e4SLinus Torvalds { 27832ad94ae6SAl Viro int error; 27841da177e4SLinus Torvalds char *name; 27851da177e4SLinus Torvalds struct dentry *dentry; 27861da177e4SLinus Torvalds struct nameidata nd; 27871da177e4SLinus Torvalds struct inode *inode = NULL; 27881da177e4SLinus Torvalds 27892ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 27901da177e4SLinus Torvalds if (error) 27912ad94ae6SAl Viro return error; 27922ad94ae6SAl Viro 27931da177e4SLinus Torvalds error = -EISDIR; 27941da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 27951da177e4SLinus Torvalds goto exit1; 27960612d9fbSOGAWA Hirofumi 27970612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 27980612d9fbSOGAWA Hirofumi 27994ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 280049705b77SChristoph Hellwig dentry = lookup_hash(&nd); 28011da177e4SLinus Torvalds error = PTR_ERR(dentry); 28021da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 28031da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 28041da177e4SLinus Torvalds if (nd.last.name[nd.last.len]) 28051da177e4SLinus Torvalds goto slashes; 28061da177e4SLinus Torvalds inode = dentry->d_inode; 28071da177e4SLinus Torvalds if (inode) 28087de9c6eeSAl Viro ihold(inode); 28090622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 28100622753bSDave Hansen if (error) 28110622753bSDave Hansen goto exit2; 2812be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 2813be6d3e56SKentaro Takeda if (error) 2814be6d3e56SKentaro Takeda goto exit3; 28154ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 2816be6d3e56SKentaro Takeda exit3: 28170622753bSDave Hansen mnt_drop_write(nd.path.mnt); 28181da177e4SLinus Torvalds exit2: 28191da177e4SLinus Torvalds dput(dentry); 28201da177e4SLinus Torvalds } 28214ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 28221da177e4SLinus Torvalds if (inode) 28231da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 28241da177e4SLinus Torvalds exit1: 28251d957f9bSJan Blunck path_put(&nd.path); 28261da177e4SLinus Torvalds putname(name); 28271da177e4SLinus Torvalds return error; 28281da177e4SLinus Torvalds 28291da177e4SLinus Torvalds slashes: 28301da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 28311da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 28321da177e4SLinus Torvalds goto exit2; 28331da177e4SLinus Torvalds } 28341da177e4SLinus Torvalds 28352e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 28365590ff0dSUlrich Drepper { 28375590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 28385590ff0dSUlrich Drepper return -EINVAL; 28395590ff0dSUlrich Drepper 28405590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 28415590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 28425590ff0dSUlrich Drepper 28435590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 28445590ff0dSUlrich Drepper } 28455590ff0dSUlrich Drepper 28463480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 28475590ff0dSUlrich Drepper { 28485590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 28495590ff0dSUlrich Drepper } 28505590ff0dSUlrich Drepper 2851db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 28521da177e4SLinus Torvalds { 2853a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 28541da177e4SLinus Torvalds 28551da177e4SLinus Torvalds if (error) 28561da177e4SLinus Torvalds return error; 28571da177e4SLinus Torvalds 2858acfa4380SAl Viro if (!dir->i_op->symlink) 28591da177e4SLinus Torvalds return -EPERM; 28601da177e4SLinus Torvalds 28611da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 28621da177e4SLinus Torvalds if (error) 28631da177e4SLinus Torvalds return error; 28641da177e4SLinus Torvalds 28651da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 2866a74574aaSStephen Smalley if (!error) 2867f38aa942SAmy Griffis fsnotify_create(dir, dentry); 28681da177e4SLinus Torvalds return error; 28691da177e4SLinus Torvalds } 28701da177e4SLinus Torvalds 28712e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 28722e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 28731da177e4SLinus Torvalds { 28742ad94ae6SAl Viro int error; 28751da177e4SLinus Torvalds char *from; 28761da177e4SLinus Torvalds char *to; 28776902d925SDave Hansen struct dentry *dentry; 28786902d925SDave Hansen struct nameidata nd; 28791da177e4SLinus Torvalds 28801da177e4SLinus Torvalds from = getname(oldname); 28811da177e4SLinus Torvalds if (IS_ERR(from)) 28821da177e4SLinus Torvalds return PTR_ERR(from); 28832ad94ae6SAl Viro 28842ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 28852ad94ae6SAl Viro if (error) 28866902d925SDave Hansen goto out_putname; 28871da177e4SLinus Torvalds 28881da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 28891da177e4SLinus Torvalds error = PTR_ERR(dentry); 28906902d925SDave Hansen if (IS_ERR(dentry)) 28916902d925SDave Hansen goto out_unlock; 28926902d925SDave Hansen 289375c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 289475c3f29dSDave Hansen if (error) 289575c3f29dSDave Hansen goto out_dput; 2896be6d3e56SKentaro Takeda error = security_path_symlink(&nd.path, dentry, from); 2897be6d3e56SKentaro Takeda if (error) 2898be6d3e56SKentaro Takeda goto out_drop_write; 2899db2e747bSMiklos Szeredi error = vfs_symlink(nd.path.dentry->d_inode, dentry, from); 2900be6d3e56SKentaro Takeda out_drop_write: 290175c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 290275c3f29dSDave Hansen out_dput: 29031da177e4SLinus Torvalds dput(dentry); 29046902d925SDave Hansen out_unlock: 29054ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 29061d957f9bSJan Blunck path_put(&nd.path); 29071da177e4SLinus Torvalds putname(to); 29086902d925SDave Hansen out_putname: 29091da177e4SLinus Torvalds putname(from); 29101da177e4SLinus Torvalds return error; 29111da177e4SLinus Torvalds } 29121da177e4SLinus Torvalds 29133480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 29145590ff0dSUlrich Drepper { 29155590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 29165590ff0dSUlrich Drepper } 29175590ff0dSUlrich Drepper 29181da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 29191da177e4SLinus Torvalds { 29201da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 29211da177e4SLinus Torvalds int error; 29221da177e4SLinus Torvalds 29231da177e4SLinus Torvalds if (!inode) 29241da177e4SLinus Torvalds return -ENOENT; 29251da177e4SLinus Torvalds 2926a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 29271da177e4SLinus Torvalds if (error) 29281da177e4SLinus Torvalds return error; 29291da177e4SLinus Torvalds 29301da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 29311da177e4SLinus Torvalds return -EXDEV; 29321da177e4SLinus Torvalds 29331da177e4SLinus Torvalds /* 29341da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 29351da177e4SLinus Torvalds */ 29361da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 29371da177e4SLinus Torvalds return -EPERM; 2938acfa4380SAl Viro if (!dir->i_op->link) 29391da177e4SLinus Torvalds return -EPERM; 29407e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 29411da177e4SLinus Torvalds return -EPERM; 29421da177e4SLinus Torvalds 29431da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 29441da177e4SLinus Torvalds if (error) 29451da177e4SLinus Torvalds return error; 29461da177e4SLinus Torvalds 29477e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 2948aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 2949aae8a97dSAneesh Kumar K.V if (inode->i_nlink == 0) 2950aae8a97dSAneesh Kumar K.V error = -ENOENT; 2951aae8a97dSAneesh Kumar K.V else 29521da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 29537e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 2954e31e14ecSStephen Smalley if (!error) 29557e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 29561da177e4SLinus Torvalds return error; 29571da177e4SLinus Torvalds } 29581da177e4SLinus Torvalds 29591da177e4SLinus Torvalds /* 29601da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 29611da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 29621da177e4SLinus Torvalds * newname. --KAB 29631da177e4SLinus Torvalds * 29641da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 29651da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 29661da177e4SLinus Torvalds * and other special files. --ADM 29671da177e4SLinus Torvalds */ 29682e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 29692e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 29701da177e4SLinus Torvalds { 29711da177e4SLinus Torvalds struct dentry *new_dentry; 29722d8f3038SAl Viro struct nameidata nd; 29732d8f3038SAl Viro struct path old_path; 297411a7b371SAneesh Kumar K.V int how = 0; 29751da177e4SLinus Torvalds int error; 29761da177e4SLinus Torvalds char *to; 29771da177e4SLinus Torvalds 297811a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 2979c04030e1SUlrich Drepper return -EINVAL; 298011a7b371SAneesh Kumar K.V /* 298111a7b371SAneesh Kumar K.V * To use null names we require CAP_DAC_READ_SEARCH 298211a7b371SAneesh Kumar K.V * This ensures that not everyone will be able to create 298311a7b371SAneesh Kumar K.V * handlink using the passed filedescriptor. 298411a7b371SAneesh Kumar K.V */ 298511a7b371SAneesh Kumar K.V if (flags & AT_EMPTY_PATH) { 298611a7b371SAneesh Kumar K.V if (!capable(CAP_DAC_READ_SEARCH)) 298711a7b371SAneesh Kumar K.V return -ENOENT; 298811a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 298911a7b371SAneesh Kumar K.V } 2990c04030e1SUlrich Drepper 299111a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 299211a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 299311a7b371SAneesh Kumar K.V 299411a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 29951da177e4SLinus Torvalds if (error) 29962ad94ae6SAl Viro return error; 29972ad94ae6SAl Viro 29982ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 29991da177e4SLinus Torvalds if (error) 30001da177e4SLinus Torvalds goto out; 30011da177e4SLinus Torvalds error = -EXDEV; 30022d8f3038SAl Viro if (old_path.mnt != nd.path.mnt) 30031da177e4SLinus Torvalds goto out_release; 30041da177e4SLinus Torvalds new_dentry = lookup_create(&nd, 0); 30051da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 30066902d925SDave Hansen if (IS_ERR(new_dentry)) 30076902d925SDave Hansen goto out_unlock; 300875c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 300975c3f29dSDave Hansen if (error) 301075c3f29dSDave Hansen goto out_dput; 3011be6d3e56SKentaro Takeda error = security_path_link(old_path.dentry, &nd.path, new_dentry); 3012be6d3e56SKentaro Takeda if (error) 3013be6d3e56SKentaro Takeda goto out_drop_write; 30142d8f3038SAl Viro error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry); 3015be6d3e56SKentaro Takeda out_drop_write: 301675c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 301775c3f29dSDave Hansen out_dput: 30181da177e4SLinus Torvalds dput(new_dentry); 30196902d925SDave Hansen out_unlock: 30204ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 30211da177e4SLinus Torvalds out_release: 30221d957f9bSJan Blunck path_put(&nd.path); 30232ad94ae6SAl Viro putname(to); 30241da177e4SLinus Torvalds out: 30252d8f3038SAl Viro path_put(&old_path); 30261da177e4SLinus Torvalds 30271da177e4SLinus Torvalds return error; 30281da177e4SLinus Torvalds } 30291da177e4SLinus Torvalds 30303480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 30315590ff0dSUlrich Drepper { 3032c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 30335590ff0dSUlrich Drepper } 30345590ff0dSUlrich Drepper 30351da177e4SLinus Torvalds /* 30361da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 30371da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 30381da177e4SLinus Torvalds * Problems: 30391da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 30401da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 30411da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 3042a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 30431da177e4SLinus Torvalds * story. 30441da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 30451b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 30461da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 30471da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 3048a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 30491da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 30501da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 30511da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 3052a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 30531da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 30541da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 30551da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 30561da177e4SLinus Torvalds * d) some filesystems don't support opened-but-unlinked directories, 30571da177e4SLinus Torvalds * either because of layout or because they are not ready to deal with 30581da177e4SLinus Torvalds * all cases correctly. The latter will be fixed (taking this sort of 30591da177e4SLinus Torvalds * stuff into VFS), but the former is not going away. Solution: the same 30601da177e4SLinus Torvalds * trick as in rmdir(). 30611da177e4SLinus Torvalds * e) conversion from fhandle to dentry may come in the wrong moment - when 30621b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 30631da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 3064c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 30651da177e4SLinus Torvalds * locking]. 30661da177e4SLinus Torvalds */ 306775c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 30681da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30691da177e4SLinus Torvalds { 30701da177e4SLinus Torvalds int error = 0; 30711da177e4SLinus Torvalds struct inode *target; 30721da177e4SLinus Torvalds 30731da177e4SLinus Torvalds /* 30741da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 30751da177e4SLinus Torvalds * we'll need to flip '..'. 30761da177e4SLinus Torvalds */ 30771da177e4SLinus Torvalds if (new_dir != old_dir) { 3078f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 30791da177e4SLinus Torvalds if (error) 30801da177e4SLinus Torvalds return error; 30811da177e4SLinus Torvalds } 30821da177e4SLinus Torvalds 30831da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 30841da177e4SLinus Torvalds if (error) 30851da177e4SLinus Torvalds return error; 30861da177e4SLinus Torvalds 30871da177e4SLinus Torvalds target = new_dentry->d_inode; 3088d83c49f3SAl Viro if (target) 30891b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 30901da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 30911da177e4SLinus Torvalds error = -EBUSY; 3092d83c49f3SAl Viro else { 3093d83c49f3SAl Viro if (target) 3094d83c49f3SAl Viro dentry_unhash(new_dentry); 30951da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 3096d83c49f3SAl Viro } 30971da177e4SLinus Torvalds if (target) { 3098d83c49f3SAl Viro if (!error) { 30991da177e4SLinus Torvalds target->i_flags |= S_DEAD; 3100d83c49f3SAl Viro dont_mount(new_dentry); 3101d83c49f3SAl Viro } 31021b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 31031da177e4SLinus Torvalds if (d_unhashed(new_dentry)) 31041da177e4SLinus Torvalds d_rehash(new_dentry); 31051da177e4SLinus Torvalds dput(new_dentry); 31061da177e4SLinus Torvalds } 3107e31e14ecSStephen Smalley if (!error) 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 return error; 31111da177e4SLinus Torvalds } 31121da177e4SLinus Torvalds 311375c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 31141da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 31151da177e4SLinus Torvalds { 31161da177e4SLinus Torvalds struct inode *target; 31171da177e4SLinus Torvalds int error; 31181da177e4SLinus Torvalds 31191da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 31201da177e4SLinus Torvalds if (error) 31211da177e4SLinus Torvalds return error; 31221da177e4SLinus Torvalds 31231da177e4SLinus Torvalds dget(new_dentry); 31241da177e4SLinus Torvalds target = new_dentry->d_inode; 31251da177e4SLinus Torvalds if (target) 31261b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 31271da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 31281da177e4SLinus Torvalds error = -EBUSY; 31291da177e4SLinus Torvalds else 31301da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 31311da177e4SLinus Torvalds if (!error) { 3132bec1052eSAl Viro if (target) 3133d83c49f3SAl Viro dont_mount(new_dentry); 3134349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 31351da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 31361da177e4SLinus Torvalds } 31371da177e4SLinus Torvalds if (target) 31381b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 31391da177e4SLinus Torvalds dput(new_dentry); 31401da177e4SLinus Torvalds return error; 31411da177e4SLinus Torvalds } 31421da177e4SLinus Torvalds 31431da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 31441da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 31451da177e4SLinus Torvalds { 31461da177e4SLinus Torvalds int error; 31471da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 314859b0df21SEric Paris const unsigned char *old_name; 31491da177e4SLinus Torvalds 31501da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 31511da177e4SLinus Torvalds return 0; 31521da177e4SLinus Torvalds 31531da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 31541da177e4SLinus Torvalds if (error) 31551da177e4SLinus Torvalds return error; 31561da177e4SLinus Torvalds 31571da177e4SLinus Torvalds if (!new_dentry->d_inode) 3158a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 31591da177e4SLinus Torvalds else 31601da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 31611da177e4SLinus Torvalds if (error) 31621da177e4SLinus Torvalds return error; 31631da177e4SLinus Torvalds 3164acfa4380SAl Viro if (!old_dir->i_op->rename) 31651da177e4SLinus Torvalds return -EPERM; 31661da177e4SLinus Torvalds 31670eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 31680eeca283SRobert Love 31691da177e4SLinus Torvalds if (is_dir) 31701da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 31711da177e4SLinus Torvalds else 31721da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3173123df294SAl Viro if (!error) 3174123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 31755a190ae6SAl Viro new_dentry->d_inode, old_dentry); 31760eeca283SRobert Love fsnotify_oldname_free(old_name); 31770eeca283SRobert Love 31781da177e4SLinus Torvalds return error; 31791da177e4SLinus Torvalds } 31801da177e4SLinus Torvalds 31812e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 31822e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 31831da177e4SLinus Torvalds { 31841da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 31851da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 31861da177e4SLinus Torvalds struct dentry *trap; 31871da177e4SLinus Torvalds struct nameidata oldnd, newnd; 31882ad94ae6SAl Viro char *from; 31892ad94ae6SAl Viro char *to; 31902ad94ae6SAl Viro int error; 31911da177e4SLinus Torvalds 31922ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 31931da177e4SLinus Torvalds if (error) 31941da177e4SLinus Torvalds goto exit; 31951da177e4SLinus Torvalds 31962ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 31971da177e4SLinus Torvalds if (error) 31981da177e4SLinus Torvalds goto exit1; 31991da177e4SLinus Torvalds 32001da177e4SLinus Torvalds error = -EXDEV; 32014ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 32021da177e4SLinus Torvalds goto exit2; 32031da177e4SLinus Torvalds 32044ac91378SJan Blunck old_dir = oldnd.path.dentry; 32051da177e4SLinus Torvalds error = -EBUSY; 32061da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 32071da177e4SLinus Torvalds goto exit2; 32081da177e4SLinus Torvalds 32094ac91378SJan Blunck new_dir = newnd.path.dentry; 32101da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 32111da177e4SLinus Torvalds goto exit2; 32121da177e4SLinus Torvalds 32130612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 32140612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 32154e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 32160612d9fbSOGAWA Hirofumi 32171da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 32181da177e4SLinus Torvalds 321949705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 32201da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 32211da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 32221da177e4SLinus Torvalds goto exit3; 32231da177e4SLinus Torvalds /* source must exist */ 32241da177e4SLinus Torvalds error = -ENOENT; 32251da177e4SLinus Torvalds if (!old_dentry->d_inode) 32261da177e4SLinus Torvalds goto exit4; 32271da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 32281da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 32291da177e4SLinus Torvalds error = -ENOTDIR; 32301da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 32311da177e4SLinus Torvalds goto exit4; 32321da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 32331da177e4SLinus Torvalds goto exit4; 32341da177e4SLinus Torvalds } 32351da177e4SLinus Torvalds /* source should not be ancestor of target */ 32361da177e4SLinus Torvalds error = -EINVAL; 32371da177e4SLinus Torvalds if (old_dentry == trap) 32381da177e4SLinus Torvalds goto exit4; 323949705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 32401da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 32411da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 32421da177e4SLinus Torvalds goto exit4; 32431da177e4SLinus Torvalds /* target should not be an ancestor of source */ 32441da177e4SLinus Torvalds error = -ENOTEMPTY; 32451da177e4SLinus Torvalds if (new_dentry == trap) 32461da177e4SLinus Torvalds goto exit5; 32471da177e4SLinus Torvalds 32489079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 32499079b1ebSDave Hansen if (error) 32509079b1ebSDave Hansen goto exit5; 3251be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3252be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3253be6d3e56SKentaro Takeda if (error) 3254be6d3e56SKentaro Takeda goto exit6; 32551da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 32561da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 3257be6d3e56SKentaro Takeda exit6: 32589079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 32591da177e4SLinus Torvalds exit5: 32601da177e4SLinus Torvalds dput(new_dentry); 32611da177e4SLinus Torvalds exit4: 32621da177e4SLinus Torvalds dput(old_dentry); 32631da177e4SLinus Torvalds exit3: 32641da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 32651da177e4SLinus Torvalds exit2: 32661d957f9bSJan Blunck path_put(&newnd.path); 32672ad94ae6SAl Viro putname(to); 32681da177e4SLinus Torvalds exit1: 32691d957f9bSJan Blunck path_put(&oldnd.path); 32701da177e4SLinus Torvalds putname(from); 32712ad94ae6SAl Viro exit: 32721da177e4SLinus Torvalds return error; 32731da177e4SLinus Torvalds } 32741da177e4SLinus Torvalds 3275a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 32765590ff0dSUlrich Drepper { 32775590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 32785590ff0dSUlrich Drepper } 32795590ff0dSUlrich Drepper 32801da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 32811da177e4SLinus Torvalds { 32821da177e4SLinus Torvalds int len; 32831da177e4SLinus Torvalds 32841da177e4SLinus Torvalds len = PTR_ERR(link); 32851da177e4SLinus Torvalds if (IS_ERR(link)) 32861da177e4SLinus Torvalds goto out; 32871da177e4SLinus Torvalds 32881da177e4SLinus Torvalds len = strlen(link); 32891da177e4SLinus Torvalds if (len > (unsigned) buflen) 32901da177e4SLinus Torvalds len = buflen; 32911da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 32921da177e4SLinus Torvalds len = -EFAULT; 32931da177e4SLinus Torvalds out: 32941da177e4SLinus Torvalds return len; 32951da177e4SLinus Torvalds } 32961da177e4SLinus Torvalds 32971da177e4SLinus Torvalds /* 32981da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 32991da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 33001da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 33011da177e4SLinus Torvalds */ 33021da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 33031da177e4SLinus Torvalds { 33041da177e4SLinus Torvalds struct nameidata nd; 3305cc314eefSLinus Torvalds void *cookie; 3306694a1764SMarcin Slusarz int res; 3307cc314eefSLinus Torvalds 33081da177e4SLinus Torvalds nd.depth = 0; 3309cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3310694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3311694a1764SMarcin Slusarz return PTR_ERR(cookie); 3312694a1764SMarcin Slusarz 3313694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 33141da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3315cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3316694a1764SMarcin Slusarz return res; 33171da177e4SLinus Torvalds } 33181da177e4SLinus Torvalds 33191da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 33201da177e4SLinus Torvalds { 33211da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 33221da177e4SLinus Torvalds } 33231da177e4SLinus Torvalds 33241da177e4SLinus Torvalds /* get the link contents into pagecache */ 33251da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 33261da177e4SLinus Torvalds { 3327ebd09abbSDuane Griffin char *kaddr; 33281da177e4SLinus Torvalds struct page *page; 33291da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3330090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 33311da177e4SLinus Torvalds if (IS_ERR(page)) 33326fe6900eSNick Piggin return (char*)page; 33331da177e4SLinus Torvalds *ppage = page; 3334ebd09abbSDuane Griffin kaddr = kmap(page); 3335ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3336ebd09abbSDuane Griffin return kaddr; 33371da177e4SLinus Torvalds } 33381da177e4SLinus Torvalds 33391da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 33401da177e4SLinus Torvalds { 33411da177e4SLinus Torvalds struct page *page = NULL; 33421da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 33431da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 33441da177e4SLinus Torvalds if (page) { 33451da177e4SLinus Torvalds kunmap(page); 33461da177e4SLinus Torvalds page_cache_release(page); 33471da177e4SLinus Torvalds } 33481da177e4SLinus Torvalds return res; 33491da177e4SLinus Torvalds } 33501da177e4SLinus Torvalds 3351cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 33521da177e4SLinus Torvalds { 3353cc314eefSLinus Torvalds struct page *page = NULL; 33541da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 3355cc314eefSLinus Torvalds return page; 33561da177e4SLinus Torvalds } 33571da177e4SLinus Torvalds 3358cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 33591da177e4SLinus Torvalds { 3360cc314eefSLinus Torvalds struct page *page = cookie; 3361cc314eefSLinus Torvalds 3362cc314eefSLinus Torvalds if (page) { 33631da177e4SLinus Torvalds kunmap(page); 33641da177e4SLinus Torvalds page_cache_release(page); 33651da177e4SLinus Torvalds } 33661da177e4SLinus Torvalds } 33671da177e4SLinus Torvalds 336854566b2cSNick Piggin /* 336954566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 337054566b2cSNick Piggin */ 337154566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 33721da177e4SLinus Torvalds { 33731da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 33740adb25d2SKirill Korotaev struct page *page; 3375afddba49SNick Piggin void *fsdata; 3376beb497abSDmitriy Monakhov int err; 33771da177e4SLinus Torvalds char *kaddr; 337854566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 337954566b2cSNick Piggin if (nofs) 338054566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 33811da177e4SLinus Torvalds 33827e53cac4SNeilBrown retry: 3383afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 338454566b2cSNick Piggin flags, &page, &fsdata); 33851da177e4SLinus Torvalds if (err) 3386afddba49SNick Piggin goto fail; 3387afddba49SNick Piggin 33881da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 33891da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 33901da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 3391afddba49SNick Piggin 3392afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 3393afddba49SNick Piggin page, fsdata); 33941da177e4SLinus Torvalds if (err < 0) 33951da177e4SLinus Torvalds goto fail; 3396afddba49SNick Piggin if (err < len-1) 3397afddba49SNick Piggin goto retry; 3398afddba49SNick Piggin 33991da177e4SLinus Torvalds mark_inode_dirty(inode); 34001da177e4SLinus Torvalds return 0; 34011da177e4SLinus Torvalds fail: 34021da177e4SLinus Torvalds return err; 34031da177e4SLinus Torvalds } 34041da177e4SLinus Torvalds 34050adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 34060adb25d2SKirill Korotaev { 34070adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 340854566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 34090adb25d2SKirill Korotaev } 34100adb25d2SKirill Korotaev 341192e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 34121da177e4SLinus Torvalds .readlink = generic_readlink, 34131da177e4SLinus Torvalds .follow_link = page_follow_link_light, 34141da177e4SLinus Torvalds .put_link = page_put_link, 34151da177e4SLinus Torvalds }; 34161da177e4SLinus Torvalds 34172d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 3418cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one); 34191da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 34201da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 34211da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 34221da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 34231da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 34241da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 34251da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 34261da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 34271da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 34280adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 34291da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 34301da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 3431c9c6cac0SAl Viro EXPORT_SYMBOL(kern_path_parent); 3432d1811465SAl Viro EXPORT_SYMBOL(kern_path); 343316f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 3434f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 34358c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission); 34361da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 34371da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 34381da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 34391da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 34401da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 34411da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 34421da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 34431da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 34441da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 34451da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 34461da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 34471da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 34481da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 34491da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 3450