11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/namei.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* 81da177e4SLinus Torvalds * Some corrections by tytso. 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname 121da177e4SLinus Torvalds * lookup logic. 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture. 151da177e4SLinus Torvalds */ 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #include <linux/init.h> 181da177e4SLinus Torvalds #include <linux/module.h> 191da177e4SLinus Torvalds #include <linux/slab.h> 201da177e4SLinus Torvalds #include <linux/fs.h> 211da177e4SLinus Torvalds #include <linux/namei.h> 221da177e4SLinus Torvalds #include <linux/pagemap.h> 230eeca283SRobert Love #include <linux/fsnotify.h> 241da177e4SLinus Torvalds #include <linux/personality.h> 251da177e4SLinus Torvalds #include <linux/security.h> 266146f0d5SMimi Zohar #include <linux/ima.h> 271da177e4SLinus Torvalds #include <linux/syscalls.h> 281da177e4SLinus Torvalds #include <linux/mount.h> 291da177e4SLinus Torvalds #include <linux/audit.h> 3016f7e0feSRandy Dunlap #include <linux/capability.h> 31834f2a4aSTrond Myklebust #include <linux/file.h> 325590ff0dSUlrich Drepper #include <linux/fcntl.h> 3308ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h> 345ad4e53bSAl Viro #include <linux/fs_struct.h> 351da177e4SLinus Torvalds #include <asm/uaccess.h> 361da177e4SLinus Torvalds 37e81e3f4dSEric Paris #include "internal.h" 38e81e3f4dSEric Paris 391da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer] 401da177e4SLinus Torvalds * Fundamental changes in the pathname lookup mechanisms (namei) 411da177e4SLinus Torvalds * were necessary because of omirr. The reason is that omirr needs 421da177e4SLinus Torvalds * to know the _real_ pathname, not the user-supplied one, in case 431da177e4SLinus Torvalds * of symlinks (and also when transname replacements occur). 441da177e4SLinus Torvalds * 451da177e4SLinus Torvalds * The new code replaces the old recursive symlink resolution with 461da177e4SLinus Torvalds * an iterative one (in case of non-nested symlink chains). It does 471da177e4SLinus Torvalds * this with calls to <fs>_follow_link(). 481da177e4SLinus Torvalds * As a side effect, dir_namei(), _namei() and follow_link() are now 491da177e4SLinus Torvalds * replaced with a single function lookup_dentry() that can handle all 501da177e4SLinus Torvalds * the special cases of the former code. 511da177e4SLinus Torvalds * 521da177e4SLinus Torvalds * With the new dcache, the pathname is stored at each inode, at least as 531da177e4SLinus Torvalds * long as the refcount of the inode is positive. As a side effect, the 541da177e4SLinus Torvalds * size of the dcache depends on the inode cache and thus is dynamic. 551da177e4SLinus Torvalds * 561da177e4SLinus Torvalds * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink 571da177e4SLinus Torvalds * resolution to correspond with current state of the code. 581da177e4SLinus Torvalds * 591da177e4SLinus Torvalds * Note that the symlink resolution is not *completely* iterative. 601da177e4SLinus Torvalds * There is still a significant amount of tail- and mid- recursion in 611da177e4SLinus Torvalds * the algorithm. Also, note that <fs>_readlink() is not used in 621da177e4SLinus Torvalds * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() 631da177e4SLinus Torvalds * may return different results than <fs>_follow_link(). Many virtual 641da177e4SLinus Torvalds * filesystems (including /proc) exhibit this behavior. 651da177e4SLinus Torvalds */ 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: 681da177e4SLinus Torvalds * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL 691da177e4SLinus Torvalds * and the name already exists in form of a symlink, try to create the new 701da177e4SLinus Torvalds * name indicated by the symlink. The old code always complained that the 711da177e4SLinus Torvalds * name already exists, due to not following the symlink even if its target 721da177e4SLinus Torvalds * is nonexistent. The new semantics affects also mknod() and link() when 7325985edcSLucas De Marchi * the name is a symlink pointing to a non-existent name. 741da177e4SLinus Torvalds * 751da177e4SLinus Torvalds * I don't know which semantics is the right one, since I have no access 761da177e4SLinus Torvalds * to standards. But I found by trial that HP-UX 9.0 has the full "new" 771da177e4SLinus Torvalds * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the 781da177e4SLinus Torvalds * "old" one. Personally, I think the new semantics is much more logical. 791da177e4SLinus Torvalds * Note that "ln old new" where "new" is a symlink pointing to a non-existing 801da177e4SLinus Torvalds * file does succeed in both HP-UX and SunOs, but not in Solaris 811da177e4SLinus Torvalds * and in the old Linux semantics. 821da177e4SLinus Torvalds */ 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink 851da177e4SLinus Torvalds * semantics. See the comments in "open_namei" and "do_link" below. 861da177e4SLinus Torvalds * 871da177e4SLinus Torvalds * [10-Sep-98 Alan Modra] Another symlink change. 881da177e4SLinus Torvalds */ 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: 911da177e4SLinus Torvalds * inside the path - always follow. 921da177e4SLinus Torvalds * in the last component in creation/removal/renaming - never follow. 931da177e4SLinus Torvalds * if LOOKUP_FOLLOW passed - follow. 941da177e4SLinus Torvalds * if the pathname has trailing slashes - follow. 951da177e4SLinus Torvalds * otherwise - don't follow. 961da177e4SLinus Torvalds * (applied in that order). 971da177e4SLinus Torvalds * 981da177e4SLinus Torvalds * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT 991da177e4SLinus Torvalds * restored for 2.4. This is the last surviving part of old 4.2BSD bug. 1001da177e4SLinus Torvalds * During the 2.4 we need to fix the userland stuff depending on it - 1011da177e4SLinus Torvalds * hopefully we will be able to get rid of that wart in 2.5. So far only 1021da177e4SLinus Torvalds * XEmacs seems to be relying on it... 1031da177e4SLinus Torvalds */ 1041da177e4SLinus Torvalds /* 1051da177e4SLinus Torvalds * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) 106a11f3a05SArjan van de Ven * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives 1071da177e4SLinus Torvalds * any extra contention... 1081da177e4SLinus Torvalds */ 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional 1111da177e4SLinus Torvalds * checking and hopefully speeding things up, we copy filenames to the 1121da177e4SLinus Torvalds * kernel data space before using them.. 1131da177e4SLinus Torvalds * 1141da177e4SLinus Torvalds * POSIX.1 2.4: an empty pathname is invalid (ENOENT). 1151da177e4SLinus Torvalds * PATH_MAX includes the nul terminator --RR. 1161da177e4SLinus Torvalds */ 117858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page) 1181da177e4SLinus Torvalds { 1191da177e4SLinus Torvalds int retval; 1201da177e4SLinus Torvalds unsigned long len = PATH_MAX; 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds if (!segment_eq(get_fs(), KERNEL_DS)) { 1231da177e4SLinus Torvalds if ((unsigned long) filename >= TASK_SIZE) 1241da177e4SLinus Torvalds return -EFAULT; 1251da177e4SLinus Torvalds if (TASK_SIZE - (unsigned long) filename < PATH_MAX) 1261da177e4SLinus Torvalds len = TASK_SIZE - (unsigned long) filename; 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds retval = strncpy_from_user(page, filename, len); 1301da177e4SLinus Torvalds if (retval > 0) { 1311da177e4SLinus Torvalds if (retval < len) 1321da177e4SLinus Torvalds return 0; 1331da177e4SLinus Torvalds return -ENAMETOOLONG; 1341da177e4SLinus Torvalds } else if (!retval) 1351da177e4SLinus Torvalds retval = -ENOENT; 1361da177e4SLinus Torvalds return retval; 1371da177e4SLinus Torvalds } 1381da177e4SLinus Torvalds 139f52e0c11SAl Viro static char *getname_flags(const char __user * filename, int flags) 1401da177e4SLinus Torvalds { 1411da177e4SLinus Torvalds char *tmp, *result; 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds result = ERR_PTR(-ENOMEM); 1441da177e4SLinus Torvalds tmp = __getname(); 1451da177e4SLinus Torvalds if (tmp) { 1461da177e4SLinus Torvalds int retval = do_getname(filename, tmp); 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds result = tmp; 1491da177e4SLinus Torvalds if (retval < 0) { 150f52e0c11SAl Viro if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) { 1511da177e4SLinus Torvalds __putname(tmp); 1521da177e4SLinus Torvalds result = ERR_PTR(retval); 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds } 155f52e0c11SAl Viro } 1561da177e4SLinus Torvalds audit_getname(result); 1571da177e4SLinus Torvalds return result; 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds 160f52e0c11SAl Viro char *getname(const char __user * filename) 161f52e0c11SAl Viro { 162f52e0c11SAl Viro return getname_flags(filename, 0); 163f52e0c11SAl Viro } 164f52e0c11SAl Viro 1651da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 1661da177e4SLinus Torvalds void putname(const char *name) 1671da177e4SLinus Torvalds { 1685ac3a9c2SAl Viro if (unlikely(!audit_dummy_context())) 1691da177e4SLinus Torvalds audit_putname(name); 1701da177e4SLinus Torvalds else 1711da177e4SLinus Torvalds __putname(name); 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds EXPORT_SYMBOL(putname); 1741da177e4SLinus Torvalds #endif 1751da177e4SLinus Torvalds 1765909ccaaSLinus Torvalds /* 1775909ccaaSLinus Torvalds * This does basic POSIX ACL permission checking 1785909ccaaSLinus Torvalds */ 179b74c79e9SNick Piggin static int acl_permission_check(struct inode *inode, int mask, unsigned int flags, 180b74c79e9SNick Piggin int (*check_acl)(struct inode *inode, int mask, unsigned int flags)) 1815909ccaaSLinus Torvalds { 18226cf46beSLinus Torvalds unsigned int mode = inode->i_mode; 1835909ccaaSLinus Torvalds 1845909ccaaSLinus Torvalds mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 1855909ccaaSLinus Torvalds 186e795b717SSerge E. Hallyn if (current_user_ns() != inode_userns(inode)) 187e795b717SSerge E. Hallyn goto other_perms; 188e795b717SSerge E. Hallyn 1895909ccaaSLinus Torvalds if (current_fsuid() == inode->i_uid) 1905909ccaaSLinus Torvalds mode >>= 6; 1915909ccaaSLinus Torvalds else { 1925909ccaaSLinus Torvalds if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) { 193b74c79e9SNick Piggin int error = check_acl(inode, mask, flags); 1945909ccaaSLinus Torvalds if (error != -EAGAIN) 1955909ccaaSLinus Torvalds return error; 1965909ccaaSLinus Torvalds } 1975909ccaaSLinus Torvalds 1985909ccaaSLinus Torvalds if (in_group_p(inode->i_gid)) 1995909ccaaSLinus Torvalds mode >>= 3; 2005909ccaaSLinus Torvalds } 2015909ccaaSLinus Torvalds 202e795b717SSerge E. Hallyn other_perms: 2035909ccaaSLinus Torvalds /* 2045909ccaaSLinus Torvalds * If the DACs are ok we don't need any capability check. 2055909ccaaSLinus Torvalds */ 2065909ccaaSLinus Torvalds if ((mask & ~mode) == 0) 2075909ccaaSLinus Torvalds return 0; 2085909ccaaSLinus Torvalds return -EACCES; 2095909ccaaSLinus Torvalds } 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds /** 2121da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 2131da177e4SLinus Torvalds * @inode: inode to check access rights for 2141da177e4SLinus Torvalds * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 2151da177e4SLinus Torvalds * @check_acl: optional callback to check for Posix ACLs 21639191628SRandy Dunlap * @flags: IPERM_FLAG_ flags. 2171da177e4SLinus Torvalds * 2181da177e4SLinus Torvalds * Used to check for read/write/execute permissions on a file. 2191da177e4SLinus Torvalds * We use "fsuid" for this, letting us set arbitrary permissions 2201da177e4SLinus Torvalds * for filesystem access without changing the "normal" uids which 221b74c79e9SNick Piggin * are used for other things. 222b74c79e9SNick Piggin * 223b74c79e9SNick Piggin * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk 224b74c79e9SNick Piggin * request cannot be satisfied (eg. requires blocking or too much complexity). 225b74c79e9SNick Piggin * It would then be called again in ref-walk mode. 2261da177e4SLinus Torvalds */ 227b74c79e9SNick Piggin int generic_permission(struct inode *inode, int mask, unsigned int flags, 228b74c79e9SNick Piggin int (*check_acl)(struct inode *inode, int mask, unsigned int flags)) 2291da177e4SLinus Torvalds { 2305909ccaaSLinus Torvalds int ret; 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds /* 2335909ccaaSLinus Torvalds * Do the basic POSIX ACL permission checks. 2341da177e4SLinus Torvalds */ 235b74c79e9SNick Piggin ret = acl_permission_check(inode, mask, flags, check_acl); 2365909ccaaSLinus Torvalds if (ret != -EACCES) 2375909ccaaSLinus Torvalds return ret; 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds /* 2401da177e4SLinus Torvalds * Read/write DACs are always overridable. 2411da177e4SLinus Torvalds * Executable DACs are overridable if at least one exec bit is set. 2421da177e4SLinus Torvalds */ 243f696a365SMiklos Szeredi if (!(mask & MAY_EXEC) || execute_ok(inode)) 244e795b717SSerge E. Hallyn if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE)) 2451da177e4SLinus Torvalds return 0; 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds /* 2481da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 2491da177e4SLinus Torvalds */ 2507ea66001SSerge E. Hallyn mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 2511da177e4SLinus Torvalds if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE))) 252e795b717SSerge E. Hallyn if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH)) 2531da177e4SLinus Torvalds return 0; 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds return -EACCES; 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds 258cb23beb5SChristoph Hellwig /** 259cb23beb5SChristoph Hellwig * inode_permission - check for access rights to a given inode 260cb23beb5SChristoph Hellwig * @inode: inode to check permission on 261cb23beb5SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 262cb23beb5SChristoph Hellwig * 263cb23beb5SChristoph Hellwig * Used to check for read/write/execute permissions on an inode. 264cb23beb5SChristoph Hellwig * We use "fsuid" for this, letting us set arbitrary permissions 265cb23beb5SChristoph Hellwig * for filesystem access without changing the "normal" uids which 266cb23beb5SChristoph Hellwig * are used for other things. 267cb23beb5SChristoph Hellwig */ 268f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask) 2691da177e4SLinus Torvalds { 270e6305c43SAl Viro int retval; 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds if (mask & MAY_WRITE) { 27322590e41SMiklos Szeredi umode_t mode = inode->i_mode; 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds /* 2761da177e4SLinus Torvalds * Nobody gets write access to a read-only fs. 2771da177e4SLinus Torvalds */ 2781da177e4SLinus Torvalds if (IS_RDONLY(inode) && 2791da177e4SLinus Torvalds (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 2801da177e4SLinus Torvalds return -EROFS; 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds /* 2831da177e4SLinus Torvalds * Nobody gets write access to an immutable file. 2841da177e4SLinus Torvalds */ 2851da177e4SLinus Torvalds if (IS_IMMUTABLE(inode)) 2861da177e4SLinus Torvalds return -EACCES; 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds 289acfa4380SAl Viro if (inode->i_op->permission) 290b74c79e9SNick Piggin retval = inode->i_op->permission(inode, mask, 0); 291f696a365SMiklos Szeredi else 292b74c79e9SNick Piggin retval = generic_permission(inode, mask, 0, 293b74c79e9SNick Piggin inode->i_op->check_acl); 294f696a365SMiklos Szeredi 2951da177e4SLinus Torvalds if (retval) 2961da177e4SLinus Torvalds return retval; 2971da177e4SLinus Torvalds 29808ce5f16SSerge E. Hallyn retval = devcgroup_inode_permission(inode, mask); 29908ce5f16SSerge E. Hallyn if (retval) 30008ce5f16SSerge E. Hallyn return retval; 30108ce5f16SSerge E. Hallyn 302d09ca739SEric Paris return security_inode_permission(inode, mask); 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds 305e4543eddSChristoph Hellwig /** 3068c744fb8SChristoph Hellwig * file_permission - check for additional access rights to a given file 3078c744fb8SChristoph Hellwig * @file: file to check access rights for 3088c744fb8SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 3098c744fb8SChristoph Hellwig * 3108c744fb8SChristoph Hellwig * Used to check for read/write/execute permissions on an already opened 3118c744fb8SChristoph Hellwig * file. 3128c744fb8SChristoph Hellwig * 3138c744fb8SChristoph Hellwig * Note: 3148c744fb8SChristoph Hellwig * Do not use this function in new code. All access checks should 315cb23beb5SChristoph Hellwig * be done using inode_permission(). 3168c744fb8SChristoph Hellwig */ 3178c744fb8SChristoph Hellwig int file_permission(struct file *file, int mask) 3188c744fb8SChristoph Hellwig { 319f419a2e3SAl Viro return inode_permission(file->f_path.dentry->d_inode, mask); 3208c744fb8SChristoph Hellwig } 3218c744fb8SChristoph Hellwig 3221da177e4SLinus Torvalds /* 3231da177e4SLinus Torvalds * get_write_access() gets write permission for a file. 3241da177e4SLinus Torvalds * put_write_access() releases this write permission. 3251da177e4SLinus Torvalds * This is used for regular files. 3261da177e4SLinus Torvalds * We cannot support write (and maybe mmap read-write shared) accesses and 3271da177e4SLinus Torvalds * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode 3281da177e4SLinus Torvalds * can have the following values: 3291da177e4SLinus Torvalds * 0: no writers, no VM_DENYWRITE mappings 3301da177e4SLinus Torvalds * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist 3311da177e4SLinus Torvalds * > 0: (i_writecount) users are writing to the file. 3321da177e4SLinus Torvalds * 3331da177e4SLinus Torvalds * Normally we operate on that counter with atomic_{inc,dec} and it's safe 3341da177e4SLinus Torvalds * except for the cases where we don't hold i_writecount yet. Then we need to 3351da177e4SLinus Torvalds * use {get,deny}_write_access() - these functions check the sign and refuse 3361da177e4SLinus Torvalds * to do the change if sign is wrong. Exclusion between them is provided by 3371da177e4SLinus Torvalds * the inode->i_lock spinlock. 3381da177e4SLinus Torvalds */ 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds int get_write_access(struct inode * inode) 3411da177e4SLinus Torvalds { 3421da177e4SLinus Torvalds spin_lock(&inode->i_lock); 3431da177e4SLinus Torvalds if (atomic_read(&inode->i_writecount) < 0) { 3441da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3451da177e4SLinus Torvalds return -ETXTBSY; 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds atomic_inc(&inode->i_writecount); 3481da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds return 0; 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds int deny_write_access(struct file * file) 3541da177e4SLinus Torvalds { 3550f7fc9e4SJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds spin_lock(&inode->i_lock); 3581da177e4SLinus Torvalds if (atomic_read(&inode->i_writecount) > 0) { 3591da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3601da177e4SLinus Torvalds return -ETXTBSY; 3611da177e4SLinus Torvalds } 3621da177e4SLinus Torvalds atomic_dec(&inode->i_writecount); 3631da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds return 0; 3661da177e4SLinus Torvalds } 3671da177e4SLinus Torvalds 3681d957f9bSJan Blunck /** 3695dd784d0SJan Blunck * path_get - get a reference to a path 3705dd784d0SJan Blunck * @path: path to get the reference to 3715dd784d0SJan Blunck * 3725dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 3735dd784d0SJan Blunck */ 3745dd784d0SJan Blunck void path_get(struct path *path) 3755dd784d0SJan Blunck { 3765dd784d0SJan Blunck mntget(path->mnt); 3775dd784d0SJan Blunck dget(path->dentry); 3785dd784d0SJan Blunck } 3795dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 3805dd784d0SJan Blunck 3815dd784d0SJan Blunck /** 3821d957f9bSJan Blunck * path_put - put a reference to a path 3831d957f9bSJan Blunck * @path: path to put the reference to 3841d957f9bSJan Blunck * 3851d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 3861d957f9bSJan Blunck */ 3871d957f9bSJan Blunck void path_put(struct path *path) 3881da177e4SLinus Torvalds { 3891d957f9bSJan Blunck dput(path->dentry); 3901d957f9bSJan Blunck mntput(path->mnt); 3911da177e4SLinus Torvalds } 3921d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 3931da177e4SLinus Torvalds 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 8158aef1884SAl Viro if (!*need_mntput) { 8168aef1884SAl Viro /* lock_mount() may release path->mnt on error */ 8178aef1884SAl Viro mntget(path->mnt); 8188aef1884SAl Viro *need_mntput = true; 8198aef1884SAl Viro } 82019a167afSAl Viro err = finish_automount(mnt, path); 821ea5b778aSDavid Howells 822ea5b778aSDavid Howells switch (err) { 823ea5b778aSDavid Howells case -EBUSY: 824ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 82519a167afSAl Viro return 0; 826ea5b778aSDavid Howells case 0: 8278aef1884SAl Viro path_put(path); 8289875cf80SDavid Howells path->mnt = mnt; 8299875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 8309875cf80SDavid Howells return 0; 83119a167afSAl Viro default: 83219a167afSAl Viro return err; 8339875cf80SDavid Howells } 83419a167afSAl Viro 835ea5b778aSDavid Howells } 8369875cf80SDavid Howells 8379875cf80SDavid Howells /* 8389875cf80SDavid Howells * Handle a dentry that is managed in some way. 839cc53ce53SDavid Howells * - Flagged for transit management (autofs) 8409875cf80SDavid Howells * - Flagged as mountpoint 8419875cf80SDavid Howells * - Flagged as automount point 8429875cf80SDavid Howells * 8439875cf80SDavid Howells * This may only be called in refwalk mode. 8449875cf80SDavid Howells * 8459875cf80SDavid Howells * Serialization is taken care of in namespace.c 8469875cf80SDavid Howells */ 8479875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 8489875cf80SDavid Howells { 8498aef1884SAl Viro struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */ 8509875cf80SDavid Howells unsigned managed; 8519875cf80SDavid Howells bool need_mntput = false; 8528aef1884SAl Viro int ret = 0; 8539875cf80SDavid Howells 8549875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 8559875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 8569875cf80SDavid Howells * the components of that value change under us */ 8579875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 8589875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 8599875cf80SDavid Howells unlikely(managed != 0)) { 860cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 861cc53ce53SDavid Howells * being held. */ 862cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 863cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 864cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 8651aed3e42SAl Viro ret = path->dentry->d_op->d_manage(path->dentry, false); 866cc53ce53SDavid Howells if (ret < 0) 8678aef1884SAl Viro break; 868cc53ce53SDavid Howells } 869cc53ce53SDavid Howells 8709875cf80SDavid Howells /* Transit to a mounted filesystem. */ 8719875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 8729875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 8739875cf80SDavid Howells if (mounted) { 8749875cf80SDavid Howells dput(path->dentry); 8759875cf80SDavid Howells if (need_mntput) 876463ffb2eSAl Viro mntput(path->mnt); 877463ffb2eSAl Viro path->mnt = mounted; 878463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 8799875cf80SDavid Howells need_mntput = true; 8809875cf80SDavid Howells continue; 881463ffb2eSAl Viro } 882463ffb2eSAl Viro 8839875cf80SDavid Howells /* Something is mounted on this dentry in another 8849875cf80SDavid Howells * namespace and/or whatever was mounted there in this 8859875cf80SDavid Howells * namespace got unmounted before we managed to get the 8869875cf80SDavid Howells * vfsmount_lock */ 8871da177e4SLinus Torvalds } 8889875cf80SDavid Howells 8899875cf80SDavid Howells /* Handle an automount point */ 8909875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 8919875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 8929875cf80SDavid Howells if (ret < 0) 8938aef1884SAl Viro break; 8949875cf80SDavid Howells continue; 8959875cf80SDavid Howells } 8969875cf80SDavid Howells 8979875cf80SDavid Howells /* We didn't change the current path point */ 8989875cf80SDavid Howells break; 8999875cf80SDavid Howells } 9008aef1884SAl Viro 9018aef1884SAl Viro if (need_mntput && path->mnt == mnt) 9028aef1884SAl Viro mntput(path->mnt); 9038aef1884SAl Viro if (ret == -EISDIR) 9048aef1884SAl Viro ret = 0; 9058aef1884SAl Viro return ret; 9061da177e4SLinus Torvalds } 9071da177e4SLinus Torvalds 908cc53ce53SDavid Howells int follow_down_one(struct path *path) 9091da177e4SLinus Torvalds { 9101da177e4SLinus Torvalds struct vfsmount *mounted; 9111da177e4SLinus Torvalds 9121c755af4SAl Viro mounted = lookup_mnt(path); 9131da177e4SLinus Torvalds if (mounted) { 9149393bd07SAl Viro dput(path->dentry); 9159393bd07SAl Viro mntput(path->mnt); 9169393bd07SAl Viro path->mnt = mounted; 9179393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 9181da177e4SLinus Torvalds return 1; 9191da177e4SLinus Torvalds } 9201da177e4SLinus Torvalds return 0; 9211da177e4SLinus Torvalds } 9221da177e4SLinus Torvalds 92362a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry) 92462a7375eSIan Kent { 92562a7375eSIan Kent return (dentry->d_flags & DCACHE_MANAGE_TRANSIT && 92662a7375eSIan Kent dentry->d_op->d_manage(dentry, true) < 0); 92762a7375eSIan Kent } 92862a7375eSIan Kent 9299875cf80SDavid Howells /* 930287548e4SAl Viro * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if 931287548e4SAl Viro * we meet a managed dentry that would need blocking. 9329875cf80SDavid Howells */ 9339875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 934287548e4SAl Viro struct inode **inode) 9359875cf80SDavid Howells { 93662a7375eSIan Kent for (;;) { 9379875cf80SDavid Howells struct vfsmount *mounted; 93862a7375eSIan Kent /* 93962a7375eSIan Kent * Don't forget we might have a non-mountpoint managed dentry 94062a7375eSIan Kent * that wants to block transit. 94162a7375eSIan Kent */ 94262a7375eSIan Kent *inode = path->dentry->d_inode; 943287548e4SAl Viro if (unlikely(managed_dentry_might_block(path->dentry))) 944ab90911fSDavid Howells return false; 94562a7375eSIan Kent 94662a7375eSIan Kent if (!d_mountpoint(path->dentry)) 94762a7375eSIan Kent break; 94862a7375eSIan Kent 9499875cf80SDavid Howells mounted = __lookup_mnt(path->mnt, path->dentry, 1); 9509875cf80SDavid Howells if (!mounted) 9519875cf80SDavid Howells break; 9529875cf80SDavid Howells path->mnt = mounted; 9539875cf80SDavid Howells path->dentry = mounted->mnt_root; 9549875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 9559875cf80SDavid Howells } 9569875cf80SDavid Howells return true; 9579875cf80SDavid Howells } 9589875cf80SDavid Howells 959dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd) 960287548e4SAl Viro { 961dea39376SAl Viro while (d_mountpoint(nd->path.dentry)) { 962287548e4SAl Viro struct vfsmount *mounted; 963dea39376SAl Viro mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1); 964287548e4SAl Viro if (!mounted) 965287548e4SAl Viro break; 966dea39376SAl Viro nd->path.mnt = mounted; 967dea39376SAl Viro nd->path.dentry = mounted->mnt_root; 968dea39376SAl Viro nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 969287548e4SAl Viro } 970287548e4SAl Viro } 971287548e4SAl Viro 97231e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 97331e6b01fSNick Piggin { 97431e6b01fSNick Piggin set_root_rcu(nd); 97531e6b01fSNick Piggin 97631e6b01fSNick Piggin while (1) { 97731e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 97831e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 97931e6b01fSNick Piggin break; 98031e6b01fSNick Piggin } 98131e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 98231e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 98331e6b01fSNick Piggin struct dentry *parent = old->d_parent; 98431e6b01fSNick Piggin unsigned seq; 98531e6b01fSNick Piggin 98631e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 98731e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 988ef7562d5SAl Viro goto failed; 98931e6b01fSNick Piggin nd->path.dentry = parent; 99031e6b01fSNick Piggin nd->seq = seq; 99131e6b01fSNick Piggin break; 99231e6b01fSNick Piggin } 99331e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 99431e6b01fSNick Piggin break; 99531e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 99631e6b01fSNick Piggin } 997dea39376SAl Viro follow_mount_rcu(nd); 998dea39376SAl Viro nd->inode = nd->path.dentry->d_inode; 99931e6b01fSNick Piggin return 0; 1000ef7562d5SAl Viro 1001ef7562d5SAl Viro failed: 1002ef7562d5SAl Viro nd->flags &= ~LOOKUP_RCU; 10035b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1004ef7562d5SAl Viro nd->root.mnt = NULL; 1005ef7562d5SAl Viro rcu_read_unlock(); 1006ef7562d5SAl Viro br_read_unlock(vfsmount_lock); 1007ef7562d5SAl Viro return -ECHILD; 100831e6b01fSNick Piggin } 100931e6b01fSNick Piggin 10109875cf80SDavid Howells /* 1011cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 1012cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 1013cc53ce53SDavid Howells * caller is permitted to proceed or not. 1014cc53ce53SDavid Howells * 1015cc53ce53SDavid Howells * Care must be taken as namespace_sem may be held (indicated by mounting_here 1016cc53ce53SDavid Howells * being true). 1017cc53ce53SDavid Howells */ 10187cc90cc3SAl Viro int follow_down(struct path *path) 1019cc53ce53SDavid Howells { 1020cc53ce53SDavid Howells unsigned managed; 1021cc53ce53SDavid Howells int ret; 1022cc53ce53SDavid Howells 1023cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 1024cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 1025cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 1026cc53ce53SDavid Howells * being held. 1027cc53ce53SDavid Howells * 1028cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 1029cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 1030cc53ce53SDavid Howells * other than its daemon the right to mount on its 1031cc53ce53SDavid Howells * superstructure. 1032cc53ce53SDavid Howells * 1033cc53ce53SDavid Howells * The filesystem may sleep at this point. 1034cc53ce53SDavid Howells */ 1035cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 1036cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 1037cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 1038ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 10391aed3e42SAl Viro path->dentry, false); 1040cc53ce53SDavid Howells if (ret < 0) 1041cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 1042cc53ce53SDavid Howells } 1043cc53ce53SDavid Howells 1044cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 1045cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 1046cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 1047cc53ce53SDavid Howells if (!mounted) 1048cc53ce53SDavid Howells break; 1049cc53ce53SDavid Howells dput(path->dentry); 1050cc53ce53SDavid Howells mntput(path->mnt); 1051cc53ce53SDavid Howells path->mnt = mounted; 1052cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 1053cc53ce53SDavid Howells continue; 1054cc53ce53SDavid Howells } 1055cc53ce53SDavid Howells 1056cc53ce53SDavid Howells /* Don't handle automount points here */ 1057cc53ce53SDavid Howells break; 1058cc53ce53SDavid Howells } 1059cc53ce53SDavid Howells return 0; 1060cc53ce53SDavid Howells } 1061cc53ce53SDavid Howells 1062cc53ce53SDavid Howells /* 10639875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 10649875cf80SDavid Howells */ 10659875cf80SDavid Howells static void follow_mount(struct path *path) 10669875cf80SDavid Howells { 10679875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 10689875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 10699875cf80SDavid Howells if (!mounted) 10709875cf80SDavid Howells break; 10719875cf80SDavid Howells dput(path->dentry); 10729875cf80SDavid Howells mntput(path->mnt); 10739875cf80SDavid Howells path->mnt = mounted; 10749875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 10759875cf80SDavid Howells } 10769875cf80SDavid Howells } 10779875cf80SDavid Howells 107831e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 10791da177e4SLinus Torvalds { 10802a737871SAl Viro set_root(nd); 1081e518ddb7SAndreas Mohr 10821da177e4SLinus Torvalds while(1) { 10834ac91378SJan Blunck struct dentry *old = nd->path.dentry; 10841da177e4SLinus Torvalds 10852a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 10862a737871SAl Viro nd->path.mnt == nd->root.mnt) { 10871da177e4SLinus Torvalds break; 10881da177e4SLinus Torvalds } 10894ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 10903088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 10913088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 10921da177e4SLinus Torvalds dput(old); 10931da177e4SLinus Torvalds break; 10941da177e4SLinus Torvalds } 10953088dd70SAl Viro if (!follow_up(&nd->path)) 10961da177e4SLinus Torvalds break; 10971da177e4SLinus Torvalds } 109879ed0226SAl Viro follow_mount(&nd->path); 109931e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 11001da177e4SLinus Torvalds } 11011da177e4SLinus Torvalds 11021da177e4SLinus Torvalds /* 1103baa03890SNick Piggin * Allocate a dentry with name and parent, and perform a parent 1104baa03890SNick Piggin * directory ->lookup on it. Returns the new dentry, or ERR_PTR 1105baa03890SNick Piggin * on error. parent->d_inode->i_mutex must be held. d_lookup must 1106baa03890SNick Piggin * have verified that no child exists while under i_mutex. 1107baa03890SNick Piggin */ 1108baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent, 1109baa03890SNick Piggin struct qstr *name, struct nameidata *nd) 1110baa03890SNick Piggin { 1111baa03890SNick Piggin struct inode *inode = parent->d_inode; 1112baa03890SNick Piggin struct dentry *dentry; 1113baa03890SNick Piggin struct dentry *old; 1114baa03890SNick Piggin 1115baa03890SNick Piggin /* Don't create child dentry for a dead directory. */ 1116baa03890SNick Piggin if (unlikely(IS_DEADDIR(inode))) 1117baa03890SNick Piggin return ERR_PTR(-ENOENT); 1118baa03890SNick Piggin 1119baa03890SNick Piggin dentry = d_alloc(parent, name); 1120baa03890SNick Piggin if (unlikely(!dentry)) 1121baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1122baa03890SNick Piggin 1123baa03890SNick Piggin old = inode->i_op->lookup(inode, dentry, nd); 1124baa03890SNick Piggin if (unlikely(old)) { 1125baa03890SNick Piggin dput(dentry); 1126baa03890SNick Piggin dentry = old; 1127baa03890SNick Piggin } 1128baa03890SNick Piggin return dentry; 1129baa03890SNick Piggin } 1130baa03890SNick Piggin 1131baa03890SNick Piggin /* 11321da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 11331da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 11341da177e4SLinus Torvalds * It _is_ time-critical. 11351da177e4SLinus Torvalds */ 11361da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name, 113731e6b01fSNick Piggin struct path *path, struct inode **inode) 11381da177e4SLinus Torvalds { 11394ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 114031e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 11415a18fff2SAl Viro int need_reval = 1; 11425a18fff2SAl Viro int status = 1; 11439875cf80SDavid Howells int err; 11449875cf80SDavid Howells 11453cac260aSAl Viro /* 1146b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1147b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1148b04f784eSNick Piggin * do the non-racy lookup, below. 1149b04f784eSNick Piggin */ 115031e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 115131e6b01fSNick Piggin unsigned seq; 115231e6b01fSNick Piggin *inode = nd->inode; 115331e6b01fSNick Piggin dentry = __d_lookup_rcu(parent, name, &seq, inode); 11545a18fff2SAl Viro if (!dentry) 11555a18fff2SAl Viro goto unlazy; 11565a18fff2SAl Viro 115731e6b01fSNick Piggin /* Memory barrier in read_seqcount_begin of child is enough */ 115831e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 115931e6b01fSNick Piggin return -ECHILD; 116031e6b01fSNick Piggin nd->seq = seq; 11615a18fff2SAl Viro 116224643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 11635a18fff2SAl Viro status = d_revalidate(dentry, nd); 11645a18fff2SAl Viro if (unlikely(status <= 0)) { 11655a18fff2SAl Viro if (status != -ECHILD) 11665a18fff2SAl Viro need_reval = 0; 11675a18fff2SAl Viro goto unlazy; 11685a18fff2SAl Viro } 116924643087SAl Viro } 117031e6b01fSNick Piggin path->mnt = mnt; 117131e6b01fSNick Piggin path->dentry = dentry; 1172d6e9bd25SAl Viro if (unlikely(!__follow_mount_rcu(nd, path, inode))) 1173d6e9bd25SAl Viro goto unlazy; 1174d6e9bd25SAl Viro if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 1175d6e9bd25SAl Viro goto unlazy; 11769875cf80SDavid Howells return 0; 11775a18fff2SAl Viro unlazy: 117819660af7SAl Viro if (unlazy_walk(nd, dentry)) 11795a18fff2SAl Viro return -ECHILD; 11805a18fff2SAl Viro } else { 118131e6b01fSNick Piggin dentry = __d_lookup(parent, name); 118224643087SAl Viro } 11835a18fff2SAl Viro 11845a18fff2SAl Viro retry: 11855a18fff2SAl Viro if (unlikely(!dentry)) { 11865a18fff2SAl Viro struct inode *dir = parent->d_inode; 11875a18fff2SAl Viro BUG_ON(nd->inode != dir); 11885a18fff2SAl Viro 11895a18fff2SAl Viro mutex_lock(&dir->i_mutex); 11905a18fff2SAl Viro dentry = d_lookup(parent, name); 11915a18fff2SAl Viro if (likely(!dentry)) { 11925a18fff2SAl Viro dentry = d_alloc_and_lookup(parent, name, nd); 11935a18fff2SAl Viro if (IS_ERR(dentry)) { 11945a18fff2SAl Viro mutex_unlock(&dir->i_mutex); 11955a18fff2SAl Viro return PTR_ERR(dentry); 11965a18fff2SAl Viro } 11975a18fff2SAl Viro /* known good */ 11985a18fff2SAl Viro need_reval = 0; 11995a18fff2SAl Viro status = 1; 12005a18fff2SAl Viro } 12015a18fff2SAl Viro mutex_unlock(&dir->i_mutex); 12025a18fff2SAl Viro } 12035a18fff2SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) 12045a18fff2SAl Viro status = d_revalidate(dentry, nd); 12055a18fff2SAl Viro if (unlikely(status <= 0)) { 12065a18fff2SAl Viro if (status < 0) { 12075a18fff2SAl Viro dput(dentry); 12085a18fff2SAl Viro return status; 12095a18fff2SAl Viro } 12105a18fff2SAl Viro if (!d_invalidate(dentry)) { 12115a18fff2SAl Viro dput(dentry); 12125a18fff2SAl Viro dentry = NULL; 12135a18fff2SAl Viro need_reval = 1; 12145a18fff2SAl Viro goto retry; 12155a18fff2SAl Viro } 12165a18fff2SAl Viro } 12175a18fff2SAl Viro 12181da177e4SLinus Torvalds path->mnt = mnt; 12191da177e4SLinus Torvalds path->dentry = dentry; 12209875cf80SDavid Howells err = follow_managed(path, nd->flags); 122189312214SIan Kent if (unlikely(err < 0)) { 122289312214SIan Kent path_put_conditional(path, nd); 12239875cf80SDavid Howells return err; 122489312214SIan Kent } 122531e6b01fSNick Piggin *inode = path->dentry->d_inode; 12261da177e4SLinus Torvalds return 0; 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds 122952094c8aSAl Viro static inline int may_lookup(struct nameidata *nd) 123052094c8aSAl Viro { 123152094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 123252094c8aSAl Viro int err = exec_permission(nd->inode, IPERM_FLAG_RCU); 123352094c8aSAl Viro if (err != -ECHILD) 123452094c8aSAl Viro return err; 123519660af7SAl Viro if (unlazy_walk(nd, NULL)) 123652094c8aSAl Viro return -ECHILD; 123752094c8aSAl Viro } 123852094c8aSAl Viro return exec_permission(nd->inode, 0); 123952094c8aSAl Viro } 124052094c8aSAl Viro 12419856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type) 12429856fa1bSAl Viro { 12439856fa1bSAl Viro if (type == LAST_DOTDOT) { 12449856fa1bSAl Viro if (nd->flags & LOOKUP_RCU) { 12459856fa1bSAl Viro if (follow_dotdot_rcu(nd)) 12469856fa1bSAl Viro return -ECHILD; 12479856fa1bSAl Viro } else 12489856fa1bSAl Viro follow_dotdot(nd); 12499856fa1bSAl Viro } 12509856fa1bSAl Viro return 0; 12519856fa1bSAl Viro } 12529856fa1bSAl Viro 1253951361f9SAl Viro static void terminate_walk(struct nameidata *nd) 1254951361f9SAl Viro { 1255951361f9SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1256951361f9SAl Viro path_put(&nd->path); 1257951361f9SAl Viro } else { 1258951361f9SAl Viro nd->flags &= ~LOOKUP_RCU; 12595b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1260951361f9SAl Viro nd->root.mnt = NULL; 1261951361f9SAl Viro rcu_read_unlock(); 1262951361f9SAl Viro br_read_unlock(vfsmount_lock); 1263951361f9SAl Viro } 1264951361f9SAl Viro } 1265951361f9SAl Viro 1266ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path, 1267ce57dfc1SAl Viro struct qstr *name, int type, int follow) 1268ce57dfc1SAl Viro { 1269ce57dfc1SAl Viro struct inode *inode; 1270ce57dfc1SAl Viro int err; 1271ce57dfc1SAl Viro /* 1272ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1273ce57dfc1SAl Viro * to be able to know about the current root directory and 1274ce57dfc1SAl Viro * parent relationships. 1275ce57dfc1SAl Viro */ 1276ce57dfc1SAl Viro if (unlikely(type != LAST_NORM)) 1277ce57dfc1SAl Viro return handle_dots(nd, type); 1278ce57dfc1SAl Viro err = do_lookup(nd, name, path, &inode); 1279ce57dfc1SAl Viro if (unlikely(err)) { 1280ce57dfc1SAl Viro terminate_walk(nd); 1281ce57dfc1SAl Viro return err; 1282ce57dfc1SAl Viro } 1283ce57dfc1SAl Viro if (!inode) { 1284ce57dfc1SAl Viro path_to_nameidata(path, nd); 1285ce57dfc1SAl Viro terminate_walk(nd); 1286ce57dfc1SAl Viro return -ENOENT; 1287ce57dfc1SAl Viro } 1288ce57dfc1SAl Viro if (unlikely(inode->i_op->follow_link) && follow) { 128919660af7SAl Viro if (nd->flags & LOOKUP_RCU) { 129019660af7SAl Viro if (unlikely(unlazy_walk(nd, path->dentry))) { 129119660af7SAl Viro terminate_walk(nd); 1292ce57dfc1SAl Viro return -ECHILD; 129319660af7SAl Viro } 129419660af7SAl Viro } 1295ce57dfc1SAl Viro BUG_ON(inode != path->dentry->d_inode); 1296ce57dfc1SAl Viro return 1; 1297ce57dfc1SAl Viro } 1298ce57dfc1SAl Viro path_to_nameidata(path, nd); 1299ce57dfc1SAl Viro nd->inode = inode; 1300ce57dfc1SAl Viro return 0; 1301ce57dfc1SAl Viro } 1302ce57dfc1SAl Viro 13031da177e4SLinus Torvalds /* 1304b356379aSAl Viro * This limits recursive symlink follows to 8, while 1305b356379aSAl Viro * limiting consecutive symlinks to 40. 1306b356379aSAl Viro * 1307b356379aSAl Viro * Without that kind of total limit, nasty chains of consecutive 1308b356379aSAl Viro * symlinks can cause almost arbitrarily long lookups. 1309b356379aSAl Viro */ 1310b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd) 1311b356379aSAl Viro { 1312b356379aSAl Viro int res; 1313b356379aSAl Viro 1314b356379aSAl Viro if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { 1315b356379aSAl Viro path_put_conditional(path, nd); 1316b356379aSAl Viro path_put(&nd->path); 1317b356379aSAl Viro return -ELOOP; 1318b356379aSAl Viro } 13191a4022f8SErez Zadok BUG_ON(nd->depth >= MAX_NESTED_LINKS); 1320b356379aSAl Viro 1321b356379aSAl Viro nd->depth++; 1322b356379aSAl Viro current->link_count++; 1323b356379aSAl Viro 1324b356379aSAl Viro do { 1325b356379aSAl Viro struct path link = *path; 1326b356379aSAl Viro void *cookie; 1327574197e0SAl Viro 1328574197e0SAl Viro res = follow_link(&link, nd, &cookie); 1329b356379aSAl Viro if (!res) 1330b356379aSAl Viro res = walk_component(nd, path, &nd->last, 1331b356379aSAl Viro nd->last_type, LOOKUP_FOLLOW); 1332574197e0SAl Viro put_link(nd, &link, cookie); 1333b356379aSAl Viro } while (res > 0); 1334b356379aSAl Viro 1335b356379aSAl Viro current->link_count--; 1336b356379aSAl Viro nd->depth--; 1337b356379aSAl Viro return res; 1338b356379aSAl Viro } 1339b356379aSAl Viro 1340b356379aSAl Viro /* 13411da177e4SLinus Torvalds * Name resolution. 1342ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1343ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 13441da177e4SLinus Torvalds * 1345ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1346ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 13471da177e4SLinus Torvalds */ 13486de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 13491da177e4SLinus Torvalds { 13501da177e4SLinus Torvalds struct path next; 13511da177e4SLinus Torvalds int err; 13521da177e4SLinus Torvalds unsigned int lookup_flags = nd->flags; 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds while (*name=='/') 13551da177e4SLinus Torvalds name++; 13561da177e4SLinus Torvalds if (!*name) 1357086e183aSAl Viro return 0; 13581da177e4SLinus Torvalds 13591da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 13601da177e4SLinus Torvalds for(;;) { 13611da177e4SLinus Torvalds unsigned long hash; 13621da177e4SLinus Torvalds struct qstr this; 13631da177e4SLinus Torvalds unsigned int c; 1364fe479a58SAl Viro int type; 13651da177e4SLinus Torvalds 1366cdce5d6bSTrond Myklebust nd->flags |= LOOKUP_CONTINUE; 136752094c8aSAl Viro 136852094c8aSAl Viro err = may_lookup(nd); 13691da177e4SLinus Torvalds if (err) 13701da177e4SLinus Torvalds break; 13711da177e4SLinus Torvalds 13721da177e4SLinus Torvalds this.name = name; 13731da177e4SLinus Torvalds c = *(const unsigned char *)name; 13741da177e4SLinus Torvalds 13751da177e4SLinus Torvalds hash = init_name_hash(); 13761da177e4SLinus Torvalds do { 13771da177e4SLinus Torvalds name++; 13781da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 13791da177e4SLinus Torvalds c = *(const unsigned char *)name; 13801da177e4SLinus Torvalds } while (c && (c != '/')); 13811da177e4SLinus Torvalds this.len = name - (const char *) this.name; 13821da177e4SLinus Torvalds this.hash = end_name_hash(hash); 13831da177e4SLinus Torvalds 1384fe479a58SAl Viro type = LAST_NORM; 1385fe479a58SAl Viro if (this.name[0] == '.') switch (this.len) { 1386fe479a58SAl Viro case 2: 138716c2cd71SAl Viro if (this.name[1] == '.') { 1388fe479a58SAl Viro type = LAST_DOTDOT; 138916c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 139016c2cd71SAl Viro } 1391fe479a58SAl Viro break; 1392fe479a58SAl Viro case 1: 1393fe479a58SAl Viro type = LAST_DOT; 1394fe479a58SAl Viro } 13955a202bcdSAl Viro if (likely(type == LAST_NORM)) { 13965a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 139716c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 13985a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 13995a202bcdSAl Viro err = parent->d_op->d_hash(parent, nd->inode, 14005a202bcdSAl Viro &this); 14015a202bcdSAl Viro if (err < 0) 14025a202bcdSAl Viro break; 14035a202bcdSAl Viro } 14045a202bcdSAl Viro } 1405fe479a58SAl Viro 14061da177e4SLinus Torvalds /* remove trailing slashes? */ 14071da177e4SLinus Torvalds if (!c) 14081da177e4SLinus Torvalds goto last_component; 14091da177e4SLinus Torvalds while (*++name == '/'); 14101da177e4SLinus Torvalds if (!*name) 1411b356379aSAl Viro goto last_component; 14121da177e4SLinus Torvalds 1413ce57dfc1SAl Viro err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW); 1414ce57dfc1SAl Viro if (err < 0) 1415ce57dfc1SAl Viro return err; 1416fe479a58SAl Viro 1417ce57dfc1SAl Viro if (err) { 1418b356379aSAl Viro err = nested_symlink(&next, nd); 14191da177e4SLinus Torvalds if (err) 1420a7472babSAl Viro return err; 142131e6b01fSNick Piggin } 14221da177e4SLinus Torvalds err = -ENOTDIR; 142331e6b01fSNick Piggin if (!nd->inode->i_op->lookup) 14241da177e4SLinus Torvalds break; 14251da177e4SLinus Torvalds continue; 14261da177e4SLinus Torvalds /* here ends the main loop */ 14271da177e4SLinus Torvalds 14281da177e4SLinus Torvalds last_component: 1429f55eab82STrond Myklebust /* Clear LOOKUP_CONTINUE iff it was previously unset */ 1430f55eab82STrond Myklebust nd->flags &= lookup_flags | ~LOOKUP_CONTINUE; 1431ce57dfc1SAl Viro nd->last = this; 1432ce57dfc1SAl Viro nd->last_type = type; 1433ce57dfc1SAl Viro return 0; 1434ce57dfc1SAl Viro } 1435951361f9SAl Viro terminate_walk(nd); 14361da177e4SLinus Torvalds return err; 14371da177e4SLinus Torvalds } 14381da177e4SLinus Torvalds 143970e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, 144070e9b357SAl Viro struct nameidata *nd, struct file **fp) 144131e6b01fSNick Piggin { 144231e6b01fSNick Piggin int retval = 0; 144331e6b01fSNick Piggin int fput_needed; 144431e6b01fSNick Piggin struct file *file; 144531e6b01fSNick Piggin 144631e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 144716c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 144831e6b01fSNick Piggin nd->depth = 0; 14495b6ca027SAl Viro if (flags & LOOKUP_ROOT) { 14505b6ca027SAl Viro struct inode *inode = nd->root.dentry->d_inode; 145173d049a4SAl Viro if (*name) { 14525b6ca027SAl Viro if (!inode->i_op->lookup) 14535b6ca027SAl Viro return -ENOTDIR; 14545b6ca027SAl Viro retval = inode_permission(inode, MAY_EXEC); 14555b6ca027SAl Viro if (retval) 14565b6ca027SAl Viro return retval; 145773d049a4SAl Viro } 14585b6ca027SAl Viro nd->path = nd->root; 14595b6ca027SAl Viro nd->inode = inode; 14605b6ca027SAl Viro if (flags & LOOKUP_RCU) { 14615b6ca027SAl Viro br_read_lock(vfsmount_lock); 14625b6ca027SAl Viro rcu_read_lock(); 14635b6ca027SAl Viro nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 14645b6ca027SAl Viro } else { 14655b6ca027SAl Viro path_get(&nd->path); 14665b6ca027SAl Viro } 14675b6ca027SAl Viro return 0; 14685b6ca027SAl Viro } 14695b6ca027SAl Viro 147031e6b01fSNick Piggin nd->root.mnt = NULL; 147131e6b01fSNick Piggin 147231e6b01fSNick Piggin if (*name=='/') { 1473e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 147431e6b01fSNick Piggin br_read_lock(vfsmount_lock); 147531e6b01fSNick Piggin rcu_read_lock(); 1476e41f7d4eSAl Viro set_root_rcu(nd); 1477e41f7d4eSAl Viro } else { 1478e41f7d4eSAl Viro set_root(nd); 1479e41f7d4eSAl Viro path_get(&nd->root); 1480e41f7d4eSAl Viro } 148131e6b01fSNick Piggin nd->path = nd->root; 148231e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 1483e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 148431e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1485c28cc364SNick Piggin unsigned seq; 148631e6b01fSNick Piggin 148731e6b01fSNick Piggin br_read_lock(vfsmount_lock); 148831e6b01fSNick Piggin rcu_read_lock(); 148931e6b01fSNick Piggin 1490c28cc364SNick Piggin do { 1491c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 149231e6b01fSNick Piggin nd->path = fs->pwd; 1493c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1494c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1495e41f7d4eSAl Viro } else { 1496e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 1497e41f7d4eSAl Viro } 149831e6b01fSNick Piggin } else { 149931e6b01fSNick Piggin struct dentry *dentry; 150031e6b01fSNick Piggin 15011abf0c71SAl Viro file = fget_raw_light(dfd, &fput_needed); 150231e6b01fSNick Piggin retval = -EBADF; 150331e6b01fSNick Piggin if (!file) 150431e6b01fSNick Piggin goto out_fail; 150531e6b01fSNick Piggin 150631e6b01fSNick Piggin dentry = file->f_path.dentry; 150731e6b01fSNick Piggin 1508f52e0c11SAl Viro if (*name) { 150931e6b01fSNick Piggin retval = -ENOTDIR; 151031e6b01fSNick Piggin if (!S_ISDIR(dentry->d_inode->i_mode)) 151131e6b01fSNick Piggin goto fput_fail; 151231e6b01fSNick Piggin 151331e6b01fSNick Piggin retval = file_permission(file, MAY_EXEC); 151431e6b01fSNick Piggin if (retval) 151531e6b01fSNick Piggin goto fput_fail; 1516f52e0c11SAl Viro } 151731e6b01fSNick Piggin 151831e6b01fSNick Piggin nd->path = file->f_path; 1519e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 152031e6b01fSNick Piggin if (fput_needed) 152170e9b357SAl Viro *fp = file; 1522c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 152331e6b01fSNick Piggin br_read_lock(vfsmount_lock); 152431e6b01fSNick Piggin rcu_read_lock(); 15255590ff0dSUlrich Drepper } else { 15265dd784d0SJan Blunck path_get(&file->f_path); 15275590ff0dSUlrich Drepper fput_light(file, fput_needed); 15281da177e4SLinus Torvalds } 1529e41f7d4eSAl Viro } 1530e41f7d4eSAl Viro 153131e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 15329b4a9b14SAl Viro return 0; 15332dfdd266SJosef 'Jeff' Sipek 15349b4a9b14SAl Viro fput_fail: 15359b4a9b14SAl Viro fput_light(file, fput_needed); 15369b4a9b14SAl Viro out_fail: 15379b4a9b14SAl Viro return retval; 15389b4a9b14SAl Viro } 15399b4a9b14SAl Viro 1540bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path) 1541bd92d7feSAl Viro { 1542bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 1543bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 1544bd92d7feSAl Viro 1545bd92d7feSAl Viro nd->flags &= ~LOOKUP_PARENT; 1546bd92d7feSAl Viro return walk_component(nd, path, &nd->last, nd->last_type, 1547bd92d7feSAl Viro nd->flags & LOOKUP_FOLLOW); 1548bd92d7feSAl Viro } 1549bd92d7feSAl Viro 15509b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1551ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name, 15529b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 15539b4a9b14SAl Viro { 155470e9b357SAl Viro struct file *base = NULL; 1555bd92d7feSAl Viro struct path path; 1556bd92d7feSAl Viro int err; 155731e6b01fSNick Piggin 155831e6b01fSNick Piggin /* 155931e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 156031e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 156131e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 156231e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 156331e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 156431e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 156531e6b01fSNick Piggin * analogue, foo_rcu(). 156631e6b01fSNick Piggin * 156731e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 156831e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 156931e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 157031e6b01fSNick Piggin * be able to complete). 157131e6b01fSNick Piggin */ 1572bd92d7feSAl Viro err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); 1573ee0827cdSAl Viro 1574bd92d7feSAl Viro if (unlikely(err)) 1575bd92d7feSAl Viro return err; 1576ee0827cdSAl Viro 1577ee0827cdSAl Viro current->total_link_count = 0; 1578bd92d7feSAl Viro err = link_path_walk(name, nd); 1579bd92d7feSAl Viro 1580bd92d7feSAl Viro if (!err && !(flags & LOOKUP_PARENT)) { 1581bd92d7feSAl Viro err = lookup_last(nd, &path); 1582bd92d7feSAl Viro while (err > 0) { 1583bd92d7feSAl Viro void *cookie; 1584bd92d7feSAl Viro struct path link = path; 1585bd92d7feSAl Viro nd->flags |= LOOKUP_PARENT; 1586574197e0SAl Viro err = follow_link(&link, nd, &cookie); 1587bd92d7feSAl Viro if (!err) 1588bd92d7feSAl Viro err = lookup_last(nd, &path); 1589574197e0SAl Viro put_link(nd, &link, cookie); 1590bd92d7feSAl Viro } 1591bd92d7feSAl Viro } 1592ee0827cdSAl Viro 15939f1fafeeSAl Viro if (!err) 15949f1fafeeSAl Viro err = complete_walk(nd); 1595bd92d7feSAl Viro 1596bd92d7feSAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) { 1597bd92d7feSAl Viro if (!nd->inode->i_op->lookup) { 1598bd92d7feSAl Viro path_put(&nd->path); 1599bd23a539SAl Viro err = -ENOTDIR; 1600bd92d7feSAl Viro } 1601bd92d7feSAl Viro } 160216c2cd71SAl Viro 160370e9b357SAl Viro if (base) 160470e9b357SAl Viro fput(base); 1605ee0827cdSAl Viro 16065b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 160731e6b01fSNick Piggin path_put(&nd->root); 160831e6b01fSNick Piggin nd->root.mnt = NULL; 160931e6b01fSNick Piggin } 1610bd92d7feSAl Viro return err; 161131e6b01fSNick Piggin } 161231e6b01fSNick Piggin 1613ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name, 1614ee0827cdSAl Viro unsigned int flags, struct nameidata *nd) 1615ee0827cdSAl Viro { 1616ee0827cdSAl Viro int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd); 1617ee0827cdSAl Viro if (unlikely(retval == -ECHILD)) 1618ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags, nd); 1619ee0827cdSAl Viro if (unlikely(retval == -ESTALE)) 1620ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd); 1621ee0827cdSAl Viro 162231e6b01fSNick Piggin if (likely(!retval)) { 162331e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) { 162431e6b01fSNick Piggin if (nd->path.dentry && nd->inode) 162531e6b01fSNick Piggin audit_inode(name, nd->path.dentry); 162631e6b01fSNick Piggin } 162731e6b01fSNick Piggin } 1628170aa3d0SUlrich Drepper return retval; 16291da177e4SLinus Torvalds } 16301da177e4SLinus Torvalds 1631c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd) 16325590ff0dSUlrich Drepper { 1633c9c6cac0SAl Viro return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd); 16345590ff0dSUlrich Drepper } 16355590ff0dSUlrich Drepper 1636d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1637d1811465SAl Viro { 1638d1811465SAl Viro struct nameidata nd; 1639d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1640d1811465SAl Viro if (!res) 1641d1811465SAl Viro *path = nd.path; 1642d1811465SAl Viro return res; 1643d1811465SAl Viro } 1644d1811465SAl Viro 164516f18200SJosef 'Jeff' Sipek /** 164616f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 164716f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 164816f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 164916f18200SJosef 'Jeff' Sipek * @name: pointer to file name 165016f18200SJosef 'Jeff' Sipek * @flags: lookup flags 165116f18200SJosef 'Jeff' Sipek * @nd: pointer to nameidata 165216f18200SJosef 'Jeff' Sipek */ 165316f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 165416f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 165516f18200SJosef 'Jeff' Sipek struct nameidata *nd) 165616f18200SJosef 'Jeff' Sipek { 16575b6ca027SAl Viro nd->root.dentry = dentry; 16585b6ca027SAl Viro nd->root.mnt = mnt; 16595b6ca027SAl Viro /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ 16605b6ca027SAl Viro return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd); 166116f18200SJosef 'Jeff' Sipek } 166216f18200SJosef 'Jeff' Sipek 1663eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name, 1664eead1911SChristoph Hellwig struct dentry *base, struct nameidata *nd) 16651da177e4SLinus Torvalds { 166681fca444SChristoph Hellwig struct inode *inode = base->d_inode; 16671da177e4SLinus Torvalds struct dentry *dentry; 16681da177e4SLinus Torvalds int err; 16691da177e4SLinus Torvalds 1670b74c79e9SNick Piggin err = exec_permission(inode, 0); 167181fca444SChristoph Hellwig if (err) 167281fca444SChristoph Hellwig return ERR_PTR(err); 16731da177e4SLinus Torvalds 16741da177e4SLinus Torvalds /* 1675b04f784eSNick Piggin * Don't bother with __d_lookup: callers are for creat as 1676b04f784eSNick Piggin * well as unlink, so a lot of the time it would cost 1677b04f784eSNick Piggin * a double lookup. 16786e6b1bd1SAl Viro */ 16796e6b1bd1SAl Viro dentry = d_lookup(base, name); 16806e6b1bd1SAl Viro 1681fb045adbSNick Piggin if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) 16826e6b1bd1SAl Viro dentry = do_revalidate(dentry, nd); 16836e6b1bd1SAl Viro 16841da177e4SLinus Torvalds if (!dentry) 1685baa03890SNick Piggin dentry = d_alloc_and_lookup(base, name, nd); 16865a202bcdSAl Viro 16871da177e4SLinus Torvalds return dentry; 16881da177e4SLinus Torvalds } 16891da177e4SLinus Torvalds 1690057f6c01SJames Morris /* 1691057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1692057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1693057f6c01SJames Morris * SMP-safe. 1694057f6c01SJames Morris */ 1695a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 16961da177e4SLinus Torvalds { 16974ac91378SJan Blunck return __lookup_hash(&nd->last, nd->path.dentry, nd); 16981da177e4SLinus Torvalds } 16991da177e4SLinus Torvalds 1700eead1911SChristoph Hellwig /** 1701a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1702eead1911SChristoph Hellwig * @name: pathname component to lookup 1703eead1911SChristoph Hellwig * @base: base directory to lookup from 1704eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1705eead1911SChristoph Hellwig * 1706a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1707a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1708eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1709eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1710eead1911SChristoph Hellwig */ 1711057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1712057f6c01SJames Morris { 1713057f6c01SJames Morris struct qstr this; 17146a96ba54SAl Viro unsigned long hash; 17156a96ba54SAl Viro unsigned int c; 1716057f6c01SJames Morris 17172f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 17182f9092e1SDavid Woodhouse 17196a96ba54SAl Viro this.name = name; 17206a96ba54SAl Viro this.len = len; 17216a96ba54SAl Viro if (!len) 17226a96ba54SAl Viro return ERR_PTR(-EACCES); 17236a96ba54SAl Viro 17246a96ba54SAl Viro hash = init_name_hash(); 17256a96ba54SAl Viro while (len--) { 17266a96ba54SAl Viro c = *(const unsigned char *)name++; 17276a96ba54SAl Viro if (c == '/' || c == '\0') 17286a96ba54SAl Viro return ERR_PTR(-EACCES); 17296a96ba54SAl Viro hash = partial_name_hash(c, hash); 17306a96ba54SAl Viro } 17316a96ba54SAl Viro this.hash = end_name_hash(hash); 17325a202bcdSAl Viro /* 17335a202bcdSAl Viro * See if the low-level filesystem might want 17345a202bcdSAl Viro * to use its own hash.. 17355a202bcdSAl Viro */ 17365a202bcdSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 17375a202bcdSAl Viro int err = base->d_op->d_hash(base, base->d_inode, &this); 17385a202bcdSAl Viro if (err < 0) 17395a202bcdSAl Viro return ERR_PTR(err); 17405a202bcdSAl Viro } 1741eead1911SChristoph Hellwig 174249705b77SChristoph Hellwig return __lookup_hash(&this, base, NULL); 1743057f6c01SJames Morris } 1744057f6c01SJames Morris 17452d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags, 17462d8f3038SAl Viro struct path *path) 17471da177e4SLinus Torvalds { 17482d8f3038SAl Viro struct nameidata nd; 1749f52e0c11SAl Viro char *tmp = getname_flags(name, flags); 17501da177e4SLinus Torvalds int err = PTR_ERR(tmp); 17511da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 17522d8f3038SAl Viro 17532d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 17542d8f3038SAl Viro 17552d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 17561da177e4SLinus Torvalds putname(tmp); 17572d8f3038SAl Viro if (!err) 17582d8f3038SAl Viro *path = nd.path; 17591da177e4SLinus Torvalds } 17601da177e4SLinus Torvalds return err; 17611da177e4SLinus Torvalds } 17621da177e4SLinus Torvalds 17632ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 17642ad94ae6SAl Viro struct nameidata *nd, char **name) 17652ad94ae6SAl Viro { 17662ad94ae6SAl Viro char *s = getname(path); 17672ad94ae6SAl Viro int error; 17682ad94ae6SAl Viro 17692ad94ae6SAl Viro if (IS_ERR(s)) 17702ad94ae6SAl Viro return PTR_ERR(s); 17712ad94ae6SAl Viro 17722ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 17732ad94ae6SAl Viro if (error) 17742ad94ae6SAl Viro putname(s); 17752ad94ae6SAl Viro else 17762ad94ae6SAl Viro *name = s; 17772ad94ae6SAl Viro 17782ad94ae6SAl Viro return error; 17792ad94ae6SAl Viro } 17802ad94ae6SAl Viro 17811da177e4SLinus Torvalds /* 17821da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 17831da177e4SLinus Torvalds * minimal. 17841da177e4SLinus Torvalds */ 17851da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 17861da177e4SLinus Torvalds { 1787da9592edSDavid Howells uid_t fsuid = current_fsuid(); 1788da9592edSDavid Howells 17891da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 17901da177e4SLinus Torvalds return 0; 1791e795b717SSerge E. Hallyn if (current_user_ns() != inode_userns(inode)) 1792e795b717SSerge E. Hallyn goto other_userns; 1793da9592edSDavid Howells if (inode->i_uid == fsuid) 17941da177e4SLinus Torvalds return 0; 1795da9592edSDavid Howells if (dir->i_uid == fsuid) 17961da177e4SLinus Torvalds return 0; 1797e795b717SSerge E. Hallyn 1798e795b717SSerge E. Hallyn other_userns: 1799e795b717SSerge E. Hallyn return !ns_capable(inode_userns(inode), CAP_FOWNER); 18001da177e4SLinus Torvalds } 18011da177e4SLinus Torvalds 18021da177e4SLinus Torvalds /* 18031da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 18041da177e4SLinus Torvalds * whether the type of victim is right. 18051da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 18061da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 18071da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 18081da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 18091da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 18101da177e4SLinus Torvalds * a. be owner of dir, or 18111da177e4SLinus Torvalds * b. be owner of victim, or 18121da177e4SLinus Torvalds * c. have CAP_FOWNER capability 18131da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 18141da177e4SLinus Torvalds * links pointing to it. 18151da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 18161da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 18171da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 18181da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 18191da177e4SLinus Torvalds * nfs_async_unlink(). 18201da177e4SLinus Torvalds */ 1821858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 18221da177e4SLinus Torvalds { 18231da177e4SLinus Torvalds int error; 18241da177e4SLinus Torvalds 18251da177e4SLinus Torvalds if (!victim->d_inode) 18261da177e4SLinus Torvalds return -ENOENT; 18271da177e4SLinus Torvalds 18281da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 1829cccc6bbaSAl Viro audit_inode_child(victim, dir); 18301da177e4SLinus Torvalds 1831f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 18321da177e4SLinus Torvalds if (error) 18331da177e4SLinus Torvalds return error; 18341da177e4SLinus Torvalds if (IS_APPEND(dir)) 18351da177e4SLinus Torvalds return -EPERM; 18361da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 1837f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 18381da177e4SLinus Torvalds return -EPERM; 18391da177e4SLinus Torvalds if (isdir) { 18401da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 18411da177e4SLinus Torvalds return -ENOTDIR; 18421da177e4SLinus Torvalds if (IS_ROOT(victim)) 18431da177e4SLinus Torvalds return -EBUSY; 18441da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 18451da177e4SLinus Torvalds return -EISDIR; 18461da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 18471da177e4SLinus Torvalds return -ENOENT; 18481da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 18491da177e4SLinus Torvalds return -EBUSY; 18501da177e4SLinus Torvalds return 0; 18511da177e4SLinus Torvalds } 18521da177e4SLinus Torvalds 18531da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 18541da177e4SLinus Torvalds * dir. 18551da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 18561da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 18571da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 18581da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 18591da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 18601da177e4SLinus Torvalds */ 1861a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 18621da177e4SLinus Torvalds { 18631da177e4SLinus Torvalds if (child->d_inode) 18641da177e4SLinus Torvalds return -EEXIST; 18651da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 18661da177e4SLinus Torvalds return -ENOENT; 1867f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 18681da177e4SLinus Torvalds } 18691da177e4SLinus Torvalds 18701da177e4SLinus Torvalds /* 18711da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 18721da177e4SLinus Torvalds */ 18731da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 18741da177e4SLinus Torvalds { 18751da177e4SLinus Torvalds struct dentry *p; 18761da177e4SLinus Torvalds 18771da177e4SLinus Torvalds if (p1 == p2) { 1878f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 18791da177e4SLinus Torvalds return NULL; 18801da177e4SLinus Torvalds } 18811da177e4SLinus Torvalds 1882a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 18831da177e4SLinus Torvalds 1884e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 1885e2761a11SOGAWA Hirofumi if (p) { 1886f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 1887f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 18881da177e4SLinus Torvalds return p; 18891da177e4SLinus Torvalds } 18901da177e4SLinus Torvalds 1891e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 1892e2761a11SOGAWA Hirofumi if (p) { 1893f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1894f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 18951da177e4SLinus Torvalds return p; 18961da177e4SLinus Torvalds } 18971da177e4SLinus Torvalds 1898f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1899f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 19001da177e4SLinus Torvalds return NULL; 19011da177e4SLinus Torvalds } 19021da177e4SLinus Torvalds 19031da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 19041da177e4SLinus Torvalds { 19051b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 19061da177e4SLinus Torvalds if (p1 != p2) { 19071b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 1908a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 19091da177e4SLinus Torvalds } 19101da177e4SLinus Torvalds } 19111da177e4SLinus Torvalds 19121da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode, 19131da177e4SLinus Torvalds struct nameidata *nd) 19141da177e4SLinus Torvalds { 1915a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 19161da177e4SLinus Torvalds 19171da177e4SLinus Torvalds if (error) 19181da177e4SLinus Torvalds return error; 19191da177e4SLinus Torvalds 1920acfa4380SAl Viro if (!dir->i_op->create) 19211da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 19221da177e4SLinus Torvalds mode &= S_IALLUGO; 19231da177e4SLinus Torvalds mode |= S_IFREG; 19241da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 19251da177e4SLinus Torvalds if (error) 19261da177e4SLinus Torvalds return error; 19271da177e4SLinus Torvalds error = dir->i_op->create(dir, dentry, mode, nd); 1928a74574aaSStephen Smalley if (!error) 1929f38aa942SAmy Griffis fsnotify_create(dir, dentry); 19301da177e4SLinus Torvalds return error; 19311da177e4SLinus Torvalds } 19321da177e4SLinus Torvalds 193373d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag) 19341da177e4SLinus Torvalds { 19353fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 19361da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 19371da177e4SLinus Torvalds int error; 19381da177e4SLinus Torvalds 1939bcda7652SAl Viro /* O_PATH? */ 1940bcda7652SAl Viro if (!acc_mode) 1941bcda7652SAl Viro return 0; 1942bcda7652SAl Viro 19431da177e4SLinus Torvalds if (!inode) 19441da177e4SLinus Torvalds return -ENOENT; 19451da177e4SLinus Torvalds 1946c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 1947c8fe8f30SChristoph Hellwig case S_IFLNK: 19481da177e4SLinus Torvalds return -ELOOP; 1949c8fe8f30SChristoph Hellwig case S_IFDIR: 1950c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 19511da177e4SLinus Torvalds return -EISDIR; 1952c8fe8f30SChristoph Hellwig break; 1953c8fe8f30SChristoph Hellwig case S_IFBLK: 1954c8fe8f30SChristoph Hellwig case S_IFCHR: 19553fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 19561da177e4SLinus Torvalds return -EACCES; 1957c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 1958c8fe8f30SChristoph Hellwig case S_IFIFO: 1959c8fe8f30SChristoph Hellwig case S_IFSOCK: 19601da177e4SLinus Torvalds flag &= ~O_TRUNC; 1961c8fe8f30SChristoph Hellwig break; 19624a3fd211SDave Hansen } 1963b41572e9SDave Hansen 19643fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 1965b41572e9SDave Hansen if (error) 1966b41572e9SDave Hansen return error; 19676146f0d5SMimi Zohar 19681da177e4SLinus Torvalds /* 19691da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 19701da177e4SLinus Torvalds */ 19711da177e4SLinus Torvalds if (IS_APPEND(inode)) { 19728737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 19737715b521SAl Viro return -EPERM; 19741da177e4SLinus Torvalds if (flag & O_TRUNC) 19757715b521SAl Viro return -EPERM; 19761da177e4SLinus Torvalds } 19771da177e4SLinus Torvalds 19781da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 19792e149670SSerge E. Hallyn if (flag & O_NOATIME && !inode_owner_or_capable(inode)) 19807715b521SAl Viro return -EPERM; 19811da177e4SLinus Torvalds 19821da177e4SLinus Torvalds /* 19831da177e4SLinus Torvalds * Ensure there are no outstanding leases on the file. 19841da177e4SLinus Torvalds */ 1985b65a9cfcSAl Viro return break_lease(inode, flag); 19867715b521SAl Viro } 19877715b521SAl Viro 1988e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 19897715b521SAl Viro { 1990e1181ee6SJeff Layton struct path *path = &filp->f_path; 19917715b521SAl Viro struct inode *inode = path->dentry->d_inode; 19927715b521SAl Viro int error = get_write_access(inode); 19931da177e4SLinus Torvalds if (error) 19947715b521SAl Viro return error; 19951da177e4SLinus Torvalds /* 19961da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 19971da177e4SLinus Torvalds */ 19981da177e4SLinus Torvalds error = locks_verify_locked(inode); 1999be6d3e56SKentaro Takeda if (!error) 2000ea0d3ab2STetsuo Handa error = security_path_truncate(path); 20011da177e4SLinus Torvalds if (!error) { 20027715b521SAl Viro error = do_truncate(path->dentry, 0, 2003d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 2004e1181ee6SJeff Layton filp); 20051da177e4SLinus Torvalds } 20061da177e4SLinus Torvalds put_write_access(inode); 2007acd0c935SMimi Zohar return error; 20081da177e4SLinus Torvalds } 20091da177e4SLinus Torvalds 2010d57999e1SDave Hansen /* 2011d57999e1SDave Hansen * Note that while the flag value (low two bits) for sys_open means: 2012d57999e1SDave Hansen * 00 - read-only 2013d57999e1SDave Hansen * 01 - write-only 2014d57999e1SDave Hansen * 10 - read-write 2015d57999e1SDave Hansen * 11 - special 2016d57999e1SDave Hansen * it is changed into 2017d57999e1SDave Hansen * 00 - no permissions needed 2018d57999e1SDave Hansen * 01 - read-permission 2019d57999e1SDave Hansen * 10 - write-permission 2020d57999e1SDave Hansen * 11 - read-write 2021d57999e1SDave Hansen * for the internal routines (ie open_namei()/follow_link() etc) 2022d57999e1SDave Hansen * This is more logical, and also allows the 00 "no perm needed" 2023d57999e1SDave Hansen * to be used for symlinks (where the permissions are checked 2024d57999e1SDave Hansen * later). 2025d57999e1SDave Hansen * 2026d57999e1SDave Hansen */ 2027d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2028d57999e1SDave Hansen { 2029d57999e1SDave Hansen if ((flag+1) & O_ACCMODE) 2030d57999e1SDave Hansen flag++; 2031d57999e1SDave Hansen return flag; 2032d57999e1SDave Hansen } 2033d57999e1SDave Hansen 203431e6b01fSNick Piggin /* 2035fe2d35ffSAl Viro * Handle the last step of open() 203631e6b01fSNick Piggin */ 2037fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path, 2038c3e380b0SAl Viro const struct open_flags *op, const char *pathname) 2039fb1cc555SAl Viro { 2040a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 20416c0d46c4SAl Viro struct dentry *dentry; 2042ca344a89SAl Viro int open_flag = op->open_flag; 20436c0d46c4SAl Viro int will_truncate = open_flag & O_TRUNC; 2044ca344a89SAl Viro int want_write = 0; 2045bcda7652SAl Viro int acc_mode = op->acc_mode; 2046fb1cc555SAl Viro struct file *filp; 204716c2cd71SAl Viro int error; 2048fb1cc555SAl Viro 2049c3e380b0SAl Viro nd->flags &= ~LOOKUP_PARENT; 2050c3e380b0SAl Viro nd->flags |= op->intent; 2051c3e380b0SAl Viro 20521f36f774SAl Viro switch (nd->last_type) { 20531f36f774SAl Viro case LAST_DOTDOT: 2054176306f5SNeil Brown case LAST_DOT: 2055fe2d35ffSAl Viro error = handle_dots(nd, nd->last_type); 2056fe2d35ffSAl Viro if (error) 2057fe2d35ffSAl Viro return ERR_PTR(error); 20581f36f774SAl Viro /* fallthrough */ 20591f36f774SAl Viro case LAST_ROOT: 20609f1fafeeSAl Viro error = complete_walk(nd); 206116c2cd71SAl Viro if (error) 20629f1fafeeSAl Viro return ERR_PTR(error); 2063fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2064ca344a89SAl Viro if (open_flag & O_CREAT) { 206516c2cd71SAl Viro error = -EISDIR; 20661f36f774SAl Viro goto exit; 2067fe2d35ffSAl Viro } 2068fe2d35ffSAl Viro goto ok; 20691f36f774SAl Viro case LAST_BIND: 20709f1fafeeSAl Viro error = complete_walk(nd); 207116c2cd71SAl Viro if (error) 20729f1fafeeSAl Viro return ERR_PTR(error); 20731f36f774SAl Viro audit_inode(pathname, dir); 20741f36f774SAl Viro goto ok; 20751f36f774SAl Viro } 2076a2c36b45SAl Viro 2077ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 2078bcda7652SAl Viro int symlink_ok = 0; 2079fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 2080fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2081bcda7652SAl Viro if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) 2082bcda7652SAl Viro symlink_ok = 1; 2083fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 2084ce57dfc1SAl Viro error = walk_component(nd, path, &nd->last, LAST_NORM, 2085ce57dfc1SAl Viro !symlink_ok); 2086ce57dfc1SAl Viro if (error < 0) 2087fe2d35ffSAl Viro return ERR_PTR(error); 2088ce57dfc1SAl Viro if (error) /* symlink */ 2089fe2d35ffSAl Viro return NULL; 2090fe2d35ffSAl Viro /* sayonara */ 20919f1fafeeSAl Viro error = complete_walk(nd); 20929f1fafeeSAl Viro if (error) 2093fe2d35ffSAl Viro return ERR_PTR(-ECHILD); 2094fe2d35ffSAl Viro 2095fe2d35ffSAl Viro error = -ENOTDIR; 2096fe2d35ffSAl Viro if (nd->flags & LOOKUP_DIRECTORY) { 2097ce57dfc1SAl Viro if (!nd->inode->i_op->lookup) 2098fe2d35ffSAl Viro goto exit; 2099fe2d35ffSAl Viro } 2100fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2101fe2d35ffSAl Viro goto ok; 2102fe2d35ffSAl Viro } 2103fe2d35ffSAl Viro 2104fe2d35ffSAl Viro /* create side of things */ 21059f1fafeeSAl Viro error = complete_walk(nd); 21069f1fafeeSAl Viro if (error) 21079f1fafeeSAl Viro return ERR_PTR(error); 2108fe2d35ffSAl Viro 2109fe2d35ffSAl Viro audit_inode(pathname, dir); 211016c2cd71SAl Viro error = -EISDIR; 21111f36f774SAl Viro /* trailing slashes? */ 211231e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 21131f36f774SAl Viro goto exit; 21141f36f774SAl Viro 2115a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 2116a1e28038SAl Viro 21176c0d46c4SAl Viro dentry = lookup_hash(nd); 21186c0d46c4SAl Viro error = PTR_ERR(dentry); 21196c0d46c4SAl Viro if (IS_ERR(dentry)) { 2120fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2121fb1cc555SAl Viro goto exit; 2122fb1cc555SAl Viro } 2123fb1cc555SAl Viro 21246c0d46c4SAl Viro path->dentry = dentry; 21256c0d46c4SAl Viro path->mnt = nd->path.mnt; 21266c0d46c4SAl Viro 2127fb1cc555SAl Viro /* Negative dentry, just create the file */ 21286c0d46c4SAl Viro if (!dentry->d_inode) { 21296c0d46c4SAl Viro int mode = op->mode; 21306c0d46c4SAl Viro if (!IS_POSIXACL(dir->d_inode)) 21316c0d46c4SAl Viro mode &= ~current_umask(); 2132fb1cc555SAl Viro /* 2133fb1cc555SAl Viro * This write is needed to ensure that a 21346c0d46c4SAl Viro * rw->ro transition does not occur between 2135fb1cc555SAl Viro * the time when the file is created and when 2136fb1cc555SAl Viro * a permanent write count is taken through 2137fb1cc555SAl Viro * the 'struct file' in nameidata_to_filp(). 2138fb1cc555SAl Viro */ 2139fb1cc555SAl Viro error = mnt_want_write(nd->path.mnt); 2140fb1cc555SAl Viro if (error) 2141fb1cc555SAl Viro goto exit_mutex_unlock; 2142ca344a89SAl Viro want_write = 1; 21439b44f1b3SAl Viro /* Don't check for write permission, don't truncate */ 2144ca344a89SAl Viro open_flag &= ~O_TRUNC; 21456c0d46c4SAl Viro will_truncate = 0; 2146bcda7652SAl Viro acc_mode = MAY_OPEN; 21476c0d46c4SAl Viro error = security_path_mknod(&nd->path, dentry, mode, 0); 21486c0d46c4SAl Viro if (error) 21496c0d46c4SAl Viro goto exit_mutex_unlock; 21506c0d46c4SAl Viro error = vfs_create(dir->d_inode, dentry, mode, nd); 21516c0d46c4SAl Viro if (error) 21526c0d46c4SAl Viro goto exit_mutex_unlock; 21536c0d46c4SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 21546c0d46c4SAl Viro dput(nd->path.dentry); 21556c0d46c4SAl Viro nd->path.dentry = dentry; 2156ca344a89SAl Viro goto common; 2157fb1cc555SAl Viro } 2158fb1cc555SAl Viro 2159fb1cc555SAl Viro /* 2160fb1cc555SAl Viro * It already exists. 2161fb1cc555SAl Viro */ 2162fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2163fb1cc555SAl Viro audit_inode(pathname, path->dentry); 2164fb1cc555SAl Viro 2165fb1cc555SAl Viro error = -EEXIST; 2166ca344a89SAl Viro if (open_flag & O_EXCL) 2167fb1cc555SAl Viro goto exit_dput; 2168fb1cc555SAl Viro 21699875cf80SDavid Howells error = follow_managed(path, nd->flags); 21709875cf80SDavid Howells if (error < 0) 2171fb1cc555SAl Viro goto exit_dput; 2172fb1cc555SAl Viro 2173fb1cc555SAl Viro error = -ENOENT; 2174fb1cc555SAl Viro if (!path->dentry->d_inode) 2175fb1cc555SAl Viro goto exit_dput; 21769e67f361SAl Viro 21779e67f361SAl Viro if (path->dentry->d_inode->i_op->follow_link) 2178fb1cc555SAl Viro return NULL; 2179fb1cc555SAl Viro 2180fb1cc555SAl Viro path_to_nameidata(path, nd); 218131e6b01fSNick Piggin nd->inode = path->dentry->d_inode; 2182fb1cc555SAl Viro error = -EISDIR; 218331e6b01fSNick Piggin if (S_ISDIR(nd->inode->i_mode)) 2184fb1cc555SAl Viro goto exit; 218567ee3ad2SAl Viro ok: 21866c0d46c4SAl Viro if (!S_ISREG(nd->inode->i_mode)) 21876c0d46c4SAl Viro will_truncate = 0; 21886c0d46c4SAl Viro 21890f9d1a10SAl Viro if (will_truncate) { 21900f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 21910f9d1a10SAl Viro if (error) 21920f9d1a10SAl Viro goto exit; 2193ca344a89SAl Viro want_write = 1; 21940f9d1a10SAl Viro } 2195ca344a89SAl Viro common: 2196bcda7652SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2197ca344a89SAl Viro if (error) 21980f9d1a10SAl Viro goto exit; 21990f9d1a10SAl Viro filp = nameidata_to_filp(nd); 22000f9d1a10SAl Viro if (!IS_ERR(filp)) { 22010f9d1a10SAl Viro error = ima_file_check(filp, op->acc_mode); 22020f9d1a10SAl Viro if (error) { 22030f9d1a10SAl Viro fput(filp); 22040f9d1a10SAl Viro filp = ERR_PTR(error); 22050f9d1a10SAl Viro } 22060f9d1a10SAl Viro } 22070f9d1a10SAl Viro if (!IS_ERR(filp)) { 22080f9d1a10SAl Viro if (will_truncate) { 22090f9d1a10SAl Viro error = handle_truncate(filp); 22100f9d1a10SAl Viro if (error) { 22110f9d1a10SAl Viro fput(filp); 22120f9d1a10SAl Viro filp = ERR_PTR(error); 22130f9d1a10SAl Viro } 22140f9d1a10SAl Viro } 22150f9d1a10SAl Viro } 2216ca344a89SAl Viro out: 2217ca344a89SAl Viro if (want_write) 22180f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 22190f9d1a10SAl Viro path_put(&nd->path); 2220fb1cc555SAl Viro return filp; 2221fb1cc555SAl Viro 2222fb1cc555SAl Viro exit_mutex_unlock: 2223fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2224fb1cc555SAl Viro exit_dput: 2225fb1cc555SAl Viro path_put_conditional(path, nd); 2226fb1cc555SAl Viro exit: 2227ca344a89SAl Viro filp = ERR_PTR(error); 2228ca344a89SAl Viro goto out; 2229fb1cc555SAl Viro } 2230fb1cc555SAl Viro 223113aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname, 223273d049a4SAl Viro struct nameidata *nd, const struct open_flags *op, int flags) 22331da177e4SLinus Torvalds { 2234fe2d35ffSAl Viro struct file *base = NULL; 22354a3fd211SDave Hansen struct file *filp; 22369850c056SAl Viro struct path path; 223713aab428SAl Viro int error; 223831e6b01fSNick Piggin 223931e6b01fSNick Piggin filp = get_empty_filp(); 224031e6b01fSNick Piggin if (!filp) 224131e6b01fSNick Piggin return ERR_PTR(-ENFILE); 224231e6b01fSNick Piggin 224347c805dcSAl Viro filp->f_flags = op->open_flag; 224473d049a4SAl Viro nd->intent.open.file = filp; 224573d049a4SAl Viro nd->intent.open.flags = open_to_namei_flags(op->open_flag); 224673d049a4SAl Viro nd->intent.open.create_mode = op->mode; 224731e6b01fSNick Piggin 224873d049a4SAl Viro error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base); 224931e6b01fSNick Piggin if (unlikely(error)) 225013aab428SAl Viro goto out_filp; 225131e6b01fSNick Piggin 2252fe2d35ffSAl Viro current->total_link_count = 0; 225373d049a4SAl Viro error = link_path_walk(pathname, nd); 225431e6b01fSNick Piggin if (unlikely(error)) 225531e6b01fSNick Piggin goto out_filp; 22561da177e4SLinus Torvalds 225773d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2258806b681cSAl Viro while (unlikely(!filp)) { /* trailing symlink */ 22597b9337aaSNick Piggin struct path link = path; 2260def4af30SAl Viro void *cookie; 2261574197e0SAl Viro if (!(nd->flags & LOOKUP_FOLLOW)) { 226273d049a4SAl Viro path_put_conditional(&path, nd); 226373d049a4SAl Viro path_put(&nd->path); 226440b39136SAl Viro filp = ERR_PTR(-ELOOP); 226540b39136SAl Viro break; 226640b39136SAl Viro } 226773d049a4SAl Viro nd->flags |= LOOKUP_PARENT; 226873d049a4SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 2269574197e0SAl Viro error = follow_link(&link, nd, &cookie); 2270c3e380b0SAl Viro if (unlikely(error)) 2271f1afe9efSAl Viro filp = ERR_PTR(error); 2272c3e380b0SAl Viro else 227373d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2274574197e0SAl Viro put_link(nd, &link, cookie); 2275806b681cSAl Viro } 227610fa8e62SAl Viro out: 227773d049a4SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) 227873d049a4SAl Viro path_put(&nd->root); 2279fe2d35ffSAl Viro if (base) 2280fe2d35ffSAl Viro fput(base); 228173d049a4SAl Viro release_open_intent(nd); 228210fa8e62SAl Viro return filp; 22831da177e4SLinus Torvalds 228431e6b01fSNick Piggin out_filp: 228510fa8e62SAl Viro filp = ERR_PTR(error); 228610fa8e62SAl Viro goto out; 2287de459215SKirill Korotaev } 22881da177e4SLinus Torvalds 228913aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname, 229013aab428SAl Viro const struct open_flags *op, int flags) 229113aab428SAl Viro { 229273d049a4SAl Viro struct nameidata nd; 229313aab428SAl Viro struct file *filp; 229413aab428SAl Viro 229573d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); 229613aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 229773d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags); 229813aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 229973d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); 230013aab428SAl Viro return filp; 230113aab428SAl Viro } 230213aab428SAl Viro 230373d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, 230473d049a4SAl Viro const char *name, const struct open_flags *op, int flags) 230573d049a4SAl Viro { 230673d049a4SAl Viro struct nameidata nd; 230773d049a4SAl Viro struct file *file; 230873d049a4SAl Viro 230973d049a4SAl Viro nd.root.mnt = mnt; 231073d049a4SAl Viro nd.root.dentry = dentry; 231173d049a4SAl Viro 231273d049a4SAl Viro flags |= LOOKUP_ROOT; 231373d049a4SAl Viro 2314bcda7652SAl Viro if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN) 231573d049a4SAl Viro return ERR_PTR(-ELOOP); 231673d049a4SAl Viro 231773d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU); 231873d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 231973d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags); 232073d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 232173d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL); 232273d049a4SAl Viro return file; 232373d049a4SAl Viro } 232473d049a4SAl Viro 23251da177e4SLinus Torvalds /** 23261da177e4SLinus Torvalds * lookup_create - lookup a dentry, creating it if it doesn't exist 23271da177e4SLinus Torvalds * @nd: nameidata info 23281da177e4SLinus Torvalds * @is_dir: directory flag 23291da177e4SLinus Torvalds * 23301da177e4SLinus Torvalds * Simple function to lookup and return a dentry and create it 23311da177e4SLinus Torvalds * if it doesn't exist. Is SMP-safe. 2332c663e5d8SChristoph Hellwig * 23334ac91378SJan Blunck * Returns with nd->path.dentry->d_inode->i_mutex locked. 23341da177e4SLinus Torvalds */ 23351da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir) 23361da177e4SLinus Torvalds { 2337c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 23381da177e4SLinus Torvalds 23394ac91378SJan Blunck mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 2340c663e5d8SChristoph Hellwig /* 2341c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 2342c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 2343c663e5d8SChristoph Hellwig */ 23441da177e4SLinus Torvalds if (nd->last_type != LAST_NORM) 23451da177e4SLinus Torvalds goto fail; 23461da177e4SLinus Torvalds nd->flags &= ~LOOKUP_PARENT; 23473516586aSAl Viro nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL; 2348a634904aSASANO Masahiro nd->intent.open.flags = O_EXCL; 2349c663e5d8SChristoph Hellwig 2350c663e5d8SChristoph Hellwig /* 2351c663e5d8SChristoph Hellwig * Do the final lookup. 2352c663e5d8SChristoph Hellwig */ 235349705b77SChristoph Hellwig dentry = lookup_hash(nd); 23541da177e4SLinus Torvalds if (IS_ERR(dentry)) 23551da177e4SLinus Torvalds goto fail; 2356c663e5d8SChristoph Hellwig 2357e9baf6e5SAl Viro if (dentry->d_inode) 2358e9baf6e5SAl Viro goto eexist; 2359c663e5d8SChristoph Hellwig /* 2360c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 2361c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 2362c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 2363c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 2364c663e5d8SChristoph Hellwig */ 2365e9baf6e5SAl Viro if (unlikely(!is_dir && nd->last.name[nd->last.len])) { 23661da177e4SLinus Torvalds dput(dentry); 23671da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 2368e9baf6e5SAl Viro } 2369e9baf6e5SAl Viro return dentry; 2370e9baf6e5SAl Viro eexist: 2371e9baf6e5SAl Viro dput(dentry); 2372e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 23731da177e4SLinus Torvalds fail: 23741da177e4SLinus Torvalds return dentry; 23751da177e4SLinus Torvalds } 2376f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create); 23771da177e4SLinus Torvalds 23781da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 23791da177e4SLinus Torvalds { 2380a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 23811da177e4SLinus Torvalds 23821da177e4SLinus Torvalds if (error) 23831da177e4SLinus Torvalds return error; 23841da177e4SLinus Torvalds 2385e795b717SSerge E. Hallyn if ((S_ISCHR(mode) || S_ISBLK(mode)) && 2386e795b717SSerge E. Hallyn !ns_capable(inode_userns(dir), CAP_MKNOD)) 23871da177e4SLinus Torvalds return -EPERM; 23881da177e4SLinus Torvalds 2389acfa4380SAl Viro if (!dir->i_op->mknod) 23901da177e4SLinus Torvalds return -EPERM; 23911da177e4SLinus Torvalds 239208ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 239308ce5f16SSerge E. Hallyn if (error) 239408ce5f16SSerge E. Hallyn return error; 239508ce5f16SSerge E. Hallyn 23961da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 23971da177e4SLinus Torvalds if (error) 23981da177e4SLinus Torvalds return error; 23991da177e4SLinus Torvalds 24001da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 2401a74574aaSStephen Smalley if (!error) 2402f38aa942SAmy Griffis fsnotify_create(dir, dentry); 24031da177e4SLinus Torvalds return error; 24041da177e4SLinus Torvalds } 24051da177e4SLinus Torvalds 2406463c3197SDave Hansen static int may_mknod(mode_t mode) 2407463c3197SDave Hansen { 2408463c3197SDave Hansen switch (mode & S_IFMT) { 2409463c3197SDave Hansen case S_IFREG: 2410463c3197SDave Hansen case S_IFCHR: 2411463c3197SDave Hansen case S_IFBLK: 2412463c3197SDave Hansen case S_IFIFO: 2413463c3197SDave Hansen case S_IFSOCK: 2414463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2415463c3197SDave Hansen return 0; 2416463c3197SDave Hansen case S_IFDIR: 2417463c3197SDave Hansen return -EPERM; 2418463c3197SDave Hansen default: 2419463c3197SDave Hansen return -EINVAL; 2420463c3197SDave Hansen } 2421463c3197SDave Hansen } 2422463c3197SDave Hansen 24232e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode, 24242e4d0924SHeiko Carstens unsigned, dev) 24251da177e4SLinus Torvalds { 24262ad94ae6SAl Viro int error; 24271da177e4SLinus Torvalds char *tmp; 24281da177e4SLinus Torvalds struct dentry *dentry; 24291da177e4SLinus Torvalds struct nameidata nd; 24301da177e4SLinus Torvalds 24311da177e4SLinus Torvalds if (S_ISDIR(mode)) 24321da177e4SLinus Torvalds return -EPERM; 24331da177e4SLinus Torvalds 24342ad94ae6SAl Viro error = user_path_parent(dfd, filename, &nd, &tmp); 24351da177e4SLinus Torvalds if (error) 24362ad94ae6SAl Viro return error; 24372ad94ae6SAl Viro 24381da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 2439463c3197SDave Hansen if (IS_ERR(dentry)) { 24401da177e4SLinus Torvalds error = PTR_ERR(dentry); 2441463c3197SDave Hansen goto out_unlock; 2442463c3197SDave Hansen } 24434ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2444ce3b0f8dSAl Viro mode &= ~current_umask(); 2445463c3197SDave Hansen error = may_mknod(mode); 2446463c3197SDave Hansen if (error) 2447463c3197SDave Hansen goto out_dput; 2448463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2449463c3197SDave Hansen if (error) 2450463c3197SDave Hansen goto out_dput; 2451be6d3e56SKentaro Takeda error = security_path_mknod(&nd.path, dentry, mode, dev); 2452be6d3e56SKentaro Takeda if (error) 2453be6d3e56SKentaro Takeda goto out_drop_write; 24541da177e4SLinus Torvalds switch (mode & S_IFMT) { 24551da177e4SLinus Torvalds case 0: case S_IFREG: 24564ac91378SJan Blunck error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd); 24571da177e4SLinus Torvalds break; 24581da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 24594ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode, 24601da177e4SLinus Torvalds new_decode_dev(dev)); 24611da177e4SLinus Torvalds break; 24621da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 24634ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0); 24641da177e4SLinus Torvalds break; 24651da177e4SLinus Torvalds } 2466be6d3e56SKentaro Takeda out_drop_write: 2467463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2468463c3197SDave Hansen out_dput: 24691da177e4SLinus Torvalds dput(dentry); 2470463c3197SDave Hansen out_unlock: 24714ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 24721d957f9bSJan Blunck path_put(&nd.path); 24731da177e4SLinus Torvalds putname(tmp); 24741da177e4SLinus Torvalds 24751da177e4SLinus Torvalds return error; 24761da177e4SLinus Torvalds } 24771da177e4SLinus Torvalds 24783480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev) 24795590ff0dSUlrich Drepper { 24805590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 24815590ff0dSUlrich Drepper } 24825590ff0dSUlrich Drepper 24831da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 24841da177e4SLinus Torvalds { 2485a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 24861da177e4SLinus Torvalds 24871da177e4SLinus Torvalds if (error) 24881da177e4SLinus Torvalds return error; 24891da177e4SLinus Torvalds 2490acfa4380SAl Viro if (!dir->i_op->mkdir) 24911da177e4SLinus Torvalds return -EPERM; 24921da177e4SLinus Torvalds 24931da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 24941da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 24951da177e4SLinus Torvalds if (error) 24961da177e4SLinus Torvalds return error; 24971da177e4SLinus Torvalds 24981da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2499a74574aaSStephen Smalley if (!error) 2500f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 25011da177e4SLinus Torvalds return error; 25021da177e4SLinus Torvalds } 25031da177e4SLinus Torvalds 25042e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode) 25051da177e4SLinus Torvalds { 25061da177e4SLinus Torvalds int error = 0; 25071da177e4SLinus Torvalds char * tmp; 25086902d925SDave Hansen struct dentry *dentry; 25096902d925SDave Hansen struct nameidata nd; 25101da177e4SLinus Torvalds 25112ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &tmp); 25122ad94ae6SAl Viro if (error) 25136902d925SDave Hansen goto out_err; 25141da177e4SLinus Torvalds 25151da177e4SLinus Torvalds dentry = lookup_create(&nd, 1); 25161da177e4SLinus Torvalds error = PTR_ERR(dentry); 25176902d925SDave Hansen if (IS_ERR(dentry)) 25186902d925SDave Hansen goto out_unlock; 25196902d925SDave Hansen 25204ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2521ce3b0f8dSAl Viro mode &= ~current_umask(); 2522463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2523463c3197SDave Hansen if (error) 2524463c3197SDave Hansen goto out_dput; 2525be6d3e56SKentaro Takeda error = security_path_mkdir(&nd.path, dentry, mode); 2526be6d3e56SKentaro Takeda if (error) 2527be6d3e56SKentaro Takeda goto out_drop_write; 25284ac91378SJan Blunck error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode); 2529be6d3e56SKentaro Takeda out_drop_write: 2530463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2531463c3197SDave Hansen out_dput: 25321da177e4SLinus Torvalds dput(dentry); 25336902d925SDave Hansen out_unlock: 25344ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 25351d957f9bSJan Blunck path_put(&nd.path); 25361da177e4SLinus Torvalds putname(tmp); 25376902d925SDave Hansen out_err: 25381da177e4SLinus Torvalds return error; 25391da177e4SLinus Torvalds } 25401da177e4SLinus Torvalds 25413cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode) 25425590ff0dSUlrich Drepper { 25435590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 25445590ff0dSUlrich Drepper } 25455590ff0dSUlrich Drepper 25461da177e4SLinus Torvalds /* 2547a71905f0SSage Weil * The dentry_unhash() helper will try to drop the dentry early: we 2548a71905f0SSage Weil * should have a usage count of 2 if we're the only user of this 2549a71905f0SSage Weil * dentry, and if that is true (possibly after pruning the dcache), 2550a71905f0SSage Weil * then we drop the dentry now. 25511da177e4SLinus Torvalds * 25521da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 25531da177e4SLinus Torvalds * do a 25541da177e4SLinus Torvalds * 25551da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 25561da177e4SLinus Torvalds * return -EBUSY; 25571da177e4SLinus Torvalds * 25581da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 25591da177e4SLinus Torvalds * that is still in use by something else.. 25601da177e4SLinus Torvalds */ 25611da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 25621da177e4SLinus Torvalds { 25631da177e4SLinus Torvalds shrink_dcache_parent(dentry); 25641da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 256564252c75SSage Weil if (dentry->d_count == 1) 25661da177e4SLinus Torvalds __d_drop(dentry); 25671da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 25681da177e4SLinus Torvalds } 25691da177e4SLinus Torvalds 25701da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 25711da177e4SLinus Torvalds { 25721da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 25731da177e4SLinus Torvalds 25741da177e4SLinus Torvalds if (error) 25751da177e4SLinus Torvalds return error; 25761da177e4SLinus Torvalds 2577acfa4380SAl Viro if (!dir->i_op->rmdir) 25781da177e4SLinus Torvalds return -EPERM; 25791da177e4SLinus Torvalds 25801b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 2581912dbc15SSage Weil 25821da177e4SLinus Torvalds error = -EBUSY; 2583912dbc15SSage Weil if (d_mountpoint(dentry)) 2584912dbc15SSage Weil goto out; 2585912dbc15SSage Weil 25861da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 2587912dbc15SSage Weil if (error) 2588912dbc15SSage Weil goto out; 2589912dbc15SSage Weil 25903cebde24SSage Weil shrink_dcache_parent(dentry); 25911da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 2592912dbc15SSage Weil if (error) 2593912dbc15SSage Weil goto out; 2594912dbc15SSage Weil 25951da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 2596d83c49f3SAl Viro dont_mount(dentry); 25971da177e4SLinus Torvalds 2598912dbc15SSage Weil out: 2599912dbc15SSage Weil mutex_unlock(&dentry->d_inode->i_mutex); 2600912dbc15SSage Weil if (!error) 2601912dbc15SSage Weil d_delete(dentry); 26021da177e4SLinus Torvalds return error; 26031da177e4SLinus Torvalds } 26041da177e4SLinus Torvalds 26055590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 26061da177e4SLinus Torvalds { 26071da177e4SLinus Torvalds int error = 0; 26081da177e4SLinus Torvalds char * name; 26091da177e4SLinus Torvalds struct dentry *dentry; 26101da177e4SLinus Torvalds struct nameidata nd; 26111da177e4SLinus Torvalds 26122ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 26131da177e4SLinus Torvalds if (error) 26142ad94ae6SAl Viro return error; 26151da177e4SLinus Torvalds 26161da177e4SLinus Torvalds switch(nd.last_type) { 26171da177e4SLinus Torvalds case LAST_DOTDOT: 26181da177e4SLinus Torvalds error = -ENOTEMPTY; 26191da177e4SLinus Torvalds goto exit1; 26201da177e4SLinus Torvalds case LAST_DOT: 26211da177e4SLinus Torvalds error = -EINVAL; 26221da177e4SLinus Torvalds goto exit1; 26231da177e4SLinus Torvalds case LAST_ROOT: 26241da177e4SLinus Torvalds error = -EBUSY; 26251da177e4SLinus Torvalds goto exit1; 26261da177e4SLinus Torvalds } 26270612d9fbSOGAWA Hirofumi 26280612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 26290612d9fbSOGAWA Hirofumi 26304ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 263149705b77SChristoph Hellwig dentry = lookup_hash(&nd); 26321da177e4SLinus Torvalds error = PTR_ERR(dentry); 26336902d925SDave Hansen if (IS_ERR(dentry)) 26346902d925SDave Hansen goto exit2; 2635e6bc45d6STheodore Ts'o if (!dentry->d_inode) { 2636e6bc45d6STheodore Ts'o error = -ENOENT; 2637e6bc45d6STheodore Ts'o goto exit3; 2638e6bc45d6STheodore Ts'o } 26390622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 26400622753bSDave Hansen if (error) 26410622753bSDave Hansen goto exit3; 2642be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 2643be6d3e56SKentaro Takeda if (error) 2644be6d3e56SKentaro Takeda goto exit4; 26454ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 2646be6d3e56SKentaro Takeda exit4: 26470622753bSDave Hansen mnt_drop_write(nd.path.mnt); 26480622753bSDave Hansen exit3: 26491da177e4SLinus Torvalds dput(dentry); 26506902d925SDave Hansen exit2: 26514ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 26521da177e4SLinus Torvalds exit1: 26531d957f9bSJan Blunck path_put(&nd.path); 26541da177e4SLinus Torvalds putname(name); 26551da177e4SLinus Torvalds return error; 26561da177e4SLinus Torvalds } 26571da177e4SLinus Torvalds 26583cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 26595590ff0dSUlrich Drepper { 26605590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 26615590ff0dSUlrich Drepper } 26625590ff0dSUlrich Drepper 26631da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 26641da177e4SLinus Torvalds { 26651da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 26661da177e4SLinus Torvalds 26671da177e4SLinus Torvalds if (error) 26681da177e4SLinus Torvalds return error; 26691da177e4SLinus Torvalds 2670acfa4380SAl Viro if (!dir->i_op->unlink) 26711da177e4SLinus Torvalds return -EPERM; 26721da177e4SLinus Torvalds 26731b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 26741da177e4SLinus Torvalds if (d_mountpoint(dentry)) 26751da177e4SLinus Torvalds error = -EBUSY; 26761da177e4SLinus Torvalds else { 26771da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 2678bec1052eSAl Viro if (!error) { 26791da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 2680bec1052eSAl Viro if (!error) 2681d83c49f3SAl Viro dont_mount(dentry); 2682bec1052eSAl Viro } 26831da177e4SLinus Torvalds } 26841b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 26851da177e4SLinus Torvalds 26861da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 26871da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 2688ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 26891da177e4SLinus Torvalds d_delete(dentry); 26901da177e4SLinus Torvalds } 26910eeca283SRobert Love 26921da177e4SLinus Torvalds return error; 26931da177e4SLinus Torvalds } 26941da177e4SLinus Torvalds 26951da177e4SLinus Torvalds /* 26961da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 26971b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 26981da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 26991da177e4SLinus Torvalds * while waiting on the I/O. 27001da177e4SLinus Torvalds */ 27015590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 27021da177e4SLinus Torvalds { 27032ad94ae6SAl Viro int error; 27041da177e4SLinus Torvalds char *name; 27051da177e4SLinus Torvalds struct dentry *dentry; 27061da177e4SLinus Torvalds struct nameidata nd; 27071da177e4SLinus Torvalds struct inode *inode = NULL; 27081da177e4SLinus Torvalds 27092ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 27101da177e4SLinus Torvalds if (error) 27112ad94ae6SAl Viro return error; 27122ad94ae6SAl Viro 27131da177e4SLinus Torvalds error = -EISDIR; 27141da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 27151da177e4SLinus Torvalds goto exit1; 27160612d9fbSOGAWA Hirofumi 27170612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 27180612d9fbSOGAWA Hirofumi 27194ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 272049705b77SChristoph Hellwig dentry = lookup_hash(&nd); 27211da177e4SLinus Torvalds error = PTR_ERR(dentry); 27221da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 27231da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 272450338b88STörök Edwin if (nd.last.name[nd.last.len]) 272550338b88STörök Edwin goto slashes; 27261da177e4SLinus Torvalds inode = dentry->d_inode; 272750338b88STörök Edwin if (!inode) 2728e6bc45d6STheodore Ts'o goto slashes; 27297de9c6eeSAl Viro ihold(inode); 27300622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 27310622753bSDave Hansen if (error) 27320622753bSDave Hansen goto exit2; 2733be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 2734be6d3e56SKentaro Takeda if (error) 2735be6d3e56SKentaro Takeda goto exit3; 27364ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 2737be6d3e56SKentaro Takeda exit3: 27380622753bSDave Hansen mnt_drop_write(nd.path.mnt); 27391da177e4SLinus Torvalds exit2: 27401da177e4SLinus Torvalds dput(dentry); 27411da177e4SLinus Torvalds } 27424ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 27431da177e4SLinus Torvalds if (inode) 27441da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 27451da177e4SLinus Torvalds exit1: 27461d957f9bSJan Blunck path_put(&nd.path); 27471da177e4SLinus Torvalds putname(name); 27481da177e4SLinus Torvalds return error; 27491da177e4SLinus Torvalds 27501da177e4SLinus Torvalds slashes: 27511da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 27521da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 27531da177e4SLinus Torvalds goto exit2; 27541da177e4SLinus Torvalds } 27551da177e4SLinus Torvalds 27562e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 27575590ff0dSUlrich Drepper { 27585590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 27595590ff0dSUlrich Drepper return -EINVAL; 27605590ff0dSUlrich Drepper 27615590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 27625590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 27635590ff0dSUlrich Drepper 27645590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 27655590ff0dSUlrich Drepper } 27665590ff0dSUlrich Drepper 27673480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 27685590ff0dSUlrich Drepper { 27695590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 27705590ff0dSUlrich Drepper } 27715590ff0dSUlrich Drepper 2772db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 27731da177e4SLinus Torvalds { 2774a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 27751da177e4SLinus Torvalds 27761da177e4SLinus Torvalds if (error) 27771da177e4SLinus Torvalds return error; 27781da177e4SLinus Torvalds 2779acfa4380SAl Viro if (!dir->i_op->symlink) 27801da177e4SLinus Torvalds return -EPERM; 27811da177e4SLinus Torvalds 27821da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 27831da177e4SLinus Torvalds if (error) 27841da177e4SLinus Torvalds return error; 27851da177e4SLinus Torvalds 27861da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 2787a74574aaSStephen Smalley if (!error) 2788f38aa942SAmy Griffis fsnotify_create(dir, dentry); 27891da177e4SLinus Torvalds return error; 27901da177e4SLinus Torvalds } 27911da177e4SLinus Torvalds 27922e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 27932e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 27941da177e4SLinus Torvalds { 27952ad94ae6SAl Viro int error; 27961da177e4SLinus Torvalds char *from; 27971da177e4SLinus Torvalds char *to; 27986902d925SDave Hansen struct dentry *dentry; 27996902d925SDave Hansen struct nameidata nd; 28001da177e4SLinus Torvalds 28011da177e4SLinus Torvalds from = getname(oldname); 28021da177e4SLinus Torvalds if (IS_ERR(from)) 28031da177e4SLinus Torvalds return PTR_ERR(from); 28042ad94ae6SAl Viro 28052ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 28062ad94ae6SAl Viro if (error) 28076902d925SDave Hansen goto out_putname; 28081da177e4SLinus Torvalds 28091da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 28101da177e4SLinus Torvalds error = PTR_ERR(dentry); 28116902d925SDave Hansen if (IS_ERR(dentry)) 28126902d925SDave Hansen goto out_unlock; 28136902d925SDave Hansen 281475c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 281575c3f29dSDave Hansen if (error) 281675c3f29dSDave Hansen goto out_dput; 2817be6d3e56SKentaro Takeda error = security_path_symlink(&nd.path, dentry, from); 2818be6d3e56SKentaro Takeda if (error) 2819be6d3e56SKentaro Takeda goto out_drop_write; 2820db2e747bSMiklos Szeredi error = vfs_symlink(nd.path.dentry->d_inode, dentry, from); 2821be6d3e56SKentaro Takeda out_drop_write: 282275c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 282375c3f29dSDave Hansen out_dput: 28241da177e4SLinus Torvalds dput(dentry); 28256902d925SDave Hansen out_unlock: 28264ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 28271d957f9bSJan Blunck path_put(&nd.path); 28281da177e4SLinus Torvalds putname(to); 28296902d925SDave Hansen out_putname: 28301da177e4SLinus Torvalds putname(from); 28311da177e4SLinus Torvalds return error; 28321da177e4SLinus Torvalds } 28331da177e4SLinus Torvalds 28343480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 28355590ff0dSUlrich Drepper { 28365590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 28375590ff0dSUlrich Drepper } 28385590ff0dSUlrich Drepper 28391da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 28401da177e4SLinus Torvalds { 28411da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 28421da177e4SLinus Torvalds int error; 28431da177e4SLinus Torvalds 28441da177e4SLinus Torvalds if (!inode) 28451da177e4SLinus Torvalds return -ENOENT; 28461da177e4SLinus Torvalds 2847a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 28481da177e4SLinus Torvalds if (error) 28491da177e4SLinus Torvalds return error; 28501da177e4SLinus Torvalds 28511da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 28521da177e4SLinus Torvalds return -EXDEV; 28531da177e4SLinus Torvalds 28541da177e4SLinus Torvalds /* 28551da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 28561da177e4SLinus Torvalds */ 28571da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 28581da177e4SLinus Torvalds return -EPERM; 2859acfa4380SAl Viro if (!dir->i_op->link) 28601da177e4SLinus Torvalds return -EPERM; 28617e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 28621da177e4SLinus Torvalds return -EPERM; 28631da177e4SLinus Torvalds 28641da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 28651da177e4SLinus Torvalds if (error) 28661da177e4SLinus Torvalds return error; 28671da177e4SLinus Torvalds 28687e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 2869aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 2870aae8a97dSAneesh Kumar K.V if (inode->i_nlink == 0) 2871aae8a97dSAneesh Kumar K.V error = -ENOENT; 2872aae8a97dSAneesh Kumar K.V else 28731da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 28747e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 2875e31e14ecSStephen Smalley if (!error) 28767e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 28771da177e4SLinus Torvalds return error; 28781da177e4SLinus Torvalds } 28791da177e4SLinus Torvalds 28801da177e4SLinus Torvalds /* 28811da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 28821da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 28831da177e4SLinus Torvalds * newname. --KAB 28841da177e4SLinus Torvalds * 28851da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 28861da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 28871da177e4SLinus Torvalds * and other special files. --ADM 28881da177e4SLinus Torvalds */ 28892e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 28902e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 28911da177e4SLinus Torvalds { 28921da177e4SLinus Torvalds struct dentry *new_dentry; 28932d8f3038SAl Viro struct nameidata nd; 28942d8f3038SAl Viro struct path old_path; 289511a7b371SAneesh Kumar K.V int how = 0; 28961da177e4SLinus Torvalds int error; 28971da177e4SLinus Torvalds char *to; 28981da177e4SLinus Torvalds 289911a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 2900c04030e1SUlrich Drepper return -EINVAL; 290111a7b371SAneesh Kumar K.V /* 290211a7b371SAneesh Kumar K.V * To use null names we require CAP_DAC_READ_SEARCH 290311a7b371SAneesh Kumar K.V * This ensures that not everyone will be able to create 290411a7b371SAneesh Kumar K.V * handlink using the passed filedescriptor. 290511a7b371SAneesh Kumar K.V */ 290611a7b371SAneesh Kumar K.V if (flags & AT_EMPTY_PATH) { 290711a7b371SAneesh Kumar K.V if (!capable(CAP_DAC_READ_SEARCH)) 290811a7b371SAneesh Kumar K.V return -ENOENT; 290911a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 291011a7b371SAneesh Kumar K.V } 2911c04030e1SUlrich Drepper 291211a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 291311a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 291411a7b371SAneesh Kumar K.V 291511a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 29161da177e4SLinus Torvalds if (error) 29172ad94ae6SAl Viro return error; 29182ad94ae6SAl Viro 29192ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 29201da177e4SLinus Torvalds if (error) 29211da177e4SLinus Torvalds goto out; 29221da177e4SLinus Torvalds error = -EXDEV; 29232d8f3038SAl Viro if (old_path.mnt != nd.path.mnt) 29241da177e4SLinus Torvalds goto out_release; 29251da177e4SLinus Torvalds new_dentry = lookup_create(&nd, 0); 29261da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 29276902d925SDave Hansen if (IS_ERR(new_dentry)) 29286902d925SDave Hansen goto out_unlock; 292975c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 293075c3f29dSDave Hansen if (error) 293175c3f29dSDave Hansen goto out_dput; 2932be6d3e56SKentaro Takeda error = security_path_link(old_path.dentry, &nd.path, new_dentry); 2933be6d3e56SKentaro Takeda if (error) 2934be6d3e56SKentaro Takeda goto out_drop_write; 29352d8f3038SAl Viro error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry); 2936be6d3e56SKentaro Takeda out_drop_write: 293775c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 293875c3f29dSDave Hansen out_dput: 29391da177e4SLinus Torvalds dput(new_dentry); 29406902d925SDave Hansen out_unlock: 29414ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 29421da177e4SLinus Torvalds out_release: 29431d957f9bSJan Blunck path_put(&nd.path); 29442ad94ae6SAl Viro putname(to); 29451da177e4SLinus Torvalds out: 29462d8f3038SAl Viro path_put(&old_path); 29471da177e4SLinus Torvalds 29481da177e4SLinus Torvalds return error; 29491da177e4SLinus Torvalds } 29501da177e4SLinus Torvalds 29513480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 29525590ff0dSUlrich Drepper { 2953c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 29545590ff0dSUlrich Drepper } 29555590ff0dSUlrich Drepper 29561da177e4SLinus Torvalds /* 29571da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 29581da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 29591da177e4SLinus Torvalds * Problems: 29601da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 29611da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 29621da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 2963a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 29641da177e4SLinus Torvalds * story. 29651da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 29661b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 29671da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 29681da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 2969a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 29701da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 29711da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 29721da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 2973a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 29741da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 29751da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 29761da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 2977e4eaac06SSage Weil * d) conversion from fhandle to dentry may come in the wrong moment - when 29781b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 29791da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 2980c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 29811da177e4SLinus Torvalds * locking]. 29821da177e4SLinus Torvalds */ 298375c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 29841da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 29851da177e4SLinus Torvalds { 29861da177e4SLinus Torvalds int error = 0; 29879055cba7SSage Weil struct inode *target = new_dentry->d_inode; 29881da177e4SLinus Torvalds 29891da177e4SLinus Torvalds /* 29901da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 29911da177e4SLinus Torvalds * we'll need to flip '..'. 29921da177e4SLinus Torvalds */ 29931da177e4SLinus Torvalds if (new_dir != old_dir) { 2994f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 29951da177e4SLinus Torvalds if (error) 29961da177e4SLinus Torvalds return error; 29971da177e4SLinus Torvalds } 29981da177e4SLinus Torvalds 29991da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 30001da177e4SLinus Torvalds if (error) 30011da177e4SLinus Torvalds return error; 30021da177e4SLinus Torvalds 3003d83c49f3SAl Viro if (target) 30041b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 30059055cba7SSage Weil 30061da177e4SLinus Torvalds error = -EBUSY; 30079055cba7SSage Weil if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) 30089055cba7SSage Weil goto out; 30099055cba7SSage Weil 30103cebde24SSage Weil if (target) 30113cebde24SSage Weil shrink_dcache_parent(new_dentry); 30121da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 30139055cba7SSage Weil if (error) 30149055cba7SSage Weil goto out; 30159055cba7SSage Weil 30161da177e4SLinus Torvalds if (target) { 30171da177e4SLinus Torvalds target->i_flags |= S_DEAD; 3018d83c49f3SAl Viro dont_mount(new_dentry); 3019d83c49f3SAl Viro } 30209055cba7SSage Weil out: 30219055cba7SSage Weil if (target) 30221b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 3023e31e14ecSStephen Smalley if (!error) 3024349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 30251da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 30261da177e4SLinus Torvalds return error; 30271da177e4SLinus Torvalds } 30281da177e4SLinus Torvalds 302975c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 30301da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30311da177e4SLinus Torvalds { 303251892bbbSSage Weil struct inode *target = new_dentry->d_inode; 30331da177e4SLinus Torvalds int error; 30341da177e4SLinus Torvalds 30351da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 30361da177e4SLinus Torvalds if (error) 30371da177e4SLinus Torvalds return error; 30381da177e4SLinus Torvalds 30391da177e4SLinus Torvalds dget(new_dentry); 30401da177e4SLinus Torvalds if (target) 30411b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 304251892bbbSSage Weil 30431da177e4SLinus Torvalds error = -EBUSY; 304451892bbbSSage Weil if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 304551892bbbSSage Weil goto out; 304651892bbbSSage Weil 30471da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 304851892bbbSSage Weil if (error) 304951892bbbSSage Weil goto out; 305051892bbbSSage Weil 3051bec1052eSAl Viro if (target) 3052d83c49f3SAl Viro dont_mount(new_dentry); 3053349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 30541da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 305551892bbbSSage Weil out: 30561da177e4SLinus Torvalds if (target) 30571b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 30581da177e4SLinus Torvalds dput(new_dentry); 30591da177e4SLinus Torvalds return error; 30601da177e4SLinus Torvalds } 30611da177e4SLinus Torvalds 30621da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 30631da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30641da177e4SLinus Torvalds { 30651da177e4SLinus Torvalds int error; 30661da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 306759b0df21SEric Paris const unsigned char *old_name; 30681da177e4SLinus Torvalds 30691da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 30701da177e4SLinus Torvalds return 0; 30711da177e4SLinus Torvalds 30721da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 30731da177e4SLinus Torvalds if (error) 30741da177e4SLinus Torvalds return error; 30751da177e4SLinus Torvalds 30761da177e4SLinus Torvalds if (!new_dentry->d_inode) 3077a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 30781da177e4SLinus Torvalds else 30791da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 30801da177e4SLinus Torvalds if (error) 30811da177e4SLinus Torvalds return error; 30821da177e4SLinus Torvalds 3083acfa4380SAl Viro if (!old_dir->i_op->rename) 30841da177e4SLinus Torvalds return -EPERM; 30851da177e4SLinus Torvalds 30860eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 30870eeca283SRobert Love 30881da177e4SLinus Torvalds if (is_dir) 30891da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 30901da177e4SLinus Torvalds else 30911da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3092123df294SAl Viro if (!error) 3093123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 30945a190ae6SAl Viro new_dentry->d_inode, old_dentry); 30950eeca283SRobert Love fsnotify_oldname_free(old_name); 30960eeca283SRobert Love 30971da177e4SLinus Torvalds return error; 30981da177e4SLinus Torvalds } 30991da177e4SLinus Torvalds 31002e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 31012e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 31021da177e4SLinus Torvalds { 31031da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 31041da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 31051da177e4SLinus Torvalds struct dentry *trap; 31061da177e4SLinus Torvalds struct nameidata oldnd, newnd; 31072ad94ae6SAl Viro char *from; 31082ad94ae6SAl Viro char *to; 31092ad94ae6SAl Viro int error; 31101da177e4SLinus Torvalds 31112ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 31121da177e4SLinus Torvalds if (error) 31131da177e4SLinus Torvalds goto exit; 31141da177e4SLinus Torvalds 31152ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 31161da177e4SLinus Torvalds if (error) 31171da177e4SLinus Torvalds goto exit1; 31181da177e4SLinus Torvalds 31191da177e4SLinus Torvalds error = -EXDEV; 31204ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 31211da177e4SLinus Torvalds goto exit2; 31221da177e4SLinus Torvalds 31234ac91378SJan Blunck old_dir = oldnd.path.dentry; 31241da177e4SLinus Torvalds error = -EBUSY; 31251da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 31261da177e4SLinus Torvalds goto exit2; 31271da177e4SLinus Torvalds 31284ac91378SJan Blunck new_dir = newnd.path.dentry; 31291da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 31301da177e4SLinus Torvalds goto exit2; 31311da177e4SLinus Torvalds 31320612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 31330612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 31344e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 31350612d9fbSOGAWA Hirofumi 31361da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 31371da177e4SLinus Torvalds 313849705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 31391da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 31401da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 31411da177e4SLinus Torvalds goto exit3; 31421da177e4SLinus Torvalds /* source must exist */ 31431da177e4SLinus Torvalds error = -ENOENT; 31441da177e4SLinus Torvalds if (!old_dentry->d_inode) 31451da177e4SLinus Torvalds goto exit4; 31461da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 31471da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 31481da177e4SLinus Torvalds error = -ENOTDIR; 31491da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 31501da177e4SLinus Torvalds goto exit4; 31511da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 31521da177e4SLinus Torvalds goto exit4; 31531da177e4SLinus Torvalds } 31541da177e4SLinus Torvalds /* source should not be ancestor of target */ 31551da177e4SLinus Torvalds error = -EINVAL; 31561da177e4SLinus Torvalds if (old_dentry == trap) 31571da177e4SLinus Torvalds goto exit4; 315849705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 31591da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 31601da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 31611da177e4SLinus Torvalds goto exit4; 31621da177e4SLinus Torvalds /* target should not be an ancestor of source */ 31631da177e4SLinus Torvalds error = -ENOTEMPTY; 31641da177e4SLinus Torvalds if (new_dentry == trap) 31651da177e4SLinus Torvalds goto exit5; 31661da177e4SLinus Torvalds 31679079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 31689079b1ebSDave Hansen if (error) 31699079b1ebSDave Hansen goto exit5; 3170be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3171be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3172be6d3e56SKentaro Takeda if (error) 3173be6d3e56SKentaro Takeda goto exit6; 31741da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 31751da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 3176be6d3e56SKentaro Takeda exit6: 31779079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 31781da177e4SLinus Torvalds exit5: 31791da177e4SLinus Torvalds dput(new_dentry); 31801da177e4SLinus Torvalds exit4: 31811da177e4SLinus Torvalds dput(old_dentry); 31821da177e4SLinus Torvalds exit3: 31831da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 31841da177e4SLinus Torvalds exit2: 31851d957f9bSJan Blunck path_put(&newnd.path); 31862ad94ae6SAl Viro putname(to); 31871da177e4SLinus Torvalds exit1: 31881d957f9bSJan Blunck path_put(&oldnd.path); 31891da177e4SLinus Torvalds putname(from); 31902ad94ae6SAl Viro exit: 31911da177e4SLinus Torvalds return error; 31921da177e4SLinus Torvalds } 31931da177e4SLinus Torvalds 3194a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 31955590ff0dSUlrich Drepper { 31965590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 31975590ff0dSUlrich Drepper } 31985590ff0dSUlrich Drepper 31991da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 32001da177e4SLinus Torvalds { 32011da177e4SLinus Torvalds int len; 32021da177e4SLinus Torvalds 32031da177e4SLinus Torvalds len = PTR_ERR(link); 32041da177e4SLinus Torvalds if (IS_ERR(link)) 32051da177e4SLinus Torvalds goto out; 32061da177e4SLinus Torvalds 32071da177e4SLinus Torvalds len = strlen(link); 32081da177e4SLinus Torvalds if (len > (unsigned) buflen) 32091da177e4SLinus Torvalds len = buflen; 32101da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 32111da177e4SLinus Torvalds len = -EFAULT; 32121da177e4SLinus Torvalds out: 32131da177e4SLinus Torvalds return len; 32141da177e4SLinus Torvalds } 32151da177e4SLinus Torvalds 32161da177e4SLinus Torvalds /* 32171da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 32181da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 32191da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 32201da177e4SLinus Torvalds */ 32211da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 32221da177e4SLinus Torvalds { 32231da177e4SLinus Torvalds struct nameidata nd; 3224cc314eefSLinus Torvalds void *cookie; 3225694a1764SMarcin Slusarz int res; 3226cc314eefSLinus Torvalds 32271da177e4SLinus Torvalds nd.depth = 0; 3228cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3229694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3230694a1764SMarcin Slusarz return PTR_ERR(cookie); 3231694a1764SMarcin Slusarz 3232694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 32331da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3234cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3235694a1764SMarcin Slusarz return res; 32361da177e4SLinus Torvalds } 32371da177e4SLinus Torvalds 32381da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 32391da177e4SLinus Torvalds { 32401da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 32411da177e4SLinus Torvalds } 32421da177e4SLinus Torvalds 32431da177e4SLinus Torvalds /* get the link contents into pagecache */ 32441da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 32451da177e4SLinus Torvalds { 3246ebd09abbSDuane Griffin char *kaddr; 32471da177e4SLinus Torvalds struct page *page; 32481da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3249090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 32501da177e4SLinus Torvalds if (IS_ERR(page)) 32516fe6900eSNick Piggin return (char*)page; 32521da177e4SLinus Torvalds *ppage = page; 3253ebd09abbSDuane Griffin kaddr = kmap(page); 3254ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3255ebd09abbSDuane Griffin return kaddr; 32561da177e4SLinus Torvalds } 32571da177e4SLinus Torvalds 32581da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 32591da177e4SLinus Torvalds { 32601da177e4SLinus Torvalds struct page *page = NULL; 32611da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 32621da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 32631da177e4SLinus Torvalds if (page) { 32641da177e4SLinus Torvalds kunmap(page); 32651da177e4SLinus Torvalds page_cache_release(page); 32661da177e4SLinus Torvalds } 32671da177e4SLinus Torvalds return res; 32681da177e4SLinus Torvalds } 32691da177e4SLinus Torvalds 3270cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 32711da177e4SLinus Torvalds { 3272cc314eefSLinus Torvalds struct page *page = NULL; 32731da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 3274cc314eefSLinus Torvalds return page; 32751da177e4SLinus Torvalds } 32761da177e4SLinus Torvalds 3277cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 32781da177e4SLinus Torvalds { 3279cc314eefSLinus Torvalds struct page *page = cookie; 3280cc314eefSLinus Torvalds 3281cc314eefSLinus Torvalds if (page) { 32821da177e4SLinus Torvalds kunmap(page); 32831da177e4SLinus Torvalds page_cache_release(page); 32841da177e4SLinus Torvalds } 32851da177e4SLinus Torvalds } 32861da177e4SLinus Torvalds 328754566b2cSNick Piggin /* 328854566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 328954566b2cSNick Piggin */ 329054566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 32911da177e4SLinus Torvalds { 32921da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 32930adb25d2SKirill Korotaev struct page *page; 3294afddba49SNick Piggin void *fsdata; 3295beb497abSDmitriy Monakhov int err; 32961da177e4SLinus Torvalds char *kaddr; 329754566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 329854566b2cSNick Piggin if (nofs) 329954566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 33001da177e4SLinus Torvalds 33017e53cac4SNeilBrown retry: 3302afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 330354566b2cSNick Piggin flags, &page, &fsdata); 33041da177e4SLinus Torvalds if (err) 3305afddba49SNick Piggin goto fail; 3306afddba49SNick Piggin 33071da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 33081da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 33091da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 3310afddba49SNick Piggin 3311afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 3312afddba49SNick Piggin page, fsdata); 33131da177e4SLinus Torvalds if (err < 0) 33141da177e4SLinus Torvalds goto fail; 3315afddba49SNick Piggin if (err < len-1) 3316afddba49SNick Piggin goto retry; 3317afddba49SNick Piggin 33181da177e4SLinus Torvalds mark_inode_dirty(inode); 33191da177e4SLinus Torvalds return 0; 33201da177e4SLinus Torvalds fail: 33211da177e4SLinus Torvalds return err; 33221da177e4SLinus Torvalds } 33231da177e4SLinus Torvalds 33240adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 33250adb25d2SKirill Korotaev { 33260adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 332754566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 33280adb25d2SKirill Korotaev } 33290adb25d2SKirill Korotaev 333092e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 33311da177e4SLinus Torvalds .readlink = generic_readlink, 33321da177e4SLinus Torvalds .follow_link = page_follow_link_light, 33331da177e4SLinus Torvalds .put_link = page_put_link, 33341da177e4SLinus Torvalds }; 33351da177e4SLinus Torvalds 33362d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 3337cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one); 33381da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 33391da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 33401da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 33411da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 33421da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 33431da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 33441da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 33451da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 33461da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 33470adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 33481da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 33491da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 3350c9c6cac0SAl Viro EXPORT_SYMBOL(kern_path_parent); 3351d1811465SAl Viro EXPORT_SYMBOL(kern_path); 335216f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 3353f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 33548c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission); 33551da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 33561da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 33571da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 33581da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 33591da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 33601da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 33611da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 33621da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 33631da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 33641da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 33651da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 33661da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 33671da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 33681da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 3369