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 /** 371b3e19d92SNick Piggin * path_get_long - get a long reference to a path 372b3e19d92SNick Piggin * @path: path to get the reference to 373b3e19d92SNick Piggin * 374b3e19d92SNick Piggin * Given a path increment the reference count to the dentry and the vfsmount. 375b3e19d92SNick Piggin */ 376b3e19d92SNick Piggin void path_get_long(struct path *path) 377b3e19d92SNick Piggin { 378b3e19d92SNick Piggin mntget_long(path->mnt); 379b3e19d92SNick Piggin dget(path->dentry); 380b3e19d92SNick Piggin } 381b3e19d92SNick Piggin 382b3e19d92SNick Piggin /** 3831d957f9bSJan Blunck * path_put - put a reference to a path 3841d957f9bSJan Blunck * @path: path to put the reference to 3851d957f9bSJan Blunck * 3861d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 3871d957f9bSJan Blunck */ 3881d957f9bSJan Blunck void path_put(struct path *path) 3891da177e4SLinus Torvalds { 3901d957f9bSJan Blunck dput(path->dentry); 3911d957f9bSJan Blunck mntput(path->mnt); 3921da177e4SLinus Torvalds } 3931d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 3941da177e4SLinus Torvalds 395834f2a4aSTrond Myklebust /** 396b3e19d92SNick Piggin * path_put_long - put a long reference to a path 397b3e19d92SNick Piggin * @path: path to put the reference to 398b3e19d92SNick Piggin * 399b3e19d92SNick Piggin * Given a path decrement the reference count to the dentry and the vfsmount. 400b3e19d92SNick Piggin */ 401b3e19d92SNick Piggin void path_put_long(struct path *path) 402b3e19d92SNick Piggin { 403b3e19d92SNick Piggin dput(path->dentry); 404b3e19d92SNick Piggin mntput_long(path->mnt); 405b3e19d92SNick Piggin } 406b3e19d92SNick Piggin 407b3e19d92SNick Piggin /** 40831e6b01fSNick Piggin * nameidata_drop_rcu - drop this nameidata out of rcu-walk 40931e6b01fSNick Piggin * @nd: nameidata pathwalk data to drop 41039191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 41131e6b01fSNick Piggin * 41231e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 41331e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). __drop_rcu* functions attempt 41431e6b01fSNick Piggin * to drop out of rcu-walk mode and take normal reference counts on dentries 41531e6b01fSNick Piggin * and vfsmounts to transition to rcu-walk mode. __drop_rcu* functions take 41631e6b01fSNick Piggin * refcounts at the last known good point before rcu-walk got stuck, so 41731e6b01fSNick Piggin * ref-walk may continue from there. If this is not successful (eg. a seqcount 41831e6b01fSNick Piggin * has changed), then failure is returned and path walk restarts from the 41931e6b01fSNick Piggin * beginning in ref-walk mode. 42031e6b01fSNick Piggin * 42131e6b01fSNick Piggin * nameidata_drop_rcu attempts to drop the current nd->path and nd->root into 42231e6b01fSNick Piggin * ref-walk. Must be called from rcu-walk context. 42331e6b01fSNick Piggin */ 42431e6b01fSNick Piggin static int nameidata_drop_rcu(struct nameidata *nd) 42531e6b01fSNick Piggin { 42631e6b01fSNick Piggin struct fs_struct *fs = current->fs; 42731e6b01fSNick Piggin struct dentry *dentry = nd->path.dentry; 42831e6b01fSNick Piggin 42931e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 43031e6b01fSNick Piggin if (nd->root.mnt) { 43131e6b01fSNick Piggin spin_lock(&fs->lock); 43231e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 43331e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 43431e6b01fSNick Piggin goto err_root; 43531e6b01fSNick Piggin } 43631e6b01fSNick Piggin spin_lock(&dentry->d_lock); 43731e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 43831e6b01fSNick Piggin goto err; 43931e6b01fSNick Piggin BUG_ON(nd->inode != dentry->d_inode); 44031e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 44131e6b01fSNick Piggin if (nd->root.mnt) { 44231e6b01fSNick Piggin path_get(&nd->root); 44331e6b01fSNick Piggin spin_unlock(&fs->lock); 44431e6b01fSNick Piggin } 44531e6b01fSNick Piggin mntget(nd->path.mnt); 44631e6b01fSNick Piggin 44731e6b01fSNick Piggin rcu_read_unlock(); 44831e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 44931e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 45031e6b01fSNick Piggin return 0; 45131e6b01fSNick Piggin err: 45231e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 45331e6b01fSNick Piggin err_root: 45431e6b01fSNick Piggin if (nd->root.mnt) 45531e6b01fSNick Piggin spin_unlock(&fs->lock); 45631e6b01fSNick Piggin return -ECHILD; 45731e6b01fSNick Piggin } 45831e6b01fSNick Piggin 45931e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */ 46031e6b01fSNick Piggin static inline int nameidata_drop_rcu_maybe(struct nameidata *nd) 46131e6b01fSNick Piggin { 46231e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) 46331e6b01fSNick Piggin return nameidata_drop_rcu(nd); 46431e6b01fSNick Piggin return 0; 46531e6b01fSNick Piggin } 46631e6b01fSNick Piggin 46731e6b01fSNick Piggin /** 46831e6b01fSNick Piggin * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk 46931e6b01fSNick Piggin * @nd: nameidata pathwalk data to drop 47031e6b01fSNick Piggin * @dentry: dentry to drop 47139191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 47231e6b01fSNick Piggin * 47331e6b01fSNick Piggin * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root, 47431e6b01fSNick Piggin * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on 47531e6b01fSNick Piggin * @nd. Must be called from rcu-walk context. 47631e6b01fSNick Piggin */ 47731e6b01fSNick Piggin static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry) 47831e6b01fSNick Piggin { 47931e6b01fSNick Piggin struct fs_struct *fs = current->fs; 48031e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 48131e6b01fSNick Piggin 48231e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 48331e6b01fSNick Piggin if (nd->root.mnt) { 48431e6b01fSNick Piggin spin_lock(&fs->lock); 48531e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 48631e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 48731e6b01fSNick Piggin goto err_root; 48831e6b01fSNick Piggin } 48931e6b01fSNick Piggin spin_lock(&parent->d_lock); 49031e6b01fSNick Piggin spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); 49131e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 49231e6b01fSNick Piggin goto err; 49331e6b01fSNick Piggin /* 49431e6b01fSNick Piggin * If the sequence check on the child dentry passed, then the child has 49531e6b01fSNick Piggin * not been removed from its parent. This means the parent dentry must 49631e6b01fSNick Piggin * be valid and able to take a reference at this point. 49731e6b01fSNick Piggin */ 49831e6b01fSNick Piggin BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); 49931e6b01fSNick Piggin BUG_ON(!parent->d_count); 50031e6b01fSNick Piggin parent->d_count++; 50131e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 50231e6b01fSNick Piggin spin_unlock(&parent->d_lock); 50331e6b01fSNick Piggin if (nd->root.mnt) { 50431e6b01fSNick Piggin path_get(&nd->root); 50531e6b01fSNick Piggin spin_unlock(&fs->lock); 50631e6b01fSNick Piggin } 50731e6b01fSNick Piggin mntget(nd->path.mnt); 50831e6b01fSNick Piggin 50931e6b01fSNick Piggin rcu_read_unlock(); 51031e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 51131e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 51231e6b01fSNick Piggin return 0; 51331e6b01fSNick Piggin err: 51431e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 51531e6b01fSNick Piggin spin_unlock(&parent->d_lock); 51631e6b01fSNick Piggin err_root: 51731e6b01fSNick Piggin if (nd->root.mnt) 51831e6b01fSNick Piggin spin_unlock(&fs->lock); 51931e6b01fSNick Piggin return -ECHILD; 52031e6b01fSNick Piggin } 52131e6b01fSNick Piggin 52231e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */ 52331e6b01fSNick Piggin static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry) 52431e6b01fSNick Piggin { 52531e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) 52631e6b01fSNick Piggin return nameidata_dentry_drop_rcu(nd, dentry); 52731e6b01fSNick Piggin return 0; 52831e6b01fSNick Piggin } 52931e6b01fSNick Piggin 53031e6b01fSNick Piggin /** 53131e6b01fSNick Piggin * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk 53231e6b01fSNick Piggin * @nd: nameidata pathwalk data to drop 53339191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 53431e6b01fSNick Piggin * 53531e6b01fSNick Piggin * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk. 53631e6b01fSNick Piggin * nd->path should be the final element of the lookup, so nd->root is discarded. 53731e6b01fSNick Piggin * Must be called from rcu-walk context. 53831e6b01fSNick Piggin */ 53931e6b01fSNick Piggin static int nameidata_drop_rcu_last(struct nameidata *nd) 54031e6b01fSNick Piggin { 54131e6b01fSNick Piggin struct dentry *dentry = nd->path.dentry; 54231e6b01fSNick Piggin 54331e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 54431e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 54531e6b01fSNick Piggin nd->root.mnt = NULL; 54631e6b01fSNick Piggin spin_lock(&dentry->d_lock); 54731e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 54831e6b01fSNick Piggin goto err_unlock; 54931e6b01fSNick Piggin BUG_ON(nd->inode != dentry->d_inode); 55031e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 55131e6b01fSNick Piggin 55231e6b01fSNick Piggin mntget(nd->path.mnt); 55331e6b01fSNick Piggin 55431e6b01fSNick Piggin rcu_read_unlock(); 55531e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 55631e6b01fSNick Piggin 55731e6b01fSNick Piggin return 0; 55831e6b01fSNick Piggin 55931e6b01fSNick Piggin err_unlock: 56031e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 56131e6b01fSNick Piggin rcu_read_unlock(); 56231e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 56331e6b01fSNick Piggin return -ECHILD; 56431e6b01fSNick Piggin } 56531e6b01fSNick Piggin 56631e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */ 56731e6b01fSNick Piggin static inline int nameidata_drop_rcu_last_maybe(struct nameidata *nd) 56831e6b01fSNick Piggin { 56931e6b01fSNick Piggin if (likely(nd->flags & LOOKUP_RCU)) 57031e6b01fSNick Piggin return nameidata_drop_rcu_last(nd); 57131e6b01fSNick Piggin return 0; 57231e6b01fSNick Piggin } 57331e6b01fSNick Piggin 57431e6b01fSNick Piggin /** 575834f2a4aSTrond Myklebust * release_open_intent - free up open intent resources 576834f2a4aSTrond Myklebust * @nd: pointer to nameidata 577834f2a4aSTrond Myklebust */ 578834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd) 579834f2a4aSTrond Myklebust { 5800f7fc9e4SJosef "Jeff" Sipek if (nd->intent.open.file->f_path.dentry == NULL) 581834f2a4aSTrond Myklebust put_filp(nd->intent.open.file); 582834f2a4aSTrond Myklebust else 583834f2a4aSTrond Myklebust fput(nd->intent.open.file); 584834f2a4aSTrond Myklebust } 585834f2a4aSTrond Myklebust 58634286d66SNick Piggin static int d_revalidate(struct dentry *dentry, struct nameidata *nd) 58734286d66SNick Piggin { 58834286d66SNick Piggin int status; 58934286d66SNick Piggin 59034286d66SNick Piggin status = dentry->d_op->d_revalidate(dentry, nd); 59134286d66SNick Piggin if (status == -ECHILD) { 59234286d66SNick Piggin if (nameidata_dentry_drop_rcu(nd, dentry)) 59334286d66SNick Piggin return status; 59434286d66SNick Piggin status = dentry->d_op->d_revalidate(dentry, nd); 59534286d66SNick Piggin } 59634286d66SNick Piggin 59734286d66SNick Piggin return status; 59834286d66SNick Piggin } 59934286d66SNick Piggin 600bcdc5e01SIan Kent static inline struct dentry * 601bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd) 602bcdc5e01SIan Kent { 60334286d66SNick Piggin int status; 60434286d66SNick Piggin 60534286d66SNick Piggin status = d_revalidate(dentry, nd); 606bcdc5e01SIan Kent if (unlikely(status <= 0)) { 607bcdc5e01SIan Kent /* 608bcdc5e01SIan Kent * The dentry failed validation. 609bcdc5e01SIan Kent * If d_revalidate returned 0 attempt to invalidate 610bcdc5e01SIan Kent * the dentry otherwise d_revalidate is asking us 611bcdc5e01SIan Kent * to return a fail status. 612bcdc5e01SIan Kent */ 61334286d66SNick Piggin if (status < 0) { 61434286d66SNick Piggin /* If we're in rcu-walk, we don't have a ref */ 61534286d66SNick Piggin if (!(nd->flags & LOOKUP_RCU)) 61634286d66SNick Piggin dput(dentry); 61734286d66SNick Piggin dentry = ERR_PTR(status); 61834286d66SNick Piggin 61934286d66SNick Piggin } else { 62034286d66SNick Piggin /* Don't d_invalidate in rcu-walk mode */ 62134286d66SNick Piggin if (nameidata_dentry_drop_rcu_maybe(nd, dentry)) 62234286d66SNick Piggin return ERR_PTR(-ECHILD); 623bcdc5e01SIan Kent if (!d_invalidate(dentry)) { 624bcdc5e01SIan Kent dput(dentry); 625bcdc5e01SIan Kent dentry = NULL; 626bcdc5e01SIan Kent } 627bcdc5e01SIan Kent } 628bcdc5e01SIan Kent } 629bcdc5e01SIan Kent return dentry; 630bcdc5e01SIan Kent } 631bcdc5e01SIan Kent 632fb045adbSNick Piggin static inline int need_reval_dot(struct dentry *dentry) 633fb045adbSNick Piggin { 634fb045adbSNick Piggin if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE))) 635fb045adbSNick Piggin return 0; 636fb045adbSNick Piggin 637fb045adbSNick Piggin if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))) 638fb045adbSNick Piggin return 0; 639fb045adbSNick Piggin 640fb045adbSNick Piggin return 1; 641fb045adbSNick Piggin } 642fb045adbSNick Piggin 6431da177e4SLinus Torvalds /* 64439159de2SJeff Layton * force_reval_path - force revalidation of a dentry 64539159de2SJeff Layton * 64639159de2SJeff Layton * In some situations the path walking code will trust dentries without 64739159de2SJeff Layton * revalidating them. This causes problems for filesystems that depend on 64839159de2SJeff Layton * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set 64939159de2SJeff Layton * (which indicates that it's possible for the dentry to go stale), force 65039159de2SJeff Layton * a d_revalidate call before proceeding. 65139159de2SJeff Layton * 65239159de2SJeff Layton * Returns 0 if the revalidation was successful. If the revalidation fails, 65339159de2SJeff Layton * either return the error returned by d_revalidate or -ESTALE if the 65439159de2SJeff Layton * revalidation it just returned 0. If d_revalidate returns 0, we attempt to 65539159de2SJeff Layton * invalidate the dentry. It's up to the caller to handle putting references 65639159de2SJeff Layton * to the path if necessary. 65739159de2SJeff Layton */ 65839159de2SJeff Layton static int 65939159de2SJeff Layton force_reval_path(struct path *path, struct nameidata *nd) 66039159de2SJeff Layton { 66139159de2SJeff Layton int status; 66239159de2SJeff Layton struct dentry *dentry = path->dentry; 66339159de2SJeff Layton 66439159de2SJeff Layton /* 66539159de2SJeff Layton * only check on filesystems where it's possible for the dentry to 666fb045adbSNick Piggin * become stale. 66739159de2SJeff Layton */ 668fb045adbSNick Piggin if (!need_reval_dot(dentry)) 66939159de2SJeff Layton return 0; 67039159de2SJeff Layton 67134286d66SNick Piggin status = d_revalidate(dentry, nd); 67239159de2SJeff Layton if (status > 0) 67339159de2SJeff Layton return 0; 67439159de2SJeff Layton 67539159de2SJeff Layton if (!status) { 67639159de2SJeff Layton d_invalidate(dentry); 67739159de2SJeff Layton status = -ESTALE; 67839159de2SJeff Layton } 67939159de2SJeff Layton return status; 68039159de2SJeff Layton } 68139159de2SJeff Layton 68239159de2SJeff Layton /* 683b75b5086SAl Viro * Short-cut version of permission(), for calling on directories 684b75b5086SAl Viro * during pathname resolution. Combines parts of permission() 685b75b5086SAl Viro * and generic_permission(), and tests ONLY for MAY_EXEC permission. 6861da177e4SLinus Torvalds * 6871da177e4SLinus Torvalds * If appropriate, check DAC only. If not appropriate, or 688b75b5086SAl Viro * short-cut DAC fails, then call ->permission() to do more 6891da177e4SLinus Torvalds * complete permission check. 6901da177e4SLinus Torvalds */ 691b74c79e9SNick Piggin static inline int exec_permission(struct inode *inode, unsigned int flags) 6921da177e4SLinus Torvalds { 6935909ccaaSLinus Torvalds int ret; 6941da177e4SLinus Torvalds 695cb9179eaSLinus Torvalds if (inode->i_op->permission) { 696b74c79e9SNick Piggin ret = inode->i_op->permission(inode, MAY_EXEC, flags); 697b74c79e9SNick Piggin } else { 698b74c79e9SNick Piggin ret = acl_permission_check(inode, MAY_EXEC, flags, 699b74c79e9SNick Piggin inode->i_op->check_acl); 700cb9179eaSLinus Torvalds } 701b74c79e9SNick Piggin if (likely(!ret)) 7021da177e4SLinus Torvalds goto ok; 703b74c79e9SNick Piggin if (ret == -ECHILD) 70431e6b01fSNick Piggin return ret; 7051da177e4SLinus Torvalds 706f1ac9f6bSLinus Torvalds if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH)) 7071da177e4SLinus Torvalds goto ok; 7081da177e4SLinus Torvalds 7095909ccaaSLinus Torvalds return ret; 7101da177e4SLinus Torvalds ok: 711b74c79e9SNick Piggin return security_inode_exec_permission(inode, flags); 7121da177e4SLinus Torvalds } 7131da177e4SLinus Torvalds 7142a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 7152a737871SAl Viro { 716f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 717f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 7182a737871SAl Viro } 7192a737871SAl Viro 7206de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 7216de88d72SAl Viro 72231e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 72331e6b01fSNick Piggin { 72431e6b01fSNick Piggin if (!nd->root.mnt) { 72531e6b01fSNick Piggin struct fs_struct *fs = current->fs; 726c28cc364SNick Piggin unsigned seq; 727c28cc364SNick Piggin 728c28cc364SNick Piggin do { 729c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 73031e6b01fSNick Piggin nd->root = fs->root; 731c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 73231e6b01fSNick Piggin } 73331e6b01fSNick Piggin } 73431e6b01fSNick Piggin 735f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 7361da177e4SLinus Torvalds { 73731e6b01fSNick Piggin int ret; 73831e6b01fSNick Piggin 7391da177e4SLinus Torvalds if (IS_ERR(link)) 7401da177e4SLinus Torvalds goto fail; 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds if (*link == '/') { 7432a737871SAl Viro set_root(nd); 7441d957f9bSJan Blunck path_put(&nd->path); 7452a737871SAl Viro nd->path = nd->root; 7462a737871SAl Viro path_get(&nd->root); 7471da177e4SLinus Torvalds } 74831e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 749b4091d5fSChristoph Hellwig 75031e6b01fSNick Piggin ret = link_path_walk(link, nd); 75131e6b01fSNick Piggin return ret; 7521da177e4SLinus Torvalds fail: 7531d957f9bSJan Blunck path_put(&nd->path); 7541da177e4SLinus Torvalds return PTR_ERR(link); 7551da177e4SLinus Torvalds } 7561da177e4SLinus Torvalds 7571d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 758051d3812SIan Kent { 759051d3812SIan Kent dput(path->dentry); 7604ac91378SJan Blunck if (path->mnt != nd->path.mnt) 761051d3812SIan Kent mntput(path->mnt); 762051d3812SIan Kent } 763051d3812SIan Kent 764051d3812SIan Kent static inline void path_to_nameidata(struct path *path, struct nameidata *nd) 765051d3812SIan Kent { 76631e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 7674ac91378SJan Blunck dput(nd->path.dentry); 76831e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 7694ac91378SJan Blunck mntput(nd->path.mnt); 7709a229683SHuang Shijie } 77131e6b01fSNick Piggin nd->path.mnt = path->mnt; 7724ac91378SJan Blunck nd->path.dentry = path->dentry; 773051d3812SIan Kent } 774051d3812SIan Kent 775def4af30SAl Viro static __always_inline int 776def4af30SAl Viro __do_follow_link(struct path *path, struct nameidata *nd, void **p) 7771da177e4SLinus Torvalds { 7781da177e4SLinus Torvalds int error; 779cd4e91d3SAl Viro struct dentry *dentry = path->dentry; 7801da177e4SLinus Torvalds 781d671a1cbSAl Viro touch_atime(path->mnt, dentry); 7821da177e4SLinus Torvalds nd_set_link(nd, NULL); 783cd4e91d3SAl Viro 7844ac91378SJan Blunck if (path->mnt != nd->path.mnt) { 785051d3812SIan Kent path_to_nameidata(path, nd); 78631e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 787051d3812SIan Kent dget(dentry); 788051d3812SIan Kent } 789cd4e91d3SAl Viro mntget(path->mnt); 79031e6b01fSNick Piggin 79186acdca1SAl Viro nd->last_type = LAST_BIND; 792def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 793def4af30SAl Viro error = PTR_ERR(*p); 794def4af30SAl Viro if (!IS_ERR(*p)) { 7951da177e4SLinus Torvalds char *s = nd_get_link(nd); 796cc314eefSLinus Torvalds error = 0; 7971da177e4SLinus Torvalds if (s) 7981da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 79939159de2SJeff Layton else if (nd->last_type == LAST_BIND) { 80039159de2SJeff Layton error = force_reval_path(&nd->path, nd); 80139159de2SJeff Layton if (error) 80239159de2SJeff Layton path_put(&nd->path); 80339159de2SJeff Layton } 8041da177e4SLinus Torvalds } 8051da177e4SLinus Torvalds return error; 8061da177e4SLinus Torvalds } 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds /* 8091da177e4SLinus Torvalds * This limits recursive symlink follows to 8, while 8101da177e4SLinus Torvalds * limiting consecutive symlinks to 40. 8111da177e4SLinus Torvalds * 8121da177e4SLinus Torvalds * Without that kind of total limit, nasty chains of consecutive 8131da177e4SLinus Torvalds * symlinks can cause almost arbitrarily long lookups. 8141da177e4SLinus Torvalds */ 81590ebe565SAl Viro static inline int do_follow_link(struct path *path, struct nameidata *nd) 8161da177e4SLinus Torvalds { 817def4af30SAl Viro void *cookie; 8181da177e4SLinus Torvalds int err = -ELOOP; 8191da177e4SLinus Torvalds if (current->link_count >= MAX_NESTED_LINKS) 8201da177e4SLinus Torvalds goto loop; 8211da177e4SLinus Torvalds if (current->total_link_count >= 40) 8221da177e4SLinus Torvalds goto loop; 8231da177e4SLinus Torvalds BUG_ON(nd->depth >= MAX_NESTED_LINKS); 8241da177e4SLinus Torvalds cond_resched(); 82590ebe565SAl Viro err = security_inode_follow_link(path->dentry, nd); 8261da177e4SLinus Torvalds if (err) 8271da177e4SLinus Torvalds goto loop; 8281da177e4SLinus Torvalds current->link_count++; 8291da177e4SLinus Torvalds current->total_link_count++; 8301da177e4SLinus Torvalds nd->depth++; 831def4af30SAl Viro err = __do_follow_link(path, nd, &cookie); 832def4af30SAl Viro if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link) 833def4af30SAl Viro path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie); 834258fa999SAl Viro path_put(path); 8351da177e4SLinus Torvalds current->link_count--; 8361da177e4SLinus Torvalds nd->depth--; 8371da177e4SLinus Torvalds return err; 8381da177e4SLinus Torvalds loop: 8391d957f9bSJan Blunck path_put_conditional(path, nd); 8401d957f9bSJan Blunck path_put(&nd->path); 8411da177e4SLinus Torvalds return err; 8421da177e4SLinus Torvalds } 8431da177e4SLinus Torvalds 84431e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 84531e6b01fSNick Piggin { 84631e6b01fSNick Piggin struct vfsmount *parent; 84731e6b01fSNick Piggin struct dentry *mountpoint; 84831e6b01fSNick Piggin 84931e6b01fSNick Piggin parent = path->mnt->mnt_parent; 85031e6b01fSNick Piggin if (parent == path->mnt) 85131e6b01fSNick Piggin return 0; 85231e6b01fSNick Piggin mountpoint = path->mnt->mnt_mountpoint; 85331e6b01fSNick Piggin path->dentry = mountpoint; 85431e6b01fSNick Piggin path->mnt = parent; 85531e6b01fSNick Piggin return 1; 85631e6b01fSNick Piggin } 85731e6b01fSNick Piggin 858bab77ebfSAl Viro int follow_up(struct path *path) 8591da177e4SLinus Torvalds { 8601da177e4SLinus Torvalds struct vfsmount *parent; 8611da177e4SLinus Torvalds struct dentry *mountpoint; 86299b7db7bSNick Piggin 86399b7db7bSNick Piggin br_read_lock(vfsmount_lock); 864bab77ebfSAl Viro parent = path->mnt->mnt_parent; 865bab77ebfSAl Viro if (parent == path->mnt) { 86699b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 8671da177e4SLinus Torvalds return 0; 8681da177e4SLinus Torvalds } 8691da177e4SLinus Torvalds mntget(parent); 870bab77ebfSAl Viro mountpoint = dget(path->mnt->mnt_mountpoint); 87199b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 872bab77ebfSAl Viro dput(path->dentry); 873bab77ebfSAl Viro path->dentry = mountpoint; 874bab77ebfSAl Viro mntput(path->mnt); 875bab77ebfSAl Viro path->mnt = parent; 8761da177e4SLinus Torvalds return 1; 8771da177e4SLinus Torvalds } 8781da177e4SLinus Torvalds 879b5c84bf6SNick Piggin /* 880b5c84bf6SNick Piggin * serialization is taken care of in namespace.c 8811da177e4SLinus Torvalds */ 88231e6b01fSNick Piggin static void __follow_mount_rcu(struct nameidata *nd, struct path *path, 88331e6b01fSNick Piggin struct inode **inode) 88431e6b01fSNick Piggin { 88531e6b01fSNick Piggin while (d_mountpoint(path->dentry)) { 88631e6b01fSNick Piggin struct vfsmount *mounted; 88731e6b01fSNick Piggin mounted = __lookup_mnt(path->mnt, path->dentry, 1); 88831e6b01fSNick Piggin if (!mounted) 88931e6b01fSNick Piggin return; 89031e6b01fSNick Piggin path->mnt = mounted; 89131e6b01fSNick Piggin path->dentry = mounted->mnt_root; 89231e6b01fSNick Piggin nd->seq = read_seqcount_begin(&path->dentry->d_seq); 89331e6b01fSNick Piggin *inode = path->dentry->d_inode; 89431e6b01fSNick Piggin } 89531e6b01fSNick Piggin } 89631e6b01fSNick Piggin 897463ffb2eSAl Viro static int __follow_mount(struct path *path) 898463ffb2eSAl Viro { 899463ffb2eSAl Viro int res = 0; 900463ffb2eSAl Viro while (d_mountpoint(path->dentry)) { 9011c755af4SAl Viro struct vfsmount *mounted = lookup_mnt(path); 902463ffb2eSAl Viro if (!mounted) 903463ffb2eSAl Viro break; 904463ffb2eSAl Viro dput(path->dentry); 905463ffb2eSAl Viro if (res) 906463ffb2eSAl Viro mntput(path->mnt); 907463ffb2eSAl Viro path->mnt = mounted; 908463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 909463ffb2eSAl Viro res = 1; 910463ffb2eSAl Viro } 911463ffb2eSAl Viro return res; 912463ffb2eSAl Viro } 913463ffb2eSAl Viro 91479ed0226SAl Viro static void follow_mount(struct path *path) 9151da177e4SLinus Torvalds { 91679ed0226SAl Viro while (d_mountpoint(path->dentry)) { 9171c755af4SAl Viro struct vfsmount *mounted = lookup_mnt(path); 9181da177e4SLinus Torvalds if (!mounted) 9191da177e4SLinus Torvalds break; 92079ed0226SAl Viro dput(path->dentry); 92179ed0226SAl Viro mntput(path->mnt); 92279ed0226SAl Viro path->mnt = mounted; 92379ed0226SAl Viro path->dentry = dget(mounted->mnt_root); 9241da177e4SLinus Torvalds } 9251da177e4SLinus Torvalds } 9261da177e4SLinus Torvalds 9279393bd07SAl Viro int follow_down(struct path *path) 9281da177e4SLinus Torvalds { 9291da177e4SLinus Torvalds struct vfsmount *mounted; 9301da177e4SLinus Torvalds 9311c755af4SAl Viro mounted = lookup_mnt(path); 9321da177e4SLinus Torvalds if (mounted) { 9339393bd07SAl Viro dput(path->dentry); 9349393bd07SAl Viro mntput(path->mnt); 9359393bd07SAl Viro path->mnt = mounted; 9369393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 9371da177e4SLinus Torvalds return 1; 9381da177e4SLinus Torvalds } 9391da177e4SLinus Torvalds return 0; 9401da177e4SLinus Torvalds } 9411da177e4SLinus Torvalds 94231e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 94331e6b01fSNick Piggin { 94431e6b01fSNick Piggin struct inode *inode = nd->inode; 94531e6b01fSNick Piggin 94631e6b01fSNick Piggin set_root_rcu(nd); 94731e6b01fSNick Piggin 94831e6b01fSNick Piggin while(1) { 94931e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 95031e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 95131e6b01fSNick Piggin break; 95231e6b01fSNick Piggin } 95331e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 95431e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 95531e6b01fSNick Piggin struct dentry *parent = old->d_parent; 95631e6b01fSNick Piggin unsigned seq; 95731e6b01fSNick Piggin 95831e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 95931e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 96031e6b01fSNick Piggin return -ECHILD; 96131e6b01fSNick Piggin inode = parent->d_inode; 96231e6b01fSNick Piggin nd->path.dentry = parent; 96331e6b01fSNick Piggin nd->seq = seq; 96431e6b01fSNick Piggin break; 96531e6b01fSNick Piggin } 96631e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 96731e6b01fSNick Piggin break; 96831e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 96931e6b01fSNick Piggin inode = nd->path.dentry->d_inode; 97031e6b01fSNick Piggin } 97131e6b01fSNick Piggin __follow_mount_rcu(nd, &nd->path, &inode); 97231e6b01fSNick Piggin nd->inode = inode; 97331e6b01fSNick Piggin 97431e6b01fSNick Piggin return 0; 97531e6b01fSNick Piggin } 97631e6b01fSNick Piggin 97731e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 9781da177e4SLinus Torvalds { 9792a737871SAl Viro set_root(nd); 980e518ddb7SAndreas Mohr 9811da177e4SLinus Torvalds while(1) { 9824ac91378SJan Blunck struct dentry *old = nd->path.dentry; 9831da177e4SLinus Torvalds 9842a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 9852a737871SAl Viro nd->path.mnt == nd->root.mnt) { 9861da177e4SLinus Torvalds break; 9871da177e4SLinus Torvalds } 9884ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 9893088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 9903088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 9911da177e4SLinus Torvalds dput(old); 9921da177e4SLinus Torvalds break; 9931da177e4SLinus Torvalds } 9943088dd70SAl Viro if (!follow_up(&nd->path)) 9951da177e4SLinus Torvalds break; 9961da177e4SLinus Torvalds } 99779ed0226SAl Viro follow_mount(&nd->path); 99831e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds /* 1002baa03890SNick Piggin * Allocate a dentry with name and parent, and perform a parent 1003baa03890SNick Piggin * directory ->lookup on it. Returns the new dentry, or ERR_PTR 1004baa03890SNick Piggin * on error. parent->d_inode->i_mutex must be held. d_lookup must 1005baa03890SNick Piggin * have verified that no child exists while under i_mutex. 1006baa03890SNick Piggin */ 1007baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent, 1008baa03890SNick Piggin struct qstr *name, struct nameidata *nd) 1009baa03890SNick Piggin { 1010baa03890SNick Piggin struct inode *inode = parent->d_inode; 1011baa03890SNick Piggin struct dentry *dentry; 1012baa03890SNick Piggin struct dentry *old; 1013baa03890SNick Piggin 1014baa03890SNick Piggin /* Don't create child dentry for a dead directory. */ 1015baa03890SNick Piggin if (unlikely(IS_DEADDIR(inode))) 1016baa03890SNick Piggin return ERR_PTR(-ENOENT); 1017baa03890SNick Piggin 1018baa03890SNick Piggin dentry = d_alloc(parent, name); 1019baa03890SNick Piggin if (unlikely(!dentry)) 1020baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1021baa03890SNick Piggin 1022baa03890SNick Piggin old = inode->i_op->lookup(inode, dentry, nd); 1023baa03890SNick Piggin if (unlikely(old)) { 1024baa03890SNick Piggin dput(dentry); 1025baa03890SNick Piggin dentry = old; 1026baa03890SNick Piggin } 1027baa03890SNick Piggin return dentry; 1028baa03890SNick Piggin } 1029baa03890SNick Piggin 1030baa03890SNick Piggin /* 10311da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 10321da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 10331da177e4SLinus Torvalds * It _is_ time-critical. 10341da177e4SLinus Torvalds */ 10351da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name, 103631e6b01fSNick Piggin struct path *path, struct inode **inode) 10371da177e4SLinus Torvalds { 10384ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 103931e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 10406e6b1bd1SAl Viro struct inode *dir; 10413cac260aSAl Viro /* 10423cac260aSAl Viro * See if the low-level filesystem might want 10433cac260aSAl Viro * to use its own hash.. 10443cac260aSAl Viro */ 1045fb045adbSNick Piggin if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 104631e6b01fSNick Piggin int err = parent->d_op->d_hash(parent, nd->inode, name); 10473cac260aSAl Viro if (err < 0) 10483cac260aSAl Viro return err; 10493cac260aSAl Viro } 10501da177e4SLinus Torvalds 1051b04f784eSNick Piggin /* 1052b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1053b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1054b04f784eSNick Piggin * do the non-racy lookup, below. 1055b04f784eSNick Piggin */ 105631e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 105731e6b01fSNick Piggin unsigned seq; 105831e6b01fSNick Piggin 105931e6b01fSNick Piggin *inode = nd->inode; 106031e6b01fSNick Piggin dentry = __d_lookup_rcu(parent, name, &seq, inode); 106131e6b01fSNick Piggin if (!dentry) { 106231e6b01fSNick Piggin if (nameidata_drop_rcu(nd)) 106331e6b01fSNick Piggin return -ECHILD; 106431e6b01fSNick Piggin goto need_lookup; 106531e6b01fSNick Piggin } 106631e6b01fSNick Piggin /* Memory barrier in read_seqcount_begin of child is enough */ 106731e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 106831e6b01fSNick Piggin return -ECHILD; 106931e6b01fSNick Piggin 107031e6b01fSNick Piggin nd->seq = seq; 107134286d66SNick Piggin if (dentry->d_flags & DCACHE_OP_REVALIDATE) 107231e6b01fSNick Piggin goto need_revalidate; 107331e6b01fSNick Piggin path->mnt = mnt; 107431e6b01fSNick Piggin path->dentry = dentry; 107531e6b01fSNick Piggin __follow_mount_rcu(nd, path, inode); 107631e6b01fSNick Piggin } else { 107731e6b01fSNick Piggin dentry = __d_lookup(parent, name); 10781da177e4SLinus Torvalds if (!dentry) 10791da177e4SLinus Torvalds goto need_lookup; 10802e2e88eaSNick Piggin found: 1081fb045adbSNick Piggin if (dentry->d_flags & DCACHE_OP_REVALIDATE) 10821da177e4SLinus Torvalds goto need_revalidate; 10831da177e4SLinus Torvalds done: 10841da177e4SLinus Torvalds path->mnt = mnt; 10851da177e4SLinus Torvalds path->dentry = dentry; 1086634ee701SAl Viro __follow_mount(path); 108731e6b01fSNick Piggin *inode = path->dentry->d_inode; 108831e6b01fSNick Piggin } 10891da177e4SLinus Torvalds return 0; 10901da177e4SLinus Torvalds 10911da177e4SLinus Torvalds need_lookup: 10926e6b1bd1SAl Viro dir = parent->d_inode; 109331e6b01fSNick Piggin BUG_ON(nd->inode != dir); 10946e6b1bd1SAl Viro 10956e6b1bd1SAl Viro mutex_lock(&dir->i_mutex); 10966e6b1bd1SAl Viro /* 10976e6b1bd1SAl Viro * First re-do the cached lookup just in case it was created 1098b04f784eSNick Piggin * while we waited for the directory semaphore, or the first 1099b04f784eSNick Piggin * lookup failed due to an unrelated rename. 11006e6b1bd1SAl Viro * 1101b04f784eSNick Piggin * This could use version numbering or similar to avoid unnecessary 1102b04f784eSNick Piggin * cache lookups, but then we'd have to do the first lookup in the 1103b04f784eSNick Piggin * non-racy way. However in the common case here, everything should 1104b04f784eSNick Piggin * be hot in cache, so would it be a big win? 11056e6b1bd1SAl Viro */ 11066e6b1bd1SAl Viro dentry = d_lookup(parent, name); 1107baa03890SNick Piggin if (likely(!dentry)) { 1108baa03890SNick Piggin dentry = d_alloc_and_lookup(parent, name, nd); 11096e6b1bd1SAl Viro mutex_unlock(&dir->i_mutex); 11106e6b1bd1SAl Viro if (IS_ERR(dentry)) 11116e6b1bd1SAl Viro goto fail; 11126e6b1bd1SAl Viro goto done; 11136e6b1bd1SAl Viro } 11146e6b1bd1SAl Viro /* 11156e6b1bd1SAl Viro * Uhhuh! Nasty case: the cache was re-populated while 11166e6b1bd1SAl Viro * we waited on the semaphore. Need to revalidate. 11176e6b1bd1SAl Viro */ 11186e6b1bd1SAl Viro mutex_unlock(&dir->i_mutex); 11192e2e88eaSNick Piggin goto found; 11201da177e4SLinus Torvalds 11211da177e4SLinus Torvalds need_revalidate: 1122bcdc5e01SIan Kent dentry = do_revalidate(dentry, nd); 1123bcdc5e01SIan Kent if (!dentry) 11241da177e4SLinus Torvalds goto need_lookup; 1125bcdc5e01SIan Kent if (IS_ERR(dentry)) 1126bcdc5e01SIan Kent goto fail; 1127bcdc5e01SIan Kent goto done; 11281da177e4SLinus Torvalds 11291da177e4SLinus Torvalds fail: 11301da177e4SLinus Torvalds return PTR_ERR(dentry); 11311da177e4SLinus Torvalds } 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds /* 1134ac278a9cSAl Viro * This is a temporary kludge to deal with "automount" symlinks; proper 1135ac278a9cSAl Viro * solution is to trigger them on follow_mount(), so that do_lookup() 1136ac278a9cSAl Viro * would DTRT. To be killed before 2.6.34-final. 1137ac278a9cSAl Viro */ 1138ac278a9cSAl Viro static inline int follow_on_final(struct inode *inode, unsigned lookup_flags) 1139ac278a9cSAl Viro { 1140ac278a9cSAl Viro return inode && unlikely(inode->i_op->follow_link) && 1141ac278a9cSAl Viro ((lookup_flags & LOOKUP_FOLLOW) || S_ISDIR(inode->i_mode)); 1142ac278a9cSAl Viro } 1143ac278a9cSAl Viro 1144ac278a9cSAl Viro /* 11451da177e4SLinus Torvalds * Name resolution. 1146ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1147ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 11481da177e4SLinus Torvalds * 1149ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1150ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 11511da177e4SLinus Torvalds */ 11526de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 11531da177e4SLinus Torvalds { 11541da177e4SLinus Torvalds struct path next; 11551da177e4SLinus Torvalds int err; 11561da177e4SLinus Torvalds unsigned int lookup_flags = nd->flags; 11571da177e4SLinus Torvalds 11581da177e4SLinus Torvalds while (*name=='/') 11591da177e4SLinus Torvalds name++; 11601da177e4SLinus Torvalds if (!*name) 11611da177e4SLinus Torvalds goto return_reval; 11621da177e4SLinus Torvalds 11631da177e4SLinus Torvalds if (nd->depth) 1164f55eab82STrond Myklebust lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE); 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 11671da177e4SLinus Torvalds for(;;) { 116831e6b01fSNick Piggin struct inode *inode; 11691da177e4SLinus Torvalds unsigned long hash; 11701da177e4SLinus Torvalds struct qstr this; 11711da177e4SLinus Torvalds unsigned int c; 11721da177e4SLinus Torvalds 1173cdce5d6bSTrond Myklebust nd->flags |= LOOKUP_CONTINUE; 117431e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 1175b74c79e9SNick Piggin err = exec_permission(nd->inode, IPERM_FLAG_RCU); 117631e6b01fSNick Piggin if (err == -ECHILD) { 117731e6b01fSNick Piggin if (nameidata_drop_rcu(nd)) 117831e6b01fSNick Piggin return -ECHILD; 117931e6b01fSNick Piggin goto exec_again; 118031e6b01fSNick Piggin } 118131e6b01fSNick Piggin } else { 118231e6b01fSNick Piggin exec_again: 1183b74c79e9SNick Piggin err = exec_permission(nd->inode, 0); 118431e6b01fSNick Piggin } 11851da177e4SLinus Torvalds if (err) 11861da177e4SLinus Torvalds break; 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds this.name = name; 11891da177e4SLinus Torvalds c = *(const unsigned char *)name; 11901da177e4SLinus Torvalds 11911da177e4SLinus Torvalds hash = init_name_hash(); 11921da177e4SLinus Torvalds do { 11931da177e4SLinus Torvalds name++; 11941da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 11951da177e4SLinus Torvalds c = *(const unsigned char *)name; 11961da177e4SLinus Torvalds } while (c && (c != '/')); 11971da177e4SLinus Torvalds this.len = name - (const char *) this.name; 11981da177e4SLinus Torvalds this.hash = end_name_hash(hash); 11991da177e4SLinus Torvalds 12001da177e4SLinus Torvalds /* remove trailing slashes? */ 12011da177e4SLinus Torvalds if (!c) 12021da177e4SLinus Torvalds goto last_component; 12031da177e4SLinus Torvalds while (*++name == '/'); 12041da177e4SLinus Torvalds if (!*name) 12051da177e4SLinus Torvalds goto last_with_slashes; 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds /* 12081da177e4SLinus Torvalds * "." and ".." are special - ".." especially so because it has 12091da177e4SLinus Torvalds * to be able to know about the current root directory and 12101da177e4SLinus Torvalds * parent relationships. 12111da177e4SLinus Torvalds */ 12121da177e4SLinus Torvalds if (this.name[0] == '.') switch (this.len) { 12131da177e4SLinus Torvalds default: 12141da177e4SLinus Torvalds break; 12151da177e4SLinus Torvalds case 2: 12161da177e4SLinus Torvalds if (this.name[1] != '.') 12171da177e4SLinus Torvalds break; 121831e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 121931e6b01fSNick Piggin if (follow_dotdot_rcu(nd)) 122031e6b01fSNick Piggin return -ECHILD; 122131e6b01fSNick Piggin } else 122258c465ebSAl Viro follow_dotdot(nd); 12231da177e4SLinus Torvalds /* fallthrough */ 12241da177e4SLinus Torvalds case 1: 12251da177e4SLinus Torvalds continue; 12261da177e4SLinus Torvalds } 12271da177e4SLinus Torvalds /* This does the actual lookups.. */ 122831e6b01fSNick Piggin err = do_lookup(nd, &this, &next, &inode); 12291da177e4SLinus Torvalds if (err) 12301da177e4SLinus Torvalds break; 12311da177e4SLinus Torvalds err = -ENOENT; 12321da177e4SLinus Torvalds if (!inode) 12331da177e4SLinus Torvalds goto out_dput; 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds if (inode->i_op->follow_link) { 123631e6b01fSNick Piggin /* We commonly drop rcu-walk here */ 123731e6b01fSNick Piggin if (nameidata_dentry_drop_rcu_maybe(nd, next.dentry)) 123831e6b01fSNick Piggin return -ECHILD; 123931e6b01fSNick Piggin BUG_ON(inode != next.dentry->d_inode); 124090ebe565SAl Viro err = do_follow_link(&next, nd); 12411da177e4SLinus Torvalds if (err) 12421da177e4SLinus Torvalds goto return_err; 124331e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 12441da177e4SLinus Torvalds err = -ENOENT; 124531e6b01fSNick Piggin if (!nd->inode) 12461da177e4SLinus Torvalds break; 124731e6b01fSNick Piggin } else { 124809dd17d3SMiklos Szeredi path_to_nameidata(&next, nd); 124931e6b01fSNick Piggin nd->inode = inode; 125031e6b01fSNick Piggin } 12511da177e4SLinus Torvalds err = -ENOTDIR; 125231e6b01fSNick Piggin if (!nd->inode->i_op->lookup) 12531da177e4SLinus Torvalds break; 12541da177e4SLinus Torvalds continue; 12551da177e4SLinus Torvalds /* here ends the main loop */ 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds last_with_slashes: 12581da177e4SLinus Torvalds lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 12591da177e4SLinus Torvalds last_component: 1260f55eab82STrond Myklebust /* Clear LOOKUP_CONTINUE iff it was previously unset */ 1261f55eab82STrond Myklebust nd->flags &= lookup_flags | ~LOOKUP_CONTINUE; 12621da177e4SLinus Torvalds if (lookup_flags & LOOKUP_PARENT) 12631da177e4SLinus Torvalds goto lookup_parent; 12641da177e4SLinus Torvalds if (this.name[0] == '.') switch (this.len) { 12651da177e4SLinus Torvalds default: 12661da177e4SLinus Torvalds break; 12671da177e4SLinus Torvalds case 2: 12681da177e4SLinus Torvalds if (this.name[1] != '.') 12691da177e4SLinus Torvalds break; 127031e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 127131e6b01fSNick Piggin if (follow_dotdot_rcu(nd)) 127231e6b01fSNick Piggin return -ECHILD; 127331e6b01fSNick Piggin } else 127458c465ebSAl Viro follow_dotdot(nd); 12751da177e4SLinus Torvalds /* fallthrough */ 12761da177e4SLinus Torvalds case 1: 12771da177e4SLinus Torvalds goto return_reval; 12781da177e4SLinus Torvalds } 127931e6b01fSNick Piggin err = do_lookup(nd, &this, &next, &inode); 12801da177e4SLinus Torvalds if (err) 12811da177e4SLinus Torvalds break; 1282ac278a9cSAl Viro if (follow_on_final(inode, lookup_flags)) { 128331e6b01fSNick Piggin if (nameidata_dentry_drop_rcu_maybe(nd, next.dentry)) 128431e6b01fSNick Piggin return -ECHILD; 128531e6b01fSNick Piggin BUG_ON(inode != next.dentry->d_inode); 128690ebe565SAl Viro err = do_follow_link(&next, nd); 12871da177e4SLinus Torvalds if (err) 12881da177e4SLinus Torvalds goto return_err; 128931e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 129031e6b01fSNick Piggin } else { 129109dd17d3SMiklos Szeredi path_to_nameidata(&next, nd); 129231e6b01fSNick Piggin nd->inode = inode; 129331e6b01fSNick Piggin } 12941da177e4SLinus Torvalds err = -ENOENT; 129531e6b01fSNick Piggin if (!nd->inode) 12961da177e4SLinus Torvalds break; 12971da177e4SLinus Torvalds if (lookup_flags & LOOKUP_DIRECTORY) { 12981da177e4SLinus Torvalds err = -ENOTDIR; 129931e6b01fSNick Piggin if (!nd->inode->i_op->lookup) 13001da177e4SLinus Torvalds break; 13011da177e4SLinus Torvalds } 13021da177e4SLinus Torvalds goto return_base; 13031da177e4SLinus Torvalds lookup_parent: 13041da177e4SLinus Torvalds nd->last = this; 13051da177e4SLinus Torvalds nd->last_type = LAST_NORM; 13061da177e4SLinus Torvalds if (this.name[0] != '.') 13071da177e4SLinus Torvalds goto return_base; 13081da177e4SLinus Torvalds if (this.len == 1) 13091da177e4SLinus Torvalds nd->last_type = LAST_DOT; 13101da177e4SLinus Torvalds else if (this.len == 2 && this.name[1] == '.') 13111da177e4SLinus Torvalds nd->last_type = LAST_DOTDOT; 13121da177e4SLinus Torvalds else 13131da177e4SLinus Torvalds goto return_base; 13141da177e4SLinus Torvalds return_reval: 13151da177e4SLinus Torvalds /* 13161da177e4SLinus Torvalds * We bypassed the ordinary revalidation routines. 13171da177e4SLinus Torvalds * We may need to check the cached dentry for staleness. 13181da177e4SLinus Torvalds */ 1319fb045adbSNick Piggin if (need_reval_dot(nd->path.dentry)) { 13201da177e4SLinus Torvalds /* Note: we do not d_invalidate() */ 132134286d66SNick Piggin err = d_revalidate(nd->path.dentry, nd); 132234286d66SNick Piggin if (!err) 132334286d66SNick Piggin err = -ESTALE; 132434286d66SNick Piggin if (err < 0) 13251da177e4SLinus Torvalds break; 13261da177e4SLinus Torvalds } 13271da177e4SLinus Torvalds return_base: 132831e6b01fSNick Piggin if (nameidata_drop_rcu_last_maybe(nd)) 132931e6b01fSNick Piggin return -ECHILD; 13301da177e4SLinus Torvalds return 0; 13311da177e4SLinus Torvalds out_dput: 133231e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) 13331d957f9bSJan Blunck path_put_conditional(&next, nd); 13341da177e4SLinus Torvalds break; 13351da177e4SLinus Torvalds } 133631e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) 13371d957f9bSJan Blunck path_put(&nd->path); 13381da177e4SLinus Torvalds return_err: 13391da177e4SLinus Torvalds return err; 13401da177e4SLinus Torvalds } 13411da177e4SLinus Torvalds 134231e6b01fSNick Piggin static inline int path_walk_rcu(const char *name, struct nameidata *nd) 134331e6b01fSNick Piggin { 134431e6b01fSNick Piggin current->total_link_count = 0; 134531e6b01fSNick Piggin 134631e6b01fSNick Piggin return link_path_walk(name, nd); 134731e6b01fSNick Piggin } 134831e6b01fSNick Piggin 134931e6b01fSNick Piggin static inline int path_walk_simple(const char *name, struct nameidata *nd) 135031e6b01fSNick Piggin { 135131e6b01fSNick Piggin current->total_link_count = 0; 135231e6b01fSNick Piggin 135331e6b01fSNick Piggin return link_path_walk(name, nd); 135431e6b01fSNick Piggin } 135531e6b01fSNick Piggin 1356fc9b52cdSHarvey Harrison static int path_walk(const char *name, struct nameidata *nd) 13571da177e4SLinus Torvalds { 13586de88d72SAl Viro struct path save = nd->path; 13596de88d72SAl Viro int result; 13606de88d72SAl Viro 13611da177e4SLinus Torvalds current->total_link_count = 0; 13626de88d72SAl Viro 13636de88d72SAl Viro /* make sure the stuff we saved doesn't go away */ 13646de88d72SAl Viro path_get(&save); 13656de88d72SAl Viro 13666de88d72SAl Viro result = link_path_walk(name, nd); 13676de88d72SAl Viro if (result == -ESTALE) { 13686de88d72SAl Viro /* nd->path had been dropped */ 13696de88d72SAl Viro current->total_link_count = 0; 13706de88d72SAl Viro nd->path = save; 13716de88d72SAl Viro path_get(&nd->path); 13726de88d72SAl Viro nd->flags |= LOOKUP_REVAL; 13736de88d72SAl Viro result = link_path_walk(name, nd); 13746de88d72SAl Viro } 13756de88d72SAl Viro 13766de88d72SAl Viro path_put(&save); 13776de88d72SAl Viro 13786de88d72SAl Viro return result; 13791da177e4SLinus Torvalds } 13801da177e4SLinus Torvalds 138131e6b01fSNick Piggin static void path_finish_rcu(struct nameidata *nd) 138231e6b01fSNick Piggin { 138331e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 138431e6b01fSNick Piggin /* RCU dangling. Cancel it. */ 138531e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 138631e6b01fSNick Piggin nd->root.mnt = NULL; 138731e6b01fSNick Piggin rcu_read_unlock(); 138831e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 138931e6b01fSNick Piggin } 139031e6b01fSNick Piggin if (nd->file) 139131e6b01fSNick Piggin fput(nd->file); 139231e6b01fSNick Piggin } 139331e6b01fSNick Piggin 139431e6b01fSNick Piggin static int path_init_rcu(int dfd, const char *name, unsigned int flags, struct nameidata *nd) 139531e6b01fSNick Piggin { 139631e6b01fSNick Piggin int retval = 0; 139731e6b01fSNick Piggin int fput_needed; 139831e6b01fSNick Piggin struct file *file; 139931e6b01fSNick Piggin 140031e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 140131e6b01fSNick Piggin nd->flags = flags | LOOKUP_RCU; 140231e6b01fSNick Piggin nd->depth = 0; 140331e6b01fSNick Piggin nd->root.mnt = NULL; 140431e6b01fSNick Piggin nd->file = NULL; 140531e6b01fSNick Piggin 140631e6b01fSNick Piggin if (*name=='/') { 140731e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1408c28cc364SNick Piggin unsigned seq; 140931e6b01fSNick Piggin 141031e6b01fSNick Piggin br_read_lock(vfsmount_lock); 141131e6b01fSNick Piggin rcu_read_lock(); 141231e6b01fSNick Piggin 1413c28cc364SNick Piggin do { 1414c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 141531e6b01fSNick Piggin nd->root = fs->root; 141631e6b01fSNick Piggin nd->path = nd->root; 1417c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1418c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 141931e6b01fSNick Piggin 142031e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 142131e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1422c28cc364SNick Piggin unsigned seq; 142331e6b01fSNick Piggin 142431e6b01fSNick Piggin br_read_lock(vfsmount_lock); 142531e6b01fSNick Piggin rcu_read_lock(); 142631e6b01fSNick Piggin 1427c28cc364SNick Piggin do { 1428c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 142931e6b01fSNick Piggin nd->path = fs->pwd; 1430c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1431c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1432c28cc364SNick Piggin 143331e6b01fSNick Piggin } else { 143431e6b01fSNick Piggin struct dentry *dentry; 143531e6b01fSNick Piggin 143631e6b01fSNick Piggin file = fget_light(dfd, &fput_needed); 143731e6b01fSNick Piggin retval = -EBADF; 143831e6b01fSNick Piggin if (!file) 143931e6b01fSNick Piggin goto out_fail; 144031e6b01fSNick Piggin 144131e6b01fSNick Piggin dentry = file->f_path.dentry; 144231e6b01fSNick Piggin 144331e6b01fSNick Piggin retval = -ENOTDIR; 144431e6b01fSNick Piggin if (!S_ISDIR(dentry->d_inode->i_mode)) 144531e6b01fSNick Piggin goto fput_fail; 144631e6b01fSNick Piggin 144731e6b01fSNick Piggin retval = file_permission(file, MAY_EXEC); 144831e6b01fSNick Piggin if (retval) 144931e6b01fSNick Piggin goto fput_fail; 145031e6b01fSNick Piggin 145131e6b01fSNick Piggin nd->path = file->f_path; 145231e6b01fSNick Piggin if (fput_needed) 145331e6b01fSNick Piggin nd->file = file; 145431e6b01fSNick Piggin 1455c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 145631e6b01fSNick Piggin br_read_lock(vfsmount_lock); 145731e6b01fSNick Piggin rcu_read_lock(); 145831e6b01fSNick Piggin } 145931e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 146031e6b01fSNick Piggin return 0; 146131e6b01fSNick Piggin 146231e6b01fSNick Piggin fput_fail: 146331e6b01fSNick Piggin fput_light(file, fput_needed); 146431e6b01fSNick Piggin out_fail: 146531e6b01fSNick Piggin return retval; 146631e6b01fSNick Piggin } 146731e6b01fSNick Piggin 14689b4a9b14SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd) 14691da177e4SLinus Torvalds { 1470ea3834d9SPrasanna Meda int retval = 0; 1471170aa3d0SUlrich Drepper int fput_needed; 1472170aa3d0SUlrich Drepper struct file *file; 14731da177e4SLinus Torvalds 14741da177e4SLinus Torvalds nd->last_type = LAST_ROOT; /* if there are only slashes... */ 14751da177e4SLinus Torvalds nd->flags = flags; 14761da177e4SLinus Torvalds nd->depth = 0; 14772a737871SAl Viro nd->root.mnt = NULL; 14781da177e4SLinus Torvalds 14791da177e4SLinus Torvalds if (*name=='/') { 14802a737871SAl Viro set_root(nd); 14812a737871SAl Viro nd->path = nd->root; 14822a737871SAl Viro path_get(&nd->root); 14835590ff0dSUlrich Drepper } else if (dfd == AT_FDCWD) { 1484f7ad3c6bSMiklos Szeredi get_fs_pwd(current->fs, &nd->path); 14855590ff0dSUlrich Drepper } else { 14865590ff0dSUlrich Drepper struct dentry *dentry; 14875590ff0dSUlrich Drepper 14885590ff0dSUlrich Drepper file = fget_light(dfd, &fput_needed); 14895590ff0dSUlrich Drepper retval = -EBADF; 1490170aa3d0SUlrich Drepper if (!file) 14916d09bb62STrond Myklebust goto out_fail; 14925590ff0dSUlrich Drepper 14930f7fc9e4SJosef "Jeff" Sipek dentry = file->f_path.dentry; 14945590ff0dSUlrich Drepper 14955590ff0dSUlrich Drepper retval = -ENOTDIR; 1496170aa3d0SUlrich Drepper if (!S_ISDIR(dentry->d_inode->i_mode)) 14976d09bb62STrond Myklebust goto fput_fail; 14985590ff0dSUlrich Drepper 14995590ff0dSUlrich Drepper retval = file_permission(file, MAY_EXEC); 1500170aa3d0SUlrich Drepper if (retval) 15016d09bb62STrond Myklebust goto fput_fail; 15025590ff0dSUlrich Drepper 15035dd784d0SJan Blunck nd->path = file->f_path; 15045dd784d0SJan Blunck path_get(&file->f_path); 15055590ff0dSUlrich Drepper 15065590ff0dSUlrich Drepper fput_light(file, fput_needed); 15071da177e4SLinus Torvalds } 150831e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 15099b4a9b14SAl Viro return 0; 15102dfdd266SJosef 'Jeff' Sipek 15119b4a9b14SAl Viro fput_fail: 15129b4a9b14SAl Viro fput_light(file, fput_needed); 15139b4a9b14SAl Viro out_fail: 15149b4a9b14SAl Viro return retval; 15159b4a9b14SAl Viro } 15169b4a9b14SAl Viro 15179b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 15189b4a9b14SAl Viro static int do_path_lookup(int dfd, const char *name, 15199b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 15209b4a9b14SAl Viro { 152131e6b01fSNick Piggin int retval; 152231e6b01fSNick Piggin 152331e6b01fSNick Piggin /* 152431e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 152531e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 152631e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 152731e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 152831e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 152931e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 153031e6b01fSNick Piggin * analogue, foo_rcu(). 153131e6b01fSNick Piggin * 153231e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 153331e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 153431e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 153531e6b01fSNick Piggin * be able to complete). 153631e6b01fSNick Piggin */ 153731e6b01fSNick Piggin retval = path_init_rcu(dfd, name, flags, nd); 153831e6b01fSNick Piggin if (unlikely(retval)) 153931e6b01fSNick Piggin return retval; 154031e6b01fSNick Piggin retval = path_walk_rcu(name, nd); 154131e6b01fSNick Piggin path_finish_rcu(nd); 15422a737871SAl Viro if (nd->root.mnt) { 15432a737871SAl Viro path_put(&nd->root); 15442a737871SAl Viro nd->root.mnt = NULL; 15452a737871SAl Viro } 154631e6b01fSNick Piggin 154731e6b01fSNick Piggin if (unlikely(retval == -ECHILD || retval == -ESTALE)) { 154831e6b01fSNick Piggin /* slower, locked walk */ 154931e6b01fSNick Piggin if (retval == -ESTALE) 155031e6b01fSNick Piggin flags |= LOOKUP_REVAL; 155131e6b01fSNick Piggin retval = path_init(dfd, name, flags, nd); 155231e6b01fSNick Piggin if (unlikely(retval)) 155331e6b01fSNick Piggin return retval; 155431e6b01fSNick Piggin retval = path_walk(name, nd); 155531e6b01fSNick Piggin if (nd->root.mnt) { 155631e6b01fSNick Piggin path_put(&nd->root); 155731e6b01fSNick Piggin nd->root.mnt = NULL; 155831e6b01fSNick Piggin } 155931e6b01fSNick Piggin } 156031e6b01fSNick Piggin 156131e6b01fSNick Piggin if (likely(!retval)) { 156231e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) { 156331e6b01fSNick Piggin if (nd->path.dentry && nd->inode) 156431e6b01fSNick Piggin audit_inode(name, nd->path.dentry); 156531e6b01fSNick Piggin } 156631e6b01fSNick Piggin } 156731e6b01fSNick Piggin 1568170aa3d0SUlrich Drepper return retval; 15691da177e4SLinus Torvalds } 15701da177e4SLinus Torvalds 1571fc9b52cdSHarvey Harrison int path_lookup(const char *name, unsigned int flags, 15725590ff0dSUlrich Drepper struct nameidata *nd) 15735590ff0dSUlrich Drepper { 15745590ff0dSUlrich Drepper return do_path_lookup(AT_FDCWD, name, flags, nd); 15755590ff0dSUlrich Drepper } 15765590ff0dSUlrich Drepper 1577d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1578d1811465SAl Viro { 1579d1811465SAl Viro struct nameidata nd; 1580d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1581d1811465SAl Viro if (!res) 1582d1811465SAl Viro *path = nd.path; 1583d1811465SAl Viro return res; 1584d1811465SAl Viro } 1585d1811465SAl Viro 158616f18200SJosef 'Jeff' Sipek /** 158716f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 158816f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 158916f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 159016f18200SJosef 'Jeff' Sipek * @name: pointer to file name 159116f18200SJosef 'Jeff' Sipek * @flags: lookup flags 159216f18200SJosef 'Jeff' Sipek * @nd: pointer to nameidata 159316f18200SJosef 'Jeff' Sipek */ 159416f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 159516f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 159616f18200SJosef 'Jeff' Sipek struct nameidata *nd) 159716f18200SJosef 'Jeff' Sipek { 159816f18200SJosef 'Jeff' Sipek int retval; 159916f18200SJosef 'Jeff' Sipek 160016f18200SJosef 'Jeff' Sipek /* same as do_path_lookup */ 160116f18200SJosef 'Jeff' Sipek nd->last_type = LAST_ROOT; 160216f18200SJosef 'Jeff' Sipek nd->flags = flags; 160316f18200SJosef 'Jeff' Sipek nd->depth = 0; 160416f18200SJosef 'Jeff' Sipek 1605c8e7f449SJan Blunck nd->path.dentry = dentry; 1606c8e7f449SJan Blunck nd->path.mnt = mnt; 1607c8e7f449SJan Blunck path_get(&nd->path); 16085b857119SAl Viro nd->root = nd->path; 16095b857119SAl Viro path_get(&nd->root); 161031e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 161116f18200SJosef 'Jeff' Sipek 161216f18200SJosef 'Jeff' Sipek retval = path_walk(name, nd); 16134ac91378SJan Blunck if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry && 161431e6b01fSNick Piggin nd->inode)) 16154ac91378SJan Blunck audit_inode(name, nd->path.dentry); 161616f18200SJosef 'Jeff' Sipek 16172a737871SAl Viro path_put(&nd->root); 16182a737871SAl Viro nd->root.mnt = NULL; 161916f18200SJosef 'Jeff' Sipek 16202a737871SAl Viro return retval; 162116f18200SJosef 'Jeff' Sipek } 162216f18200SJosef 'Jeff' Sipek 1623eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name, 1624eead1911SChristoph Hellwig struct dentry *base, struct nameidata *nd) 16251da177e4SLinus Torvalds { 162681fca444SChristoph Hellwig struct inode *inode = base->d_inode; 16271da177e4SLinus Torvalds struct dentry *dentry; 16281da177e4SLinus Torvalds int err; 16291da177e4SLinus Torvalds 1630b74c79e9SNick Piggin err = exec_permission(inode, 0); 163181fca444SChristoph Hellwig if (err) 163281fca444SChristoph Hellwig return ERR_PTR(err); 16331da177e4SLinus Torvalds 16341da177e4SLinus Torvalds /* 16351da177e4SLinus Torvalds * See if the low-level filesystem might want 16361da177e4SLinus Torvalds * to use its own hash.. 16371da177e4SLinus Torvalds */ 1638fb045adbSNick Piggin if (base->d_flags & DCACHE_OP_HASH) { 1639b1e6a015SNick Piggin err = base->d_op->d_hash(base, inode, name); 16401da177e4SLinus Torvalds dentry = ERR_PTR(err); 16411da177e4SLinus Torvalds if (err < 0) 16421da177e4SLinus Torvalds goto out; 16431da177e4SLinus Torvalds } 16441da177e4SLinus Torvalds 1645b04f784eSNick Piggin /* 1646b04f784eSNick Piggin * Don't bother with __d_lookup: callers are for creat as 1647b04f784eSNick Piggin * well as unlink, so a lot of the time it would cost 1648b04f784eSNick Piggin * a double lookup. 16496e6b1bd1SAl Viro */ 16506e6b1bd1SAl Viro dentry = d_lookup(base, name); 16516e6b1bd1SAl Viro 1652fb045adbSNick Piggin if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) 16536e6b1bd1SAl Viro dentry = do_revalidate(dentry, nd); 16546e6b1bd1SAl Viro 16551da177e4SLinus Torvalds if (!dentry) 1656baa03890SNick Piggin dentry = d_alloc_and_lookup(base, name, nd); 16571da177e4SLinus Torvalds out: 16581da177e4SLinus Torvalds return dentry; 16591da177e4SLinus Torvalds } 16601da177e4SLinus Torvalds 1661057f6c01SJames Morris /* 1662057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1663057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1664057f6c01SJames Morris * SMP-safe. 1665057f6c01SJames Morris */ 1666a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 16671da177e4SLinus Torvalds { 16684ac91378SJan Blunck return __lookup_hash(&nd->last, nd->path.dentry, nd); 16691da177e4SLinus Torvalds } 16701da177e4SLinus Torvalds 1671eead1911SChristoph Hellwig static int __lookup_one_len(const char *name, struct qstr *this, 1672eead1911SChristoph Hellwig struct dentry *base, int len) 16731da177e4SLinus Torvalds { 16741da177e4SLinus Torvalds unsigned long hash; 16751da177e4SLinus Torvalds unsigned int c; 16761da177e4SLinus Torvalds 1677057f6c01SJames Morris this->name = name; 1678057f6c01SJames Morris this->len = len; 16791da177e4SLinus Torvalds if (!len) 1680057f6c01SJames Morris return -EACCES; 16811da177e4SLinus Torvalds 16821da177e4SLinus Torvalds hash = init_name_hash(); 16831da177e4SLinus Torvalds while (len--) { 16841da177e4SLinus Torvalds c = *(const unsigned char *)name++; 16851da177e4SLinus Torvalds if (c == '/' || c == '\0') 1686057f6c01SJames Morris return -EACCES; 16871da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 16881da177e4SLinus Torvalds } 1689057f6c01SJames Morris this->hash = end_name_hash(hash); 1690057f6c01SJames Morris return 0; 1691057f6c01SJames Morris } 16921da177e4SLinus Torvalds 1693eead1911SChristoph Hellwig /** 1694a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1695eead1911SChristoph Hellwig * @name: pathname component to lookup 1696eead1911SChristoph Hellwig * @base: base directory to lookup from 1697eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1698eead1911SChristoph Hellwig * 1699a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1700a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1701eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1702eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1703eead1911SChristoph Hellwig */ 1704057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1705057f6c01SJames Morris { 1706057f6c01SJames Morris int err; 1707057f6c01SJames Morris struct qstr this; 1708057f6c01SJames Morris 17092f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 17102f9092e1SDavid Woodhouse 1711057f6c01SJames Morris err = __lookup_one_len(name, &this, base, len); 1712057f6c01SJames Morris if (err) 1713057f6c01SJames Morris return ERR_PTR(err); 1714eead1911SChristoph Hellwig 171549705b77SChristoph Hellwig return __lookup_hash(&this, base, NULL); 1716057f6c01SJames Morris } 1717057f6c01SJames Morris 17182d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags, 17192d8f3038SAl Viro struct path *path) 17201da177e4SLinus Torvalds { 17212d8f3038SAl Viro struct nameidata nd; 17221da177e4SLinus Torvalds char *tmp = getname(name); 17231da177e4SLinus Torvalds int err = PTR_ERR(tmp); 17241da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 17252d8f3038SAl Viro 17262d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 17272d8f3038SAl Viro 17282d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 17291da177e4SLinus Torvalds putname(tmp); 17302d8f3038SAl Viro if (!err) 17312d8f3038SAl Viro *path = nd.path; 17321da177e4SLinus Torvalds } 17331da177e4SLinus Torvalds return err; 17341da177e4SLinus Torvalds } 17351da177e4SLinus Torvalds 17362ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 17372ad94ae6SAl Viro struct nameidata *nd, char **name) 17382ad94ae6SAl Viro { 17392ad94ae6SAl Viro char *s = getname(path); 17402ad94ae6SAl Viro int error; 17412ad94ae6SAl Viro 17422ad94ae6SAl Viro if (IS_ERR(s)) 17432ad94ae6SAl Viro return PTR_ERR(s); 17442ad94ae6SAl Viro 17452ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 17462ad94ae6SAl Viro if (error) 17472ad94ae6SAl Viro putname(s); 17482ad94ae6SAl Viro else 17492ad94ae6SAl Viro *name = s; 17502ad94ae6SAl Viro 17512ad94ae6SAl Viro return error; 17522ad94ae6SAl Viro } 17532ad94ae6SAl Viro 17541da177e4SLinus Torvalds /* 17551da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 17561da177e4SLinus Torvalds * minimal. 17571da177e4SLinus Torvalds */ 17581da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 17591da177e4SLinus Torvalds { 1760da9592edSDavid Howells uid_t fsuid = current_fsuid(); 1761da9592edSDavid Howells 17621da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 17631da177e4SLinus Torvalds return 0; 1764da9592edSDavid Howells if (inode->i_uid == fsuid) 17651da177e4SLinus Torvalds return 0; 1766da9592edSDavid Howells if (dir->i_uid == fsuid) 17671da177e4SLinus Torvalds return 0; 17681da177e4SLinus Torvalds return !capable(CAP_FOWNER); 17691da177e4SLinus Torvalds } 17701da177e4SLinus Torvalds 17711da177e4SLinus Torvalds /* 17721da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 17731da177e4SLinus Torvalds * whether the type of victim is right. 17741da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 17751da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 17761da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 17771da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 17781da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 17791da177e4SLinus Torvalds * a. be owner of dir, or 17801da177e4SLinus Torvalds * b. be owner of victim, or 17811da177e4SLinus Torvalds * c. have CAP_FOWNER capability 17821da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 17831da177e4SLinus Torvalds * links pointing to it. 17841da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 17851da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 17861da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 17871da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 17881da177e4SLinus Torvalds * nfs_async_unlink(). 17891da177e4SLinus Torvalds */ 1790858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 17911da177e4SLinus Torvalds { 17921da177e4SLinus Torvalds int error; 17931da177e4SLinus Torvalds 17941da177e4SLinus Torvalds if (!victim->d_inode) 17951da177e4SLinus Torvalds return -ENOENT; 17961da177e4SLinus Torvalds 17971da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 1798cccc6bbaSAl Viro audit_inode_child(victim, dir); 17991da177e4SLinus Torvalds 1800f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 18011da177e4SLinus Torvalds if (error) 18021da177e4SLinus Torvalds return error; 18031da177e4SLinus Torvalds if (IS_APPEND(dir)) 18041da177e4SLinus Torvalds return -EPERM; 18051da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 1806f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 18071da177e4SLinus Torvalds return -EPERM; 18081da177e4SLinus Torvalds if (isdir) { 18091da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 18101da177e4SLinus Torvalds return -ENOTDIR; 18111da177e4SLinus Torvalds if (IS_ROOT(victim)) 18121da177e4SLinus Torvalds return -EBUSY; 18131da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 18141da177e4SLinus Torvalds return -EISDIR; 18151da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 18161da177e4SLinus Torvalds return -ENOENT; 18171da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 18181da177e4SLinus Torvalds return -EBUSY; 18191da177e4SLinus Torvalds return 0; 18201da177e4SLinus Torvalds } 18211da177e4SLinus Torvalds 18221da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 18231da177e4SLinus Torvalds * dir. 18241da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 18251da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 18261da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 18271da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 18281da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 18291da177e4SLinus Torvalds */ 1830a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 18311da177e4SLinus Torvalds { 18321da177e4SLinus Torvalds if (child->d_inode) 18331da177e4SLinus Torvalds return -EEXIST; 18341da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 18351da177e4SLinus Torvalds return -ENOENT; 1836f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 18371da177e4SLinus Torvalds } 18381da177e4SLinus Torvalds 18391da177e4SLinus Torvalds /* 18401da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 18411da177e4SLinus Torvalds */ 18421da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 18431da177e4SLinus Torvalds { 18441da177e4SLinus Torvalds struct dentry *p; 18451da177e4SLinus Torvalds 18461da177e4SLinus Torvalds if (p1 == p2) { 1847f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 18481da177e4SLinus Torvalds return NULL; 18491da177e4SLinus Torvalds } 18501da177e4SLinus Torvalds 1851a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 18521da177e4SLinus Torvalds 1853e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 1854e2761a11SOGAWA Hirofumi if (p) { 1855f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 1856f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 18571da177e4SLinus Torvalds return p; 18581da177e4SLinus Torvalds } 18591da177e4SLinus Torvalds 1860e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 1861e2761a11SOGAWA Hirofumi if (p) { 1862f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1863f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 18641da177e4SLinus Torvalds return p; 18651da177e4SLinus Torvalds } 18661da177e4SLinus Torvalds 1867f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1868f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 18691da177e4SLinus Torvalds return NULL; 18701da177e4SLinus Torvalds } 18711da177e4SLinus Torvalds 18721da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 18731da177e4SLinus Torvalds { 18741b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 18751da177e4SLinus Torvalds if (p1 != p2) { 18761b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 1877a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 18781da177e4SLinus Torvalds } 18791da177e4SLinus Torvalds } 18801da177e4SLinus Torvalds 18811da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode, 18821da177e4SLinus Torvalds struct nameidata *nd) 18831da177e4SLinus Torvalds { 1884a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 18851da177e4SLinus Torvalds 18861da177e4SLinus Torvalds if (error) 18871da177e4SLinus Torvalds return error; 18881da177e4SLinus Torvalds 1889acfa4380SAl Viro if (!dir->i_op->create) 18901da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 18911da177e4SLinus Torvalds mode &= S_IALLUGO; 18921da177e4SLinus Torvalds mode |= S_IFREG; 18931da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 18941da177e4SLinus Torvalds if (error) 18951da177e4SLinus Torvalds return error; 18961da177e4SLinus Torvalds error = dir->i_op->create(dir, dentry, mode, nd); 1897a74574aaSStephen Smalley if (!error) 1898f38aa942SAmy Griffis fsnotify_create(dir, dentry); 18991da177e4SLinus Torvalds return error; 19001da177e4SLinus Torvalds } 19011da177e4SLinus Torvalds 19023fb64190SChristoph Hellwig int may_open(struct path *path, int acc_mode, int flag) 19031da177e4SLinus Torvalds { 19043fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 19051da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 19061da177e4SLinus Torvalds int error; 19071da177e4SLinus Torvalds 19081da177e4SLinus Torvalds if (!inode) 19091da177e4SLinus Torvalds return -ENOENT; 19101da177e4SLinus Torvalds 1911c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 1912c8fe8f30SChristoph Hellwig case S_IFLNK: 19131da177e4SLinus Torvalds return -ELOOP; 1914c8fe8f30SChristoph Hellwig case S_IFDIR: 1915c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 19161da177e4SLinus Torvalds return -EISDIR; 1917c8fe8f30SChristoph Hellwig break; 1918c8fe8f30SChristoph Hellwig case S_IFBLK: 1919c8fe8f30SChristoph Hellwig case S_IFCHR: 19203fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 19211da177e4SLinus Torvalds return -EACCES; 1922c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 1923c8fe8f30SChristoph Hellwig case S_IFIFO: 1924c8fe8f30SChristoph Hellwig case S_IFSOCK: 19251da177e4SLinus Torvalds flag &= ~O_TRUNC; 1926c8fe8f30SChristoph Hellwig break; 19274a3fd211SDave Hansen } 1928b41572e9SDave Hansen 19293fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 1930b41572e9SDave Hansen if (error) 1931b41572e9SDave Hansen return error; 19326146f0d5SMimi Zohar 19331da177e4SLinus Torvalds /* 19341da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 19351da177e4SLinus Torvalds */ 19361da177e4SLinus Torvalds if (IS_APPEND(inode)) { 19378737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 19387715b521SAl Viro return -EPERM; 19391da177e4SLinus Torvalds if (flag & O_TRUNC) 19407715b521SAl Viro return -EPERM; 19411da177e4SLinus Torvalds } 19421da177e4SLinus Torvalds 19431da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 19447715b521SAl Viro if (flag & O_NOATIME && !is_owner_or_cap(inode)) 19457715b521SAl Viro return -EPERM; 19461da177e4SLinus Torvalds 19471da177e4SLinus Torvalds /* 19481da177e4SLinus Torvalds * Ensure there are no outstanding leases on the file. 19491da177e4SLinus Torvalds */ 1950b65a9cfcSAl Viro return break_lease(inode, flag); 19517715b521SAl Viro } 19527715b521SAl Viro 1953e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 19547715b521SAl Viro { 1955e1181ee6SJeff Layton struct path *path = &filp->f_path; 19567715b521SAl Viro struct inode *inode = path->dentry->d_inode; 19577715b521SAl Viro int error = get_write_access(inode); 19581da177e4SLinus Torvalds if (error) 19597715b521SAl Viro return error; 19601da177e4SLinus Torvalds /* 19611da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 19621da177e4SLinus Torvalds */ 19631da177e4SLinus Torvalds error = locks_verify_locked(inode); 1964be6d3e56SKentaro Takeda if (!error) 1965ea0d3ab2STetsuo Handa error = security_path_truncate(path); 19661da177e4SLinus Torvalds if (!error) { 19677715b521SAl Viro error = do_truncate(path->dentry, 0, 1968d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 1969e1181ee6SJeff Layton filp); 19701da177e4SLinus Torvalds } 19711da177e4SLinus Torvalds put_write_access(inode); 1972acd0c935SMimi Zohar return error; 19731da177e4SLinus Torvalds } 19741da177e4SLinus Torvalds 1975d57999e1SDave Hansen /* 1976d57999e1SDave Hansen * Be careful about ever adding any more callers of this 1977d57999e1SDave Hansen * function. Its flags must be in the namei format, not 1978d57999e1SDave Hansen * what get passed to sys_open(). 1979d57999e1SDave Hansen */ 1980d57999e1SDave Hansen static int __open_namei_create(struct nameidata *nd, struct path *path, 19818737c930SAl Viro int open_flag, int mode) 1982aab520e2SDave Hansen { 1983aab520e2SDave Hansen int error; 19844ac91378SJan Blunck struct dentry *dir = nd->path.dentry; 1985aab520e2SDave Hansen 1986aab520e2SDave Hansen if (!IS_POSIXACL(dir->d_inode)) 1987ce3b0f8dSAl Viro mode &= ~current_umask(); 1988be6d3e56SKentaro Takeda error = security_path_mknod(&nd->path, path->dentry, mode, 0); 1989be6d3e56SKentaro Takeda if (error) 1990be6d3e56SKentaro Takeda goto out_unlock; 1991aab520e2SDave Hansen error = vfs_create(dir->d_inode, path->dentry, mode, nd); 1992be6d3e56SKentaro Takeda out_unlock: 1993aab520e2SDave Hansen mutex_unlock(&dir->d_inode->i_mutex); 19944ac91378SJan Blunck dput(nd->path.dentry); 19954ac91378SJan Blunck nd->path.dentry = path->dentry; 199631e6b01fSNick Piggin 1997aab520e2SDave Hansen if (error) 1998aab520e2SDave Hansen return error; 1999aab520e2SDave Hansen /* Don't check for write permission, don't truncate */ 20008737c930SAl Viro return may_open(&nd->path, 0, open_flag & ~O_TRUNC); 2001aab520e2SDave Hansen } 2002aab520e2SDave Hansen 20031da177e4SLinus Torvalds /* 2004d57999e1SDave Hansen * Note that while the flag value (low two bits) for sys_open means: 2005d57999e1SDave Hansen * 00 - read-only 2006d57999e1SDave Hansen * 01 - write-only 2007d57999e1SDave Hansen * 10 - read-write 2008d57999e1SDave Hansen * 11 - special 2009d57999e1SDave Hansen * it is changed into 2010d57999e1SDave Hansen * 00 - no permissions needed 2011d57999e1SDave Hansen * 01 - read-permission 2012d57999e1SDave Hansen * 10 - write-permission 2013d57999e1SDave Hansen * 11 - read-write 2014d57999e1SDave Hansen * for the internal routines (ie open_namei()/follow_link() etc) 2015d57999e1SDave Hansen * This is more logical, and also allows the 00 "no perm needed" 2016d57999e1SDave Hansen * to be used for symlinks (where the permissions are checked 2017d57999e1SDave Hansen * later). 2018d57999e1SDave Hansen * 2019d57999e1SDave Hansen */ 2020d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2021d57999e1SDave Hansen { 2022d57999e1SDave Hansen if ((flag+1) & O_ACCMODE) 2023d57999e1SDave Hansen flag++; 2024d57999e1SDave Hansen return flag; 2025d57999e1SDave Hansen } 2026d57999e1SDave Hansen 20277715b521SAl Viro static int open_will_truncate(int flag, struct inode *inode) 20284a3fd211SDave Hansen { 2029d57999e1SDave Hansen /* 20304a3fd211SDave Hansen * We'll never write to the fs underlying 20314a3fd211SDave Hansen * a device file. 20324a3fd211SDave Hansen */ 20334a3fd211SDave Hansen if (special_file(inode->i_mode)) 20344a3fd211SDave Hansen return 0; 20354a3fd211SDave Hansen return (flag & O_TRUNC); 20364a3fd211SDave Hansen } 20374a3fd211SDave Hansen 2038648fa861SAl Viro static struct file *finish_open(struct nameidata *nd, 20399a66179eSAl Viro int open_flag, int acc_mode) 2040648fa861SAl Viro { 2041648fa861SAl Viro struct file *filp; 2042648fa861SAl Viro int will_truncate; 2043648fa861SAl Viro int error; 2044648fa861SAl Viro 20459a66179eSAl Viro will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode); 2046648fa861SAl Viro if (will_truncate) { 2047648fa861SAl Viro error = mnt_want_write(nd->path.mnt); 2048648fa861SAl Viro if (error) 2049648fa861SAl Viro goto exit; 2050648fa861SAl Viro } 2051648fa861SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2052648fa861SAl Viro if (error) { 2053648fa861SAl Viro if (will_truncate) 2054648fa861SAl Viro mnt_drop_write(nd->path.mnt); 2055648fa861SAl Viro goto exit; 2056648fa861SAl Viro } 2057648fa861SAl Viro filp = nameidata_to_filp(nd); 2058648fa861SAl Viro if (!IS_ERR(filp)) { 2059648fa861SAl Viro error = ima_file_check(filp, acc_mode); 2060648fa861SAl Viro if (error) { 2061648fa861SAl Viro fput(filp); 2062648fa861SAl Viro filp = ERR_PTR(error); 2063648fa861SAl Viro } 2064648fa861SAl Viro } 2065648fa861SAl Viro if (!IS_ERR(filp)) { 2066648fa861SAl Viro if (will_truncate) { 2067e1181ee6SJeff Layton error = handle_truncate(filp); 2068648fa861SAl Viro if (error) { 2069648fa861SAl Viro fput(filp); 2070648fa861SAl Viro filp = ERR_PTR(error); 2071648fa861SAl Viro } 2072648fa861SAl Viro } 2073648fa861SAl Viro } 2074648fa861SAl Viro /* 2075648fa861SAl Viro * It is now safe to drop the mnt write 2076648fa861SAl Viro * because the filp has had a write taken 2077648fa861SAl Viro * on its behalf. 2078648fa861SAl Viro */ 2079648fa861SAl Viro if (will_truncate) 2080648fa861SAl Viro mnt_drop_write(nd->path.mnt); 2081d893f1bcSAl Viro path_put(&nd->path); 2082648fa861SAl Viro return filp; 2083648fa861SAl Viro 2084648fa861SAl Viro exit: 2085648fa861SAl Viro if (!IS_ERR(nd->intent.open.file)) 2086648fa861SAl Viro release_open_intent(nd); 2087648fa861SAl Viro path_put(&nd->path); 2088648fa861SAl Viro return ERR_PTR(error); 2089648fa861SAl Viro } 2090648fa861SAl Viro 209131e6b01fSNick Piggin /* 209231e6b01fSNick Piggin * Handle O_CREAT case for do_filp_open 209331e6b01fSNick Piggin */ 2094fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path, 20955b369df8SAl Viro int open_flag, int acc_mode, 20963e297b61SAl Viro int mode, const char *pathname) 2097fb1cc555SAl Viro { 2098a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 2099fb1cc555SAl Viro struct file *filp; 21001f36f774SAl Viro int error = -EISDIR; 2101fb1cc555SAl Viro 21021f36f774SAl Viro switch (nd->last_type) { 21031f36f774SAl Viro case LAST_DOTDOT: 21041f36f774SAl Viro follow_dotdot(nd); 21051f36f774SAl Viro dir = nd->path.dentry; 2106176306f5SNeil Brown case LAST_DOT: 2107fb045adbSNick Piggin if (need_reval_dot(dir)) { 210834286d66SNick Piggin error = d_revalidate(nd->path.dentry, nd); 210934286d66SNick Piggin if (!error) 21101f36f774SAl Viro error = -ESTALE; 211134286d66SNick Piggin if (error < 0) 2112a2c36b45SAl Viro goto exit; 21131f36f774SAl Viro } 21141f36f774SAl Viro /* fallthrough */ 21151f36f774SAl Viro case LAST_ROOT: 21161f36f774SAl Viro goto exit; 21171f36f774SAl Viro case LAST_BIND: 21181f36f774SAl Viro audit_inode(pathname, dir); 21191f36f774SAl Viro goto ok; 21201f36f774SAl Viro } 2121a2c36b45SAl Viro 21221f36f774SAl Viro /* trailing slashes? */ 212331e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 21241f36f774SAl Viro goto exit; 21251f36f774SAl Viro 2126a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 2127a1e28038SAl Viro 2128a1e28038SAl Viro path->dentry = lookup_hash(nd); 2129a1e28038SAl Viro path->mnt = nd->path.mnt; 2130a1e28038SAl Viro 2131fb1cc555SAl Viro error = PTR_ERR(path->dentry); 2132fb1cc555SAl Viro if (IS_ERR(path->dentry)) { 2133fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2134fb1cc555SAl Viro goto exit; 2135fb1cc555SAl Viro } 2136fb1cc555SAl Viro 2137fb1cc555SAl Viro if (IS_ERR(nd->intent.open.file)) { 2138fb1cc555SAl Viro error = PTR_ERR(nd->intent.open.file); 2139fb1cc555SAl Viro goto exit_mutex_unlock; 2140fb1cc555SAl Viro } 2141fb1cc555SAl Viro 2142fb1cc555SAl Viro /* Negative dentry, just create the file */ 2143fb1cc555SAl Viro if (!path->dentry->d_inode) { 2144fb1cc555SAl Viro /* 2145fb1cc555SAl Viro * This write is needed to ensure that a 2146fb1cc555SAl Viro * ro->rw transition does not occur between 2147fb1cc555SAl Viro * the time when the file is created and when 2148fb1cc555SAl Viro * a permanent write count is taken through 2149fb1cc555SAl Viro * the 'struct file' in nameidata_to_filp(). 2150fb1cc555SAl Viro */ 2151fb1cc555SAl Viro error = mnt_want_write(nd->path.mnt); 2152fb1cc555SAl Viro if (error) 2153fb1cc555SAl Viro goto exit_mutex_unlock; 2154fb1cc555SAl Viro error = __open_namei_create(nd, path, open_flag, mode); 2155fb1cc555SAl Viro if (error) { 2156fb1cc555SAl Viro mnt_drop_write(nd->path.mnt); 2157fb1cc555SAl Viro goto exit; 2158fb1cc555SAl Viro } 2159fb1cc555SAl Viro filp = nameidata_to_filp(nd); 2160fb1cc555SAl Viro mnt_drop_write(nd->path.mnt); 2161d893f1bcSAl Viro path_put(&nd->path); 2162fb1cc555SAl Viro if (!IS_ERR(filp)) { 2163fb1cc555SAl Viro error = ima_file_check(filp, acc_mode); 2164fb1cc555SAl Viro if (error) { 2165fb1cc555SAl Viro fput(filp); 2166fb1cc555SAl Viro filp = ERR_PTR(error); 2167fb1cc555SAl Viro } 2168fb1cc555SAl Viro } 2169fb1cc555SAl Viro return filp; 2170fb1cc555SAl Viro } 2171fb1cc555SAl Viro 2172fb1cc555SAl Viro /* 2173fb1cc555SAl Viro * It already exists. 2174fb1cc555SAl Viro */ 2175fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2176fb1cc555SAl Viro audit_inode(pathname, path->dentry); 2177fb1cc555SAl Viro 2178fb1cc555SAl Viro error = -EEXIST; 21795b369df8SAl Viro if (open_flag & O_EXCL) 2180fb1cc555SAl Viro goto exit_dput; 2181fb1cc555SAl Viro 2182fb1cc555SAl Viro if (__follow_mount(path)) { 2183fb1cc555SAl Viro error = -ELOOP; 21845b369df8SAl Viro if (open_flag & O_NOFOLLOW) 2185fb1cc555SAl Viro goto exit_dput; 2186fb1cc555SAl Viro } 2187fb1cc555SAl Viro 2188fb1cc555SAl Viro error = -ENOENT; 2189fb1cc555SAl Viro if (!path->dentry->d_inode) 2190fb1cc555SAl Viro goto exit_dput; 21919e67f361SAl Viro 21929e67f361SAl Viro if (path->dentry->d_inode->i_op->follow_link) 2193fb1cc555SAl Viro return NULL; 2194fb1cc555SAl Viro 2195fb1cc555SAl Viro path_to_nameidata(path, nd); 219631e6b01fSNick Piggin nd->inode = path->dentry->d_inode; 2197fb1cc555SAl Viro error = -EISDIR; 219831e6b01fSNick Piggin if (S_ISDIR(nd->inode->i_mode)) 2199fb1cc555SAl Viro goto exit; 220067ee3ad2SAl Viro ok: 22019a66179eSAl Viro filp = finish_open(nd, open_flag, acc_mode); 2202fb1cc555SAl Viro return filp; 2203fb1cc555SAl Viro 2204fb1cc555SAl Viro exit_mutex_unlock: 2205fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2206fb1cc555SAl Viro exit_dput: 2207fb1cc555SAl Viro path_put_conditional(path, nd); 2208fb1cc555SAl Viro exit: 2209fb1cc555SAl Viro if (!IS_ERR(nd->intent.open.file)) 2210fb1cc555SAl Viro release_open_intent(nd); 2211fb1cc555SAl Viro path_put(&nd->path); 2212fb1cc555SAl Viro return ERR_PTR(error); 2213fb1cc555SAl Viro } 2214fb1cc555SAl Viro 22154a3fd211SDave Hansen /* 22164a3fd211SDave Hansen * Note that the low bits of the passed in "open_flag" 22174a3fd211SDave Hansen * are not the same as in the local variable "flag". See 22184a3fd211SDave Hansen * open_to_namei_flags() for more details. 22191da177e4SLinus Torvalds */ 2220a70e65dfSChristoph Hellwig struct file *do_filp_open(int dfd, const char *pathname, 22216e8341a1SAl Viro int open_flag, int mode, int acc_mode) 22221da177e4SLinus Torvalds { 22234a3fd211SDave Hansen struct file *filp; 2224a70e65dfSChristoph Hellwig struct nameidata nd; 22256e8341a1SAl Viro int error; 22269850c056SAl Viro struct path path; 22271da177e4SLinus Torvalds int count = 0; 2228d57999e1SDave Hansen int flag = open_to_namei_flags(open_flag); 222931e6b01fSNick Piggin int flags; 22301f36f774SAl Viro 22311f36f774SAl Viro if (!(open_flag & O_CREAT)) 22321f36f774SAl Viro mode = 0; 22331da177e4SLinus Torvalds 2234b1085ba8SLino Sanfilippo /* Must never be set by userspace */ 2235b1085ba8SLino Sanfilippo open_flag &= ~FMODE_NONOTIFY; 2236b1085ba8SLino Sanfilippo 22376b2f3d1fSChristoph Hellwig /* 22386b2f3d1fSChristoph Hellwig * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only 22396b2f3d1fSChristoph Hellwig * check for O_DSYNC if the need any syncing at all we enforce it's 22406b2f3d1fSChristoph Hellwig * always set instead of having to deal with possibly weird behaviour 22416b2f3d1fSChristoph Hellwig * for malicious applications setting only __O_SYNC. 22426b2f3d1fSChristoph Hellwig */ 22436b2f3d1fSChristoph Hellwig if (open_flag & __O_SYNC) 22446b2f3d1fSChristoph Hellwig open_flag |= O_DSYNC; 22456b2f3d1fSChristoph Hellwig 22466e8341a1SAl Viro if (!acc_mode) 22476d125529SAl Viro acc_mode = MAY_OPEN | ACC_MODE(open_flag); 22481da177e4SLinus Torvalds 2249834f2a4aSTrond Myklebust /* O_TRUNC implies we need access checks for write permissions */ 22504296e2cbSAl Viro if (open_flag & O_TRUNC) 2251834f2a4aSTrond Myklebust acc_mode |= MAY_WRITE; 2252834f2a4aSTrond Myklebust 22531da177e4SLinus Torvalds /* Allow the LSM permission hook to distinguish append 22541da177e4SLinus Torvalds access from general write access. */ 22554296e2cbSAl Viro if (open_flag & O_APPEND) 22561da177e4SLinus Torvalds acc_mode |= MAY_APPEND; 22571da177e4SLinus Torvalds 225831e6b01fSNick Piggin flags = LOOKUP_OPEN; 225931e6b01fSNick Piggin if (open_flag & O_CREAT) { 226031e6b01fSNick Piggin flags |= LOOKUP_CREATE; 226131e6b01fSNick Piggin if (open_flag & O_EXCL) 226231e6b01fSNick Piggin flags |= LOOKUP_EXCL; 2263654f562cSJ. R. Okajima } 226431e6b01fSNick Piggin if (open_flag & O_DIRECTORY) 226531e6b01fSNick Piggin flags |= LOOKUP_DIRECTORY; 226631e6b01fSNick Piggin if (!(open_flag & O_NOFOLLOW)) 226731e6b01fSNick Piggin flags |= LOOKUP_FOLLOW; 226831e6b01fSNick Piggin 226931e6b01fSNick Piggin filp = get_empty_filp(); 227031e6b01fSNick Piggin if (!filp) 227131e6b01fSNick Piggin return ERR_PTR(-ENFILE); 227231e6b01fSNick Piggin 227331e6b01fSNick Piggin filp->f_flags = open_flag; 227431e6b01fSNick Piggin nd.intent.open.file = filp; 227531e6b01fSNick Piggin nd.intent.open.flags = flag; 227631e6b01fSNick Piggin nd.intent.open.create_mode = mode; 227731e6b01fSNick Piggin 227831e6b01fSNick Piggin if (open_flag & O_CREAT) 227931e6b01fSNick Piggin goto creat; 228031e6b01fSNick Piggin 228131e6b01fSNick Piggin /* !O_CREAT, simple open */ 228231e6b01fSNick Piggin error = do_path_lookup(dfd, pathname, flags, &nd); 228331e6b01fSNick Piggin if (unlikely(error)) 228431e6b01fSNick Piggin goto out_filp; 228531e6b01fSNick Piggin error = -ELOOP; 228631e6b01fSNick Piggin if (!(nd.flags & LOOKUP_FOLLOW)) { 228731e6b01fSNick Piggin if (nd.inode->i_op->follow_link) 228831e6b01fSNick Piggin goto out_path; 228931e6b01fSNick Piggin } 229031e6b01fSNick Piggin error = -ENOTDIR; 229131e6b01fSNick Piggin if (nd.flags & LOOKUP_DIRECTORY) { 229231e6b01fSNick Piggin if (!nd.inode->i_op->lookup) 229331e6b01fSNick Piggin goto out_path; 229431e6b01fSNick Piggin } 229531e6b01fSNick Piggin audit_inode(pathname, nd.path.dentry); 229631e6b01fSNick Piggin filp = finish_open(&nd, open_flag, acc_mode); 229731e6b01fSNick Piggin return filp; 229831e6b01fSNick Piggin 229931e6b01fSNick Piggin creat: 230031e6b01fSNick Piggin /* OK, have to create the file. Find the parent. */ 230131e6b01fSNick Piggin error = path_init_rcu(dfd, pathname, 230231e6b01fSNick Piggin LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd); 230331e6b01fSNick Piggin if (error) 230431e6b01fSNick Piggin goto out_filp; 230531e6b01fSNick Piggin error = path_walk_rcu(pathname, &nd); 230631e6b01fSNick Piggin path_finish_rcu(&nd); 230731e6b01fSNick Piggin if (unlikely(error == -ECHILD || error == -ESTALE)) { 230831e6b01fSNick Piggin /* slower, locked walk */ 230931e6b01fSNick Piggin if (error == -ESTALE) { 231031e6b01fSNick Piggin reval: 231131e6b01fSNick Piggin flags |= LOOKUP_REVAL; 231231e6b01fSNick Piggin } 231331e6b01fSNick Piggin error = path_init(dfd, pathname, 231431e6b01fSNick Piggin LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd); 231531e6b01fSNick Piggin if (error) 231631e6b01fSNick Piggin goto out_filp; 231731e6b01fSNick Piggin 231831e6b01fSNick Piggin error = path_walk_simple(pathname, &nd); 231931e6b01fSNick Piggin } 232031e6b01fSNick Piggin if (unlikely(error)) 232131e6b01fSNick Piggin goto out_filp; 232231e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) 23239b4a9b14SAl Viro audit_inode(pathname, nd.path.dentry); 23241da177e4SLinus Torvalds 23251da177e4SLinus Torvalds /* 2326a2c36b45SAl Viro * We have the parent and last component. 23271da177e4SLinus Torvalds */ 232831e6b01fSNick Piggin nd.flags = flags; 23293e297b61SAl Viro filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname); 2330806b681cSAl Viro while (unlikely(!filp)) { /* trailing symlink */ 2331def4af30SAl Viro struct path holder; 2332def4af30SAl Viro void *cookie; 2333806b681cSAl Viro error = -ELOOP; 23341f36f774SAl Viro /* S_ISDIR part is a temporary automount kludge */ 233531e6b01fSNick Piggin if (!(nd.flags & LOOKUP_FOLLOW) && !S_ISDIR(nd.inode->i_mode)) 23361f36f774SAl Viro goto exit_dput; 23371f36f774SAl Viro if (count++ == 32) 2338806b681cSAl Viro goto exit_dput; 2339806b681cSAl Viro /* 2340806b681cSAl Viro * This is subtle. Instead of calling do_follow_link() we do 2341806b681cSAl Viro * the thing by hands. The reason is that this way we have zero 2342806b681cSAl Viro * link_count and path_walk() (called from ->follow_link) 2343806b681cSAl Viro * honoring LOOKUP_PARENT. After that we have the parent and 2344806b681cSAl Viro * last component, i.e. we are in the same situation as after 2345806b681cSAl Viro * the first path_walk(). Well, almost - if the last component 2346806b681cSAl Viro * is normal we get its copy stored in nd->last.name and we will 2347806b681cSAl Viro * have to putname() it when we are done. Procfs-like symlinks 2348806b681cSAl Viro * just set LAST_BIND. 2349806b681cSAl Viro */ 2350806b681cSAl Viro nd.flags |= LOOKUP_PARENT; 2351806b681cSAl Viro error = security_inode_follow_link(path.dentry, &nd); 2352806b681cSAl Viro if (error) 2353806b681cSAl Viro goto exit_dput; 2354def4af30SAl Viro error = __do_follow_link(&path, &nd, &cookie); 2355def4af30SAl Viro if (unlikely(error)) { 235631e6b01fSNick Piggin if (!IS_ERR(cookie) && nd.inode->i_op->put_link) 235731e6b01fSNick Piggin nd.inode->i_op->put_link(path.dentry, &nd, cookie); 2358806b681cSAl Viro /* nd.path had been dropped */ 235931e6b01fSNick Piggin nd.path = path; 236031e6b01fSNick Piggin goto out_path; 2361806b681cSAl Viro } 2362def4af30SAl Viro holder = path; 2363806b681cSAl Viro nd.flags &= ~LOOKUP_PARENT; 23643e297b61SAl Viro filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname); 236531e6b01fSNick Piggin if (nd.inode->i_op->put_link) 236631e6b01fSNick Piggin nd.inode->i_op->put_link(holder.dentry, &nd, cookie); 2367def4af30SAl Viro path_put(&holder); 2368806b681cSAl Viro } 236910fa8e62SAl Viro out: 23702a737871SAl Viro if (nd.root.mnt) 23712a737871SAl Viro path_put(&nd.root); 237231e6b01fSNick Piggin if (filp == ERR_PTR(-ESTALE) && !(flags & LOOKUP_REVAL)) 237310fa8e62SAl Viro goto reval; 237410fa8e62SAl Viro return filp; 23751da177e4SLinus Torvalds 2376806b681cSAl Viro exit_dput: 2377806b681cSAl Viro path_put_conditional(&path, &nd); 237831e6b01fSNick Piggin out_path: 237931e6b01fSNick Piggin path_put(&nd.path); 238031e6b01fSNick Piggin out_filp: 2381806b681cSAl Viro if (!IS_ERR(nd.intent.open.file)) 2382a70e65dfSChristoph Hellwig release_open_intent(&nd); 238310fa8e62SAl Viro filp = ERR_PTR(error); 238410fa8e62SAl Viro goto out; 2385de459215SKirill Korotaev } 23861da177e4SLinus Torvalds 23871da177e4SLinus Torvalds /** 2388a70e65dfSChristoph Hellwig * filp_open - open file and return file pointer 2389a70e65dfSChristoph Hellwig * 2390a70e65dfSChristoph Hellwig * @filename: path to open 2391a70e65dfSChristoph Hellwig * @flags: open flags as per the open(2) second argument 2392a70e65dfSChristoph Hellwig * @mode: mode for the new file if O_CREAT is set, else ignored 2393a70e65dfSChristoph Hellwig * 2394a70e65dfSChristoph Hellwig * This is the helper to open a file from kernelspace if you really 2395a70e65dfSChristoph Hellwig * have to. But in generally you should not do this, so please move 2396a70e65dfSChristoph Hellwig * along, nothing to see here.. 2397a70e65dfSChristoph Hellwig */ 2398a70e65dfSChristoph Hellwig struct file *filp_open(const char *filename, int flags, int mode) 2399a70e65dfSChristoph Hellwig { 24006e8341a1SAl Viro return do_filp_open(AT_FDCWD, filename, flags, mode, 0); 2401a70e65dfSChristoph Hellwig } 2402a70e65dfSChristoph Hellwig EXPORT_SYMBOL(filp_open); 2403a70e65dfSChristoph Hellwig 2404a70e65dfSChristoph Hellwig /** 24051da177e4SLinus Torvalds * lookup_create - lookup a dentry, creating it if it doesn't exist 24061da177e4SLinus Torvalds * @nd: nameidata info 24071da177e4SLinus Torvalds * @is_dir: directory flag 24081da177e4SLinus Torvalds * 24091da177e4SLinus Torvalds * Simple function to lookup and return a dentry and create it 24101da177e4SLinus Torvalds * if it doesn't exist. Is SMP-safe. 2411c663e5d8SChristoph Hellwig * 24124ac91378SJan Blunck * Returns with nd->path.dentry->d_inode->i_mutex locked. 24131da177e4SLinus Torvalds */ 24141da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir) 24151da177e4SLinus Torvalds { 2416c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 24171da177e4SLinus Torvalds 24184ac91378SJan Blunck mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 2419c663e5d8SChristoph Hellwig /* 2420c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 2421c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 2422c663e5d8SChristoph Hellwig */ 24231da177e4SLinus Torvalds if (nd->last_type != LAST_NORM) 24241da177e4SLinus Torvalds goto fail; 24251da177e4SLinus Torvalds nd->flags &= ~LOOKUP_PARENT; 24263516586aSAl Viro nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL; 2427a634904aSASANO Masahiro nd->intent.open.flags = O_EXCL; 2428c663e5d8SChristoph Hellwig 2429c663e5d8SChristoph Hellwig /* 2430c663e5d8SChristoph Hellwig * Do the final lookup. 2431c663e5d8SChristoph Hellwig */ 243249705b77SChristoph Hellwig dentry = lookup_hash(nd); 24331da177e4SLinus Torvalds if (IS_ERR(dentry)) 24341da177e4SLinus Torvalds goto fail; 2435c663e5d8SChristoph Hellwig 2436e9baf6e5SAl Viro if (dentry->d_inode) 2437e9baf6e5SAl Viro goto eexist; 2438c663e5d8SChristoph Hellwig /* 2439c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 2440c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 2441c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 2442c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 2443c663e5d8SChristoph Hellwig */ 2444e9baf6e5SAl Viro if (unlikely(!is_dir && nd->last.name[nd->last.len])) { 24451da177e4SLinus Torvalds dput(dentry); 24461da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 2447e9baf6e5SAl Viro } 2448e9baf6e5SAl Viro return dentry; 2449e9baf6e5SAl Viro eexist: 2450e9baf6e5SAl Viro dput(dentry); 2451e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 24521da177e4SLinus Torvalds fail: 24531da177e4SLinus Torvalds return dentry; 24541da177e4SLinus Torvalds } 2455f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create); 24561da177e4SLinus Torvalds 24571da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 24581da177e4SLinus Torvalds { 2459a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 24601da177e4SLinus Torvalds 24611da177e4SLinus Torvalds if (error) 24621da177e4SLinus Torvalds return error; 24631da177e4SLinus Torvalds 24641da177e4SLinus Torvalds if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) 24651da177e4SLinus Torvalds return -EPERM; 24661da177e4SLinus Torvalds 2467acfa4380SAl Viro if (!dir->i_op->mknod) 24681da177e4SLinus Torvalds return -EPERM; 24691da177e4SLinus Torvalds 247008ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 247108ce5f16SSerge E. Hallyn if (error) 247208ce5f16SSerge E. Hallyn return error; 247308ce5f16SSerge E. Hallyn 24741da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 24751da177e4SLinus Torvalds if (error) 24761da177e4SLinus Torvalds return error; 24771da177e4SLinus Torvalds 24781da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 2479a74574aaSStephen Smalley if (!error) 2480f38aa942SAmy Griffis fsnotify_create(dir, dentry); 24811da177e4SLinus Torvalds return error; 24821da177e4SLinus Torvalds } 24831da177e4SLinus Torvalds 2484463c3197SDave Hansen static int may_mknod(mode_t mode) 2485463c3197SDave Hansen { 2486463c3197SDave Hansen switch (mode & S_IFMT) { 2487463c3197SDave Hansen case S_IFREG: 2488463c3197SDave Hansen case S_IFCHR: 2489463c3197SDave Hansen case S_IFBLK: 2490463c3197SDave Hansen case S_IFIFO: 2491463c3197SDave Hansen case S_IFSOCK: 2492463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2493463c3197SDave Hansen return 0; 2494463c3197SDave Hansen case S_IFDIR: 2495463c3197SDave Hansen return -EPERM; 2496463c3197SDave Hansen default: 2497463c3197SDave Hansen return -EINVAL; 2498463c3197SDave Hansen } 2499463c3197SDave Hansen } 2500463c3197SDave Hansen 25012e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode, 25022e4d0924SHeiko Carstens unsigned, dev) 25031da177e4SLinus Torvalds { 25042ad94ae6SAl Viro int error; 25051da177e4SLinus Torvalds char *tmp; 25061da177e4SLinus Torvalds struct dentry *dentry; 25071da177e4SLinus Torvalds struct nameidata nd; 25081da177e4SLinus Torvalds 25091da177e4SLinus Torvalds if (S_ISDIR(mode)) 25101da177e4SLinus Torvalds return -EPERM; 25111da177e4SLinus Torvalds 25122ad94ae6SAl Viro error = user_path_parent(dfd, filename, &nd, &tmp); 25131da177e4SLinus Torvalds if (error) 25142ad94ae6SAl Viro return error; 25152ad94ae6SAl Viro 25161da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 2517463c3197SDave Hansen if (IS_ERR(dentry)) { 25181da177e4SLinus Torvalds error = PTR_ERR(dentry); 2519463c3197SDave Hansen goto out_unlock; 2520463c3197SDave Hansen } 25214ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2522ce3b0f8dSAl Viro mode &= ~current_umask(); 2523463c3197SDave Hansen error = may_mknod(mode); 2524463c3197SDave Hansen if (error) 2525463c3197SDave Hansen goto out_dput; 2526463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2527463c3197SDave Hansen if (error) 2528463c3197SDave Hansen goto out_dput; 2529be6d3e56SKentaro Takeda error = security_path_mknod(&nd.path, dentry, mode, dev); 2530be6d3e56SKentaro Takeda if (error) 2531be6d3e56SKentaro Takeda goto out_drop_write; 25321da177e4SLinus Torvalds switch (mode & S_IFMT) { 25331da177e4SLinus Torvalds case 0: case S_IFREG: 25344ac91378SJan Blunck error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd); 25351da177e4SLinus Torvalds break; 25361da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 25374ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode, 25381da177e4SLinus Torvalds new_decode_dev(dev)); 25391da177e4SLinus Torvalds break; 25401da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 25414ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0); 25421da177e4SLinus Torvalds break; 25431da177e4SLinus Torvalds } 2544be6d3e56SKentaro Takeda out_drop_write: 2545463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2546463c3197SDave Hansen out_dput: 25471da177e4SLinus Torvalds dput(dentry); 2548463c3197SDave Hansen out_unlock: 25494ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 25501d957f9bSJan Blunck path_put(&nd.path); 25511da177e4SLinus Torvalds putname(tmp); 25521da177e4SLinus Torvalds 25531da177e4SLinus Torvalds return error; 25541da177e4SLinus Torvalds } 25551da177e4SLinus Torvalds 25563480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev) 25575590ff0dSUlrich Drepper { 25585590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 25595590ff0dSUlrich Drepper } 25605590ff0dSUlrich Drepper 25611da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 25621da177e4SLinus Torvalds { 2563a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 25641da177e4SLinus Torvalds 25651da177e4SLinus Torvalds if (error) 25661da177e4SLinus Torvalds return error; 25671da177e4SLinus Torvalds 2568acfa4380SAl Viro if (!dir->i_op->mkdir) 25691da177e4SLinus Torvalds return -EPERM; 25701da177e4SLinus Torvalds 25711da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 25721da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 25731da177e4SLinus Torvalds if (error) 25741da177e4SLinus Torvalds return error; 25751da177e4SLinus Torvalds 25761da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2577a74574aaSStephen Smalley if (!error) 2578f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 25791da177e4SLinus Torvalds return error; 25801da177e4SLinus Torvalds } 25811da177e4SLinus Torvalds 25822e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode) 25831da177e4SLinus Torvalds { 25841da177e4SLinus Torvalds int error = 0; 25851da177e4SLinus Torvalds char * tmp; 25866902d925SDave Hansen struct dentry *dentry; 25876902d925SDave Hansen struct nameidata nd; 25881da177e4SLinus Torvalds 25892ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &tmp); 25902ad94ae6SAl Viro if (error) 25916902d925SDave Hansen goto out_err; 25921da177e4SLinus Torvalds 25931da177e4SLinus Torvalds dentry = lookup_create(&nd, 1); 25941da177e4SLinus Torvalds error = PTR_ERR(dentry); 25956902d925SDave Hansen if (IS_ERR(dentry)) 25966902d925SDave Hansen goto out_unlock; 25976902d925SDave Hansen 25984ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2599ce3b0f8dSAl Viro mode &= ~current_umask(); 2600463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2601463c3197SDave Hansen if (error) 2602463c3197SDave Hansen goto out_dput; 2603be6d3e56SKentaro Takeda error = security_path_mkdir(&nd.path, dentry, mode); 2604be6d3e56SKentaro Takeda if (error) 2605be6d3e56SKentaro Takeda goto out_drop_write; 26064ac91378SJan Blunck error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode); 2607be6d3e56SKentaro Takeda out_drop_write: 2608463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2609463c3197SDave Hansen out_dput: 26101da177e4SLinus Torvalds dput(dentry); 26116902d925SDave Hansen out_unlock: 26124ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 26131d957f9bSJan Blunck path_put(&nd.path); 26141da177e4SLinus Torvalds putname(tmp); 26156902d925SDave Hansen out_err: 26161da177e4SLinus Torvalds return error; 26171da177e4SLinus Torvalds } 26181da177e4SLinus Torvalds 26193cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode) 26205590ff0dSUlrich Drepper { 26215590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 26225590ff0dSUlrich Drepper } 26235590ff0dSUlrich Drepper 26241da177e4SLinus Torvalds /* 26251da177e4SLinus Torvalds * We try to drop the dentry early: we should have 26261da177e4SLinus Torvalds * a usage count of 2 if we're the only user of this 26271da177e4SLinus Torvalds * dentry, and if that is true (possibly after pruning 26281da177e4SLinus Torvalds * the dcache), then we drop the dentry now. 26291da177e4SLinus Torvalds * 26301da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 26311da177e4SLinus Torvalds * do a 26321da177e4SLinus Torvalds * 26331da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 26341da177e4SLinus Torvalds * return -EBUSY; 26351da177e4SLinus Torvalds * 26361da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 26371da177e4SLinus Torvalds * that is still in use by something else.. 26381da177e4SLinus Torvalds */ 26391da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 26401da177e4SLinus Torvalds { 26411da177e4SLinus Torvalds dget(dentry); 26421da177e4SLinus Torvalds shrink_dcache_parent(dentry); 26431da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 2644b7ab39f6SNick Piggin if (dentry->d_count == 2) 26451da177e4SLinus Torvalds __d_drop(dentry); 26461da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 26471da177e4SLinus Torvalds } 26481da177e4SLinus Torvalds 26491da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 26501da177e4SLinus Torvalds { 26511da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 26521da177e4SLinus Torvalds 26531da177e4SLinus Torvalds if (error) 26541da177e4SLinus Torvalds return error; 26551da177e4SLinus Torvalds 2656acfa4380SAl Viro if (!dir->i_op->rmdir) 26571da177e4SLinus Torvalds return -EPERM; 26581da177e4SLinus Torvalds 26591b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 26601da177e4SLinus Torvalds dentry_unhash(dentry); 26611da177e4SLinus Torvalds if (d_mountpoint(dentry)) 26621da177e4SLinus Torvalds error = -EBUSY; 26631da177e4SLinus Torvalds else { 26641da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 26651da177e4SLinus Torvalds if (!error) { 26661da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 2667d83c49f3SAl Viro if (!error) { 26681da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 2669d83c49f3SAl Viro dont_mount(dentry); 2670d83c49f3SAl Viro } 26711da177e4SLinus Torvalds } 26721da177e4SLinus Torvalds } 26731b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 26741da177e4SLinus Torvalds if (!error) { 26751da177e4SLinus Torvalds d_delete(dentry); 26761da177e4SLinus Torvalds } 26771da177e4SLinus Torvalds dput(dentry); 26781da177e4SLinus Torvalds 26791da177e4SLinus Torvalds return error; 26801da177e4SLinus Torvalds } 26811da177e4SLinus Torvalds 26825590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 26831da177e4SLinus Torvalds { 26841da177e4SLinus Torvalds int error = 0; 26851da177e4SLinus Torvalds char * name; 26861da177e4SLinus Torvalds struct dentry *dentry; 26871da177e4SLinus Torvalds struct nameidata nd; 26881da177e4SLinus Torvalds 26892ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 26901da177e4SLinus Torvalds if (error) 26912ad94ae6SAl Viro return error; 26921da177e4SLinus Torvalds 26931da177e4SLinus Torvalds switch(nd.last_type) { 26941da177e4SLinus Torvalds case LAST_DOTDOT: 26951da177e4SLinus Torvalds error = -ENOTEMPTY; 26961da177e4SLinus Torvalds goto exit1; 26971da177e4SLinus Torvalds case LAST_DOT: 26981da177e4SLinus Torvalds error = -EINVAL; 26991da177e4SLinus Torvalds goto exit1; 27001da177e4SLinus Torvalds case LAST_ROOT: 27011da177e4SLinus Torvalds error = -EBUSY; 27021da177e4SLinus Torvalds goto exit1; 27031da177e4SLinus Torvalds } 27040612d9fbSOGAWA Hirofumi 27050612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 27060612d9fbSOGAWA Hirofumi 27074ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 270849705b77SChristoph Hellwig dentry = lookup_hash(&nd); 27091da177e4SLinus Torvalds error = PTR_ERR(dentry); 27106902d925SDave Hansen if (IS_ERR(dentry)) 27116902d925SDave Hansen goto exit2; 27120622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 27130622753bSDave Hansen if (error) 27140622753bSDave Hansen goto exit3; 2715be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 2716be6d3e56SKentaro Takeda if (error) 2717be6d3e56SKentaro Takeda goto exit4; 27184ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 2719be6d3e56SKentaro Takeda exit4: 27200622753bSDave Hansen mnt_drop_write(nd.path.mnt); 27210622753bSDave Hansen exit3: 27221da177e4SLinus Torvalds dput(dentry); 27236902d925SDave Hansen exit2: 27244ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 27251da177e4SLinus Torvalds exit1: 27261d957f9bSJan Blunck path_put(&nd.path); 27271da177e4SLinus Torvalds putname(name); 27281da177e4SLinus Torvalds return error; 27291da177e4SLinus Torvalds } 27301da177e4SLinus Torvalds 27313cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 27325590ff0dSUlrich Drepper { 27335590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 27345590ff0dSUlrich Drepper } 27355590ff0dSUlrich Drepper 27361da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 27371da177e4SLinus Torvalds { 27381da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 27391da177e4SLinus Torvalds 27401da177e4SLinus Torvalds if (error) 27411da177e4SLinus Torvalds return error; 27421da177e4SLinus Torvalds 2743acfa4380SAl Viro if (!dir->i_op->unlink) 27441da177e4SLinus Torvalds return -EPERM; 27451da177e4SLinus Torvalds 27461b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 27471da177e4SLinus Torvalds if (d_mountpoint(dentry)) 27481da177e4SLinus Torvalds error = -EBUSY; 27491da177e4SLinus Torvalds else { 27501da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 2751bec1052eSAl Viro if (!error) { 27521da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 2753bec1052eSAl Viro if (!error) 2754d83c49f3SAl Viro dont_mount(dentry); 2755bec1052eSAl Viro } 27561da177e4SLinus Torvalds } 27571b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 27581da177e4SLinus Torvalds 27591da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 27601da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 2761ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 27621da177e4SLinus Torvalds d_delete(dentry); 27631da177e4SLinus Torvalds } 27640eeca283SRobert Love 27651da177e4SLinus Torvalds return error; 27661da177e4SLinus Torvalds } 27671da177e4SLinus Torvalds 27681da177e4SLinus Torvalds /* 27691da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 27701b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 27711da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 27721da177e4SLinus Torvalds * while waiting on the I/O. 27731da177e4SLinus Torvalds */ 27745590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 27751da177e4SLinus Torvalds { 27762ad94ae6SAl Viro int error; 27771da177e4SLinus Torvalds char *name; 27781da177e4SLinus Torvalds struct dentry *dentry; 27791da177e4SLinus Torvalds struct nameidata nd; 27801da177e4SLinus Torvalds struct inode *inode = NULL; 27811da177e4SLinus Torvalds 27822ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 27831da177e4SLinus Torvalds if (error) 27842ad94ae6SAl Viro return error; 27852ad94ae6SAl Viro 27861da177e4SLinus Torvalds error = -EISDIR; 27871da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 27881da177e4SLinus Torvalds goto exit1; 27890612d9fbSOGAWA Hirofumi 27900612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 27910612d9fbSOGAWA Hirofumi 27924ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 279349705b77SChristoph Hellwig dentry = lookup_hash(&nd); 27941da177e4SLinus Torvalds error = PTR_ERR(dentry); 27951da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 27961da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 27971da177e4SLinus Torvalds if (nd.last.name[nd.last.len]) 27981da177e4SLinus Torvalds goto slashes; 27991da177e4SLinus Torvalds inode = dentry->d_inode; 28001da177e4SLinus Torvalds if (inode) 28017de9c6eeSAl Viro ihold(inode); 28020622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 28030622753bSDave Hansen if (error) 28040622753bSDave Hansen goto exit2; 2805be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 2806be6d3e56SKentaro Takeda if (error) 2807be6d3e56SKentaro Takeda goto exit3; 28084ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 2809be6d3e56SKentaro Takeda exit3: 28100622753bSDave Hansen mnt_drop_write(nd.path.mnt); 28111da177e4SLinus Torvalds exit2: 28121da177e4SLinus Torvalds dput(dentry); 28131da177e4SLinus Torvalds } 28144ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 28151da177e4SLinus Torvalds if (inode) 28161da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 28171da177e4SLinus Torvalds exit1: 28181d957f9bSJan Blunck path_put(&nd.path); 28191da177e4SLinus Torvalds putname(name); 28201da177e4SLinus Torvalds return error; 28211da177e4SLinus Torvalds 28221da177e4SLinus Torvalds slashes: 28231da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 28241da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 28251da177e4SLinus Torvalds goto exit2; 28261da177e4SLinus Torvalds } 28271da177e4SLinus Torvalds 28282e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 28295590ff0dSUlrich Drepper { 28305590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 28315590ff0dSUlrich Drepper return -EINVAL; 28325590ff0dSUlrich Drepper 28335590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 28345590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 28355590ff0dSUlrich Drepper 28365590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 28375590ff0dSUlrich Drepper } 28385590ff0dSUlrich Drepper 28393480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 28405590ff0dSUlrich Drepper { 28415590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 28425590ff0dSUlrich Drepper } 28435590ff0dSUlrich Drepper 2844db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 28451da177e4SLinus Torvalds { 2846a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 28471da177e4SLinus Torvalds 28481da177e4SLinus Torvalds if (error) 28491da177e4SLinus Torvalds return error; 28501da177e4SLinus Torvalds 2851acfa4380SAl Viro if (!dir->i_op->symlink) 28521da177e4SLinus Torvalds return -EPERM; 28531da177e4SLinus Torvalds 28541da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 28551da177e4SLinus Torvalds if (error) 28561da177e4SLinus Torvalds return error; 28571da177e4SLinus Torvalds 28581da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 2859a74574aaSStephen Smalley if (!error) 2860f38aa942SAmy Griffis fsnotify_create(dir, dentry); 28611da177e4SLinus Torvalds return error; 28621da177e4SLinus Torvalds } 28631da177e4SLinus Torvalds 28642e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 28652e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 28661da177e4SLinus Torvalds { 28672ad94ae6SAl Viro int error; 28681da177e4SLinus Torvalds char *from; 28691da177e4SLinus Torvalds char *to; 28706902d925SDave Hansen struct dentry *dentry; 28716902d925SDave Hansen struct nameidata nd; 28721da177e4SLinus Torvalds 28731da177e4SLinus Torvalds from = getname(oldname); 28741da177e4SLinus Torvalds if (IS_ERR(from)) 28751da177e4SLinus Torvalds return PTR_ERR(from); 28762ad94ae6SAl Viro 28772ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 28782ad94ae6SAl Viro if (error) 28796902d925SDave Hansen goto out_putname; 28801da177e4SLinus Torvalds 28811da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 28821da177e4SLinus Torvalds error = PTR_ERR(dentry); 28836902d925SDave Hansen if (IS_ERR(dentry)) 28846902d925SDave Hansen goto out_unlock; 28856902d925SDave Hansen 288675c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 288775c3f29dSDave Hansen if (error) 288875c3f29dSDave Hansen goto out_dput; 2889be6d3e56SKentaro Takeda error = security_path_symlink(&nd.path, dentry, from); 2890be6d3e56SKentaro Takeda if (error) 2891be6d3e56SKentaro Takeda goto out_drop_write; 2892db2e747bSMiklos Szeredi error = vfs_symlink(nd.path.dentry->d_inode, dentry, from); 2893be6d3e56SKentaro Takeda out_drop_write: 289475c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 289575c3f29dSDave Hansen out_dput: 28961da177e4SLinus Torvalds dput(dentry); 28976902d925SDave Hansen out_unlock: 28984ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 28991d957f9bSJan Blunck path_put(&nd.path); 29001da177e4SLinus Torvalds putname(to); 29016902d925SDave Hansen out_putname: 29021da177e4SLinus Torvalds putname(from); 29031da177e4SLinus Torvalds return error; 29041da177e4SLinus Torvalds } 29051da177e4SLinus Torvalds 29063480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 29075590ff0dSUlrich Drepper { 29085590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 29095590ff0dSUlrich Drepper } 29105590ff0dSUlrich Drepper 29111da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 29121da177e4SLinus Torvalds { 29131da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 29141da177e4SLinus Torvalds int error; 29151da177e4SLinus Torvalds 29161da177e4SLinus Torvalds if (!inode) 29171da177e4SLinus Torvalds return -ENOENT; 29181da177e4SLinus Torvalds 2919a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 29201da177e4SLinus Torvalds if (error) 29211da177e4SLinus Torvalds return error; 29221da177e4SLinus Torvalds 29231da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 29241da177e4SLinus Torvalds return -EXDEV; 29251da177e4SLinus Torvalds 29261da177e4SLinus Torvalds /* 29271da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 29281da177e4SLinus Torvalds */ 29291da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 29301da177e4SLinus Torvalds return -EPERM; 2931acfa4380SAl Viro if (!dir->i_op->link) 29321da177e4SLinus Torvalds return -EPERM; 29337e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 29341da177e4SLinus Torvalds return -EPERM; 29351da177e4SLinus Torvalds 29361da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 29371da177e4SLinus Torvalds if (error) 29381da177e4SLinus Torvalds return error; 29391da177e4SLinus Torvalds 29407e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 29411da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 29427e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 2943e31e14ecSStephen Smalley if (!error) 29447e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 29451da177e4SLinus Torvalds return error; 29461da177e4SLinus Torvalds } 29471da177e4SLinus Torvalds 29481da177e4SLinus Torvalds /* 29491da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 29501da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 29511da177e4SLinus Torvalds * newname. --KAB 29521da177e4SLinus Torvalds * 29531da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 29541da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 29551da177e4SLinus Torvalds * and other special files. --ADM 29561da177e4SLinus Torvalds */ 29572e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 29582e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 29591da177e4SLinus Torvalds { 29601da177e4SLinus Torvalds struct dentry *new_dentry; 29612d8f3038SAl Viro struct nameidata nd; 29622d8f3038SAl Viro struct path old_path; 29631da177e4SLinus Torvalds int error; 29641da177e4SLinus Torvalds char *to; 29651da177e4SLinus Torvalds 296645c9b11aSUlrich Drepper if ((flags & ~AT_SYMLINK_FOLLOW) != 0) 2967c04030e1SUlrich Drepper return -EINVAL; 2968c04030e1SUlrich Drepper 29692d8f3038SAl Viro error = user_path_at(olddfd, oldname, 297045c9b11aSUlrich Drepper flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0, 29712d8f3038SAl Viro &old_path); 29721da177e4SLinus Torvalds if (error) 29732ad94ae6SAl Viro return error; 29742ad94ae6SAl Viro 29752ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 29761da177e4SLinus Torvalds if (error) 29771da177e4SLinus Torvalds goto out; 29781da177e4SLinus Torvalds error = -EXDEV; 29792d8f3038SAl Viro if (old_path.mnt != nd.path.mnt) 29801da177e4SLinus Torvalds goto out_release; 29811da177e4SLinus Torvalds new_dentry = lookup_create(&nd, 0); 29821da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 29836902d925SDave Hansen if (IS_ERR(new_dentry)) 29846902d925SDave Hansen goto out_unlock; 298575c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 298675c3f29dSDave Hansen if (error) 298775c3f29dSDave Hansen goto out_dput; 2988be6d3e56SKentaro Takeda error = security_path_link(old_path.dentry, &nd.path, new_dentry); 2989be6d3e56SKentaro Takeda if (error) 2990be6d3e56SKentaro Takeda goto out_drop_write; 29912d8f3038SAl Viro error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry); 2992be6d3e56SKentaro Takeda out_drop_write: 299375c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 299475c3f29dSDave Hansen out_dput: 29951da177e4SLinus Torvalds dput(new_dentry); 29966902d925SDave Hansen out_unlock: 29974ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 29981da177e4SLinus Torvalds out_release: 29991d957f9bSJan Blunck path_put(&nd.path); 30002ad94ae6SAl Viro putname(to); 30011da177e4SLinus Torvalds out: 30022d8f3038SAl Viro path_put(&old_path); 30031da177e4SLinus Torvalds 30041da177e4SLinus Torvalds return error; 30051da177e4SLinus Torvalds } 30061da177e4SLinus Torvalds 30073480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 30085590ff0dSUlrich Drepper { 3009c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 30105590ff0dSUlrich Drepper } 30115590ff0dSUlrich Drepper 30121da177e4SLinus Torvalds /* 30131da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 30141da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 30151da177e4SLinus Torvalds * Problems: 30161da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 30171da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 30181da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 3019a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 30201da177e4SLinus Torvalds * story. 30211da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 30221b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 30231da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 30241da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 3025a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 30261da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 30271da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 30281da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 3029a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 30301da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 30311da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 30321da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 30331da177e4SLinus Torvalds * d) some filesystems don't support opened-but-unlinked directories, 30341da177e4SLinus Torvalds * either because of layout or because they are not ready to deal with 30351da177e4SLinus Torvalds * all cases correctly. The latter will be fixed (taking this sort of 30361da177e4SLinus Torvalds * stuff into VFS), but the former is not going away. Solution: the same 30371da177e4SLinus Torvalds * trick as in rmdir(). 30381da177e4SLinus Torvalds * e) conversion from fhandle to dentry may come in the wrong moment - when 30391b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 30401da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 3041c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 30421da177e4SLinus Torvalds * locking]. 30431da177e4SLinus Torvalds */ 304475c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 30451da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30461da177e4SLinus Torvalds { 30471da177e4SLinus Torvalds int error = 0; 30481da177e4SLinus Torvalds struct inode *target; 30491da177e4SLinus Torvalds 30501da177e4SLinus Torvalds /* 30511da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 30521da177e4SLinus Torvalds * we'll need to flip '..'. 30531da177e4SLinus Torvalds */ 30541da177e4SLinus Torvalds if (new_dir != old_dir) { 3055f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 30561da177e4SLinus Torvalds if (error) 30571da177e4SLinus Torvalds return error; 30581da177e4SLinus Torvalds } 30591da177e4SLinus Torvalds 30601da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 30611da177e4SLinus Torvalds if (error) 30621da177e4SLinus Torvalds return error; 30631da177e4SLinus Torvalds 30641da177e4SLinus Torvalds target = new_dentry->d_inode; 3065d83c49f3SAl Viro if (target) 30661b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 30671da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 30681da177e4SLinus Torvalds error = -EBUSY; 3069d83c49f3SAl Viro else { 3070d83c49f3SAl Viro if (target) 3071d83c49f3SAl Viro dentry_unhash(new_dentry); 30721da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 3073d83c49f3SAl Viro } 30741da177e4SLinus Torvalds if (target) { 3075d83c49f3SAl Viro if (!error) { 30761da177e4SLinus Torvalds target->i_flags |= S_DEAD; 3077d83c49f3SAl Viro dont_mount(new_dentry); 3078d83c49f3SAl Viro } 30791b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 30801da177e4SLinus Torvalds if (d_unhashed(new_dentry)) 30811da177e4SLinus Torvalds d_rehash(new_dentry); 30821da177e4SLinus Torvalds dput(new_dentry); 30831da177e4SLinus Torvalds } 3084e31e14ecSStephen Smalley if (!error) 3085349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 30861da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 30871da177e4SLinus Torvalds return error; 30881da177e4SLinus Torvalds } 30891da177e4SLinus Torvalds 309075c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 30911da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30921da177e4SLinus Torvalds { 30931da177e4SLinus Torvalds struct inode *target; 30941da177e4SLinus Torvalds int error; 30951da177e4SLinus Torvalds 30961da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 30971da177e4SLinus Torvalds if (error) 30981da177e4SLinus Torvalds return error; 30991da177e4SLinus Torvalds 31001da177e4SLinus Torvalds dget(new_dentry); 31011da177e4SLinus Torvalds target = new_dentry->d_inode; 31021da177e4SLinus Torvalds if (target) 31031b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 31041da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 31051da177e4SLinus Torvalds error = -EBUSY; 31061da177e4SLinus Torvalds else 31071da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 31081da177e4SLinus Torvalds if (!error) { 3109bec1052eSAl Viro if (target) 3110d83c49f3SAl Viro dont_mount(new_dentry); 3111349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 31121da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 31131da177e4SLinus Torvalds } 31141da177e4SLinus Torvalds if (target) 31151b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 31161da177e4SLinus Torvalds dput(new_dentry); 31171da177e4SLinus Torvalds return error; 31181da177e4SLinus Torvalds } 31191da177e4SLinus Torvalds 31201da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 31211da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 31221da177e4SLinus Torvalds { 31231da177e4SLinus Torvalds int error; 31241da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 312559b0df21SEric Paris const unsigned char *old_name; 31261da177e4SLinus Torvalds 31271da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 31281da177e4SLinus Torvalds return 0; 31291da177e4SLinus Torvalds 31301da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 31311da177e4SLinus Torvalds if (error) 31321da177e4SLinus Torvalds return error; 31331da177e4SLinus Torvalds 31341da177e4SLinus Torvalds if (!new_dentry->d_inode) 3135a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 31361da177e4SLinus Torvalds else 31371da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 31381da177e4SLinus Torvalds if (error) 31391da177e4SLinus Torvalds return error; 31401da177e4SLinus Torvalds 3141acfa4380SAl Viro if (!old_dir->i_op->rename) 31421da177e4SLinus Torvalds return -EPERM; 31431da177e4SLinus Torvalds 31440eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 31450eeca283SRobert Love 31461da177e4SLinus Torvalds if (is_dir) 31471da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 31481da177e4SLinus Torvalds else 31491da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3150123df294SAl Viro if (!error) 3151123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 31525a190ae6SAl Viro new_dentry->d_inode, old_dentry); 31530eeca283SRobert Love fsnotify_oldname_free(old_name); 31540eeca283SRobert Love 31551da177e4SLinus Torvalds return error; 31561da177e4SLinus Torvalds } 31571da177e4SLinus Torvalds 31582e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 31592e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 31601da177e4SLinus Torvalds { 31611da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 31621da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 31631da177e4SLinus Torvalds struct dentry *trap; 31641da177e4SLinus Torvalds struct nameidata oldnd, newnd; 31652ad94ae6SAl Viro char *from; 31662ad94ae6SAl Viro char *to; 31672ad94ae6SAl Viro int error; 31681da177e4SLinus Torvalds 31692ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 31701da177e4SLinus Torvalds if (error) 31711da177e4SLinus Torvalds goto exit; 31721da177e4SLinus Torvalds 31732ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 31741da177e4SLinus Torvalds if (error) 31751da177e4SLinus Torvalds goto exit1; 31761da177e4SLinus Torvalds 31771da177e4SLinus Torvalds error = -EXDEV; 31784ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 31791da177e4SLinus Torvalds goto exit2; 31801da177e4SLinus Torvalds 31814ac91378SJan Blunck old_dir = oldnd.path.dentry; 31821da177e4SLinus Torvalds error = -EBUSY; 31831da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 31841da177e4SLinus Torvalds goto exit2; 31851da177e4SLinus Torvalds 31864ac91378SJan Blunck new_dir = newnd.path.dentry; 31871da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 31881da177e4SLinus Torvalds goto exit2; 31891da177e4SLinus Torvalds 31900612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 31910612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 31924e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 31930612d9fbSOGAWA Hirofumi 31941da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 31951da177e4SLinus Torvalds 319649705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 31971da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 31981da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 31991da177e4SLinus Torvalds goto exit3; 32001da177e4SLinus Torvalds /* source must exist */ 32011da177e4SLinus Torvalds error = -ENOENT; 32021da177e4SLinus Torvalds if (!old_dentry->d_inode) 32031da177e4SLinus Torvalds goto exit4; 32041da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 32051da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 32061da177e4SLinus Torvalds error = -ENOTDIR; 32071da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 32081da177e4SLinus Torvalds goto exit4; 32091da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 32101da177e4SLinus Torvalds goto exit4; 32111da177e4SLinus Torvalds } 32121da177e4SLinus Torvalds /* source should not be ancestor of target */ 32131da177e4SLinus Torvalds error = -EINVAL; 32141da177e4SLinus Torvalds if (old_dentry == trap) 32151da177e4SLinus Torvalds goto exit4; 321649705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 32171da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 32181da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 32191da177e4SLinus Torvalds goto exit4; 32201da177e4SLinus Torvalds /* target should not be an ancestor of source */ 32211da177e4SLinus Torvalds error = -ENOTEMPTY; 32221da177e4SLinus Torvalds if (new_dentry == trap) 32231da177e4SLinus Torvalds goto exit5; 32241da177e4SLinus Torvalds 32259079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 32269079b1ebSDave Hansen if (error) 32279079b1ebSDave Hansen goto exit5; 3228be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3229be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3230be6d3e56SKentaro Takeda if (error) 3231be6d3e56SKentaro Takeda goto exit6; 32321da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 32331da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 3234be6d3e56SKentaro Takeda exit6: 32359079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 32361da177e4SLinus Torvalds exit5: 32371da177e4SLinus Torvalds dput(new_dentry); 32381da177e4SLinus Torvalds exit4: 32391da177e4SLinus Torvalds dput(old_dentry); 32401da177e4SLinus Torvalds exit3: 32411da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 32421da177e4SLinus Torvalds exit2: 32431d957f9bSJan Blunck path_put(&newnd.path); 32442ad94ae6SAl Viro putname(to); 32451da177e4SLinus Torvalds exit1: 32461d957f9bSJan Blunck path_put(&oldnd.path); 32471da177e4SLinus Torvalds putname(from); 32482ad94ae6SAl Viro exit: 32491da177e4SLinus Torvalds return error; 32501da177e4SLinus Torvalds } 32511da177e4SLinus Torvalds 3252a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 32535590ff0dSUlrich Drepper { 32545590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 32555590ff0dSUlrich Drepper } 32565590ff0dSUlrich Drepper 32571da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 32581da177e4SLinus Torvalds { 32591da177e4SLinus Torvalds int len; 32601da177e4SLinus Torvalds 32611da177e4SLinus Torvalds len = PTR_ERR(link); 32621da177e4SLinus Torvalds if (IS_ERR(link)) 32631da177e4SLinus Torvalds goto out; 32641da177e4SLinus Torvalds 32651da177e4SLinus Torvalds len = strlen(link); 32661da177e4SLinus Torvalds if (len > (unsigned) buflen) 32671da177e4SLinus Torvalds len = buflen; 32681da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 32691da177e4SLinus Torvalds len = -EFAULT; 32701da177e4SLinus Torvalds out: 32711da177e4SLinus Torvalds return len; 32721da177e4SLinus Torvalds } 32731da177e4SLinus Torvalds 32741da177e4SLinus Torvalds /* 32751da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 32761da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 32771da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 32781da177e4SLinus Torvalds */ 32791da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 32801da177e4SLinus Torvalds { 32811da177e4SLinus Torvalds struct nameidata nd; 3282cc314eefSLinus Torvalds void *cookie; 3283694a1764SMarcin Slusarz int res; 3284cc314eefSLinus Torvalds 32851da177e4SLinus Torvalds nd.depth = 0; 3286cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3287694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3288694a1764SMarcin Slusarz return PTR_ERR(cookie); 3289694a1764SMarcin Slusarz 3290694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 32911da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3292cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3293694a1764SMarcin Slusarz return res; 32941da177e4SLinus Torvalds } 32951da177e4SLinus Torvalds 32961da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 32971da177e4SLinus Torvalds { 32981da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 32991da177e4SLinus Torvalds } 33001da177e4SLinus Torvalds 33011da177e4SLinus Torvalds /* get the link contents into pagecache */ 33021da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 33031da177e4SLinus Torvalds { 3304ebd09abbSDuane Griffin char *kaddr; 33051da177e4SLinus Torvalds struct page *page; 33061da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3307090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 33081da177e4SLinus Torvalds if (IS_ERR(page)) 33096fe6900eSNick Piggin return (char*)page; 33101da177e4SLinus Torvalds *ppage = page; 3311ebd09abbSDuane Griffin kaddr = kmap(page); 3312ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3313ebd09abbSDuane Griffin return kaddr; 33141da177e4SLinus Torvalds } 33151da177e4SLinus Torvalds 33161da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 33171da177e4SLinus Torvalds { 33181da177e4SLinus Torvalds struct page *page = NULL; 33191da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 33201da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 33211da177e4SLinus Torvalds if (page) { 33221da177e4SLinus Torvalds kunmap(page); 33231da177e4SLinus Torvalds page_cache_release(page); 33241da177e4SLinus Torvalds } 33251da177e4SLinus Torvalds return res; 33261da177e4SLinus Torvalds } 33271da177e4SLinus Torvalds 3328cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 33291da177e4SLinus Torvalds { 3330cc314eefSLinus Torvalds struct page *page = NULL; 33311da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 3332cc314eefSLinus Torvalds return page; 33331da177e4SLinus Torvalds } 33341da177e4SLinus Torvalds 3335cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 33361da177e4SLinus Torvalds { 3337cc314eefSLinus Torvalds struct page *page = cookie; 3338cc314eefSLinus Torvalds 3339cc314eefSLinus Torvalds if (page) { 33401da177e4SLinus Torvalds kunmap(page); 33411da177e4SLinus Torvalds page_cache_release(page); 33421da177e4SLinus Torvalds } 33431da177e4SLinus Torvalds } 33441da177e4SLinus Torvalds 334554566b2cSNick Piggin /* 334654566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 334754566b2cSNick Piggin */ 334854566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 33491da177e4SLinus Torvalds { 33501da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 33510adb25d2SKirill Korotaev struct page *page; 3352afddba49SNick Piggin void *fsdata; 3353beb497abSDmitriy Monakhov int err; 33541da177e4SLinus Torvalds char *kaddr; 335554566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 335654566b2cSNick Piggin if (nofs) 335754566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 33581da177e4SLinus Torvalds 33597e53cac4SNeilBrown retry: 3360afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 336154566b2cSNick Piggin flags, &page, &fsdata); 33621da177e4SLinus Torvalds if (err) 3363afddba49SNick Piggin goto fail; 3364afddba49SNick Piggin 33651da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 33661da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 33671da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 3368afddba49SNick Piggin 3369afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 3370afddba49SNick Piggin page, fsdata); 33711da177e4SLinus Torvalds if (err < 0) 33721da177e4SLinus Torvalds goto fail; 3373afddba49SNick Piggin if (err < len-1) 3374afddba49SNick Piggin goto retry; 3375afddba49SNick Piggin 33761da177e4SLinus Torvalds mark_inode_dirty(inode); 33771da177e4SLinus Torvalds return 0; 33781da177e4SLinus Torvalds fail: 33791da177e4SLinus Torvalds return err; 33801da177e4SLinus Torvalds } 33811da177e4SLinus Torvalds 33820adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 33830adb25d2SKirill Korotaev { 33840adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 338554566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 33860adb25d2SKirill Korotaev } 33870adb25d2SKirill Korotaev 338892e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 33891da177e4SLinus Torvalds .readlink = generic_readlink, 33901da177e4SLinus Torvalds .follow_link = page_follow_link_light, 33911da177e4SLinus Torvalds .put_link = page_put_link, 33921da177e4SLinus Torvalds }; 33931da177e4SLinus Torvalds 33942d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 33951da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 33961da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 33971da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 33981da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 33991da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 34001da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 34011da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 34021da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 34031da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 34040adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 34051da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 34061da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 34071da177e4SLinus Torvalds EXPORT_SYMBOL(path_lookup); 3408d1811465SAl Viro EXPORT_SYMBOL(kern_path); 340916f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 3410f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 34118c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission); 34121da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 34131da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 34141da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 34151da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 34161da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 34171da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 34181da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 34191da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 34201da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 34211da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 34221da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 34231da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 34241da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 34251da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 3426