11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/namei.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* 81da177e4SLinus Torvalds * Some corrections by tytso. 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname 121da177e4SLinus Torvalds * lookup logic. 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture. 151da177e4SLinus Torvalds */ 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #include <linux/init.h> 181da177e4SLinus Torvalds #include <linux/module.h> 191da177e4SLinus Torvalds #include <linux/slab.h> 201da177e4SLinus Torvalds #include <linux/fs.h> 211da177e4SLinus Torvalds #include <linux/namei.h> 221da177e4SLinus Torvalds #include <linux/pagemap.h> 230eeca283SRobert Love #include <linux/fsnotify.h> 241da177e4SLinus Torvalds #include <linux/personality.h> 251da177e4SLinus Torvalds #include <linux/security.h> 266146f0d5SMimi Zohar #include <linux/ima.h> 271da177e4SLinus Torvalds #include <linux/syscalls.h> 281da177e4SLinus Torvalds #include <linux/mount.h> 291da177e4SLinus Torvalds #include <linux/audit.h> 3016f7e0feSRandy Dunlap #include <linux/capability.h> 31834f2a4aSTrond Myklebust #include <linux/file.h> 325590ff0dSUlrich Drepper #include <linux/fcntl.h> 3308ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h> 345ad4e53bSAl Viro #include <linux/fs_struct.h> 351da177e4SLinus Torvalds #include <asm/uaccess.h> 361da177e4SLinus Torvalds 37e81e3f4dSEric Paris #include "internal.h" 38e81e3f4dSEric Paris 391da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer] 401da177e4SLinus Torvalds * Fundamental changes in the pathname lookup mechanisms (namei) 411da177e4SLinus Torvalds * were necessary because of omirr. The reason is that omirr needs 421da177e4SLinus Torvalds * to know the _real_ pathname, not the user-supplied one, in case 431da177e4SLinus Torvalds * of symlinks (and also when transname replacements occur). 441da177e4SLinus Torvalds * 451da177e4SLinus Torvalds * The new code replaces the old recursive symlink resolution with 461da177e4SLinus Torvalds * an iterative one (in case of non-nested symlink chains). It does 471da177e4SLinus Torvalds * this with calls to <fs>_follow_link(). 481da177e4SLinus Torvalds * As a side effect, dir_namei(), _namei() and follow_link() are now 491da177e4SLinus Torvalds * replaced with a single function lookup_dentry() that can handle all 501da177e4SLinus Torvalds * the special cases of the former code. 511da177e4SLinus Torvalds * 521da177e4SLinus Torvalds * With the new dcache, the pathname is stored at each inode, at least as 531da177e4SLinus Torvalds * long as the refcount of the inode is positive. As a side effect, the 541da177e4SLinus Torvalds * size of the dcache depends on the inode cache and thus is dynamic. 551da177e4SLinus Torvalds * 561da177e4SLinus Torvalds * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink 571da177e4SLinus Torvalds * resolution to correspond with current state of the code. 581da177e4SLinus Torvalds * 591da177e4SLinus Torvalds * Note that the symlink resolution is not *completely* iterative. 601da177e4SLinus Torvalds * There is still a significant amount of tail- and mid- recursion in 611da177e4SLinus Torvalds * the algorithm. Also, note that <fs>_readlink() is not used in 621da177e4SLinus Torvalds * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() 631da177e4SLinus Torvalds * may return different results than <fs>_follow_link(). Many virtual 641da177e4SLinus Torvalds * filesystems (including /proc) exhibit this behavior. 651da177e4SLinus Torvalds */ 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: 681da177e4SLinus Torvalds * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL 691da177e4SLinus Torvalds * and the name already exists in form of a symlink, try to create the new 701da177e4SLinus Torvalds * name indicated by the symlink. The old code always complained that the 711da177e4SLinus Torvalds * name already exists, due to not following the symlink even if its target 721da177e4SLinus Torvalds * is nonexistent. The new semantics affects also mknod() and link() when 731da177e4SLinus Torvalds * the name is a symlink pointing to a non-existant name. 741da177e4SLinus Torvalds * 751da177e4SLinus Torvalds * I don't know which semantics is the right one, since I have no access 761da177e4SLinus Torvalds * to standards. But I found by trial that HP-UX 9.0 has the full "new" 771da177e4SLinus Torvalds * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the 781da177e4SLinus Torvalds * "old" one. Personally, I think the new semantics is much more logical. 791da177e4SLinus Torvalds * Note that "ln old new" where "new" is a symlink pointing to a non-existing 801da177e4SLinus Torvalds * file does succeed in both HP-UX and SunOs, but not in Solaris 811da177e4SLinus Torvalds * and in the old Linux semantics. 821da177e4SLinus Torvalds */ 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink 851da177e4SLinus Torvalds * semantics. See the comments in "open_namei" and "do_link" below. 861da177e4SLinus Torvalds * 871da177e4SLinus Torvalds * [10-Sep-98 Alan Modra] Another symlink change. 881da177e4SLinus Torvalds */ 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: 911da177e4SLinus Torvalds * inside the path - always follow. 921da177e4SLinus Torvalds * in the last component in creation/removal/renaming - never follow. 931da177e4SLinus Torvalds * if LOOKUP_FOLLOW passed - follow. 941da177e4SLinus Torvalds * if the pathname has trailing slashes - follow. 951da177e4SLinus Torvalds * otherwise - don't follow. 961da177e4SLinus Torvalds * (applied in that order). 971da177e4SLinus Torvalds * 981da177e4SLinus Torvalds * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT 991da177e4SLinus Torvalds * restored for 2.4. This is the last surviving part of old 4.2BSD bug. 1001da177e4SLinus Torvalds * During the 2.4 we need to fix the userland stuff depending on it - 1011da177e4SLinus Torvalds * hopefully we will be able to get rid of that wart in 2.5. So far only 1021da177e4SLinus Torvalds * XEmacs seems to be relying on it... 1031da177e4SLinus Torvalds */ 1041da177e4SLinus Torvalds /* 1051da177e4SLinus Torvalds * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) 106a11f3a05SArjan van de Ven * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives 1071da177e4SLinus Torvalds * any extra contention... 1081da177e4SLinus Torvalds */ 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional 1111da177e4SLinus Torvalds * checking and hopefully speeding things up, we copy filenames to the 1121da177e4SLinus Torvalds * kernel data space before using them.. 1131da177e4SLinus Torvalds * 1141da177e4SLinus Torvalds * POSIX.1 2.4: an empty pathname is invalid (ENOENT). 1151da177e4SLinus Torvalds * PATH_MAX includes the nul terminator --RR. 1161da177e4SLinus Torvalds */ 117858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page) 1181da177e4SLinus Torvalds { 1191da177e4SLinus Torvalds int retval; 1201da177e4SLinus Torvalds unsigned long len = PATH_MAX; 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds if (!segment_eq(get_fs(), KERNEL_DS)) { 1231da177e4SLinus Torvalds if ((unsigned long) filename >= TASK_SIZE) 1241da177e4SLinus Torvalds return -EFAULT; 1251da177e4SLinus Torvalds if (TASK_SIZE - (unsigned long) filename < PATH_MAX) 1261da177e4SLinus Torvalds len = TASK_SIZE - (unsigned long) filename; 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds retval = strncpy_from_user(page, filename, len); 1301da177e4SLinus Torvalds if (retval > 0) { 1311da177e4SLinus Torvalds if (retval < len) 1321da177e4SLinus Torvalds return 0; 1331da177e4SLinus Torvalds return -ENAMETOOLONG; 1341da177e4SLinus Torvalds } else if (!retval) 1351da177e4SLinus Torvalds retval = -ENOENT; 1361da177e4SLinus Torvalds return retval; 1371da177e4SLinus Torvalds } 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds char * getname(const char __user * filename) 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) { 1501da177e4SLinus Torvalds __putname(tmp); 1511da177e4SLinus Torvalds result = ERR_PTR(retval); 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds audit_getname(result); 1551da177e4SLinus Torvalds return result; 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 1591da177e4SLinus Torvalds void putname(const char *name) 1601da177e4SLinus Torvalds { 1615ac3a9c2SAl Viro if (unlikely(!audit_dummy_context())) 1621da177e4SLinus Torvalds audit_putname(name); 1631da177e4SLinus Torvalds else 1641da177e4SLinus Torvalds __putname(name); 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds EXPORT_SYMBOL(putname); 1671da177e4SLinus Torvalds #endif 1681da177e4SLinus Torvalds 1695909ccaaSLinus Torvalds /* 1705909ccaaSLinus Torvalds * This does basic POSIX ACL permission checking 1715909ccaaSLinus Torvalds */ 172b74c79e9SNick Piggin static int acl_permission_check(struct inode *inode, int mask, unsigned int flags, 173b74c79e9SNick Piggin int (*check_acl)(struct inode *inode, int mask, unsigned int flags)) 1745909ccaaSLinus Torvalds { 1755909ccaaSLinus Torvalds umode_t mode = inode->i_mode; 1765909ccaaSLinus Torvalds 1775909ccaaSLinus Torvalds mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 1785909ccaaSLinus Torvalds 1795909ccaaSLinus Torvalds if (current_fsuid() == inode->i_uid) 1805909ccaaSLinus Torvalds mode >>= 6; 1815909ccaaSLinus Torvalds else { 1825909ccaaSLinus Torvalds if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) { 183b74c79e9SNick Piggin int error = check_acl(inode, mask, flags); 1845909ccaaSLinus Torvalds if (error != -EAGAIN) 1855909ccaaSLinus Torvalds return error; 1865909ccaaSLinus Torvalds } 1875909ccaaSLinus Torvalds 1885909ccaaSLinus Torvalds if (in_group_p(inode->i_gid)) 1895909ccaaSLinus Torvalds mode >>= 3; 1905909ccaaSLinus Torvalds } 1915909ccaaSLinus Torvalds 1925909ccaaSLinus Torvalds /* 1935909ccaaSLinus Torvalds * If the DACs are ok we don't need any capability check. 1945909ccaaSLinus Torvalds */ 1955909ccaaSLinus Torvalds if ((mask & ~mode) == 0) 1965909ccaaSLinus Torvalds return 0; 1975909ccaaSLinus Torvalds return -EACCES; 1985909ccaaSLinus Torvalds } 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds /** 2011da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 2021da177e4SLinus Torvalds * @inode: inode to check access rights for 2031da177e4SLinus Torvalds * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 2041da177e4SLinus Torvalds * @check_acl: optional callback to check for Posix ACLs 20539191628SRandy Dunlap * @flags: IPERM_FLAG_ flags. 2061da177e4SLinus Torvalds * 2071da177e4SLinus Torvalds * Used to check for read/write/execute permissions on a file. 2081da177e4SLinus Torvalds * We use "fsuid" for this, letting us set arbitrary permissions 2091da177e4SLinus Torvalds * for filesystem access without changing the "normal" uids which 210b74c79e9SNick Piggin * are used for other things. 211b74c79e9SNick Piggin * 212b74c79e9SNick Piggin * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk 213b74c79e9SNick Piggin * request cannot be satisfied (eg. requires blocking or too much complexity). 214b74c79e9SNick Piggin * It would then be called again in ref-walk mode. 2151da177e4SLinus Torvalds */ 216b74c79e9SNick Piggin int generic_permission(struct inode *inode, int mask, unsigned int flags, 217b74c79e9SNick Piggin int (*check_acl)(struct inode *inode, int mask, unsigned int flags)) 2181da177e4SLinus Torvalds { 2195909ccaaSLinus Torvalds int ret; 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds /* 2225909ccaaSLinus Torvalds * Do the basic POSIX ACL permission checks. 2231da177e4SLinus Torvalds */ 224b74c79e9SNick Piggin ret = acl_permission_check(inode, mask, flags, check_acl); 2255909ccaaSLinus Torvalds if (ret != -EACCES) 2265909ccaaSLinus Torvalds return ret; 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds /* 2291da177e4SLinus Torvalds * Read/write DACs are always overridable. 2301da177e4SLinus Torvalds * Executable DACs are overridable if at least one exec bit is set. 2311da177e4SLinus Torvalds */ 232f696a365SMiklos Szeredi if (!(mask & MAY_EXEC) || execute_ok(inode)) 2331da177e4SLinus Torvalds if (capable(CAP_DAC_OVERRIDE)) 2341da177e4SLinus Torvalds return 0; 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds /* 2371da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 2381da177e4SLinus Torvalds */ 2397ea66001SSerge E. Hallyn mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 2401da177e4SLinus Torvalds if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE))) 2411da177e4SLinus Torvalds if (capable(CAP_DAC_READ_SEARCH)) 2421da177e4SLinus Torvalds return 0; 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds return -EACCES; 2451da177e4SLinus Torvalds } 2461da177e4SLinus Torvalds 247cb23beb5SChristoph Hellwig /** 248cb23beb5SChristoph Hellwig * inode_permission - check for access rights to a given inode 249cb23beb5SChristoph Hellwig * @inode: inode to check permission on 250cb23beb5SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 251cb23beb5SChristoph Hellwig * 252cb23beb5SChristoph Hellwig * Used to check for read/write/execute permissions on an inode. 253cb23beb5SChristoph Hellwig * We use "fsuid" for this, letting us set arbitrary permissions 254cb23beb5SChristoph Hellwig * for filesystem access without changing the "normal" uids which 255cb23beb5SChristoph Hellwig * are used for other things. 256cb23beb5SChristoph Hellwig */ 257f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask) 2581da177e4SLinus Torvalds { 259e6305c43SAl Viro int retval; 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds if (mask & MAY_WRITE) { 26222590e41SMiklos Szeredi umode_t mode = inode->i_mode; 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds /* 2651da177e4SLinus Torvalds * Nobody gets write access to a read-only fs. 2661da177e4SLinus Torvalds */ 2671da177e4SLinus Torvalds if (IS_RDONLY(inode) && 2681da177e4SLinus Torvalds (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 2691da177e4SLinus Torvalds return -EROFS; 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds /* 2721da177e4SLinus Torvalds * Nobody gets write access to an immutable file. 2731da177e4SLinus Torvalds */ 2741da177e4SLinus Torvalds if (IS_IMMUTABLE(inode)) 2751da177e4SLinus Torvalds return -EACCES; 2761da177e4SLinus Torvalds } 2771da177e4SLinus Torvalds 278acfa4380SAl Viro if (inode->i_op->permission) 279b74c79e9SNick Piggin retval = inode->i_op->permission(inode, mask, 0); 280f696a365SMiklos Szeredi else 281b74c79e9SNick Piggin retval = generic_permission(inode, mask, 0, 282b74c79e9SNick Piggin inode->i_op->check_acl); 283f696a365SMiklos Szeredi 2841da177e4SLinus Torvalds if (retval) 2851da177e4SLinus Torvalds return retval; 2861da177e4SLinus Torvalds 28708ce5f16SSerge E. Hallyn retval = devcgroup_inode_permission(inode, mask); 28808ce5f16SSerge E. Hallyn if (retval) 28908ce5f16SSerge E. Hallyn return retval; 29008ce5f16SSerge E. Hallyn 291d09ca739SEric Paris return security_inode_permission(inode, mask); 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds 294e4543eddSChristoph Hellwig /** 2958c744fb8SChristoph Hellwig * file_permission - check for additional access rights to a given file 2968c744fb8SChristoph Hellwig * @file: file to check access rights for 2978c744fb8SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 2988c744fb8SChristoph Hellwig * 2998c744fb8SChristoph Hellwig * Used to check for read/write/execute permissions on an already opened 3008c744fb8SChristoph Hellwig * file. 3018c744fb8SChristoph Hellwig * 3028c744fb8SChristoph Hellwig * Note: 3038c744fb8SChristoph Hellwig * Do not use this function in new code. All access checks should 304cb23beb5SChristoph Hellwig * be done using inode_permission(). 3058c744fb8SChristoph Hellwig */ 3068c744fb8SChristoph Hellwig int file_permission(struct file *file, int mask) 3078c744fb8SChristoph Hellwig { 308f419a2e3SAl Viro return inode_permission(file->f_path.dentry->d_inode, mask); 3098c744fb8SChristoph Hellwig } 3108c744fb8SChristoph Hellwig 3111da177e4SLinus Torvalds /* 3121da177e4SLinus Torvalds * get_write_access() gets write permission for a file. 3131da177e4SLinus Torvalds * put_write_access() releases this write permission. 3141da177e4SLinus Torvalds * This is used for regular files. 3151da177e4SLinus Torvalds * We cannot support write (and maybe mmap read-write shared) accesses and 3161da177e4SLinus Torvalds * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode 3171da177e4SLinus Torvalds * can have the following values: 3181da177e4SLinus Torvalds * 0: no writers, no VM_DENYWRITE mappings 3191da177e4SLinus Torvalds * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist 3201da177e4SLinus Torvalds * > 0: (i_writecount) users are writing to the file. 3211da177e4SLinus Torvalds * 3221da177e4SLinus Torvalds * Normally we operate on that counter with atomic_{inc,dec} and it's safe 3231da177e4SLinus Torvalds * except for the cases where we don't hold i_writecount yet. Then we need to 3241da177e4SLinus Torvalds * use {get,deny}_write_access() - these functions check the sign and refuse 3251da177e4SLinus Torvalds * to do the change if sign is wrong. Exclusion between them is provided by 3261da177e4SLinus Torvalds * the inode->i_lock spinlock. 3271da177e4SLinus Torvalds */ 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds int get_write_access(struct inode * inode) 3301da177e4SLinus Torvalds { 3311da177e4SLinus Torvalds spin_lock(&inode->i_lock); 3321da177e4SLinus Torvalds if (atomic_read(&inode->i_writecount) < 0) { 3331da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3341da177e4SLinus Torvalds return -ETXTBSY; 3351da177e4SLinus Torvalds } 3361da177e4SLinus Torvalds atomic_inc(&inode->i_writecount); 3371da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds return 0; 3401da177e4SLinus Torvalds } 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds int deny_write_access(struct file * file) 3431da177e4SLinus Torvalds { 3440f7fc9e4SJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds spin_lock(&inode->i_lock); 3471da177e4SLinus Torvalds if (atomic_read(&inode->i_writecount) > 0) { 3481da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3491da177e4SLinus Torvalds return -ETXTBSY; 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds atomic_dec(&inode->i_writecount); 3521da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds return 0; 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds 3571d957f9bSJan Blunck /** 3585dd784d0SJan Blunck * path_get - get a reference to a path 3595dd784d0SJan Blunck * @path: path to get the reference to 3605dd784d0SJan Blunck * 3615dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 3625dd784d0SJan Blunck */ 3635dd784d0SJan Blunck void path_get(struct path *path) 3645dd784d0SJan Blunck { 3655dd784d0SJan Blunck mntget(path->mnt); 3665dd784d0SJan Blunck dget(path->dentry); 3675dd784d0SJan Blunck } 3685dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 3695dd784d0SJan Blunck 3705dd784d0SJan Blunck /** 3711d957f9bSJan Blunck * path_put - put a reference to a path 3721d957f9bSJan Blunck * @path: path to put the reference to 3731d957f9bSJan Blunck * 3741d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 3751d957f9bSJan Blunck */ 3761d957f9bSJan Blunck void path_put(struct path *path) 3771da177e4SLinus Torvalds { 3781d957f9bSJan Blunck dput(path->dentry); 3791d957f9bSJan Blunck mntput(path->mnt); 3801da177e4SLinus Torvalds } 3811d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 3821da177e4SLinus Torvalds 383834f2a4aSTrond Myklebust /** 38431e6b01fSNick Piggin * nameidata_drop_rcu - drop this nameidata out of rcu-walk 38531e6b01fSNick Piggin * @nd: nameidata pathwalk data to drop 38639191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 38731e6b01fSNick Piggin * 38831e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 38931e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). __drop_rcu* functions attempt 39031e6b01fSNick Piggin * to drop out of rcu-walk mode and take normal reference counts on dentries 39131e6b01fSNick Piggin * and vfsmounts to transition to rcu-walk mode. __drop_rcu* functions take 39231e6b01fSNick Piggin * refcounts at the last known good point before rcu-walk got stuck, so 39331e6b01fSNick Piggin * ref-walk may continue from there. If this is not successful (eg. a seqcount 39431e6b01fSNick Piggin * has changed), then failure is returned and path walk restarts from the 39531e6b01fSNick Piggin * beginning in ref-walk mode. 39631e6b01fSNick Piggin * 39731e6b01fSNick Piggin * nameidata_drop_rcu attempts to drop the current nd->path and nd->root into 39831e6b01fSNick Piggin * ref-walk. Must be called from rcu-walk context. 39931e6b01fSNick Piggin */ 40031e6b01fSNick Piggin static int nameidata_drop_rcu(struct nameidata *nd) 40131e6b01fSNick Piggin { 40231e6b01fSNick Piggin struct fs_struct *fs = current->fs; 40331e6b01fSNick Piggin struct dentry *dentry = nd->path.dentry; 40431e6b01fSNick Piggin 40531e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 40631e6b01fSNick Piggin if (nd->root.mnt) { 40731e6b01fSNick Piggin spin_lock(&fs->lock); 40831e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 40931e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 41031e6b01fSNick Piggin goto err_root; 41131e6b01fSNick Piggin } 41231e6b01fSNick Piggin spin_lock(&dentry->d_lock); 41331e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 41431e6b01fSNick Piggin goto err; 41531e6b01fSNick Piggin BUG_ON(nd->inode != dentry->d_inode); 41631e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 41731e6b01fSNick Piggin if (nd->root.mnt) { 41831e6b01fSNick Piggin path_get(&nd->root); 41931e6b01fSNick Piggin spin_unlock(&fs->lock); 42031e6b01fSNick Piggin } 42131e6b01fSNick Piggin mntget(nd->path.mnt); 42231e6b01fSNick Piggin 42331e6b01fSNick Piggin rcu_read_unlock(); 42431e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 42531e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 42631e6b01fSNick Piggin return 0; 42731e6b01fSNick Piggin err: 42831e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 42931e6b01fSNick Piggin err_root: 43031e6b01fSNick Piggin if (nd->root.mnt) 43131e6b01fSNick Piggin spin_unlock(&fs->lock); 43231e6b01fSNick Piggin return -ECHILD; 43331e6b01fSNick Piggin } 43431e6b01fSNick Piggin 43531e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */ 43631e6b01fSNick Piggin static inline int nameidata_drop_rcu_maybe(struct nameidata *nd) 43731e6b01fSNick Piggin { 43831e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) 43931e6b01fSNick Piggin return nameidata_drop_rcu(nd); 44031e6b01fSNick Piggin return 0; 44131e6b01fSNick Piggin } 44231e6b01fSNick Piggin 44331e6b01fSNick Piggin /** 44431e6b01fSNick Piggin * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk 44531e6b01fSNick Piggin * @nd: nameidata pathwalk data to drop 44631e6b01fSNick Piggin * @dentry: dentry to drop 44739191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 44831e6b01fSNick Piggin * 44931e6b01fSNick Piggin * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root, 45031e6b01fSNick Piggin * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on 45131e6b01fSNick Piggin * @nd. Must be called from rcu-walk context. 45231e6b01fSNick Piggin */ 45331e6b01fSNick Piggin static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry) 45431e6b01fSNick Piggin { 45531e6b01fSNick Piggin struct fs_struct *fs = current->fs; 45631e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 45731e6b01fSNick Piggin 45831e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 45931e6b01fSNick Piggin if (nd->root.mnt) { 46031e6b01fSNick Piggin spin_lock(&fs->lock); 46131e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 46231e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 46331e6b01fSNick Piggin goto err_root; 46431e6b01fSNick Piggin } 46531e6b01fSNick Piggin spin_lock(&parent->d_lock); 46631e6b01fSNick Piggin spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); 46731e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 46831e6b01fSNick Piggin goto err; 46931e6b01fSNick Piggin /* 47031e6b01fSNick Piggin * If the sequence check on the child dentry passed, then the child has 47131e6b01fSNick Piggin * not been removed from its parent. This means the parent dentry must 47231e6b01fSNick Piggin * be valid and able to take a reference at this point. 47331e6b01fSNick Piggin */ 47431e6b01fSNick Piggin BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); 47531e6b01fSNick Piggin BUG_ON(!parent->d_count); 47631e6b01fSNick Piggin parent->d_count++; 47731e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 47831e6b01fSNick Piggin spin_unlock(&parent->d_lock); 47931e6b01fSNick Piggin if (nd->root.mnt) { 48031e6b01fSNick Piggin path_get(&nd->root); 48131e6b01fSNick Piggin spin_unlock(&fs->lock); 48231e6b01fSNick Piggin } 48331e6b01fSNick Piggin mntget(nd->path.mnt); 48431e6b01fSNick Piggin 48531e6b01fSNick Piggin rcu_read_unlock(); 48631e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 48731e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 48831e6b01fSNick Piggin return 0; 48931e6b01fSNick Piggin err: 49031e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 49131e6b01fSNick Piggin spin_unlock(&parent->d_lock); 49231e6b01fSNick Piggin err_root: 49331e6b01fSNick Piggin if (nd->root.mnt) 49431e6b01fSNick Piggin spin_unlock(&fs->lock); 49531e6b01fSNick Piggin return -ECHILD; 49631e6b01fSNick Piggin } 49731e6b01fSNick Piggin 49831e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */ 49931e6b01fSNick Piggin static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry) 50031e6b01fSNick Piggin { 501a7472babSAl Viro if (nd->flags & LOOKUP_RCU) { 502a7472babSAl Viro if (unlikely(nameidata_dentry_drop_rcu(nd, dentry))) { 503a7472babSAl Viro nd->flags &= ~LOOKUP_RCU; 504a7472babSAl Viro nd->root.mnt = NULL; 505a7472babSAl Viro rcu_read_unlock(); 506a7472babSAl Viro br_read_unlock(vfsmount_lock); 507a7472babSAl Viro return -ECHILD; 508a7472babSAl Viro } 509a7472babSAl Viro } 51031e6b01fSNick Piggin return 0; 51131e6b01fSNick Piggin } 51231e6b01fSNick Piggin 51331e6b01fSNick Piggin /** 51431e6b01fSNick Piggin * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk 51531e6b01fSNick Piggin * @nd: nameidata pathwalk data to drop 51639191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 51731e6b01fSNick Piggin * 51831e6b01fSNick Piggin * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk. 51931e6b01fSNick Piggin * nd->path should be the final element of the lookup, so nd->root is discarded. 52031e6b01fSNick Piggin * Must be called from rcu-walk context. 52131e6b01fSNick Piggin */ 52231e6b01fSNick Piggin static int nameidata_drop_rcu_last(struct nameidata *nd) 52331e6b01fSNick Piggin { 52431e6b01fSNick Piggin struct dentry *dentry = nd->path.dentry; 52531e6b01fSNick Piggin 52631e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 52731e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 52831e6b01fSNick Piggin nd->root.mnt = NULL; 52931e6b01fSNick Piggin spin_lock(&dentry->d_lock); 53031e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 53131e6b01fSNick Piggin goto err_unlock; 53231e6b01fSNick Piggin BUG_ON(nd->inode != dentry->d_inode); 53331e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 53431e6b01fSNick Piggin 53531e6b01fSNick Piggin mntget(nd->path.mnt); 53631e6b01fSNick Piggin 53731e6b01fSNick Piggin rcu_read_unlock(); 53831e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 53931e6b01fSNick Piggin 54031e6b01fSNick Piggin return 0; 54131e6b01fSNick Piggin 54231e6b01fSNick Piggin err_unlock: 54331e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 54431e6b01fSNick Piggin rcu_read_unlock(); 54531e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 54631e6b01fSNick Piggin return -ECHILD; 54731e6b01fSNick Piggin } 54831e6b01fSNick Piggin 54931e6b01fSNick Piggin /** 550834f2a4aSTrond Myklebust * release_open_intent - free up open intent resources 551834f2a4aSTrond Myklebust * @nd: pointer to nameidata 552834f2a4aSTrond Myklebust */ 553834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd) 554834f2a4aSTrond Myklebust { 5552dab5974SLinus Torvalds struct file *file = nd->intent.open.file; 5562dab5974SLinus Torvalds 5572dab5974SLinus Torvalds if (file && !IS_ERR(file)) { 5582dab5974SLinus Torvalds if (file->f_path.dentry == NULL) 5592dab5974SLinus Torvalds put_filp(file); 560834f2a4aSTrond Myklebust else 5612dab5974SLinus Torvalds fput(file); 5622dab5974SLinus Torvalds } 563834f2a4aSTrond Myklebust } 564834f2a4aSTrond Myklebust 565f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd) 56634286d66SNick Piggin { 567f60aef7eSAl Viro return dentry->d_op->d_revalidate(dentry, nd); 56834286d66SNick Piggin } 56934286d66SNick Piggin 570f5e1c1c1SAl Viro static struct dentry * 571bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd) 572bcdc5e01SIan Kent { 573f5e1c1c1SAl Viro int status = d_revalidate(dentry, nd); 574bcdc5e01SIan Kent if (unlikely(status <= 0)) { 575bcdc5e01SIan Kent /* 576bcdc5e01SIan Kent * The dentry failed validation. 577bcdc5e01SIan Kent * If d_revalidate returned 0 attempt to invalidate 578bcdc5e01SIan Kent * the dentry otherwise d_revalidate is asking us 579bcdc5e01SIan Kent * to return a fail status. 580bcdc5e01SIan Kent */ 58134286d66SNick Piggin if (status < 0) { 58234286d66SNick Piggin dput(dentry); 58334286d66SNick Piggin dentry = ERR_PTR(status); 584f5e1c1c1SAl Viro } else if (!d_invalidate(dentry)) { 585bcdc5e01SIan Kent dput(dentry); 586bcdc5e01SIan Kent dentry = NULL; 587bcdc5e01SIan Kent } 588bcdc5e01SIan Kent } 589f5e1c1c1SAl Viro return dentry; 590f5e1c1c1SAl Viro } 591f5e1c1c1SAl Viro 592f5e1c1c1SAl Viro static inline struct dentry * 593f5e1c1c1SAl Viro do_revalidate_rcu(struct dentry *dentry, struct nameidata *nd) 594f5e1c1c1SAl Viro { 595f60aef7eSAl Viro int status = d_revalidate(dentry, nd); 596f5e1c1c1SAl Viro if (likely(status > 0)) 597f5e1c1c1SAl Viro return dentry; 598f5e1c1c1SAl Viro if (status == -ECHILD) { 599f5e1c1c1SAl Viro if (nameidata_dentry_drop_rcu(nd, dentry)) 600f5e1c1c1SAl Viro return ERR_PTR(-ECHILD); 601f5e1c1c1SAl Viro return do_revalidate(dentry, nd); 602f5e1c1c1SAl Viro } 603f5e1c1c1SAl Viro if (status < 0) 604f5e1c1c1SAl Viro return ERR_PTR(status); 605f5e1c1c1SAl Viro /* Don't d_invalidate in rcu-walk mode */ 606f5e1c1c1SAl Viro if (nameidata_dentry_drop_rcu(nd, dentry)) 607f5e1c1c1SAl Viro return ERR_PTR(-ECHILD); 608f5e1c1c1SAl Viro if (!d_invalidate(dentry)) { 609f5e1c1c1SAl Viro dput(dentry); 610f5e1c1c1SAl Viro dentry = NULL; 611bcdc5e01SIan Kent } 612bcdc5e01SIan Kent return dentry; 613bcdc5e01SIan Kent } 614bcdc5e01SIan Kent 6151da177e4SLinus Torvalds /* 61616c2cd71SAl Viro * handle_reval_path - force revalidation of a dentry 61739159de2SJeff Layton * 61839159de2SJeff Layton * In some situations the path walking code will trust dentries without 61939159de2SJeff Layton * revalidating them. This causes problems for filesystems that depend on 62039159de2SJeff Layton * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set 62139159de2SJeff Layton * (which indicates that it's possible for the dentry to go stale), force 62239159de2SJeff Layton * a d_revalidate call before proceeding. 62339159de2SJeff Layton * 62439159de2SJeff Layton * Returns 0 if the revalidation was successful. If the revalidation fails, 62539159de2SJeff Layton * either return the error returned by d_revalidate or -ESTALE if the 62639159de2SJeff Layton * revalidation it just returned 0. If d_revalidate returns 0, we attempt to 62739159de2SJeff Layton * invalidate the dentry. It's up to the caller to handle putting references 62839159de2SJeff Layton * to the path if necessary. 62939159de2SJeff Layton */ 63016c2cd71SAl Viro static inline int handle_reval_path(struct nameidata *nd) 63139159de2SJeff Layton { 63216c2cd71SAl Viro struct dentry *dentry = nd->path.dentry; 63339159de2SJeff Layton int status; 63439159de2SJeff Layton 63516c2cd71SAl Viro if (likely(!(nd->flags & LOOKUP_JUMPED))) 63639159de2SJeff Layton return 0; 63739159de2SJeff Layton 63816c2cd71SAl Viro if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE))) 63916c2cd71SAl Viro return 0; 64016c2cd71SAl Viro 64116c2cd71SAl Viro if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))) 64216c2cd71SAl Viro return 0; 64316c2cd71SAl Viro 64416c2cd71SAl Viro /* Note: we do not d_invalidate() */ 64534286d66SNick Piggin status = d_revalidate(dentry, nd); 64639159de2SJeff Layton if (status > 0) 64739159de2SJeff Layton return 0; 64839159de2SJeff Layton 64916c2cd71SAl Viro if (!status) 65039159de2SJeff Layton status = -ESTALE; 65116c2cd71SAl Viro 65239159de2SJeff Layton return status; 65339159de2SJeff Layton } 65439159de2SJeff Layton 65539159de2SJeff Layton /* 656b75b5086SAl Viro * Short-cut version of permission(), for calling on directories 657b75b5086SAl Viro * during pathname resolution. Combines parts of permission() 658b75b5086SAl Viro * and generic_permission(), and tests ONLY for MAY_EXEC permission. 6591da177e4SLinus Torvalds * 6601da177e4SLinus Torvalds * If appropriate, check DAC only. If not appropriate, or 661b75b5086SAl Viro * short-cut DAC fails, then call ->permission() to do more 6621da177e4SLinus Torvalds * complete permission check. 6631da177e4SLinus Torvalds */ 664b74c79e9SNick Piggin static inline int exec_permission(struct inode *inode, unsigned int flags) 6651da177e4SLinus Torvalds { 6665909ccaaSLinus Torvalds int ret; 6671da177e4SLinus Torvalds 668cb9179eaSLinus Torvalds if (inode->i_op->permission) { 669b74c79e9SNick Piggin ret = inode->i_op->permission(inode, MAY_EXEC, flags); 670b74c79e9SNick Piggin } else { 671b74c79e9SNick Piggin ret = acl_permission_check(inode, MAY_EXEC, flags, 672b74c79e9SNick Piggin inode->i_op->check_acl); 673cb9179eaSLinus Torvalds } 674b74c79e9SNick Piggin if (likely(!ret)) 6751da177e4SLinus Torvalds goto ok; 676b74c79e9SNick Piggin if (ret == -ECHILD) 67731e6b01fSNick Piggin return ret; 6781da177e4SLinus Torvalds 679f1ac9f6bSLinus Torvalds if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH)) 6801da177e4SLinus Torvalds goto ok; 6811da177e4SLinus Torvalds 6825909ccaaSLinus Torvalds return ret; 6831da177e4SLinus Torvalds ok: 684b74c79e9SNick Piggin return security_inode_exec_permission(inode, flags); 6851da177e4SLinus Torvalds } 6861da177e4SLinus Torvalds 6872a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 6882a737871SAl Viro { 689f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 690f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 6912a737871SAl Viro } 6922a737871SAl Viro 6936de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 6946de88d72SAl Viro 69531e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 69631e6b01fSNick Piggin { 69731e6b01fSNick Piggin if (!nd->root.mnt) { 69831e6b01fSNick Piggin struct fs_struct *fs = current->fs; 699c28cc364SNick Piggin unsigned seq; 700c28cc364SNick Piggin 701c28cc364SNick Piggin do { 702c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 70331e6b01fSNick Piggin nd->root = fs->root; 704c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 70531e6b01fSNick Piggin } 70631e6b01fSNick Piggin } 70731e6b01fSNick Piggin 708f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 7091da177e4SLinus Torvalds { 71031e6b01fSNick Piggin int ret; 71131e6b01fSNick Piggin 7121da177e4SLinus Torvalds if (IS_ERR(link)) 7131da177e4SLinus Torvalds goto fail; 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds if (*link == '/') { 7162a737871SAl Viro set_root(nd); 7171d957f9bSJan Blunck path_put(&nd->path); 7182a737871SAl Viro nd->path = nd->root; 7192a737871SAl Viro path_get(&nd->root); 72016c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 7211da177e4SLinus Torvalds } 72231e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 723b4091d5fSChristoph Hellwig 72431e6b01fSNick Piggin ret = link_path_walk(link, nd); 72531e6b01fSNick Piggin return ret; 7261da177e4SLinus Torvalds fail: 7271d957f9bSJan Blunck path_put(&nd->path); 7281da177e4SLinus Torvalds return PTR_ERR(link); 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds 7311d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 732051d3812SIan Kent { 733051d3812SIan Kent dput(path->dentry); 7344ac91378SJan Blunck if (path->mnt != nd->path.mnt) 735051d3812SIan Kent mntput(path->mnt); 736051d3812SIan Kent } 737051d3812SIan Kent 7387b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path, 7397b9337aaSNick Piggin struct nameidata *nd) 740051d3812SIan Kent { 74131e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 7424ac91378SJan Blunck dput(nd->path.dentry); 74331e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 7444ac91378SJan Blunck mntput(nd->path.mnt); 7459a229683SHuang Shijie } 74631e6b01fSNick Piggin nd->path.mnt = path->mnt; 7474ac91378SJan Blunck nd->path.dentry = path->dentry; 748051d3812SIan Kent } 749051d3812SIan Kent 750def4af30SAl Viro static __always_inline int 7517b9337aaSNick Piggin __do_follow_link(const struct path *link, struct nameidata *nd, void **p) 7521da177e4SLinus Torvalds { 7531da177e4SLinus Torvalds int error; 7547b9337aaSNick Piggin struct dentry *dentry = link->dentry; 7551da177e4SLinus Torvalds 756844a3917SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 757844a3917SAl Viro 7587b9337aaSNick Piggin touch_atime(link->mnt, dentry); 7591da177e4SLinus Torvalds nd_set_link(nd, NULL); 760cd4e91d3SAl Viro 76187556ef1SDavid Howells if (link->mnt == nd->path.mnt) 7627b9337aaSNick Piggin mntget(link->mnt); 76331e6b01fSNick Piggin 76436f3b4f6SAl Viro error = security_inode_follow_link(link->dentry, nd); 76536f3b4f6SAl Viro if (error) { 76636f3b4f6SAl Viro *p = ERR_PTR(error); /* no ->put_link(), please */ 76736f3b4f6SAl Viro path_put(&nd->path); 76836f3b4f6SAl Viro return error; 76936f3b4f6SAl Viro } 77036f3b4f6SAl Viro 77186acdca1SAl Viro nd->last_type = LAST_BIND; 772def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 773def4af30SAl Viro error = PTR_ERR(*p); 774def4af30SAl Viro if (!IS_ERR(*p)) { 7751da177e4SLinus Torvalds char *s = nd_get_link(nd); 776cc314eefSLinus Torvalds error = 0; 7771da177e4SLinus Torvalds if (s) 7781da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 77916c2cd71SAl Viro else if (nd->last_type == LAST_BIND) 78016c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 7811da177e4SLinus Torvalds } 7821da177e4SLinus Torvalds return error; 7831da177e4SLinus Torvalds } 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds /* 7861da177e4SLinus Torvalds * This limits recursive symlink follows to 8, while 7871da177e4SLinus Torvalds * limiting consecutive symlinks to 40. 7881da177e4SLinus Torvalds * 7891da177e4SLinus Torvalds * Without that kind of total limit, nasty chains of consecutive 7901da177e4SLinus Torvalds * symlinks can cause almost arbitrarily long lookups. 7911da177e4SLinus Torvalds */ 7923abb17e8SLinus Torvalds static inline int do_follow_link(struct inode *inode, struct path *path, struct nameidata *nd) 7931da177e4SLinus Torvalds { 794def4af30SAl Viro void *cookie; 7951da177e4SLinus Torvalds int err = -ELOOP; 796844a3917SAl Viro 797844a3917SAl Viro /* We drop rcu-walk here */ 798844a3917SAl Viro if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry)) 799844a3917SAl Viro return -ECHILD; 8003abb17e8SLinus Torvalds BUG_ON(inode != path->dentry->d_inode); 801844a3917SAl Viro 8021da177e4SLinus Torvalds if (current->link_count >= MAX_NESTED_LINKS) 8031da177e4SLinus Torvalds goto loop; 8041da177e4SLinus Torvalds if (current->total_link_count >= 40) 8051da177e4SLinus Torvalds goto loop; 8061da177e4SLinus Torvalds BUG_ON(nd->depth >= MAX_NESTED_LINKS); 8071da177e4SLinus Torvalds cond_resched(); 8081da177e4SLinus Torvalds current->link_count++; 8091da177e4SLinus Torvalds current->total_link_count++; 8101da177e4SLinus Torvalds nd->depth++; 811def4af30SAl Viro err = __do_follow_link(path, nd, &cookie); 812def4af30SAl Viro if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link) 813def4af30SAl Viro path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie); 814258fa999SAl Viro path_put(path); 8151da177e4SLinus Torvalds current->link_count--; 8161da177e4SLinus Torvalds nd->depth--; 8171da177e4SLinus Torvalds return err; 8181da177e4SLinus Torvalds loop: 8191d957f9bSJan Blunck path_put_conditional(path, nd); 8201d957f9bSJan Blunck path_put(&nd->path); 8211da177e4SLinus Torvalds return err; 8221da177e4SLinus Torvalds } 8231da177e4SLinus Torvalds 82431e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 82531e6b01fSNick Piggin { 82631e6b01fSNick Piggin struct vfsmount *parent; 82731e6b01fSNick Piggin struct dentry *mountpoint; 82831e6b01fSNick Piggin 82931e6b01fSNick Piggin parent = path->mnt->mnt_parent; 83031e6b01fSNick Piggin if (parent == path->mnt) 83131e6b01fSNick Piggin return 0; 83231e6b01fSNick Piggin mountpoint = path->mnt->mnt_mountpoint; 83331e6b01fSNick Piggin path->dentry = mountpoint; 83431e6b01fSNick Piggin path->mnt = parent; 83531e6b01fSNick Piggin return 1; 83631e6b01fSNick Piggin } 83731e6b01fSNick Piggin 838bab77ebfSAl Viro int follow_up(struct path *path) 8391da177e4SLinus Torvalds { 8401da177e4SLinus Torvalds struct vfsmount *parent; 8411da177e4SLinus Torvalds struct dentry *mountpoint; 84299b7db7bSNick Piggin 84399b7db7bSNick Piggin br_read_lock(vfsmount_lock); 844bab77ebfSAl Viro parent = path->mnt->mnt_parent; 845bab77ebfSAl Viro if (parent == path->mnt) { 84699b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 8471da177e4SLinus Torvalds return 0; 8481da177e4SLinus Torvalds } 8491da177e4SLinus Torvalds mntget(parent); 850bab77ebfSAl Viro mountpoint = dget(path->mnt->mnt_mountpoint); 85199b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 852bab77ebfSAl Viro dput(path->dentry); 853bab77ebfSAl Viro path->dentry = mountpoint; 854bab77ebfSAl Viro mntput(path->mnt); 855bab77ebfSAl Viro path->mnt = parent; 8561da177e4SLinus Torvalds return 1; 8571da177e4SLinus Torvalds } 8581da177e4SLinus Torvalds 859b5c84bf6SNick Piggin /* 8609875cf80SDavid Howells * Perform an automount 8619875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 8629875cf80SDavid Howells * were called with. 8631da177e4SLinus Torvalds */ 8649875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags, 8659875cf80SDavid Howells bool *need_mntput) 86631e6b01fSNick Piggin { 8679875cf80SDavid Howells struct vfsmount *mnt; 868ea5b778aSDavid Howells int err; 8699875cf80SDavid Howells 8709875cf80SDavid Howells if (!path->dentry->d_op || !path->dentry->d_op->d_automount) 8719875cf80SDavid Howells return -EREMOTE; 8729875cf80SDavid Howells 8736f45b656SDavid Howells /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT 8746f45b656SDavid Howells * and this is the terminal part of the path. 8756f45b656SDavid Howells */ 8766f45b656SDavid Howells if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE)) 8776f45b656SDavid Howells return -EISDIR; /* we actually want to stop here */ 8786f45b656SDavid Howells 8799875cf80SDavid Howells /* We want to mount if someone is trying to open/create a file of any 8809875cf80SDavid Howells * type under the mountpoint, wants to traverse through the mountpoint 8819875cf80SDavid Howells * or wants to open the mounted directory. 8829875cf80SDavid Howells * 8839875cf80SDavid Howells * We don't want to mount if someone's just doing a stat and they've 8849875cf80SDavid Howells * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and 8859875cf80SDavid Howells * appended a '/' to the name. 8869875cf80SDavid Howells */ 8879875cf80SDavid Howells if (!(flags & LOOKUP_FOLLOW) && 8889875cf80SDavid Howells !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY | 8899875cf80SDavid Howells LOOKUP_OPEN | LOOKUP_CREATE))) 8909875cf80SDavid Howells return -EISDIR; 8919875cf80SDavid Howells 8929875cf80SDavid Howells current->total_link_count++; 8939875cf80SDavid Howells if (current->total_link_count >= 40) 8949875cf80SDavid Howells return -ELOOP; 8959875cf80SDavid Howells 8969875cf80SDavid Howells mnt = path->dentry->d_op->d_automount(path); 8979875cf80SDavid Howells if (IS_ERR(mnt)) { 8989875cf80SDavid Howells /* 8999875cf80SDavid Howells * The filesystem is allowed to return -EISDIR here to indicate 9009875cf80SDavid Howells * it doesn't want to automount. For instance, autofs would do 9019875cf80SDavid Howells * this so that its userspace daemon can mount on this dentry. 9029875cf80SDavid Howells * 9039875cf80SDavid Howells * However, we can only permit this if it's a terminal point in 9049875cf80SDavid Howells * the path being looked up; if it wasn't then the remainder of 9059875cf80SDavid Howells * the path is inaccessible and we should say so. 9069875cf80SDavid Howells */ 9079875cf80SDavid Howells if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE)) 9089875cf80SDavid Howells return -EREMOTE; 9099875cf80SDavid Howells return PTR_ERR(mnt); 91031e6b01fSNick Piggin } 911ea5b778aSDavid Howells 9129875cf80SDavid Howells if (!mnt) /* mount collision */ 9139875cf80SDavid Howells return 0; 9149875cf80SDavid Howells 91519a167afSAl Viro err = finish_automount(mnt, path); 916ea5b778aSDavid Howells 917ea5b778aSDavid Howells switch (err) { 918ea5b778aSDavid Howells case -EBUSY: 919ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 92019a167afSAl Viro return 0; 921ea5b778aSDavid Howells case 0: 922463ffb2eSAl Viro dput(path->dentry); 9239875cf80SDavid Howells if (*need_mntput) 9249875cf80SDavid Howells mntput(path->mnt); 9259875cf80SDavid Howells path->mnt = mnt; 9269875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 9279875cf80SDavid Howells *need_mntput = true; 9289875cf80SDavid Howells return 0; 92919a167afSAl Viro default: 93019a167afSAl Viro return err; 9319875cf80SDavid Howells } 93219a167afSAl Viro 933ea5b778aSDavid Howells } 9349875cf80SDavid Howells 9359875cf80SDavid Howells /* 9369875cf80SDavid Howells * Handle a dentry that is managed in some way. 937cc53ce53SDavid Howells * - Flagged for transit management (autofs) 9389875cf80SDavid Howells * - Flagged as mountpoint 9399875cf80SDavid Howells * - Flagged as automount point 9409875cf80SDavid Howells * 9419875cf80SDavid Howells * This may only be called in refwalk mode. 9429875cf80SDavid Howells * 9439875cf80SDavid Howells * Serialization is taken care of in namespace.c 9449875cf80SDavid Howells */ 9459875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 9469875cf80SDavid Howells { 9479875cf80SDavid Howells unsigned managed; 9489875cf80SDavid Howells bool need_mntput = false; 9499875cf80SDavid Howells int ret; 9509875cf80SDavid Howells 9519875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 9529875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 9539875cf80SDavid Howells * the components of that value change under us */ 9549875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 9559875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 9569875cf80SDavid Howells unlikely(managed != 0)) { 957cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 958cc53ce53SDavid Howells * being held. */ 959cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 960cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 961cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 962ab90911fSDavid Howells ret = path->dentry->d_op->d_manage(path->dentry, 963ab90911fSDavid Howells false, false); 964cc53ce53SDavid Howells if (ret < 0) 965cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 966cc53ce53SDavid Howells } 967cc53ce53SDavid Howells 9689875cf80SDavid Howells /* Transit to a mounted filesystem. */ 9699875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 9709875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 9719875cf80SDavid Howells if (mounted) { 9729875cf80SDavid Howells dput(path->dentry); 9739875cf80SDavid Howells if (need_mntput) 974463ffb2eSAl Viro mntput(path->mnt); 975463ffb2eSAl Viro path->mnt = mounted; 976463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 9779875cf80SDavid Howells need_mntput = true; 9789875cf80SDavid Howells continue; 979463ffb2eSAl Viro } 980463ffb2eSAl Viro 9819875cf80SDavid Howells /* Something is mounted on this dentry in another 9829875cf80SDavid Howells * namespace and/or whatever was mounted there in this 9839875cf80SDavid Howells * namespace got unmounted before we managed to get the 9849875cf80SDavid Howells * vfsmount_lock */ 9851da177e4SLinus Torvalds } 9869875cf80SDavid Howells 9879875cf80SDavid Howells /* Handle an automount point */ 9889875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 9899875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 9909875cf80SDavid Howells if (ret < 0) 9919875cf80SDavid Howells return ret == -EISDIR ? 0 : ret; 9929875cf80SDavid Howells continue; 9939875cf80SDavid Howells } 9949875cf80SDavid Howells 9959875cf80SDavid Howells /* We didn't change the current path point */ 9969875cf80SDavid Howells break; 9979875cf80SDavid Howells } 9989875cf80SDavid Howells return 0; 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds 1001cc53ce53SDavid Howells int follow_down_one(struct path *path) 10021da177e4SLinus Torvalds { 10031da177e4SLinus Torvalds struct vfsmount *mounted; 10041da177e4SLinus Torvalds 10051c755af4SAl Viro mounted = lookup_mnt(path); 10061da177e4SLinus Torvalds if (mounted) { 10079393bd07SAl Viro dput(path->dentry); 10089393bd07SAl Viro mntput(path->mnt); 10099393bd07SAl Viro path->mnt = mounted; 10109393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 10111da177e4SLinus Torvalds return 1; 10121da177e4SLinus Torvalds } 10131da177e4SLinus Torvalds return 0; 10141da177e4SLinus Torvalds } 10151da177e4SLinus Torvalds 10169875cf80SDavid Howells /* 10179875cf80SDavid Howells * Skip to top of mountpoint pile in rcuwalk mode. We abort the rcu-walk if we 1018cc53ce53SDavid Howells * meet a managed dentry and we're not walking to "..". True is returned to 10199875cf80SDavid Howells * continue, false to abort. 10209875cf80SDavid Howells */ 10219875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 10229875cf80SDavid Howells struct inode **inode, bool reverse_transit) 10239875cf80SDavid Howells { 10249875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 10259875cf80SDavid Howells struct vfsmount *mounted; 1026ab90911fSDavid Howells if (unlikely(path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) && 1027ab90911fSDavid Howells !reverse_transit && 1028ab90911fSDavid Howells path->dentry->d_op->d_manage(path->dentry, false, true) < 0) 1029ab90911fSDavid Howells return false; 10309875cf80SDavid Howells mounted = __lookup_mnt(path->mnt, path->dentry, 1); 10319875cf80SDavid Howells if (!mounted) 10329875cf80SDavid Howells break; 10339875cf80SDavid Howells path->mnt = mounted; 10349875cf80SDavid Howells path->dentry = mounted->mnt_root; 10359875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 10369875cf80SDavid Howells *inode = path->dentry->d_inode; 10379875cf80SDavid Howells } 10389875cf80SDavid Howells 10399875cf80SDavid Howells if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 10409875cf80SDavid Howells return reverse_transit; 10419875cf80SDavid Howells return true; 10429875cf80SDavid Howells } 10439875cf80SDavid Howells 104431e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 104531e6b01fSNick Piggin { 104631e6b01fSNick Piggin struct inode *inode = nd->inode; 104731e6b01fSNick Piggin 104831e6b01fSNick Piggin set_root_rcu(nd); 104931e6b01fSNick Piggin 105031e6b01fSNick Piggin while (1) { 105131e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 105231e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 105331e6b01fSNick Piggin break; 105431e6b01fSNick Piggin } 105531e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 105631e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 105731e6b01fSNick Piggin struct dentry *parent = old->d_parent; 105831e6b01fSNick Piggin unsigned seq; 105931e6b01fSNick Piggin 106031e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 106131e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 1062ef7562d5SAl Viro goto failed; 106331e6b01fSNick Piggin inode = parent->d_inode; 106431e6b01fSNick Piggin nd->path.dentry = parent; 106531e6b01fSNick Piggin nd->seq = seq; 106631e6b01fSNick Piggin break; 106731e6b01fSNick Piggin } 106831e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 106931e6b01fSNick Piggin break; 107031e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 107131e6b01fSNick Piggin inode = nd->path.dentry->d_inode; 107231e6b01fSNick Piggin } 10739875cf80SDavid Howells __follow_mount_rcu(nd, &nd->path, &inode, true); 107431e6b01fSNick Piggin nd->inode = inode; 107531e6b01fSNick Piggin return 0; 1076ef7562d5SAl Viro 1077ef7562d5SAl Viro failed: 1078ef7562d5SAl Viro nd->flags &= ~LOOKUP_RCU; 1079ef7562d5SAl Viro nd->root.mnt = NULL; 1080ef7562d5SAl Viro rcu_read_unlock(); 1081ef7562d5SAl Viro br_read_unlock(vfsmount_lock); 1082ef7562d5SAl Viro return -ECHILD; 108331e6b01fSNick Piggin } 108431e6b01fSNick Piggin 10859875cf80SDavid Howells /* 1086cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 1087cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 1088cc53ce53SDavid Howells * caller is permitted to proceed or not. 1089cc53ce53SDavid Howells * 1090cc53ce53SDavid Howells * Care must be taken as namespace_sem may be held (indicated by mounting_here 1091cc53ce53SDavid Howells * being true). 1092cc53ce53SDavid Howells */ 1093cc53ce53SDavid Howells int follow_down(struct path *path, bool mounting_here) 1094cc53ce53SDavid Howells { 1095cc53ce53SDavid Howells unsigned managed; 1096cc53ce53SDavid Howells int ret; 1097cc53ce53SDavid Howells 1098cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 1099cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 1100cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 1101cc53ce53SDavid Howells * being held. 1102cc53ce53SDavid Howells * 1103cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 1104cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 1105cc53ce53SDavid Howells * other than its daemon the right to mount on its 1106cc53ce53SDavid Howells * superstructure. 1107cc53ce53SDavid Howells * 1108cc53ce53SDavid Howells * The filesystem may sleep at this point. 1109cc53ce53SDavid Howells */ 1110cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 1111cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 1112cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 1113ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 1114ab90911fSDavid Howells path->dentry, mounting_here, false); 1115cc53ce53SDavid Howells if (ret < 0) 1116cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 1117cc53ce53SDavid Howells } 1118cc53ce53SDavid Howells 1119cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 1120cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 1121cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 1122cc53ce53SDavid Howells if (!mounted) 1123cc53ce53SDavid Howells break; 1124cc53ce53SDavid Howells dput(path->dentry); 1125cc53ce53SDavid Howells mntput(path->mnt); 1126cc53ce53SDavid Howells path->mnt = mounted; 1127cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 1128cc53ce53SDavid Howells continue; 1129cc53ce53SDavid Howells } 1130cc53ce53SDavid Howells 1131cc53ce53SDavid Howells /* Don't handle automount points here */ 1132cc53ce53SDavid Howells break; 1133cc53ce53SDavid Howells } 1134cc53ce53SDavid Howells return 0; 1135cc53ce53SDavid Howells } 1136cc53ce53SDavid Howells 1137cc53ce53SDavid Howells /* 11389875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 11399875cf80SDavid Howells */ 11409875cf80SDavid Howells static void follow_mount(struct path *path) 11419875cf80SDavid Howells { 11429875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 11439875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 11449875cf80SDavid Howells if (!mounted) 11459875cf80SDavid Howells break; 11469875cf80SDavid Howells dput(path->dentry); 11479875cf80SDavid Howells mntput(path->mnt); 11489875cf80SDavid Howells path->mnt = mounted; 11499875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 11509875cf80SDavid Howells } 11519875cf80SDavid Howells } 11529875cf80SDavid Howells 115331e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 11541da177e4SLinus Torvalds { 11552a737871SAl Viro set_root(nd); 1156e518ddb7SAndreas Mohr 11571da177e4SLinus Torvalds while(1) { 11584ac91378SJan Blunck struct dentry *old = nd->path.dentry; 11591da177e4SLinus Torvalds 11602a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 11612a737871SAl Viro nd->path.mnt == nd->root.mnt) { 11621da177e4SLinus Torvalds break; 11631da177e4SLinus Torvalds } 11644ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 11653088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 11663088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 11671da177e4SLinus Torvalds dput(old); 11681da177e4SLinus Torvalds break; 11691da177e4SLinus Torvalds } 11703088dd70SAl Viro if (!follow_up(&nd->path)) 11711da177e4SLinus Torvalds break; 11721da177e4SLinus Torvalds } 117379ed0226SAl Viro follow_mount(&nd->path); 117431e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 11751da177e4SLinus Torvalds } 11761da177e4SLinus Torvalds 11771da177e4SLinus Torvalds /* 1178baa03890SNick Piggin * Allocate a dentry with name and parent, and perform a parent 1179baa03890SNick Piggin * directory ->lookup on it. Returns the new dentry, or ERR_PTR 1180baa03890SNick Piggin * on error. parent->d_inode->i_mutex must be held. d_lookup must 1181baa03890SNick Piggin * have verified that no child exists while under i_mutex. 1182baa03890SNick Piggin */ 1183baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent, 1184baa03890SNick Piggin struct qstr *name, struct nameidata *nd) 1185baa03890SNick Piggin { 1186baa03890SNick Piggin struct inode *inode = parent->d_inode; 1187baa03890SNick Piggin struct dentry *dentry; 1188baa03890SNick Piggin struct dentry *old; 1189baa03890SNick Piggin 1190baa03890SNick Piggin /* Don't create child dentry for a dead directory. */ 1191baa03890SNick Piggin if (unlikely(IS_DEADDIR(inode))) 1192baa03890SNick Piggin return ERR_PTR(-ENOENT); 1193baa03890SNick Piggin 1194baa03890SNick Piggin dentry = d_alloc(parent, name); 1195baa03890SNick Piggin if (unlikely(!dentry)) 1196baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1197baa03890SNick Piggin 1198baa03890SNick Piggin old = inode->i_op->lookup(inode, dentry, nd); 1199baa03890SNick Piggin if (unlikely(old)) { 1200baa03890SNick Piggin dput(dentry); 1201baa03890SNick Piggin dentry = old; 1202baa03890SNick Piggin } 1203baa03890SNick Piggin return dentry; 1204baa03890SNick Piggin } 1205baa03890SNick Piggin 1206baa03890SNick Piggin /* 12071da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 12081da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 12091da177e4SLinus Torvalds * It _is_ time-critical. 12101da177e4SLinus Torvalds */ 12111da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name, 121231e6b01fSNick Piggin struct path *path, struct inode **inode) 12131da177e4SLinus Torvalds { 12144ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 121531e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 12166e6b1bd1SAl Viro struct inode *dir; 12179875cf80SDavid Howells int err; 12189875cf80SDavid Howells 12193cac260aSAl Viro /* 12203cac260aSAl Viro * See if the low-level filesystem might want 12213cac260aSAl Viro * to use its own hash.. 12223cac260aSAl Viro */ 1223fb045adbSNick Piggin if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 12249875cf80SDavid Howells err = parent->d_op->d_hash(parent, nd->inode, name); 12253cac260aSAl Viro if (err < 0) 12263cac260aSAl Viro return err; 12273cac260aSAl Viro } 12281da177e4SLinus Torvalds 1229b04f784eSNick Piggin /* 1230b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1231b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1232b04f784eSNick Piggin * do the non-racy lookup, below. 1233b04f784eSNick Piggin */ 123431e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 123531e6b01fSNick Piggin unsigned seq; 123631e6b01fSNick Piggin 123731e6b01fSNick Piggin *inode = nd->inode; 123831e6b01fSNick Piggin dentry = __d_lookup_rcu(parent, name, &seq, inode); 123931e6b01fSNick Piggin if (!dentry) { 124031e6b01fSNick Piggin if (nameidata_drop_rcu(nd)) 124131e6b01fSNick Piggin return -ECHILD; 124231e6b01fSNick Piggin goto need_lookup; 124331e6b01fSNick Piggin } 124431e6b01fSNick Piggin /* Memory barrier in read_seqcount_begin of child is enough */ 124531e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 124631e6b01fSNick Piggin return -ECHILD; 124731e6b01fSNick Piggin 124831e6b01fSNick Piggin nd->seq = seq; 124924643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 1250f5e1c1c1SAl Viro dentry = do_revalidate_rcu(dentry, nd); 125124643087SAl Viro if (!dentry) 125224643087SAl Viro goto need_lookup; 125324643087SAl Viro if (IS_ERR(dentry)) 125424643087SAl Viro goto fail; 125524643087SAl Viro if (!(nd->flags & LOOKUP_RCU)) 125624643087SAl Viro goto done; 125724643087SAl Viro } 125831e6b01fSNick Piggin path->mnt = mnt; 125931e6b01fSNick Piggin path->dentry = dentry; 12609875cf80SDavid Howells if (likely(__follow_mount_rcu(nd, path, inode, false))) 12619875cf80SDavid Howells return 0; 12629875cf80SDavid Howells if (nameidata_drop_rcu(nd)) 12639875cf80SDavid Howells return -ECHILD; 12649875cf80SDavid Howells /* fallthru */ 12659875cf80SDavid Howells } 126631e6b01fSNick Piggin dentry = __d_lookup(parent, name); 12671da177e4SLinus Torvalds if (!dentry) 12681da177e4SLinus Torvalds goto need_lookup; 12692e2e88eaSNick Piggin found: 127024643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 127124643087SAl Viro dentry = do_revalidate(dentry, nd); 127224643087SAl Viro if (!dentry) 127324643087SAl Viro goto need_lookup; 127424643087SAl Viro if (IS_ERR(dentry)) 127524643087SAl Viro goto fail; 127624643087SAl Viro } 12771da177e4SLinus Torvalds done: 12781da177e4SLinus Torvalds path->mnt = mnt; 12791da177e4SLinus Torvalds path->dentry = dentry; 12809875cf80SDavid Howells err = follow_managed(path, nd->flags); 128189312214SIan Kent if (unlikely(err < 0)) { 128289312214SIan Kent path_put_conditional(path, nd); 12839875cf80SDavid Howells return err; 128489312214SIan Kent } 128531e6b01fSNick Piggin *inode = path->dentry->d_inode; 12861da177e4SLinus Torvalds return 0; 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds need_lookup: 12896e6b1bd1SAl Viro dir = parent->d_inode; 129031e6b01fSNick Piggin BUG_ON(nd->inode != dir); 12916e6b1bd1SAl Viro 12926e6b1bd1SAl Viro mutex_lock(&dir->i_mutex); 12936e6b1bd1SAl Viro /* 12946e6b1bd1SAl Viro * First re-do the cached lookup just in case it was created 1295b04f784eSNick Piggin * while we waited for the directory semaphore, or the first 1296b04f784eSNick Piggin * lookup failed due to an unrelated rename. 12976e6b1bd1SAl Viro * 1298b04f784eSNick Piggin * This could use version numbering or similar to avoid unnecessary 1299b04f784eSNick Piggin * cache lookups, but then we'd have to do the first lookup in the 1300b04f784eSNick Piggin * non-racy way. However in the common case here, everything should 1301b04f784eSNick Piggin * be hot in cache, so would it be a big win? 13026e6b1bd1SAl Viro */ 13036e6b1bd1SAl Viro dentry = d_lookup(parent, name); 1304baa03890SNick Piggin if (likely(!dentry)) { 1305baa03890SNick Piggin dentry = d_alloc_and_lookup(parent, name, nd); 13066e6b1bd1SAl Viro mutex_unlock(&dir->i_mutex); 13076e6b1bd1SAl Viro if (IS_ERR(dentry)) 13086e6b1bd1SAl Viro goto fail; 13096e6b1bd1SAl Viro goto done; 13106e6b1bd1SAl Viro } 13116e6b1bd1SAl Viro /* 13126e6b1bd1SAl Viro * Uhhuh! Nasty case: the cache was re-populated while 13136e6b1bd1SAl Viro * we waited on the semaphore. Need to revalidate. 13146e6b1bd1SAl Viro */ 13156e6b1bd1SAl Viro mutex_unlock(&dir->i_mutex); 13162e2e88eaSNick Piggin goto found; 13171da177e4SLinus Torvalds 13181da177e4SLinus Torvalds fail: 13191da177e4SLinus Torvalds return PTR_ERR(dentry); 13201da177e4SLinus Torvalds } 13211da177e4SLinus Torvalds 132252094c8aSAl Viro static inline int may_lookup(struct nameidata *nd) 132352094c8aSAl Viro { 132452094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 132552094c8aSAl Viro int err = exec_permission(nd->inode, IPERM_FLAG_RCU); 132652094c8aSAl Viro if (err != -ECHILD) 132752094c8aSAl Viro return err; 132852094c8aSAl Viro if (nameidata_drop_rcu(nd)) 132952094c8aSAl Viro return -ECHILD; 133052094c8aSAl Viro } 133152094c8aSAl Viro return exec_permission(nd->inode, 0); 133252094c8aSAl Viro } 133352094c8aSAl Viro 13349856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type) 13359856fa1bSAl Viro { 13369856fa1bSAl Viro if (type == LAST_DOTDOT) { 13379856fa1bSAl Viro if (nd->flags & LOOKUP_RCU) { 13389856fa1bSAl Viro if (follow_dotdot_rcu(nd)) 13399856fa1bSAl Viro return -ECHILD; 13409856fa1bSAl Viro } else 13419856fa1bSAl Viro follow_dotdot(nd); 13429856fa1bSAl Viro } 13439856fa1bSAl Viro return 0; 13449856fa1bSAl Viro } 13459856fa1bSAl Viro 1346951361f9SAl Viro static void terminate_walk(struct nameidata *nd) 1347951361f9SAl Viro { 1348951361f9SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1349951361f9SAl Viro path_put(&nd->path); 1350951361f9SAl Viro } else { 1351951361f9SAl Viro nd->flags &= ~LOOKUP_RCU; 1352951361f9SAl Viro nd->root.mnt = NULL; 1353951361f9SAl Viro rcu_read_unlock(); 1354951361f9SAl Viro br_read_unlock(vfsmount_lock); 1355951361f9SAl Viro } 1356951361f9SAl Viro } 1357951361f9SAl Viro 13581da177e4SLinus Torvalds /* 13591da177e4SLinus Torvalds * Name resolution. 1360ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1361ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 13621da177e4SLinus Torvalds * 1363ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1364ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 13651da177e4SLinus Torvalds */ 13666de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 13671da177e4SLinus Torvalds { 13681da177e4SLinus Torvalds struct path next; 13691da177e4SLinus Torvalds int err; 13701da177e4SLinus Torvalds unsigned int lookup_flags = nd->flags; 13711da177e4SLinus Torvalds 13721da177e4SLinus Torvalds while (*name=='/') 13731da177e4SLinus Torvalds name++; 13741da177e4SLinus Torvalds if (!*name) 1375086e183aSAl Viro return 0; 13761da177e4SLinus Torvalds 13771da177e4SLinus Torvalds if (nd->depth) 1378f55eab82STrond Myklebust lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE); 13791da177e4SLinus Torvalds 13801da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 13811da177e4SLinus Torvalds for(;;) { 138231e6b01fSNick Piggin struct inode *inode; 13831da177e4SLinus Torvalds unsigned long hash; 13841da177e4SLinus Torvalds struct qstr this; 13851da177e4SLinus Torvalds unsigned int c; 1386fe479a58SAl Viro int type; 13871da177e4SLinus Torvalds 1388cdce5d6bSTrond Myklebust nd->flags |= LOOKUP_CONTINUE; 138952094c8aSAl Viro 139052094c8aSAl Viro err = may_lookup(nd); 13911da177e4SLinus Torvalds if (err) 13921da177e4SLinus Torvalds break; 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds this.name = name; 13951da177e4SLinus Torvalds c = *(const unsigned char *)name; 13961da177e4SLinus Torvalds 13971da177e4SLinus Torvalds hash = init_name_hash(); 13981da177e4SLinus Torvalds do { 13991da177e4SLinus Torvalds name++; 14001da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 14011da177e4SLinus Torvalds c = *(const unsigned char *)name; 14021da177e4SLinus Torvalds } while (c && (c != '/')); 14031da177e4SLinus Torvalds this.len = name - (const char *) this.name; 14041da177e4SLinus Torvalds this.hash = end_name_hash(hash); 14051da177e4SLinus Torvalds 1406fe479a58SAl Viro type = LAST_NORM; 1407fe479a58SAl Viro if (this.name[0] == '.') switch (this.len) { 1408fe479a58SAl Viro case 2: 140916c2cd71SAl Viro if (this.name[1] == '.') { 1410fe479a58SAl Viro type = LAST_DOTDOT; 141116c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 141216c2cd71SAl Viro } 1413fe479a58SAl Viro break; 1414fe479a58SAl Viro case 1: 1415fe479a58SAl Viro type = LAST_DOT; 1416fe479a58SAl Viro } 141716c2cd71SAl Viro if (likely(type == LAST_NORM)) 141816c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 1419fe479a58SAl Viro 14201da177e4SLinus Torvalds /* remove trailing slashes? */ 14211da177e4SLinus Torvalds if (!c) 14221da177e4SLinus Torvalds goto last_component; 14231da177e4SLinus Torvalds while (*++name == '/'); 14241da177e4SLinus Torvalds if (!*name) 14251da177e4SLinus Torvalds goto last_with_slashes; 14261da177e4SLinus Torvalds 14271da177e4SLinus Torvalds /* 14281da177e4SLinus Torvalds * "." and ".." are special - ".." especially so because it has 14291da177e4SLinus Torvalds * to be able to know about the current root directory and 14301da177e4SLinus Torvalds * parent relationships. 14311da177e4SLinus Torvalds */ 1432fe479a58SAl Viro if (unlikely(type != LAST_NORM)) { 1433ef7562d5SAl Viro if (handle_dots(nd, type)) 1434ef7562d5SAl Viro return -ECHILD; 14351da177e4SLinus Torvalds continue; 14361da177e4SLinus Torvalds } 1437fe479a58SAl Viro 14381da177e4SLinus Torvalds /* This does the actual lookups.. */ 143931e6b01fSNick Piggin err = do_lookup(nd, &this, &next, &inode); 14401da177e4SLinus Torvalds if (err) 14411da177e4SLinus Torvalds break; 14421da177e4SLinus Torvalds 14437bc055d1SAl Viro if (inode && inode->i_op->follow_link) { 14443abb17e8SLinus Torvalds err = do_follow_link(inode, &next, nd); 14451da177e4SLinus Torvalds if (err) 1446a7472babSAl Viro return err; 144731e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 144831e6b01fSNick Piggin } else { 144909dd17d3SMiklos Szeredi path_to_nameidata(&next, nd); 145031e6b01fSNick Piggin nd->inode = inode; 145131e6b01fSNick Piggin } 14527bc055d1SAl Viro err = -ENOENT; 14537bc055d1SAl Viro if (!nd->inode) 14547bc055d1SAl Viro break; 14551da177e4SLinus Torvalds err = -ENOTDIR; 145631e6b01fSNick Piggin if (!nd->inode->i_op->lookup) 14571da177e4SLinus Torvalds break; 14581da177e4SLinus Torvalds continue; 14591da177e4SLinus Torvalds /* here ends the main loop */ 14601da177e4SLinus Torvalds 14611da177e4SLinus Torvalds last_with_slashes: 14621da177e4SLinus Torvalds lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 14631da177e4SLinus Torvalds last_component: 1464f55eab82STrond Myklebust /* Clear LOOKUP_CONTINUE iff it was previously unset */ 1465f55eab82STrond Myklebust nd->flags &= lookup_flags | ~LOOKUP_CONTINUE; 14661da177e4SLinus Torvalds if (lookup_flags & LOOKUP_PARENT) 14671da177e4SLinus Torvalds goto lookup_parent; 1468ef7562d5SAl Viro if (unlikely(type != LAST_NORM)) 1469ef7562d5SAl Viro return handle_dots(nd, type); 147031e6b01fSNick Piggin err = do_lookup(nd, &this, &next, &inode); 14711da177e4SLinus Torvalds if (err) 14721da177e4SLinus Torvalds break; 1473db372915SDavid Howells if (inode && unlikely(inode->i_op->follow_link) && 1474db372915SDavid Howells (lookup_flags & LOOKUP_FOLLOW)) { 14753abb17e8SLinus Torvalds err = do_follow_link(inode, &next, nd); 14761da177e4SLinus Torvalds if (err) 1477a7472babSAl Viro return err; 147831e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 147931e6b01fSNick Piggin } else { 148009dd17d3SMiklos Szeredi path_to_nameidata(&next, nd); 148131e6b01fSNick Piggin nd->inode = inode; 148231e6b01fSNick Piggin } 14831da177e4SLinus Torvalds err = -ENOENT; 148431e6b01fSNick Piggin if (!nd->inode) 14851da177e4SLinus Torvalds break; 14861da177e4SLinus Torvalds if (lookup_flags & LOOKUP_DIRECTORY) { 14871da177e4SLinus Torvalds err = -ENOTDIR; 148831e6b01fSNick Piggin if (!nd->inode->i_op->lookup) 14891da177e4SLinus Torvalds break; 14901da177e4SLinus Torvalds } 1491086e183aSAl Viro return 0; 14921da177e4SLinus Torvalds lookup_parent: 14931da177e4SLinus Torvalds nd->last = this; 1494fe479a58SAl Viro nd->last_type = type; 14951da177e4SLinus Torvalds return 0; 14961da177e4SLinus Torvalds } 1497951361f9SAl Viro terminate_walk(nd); 14981da177e4SLinus Torvalds return err; 14991da177e4SLinus Torvalds } 15001da177e4SLinus Torvalds 150170e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, 150270e9b357SAl Viro struct nameidata *nd, struct file **fp) 150331e6b01fSNick Piggin { 150431e6b01fSNick Piggin int retval = 0; 150531e6b01fSNick Piggin int fput_needed; 150631e6b01fSNick Piggin struct file *file; 150731e6b01fSNick Piggin 150831e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 150916c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 151031e6b01fSNick Piggin nd->depth = 0; 151131e6b01fSNick Piggin nd->root.mnt = NULL; 151231e6b01fSNick Piggin 151331e6b01fSNick Piggin if (*name=='/') { 1514e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 151531e6b01fSNick Piggin br_read_lock(vfsmount_lock); 151631e6b01fSNick Piggin rcu_read_lock(); 1517e41f7d4eSAl Viro set_root_rcu(nd); 1518e41f7d4eSAl Viro } else { 1519e41f7d4eSAl Viro set_root(nd); 1520e41f7d4eSAl Viro path_get(&nd->root); 1521e41f7d4eSAl Viro } 152231e6b01fSNick Piggin nd->path = nd->root; 152331e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 1524e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 152531e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1526c28cc364SNick Piggin unsigned seq; 152731e6b01fSNick Piggin 152831e6b01fSNick Piggin br_read_lock(vfsmount_lock); 152931e6b01fSNick Piggin rcu_read_lock(); 153031e6b01fSNick Piggin 1531c28cc364SNick Piggin do { 1532c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 153331e6b01fSNick Piggin nd->path = fs->pwd; 1534c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1535c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1536e41f7d4eSAl Viro } else { 1537e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 1538e41f7d4eSAl Viro } 153931e6b01fSNick Piggin } else { 154031e6b01fSNick Piggin struct dentry *dentry; 154131e6b01fSNick Piggin 154231e6b01fSNick Piggin file = fget_light(dfd, &fput_needed); 154331e6b01fSNick Piggin retval = -EBADF; 154431e6b01fSNick Piggin if (!file) 154531e6b01fSNick Piggin goto out_fail; 154631e6b01fSNick Piggin 154731e6b01fSNick Piggin dentry = file->f_path.dentry; 154831e6b01fSNick Piggin 154931e6b01fSNick Piggin retval = -ENOTDIR; 155031e6b01fSNick Piggin if (!S_ISDIR(dentry->d_inode->i_mode)) 155131e6b01fSNick Piggin goto fput_fail; 155231e6b01fSNick Piggin 155331e6b01fSNick Piggin retval = file_permission(file, MAY_EXEC); 155431e6b01fSNick Piggin if (retval) 155531e6b01fSNick Piggin goto fput_fail; 155631e6b01fSNick Piggin 155731e6b01fSNick Piggin nd->path = file->f_path; 1558e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 155931e6b01fSNick Piggin if (fput_needed) 156070e9b357SAl Viro *fp = file; 1561c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 156231e6b01fSNick Piggin br_read_lock(vfsmount_lock); 156331e6b01fSNick Piggin rcu_read_lock(); 15645590ff0dSUlrich Drepper } else { 15655dd784d0SJan Blunck path_get(&file->f_path); 15665590ff0dSUlrich Drepper fput_light(file, fput_needed); 15671da177e4SLinus Torvalds } 1568e41f7d4eSAl Viro } 1569e41f7d4eSAl Viro 157031e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 15719b4a9b14SAl Viro return 0; 15722dfdd266SJosef 'Jeff' Sipek 15739b4a9b14SAl Viro fput_fail: 15749b4a9b14SAl Viro fput_light(file, fput_needed); 15759b4a9b14SAl Viro out_fail: 15769b4a9b14SAl Viro return retval; 15779b4a9b14SAl Viro } 15789b4a9b14SAl Viro 15799b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1580ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name, 15819b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 15829b4a9b14SAl Viro { 158370e9b357SAl Viro struct file *base = NULL; 158431e6b01fSNick Piggin int retval; 158531e6b01fSNick Piggin 158631e6b01fSNick Piggin /* 158731e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 158831e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 158931e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 159031e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 159131e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 159231e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 159331e6b01fSNick Piggin * analogue, foo_rcu(). 159431e6b01fSNick Piggin * 159531e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 159631e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 159731e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 159831e6b01fSNick Piggin * be able to complete). 159931e6b01fSNick Piggin */ 160070e9b357SAl Viro retval = path_init(dfd, name, flags, nd, &base); 1601ee0827cdSAl Viro 160231e6b01fSNick Piggin if (unlikely(retval)) 160331e6b01fSNick Piggin return retval; 1604ee0827cdSAl Viro 1605ee0827cdSAl Viro current->total_link_count = 0; 1606ee0827cdSAl Viro retval = link_path_walk(name, nd); 1607ee0827cdSAl Viro 1608ee0827cdSAl Viro if (nd->flags & LOOKUP_RCU) { 16094455ca62SAl Viro /* went all way through without dropping RCU */ 16104455ca62SAl Viro BUG_ON(retval); 1611086e183aSAl Viro if (nameidata_drop_rcu_last(nd)) 1612086e183aSAl Viro retval = -ECHILD; 1613086e183aSAl Viro } 161431e6b01fSNick Piggin 161516c2cd71SAl Viro if (!retval) 161616c2cd71SAl Viro retval = handle_reval_path(nd); 161716c2cd71SAl Viro 161870e9b357SAl Viro if (base) 161970e9b357SAl Viro fput(base); 1620ee0827cdSAl Viro 162131e6b01fSNick Piggin if (nd->root.mnt) { 162231e6b01fSNick Piggin path_put(&nd->root); 162331e6b01fSNick Piggin nd->root.mnt = NULL; 162431e6b01fSNick Piggin } 1625ee0827cdSAl Viro return retval; 162631e6b01fSNick Piggin } 162731e6b01fSNick Piggin 1628ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name, 1629ee0827cdSAl Viro unsigned int flags, struct nameidata *nd) 1630ee0827cdSAl Viro { 1631ee0827cdSAl Viro int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd); 1632ee0827cdSAl Viro if (unlikely(retval == -ECHILD)) 1633ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags, nd); 1634ee0827cdSAl Viro if (unlikely(retval == -ESTALE)) 1635ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd); 1636ee0827cdSAl Viro 163731e6b01fSNick Piggin if (likely(!retval)) { 163831e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) { 163931e6b01fSNick Piggin if (nd->path.dentry && nd->inode) 164031e6b01fSNick Piggin audit_inode(name, nd->path.dentry); 164131e6b01fSNick Piggin } 164231e6b01fSNick Piggin } 1643170aa3d0SUlrich Drepper return retval; 16441da177e4SLinus Torvalds } 16451da177e4SLinus Torvalds 1646c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd) 16475590ff0dSUlrich Drepper { 1648c9c6cac0SAl Viro return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd); 16495590ff0dSUlrich Drepper } 16505590ff0dSUlrich Drepper 1651d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1652d1811465SAl Viro { 1653d1811465SAl Viro struct nameidata nd; 1654d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1655d1811465SAl Viro if (!res) 1656d1811465SAl Viro *path = nd.path; 1657d1811465SAl Viro return res; 1658d1811465SAl Viro } 1659d1811465SAl Viro 166016f18200SJosef 'Jeff' Sipek /** 166116f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 166216f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 166316f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 166416f18200SJosef 'Jeff' Sipek * @name: pointer to file name 166516f18200SJosef 'Jeff' Sipek * @flags: lookup flags 166616f18200SJosef 'Jeff' Sipek * @nd: pointer to nameidata 166716f18200SJosef 'Jeff' Sipek */ 166816f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 166916f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 167016f18200SJosef 'Jeff' Sipek struct nameidata *nd) 167116f18200SJosef 'Jeff' Sipek { 1672ee0827cdSAl Viro int result; 167316f18200SJosef 'Jeff' Sipek 167416f18200SJosef 'Jeff' Sipek /* same as do_path_lookup */ 167516f18200SJosef 'Jeff' Sipek nd->last_type = LAST_ROOT; 167616c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 167716f18200SJosef 'Jeff' Sipek nd->depth = 0; 167816f18200SJosef 'Jeff' Sipek 1679c8e7f449SJan Blunck nd->path.dentry = dentry; 1680c8e7f449SJan Blunck nd->path.mnt = mnt; 1681c8e7f449SJan Blunck path_get(&nd->path); 16825b857119SAl Viro nd->root = nd->path; 16835b857119SAl Viro path_get(&nd->root); 168431e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 168516f18200SJosef 'Jeff' Sipek 1686ee0827cdSAl Viro current->total_link_count = 0; 1687ee0827cdSAl Viro 1688ee0827cdSAl Viro result = link_path_walk(name, nd); 168916c2cd71SAl Viro if (!result) 169016c2cd71SAl Viro result = handle_reval_path(nd); 1691ee0827cdSAl Viro if (result == -ESTALE) { 1692ee0827cdSAl Viro /* nd->path had been dropped */ 1693ee0827cdSAl Viro current->total_link_count = 0; 1694ee0827cdSAl Viro nd->path.dentry = dentry; 1695ee0827cdSAl Viro nd->path.mnt = mnt; 1696ee0827cdSAl Viro nd->inode = dentry->d_inode; 1697ee0827cdSAl Viro path_get(&nd->path); 169816c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED | LOOKUP_REVAL; 169916c2cd71SAl Viro 1700ee0827cdSAl Viro result = link_path_walk(name, nd); 170116c2cd71SAl Viro if (!result) 170216c2cd71SAl Viro result = handle_reval_path(nd); 1703ee0827cdSAl Viro } 1704ee0827cdSAl Viro if (unlikely(!result && !audit_dummy_context() && nd->path.dentry && 170531e6b01fSNick Piggin nd->inode)) 17064ac91378SJan Blunck audit_inode(name, nd->path.dentry); 170716f18200SJosef 'Jeff' Sipek 17082a737871SAl Viro path_put(&nd->root); 17092a737871SAl Viro nd->root.mnt = NULL; 171016f18200SJosef 'Jeff' Sipek 1711ee0827cdSAl Viro return result; 171216f18200SJosef 'Jeff' Sipek } 171316f18200SJosef 'Jeff' Sipek 1714eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name, 1715eead1911SChristoph Hellwig struct dentry *base, struct nameidata *nd) 17161da177e4SLinus Torvalds { 171781fca444SChristoph Hellwig struct inode *inode = base->d_inode; 17181da177e4SLinus Torvalds struct dentry *dentry; 17191da177e4SLinus Torvalds int err; 17201da177e4SLinus Torvalds 1721b74c79e9SNick Piggin err = exec_permission(inode, 0); 172281fca444SChristoph Hellwig if (err) 172381fca444SChristoph Hellwig return ERR_PTR(err); 17241da177e4SLinus Torvalds 17251da177e4SLinus Torvalds /* 17261da177e4SLinus Torvalds * See if the low-level filesystem might want 17271da177e4SLinus Torvalds * to use its own hash.. 17281da177e4SLinus Torvalds */ 1729fb045adbSNick Piggin if (base->d_flags & DCACHE_OP_HASH) { 1730b1e6a015SNick Piggin err = base->d_op->d_hash(base, inode, name); 17311da177e4SLinus Torvalds dentry = ERR_PTR(err); 17321da177e4SLinus Torvalds if (err < 0) 17331da177e4SLinus Torvalds goto out; 17341da177e4SLinus Torvalds } 17351da177e4SLinus Torvalds 1736b04f784eSNick Piggin /* 1737b04f784eSNick Piggin * Don't bother with __d_lookup: callers are for creat as 1738b04f784eSNick Piggin * well as unlink, so a lot of the time it would cost 1739b04f784eSNick Piggin * a double lookup. 17406e6b1bd1SAl Viro */ 17416e6b1bd1SAl Viro dentry = d_lookup(base, name); 17426e6b1bd1SAl Viro 1743fb045adbSNick Piggin if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) 17446e6b1bd1SAl Viro dentry = do_revalidate(dentry, nd); 17456e6b1bd1SAl Viro 17461da177e4SLinus Torvalds if (!dentry) 1747baa03890SNick Piggin dentry = d_alloc_and_lookup(base, name, nd); 17481da177e4SLinus Torvalds out: 17491da177e4SLinus Torvalds return dentry; 17501da177e4SLinus Torvalds } 17511da177e4SLinus Torvalds 1752057f6c01SJames Morris /* 1753057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1754057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1755057f6c01SJames Morris * SMP-safe. 1756057f6c01SJames Morris */ 1757a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 17581da177e4SLinus Torvalds { 17594ac91378SJan Blunck return __lookup_hash(&nd->last, nd->path.dentry, nd); 17601da177e4SLinus Torvalds } 17611da177e4SLinus Torvalds 1762eead1911SChristoph Hellwig static int __lookup_one_len(const char *name, struct qstr *this, 1763eead1911SChristoph Hellwig struct dentry *base, int len) 17641da177e4SLinus Torvalds { 17651da177e4SLinus Torvalds unsigned long hash; 17661da177e4SLinus Torvalds unsigned int c; 17671da177e4SLinus Torvalds 1768057f6c01SJames Morris this->name = name; 1769057f6c01SJames Morris this->len = len; 17701da177e4SLinus Torvalds if (!len) 1771057f6c01SJames Morris return -EACCES; 17721da177e4SLinus Torvalds 17731da177e4SLinus Torvalds hash = init_name_hash(); 17741da177e4SLinus Torvalds while (len--) { 17751da177e4SLinus Torvalds c = *(const unsigned char *)name++; 17761da177e4SLinus Torvalds if (c == '/' || c == '\0') 1777057f6c01SJames Morris return -EACCES; 17781da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 17791da177e4SLinus Torvalds } 1780057f6c01SJames Morris this->hash = end_name_hash(hash); 1781057f6c01SJames Morris return 0; 1782057f6c01SJames Morris } 17831da177e4SLinus Torvalds 1784eead1911SChristoph Hellwig /** 1785a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1786eead1911SChristoph Hellwig * @name: pathname component to lookup 1787eead1911SChristoph Hellwig * @base: base directory to lookup from 1788eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1789eead1911SChristoph Hellwig * 1790a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1791a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1792eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1793eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1794eead1911SChristoph Hellwig */ 1795057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1796057f6c01SJames Morris { 1797057f6c01SJames Morris int err; 1798057f6c01SJames Morris struct qstr this; 1799057f6c01SJames Morris 18002f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 18012f9092e1SDavid Woodhouse 1802057f6c01SJames Morris err = __lookup_one_len(name, &this, base, len); 1803057f6c01SJames Morris if (err) 1804057f6c01SJames Morris return ERR_PTR(err); 1805eead1911SChristoph Hellwig 180649705b77SChristoph Hellwig return __lookup_hash(&this, base, NULL); 1807057f6c01SJames Morris } 1808057f6c01SJames Morris 18092d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags, 18102d8f3038SAl Viro struct path *path) 18111da177e4SLinus Torvalds { 18122d8f3038SAl Viro struct nameidata nd; 18131da177e4SLinus Torvalds char *tmp = getname(name); 18141da177e4SLinus Torvalds int err = PTR_ERR(tmp); 18151da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 18162d8f3038SAl Viro 18172d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 18182d8f3038SAl Viro 18192d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 18201da177e4SLinus Torvalds putname(tmp); 18212d8f3038SAl Viro if (!err) 18222d8f3038SAl Viro *path = nd.path; 18231da177e4SLinus Torvalds } 18241da177e4SLinus Torvalds return err; 18251da177e4SLinus Torvalds } 18261da177e4SLinus Torvalds 18272ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 18282ad94ae6SAl Viro struct nameidata *nd, char **name) 18292ad94ae6SAl Viro { 18302ad94ae6SAl Viro char *s = getname(path); 18312ad94ae6SAl Viro int error; 18322ad94ae6SAl Viro 18332ad94ae6SAl Viro if (IS_ERR(s)) 18342ad94ae6SAl Viro return PTR_ERR(s); 18352ad94ae6SAl Viro 18362ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 18372ad94ae6SAl Viro if (error) 18382ad94ae6SAl Viro putname(s); 18392ad94ae6SAl Viro else 18402ad94ae6SAl Viro *name = s; 18412ad94ae6SAl Viro 18422ad94ae6SAl Viro return error; 18432ad94ae6SAl Viro } 18442ad94ae6SAl Viro 18451da177e4SLinus Torvalds /* 18461da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 18471da177e4SLinus Torvalds * minimal. 18481da177e4SLinus Torvalds */ 18491da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 18501da177e4SLinus Torvalds { 1851da9592edSDavid Howells uid_t fsuid = current_fsuid(); 1852da9592edSDavid Howells 18531da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 18541da177e4SLinus Torvalds return 0; 1855da9592edSDavid Howells if (inode->i_uid == fsuid) 18561da177e4SLinus Torvalds return 0; 1857da9592edSDavid Howells if (dir->i_uid == fsuid) 18581da177e4SLinus Torvalds return 0; 18591da177e4SLinus Torvalds return !capable(CAP_FOWNER); 18601da177e4SLinus Torvalds } 18611da177e4SLinus Torvalds 18621da177e4SLinus Torvalds /* 18631da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 18641da177e4SLinus Torvalds * whether the type of victim is right. 18651da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 18661da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 18671da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 18681da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 18691da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 18701da177e4SLinus Torvalds * a. be owner of dir, or 18711da177e4SLinus Torvalds * b. be owner of victim, or 18721da177e4SLinus Torvalds * c. have CAP_FOWNER capability 18731da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 18741da177e4SLinus Torvalds * links pointing to it. 18751da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 18761da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 18771da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 18781da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 18791da177e4SLinus Torvalds * nfs_async_unlink(). 18801da177e4SLinus Torvalds */ 1881858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 18821da177e4SLinus Torvalds { 18831da177e4SLinus Torvalds int error; 18841da177e4SLinus Torvalds 18851da177e4SLinus Torvalds if (!victim->d_inode) 18861da177e4SLinus Torvalds return -ENOENT; 18871da177e4SLinus Torvalds 18881da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 1889cccc6bbaSAl Viro audit_inode_child(victim, dir); 18901da177e4SLinus Torvalds 1891f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 18921da177e4SLinus Torvalds if (error) 18931da177e4SLinus Torvalds return error; 18941da177e4SLinus Torvalds if (IS_APPEND(dir)) 18951da177e4SLinus Torvalds return -EPERM; 18961da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 1897f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 18981da177e4SLinus Torvalds return -EPERM; 18991da177e4SLinus Torvalds if (isdir) { 19001da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 19011da177e4SLinus Torvalds return -ENOTDIR; 19021da177e4SLinus Torvalds if (IS_ROOT(victim)) 19031da177e4SLinus Torvalds return -EBUSY; 19041da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 19051da177e4SLinus Torvalds return -EISDIR; 19061da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 19071da177e4SLinus Torvalds return -ENOENT; 19081da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 19091da177e4SLinus Torvalds return -EBUSY; 19101da177e4SLinus Torvalds return 0; 19111da177e4SLinus Torvalds } 19121da177e4SLinus Torvalds 19131da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 19141da177e4SLinus Torvalds * dir. 19151da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 19161da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 19171da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 19181da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 19191da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 19201da177e4SLinus Torvalds */ 1921a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 19221da177e4SLinus Torvalds { 19231da177e4SLinus Torvalds if (child->d_inode) 19241da177e4SLinus Torvalds return -EEXIST; 19251da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 19261da177e4SLinus Torvalds return -ENOENT; 1927f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 19281da177e4SLinus Torvalds } 19291da177e4SLinus Torvalds 19301da177e4SLinus Torvalds /* 19311da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 19321da177e4SLinus Torvalds */ 19331da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 19341da177e4SLinus Torvalds { 19351da177e4SLinus Torvalds struct dentry *p; 19361da177e4SLinus Torvalds 19371da177e4SLinus Torvalds if (p1 == p2) { 1938f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 19391da177e4SLinus Torvalds return NULL; 19401da177e4SLinus Torvalds } 19411da177e4SLinus Torvalds 1942a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 19431da177e4SLinus Torvalds 1944e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 1945e2761a11SOGAWA Hirofumi if (p) { 1946f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 1947f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 19481da177e4SLinus Torvalds return p; 19491da177e4SLinus Torvalds } 19501da177e4SLinus Torvalds 1951e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 1952e2761a11SOGAWA Hirofumi if (p) { 1953f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1954f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 19551da177e4SLinus Torvalds return p; 19561da177e4SLinus Torvalds } 19571da177e4SLinus Torvalds 1958f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1959f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 19601da177e4SLinus Torvalds return NULL; 19611da177e4SLinus Torvalds } 19621da177e4SLinus Torvalds 19631da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 19641da177e4SLinus Torvalds { 19651b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 19661da177e4SLinus Torvalds if (p1 != p2) { 19671b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 1968a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 19691da177e4SLinus Torvalds } 19701da177e4SLinus Torvalds } 19711da177e4SLinus Torvalds 19721da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode, 19731da177e4SLinus Torvalds struct nameidata *nd) 19741da177e4SLinus Torvalds { 1975a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 19761da177e4SLinus Torvalds 19771da177e4SLinus Torvalds if (error) 19781da177e4SLinus Torvalds return error; 19791da177e4SLinus Torvalds 1980acfa4380SAl Viro if (!dir->i_op->create) 19811da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 19821da177e4SLinus Torvalds mode &= S_IALLUGO; 19831da177e4SLinus Torvalds mode |= S_IFREG; 19841da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 19851da177e4SLinus Torvalds if (error) 19861da177e4SLinus Torvalds return error; 19871da177e4SLinus Torvalds error = dir->i_op->create(dir, dentry, mode, nd); 1988a74574aaSStephen Smalley if (!error) 1989f38aa942SAmy Griffis fsnotify_create(dir, dentry); 19901da177e4SLinus Torvalds return error; 19911da177e4SLinus Torvalds } 19921da177e4SLinus Torvalds 19933fb64190SChristoph Hellwig int may_open(struct path *path, int acc_mode, int flag) 19941da177e4SLinus Torvalds { 19953fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 19961da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 19971da177e4SLinus Torvalds int error; 19981da177e4SLinus Torvalds 19991da177e4SLinus Torvalds if (!inode) 20001da177e4SLinus Torvalds return -ENOENT; 20011da177e4SLinus Torvalds 2002c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2003c8fe8f30SChristoph Hellwig case S_IFLNK: 20041da177e4SLinus Torvalds return -ELOOP; 2005c8fe8f30SChristoph Hellwig case S_IFDIR: 2006c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 20071da177e4SLinus Torvalds return -EISDIR; 2008c8fe8f30SChristoph Hellwig break; 2009c8fe8f30SChristoph Hellwig case S_IFBLK: 2010c8fe8f30SChristoph Hellwig case S_IFCHR: 20113fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 20121da177e4SLinus Torvalds return -EACCES; 2013c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 2014c8fe8f30SChristoph Hellwig case S_IFIFO: 2015c8fe8f30SChristoph Hellwig case S_IFSOCK: 20161da177e4SLinus Torvalds flag &= ~O_TRUNC; 2017c8fe8f30SChristoph Hellwig break; 20184a3fd211SDave Hansen } 2019b41572e9SDave Hansen 20203fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 2021b41572e9SDave Hansen if (error) 2022b41572e9SDave Hansen return error; 20236146f0d5SMimi Zohar 20241da177e4SLinus Torvalds /* 20251da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 20261da177e4SLinus Torvalds */ 20271da177e4SLinus Torvalds if (IS_APPEND(inode)) { 20288737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 20297715b521SAl Viro return -EPERM; 20301da177e4SLinus Torvalds if (flag & O_TRUNC) 20317715b521SAl Viro return -EPERM; 20321da177e4SLinus Torvalds } 20331da177e4SLinus Torvalds 20341da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 20357715b521SAl Viro if (flag & O_NOATIME && !is_owner_or_cap(inode)) 20367715b521SAl Viro return -EPERM; 20371da177e4SLinus Torvalds 20381da177e4SLinus Torvalds /* 20391da177e4SLinus Torvalds * Ensure there are no outstanding leases on the file. 20401da177e4SLinus Torvalds */ 2041b65a9cfcSAl Viro return break_lease(inode, flag); 20427715b521SAl Viro } 20437715b521SAl Viro 2044e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 20457715b521SAl Viro { 2046e1181ee6SJeff Layton struct path *path = &filp->f_path; 20477715b521SAl Viro struct inode *inode = path->dentry->d_inode; 20487715b521SAl Viro int error = get_write_access(inode); 20491da177e4SLinus Torvalds if (error) 20507715b521SAl Viro return error; 20511da177e4SLinus Torvalds /* 20521da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 20531da177e4SLinus Torvalds */ 20541da177e4SLinus Torvalds error = locks_verify_locked(inode); 2055be6d3e56SKentaro Takeda if (!error) 2056ea0d3ab2STetsuo Handa error = security_path_truncate(path); 20571da177e4SLinus Torvalds if (!error) { 20587715b521SAl Viro error = do_truncate(path->dentry, 0, 2059d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 2060e1181ee6SJeff Layton filp); 20611da177e4SLinus Torvalds } 20621da177e4SLinus Torvalds put_write_access(inode); 2063acd0c935SMimi Zohar return error; 20641da177e4SLinus Torvalds } 20651da177e4SLinus Torvalds 2066d57999e1SDave Hansen /* 2067d57999e1SDave Hansen * Be careful about ever adding any more callers of this 2068d57999e1SDave Hansen * function. Its flags must be in the namei format, not 2069d57999e1SDave Hansen * what get passed to sys_open(). 2070d57999e1SDave Hansen */ 2071d57999e1SDave Hansen static int __open_namei_create(struct nameidata *nd, struct path *path, 20728737c930SAl Viro int open_flag, int mode) 2073aab520e2SDave Hansen { 2074aab520e2SDave Hansen int error; 20754ac91378SJan Blunck struct dentry *dir = nd->path.dentry; 2076aab520e2SDave Hansen 2077aab520e2SDave Hansen if (!IS_POSIXACL(dir->d_inode)) 2078ce3b0f8dSAl Viro mode &= ~current_umask(); 2079be6d3e56SKentaro Takeda error = security_path_mknod(&nd->path, path->dentry, mode, 0); 2080be6d3e56SKentaro Takeda if (error) 2081be6d3e56SKentaro Takeda goto out_unlock; 2082aab520e2SDave Hansen error = vfs_create(dir->d_inode, path->dentry, mode, nd); 2083be6d3e56SKentaro Takeda out_unlock: 2084aab520e2SDave Hansen mutex_unlock(&dir->d_inode->i_mutex); 20854ac91378SJan Blunck dput(nd->path.dentry); 20864ac91378SJan Blunck nd->path.dentry = path->dentry; 208731e6b01fSNick Piggin 2088aab520e2SDave Hansen if (error) 2089aab520e2SDave Hansen return error; 2090aab520e2SDave Hansen /* Don't check for write permission, don't truncate */ 20918737c930SAl Viro return may_open(&nd->path, 0, open_flag & ~O_TRUNC); 2092aab520e2SDave Hansen } 2093aab520e2SDave Hansen 20941da177e4SLinus Torvalds /* 2095d57999e1SDave Hansen * Note that while the flag value (low two bits) for sys_open means: 2096d57999e1SDave Hansen * 00 - read-only 2097d57999e1SDave Hansen * 01 - write-only 2098d57999e1SDave Hansen * 10 - read-write 2099d57999e1SDave Hansen * 11 - special 2100d57999e1SDave Hansen * it is changed into 2101d57999e1SDave Hansen * 00 - no permissions needed 2102d57999e1SDave Hansen * 01 - read-permission 2103d57999e1SDave Hansen * 10 - write-permission 2104d57999e1SDave Hansen * 11 - read-write 2105d57999e1SDave Hansen * for the internal routines (ie open_namei()/follow_link() etc) 2106d57999e1SDave Hansen * This is more logical, and also allows the 00 "no perm needed" 2107d57999e1SDave Hansen * to be used for symlinks (where the permissions are checked 2108d57999e1SDave Hansen * later). 2109d57999e1SDave Hansen * 2110d57999e1SDave Hansen */ 2111d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2112d57999e1SDave Hansen { 2113d57999e1SDave Hansen if ((flag+1) & O_ACCMODE) 2114d57999e1SDave Hansen flag++; 2115d57999e1SDave Hansen return flag; 2116d57999e1SDave Hansen } 2117d57999e1SDave Hansen 21187715b521SAl Viro static int open_will_truncate(int flag, struct inode *inode) 21194a3fd211SDave Hansen { 2120d57999e1SDave Hansen /* 21214a3fd211SDave Hansen * We'll never write to the fs underlying 21224a3fd211SDave Hansen * a device file. 21234a3fd211SDave Hansen */ 21244a3fd211SDave Hansen if (special_file(inode->i_mode)) 21254a3fd211SDave Hansen return 0; 21264a3fd211SDave Hansen return (flag & O_TRUNC); 21274a3fd211SDave Hansen } 21284a3fd211SDave Hansen 2129648fa861SAl Viro static struct file *finish_open(struct nameidata *nd, 21309a66179eSAl Viro int open_flag, int acc_mode) 2131648fa861SAl Viro { 2132648fa861SAl Viro struct file *filp; 2133648fa861SAl Viro int will_truncate; 2134648fa861SAl Viro int error; 2135648fa861SAl Viro 21369a66179eSAl Viro will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode); 2137648fa861SAl Viro if (will_truncate) { 2138648fa861SAl Viro error = mnt_want_write(nd->path.mnt); 2139648fa861SAl Viro if (error) 2140648fa861SAl Viro goto exit; 2141648fa861SAl Viro } 2142648fa861SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2143648fa861SAl Viro if (error) { 2144648fa861SAl Viro if (will_truncate) 2145648fa861SAl Viro mnt_drop_write(nd->path.mnt); 2146648fa861SAl Viro goto exit; 2147648fa861SAl Viro } 2148648fa861SAl Viro filp = nameidata_to_filp(nd); 2149648fa861SAl Viro if (!IS_ERR(filp)) { 2150648fa861SAl Viro error = ima_file_check(filp, acc_mode); 2151648fa861SAl Viro if (error) { 2152648fa861SAl Viro fput(filp); 2153648fa861SAl Viro filp = ERR_PTR(error); 2154648fa861SAl Viro } 2155648fa861SAl Viro } 2156648fa861SAl Viro if (!IS_ERR(filp)) { 2157648fa861SAl Viro if (will_truncate) { 2158e1181ee6SJeff Layton error = handle_truncate(filp); 2159648fa861SAl Viro if (error) { 2160648fa861SAl Viro fput(filp); 2161648fa861SAl Viro filp = ERR_PTR(error); 2162648fa861SAl Viro } 2163648fa861SAl Viro } 2164648fa861SAl Viro } 2165648fa861SAl Viro /* 2166648fa861SAl Viro * It is now safe to drop the mnt write 2167648fa861SAl Viro * because the filp has had a write taken 2168648fa861SAl Viro * on its behalf. 2169648fa861SAl Viro */ 2170648fa861SAl Viro if (will_truncate) 2171648fa861SAl Viro mnt_drop_write(nd->path.mnt); 2172d893f1bcSAl Viro path_put(&nd->path); 2173648fa861SAl Viro return filp; 2174648fa861SAl Viro 2175648fa861SAl Viro exit: 2176648fa861SAl Viro path_put(&nd->path); 2177648fa861SAl Viro return ERR_PTR(error); 2178648fa861SAl Viro } 2179648fa861SAl Viro 218031e6b01fSNick Piggin /* 2181fe2d35ffSAl Viro * Handle the last step of open() 218231e6b01fSNick Piggin */ 2183fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path, 2184c3e380b0SAl Viro const struct open_flags *op, const char *pathname) 2185fb1cc555SAl Viro { 2186a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 2187fb1cc555SAl Viro struct file *filp; 2188fe2d35ffSAl Viro struct inode *inode; 218916c2cd71SAl Viro int error; 2190fb1cc555SAl Viro 2191c3e380b0SAl Viro nd->flags &= ~LOOKUP_PARENT; 2192c3e380b0SAl Viro nd->flags |= op->intent; 2193c3e380b0SAl Viro 21941f36f774SAl Viro switch (nd->last_type) { 21951f36f774SAl Viro case LAST_DOTDOT: 2196176306f5SNeil Brown case LAST_DOT: 2197fe2d35ffSAl Viro error = handle_dots(nd, nd->last_type); 2198fe2d35ffSAl Viro if (error) 2199fe2d35ffSAl Viro return ERR_PTR(error); 22001f36f774SAl Viro /* fallthrough */ 22011f36f774SAl Viro case LAST_ROOT: 2202fe2d35ffSAl Viro if (nd->flags & LOOKUP_RCU) { 2203fe2d35ffSAl Viro if (nameidata_drop_rcu_last(nd)) 2204fe2d35ffSAl Viro return ERR_PTR(-ECHILD); 2205fe2d35ffSAl Viro } 220616c2cd71SAl Viro error = handle_reval_path(nd); 220716c2cd71SAl Viro if (error) 220816c2cd71SAl Viro goto exit; 2209fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2210fe2d35ffSAl Viro if (op->open_flag & O_CREAT) { 221116c2cd71SAl Viro error = -EISDIR; 22121f36f774SAl Viro goto exit; 2213fe2d35ffSAl Viro } 2214fe2d35ffSAl Viro goto ok; 22151f36f774SAl Viro case LAST_BIND: 2216fe2d35ffSAl Viro /* can't be RCU mode here */ 221716c2cd71SAl Viro error = handle_reval_path(nd); 221816c2cd71SAl Viro if (error) 221916c2cd71SAl Viro goto exit; 22201f36f774SAl Viro audit_inode(pathname, dir); 22211f36f774SAl Viro goto ok; 22221f36f774SAl Viro } 2223a2c36b45SAl Viro 2224fe2d35ffSAl Viro if (!(op->open_flag & O_CREAT)) { 2225fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 2226fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2227fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 2228fe2d35ffSAl Viro error = do_lookup(nd, &nd->last, path, &inode); 2229fe2d35ffSAl Viro if (error) { 2230fe2d35ffSAl Viro terminate_walk(nd); 2231fe2d35ffSAl Viro return ERR_PTR(error); 2232fe2d35ffSAl Viro } 2233fe2d35ffSAl Viro if (!inode) { 2234fe2d35ffSAl Viro path_to_nameidata(path, nd); 2235fe2d35ffSAl Viro terminate_walk(nd); 2236fe2d35ffSAl Viro return ERR_PTR(-ENOENT); 2237fe2d35ffSAl Viro } 2238fe2d35ffSAl Viro if (unlikely(inode->i_op->follow_link)) { 2239fe2d35ffSAl Viro /* We drop rcu-walk here */ 2240fe2d35ffSAl Viro if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry)) 2241fe2d35ffSAl Viro return ERR_PTR(-ECHILD); 2242fe2d35ffSAl Viro return NULL; 2243fe2d35ffSAl Viro } 2244fe2d35ffSAl Viro path_to_nameidata(path, nd); 2245fe2d35ffSAl Viro nd->inode = inode; 2246fe2d35ffSAl Viro /* sayonara */ 2247fe2d35ffSAl Viro if (nd->flags & LOOKUP_RCU) { 2248fe2d35ffSAl Viro if (nameidata_drop_rcu_last(nd)) 2249fe2d35ffSAl Viro return ERR_PTR(-ECHILD); 2250fe2d35ffSAl Viro } 2251fe2d35ffSAl Viro 2252fe2d35ffSAl Viro error = -ENOTDIR; 2253fe2d35ffSAl Viro if (nd->flags & LOOKUP_DIRECTORY) { 2254fe2d35ffSAl Viro if (!inode->i_op->lookup) 2255fe2d35ffSAl Viro goto exit; 2256fe2d35ffSAl Viro } 2257fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2258fe2d35ffSAl Viro goto ok; 2259fe2d35ffSAl Viro } 2260fe2d35ffSAl Viro 2261fe2d35ffSAl Viro /* create side of things */ 2262fe2d35ffSAl Viro 2263fe2d35ffSAl Viro if (nd->flags & LOOKUP_RCU) { 2264fe2d35ffSAl Viro if (nameidata_drop_rcu_last(nd)) 2265fe2d35ffSAl Viro return ERR_PTR(-ECHILD); 2266fe2d35ffSAl Viro } 2267fe2d35ffSAl Viro 2268fe2d35ffSAl Viro audit_inode(pathname, dir); 226916c2cd71SAl Viro error = -EISDIR; 22701f36f774SAl Viro /* trailing slashes? */ 227131e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 22721f36f774SAl Viro goto exit; 22731f36f774SAl Viro 2274a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 2275a1e28038SAl Viro 2276a1e28038SAl Viro path->dentry = lookup_hash(nd); 2277a1e28038SAl Viro path->mnt = nd->path.mnt; 2278a1e28038SAl Viro 2279fb1cc555SAl Viro error = PTR_ERR(path->dentry); 2280fb1cc555SAl Viro if (IS_ERR(path->dentry)) { 2281fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2282fb1cc555SAl Viro goto exit; 2283fb1cc555SAl Viro } 2284fb1cc555SAl Viro 2285fb1cc555SAl Viro if (IS_ERR(nd->intent.open.file)) { 2286fb1cc555SAl Viro error = PTR_ERR(nd->intent.open.file); 2287fb1cc555SAl Viro goto exit_mutex_unlock; 2288fb1cc555SAl Viro } 2289fb1cc555SAl Viro 2290fb1cc555SAl Viro /* Negative dentry, just create the file */ 2291fb1cc555SAl Viro if (!path->dentry->d_inode) { 2292fb1cc555SAl Viro /* 2293fb1cc555SAl Viro * This write is needed to ensure that a 2294fb1cc555SAl Viro * ro->rw transition does not occur between 2295fb1cc555SAl Viro * the time when the file is created and when 2296fb1cc555SAl Viro * a permanent write count is taken through 2297fb1cc555SAl Viro * the 'struct file' in nameidata_to_filp(). 2298fb1cc555SAl Viro */ 2299fb1cc555SAl Viro error = mnt_want_write(nd->path.mnt); 2300fb1cc555SAl Viro if (error) 2301fb1cc555SAl Viro goto exit_mutex_unlock; 2302c3e380b0SAl Viro error = __open_namei_create(nd, path, op->open_flag, op->mode); 2303fb1cc555SAl Viro if (error) { 2304fb1cc555SAl Viro mnt_drop_write(nd->path.mnt); 2305fb1cc555SAl Viro goto exit; 2306fb1cc555SAl Viro } 2307fb1cc555SAl Viro filp = nameidata_to_filp(nd); 2308fb1cc555SAl Viro mnt_drop_write(nd->path.mnt); 2309d893f1bcSAl Viro path_put(&nd->path); 2310fb1cc555SAl Viro if (!IS_ERR(filp)) { 2311c3e380b0SAl Viro error = ima_file_check(filp, op->acc_mode); 2312fb1cc555SAl Viro if (error) { 2313fb1cc555SAl Viro fput(filp); 2314fb1cc555SAl Viro filp = ERR_PTR(error); 2315fb1cc555SAl Viro } 2316fb1cc555SAl Viro } 2317fb1cc555SAl Viro return filp; 2318fb1cc555SAl Viro } 2319fb1cc555SAl Viro 2320fb1cc555SAl Viro /* 2321fb1cc555SAl Viro * It already exists. 2322fb1cc555SAl Viro */ 2323fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2324fb1cc555SAl Viro audit_inode(pathname, path->dentry); 2325fb1cc555SAl Viro 2326fb1cc555SAl Viro error = -EEXIST; 2327c3e380b0SAl Viro if (op->open_flag & O_EXCL) 2328fb1cc555SAl Viro goto exit_dput; 2329fb1cc555SAl Viro 23309875cf80SDavid Howells error = follow_managed(path, nd->flags); 23319875cf80SDavid Howells if (error < 0) 2332fb1cc555SAl Viro goto exit_dput; 2333fb1cc555SAl Viro 2334fb1cc555SAl Viro error = -ENOENT; 2335fb1cc555SAl Viro if (!path->dentry->d_inode) 2336fb1cc555SAl Viro goto exit_dput; 23379e67f361SAl Viro 23389e67f361SAl Viro if (path->dentry->d_inode->i_op->follow_link) 2339fb1cc555SAl Viro return NULL; 2340fb1cc555SAl Viro 2341fb1cc555SAl Viro path_to_nameidata(path, nd); 234231e6b01fSNick Piggin nd->inode = path->dentry->d_inode; 2343fb1cc555SAl Viro error = -EISDIR; 234431e6b01fSNick Piggin if (S_ISDIR(nd->inode->i_mode)) 2345fb1cc555SAl Viro goto exit; 234667ee3ad2SAl Viro ok: 2347c3e380b0SAl Viro filp = finish_open(nd, op->open_flag, op->acc_mode); 2348fb1cc555SAl Viro return filp; 2349fb1cc555SAl Viro 2350fb1cc555SAl Viro exit_mutex_unlock: 2351fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2352fb1cc555SAl Viro exit_dput: 2353fb1cc555SAl Viro path_put_conditional(path, nd); 2354fb1cc555SAl Viro exit: 2355fb1cc555SAl Viro path_put(&nd->path); 2356fb1cc555SAl Viro return ERR_PTR(error); 2357fb1cc555SAl Viro } 2358fb1cc555SAl Viro 235913aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname, 236047c805dcSAl Viro const struct open_flags *op, int flags) 23611da177e4SLinus Torvalds { 2362fe2d35ffSAl Viro struct file *base = NULL; 23634a3fd211SDave Hansen struct file *filp; 2364a70e65dfSChristoph Hellwig struct nameidata nd; 23659850c056SAl Viro struct path path; 23661da177e4SLinus Torvalds int count = 0; 236713aab428SAl Viro int error; 236831e6b01fSNick Piggin 236931e6b01fSNick Piggin filp = get_empty_filp(); 237031e6b01fSNick Piggin if (!filp) 237131e6b01fSNick Piggin return ERR_PTR(-ENFILE); 237231e6b01fSNick Piggin 237347c805dcSAl Viro filp->f_flags = op->open_flag; 237431e6b01fSNick Piggin nd.intent.open.file = filp; 237547c805dcSAl Viro nd.intent.open.flags = open_to_namei_flags(op->open_flag); 237647c805dcSAl Viro nd.intent.open.create_mode = op->mode; 237731e6b01fSNick Piggin 2378fe2d35ffSAl Viro error = path_init(dfd, pathname, flags | LOOKUP_PARENT, &nd, &base); 237931e6b01fSNick Piggin if (unlikely(error)) 238013aab428SAl Viro goto out_filp; 238131e6b01fSNick Piggin 2382fe2d35ffSAl Viro current->total_link_count = 0; 2383fe2d35ffSAl Viro error = link_path_walk(pathname, &nd); 238431e6b01fSNick Piggin if (unlikely(error)) 238531e6b01fSNick Piggin goto out_filp; 23861da177e4SLinus Torvalds 238747c805dcSAl Viro filp = do_last(&nd, &path, op, pathname); 2388806b681cSAl Viro while (unlikely(!filp)) { /* trailing symlink */ 23897b9337aaSNick Piggin struct path link = path; 23907b9337aaSNick Piggin struct inode *linki = link.dentry->d_inode; 2391def4af30SAl Viro void *cookie; 2392806b681cSAl Viro error = -ELOOP; 2393db372915SDavid Howells if (!(nd.flags & LOOKUP_FOLLOW)) 23941f36f774SAl Viro goto exit_dput; 23951f36f774SAl Viro if (count++ == 32) 2396806b681cSAl Viro goto exit_dput; 2397806b681cSAl Viro /* 2398806b681cSAl Viro * This is subtle. Instead of calling do_follow_link() we do 2399806b681cSAl Viro * the thing by hands. The reason is that this way we have zero 2400806b681cSAl Viro * link_count and path_walk() (called from ->follow_link) 2401806b681cSAl Viro * honoring LOOKUP_PARENT. After that we have the parent and 2402806b681cSAl Viro * last component, i.e. we are in the same situation as after 2403806b681cSAl Viro * the first path_walk(). Well, almost - if the last component 2404806b681cSAl Viro * is normal we get its copy stored in nd->last.name and we will 2405806b681cSAl Viro * have to putname() it when we are done. Procfs-like symlinks 2406806b681cSAl Viro * just set LAST_BIND. 2407806b681cSAl Viro */ 2408806b681cSAl Viro nd.flags |= LOOKUP_PARENT; 2409c3e380b0SAl Viro nd.flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 24107b9337aaSNick Piggin error = __do_follow_link(&link, &nd, &cookie); 2411c3e380b0SAl Viro if (unlikely(error)) 2412f1afe9efSAl Viro filp = ERR_PTR(error); 2413c3e380b0SAl Viro else 241447c805dcSAl Viro filp = do_last(&nd, &path, op, pathname); 2415f1afe9efSAl Viro if (!IS_ERR(cookie) && linki->i_op->put_link) 24167b9337aaSNick Piggin linki->i_op->put_link(link.dentry, &nd, cookie); 24177b9337aaSNick Piggin path_put(&link); 2418806b681cSAl Viro } 241910fa8e62SAl Viro out: 24202a737871SAl Viro if (nd.root.mnt) 24212a737871SAl Viro path_put(&nd.root); 2422fe2d35ffSAl Viro if (base) 2423fe2d35ffSAl Viro fput(base); 24242dab5974SLinus Torvalds release_open_intent(&nd); 242510fa8e62SAl Viro return filp; 24261da177e4SLinus Torvalds 2427806b681cSAl Viro exit_dput: 2428806b681cSAl Viro path_put_conditional(&path, &nd); 242931e6b01fSNick Piggin path_put(&nd.path); 243031e6b01fSNick Piggin out_filp: 243110fa8e62SAl Viro filp = ERR_PTR(error); 243210fa8e62SAl Viro goto out; 2433de459215SKirill Korotaev } 24341da177e4SLinus Torvalds 243513aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname, 243613aab428SAl Viro const struct open_flags *op, int flags) 243713aab428SAl Viro { 243813aab428SAl Viro struct file *filp; 243913aab428SAl Viro 244013aab428SAl Viro filp = path_openat(dfd, pathname, op, flags | LOOKUP_RCU); 244113aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 244213aab428SAl Viro filp = path_openat(dfd, pathname, op, flags); 244313aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 244413aab428SAl Viro filp = path_openat(dfd, pathname, op, flags | LOOKUP_REVAL); 244513aab428SAl Viro return filp; 244613aab428SAl Viro } 244713aab428SAl Viro 24481da177e4SLinus Torvalds /** 24491da177e4SLinus Torvalds * lookup_create - lookup a dentry, creating it if it doesn't exist 24501da177e4SLinus Torvalds * @nd: nameidata info 24511da177e4SLinus Torvalds * @is_dir: directory flag 24521da177e4SLinus Torvalds * 24531da177e4SLinus Torvalds * Simple function to lookup and return a dentry and create it 24541da177e4SLinus Torvalds * if it doesn't exist. Is SMP-safe. 2455c663e5d8SChristoph Hellwig * 24564ac91378SJan Blunck * Returns with nd->path.dentry->d_inode->i_mutex locked. 24571da177e4SLinus Torvalds */ 24581da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir) 24591da177e4SLinus Torvalds { 2460c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 24611da177e4SLinus Torvalds 24624ac91378SJan Blunck mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 2463c663e5d8SChristoph Hellwig /* 2464c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 2465c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 2466c663e5d8SChristoph Hellwig */ 24671da177e4SLinus Torvalds if (nd->last_type != LAST_NORM) 24681da177e4SLinus Torvalds goto fail; 24691da177e4SLinus Torvalds nd->flags &= ~LOOKUP_PARENT; 24703516586aSAl Viro nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL; 2471a634904aSASANO Masahiro nd->intent.open.flags = O_EXCL; 2472c663e5d8SChristoph Hellwig 2473c663e5d8SChristoph Hellwig /* 2474c663e5d8SChristoph Hellwig * Do the final lookup. 2475c663e5d8SChristoph Hellwig */ 247649705b77SChristoph Hellwig dentry = lookup_hash(nd); 24771da177e4SLinus Torvalds if (IS_ERR(dentry)) 24781da177e4SLinus Torvalds goto fail; 2479c663e5d8SChristoph Hellwig 2480e9baf6e5SAl Viro if (dentry->d_inode) 2481e9baf6e5SAl Viro goto eexist; 2482c663e5d8SChristoph Hellwig /* 2483c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 2484c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 2485c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 2486c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 2487c663e5d8SChristoph Hellwig */ 2488e9baf6e5SAl Viro if (unlikely(!is_dir && nd->last.name[nd->last.len])) { 24891da177e4SLinus Torvalds dput(dentry); 24901da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 2491e9baf6e5SAl Viro } 2492e9baf6e5SAl Viro return dentry; 2493e9baf6e5SAl Viro eexist: 2494e9baf6e5SAl Viro dput(dentry); 2495e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 24961da177e4SLinus Torvalds fail: 24971da177e4SLinus Torvalds return dentry; 24981da177e4SLinus Torvalds } 2499f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create); 25001da177e4SLinus Torvalds 25011da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 25021da177e4SLinus Torvalds { 2503a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 25041da177e4SLinus Torvalds 25051da177e4SLinus Torvalds if (error) 25061da177e4SLinus Torvalds return error; 25071da177e4SLinus Torvalds 25081da177e4SLinus Torvalds if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) 25091da177e4SLinus Torvalds return -EPERM; 25101da177e4SLinus Torvalds 2511acfa4380SAl Viro if (!dir->i_op->mknod) 25121da177e4SLinus Torvalds return -EPERM; 25131da177e4SLinus Torvalds 251408ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 251508ce5f16SSerge E. Hallyn if (error) 251608ce5f16SSerge E. Hallyn return error; 251708ce5f16SSerge E. Hallyn 25181da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 25191da177e4SLinus Torvalds if (error) 25201da177e4SLinus Torvalds return error; 25211da177e4SLinus Torvalds 25221da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 2523a74574aaSStephen Smalley if (!error) 2524f38aa942SAmy Griffis fsnotify_create(dir, dentry); 25251da177e4SLinus Torvalds return error; 25261da177e4SLinus Torvalds } 25271da177e4SLinus Torvalds 2528463c3197SDave Hansen static int may_mknod(mode_t mode) 2529463c3197SDave Hansen { 2530463c3197SDave Hansen switch (mode & S_IFMT) { 2531463c3197SDave Hansen case S_IFREG: 2532463c3197SDave Hansen case S_IFCHR: 2533463c3197SDave Hansen case S_IFBLK: 2534463c3197SDave Hansen case S_IFIFO: 2535463c3197SDave Hansen case S_IFSOCK: 2536463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2537463c3197SDave Hansen return 0; 2538463c3197SDave Hansen case S_IFDIR: 2539463c3197SDave Hansen return -EPERM; 2540463c3197SDave Hansen default: 2541463c3197SDave Hansen return -EINVAL; 2542463c3197SDave Hansen } 2543463c3197SDave Hansen } 2544463c3197SDave Hansen 25452e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode, 25462e4d0924SHeiko Carstens unsigned, dev) 25471da177e4SLinus Torvalds { 25482ad94ae6SAl Viro int error; 25491da177e4SLinus Torvalds char *tmp; 25501da177e4SLinus Torvalds struct dentry *dentry; 25511da177e4SLinus Torvalds struct nameidata nd; 25521da177e4SLinus Torvalds 25531da177e4SLinus Torvalds if (S_ISDIR(mode)) 25541da177e4SLinus Torvalds return -EPERM; 25551da177e4SLinus Torvalds 25562ad94ae6SAl Viro error = user_path_parent(dfd, filename, &nd, &tmp); 25571da177e4SLinus Torvalds if (error) 25582ad94ae6SAl Viro return error; 25592ad94ae6SAl Viro 25601da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 2561463c3197SDave Hansen if (IS_ERR(dentry)) { 25621da177e4SLinus Torvalds error = PTR_ERR(dentry); 2563463c3197SDave Hansen goto out_unlock; 2564463c3197SDave Hansen } 25654ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2566ce3b0f8dSAl Viro mode &= ~current_umask(); 2567463c3197SDave Hansen error = may_mknod(mode); 2568463c3197SDave Hansen if (error) 2569463c3197SDave Hansen goto out_dput; 2570463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2571463c3197SDave Hansen if (error) 2572463c3197SDave Hansen goto out_dput; 2573be6d3e56SKentaro Takeda error = security_path_mknod(&nd.path, dentry, mode, dev); 2574be6d3e56SKentaro Takeda if (error) 2575be6d3e56SKentaro Takeda goto out_drop_write; 25761da177e4SLinus Torvalds switch (mode & S_IFMT) { 25771da177e4SLinus Torvalds case 0: case S_IFREG: 25784ac91378SJan Blunck error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd); 25791da177e4SLinus Torvalds break; 25801da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 25814ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode, 25821da177e4SLinus Torvalds new_decode_dev(dev)); 25831da177e4SLinus Torvalds break; 25841da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 25854ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0); 25861da177e4SLinus Torvalds break; 25871da177e4SLinus Torvalds } 2588be6d3e56SKentaro Takeda out_drop_write: 2589463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2590463c3197SDave Hansen out_dput: 25911da177e4SLinus Torvalds dput(dentry); 2592463c3197SDave Hansen out_unlock: 25934ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 25941d957f9bSJan Blunck path_put(&nd.path); 25951da177e4SLinus Torvalds putname(tmp); 25961da177e4SLinus Torvalds 25971da177e4SLinus Torvalds return error; 25981da177e4SLinus Torvalds } 25991da177e4SLinus Torvalds 26003480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev) 26015590ff0dSUlrich Drepper { 26025590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 26035590ff0dSUlrich Drepper } 26045590ff0dSUlrich Drepper 26051da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 26061da177e4SLinus Torvalds { 2607a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 26081da177e4SLinus Torvalds 26091da177e4SLinus Torvalds if (error) 26101da177e4SLinus Torvalds return error; 26111da177e4SLinus Torvalds 2612acfa4380SAl Viro if (!dir->i_op->mkdir) 26131da177e4SLinus Torvalds return -EPERM; 26141da177e4SLinus Torvalds 26151da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 26161da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 26171da177e4SLinus Torvalds if (error) 26181da177e4SLinus Torvalds return error; 26191da177e4SLinus Torvalds 26201da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2621a74574aaSStephen Smalley if (!error) 2622f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 26231da177e4SLinus Torvalds return error; 26241da177e4SLinus Torvalds } 26251da177e4SLinus Torvalds 26262e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode) 26271da177e4SLinus Torvalds { 26281da177e4SLinus Torvalds int error = 0; 26291da177e4SLinus Torvalds char * tmp; 26306902d925SDave Hansen struct dentry *dentry; 26316902d925SDave Hansen struct nameidata nd; 26321da177e4SLinus Torvalds 26332ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &tmp); 26342ad94ae6SAl Viro if (error) 26356902d925SDave Hansen goto out_err; 26361da177e4SLinus Torvalds 26371da177e4SLinus Torvalds dentry = lookup_create(&nd, 1); 26381da177e4SLinus Torvalds error = PTR_ERR(dentry); 26396902d925SDave Hansen if (IS_ERR(dentry)) 26406902d925SDave Hansen goto out_unlock; 26416902d925SDave Hansen 26424ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2643ce3b0f8dSAl Viro mode &= ~current_umask(); 2644463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2645463c3197SDave Hansen if (error) 2646463c3197SDave Hansen goto out_dput; 2647be6d3e56SKentaro Takeda error = security_path_mkdir(&nd.path, dentry, mode); 2648be6d3e56SKentaro Takeda if (error) 2649be6d3e56SKentaro Takeda goto out_drop_write; 26504ac91378SJan Blunck error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode); 2651be6d3e56SKentaro Takeda out_drop_write: 2652463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2653463c3197SDave Hansen out_dput: 26541da177e4SLinus Torvalds dput(dentry); 26556902d925SDave Hansen out_unlock: 26564ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 26571d957f9bSJan Blunck path_put(&nd.path); 26581da177e4SLinus Torvalds putname(tmp); 26596902d925SDave Hansen out_err: 26601da177e4SLinus Torvalds return error; 26611da177e4SLinus Torvalds } 26621da177e4SLinus Torvalds 26633cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode) 26645590ff0dSUlrich Drepper { 26655590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 26665590ff0dSUlrich Drepper } 26675590ff0dSUlrich Drepper 26681da177e4SLinus Torvalds /* 26691da177e4SLinus Torvalds * We try to drop the dentry early: we should have 26701da177e4SLinus Torvalds * a usage count of 2 if we're the only user of this 26711da177e4SLinus Torvalds * dentry, and if that is true (possibly after pruning 26721da177e4SLinus Torvalds * the dcache), then we drop the dentry now. 26731da177e4SLinus Torvalds * 26741da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 26751da177e4SLinus Torvalds * do a 26761da177e4SLinus Torvalds * 26771da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 26781da177e4SLinus Torvalds * return -EBUSY; 26791da177e4SLinus Torvalds * 26801da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 26811da177e4SLinus Torvalds * that is still in use by something else.. 26821da177e4SLinus Torvalds */ 26831da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 26841da177e4SLinus Torvalds { 26851da177e4SLinus Torvalds dget(dentry); 26861da177e4SLinus Torvalds shrink_dcache_parent(dentry); 26871da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 2688b7ab39f6SNick Piggin if (dentry->d_count == 2) 26891da177e4SLinus Torvalds __d_drop(dentry); 26901da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 26911da177e4SLinus Torvalds } 26921da177e4SLinus Torvalds 26931da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 26941da177e4SLinus Torvalds { 26951da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 26961da177e4SLinus Torvalds 26971da177e4SLinus Torvalds if (error) 26981da177e4SLinus Torvalds return error; 26991da177e4SLinus Torvalds 2700acfa4380SAl Viro if (!dir->i_op->rmdir) 27011da177e4SLinus Torvalds return -EPERM; 27021da177e4SLinus Torvalds 27031b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 27041da177e4SLinus Torvalds dentry_unhash(dentry); 27051da177e4SLinus Torvalds if (d_mountpoint(dentry)) 27061da177e4SLinus Torvalds error = -EBUSY; 27071da177e4SLinus Torvalds else { 27081da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 27091da177e4SLinus Torvalds if (!error) { 27101da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 2711d83c49f3SAl Viro if (!error) { 27121da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 2713d83c49f3SAl Viro dont_mount(dentry); 2714d83c49f3SAl Viro } 27151da177e4SLinus Torvalds } 27161da177e4SLinus Torvalds } 27171b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 27181da177e4SLinus Torvalds if (!error) { 27191da177e4SLinus Torvalds d_delete(dentry); 27201da177e4SLinus Torvalds } 27211da177e4SLinus Torvalds dput(dentry); 27221da177e4SLinus Torvalds 27231da177e4SLinus Torvalds return error; 27241da177e4SLinus Torvalds } 27251da177e4SLinus Torvalds 27265590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 27271da177e4SLinus Torvalds { 27281da177e4SLinus Torvalds int error = 0; 27291da177e4SLinus Torvalds char * name; 27301da177e4SLinus Torvalds struct dentry *dentry; 27311da177e4SLinus Torvalds struct nameidata nd; 27321da177e4SLinus Torvalds 27332ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 27341da177e4SLinus Torvalds if (error) 27352ad94ae6SAl Viro return error; 27361da177e4SLinus Torvalds 27371da177e4SLinus Torvalds switch(nd.last_type) { 27381da177e4SLinus Torvalds case LAST_DOTDOT: 27391da177e4SLinus Torvalds error = -ENOTEMPTY; 27401da177e4SLinus Torvalds goto exit1; 27411da177e4SLinus Torvalds case LAST_DOT: 27421da177e4SLinus Torvalds error = -EINVAL; 27431da177e4SLinus Torvalds goto exit1; 27441da177e4SLinus Torvalds case LAST_ROOT: 27451da177e4SLinus Torvalds error = -EBUSY; 27461da177e4SLinus Torvalds goto exit1; 27471da177e4SLinus Torvalds } 27480612d9fbSOGAWA Hirofumi 27490612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 27500612d9fbSOGAWA Hirofumi 27514ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 275249705b77SChristoph Hellwig dentry = lookup_hash(&nd); 27531da177e4SLinus Torvalds error = PTR_ERR(dentry); 27546902d925SDave Hansen if (IS_ERR(dentry)) 27556902d925SDave Hansen goto exit2; 27560622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 27570622753bSDave Hansen if (error) 27580622753bSDave Hansen goto exit3; 2759be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 2760be6d3e56SKentaro Takeda if (error) 2761be6d3e56SKentaro Takeda goto exit4; 27624ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 2763be6d3e56SKentaro Takeda exit4: 27640622753bSDave Hansen mnt_drop_write(nd.path.mnt); 27650622753bSDave Hansen exit3: 27661da177e4SLinus Torvalds dput(dentry); 27676902d925SDave Hansen exit2: 27684ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 27691da177e4SLinus Torvalds exit1: 27701d957f9bSJan Blunck path_put(&nd.path); 27711da177e4SLinus Torvalds putname(name); 27721da177e4SLinus Torvalds return error; 27731da177e4SLinus Torvalds } 27741da177e4SLinus Torvalds 27753cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 27765590ff0dSUlrich Drepper { 27775590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 27785590ff0dSUlrich Drepper } 27795590ff0dSUlrich Drepper 27801da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 27811da177e4SLinus Torvalds { 27821da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 27831da177e4SLinus Torvalds 27841da177e4SLinus Torvalds if (error) 27851da177e4SLinus Torvalds return error; 27861da177e4SLinus Torvalds 2787acfa4380SAl Viro if (!dir->i_op->unlink) 27881da177e4SLinus Torvalds return -EPERM; 27891da177e4SLinus Torvalds 27901b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 27911da177e4SLinus Torvalds if (d_mountpoint(dentry)) 27921da177e4SLinus Torvalds error = -EBUSY; 27931da177e4SLinus Torvalds else { 27941da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 2795bec1052eSAl Viro if (!error) { 27961da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 2797bec1052eSAl Viro if (!error) 2798d83c49f3SAl Viro dont_mount(dentry); 2799bec1052eSAl Viro } 28001da177e4SLinus Torvalds } 28011b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 28021da177e4SLinus Torvalds 28031da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 28041da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 2805ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 28061da177e4SLinus Torvalds d_delete(dentry); 28071da177e4SLinus Torvalds } 28080eeca283SRobert Love 28091da177e4SLinus Torvalds return error; 28101da177e4SLinus Torvalds } 28111da177e4SLinus Torvalds 28121da177e4SLinus Torvalds /* 28131da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 28141b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 28151da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 28161da177e4SLinus Torvalds * while waiting on the I/O. 28171da177e4SLinus Torvalds */ 28185590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 28191da177e4SLinus Torvalds { 28202ad94ae6SAl Viro int error; 28211da177e4SLinus Torvalds char *name; 28221da177e4SLinus Torvalds struct dentry *dentry; 28231da177e4SLinus Torvalds struct nameidata nd; 28241da177e4SLinus Torvalds struct inode *inode = NULL; 28251da177e4SLinus Torvalds 28262ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 28271da177e4SLinus Torvalds if (error) 28282ad94ae6SAl Viro return error; 28292ad94ae6SAl Viro 28301da177e4SLinus Torvalds error = -EISDIR; 28311da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 28321da177e4SLinus Torvalds goto exit1; 28330612d9fbSOGAWA Hirofumi 28340612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 28350612d9fbSOGAWA Hirofumi 28364ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 283749705b77SChristoph Hellwig dentry = lookup_hash(&nd); 28381da177e4SLinus Torvalds error = PTR_ERR(dentry); 28391da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 28401da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 28411da177e4SLinus Torvalds if (nd.last.name[nd.last.len]) 28421da177e4SLinus Torvalds goto slashes; 28431da177e4SLinus Torvalds inode = dentry->d_inode; 28441da177e4SLinus Torvalds if (inode) 28457de9c6eeSAl Viro ihold(inode); 28460622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 28470622753bSDave Hansen if (error) 28480622753bSDave Hansen goto exit2; 2849be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 2850be6d3e56SKentaro Takeda if (error) 2851be6d3e56SKentaro Takeda goto exit3; 28524ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 2853be6d3e56SKentaro Takeda exit3: 28540622753bSDave Hansen mnt_drop_write(nd.path.mnt); 28551da177e4SLinus Torvalds exit2: 28561da177e4SLinus Torvalds dput(dentry); 28571da177e4SLinus Torvalds } 28584ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 28591da177e4SLinus Torvalds if (inode) 28601da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 28611da177e4SLinus Torvalds exit1: 28621d957f9bSJan Blunck path_put(&nd.path); 28631da177e4SLinus Torvalds putname(name); 28641da177e4SLinus Torvalds return error; 28651da177e4SLinus Torvalds 28661da177e4SLinus Torvalds slashes: 28671da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 28681da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 28691da177e4SLinus Torvalds goto exit2; 28701da177e4SLinus Torvalds } 28711da177e4SLinus Torvalds 28722e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 28735590ff0dSUlrich Drepper { 28745590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 28755590ff0dSUlrich Drepper return -EINVAL; 28765590ff0dSUlrich Drepper 28775590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 28785590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 28795590ff0dSUlrich Drepper 28805590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 28815590ff0dSUlrich Drepper } 28825590ff0dSUlrich Drepper 28833480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 28845590ff0dSUlrich Drepper { 28855590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 28865590ff0dSUlrich Drepper } 28875590ff0dSUlrich Drepper 2888db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 28891da177e4SLinus Torvalds { 2890a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 28911da177e4SLinus Torvalds 28921da177e4SLinus Torvalds if (error) 28931da177e4SLinus Torvalds return error; 28941da177e4SLinus Torvalds 2895acfa4380SAl Viro if (!dir->i_op->symlink) 28961da177e4SLinus Torvalds return -EPERM; 28971da177e4SLinus Torvalds 28981da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 28991da177e4SLinus Torvalds if (error) 29001da177e4SLinus Torvalds return error; 29011da177e4SLinus Torvalds 29021da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 2903a74574aaSStephen Smalley if (!error) 2904f38aa942SAmy Griffis fsnotify_create(dir, dentry); 29051da177e4SLinus Torvalds return error; 29061da177e4SLinus Torvalds } 29071da177e4SLinus Torvalds 29082e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 29092e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 29101da177e4SLinus Torvalds { 29112ad94ae6SAl Viro int error; 29121da177e4SLinus Torvalds char *from; 29131da177e4SLinus Torvalds char *to; 29146902d925SDave Hansen struct dentry *dentry; 29156902d925SDave Hansen struct nameidata nd; 29161da177e4SLinus Torvalds 29171da177e4SLinus Torvalds from = getname(oldname); 29181da177e4SLinus Torvalds if (IS_ERR(from)) 29191da177e4SLinus Torvalds return PTR_ERR(from); 29202ad94ae6SAl Viro 29212ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 29222ad94ae6SAl Viro if (error) 29236902d925SDave Hansen goto out_putname; 29241da177e4SLinus Torvalds 29251da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 29261da177e4SLinus Torvalds error = PTR_ERR(dentry); 29276902d925SDave Hansen if (IS_ERR(dentry)) 29286902d925SDave Hansen goto out_unlock; 29296902d925SDave Hansen 293075c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 293175c3f29dSDave Hansen if (error) 293275c3f29dSDave Hansen goto out_dput; 2933be6d3e56SKentaro Takeda error = security_path_symlink(&nd.path, dentry, from); 2934be6d3e56SKentaro Takeda if (error) 2935be6d3e56SKentaro Takeda goto out_drop_write; 2936db2e747bSMiklos Szeredi error = vfs_symlink(nd.path.dentry->d_inode, dentry, from); 2937be6d3e56SKentaro Takeda out_drop_write: 293875c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 293975c3f29dSDave Hansen out_dput: 29401da177e4SLinus Torvalds dput(dentry); 29416902d925SDave Hansen out_unlock: 29424ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 29431d957f9bSJan Blunck path_put(&nd.path); 29441da177e4SLinus Torvalds putname(to); 29456902d925SDave Hansen out_putname: 29461da177e4SLinus Torvalds putname(from); 29471da177e4SLinus Torvalds return error; 29481da177e4SLinus Torvalds } 29491da177e4SLinus Torvalds 29503480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 29515590ff0dSUlrich Drepper { 29525590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 29535590ff0dSUlrich Drepper } 29545590ff0dSUlrich Drepper 29551da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 29561da177e4SLinus Torvalds { 29571da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 29581da177e4SLinus Torvalds int error; 29591da177e4SLinus Torvalds 29601da177e4SLinus Torvalds if (!inode) 29611da177e4SLinus Torvalds return -ENOENT; 29621da177e4SLinus Torvalds 2963a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 29641da177e4SLinus Torvalds if (error) 29651da177e4SLinus Torvalds return error; 29661da177e4SLinus Torvalds 29671da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 29681da177e4SLinus Torvalds return -EXDEV; 29691da177e4SLinus Torvalds 29701da177e4SLinus Torvalds /* 29711da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 29721da177e4SLinus Torvalds */ 29731da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 29741da177e4SLinus Torvalds return -EPERM; 2975acfa4380SAl Viro if (!dir->i_op->link) 29761da177e4SLinus Torvalds return -EPERM; 29777e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 29781da177e4SLinus Torvalds return -EPERM; 29791da177e4SLinus Torvalds 29801da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 29811da177e4SLinus Torvalds if (error) 29821da177e4SLinus Torvalds return error; 29831da177e4SLinus Torvalds 29847e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 29851da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 29867e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 2987e31e14ecSStephen Smalley if (!error) 29887e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 29891da177e4SLinus Torvalds return error; 29901da177e4SLinus Torvalds } 29911da177e4SLinus Torvalds 29921da177e4SLinus Torvalds /* 29931da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 29941da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 29951da177e4SLinus Torvalds * newname. --KAB 29961da177e4SLinus Torvalds * 29971da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 29981da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 29991da177e4SLinus Torvalds * and other special files. --ADM 30001da177e4SLinus Torvalds */ 30012e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 30022e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 30031da177e4SLinus Torvalds { 30041da177e4SLinus Torvalds struct dentry *new_dentry; 30052d8f3038SAl Viro struct nameidata nd; 30062d8f3038SAl Viro struct path old_path; 30071da177e4SLinus Torvalds int error; 30081da177e4SLinus Torvalds char *to; 30091da177e4SLinus Torvalds 301045c9b11aSUlrich Drepper if ((flags & ~AT_SYMLINK_FOLLOW) != 0) 3011c04030e1SUlrich Drepper return -EINVAL; 3012c04030e1SUlrich Drepper 30132d8f3038SAl Viro error = user_path_at(olddfd, oldname, 301445c9b11aSUlrich Drepper flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0, 30152d8f3038SAl Viro &old_path); 30161da177e4SLinus Torvalds if (error) 30172ad94ae6SAl Viro return error; 30182ad94ae6SAl Viro 30192ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 30201da177e4SLinus Torvalds if (error) 30211da177e4SLinus Torvalds goto out; 30221da177e4SLinus Torvalds error = -EXDEV; 30232d8f3038SAl Viro if (old_path.mnt != nd.path.mnt) 30241da177e4SLinus Torvalds goto out_release; 30251da177e4SLinus Torvalds new_dentry = lookup_create(&nd, 0); 30261da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 30276902d925SDave Hansen if (IS_ERR(new_dentry)) 30286902d925SDave Hansen goto out_unlock; 302975c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 303075c3f29dSDave Hansen if (error) 303175c3f29dSDave Hansen goto out_dput; 3032be6d3e56SKentaro Takeda error = security_path_link(old_path.dentry, &nd.path, new_dentry); 3033be6d3e56SKentaro Takeda if (error) 3034be6d3e56SKentaro Takeda goto out_drop_write; 30352d8f3038SAl Viro error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry); 3036be6d3e56SKentaro Takeda out_drop_write: 303775c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 303875c3f29dSDave Hansen out_dput: 30391da177e4SLinus Torvalds dput(new_dentry); 30406902d925SDave Hansen out_unlock: 30414ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 30421da177e4SLinus Torvalds out_release: 30431d957f9bSJan Blunck path_put(&nd.path); 30442ad94ae6SAl Viro putname(to); 30451da177e4SLinus Torvalds out: 30462d8f3038SAl Viro path_put(&old_path); 30471da177e4SLinus Torvalds 30481da177e4SLinus Torvalds return error; 30491da177e4SLinus Torvalds } 30501da177e4SLinus Torvalds 30513480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 30525590ff0dSUlrich Drepper { 3053c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 30545590ff0dSUlrich Drepper } 30555590ff0dSUlrich Drepper 30561da177e4SLinus Torvalds /* 30571da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 30581da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 30591da177e4SLinus Torvalds * Problems: 30601da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 30611da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 30621da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 3063a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 30641da177e4SLinus Torvalds * story. 30651da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 30661b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 30671da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 30681da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 3069a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 30701da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 30711da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 30721da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 3073a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 30741da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 30751da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 30761da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 30771da177e4SLinus Torvalds * d) some filesystems don't support opened-but-unlinked directories, 30781da177e4SLinus Torvalds * either because of layout or because they are not ready to deal with 30791da177e4SLinus Torvalds * all cases correctly. The latter will be fixed (taking this sort of 30801da177e4SLinus Torvalds * stuff into VFS), but the former is not going away. Solution: the same 30811da177e4SLinus Torvalds * trick as in rmdir(). 30821da177e4SLinus Torvalds * e) conversion from fhandle to dentry may come in the wrong moment - when 30831b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 30841da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 3085c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 30861da177e4SLinus Torvalds * locking]. 30871da177e4SLinus Torvalds */ 308875c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 30891da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30901da177e4SLinus Torvalds { 30911da177e4SLinus Torvalds int error = 0; 30921da177e4SLinus Torvalds struct inode *target; 30931da177e4SLinus Torvalds 30941da177e4SLinus Torvalds /* 30951da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 30961da177e4SLinus Torvalds * we'll need to flip '..'. 30971da177e4SLinus Torvalds */ 30981da177e4SLinus Torvalds if (new_dir != old_dir) { 3099f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 31001da177e4SLinus Torvalds if (error) 31011da177e4SLinus Torvalds return error; 31021da177e4SLinus Torvalds } 31031da177e4SLinus Torvalds 31041da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 31051da177e4SLinus Torvalds if (error) 31061da177e4SLinus Torvalds return error; 31071da177e4SLinus Torvalds 31081da177e4SLinus Torvalds target = new_dentry->d_inode; 3109d83c49f3SAl Viro if (target) 31101b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 31111da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 31121da177e4SLinus Torvalds error = -EBUSY; 3113d83c49f3SAl Viro else { 3114d83c49f3SAl Viro if (target) 3115d83c49f3SAl Viro dentry_unhash(new_dentry); 31161da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 3117d83c49f3SAl Viro } 31181da177e4SLinus Torvalds if (target) { 3119d83c49f3SAl Viro if (!error) { 31201da177e4SLinus Torvalds target->i_flags |= S_DEAD; 3121d83c49f3SAl Viro dont_mount(new_dentry); 3122d83c49f3SAl Viro } 31231b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 31241da177e4SLinus Torvalds if (d_unhashed(new_dentry)) 31251da177e4SLinus Torvalds d_rehash(new_dentry); 31261da177e4SLinus Torvalds dput(new_dentry); 31271da177e4SLinus Torvalds } 3128e31e14ecSStephen Smalley if (!error) 3129349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 31301da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 31311da177e4SLinus Torvalds return error; 31321da177e4SLinus Torvalds } 31331da177e4SLinus Torvalds 313475c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 31351da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 31361da177e4SLinus Torvalds { 31371da177e4SLinus Torvalds struct inode *target; 31381da177e4SLinus Torvalds int error; 31391da177e4SLinus Torvalds 31401da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 31411da177e4SLinus Torvalds if (error) 31421da177e4SLinus Torvalds return error; 31431da177e4SLinus Torvalds 31441da177e4SLinus Torvalds dget(new_dentry); 31451da177e4SLinus Torvalds target = new_dentry->d_inode; 31461da177e4SLinus Torvalds if (target) 31471b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 31481da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 31491da177e4SLinus Torvalds error = -EBUSY; 31501da177e4SLinus Torvalds else 31511da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 31521da177e4SLinus Torvalds if (!error) { 3153bec1052eSAl Viro if (target) 3154d83c49f3SAl Viro dont_mount(new_dentry); 3155349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 31561da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 31571da177e4SLinus Torvalds } 31581da177e4SLinus Torvalds if (target) 31591b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 31601da177e4SLinus Torvalds dput(new_dentry); 31611da177e4SLinus Torvalds return error; 31621da177e4SLinus Torvalds } 31631da177e4SLinus Torvalds 31641da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 31651da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 31661da177e4SLinus Torvalds { 31671da177e4SLinus Torvalds int error; 31681da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 316959b0df21SEric Paris const unsigned char *old_name; 31701da177e4SLinus Torvalds 31711da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 31721da177e4SLinus Torvalds return 0; 31731da177e4SLinus Torvalds 31741da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 31751da177e4SLinus Torvalds if (error) 31761da177e4SLinus Torvalds return error; 31771da177e4SLinus Torvalds 31781da177e4SLinus Torvalds if (!new_dentry->d_inode) 3179a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 31801da177e4SLinus Torvalds else 31811da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 31821da177e4SLinus Torvalds if (error) 31831da177e4SLinus Torvalds return error; 31841da177e4SLinus Torvalds 3185acfa4380SAl Viro if (!old_dir->i_op->rename) 31861da177e4SLinus Torvalds return -EPERM; 31871da177e4SLinus Torvalds 31880eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 31890eeca283SRobert Love 31901da177e4SLinus Torvalds if (is_dir) 31911da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 31921da177e4SLinus Torvalds else 31931da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3194123df294SAl Viro if (!error) 3195123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 31965a190ae6SAl Viro new_dentry->d_inode, old_dentry); 31970eeca283SRobert Love fsnotify_oldname_free(old_name); 31980eeca283SRobert Love 31991da177e4SLinus Torvalds return error; 32001da177e4SLinus Torvalds } 32011da177e4SLinus Torvalds 32022e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 32032e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 32041da177e4SLinus Torvalds { 32051da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 32061da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 32071da177e4SLinus Torvalds struct dentry *trap; 32081da177e4SLinus Torvalds struct nameidata oldnd, newnd; 32092ad94ae6SAl Viro char *from; 32102ad94ae6SAl Viro char *to; 32112ad94ae6SAl Viro int error; 32121da177e4SLinus Torvalds 32132ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 32141da177e4SLinus Torvalds if (error) 32151da177e4SLinus Torvalds goto exit; 32161da177e4SLinus Torvalds 32172ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 32181da177e4SLinus Torvalds if (error) 32191da177e4SLinus Torvalds goto exit1; 32201da177e4SLinus Torvalds 32211da177e4SLinus Torvalds error = -EXDEV; 32224ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 32231da177e4SLinus Torvalds goto exit2; 32241da177e4SLinus Torvalds 32254ac91378SJan Blunck old_dir = oldnd.path.dentry; 32261da177e4SLinus Torvalds error = -EBUSY; 32271da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 32281da177e4SLinus Torvalds goto exit2; 32291da177e4SLinus Torvalds 32304ac91378SJan Blunck new_dir = newnd.path.dentry; 32311da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 32321da177e4SLinus Torvalds goto exit2; 32331da177e4SLinus Torvalds 32340612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 32350612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 32364e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 32370612d9fbSOGAWA Hirofumi 32381da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 32391da177e4SLinus Torvalds 324049705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 32411da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 32421da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 32431da177e4SLinus Torvalds goto exit3; 32441da177e4SLinus Torvalds /* source must exist */ 32451da177e4SLinus Torvalds error = -ENOENT; 32461da177e4SLinus Torvalds if (!old_dentry->d_inode) 32471da177e4SLinus Torvalds goto exit4; 32481da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 32491da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 32501da177e4SLinus Torvalds error = -ENOTDIR; 32511da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 32521da177e4SLinus Torvalds goto exit4; 32531da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 32541da177e4SLinus Torvalds goto exit4; 32551da177e4SLinus Torvalds } 32561da177e4SLinus Torvalds /* source should not be ancestor of target */ 32571da177e4SLinus Torvalds error = -EINVAL; 32581da177e4SLinus Torvalds if (old_dentry == trap) 32591da177e4SLinus Torvalds goto exit4; 326049705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 32611da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 32621da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 32631da177e4SLinus Torvalds goto exit4; 32641da177e4SLinus Torvalds /* target should not be an ancestor of source */ 32651da177e4SLinus Torvalds error = -ENOTEMPTY; 32661da177e4SLinus Torvalds if (new_dentry == trap) 32671da177e4SLinus Torvalds goto exit5; 32681da177e4SLinus Torvalds 32699079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 32709079b1ebSDave Hansen if (error) 32719079b1ebSDave Hansen goto exit5; 3272be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3273be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3274be6d3e56SKentaro Takeda if (error) 3275be6d3e56SKentaro Takeda goto exit6; 32761da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 32771da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 3278be6d3e56SKentaro Takeda exit6: 32799079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 32801da177e4SLinus Torvalds exit5: 32811da177e4SLinus Torvalds dput(new_dentry); 32821da177e4SLinus Torvalds exit4: 32831da177e4SLinus Torvalds dput(old_dentry); 32841da177e4SLinus Torvalds exit3: 32851da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 32861da177e4SLinus Torvalds exit2: 32871d957f9bSJan Blunck path_put(&newnd.path); 32882ad94ae6SAl Viro putname(to); 32891da177e4SLinus Torvalds exit1: 32901d957f9bSJan Blunck path_put(&oldnd.path); 32911da177e4SLinus Torvalds putname(from); 32922ad94ae6SAl Viro exit: 32931da177e4SLinus Torvalds return error; 32941da177e4SLinus Torvalds } 32951da177e4SLinus Torvalds 3296a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 32975590ff0dSUlrich Drepper { 32985590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 32995590ff0dSUlrich Drepper } 33005590ff0dSUlrich Drepper 33011da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 33021da177e4SLinus Torvalds { 33031da177e4SLinus Torvalds int len; 33041da177e4SLinus Torvalds 33051da177e4SLinus Torvalds len = PTR_ERR(link); 33061da177e4SLinus Torvalds if (IS_ERR(link)) 33071da177e4SLinus Torvalds goto out; 33081da177e4SLinus Torvalds 33091da177e4SLinus Torvalds len = strlen(link); 33101da177e4SLinus Torvalds if (len > (unsigned) buflen) 33111da177e4SLinus Torvalds len = buflen; 33121da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 33131da177e4SLinus Torvalds len = -EFAULT; 33141da177e4SLinus Torvalds out: 33151da177e4SLinus Torvalds return len; 33161da177e4SLinus Torvalds } 33171da177e4SLinus Torvalds 33181da177e4SLinus Torvalds /* 33191da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 33201da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 33211da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 33221da177e4SLinus Torvalds */ 33231da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 33241da177e4SLinus Torvalds { 33251da177e4SLinus Torvalds struct nameidata nd; 3326cc314eefSLinus Torvalds void *cookie; 3327694a1764SMarcin Slusarz int res; 3328cc314eefSLinus Torvalds 33291da177e4SLinus Torvalds nd.depth = 0; 3330cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3331694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3332694a1764SMarcin Slusarz return PTR_ERR(cookie); 3333694a1764SMarcin Slusarz 3334694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 33351da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3336cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3337694a1764SMarcin Slusarz return res; 33381da177e4SLinus Torvalds } 33391da177e4SLinus Torvalds 33401da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 33411da177e4SLinus Torvalds { 33421da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 33431da177e4SLinus Torvalds } 33441da177e4SLinus Torvalds 33451da177e4SLinus Torvalds /* get the link contents into pagecache */ 33461da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 33471da177e4SLinus Torvalds { 3348ebd09abbSDuane Griffin char *kaddr; 33491da177e4SLinus Torvalds struct page *page; 33501da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3351090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 33521da177e4SLinus Torvalds if (IS_ERR(page)) 33536fe6900eSNick Piggin return (char*)page; 33541da177e4SLinus Torvalds *ppage = page; 3355ebd09abbSDuane Griffin kaddr = kmap(page); 3356ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3357ebd09abbSDuane Griffin return kaddr; 33581da177e4SLinus Torvalds } 33591da177e4SLinus Torvalds 33601da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 33611da177e4SLinus Torvalds { 33621da177e4SLinus Torvalds struct page *page = NULL; 33631da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 33641da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 33651da177e4SLinus Torvalds if (page) { 33661da177e4SLinus Torvalds kunmap(page); 33671da177e4SLinus Torvalds page_cache_release(page); 33681da177e4SLinus Torvalds } 33691da177e4SLinus Torvalds return res; 33701da177e4SLinus Torvalds } 33711da177e4SLinus Torvalds 3372cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 33731da177e4SLinus Torvalds { 3374cc314eefSLinus Torvalds struct page *page = NULL; 33751da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 3376cc314eefSLinus Torvalds return page; 33771da177e4SLinus Torvalds } 33781da177e4SLinus Torvalds 3379cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 33801da177e4SLinus Torvalds { 3381cc314eefSLinus Torvalds struct page *page = cookie; 3382cc314eefSLinus Torvalds 3383cc314eefSLinus Torvalds if (page) { 33841da177e4SLinus Torvalds kunmap(page); 33851da177e4SLinus Torvalds page_cache_release(page); 33861da177e4SLinus Torvalds } 33871da177e4SLinus Torvalds } 33881da177e4SLinus Torvalds 338954566b2cSNick Piggin /* 339054566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 339154566b2cSNick Piggin */ 339254566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 33931da177e4SLinus Torvalds { 33941da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 33950adb25d2SKirill Korotaev struct page *page; 3396afddba49SNick Piggin void *fsdata; 3397beb497abSDmitriy Monakhov int err; 33981da177e4SLinus Torvalds char *kaddr; 339954566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 340054566b2cSNick Piggin if (nofs) 340154566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 34021da177e4SLinus Torvalds 34037e53cac4SNeilBrown retry: 3404afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 340554566b2cSNick Piggin flags, &page, &fsdata); 34061da177e4SLinus Torvalds if (err) 3407afddba49SNick Piggin goto fail; 3408afddba49SNick Piggin 34091da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 34101da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 34111da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 3412afddba49SNick Piggin 3413afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 3414afddba49SNick Piggin page, fsdata); 34151da177e4SLinus Torvalds if (err < 0) 34161da177e4SLinus Torvalds goto fail; 3417afddba49SNick Piggin if (err < len-1) 3418afddba49SNick Piggin goto retry; 3419afddba49SNick Piggin 34201da177e4SLinus Torvalds mark_inode_dirty(inode); 34211da177e4SLinus Torvalds return 0; 34221da177e4SLinus Torvalds fail: 34231da177e4SLinus Torvalds return err; 34241da177e4SLinus Torvalds } 34251da177e4SLinus Torvalds 34260adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 34270adb25d2SKirill Korotaev { 34280adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 342954566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 34300adb25d2SKirill Korotaev } 34310adb25d2SKirill Korotaev 343292e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 34331da177e4SLinus Torvalds .readlink = generic_readlink, 34341da177e4SLinus Torvalds .follow_link = page_follow_link_light, 34351da177e4SLinus Torvalds .put_link = page_put_link, 34361da177e4SLinus Torvalds }; 34371da177e4SLinus Torvalds 34382d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 3439cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one); 34401da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 34411da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 34421da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 34431da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 34441da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 34451da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 34461da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 34471da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 34481da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 34490adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 34501da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 34511da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 3452c9c6cac0SAl Viro EXPORT_SYMBOL(kern_path_parent); 3453d1811465SAl Viro EXPORT_SYMBOL(kern_path); 345416f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 3455f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 34568c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission); 34571da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 34581da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 34591da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 34601da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 34611da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 34621da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 34631da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 34641da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 34651da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 34661da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 34671da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 34681da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 34691da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 34701da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 3471