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 */ 17231e6b01fSNick Piggin static inline int __acl_permission_check(struct inode *inode, int mask, 17331e6b01fSNick Piggin int (*check_acl)(struct inode *inode, int mask), int rcu) 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) { 18331e6b01fSNick Piggin if (rcu) { 18431e6b01fSNick Piggin return -ECHILD; 18531e6b01fSNick Piggin } else { 1865909ccaaSLinus Torvalds int error = check_acl(inode, mask); 1875909ccaaSLinus Torvalds if (error != -EAGAIN) 1885909ccaaSLinus Torvalds return error; 1895909ccaaSLinus Torvalds } 19031e6b01fSNick Piggin } 1915909ccaaSLinus Torvalds 1925909ccaaSLinus Torvalds if (in_group_p(inode->i_gid)) 1935909ccaaSLinus Torvalds mode >>= 3; 1945909ccaaSLinus Torvalds } 1955909ccaaSLinus Torvalds 1965909ccaaSLinus Torvalds /* 1975909ccaaSLinus Torvalds * If the DACs are ok we don't need any capability check. 1985909ccaaSLinus Torvalds */ 1995909ccaaSLinus Torvalds if ((mask & ~mode) == 0) 2005909ccaaSLinus Torvalds return 0; 2015909ccaaSLinus Torvalds return -EACCES; 2025909ccaaSLinus Torvalds } 2031da177e4SLinus Torvalds 20431e6b01fSNick Piggin static inline int acl_permission_check(struct inode *inode, int mask, 20531e6b01fSNick Piggin int (*check_acl)(struct inode *inode, int mask)) 20631e6b01fSNick Piggin { 20731e6b01fSNick Piggin return __acl_permission_check(inode, mask, check_acl, 0); 20831e6b01fSNick Piggin } 20931e6b01fSNick Piggin 2101da177e4SLinus Torvalds /** 2111da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 2121da177e4SLinus Torvalds * @inode: inode to check access rights for 2131da177e4SLinus Torvalds * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 2141da177e4SLinus Torvalds * @check_acl: optional callback to check for Posix ACLs 2151da177e4SLinus Torvalds * 2161da177e4SLinus Torvalds * Used to check for read/write/execute permissions on a file. 2171da177e4SLinus Torvalds * We use "fsuid" for this, letting us set arbitrary permissions 2181da177e4SLinus Torvalds * for filesystem access without changing the "normal" uids which 2191da177e4SLinus Torvalds * are used for other things.. 2201da177e4SLinus Torvalds */ 2211da177e4SLinus Torvalds int generic_permission(struct inode *inode, int mask, 2221da177e4SLinus Torvalds int (*check_acl)(struct inode *inode, int mask)) 2231da177e4SLinus Torvalds { 2245909ccaaSLinus Torvalds int ret; 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds /* 2275909ccaaSLinus Torvalds * Do the basic POSIX ACL permission checks. 2281da177e4SLinus Torvalds */ 2295909ccaaSLinus Torvalds ret = acl_permission_check(inode, mask, check_acl); 2305909ccaaSLinus Torvalds if (ret != -EACCES) 2315909ccaaSLinus Torvalds return ret; 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds /* 2341da177e4SLinus Torvalds * Read/write DACs are always overridable. 2351da177e4SLinus Torvalds * Executable DACs are overridable if at least one exec bit is set. 2361da177e4SLinus Torvalds */ 237f696a365SMiklos Szeredi if (!(mask & MAY_EXEC) || execute_ok(inode)) 2381da177e4SLinus Torvalds if (capable(CAP_DAC_OVERRIDE)) 2391da177e4SLinus Torvalds return 0; 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds /* 2421da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 2431da177e4SLinus Torvalds */ 2447ea66001SSerge E. Hallyn mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 2451da177e4SLinus Torvalds if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE))) 2461da177e4SLinus Torvalds if (capable(CAP_DAC_READ_SEARCH)) 2471da177e4SLinus Torvalds return 0; 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds return -EACCES; 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds 252cb23beb5SChristoph Hellwig /** 253cb23beb5SChristoph Hellwig * inode_permission - check for access rights to a given inode 254cb23beb5SChristoph Hellwig * @inode: inode to check permission on 255cb23beb5SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 256cb23beb5SChristoph Hellwig * 257cb23beb5SChristoph Hellwig * Used to check for read/write/execute permissions on an inode. 258cb23beb5SChristoph Hellwig * We use "fsuid" for this, letting us set arbitrary permissions 259cb23beb5SChristoph Hellwig * for filesystem access without changing the "normal" uids which 260cb23beb5SChristoph Hellwig * are used for other things. 261cb23beb5SChristoph Hellwig */ 262f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask) 2631da177e4SLinus Torvalds { 264e6305c43SAl Viro int retval; 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds if (mask & MAY_WRITE) { 26722590e41SMiklos Szeredi umode_t mode = inode->i_mode; 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds /* 2701da177e4SLinus Torvalds * Nobody gets write access to a read-only fs. 2711da177e4SLinus Torvalds */ 2721da177e4SLinus Torvalds if (IS_RDONLY(inode) && 2731da177e4SLinus Torvalds (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 2741da177e4SLinus Torvalds return -EROFS; 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds /* 2771da177e4SLinus Torvalds * Nobody gets write access to an immutable file. 2781da177e4SLinus Torvalds */ 2791da177e4SLinus Torvalds if (IS_IMMUTABLE(inode)) 2801da177e4SLinus Torvalds return -EACCES; 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds 283acfa4380SAl Viro if (inode->i_op->permission) 284b77b0646SAl Viro retval = inode->i_op->permission(inode, mask); 285f696a365SMiklos Szeredi else 2865909ccaaSLinus Torvalds retval = generic_permission(inode, mask, inode->i_op->check_acl); 287f696a365SMiklos Szeredi 2881da177e4SLinus Torvalds if (retval) 2891da177e4SLinus Torvalds return retval; 2901da177e4SLinus Torvalds 29108ce5f16SSerge E. Hallyn retval = devcgroup_inode_permission(inode, mask); 29208ce5f16SSerge E. Hallyn if (retval) 29308ce5f16SSerge E. Hallyn return retval; 29408ce5f16SSerge E. Hallyn 295d09ca739SEric Paris return security_inode_permission(inode, mask); 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 298e4543eddSChristoph Hellwig /** 2998c744fb8SChristoph Hellwig * file_permission - check for additional access rights to a given file 3008c744fb8SChristoph Hellwig * @file: file to check access rights for 3018c744fb8SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 3028c744fb8SChristoph Hellwig * 3038c744fb8SChristoph Hellwig * Used to check for read/write/execute permissions on an already opened 3048c744fb8SChristoph Hellwig * file. 3058c744fb8SChristoph Hellwig * 3068c744fb8SChristoph Hellwig * Note: 3078c744fb8SChristoph Hellwig * Do not use this function in new code. All access checks should 308cb23beb5SChristoph Hellwig * be done using inode_permission(). 3098c744fb8SChristoph Hellwig */ 3108c744fb8SChristoph Hellwig int file_permission(struct file *file, int mask) 3118c744fb8SChristoph Hellwig { 312f419a2e3SAl Viro return inode_permission(file->f_path.dentry->d_inode, mask); 3138c744fb8SChristoph Hellwig } 3148c744fb8SChristoph Hellwig 3151da177e4SLinus Torvalds /* 3161da177e4SLinus Torvalds * get_write_access() gets write permission for a file. 3171da177e4SLinus Torvalds * put_write_access() releases this write permission. 3181da177e4SLinus Torvalds * This is used for regular files. 3191da177e4SLinus Torvalds * We cannot support write (and maybe mmap read-write shared) accesses and 3201da177e4SLinus Torvalds * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode 3211da177e4SLinus Torvalds * can have the following values: 3221da177e4SLinus Torvalds * 0: no writers, no VM_DENYWRITE mappings 3231da177e4SLinus Torvalds * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist 3241da177e4SLinus Torvalds * > 0: (i_writecount) users are writing to the file. 3251da177e4SLinus Torvalds * 3261da177e4SLinus Torvalds * Normally we operate on that counter with atomic_{inc,dec} and it's safe 3271da177e4SLinus Torvalds * except for the cases where we don't hold i_writecount yet. Then we need to 3281da177e4SLinus Torvalds * use {get,deny}_write_access() - these functions check the sign and refuse 3291da177e4SLinus Torvalds * to do the change if sign is wrong. Exclusion between them is provided by 3301da177e4SLinus Torvalds * the inode->i_lock spinlock. 3311da177e4SLinus Torvalds */ 3321da177e4SLinus Torvalds 3331da177e4SLinus Torvalds int get_write_access(struct inode * inode) 3341da177e4SLinus Torvalds { 3351da177e4SLinus Torvalds spin_lock(&inode->i_lock); 3361da177e4SLinus Torvalds if (atomic_read(&inode->i_writecount) < 0) { 3371da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3381da177e4SLinus Torvalds return -ETXTBSY; 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds atomic_inc(&inode->i_writecount); 3411da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds return 0; 3441da177e4SLinus Torvalds } 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds int deny_write_access(struct file * file) 3471da177e4SLinus Torvalds { 3480f7fc9e4SJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds spin_lock(&inode->i_lock); 3511da177e4SLinus Torvalds if (atomic_read(&inode->i_writecount) > 0) { 3521da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3531da177e4SLinus Torvalds return -ETXTBSY; 3541da177e4SLinus Torvalds } 3551da177e4SLinus Torvalds atomic_dec(&inode->i_writecount); 3561da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds return 0; 3591da177e4SLinus Torvalds } 3601da177e4SLinus Torvalds 3611d957f9bSJan Blunck /** 3625dd784d0SJan Blunck * path_get - get a reference to a path 3635dd784d0SJan Blunck * @path: path to get the reference to 3645dd784d0SJan Blunck * 3655dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 3665dd784d0SJan Blunck */ 3675dd784d0SJan Blunck void path_get(struct path *path) 3685dd784d0SJan Blunck { 3695dd784d0SJan Blunck mntget(path->mnt); 3705dd784d0SJan Blunck dget(path->dentry); 3715dd784d0SJan Blunck } 3725dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 3735dd784d0SJan Blunck 3745dd784d0SJan Blunck /** 3751d957f9bSJan Blunck * path_put - put a reference to a path 3761d957f9bSJan Blunck * @path: path to put the reference to 3771d957f9bSJan Blunck * 3781d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 3791d957f9bSJan Blunck */ 3801d957f9bSJan Blunck void path_put(struct path *path) 3811da177e4SLinus Torvalds { 3821d957f9bSJan Blunck dput(path->dentry); 3831d957f9bSJan Blunck mntput(path->mnt); 3841da177e4SLinus Torvalds } 3851d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 3861da177e4SLinus Torvalds 387834f2a4aSTrond Myklebust /** 38831e6b01fSNick Piggin * nameidata_drop_rcu - drop this nameidata out of rcu-walk 38931e6b01fSNick Piggin * @nd: nameidata pathwalk data to drop 39031e6b01fSNick Piggin * @Returns: 0 on success, -ECHLID on failure 39131e6b01fSNick Piggin * 39231e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 39331e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). __drop_rcu* functions attempt 39431e6b01fSNick Piggin * to drop out of rcu-walk mode and take normal reference counts on dentries 39531e6b01fSNick Piggin * and vfsmounts to transition to rcu-walk mode. __drop_rcu* functions take 39631e6b01fSNick Piggin * refcounts at the last known good point before rcu-walk got stuck, so 39731e6b01fSNick Piggin * ref-walk may continue from there. If this is not successful (eg. a seqcount 39831e6b01fSNick Piggin * has changed), then failure is returned and path walk restarts from the 39931e6b01fSNick Piggin * beginning in ref-walk mode. 40031e6b01fSNick Piggin * 40131e6b01fSNick Piggin * nameidata_drop_rcu attempts to drop the current nd->path and nd->root into 40231e6b01fSNick Piggin * ref-walk. Must be called from rcu-walk context. 40331e6b01fSNick Piggin */ 40431e6b01fSNick Piggin static int nameidata_drop_rcu(struct nameidata *nd) 40531e6b01fSNick Piggin { 40631e6b01fSNick Piggin struct fs_struct *fs = current->fs; 40731e6b01fSNick Piggin struct dentry *dentry = nd->path.dentry; 40831e6b01fSNick Piggin 40931e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 41031e6b01fSNick Piggin if (nd->root.mnt) { 41131e6b01fSNick Piggin spin_lock(&fs->lock); 41231e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 41331e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 41431e6b01fSNick Piggin goto err_root; 41531e6b01fSNick Piggin } 41631e6b01fSNick Piggin spin_lock(&dentry->d_lock); 41731e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 41831e6b01fSNick Piggin goto err; 41931e6b01fSNick Piggin BUG_ON(nd->inode != dentry->d_inode); 42031e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 42131e6b01fSNick Piggin if (nd->root.mnt) { 42231e6b01fSNick Piggin path_get(&nd->root); 42331e6b01fSNick Piggin spin_unlock(&fs->lock); 42431e6b01fSNick Piggin } 42531e6b01fSNick Piggin mntget(nd->path.mnt); 42631e6b01fSNick Piggin 42731e6b01fSNick Piggin rcu_read_unlock(); 42831e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 42931e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 43031e6b01fSNick Piggin return 0; 43131e6b01fSNick Piggin err: 43231e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 43331e6b01fSNick Piggin err_root: 43431e6b01fSNick Piggin if (nd->root.mnt) 43531e6b01fSNick Piggin spin_unlock(&fs->lock); 43631e6b01fSNick Piggin return -ECHILD; 43731e6b01fSNick Piggin } 43831e6b01fSNick Piggin 43931e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */ 44031e6b01fSNick Piggin static inline int nameidata_drop_rcu_maybe(struct nameidata *nd) 44131e6b01fSNick Piggin { 44231e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) 44331e6b01fSNick Piggin return nameidata_drop_rcu(nd); 44431e6b01fSNick Piggin return 0; 44531e6b01fSNick Piggin } 44631e6b01fSNick Piggin 44731e6b01fSNick Piggin /** 44831e6b01fSNick Piggin * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk 44931e6b01fSNick Piggin * @nd: nameidata pathwalk data to drop 45031e6b01fSNick Piggin * @dentry: dentry to drop 45131e6b01fSNick Piggin * @Returns: 0 on success, -ECHLID on failure 45231e6b01fSNick Piggin * 45331e6b01fSNick Piggin * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root, 45431e6b01fSNick Piggin * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on 45531e6b01fSNick Piggin * @nd. Must be called from rcu-walk context. 45631e6b01fSNick Piggin */ 45731e6b01fSNick Piggin static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry) 45831e6b01fSNick Piggin { 45931e6b01fSNick Piggin struct fs_struct *fs = current->fs; 46031e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 46131e6b01fSNick Piggin 46231e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 46331e6b01fSNick Piggin if (nd->root.mnt) { 46431e6b01fSNick Piggin spin_lock(&fs->lock); 46531e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 46631e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 46731e6b01fSNick Piggin goto err_root; 46831e6b01fSNick Piggin } 46931e6b01fSNick Piggin spin_lock(&parent->d_lock); 47031e6b01fSNick Piggin spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); 47131e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 47231e6b01fSNick Piggin goto err; 47331e6b01fSNick Piggin /* 47431e6b01fSNick Piggin * If the sequence check on the child dentry passed, then the child has 47531e6b01fSNick Piggin * not been removed from its parent. This means the parent dentry must 47631e6b01fSNick Piggin * be valid and able to take a reference at this point. 47731e6b01fSNick Piggin */ 47831e6b01fSNick Piggin BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); 47931e6b01fSNick Piggin BUG_ON(!parent->d_count); 48031e6b01fSNick Piggin parent->d_count++; 48131e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 48231e6b01fSNick Piggin spin_unlock(&parent->d_lock); 48331e6b01fSNick Piggin if (nd->root.mnt) { 48431e6b01fSNick Piggin path_get(&nd->root); 48531e6b01fSNick Piggin spin_unlock(&fs->lock); 48631e6b01fSNick Piggin } 48731e6b01fSNick Piggin mntget(nd->path.mnt); 48831e6b01fSNick Piggin 48931e6b01fSNick Piggin rcu_read_unlock(); 49031e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 49131e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 49231e6b01fSNick Piggin return 0; 49331e6b01fSNick Piggin err: 49431e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 49531e6b01fSNick Piggin spin_unlock(&parent->d_lock); 49631e6b01fSNick Piggin err_root: 49731e6b01fSNick Piggin if (nd->root.mnt) 49831e6b01fSNick Piggin spin_unlock(&fs->lock); 49931e6b01fSNick Piggin return -ECHILD; 50031e6b01fSNick Piggin } 50131e6b01fSNick Piggin 50231e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */ 50331e6b01fSNick Piggin static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry) 50431e6b01fSNick Piggin { 50531e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) 50631e6b01fSNick Piggin return nameidata_dentry_drop_rcu(nd, dentry); 50731e6b01fSNick Piggin return 0; 50831e6b01fSNick Piggin } 50931e6b01fSNick Piggin 51031e6b01fSNick Piggin /** 51131e6b01fSNick Piggin * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk 51231e6b01fSNick Piggin * @nd: nameidata pathwalk data to drop 51331e6b01fSNick Piggin * @Returns: 0 on success, -ECHLID on failure 51431e6b01fSNick Piggin * 51531e6b01fSNick Piggin * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk. 51631e6b01fSNick Piggin * nd->path should be the final element of the lookup, so nd->root is discarded. 51731e6b01fSNick Piggin * Must be called from rcu-walk context. 51831e6b01fSNick Piggin */ 51931e6b01fSNick Piggin static int nameidata_drop_rcu_last(struct nameidata *nd) 52031e6b01fSNick Piggin { 52131e6b01fSNick Piggin struct dentry *dentry = nd->path.dentry; 52231e6b01fSNick Piggin 52331e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 52431e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 52531e6b01fSNick Piggin nd->root.mnt = NULL; 52631e6b01fSNick Piggin spin_lock(&dentry->d_lock); 52731e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 52831e6b01fSNick Piggin goto err_unlock; 52931e6b01fSNick Piggin BUG_ON(nd->inode != dentry->d_inode); 53031e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 53131e6b01fSNick Piggin 53231e6b01fSNick Piggin mntget(nd->path.mnt); 53331e6b01fSNick Piggin 53431e6b01fSNick Piggin rcu_read_unlock(); 53531e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 53631e6b01fSNick Piggin 53731e6b01fSNick Piggin return 0; 53831e6b01fSNick Piggin 53931e6b01fSNick Piggin err_unlock: 54031e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 54131e6b01fSNick Piggin rcu_read_unlock(); 54231e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 54331e6b01fSNick Piggin return -ECHILD; 54431e6b01fSNick Piggin } 54531e6b01fSNick Piggin 54631e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */ 54731e6b01fSNick Piggin static inline int nameidata_drop_rcu_last_maybe(struct nameidata *nd) 54831e6b01fSNick Piggin { 54931e6b01fSNick Piggin if (likely(nd->flags & LOOKUP_RCU)) 55031e6b01fSNick Piggin return nameidata_drop_rcu_last(nd); 55131e6b01fSNick Piggin return 0; 55231e6b01fSNick Piggin } 55331e6b01fSNick Piggin 55431e6b01fSNick Piggin /** 555834f2a4aSTrond Myklebust * release_open_intent - free up open intent resources 556834f2a4aSTrond Myklebust * @nd: pointer to nameidata 557834f2a4aSTrond Myklebust */ 558834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd) 559834f2a4aSTrond Myklebust { 5600f7fc9e4SJosef "Jeff" Sipek if (nd->intent.open.file->f_path.dentry == NULL) 561834f2a4aSTrond Myklebust put_filp(nd->intent.open.file); 562834f2a4aSTrond Myklebust else 563834f2a4aSTrond Myklebust fput(nd->intent.open.file); 564834f2a4aSTrond Myklebust } 565834f2a4aSTrond Myklebust 566bcdc5e01SIan Kent static inline struct dentry * 567bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd) 568bcdc5e01SIan Kent { 569bcdc5e01SIan Kent int status = dentry->d_op->d_revalidate(dentry, nd); 570bcdc5e01SIan Kent if (unlikely(status <= 0)) { 571bcdc5e01SIan Kent /* 572bcdc5e01SIan Kent * The dentry failed validation. 573bcdc5e01SIan Kent * If d_revalidate returned 0 attempt to invalidate 574bcdc5e01SIan Kent * the dentry otherwise d_revalidate is asking us 575bcdc5e01SIan Kent * to return a fail status. 576bcdc5e01SIan Kent */ 577bcdc5e01SIan Kent if (!status) { 578bcdc5e01SIan Kent if (!d_invalidate(dentry)) { 579bcdc5e01SIan Kent dput(dentry); 580bcdc5e01SIan Kent dentry = NULL; 581bcdc5e01SIan Kent } 582bcdc5e01SIan Kent } else { 583bcdc5e01SIan Kent dput(dentry); 584bcdc5e01SIan Kent dentry = ERR_PTR(status); 585bcdc5e01SIan Kent } 586bcdc5e01SIan Kent } 587bcdc5e01SIan Kent return dentry; 588bcdc5e01SIan Kent } 589bcdc5e01SIan Kent 5901da177e4SLinus Torvalds /* 59139159de2SJeff Layton * force_reval_path - force revalidation of a dentry 59239159de2SJeff Layton * 59339159de2SJeff Layton * In some situations the path walking code will trust dentries without 59439159de2SJeff Layton * revalidating them. This causes problems for filesystems that depend on 59539159de2SJeff Layton * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set 59639159de2SJeff Layton * (which indicates that it's possible for the dentry to go stale), force 59739159de2SJeff Layton * a d_revalidate call before proceeding. 59839159de2SJeff Layton * 59939159de2SJeff Layton * Returns 0 if the revalidation was successful. If the revalidation fails, 60039159de2SJeff Layton * either return the error returned by d_revalidate or -ESTALE if the 60139159de2SJeff Layton * revalidation it just returned 0. If d_revalidate returns 0, we attempt to 60239159de2SJeff Layton * invalidate the dentry. It's up to the caller to handle putting references 60339159de2SJeff Layton * to the path if necessary. 60439159de2SJeff Layton */ 60539159de2SJeff Layton static int 60639159de2SJeff Layton force_reval_path(struct path *path, struct nameidata *nd) 60739159de2SJeff Layton { 60839159de2SJeff Layton int status; 60939159de2SJeff Layton struct dentry *dentry = path->dentry; 61039159de2SJeff Layton 61139159de2SJeff Layton /* 61239159de2SJeff Layton * only check on filesystems where it's possible for the dentry to 61339159de2SJeff Layton * become stale. It's assumed that if this flag is set then the 61439159de2SJeff Layton * d_revalidate op will also be defined. 61539159de2SJeff Layton */ 61639159de2SJeff Layton if (!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) 61739159de2SJeff Layton return 0; 61839159de2SJeff Layton 61939159de2SJeff Layton status = dentry->d_op->d_revalidate(dentry, nd); 62039159de2SJeff Layton if (status > 0) 62139159de2SJeff Layton return 0; 62239159de2SJeff Layton 62339159de2SJeff Layton if (!status) { 62439159de2SJeff Layton d_invalidate(dentry); 62539159de2SJeff Layton status = -ESTALE; 62639159de2SJeff Layton } 62739159de2SJeff Layton return status; 62839159de2SJeff Layton } 62939159de2SJeff Layton 63039159de2SJeff Layton /* 631b75b5086SAl Viro * Short-cut version of permission(), for calling on directories 632b75b5086SAl Viro * during pathname resolution. Combines parts of permission() 633b75b5086SAl Viro * and generic_permission(), and tests ONLY for MAY_EXEC permission. 6341da177e4SLinus Torvalds * 6351da177e4SLinus Torvalds * If appropriate, check DAC only. If not appropriate, or 636b75b5086SAl Viro * short-cut DAC fails, then call ->permission() to do more 6371da177e4SLinus Torvalds * complete permission check. 6381da177e4SLinus Torvalds */ 63931e6b01fSNick Piggin static inline int __exec_permission(struct inode *inode, int rcu) 6401da177e4SLinus Torvalds { 6415909ccaaSLinus Torvalds int ret; 6421da177e4SLinus Torvalds 643cb9179eaSLinus Torvalds if (inode->i_op->permission) { 64431e6b01fSNick Piggin if (rcu) 64531e6b01fSNick Piggin return -ECHILD; 6465909ccaaSLinus Torvalds ret = inode->i_op->permission(inode, MAY_EXEC); 647cb9179eaSLinus Torvalds if (!ret) 648cb9179eaSLinus Torvalds goto ok; 649cb9179eaSLinus Torvalds return ret; 650cb9179eaSLinus Torvalds } 65131e6b01fSNick Piggin ret = __acl_permission_check(inode, MAY_EXEC, inode->i_op->check_acl, rcu); 6525909ccaaSLinus Torvalds if (!ret) 6531da177e4SLinus Torvalds goto ok; 65431e6b01fSNick Piggin if (rcu && ret == -ECHILD) 65531e6b01fSNick Piggin return ret; 6561da177e4SLinus Torvalds 657f1ac9f6bSLinus Torvalds if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH)) 6581da177e4SLinus Torvalds goto ok; 6591da177e4SLinus Torvalds 6605909ccaaSLinus Torvalds return ret; 6611da177e4SLinus Torvalds ok: 66231e6b01fSNick Piggin return security_inode_exec_permission(inode, rcu); 66331e6b01fSNick Piggin } 66431e6b01fSNick Piggin 66531e6b01fSNick Piggin static int exec_permission(struct inode *inode) 66631e6b01fSNick Piggin { 66731e6b01fSNick Piggin return __exec_permission(inode, 0); 66831e6b01fSNick Piggin } 66931e6b01fSNick Piggin 67031e6b01fSNick Piggin static int exec_permission_rcu(struct inode *inode) 67131e6b01fSNick Piggin { 67231e6b01fSNick Piggin return __exec_permission(inode, 1); 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds 6752a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 6762a737871SAl Viro { 677f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 678f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 6792a737871SAl Viro } 6802a737871SAl Viro 6816de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 6826de88d72SAl Viro 68331e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 68431e6b01fSNick Piggin { 68531e6b01fSNick Piggin if (!nd->root.mnt) { 68631e6b01fSNick Piggin struct fs_struct *fs = current->fs; 687c28cc364SNick Piggin unsigned seq; 688c28cc364SNick Piggin 689c28cc364SNick Piggin do { 690c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 69131e6b01fSNick Piggin nd->root = fs->root; 692c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 69331e6b01fSNick Piggin } 69431e6b01fSNick Piggin } 69531e6b01fSNick Piggin 696f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 6971da177e4SLinus Torvalds { 69831e6b01fSNick Piggin int ret; 69931e6b01fSNick Piggin 7001da177e4SLinus Torvalds if (IS_ERR(link)) 7011da177e4SLinus Torvalds goto fail; 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds if (*link == '/') { 7042a737871SAl Viro set_root(nd); 7051d957f9bSJan Blunck path_put(&nd->path); 7062a737871SAl Viro nd->path = nd->root; 7072a737871SAl Viro path_get(&nd->root); 7081da177e4SLinus Torvalds } 70931e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 710b4091d5fSChristoph Hellwig 71131e6b01fSNick Piggin ret = link_path_walk(link, nd); 71231e6b01fSNick Piggin return ret; 7131da177e4SLinus Torvalds fail: 7141d957f9bSJan Blunck path_put(&nd->path); 7151da177e4SLinus Torvalds return PTR_ERR(link); 7161da177e4SLinus Torvalds } 7171da177e4SLinus Torvalds 7181d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 719051d3812SIan Kent { 720051d3812SIan Kent dput(path->dentry); 7214ac91378SJan Blunck if (path->mnt != nd->path.mnt) 722051d3812SIan Kent mntput(path->mnt); 723051d3812SIan Kent } 724051d3812SIan Kent 725051d3812SIan Kent static inline void path_to_nameidata(struct path *path, struct nameidata *nd) 726051d3812SIan Kent { 72731e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 7284ac91378SJan Blunck dput(nd->path.dentry); 72931e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 7304ac91378SJan Blunck mntput(nd->path.mnt); 7319a229683SHuang Shijie } 73231e6b01fSNick Piggin nd->path.mnt = path->mnt; 7334ac91378SJan Blunck nd->path.dentry = path->dentry; 734051d3812SIan Kent } 735051d3812SIan Kent 736def4af30SAl Viro static __always_inline int 737def4af30SAl Viro __do_follow_link(struct path *path, struct nameidata *nd, void **p) 7381da177e4SLinus Torvalds { 7391da177e4SLinus Torvalds int error; 740cd4e91d3SAl Viro struct dentry *dentry = path->dentry; 7411da177e4SLinus Torvalds 742d671a1cbSAl Viro touch_atime(path->mnt, dentry); 7431da177e4SLinus Torvalds nd_set_link(nd, NULL); 744cd4e91d3SAl Viro 7454ac91378SJan Blunck if (path->mnt != nd->path.mnt) { 746051d3812SIan Kent path_to_nameidata(path, nd); 74731e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 748051d3812SIan Kent dget(dentry); 749051d3812SIan Kent } 750cd4e91d3SAl Viro mntget(path->mnt); 75131e6b01fSNick Piggin 75286acdca1SAl Viro nd->last_type = LAST_BIND; 753def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 754def4af30SAl Viro error = PTR_ERR(*p); 755def4af30SAl Viro if (!IS_ERR(*p)) { 7561da177e4SLinus Torvalds char *s = nd_get_link(nd); 757cc314eefSLinus Torvalds error = 0; 7581da177e4SLinus Torvalds if (s) 7591da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 76039159de2SJeff Layton else if (nd->last_type == LAST_BIND) { 76139159de2SJeff Layton error = force_reval_path(&nd->path, nd); 76239159de2SJeff Layton if (error) 76339159de2SJeff Layton path_put(&nd->path); 76439159de2SJeff Layton } 7651da177e4SLinus Torvalds } 7661da177e4SLinus Torvalds return error; 7671da177e4SLinus Torvalds } 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds /* 7701da177e4SLinus Torvalds * This limits recursive symlink follows to 8, while 7711da177e4SLinus Torvalds * limiting consecutive symlinks to 40. 7721da177e4SLinus Torvalds * 7731da177e4SLinus Torvalds * Without that kind of total limit, nasty chains of consecutive 7741da177e4SLinus Torvalds * symlinks can cause almost arbitrarily long lookups. 7751da177e4SLinus Torvalds */ 77690ebe565SAl Viro static inline int do_follow_link(struct path *path, struct nameidata *nd) 7771da177e4SLinus Torvalds { 778def4af30SAl Viro void *cookie; 7791da177e4SLinus Torvalds int err = -ELOOP; 7801da177e4SLinus Torvalds if (current->link_count >= MAX_NESTED_LINKS) 7811da177e4SLinus Torvalds goto loop; 7821da177e4SLinus Torvalds if (current->total_link_count >= 40) 7831da177e4SLinus Torvalds goto loop; 7841da177e4SLinus Torvalds BUG_ON(nd->depth >= MAX_NESTED_LINKS); 7851da177e4SLinus Torvalds cond_resched(); 78690ebe565SAl Viro err = security_inode_follow_link(path->dentry, nd); 7871da177e4SLinus Torvalds if (err) 7881da177e4SLinus Torvalds goto loop; 7891da177e4SLinus Torvalds current->link_count++; 7901da177e4SLinus Torvalds current->total_link_count++; 7911da177e4SLinus Torvalds nd->depth++; 792def4af30SAl Viro err = __do_follow_link(path, nd, &cookie); 793def4af30SAl Viro if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link) 794def4af30SAl Viro path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie); 795258fa999SAl Viro path_put(path); 7961da177e4SLinus Torvalds current->link_count--; 7971da177e4SLinus Torvalds nd->depth--; 7981da177e4SLinus Torvalds return err; 7991da177e4SLinus Torvalds loop: 8001d957f9bSJan Blunck path_put_conditional(path, nd); 8011d957f9bSJan Blunck path_put(&nd->path); 8021da177e4SLinus Torvalds return err; 8031da177e4SLinus Torvalds } 8041da177e4SLinus Torvalds 80531e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 80631e6b01fSNick Piggin { 80731e6b01fSNick Piggin struct vfsmount *parent; 80831e6b01fSNick Piggin struct dentry *mountpoint; 80931e6b01fSNick Piggin 81031e6b01fSNick Piggin parent = path->mnt->mnt_parent; 81131e6b01fSNick Piggin if (parent == path->mnt) 81231e6b01fSNick Piggin return 0; 81331e6b01fSNick Piggin mountpoint = path->mnt->mnt_mountpoint; 81431e6b01fSNick Piggin path->dentry = mountpoint; 81531e6b01fSNick Piggin path->mnt = parent; 81631e6b01fSNick Piggin return 1; 81731e6b01fSNick Piggin } 81831e6b01fSNick Piggin 819bab77ebfSAl Viro int follow_up(struct path *path) 8201da177e4SLinus Torvalds { 8211da177e4SLinus Torvalds struct vfsmount *parent; 8221da177e4SLinus Torvalds struct dentry *mountpoint; 82399b7db7bSNick Piggin 82499b7db7bSNick Piggin br_read_lock(vfsmount_lock); 825bab77ebfSAl Viro parent = path->mnt->mnt_parent; 826bab77ebfSAl Viro if (parent == path->mnt) { 82799b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 8281da177e4SLinus Torvalds return 0; 8291da177e4SLinus Torvalds } 8301da177e4SLinus Torvalds mntget(parent); 831bab77ebfSAl Viro mountpoint = dget(path->mnt->mnt_mountpoint); 83299b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 833bab77ebfSAl Viro dput(path->dentry); 834bab77ebfSAl Viro path->dentry = mountpoint; 835bab77ebfSAl Viro mntput(path->mnt); 836bab77ebfSAl Viro path->mnt = parent; 8371da177e4SLinus Torvalds return 1; 8381da177e4SLinus Torvalds } 8391da177e4SLinus Torvalds 840b5c84bf6SNick Piggin /* 841b5c84bf6SNick Piggin * serialization is taken care of in namespace.c 8421da177e4SLinus Torvalds */ 84331e6b01fSNick Piggin static void __follow_mount_rcu(struct nameidata *nd, struct path *path, 84431e6b01fSNick Piggin struct inode **inode) 84531e6b01fSNick Piggin { 84631e6b01fSNick Piggin while (d_mountpoint(path->dentry)) { 84731e6b01fSNick Piggin struct vfsmount *mounted; 84831e6b01fSNick Piggin mounted = __lookup_mnt(path->mnt, path->dentry, 1); 84931e6b01fSNick Piggin if (!mounted) 85031e6b01fSNick Piggin return; 85131e6b01fSNick Piggin path->mnt = mounted; 85231e6b01fSNick Piggin path->dentry = mounted->mnt_root; 85331e6b01fSNick Piggin nd->seq = read_seqcount_begin(&path->dentry->d_seq); 85431e6b01fSNick Piggin *inode = path->dentry->d_inode; 85531e6b01fSNick Piggin } 85631e6b01fSNick Piggin } 85731e6b01fSNick Piggin 858463ffb2eSAl Viro static int __follow_mount(struct path *path) 859463ffb2eSAl Viro { 860463ffb2eSAl Viro int res = 0; 861463ffb2eSAl Viro while (d_mountpoint(path->dentry)) { 8621c755af4SAl Viro struct vfsmount *mounted = lookup_mnt(path); 863463ffb2eSAl Viro if (!mounted) 864463ffb2eSAl Viro break; 865463ffb2eSAl Viro dput(path->dentry); 866463ffb2eSAl Viro if (res) 867463ffb2eSAl Viro mntput(path->mnt); 868463ffb2eSAl Viro path->mnt = mounted; 869463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 870463ffb2eSAl Viro res = 1; 871463ffb2eSAl Viro } 872463ffb2eSAl Viro return res; 873463ffb2eSAl Viro } 874463ffb2eSAl Viro 87579ed0226SAl Viro static void follow_mount(struct path *path) 8761da177e4SLinus Torvalds { 87779ed0226SAl Viro while (d_mountpoint(path->dentry)) { 8781c755af4SAl Viro struct vfsmount *mounted = lookup_mnt(path); 8791da177e4SLinus Torvalds if (!mounted) 8801da177e4SLinus Torvalds break; 88179ed0226SAl Viro dput(path->dentry); 88279ed0226SAl Viro mntput(path->mnt); 88379ed0226SAl Viro path->mnt = mounted; 88479ed0226SAl Viro path->dentry = dget(mounted->mnt_root); 8851da177e4SLinus Torvalds } 8861da177e4SLinus Torvalds } 8871da177e4SLinus Torvalds 8889393bd07SAl Viro int follow_down(struct path *path) 8891da177e4SLinus Torvalds { 8901da177e4SLinus Torvalds struct vfsmount *mounted; 8911da177e4SLinus Torvalds 8921c755af4SAl Viro mounted = lookup_mnt(path); 8931da177e4SLinus Torvalds if (mounted) { 8949393bd07SAl Viro dput(path->dentry); 8959393bd07SAl Viro mntput(path->mnt); 8969393bd07SAl Viro path->mnt = mounted; 8979393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 8981da177e4SLinus Torvalds return 1; 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds return 0; 9011da177e4SLinus Torvalds } 9021da177e4SLinus Torvalds 90331e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 90431e6b01fSNick Piggin { 90531e6b01fSNick Piggin struct inode *inode = nd->inode; 90631e6b01fSNick Piggin 90731e6b01fSNick Piggin set_root_rcu(nd); 90831e6b01fSNick Piggin 90931e6b01fSNick Piggin while(1) { 91031e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 91131e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 91231e6b01fSNick Piggin break; 91331e6b01fSNick Piggin } 91431e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 91531e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 91631e6b01fSNick Piggin struct dentry *parent = old->d_parent; 91731e6b01fSNick Piggin unsigned seq; 91831e6b01fSNick Piggin 91931e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 92031e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 92131e6b01fSNick Piggin return -ECHILD; 92231e6b01fSNick Piggin inode = parent->d_inode; 92331e6b01fSNick Piggin nd->path.dentry = parent; 92431e6b01fSNick Piggin nd->seq = seq; 92531e6b01fSNick Piggin break; 92631e6b01fSNick Piggin } 92731e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 92831e6b01fSNick Piggin break; 92931e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 93031e6b01fSNick Piggin inode = nd->path.dentry->d_inode; 93131e6b01fSNick Piggin } 93231e6b01fSNick Piggin __follow_mount_rcu(nd, &nd->path, &inode); 93331e6b01fSNick Piggin nd->inode = inode; 93431e6b01fSNick Piggin 93531e6b01fSNick Piggin return 0; 93631e6b01fSNick Piggin } 93731e6b01fSNick Piggin 93831e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 9391da177e4SLinus Torvalds { 9402a737871SAl Viro set_root(nd); 941e518ddb7SAndreas Mohr 9421da177e4SLinus Torvalds while(1) { 9434ac91378SJan Blunck struct dentry *old = nd->path.dentry; 9441da177e4SLinus Torvalds 9452a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 9462a737871SAl Viro nd->path.mnt == nd->root.mnt) { 9471da177e4SLinus Torvalds break; 9481da177e4SLinus Torvalds } 9494ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 9503088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 9513088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 9521da177e4SLinus Torvalds dput(old); 9531da177e4SLinus Torvalds break; 9541da177e4SLinus Torvalds } 9553088dd70SAl Viro if (!follow_up(&nd->path)) 9561da177e4SLinus Torvalds break; 9571da177e4SLinus Torvalds } 95879ed0226SAl Viro follow_mount(&nd->path); 95931e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds /* 963baa03890SNick Piggin * Allocate a dentry with name and parent, and perform a parent 964baa03890SNick Piggin * directory ->lookup on it. Returns the new dentry, or ERR_PTR 965baa03890SNick Piggin * on error. parent->d_inode->i_mutex must be held. d_lookup must 966baa03890SNick Piggin * have verified that no child exists while under i_mutex. 967baa03890SNick Piggin */ 968baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent, 969baa03890SNick Piggin struct qstr *name, struct nameidata *nd) 970baa03890SNick Piggin { 971baa03890SNick Piggin struct inode *inode = parent->d_inode; 972baa03890SNick Piggin struct dentry *dentry; 973baa03890SNick Piggin struct dentry *old; 974baa03890SNick Piggin 975baa03890SNick Piggin /* Don't create child dentry for a dead directory. */ 976baa03890SNick Piggin if (unlikely(IS_DEADDIR(inode))) 977baa03890SNick Piggin return ERR_PTR(-ENOENT); 978baa03890SNick Piggin 979baa03890SNick Piggin dentry = d_alloc(parent, name); 980baa03890SNick Piggin if (unlikely(!dentry)) 981baa03890SNick Piggin return ERR_PTR(-ENOMEM); 982baa03890SNick Piggin 983baa03890SNick Piggin old = inode->i_op->lookup(inode, dentry, nd); 984baa03890SNick Piggin if (unlikely(old)) { 985baa03890SNick Piggin dput(dentry); 986baa03890SNick Piggin dentry = old; 987baa03890SNick Piggin } 988baa03890SNick Piggin return dentry; 989baa03890SNick Piggin } 990baa03890SNick Piggin 991baa03890SNick Piggin /* 9921da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 9931da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 9941da177e4SLinus Torvalds * It _is_ time-critical. 9951da177e4SLinus Torvalds */ 9961da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name, 99731e6b01fSNick Piggin struct path *path, struct inode **inode) 9981da177e4SLinus Torvalds { 9994ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 100031e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 10016e6b1bd1SAl Viro struct inode *dir; 10023cac260aSAl Viro /* 10033cac260aSAl Viro * See if the low-level filesystem might want 10043cac260aSAl Viro * to use its own hash.. 10053cac260aSAl Viro */ 100631e6b01fSNick Piggin if (parent->d_op && parent->d_op->d_hash) { 100731e6b01fSNick Piggin int err = parent->d_op->d_hash(parent, nd->inode, name); 10083cac260aSAl Viro if (err < 0) 10093cac260aSAl Viro return err; 10103cac260aSAl Viro } 10111da177e4SLinus Torvalds 1012b04f784eSNick Piggin /* 1013b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1014b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1015b04f784eSNick Piggin * do the non-racy lookup, below. 1016b04f784eSNick Piggin */ 101731e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 101831e6b01fSNick Piggin unsigned seq; 101931e6b01fSNick Piggin 102031e6b01fSNick Piggin *inode = nd->inode; 102131e6b01fSNick Piggin dentry = __d_lookup_rcu(parent, name, &seq, inode); 102231e6b01fSNick Piggin if (!dentry) { 102331e6b01fSNick Piggin if (nameidata_drop_rcu(nd)) 102431e6b01fSNick Piggin return -ECHILD; 102531e6b01fSNick Piggin goto need_lookup; 102631e6b01fSNick Piggin } 102731e6b01fSNick Piggin /* Memory barrier in read_seqcount_begin of child is enough */ 102831e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 102931e6b01fSNick Piggin return -ECHILD; 103031e6b01fSNick Piggin 103131e6b01fSNick Piggin nd->seq = seq; 103231e6b01fSNick Piggin if (dentry->d_op && dentry->d_op->d_revalidate) { 103331e6b01fSNick Piggin /* We commonly drop rcu-walk here */ 103431e6b01fSNick Piggin if (nameidata_dentry_drop_rcu(nd, dentry)) 103531e6b01fSNick Piggin return -ECHILD; 103631e6b01fSNick Piggin goto need_revalidate; 103731e6b01fSNick Piggin } 103831e6b01fSNick Piggin path->mnt = mnt; 103931e6b01fSNick Piggin path->dentry = dentry; 104031e6b01fSNick Piggin __follow_mount_rcu(nd, path, inode); 104131e6b01fSNick Piggin } else { 104231e6b01fSNick Piggin dentry = __d_lookup(parent, name); 10431da177e4SLinus Torvalds if (!dentry) 10441da177e4SLinus Torvalds goto need_lookup; 10452e2e88eaSNick Piggin found: 10461da177e4SLinus Torvalds if (dentry->d_op && dentry->d_op->d_revalidate) 10471da177e4SLinus Torvalds goto need_revalidate; 10481da177e4SLinus Torvalds done: 10491da177e4SLinus Torvalds path->mnt = mnt; 10501da177e4SLinus Torvalds path->dentry = dentry; 1051634ee701SAl Viro __follow_mount(path); 105231e6b01fSNick Piggin *inode = path->dentry->d_inode; 105331e6b01fSNick Piggin } 10541da177e4SLinus Torvalds return 0; 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds need_lookup: 10576e6b1bd1SAl Viro dir = parent->d_inode; 105831e6b01fSNick Piggin BUG_ON(nd->inode != dir); 10596e6b1bd1SAl Viro 10606e6b1bd1SAl Viro mutex_lock(&dir->i_mutex); 10616e6b1bd1SAl Viro /* 10626e6b1bd1SAl Viro * First re-do the cached lookup just in case it was created 1063b04f784eSNick Piggin * while we waited for the directory semaphore, or the first 1064b04f784eSNick Piggin * lookup failed due to an unrelated rename. 10656e6b1bd1SAl Viro * 1066b04f784eSNick Piggin * This could use version numbering or similar to avoid unnecessary 1067b04f784eSNick Piggin * cache lookups, but then we'd have to do the first lookup in the 1068b04f784eSNick Piggin * non-racy way. However in the common case here, everything should 1069b04f784eSNick Piggin * be hot in cache, so would it be a big win? 10706e6b1bd1SAl Viro */ 10716e6b1bd1SAl Viro dentry = d_lookup(parent, name); 1072baa03890SNick Piggin if (likely(!dentry)) { 1073baa03890SNick Piggin dentry = d_alloc_and_lookup(parent, name, nd); 10746e6b1bd1SAl Viro mutex_unlock(&dir->i_mutex); 10756e6b1bd1SAl Viro if (IS_ERR(dentry)) 10766e6b1bd1SAl Viro goto fail; 10776e6b1bd1SAl Viro goto done; 10786e6b1bd1SAl Viro } 10796e6b1bd1SAl Viro /* 10806e6b1bd1SAl Viro * Uhhuh! Nasty case: the cache was re-populated while 10816e6b1bd1SAl Viro * we waited on the semaphore. Need to revalidate. 10826e6b1bd1SAl Viro */ 10836e6b1bd1SAl Viro mutex_unlock(&dir->i_mutex); 10842e2e88eaSNick Piggin goto found; 10851da177e4SLinus Torvalds 10861da177e4SLinus Torvalds need_revalidate: 1087bcdc5e01SIan Kent dentry = do_revalidate(dentry, nd); 1088bcdc5e01SIan Kent if (!dentry) 10891da177e4SLinus Torvalds goto need_lookup; 1090bcdc5e01SIan Kent if (IS_ERR(dentry)) 1091bcdc5e01SIan Kent goto fail; 1092bcdc5e01SIan Kent goto done; 10931da177e4SLinus Torvalds 10941da177e4SLinus Torvalds fail: 10951da177e4SLinus Torvalds return PTR_ERR(dentry); 10961da177e4SLinus Torvalds } 10971da177e4SLinus Torvalds 10981da177e4SLinus Torvalds /* 1099ac278a9cSAl Viro * This is a temporary kludge to deal with "automount" symlinks; proper 1100ac278a9cSAl Viro * solution is to trigger them on follow_mount(), so that do_lookup() 1101ac278a9cSAl Viro * would DTRT. To be killed before 2.6.34-final. 1102ac278a9cSAl Viro */ 1103ac278a9cSAl Viro static inline int follow_on_final(struct inode *inode, unsigned lookup_flags) 1104ac278a9cSAl Viro { 1105ac278a9cSAl Viro return inode && unlikely(inode->i_op->follow_link) && 1106ac278a9cSAl Viro ((lookup_flags & LOOKUP_FOLLOW) || S_ISDIR(inode->i_mode)); 1107ac278a9cSAl Viro } 1108ac278a9cSAl Viro 1109ac278a9cSAl Viro /* 11101da177e4SLinus Torvalds * Name resolution. 1111ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1112ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 11131da177e4SLinus Torvalds * 1114ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1115ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 11161da177e4SLinus Torvalds */ 11176de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 11181da177e4SLinus Torvalds { 11191da177e4SLinus Torvalds struct path next; 11201da177e4SLinus Torvalds int err; 11211da177e4SLinus Torvalds unsigned int lookup_flags = nd->flags; 11221da177e4SLinus Torvalds 11231da177e4SLinus Torvalds while (*name=='/') 11241da177e4SLinus Torvalds name++; 11251da177e4SLinus Torvalds if (!*name) 11261da177e4SLinus Torvalds goto return_reval; 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds if (nd->depth) 1129f55eab82STrond Myklebust lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE); 11301da177e4SLinus Torvalds 11311da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 11321da177e4SLinus Torvalds for(;;) { 113331e6b01fSNick Piggin struct inode *inode; 11341da177e4SLinus Torvalds unsigned long hash; 11351da177e4SLinus Torvalds struct qstr this; 11361da177e4SLinus Torvalds unsigned int c; 11371da177e4SLinus Torvalds 1138cdce5d6bSTrond Myklebust nd->flags |= LOOKUP_CONTINUE; 113931e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 114031e6b01fSNick Piggin err = exec_permission_rcu(nd->inode); 114131e6b01fSNick Piggin if (err == -ECHILD) { 114231e6b01fSNick Piggin if (nameidata_drop_rcu(nd)) 114331e6b01fSNick Piggin return -ECHILD; 114431e6b01fSNick Piggin goto exec_again; 114531e6b01fSNick Piggin } 114631e6b01fSNick Piggin } else { 114731e6b01fSNick Piggin exec_again: 114831e6b01fSNick Piggin err = exec_permission(nd->inode); 114931e6b01fSNick Piggin } 11501da177e4SLinus Torvalds if (err) 11511da177e4SLinus Torvalds break; 11521da177e4SLinus Torvalds 11531da177e4SLinus Torvalds this.name = name; 11541da177e4SLinus Torvalds c = *(const unsigned char *)name; 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds hash = init_name_hash(); 11571da177e4SLinus Torvalds do { 11581da177e4SLinus Torvalds name++; 11591da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 11601da177e4SLinus Torvalds c = *(const unsigned char *)name; 11611da177e4SLinus Torvalds } while (c && (c != '/')); 11621da177e4SLinus Torvalds this.len = name - (const char *) this.name; 11631da177e4SLinus Torvalds this.hash = end_name_hash(hash); 11641da177e4SLinus Torvalds 11651da177e4SLinus Torvalds /* remove trailing slashes? */ 11661da177e4SLinus Torvalds if (!c) 11671da177e4SLinus Torvalds goto last_component; 11681da177e4SLinus Torvalds while (*++name == '/'); 11691da177e4SLinus Torvalds if (!*name) 11701da177e4SLinus Torvalds goto last_with_slashes; 11711da177e4SLinus Torvalds 11721da177e4SLinus Torvalds /* 11731da177e4SLinus Torvalds * "." and ".." are special - ".." especially so because it has 11741da177e4SLinus Torvalds * to be able to know about the current root directory and 11751da177e4SLinus Torvalds * parent relationships. 11761da177e4SLinus Torvalds */ 11771da177e4SLinus Torvalds if (this.name[0] == '.') switch (this.len) { 11781da177e4SLinus Torvalds default: 11791da177e4SLinus Torvalds break; 11801da177e4SLinus Torvalds case 2: 11811da177e4SLinus Torvalds if (this.name[1] != '.') 11821da177e4SLinus Torvalds break; 118331e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 118431e6b01fSNick Piggin if (follow_dotdot_rcu(nd)) 118531e6b01fSNick Piggin return -ECHILD; 118631e6b01fSNick Piggin } else 118758c465ebSAl Viro follow_dotdot(nd); 11881da177e4SLinus Torvalds /* fallthrough */ 11891da177e4SLinus Torvalds case 1: 11901da177e4SLinus Torvalds continue; 11911da177e4SLinus Torvalds } 11921da177e4SLinus Torvalds /* This does the actual lookups.. */ 119331e6b01fSNick Piggin err = do_lookup(nd, &this, &next, &inode); 11941da177e4SLinus Torvalds if (err) 11951da177e4SLinus Torvalds break; 11961da177e4SLinus Torvalds err = -ENOENT; 11971da177e4SLinus Torvalds if (!inode) 11981da177e4SLinus Torvalds goto out_dput; 11991da177e4SLinus Torvalds 12001da177e4SLinus Torvalds if (inode->i_op->follow_link) { 120131e6b01fSNick Piggin /* We commonly drop rcu-walk here */ 120231e6b01fSNick Piggin if (nameidata_dentry_drop_rcu_maybe(nd, next.dentry)) 120331e6b01fSNick Piggin return -ECHILD; 120431e6b01fSNick Piggin BUG_ON(inode != next.dentry->d_inode); 120590ebe565SAl Viro err = do_follow_link(&next, nd); 12061da177e4SLinus Torvalds if (err) 12071da177e4SLinus Torvalds goto return_err; 120831e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 12091da177e4SLinus Torvalds err = -ENOENT; 121031e6b01fSNick Piggin if (!nd->inode) 12111da177e4SLinus Torvalds break; 121231e6b01fSNick Piggin } else { 121309dd17d3SMiklos Szeredi path_to_nameidata(&next, nd); 121431e6b01fSNick Piggin nd->inode = inode; 121531e6b01fSNick Piggin } 12161da177e4SLinus Torvalds err = -ENOTDIR; 121731e6b01fSNick Piggin if (!nd->inode->i_op->lookup) 12181da177e4SLinus Torvalds break; 12191da177e4SLinus Torvalds continue; 12201da177e4SLinus Torvalds /* here ends the main loop */ 12211da177e4SLinus Torvalds 12221da177e4SLinus Torvalds last_with_slashes: 12231da177e4SLinus Torvalds lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 12241da177e4SLinus Torvalds last_component: 1225f55eab82STrond Myklebust /* Clear LOOKUP_CONTINUE iff it was previously unset */ 1226f55eab82STrond Myklebust nd->flags &= lookup_flags | ~LOOKUP_CONTINUE; 12271da177e4SLinus Torvalds if (lookup_flags & LOOKUP_PARENT) 12281da177e4SLinus Torvalds goto lookup_parent; 12291da177e4SLinus Torvalds if (this.name[0] == '.') switch (this.len) { 12301da177e4SLinus Torvalds default: 12311da177e4SLinus Torvalds break; 12321da177e4SLinus Torvalds case 2: 12331da177e4SLinus Torvalds if (this.name[1] != '.') 12341da177e4SLinus Torvalds break; 123531e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 123631e6b01fSNick Piggin if (follow_dotdot_rcu(nd)) 123731e6b01fSNick Piggin return -ECHILD; 123831e6b01fSNick Piggin } else 123958c465ebSAl Viro follow_dotdot(nd); 12401da177e4SLinus Torvalds /* fallthrough */ 12411da177e4SLinus Torvalds case 1: 12421da177e4SLinus Torvalds goto return_reval; 12431da177e4SLinus Torvalds } 124431e6b01fSNick Piggin err = do_lookup(nd, &this, &next, &inode); 12451da177e4SLinus Torvalds if (err) 12461da177e4SLinus Torvalds break; 1247ac278a9cSAl Viro if (follow_on_final(inode, lookup_flags)) { 124831e6b01fSNick Piggin if (nameidata_dentry_drop_rcu_maybe(nd, next.dentry)) 124931e6b01fSNick Piggin return -ECHILD; 125031e6b01fSNick Piggin BUG_ON(inode != next.dentry->d_inode); 125190ebe565SAl Viro err = do_follow_link(&next, nd); 12521da177e4SLinus Torvalds if (err) 12531da177e4SLinus Torvalds goto return_err; 125431e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 125531e6b01fSNick Piggin } else { 125609dd17d3SMiklos Szeredi path_to_nameidata(&next, nd); 125731e6b01fSNick Piggin nd->inode = inode; 125831e6b01fSNick Piggin } 12591da177e4SLinus Torvalds err = -ENOENT; 126031e6b01fSNick Piggin if (!nd->inode) 12611da177e4SLinus Torvalds break; 12621da177e4SLinus Torvalds if (lookup_flags & LOOKUP_DIRECTORY) { 12631da177e4SLinus Torvalds err = -ENOTDIR; 126431e6b01fSNick Piggin if (!nd->inode->i_op->lookup) 12651da177e4SLinus Torvalds break; 12661da177e4SLinus Torvalds } 12671da177e4SLinus Torvalds goto return_base; 12681da177e4SLinus Torvalds lookup_parent: 12691da177e4SLinus Torvalds nd->last = this; 12701da177e4SLinus Torvalds nd->last_type = LAST_NORM; 12711da177e4SLinus Torvalds if (this.name[0] != '.') 12721da177e4SLinus Torvalds goto return_base; 12731da177e4SLinus Torvalds if (this.len == 1) 12741da177e4SLinus Torvalds nd->last_type = LAST_DOT; 12751da177e4SLinus Torvalds else if (this.len == 2 && this.name[1] == '.') 12761da177e4SLinus Torvalds nd->last_type = LAST_DOTDOT; 12771da177e4SLinus Torvalds else 12781da177e4SLinus Torvalds goto return_base; 12791da177e4SLinus Torvalds return_reval: 12801da177e4SLinus Torvalds /* 12811da177e4SLinus Torvalds * We bypassed the ordinary revalidation routines. 12821da177e4SLinus Torvalds * We may need to check the cached dentry for staleness. 12831da177e4SLinus Torvalds */ 12844ac91378SJan Blunck if (nd->path.dentry && nd->path.dentry->d_sb && 12854ac91378SJan Blunck (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) { 128631e6b01fSNick Piggin if (nameidata_drop_rcu_maybe(nd)) 128731e6b01fSNick Piggin return -ECHILD; 12881da177e4SLinus Torvalds err = -ESTALE; 12891da177e4SLinus Torvalds /* Note: we do not d_invalidate() */ 12904ac91378SJan Blunck if (!nd->path.dentry->d_op->d_revalidate( 12914ac91378SJan Blunck nd->path.dentry, nd)) 12921da177e4SLinus Torvalds break; 12931da177e4SLinus Torvalds } 12941da177e4SLinus Torvalds return_base: 129531e6b01fSNick Piggin if (nameidata_drop_rcu_last_maybe(nd)) 129631e6b01fSNick Piggin return -ECHILD; 12971da177e4SLinus Torvalds return 0; 12981da177e4SLinus Torvalds out_dput: 129931e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) 13001d957f9bSJan Blunck path_put_conditional(&next, nd); 13011da177e4SLinus Torvalds break; 13021da177e4SLinus Torvalds } 130331e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) 13041d957f9bSJan Blunck path_put(&nd->path); 13051da177e4SLinus Torvalds return_err: 13061da177e4SLinus Torvalds return err; 13071da177e4SLinus Torvalds } 13081da177e4SLinus Torvalds 130931e6b01fSNick Piggin static inline int path_walk_rcu(const char *name, struct nameidata *nd) 131031e6b01fSNick Piggin { 131131e6b01fSNick Piggin current->total_link_count = 0; 131231e6b01fSNick Piggin 131331e6b01fSNick Piggin return link_path_walk(name, nd); 131431e6b01fSNick Piggin } 131531e6b01fSNick Piggin 131631e6b01fSNick Piggin static inline int path_walk_simple(const char *name, struct nameidata *nd) 131731e6b01fSNick Piggin { 131831e6b01fSNick Piggin current->total_link_count = 0; 131931e6b01fSNick Piggin 132031e6b01fSNick Piggin return link_path_walk(name, nd); 132131e6b01fSNick Piggin } 132231e6b01fSNick Piggin 1323fc9b52cdSHarvey Harrison static int path_walk(const char *name, struct nameidata *nd) 13241da177e4SLinus Torvalds { 13256de88d72SAl Viro struct path save = nd->path; 13266de88d72SAl Viro int result; 13276de88d72SAl Viro 13281da177e4SLinus Torvalds current->total_link_count = 0; 13296de88d72SAl Viro 13306de88d72SAl Viro /* make sure the stuff we saved doesn't go away */ 13316de88d72SAl Viro path_get(&save); 13326de88d72SAl Viro 13336de88d72SAl Viro result = link_path_walk(name, nd); 13346de88d72SAl Viro if (result == -ESTALE) { 13356de88d72SAl Viro /* nd->path had been dropped */ 13366de88d72SAl Viro current->total_link_count = 0; 13376de88d72SAl Viro nd->path = save; 13386de88d72SAl Viro path_get(&nd->path); 13396de88d72SAl Viro nd->flags |= LOOKUP_REVAL; 13406de88d72SAl Viro result = link_path_walk(name, nd); 13416de88d72SAl Viro } 13426de88d72SAl Viro 13436de88d72SAl Viro path_put(&save); 13446de88d72SAl Viro 13456de88d72SAl Viro return result; 13461da177e4SLinus Torvalds } 13471da177e4SLinus Torvalds 134831e6b01fSNick Piggin static void path_finish_rcu(struct nameidata *nd) 134931e6b01fSNick Piggin { 135031e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 135131e6b01fSNick Piggin /* RCU dangling. Cancel it. */ 135231e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 135331e6b01fSNick Piggin nd->root.mnt = NULL; 135431e6b01fSNick Piggin rcu_read_unlock(); 135531e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 135631e6b01fSNick Piggin } 135731e6b01fSNick Piggin if (nd->file) 135831e6b01fSNick Piggin fput(nd->file); 135931e6b01fSNick Piggin } 136031e6b01fSNick Piggin 136131e6b01fSNick Piggin static int path_init_rcu(int dfd, const char *name, unsigned int flags, struct nameidata *nd) 136231e6b01fSNick Piggin { 136331e6b01fSNick Piggin int retval = 0; 136431e6b01fSNick Piggin int fput_needed; 136531e6b01fSNick Piggin struct file *file; 136631e6b01fSNick Piggin 136731e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 136831e6b01fSNick Piggin nd->flags = flags | LOOKUP_RCU; 136931e6b01fSNick Piggin nd->depth = 0; 137031e6b01fSNick Piggin nd->root.mnt = NULL; 137131e6b01fSNick Piggin nd->file = NULL; 137231e6b01fSNick Piggin 137331e6b01fSNick Piggin if (*name=='/') { 137431e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1375c28cc364SNick Piggin unsigned seq; 137631e6b01fSNick Piggin 137731e6b01fSNick Piggin br_read_lock(vfsmount_lock); 137831e6b01fSNick Piggin rcu_read_lock(); 137931e6b01fSNick Piggin 1380c28cc364SNick Piggin do { 1381c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 138231e6b01fSNick Piggin nd->root = fs->root; 138331e6b01fSNick Piggin nd->path = nd->root; 1384c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1385c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 138631e6b01fSNick Piggin 138731e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 138831e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1389c28cc364SNick Piggin unsigned seq; 139031e6b01fSNick Piggin 139131e6b01fSNick Piggin br_read_lock(vfsmount_lock); 139231e6b01fSNick Piggin rcu_read_lock(); 139331e6b01fSNick Piggin 1394c28cc364SNick Piggin do { 1395c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 139631e6b01fSNick Piggin nd->path = fs->pwd; 1397c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1398c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1399c28cc364SNick Piggin 140031e6b01fSNick Piggin } else { 140131e6b01fSNick Piggin struct dentry *dentry; 140231e6b01fSNick Piggin 140331e6b01fSNick Piggin file = fget_light(dfd, &fput_needed); 140431e6b01fSNick Piggin retval = -EBADF; 140531e6b01fSNick Piggin if (!file) 140631e6b01fSNick Piggin goto out_fail; 140731e6b01fSNick Piggin 140831e6b01fSNick Piggin dentry = file->f_path.dentry; 140931e6b01fSNick Piggin 141031e6b01fSNick Piggin retval = -ENOTDIR; 141131e6b01fSNick Piggin if (!S_ISDIR(dentry->d_inode->i_mode)) 141231e6b01fSNick Piggin goto fput_fail; 141331e6b01fSNick Piggin 141431e6b01fSNick Piggin retval = file_permission(file, MAY_EXEC); 141531e6b01fSNick Piggin if (retval) 141631e6b01fSNick Piggin goto fput_fail; 141731e6b01fSNick Piggin 141831e6b01fSNick Piggin nd->path = file->f_path; 141931e6b01fSNick Piggin if (fput_needed) 142031e6b01fSNick Piggin nd->file = file; 142131e6b01fSNick Piggin 1422c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 142331e6b01fSNick Piggin br_read_lock(vfsmount_lock); 142431e6b01fSNick Piggin rcu_read_lock(); 142531e6b01fSNick Piggin } 142631e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 142731e6b01fSNick Piggin return 0; 142831e6b01fSNick Piggin 142931e6b01fSNick Piggin fput_fail: 143031e6b01fSNick Piggin fput_light(file, fput_needed); 143131e6b01fSNick Piggin out_fail: 143231e6b01fSNick Piggin return retval; 143331e6b01fSNick Piggin } 143431e6b01fSNick Piggin 14359b4a9b14SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd) 14361da177e4SLinus Torvalds { 1437ea3834d9SPrasanna Meda int retval = 0; 1438170aa3d0SUlrich Drepper int fput_needed; 1439170aa3d0SUlrich Drepper struct file *file; 14401da177e4SLinus Torvalds 14411da177e4SLinus Torvalds nd->last_type = LAST_ROOT; /* if there are only slashes... */ 14421da177e4SLinus Torvalds nd->flags = flags; 14431da177e4SLinus Torvalds nd->depth = 0; 14442a737871SAl Viro nd->root.mnt = NULL; 14451da177e4SLinus Torvalds 14461da177e4SLinus Torvalds if (*name=='/') { 14472a737871SAl Viro set_root(nd); 14482a737871SAl Viro nd->path = nd->root; 14492a737871SAl Viro path_get(&nd->root); 14505590ff0dSUlrich Drepper } else if (dfd == AT_FDCWD) { 1451f7ad3c6bSMiklos Szeredi get_fs_pwd(current->fs, &nd->path); 14525590ff0dSUlrich Drepper } else { 14535590ff0dSUlrich Drepper struct dentry *dentry; 14545590ff0dSUlrich Drepper 14555590ff0dSUlrich Drepper file = fget_light(dfd, &fput_needed); 14565590ff0dSUlrich Drepper retval = -EBADF; 1457170aa3d0SUlrich Drepper if (!file) 14586d09bb62STrond Myklebust goto out_fail; 14595590ff0dSUlrich Drepper 14600f7fc9e4SJosef "Jeff" Sipek dentry = file->f_path.dentry; 14615590ff0dSUlrich Drepper 14625590ff0dSUlrich Drepper retval = -ENOTDIR; 1463170aa3d0SUlrich Drepper if (!S_ISDIR(dentry->d_inode->i_mode)) 14646d09bb62STrond Myklebust goto fput_fail; 14655590ff0dSUlrich Drepper 14665590ff0dSUlrich Drepper retval = file_permission(file, MAY_EXEC); 1467170aa3d0SUlrich Drepper if (retval) 14686d09bb62STrond Myklebust goto fput_fail; 14695590ff0dSUlrich Drepper 14705dd784d0SJan Blunck nd->path = file->f_path; 14715dd784d0SJan Blunck path_get(&file->f_path); 14725590ff0dSUlrich Drepper 14735590ff0dSUlrich Drepper fput_light(file, fput_needed); 14741da177e4SLinus Torvalds } 147531e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 14769b4a9b14SAl Viro return 0; 14772dfdd266SJosef 'Jeff' Sipek 14789b4a9b14SAl Viro fput_fail: 14799b4a9b14SAl Viro fput_light(file, fput_needed); 14809b4a9b14SAl Viro out_fail: 14819b4a9b14SAl Viro return retval; 14829b4a9b14SAl Viro } 14839b4a9b14SAl Viro 14849b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 14859b4a9b14SAl Viro static int do_path_lookup(int dfd, const char *name, 14869b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 14879b4a9b14SAl Viro { 148831e6b01fSNick Piggin int retval; 148931e6b01fSNick Piggin 149031e6b01fSNick Piggin /* 149131e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 149231e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 149331e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 149431e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 149531e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 149631e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 149731e6b01fSNick Piggin * analogue, foo_rcu(). 149831e6b01fSNick Piggin * 149931e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 150031e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 150131e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 150231e6b01fSNick Piggin * be able to complete). 150331e6b01fSNick Piggin */ 150431e6b01fSNick Piggin retval = path_init_rcu(dfd, name, flags, nd); 150531e6b01fSNick Piggin if (unlikely(retval)) 150631e6b01fSNick Piggin return retval; 150731e6b01fSNick Piggin retval = path_walk_rcu(name, nd); 150831e6b01fSNick Piggin path_finish_rcu(nd); 15092a737871SAl Viro if (nd->root.mnt) { 15102a737871SAl Viro path_put(&nd->root); 15112a737871SAl Viro nd->root.mnt = NULL; 15122a737871SAl Viro } 151331e6b01fSNick Piggin 151431e6b01fSNick Piggin if (unlikely(retval == -ECHILD || retval == -ESTALE)) { 151531e6b01fSNick Piggin /* slower, locked walk */ 151631e6b01fSNick Piggin if (retval == -ESTALE) 151731e6b01fSNick Piggin flags |= LOOKUP_REVAL; 151831e6b01fSNick Piggin retval = path_init(dfd, name, flags, nd); 151931e6b01fSNick Piggin if (unlikely(retval)) 152031e6b01fSNick Piggin return retval; 152131e6b01fSNick Piggin retval = path_walk(name, nd); 152231e6b01fSNick Piggin if (nd->root.mnt) { 152331e6b01fSNick Piggin path_put(&nd->root); 152431e6b01fSNick Piggin nd->root.mnt = NULL; 152531e6b01fSNick Piggin } 152631e6b01fSNick Piggin } 152731e6b01fSNick Piggin 152831e6b01fSNick Piggin if (likely(!retval)) { 152931e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) { 153031e6b01fSNick Piggin if (nd->path.dentry && nd->inode) 153131e6b01fSNick Piggin audit_inode(name, nd->path.dentry); 153231e6b01fSNick Piggin } 153331e6b01fSNick Piggin } 153431e6b01fSNick Piggin 1535170aa3d0SUlrich Drepper return retval; 15361da177e4SLinus Torvalds } 15371da177e4SLinus Torvalds 1538fc9b52cdSHarvey Harrison int path_lookup(const char *name, unsigned int flags, 15395590ff0dSUlrich Drepper struct nameidata *nd) 15405590ff0dSUlrich Drepper { 15415590ff0dSUlrich Drepper return do_path_lookup(AT_FDCWD, name, flags, nd); 15425590ff0dSUlrich Drepper } 15435590ff0dSUlrich Drepper 1544d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1545d1811465SAl Viro { 1546d1811465SAl Viro struct nameidata nd; 1547d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1548d1811465SAl Viro if (!res) 1549d1811465SAl Viro *path = nd.path; 1550d1811465SAl Viro return res; 1551d1811465SAl Viro } 1552d1811465SAl Viro 155316f18200SJosef 'Jeff' Sipek /** 155416f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 155516f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 155616f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 155716f18200SJosef 'Jeff' Sipek * @name: pointer to file name 155816f18200SJosef 'Jeff' Sipek * @flags: lookup flags 155916f18200SJosef 'Jeff' Sipek * @nd: pointer to nameidata 156016f18200SJosef 'Jeff' Sipek */ 156116f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 156216f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 156316f18200SJosef 'Jeff' Sipek struct nameidata *nd) 156416f18200SJosef 'Jeff' Sipek { 156516f18200SJosef 'Jeff' Sipek int retval; 156616f18200SJosef 'Jeff' Sipek 156716f18200SJosef 'Jeff' Sipek /* same as do_path_lookup */ 156816f18200SJosef 'Jeff' Sipek nd->last_type = LAST_ROOT; 156916f18200SJosef 'Jeff' Sipek nd->flags = flags; 157016f18200SJosef 'Jeff' Sipek nd->depth = 0; 157116f18200SJosef 'Jeff' Sipek 1572c8e7f449SJan Blunck nd->path.dentry = dentry; 1573c8e7f449SJan Blunck nd->path.mnt = mnt; 1574c8e7f449SJan Blunck path_get(&nd->path); 15755b857119SAl Viro nd->root = nd->path; 15765b857119SAl Viro path_get(&nd->root); 157731e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 157816f18200SJosef 'Jeff' Sipek 157916f18200SJosef 'Jeff' Sipek retval = path_walk(name, nd); 15804ac91378SJan Blunck if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry && 158131e6b01fSNick Piggin nd->inode)) 15824ac91378SJan Blunck audit_inode(name, nd->path.dentry); 158316f18200SJosef 'Jeff' Sipek 15842a737871SAl Viro path_put(&nd->root); 15852a737871SAl Viro nd->root.mnt = NULL; 158616f18200SJosef 'Jeff' Sipek 15872a737871SAl Viro return retval; 158816f18200SJosef 'Jeff' Sipek } 158916f18200SJosef 'Jeff' Sipek 1590eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name, 1591eead1911SChristoph Hellwig struct dentry *base, struct nameidata *nd) 15921da177e4SLinus Torvalds { 159381fca444SChristoph Hellwig struct inode *inode = base->d_inode; 15941da177e4SLinus Torvalds struct dentry *dentry; 15951da177e4SLinus Torvalds int err; 15961da177e4SLinus Torvalds 159781fca444SChristoph Hellwig err = exec_permission(inode); 159881fca444SChristoph Hellwig if (err) 159981fca444SChristoph Hellwig return ERR_PTR(err); 16001da177e4SLinus Torvalds 16011da177e4SLinus Torvalds /* 16021da177e4SLinus Torvalds * See if the low-level filesystem might want 16031da177e4SLinus Torvalds * to use its own hash.. 16041da177e4SLinus Torvalds */ 16051da177e4SLinus Torvalds if (base->d_op && base->d_op->d_hash) { 1606b1e6a015SNick Piggin err = base->d_op->d_hash(base, inode, name); 16071da177e4SLinus Torvalds dentry = ERR_PTR(err); 16081da177e4SLinus Torvalds if (err < 0) 16091da177e4SLinus Torvalds goto out; 16101da177e4SLinus Torvalds } 16111da177e4SLinus Torvalds 1612b04f784eSNick Piggin /* 1613b04f784eSNick Piggin * Don't bother with __d_lookup: callers are for creat as 1614b04f784eSNick Piggin * well as unlink, so a lot of the time it would cost 1615b04f784eSNick Piggin * a double lookup. 16166e6b1bd1SAl Viro */ 16176e6b1bd1SAl Viro dentry = d_lookup(base, name); 16186e6b1bd1SAl Viro 16196e6b1bd1SAl Viro if (dentry && dentry->d_op && dentry->d_op->d_revalidate) 16206e6b1bd1SAl Viro dentry = do_revalidate(dentry, nd); 16216e6b1bd1SAl Viro 16221da177e4SLinus Torvalds if (!dentry) 1623baa03890SNick Piggin dentry = d_alloc_and_lookup(base, name, nd); 16241da177e4SLinus Torvalds out: 16251da177e4SLinus Torvalds return dentry; 16261da177e4SLinus Torvalds } 16271da177e4SLinus Torvalds 1628057f6c01SJames Morris /* 1629057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1630057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1631057f6c01SJames Morris * SMP-safe. 1632057f6c01SJames Morris */ 1633a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 16341da177e4SLinus Torvalds { 16354ac91378SJan Blunck return __lookup_hash(&nd->last, nd->path.dentry, nd); 16361da177e4SLinus Torvalds } 16371da177e4SLinus Torvalds 1638eead1911SChristoph Hellwig static int __lookup_one_len(const char *name, struct qstr *this, 1639eead1911SChristoph Hellwig struct dentry *base, int len) 16401da177e4SLinus Torvalds { 16411da177e4SLinus Torvalds unsigned long hash; 16421da177e4SLinus Torvalds unsigned int c; 16431da177e4SLinus Torvalds 1644057f6c01SJames Morris this->name = name; 1645057f6c01SJames Morris this->len = len; 16461da177e4SLinus Torvalds if (!len) 1647057f6c01SJames Morris return -EACCES; 16481da177e4SLinus Torvalds 16491da177e4SLinus Torvalds hash = init_name_hash(); 16501da177e4SLinus Torvalds while (len--) { 16511da177e4SLinus Torvalds c = *(const unsigned char *)name++; 16521da177e4SLinus Torvalds if (c == '/' || c == '\0') 1653057f6c01SJames Morris return -EACCES; 16541da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 16551da177e4SLinus Torvalds } 1656057f6c01SJames Morris this->hash = end_name_hash(hash); 1657057f6c01SJames Morris return 0; 1658057f6c01SJames Morris } 16591da177e4SLinus Torvalds 1660eead1911SChristoph Hellwig /** 1661a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1662eead1911SChristoph Hellwig * @name: pathname component to lookup 1663eead1911SChristoph Hellwig * @base: base directory to lookup from 1664eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1665eead1911SChristoph Hellwig * 1666a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1667a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1668eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1669eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1670eead1911SChristoph Hellwig */ 1671057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1672057f6c01SJames Morris { 1673057f6c01SJames Morris int err; 1674057f6c01SJames Morris struct qstr this; 1675057f6c01SJames Morris 16762f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 16772f9092e1SDavid Woodhouse 1678057f6c01SJames Morris err = __lookup_one_len(name, &this, base, len); 1679057f6c01SJames Morris if (err) 1680057f6c01SJames Morris return ERR_PTR(err); 1681eead1911SChristoph Hellwig 168249705b77SChristoph Hellwig return __lookup_hash(&this, base, NULL); 1683057f6c01SJames Morris } 1684057f6c01SJames Morris 16852d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags, 16862d8f3038SAl Viro struct path *path) 16871da177e4SLinus Torvalds { 16882d8f3038SAl Viro struct nameidata nd; 16891da177e4SLinus Torvalds char *tmp = getname(name); 16901da177e4SLinus Torvalds int err = PTR_ERR(tmp); 16911da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 16922d8f3038SAl Viro 16932d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 16942d8f3038SAl Viro 16952d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 16961da177e4SLinus Torvalds putname(tmp); 16972d8f3038SAl Viro if (!err) 16982d8f3038SAl Viro *path = nd.path; 16991da177e4SLinus Torvalds } 17001da177e4SLinus Torvalds return err; 17011da177e4SLinus Torvalds } 17021da177e4SLinus Torvalds 17032ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 17042ad94ae6SAl Viro struct nameidata *nd, char **name) 17052ad94ae6SAl Viro { 17062ad94ae6SAl Viro char *s = getname(path); 17072ad94ae6SAl Viro int error; 17082ad94ae6SAl Viro 17092ad94ae6SAl Viro if (IS_ERR(s)) 17102ad94ae6SAl Viro return PTR_ERR(s); 17112ad94ae6SAl Viro 17122ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 17132ad94ae6SAl Viro if (error) 17142ad94ae6SAl Viro putname(s); 17152ad94ae6SAl Viro else 17162ad94ae6SAl Viro *name = s; 17172ad94ae6SAl Viro 17182ad94ae6SAl Viro return error; 17192ad94ae6SAl Viro } 17202ad94ae6SAl Viro 17211da177e4SLinus Torvalds /* 17221da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 17231da177e4SLinus Torvalds * minimal. 17241da177e4SLinus Torvalds */ 17251da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 17261da177e4SLinus Torvalds { 1727da9592edSDavid Howells uid_t fsuid = current_fsuid(); 1728da9592edSDavid Howells 17291da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 17301da177e4SLinus Torvalds return 0; 1731da9592edSDavid Howells if (inode->i_uid == fsuid) 17321da177e4SLinus Torvalds return 0; 1733da9592edSDavid Howells if (dir->i_uid == fsuid) 17341da177e4SLinus Torvalds return 0; 17351da177e4SLinus Torvalds return !capable(CAP_FOWNER); 17361da177e4SLinus Torvalds } 17371da177e4SLinus Torvalds 17381da177e4SLinus Torvalds /* 17391da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 17401da177e4SLinus Torvalds * whether the type of victim is right. 17411da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 17421da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 17431da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 17441da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 17451da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 17461da177e4SLinus Torvalds * a. be owner of dir, or 17471da177e4SLinus Torvalds * b. be owner of victim, or 17481da177e4SLinus Torvalds * c. have CAP_FOWNER capability 17491da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 17501da177e4SLinus Torvalds * links pointing to it. 17511da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 17521da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 17531da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 17541da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 17551da177e4SLinus Torvalds * nfs_async_unlink(). 17561da177e4SLinus Torvalds */ 1757858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 17581da177e4SLinus Torvalds { 17591da177e4SLinus Torvalds int error; 17601da177e4SLinus Torvalds 17611da177e4SLinus Torvalds if (!victim->d_inode) 17621da177e4SLinus Torvalds return -ENOENT; 17631da177e4SLinus Torvalds 17641da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 1765cccc6bbaSAl Viro audit_inode_child(victim, dir); 17661da177e4SLinus Torvalds 1767f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 17681da177e4SLinus Torvalds if (error) 17691da177e4SLinus Torvalds return error; 17701da177e4SLinus Torvalds if (IS_APPEND(dir)) 17711da177e4SLinus Torvalds return -EPERM; 17721da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 1773f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 17741da177e4SLinus Torvalds return -EPERM; 17751da177e4SLinus Torvalds if (isdir) { 17761da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 17771da177e4SLinus Torvalds return -ENOTDIR; 17781da177e4SLinus Torvalds if (IS_ROOT(victim)) 17791da177e4SLinus Torvalds return -EBUSY; 17801da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 17811da177e4SLinus Torvalds return -EISDIR; 17821da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 17831da177e4SLinus Torvalds return -ENOENT; 17841da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 17851da177e4SLinus Torvalds return -EBUSY; 17861da177e4SLinus Torvalds return 0; 17871da177e4SLinus Torvalds } 17881da177e4SLinus Torvalds 17891da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 17901da177e4SLinus Torvalds * dir. 17911da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 17921da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 17931da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 17941da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 17951da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 17961da177e4SLinus Torvalds */ 1797a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 17981da177e4SLinus Torvalds { 17991da177e4SLinus Torvalds if (child->d_inode) 18001da177e4SLinus Torvalds return -EEXIST; 18011da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 18021da177e4SLinus Torvalds return -ENOENT; 1803f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 18041da177e4SLinus Torvalds } 18051da177e4SLinus Torvalds 18061da177e4SLinus Torvalds /* 18071da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 18081da177e4SLinus Torvalds */ 18091da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 18101da177e4SLinus Torvalds { 18111da177e4SLinus Torvalds struct dentry *p; 18121da177e4SLinus Torvalds 18131da177e4SLinus Torvalds if (p1 == p2) { 1814f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 18151da177e4SLinus Torvalds return NULL; 18161da177e4SLinus Torvalds } 18171da177e4SLinus Torvalds 1818a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 18191da177e4SLinus Torvalds 1820e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 1821e2761a11SOGAWA Hirofumi if (p) { 1822f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 1823f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 18241da177e4SLinus Torvalds return p; 18251da177e4SLinus Torvalds } 18261da177e4SLinus Torvalds 1827e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 1828e2761a11SOGAWA Hirofumi if (p) { 1829f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1830f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 18311da177e4SLinus Torvalds return p; 18321da177e4SLinus Torvalds } 18331da177e4SLinus Torvalds 1834f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1835f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 18361da177e4SLinus Torvalds return NULL; 18371da177e4SLinus Torvalds } 18381da177e4SLinus Torvalds 18391da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 18401da177e4SLinus Torvalds { 18411b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 18421da177e4SLinus Torvalds if (p1 != p2) { 18431b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 1844a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 18451da177e4SLinus Torvalds } 18461da177e4SLinus Torvalds } 18471da177e4SLinus Torvalds 18481da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode, 18491da177e4SLinus Torvalds struct nameidata *nd) 18501da177e4SLinus Torvalds { 1851a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 18521da177e4SLinus Torvalds 18531da177e4SLinus Torvalds if (error) 18541da177e4SLinus Torvalds return error; 18551da177e4SLinus Torvalds 1856acfa4380SAl Viro if (!dir->i_op->create) 18571da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 18581da177e4SLinus Torvalds mode &= S_IALLUGO; 18591da177e4SLinus Torvalds mode |= S_IFREG; 18601da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 18611da177e4SLinus Torvalds if (error) 18621da177e4SLinus Torvalds return error; 18631da177e4SLinus Torvalds error = dir->i_op->create(dir, dentry, mode, nd); 1864a74574aaSStephen Smalley if (!error) 1865f38aa942SAmy Griffis fsnotify_create(dir, dentry); 18661da177e4SLinus Torvalds return error; 18671da177e4SLinus Torvalds } 18681da177e4SLinus Torvalds 18693fb64190SChristoph Hellwig int may_open(struct path *path, int acc_mode, int flag) 18701da177e4SLinus Torvalds { 18713fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 18721da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 18731da177e4SLinus Torvalds int error; 18741da177e4SLinus Torvalds 18751da177e4SLinus Torvalds if (!inode) 18761da177e4SLinus Torvalds return -ENOENT; 18771da177e4SLinus Torvalds 1878c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 1879c8fe8f30SChristoph Hellwig case S_IFLNK: 18801da177e4SLinus Torvalds return -ELOOP; 1881c8fe8f30SChristoph Hellwig case S_IFDIR: 1882c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 18831da177e4SLinus Torvalds return -EISDIR; 1884c8fe8f30SChristoph Hellwig break; 1885c8fe8f30SChristoph Hellwig case S_IFBLK: 1886c8fe8f30SChristoph Hellwig case S_IFCHR: 18873fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 18881da177e4SLinus Torvalds return -EACCES; 1889c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 1890c8fe8f30SChristoph Hellwig case S_IFIFO: 1891c8fe8f30SChristoph Hellwig case S_IFSOCK: 18921da177e4SLinus Torvalds flag &= ~O_TRUNC; 1893c8fe8f30SChristoph Hellwig break; 18944a3fd211SDave Hansen } 1895b41572e9SDave Hansen 18963fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 1897b41572e9SDave Hansen if (error) 1898b41572e9SDave Hansen return error; 18996146f0d5SMimi Zohar 19001da177e4SLinus Torvalds /* 19011da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 19021da177e4SLinus Torvalds */ 19031da177e4SLinus Torvalds if (IS_APPEND(inode)) { 19048737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 19057715b521SAl Viro return -EPERM; 19061da177e4SLinus Torvalds if (flag & O_TRUNC) 19077715b521SAl Viro return -EPERM; 19081da177e4SLinus Torvalds } 19091da177e4SLinus Torvalds 19101da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 19117715b521SAl Viro if (flag & O_NOATIME && !is_owner_or_cap(inode)) 19127715b521SAl Viro return -EPERM; 19131da177e4SLinus Torvalds 19141da177e4SLinus Torvalds /* 19151da177e4SLinus Torvalds * Ensure there are no outstanding leases on the file. 19161da177e4SLinus Torvalds */ 1917b65a9cfcSAl Viro return break_lease(inode, flag); 19187715b521SAl Viro } 19197715b521SAl Viro 19207715b521SAl Viro static int handle_truncate(struct path *path) 19217715b521SAl Viro { 19227715b521SAl Viro struct inode *inode = path->dentry->d_inode; 19237715b521SAl Viro int error = get_write_access(inode); 19241da177e4SLinus Torvalds if (error) 19257715b521SAl Viro return error; 19261da177e4SLinus Torvalds /* 19271da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 19281da177e4SLinus Torvalds */ 19291da177e4SLinus Torvalds error = locks_verify_locked(inode); 1930be6d3e56SKentaro Takeda if (!error) 1931ea0d3ab2STetsuo Handa error = security_path_truncate(path); 19321da177e4SLinus Torvalds if (!error) { 19337715b521SAl Viro error = do_truncate(path->dentry, 0, 1934d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 1935d139d7ffSMiklos Szeredi NULL); 19361da177e4SLinus Torvalds } 19371da177e4SLinus Torvalds put_write_access(inode); 1938acd0c935SMimi Zohar return error; 19391da177e4SLinus Torvalds } 19401da177e4SLinus Torvalds 1941d57999e1SDave Hansen /* 1942d57999e1SDave Hansen * Be careful about ever adding any more callers of this 1943d57999e1SDave Hansen * function. Its flags must be in the namei format, not 1944d57999e1SDave Hansen * what get passed to sys_open(). 1945d57999e1SDave Hansen */ 1946d57999e1SDave Hansen static int __open_namei_create(struct nameidata *nd, struct path *path, 19478737c930SAl Viro int open_flag, int mode) 1948aab520e2SDave Hansen { 1949aab520e2SDave Hansen int error; 19504ac91378SJan Blunck struct dentry *dir = nd->path.dentry; 1951aab520e2SDave Hansen 1952aab520e2SDave Hansen if (!IS_POSIXACL(dir->d_inode)) 1953ce3b0f8dSAl Viro mode &= ~current_umask(); 1954be6d3e56SKentaro Takeda error = security_path_mknod(&nd->path, path->dentry, mode, 0); 1955be6d3e56SKentaro Takeda if (error) 1956be6d3e56SKentaro Takeda goto out_unlock; 1957aab520e2SDave Hansen error = vfs_create(dir->d_inode, path->dentry, mode, nd); 1958be6d3e56SKentaro Takeda out_unlock: 1959aab520e2SDave Hansen mutex_unlock(&dir->d_inode->i_mutex); 19604ac91378SJan Blunck dput(nd->path.dentry); 19614ac91378SJan Blunck nd->path.dentry = path->dentry; 196231e6b01fSNick Piggin 1963aab520e2SDave Hansen if (error) 1964aab520e2SDave Hansen return error; 1965aab520e2SDave Hansen /* Don't check for write permission, don't truncate */ 19668737c930SAl Viro return may_open(&nd->path, 0, open_flag & ~O_TRUNC); 1967aab520e2SDave Hansen } 1968aab520e2SDave Hansen 19691da177e4SLinus Torvalds /* 1970d57999e1SDave Hansen * Note that while the flag value (low two bits) for sys_open means: 1971d57999e1SDave Hansen * 00 - read-only 1972d57999e1SDave Hansen * 01 - write-only 1973d57999e1SDave Hansen * 10 - read-write 1974d57999e1SDave Hansen * 11 - special 1975d57999e1SDave Hansen * it is changed into 1976d57999e1SDave Hansen * 00 - no permissions needed 1977d57999e1SDave Hansen * 01 - read-permission 1978d57999e1SDave Hansen * 10 - write-permission 1979d57999e1SDave Hansen * 11 - read-write 1980d57999e1SDave Hansen * for the internal routines (ie open_namei()/follow_link() etc) 1981d57999e1SDave Hansen * This is more logical, and also allows the 00 "no perm needed" 1982d57999e1SDave Hansen * to be used for symlinks (where the permissions are checked 1983d57999e1SDave Hansen * later). 1984d57999e1SDave Hansen * 1985d57999e1SDave Hansen */ 1986d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 1987d57999e1SDave Hansen { 1988d57999e1SDave Hansen if ((flag+1) & O_ACCMODE) 1989d57999e1SDave Hansen flag++; 1990d57999e1SDave Hansen return flag; 1991d57999e1SDave Hansen } 1992d57999e1SDave Hansen 19937715b521SAl Viro static int open_will_truncate(int flag, struct inode *inode) 19944a3fd211SDave Hansen { 1995d57999e1SDave Hansen /* 19964a3fd211SDave Hansen * We'll never write to the fs underlying 19974a3fd211SDave Hansen * a device file. 19984a3fd211SDave Hansen */ 19994a3fd211SDave Hansen if (special_file(inode->i_mode)) 20004a3fd211SDave Hansen return 0; 20014a3fd211SDave Hansen return (flag & O_TRUNC); 20024a3fd211SDave Hansen } 20034a3fd211SDave Hansen 2004648fa861SAl Viro static struct file *finish_open(struct nameidata *nd, 20059a66179eSAl Viro int open_flag, int acc_mode) 2006648fa861SAl Viro { 2007648fa861SAl Viro struct file *filp; 2008648fa861SAl Viro int will_truncate; 2009648fa861SAl Viro int error; 2010648fa861SAl Viro 20119a66179eSAl Viro will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode); 2012648fa861SAl Viro if (will_truncate) { 2013648fa861SAl Viro error = mnt_want_write(nd->path.mnt); 2014648fa861SAl Viro if (error) 2015648fa861SAl Viro goto exit; 2016648fa861SAl Viro } 2017648fa861SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2018648fa861SAl Viro if (error) { 2019648fa861SAl Viro if (will_truncate) 2020648fa861SAl Viro mnt_drop_write(nd->path.mnt); 2021648fa861SAl Viro goto exit; 2022648fa861SAl Viro } 2023648fa861SAl Viro filp = nameidata_to_filp(nd); 2024648fa861SAl Viro if (!IS_ERR(filp)) { 2025648fa861SAl Viro error = ima_file_check(filp, acc_mode); 2026648fa861SAl Viro if (error) { 2027648fa861SAl Viro fput(filp); 2028648fa861SAl Viro filp = ERR_PTR(error); 2029648fa861SAl Viro } 2030648fa861SAl Viro } 2031648fa861SAl Viro if (!IS_ERR(filp)) { 2032648fa861SAl Viro if (will_truncate) { 2033648fa861SAl Viro error = handle_truncate(&nd->path); 2034648fa861SAl Viro if (error) { 2035648fa861SAl Viro fput(filp); 2036648fa861SAl Viro filp = ERR_PTR(error); 2037648fa861SAl Viro } 2038648fa861SAl Viro } 2039648fa861SAl Viro } 2040648fa861SAl Viro /* 2041648fa861SAl Viro * It is now safe to drop the mnt write 2042648fa861SAl Viro * because the filp has had a write taken 2043648fa861SAl Viro * on its behalf. 2044648fa861SAl Viro */ 2045648fa861SAl Viro if (will_truncate) 2046648fa861SAl Viro mnt_drop_write(nd->path.mnt); 2047d893f1bcSAl Viro path_put(&nd->path); 2048648fa861SAl Viro return filp; 2049648fa861SAl Viro 2050648fa861SAl Viro exit: 2051648fa861SAl Viro if (!IS_ERR(nd->intent.open.file)) 2052648fa861SAl Viro release_open_intent(nd); 2053648fa861SAl Viro path_put(&nd->path); 2054648fa861SAl Viro return ERR_PTR(error); 2055648fa861SAl Viro } 2056648fa861SAl Viro 205731e6b01fSNick Piggin /* 205831e6b01fSNick Piggin * Handle O_CREAT case for do_filp_open 205931e6b01fSNick Piggin */ 2060fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path, 20615b369df8SAl Viro int open_flag, int acc_mode, 20623e297b61SAl Viro int mode, const char *pathname) 2063fb1cc555SAl Viro { 2064a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 2065fb1cc555SAl Viro struct file *filp; 20661f36f774SAl Viro int error = -EISDIR; 2067fb1cc555SAl Viro 20681f36f774SAl Viro switch (nd->last_type) { 20691f36f774SAl Viro case LAST_DOTDOT: 20701f36f774SAl Viro follow_dotdot(nd); 20711f36f774SAl Viro dir = nd->path.dentry; 2072176306f5SNeil Brown case LAST_DOT: 20731f36f774SAl Viro if (nd->path.mnt->mnt_sb->s_type->fs_flags & FS_REVAL_DOT) { 20741f36f774SAl Viro if (!dir->d_op->d_revalidate(dir, nd)) { 20751f36f774SAl Viro error = -ESTALE; 2076a2c36b45SAl Viro goto exit; 20771f36f774SAl Viro } 20781f36f774SAl Viro } 20791f36f774SAl Viro /* fallthrough */ 20801f36f774SAl Viro case LAST_ROOT: 20811f36f774SAl Viro goto exit; 20821f36f774SAl Viro case LAST_BIND: 20831f36f774SAl Viro audit_inode(pathname, dir); 20841f36f774SAl Viro goto ok; 20851f36f774SAl Viro } 2086a2c36b45SAl Viro 20871f36f774SAl Viro /* trailing slashes? */ 208831e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 20891f36f774SAl Viro goto exit; 20901f36f774SAl Viro 2091a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 2092a1e28038SAl Viro 2093a1e28038SAl Viro path->dentry = lookup_hash(nd); 2094a1e28038SAl Viro path->mnt = nd->path.mnt; 2095a1e28038SAl Viro 2096fb1cc555SAl Viro error = PTR_ERR(path->dentry); 2097fb1cc555SAl Viro if (IS_ERR(path->dentry)) { 2098fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2099fb1cc555SAl Viro goto exit; 2100fb1cc555SAl Viro } 2101fb1cc555SAl Viro 2102fb1cc555SAl Viro if (IS_ERR(nd->intent.open.file)) { 2103fb1cc555SAl Viro error = PTR_ERR(nd->intent.open.file); 2104fb1cc555SAl Viro goto exit_mutex_unlock; 2105fb1cc555SAl Viro } 2106fb1cc555SAl Viro 2107fb1cc555SAl Viro /* Negative dentry, just create the file */ 2108fb1cc555SAl Viro if (!path->dentry->d_inode) { 2109fb1cc555SAl Viro /* 2110fb1cc555SAl Viro * This write is needed to ensure that a 2111fb1cc555SAl Viro * ro->rw transition does not occur between 2112fb1cc555SAl Viro * the time when the file is created and when 2113fb1cc555SAl Viro * a permanent write count is taken through 2114fb1cc555SAl Viro * the 'struct file' in nameidata_to_filp(). 2115fb1cc555SAl Viro */ 2116fb1cc555SAl Viro error = mnt_want_write(nd->path.mnt); 2117fb1cc555SAl Viro if (error) 2118fb1cc555SAl Viro goto exit_mutex_unlock; 2119fb1cc555SAl Viro error = __open_namei_create(nd, path, open_flag, mode); 2120fb1cc555SAl Viro if (error) { 2121fb1cc555SAl Viro mnt_drop_write(nd->path.mnt); 2122fb1cc555SAl Viro goto exit; 2123fb1cc555SAl Viro } 2124fb1cc555SAl Viro filp = nameidata_to_filp(nd); 2125fb1cc555SAl Viro mnt_drop_write(nd->path.mnt); 2126d893f1bcSAl Viro path_put(&nd->path); 2127fb1cc555SAl Viro if (!IS_ERR(filp)) { 2128fb1cc555SAl Viro error = ima_file_check(filp, acc_mode); 2129fb1cc555SAl Viro if (error) { 2130fb1cc555SAl Viro fput(filp); 2131fb1cc555SAl Viro filp = ERR_PTR(error); 2132fb1cc555SAl Viro } 2133fb1cc555SAl Viro } 2134fb1cc555SAl Viro return filp; 2135fb1cc555SAl Viro } 2136fb1cc555SAl Viro 2137fb1cc555SAl Viro /* 2138fb1cc555SAl Viro * It already exists. 2139fb1cc555SAl Viro */ 2140fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2141fb1cc555SAl Viro audit_inode(pathname, path->dentry); 2142fb1cc555SAl Viro 2143fb1cc555SAl Viro error = -EEXIST; 21445b369df8SAl Viro if (open_flag & O_EXCL) 2145fb1cc555SAl Viro goto exit_dput; 2146fb1cc555SAl Viro 2147fb1cc555SAl Viro if (__follow_mount(path)) { 2148fb1cc555SAl Viro error = -ELOOP; 21495b369df8SAl Viro if (open_flag & O_NOFOLLOW) 2150fb1cc555SAl Viro goto exit_dput; 2151fb1cc555SAl Viro } 2152fb1cc555SAl Viro 2153fb1cc555SAl Viro error = -ENOENT; 2154fb1cc555SAl Viro if (!path->dentry->d_inode) 2155fb1cc555SAl Viro goto exit_dput; 21569e67f361SAl Viro 21579e67f361SAl Viro if (path->dentry->d_inode->i_op->follow_link) 2158fb1cc555SAl Viro return NULL; 2159fb1cc555SAl Viro 2160fb1cc555SAl Viro path_to_nameidata(path, nd); 216131e6b01fSNick Piggin nd->inode = path->dentry->d_inode; 2162fb1cc555SAl Viro error = -EISDIR; 216331e6b01fSNick Piggin if (S_ISDIR(nd->inode->i_mode)) 2164fb1cc555SAl Viro goto exit; 216567ee3ad2SAl Viro ok: 21669a66179eSAl Viro filp = finish_open(nd, open_flag, acc_mode); 2167fb1cc555SAl Viro return filp; 2168fb1cc555SAl Viro 2169fb1cc555SAl Viro exit_mutex_unlock: 2170fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2171fb1cc555SAl Viro exit_dput: 2172fb1cc555SAl Viro path_put_conditional(path, nd); 2173fb1cc555SAl Viro exit: 2174fb1cc555SAl Viro if (!IS_ERR(nd->intent.open.file)) 2175fb1cc555SAl Viro release_open_intent(nd); 2176fb1cc555SAl Viro path_put(&nd->path); 2177fb1cc555SAl Viro return ERR_PTR(error); 2178fb1cc555SAl Viro } 2179fb1cc555SAl Viro 21804a3fd211SDave Hansen /* 21814a3fd211SDave Hansen * Note that the low bits of the passed in "open_flag" 21824a3fd211SDave Hansen * are not the same as in the local variable "flag". See 21834a3fd211SDave Hansen * open_to_namei_flags() for more details. 21841da177e4SLinus Torvalds */ 2185a70e65dfSChristoph Hellwig struct file *do_filp_open(int dfd, const char *pathname, 21866e8341a1SAl Viro int open_flag, int mode, int acc_mode) 21871da177e4SLinus Torvalds { 21884a3fd211SDave Hansen struct file *filp; 2189a70e65dfSChristoph Hellwig struct nameidata nd; 21906e8341a1SAl Viro int error; 21919850c056SAl Viro struct path path; 21921da177e4SLinus Torvalds int count = 0; 2193d57999e1SDave Hansen int flag = open_to_namei_flags(open_flag); 219431e6b01fSNick Piggin int flags; 21951f36f774SAl Viro 21961f36f774SAl Viro if (!(open_flag & O_CREAT)) 21971f36f774SAl Viro mode = 0; 21981da177e4SLinus Torvalds 2199b1085ba8SLino Sanfilippo /* Must never be set by userspace */ 2200b1085ba8SLino Sanfilippo open_flag &= ~FMODE_NONOTIFY; 2201b1085ba8SLino Sanfilippo 22026b2f3d1fSChristoph Hellwig /* 22036b2f3d1fSChristoph Hellwig * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only 22046b2f3d1fSChristoph Hellwig * check for O_DSYNC if the need any syncing at all we enforce it's 22056b2f3d1fSChristoph Hellwig * always set instead of having to deal with possibly weird behaviour 22066b2f3d1fSChristoph Hellwig * for malicious applications setting only __O_SYNC. 22076b2f3d1fSChristoph Hellwig */ 22086b2f3d1fSChristoph Hellwig if (open_flag & __O_SYNC) 22096b2f3d1fSChristoph Hellwig open_flag |= O_DSYNC; 22106b2f3d1fSChristoph Hellwig 22116e8341a1SAl Viro if (!acc_mode) 22126d125529SAl Viro acc_mode = MAY_OPEN | ACC_MODE(open_flag); 22131da177e4SLinus Torvalds 2214834f2a4aSTrond Myklebust /* O_TRUNC implies we need access checks for write permissions */ 22154296e2cbSAl Viro if (open_flag & O_TRUNC) 2216834f2a4aSTrond Myklebust acc_mode |= MAY_WRITE; 2217834f2a4aSTrond Myklebust 22181da177e4SLinus Torvalds /* Allow the LSM permission hook to distinguish append 22191da177e4SLinus Torvalds access from general write access. */ 22204296e2cbSAl Viro if (open_flag & O_APPEND) 22211da177e4SLinus Torvalds acc_mode |= MAY_APPEND; 22221da177e4SLinus Torvalds 222331e6b01fSNick Piggin flags = LOOKUP_OPEN; 222431e6b01fSNick Piggin if (open_flag & O_CREAT) { 222531e6b01fSNick Piggin flags |= LOOKUP_CREATE; 222631e6b01fSNick Piggin if (open_flag & O_EXCL) 222731e6b01fSNick Piggin flags |= LOOKUP_EXCL; 2228654f562cSJ. R. Okajima } 222931e6b01fSNick Piggin if (open_flag & O_DIRECTORY) 223031e6b01fSNick Piggin flags |= LOOKUP_DIRECTORY; 223131e6b01fSNick Piggin if (!(open_flag & O_NOFOLLOW)) 223231e6b01fSNick Piggin flags |= LOOKUP_FOLLOW; 223331e6b01fSNick Piggin 223431e6b01fSNick Piggin filp = get_empty_filp(); 223531e6b01fSNick Piggin if (!filp) 223631e6b01fSNick Piggin return ERR_PTR(-ENFILE); 223731e6b01fSNick Piggin 223831e6b01fSNick Piggin filp->f_flags = open_flag; 223931e6b01fSNick Piggin nd.intent.open.file = filp; 224031e6b01fSNick Piggin nd.intent.open.flags = flag; 224131e6b01fSNick Piggin nd.intent.open.create_mode = mode; 224231e6b01fSNick Piggin 224331e6b01fSNick Piggin if (open_flag & O_CREAT) 224431e6b01fSNick Piggin goto creat; 224531e6b01fSNick Piggin 224631e6b01fSNick Piggin /* !O_CREAT, simple open */ 224731e6b01fSNick Piggin error = do_path_lookup(dfd, pathname, flags, &nd); 224831e6b01fSNick Piggin if (unlikely(error)) 224931e6b01fSNick Piggin goto out_filp; 225031e6b01fSNick Piggin error = -ELOOP; 225131e6b01fSNick Piggin if (!(nd.flags & LOOKUP_FOLLOW)) { 225231e6b01fSNick Piggin if (nd.inode->i_op->follow_link) 225331e6b01fSNick Piggin goto out_path; 225431e6b01fSNick Piggin } 225531e6b01fSNick Piggin error = -ENOTDIR; 225631e6b01fSNick Piggin if (nd.flags & LOOKUP_DIRECTORY) { 225731e6b01fSNick Piggin if (!nd.inode->i_op->lookup) 225831e6b01fSNick Piggin goto out_path; 225931e6b01fSNick Piggin } 226031e6b01fSNick Piggin audit_inode(pathname, nd.path.dentry); 226131e6b01fSNick Piggin filp = finish_open(&nd, open_flag, acc_mode); 226231e6b01fSNick Piggin return filp; 226331e6b01fSNick Piggin 226431e6b01fSNick Piggin creat: 226531e6b01fSNick Piggin /* OK, have to create the file. Find the parent. */ 226631e6b01fSNick Piggin error = path_init_rcu(dfd, pathname, 226731e6b01fSNick Piggin LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd); 226831e6b01fSNick Piggin if (error) 226931e6b01fSNick Piggin goto out_filp; 227031e6b01fSNick Piggin error = path_walk_rcu(pathname, &nd); 227131e6b01fSNick Piggin path_finish_rcu(&nd); 227231e6b01fSNick Piggin if (unlikely(error == -ECHILD || error == -ESTALE)) { 227331e6b01fSNick Piggin /* slower, locked walk */ 227431e6b01fSNick Piggin if (error == -ESTALE) { 227531e6b01fSNick Piggin reval: 227631e6b01fSNick Piggin flags |= LOOKUP_REVAL; 227731e6b01fSNick Piggin } 227831e6b01fSNick Piggin error = path_init(dfd, pathname, 227931e6b01fSNick Piggin LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd); 228031e6b01fSNick Piggin if (error) 228131e6b01fSNick Piggin goto out_filp; 228231e6b01fSNick Piggin 228331e6b01fSNick Piggin error = path_walk_simple(pathname, &nd); 228431e6b01fSNick Piggin } 228531e6b01fSNick Piggin if (unlikely(error)) 228631e6b01fSNick Piggin goto out_filp; 228731e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) 22889b4a9b14SAl Viro audit_inode(pathname, nd.path.dentry); 22891da177e4SLinus Torvalds 22901da177e4SLinus Torvalds /* 2291a2c36b45SAl Viro * We have the parent and last component. 22921da177e4SLinus Torvalds */ 229331e6b01fSNick Piggin nd.flags = flags; 22943e297b61SAl Viro filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname); 2295806b681cSAl Viro while (unlikely(!filp)) { /* trailing symlink */ 2296def4af30SAl Viro struct path holder; 2297def4af30SAl Viro void *cookie; 2298806b681cSAl Viro error = -ELOOP; 22991f36f774SAl Viro /* S_ISDIR part is a temporary automount kludge */ 230031e6b01fSNick Piggin if (!(nd.flags & LOOKUP_FOLLOW) && !S_ISDIR(nd.inode->i_mode)) 23011f36f774SAl Viro goto exit_dput; 23021f36f774SAl Viro if (count++ == 32) 2303806b681cSAl Viro goto exit_dput; 2304806b681cSAl Viro /* 2305806b681cSAl Viro * This is subtle. Instead of calling do_follow_link() we do 2306806b681cSAl Viro * the thing by hands. The reason is that this way we have zero 2307806b681cSAl Viro * link_count and path_walk() (called from ->follow_link) 2308806b681cSAl Viro * honoring LOOKUP_PARENT. After that we have the parent and 2309806b681cSAl Viro * last component, i.e. we are in the same situation as after 2310806b681cSAl Viro * the first path_walk(). Well, almost - if the last component 2311806b681cSAl Viro * is normal we get its copy stored in nd->last.name and we will 2312806b681cSAl Viro * have to putname() it when we are done. Procfs-like symlinks 2313806b681cSAl Viro * just set LAST_BIND. 2314806b681cSAl Viro */ 2315806b681cSAl Viro nd.flags |= LOOKUP_PARENT; 2316806b681cSAl Viro error = security_inode_follow_link(path.dentry, &nd); 2317806b681cSAl Viro if (error) 2318806b681cSAl Viro goto exit_dput; 2319def4af30SAl Viro error = __do_follow_link(&path, &nd, &cookie); 2320def4af30SAl Viro if (unlikely(error)) { 232131e6b01fSNick Piggin if (!IS_ERR(cookie) && nd.inode->i_op->put_link) 232231e6b01fSNick Piggin nd.inode->i_op->put_link(path.dentry, &nd, cookie); 2323806b681cSAl Viro /* nd.path had been dropped */ 232431e6b01fSNick Piggin nd.path = path; 232531e6b01fSNick Piggin goto out_path; 2326806b681cSAl Viro } 2327def4af30SAl Viro holder = path; 2328806b681cSAl Viro nd.flags &= ~LOOKUP_PARENT; 23293e297b61SAl Viro filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname); 233031e6b01fSNick Piggin if (nd.inode->i_op->put_link) 233131e6b01fSNick Piggin nd.inode->i_op->put_link(holder.dentry, &nd, cookie); 2332def4af30SAl Viro path_put(&holder); 2333806b681cSAl Viro } 233410fa8e62SAl Viro out: 23352a737871SAl Viro if (nd.root.mnt) 23362a737871SAl Viro path_put(&nd.root); 233731e6b01fSNick Piggin if (filp == ERR_PTR(-ESTALE) && !(flags & LOOKUP_REVAL)) 233810fa8e62SAl Viro goto reval; 233910fa8e62SAl Viro return filp; 23401da177e4SLinus Torvalds 2341806b681cSAl Viro exit_dput: 2342806b681cSAl Viro path_put_conditional(&path, &nd); 234331e6b01fSNick Piggin out_path: 234431e6b01fSNick Piggin path_put(&nd.path); 234531e6b01fSNick Piggin out_filp: 2346806b681cSAl Viro if (!IS_ERR(nd.intent.open.file)) 2347a70e65dfSChristoph Hellwig release_open_intent(&nd); 234810fa8e62SAl Viro filp = ERR_PTR(error); 234910fa8e62SAl Viro goto out; 2350de459215SKirill Korotaev } 23511da177e4SLinus Torvalds 23521da177e4SLinus Torvalds /** 2353a70e65dfSChristoph Hellwig * filp_open - open file and return file pointer 2354a70e65dfSChristoph Hellwig * 2355a70e65dfSChristoph Hellwig * @filename: path to open 2356a70e65dfSChristoph Hellwig * @flags: open flags as per the open(2) second argument 2357a70e65dfSChristoph Hellwig * @mode: mode for the new file if O_CREAT is set, else ignored 2358a70e65dfSChristoph Hellwig * 2359a70e65dfSChristoph Hellwig * This is the helper to open a file from kernelspace if you really 2360a70e65dfSChristoph Hellwig * have to. But in generally you should not do this, so please move 2361a70e65dfSChristoph Hellwig * along, nothing to see here.. 2362a70e65dfSChristoph Hellwig */ 2363a70e65dfSChristoph Hellwig struct file *filp_open(const char *filename, int flags, int mode) 2364a70e65dfSChristoph Hellwig { 23656e8341a1SAl Viro return do_filp_open(AT_FDCWD, filename, flags, mode, 0); 2366a70e65dfSChristoph Hellwig } 2367a70e65dfSChristoph Hellwig EXPORT_SYMBOL(filp_open); 2368a70e65dfSChristoph Hellwig 2369a70e65dfSChristoph Hellwig /** 23701da177e4SLinus Torvalds * lookup_create - lookup a dentry, creating it if it doesn't exist 23711da177e4SLinus Torvalds * @nd: nameidata info 23721da177e4SLinus Torvalds * @is_dir: directory flag 23731da177e4SLinus Torvalds * 23741da177e4SLinus Torvalds * Simple function to lookup and return a dentry and create it 23751da177e4SLinus Torvalds * if it doesn't exist. Is SMP-safe. 2376c663e5d8SChristoph Hellwig * 23774ac91378SJan Blunck * Returns with nd->path.dentry->d_inode->i_mutex locked. 23781da177e4SLinus Torvalds */ 23791da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir) 23801da177e4SLinus Torvalds { 2381c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 23821da177e4SLinus Torvalds 23834ac91378SJan Blunck mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 2384c663e5d8SChristoph Hellwig /* 2385c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 2386c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 2387c663e5d8SChristoph Hellwig */ 23881da177e4SLinus Torvalds if (nd->last_type != LAST_NORM) 23891da177e4SLinus Torvalds goto fail; 23901da177e4SLinus Torvalds nd->flags &= ~LOOKUP_PARENT; 23913516586aSAl Viro nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL; 2392a634904aSASANO Masahiro nd->intent.open.flags = O_EXCL; 2393c663e5d8SChristoph Hellwig 2394c663e5d8SChristoph Hellwig /* 2395c663e5d8SChristoph Hellwig * Do the final lookup. 2396c663e5d8SChristoph Hellwig */ 239749705b77SChristoph Hellwig dentry = lookup_hash(nd); 23981da177e4SLinus Torvalds if (IS_ERR(dentry)) 23991da177e4SLinus Torvalds goto fail; 2400c663e5d8SChristoph Hellwig 2401e9baf6e5SAl Viro if (dentry->d_inode) 2402e9baf6e5SAl Viro goto eexist; 2403c663e5d8SChristoph Hellwig /* 2404c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 2405c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 2406c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 2407c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 2408c663e5d8SChristoph Hellwig */ 2409e9baf6e5SAl Viro if (unlikely(!is_dir && nd->last.name[nd->last.len])) { 24101da177e4SLinus Torvalds dput(dentry); 24111da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 2412e9baf6e5SAl Viro } 2413e9baf6e5SAl Viro return dentry; 2414e9baf6e5SAl Viro eexist: 2415e9baf6e5SAl Viro dput(dentry); 2416e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 24171da177e4SLinus Torvalds fail: 24181da177e4SLinus Torvalds return dentry; 24191da177e4SLinus Torvalds } 2420f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create); 24211da177e4SLinus Torvalds 24221da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 24231da177e4SLinus Torvalds { 2424a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 24251da177e4SLinus Torvalds 24261da177e4SLinus Torvalds if (error) 24271da177e4SLinus Torvalds return error; 24281da177e4SLinus Torvalds 24291da177e4SLinus Torvalds if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) 24301da177e4SLinus Torvalds return -EPERM; 24311da177e4SLinus Torvalds 2432acfa4380SAl Viro if (!dir->i_op->mknod) 24331da177e4SLinus Torvalds return -EPERM; 24341da177e4SLinus Torvalds 243508ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 243608ce5f16SSerge E. Hallyn if (error) 243708ce5f16SSerge E. Hallyn return error; 243808ce5f16SSerge E. Hallyn 24391da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 24401da177e4SLinus Torvalds if (error) 24411da177e4SLinus Torvalds return error; 24421da177e4SLinus Torvalds 24431da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 2444a74574aaSStephen Smalley if (!error) 2445f38aa942SAmy Griffis fsnotify_create(dir, dentry); 24461da177e4SLinus Torvalds return error; 24471da177e4SLinus Torvalds } 24481da177e4SLinus Torvalds 2449463c3197SDave Hansen static int may_mknod(mode_t mode) 2450463c3197SDave Hansen { 2451463c3197SDave Hansen switch (mode & S_IFMT) { 2452463c3197SDave Hansen case S_IFREG: 2453463c3197SDave Hansen case S_IFCHR: 2454463c3197SDave Hansen case S_IFBLK: 2455463c3197SDave Hansen case S_IFIFO: 2456463c3197SDave Hansen case S_IFSOCK: 2457463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2458463c3197SDave Hansen return 0; 2459463c3197SDave Hansen case S_IFDIR: 2460463c3197SDave Hansen return -EPERM; 2461463c3197SDave Hansen default: 2462463c3197SDave Hansen return -EINVAL; 2463463c3197SDave Hansen } 2464463c3197SDave Hansen } 2465463c3197SDave Hansen 24662e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode, 24672e4d0924SHeiko Carstens unsigned, dev) 24681da177e4SLinus Torvalds { 24692ad94ae6SAl Viro int error; 24701da177e4SLinus Torvalds char *tmp; 24711da177e4SLinus Torvalds struct dentry *dentry; 24721da177e4SLinus Torvalds struct nameidata nd; 24731da177e4SLinus Torvalds 24741da177e4SLinus Torvalds if (S_ISDIR(mode)) 24751da177e4SLinus Torvalds return -EPERM; 24761da177e4SLinus Torvalds 24772ad94ae6SAl Viro error = user_path_parent(dfd, filename, &nd, &tmp); 24781da177e4SLinus Torvalds if (error) 24792ad94ae6SAl Viro return error; 24802ad94ae6SAl Viro 24811da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 2482463c3197SDave Hansen if (IS_ERR(dentry)) { 24831da177e4SLinus Torvalds error = PTR_ERR(dentry); 2484463c3197SDave Hansen goto out_unlock; 2485463c3197SDave Hansen } 24864ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2487ce3b0f8dSAl Viro mode &= ~current_umask(); 2488463c3197SDave Hansen error = may_mknod(mode); 2489463c3197SDave Hansen if (error) 2490463c3197SDave Hansen goto out_dput; 2491463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2492463c3197SDave Hansen if (error) 2493463c3197SDave Hansen goto out_dput; 2494be6d3e56SKentaro Takeda error = security_path_mknod(&nd.path, dentry, mode, dev); 2495be6d3e56SKentaro Takeda if (error) 2496be6d3e56SKentaro Takeda goto out_drop_write; 24971da177e4SLinus Torvalds switch (mode & S_IFMT) { 24981da177e4SLinus Torvalds case 0: case S_IFREG: 24994ac91378SJan Blunck error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd); 25001da177e4SLinus Torvalds break; 25011da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 25024ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode, 25031da177e4SLinus Torvalds new_decode_dev(dev)); 25041da177e4SLinus Torvalds break; 25051da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 25064ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0); 25071da177e4SLinus Torvalds break; 25081da177e4SLinus Torvalds } 2509be6d3e56SKentaro Takeda out_drop_write: 2510463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2511463c3197SDave Hansen out_dput: 25121da177e4SLinus Torvalds dput(dentry); 2513463c3197SDave Hansen out_unlock: 25144ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 25151d957f9bSJan Blunck path_put(&nd.path); 25161da177e4SLinus Torvalds putname(tmp); 25171da177e4SLinus Torvalds 25181da177e4SLinus Torvalds return error; 25191da177e4SLinus Torvalds } 25201da177e4SLinus Torvalds 25213480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev) 25225590ff0dSUlrich Drepper { 25235590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 25245590ff0dSUlrich Drepper } 25255590ff0dSUlrich Drepper 25261da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 25271da177e4SLinus Torvalds { 2528a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 25291da177e4SLinus Torvalds 25301da177e4SLinus Torvalds if (error) 25311da177e4SLinus Torvalds return error; 25321da177e4SLinus Torvalds 2533acfa4380SAl Viro if (!dir->i_op->mkdir) 25341da177e4SLinus Torvalds return -EPERM; 25351da177e4SLinus Torvalds 25361da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 25371da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 25381da177e4SLinus Torvalds if (error) 25391da177e4SLinus Torvalds return error; 25401da177e4SLinus Torvalds 25411da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2542a74574aaSStephen Smalley if (!error) 2543f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 25441da177e4SLinus Torvalds return error; 25451da177e4SLinus Torvalds } 25461da177e4SLinus Torvalds 25472e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode) 25481da177e4SLinus Torvalds { 25491da177e4SLinus Torvalds int error = 0; 25501da177e4SLinus Torvalds char * tmp; 25516902d925SDave Hansen struct dentry *dentry; 25526902d925SDave Hansen struct nameidata nd; 25531da177e4SLinus Torvalds 25542ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &tmp); 25552ad94ae6SAl Viro if (error) 25566902d925SDave Hansen goto out_err; 25571da177e4SLinus Torvalds 25581da177e4SLinus Torvalds dentry = lookup_create(&nd, 1); 25591da177e4SLinus Torvalds error = PTR_ERR(dentry); 25606902d925SDave Hansen if (IS_ERR(dentry)) 25616902d925SDave Hansen goto out_unlock; 25626902d925SDave Hansen 25634ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2564ce3b0f8dSAl Viro mode &= ~current_umask(); 2565463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2566463c3197SDave Hansen if (error) 2567463c3197SDave Hansen goto out_dput; 2568be6d3e56SKentaro Takeda error = security_path_mkdir(&nd.path, dentry, mode); 2569be6d3e56SKentaro Takeda if (error) 2570be6d3e56SKentaro Takeda goto out_drop_write; 25714ac91378SJan Blunck error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode); 2572be6d3e56SKentaro Takeda out_drop_write: 2573463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2574463c3197SDave Hansen out_dput: 25751da177e4SLinus Torvalds dput(dentry); 25766902d925SDave Hansen out_unlock: 25774ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 25781d957f9bSJan Blunck path_put(&nd.path); 25791da177e4SLinus Torvalds putname(tmp); 25806902d925SDave Hansen out_err: 25811da177e4SLinus Torvalds return error; 25821da177e4SLinus Torvalds } 25831da177e4SLinus Torvalds 25843cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode) 25855590ff0dSUlrich Drepper { 25865590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 25875590ff0dSUlrich Drepper } 25885590ff0dSUlrich Drepper 25891da177e4SLinus Torvalds /* 25901da177e4SLinus Torvalds * We try to drop the dentry early: we should have 25911da177e4SLinus Torvalds * a usage count of 2 if we're the only user of this 25921da177e4SLinus Torvalds * dentry, and if that is true (possibly after pruning 25931da177e4SLinus Torvalds * the dcache), then we drop the dentry now. 25941da177e4SLinus Torvalds * 25951da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 25961da177e4SLinus Torvalds * do a 25971da177e4SLinus Torvalds * 25981da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 25991da177e4SLinus Torvalds * return -EBUSY; 26001da177e4SLinus Torvalds * 26011da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 26021da177e4SLinus Torvalds * that is still in use by something else.. 26031da177e4SLinus Torvalds */ 26041da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 26051da177e4SLinus Torvalds { 26061da177e4SLinus Torvalds dget(dentry); 26071da177e4SLinus Torvalds shrink_dcache_parent(dentry); 26081da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 2609b7ab39f6SNick Piggin if (dentry->d_count == 2) 26101da177e4SLinus Torvalds __d_drop(dentry); 26111da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 26121da177e4SLinus Torvalds } 26131da177e4SLinus Torvalds 26141da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 26151da177e4SLinus Torvalds { 26161da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 26171da177e4SLinus Torvalds 26181da177e4SLinus Torvalds if (error) 26191da177e4SLinus Torvalds return error; 26201da177e4SLinus Torvalds 2621acfa4380SAl Viro if (!dir->i_op->rmdir) 26221da177e4SLinus Torvalds return -EPERM; 26231da177e4SLinus Torvalds 26241b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 26251da177e4SLinus Torvalds dentry_unhash(dentry); 26261da177e4SLinus Torvalds if (d_mountpoint(dentry)) 26271da177e4SLinus Torvalds error = -EBUSY; 26281da177e4SLinus Torvalds else { 26291da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 26301da177e4SLinus Torvalds if (!error) { 26311da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 2632d83c49f3SAl Viro if (!error) { 26331da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 2634d83c49f3SAl Viro dont_mount(dentry); 2635d83c49f3SAl Viro } 26361da177e4SLinus Torvalds } 26371da177e4SLinus Torvalds } 26381b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 26391da177e4SLinus Torvalds if (!error) { 26401da177e4SLinus Torvalds d_delete(dentry); 26411da177e4SLinus Torvalds } 26421da177e4SLinus Torvalds dput(dentry); 26431da177e4SLinus Torvalds 26441da177e4SLinus Torvalds return error; 26451da177e4SLinus Torvalds } 26461da177e4SLinus Torvalds 26475590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 26481da177e4SLinus Torvalds { 26491da177e4SLinus Torvalds int error = 0; 26501da177e4SLinus Torvalds char * name; 26511da177e4SLinus Torvalds struct dentry *dentry; 26521da177e4SLinus Torvalds struct nameidata nd; 26531da177e4SLinus Torvalds 26542ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 26551da177e4SLinus Torvalds if (error) 26562ad94ae6SAl Viro return error; 26571da177e4SLinus Torvalds 26581da177e4SLinus Torvalds switch(nd.last_type) { 26591da177e4SLinus Torvalds case LAST_DOTDOT: 26601da177e4SLinus Torvalds error = -ENOTEMPTY; 26611da177e4SLinus Torvalds goto exit1; 26621da177e4SLinus Torvalds case LAST_DOT: 26631da177e4SLinus Torvalds error = -EINVAL; 26641da177e4SLinus Torvalds goto exit1; 26651da177e4SLinus Torvalds case LAST_ROOT: 26661da177e4SLinus Torvalds error = -EBUSY; 26671da177e4SLinus Torvalds goto exit1; 26681da177e4SLinus Torvalds } 26690612d9fbSOGAWA Hirofumi 26700612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 26710612d9fbSOGAWA Hirofumi 26724ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 267349705b77SChristoph Hellwig dentry = lookup_hash(&nd); 26741da177e4SLinus Torvalds error = PTR_ERR(dentry); 26756902d925SDave Hansen if (IS_ERR(dentry)) 26766902d925SDave Hansen goto exit2; 26770622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 26780622753bSDave Hansen if (error) 26790622753bSDave Hansen goto exit3; 2680be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 2681be6d3e56SKentaro Takeda if (error) 2682be6d3e56SKentaro Takeda goto exit4; 26834ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 2684be6d3e56SKentaro Takeda exit4: 26850622753bSDave Hansen mnt_drop_write(nd.path.mnt); 26860622753bSDave Hansen exit3: 26871da177e4SLinus Torvalds dput(dentry); 26886902d925SDave Hansen exit2: 26894ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 26901da177e4SLinus Torvalds exit1: 26911d957f9bSJan Blunck path_put(&nd.path); 26921da177e4SLinus Torvalds putname(name); 26931da177e4SLinus Torvalds return error; 26941da177e4SLinus Torvalds } 26951da177e4SLinus Torvalds 26963cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 26975590ff0dSUlrich Drepper { 26985590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 26995590ff0dSUlrich Drepper } 27005590ff0dSUlrich Drepper 27011da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 27021da177e4SLinus Torvalds { 27031da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 27041da177e4SLinus Torvalds 27051da177e4SLinus Torvalds if (error) 27061da177e4SLinus Torvalds return error; 27071da177e4SLinus Torvalds 2708acfa4380SAl Viro if (!dir->i_op->unlink) 27091da177e4SLinus Torvalds return -EPERM; 27101da177e4SLinus Torvalds 27111b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 27121da177e4SLinus Torvalds if (d_mountpoint(dentry)) 27131da177e4SLinus Torvalds error = -EBUSY; 27141da177e4SLinus Torvalds else { 27151da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 2716bec1052eSAl Viro if (!error) { 27171da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 2718bec1052eSAl Viro if (!error) 2719d83c49f3SAl Viro dont_mount(dentry); 2720bec1052eSAl Viro } 27211da177e4SLinus Torvalds } 27221b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 27231da177e4SLinus Torvalds 27241da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 27251da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 2726ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 27271da177e4SLinus Torvalds d_delete(dentry); 27281da177e4SLinus Torvalds } 27290eeca283SRobert Love 27301da177e4SLinus Torvalds return error; 27311da177e4SLinus Torvalds } 27321da177e4SLinus Torvalds 27331da177e4SLinus Torvalds /* 27341da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 27351b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 27361da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 27371da177e4SLinus Torvalds * while waiting on the I/O. 27381da177e4SLinus Torvalds */ 27395590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 27401da177e4SLinus Torvalds { 27412ad94ae6SAl Viro int error; 27421da177e4SLinus Torvalds char *name; 27431da177e4SLinus Torvalds struct dentry *dentry; 27441da177e4SLinus Torvalds struct nameidata nd; 27451da177e4SLinus Torvalds struct inode *inode = NULL; 27461da177e4SLinus Torvalds 27472ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 27481da177e4SLinus Torvalds if (error) 27492ad94ae6SAl Viro return error; 27502ad94ae6SAl Viro 27511da177e4SLinus Torvalds error = -EISDIR; 27521da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 27531da177e4SLinus Torvalds goto exit1; 27540612d9fbSOGAWA Hirofumi 27550612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 27560612d9fbSOGAWA Hirofumi 27574ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 275849705b77SChristoph Hellwig dentry = lookup_hash(&nd); 27591da177e4SLinus Torvalds error = PTR_ERR(dentry); 27601da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 27611da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 27621da177e4SLinus Torvalds if (nd.last.name[nd.last.len]) 27631da177e4SLinus Torvalds goto slashes; 27641da177e4SLinus Torvalds inode = dentry->d_inode; 27651da177e4SLinus Torvalds if (inode) 27667de9c6eeSAl Viro ihold(inode); 27670622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 27680622753bSDave Hansen if (error) 27690622753bSDave Hansen goto exit2; 2770be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 2771be6d3e56SKentaro Takeda if (error) 2772be6d3e56SKentaro Takeda goto exit3; 27734ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 2774be6d3e56SKentaro Takeda exit3: 27750622753bSDave Hansen mnt_drop_write(nd.path.mnt); 27761da177e4SLinus Torvalds exit2: 27771da177e4SLinus Torvalds dput(dentry); 27781da177e4SLinus Torvalds } 27794ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 27801da177e4SLinus Torvalds if (inode) 27811da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 27821da177e4SLinus Torvalds exit1: 27831d957f9bSJan Blunck path_put(&nd.path); 27841da177e4SLinus Torvalds putname(name); 27851da177e4SLinus Torvalds return error; 27861da177e4SLinus Torvalds 27871da177e4SLinus Torvalds slashes: 27881da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 27891da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 27901da177e4SLinus Torvalds goto exit2; 27911da177e4SLinus Torvalds } 27921da177e4SLinus Torvalds 27932e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 27945590ff0dSUlrich Drepper { 27955590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 27965590ff0dSUlrich Drepper return -EINVAL; 27975590ff0dSUlrich Drepper 27985590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 27995590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 28005590ff0dSUlrich Drepper 28015590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 28025590ff0dSUlrich Drepper } 28035590ff0dSUlrich Drepper 28043480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 28055590ff0dSUlrich Drepper { 28065590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 28075590ff0dSUlrich Drepper } 28085590ff0dSUlrich Drepper 2809db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 28101da177e4SLinus Torvalds { 2811a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 28121da177e4SLinus Torvalds 28131da177e4SLinus Torvalds if (error) 28141da177e4SLinus Torvalds return error; 28151da177e4SLinus Torvalds 2816acfa4380SAl Viro if (!dir->i_op->symlink) 28171da177e4SLinus Torvalds return -EPERM; 28181da177e4SLinus Torvalds 28191da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 28201da177e4SLinus Torvalds if (error) 28211da177e4SLinus Torvalds return error; 28221da177e4SLinus Torvalds 28231da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 2824a74574aaSStephen Smalley if (!error) 2825f38aa942SAmy Griffis fsnotify_create(dir, dentry); 28261da177e4SLinus Torvalds return error; 28271da177e4SLinus Torvalds } 28281da177e4SLinus Torvalds 28292e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 28302e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 28311da177e4SLinus Torvalds { 28322ad94ae6SAl Viro int error; 28331da177e4SLinus Torvalds char *from; 28341da177e4SLinus Torvalds char *to; 28356902d925SDave Hansen struct dentry *dentry; 28366902d925SDave Hansen struct nameidata nd; 28371da177e4SLinus Torvalds 28381da177e4SLinus Torvalds from = getname(oldname); 28391da177e4SLinus Torvalds if (IS_ERR(from)) 28401da177e4SLinus Torvalds return PTR_ERR(from); 28412ad94ae6SAl Viro 28422ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 28432ad94ae6SAl Viro if (error) 28446902d925SDave Hansen goto out_putname; 28451da177e4SLinus Torvalds 28461da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 28471da177e4SLinus Torvalds error = PTR_ERR(dentry); 28486902d925SDave Hansen if (IS_ERR(dentry)) 28496902d925SDave Hansen goto out_unlock; 28506902d925SDave Hansen 285175c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 285275c3f29dSDave Hansen if (error) 285375c3f29dSDave Hansen goto out_dput; 2854be6d3e56SKentaro Takeda error = security_path_symlink(&nd.path, dentry, from); 2855be6d3e56SKentaro Takeda if (error) 2856be6d3e56SKentaro Takeda goto out_drop_write; 2857db2e747bSMiklos Szeredi error = vfs_symlink(nd.path.dentry->d_inode, dentry, from); 2858be6d3e56SKentaro Takeda out_drop_write: 285975c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 286075c3f29dSDave Hansen out_dput: 28611da177e4SLinus Torvalds dput(dentry); 28626902d925SDave Hansen out_unlock: 28634ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 28641d957f9bSJan Blunck path_put(&nd.path); 28651da177e4SLinus Torvalds putname(to); 28666902d925SDave Hansen out_putname: 28671da177e4SLinus Torvalds putname(from); 28681da177e4SLinus Torvalds return error; 28691da177e4SLinus Torvalds } 28701da177e4SLinus Torvalds 28713480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 28725590ff0dSUlrich Drepper { 28735590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 28745590ff0dSUlrich Drepper } 28755590ff0dSUlrich Drepper 28761da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 28771da177e4SLinus Torvalds { 28781da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 28791da177e4SLinus Torvalds int error; 28801da177e4SLinus Torvalds 28811da177e4SLinus Torvalds if (!inode) 28821da177e4SLinus Torvalds return -ENOENT; 28831da177e4SLinus Torvalds 2884a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 28851da177e4SLinus Torvalds if (error) 28861da177e4SLinus Torvalds return error; 28871da177e4SLinus Torvalds 28881da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 28891da177e4SLinus Torvalds return -EXDEV; 28901da177e4SLinus Torvalds 28911da177e4SLinus Torvalds /* 28921da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 28931da177e4SLinus Torvalds */ 28941da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 28951da177e4SLinus Torvalds return -EPERM; 2896acfa4380SAl Viro if (!dir->i_op->link) 28971da177e4SLinus Torvalds return -EPERM; 28987e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 28991da177e4SLinus Torvalds return -EPERM; 29001da177e4SLinus Torvalds 29011da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 29021da177e4SLinus Torvalds if (error) 29031da177e4SLinus Torvalds return error; 29041da177e4SLinus Torvalds 29057e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 29061da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 29077e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 2908e31e14ecSStephen Smalley if (!error) 29097e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 29101da177e4SLinus Torvalds return error; 29111da177e4SLinus Torvalds } 29121da177e4SLinus Torvalds 29131da177e4SLinus Torvalds /* 29141da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 29151da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 29161da177e4SLinus Torvalds * newname. --KAB 29171da177e4SLinus Torvalds * 29181da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 29191da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 29201da177e4SLinus Torvalds * and other special files. --ADM 29211da177e4SLinus Torvalds */ 29222e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 29232e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 29241da177e4SLinus Torvalds { 29251da177e4SLinus Torvalds struct dentry *new_dentry; 29262d8f3038SAl Viro struct nameidata nd; 29272d8f3038SAl Viro struct path old_path; 29281da177e4SLinus Torvalds int error; 29291da177e4SLinus Torvalds char *to; 29301da177e4SLinus Torvalds 293145c9b11aSUlrich Drepper if ((flags & ~AT_SYMLINK_FOLLOW) != 0) 2932c04030e1SUlrich Drepper return -EINVAL; 2933c04030e1SUlrich Drepper 29342d8f3038SAl Viro error = user_path_at(olddfd, oldname, 293545c9b11aSUlrich Drepper flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0, 29362d8f3038SAl Viro &old_path); 29371da177e4SLinus Torvalds if (error) 29382ad94ae6SAl Viro return error; 29392ad94ae6SAl Viro 29402ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 29411da177e4SLinus Torvalds if (error) 29421da177e4SLinus Torvalds goto out; 29431da177e4SLinus Torvalds error = -EXDEV; 29442d8f3038SAl Viro if (old_path.mnt != nd.path.mnt) 29451da177e4SLinus Torvalds goto out_release; 29461da177e4SLinus Torvalds new_dentry = lookup_create(&nd, 0); 29471da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 29486902d925SDave Hansen if (IS_ERR(new_dentry)) 29496902d925SDave Hansen goto out_unlock; 295075c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 295175c3f29dSDave Hansen if (error) 295275c3f29dSDave Hansen goto out_dput; 2953be6d3e56SKentaro Takeda error = security_path_link(old_path.dentry, &nd.path, new_dentry); 2954be6d3e56SKentaro Takeda if (error) 2955be6d3e56SKentaro Takeda goto out_drop_write; 29562d8f3038SAl Viro error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry); 2957be6d3e56SKentaro Takeda out_drop_write: 295875c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 295975c3f29dSDave Hansen out_dput: 29601da177e4SLinus Torvalds dput(new_dentry); 29616902d925SDave Hansen out_unlock: 29624ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 29631da177e4SLinus Torvalds out_release: 29641d957f9bSJan Blunck path_put(&nd.path); 29652ad94ae6SAl Viro putname(to); 29661da177e4SLinus Torvalds out: 29672d8f3038SAl Viro path_put(&old_path); 29681da177e4SLinus Torvalds 29691da177e4SLinus Torvalds return error; 29701da177e4SLinus Torvalds } 29711da177e4SLinus Torvalds 29723480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 29735590ff0dSUlrich Drepper { 2974c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 29755590ff0dSUlrich Drepper } 29765590ff0dSUlrich Drepper 29771da177e4SLinus Torvalds /* 29781da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 29791da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 29801da177e4SLinus Torvalds * Problems: 29811da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 29821da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 29831da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 2984a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 29851da177e4SLinus Torvalds * story. 29861da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 29871b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 29881da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 29891da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 2990a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 29911da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 29921da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 29931da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 2994a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 29951da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 29961da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 29971da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 29981da177e4SLinus Torvalds * d) some filesystems don't support opened-but-unlinked directories, 29991da177e4SLinus Torvalds * either because of layout or because they are not ready to deal with 30001da177e4SLinus Torvalds * all cases correctly. The latter will be fixed (taking this sort of 30011da177e4SLinus Torvalds * stuff into VFS), but the former is not going away. Solution: the same 30021da177e4SLinus Torvalds * trick as in rmdir(). 30031da177e4SLinus Torvalds * e) conversion from fhandle to dentry may come in the wrong moment - when 30041b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 30051da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 3006c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 30071da177e4SLinus Torvalds * locking]. 30081da177e4SLinus Torvalds */ 300975c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 30101da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30111da177e4SLinus Torvalds { 30121da177e4SLinus Torvalds int error = 0; 30131da177e4SLinus Torvalds struct inode *target; 30141da177e4SLinus Torvalds 30151da177e4SLinus Torvalds /* 30161da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 30171da177e4SLinus Torvalds * we'll need to flip '..'. 30181da177e4SLinus Torvalds */ 30191da177e4SLinus Torvalds if (new_dir != old_dir) { 3020f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 30211da177e4SLinus Torvalds if (error) 30221da177e4SLinus Torvalds return error; 30231da177e4SLinus Torvalds } 30241da177e4SLinus Torvalds 30251da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 30261da177e4SLinus Torvalds if (error) 30271da177e4SLinus Torvalds return error; 30281da177e4SLinus Torvalds 30291da177e4SLinus Torvalds target = new_dentry->d_inode; 3030d83c49f3SAl Viro if (target) 30311b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 30321da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 30331da177e4SLinus Torvalds error = -EBUSY; 3034d83c49f3SAl Viro else { 3035d83c49f3SAl Viro if (target) 3036d83c49f3SAl Viro dentry_unhash(new_dentry); 30371da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 3038d83c49f3SAl Viro } 30391da177e4SLinus Torvalds if (target) { 3040d83c49f3SAl Viro if (!error) { 30411da177e4SLinus Torvalds target->i_flags |= S_DEAD; 3042d83c49f3SAl Viro dont_mount(new_dentry); 3043d83c49f3SAl Viro } 30441b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 30451da177e4SLinus Torvalds if (d_unhashed(new_dentry)) 30461da177e4SLinus Torvalds d_rehash(new_dentry); 30471da177e4SLinus Torvalds dput(new_dentry); 30481da177e4SLinus Torvalds } 3049e31e14ecSStephen Smalley if (!error) 3050349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 30511da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 30521da177e4SLinus Torvalds return error; 30531da177e4SLinus Torvalds } 30541da177e4SLinus Torvalds 305575c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 30561da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30571da177e4SLinus Torvalds { 30581da177e4SLinus Torvalds struct inode *target; 30591da177e4SLinus Torvalds int error; 30601da177e4SLinus Torvalds 30611da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 30621da177e4SLinus Torvalds if (error) 30631da177e4SLinus Torvalds return error; 30641da177e4SLinus Torvalds 30651da177e4SLinus Torvalds dget(new_dentry); 30661da177e4SLinus Torvalds target = new_dentry->d_inode; 30671da177e4SLinus Torvalds if (target) 30681b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 30691da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 30701da177e4SLinus Torvalds error = -EBUSY; 30711da177e4SLinus Torvalds else 30721da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 30731da177e4SLinus Torvalds if (!error) { 3074bec1052eSAl Viro if (target) 3075d83c49f3SAl Viro dont_mount(new_dentry); 3076349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 30771da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 30781da177e4SLinus Torvalds } 30791da177e4SLinus Torvalds if (target) 30801b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 30811da177e4SLinus Torvalds dput(new_dentry); 30821da177e4SLinus Torvalds return error; 30831da177e4SLinus Torvalds } 30841da177e4SLinus Torvalds 30851da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 30861da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30871da177e4SLinus Torvalds { 30881da177e4SLinus Torvalds int error; 30891da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 309059b0df21SEric Paris const unsigned char *old_name; 30911da177e4SLinus Torvalds 30921da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 30931da177e4SLinus Torvalds return 0; 30941da177e4SLinus Torvalds 30951da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 30961da177e4SLinus Torvalds if (error) 30971da177e4SLinus Torvalds return error; 30981da177e4SLinus Torvalds 30991da177e4SLinus Torvalds if (!new_dentry->d_inode) 3100a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 31011da177e4SLinus Torvalds else 31021da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 31031da177e4SLinus Torvalds if (error) 31041da177e4SLinus Torvalds return error; 31051da177e4SLinus Torvalds 3106acfa4380SAl Viro if (!old_dir->i_op->rename) 31071da177e4SLinus Torvalds return -EPERM; 31081da177e4SLinus Torvalds 31090eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 31100eeca283SRobert Love 31111da177e4SLinus Torvalds if (is_dir) 31121da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 31131da177e4SLinus Torvalds else 31141da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3115123df294SAl Viro if (!error) 3116123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 31175a190ae6SAl Viro new_dentry->d_inode, old_dentry); 31180eeca283SRobert Love fsnotify_oldname_free(old_name); 31190eeca283SRobert Love 31201da177e4SLinus Torvalds return error; 31211da177e4SLinus Torvalds } 31221da177e4SLinus Torvalds 31232e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 31242e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 31251da177e4SLinus Torvalds { 31261da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 31271da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 31281da177e4SLinus Torvalds struct dentry *trap; 31291da177e4SLinus Torvalds struct nameidata oldnd, newnd; 31302ad94ae6SAl Viro char *from; 31312ad94ae6SAl Viro char *to; 31322ad94ae6SAl Viro int error; 31331da177e4SLinus Torvalds 31342ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 31351da177e4SLinus Torvalds if (error) 31361da177e4SLinus Torvalds goto exit; 31371da177e4SLinus Torvalds 31382ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 31391da177e4SLinus Torvalds if (error) 31401da177e4SLinus Torvalds goto exit1; 31411da177e4SLinus Torvalds 31421da177e4SLinus Torvalds error = -EXDEV; 31434ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 31441da177e4SLinus Torvalds goto exit2; 31451da177e4SLinus Torvalds 31464ac91378SJan Blunck old_dir = oldnd.path.dentry; 31471da177e4SLinus Torvalds error = -EBUSY; 31481da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 31491da177e4SLinus Torvalds goto exit2; 31501da177e4SLinus Torvalds 31514ac91378SJan Blunck new_dir = newnd.path.dentry; 31521da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 31531da177e4SLinus Torvalds goto exit2; 31541da177e4SLinus Torvalds 31550612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 31560612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 31574e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 31580612d9fbSOGAWA Hirofumi 31591da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 31601da177e4SLinus Torvalds 316149705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 31621da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 31631da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 31641da177e4SLinus Torvalds goto exit3; 31651da177e4SLinus Torvalds /* source must exist */ 31661da177e4SLinus Torvalds error = -ENOENT; 31671da177e4SLinus Torvalds if (!old_dentry->d_inode) 31681da177e4SLinus Torvalds goto exit4; 31691da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 31701da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 31711da177e4SLinus Torvalds error = -ENOTDIR; 31721da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 31731da177e4SLinus Torvalds goto exit4; 31741da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 31751da177e4SLinus Torvalds goto exit4; 31761da177e4SLinus Torvalds } 31771da177e4SLinus Torvalds /* source should not be ancestor of target */ 31781da177e4SLinus Torvalds error = -EINVAL; 31791da177e4SLinus Torvalds if (old_dentry == trap) 31801da177e4SLinus Torvalds goto exit4; 318149705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 31821da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 31831da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 31841da177e4SLinus Torvalds goto exit4; 31851da177e4SLinus Torvalds /* target should not be an ancestor of source */ 31861da177e4SLinus Torvalds error = -ENOTEMPTY; 31871da177e4SLinus Torvalds if (new_dentry == trap) 31881da177e4SLinus Torvalds goto exit5; 31891da177e4SLinus Torvalds 31909079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 31919079b1ebSDave Hansen if (error) 31929079b1ebSDave Hansen goto exit5; 3193be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3194be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3195be6d3e56SKentaro Takeda if (error) 3196be6d3e56SKentaro Takeda goto exit6; 31971da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 31981da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 3199be6d3e56SKentaro Takeda exit6: 32009079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 32011da177e4SLinus Torvalds exit5: 32021da177e4SLinus Torvalds dput(new_dentry); 32031da177e4SLinus Torvalds exit4: 32041da177e4SLinus Torvalds dput(old_dentry); 32051da177e4SLinus Torvalds exit3: 32061da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 32071da177e4SLinus Torvalds exit2: 32081d957f9bSJan Blunck path_put(&newnd.path); 32092ad94ae6SAl Viro putname(to); 32101da177e4SLinus Torvalds exit1: 32111d957f9bSJan Blunck path_put(&oldnd.path); 32121da177e4SLinus Torvalds putname(from); 32132ad94ae6SAl Viro exit: 32141da177e4SLinus Torvalds return error; 32151da177e4SLinus Torvalds } 32161da177e4SLinus Torvalds 3217a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 32185590ff0dSUlrich Drepper { 32195590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 32205590ff0dSUlrich Drepper } 32215590ff0dSUlrich Drepper 32221da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 32231da177e4SLinus Torvalds { 32241da177e4SLinus Torvalds int len; 32251da177e4SLinus Torvalds 32261da177e4SLinus Torvalds len = PTR_ERR(link); 32271da177e4SLinus Torvalds if (IS_ERR(link)) 32281da177e4SLinus Torvalds goto out; 32291da177e4SLinus Torvalds 32301da177e4SLinus Torvalds len = strlen(link); 32311da177e4SLinus Torvalds if (len > (unsigned) buflen) 32321da177e4SLinus Torvalds len = buflen; 32331da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 32341da177e4SLinus Torvalds len = -EFAULT; 32351da177e4SLinus Torvalds out: 32361da177e4SLinus Torvalds return len; 32371da177e4SLinus Torvalds } 32381da177e4SLinus Torvalds 32391da177e4SLinus Torvalds /* 32401da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 32411da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 32421da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 32431da177e4SLinus Torvalds */ 32441da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 32451da177e4SLinus Torvalds { 32461da177e4SLinus Torvalds struct nameidata nd; 3247cc314eefSLinus Torvalds void *cookie; 3248694a1764SMarcin Slusarz int res; 3249cc314eefSLinus Torvalds 32501da177e4SLinus Torvalds nd.depth = 0; 3251cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3252694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3253694a1764SMarcin Slusarz return PTR_ERR(cookie); 3254694a1764SMarcin Slusarz 3255694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 32561da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3257cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3258694a1764SMarcin Slusarz return res; 32591da177e4SLinus Torvalds } 32601da177e4SLinus Torvalds 32611da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 32621da177e4SLinus Torvalds { 32631da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 32641da177e4SLinus Torvalds } 32651da177e4SLinus Torvalds 32661da177e4SLinus Torvalds /* get the link contents into pagecache */ 32671da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 32681da177e4SLinus Torvalds { 3269ebd09abbSDuane Griffin char *kaddr; 32701da177e4SLinus Torvalds struct page *page; 32711da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3272090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 32731da177e4SLinus Torvalds if (IS_ERR(page)) 32746fe6900eSNick Piggin return (char*)page; 32751da177e4SLinus Torvalds *ppage = page; 3276ebd09abbSDuane Griffin kaddr = kmap(page); 3277ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3278ebd09abbSDuane Griffin return kaddr; 32791da177e4SLinus Torvalds } 32801da177e4SLinus Torvalds 32811da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 32821da177e4SLinus Torvalds { 32831da177e4SLinus Torvalds struct page *page = NULL; 32841da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 32851da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 32861da177e4SLinus Torvalds if (page) { 32871da177e4SLinus Torvalds kunmap(page); 32881da177e4SLinus Torvalds page_cache_release(page); 32891da177e4SLinus Torvalds } 32901da177e4SLinus Torvalds return res; 32911da177e4SLinus Torvalds } 32921da177e4SLinus Torvalds 3293cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 32941da177e4SLinus Torvalds { 3295cc314eefSLinus Torvalds struct page *page = NULL; 32961da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 3297cc314eefSLinus Torvalds return page; 32981da177e4SLinus Torvalds } 32991da177e4SLinus Torvalds 3300cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 33011da177e4SLinus Torvalds { 3302cc314eefSLinus Torvalds struct page *page = cookie; 3303cc314eefSLinus Torvalds 3304cc314eefSLinus Torvalds if (page) { 33051da177e4SLinus Torvalds kunmap(page); 33061da177e4SLinus Torvalds page_cache_release(page); 33071da177e4SLinus Torvalds } 33081da177e4SLinus Torvalds } 33091da177e4SLinus Torvalds 331054566b2cSNick Piggin /* 331154566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 331254566b2cSNick Piggin */ 331354566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 33141da177e4SLinus Torvalds { 33151da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 33160adb25d2SKirill Korotaev struct page *page; 3317afddba49SNick Piggin void *fsdata; 3318beb497abSDmitriy Monakhov int err; 33191da177e4SLinus Torvalds char *kaddr; 332054566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 332154566b2cSNick Piggin if (nofs) 332254566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 33231da177e4SLinus Torvalds 33247e53cac4SNeilBrown retry: 3325afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 332654566b2cSNick Piggin flags, &page, &fsdata); 33271da177e4SLinus Torvalds if (err) 3328afddba49SNick Piggin goto fail; 3329afddba49SNick Piggin 33301da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 33311da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 33321da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 3333afddba49SNick Piggin 3334afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 3335afddba49SNick Piggin page, fsdata); 33361da177e4SLinus Torvalds if (err < 0) 33371da177e4SLinus Torvalds goto fail; 3338afddba49SNick Piggin if (err < len-1) 3339afddba49SNick Piggin goto retry; 3340afddba49SNick Piggin 33411da177e4SLinus Torvalds mark_inode_dirty(inode); 33421da177e4SLinus Torvalds return 0; 33431da177e4SLinus Torvalds fail: 33441da177e4SLinus Torvalds return err; 33451da177e4SLinus Torvalds } 33461da177e4SLinus Torvalds 33470adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 33480adb25d2SKirill Korotaev { 33490adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 335054566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 33510adb25d2SKirill Korotaev } 33520adb25d2SKirill Korotaev 335392e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 33541da177e4SLinus Torvalds .readlink = generic_readlink, 33551da177e4SLinus Torvalds .follow_link = page_follow_link_light, 33561da177e4SLinus Torvalds .put_link = page_put_link, 33571da177e4SLinus Torvalds }; 33581da177e4SLinus Torvalds 33592d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 33601da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 33611da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 33621da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 33631da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 33641da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 33651da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 33661da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 33671da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 33681da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 33690adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 33701da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 33711da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 33721da177e4SLinus Torvalds EXPORT_SYMBOL(path_lookup); 3373d1811465SAl Viro EXPORT_SYMBOL(kern_path); 337416f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 3375f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 33768c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission); 33771da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 33781da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 33791da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 33801da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 33811da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 33821da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 33831da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 33841da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 33851da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 33861da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 33871da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 33881da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 33891da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 33901da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 3391