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 { 1825909ccaaSLinus Torvalds umode_t 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 39419660af7SAl Viro /* 39531e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 39619660af7SAl Viro * Documentation/filesystems/path-lookup.txt). In situations when we can't 39719660af7SAl Viro * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab 39819660af7SAl Viro * normal reference counts on dentries and vfsmounts to transition to rcu-walk 39919660af7SAl Viro * mode. Refcounts are grabbed at the last known good point before rcu-walk 40019660af7SAl Viro * got stuck, so ref-walk may continue from there. If this is not successful 40119660af7SAl Viro * (eg. a seqcount has changed), then failure is returned and it's up to caller 40219660af7SAl Viro * to restart the path walk from the beginning in ref-walk mode. 40331e6b01fSNick Piggin */ 40431e6b01fSNick Piggin 40531e6b01fSNick Piggin /** 40619660af7SAl Viro * unlazy_walk - try to switch to ref-walk mode. 40719660af7SAl Viro * @nd: nameidata pathwalk data 40819660af7SAl Viro * @dentry: child of nd->path.dentry or NULL 40939191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 41031e6b01fSNick Piggin * 41119660af7SAl Viro * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry 41219660af7SAl Viro * for ref-walk mode. @dentry must be a path found by a do_lookup call on 41319660af7SAl Viro * @nd or NULL. Must be called from rcu-walk context. 41431e6b01fSNick Piggin */ 41519660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry) 41631e6b01fSNick Piggin { 41731e6b01fSNick Piggin struct fs_struct *fs = current->fs; 41831e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 4195b6ca027SAl Viro int want_root = 0; 42031e6b01fSNick Piggin 42131e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 4225b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 4235b6ca027SAl Viro want_root = 1; 42431e6b01fSNick Piggin spin_lock(&fs->lock); 42531e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 42631e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 42731e6b01fSNick Piggin goto err_root; 42831e6b01fSNick Piggin } 42931e6b01fSNick Piggin spin_lock(&parent->d_lock); 43019660af7SAl Viro if (!dentry) { 43119660af7SAl Viro if (!__d_rcu_to_refcount(parent, nd->seq)) 43219660af7SAl Viro goto err_parent; 43319660af7SAl Viro BUG_ON(nd->inode != parent->d_inode); 43419660af7SAl Viro } else { 43531e6b01fSNick Piggin spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); 43631e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 43719660af7SAl Viro goto err_child; 43831e6b01fSNick Piggin /* 43919660af7SAl Viro * If the sequence check on the child dentry passed, then 44019660af7SAl Viro * the child has not been removed from its parent. This 44119660af7SAl Viro * means the parent dentry must be valid and able to take 44219660af7SAl Viro * a reference at this point. 44331e6b01fSNick Piggin */ 44431e6b01fSNick Piggin BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); 44531e6b01fSNick Piggin BUG_ON(!parent->d_count); 44631e6b01fSNick Piggin parent->d_count++; 44731e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 44819660af7SAl Viro } 44931e6b01fSNick Piggin spin_unlock(&parent->d_lock); 4505b6ca027SAl Viro if (want_root) { 45131e6b01fSNick Piggin path_get(&nd->root); 45231e6b01fSNick Piggin spin_unlock(&fs->lock); 45331e6b01fSNick Piggin } 45431e6b01fSNick Piggin mntget(nd->path.mnt); 45531e6b01fSNick Piggin 45631e6b01fSNick Piggin rcu_read_unlock(); 45731e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 45831e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 45931e6b01fSNick Piggin return 0; 46019660af7SAl Viro 46119660af7SAl Viro err_child: 46231e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 46319660af7SAl Viro err_parent: 46431e6b01fSNick Piggin spin_unlock(&parent->d_lock); 46531e6b01fSNick Piggin err_root: 4665b6ca027SAl Viro if (want_root) 46731e6b01fSNick Piggin spin_unlock(&fs->lock); 46831e6b01fSNick Piggin return -ECHILD; 46931e6b01fSNick Piggin } 47031e6b01fSNick Piggin 47131e6b01fSNick Piggin /** 472834f2a4aSTrond Myklebust * release_open_intent - free up open intent resources 473834f2a4aSTrond Myklebust * @nd: pointer to nameidata 474834f2a4aSTrond Myklebust */ 475834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd) 476834f2a4aSTrond Myklebust { 4772dab5974SLinus Torvalds struct file *file = nd->intent.open.file; 4782dab5974SLinus Torvalds 4792dab5974SLinus Torvalds if (file && !IS_ERR(file)) { 4802dab5974SLinus Torvalds if (file->f_path.dentry == NULL) 4812dab5974SLinus Torvalds put_filp(file); 482834f2a4aSTrond Myklebust else 4832dab5974SLinus Torvalds fput(file); 4842dab5974SLinus Torvalds } 485834f2a4aSTrond Myklebust } 486834f2a4aSTrond Myklebust 487f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd) 48834286d66SNick Piggin { 489f60aef7eSAl Viro return dentry->d_op->d_revalidate(dentry, nd); 49034286d66SNick Piggin } 49134286d66SNick Piggin 492f5e1c1c1SAl Viro static struct dentry * 493bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd) 494bcdc5e01SIan Kent { 495f5e1c1c1SAl Viro int status = d_revalidate(dentry, nd); 496bcdc5e01SIan Kent if (unlikely(status <= 0)) { 497bcdc5e01SIan Kent /* 498bcdc5e01SIan Kent * The dentry failed validation. 499bcdc5e01SIan Kent * If d_revalidate returned 0 attempt to invalidate 500bcdc5e01SIan Kent * the dentry otherwise d_revalidate is asking us 501bcdc5e01SIan Kent * to return a fail status. 502bcdc5e01SIan Kent */ 50334286d66SNick Piggin if (status < 0) { 50434286d66SNick Piggin dput(dentry); 50534286d66SNick Piggin dentry = ERR_PTR(status); 506f5e1c1c1SAl Viro } else if (!d_invalidate(dentry)) { 507bcdc5e01SIan Kent dput(dentry); 508bcdc5e01SIan Kent dentry = NULL; 509bcdc5e01SIan Kent } 510bcdc5e01SIan Kent } 511f5e1c1c1SAl Viro return dentry; 512f5e1c1c1SAl Viro } 513f5e1c1c1SAl Viro 5149f1fafeeSAl Viro /** 5159f1fafeeSAl Viro * complete_walk - successful completion of path walk 5169f1fafeeSAl Viro * @nd: pointer nameidata 51739159de2SJeff Layton * 5189f1fafeeSAl Viro * If we had been in RCU mode, drop out of it and legitimize nd->path. 5199f1fafeeSAl Viro * Revalidate the final result, unless we'd already done that during 5209f1fafeeSAl Viro * the path walk or the filesystem doesn't ask for it. Return 0 on 5219f1fafeeSAl Viro * success, -error on failure. In case of failure caller does not 5229f1fafeeSAl Viro * need to drop nd->path. 52339159de2SJeff Layton */ 5249f1fafeeSAl Viro static int complete_walk(struct nameidata *nd) 52539159de2SJeff Layton { 52616c2cd71SAl Viro struct dentry *dentry = nd->path.dentry; 52739159de2SJeff Layton int status; 52839159de2SJeff Layton 5299f1fafeeSAl Viro if (nd->flags & LOOKUP_RCU) { 5309f1fafeeSAl Viro nd->flags &= ~LOOKUP_RCU; 5319f1fafeeSAl Viro if (!(nd->flags & LOOKUP_ROOT)) 5329f1fafeeSAl Viro nd->root.mnt = NULL; 5339f1fafeeSAl Viro spin_lock(&dentry->d_lock); 5349f1fafeeSAl Viro if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) { 5359f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 5369f1fafeeSAl Viro rcu_read_unlock(); 5379f1fafeeSAl Viro br_read_unlock(vfsmount_lock); 5389f1fafeeSAl Viro return -ECHILD; 5399f1fafeeSAl Viro } 5409f1fafeeSAl Viro BUG_ON(nd->inode != dentry->d_inode); 5419f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 5429f1fafeeSAl Viro mntget(nd->path.mnt); 5439f1fafeeSAl Viro rcu_read_unlock(); 5449f1fafeeSAl Viro br_read_unlock(vfsmount_lock); 5459f1fafeeSAl Viro } 5469f1fafeeSAl Viro 54716c2cd71SAl Viro if (likely(!(nd->flags & LOOKUP_JUMPED))) 54839159de2SJeff Layton return 0; 54939159de2SJeff Layton 55016c2cd71SAl Viro if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE))) 55116c2cd71SAl Viro return 0; 55216c2cd71SAl Viro 55316c2cd71SAl Viro if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))) 55416c2cd71SAl Viro return 0; 55516c2cd71SAl Viro 55616c2cd71SAl Viro /* Note: we do not d_invalidate() */ 55734286d66SNick Piggin status = d_revalidate(dentry, nd); 55839159de2SJeff Layton if (status > 0) 55939159de2SJeff Layton return 0; 56039159de2SJeff Layton 56116c2cd71SAl Viro if (!status) 56239159de2SJeff Layton status = -ESTALE; 56316c2cd71SAl Viro 5649f1fafeeSAl Viro path_put(&nd->path); 56539159de2SJeff Layton return status; 56639159de2SJeff Layton } 56739159de2SJeff Layton 56839159de2SJeff Layton /* 569b75b5086SAl Viro * Short-cut version of permission(), for calling on directories 570b75b5086SAl Viro * during pathname resolution. Combines parts of permission() 571b75b5086SAl Viro * and generic_permission(), and tests ONLY for MAY_EXEC permission. 5721da177e4SLinus Torvalds * 5731da177e4SLinus Torvalds * If appropriate, check DAC only. If not appropriate, or 574b75b5086SAl Viro * short-cut DAC fails, then call ->permission() to do more 5751da177e4SLinus Torvalds * complete permission check. 5761da177e4SLinus Torvalds */ 577b74c79e9SNick Piggin static inline int exec_permission(struct inode *inode, unsigned int flags) 5781da177e4SLinus Torvalds { 5795909ccaaSLinus Torvalds int ret; 580e795b717SSerge E. Hallyn struct user_namespace *ns = inode_userns(inode); 5811da177e4SLinus Torvalds 582cb9179eaSLinus Torvalds if (inode->i_op->permission) { 583b74c79e9SNick Piggin ret = inode->i_op->permission(inode, MAY_EXEC, flags); 584b74c79e9SNick Piggin } else { 585b74c79e9SNick Piggin ret = acl_permission_check(inode, MAY_EXEC, flags, 586b74c79e9SNick Piggin inode->i_op->check_acl); 587cb9179eaSLinus Torvalds } 588b74c79e9SNick Piggin if (likely(!ret)) 5891da177e4SLinus Torvalds goto ok; 590b74c79e9SNick Piggin if (ret == -ECHILD) 59131e6b01fSNick Piggin return ret; 5921da177e4SLinus Torvalds 593e795b717SSerge E. Hallyn if (ns_capable(ns, CAP_DAC_OVERRIDE) || 594e795b717SSerge E. Hallyn ns_capable(ns, CAP_DAC_READ_SEARCH)) 5951da177e4SLinus Torvalds goto ok; 5961da177e4SLinus Torvalds 5975909ccaaSLinus Torvalds return ret; 5981da177e4SLinus Torvalds ok: 599b74c79e9SNick Piggin return security_inode_exec_permission(inode, flags); 6001da177e4SLinus Torvalds } 6011da177e4SLinus Torvalds 6022a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 6032a737871SAl Viro { 604f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 605f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 6062a737871SAl Viro } 6072a737871SAl Viro 6086de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 6096de88d72SAl Viro 61031e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 61131e6b01fSNick Piggin { 61231e6b01fSNick Piggin if (!nd->root.mnt) { 61331e6b01fSNick Piggin struct fs_struct *fs = current->fs; 614c28cc364SNick Piggin unsigned seq; 615c28cc364SNick Piggin 616c28cc364SNick Piggin do { 617c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 61831e6b01fSNick Piggin nd->root = fs->root; 619c1530019STim Chen nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq); 620c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 62131e6b01fSNick Piggin } 62231e6b01fSNick Piggin } 62331e6b01fSNick Piggin 624f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 6251da177e4SLinus Torvalds { 62631e6b01fSNick Piggin int ret; 62731e6b01fSNick Piggin 6281da177e4SLinus Torvalds if (IS_ERR(link)) 6291da177e4SLinus Torvalds goto fail; 6301da177e4SLinus Torvalds 6311da177e4SLinus Torvalds if (*link == '/') { 6322a737871SAl Viro set_root(nd); 6331d957f9bSJan Blunck path_put(&nd->path); 6342a737871SAl Viro nd->path = nd->root; 6352a737871SAl Viro path_get(&nd->root); 63616c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 6371da177e4SLinus Torvalds } 63831e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 639b4091d5fSChristoph Hellwig 64031e6b01fSNick Piggin ret = link_path_walk(link, nd); 64131e6b01fSNick Piggin return ret; 6421da177e4SLinus Torvalds fail: 6431d957f9bSJan Blunck path_put(&nd->path); 6441da177e4SLinus Torvalds return PTR_ERR(link); 6451da177e4SLinus Torvalds } 6461da177e4SLinus Torvalds 6471d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 648051d3812SIan Kent { 649051d3812SIan Kent dput(path->dentry); 6504ac91378SJan Blunck if (path->mnt != nd->path.mnt) 651051d3812SIan Kent mntput(path->mnt); 652051d3812SIan Kent } 653051d3812SIan Kent 6547b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path, 6557b9337aaSNick Piggin struct nameidata *nd) 656051d3812SIan Kent { 65731e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 6584ac91378SJan Blunck dput(nd->path.dentry); 65931e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 6604ac91378SJan Blunck mntput(nd->path.mnt); 6619a229683SHuang Shijie } 66231e6b01fSNick Piggin nd->path.mnt = path->mnt; 6634ac91378SJan Blunck nd->path.dentry = path->dentry; 664051d3812SIan Kent } 665051d3812SIan Kent 666574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie) 667574197e0SAl Viro { 668574197e0SAl Viro struct inode *inode = link->dentry->d_inode; 669574197e0SAl Viro if (!IS_ERR(cookie) && inode->i_op->put_link) 670574197e0SAl Viro inode->i_op->put_link(link->dentry, nd, cookie); 671574197e0SAl Viro path_put(link); 672574197e0SAl Viro } 673574197e0SAl Viro 674def4af30SAl Viro static __always_inline int 675574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p) 6761da177e4SLinus Torvalds { 6771da177e4SLinus Torvalds int error; 6787b9337aaSNick Piggin struct dentry *dentry = link->dentry; 6791da177e4SLinus Torvalds 680844a3917SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 681844a3917SAl Viro 6820e794589SAl Viro if (link->mnt == nd->path.mnt) 6830e794589SAl Viro mntget(link->mnt); 6840e794589SAl Viro 685574197e0SAl Viro if (unlikely(current->total_link_count >= 40)) { 686574197e0SAl Viro *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */ 687574197e0SAl Viro path_put(&nd->path); 688574197e0SAl Viro return -ELOOP; 689574197e0SAl Viro } 690574197e0SAl Viro cond_resched(); 691574197e0SAl Viro current->total_link_count++; 692574197e0SAl Viro 6937b9337aaSNick Piggin touch_atime(link->mnt, dentry); 6941da177e4SLinus Torvalds nd_set_link(nd, NULL); 695cd4e91d3SAl Viro 69636f3b4f6SAl Viro error = security_inode_follow_link(link->dentry, nd); 69736f3b4f6SAl Viro if (error) { 69836f3b4f6SAl Viro *p = ERR_PTR(error); /* no ->put_link(), please */ 69936f3b4f6SAl Viro path_put(&nd->path); 70036f3b4f6SAl Viro return error; 70136f3b4f6SAl Viro } 70236f3b4f6SAl Viro 70386acdca1SAl Viro nd->last_type = LAST_BIND; 704def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 705def4af30SAl Viro error = PTR_ERR(*p); 706def4af30SAl Viro if (!IS_ERR(*p)) { 7071da177e4SLinus Torvalds char *s = nd_get_link(nd); 708cc314eefSLinus Torvalds error = 0; 7091da177e4SLinus Torvalds if (s) 7101da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 711bcda7652SAl Viro else if (nd->last_type == LAST_BIND) { 71216c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 713b21041d0SAl Viro nd->inode = nd->path.dentry->d_inode; 714b21041d0SAl Viro if (nd->inode->i_op->follow_link) { 715bcda7652SAl Viro /* stepped on a _really_ weird one */ 716bcda7652SAl Viro path_put(&nd->path); 717bcda7652SAl Viro error = -ELOOP; 718bcda7652SAl Viro } 719bcda7652SAl Viro } 7201da177e4SLinus Torvalds } 7211da177e4SLinus Torvalds return error; 7221da177e4SLinus Torvalds } 7231da177e4SLinus Torvalds 72431e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 72531e6b01fSNick Piggin { 72631e6b01fSNick Piggin struct vfsmount *parent; 72731e6b01fSNick Piggin struct dentry *mountpoint; 72831e6b01fSNick Piggin 72931e6b01fSNick Piggin parent = path->mnt->mnt_parent; 73031e6b01fSNick Piggin if (parent == path->mnt) 73131e6b01fSNick Piggin return 0; 73231e6b01fSNick Piggin mountpoint = path->mnt->mnt_mountpoint; 73331e6b01fSNick Piggin path->dentry = mountpoint; 73431e6b01fSNick Piggin path->mnt = parent; 73531e6b01fSNick Piggin return 1; 73631e6b01fSNick Piggin } 73731e6b01fSNick Piggin 738bab77ebfSAl Viro int follow_up(struct path *path) 7391da177e4SLinus Torvalds { 7401da177e4SLinus Torvalds struct vfsmount *parent; 7411da177e4SLinus Torvalds struct dentry *mountpoint; 74299b7db7bSNick Piggin 74399b7db7bSNick Piggin br_read_lock(vfsmount_lock); 744bab77ebfSAl Viro parent = path->mnt->mnt_parent; 745bab77ebfSAl Viro if (parent == path->mnt) { 74699b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 7471da177e4SLinus Torvalds return 0; 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds mntget(parent); 750bab77ebfSAl Viro mountpoint = dget(path->mnt->mnt_mountpoint); 75199b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 752bab77ebfSAl Viro dput(path->dentry); 753bab77ebfSAl Viro path->dentry = mountpoint; 754bab77ebfSAl Viro mntput(path->mnt); 755bab77ebfSAl Viro path->mnt = parent; 7561da177e4SLinus Torvalds return 1; 7571da177e4SLinus Torvalds } 7581da177e4SLinus Torvalds 759b5c84bf6SNick Piggin /* 7609875cf80SDavid Howells * Perform an automount 7619875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 7629875cf80SDavid Howells * were called with. 7631da177e4SLinus Torvalds */ 7649875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags, 7659875cf80SDavid Howells bool *need_mntput) 76631e6b01fSNick Piggin { 7679875cf80SDavid Howells struct vfsmount *mnt; 768ea5b778aSDavid Howells int err; 7699875cf80SDavid Howells 7709875cf80SDavid Howells if (!path->dentry->d_op || !path->dentry->d_op->d_automount) 7719875cf80SDavid Howells return -EREMOTE; 7729875cf80SDavid Howells 7736f45b656SDavid Howells /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT 7746f45b656SDavid Howells * and this is the terminal part of the path. 7756f45b656SDavid Howells */ 7766f45b656SDavid Howells if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE)) 7776f45b656SDavid Howells return -EISDIR; /* we actually want to stop here */ 7786f45b656SDavid Howells 7799875cf80SDavid Howells /* We want to mount if someone is trying to open/create a file of any 7809875cf80SDavid Howells * type under the mountpoint, wants to traverse through the mountpoint 7819875cf80SDavid Howells * or wants to open the mounted directory. 7829875cf80SDavid Howells * 7839875cf80SDavid Howells * We don't want to mount if someone's just doing a stat and they've 7849875cf80SDavid Howells * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and 7859875cf80SDavid Howells * appended a '/' to the name. 7869875cf80SDavid Howells */ 7879875cf80SDavid Howells if (!(flags & LOOKUP_FOLLOW) && 7889875cf80SDavid Howells !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY | 7899875cf80SDavid Howells LOOKUP_OPEN | LOOKUP_CREATE))) 7909875cf80SDavid Howells return -EISDIR; 7919875cf80SDavid Howells 7929875cf80SDavid Howells current->total_link_count++; 7939875cf80SDavid Howells if (current->total_link_count >= 40) 7949875cf80SDavid Howells return -ELOOP; 7959875cf80SDavid Howells 7969875cf80SDavid Howells mnt = path->dentry->d_op->d_automount(path); 7979875cf80SDavid Howells if (IS_ERR(mnt)) { 7989875cf80SDavid Howells /* 7999875cf80SDavid Howells * The filesystem is allowed to return -EISDIR here to indicate 8009875cf80SDavid Howells * it doesn't want to automount. For instance, autofs would do 8019875cf80SDavid Howells * this so that its userspace daemon can mount on this dentry. 8029875cf80SDavid Howells * 8039875cf80SDavid Howells * However, we can only permit this if it's a terminal point in 8049875cf80SDavid Howells * the path being looked up; if it wasn't then the remainder of 8059875cf80SDavid Howells * the path is inaccessible and we should say so. 8069875cf80SDavid Howells */ 8079875cf80SDavid Howells if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE)) 8089875cf80SDavid Howells return -EREMOTE; 8099875cf80SDavid Howells return PTR_ERR(mnt); 81031e6b01fSNick Piggin } 811ea5b778aSDavid Howells 8129875cf80SDavid Howells if (!mnt) /* mount collision */ 8139875cf80SDavid Howells return 0; 8149875cf80SDavid Howells 81519a167afSAl Viro err = finish_automount(mnt, path); 816ea5b778aSDavid Howells 817ea5b778aSDavid Howells switch (err) { 818ea5b778aSDavid Howells case -EBUSY: 819ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 82019a167afSAl Viro return 0; 821ea5b778aSDavid Howells case 0: 822463ffb2eSAl Viro dput(path->dentry); 8239875cf80SDavid Howells if (*need_mntput) 8249875cf80SDavid Howells mntput(path->mnt); 8259875cf80SDavid Howells path->mnt = mnt; 8269875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 8279875cf80SDavid Howells *need_mntput = true; 8289875cf80SDavid Howells return 0; 82919a167afSAl Viro default: 83019a167afSAl Viro return err; 8319875cf80SDavid Howells } 83219a167afSAl Viro 833ea5b778aSDavid Howells } 8349875cf80SDavid Howells 8359875cf80SDavid Howells /* 8369875cf80SDavid Howells * Handle a dentry that is managed in some way. 837cc53ce53SDavid Howells * - Flagged for transit management (autofs) 8389875cf80SDavid Howells * - Flagged as mountpoint 8399875cf80SDavid Howells * - Flagged as automount point 8409875cf80SDavid Howells * 8419875cf80SDavid Howells * This may only be called in refwalk mode. 8429875cf80SDavid Howells * 8439875cf80SDavid Howells * Serialization is taken care of in namespace.c 8449875cf80SDavid Howells */ 8459875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 8469875cf80SDavid Howells { 8479875cf80SDavid Howells unsigned managed; 8489875cf80SDavid Howells bool need_mntput = false; 8499875cf80SDavid Howells int ret; 8509875cf80SDavid Howells 8519875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 8529875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 8539875cf80SDavid Howells * the components of that value change under us */ 8549875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 8559875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 8569875cf80SDavid Howells unlikely(managed != 0)) { 857cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 858cc53ce53SDavid Howells * being held. */ 859cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 860cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 861cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 8621aed3e42SAl Viro ret = path->dentry->d_op->d_manage(path->dentry, false); 863cc53ce53SDavid Howells if (ret < 0) 864cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 865cc53ce53SDavid Howells } 866cc53ce53SDavid Howells 8679875cf80SDavid Howells /* Transit to a mounted filesystem. */ 8689875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 8699875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 8709875cf80SDavid Howells if (mounted) { 8719875cf80SDavid Howells dput(path->dentry); 8729875cf80SDavid Howells if (need_mntput) 873463ffb2eSAl Viro mntput(path->mnt); 874463ffb2eSAl Viro path->mnt = mounted; 875463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 8769875cf80SDavid Howells need_mntput = true; 8779875cf80SDavid Howells continue; 878463ffb2eSAl Viro } 879463ffb2eSAl Viro 8809875cf80SDavid Howells /* Something is mounted on this dentry in another 8819875cf80SDavid Howells * namespace and/or whatever was mounted there in this 8829875cf80SDavid Howells * namespace got unmounted before we managed to get the 8839875cf80SDavid Howells * vfsmount_lock */ 8841da177e4SLinus Torvalds } 8859875cf80SDavid Howells 8869875cf80SDavid Howells /* Handle an automount point */ 8879875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 8889875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 8899875cf80SDavid Howells if (ret < 0) 8909875cf80SDavid Howells return ret == -EISDIR ? 0 : ret; 8919875cf80SDavid Howells continue; 8929875cf80SDavid Howells } 8939875cf80SDavid Howells 8949875cf80SDavid Howells /* We didn't change the current path point */ 8959875cf80SDavid Howells break; 8969875cf80SDavid Howells } 8979875cf80SDavid Howells return 0; 8981da177e4SLinus Torvalds } 8991da177e4SLinus Torvalds 900cc53ce53SDavid Howells int follow_down_one(struct path *path) 9011da177e4SLinus Torvalds { 9021da177e4SLinus Torvalds struct vfsmount *mounted; 9031da177e4SLinus Torvalds 9041c755af4SAl Viro mounted = lookup_mnt(path); 9051da177e4SLinus Torvalds if (mounted) { 9069393bd07SAl Viro dput(path->dentry); 9079393bd07SAl Viro mntput(path->mnt); 9089393bd07SAl Viro path->mnt = mounted; 9099393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 9101da177e4SLinus Torvalds return 1; 9111da177e4SLinus Torvalds } 9121da177e4SLinus Torvalds return 0; 9131da177e4SLinus Torvalds } 9141da177e4SLinus Torvalds 91562a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry) 91662a7375eSIan Kent { 91762a7375eSIan Kent return (dentry->d_flags & DCACHE_MANAGE_TRANSIT && 91862a7375eSIan Kent dentry->d_op->d_manage(dentry, true) < 0); 91962a7375eSIan Kent } 92062a7375eSIan Kent 9219875cf80SDavid Howells /* 9229875cf80SDavid Howells * Skip to top of mountpoint pile in rcuwalk mode. We abort the rcu-walk if we 923cc53ce53SDavid Howells * meet a managed dentry and we're not walking to "..". True is returned to 9249875cf80SDavid Howells * continue, false to abort. 9259875cf80SDavid Howells */ 9269875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 9279875cf80SDavid Howells struct inode **inode, bool reverse_transit) 9289875cf80SDavid Howells { 92962a7375eSIan Kent for (;;) { 9309875cf80SDavid Howells struct vfsmount *mounted; 93162a7375eSIan Kent /* 93262a7375eSIan Kent * Don't forget we might have a non-mountpoint managed dentry 93362a7375eSIan Kent * that wants to block transit. 93462a7375eSIan Kent */ 93562a7375eSIan Kent *inode = path->dentry->d_inode; 93662a7375eSIan Kent if (!reverse_transit && 93762a7375eSIan Kent unlikely(managed_dentry_might_block(path->dentry))) 938ab90911fSDavid Howells return false; 93962a7375eSIan Kent 94062a7375eSIan Kent if (!d_mountpoint(path->dentry)) 94162a7375eSIan Kent break; 94262a7375eSIan Kent 9439875cf80SDavid Howells mounted = __lookup_mnt(path->mnt, path->dentry, 1); 9449875cf80SDavid Howells if (!mounted) 9459875cf80SDavid Howells break; 9469875cf80SDavid Howells path->mnt = mounted; 9479875cf80SDavid Howells path->dentry = mounted->mnt_root; 9489875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 9499875cf80SDavid Howells } 9509875cf80SDavid Howells 9519875cf80SDavid Howells if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 9529875cf80SDavid Howells return reverse_transit; 9539875cf80SDavid Howells return true; 9549875cf80SDavid Howells } 9559875cf80SDavid Howells 95631e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 95731e6b01fSNick Piggin { 95831e6b01fSNick Piggin struct inode *inode = nd->inode; 95931e6b01fSNick Piggin 96031e6b01fSNick Piggin set_root_rcu(nd); 96131e6b01fSNick Piggin 96231e6b01fSNick Piggin while (1) { 96331e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 96431e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 96531e6b01fSNick Piggin break; 96631e6b01fSNick Piggin } 96731e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 96831e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 96931e6b01fSNick Piggin struct dentry *parent = old->d_parent; 97031e6b01fSNick Piggin unsigned seq; 97131e6b01fSNick Piggin 97231e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 97331e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 974ef7562d5SAl Viro goto failed; 97531e6b01fSNick Piggin inode = parent->d_inode; 97631e6b01fSNick Piggin nd->path.dentry = parent; 97731e6b01fSNick Piggin nd->seq = seq; 97831e6b01fSNick Piggin break; 97931e6b01fSNick Piggin } 98031e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 98131e6b01fSNick Piggin break; 98231e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 98331e6b01fSNick Piggin inode = nd->path.dentry->d_inode; 98431e6b01fSNick Piggin } 9859875cf80SDavid Howells __follow_mount_rcu(nd, &nd->path, &inode, true); 98631e6b01fSNick Piggin nd->inode = inode; 98731e6b01fSNick Piggin return 0; 988ef7562d5SAl Viro 989ef7562d5SAl Viro failed: 990ef7562d5SAl Viro nd->flags &= ~LOOKUP_RCU; 9915b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 992ef7562d5SAl Viro nd->root.mnt = NULL; 993ef7562d5SAl Viro rcu_read_unlock(); 994ef7562d5SAl Viro br_read_unlock(vfsmount_lock); 995ef7562d5SAl Viro return -ECHILD; 99631e6b01fSNick Piggin } 99731e6b01fSNick Piggin 9989875cf80SDavid Howells /* 999cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 1000cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 1001cc53ce53SDavid Howells * caller is permitted to proceed or not. 1002cc53ce53SDavid Howells * 1003cc53ce53SDavid Howells * Care must be taken as namespace_sem may be held (indicated by mounting_here 1004cc53ce53SDavid Howells * being true). 1005cc53ce53SDavid Howells */ 10067cc90cc3SAl Viro int follow_down(struct path *path) 1007cc53ce53SDavid Howells { 1008cc53ce53SDavid Howells unsigned managed; 1009cc53ce53SDavid Howells int ret; 1010cc53ce53SDavid Howells 1011cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 1012cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 1013cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 1014cc53ce53SDavid Howells * being held. 1015cc53ce53SDavid Howells * 1016cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 1017cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 1018cc53ce53SDavid Howells * other than its daemon the right to mount on its 1019cc53ce53SDavid Howells * superstructure. 1020cc53ce53SDavid Howells * 1021cc53ce53SDavid Howells * The filesystem may sleep at this point. 1022cc53ce53SDavid Howells */ 1023cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 1024cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 1025cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 1026ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 10271aed3e42SAl Viro path->dentry, false); 1028cc53ce53SDavid Howells if (ret < 0) 1029cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 1030cc53ce53SDavid Howells } 1031cc53ce53SDavid Howells 1032cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 1033cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 1034cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 1035cc53ce53SDavid Howells if (!mounted) 1036cc53ce53SDavid Howells break; 1037cc53ce53SDavid Howells dput(path->dentry); 1038cc53ce53SDavid Howells mntput(path->mnt); 1039cc53ce53SDavid Howells path->mnt = mounted; 1040cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 1041cc53ce53SDavid Howells continue; 1042cc53ce53SDavid Howells } 1043cc53ce53SDavid Howells 1044cc53ce53SDavid Howells /* Don't handle automount points here */ 1045cc53ce53SDavid Howells break; 1046cc53ce53SDavid Howells } 1047cc53ce53SDavid Howells return 0; 1048cc53ce53SDavid Howells } 1049cc53ce53SDavid Howells 1050cc53ce53SDavid Howells /* 10519875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 10529875cf80SDavid Howells */ 10539875cf80SDavid Howells static void follow_mount(struct path *path) 10549875cf80SDavid Howells { 10559875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 10569875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 10579875cf80SDavid Howells if (!mounted) 10589875cf80SDavid Howells break; 10599875cf80SDavid Howells dput(path->dentry); 10609875cf80SDavid Howells mntput(path->mnt); 10619875cf80SDavid Howells path->mnt = mounted; 10629875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 10639875cf80SDavid Howells } 10649875cf80SDavid Howells } 10659875cf80SDavid Howells 106631e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 10671da177e4SLinus Torvalds { 10682a737871SAl Viro set_root(nd); 1069e518ddb7SAndreas Mohr 10701da177e4SLinus Torvalds while(1) { 10714ac91378SJan Blunck struct dentry *old = nd->path.dentry; 10721da177e4SLinus Torvalds 10732a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 10742a737871SAl Viro nd->path.mnt == nd->root.mnt) { 10751da177e4SLinus Torvalds break; 10761da177e4SLinus Torvalds } 10774ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 10783088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 10793088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 10801da177e4SLinus Torvalds dput(old); 10811da177e4SLinus Torvalds break; 10821da177e4SLinus Torvalds } 10833088dd70SAl Viro if (!follow_up(&nd->path)) 10841da177e4SLinus Torvalds break; 10851da177e4SLinus Torvalds } 108679ed0226SAl Viro follow_mount(&nd->path); 108731e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 10881da177e4SLinus Torvalds } 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds /* 1091baa03890SNick Piggin * Allocate a dentry with name and parent, and perform a parent 1092baa03890SNick Piggin * directory ->lookup on it. Returns the new dentry, or ERR_PTR 1093baa03890SNick Piggin * on error. parent->d_inode->i_mutex must be held. d_lookup must 1094baa03890SNick Piggin * have verified that no child exists while under i_mutex. 1095baa03890SNick Piggin */ 1096baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent, 1097baa03890SNick Piggin struct qstr *name, struct nameidata *nd) 1098baa03890SNick Piggin { 1099baa03890SNick Piggin struct inode *inode = parent->d_inode; 1100baa03890SNick Piggin struct dentry *dentry; 1101baa03890SNick Piggin struct dentry *old; 1102baa03890SNick Piggin 1103baa03890SNick Piggin /* Don't create child dentry for a dead directory. */ 1104baa03890SNick Piggin if (unlikely(IS_DEADDIR(inode))) 1105baa03890SNick Piggin return ERR_PTR(-ENOENT); 1106baa03890SNick Piggin 1107baa03890SNick Piggin dentry = d_alloc(parent, name); 1108baa03890SNick Piggin if (unlikely(!dentry)) 1109baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1110baa03890SNick Piggin 1111baa03890SNick Piggin old = inode->i_op->lookup(inode, dentry, nd); 1112baa03890SNick Piggin if (unlikely(old)) { 1113baa03890SNick Piggin dput(dentry); 1114baa03890SNick Piggin dentry = old; 1115baa03890SNick Piggin } 1116baa03890SNick Piggin return dentry; 1117baa03890SNick Piggin } 1118baa03890SNick Piggin 1119baa03890SNick Piggin /* 11201da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 11211da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 11221da177e4SLinus Torvalds * It _is_ time-critical. 11231da177e4SLinus Torvalds */ 11241da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name, 112531e6b01fSNick Piggin struct path *path, struct inode **inode) 11261da177e4SLinus Torvalds { 11274ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 112831e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 11295a18fff2SAl Viro int need_reval = 1; 11305a18fff2SAl Viro int status = 1; 11319875cf80SDavid Howells int err; 11329875cf80SDavid Howells 11333cac260aSAl Viro /* 1134b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1135b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1136b04f784eSNick Piggin * do the non-racy lookup, below. 1137b04f784eSNick Piggin */ 113831e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 113931e6b01fSNick Piggin unsigned seq; 114031e6b01fSNick Piggin *inode = nd->inode; 114131e6b01fSNick Piggin dentry = __d_lookup_rcu(parent, name, &seq, inode); 11425a18fff2SAl Viro if (!dentry) 11435a18fff2SAl Viro goto unlazy; 11445a18fff2SAl Viro 114531e6b01fSNick Piggin /* Memory barrier in read_seqcount_begin of child is enough */ 114631e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 114731e6b01fSNick Piggin return -ECHILD; 114831e6b01fSNick Piggin nd->seq = seq; 11495a18fff2SAl Viro 115024643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 11515a18fff2SAl Viro status = d_revalidate(dentry, nd); 11525a18fff2SAl Viro if (unlikely(status <= 0)) { 11535a18fff2SAl Viro if (status != -ECHILD) 11545a18fff2SAl Viro need_reval = 0; 11555a18fff2SAl Viro goto unlazy; 11565a18fff2SAl Viro } 115724643087SAl Viro } 115831e6b01fSNick Piggin path->mnt = mnt; 115931e6b01fSNick Piggin path->dentry = dentry; 11609875cf80SDavid Howells if (likely(__follow_mount_rcu(nd, path, inode, false))) 11619875cf80SDavid Howells return 0; 11625a18fff2SAl Viro unlazy: 116319660af7SAl Viro if (unlazy_walk(nd, dentry)) 11645a18fff2SAl Viro return -ECHILD; 11655a18fff2SAl Viro } else { 116631e6b01fSNick Piggin dentry = __d_lookup(parent, name); 116724643087SAl Viro } 11685a18fff2SAl Viro 11695a18fff2SAl Viro retry: 11705a18fff2SAl Viro if (unlikely(!dentry)) { 11715a18fff2SAl Viro struct inode *dir = parent->d_inode; 11725a18fff2SAl Viro BUG_ON(nd->inode != dir); 11735a18fff2SAl Viro 11745a18fff2SAl Viro mutex_lock(&dir->i_mutex); 11755a18fff2SAl Viro dentry = d_lookup(parent, name); 11765a18fff2SAl Viro if (likely(!dentry)) { 11775a18fff2SAl Viro dentry = d_alloc_and_lookup(parent, name, nd); 11785a18fff2SAl Viro if (IS_ERR(dentry)) { 11795a18fff2SAl Viro mutex_unlock(&dir->i_mutex); 11805a18fff2SAl Viro return PTR_ERR(dentry); 11815a18fff2SAl Viro } 11825a18fff2SAl Viro /* known good */ 11835a18fff2SAl Viro need_reval = 0; 11845a18fff2SAl Viro status = 1; 11855a18fff2SAl Viro } 11865a18fff2SAl Viro mutex_unlock(&dir->i_mutex); 11875a18fff2SAl Viro } 11885a18fff2SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) 11895a18fff2SAl Viro status = d_revalidate(dentry, nd); 11905a18fff2SAl Viro if (unlikely(status <= 0)) { 11915a18fff2SAl Viro if (status < 0) { 11925a18fff2SAl Viro dput(dentry); 11935a18fff2SAl Viro return status; 11945a18fff2SAl Viro } 11955a18fff2SAl Viro if (!d_invalidate(dentry)) { 11965a18fff2SAl Viro dput(dentry); 11975a18fff2SAl Viro dentry = NULL; 11985a18fff2SAl Viro need_reval = 1; 11995a18fff2SAl Viro goto retry; 12005a18fff2SAl Viro } 12015a18fff2SAl Viro } 12025a18fff2SAl Viro 12031da177e4SLinus Torvalds path->mnt = mnt; 12041da177e4SLinus Torvalds path->dentry = dentry; 12059875cf80SDavid Howells err = follow_managed(path, nd->flags); 120689312214SIan Kent if (unlikely(err < 0)) { 120789312214SIan Kent path_put_conditional(path, nd); 12089875cf80SDavid Howells return err; 120989312214SIan Kent } 121031e6b01fSNick Piggin *inode = path->dentry->d_inode; 12111da177e4SLinus Torvalds return 0; 12121da177e4SLinus Torvalds } 12131da177e4SLinus Torvalds 121452094c8aSAl Viro static inline int may_lookup(struct nameidata *nd) 121552094c8aSAl Viro { 121652094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 121752094c8aSAl Viro int err = exec_permission(nd->inode, IPERM_FLAG_RCU); 121852094c8aSAl Viro if (err != -ECHILD) 121952094c8aSAl Viro return err; 122019660af7SAl Viro if (unlazy_walk(nd, NULL)) 122152094c8aSAl Viro return -ECHILD; 122252094c8aSAl Viro } 122352094c8aSAl Viro return exec_permission(nd->inode, 0); 122452094c8aSAl Viro } 122552094c8aSAl Viro 12269856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type) 12279856fa1bSAl Viro { 12289856fa1bSAl Viro if (type == LAST_DOTDOT) { 12299856fa1bSAl Viro if (nd->flags & LOOKUP_RCU) { 12309856fa1bSAl Viro if (follow_dotdot_rcu(nd)) 12319856fa1bSAl Viro return -ECHILD; 12329856fa1bSAl Viro } else 12339856fa1bSAl Viro follow_dotdot(nd); 12349856fa1bSAl Viro } 12359856fa1bSAl Viro return 0; 12369856fa1bSAl Viro } 12379856fa1bSAl Viro 1238951361f9SAl Viro static void terminate_walk(struct nameidata *nd) 1239951361f9SAl Viro { 1240951361f9SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1241951361f9SAl Viro path_put(&nd->path); 1242951361f9SAl Viro } else { 1243951361f9SAl Viro nd->flags &= ~LOOKUP_RCU; 12445b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1245951361f9SAl Viro nd->root.mnt = NULL; 1246951361f9SAl Viro rcu_read_unlock(); 1247951361f9SAl Viro br_read_unlock(vfsmount_lock); 1248951361f9SAl Viro } 1249951361f9SAl Viro } 1250951361f9SAl Viro 1251ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path, 1252ce57dfc1SAl Viro struct qstr *name, int type, int follow) 1253ce57dfc1SAl Viro { 1254ce57dfc1SAl Viro struct inode *inode; 1255ce57dfc1SAl Viro int err; 1256ce57dfc1SAl Viro /* 1257ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1258ce57dfc1SAl Viro * to be able to know about the current root directory and 1259ce57dfc1SAl Viro * parent relationships. 1260ce57dfc1SAl Viro */ 1261ce57dfc1SAl Viro if (unlikely(type != LAST_NORM)) 1262ce57dfc1SAl Viro return handle_dots(nd, type); 1263ce57dfc1SAl Viro err = do_lookup(nd, name, path, &inode); 1264ce57dfc1SAl Viro if (unlikely(err)) { 1265ce57dfc1SAl Viro terminate_walk(nd); 1266ce57dfc1SAl Viro return err; 1267ce57dfc1SAl Viro } 1268ce57dfc1SAl Viro if (!inode) { 1269ce57dfc1SAl Viro path_to_nameidata(path, nd); 1270ce57dfc1SAl Viro terminate_walk(nd); 1271ce57dfc1SAl Viro return -ENOENT; 1272ce57dfc1SAl Viro } 1273ce57dfc1SAl Viro if (unlikely(inode->i_op->follow_link) && follow) { 127419660af7SAl Viro if (nd->flags & LOOKUP_RCU) { 127519660af7SAl Viro if (unlikely(unlazy_walk(nd, path->dentry))) { 127619660af7SAl Viro terminate_walk(nd); 1277ce57dfc1SAl Viro return -ECHILD; 127819660af7SAl Viro } 127919660af7SAl Viro } 1280ce57dfc1SAl Viro BUG_ON(inode != path->dentry->d_inode); 1281ce57dfc1SAl Viro return 1; 1282ce57dfc1SAl Viro } 1283ce57dfc1SAl Viro path_to_nameidata(path, nd); 1284ce57dfc1SAl Viro nd->inode = inode; 1285ce57dfc1SAl Viro return 0; 1286ce57dfc1SAl Viro } 1287ce57dfc1SAl Viro 12881da177e4SLinus Torvalds /* 1289b356379aSAl Viro * This limits recursive symlink follows to 8, while 1290b356379aSAl Viro * limiting consecutive symlinks to 40. 1291b356379aSAl Viro * 1292b356379aSAl Viro * Without that kind of total limit, nasty chains of consecutive 1293b356379aSAl Viro * symlinks can cause almost arbitrarily long lookups. 1294b356379aSAl Viro */ 1295b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd) 1296b356379aSAl Viro { 1297b356379aSAl Viro int res; 1298b356379aSAl Viro 1299b356379aSAl Viro BUG_ON(nd->depth >= MAX_NESTED_LINKS); 1300b356379aSAl Viro if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { 1301b356379aSAl Viro path_put_conditional(path, nd); 1302b356379aSAl Viro path_put(&nd->path); 1303b356379aSAl Viro return -ELOOP; 1304b356379aSAl Viro } 1305b356379aSAl Viro 1306b356379aSAl Viro nd->depth++; 1307b356379aSAl Viro current->link_count++; 1308b356379aSAl Viro 1309b356379aSAl Viro do { 1310b356379aSAl Viro struct path link = *path; 1311b356379aSAl Viro void *cookie; 1312574197e0SAl Viro 1313574197e0SAl Viro res = follow_link(&link, nd, &cookie); 1314b356379aSAl Viro if (!res) 1315b356379aSAl Viro res = walk_component(nd, path, &nd->last, 1316b356379aSAl Viro nd->last_type, LOOKUP_FOLLOW); 1317574197e0SAl Viro put_link(nd, &link, cookie); 1318b356379aSAl Viro } while (res > 0); 1319b356379aSAl Viro 1320b356379aSAl Viro current->link_count--; 1321b356379aSAl Viro nd->depth--; 1322b356379aSAl Viro return res; 1323b356379aSAl Viro } 1324b356379aSAl Viro 1325b356379aSAl Viro /* 13261da177e4SLinus Torvalds * Name resolution. 1327ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1328ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 13291da177e4SLinus Torvalds * 1330ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1331ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 13321da177e4SLinus Torvalds */ 13336de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 13341da177e4SLinus Torvalds { 13351da177e4SLinus Torvalds struct path next; 13361da177e4SLinus Torvalds int err; 13371da177e4SLinus Torvalds unsigned int lookup_flags = nd->flags; 13381da177e4SLinus Torvalds 13391da177e4SLinus Torvalds while (*name=='/') 13401da177e4SLinus Torvalds name++; 13411da177e4SLinus Torvalds if (!*name) 1342086e183aSAl Viro return 0; 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 13451da177e4SLinus Torvalds for(;;) { 13461da177e4SLinus Torvalds unsigned long hash; 13471da177e4SLinus Torvalds struct qstr this; 13481da177e4SLinus Torvalds unsigned int c; 1349fe479a58SAl Viro int type; 13501da177e4SLinus Torvalds 1351cdce5d6bSTrond Myklebust nd->flags |= LOOKUP_CONTINUE; 135252094c8aSAl Viro 135352094c8aSAl Viro err = may_lookup(nd); 13541da177e4SLinus Torvalds if (err) 13551da177e4SLinus Torvalds break; 13561da177e4SLinus Torvalds 13571da177e4SLinus Torvalds this.name = name; 13581da177e4SLinus Torvalds c = *(const unsigned char *)name; 13591da177e4SLinus Torvalds 13601da177e4SLinus Torvalds hash = init_name_hash(); 13611da177e4SLinus Torvalds do { 13621da177e4SLinus Torvalds name++; 13631da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 13641da177e4SLinus Torvalds c = *(const unsigned char *)name; 13651da177e4SLinus Torvalds } while (c && (c != '/')); 13661da177e4SLinus Torvalds this.len = name - (const char *) this.name; 13671da177e4SLinus Torvalds this.hash = end_name_hash(hash); 13681da177e4SLinus Torvalds 1369fe479a58SAl Viro type = LAST_NORM; 1370fe479a58SAl Viro if (this.name[0] == '.') switch (this.len) { 1371fe479a58SAl Viro case 2: 137216c2cd71SAl Viro if (this.name[1] == '.') { 1373fe479a58SAl Viro type = LAST_DOTDOT; 137416c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 137516c2cd71SAl Viro } 1376fe479a58SAl Viro break; 1377fe479a58SAl Viro case 1: 1378fe479a58SAl Viro type = LAST_DOT; 1379fe479a58SAl Viro } 13805a202bcdSAl Viro if (likely(type == LAST_NORM)) { 13815a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 138216c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 13835a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 13845a202bcdSAl Viro err = parent->d_op->d_hash(parent, nd->inode, 13855a202bcdSAl Viro &this); 13865a202bcdSAl Viro if (err < 0) 13875a202bcdSAl Viro break; 13885a202bcdSAl Viro } 13895a202bcdSAl Viro } 1390fe479a58SAl Viro 13911da177e4SLinus Torvalds /* remove trailing slashes? */ 13921da177e4SLinus Torvalds if (!c) 13931da177e4SLinus Torvalds goto last_component; 13941da177e4SLinus Torvalds while (*++name == '/'); 13951da177e4SLinus Torvalds if (!*name) 1396b356379aSAl Viro goto last_component; 13971da177e4SLinus Torvalds 1398ce57dfc1SAl Viro err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW); 1399ce57dfc1SAl Viro if (err < 0) 1400ce57dfc1SAl Viro return err; 1401fe479a58SAl Viro 1402ce57dfc1SAl Viro if (err) { 1403b356379aSAl Viro err = nested_symlink(&next, nd); 14041da177e4SLinus Torvalds if (err) 1405a7472babSAl Viro return err; 140631e6b01fSNick Piggin } 14071da177e4SLinus Torvalds err = -ENOTDIR; 140831e6b01fSNick Piggin if (!nd->inode->i_op->lookup) 14091da177e4SLinus Torvalds break; 14101da177e4SLinus Torvalds continue; 14111da177e4SLinus Torvalds /* here ends the main loop */ 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds last_component: 1414f55eab82STrond Myklebust /* Clear LOOKUP_CONTINUE iff it was previously unset */ 1415f55eab82STrond Myklebust nd->flags &= lookup_flags | ~LOOKUP_CONTINUE; 1416ce57dfc1SAl Viro nd->last = this; 1417ce57dfc1SAl Viro nd->last_type = type; 1418ce57dfc1SAl Viro return 0; 1419ce57dfc1SAl Viro } 1420951361f9SAl Viro terminate_walk(nd); 14211da177e4SLinus Torvalds return err; 14221da177e4SLinus Torvalds } 14231da177e4SLinus Torvalds 142470e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, 142570e9b357SAl Viro struct nameidata *nd, struct file **fp) 142631e6b01fSNick Piggin { 142731e6b01fSNick Piggin int retval = 0; 142831e6b01fSNick Piggin int fput_needed; 142931e6b01fSNick Piggin struct file *file; 143031e6b01fSNick Piggin 143131e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 143216c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 143331e6b01fSNick Piggin nd->depth = 0; 14345b6ca027SAl Viro if (flags & LOOKUP_ROOT) { 14355b6ca027SAl Viro struct inode *inode = nd->root.dentry->d_inode; 143673d049a4SAl Viro if (*name) { 14375b6ca027SAl Viro if (!inode->i_op->lookup) 14385b6ca027SAl Viro return -ENOTDIR; 14395b6ca027SAl Viro retval = inode_permission(inode, MAY_EXEC); 14405b6ca027SAl Viro if (retval) 14415b6ca027SAl Viro return retval; 144273d049a4SAl Viro } 14435b6ca027SAl Viro nd->path = nd->root; 14445b6ca027SAl Viro nd->inode = inode; 14455b6ca027SAl Viro if (flags & LOOKUP_RCU) { 14465b6ca027SAl Viro br_read_lock(vfsmount_lock); 14475b6ca027SAl Viro rcu_read_lock(); 14485b6ca027SAl Viro nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 14495b6ca027SAl Viro } else { 14505b6ca027SAl Viro path_get(&nd->path); 14515b6ca027SAl Viro } 14525b6ca027SAl Viro return 0; 14535b6ca027SAl Viro } 14545b6ca027SAl Viro 145531e6b01fSNick Piggin nd->root.mnt = NULL; 145631e6b01fSNick Piggin 145731e6b01fSNick Piggin if (*name=='/') { 1458e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 145931e6b01fSNick Piggin br_read_lock(vfsmount_lock); 146031e6b01fSNick Piggin rcu_read_lock(); 1461e41f7d4eSAl Viro set_root_rcu(nd); 1462e41f7d4eSAl Viro } else { 1463e41f7d4eSAl Viro set_root(nd); 1464e41f7d4eSAl Viro path_get(&nd->root); 1465e41f7d4eSAl Viro } 146631e6b01fSNick Piggin nd->path = nd->root; 146731e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 1468e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 146931e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1470c28cc364SNick Piggin unsigned seq; 147131e6b01fSNick Piggin 147231e6b01fSNick Piggin br_read_lock(vfsmount_lock); 147331e6b01fSNick Piggin rcu_read_lock(); 147431e6b01fSNick Piggin 1475c28cc364SNick Piggin do { 1476c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 147731e6b01fSNick Piggin nd->path = fs->pwd; 1478c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1479c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1480e41f7d4eSAl Viro } else { 1481e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 1482e41f7d4eSAl Viro } 148331e6b01fSNick Piggin } else { 148431e6b01fSNick Piggin struct dentry *dentry; 148531e6b01fSNick Piggin 14861abf0c71SAl Viro file = fget_raw_light(dfd, &fput_needed); 148731e6b01fSNick Piggin retval = -EBADF; 148831e6b01fSNick Piggin if (!file) 148931e6b01fSNick Piggin goto out_fail; 149031e6b01fSNick Piggin 149131e6b01fSNick Piggin dentry = file->f_path.dentry; 149231e6b01fSNick Piggin 1493f52e0c11SAl Viro if (*name) { 149431e6b01fSNick Piggin retval = -ENOTDIR; 149531e6b01fSNick Piggin if (!S_ISDIR(dentry->d_inode->i_mode)) 149631e6b01fSNick Piggin goto fput_fail; 149731e6b01fSNick Piggin 149831e6b01fSNick Piggin retval = file_permission(file, MAY_EXEC); 149931e6b01fSNick Piggin if (retval) 150031e6b01fSNick Piggin goto fput_fail; 1501f52e0c11SAl Viro } 150231e6b01fSNick Piggin 150331e6b01fSNick Piggin nd->path = file->f_path; 1504e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 150531e6b01fSNick Piggin if (fput_needed) 150670e9b357SAl Viro *fp = file; 1507c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 150831e6b01fSNick Piggin br_read_lock(vfsmount_lock); 150931e6b01fSNick Piggin rcu_read_lock(); 15105590ff0dSUlrich Drepper } else { 15115dd784d0SJan Blunck path_get(&file->f_path); 15125590ff0dSUlrich Drepper fput_light(file, fput_needed); 15131da177e4SLinus Torvalds } 1514e41f7d4eSAl Viro } 1515e41f7d4eSAl Viro 151631e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 15179b4a9b14SAl Viro return 0; 15182dfdd266SJosef 'Jeff' Sipek 15199b4a9b14SAl Viro fput_fail: 15209b4a9b14SAl Viro fput_light(file, fput_needed); 15219b4a9b14SAl Viro out_fail: 15229b4a9b14SAl Viro return retval; 15239b4a9b14SAl Viro } 15249b4a9b14SAl Viro 1525bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path) 1526bd92d7feSAl Viro { 1527bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 1528bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 1529bd92d7feSAl Viro 1530bd92d7feSAl Viro nd->flags &= ~LOOKUP_PARENT; 1531bd92d7feSAl Viro return walk_component(nd, path, &nd->last, nd->last_type, 1532bd92d7feSAl Viro nd->flags & LOOKUP_FOLLOW); 1533bd92d7feSAl Viro } 1534bd92d7feSAl Viro 15359b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1536ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name, 15379b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 15389b4a9b14SAl Viro { 153970e9b357SAl Viro struct file *base = NULL; 1540bd92d7feSAl Viro struct path path; 1541bd92d7feSAl Viro int err; 154231e6b01fSNick Piggin 154331e6b01fSNick Piggin /* 154431e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 154531e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 154631e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 154731e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 154831e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 154931e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 155031e6b01fSNick Piggin * analogue, foo_rcu(). 155131e6b01fSNick Piggin * 155231e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 155331e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 155431e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 155531e6b01fSNick Piggin * be able to complete). 155631e6b01fSNick Piggin */ 1557bd92d7feSAl Viro err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); 1558ee0827cdSAl Viro 1559bd92d7feSAl Viro if (unlikely(err)) 1560bd92d7feSAl Viro return err; 1561ee0827cdSAl Viro 1562ee0827cdSAl Viro current->total_link_count = 0; 1563bd92d7feSAl Viro err = link_path_walk(name, nd); 1564bd92d7feSAl Viro 1565bd92d7feSAl Viro if (!err && !(flags & LOOKUP_PARENT)) { 1566bd92d7feSAl Viro err = lookup_last(nd, &path); 1567bd92d7feSAl Viro while (err > 0) { 1568bd92d7feSAl Viro void *cookie; 1569bd92d7feSAl Viro struct path link = path; 1570bd92d7feSAl Viro nd->flags |= LOOKUP_PARENT; 1571574197e0SAl Viro err = follow_link(&link, nd, &cookie); 1572bd92d7feSAl Viro if (!err) 1573bd92d7feSAl Viro err = lookup_last(nd, &path); 1574574197e0SAl Viro put_link(nd, &link, cookie); 1575bd92d7feSAl Viro } 1576bd92d7feSAl Viro } 1577ee0827cdSAl Viro 15789f1fafeeSAl Viro if (!err) 15799f1fafeeSAl Viro err = complete_walk(nd); 1580bd92d7feSAl Viro 1581bd92d7feSAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) { 1582bd92d7feSAl Viro if (!nd->inode->i_op->lookup) { 1583bd92d7feSAl Viro path_put(&nd->path); 1584bd23a539SAl Viro err = -ENOTDIR; 1585bd92d7feSAl Viro } 1586bd92d7feSAl Viro } 158716c2cd71SAl Viro 158870e9b357SAl Viro if (base) 158970e9b357SAl Viro fput(base); 1590ee0827cdSAl Viro 15915b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 159231e6b01fSNick Piggin path_put(&nd->root); 159331e6b01fSNick Piggin nd->root.mnt = NULL; 159431e6b01fSNick Piggin } 1595bd92d7feSAl Viro return err; 159631e6b01fSNick Piggin } 159731e6b01fSNick Piggin 1598ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name, 1599ee0827cdSAl Viro unsigned int flags, struct nameidata *nd) 1600ee0827cdSAl Viro { 1601ee0827cdSAl Viro int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd); 1602ee0827cdSAl Viro if (unlikely(retval == -ECHILD)) 1603ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags, nd); 1604ee0827cdSAl Viro if (unlikely(retval == -ESTALE)) 1605ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd); 1606ee0827cdSAl Viro 160731e6b01fSNick Piggin if (likely(!retval)) { 160831e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) { 160931e6b01fSNick Piggin if (nd->path.dentry && nd->inode) 161031e6b01fSNick Piggin audit_inode(name, nd->path.dentry); 161131e6b01fSNick Piggin } 161231e6b01fSNick Piggin } 1613170aa3d0SUlrich Drepper return retval; 16141da177e4SLinus Torvalds } 16151da177e4SLinus Torvalds 1616c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd) 16175590ff0dSUlrich Drepper { 1618c9c6cac0SAl Viro return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd); 16195590ff0dSUlrich Drepper } 16205590ff0dSUlrich Drepper 1621d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1622d1811465SAl Viro { 1623d1811465SAl Viro struct nameidata nd; 1624d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1625d1811465SAl Viro if (!res) 1626d1811465SAl Viro *path = nd.path; 1627d1811465SAl Viro return res; 1628d1811465SAl Viro } 1629d1811465SAl Viro 163016f18200SJosef 'Jeff' Sipek /** 163116f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 163216f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 163316f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 163416f18200SJosef 'Jeff' Sipek * @name: pointer to file name 163516f18200SJosef 'Jeff' Sipek * @flags: lookup flags 163616f18200SJosef 'Jeff' Sipek * @nd: pointer to nameidata 163716f18200SJosef 'Jeff' Sipek */ 163816f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 163916f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 164016f18200SJosef 'Jeff' Sipek struct nameidata *nd) 164116f18200SJosef 'Jeff' Sipek { 16425b6ca027SAl Viro nd->root.dentry = dentry; 16435b6ca027SAl Viro nd->root.mnt = mnt; 16445b6ca027SAl Viro /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ 16455b6ca027SAl Viro return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd); 164616f18200SJosef 'Jeff' Sipek } 164716f18200SJosef 'Jeff' Sipek 1648eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name, 1649eead1911SChristoph Hellwig struct dentry *base, struct nameidata *nd) 16501da177e4SLinus Torvalds { 165181fca444SChristoph Hellwig struct inode *inode = base->d_inode; 16521da177e4SLinus Torvalds struct dentry *dentry; 16531da177e4SLinus Torvalds int err; 16541da177e4SLinus Torvalds 1655b74c79e9SNick Piggin err = exec_permission(inode, 0); 165681fca444SChristoph Hellwig if (err) 165781fca444SChristoph Hellwig return ERR_PTR(err); 16581da177e4SLinus Torvalds 16591da177e4SLinus Torvalds /* 1660b04f784eSNick Piggin * Don't bother with __d_lookup: callers are for creat as 1661b04f784eSNick Piggin * well as unlink, so a lot of the time it would cost 1662b04f784eSNick Piggin * a double lookup. 16636e6b1bd1SAl Viro */ 16646e6b1bd1SAl Viro dentry = d_lookup(base, name); 16656e6b1bd1SAl Viro 1666fb045adbSNick Piggin if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) 16676e6b1bd1SAl Viro dentry = do_revalidate(dentry, nd); 16686e6b1bd1SAl Viro 16691da177e4SLinus Torvalds if (!dentry) 1670baa03890SNick Piggin dentry = d_alloc_and_lookup(base, name, nd); 16715a202bcdSAl Viro 16721da177e4SLinus Torvalds return dentry; 16731da177e4SLinus Torvalds } 16741da177e4SLinus Torvalds 1675057f6c01SJames Morris /* 1676057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1677057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1678057f6c01SJames Morris * SMP-safe. 1679057f6c01SJames Morris */ 1680a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 16811da177e4SLinus Torvalds { 16824ac91378SJan Blunck return __lookup_hash(&nd->last, nd->path.dentry, nd); 16831da177e4SLinus Torvalds } 16841da177e4SLinus Torvalds 1685eead1911SChristoph Hellwig /** 1686a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1687eead1911SChristoph Hellwig * @name: pathname component to lookup 1688eead1911SChristoph Hellwig * @base: base directory to lookup from 1689eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1690eead1911SChristoph Hellwig * 1691a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1692a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1693eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1694eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1695eead1911SChristoph Hellwig */ 1696057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1697057f6c01SJames Morris { 1698057f6c01SJames Morris struct qstr this; 16996a96ba54SAl Viro unsigned long hash; 17006a96ba54SAl Viro unsigned int c; 1701057f6c01SJames Morris 17022f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 17032f9092e1SDavid Woodhouse 17046a96ba54SAl Viro this.name = name; 17056a96ba54SAl Viro this.len = len; 17066a96ba54SAl Viro if (!len) 17076a96ba54SAl Viro return ERR_PTR(-EACCES); 17086a96ba54SAl Viro 17096a96ba54SAl Viro hash = init_name_hash(); 17106a96ba54SAl Viro while (len--) { 17116a96ba54SAl Viro c = *(const unsigned char *)name++; 17126a96ba54SAl Viro if (c == '/' || c == '\0') 17136a96ba54SAl Viro return ERR_PTR(-EACCES); 17146a96ba54SAl Viro hash = partial_name_hash(c, hash); 17156a96ba54SAl Viro } 17166a96ba54SAl Viro this.hash = end_name_hash(hash); 17175a202bcdSAl Viro /* 17185a202bcdSAl Viro * See if the low-level filesystem might want 17195a202bcdSAl Viro * to use its own hash.. 17205a202bcdSAl Viro */ 17215a202bcdSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 17225a202bcdSAl Viro int err = base->d_op->d_hash(base, base->d_inode, &this); 17235a202bcdSAl Viro if (err < 0) 17245a202bcdSAl Viro return ERR_PTR(err); 17255a202bcdSAl Viro } 1726eead1911SChristoph Hellwig 172749705b77SChristoph Hellwig return __lookup_hash(&this, base, NULL); 1728057f6c01SJames Morris } 1729057f6c01SJames Morris 17302d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags, 17312d8f3038SAl Viro struct path *path) 17321da177e4SLinus Torvalds { 17332d8f3038SAl Viro struct nameidata nd; 1734f52e0c11SAl Viro char *tmp = getname_flags(name, flags); 17351da177e4SLinus Torvalds int err = PTR_ERR(tmp); 17361da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 17372d8f3038SAl Viro 17382d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 17392d8f3038SAl Viro 17402d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 17411da177e4SLinus Torvalds putname(tmp); 17422d8f3038SAl Viro if (!err) 17432d8f3038SAl Viro *path = nd.path; 17441da177e4SLinus Torvalds } 17451da177e4SLinus Torvalds return err; 17461da177e4SLinus Torvalds } 17471da177e4SLinus Torvalds 17482ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 17492ad94ae6SAl Viro struct nameidata *nd, char **name) 17502ad94ae6SAl Viro { 17512ad94ae6SAl Viro char *s = getname(path); 17522ad94ae6SAl Viro int error; 17532ad94ae6SAl Viro 17542ad94ae6SAl Viro if (IS_ERR(s)) 17552ad94ae6SAl Viro return PTR_ERR(s); 17562ad94ae6SAl Viro 17572ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 17582ad94ae6SAl Viro if (error) 17592ad94ae6SAl Viro putname(s); 17602ad94ae6SAl Viro else 17612ad94ae6SAl Viro *name = s; 17622ad94ae6SAl Viro 17632ad94ae6SAl Viro return error; 17642ad94ae6SAl Viro } 17652ad94ae6SAl Viro 17661da177e4SLinus Torvalds /* 17671da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 17681da177e4SLinus Torvalds * minimal. 17691da177e4SLinus Torvalds */ 17701da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 17711da177e4SLinus Torvalds { 1772da9592edSDavid Howells uid_t fsuid = current_fsuid(); 1773da9592edSDavid Howells 17741da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 17751da177e4SLinus Torvalds return 0; 1776e795b717SSerge E. Hallyn if (current_user_ns() != inode_userns(inode)) 1777e795b717SSerge E. Hallyn goto other_userns; 1778da9592edSDavid Howells if (inode->i_uid == fsuid) 17791da177e4SLinus Torvalds return 0; 1780da9592edSDavid Howells if (dir->i_uid == fsuid) 17811da177e4SLinus Torvalds return 0; 1782e795b717SSerge E. Hallyn 1783e795b717SSerge E. Hallyn other_userns: 1784e795b717SSerge E. Hallyn return !ns_capable(inode_userns(inode), CAP_FOWNER); 17851da177e4SLinus Torvalds } 17861da177e4SLinus Torvalds 17871da177e4SLinus Torvalds /* 17881da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 17891da177e4SLinus Torvalds * whether the type of victim is right. 17901da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 17911da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 17921da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 17931da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 17941da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 17951da177e4SLinus Torvalds * a. be owner of dir, or 17961da177e4SLinus Torvalds * b. be owner of victim, or 17971da177e4SLinus Torvalds * c. have CAP_FOWNER capability 17981da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 17991da177e4SLinus Torvalds * links pointing to it. 18001da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 18011da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 18021da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 18031da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 18041da177e4SLinus Torvalds * nfs_async_unlink(). 18051da177e4SLinus Torvalds */ 1806858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 18071da177e4SLinus Torvalds { 18081da177e4SLinus Torvalds int error; 18091da177e4SLinus Torvalds 18101da177e4SLinus Torvalds if (!victim->d_inode) 18111da177e4SLinus Torvalds return -ENOENT; 18121da177e4SLinus Torvalds 18131da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 1814cccc6bbaSAl Viro audit_inode_child(victim, dir); 18151da177e4SLinus Torvalds 1816f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 18171da177e4SLinus Torvalds if (error) 18181da177e4SLinus Torvalds return error; 18191da177e4SLinus Torvalds if (IS_APPEND(dir)) 18201da177e4SLinus Torvalds return -EPERM; 18211da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 1822f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 18231da177e4SLinus Torvalds return -EPERM; 18241da177e4SLinus Torvalds if (isdir) { 18251da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 18261da177e4SLinus Torvalds return -ENOTDIR; 18271da177e4SLinus Torvalds if (IS_ROOT(victim)) 18281da177e4SLinus Torvalds return -EBUSY; 18291da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 18301da177e4SLinus Torvalds return -EISDIR; 18311da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 18321da177e4SLinus Torvalds return -ENOENT; 18331da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 18341da177e4SLinus Torvalds return -EBUSY; 18351da177e4SLinus Torvalds return 0; 18361da177e4SLinus Torvalds } 18371da177e4SLinus Torvalds 18381da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 18391da177e4SLinus Torvalds * dir. 18401da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 18411da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 18421da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 18431da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 18441da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 18451da177e4SLinus Torvalds */ 1846a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 18471da177e4SLinus Torvalds { 18481da177e4SLinus Torvalds if (child->d_inode) 18491da177e4SLinus Torvalds return -EEXIST; 18501da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 18511da177e4SLinus Torvalds return -ENOENT; 1852f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 18531da177e4SLinus Torvalds } 18541da177e4SLinus Torvalds 18551da177e4SLinus Torvalds /* 18561da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 18571da177e4SLinus Torvalds */ 18581da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 18591da177e4SLinus Torvalds { 18601da177e4SLinus Torvalds struct dentry *p; 18611da177e4SLinus Torvalds 18621da177e4SLinus Torvalds if (p1 == p2) { 1863f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 18641da177e4SLinus Torvalds return NULL; 18651da177e4SLinus Torvalds } 18661da177e4SLinus Torvalds 1867a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 18681da177e4SLinus Torvalds 1869e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 1870e2761a11SOGAWA Hirofumi if (p) { 1871f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 1872f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 18731da177e4SLinus Torvalds return p; 18741da177e4SLinus Torvalds } 18751da177e4SLinus Torvalds 1876e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 1877e2761a11SOGAWA Hirofumi if (p) { 1878f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1879f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 18801da177e4SLinus Torvalds return p; 18811da177e4SLinus Torvalds } 18821da177e4SLinus Torvalds 1883f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1884f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 18851da177e4SLinus Torvalds return NULL; 18861da177e4SLinus Torvalds } 18871da177e4SLinus Torvalds 18881da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 18891da177e4SLinus Torvalds { 18901b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 18911da177e4SLinus Torvalds if (p1 != p2) { 18921b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 1893a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 18941da177e4SLinus Torvalds } 18951da177e4SLinus Torvalds } 18961da177e4SLinus Torvalds 18971da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode, 18981da177e4SLinus Torvalds struct nameidata *nd) 18991da177e4SLinus Torvalds { 1900a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 19011da177e4SLinus Torvalds 19021da177e4SLinus Torvalds if (error) 19031da177e4SLinus Torvalds return error; 19041da177e4SLinus Torvalds 1905acfa4380SAl Viro if (!dir->i_op->create) 19061da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 19071da177e4SLinus Torvalds mode &= S_IALLUGO; 19081da177e4SLinus Torvalds mode |= S_IFREG; 19091da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 19101da177e4SLinus Torvalds if (error) 19111da177e4SLinus Torvalds return error; 19121da177e4SLinus Torvalds error = dir->i_op->create(dir, dentry, mode, nd); 1913a74574aaSStephen Smalley if (!error) 1914f38aa942SAmy Griffis fsnotify_create(dir, dentry); 19151da177e4SLinus Torvalds return error; 19161da177e4SLinus Torvalds } 19171da177e4SLinus Torvalds 191873d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag) 19191da177e4SLinus Torvalds { 19203fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 19211da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 19221da177e4SLinus Torvalds int error; 19231da177e4SLinus Torvalds 1924bcda7652SAl Viro /* O_PATH? */ 1925bcda7652SAl Viro if (!acc_mode) 1926bcda7652SAl Viro return 0; 1927bcda7652SAl Viro 19281da177e4SLinus Torvalds if (!inode) 19291da177e4SLinus Torvalds return -ENOENT; 19301da177e4SLinus Torvalds 1931c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 1932c8fe8f30SChristoph Hellwig case S_IFLNK: 19331da177e4SLinus Torvalds return -ELOOP; 1934c8fe8f30SChristoph Hellwig case S_IFDIR: 1935c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 19361da177e4SLinus Torvalds return -EISDIR; 1937c8fe8f30SChristoph Hellwig break; 1938c8fe8f30SChristoph Hellwig case S_IFBLK: 1939c8fe8f30SChristoph Hellwig case S_IFCHR: 19403fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 19411da177e4SLinus Torvalds return -EACCES; 1942c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 1943c8fe8f30SChristoph Hellwig case S_IFIFO: 1944c8fe8f30SChristoph Hellwig case S_IFSOCK: 19451da177e4SLinus Torvalds flag &= ~O_TRUNC; 1946c8fe8f30SChristoph Hellwig break; 19474a3fd211SDave Hansen } 1948b41572e9SDave Hansen 19493fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 1950b41572e9SDave Hansen if (error) 1951b41572e9SDave Hansen return error; 19526146f0d5SMimi Zohar 19531da177e4SLinus Torvalds /* 19541da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 19551da177e4SLinus Torvalds */ 19561da177e4SLinus Torvalds if (IS_APPEND(inode)) { 19578737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 19587715b521SAl Viro return -EPERM; 19591da177e4SLinus Torvalds if (flag & O_TRUNC) 19607715b521SAl Viro return -EPERM; 19611da177e4SLinus Torvalds } 19621da177e4SLinus Torvalds 19631da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 19642e149670SSerge E. Hallyn if (flag & O_NOATIME && !inode_owner_or_capable(inode)) 19657715b521SAl Viro return -EPERM; 19661da177e4SLinus Torvalds 19671da177e4SLinus Torvalds /* 19681da177e4SLinus Torvalds * Ensure there are no outstanding leases on the file. 19691da177e4SLinus Torvalds */ 1970b65a9cfcSAl Viro return break_lease(inode, flag); 19717715b521SAl Viro } 19727715b521SAl Viro 1973e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 19747715b521SAl Viro { 1975e1181ee6SJeff Layton struct path *path = &filp->f_path; 19767715b521SAl Viro struct inode *inode = path->dentry->d_inode; 19777715b521SAl Viro int error = get_write_access(inode); 19781da177e4SLinus Torvalds if (error) 19797715b521SAl Viro return error; 19801da177e4SLinus Torvalds /* 19811da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 19821da177e4SLinus Torvalds */ 19831da177e4SLinus Torvalds error = locks_verify_locked(inode); 1984be6d3e56SKentaro Takeda if (!error) 1985ea0d3ab2STetsuo Handa error = security_path_truncate(path); 19861da177e4SLinus Torvalds if (!error) { 19877715b521SAl Viro error = do_truncate(path->dentry, 0, 1988d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 1989e1181ee6SJeff Layton filp); 19901da177e4SLinus Torvalds } 19911da177e4SLinus Torvalds put_write_access(inode); 1992acd0c935SMimi Zohar return error; 19931da177e4SLinus Torvalds } 19941da177e4SLinus Torvalds 1995d57999e1SDave Hansen /* 1996d57999e1SDave Hansen * Note that while the flag value (low two bits) for sys_open means: 1997d57999e1SDave Hansen * 00 - read-only 1998d57999e1SDave Hansen * 01 - write-only 1999d57999e1SDave Hansen * 10 - read-write 2000d57999e1SDave Hansen * 11 - special 2001d57999e1SDave Hansen * it is changed into 2002d57999e1SDave Hansen * 00 - no permissions needed 2003d57999e1SDave Hansen * 01 - read-permission 2004d57999e1SDave Hansen * 10 - write-permission 2005d57999e1SDave Hansen * 11 - read-write 2006d57999e1SDave Hansen * for the internal routines (ie open_namei()/follow_link() etc) 2007d57999e1SDave Hansen * This is more logical, and also allows the 00 "no perm needed" 2008d57999e1SDave Hansen * to be used for symlinks (where the permissions are checked 2009d57999e1SDave Hansen * later). 2010d57999e1SDave Hansen * 2011d57999e1SDave Hansen */ 2012d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2013d57999e1SDave Hansen { 2014d57999e1SDave Hansen if ((flag+1) & O_ACCMODE) 2015d57999e1SDave Hansen flag++; 2016d57999e1SDave Hansen return flag; 2017d57999e1SDave Hansen } 2018d57999e1SDave Hansen 201931e6b01fSNick Piggin /* 2020fe2d35ffSAl Viro * Handle the last step of open() 202131e6b01fSNick Piggin */ 2022fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path, 2023c3e380b0SAl Viro const struct open_flags *op, const char *pathname) 2024fb1cc555SAl Viro { 2025a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 20266c0d46c4SAl Viro struct dentry *dentry; 2027ca344a89SAl Viro int open_flag = op->open_flag; 20286c0d46c4SAl Viro int will_truncate = open_flag & O_TRUNC; 2029ca344a89SAl Viro int want_write = 0; 2030bcda7652SAl Viro int acc_mode = op->acc_mode; 2031fb1cc555SAl Viro struct file *filp; 203216c2cd71SAl Viro int error; 2033fb1cc555SAl Viro 2034c3e380b0SAl Viro nd->flags &= ~LOOKUP_PARENT; 2035c3e380b0SAl Viro nd->flags |= op->intent; 2036c3e380b0SAl Viro 20371f36f774SAl Viro switch (nd->last_type) { 20381f36f774SAl Viro case LAST_DOTDOT: 2039176306f5SNeil Brown case LAST_DOT: 2040fe2d35ffSAl Viro error = handle_dots(nd, nd->last_type); 2041fe2d35ffSAl Viro if (error) 2042fe2d35ffSAl Viro return ERR_PTR(error); 20431f36f774SAl Viro /* fallthrough */ 20441f36f774SAl Viro case LAST_ROOT: 20459f1fafeeSAl Viro error = complete_walk(nd); 204616c2cd71SAl Viro if (error) 20479f1fafeeSAl Viro return ERR_PTR(error); 2048fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2049ca344a89SAl Viro if (open_flag & O_CREAT) { 205016c2cd71SAl Viro error = -EISDIR; 20511f36f774SAl Viro goto exit; 2052fe2d35ffSAl Viro } 2053fe2d35ffSAl Viro goto ok; 20541f36f774SAl Viro case LAST_BIND: 20559f1fafeeSAl Viro error = complete_walk(nd); 205616c2cd71SAl Viro if (error) 20579f1fafeeSAl Viro return ERR_PTR(error); 20581f36f774SAl Viro audit_inode(pathname, dir); 20591f36f774SAl Viro goto ok; 20601f36f774SAl Viro } 2061a2c36b45SAl Viro 2062ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 2063bcda7652SAl Viro int symlink_ok = 0; 2064fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 2065fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2066bcda7652SAl Viro if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) 2067bcda7652SAl Viro symlink_ok = 1; 2068fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 2069ce57dfc1SAl Viro error = walk_component(nd, path, &nd->last, LAST_NORM, 2070ce57dfc1SAl Viro !symlink_ok); 2071ce57dfc1SAl Viro if (error < 0) 2072fe2d35ffSAl Viro return ERR_PTR(error); 2073ce57dfc1SAl Viro if (error) /* symlink */ 2074fe2d35ffSAl Viro return NULL; 2075fe2d35ffSAl Viro /* sayonara */ 20769f1fafeeSAl Viro error = complete_walk(nd); 20779f1fafeeSAl Viro if (error) 2078fe2d35ffSAl Viro return ERR_PTR(-ECHILD); 2079fe2d35ffSAl Viro 2080fe2d35ffSAl Viro error = -ENOTDIR; 2081fe2d35ffSAl Viro if (nd->flags & LOOKUP_DIRECTORY) { 2082ce57dfc1SAl Viro if (!nd->inode->i_op->lookup) 2083fe2d35ffSAl Viro goto exit; 2084fe2d35ffSAl Viro } 2085fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2086fe2d35ffSAl Viro goto ok; 2087fe2d35ffSAl Viro } 2088fe2d35ffSAl Viro 2089fe2d35ffSAl Viro /* create side of things */ 20909f1fafeeSAl Viro error = complete_walk(nd); 20919f1fafeeSAl Viro if (error) 20929f1fafeeSAl Viro return ERR_PTR(error); 2093fe2d35ffSAl Viro 2094fe2d35ffSAl Viro audit_inode(pathname, dir); 209516c2cd71SAl Viro error = -EISDIR; 20961f36f774SAl Viro /* trailing slashes? */ 209731e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 20981f36f774SAl Viro goto exit; 20991f36f774SAl Viro 2100a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 2101a1e28038SAl Viro 21026c0d46c4SAl Viro dentry = lookup_hash(nd); 21036c0d46c4SAl Viro error = PTR_ERR(dentry); 21046c0d46c4SAl Viro if (IS_ERR(dentry)) { 2105fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2106fb1cc555SAl Viro goto exit; 2107fb1cc555SAl Viro } 2108fb1cc555SAl Viro 21096c0d46c4SAl Viro path->dentry = dentry; 21106c0d46c4SAl Viro path->mnt = nd->path.mnt; 21116c0d46c4SAl Viro 2112fb1cc555SAl Viro /* Negative dentry, just create the file */ 21136c0d46c4SAl Viro if (!dentry->d_inode) { 21146c0d46c4SAl Viro int mode = op->mode; 21156c0d46c4SAl Viro if (!IS_POSIXACL(dir->d_inode)) 21166c0d46c4SAl Viro mode &= ~current_umask(); 2117fb1cc555SAl Viro /* 2118fb1cc555SAl Viro * This write is needed to ensure that a 21196c0d46c4SAl Viro * rw->ro transition does not occur between 2120fb1cc555SAl Viro * the time when the file is created and when 2121fb1cc555SAl Viro * a permanent write count is taken through 2122fb1cc555SAl Viro * the 'struct file' in nameidata_to_filp(). 2123fb1cc555SAl Viro */ 2124fb1cc555SAl Viro error = mnt_want_write(nd->path.mnt); 2125fb1cc555SAl Viro if (error) 2126fb1cc555SAl Viro goto exit_mutex_unlock; 2127ca344a89SAl Viro want_write = 1; 21289b44f1b3SAl Viro /* Don't check for write permission, don't truncate */ 2129ca344a89SAl Viro open_flag &= ~O_TRUNC; 21306c0d46c4SAl Viro will_truncate = 0; 2131bcda7652SAl Viro acc_mode = MAY_OPEN; 21326c0d46c4SAl Viro error = security_path_mknod(&nd->path, dentry, mode, 0); 21336c0d46c4SAl Viro if (error) 21346c0d46c4SAl Viro goto exit_mutex_unlock; 21356c0d46c4SAl Viro error = vfs_create(dir->d_inode, dentry, mode, nd); 21366c0d46c4SAl Viro if (error) 21376c0d46c4SAl Viro goto exit_mutex_unlock; 21386c0d46c4SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 21396c0d46c4SAl Viro dput(nd->path.dentry); 21406c0d46c4SAl Viro nd->path.dentry = dentry; 2141ca344a89SAl Viro goto common; 2142fb1cc555SAl Viro } 2143fb1cc555SAl Viro 2144fb1cc555SAl Viro /* 2145fb1cc555SAl Viro * It already exists. 2146fb1cc555SAl Viro */ 2147fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2148fb1cc555SAl Viro audit_inode(pathname, path->dentry); 2149fb1cc555SAl Viro 2150fb1cc555SAl Viro error = -EEXIST; 2151ca344a89SAl Viro if (open_flag & O_EXCL) 2152fb1cc555SAl Viro goto exit_dput; 2153fb1cc555SAl Viro 21549875cf80SDavid Howells error = follow_managed(path, nd->flags); 21559875cf80SDavid Howells if (error < 0) 2156fb1cc555SAl Viro goto exit_dput; 2157fb1cc555SAl Viro 2158fb1cc555SAl Viro error = -ENOENT; 2159fb1cc555SAl Viro if (!path->dentry->d_inode) 2160fb1cc555SAl Viro goto exit_dput; 21619e67f361SAl Viro 21629e67f361SAl Viro if (path->dentry->d_inode->i_op->follow_link) 2163fb1cc555SAl Viro return NULL; 2164fb1cc555SAl Viro 2165fb1cc555SAl Viro path_to_nameidata(path, nd); 216631e6b01fSNick Piggin nd->inode = path->dentry->d_inode; 2167fb1cc555SAl Viro error = -EISDIR; 216831e6b01fSNick Piggin if (S_ISDIR(nd->inode->i_mode)) 2169fb1cc555SAl Viro goto exit; 217067ee3ad2SAl Viro ok: 21716c0d46c4SAl Viro if (!S_ISREG(nd->inode->i_mode)) 21726c0d46c4SAl Viro will_truncate = 0; 21736c0d46c4SAl Viro 21740f9d1a10SAl Viro if (will_truncate) { 21750f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 21760f9d1a10SAl Viro if (error) 21770f9d1a10SAl Viro goto exit; 2178ca344a89SAl Viro want_write = 1; 21790f9d1a10SAl Viro } 2180ca344a89SAl Viro common: 2181bcda7652SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2182ca344a89SAl Viro if (error) 21830f9d1a10SAl Viro goto exit; 21840f9d1a10SAl Viro filp = nameidata_to_filp(nd); 21850f9d1a10SAl Viro if (!IS_ERR(filp)) { 21860f9d1a10SAl Viro error = ima_file_check(filp, op->acc_mode); 21870f9d1a10SAl Viro if (error) { 21880f9d1a10SAl Viro fput(filp); 21890f9d1a10SAl Viro filp = ERR_PTR(error); 21900f9d1a10SAl Viro } 21910f9d1a10SAl Viro } 21920f9d1a10SAl Viro if (!IS_ERR(filp)) { 21930f9d1a10SAl Viro if (will_truncate) { 21940f9d1a10SAl Viro error = handle_truncate(filp); 21950f9d1a10SAl Viro if (error) { 21960f9d1a10SAl Viro fput(filp); 21970f9d1a10SAl Viro filp = ERR_PTR(error); 21980f9d1a10SAl Viro } 21990f9d1a10SAl Viro } 22000f9d1a10SAl Viro } 2201ca344a89SAl Viro out: 2202ca344a89SAl Viro if (want_write) 22030f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 22040f9d1a10SAl Viro path_put(&nd->path); 2205fb1cc555SAl Viro return filp; 2206fb1cc555SAl Viro 2207fb1cc555SAl Viro exit_mutex_unlock: 2208fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2209fb1cc555SAl Viro exit_dput: 2210fb1cc555SAl Viro path_put_conditional(path, nd); 2211fb1cc555SAl Viro exit: 2212ca344a89SAl Viro filp = ERR_PTR(error); 2213ca344a89SAl Viro goto out; 2214fb1cc555SAl Viro } 2215fb1cc555SAl Viro 221613aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname, 221773d049a4SAl Viro struct nameidata *nd, const struct open_flags *op, int flags) 22181da177e4SLinus Torvalds { 2219fe2d35ffSAl Viro struct file *base = NULL; 22204a3fd211SDave Hansen struct file *filp; 22219850c056SAl Viro struct path path; 222213aab428SAl Viro int error; 222331e6b01fSNick Piggin 222431e6b01fSNick Piggin filp = get_empty_filp(); 222531e6b01fSNick Piggin if (!filp) 222631e6b01fSNick Piggin return ERR_PTR(-ENFILE); 222731e6b01fSNick Piggin 222847c805dcSAl Viro filp->f_flags = op->open_flag; 222973d049a4SAl Viro nd->intent.open.file = filp; 223073d049a4SAl Viro nd->intent.open.flags = open_to_namei_flags(op->open_flag); 223173d049a4SAl Viro nd->intent.open.create_mode = op->mode; 223231e6b01fSNick Piggin 223373d049a4SAl Viro error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base); 223431e6b01fSNick Piggin if (unlikely(error)) 223513aab428SAl Viro goto out_filp; 223631e6b01fSNick Piggin 2237fe2d35ffSAl Viro current->total_link_count = 0; 223873d049a4SAl Viro error = link_path_walk(pathname, nd); 223931e6b01fSNick Piggin if (unlikely(error)) 224031e6b01fSNick Piggin goto out_filp; 22411da177e4SLinus Torvalds 224273d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2243806b681cSAl Viro while (unlikely(!filp)) { /* trailing symlink */ 22447b9337aaSNick Piggin struct path link = path; 2245def4af30SAl Viro void *cookie; 2246574197e0SAl Viro if (!(nd->flags & LOOKUP_FOLLOW)) { 224773d049a4SAl Viro path_put_conditional(&path, nd); 224873d049a4SAl Viro path_put(&nd->path); 224940b39136SAl Viro filp = ERR_PTR(-ELOOP); 225040b39136SAl Viro break; 225140b39136SAl Viro } 225273d049a4SAl Viro nd->flags |= LOOKUP_PARENT; 225373d049a4SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 2254574197e0SAl Viro error = follow_link(&link, nd, &cookie); 2255c3e380b0SAl Viro if (unlikely(error)) 2256f1afe9efSAl Viro filp = ERR_PTR(error); 2257c3e380b0SAl Viro else 225873d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2259574197e0SAl Viro put_link(nd, &link, cookie); 2260806b681cSAl Viro } 226110fa8e62SAl Viro out: 226273d049a4SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) 226373d049a4SAl Viro path_put(&nd->root); 2264fe2d35ffSAl Viro if (base) 2265fe2d35ffSAl Viro fput(base); 226673d049a4SAl Viro release_open_intent(nd); 226710fa8e62SAl Viro return filp; 22681da177e4SLinus Torvalds 226931e6b01fSNick Piggin out_filp: 227010fa8e62SAl Viro filp = ERR_PTR(error); 227110fa8e62SAl Viro goto out; 2272de459215SKirill Korotaev } 22731da177e4SLinus Torvalds 227413aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname, 227513aab428SAl Viro const struct open_flags *op, int flags) 227613aab428SAl Viro { 227773d049a4SAl Viro struct nameidata nd; 227813aab428SAl Viro struct file *filp; 227913aab428SAl Viro 228073d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); 228113aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 228273d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags); 228313aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 228473d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); 228513aab428SAl Viro return filp; 228613aab428SAl Viro } 228713aab428SAl Viro 228873d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, 228973d049a4SAl Viro const char *name, const struct open_flags *op, int flags) 229073d049a4SAl Viro { 229173d049a4SAl Viro struct nameidata nd; 229273d049a4SAl Viro struct file *file; 229373d049a4SAl Viro 229473d049a4SAl Viro nd.root.mnt = mnt; 229573d049a4SAl Viro nd.root.dentry = dentry; 229673d049a4SAl Viro 229773d049a4SAl Viro flags |= LOOKUP_ROOT; 229873d049a4SAl Viro 2299bcda7652SAl Viro if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN) 230073d049a4SAl Viro return ERR_PTR(-ELOOP); 230173d049a4SAl Viro 230273d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU); 230373d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 230473d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags); 230573d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 230673d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL); 230773d049a4SAl Viro return file; 230873d049a4SAl Viro } 230973d049a4SAl Viro 23101da177e4SLinus Torvalds /** 23111da177e4SLinus Torvalds * lookup_create - lookup a dentry, creating it if it doesn't exist 23121da177e4SLinus Torvalds * @nd: nameidata info 23131da177e4SLinus Torvalds * @is_dir: directory flag 23141da177e4SLinus Torvalds * 23151da177e4SLinus Torvalds * Simple function to lookup and return a dentry and create it 23161da177e4SLinus Torvalds * if it doesn't exist. Is SMP-safe. 2317c663e5d8SChristoph Hellwig * 23184ac91378SJan Blunck * Returns with nd->path.dentry->d_inode->i_mutex locked. 23191da177e4SLinus Torvalds */ 23201da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir) 23211da177e4SLinus Torvalds { 2322c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 23231da177e4SLinus Torvalds 23244ac91378SJan Blunck mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 2325c663e5d8SChristoph Hellwig /* 2326c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 2327c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 2328c663e5d8SChristoph Hellwig */ 23291da177e4SLinus Torvalds if (nd->last_type != LAST_NORM) 23301da177e4SLinus Torvalds goto fail; 23311da177e4SLinus Torvalds nd->flags &= ~LOOKUP_PARENT; 23323516586aSAl Viro nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL; 2333a634904aSASANO Masahiro nd->intent.open.flags = O_EXCL; 2334c663e5d8SChristoph Hellwig 2335c663e5d8SChristoph Hellwig /* 2336c663e5d8SChristoph Hellwig * Do the final lookup. 2337c663e5d8SChristoph Hellwig */ 233849705b77SChristoph Hellwig dentry = lookup_hash(nd); 23391da177e4SLinus Torvalds if (IS_ERR(dentry)) 23401da177e4SLinus Torvalds goto fail; 2341c663e5d8SChristoph Hellwig 2342e9baf6e5SAl Viro if (dentry->d_inode) 2343e9baf6e5SAl Viro goto eexist; 2344c663e5d8SChristoph Hellwig /* 2345c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 2346c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 2347c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 2348c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 2349c663e5d8SChristoph Hellwig */ 2350e9baf6e5SAl Viro if (unlikely(!is_dir && nd->last.name[nd->last.len])) { 23511da177e4SLinus Torvalds dput(dentry); 23521da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 2353e9baf6e5SAl Viro } 2354e9baf6e5SAl Viro return dentry; 2355e9baf6e5SAl Viro eexist: 2356e9baf6e5SAl Viro dput(dentry); 2357e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 23581da177e4SLinus Torvalds fail: 23591da177e4SLinus Torvalds return dentry; 23601da177e4SLinus Torvalds } 2361f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create); 23621da177e4SLinus Torvalds 23631da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 23641da177e4SLinus Torvalds { 2365a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 23661da177e4SLinus Torvalds 23671da177e4SLinus Torvalds if (error) 23681da177e4SLinus Torvalds return error; 23691da177e4SLinus Torvalds 2370e795b717SSerge E. Hallyn if ((S_ISCHR(mode) || S_ISBLK(mode)) && 2371e795b717SSerge E. Hallyn !ns_capable(inode_userns(dir), CAP_MKNOD)) 23721da177e4SLinus Torvalds return -EPERM; 23731da177e4SLinus Torvalds 2374acfa4380SAl Viro if (!dir->i_op->mknod) 23751da177e4SLinus Torvalds return -EPERM; 23761da177e4SLinus Torvalds 237708ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 237808ce5f16SSerge E. Hallyn if (error) 237908ce5f16SSerge E. Hallyn return error; 238008ce5f16SSerge E. Hallyn 23811da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 23821da177e4SLinus Torvalds if (error) 23831da177e4SLinus Torvalds return error; 23841da177e4SLinus Torvalds 23851da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 2386a74574aaSStephen Smalley if (!error) 2387f38aa942SAmy Griffis fsnotify_create(dir, dentry); 23881da177e4SLinus Torvalds return error; 23891da177e4SLinus Torvalds } 23901da177e4SLinus Torvalds 2391463c3197SDave Hansen static int may_mknod(mode_t mode) 2392463c3197SDave Hansen { 2393463c3197SDave Hansen switch (mode & S_IFMT) { 2394463c3197SDave Hansen case S_IFREG: 2395463c3197SDave Hansen case S_IFCHR: 2396463c3197SDave Hansen case S_IFBLK: 2397463c3197SDave Hansen case S_IFIFO: 2398463c3197SDave Hansen case S_IFSOCK: 2399463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2400463c3197SDave Hansen return 0; 2401463c3197SDave Hansen case S_IFDIR: 2402463c3197SDave Hansen return -EPERM; 2403463c3197SDave Hansen default: 2404463c3197SDave Hansen return -EINVAL; 2405463c3197SDave Hansen } 2406463c3197SDave Hansen } 2407463c3197SDave Hansen 24082e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode, 24092e4d0924SHeiko Carstens unsigned, dev) 24101da177e4SLinus Torvalds { 24112ad94ae6SAl Viro int error; 24121da177e4SLinus Torvalds char *tmp; 24131da177e4SLinus Torvalds struct dentry *dentry; 24141da177e4SLinus Torvalds struct nameidata nd; 24151da177e4SLinus Torvalds 24161da177e4SLinus Torvalds if (S_ISDIR(mode)) 24171da177e4SLinus Torvalds return -EPERM; 24181da177e4SLinus Torvalds 24192ad94ae6SAl Viro error = user_path_parent(dfd, filename, &nd, &tmp); 24201da177e4SLinus Torvalds if (error) 24212ad94ae6SAl Viro return error; 24222ad94ae6SAl Viro 24231da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 2424463c3197SDave Hansen if (IS_ERR(dentry)) { 24251da177e4SLinus Torvalds error = PTR_ERR(dentry); 2426463c3197SDave Hansen goto out_unlock; 2427463c3197SDave Hansen } 24284ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2429ce3b0f8dSAl Viro mode &= ~current_umask(); 2430463c3197SDave Hansen error = may_mknod(mode); 2431463c3197SDave Hansen if (error) 2432463c3197SDave Hansen goto out_dput; 2433463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2434463c3197SDave Hansen if (error) 2435463c3197SDave Hansen goto out_dput; 2436be6d3e56SKentaro Takeda error = security_path_mknod(&nd.path, dentry, mode, dev); 2437be6d3e56SKentaro Takeda if (error) 2438be6d3e56SKentaro Takeda goto out_drop_write; 24391da177e4SLinus Torvalds switch (mode & S_IFMT) { 24401da177e4SLinus Torvalds case 0: case S_IFREG: 24414ac91378SJan Blunck error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd); 24421da177e4SLinus Torvalds break; 24431da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 24444ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode, 24451da177e4SLinus Torvalds new_decode_dev(dev)); 24461da177e4SLinus Torvalds break; 24471da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 24484ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0); 24491da177e4SLinus Torvalds break; 24501da177e4SLinus Torvalds } 2451be6d3e56SKentaro Takeda out_drop_write: 2452463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2453463c3197SDave Hansen out_dput: 24541da177e4SLinus Torvalds dput(dentry); 2455463c3197SDave Hansen out_unlock: 24564ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 24571d957f9bSJan Blunck path_put(&nd.path); 24581da177e4SLinus Torvalds putname(tmp); 24591da177e4SLinus Torvalds 24601da177e4SLinus Torvalds return error; 24611da177e4SLinus Torvalds } 24621da177e4SLinus Torvalds 24633480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev) 24645590ff0dSUlrich Drepper { 24655590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 24665590ff0dSUlrich Drepper } 24675590ff0dSUlrich Drepper 24681da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 24691da177e4SLinus Torvalds { 2470a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 24711da177e4SLinus Torvalds 24721da177e4SLinus Torvalds if (error) 24731da177e4SLinus Torvalds return error; 24741da177e4SLinus Torvalds 2475acfa4380SAl Viro if (!dir->i_op->mkdir) 24761da177e4SLinus Torvalds return -EPERM; 24771da177e4SLinus Torvalds 24781da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 24791da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 24801da177e4SLinus Torvalds if (error) 24811da177e4SLinus Torvalds return error; 24821da177e4SLinus Torvalds 24831da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2484a74574aaSStephen Smalley if (!error) 2485f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 24861da177e4SLinus Torvalds return error; 24871da177e4SLinus Torvalds } 24881da177e4SLinus Torvalds 24892e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode) 24901da177e4SLinus Torvalds { 24911da177e4SLinus Torvalds int error = 0; 24921da177e4SLinus Torvalds char * tmp; 24936902d925SDave Hansen struct dentry *dentry; 24946902d925SDave Hansen struct nameidata nd; 24951da177e4SLinus Torvalds 24962ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &tmp); 24972ad94ae6SAl Viro if (error) 24986902d925SDave Hansen goto out_err; 24991da177e4SLinus Torvalds 25001da177e4SLinus Torvalds dentry = lookup_create(&nd, 1); 25011da177e4SLinus Torvalds error = PTR_ERR(dentry); 25026902d925SDave Hansen if (IS_ERR(dentry)) 25036902d925SDave Hansen goto out_unlock; 25046902d925SDave Hansen 25054ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2506ce3b0f8dSAl Viro mode &= ~current_umask(); 2507463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2508463c3197SDave Hansen if (error) 2509463c3197SDave Hansen goto out_dput; 2510be6d3e56SKentaro Takeda error = security_path_mkdir(&nd.path, dentry, mode); 2511be6d3e56SKentaro Takeda if (error) 2512be6d3e56SKentaro Takeda goto out_drop_write; 25134ac91378SJan Blunck error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode); 2514be6d3e56SKentaro Takeda out_drop_write: 2515463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2516463c3197SDave Hansen out_dput: 25171da177e4SLinus Torvalds dput(dentry); 25186902d925SDave Hansen out_unlock: 25194ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 25201d957f9bSJan Blunck path_put(&nd.path); 25211da177e4SLinus Torvalds putname(tmp); 25226902d925SDave Hansen out_err: 25231da177e4SLinus Torvalds return error; 25241da177e4SLinus Torvalds } 25251da177e4SLinus Torvalds 25263cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode) 25275590ff0dSUlrich Drepper { 25285590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 25295590ff0dSUlrich Drepper } 25305590ff0dSUlrich Drepper 25311da177e4SLinus Torvalds /* 2532a71905f0SSage Weil * The dentry_unhash() helper will try to drop the dentry early: we 2533a71905f0SSage Weil * should have a usage count of 2 if we're the only user of this 2534a71905f0SSage Weil * dentry, and if that is true (possibly after pruning the dcache), 2535a71905f0SSage Weil * then we drop the dentry now. 25361da177e4SLinus Torvalds * 25371da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 25381da177e4SLinus Torvalds * do a 25391da177e4SLinus Torvalds * 25401da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 25411da177e4SLinus Torvalds * return -EBUSY; 25421da177e4SLinus Torvalds * 25431da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 25441da177e4SLinus Torvalds * that is still in use by something else.. 25451da177e4SLinus Torvalds */ 25461da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 25471da177e4SLinus Torvalds { 25481da177e4SLinus Torvalds shrink_dcache_parent(dentry); 25491da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 255064252c75SSage Weil if (dentry->d_count == 1) 25511da177e4SLinus Torvalds __d_drop(dentry); 25521da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 25531da177e4SLinus Torvalds } 25541da177e4SLinus Torvalds 25551da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 25561da177e4SLinus Torvalds { 25571da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 25581da177e4SLinus Torvalds 25591da177e4SLinus Torvalds if (error) 25601da177e4SLinus Torvalds return error; 25611da177e4SLinus Torvalds 2562acfa4380SAl Viro if (!dir->i_op->rmdir) 25631da177e4SLinus Torvalds return -EPERM; 25641da177e4SLinus Torvalds 25651b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 2566912dbc15SSage Weil 25671da177e4SLinus Torvalds error = -EBUSY; 2568912dbc15SSage Weil if (d_mountpoint(dentry)) 2569912dbc15SSage Weil goto out; 2570912dbc15SSage Weil 25711da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 2572912dbc15SSage Weil if (error) 2573912dbc15SSage Weil goto out; 2574912dbc15SSage Weil 25751da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 2576912dbc15SSage Weil if (error) 2577912dbc15SSage Weil goto out; 2578912dbc15SSage Weil 25791da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 2580d83c49f3SAl Viro dont_mount(dentry); 25811da177e4SLinus Torvalds 2582912dbc15SSage Weil out: 2583912dbc15SSage Weil mutex_unlock(&dentry->d_inode->i_mutex); 2584912dbc15SSage Weil if (!error) 2585912dbc15SSage Weil d_delete(dentry); 25861da177e4SLinus Torvalds return error; 25871da177e4SLinus Torvalds } 25881da177e4SLinus Torvalds 25895590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 25901da177e4SLinus Torvalds { 25911da177e4SLinus Torvalds int error = 0; 25921da177e4SLinus Torvalds char * name; 25931da177e4SLinus Torvalds struct dentry *dentry; 25941da177e4SLinus Torvalds struct nameidata nd; 25951da177e4SLinus Torvalds 25962ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 25971da177e4SLinus Torvalds if (error) 25982ad94ae6SAl Viro return error; 25991da177e4SLinus Torvalds 26001da177e4SLinus Torvalds switch(nd.last_type) { 26011da177e4SLinus Torvalds case LAST_DOTDOT: 26021da177e4SLinus Torvalds error = -ENOTEMPTY; 26031da177e4SLinus Torvalds goto exit1; 26041da177e4SLinus Torvalds case LAST_DOT: 26051da177e4SLinus Torvalds error = -EINVAL; 26061da177e4SLinus Torvalds goto exit1; 26071da177e4SLinus Torvalds case LAST_ROOT: 26081da177e4SLinus Torvalds error = -EBUSY; 26091da177e4SLinus Torvalds goto exit1; 26101da177e4SLinus Torvalds } 26110612d9fbSOGAWA Hirofumi 26120612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 26130612d9fbSOGAWA Hirofumi 26144ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 261549705b77SChristoph Hellwig dentry = lookup_hash(&nd); 26161da177e4SLinus Torvalds error = PTR_ERR(dentry); 26176902d925SDave Hansen if (IS_ERR(dentry)) 26186902d925SDave Hansen goto exit2; 26190622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 26200622753bSDave Hansen if (error) 26210622753bSDave Hansen goto exit3; 2622be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 2623be6d3e56SKentaro Takeda if (error) 2624be6d3e56SKentaro Takeda goto exit4; 26254ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 2626be6d3e56SKentaro Takeda exit4: 26270622753bSDave Hansen mnt_drop_write(nd.path.mnt); 26280622753bSDave Hansen exit3: 26291da177e4SLinus Torvalds dput(dentry); 26306902d925SDave Hansen exit2: 26314ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 26321da177e4SLinus Torvalds exit1: 26331d957f9bSJan Blunck path_put(&nd.path); 26341da177e4SLinus Torvalds putname(name); 26351da177e4SLinus Torvalds return error; 26361da177e4SLinus Torvalds } 26371da177e4SLinus Torvalds 26383cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 26395590ff0dSUlrich Drepper { 26405590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 26415590ff0dSUlrich Drepper } 26425590ff0dSUlrich Drepper 26431da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 26441da177e4SLinus Torvalds { 26451da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 26461da177e4SLinus Torvalds 26471da177e4SLinus Torvalds if (error) 26481da177e4SLinus Torvalds return error; 26491da177e4SLinus Torvalds 2650acfa4380SAl Viro if (!dir->i_op->unlink) 26511da177e4SLinus Torvalds return -EPERM; 26521da177e4SLinus Torvalds 26531b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 26541da177e4SLinus Torvalds if (d_mountpoint(dentry)) 26551da177e4SLinus Torvalds error = -EBUSY; 26561da177e4SLinus Torvalds else { 26571da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 2658bec1052eSAl Viro if (!error) { 26591da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 2660bec1052eSAl Viro if (!error) 2661d83c49f3SAl Viro dont_mount(dentry); 2662bec1052eSAl Viro } 26631da177e4SLinus Torvalds } 26641b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 26651da177e4SLinus Torvalds 26661da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 26671da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 2668ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 26691da177e4SLinus Torvalds d_delete(dentry); 26701da177e4SLinus Torvalds } 26710eeca283SRobert Love 26721da177e4SLinus Torvalds return error; 26731da177e4SLinus Torvalds } 26741da177e4SLinus Torvalds 26751da177e4SLinus Torvalds /* 26761da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 26771b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 26781da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 26791da177e4SLinus Torvalds * while waiting on the I/O. 26801da177e4SLinus Torvalds */ 26815590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 26821da177e4SLinus Torvalds { 26832ad94ae6SAl Viro int error; 26841da177e4SLinus Torvalds char *name; 26851da177e4SLinus Torvalds struct dentry *dentry; 26861da177e4SLinus Torvalds struct nameidata nd; 26871da177e4SLinus Torvalds struct inode *inode = NULL; 26881da177e4SLinus Torvalds 26892ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 26901da177e4SLinus Torvalds if (error) 26912ad94ae6SAl Viro return error; 26922ad94ae6SAl Viro 26931da177e4SLinus Torvalds error = -EISDIR; 26941da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 26951da177e4SLinus Torvalds goto exit1; 26960612d9fbSOGAWA Hirofumi 26970612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 26980612d9fbSOGAWA Hirofumi 26994ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 270049705b77SChristoph Hellwig dentry = lookup_hash(&nd); 27011da177e4SLinus Torvalds error = PTR_ERR(dentry); 27021da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 27031da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 27041da177e4SLinus Torvalds if (nd.last.name[nd.last.len]) 27051da177e4SLinus Torvalds goto slashes; 27061da177e4SLinus Torvalds inode = dentry->d_inode; 27071da177e4SLinus Torvalds if (inode) 27087de9c6eeSAl Viro ihold(inode); 27090622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 27100622753bSDave Hansen if (error) 27110622753bSDave Hansen goto exit2; 2712be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 2713be6d3e56SKentaro Takeda if (error) 2714be6d3e56SKentaro Takeda goto exit3; 27154ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 2716be6d3e56SKentaro Takeda exit3: 27170622753bSDave Hansen mnt_drop_write(nd.path.mnt); 27181da177e4SLinus Torvalds exit2: 27191da177e4SLinus Torvalds dput(dentry); 27201da177e4SLinus Torvalds } 27214ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 27221da177e4SLinus Torvalds if (inode) 27231da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 27241da177e4SLinus Torvalds exit1: 27251d957f9bSJan Blunck path_put(&nd.path); 27261da177e4SLinus Torvalds putname(name); 27271da177e4SLinus Torvalds return error; 27281da177e4SLinus Torvalds 27291da177e4SLinus Torvalds slashes: 27301da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 27311da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 27321da177e4SLinus Torvalds goto exit2; 27331da177e4SLinus Torvalds } 27341da177e4SLinus Torvalds 27352e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 27365590ff0dSUlrich Drepper { 27375590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 27385590ff0dSUlrich Drepper return -EINVAL; 27395590ff0dSUlrich Drepper 27405590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 27415590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 27425590ff0dSUlrich Drepper 27435590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 27445590ff0dSUlrich Drepper } 27455590ff0dSUlrich Drepper 27463480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 27475590ff0dSUlrich Drepper { 27485590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 27495590ff0dSUlrich Drepper } 27505590ff0dSUlrich Drepper 2751db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 27521da177e4SLinus Torvalds { 2753a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 27541da177e4SLinus Torvalds 27551da177e4SLinus Torvalds if (error) 27561da177e4SLinus Torvalds return error; 27571da177e4SLinus Torvalds 2758acfa4380SAl Viro if (!dir->i_op->symlink) 27591da177e4SLinus Torvalds return -EPERM; 27601da177e4SLinus Torvalds 27611da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 27621da177e4SLinus Torvalds if (error) 27631da177e4SLinus Torvalds return error; 27641da177e4SLinus Torvalds 27651da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 2766a74574aaSStephen Smalley if (!error) 2767f38aa942SAmy Griffis fsnotify_create(dir, dentry); 27681da177e4SLinus Torvalds return error; 27691da177e4SLinus Torvalds } 27701da177e4SLinus Torvalds 27712e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 27722e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 27731da177e4SLinus Torvalds { 27742ad94ae6SAl Viro int error; 27751da177e4SLinus Torvalds char *from; 27761da177e4SLinus Torvalds char *to; 27776902d925SDave Hansen struct dentry *dentry; 27786902d925SDave Hansen struct nameidata nd; 27791da177e4SLinus Torvalds 27801da177e4SLinus Torvalds from = getname(oldname); 27811da177e4SLinus Torvalds if (IS_ERR(from)) 27821da177e4SLinus Torvalds return PTR_ERR(from); 27832ad94ae6SAl Viro 27842ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 27852ad94ae6SAl Viro if (error) 27866902d925SDave Hansen goto out_putname; 27871da177e4SLinus Torvalds 27881da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 27891da177e4SLinus Torvalds error = PTR_ERR(dentry); 27906902d925SDave Hansen if (IS_ERR(dentry)) 27916902d925SDave Hansen goto out_unlock; 27926902d925SDave Hansen 279375c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 279475c3f29dSDave Hansen if (error) 279575c3f29dSDave Hansen goto out_dput; 2796be6d3e56SKentaro Takeda error = security_path_symlink(&nd.path, dentry, from); 2797be6d3e56SKentaro Takeda if (error) 2798be6d3e56SKentaro Takeda goto out_drop_write; 2799db2e747bSMiklos Szeredi error = vfs_symlink(nd.path.dentry->d_inode, dentry, from); 2800be6d3e56SKentaro Takeda out_drop_write: 280175c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 280275c3f29dSDave Hansen out_dput: 28031da177e4SLinus Torvalds dput(dentry); 28046902d925SDave Hansen out_unlock: 28054ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 28061d957f9bSJan Blunck path_put(&nd.path); 28071da177e4SLinus Torvalds putname(to); 28086902d925SDave Hansen out_putname: 28091da177e4SLinus Torvalds putname(from); 28101da177e4SLinus Torvalds return error; 28111da177e4SLinus Torvalds } 28121da177e4SLinus Torvalds 28133480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 28145590ff0dSUlrich Drepper { 28155590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 28165590ff0dSUlrich Drepper } 28175590ff0dSUlrich Drepper 28181da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 28191da177e4SLinus Torvalds { 28201da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 28211da177e4SLinus Torvalds int error; 28221da177e4SLinus Torvalds 28231da177e4SLinus Torvalds if (!inode) 28241da177e4SLinus Torvalds return -ENOENT; 28251da177e4SLinus Torvalds 2826a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 28271da177e4SLinus Torvalds if (error) 28281da177e4SLinus Torvalds return error; 28291da177e4SLinus Torvalds 28301da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 28311da177e4SLinus Torvalds return -EXDEV; 28321da177e4SLinus Torvalds 28331da177e4SLinus Torvalds /* 28341da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 28351da177e4SLinus Torvalds */ 28361da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 28371da177e4SLinus Torvalds return -EPERM; 2838acfa4380SAl Viro if (!dir->i_op->link) 28391da177e4SLinus Torvalds return -EPERM; 28407e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 28411da177e4SLinus Torvalds return -EPERM; 28421da177e4SLinus Torvalds 28431da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 28441da177e4SLinus Torvalds if (error) 28451da177e4SLinus Torvalds return error; 28461da177e4SLinus Torvalds 28477e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 2848aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 2849aae8a97dSAneesh Kumar K.V if (inode->i_nlink == 0) 2850aae8a97dSAneesh Kumar K.V error = -ENOENT; 2851aae8a97dSAneesh Kumar K.V else 28521da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 28537e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 2854e31e14ecSStephen Smalley if (!error) 28557e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 28561da177e4SLinus Torvalds return error; 28571da177e4SLinus Torvalds } 28581da177e4SLinus Torvalds 28591da177e4SLinus Torvalds /* 28601da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 28611da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 28621da177e4SLinus Torvalds * newname. --KAB 28631da177e4SLinus Torvalds * 28641da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 28651da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 28661da177e4SLinus Torvalds * and other special files. --ADM 28671da177e4SLinus Torvalds */ 28682e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 28692e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 28701da177e4SLinus Torvalds { 28711da177e4SLinus Torvalds struct dentry *new_dentry; 28722d8f3038SAl Viro struct nameidata nd; 28732d8f3038SAl Viro struct path old_path; 287411a7b371SAneesh Kumar K.V int how = 0; 28751da177e4SLinus Torvalds int error; 28761da177e4SLinus Torvalds char *to; 28771da177e4SLinus Torvalds 287811a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 2879c04030e1SUlrich Drepper return -EINVAL; 288011a7b371SAneesh Kumar K.V /* 288111a7b371SAneesh Kumar K.V * To use null names we require CAP_DAC_READ_SEARCH 288211a7b371SAneesh Kumar K.V * This ensures that not everyone will be able to create 288311a7b371SAneesh Kumar K.V * handlink using the passed filedescriptor. 288411a7b371SAneesh Kumar K.V */ 288511a7b371SAneesh Kumar K.V if (flags & AT_EMPTY_PATH) { 288611a7b371SAneesh Kumar K.V if (!capable(CAP_DAC_READ_SEARCH)) 288711a7b371SAneesh Kumar K.V return -ENOENT; 288811a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 288911a7b371SAneesh Kumar K.V } 2890c04030e1SUlrich Drepper 289111a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 289211a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 289311a7b371SAneesh Kumar K.V 289411a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 28951da177e4SLinus Torvalds if (error) 28962ad94ae6SAl Viro return error; 28972ad94ae6SAl Viro 28982ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 28991da177e4SLinus Torvalds if (error) 29001da177e4SLinus Torvalds goto out; 29011da177e4SLinus Torvalds error = -EXDEV; 29022d8f3038SAl Viro if (old_path.mnt != nd.path.mnt) 29031da177e4SLinus Torvalds goto out_release; 29041da177e4SLinus Torvalds new_dentry = lookup_create(&nd, 0); 29051da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 29066902d925SDave Hansen if (IS_ERR(new_dentry)) 29076902d925SDave Hansen goto out_unlock; 290875c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 290975c3f29dSDave Hansen if (error) 291075c3f29dSDave Hansen goto out_dput; 2911be6d3e56SKentaro Takeda error = security_path_link(old_path.dentry, &nd.path, new_dentry); 2912be6d3e56SKentaro Takeda if (error) 2913be6d3e56SKentaro Takeda goto out_drop_write; 29142d8f3038SAl Viro error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry); 2915be6d3e56SKentaro Takeda out_drop_write: 291675c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 291775c3f29dSDave Hansen out_dput: 29181da177e4SLinus Torvalds dput(new_dentry); 29196902d925SDave Hansen out_unlock: 29204ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 29211da177e4SLinus Torvalds out_release: 29221d957f9bSJan Blunck path_put(&nd.path); 29232ad94ae6SAl Viro putname(to); 29241da177e4SLinus Torvalds out: 29252d8f3038SAl Viro path_put(&old_path); 29261da177e4SLinus Torvalds 29271da177e4SLinus Torvalds return error; 29281da177e4SLinus Torvalds } 29291da177e4SLinus Torvalds 29303480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 29315590ff0dSUlrich Drepper { 2932c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 29335590ff0dSUlrich Drepper } 29345590ff0dSUlrich Drepper 29351da177e4SLinus Torvalds /* 29361da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 29371da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 29381da177e4SLinus Torvalds * Problems: 29391da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 29401da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 29411da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 2942a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 29431da177e4SLinus Torvalds * story. 29441da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 29451b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 29461da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 29471da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 2948a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 29491da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 29501da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 29511da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 2952a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 29531da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 29541da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 29551da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 2956e4eaac06SSage Weil * d) conversion from fhandle to dentry may come in the wrong moment - when 29571b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 29581da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 2959c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 29601da177e4SLinus Torvalds * locking]. 29611da177e4SLinus Torvalds */ 296275c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 29631da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 29641da177e4SLinus Torvalds { 29651da177e4SLinus Torvalds int error = 0; 29669055cba7SSage Weil struct inode *target = new_dentry->d_inode; 29671da177e4SLinus Torvalds 29681da177e4SLinus Torvalds /* 29691da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 29701da177e4SLinus Torvalds * we'll need to flip '..'. 29711da177e4SLinus Torvalds */ 29721da177e4SLinus Torvalds if (new_dir != old_dir) { 2973f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 29741da177e4SLinus Torvalds if (error) 29751da177e4SLinus Torvalds return error; 29761da177e4SLinus Torvalds } 29771da177e4SLinus Torvalds 29781da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 29791da177e4SLinus Torvalds if (error) 29801da177e4SLinus Torvalds return error; 29811da177e4SLinus Torvalds 2982d83c49f3SAl Viro if (target) 29831b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 29849055cba7SSage Weil 29851da177e4SLinus Torvalds error = -EBUSY; 29869055cba7SSage Weil if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) 29879055cba7SSage Weil goto out; 29889055cba7SSage Weil 29891da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 29909055cba7SSage Weil if (error) 29919055cba7SSage Weil goto out; 29929055cba7SSage Weil 29931da177e4SLinus Torvalds if (target) { 29941da177e4SLinus Torvalds target->i_flags |= S_DEAD; 2995d83c49f3SAl Viro dont_mount(new_dentry); 2996d83c49f3SAl Viro } 29979055cba7SSage Weil out: 29989055cba7SSage Weil if (target) 29991b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 3000e31e14ecSStephen Smalley if (!error) 3001349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 30021da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 30031da177e4SLinus Torvalds return error; 30041da177e4SLinus Torvalds } 30051da177e4SLinus Torvalds 300675c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 30071da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30081da177e4SLinus Torvalds { 30091da177e4SLinus Torvalds struct inode *target; 30101da177e4SLinus Torvalds int error; 30111da177e4SLinus Torvalds 30121da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 30131da177e4SLinus Torvalds if (error) 30141da177e4SLinus Torvalds return error; 30151da177e4SLinus Torvalds 30161da177e4SLinus Torvalds dget(new_dentry); 30171da177e4SLinus Torvalds target = new_dentry->d_inode; 30181da177e4SLinus Torvalds if (target) 30191b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 30201da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 30211da177e4SLinus Torvalds error = -EBUSY; 30221da177e4SLinus Torvalds else 30231da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 30241da177e4SLinus Torvalds if (!error) { 3025bec1052eSAl Viro if (target) 3026d83c49f3SAl Viro dont_mount(new_dentry); 3027349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 30281da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 30291da177e4SLinus Torvalds } 30301da177e4SLinus Torvalds if (target) 30311b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 30321da177e4SLinus Torvalds dput(new_dentry); 30331da177e4SLinus Torvalds return error; 30341da177e4SLinus Torvalds } 30351da177e4SLinus Torvalds 30361da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 30371da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30381da177e4SLinus Torvalds { 30391da177e4SLinus Torvalds int error; 30401da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 304159b0df21SEric Paris const unsigned char *old_name; 30421da177e4SLinus Torvalds 30431da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 30441da177e4SLinus Torvalds return 0; 30451da177e4SLinus Torvalds 30461da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 30471da177e4SLinus Torvalds if (error) 30481da177e4SLinus Torvalds return error; 30491da177e4SLinus Torvalds 30501da177e4SLinus Torvalds if (!new_dentry->d_inode) 3051a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 30521da177e4SLinus Torvalds else 30531da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 30541da177e4SLinus Torvalds if (error) 30551da177e4SLinus Torvalds return error; 30561da177e4SLinus Torvalds 3057acfa4380SAl Viro if (!old_dir->i_op->rename) 30581da177e4SLinus Torvalds return -EPERM; 30591da177e4SLinus Torvalds 30600eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 30610eeca283SRobert Love 30621da177e4SLinus Torvalds if (is_dir) 30631da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 30641da177e4SLinus Torvalds else 30651da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3066123df294SAl Viro if (!error) 3067123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 30685a190ae6SAl Viro new_dentry->d_inode, old_dentry); 30690eeca283SRobert Love fsnotify_oldname_free(old_name); 30700eeca283SRobert Love 30711da177e4SLinus Torvalds return error; 30721da177e4SLinus Torvalds } 30731da177e4SLinus Torvalds 30742e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 30752e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 30761da177e4SLinus Torvalds { 30771da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 30781da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 30791da177e4SLinus Torvalds struct dentry *trap; 30801da177e4SLinus Torvalds struct nameidata oldnd, newnd; 30812ad94ae6SAl Viro char *from; 30822ad94ae6SAl Viro char *to; 30832ad94ae6SAl Viro int error; 30841da177e4SLinus Torvalds 30852ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 30861da177e4SLinus Torvalds if (error) 30871da177e4SLinus Torvalds goto exit; 30881da177e4SLinus Torvalds 30892ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 30901da177e4SLinus Torvalds if (error) 30911da177e4SLinus Torvalds goto exit1; 30921da177e4SLinus Torvalds 30931da177e4SLinus Torvalds error = -EXDEV; 30944ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 30951da177e4SLinus Torvalds goto exit2; 30961da177e4SLinus Torvalds 30974ac91378SJan Blunck old_dir = oldnd.path.dentry; 30981da177e4SLinus Torvalds error = -EBUSY; 30991da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 31001da177e4SLinus Torvalds goto exit2; 31011da177e4SLinus Torvalds 31024ac91378SJan Blunck new_dir = newnd.path.dentry; 31031da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 31041da177e4SLinus Torvalds goto exit2; 31051da177e4SLinus Torvalds 31060612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 31070612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 31084e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 31090612d9fbSOGAWA Hirofumi 31101da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 31111da177e4SLinus Torvalds 311249705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 31131da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 31141da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 31151da177e4SLinus Torvalds goto exit3; 31161da177e4SLinus Torvalds /* source must exist */ 31171da177e4SLinus Torvalds error = -ENOENT; 31181da177e4SLinus Torvalds if (!old_dentry->d_inode) 31191da177e4SLinus Torvalds goto exit4; 31201da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 31211da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 31221da177e4SLinus Torvalds error = -ENOTDIR; 31231da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 31241da177e4SLinus Torvalds goto exit4; 31251da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 31261da177e4SLinus Torvalds goto exit4; 31271da177e4SLinus Torvalds } 31281da177e4SLinus Torvalds /* source should not be ancestor of target */ 31291da177e4SLinus Torvalds error = -EINVAL; 31301da177e4SLinus Torvalds if (old_dentry == trap) 31311da177e4SLinus Torvalds goto exit4; 313249705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 31331da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 31341da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 31351da177e4SLinus Torvalds goto exit4; 31361da177e4SLinus Torvalds /* target should not be an ancestor of source */ 31371da177e4SLinus Torvalds error = -ENOTEMPTY; 31381da177e4SLinus Torvalds if (new_dentry == trap) 31391da177e4SLinus Torvalds goto exit5; 31401da177e4SLinus Torvalds 31419079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 31429079b1ebSDave Hansen if (error) 31439079b1ebSDave Hansen goto exit5; 3144be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3145be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3146be6d3e56SKentaro Takeda if (error) 3147be6d3e56SKentaro Takeda goto exit6; 31481da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 31491da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 3150be6d3e56SKentaro Takeda exit6: 31519079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 31521da177e4SLinus Torvalds exit5: 31531da177e4SLinus Torvalds dput(new_dentry); 31541da177e4SLinus Torvalds exit4: 31551da177e4SLinus Torvalds dput(old_dentry); 31561da177e4SLinus Torvalds exit3: 31571da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 31581da177e4SLinus Torvalds exit2: 31591d957f9bSJan Blunck path_put(&newnd.path); 31602ad94ae6SAl Viro putname(to); 31611da177e4SLinus Torvalds exit1: 31621d957f9bSJan Blunck path_put(&oldnd.path); 31631da177e4SLinus Torvalds putname(from); 31642ad94ae6SAl Viro exit: 31651da177e4SLinus Torvalds return error; 31661da177e4SLinus Torvalds } 31671da177e4SLinus Torvalds 3168a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 31695590ff0dSUlrich Drepper { 31705590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 31715590ff0dSUlrich Drepper } 31725590ff0dSUlrich Drepper 31731da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 31741da177e4SLinus Torvalds { 31751da177e4SLinus Torvalds int len; 31761da177e4SLinus Torvalds 31771da177e4SLinus Torvalds len = PTR_ERR(link); 31781da177e4SLinus Torvalds if (IS_ERR(link)) 31791da177e4SLinus Torvalds goto out; 31801da177e4SLinus Torvalds 31811da177e4SLinus Torvalds len = strlen(link); 31821da177e4SLinus Torvalds if (len > (unsigned) buflen) 31831da177e4SLinus Torvalds len = buflen; 31841da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 31851da177e4SLinus Torvalds len = -EFAULT; 31861da177e4SLinus Torvalds out: 31871da177e4SLinus Torvalds return len; 31881da177e4SLinus Torvalds } 31891da177e4SLinus Torvalds 31901da177e4SLinus Torvalds /* 31911da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 31921da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 31931da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 31941da177e4SLinus Torvalds */ 31951da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 31961da177e4SLinus Torvalds { 31971da177e4SLinus Torvalds struct nameidata nd; 3198cc314eefSLinus Torvalds void *cookie; 3199694a1764SMarcin Slusarz int res; 3200cc314eefSLinus Torvalds 32011da177e4SLinus Torvalds nd.depth = 0; 3202cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3203694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3204694a1764SMarcin Slusarz return PTR_ERR(cookie); 3205694a1764SMarcin Slusarz 3206694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 32071da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3208cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3209694a1764SMarcin Slusarz return res; 32101da177e4SLinus Torvalds } 32111da177e4SLinus Torvalds 32121da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 32131da177e4SLinus Torvalds { 32141da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 32151da177e4SLinus Torvalds } 32161da177e4SLinus Torvalds 32171da177e4SLinus Torvalds /* get the link contents into pagecache */ 32181da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 32191da177e4SLinus Torvalds { 3220ebd09abbSDuane Griffin char *kaddr; 32211da177e4SLinus Torvalds struct page *page; 32221da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3223090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 32241da177e4SLinus Torvalds if (IS_ERR(page)) 32256fe6900eSNick Piggin return (char*)page; 32261da177e4SLinus Torvalds *ppage = page; 3227ebd09abbSDuane Griffin kaddr = kmap(page); 3228ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3229ebd09abbSDuane Griffin return kaddr; 32301da177e4SLinus Torvalds } 32311da177e4SLinus Torvalds 32321da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 32331da177e4SLinus Torvalds { 32341da177e4SLinus Torvalds struct page *page = NULL; 32351da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 32361da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 32371da177e4SLinus Torvalds if (page) { 32381da177e4SLinus Torvalds kunmap(page); 32391da177e4SLinus Torvalds page_cache_release(page); 32401da177e4SLinus Torvalds } 32411da177e4SLinus Torvalds return res; 32421da177e4SLinus Torvalds } 32431da177e4SLinus Torvalds 3244cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 32451da177e4SLinus Torvalds { 3246cc314eefSLinus Torvalds struct page *page = NULL; 32471da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 3248cc314eefSLinus Torvalds return page; 32491da177e4SLinus Torvalds } 32501da177e4SLinus Torvalds 3251cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 32521da177e4SLinus Torvalds { 3253cc314eefSLinus Torvalds struct page *page = cookie; 3254cc314eefSLinus Torvalds 3255cc314eefSLinus Torvalds if (page) { 32561da177e4SLinus Torvalds kunmap(page); 32571da177e4SLinus Torvalds page_cache_release(page); 32581da177e4SLinus Torvalds } 32591da177e4SLinus Torvalds } 32601da177e4SLinus Torvalds 326154566b2cSNick Piggin /* 326254566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 326354566b2cSNick Piggin */ 326454566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 32651da177e4SLinus Torvalds { 32661da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 32670adb25d2SKirill Korotaev struct page *page; 3268afddba49SNick Piggin void *fsdata; 3269beb497abSDmitriy Monakhov int err; 32701da177e4SLinus Torvalds char *kaddr; 327154566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 327254566b2cSNick Piggin if (nofs) 327354566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 32741da177e4SLinus Torvalds 32757e53cac4SNeilBrown retry: 3276afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 327754566b2cSNick Piggin flags, &page, &fsdata); 32781da177e4SLinus Torvalds if (err) 3279afddba49SNick Piggin goto fail; 3280afddba49SNick Piggin 32811da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 32821da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 32831da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 3284afddba49SNick Piggin 3285afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 3286afddba49SNick Piggin page, fsdata); 32871da177e4SLinus Torvalds if (err < 0) 32881da177e4SLinus Torvalds goto fail; 3289afddba49SNick Piggin if (err < len-1) 3290afddba49SNick Piggin goto retry; 3291afddba49SNick Piggin 32921da177e4SLinus Torvalds mark_inode_dirty(inode); 32931da177e4SLinus Torvalds return 0; 32941da177e4SLinus Torvalds fail: 32951da177e4SLinus Torvalds return err; 32961da177e4SLinus Torvalds } 32971da177e4SLinus Torvalds 32980adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 32990adb25d2SKirill Korotaev { 33000adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 330154566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 33020adb25d2SKirill Korotaev } 33030adb25d2SKirill Korotaev 330492e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 33051da177e4SLinus Torvalds .readlink = generic_readlink, 33061da177e4SLinus Torvalds .follow_link = page_follow_link_light, 33071da177e4SLinus Torvalds .put_link = page_put_link, 33081da177e4SLinus Torvalds }; 33091da177e4SLinus Torvalds 33102d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 3311cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one); 33121da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 33131da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 33141da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 33151da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 33161da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 33171da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 33181da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 33191da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 33201da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 33210adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 33221da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 33231da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 3324c9c6cac0SAl Viro EXPORT_SYMBOL(kern_path_parent); 3325d1811465SAl Viro EXPORT_SYMBOL(kern_path); 332616f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 3327f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 33288c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission); 33291da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 33301da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 33311da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 33321da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 33331da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 33341da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 33351da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 33361da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 33371da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 33381da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 33391da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 33401da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 33411da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 33421da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 3343