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 56634286d66SNick Piggin static int d_revalidate(struct dentry *dentry, struct nameidata *nd) 56734286d66SNick Piggin { 56834286d66SNick Piggin int status; 56934286d66SNick Piggin 57034286d66SNick Piggin status = dentry->d_op->d_revalidate(dentry, nd); 57134286d66SNick Piggin if (status == -ECHILD) { 57234286d66SNick Piggin if (nameidata_dentry_drop_rcu(nd, dentry)) 57334286d66SNick Piggin return status; 57434286d66SNick Piggin status = dentry->d_op->d_revalidate(dentry, nd); 57534286d66SNick Piggin } 57634286d66SNick Piggin 57734286d66SNick Piggin return status; 57834286d66SNick Piggin } 57934286d66SNick Piggin 580bcdc5e01SIan Kent static inline struct dentry * 581bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd) 582bcdc5e01SIan Kent { 58334286d66SNick Piggin int status; 58434286d66SNick Piggin 58534286d66SNick Piggin status = d_revalidate(dentry, nd); 586bcdc5e01SIan Kent if (unlikely(status <= 0)) { 587bcdc5e01SIan Kent /* 588bcdc5e01SIan Kent * The dentry failed validation. 589bcdc5e01SIan Kent * If d_revalidate returned 0 attempt to invalidate 590bcdc5e01SIan Kent * the dentry otherwise d_revalidate is asking us 591bcdc5e01SIan Kent * to return a fail status. 592bcdc5e01SIan Kent */ 59334286d66SNick Piggin if (status < 0) { 59434286d66SNick Piggin /* If we're in rcu-walk, we don't have a ref */ 59534286d66SNick Piggin if (!(nd->flags & LOOKUP_RCU)) 59634286d66SNick Piggin dput(dentry); 59734286d66SNick Piggin dentry = ERR_PTR(status); 59834286d66SNick Piggin 59934286d66SNick Piggin } else { 60034286d66SNick Piggin /* Don't d_invalidate in rcu-walk mode */ 60134286d66SNick Piggin if (nameidata_dentry_drop_rcu_maybe(nd, dentry)) 60234286d66SNick Piggin return ERR_PTR(-ECHILD); 603bcdc5e01SIan Kent if (!d_invalidate(dentry)) { 604bcdc5e01SIan Kent dput(dentry); 605bcdc5e01SIan Kent dentry = NULL; 606bcdc5e01SIan Kent } 607bcdc5e01SIan Kent } 608bcdc5e01SIan Kent } 609bcdc5e01SIan Kent return dentry; 610bcdc5e01SIan Kent } 611bcdc5e01SIan Kent 612fb045adbSNick Piggin static inline int need_reval_dot(struct dentry *dentry) 613fb045adbSNick Piggin { 614fb045adbSNick Piggin if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE))) 615fb045adbSNick Piggin return 0; 616fb045adbSNick Piggin 617fb045adbSNick Piggin if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))) 618fb045adbSNick Piggin return 0; 619fb045adbSNick Piggin 620fb045adbSNick Piggin return 1; 621fb045adbSNick Piggin } 622fb045adbSNick Piggin 6231da177e4SLinus Torvalds /* 62439159de2SJeff Layton * force_reval_path - force revalidation of a dentry 62539159de2SJeff Layton * 62639159de2SJeff Layton * In some situations the path walking code will trust dentries without 62739159de2SJeff Layton * revalidating them. This causes problems for filesystems that depend on 62839159de2SJeff Layton * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set 62939159de2SJeff Layton * (which indicates that it's possible for the dentry to go stale), force 63039159de2SJeff Layton * a d_revalidate call before proceeding. 63139159de2SJeff Layton * 63239159de2SJeff Layton * Returns 0 if the revalidation was successful. If the revalidation fails, 63339159de2SJeff Layton * either return the error returned by d_revalidate or -ESTALE if the 63439159de2SJeff Layton * revalidation it just returned 0. If d_revalidate returns 0, we attempt to 63539159de2SJeff Layton * invalidate the dentry. It's up to the caller to handle putting references 63639159de2SJeff Layton * to the path if necessary. 63739159de2SJeff Layton */ 63839159de2SJeff Layton static int 63939159de2SJeff Layton force_reval_path(struct path *path, struct nameidata *nd) 64039159de2SJeff Layton { 64139159de2SJeff Layton int status; 64239159de2SJeff Layton struct dentry *dentry = path->dentry; 64339159de2SJeff Layton 64439159de2SJeff Layton /* 64539159de2SJeff Layton * only check on filesystems where it's possible for the dentry to 646fb045adbSNick Piggin * become stale. 64739159de2SJeff Layton */ 648fb045adbSNick Piggin if (!need_reval_dot(dentry)) 64939159de2SJeff Layton return 0; 65039159de2SJeff Layton 65134286d66SNick Piggin status = d_revalidate(dentry, nd); 65239159de2SJeff Layton if (status > 0) 65339159de2SJeff Layton return 0; 65439159de2SJeff Layton 65539159de2SJeff Layton if (!status) { 65639159de2SJeff Layton d_invalidate(dentry); 65739159de2SJeff Layton status = -ESTALE; 65839159de2SJeff Layton } 65939159de2SJeff Layton return status; 66039159de2SJeff Layton } 66139159de2SJeff Layton 66239159de2SJeff Layton /* 663b75b5086SAl Viro * Short-cut version of permission(), for calling on directories 664b75b5086SAl Viro * during pathname resolution. Combines parts of permission() 665b75b5086SAl Viro * and generic_permission(), and tests ONLY for MAY_EXEC permission. 6661da177e4SLinus Torvalds * 6671da177e4SLinus Torvalds * If appropriate, check DAC only. If not appropriate, or 668b75b5086SAl Viro * short-cut DAC fails, then call ->permission() to do more 6691da177e4SLinus Torvalds * complete permission check. 6701da177e4SLinus Torvalds */ 67131e6b01fSNick Piggin static inline int __exec_permission(struct inode *inode, int rcu) 6721da177e4SLinus Torvalds { 6735909ccaaSLinus Torvalds int ret; 6741da177e4SLinus Torvalds 675cb9179eaSLinus Torvalds if (inode->i_op->permission) { 67631e6b01fSNick Piggin if (rcu) 67731e6b01fSNick Piggin return -ECHILD; 6785909ccaaSLinus Torvalds ret = inode->i_op->permission(inode, MAY_EXEC); 679cb9179eaSLinus Torvalds if (!ret) 680cb9179eaSLinus Torvalds goto ok; 681cb9179eaSLinus Torvalds return ret; 682cb9179eaSLinus Torvalds } 68331e6b01fSNick Piggin ret = __acl_permission_check(inode, MAY_EXEC, inode->i_op->check_acl, rcu); 6845909ccaaSLinus Torvalds if (!ret) 6851da177e4SLinus Torvalds goto ok; 68631e6b01fSNick Piggin if (rcu && ret == -ECHILD) 68731e6b01fSNick Piggin return ret; 6881da177e4SLinus Torvalds 689f1ac9f6bSLinus Torvalds if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH)) 6901da177e4SLinus Torvalds goto ok; 6911da177e4SLinus Torvalds 6925909ccaaSLinus Torvalds return ret; 6931da177e4SLinus Torvalds ok: 69431e6b01fSNick Piggin return security_inode_exec_permission(inode, rcu); 69531e6b01fSNick Piggin } 69631e6b01fSNick Piggin 69731e6b01fSNick Piggin static int exec_permission(struct inode *inode) 69831e6b01fSNick Piggin { 69931e6b01fSNick Piggin return __exec_permission(inode, 0); 70031e6b01fSNick Piggin } 70131e6b01fSNick Piggin 70231e6b01fSNick Piggin static int exec_permission_rcu(struct inode *inode) 70331e6b01fSNick Piggin { 70431e6b01fSNick Piggin return __exec_permission(inode, 1); 7051da177e4SLinus Torvalds } 7061da177e4SLinus Torvalds 7072a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 7082a737871SAl Viro { 709f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 710f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 7112a737871SAl Viro } 7122a737871SAl Viro 7136de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 7146de88d72SAl Viro 71531e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 71631e6b01fSNick Piggin { 71731e6b01fSNick Piggin if (!nd->root.mnt) { 71831e6b01fSNick Piggin struct fs_struct *fs = current->fs; 719c28cc364SNick Piggin unsigned seq; 720c28cc364SNick Piggin 721c28cc364SNick Piggin do { 722c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 72331e6b01fSNick Piggin nd->root = fs->root; 724c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 72531e6b01fSNick Piggin } 72631e6b01fSNick Piggin } 72731e6b01fSNick Piggin 728f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 7291da177e4SLinus Torvalds { 73031e6b01fSNick Piggin int ret; 73131e6b01fSNick Piggin 7321da177e4SLinus Torvalds if (IS_ERR(link)) 7331da177e4SLinus Torvalds goto fail; 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds if (*link == '/') { 7362a737871SAl Viro set_root(nd); 7371d957f9bSJan Blunck path_put(&nd->path); 7382a737871SAl Viro nd->path = nd->root; 7392a737871SAl Viro path_get(&nd->root); 7401da177e4SLinus Torvalds } 74131e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 742b4091d5fSChristoph Hellwig 74331e6b01fSNick Piggin ret = link_path_walk(link, nd); 74431e6b01fSNick Piggin return ret; 7451da177e4SLinus Torvalds fail: 7461d957f9bSJan Blunck path_put(&nd->path); 7471da177e4SLinus Torvalds return PTR_ERR(link); 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds 7501d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 751051d3812SIan Kent { 752051d3812SIan Kent dput(path->dentry); 7534ac91378SJan Blunck if (path->mnt != nd->path.mnt) 754051d3812SIan Kent mntput(path->mnt); 755051d3812SIan Kent } 756051d3812SIan Kent 757051d3812SIan Kent static inline void path_to_nameidata(struct path *path, struct nameidata *nd) 758051d3812SIan Kent { 75931e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 7604ac91378SJan Blunck dput(nd->path.dentry); 76131e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 7624ac91378SJan Blunck mntput(nd->path.mnt); 7639a229683SHuang Shijie } 76431e6b01fSNick Piggin nd->path.mnt = path->mnt; 7654ac91378SJan Blunck nd->path.dentry = path->dentry; 766051d3812SIan Kent } 767051d3812SIan Kent 768def4af30SAl Viro static __always_inline int 769def4af30SAl Viro __do_follow_link(struct path *path, struct nameidata *nd, void **p) 7701da177e4SLinus Torvalds { 7711da177e4SLinus Torvalds int error; 772cd4e91d3SAl Viro struct dentry *dentry = path->dentry; 7731da177e4SLinus Torvalds 774d671a1cbSAl Viro touch_atime(path->mnt, dentry); 7751da177e4SLinus Torvalds nd_set_link(nd, NULL); 776cd4e91d3SAl Viro 7774ac91378SJan Blunck if (path->mnt != nd->path.mnt) { 778051d3812SIan Kent path_to_nameidata(path, nd); 77931e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 780051d3812SIan Kent dget(dentry); 781051d3812SIan Kent } 782cd4e91d3SAl Viro mntget(path->mnt); 78331e6b01fSNick Piggin 78486acdca1SAl Viro nd->last_type = LAST_BIND; 785def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 786def4af30SAl Viro error = PTR_ERR(*p); 787def4af30SAl Viro if (!IS_ERR(*p)) { 7881da177e4SLinus Torvalds char *s = nd_get_link(nd); 789cc314eefSLinus Torvalds error = 0; 7901da177e4SLinus Torvalds if (s) 7911da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 79239159de2SJeff Layton else if (nd->last_type == LAST_BIND) { 79339159de2SJeff Layton error = force_reval_path(&nd->path, nd); 79439159de2SJeff Layton if (error) 79539159de2SJeff Layton path_put(&nd->path); 79639159de2SJeff Layton } 7971da177e4SLinus Torvalds } 7981da177e4SLinus Torvalds return error; 7991da177e4SLinus Torvalds } 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds /* 8021da177e4SLinus Torvalds * This limits recursive symlink follows to 8, while 8031da177e4SLinus Torvalds * limiting consecutive symlinks to 40. 8041da177e4SLinus Torvalds * 8051da177e4SLinus Torvalds * Without that kind of total limit, nasty chains of consecutive 8061da177e4SLinus Torvalds * symlinks can cause almost arbitrarily long lookups. 8071da177e4SLinus Torvalds */ 80890ebe565SAl Viro static inline int do_follow_link(struct path *path, struct nameidata *nd) 8091da177e4SLinus Torvalds { 810def4af30SAl Viro void *cookie; 8111da177e4SLinus Torvalds int err = -ELOOP; 8121da177e4SLinus Torvalds if (current->link_count >= MAX_NESTED_LINKS) 8131da177e4SLinus Torvalds goto loop; 8141da177e4SLinus Torvalds if (current->total_link_count >= 40) 8151da177e4SLinus Torvalds goto loop; 8161da177e4SLinus Torvalds BUG_ON(nd->depth >= MAX_NESTED_LINKS); 8171da177e4SLinus Torvalds cond_resched(); 81890ebe565SAl Viro err = security_inode_follow_link(path->dentry, nd); 8191da177e4SLinus Torvalds if (err) 8201da177e4SLinus Torvalds goto loop; 8211da177e4SLinus Torvalds current->link_count++; 8221da177e4SLinus Torvalds current->total_link_count++; 8231da177e4SLinus Torvalds nd->depth++; 824def4af30SAl Viro err = __do_follow_link(path, nd, &cookie); 825def4af30SAl Viro if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link) 826def4af30SAl Viro path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie); 827258fa999SAl Viro path_put(path); 8281da177e4SLinus Torvalds current->link_count--; 8291da177e4SLinus Torvalds nd->depth--; 8301da177e4SLinus Torvalds return err; 8311da177e4SLinus Torvalds loop: 8321d957f9bSJan Blunck path_put_conditional(path, nd); 8331d957f9bSJan Blunck path_put(&nd->path); 8341da177e4SLinus Torvalds return err; 8351da177e4SLinus Torvalds } 8361da177e4SLinus Torvalds 83731e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 83831e6b01fSNick Piggin { 83931e6b01fSNick Piggin struct vfsmount *parent; 84031e6b01fSNick Piggin struct dentry *mountpoint; 84131e6b01fSNick Piggin 84231e6b01fSNick Piggin parent = path->mnt->mnt_parent; 84331e6b01fSNick Piggin if (parent == path->mnt) 84431e6b01fSNick Piggin return 0; 84531e6b01fSNick Piggin mountpoint = path->mnt->mnt_mountpoint; 84631e6b01fSNick Piggin path->dentry = mountpoint; 84731e6b01fSNick Piggin path->mnt = parent; 84831e6b01fSNick Piggin return 1; 84931e6b01fSNick Piggin } 85031e6b01fSNick Piggin 851bab77ebfSAl Viro int follow_up(struct path *path) 8521da177e4SLinus Torvalds { 8531da177e4SLinus Torvalds struct vfsmount *parent; 8541da177e4SLinus Torvalds struct dentry *mountpoint; 85599b7db7bSNick Piggin 85699b7db7bSNick Piggin br_read_lock(vfsmount_lock); 857bab77ebfSAl Viro parent = path->mnt->mnt_parent; 858bab77ebfSAl Viro if (parent == path->mnt) { 85999b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 8601da177e4SLinus Torvalds return 0; 8611da177e4SLinus Torvalds } 8621da177e4SLinus Torvalds mntget(parent); 863bab77ebfSAl Viro mountpoint = dget(path->mnt->mnt_mountpoint); 86499b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 865bab77ebfSAl Viro dput(path->dentry); 866bab77ebfSAl Viro path->dentry = mountpoint; 867bab77ebfSAl Viro mntput(path->mnt); 868bab77ebfSAl Viro path->mnt = parent; 8691da177e4SLinus Torvalds return 1; 8701da177e4SLinus Torvalds } 8711da177e4SLinus Torvalds 872b5c84bf6SNick Piggin /* 873b5c84bf6SNick Piggin * serialization is taken care of in namespace.c 8741da177e4SLinus Torvalds */ 87531e6b01fSNick Piggin static void __follow_mount_rcu(struct nameidata *nd, struct path *path, 87631e6b01fSNick Piggin struct inode **inode) 87731e6b01fSNick Piggin { 87831e6b01fSNick Piggin while (d_mountpoint(path->dentry)) { 87931e6b01fSNick Piggin struct vfsmount *mounted; 88031e6b01fSNick Piggin mounted = __lookup_mnt(path->mnt, path->dentry, 1); 88131e6b01fSNick Piggin if (!mounted) 88231e6b01fSNick Piggin return; 88331e6b01fSNick Piggin path->mnt = mounted; 88431e6b01fSNick Piggin path->dentry = mounted->mnt_root; 88531e6b01fSNick Piggin nd->seq = read_seqcount_begin(&path->dentry->d_seq); 88631e6b01fSNick Piggin *inode = path->dentry->d_inode; 88731e6b01fSNick Piggin } 88831e6b01fSNick Piggin } 88931e6b01fSNick Piggin 890463ffb2eSAl Viro static int __follow_mount(struct path *path) 891463ffb2eSAl Viro { 892463ffb2eSAl Viro int res = 0; 893463ffb2eSAl Viro while (d_mountpoint(path->dentry)) { 8941c755af4SAl Viro struct vfsmount *mounted = lookup_mnt(path); 895463ffb2eSAl Viro if (!mounted) 896463ffb2eSAl Viro break; 897463ffb2eSAl Viro dput(path->dentry); 898463ffb2eSAl Viro if (res) 899463ffb2eSAl Viro mntput(path->mnt); 900463ffb2eSAl Viro path->mnt = mounted; 901463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 902463ffb2eSAl Viro res = 1; 903463ffb2eSAl Viro } 904463ffb2eSAl Viro return res; 905463ffb2eSAl Viro } 906463ffb2eSAl Viro 90779ed0226SAl Viro static void follow_mount(struct path *path) 9081da177e4SLinus Torvalds { 90979ed0226SAl Viro while (d_mountpoint(path->dentry)) { 9101c755af4SAl Viro struct vfsmount *mounted = lookup_mnt(path); 9111da177e4SLinus Torvalds if (!mounted) 9121da177e4SLinus Torvalds break; 91379ed0226SAl Viro dput(path->dentry); 91479ed0226SAl Viro mntput(path->mnt); 91579ed0226SAl Viro path->mnt = mounted; 91679ed0226SAl Viro path->dentry = dget(mounted->mnt_root); 9171da177e4SLinus Torvalds } 9181da177e4SLinus Torvalds } 9191da177e4SLinus Torvalds 9209393bd07SAl Viro int follow_down(struct path *path) 9211da177e4SLinus Torvalds { 9221da177e4SLinus Torvalds struct vfsmount *mounted; 9231da177e4SLinus Torvalds 9241c755af4SAl Viro mounted = lookup_mnt(path); 9251da177e4SLinus Torvalds if (mounted) { 9269393bd07SAl Viro dput(path->dentry); 9279393bd07SAl Viro mntput(path->mnt); 9289393bd07SAl Viro path->mnt = mounted; 9299393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 9301da177e4SLinus Torvalds return 1; 9311da177e4SLinus Torvalds } 9321da177e4SLinus Torvalds return 0; 9331da177e4SLinus Torvalds } 9341da177e4SLinus Torvalds 93531e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 93631e6b01fSNick Piggin { 93731e6b01fSNick Piggin struct inode *inode = nd->inode; 93831e6b01fSNick Piggin 93931e6b01fSNick Piggin set_root_rcu(nd); 94031e6b01fSNick Piggin 94131e6b01fSNick Piggin while(1) { 94231e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 94331e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 94431e6b01fSNick Piggin break; 94531e6b01fSNick Piggin } 94631e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 94731e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 94831e6b01fSNick Piggin struct dentry *parent = old->d_parent; 94931e6b01fSNick Piggin unsigned seq; 95031e6b01fSNick Piggin 95131e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 95231e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 95331e6b01fSNick Piggin return -ECHILD; 95431e6b01fSNick Piggin inode = parent->d_inode; 95531e6b01fSNick Piggin nd->path.dentry = parent; 95631e6b01fSNick Piggin nd->seq = seq; 95731e6b01fSNick Piggin break; 95831e6b01fSNick Piggin } 95931e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 96031e6b01fSNick Piggin break; 96131e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 96231e6b01fSNick Piggin inode = nd->path.dentry->d_inode; 96331e6b01fSNick Piggin } 96431e6b01fSNick Piggin __follow_mount_rcu(nd, &nd->path, &inode); 96531e6b01fSNick Piggin nd->inode = inode; 96631e6b01fSNick Piggin 96731e6b01fSNick Piggin return 0; 96831e6b01fSNick Piggin } 96931e6b01fSNick Piggin 97031e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 9711da177e4SLinus Torvalds { 9722a737871SAl Viro set_root(nd); 973e518ddb7SAndreas Mohr 9741da177e4SLinus Torvalds while(1) { 9754ac91378SJan Blunck struct dentry *old = nd->path.dentry; 9761da177e4SLinus Torvalds 9772a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 9782a737871SAl Viro nd->path.mnt == nd->root.mnt) { 9791da177e4SLinus Torvalds break; 9801da177e4SLinus Torvalds } 9814ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 9823088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 9833088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 9841da177e4SLinus Torvalds dput(old); 9851da177e4SLinus Torvalds break; 9861da177e4SLinus Torvalds } 9873088dd70SAl Viro if (!follow_up(&nd->path)) 9881da177e4SLinus Torvalds break; 9891da177e4SLinus Torvalds } 99079ed0226SAl Viro follow_mount(&nd->path); 99131e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds /* 995baa03890SNick Piggin * Allocate a dentry with name and parent, and perform a parent 996baa03890SNick Piggin * directory ->lookup on it. Returns the new dentry, or ERR_PTR 997baa03890SNick Piggin * on error. parent->d_inode->i_mutex must be held. d_lookup must 998baa03890SNick Piggin * have verified that no child exists while under i_mutex. 999baa03890SNick Piggin */ 1000baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent, 1001baa03890SNick Piggin struct qstr *name, struct nameidata *nd) 1002baa03890SNick Piggin { 1003baa03890SNick Piggin struct inode *inode = parent->d_inode; 1004baa03890SNick Piggin struct dentry *dentry; 1005baa03890SNick Piggin struct dentry *old; 1006baa03890SNick Piggin 1007baa03890SNick Piggin /* Don't create child dentry for a dead directory. */ 1008baa03890SNick Piggin if (unlikely(IS_DEADDIR(inode))) 1009baa03890SNick Piggin return ERR_PTR(-ENOENT); 1010baa03890SNick Piggin 1011baa03890SNick Piggin dentry = d_alloc(parent, name); 1012baa03890SNick Piggin if (unlikely(!dentry)) 1013baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1014baa03890SNick Piggin 1015baa03890SNick Piggin old = inode->i_op->lookup(inode, dentry, nd); 1016baa03890SNick Piggin if (unlikely(old)) { 1017baa03890SNick Piggin dput(dentry); 1018baa03890SNick Piggin dentry = old; 1019baa03890SNick Piggin } 1020baa03890SNick Piggin return dentry; 1021baa03890SNick Piggin } 1022baa03890SNick Piggin 1023baa03890SNick Piggin /* 10241da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 10251da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 10261da177e4SLinus Torvalds * It _is_ time-critical. 10271da177e4SLinus Torvalds */ 10281da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name, 102931e6b01fSNick Piggin struct path *path, struct inode **inode) 10301da177e4SLinus Torvalds { 10314ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 103231e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 10336e6b1bd1SAl Viro struct inode *dir; 10343cac260aSAl Viro /* 10353cac260aSAl Viro * See if the low-level filesystem might want 10363cac260aSAl Viro * to use its own hash.. 10373cac260aSAl Viro */ 1038fb045adbSNick Piggin if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 103931e6b01fSNick Piggin int err = parent->d_op->d_hash(parent, nd->inode, name); 10403cac260aSAl Viro if (err < 0) 10413cac260aSAl Viro return err; 10423cac260aSAl Viro } 10431da177e4SLinus Torvalds 1044b04f784eSNick Piggin /* 1045b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1046b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1047b04f784eSNick Piggin * do the non-racy lookup, below. 1048b04f784eSNick Piggin */ 104931e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 105031e6b01fSNick Piggin unsigned seq; 105131e6b01fSNick Piggin 105231e6b01fSNick Piggin *inode = nd->inode; 105331e6b01fSNick Piggin dentry = __d_lookup_rcu(parent, name, &seq, inode); 105431e6b01fSNick Piggin if (!dentry) { 105531e6b01fSNick Piggin if (nameidata_drop_rcu(nd)) 105631e6b01fSNick Piggin return -ECHILD; 105731e6b01fSNick Piggin goto need_lookup; 105831e6b01fSNick Piggin } 105931e6b01fSNick Piggin /* Memory barrier in read_seqcount_begin of child is enough */ 106031e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 106131e6b01fSNick Piggin return -ECHILD; 106231e6b01fSNick Piggin 106331e6b01fSNick Piggin nd->seq = seq; 106434286d66SNick Piggin if (dentry->d_flags & DCACHE_OP_REVALIDATE) 106531e6b01fSNick Piggin goto need_revalidate; 106631e6b01fSNick Piggin path->mnt = mnt; 106731e6b01fSNick Piggin path->dentry = dentry; 106831e6b01fSNick Piggin __follow_mount_rcu(nd, path, inode); 106931e6b01fSNick Piggin } else { 107031e6b01fSNick Piggin dentry = __d_lookup(parent, name); 10711da177e4SLinus Torvalds if (!dentry) 10721da177e4SLinus Torvalds goto need_lookup; 10732e2e88eaSNick Piggin found: 1074fb045adbSNick Piggin if (dentry->d_flags & DCACHE_OP_REVALIDATE) 10751da177e4SLinus Torvalds goto need_revalidate; 10761da177e4SLinus Torvalds done: 10771da177e4SLinus Torvalds path->mnt = mnt; 10781da177e4SLinus Torvalds path->dentry = dentry; 1079634ee701SAl Viro __follow_mount(path); 108031e6b01fSNick Piggin *inode = path->dentry->d_inode; 108131e6b01fSNick Piggin } 10821da177e4SLinus Torvalds return 0; 10831da177e4SLinus Torvalds 10841da177e4SLinus Torvalds need_lookup: 10856e6b1bd1SAl Viro dir = parent->d_inode; 108631e6b01fSNick Piggin BUG_ON(nd->inode != dir); 10876e6b1bd1SAl Viro 10886e6b1bd1SAl Viro mutex_lock(&dir->i_mutex); 10896e6b1bd1SAl Viro /* 10906e6b1bd1SAl Viro * First re-do the cached lookup just in case it was created 1091b04f784eSNick Piggin * while we waited for the directory semaphore, or the first 1092b04f784eSNick Piggin * lookup failed due to an unrelated rename. 10936e6b1bd1SAl Viro * 1094b04f784eSNick Piggin * This could use version numbering or similar to avoid unnecessary 1095b04f784eSNick Piggin * cache lookups, but then we'd have to do the first lookup in the 1096b04f784eSNick Piggin * non-racy way. However in the common case here, everything should 1097b04f784eSNick Piggin * be hot in cache, so would it be a big win? 10986e6b1bd1SAl Viro */ 10996e6b1bd1SAl Viro dentry = d_lookup(parent, name); 1100baa03890SNick Piggin if (likely(!dentry)) { 1101baa03890SNick Piggin dentry = d_alloc_and_lookup(parent, name, nd); 11026e6b1bd1SAl Viro mutex_unlock(&dir->i_mutex); 11036e6b1bd1SAl Viro if (IS_ERR(dentry)) 11046e6b1bd1SAl Viro goto fail; 11056e6b1bd1SAl Viro goto done; 11066e6b1bd1SAl Viro } 11076e6b1bd1SAl Viro /* 11086e6b1bd1SAl Viro * Uhhuh! Nasty case: the cache was re-populated while 11096e6b1bd1SAl Viro * we waited on the semaphore. Need to revalidate. 11106e6b1bd1SAl Viro */ 11116e6b1bd1SAl Viro mutex_unlock(&dir->i_mutex); 11122e2e88eaSNick Piggin goto found; 11131da177e4SLinus Torvalds 11141da177e4SLinus Torvalds need_revalidate: 1115bcdc5e01SIan Kent dentry = do_revalidate(dentry, nd); 1116bcdc5e01SIan Kent if (!dentry) 11171da177e4SLinus Torvalds goto need_lookup; 1118bcdc5e01SIan Kent if (IS_ERR(dentry)) 1119bcdc5e01SIan Kent goto fail; 1120bcdc5e01SIan Kent goto done; 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds fail: 11231da177e4SLinus Torvalds return PTR_ERR(dentry); 11241da177e4SLinus Torvalds } 11251da177e4SLinus Torvalds 11261da177e4SLinus Torvalds /* 1127ac278a9cSAl Viro * This is a temporary kludge to deal with "automount" symlinks; proper 1128ac278a9cSAl Viro * solution is to trigger them on follow_mount(), so that do_lookup() 1129ac278a9cSAl Viro * would DTRT. To be killed before 2.6.34-final. 1130ac278a9cSAl Viro */ 1131ac278a9cSAl Viro static inline int follow_on_final(struct inode *inode, unsigned lookup_flags) 1132ac278a9cSAl Viro { 1133ac278a9cSAl Viro return inode && unlikely(inode->i_op->follow_link) && 1134ac278a9cSAl Viro ((lookup_flags & LOOKUP_FOLLOW) || S_ISDIR(inode->i_mode)); 1135ac278a9cSAl Viro } 1136ac278a9cSAl Viro 1137ac278a9cSAl Viro /* 11381da177e4SLinus Torvalds * Name resolution. 1139ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1140ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 11411da177e4SLinus Torvalds * 1142ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1143ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 11441da177e4SLinus Torvalds */ 11456de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 11461da177e4SLinus Torvalds { 11471da177e4SLinus Torvalds struct path next; 11481da177e4SLinus Torvalds int err; 11491da177e4SLinus Torvalds unsigned int lookup_flags = nd->flags; 11501da177e4SLinus Torvalds 11511da177e4SLinus Torvalds while (*name=='/') 11521da177e4SLinus Torvalds name++; 11531da177e4SLinus Torvalds if (!*name) 11541da177e4SLinus Torvalds goto return_reval; 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds if (nd->depth) 1157f55eab82STrond Myklebust lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE); 11581da177e4SLinus Torvalds 11591da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 11601da177e4SLinus Torvalds for(;;) { 116131e6b01fSNick Piggin struct inode *inode; 11621da177e4SLinus Torvalds unsigned long hash; 11631da177e4SLinus Torvalds struct qstr this; 11641da177e4SLinus Torvalds unsigned int c; 11651da177e4SLinus Torvalds 1166cdce5d6bSTrond Myklebust nd->flags |= LOOKUP_CONTINUE; 116731e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 116831e6b01fSNick Piggin err = exec_permission_rcu(nd->inode); 116931e6b01fSNick Piggin if (err == -ECHILD) { 117031e6b01fSNick Piggin if (nameidata_drop_rcu(nd)) 117131e6b01fSNick Piggin return -ECHILD; 117231e6b01fSNick Piggin goto exec_again; 117331e6b01fSNick Piggin } 117431e6b01fSNick Piggin } else { 117531e6b01fSNick Piggin exec_again: 117631e6b01fSNick Piggin err = exec_permission(nd->inode); 117731e6b01fSNick Piggin } 11781da177e4SLinus Torvalds if (err) 11791da177e4SLinus Torvalds break; 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds this.name = name; 11821da177e4SLinus Torvalds c = *(const unsigned char *)name; 11831da177e4SLinus Torvalds 11841da177e4SLinus Torvalds hash = init_name_hash(); 11851da177e4SLinus Torvalds do { 11861da177e4SLinus Torvalds name++; 11871da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 11881da177e4SLinus Torvalds c = *(const unsigned char *)name; 11891da177e4SLinus Torvalds } while (c && (c != '/')); 11901da177e4SLinus Torvalds this.len = name - (const char *) this.name; 11911da177e4SLinus Torvalds this.hash = end_name_hash(hash); 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds /* remove trailing slashes? */ 11941da177e4SLinus Torvalds if (!c) 11951da177e4SLinus Torvalds goto last_component; 11961da177e4SLinus Torvalds while (*++name == '/'); 11971da177e4SLinus Torvalds if (!*name) 11981da177e4SLinus Torvalds goto last_with_slashes; 11991da177e4SLinus Torvalds 12001da177e4SLinus Torvalds /* 12011da177e4SLinus Torvalds * "." and ".." are special - ".." especially so because it has 12021da177e4SLinus Torvalds * to be able to know about the current root directory and 12031da177e4SLinus Torvalds * parent relationships. 12041da177e4SLinus Torvalds */ 12051da177e4SLinus Torvalds if (this.name[0] == '.') switch (this.len) { 12061da177e4SLinus Torvalds default: 12071da177e4SLinus Torvalds break; 12081da177e4SLinus Torvalds case 2: 12091da177e4SLinus Torvalds if (this.name[1] != '.') 12101da177e4SLinus Torvalds break; 121131e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 121231e6b01fSNick Piggin if (follow_dotdot_rcu(nd)) 121331e6b01fSNick Piggin return -ECHILD; 121431e6b01fSNick Piggin } else 121558c465ebSAl Viro follow_dotdot(nd); 12161da177e4SLinus Torvalds /* fallthrough */ 12171da177e4SLinus Torvalds case 1: 12181da177e4SLinus Torvalds continue; 12191da177e4SLinus Torvalds } 12201da177e4SLinus Torvalds /* This does the actual lookups.. */ 122131e6b01fSNick Piggin err = do_lookup(nd, &this, &next, &inode); 12221da177e4SLinus Torvalds if (err) 12231da177e4SLinus Torvalds break; 12241da177e4SLinus Torvalds err = -ENOENT; 12251da177e4SLinus Torvalds if (!inode) 12261da177e4SLinus Torvalds goto out_dput; 12271da177e4SLinus Torvalds 12281da177e4SLinus Torvalds if (inode->i_op->follow_link) { 122931e6b01fSNick Piggin /* We commonly drop rcu-walk here */ 123031e6b01fSNick Piggin if (nameidata_dentry_drop_rcu_maybe(nd, next.dentry)) 123131e6b01fSNick Piggin return -ECHILD; 123231e6b01fSNick Piggin BUG_ON(inode != next.dentry->d_inode); 123390ebe565SAl Viro err = do_follow_link(&next, nd); 12341da177e4SLinus Torvalds if (err) 12351da177e4SLinus Torvalds goto return_err; 123631e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 12371da177e4SLinus Torvalds err = -ENOENT; 123831e6b01fSNick Piggin if (!nd->inode) 12391da177e4SLinus Torvalds break; 124031e6b01fSNick Piggin } else { 124109dd17d3SMiklos Szeredi path_to_nameidata(&next, nd); 124231e6b01fSNick Piggin nd->inode = inode; 124331e6b01fSNick Piggin } 12441da177e4SLinus Torvalds err = -ENOTDIR; 124531e6b01fSNick Piggin if (!nd->inode->i_op->lookup) 12461da177e4SLinus Torvalds break; 12471da177e4SLinus Torvalds continue; 12481da177e4SLinus Torvalds /* here ends the main loop */ 12491da177e4SLinus Torvalds 12501da177e4SLinus Torvalds last_with_slashes: 12511da177e4SLinus Torvalds lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 12521da177e4SLinus Torvalds last_component: 1253f55eab82STrond Myklebust /* Clear LOOKUP_CONTINUE iff it was previously unset */ 1254f55eab82STrond Myklebust nd->flags &= lookup_flags | ~LOOKUP_CONTINUE; 12551da177e4SLinus Torvalds if (lookup_flags & LOOKUP_PARENT) 12561da177e4SLinus Torvalds goto lookup_parent; 12571da177e4SLinus Torvalds if (this.name[0] == '.') switch (this.len) { 12581da177e4SLinus Torvalds default: 12591da177e4SLinus Torvalds break; 12601da177e4SLinus Torvalds case 2: 12611da177e4SLinus Torvalds if (this.name[1] != '.') 12621da177e4SLinus Torvalds break; 126331e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 126431e6b01fSNick Piggin if (follow_dotdot_rcu(nd)) 126531e6b01fSNick Piggin return -ECHILD; 126631e6b01fSNick Piggin } else 126758c465ebSAl Viro follow_dotdot(nd); 12681da177e4SLinus Torvalds /* fallthrough */ 12691da177e4SLinus Torvalds case 1: 12701da177e4SLinus Torvalds goto return_reval; 12711da177e4SLinus Torvalds } 127231e6b01fSNick Piggin err = do_lookup(nd, &this, &next, &inode); 12731da177e4SLinus Torvalds if (err) 12741da177e4SLinus Torvalds break; 1275ac278a9cSAl Viro if (follow_on_final(inode, lookup_flags)) { 127631e6b01fSNick Piggin if (nameidata_dentry_drop_rcu_maybe(nd, next.dentry)) 127731e6b01fSNick Piggin return -ECHILD; 127831e6b01fSNick Piggin BUG_ON(inode != next.dentry->d_inode); 127990ebe565SAl Viro err = do_follow_link(&next, nd); 12801da177e4SLinus Torvalds if (err) 12811da177e4SLinus Torvalds goto return_err; 128231e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 128331e6b01fSNick Piggin } else { 128409dd17d3SMiklos Szeredi path_to_nameidata(&next, nd); 128531e6b01fSNick Piggin nd->inode = inode; 128631e6b01fSNick Piggin } 12871da177e4SLinus Torvalds err = -ENOENT; 128831e6b01fSNick Piggin if (!nd->inode) 12891da177e4SLinus Torvalds break; 12901da177e4SLinus Torvalds if (lookup_flags & LOOKUP_DIRECTORY) { 12911da177e4SLinus Torvalds err = -ENOTDIR; 129231e6b01fSNick Piggin if (!nd->inode->i_op->lookup) 12931da177e4SLinus Torvalds break; 12941da177e4SLinus Torvalds } 12951da177e4SLinus Torvalds goto return_base; 12961da177e4SLinus Torvalds lookup_parent: 12971da177e4SLinus Torvalds nd->last = this; 12981da177e4SLinus Torvalds nd->last_type = LAST_NORM; 12991da177e4SLinus Torvalds if (this.name[0] != '.') 13001da177e4SLinus Torvalds goto return_base; 13011da177e4SLinus Torvalds if (this.len == 1) 13021da177e4SLinus Torvalds nd->last_type = LAST_DOT; 13031da177e4SLinus Torvalds else if (this.len == 2 && this.name[1] == '.') 13041da177e4SLinus Torvalds nd->last_type = LAST_DOTDOT; 13051da177e4SLinus Torvalds else 13061da177e4SLinus Torvalds goto return_base; 13071da177e4SLinus Torvalds return_reval: 13081da177e4SLinus Torvalds /* 13091da177e4SLinus Torvalds * We bypassed the ordinary revalidation routines. 13101da177e4SLinus Torvalds * We may need to check the cached dentry for staleness. 13111da177e4SLinus Torvalds */ 1312fb045adbSNick Piggin if (need_reval_dot(nd->path.dentry)) { 13131da177e4SLinus Torvalds /* Note: we do not d_invalidate() */ 131434286d66SNick Piggin err = d_revalidate(nd->path.dentry, nd); 131534286d66SNick Piggin if (!err) 131634286d66SNick Piggin err = -ESTALE; 131734286d66SNick Piggin if (err < 0) 13181da177e4SLinus Torvalds break; 13191da177e4SLinus Torvalds } 13201da177e4SLinus Torvalds return_base: 132131e6b01fSNick Piggin if (nameidata_drop_rcu_last_maybe(nd)) 132231e6b01fSNick Piggin return -ECHILD; 13231da177e4SLinus Torvalds return 0; 13241da177e4SLinus Torvalds out_dput: 132531e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) 13261d957f9bSJan Blunck path_put_conditional(&next, nd); 13271da177e4SLinus Torvalds break; 13281da177e4SLinus Torvalds } 132931e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) 13301d957f9bSJan Blunck path_put(&nd->path); 13311da177e4SLinus Torvalds return_err: 13321da177e4SLinus Torvalds return err; 13331da177e4SLinus Torvalds } 13341da177e4SLinus Torvalds 133531e6b01fSNick Piggin static inline int path_walk_rcu(const char *name, struct nameidata *nd) 133631e6b01fSNick Piggin { 133731e6b01fSNick Piggin current->total_link_count = 0; 133831e6b01fSNick Piggin 133931e6b01fSNick Piggin return link_path_walk(name, nd); 134031e6b01fSNick Piggin } 134131e6b01fSNick Piggin 134231e6b01fSNick Piggin static inline int path_walk_simple(const char *name, struct nameidata *nd) 134331e6b01fSNick Piggin { 134431e6b01fSNick Piggin current->total_link_count = 0; 134531e6b01fSNick Piggin 134631e6b01fSNick Piggin return link_path_walk(name, nd); 134731e6b01fSNick Piggin } 134831e6b01fSNick Piggin 1349fc9b52cdSHarvey Harrison static int path_walk(const char *name, struct nameidata *nd) 13501da177e4SLinus Torvalds { 13516de88d72SAl Viro struct path save = nd->path; 13526de88d72SAl Viro int result; 13536de88d72SAl Viro 13541da177e4SLinus Torvalds current->total_link_count = 0; 13556de88d72SAl Viro 13566de88d72SAl Viro /* make sure the stuff we saved doesn't go away */ 13576de88d72SAl Viro path_get(&save); 13586de88d72SAl Viro 13596de88d72SAl Viro result = link_path_walk(name, nd); 13606de88d72SAl Viro if (result == -ESTALE) { 13616de88d72SAl Viro /* nd->path had been dropped */ 13626de88d72SAl Viro current->total_link_count = 0; 13636de88d72SAl Viro nd->path = save; 13646de88d72SAl Viro path_get(&nd->path); 13656de88d72SAl Viro nd->flags |= LOOKUP_REVAL; 13666de88d72SAl Viro result = link_path_walk(name, nd); 13676de88d72SAl Viro } 13686de88d72SAl Viro 13696de88d72SAl Viro path_put(&save); 13706de88d72SAl Viro 13716de88d72SAl Viro return result; 13721da177e4SLinus Torvalds } 13731da177e4SLinus Torvalds 137431e6b01fSNick Piggin static void path_finish_rcu(struct nameidata *nd) 137531e6b01fSNick Piggin { 137631e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 137731e6b01fSNick Piggin /* RCU dangling. Cancel it. */ 137831e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 137931e6b01fSNick Piggin nd->root.mnt = NULL; 138031e6b01fSNick Piggin rcu_read_unlock(); 138131e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 138231e6b01fSNick Piggin } 138331e6b01fSNick Piggin if (nd->file) 138431e6b01fSNick Piggin fput(nd->file); 138531e6b01fSNick Piggin } 138631e6b01fSNick Piggin 138731e6b01fSNick Piggin static int path_init_rcu(int dfd, const char *name, unsigned int flags, struct nameidata *nd) 138831e6b01fSNick Piggin { 138931e6b01fSNick Piggin int retval = 0; 139031e6b01fSNick Piggin int fput_needed; 139131e6b01fSNick Piggin struct file *file; 139231e6b01fSNick Piggin 139331e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 139431e6b01fSNick Piggin nd->flags = flags | LOOKUP_RCU; 139531e6b01fSNick Piggin nd->depth = 0; 139631e6b01fSNick Piggin nd->root.mnt = NULL; 139731e6b01fSNick Piggin nd->file = NULL; 139831e6b01fSNick Piggin 139931e6b01fSNick Piggin if (*name=='/') { 140031e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1401c28cc364SNick Piggin unsigned seq; 140231e6b01fSNick Piggin 140331e6b01fSNick Piggin br_read_lock(vfsmount_lock); 140431e6b01fSNick Piggin rcu_read_lock(); 140531e6b01fSNick Piggin 1406c28cc364SNick Piggin do { 1407c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 140831e6b01fSNick Piggin nd->root = fs->root; 140931e6b01fSNick Piggin nd->path = nd->root; 1410c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1411c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 141231e6b01fSNick Piggin 141331e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 141431e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1415c28cc364SNick Piggin unsigned seq; 141631e6b01fSNick Piggin 141731e6b01fSNick Piggin br_read_lock(vfsmount_lock); 141831e6b01fSNick Piggin rcu_read_lock(); 141931e6b01fSNick Piggin 1420c28cc364SNick Piggin do { 1421c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 142231e6b01fSNick Piggin nd->path = fs->pwd; 1423c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1424c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1425c28cc364SNick Piggin 142631e6b01fSNick Piggin } else { 142731e6b01fSNick Piggin struct dentry *dentry; 142831e6b01fSNick Piggin 142931e6b01fSNick Piggin file = fget_light(dfd, &fput_needed); 143031e6b01fSNick Piggin retval = -EBADF; 143131e6b01fSNick Piggin if (!file) 143231e6b01fSNick Piggin goto out_fail; 143331e6b01fSNick Piggin 143431e6b01fSNick Piggin dentry = file->f_path.dentry; 143531e6b01fSNick Piggin 143631e6b01fSNick Piggin retval = -ENOTDIR; 143731e6b01fSNick Piggin if (!S_ISDIR(dentry->d_inode->i_mode)) 143831e6b01fSNick Piggin goto fput_fail; 143931e6b01fSNick Piggin 144031e6b01fSNick Piggin retval = file_permission(file, MAY_EXEC); 144131e6b01fSNick Piggin if (retval) 144231e6b01fSNick Piggin goto fput_fail; 144331e6b01fSNick Piggin 144431e6b01fSNick Piggin nd->path = file->f_path; 144531e6b01fSNick Piggin if (fput_needed) 144631e6b01fSNick Piggin nd->file = file; 144731e6b01fSNick Piggin 1448c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 144931e6b01fSNick Piggin br_read_lock(vfsmount_lock); 145031e6b01fSNick Piggin rcu_read_lock(); 145131e6b01fSNick Piggin } 145231e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 145331e6b01fSNick Piggin return 0; 145431e6b01fSNick Piggin 145531e6b01fSNick Piggin fput_fail: 145631e6b01fSNick Piggin fput_light(file, fput_needed); 145731e6b01fSNick Piggin out_fail: 145831e6b01fSNick Piggin return retval; 145931e6b01fSNick Piggin } 146031e6b01fSNick Piggin 14619b4a9b14SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd) 14621da177e4SLinus Torvalds { 1463ea3834d9SPrasanna Meda int retval = 0; 1464170aa3d0SUlrich Drepper int fput_needed; 1465170aa3d0SUlrich Drepper struct file *file; 14661da177e4SLinus Torvalds 14671da177e4SLinus Torvalds nd->last_type = LAST_ROOT; /* if there are only slashes... */ 14681da177e4SLinus Torvalds nd->flags = flags; 14691da177e4SLinus Torvalds nd->depth = 0; 14702a737871SAl Viro nd->root.mnt = NULL; 14711da177e4SLinus Torvalds 14721da177e4SLinus Torvalds if (*name=='/') { 14732a737871SAl Viro set_root(nd); 14742a737871SAl Viro nd->path = nd->root; 14752a737871SAl Viro path_get(&nd->root); 14765590ff0dSUlrich Drepper } else if (dfd == AT_FDCWD) { 1477f7ad3c6bSMiklos Szeredi get_fs_pwd(current->fs, &nd->path); 14785590ff0dSUlrich Drepper } else { 14795590ff0dSUlrich Drepper struct dentry *dentry; 14805590ff0dSUlrich Drepper 14815590ff0dSUlrich Drepper file = fget_light(dfd, &fput_needed); 14825590ff0dSUlrich Drepper retval = -EBADF; 1483170aa3d0SUlrich Drepper if (!file) 14846d09bb62STrond Myklebust goto out_fail; 14855590ff0dSUlrich Drepper 14860f7fc9e4SJosef "Jeff" Sipek dentry = file->f_path.dentry; 14875590ff0dSUlrich Drepper 14885590ff0dSUlrich Drepper retval = -ENOTDIR; 1489170aa3d0SUlrich Drepper if (!S_ISDIR(dentry->d_inode->i_mode)) 14906d09bb62STrond Myklebust goto fput_fail; 14915590ff0dSUlrich Drepper 14925590ff0dSUlrich Drepper retval = file_permission(file, MAY_EXEC); 1493170aa3d0SUlrich Drepper if (retval) 14946d09bb62STrond Myklebust goto fput_fail; 14955590ff0dSUlrich Drepper 14965dd784d0SJan Blunck nd->path = file->f_path; 14975dd784d0SJan Blunck path_get(&file->f_path); 14985590ff0dSUlrich Drepper 14995590ff0dSUlrich Drepper fput_light(file, fput_needed); 15001da177e4SLinus Torvalds } 150131e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 15029b4a9b14SAl Viro return 0; 15032dfdd266SJosef 'Jeff' Sipek 15049b4a9b14SAl Viro fput_fail: 15059b4a9b14SAl Viro fput_light(file, fput_needed); 15069b4a9b14SAl Viro out_fail: 15079b4a9b14SAl Viro return retval; 15089b4a9b14SAl Viro } 15099b4a9b14SAl Viro 15109b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 15119b4a9b14SAl Viro static int do_path_lookup(int dfd, const char *name, 15129b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 15139b4a9b14SAl Viro { 151431e6b01fSNick Piggin int retval; 151531e6b01fSNick Piggin 151631e6b01fSNick Piggin /* 151731e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 151831e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 151931e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 152031e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 152131e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 152231e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 152331e6b01fSNick Piggin * analogue, foo_rcu(). 152431e6b01fSNick Piggin * 152531e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 152631e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 152731e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 152831e6b01fSNick Piggin * be able to complete). 152931e6b01fSNick Piggin */ 153031e6b01fSNick Piggin retval = path_init_rcu(dfd, name, flags, nd); 153131e6b01fSNick Piggin if (unlikely(retval)) 153231e6b01fSNick Piggin return retval; 153331e6b01fSNick Piggin retval = path_walk_rcu(name, nd); 153431e6b01fSNick Piggin path_finish_rcu(nd); 15352a737871SAl Viro if (nd->root.mnt) { 15362a737871SAl Viro path_put(&nd->root); 15372a737871SAl Viro nd->root.mnt = NULL; 15382a737871SAl Viro } 153931e6b01fSNick Piggin 154031e6b01fSNick Piggin if (unlikely(retval == -ECHILD || retval == -ESTALE)) { 154131e6b01fSNick Piggin /* slower, locked walk */ 154231e6b01fSNick Piggin if (retval == -ESTALE) 154331e6b01fSNick Piggin flags |= LOOKUP_REVAL; 154431e6b01fSNick Piggin retval = path_init(dfd, name, flags, nd); 154531e6b01fSNick Piggin if (unlikely(retval)) 154631e6b01fSNick Piggin return retval; 154731e6b01fSNick Piggin retval = path_walk(name, nd); 154831e6b01fSNick Piggin if (nd->root.mnt) { 154931e6b01fSNick Piggin path_put(&nd->root); 155031e6b01fSNick Piggin nd->root.mnt = NULL; 155131e6b01fSNick Piggin } 155231e6b01fSNick Piggin } 155331e6b01fSNick Piggin 155431e6b01fSNick Piggin if (likely(!retval)) { 155531e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) { 155631e6b01fSNick Piggin if (nd->path.dentry && nd->inode) 155731e6b01fSNick Piggin audit_inode(name, nd->path.dentry); 155831e6b01fSNick Piggin } 155931e6b01fSNick Piggin } 156031e6b01fSNick Piggin 1561170aa3d0SUlrich Drepper return retval; 15621da177e4SLinus Torvalds } 15631da177e4SLinus Torvalds 1564fc9b52cdSHarvey Harrison int path_lookup(const char *name, unsigned int flags, 15655590ff0dSUlrich Drepper struct nameidata *nd) 15665590ff0dSUlrich Drepper { 15675590ff0dSUlrich Drepper return do_path_lookup(AT_FDCWD, name, flags, nd); 15685590ff0dSUlrich Drepper } 15695590ff0dSUlrich Drepper 1570d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1571d1811465SAl Viro { 1572d1811465SAl Viro struct nameidata nd; 1573d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1574d1811465SAl Viro if (!res) 1575d1811465SAl Viro *path = nd.path; 1576d1811465SAl Viro return res; 1577d1811465SAl Viro } 1578d1811465SAl Viro 157916f18200SJosef 'Jeff' Sipek /** 158016f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 158116f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 158216f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 158316f18200SJosef 'Jeff' Sipek * @name: pointer to file name 158416f18200SJosef 'Jeff' Sipek * @flags: lookup flags 158516f18200SJosef 'Jeff' Sipek * @nd: pointer to nameidata 158616f18200SJosef 'Jeff' Sipek */ 158716f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 158816f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 158916f18200SJosef 'Jeff' Sipek struct nameidata *nd) 159016f18200SJosef 'Jeff' Sipek { 159116f18200SJosef 'Jeff' Sipek int retval; 159216f18200SJosef 'Jeff' Sipek 159316f18200SJosef 'Jeff' Sipek /* same as do_path_lookup */ 159416f18200SJosef 'Jeff' Sipek nd->last_type = LAST_ROOT; 159516f18200SJosef 'Jeff' Sipek nd->flags = flags; 159616f18200SJosef 'Jeff' Sipek nd->depth = 0; 159716f18200SJosef 'Jeff' Sipek 1598c8e7f449SJan Blunck nd->path.dentry = dentry; 1599c8e7f449SJan Blunck nd->path.mnt = mnt; 1600c8e7f449SJan Blunck path_get(&nd->path); 16015b857119SAl Viro nd->root = nd->path; 16025b857119SAl Viro path_get(&nd->root); 160331e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 160416f18200SJosef 'Jeff' Sipek 160516f18200SJosef 'Jeff' Sipek retval = path_walk(name, nd); 16064ac91378SJan Blunck if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry && 160731e6b01fSNick Piggin nd->inode)) 16084ac91378SJan Blunck audit_inode(name, nd->path.dentry); 160916f18200SJosef 'Jeff' Sipek 16102a737871SAl Viro path_put(&nd->root); 16112a737871SAl Viro nd->root.mnt = NULL; 161216f18200SJosef 'Jeff' Sipek 16132a737871SAl Viro return retval; 161416f18200SJosef 'Jeff' Sipek } 161516f18200SJosef 'Jeff' Sipek 1616eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name, 1617eead1911SChristoph Hellwig struct dentry *base, struct nameidata *nd) 16181da177e4SLinus Torvalds { 161981fca444SChristoph Hellwig struct inode *inode = base->d_inode; 16201da177e4SLinus Torvalds struct dentry *dentry; 16211da177e4SLinus Torvalds int err; 16221da177e4SLinus Torvalds 162381fca444SChristoph Hellwig err = exec_permission(inode); 162481fca444SChristoph Hellwig if (err) 162581fca444SChristoph Hellwig return ERR_PTR(err); 16261da177e4SLinus Torvalds 16271da177e4SLinus Torvalds /* 16281da177e4SLinus Torvalds * See if the low-level filesystem might want 16291da177e4SLinus Torvalds * to use its own hash.. 16301da177e4SLinus Torvalds */ 1631fb045adbSNick Piggin if (base->d_flags & DCACHE_OP_HASH) { 1632b1e6a015SNick Piggin err = base->d_op->d_hash(base, inode, name); 16331da177e4SLinus Torvalds dentry = ERR_PTR(err); 16341da177e4SLinus Torvalds if (err < 0) 16351da177e4SLinus Torvalds goto out; 16361da177e4SLinus Torvalds } 16371da177e4SLinus Torvalds 1638b04f784eSNick Piggin /* 1639b04f784eSNick Piggin * Don't bother with __d_lookup: callers are for creat as 1640b04f784eSNick Piggin * well as unlink, so a lot of the time it would cost 1641b04f784eSNick Piggin * a double lookup. 16426e6b1bd1SAl Viro */ 16436e6b1bd1SAl Viro dentry = d_lookup(base, name); 16446e6b1bd1SAl Viro 1645fb045adbSNick Piggin if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) 16466e6b1bd1SAl Viro dentry = do_revalidate(dentry, nd); 16476e6b1bd1SAl Viro 16481da177e4SLinus Torvalds if (!dentry) 1649baa03890SNick Piggin dentry = d_alloc_and_lookup(base, name, nd); 16501da177e4SLinus Torvalds out: 16511da177e4SLinus Torvalds return dentry; 16521da177e4SLinus Torvalds } 16531da177e4SLinus Torvalds 1654057f6c01SJames Morris /* 1655057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1656057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1657057f6c01SJames Morris * SMP-safe. 1658057f6c01SJames Morris */ 1659a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 16601da177e4SLinus Torvalds { 16614ac91378SJan Blunck return __lookup_hash(&nd->last, nd->path.dentry, nd); 16621da177e4SLinus Torvalds } 16631da177e4SLinus Torvalds 1664eead1911SChristoph Hellwig static int __lookup_one_len(const char *name, struct qstr *this, 1665eead1911SChristoph Hellwig struct dentry *base, int len) 16661da177e4SLinus Torvalds { 16671da177e4SLinus Torvalds unsigned long hash; 16681da177e4SLinus Torvalds unsigned int c; 16691da177e4SLinus Torvalds 1670057f6c01SJames Morris this->name = name; 1671057f6c01SJames Morris this->len = len; 16721da177e4SLinus Torvalds if (!len) 1673057f6c01SJames Morris return -EACCES; 16741da177e4SLinus Torvalds 16751da177e4SLinus Torvalds hash = init_name_hash(); 16761da177e4SLinus Torvalds while (len--) { 16771da177e4SLinus Torvalds c = *(const unsigned char *)name++; 16781da177e4SLinus Torvalds if (c == '/' || c == '\0') 1679057f6c01SJames Morris return -EACCES; 16801da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 16811da177e4SLinus Torvalds } 1682057f6c01SJames Morris this->hash = end_name_hash(hash); 1683057f6c01SJames Morris return 0; 1684057f6c01SJames Morris } 16851da177e4SLinus Torvalds 1686eead1911SChristoph Hellwig /** 1687a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1688eead1911SChristoph Hellwig * @name: pathname component to lookup 1689eead1911SChristoph Hellwig * @base: base directory to lookup from 1690eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1691eead1911SChristoph Hellwig * 1692a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1693a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1694eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1695eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1696eead1911SChristoph Hellwig */ 1697057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1698057f6c01SJames Morris { 1699057f6c01SJames Morris int err; 1700057f6c01SJames Morris struct qstr this; 1701057f6c01SJames Morris 17022f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 17032f9092e1SDavid Woodhouse 1704057f6c01SJames Morris err = __lookup_one_len(name, &this, base, len); 1705057f6c01SJames Morris if (err) 1706057f6c01SJames Morris return ERR_PTR(err); 1707eead1911SChristoph Hellwig 170849705b77SChristoph Hellwig return __lookup_hash(&this, base, NULL); 1709057f6c01SJames Morris } 1710057f6c01SJames Morris 17112d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags, 17122d8f3038SAl Viro struct path *path) 17131da177e4SLinus Torvalds { 17142d8f3038SAl Viro struct nameidata nd; 17151da177e4SLinus Torvalds char *tmp = getname(name); 17161da177e4SLinus Torvalds int err = PTR_ERR(tmp); 17171da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 17182d8f3038SAl Viro 17192d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 17202d8f3038SAl Viro 17212d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 17221da177e4SLinus Torvalds putname(tmp); 17232d8f3038SAl Viro if (!err) 17242d8f3038SAl Viro *path = nd.path; 17251da177e4SLinus Torvalds } 17261da177e4SLinus Torvalds return err; 17271da177e4SLinus Torvalds } 17281da177e4SLinus Torvalds 17292ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 17302ad94ae6SAl Viro struct nameidata *nd, char **name) 17312ad94ae6SAl Viro { 17322ad94ae6SAl Viro char *s = getname(path); 17332ad94ae6SAl Viro int error; 17342ad94ae6SAl Viro 17352ad94ae6SAl Viro if (IS_ERR(s)) 17362ad94ae6SAl Viro return PTR_ERR(s); 17372ad94ae6SAl Viro 17382ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 17392ad94ae6SAl Viro if (error) 17402ad94ae6SAl Viro putname(s); 17412ad94ae6SAl Viro else 17422ad94ae6SAl Viro *name = s; 17432ad94ae6SAl Viro 17442ad94ae6SAl Viro return error; 17452ad94ae6SAl Viro } 17462ad94ae6SAl Viro 17471da177e4SLinus Torvalds /* 17481da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 17491da177e4SLinus Torvalds * minimal. 17501da177e4SLinus Torvalds */ 17511da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 17521da177e4SLinus Torvalds { 1753da9592edSDavid Howells uid_t fsuid = current_fsuid(); 1754da9592edSDavid Howells 17551da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 17561da177e4SLinus Torvalds return 0; 1757da9592edSDavid Howells if (inode->i_uid == fsuid) 17581da177e4SLinus Torvalds return 0; 1759da9592edSDavid Howells if (dir->i_uid == fsuid) 17601da177e4SLinus Torvalds return 0; 17611da177e4SLinus Torvalds return !capable(CAP_FOWNER); 17621da177e4SLinus Torvalds } 17631da177e4SLinus Torvalds 17641da177e4SLinus Torvalds /* 17651da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 17661da177e4SLinus Torvalds * whether the type of victim is right. 17671da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 17681da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 17691da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 17701da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 17711da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 17721da177e4SLinus Torvalds * a. be owner of dir, or 17731da177e4SLinus Torvalds * b. be owner of victim, or 17741da177e4SLinus Torvalds * c. have CAP_FOWNER capability 17751da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 17761da177e4SLinus Torvalds * links pointing to it. 17771da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 17781da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 17791da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 17801da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 17811da177e4SLinus Torvalds * nfs_async_unlink(). 17821da177e4SLinus Torvalds */ 1783858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 17841da177e4SLinus Torvalds { 17851da177e4SLinus Torvalds int error; 17861da177e4SLinus Torvalds 17871da177e4SLinus Torvalds if (!victim->d_inode) 17881da177e4SLinus Torvalds return -ENOENT; 17891da177e4SLinus Torvalds 17901da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 1791cccc6bbaSAl Viro audit_inode_child(victim, dir); 17921da177e4SLinus Torvalds 1793f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 17941da177e4SLinus Torvalds if (error) 17951da177e4SLinus Torvalds return error; 17961da177e4SLinus Torvalds if (IS_APPEND(dir)) 17971da177e4SLinus Torvalds return -EPERM; 17981da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 1799f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 18001da177e4SLinus Torvalds return -EPERM; 18011da177e4SLinus Torvalds if (isdir) { 18021da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 18031da177e4SLinus Torvalds return -ENOTDIR; 18041da177e4SLinus Torvalds if (IS_ROOT(victim)) 18051da177e4SLinus Torvalds return -EBUSY; 18061da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 18071da177e4SLinus Torvalds return -EISDIR; 18081da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 18091da177e4SLinus Torvalds return -ENOENT; 18101da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 18111da177e4SLinus Torvalds return -EBUSY; 18121da177e4SLinus Torvalds return 0; 18131da177e4SLinus Torvalds } 18141da177e4SLinus Torvalds 18151da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 18161da177e4SLinus Torvalds * dir. 18171da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 18181da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 18191da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 18201da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 18211da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 18221da177e4SLinus Torvalds */ 1823a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 18241da177e4SLinus Torvalds { 18251da177e4SLinus Torvalds if (child->d_inode) 18261da177e4SLinus Torvalds return -EEXIST; 18271da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 18281da177e4SLinus Torvalds return -ENOENT; 1829f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 18301da177e4SLinus Torvalds } 18311da177e4SLinus Torvalds 18321da177e4SLinus Torvalds /* 18331da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 18341da177e4SLinus Torvalds */ 18351da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 18361da177e4SLinus Torvalds { 18371da177e4SLinus Torvalds struct dentry *p; 18381da177e4SLinus Torvalds 18391da177e4SLinus Torvalds if (p1 == p2) { 1840f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 18411da177e4SLinus Torvalds return NULL; 18421da177e4SLinus Torvalds } 18431da177e4SLinus Torvalds 1844a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 18451da177e4SLinus Torvalds 1846e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 1847e2761a11SOGAWA Hirofumi if (p) { 1848f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 1849f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 18501da177e4SLinus Torvalds return p; 18511da177e4SLinus Torvalds } 18521da177e4SLinus Torvalds 1853e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 1854e2761a11SOGAWA Hirofumi if (p) { 1855f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1856f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 18571da177e4SLinus Torvalds return p; 18581da177e4SLinus Torvalds } 18591da177e4SLinus Torvalds 1860f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1861f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 18621da177e4SLinus Torvalds return NULL; 18631da177e4SLinus Torvalds } 18641da177e4SLinus Torvalds 18651da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 18661da177e4SLinus Torvalds { 18671b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 18681da177e4SLinus Torvalds if (p1 != p2) { 18691b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 1870a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 18711da177e4SLinus Torvalds } 18721da177e4SLinus Torvalds } 18731da177e4SLinus Torvalds 18741da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode, 18751da177e4SLinus Torvalds struct nameidata *nd) 18761da177e4SLinus Torvalds { 1877a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 18781da177e4SLinus Torvalds 18791da177e4SLinus Torvalds if (error) 18801da177e4SLinus Torvalds return error; 18811da177e4SLinus Torvalds 1882acfa4380SAl Viro if (!dir->i_op->create) 18831da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 18841da177e4SLinus Torvalds mode &= S_IALLUGO; 18851da177e4SLinus Torvalds mode |= S_IFREG; 18861da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 18871da177e4SLinus Torvalds if (error) 18881da177e4SLinus Torvalds return error; 18891da177e4SLinus Torvalds error = dir->i_op->create(dir, dentry, mode, nd); 1890a74574aaSStephen Smalley if (!error) 1891f38aa942SAmy Griffis fsnotify_create(dir, dentry); 18921da177e4SLinus Torvalds return error; 18931da177e4SLinus Torvalds } 18941da177e4SLinus Torvalds 18953fb64190SChristoph Hellwig int may_open(struct path *path, int acc_mode, int flag) 18961da177e4SLinus Torvalds { 18973fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 18981da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 18991da177e4SLinus Torvalds int error; 19001da177e4SLinus Torvalds 19011da177e4SLinus Torvalds if (!inode) 19021da177e4SLinus Torvalds return -ENOENT; 19031da177e4SLinus Torvalds 1904c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 1905c8fe8f30SChristoph Hellwig case S_IFLNK: 19061da177e4SLinus Torvalds return -ELOOP; 1907c8fe8f30SChristoph Hellwig case S_IFDIR: 1908c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 19091da177e4SLinus Torvalds return -EISDIR; 1910c8fe8f30SChristoph Hellwig break; 1911c8fe8f30SChristoph Hellwig case S_IFBLK: 1912c8fe8f30SChristoph Hellwig case S_IFCHR: 19133fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 19141da177e4SLinus Torvalds return -EACCES; 1915c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 1916c8fe8f30SChristoph Hellwig case S_IFIFO: 1917c8fe8f30SChristoph Hellwig case S_IFSOCK: 19181da177e4SLinus Torvalds flag &= ~O_TRUNC; 1919c8fe8f30SChristoph Hellwig break; 19204a3fd211SDave Hansen } 1921b41572e9SDave Hansen 19223fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 1923b41572e9SDave Hansen if (error) 1924b41572e9SDave Hansen return error; 19256146f0d5SMimi Zohar 19261da177e4SLinus Torvalds /* 19271da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 19281da177e4SLinus Torvalds */ 19291da177e4SLinus Torvalds if (IS_APPEND(inode)) { 19308737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 19317715b521SAl Viro return -EPERM; 19321da177e4SLinus Torvalds if (flag & O_TRUNC) 19337715b521SAl Viro return -EPERM; 19341da177e4SLinus Torvalds } 19351da177e4SLinus Torvalds 19361da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 19377715b521SAl Viro if (flag & O_NOATIME && !is_owner_or_cap(inode)) 19387715b521SAl Viro return -EPERM; 19391da177e4SLinus Torvalds 19401da177e4SLinus Torvalds /* 19411da177e4SLinus Torvalds * Ensure there are no outstanding leases on the file. 19421da177e4SLinus Torvalds */ 1943b65a9cfcSAl Viro return break_lease(inode, flag); 19447715b521SAl Viro } 19457715b521SAl Viro 19467715b521SAl Viro static int handle_truncate(struct path *path) 19477715b521SAl Viro { 19487715b521SAl Viro struct inode *inode = path->dentry->d_inode; 19497715b521SAl Viro int error = get_write_access(inode); 19501da177e4SLinus Torvalds if (error) 19517715b521SAl Viro return error; 19521da177e4SLinus Torvalds /* 19531da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 19541da177e4SLinus Torvalds */ 19551da177e4SLinus Torvalds error = locks_verify_locked(inode); 1956be6d3e56SKentaro Takeda if (!error) 1957ea0d3ab2STetsuo Handa error = security_path_truncate(path); 19581da177e4SLinus Torvalds if (!error) { 19597715b521SAl Viro error = do_truncate(path->dentry, 0, 1960d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 1961d139d7ffSMiklos Szeredi NULL); 19621da177e4SLinus Torvalds } 19631da177e4SLinus Torvalds put_write_access(inode); 1964acd0c935SMimi Zohar return error; 19651da177e4SLinus Torvalds } 19661da177e4SLinus Torvalds 1967d57999e1SDave Hansen /* 1968d57999e1SDave Hansen * Be careful about ever adding any more callers of this 1969d57999e1SDave Hansen * function. Its flags must be in the namei format, not 1970d57999e1SDave Hansen * what get passed to sys_open(). 1971d57999e1SDave Hansen */ 1972d57999e1SDave Hansen static int __open_namei_create(struct nameidata *nd, struct path *path, 19738737c930SAl Viro int open_flag, int mode) 1974aab520e2SDave Hansen { 1975aab520e2SDave Hansen int error; 19764ac91378SJan Blunck struct dentry *dir = nd->path.dentry; 1977aab520e2SDave Hansen 1978aab520e2SDave Hansen if (!IS_POSIXACL(dir->d_inode)) 1979ce3b0f8dSAl Viro mode &= ~current_umask(); 1980be6d3e56SKentaro Takeda error = security_path_mknod(&nd->path, path->dentry, mode, 0); 1981be6d3e56SKentaro Takeda if (error) 1982be6d3e56SKentaro Takeda goto out_unlock; 1983aab520e2SDave Hansen error = vfs_create(dir->d_inode, path->dentry, mode, nd); 1984be6d3e56SKentaro Takeda out_unlock: 1985aab520e2SDave Hansen mutex_unlock(&dir->d_inode->i_mutex); 19864ac91378SJan Blunck dput(nd->path.dentry); 19874ac91378SJan Blunck nd->path.dentry = path->dentry; 198831e6b01fSNick Piggin 1989aab520e2SDave Hansen if (error) 1990aab520e2SDave Hansen return error; 1991aab520e2SDave Hansen /* Don't check for write permission, don't truncate */ 19928737c930SAl Viro return may_open(&nd->path, 0, open_flag & ~O_TRUNC); 1993aab520e2SDave Hansen } 1994aab520e2SDave Hansen 19951da177e4SLinus Torvalds /* 1996d57999e1SDave Hansen * Note that while the flag value (low two bits) for sys_open means: 1997d57999e1SDave Hansen * 00 - read-only 1998d57999e1SDave Hansen * 01 - write-only 1999d57999e1SDave Hansen * 10 - read-write 2000d57999e1SDave Hansen * 11 - special 2001d57999e1SDave Hansen * it is changed into 2002d57999e1SDave Hansen * 00 - no permissions needed 2003d57999e1SDave Hansen * 01 - read-permission 2004d57999e1SDave Hansen * 10 - write-permission 2005d57999e1SDave Hansen * 11 - read-write 2006d57999e1SDave Hansen * for the internal routines (ie open_namei()/follow_link() etc) 2007d57999e1SDave Hansen * This is more logical, and also allows the 00 "no perm needed" 2008d57999e1SDave Hansen * to be used for symlinks (where the permissions are checked 2009d57999e1SDave Hansen * later). 2010d57999e1SDave Hansen * 2011d57999e1SDave Hansen */ 2012d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2013d57999e1SDave Hansen { 2014d57999e1SDave Hansen if ((flag+1) & O_ACCMODE) 2015d57999e1SDave Hansen flag++; 2016d57999e1SDave Hansen return flag; 2017d57999e1SDave Hansen } 2018d57999e1SDave Hansen 20197715b521SAl Viro static int open_will_truncate(int flag, struct inode *inode) 20204a3fd211SDave Hansen { 2021d57999e1SDave Hansen /* 20224a3fd211SDave Hansen * We'll never write to the fs underlying 20234a3fd211SDave Hansen * a device file. 20244a3fd211SDave Hansen */ 20254a3fd211SDave Hansen if (special_file(inode->i_mode)) 20264a3fd211SDave Hansen return 0; 20274a3fd211SDave Hansen return (flag & O_TRUNC); 20284a3fd211SDave Hansen } 20294a3fd211SDave Hansen 2030648fa861SAl Viro static struct file *finish_open(struct nameidata *nd, 20319a66179eSAl Viro int open_flag, int acc_mode) 2032648fa861SAl Viro { 2033648fa861SAl Viro struct file *filp; 2034648fa861SAl Viro int will_truncate; 2035648fa861SAl Viro int error; 2036648fa861SAl Viro 20379a66179eSAl Viro will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode); 2038648fa861SAl Viro if (will_truncate) { 2039648fa861SAl Viro error = mnt_want_write(nd->path.mnt); 2040648fa861SAl Viro if (error) 2041648fa861SAl Viro goto exit; 2042648fa861SAl Viro } 2043648fa861SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2044648fa861SAl Viro if (error) { 2045648fa861SAl Viro if (will_truncate) 2046648fa861SAl Viro mnt_drop_write(nd->path.mnt); 2047648fa861SAl Viro goto exit; 2048648fa861SAl Viro } 2049648fa861SAl Viro filp = nameidata_to_filp(nd); 2050648fa861SAl Viro if (!IS_ERR(filp)) { 2051648fa861SAl Viro error = ima_file_check(filp, acc_mode); 2052648fa861SAl Viro if (error) { 2053648fa861SAl Viro fput(filp); 2054648fa861SAl Viro filp = ERR_PTR(error); 2055648fa861SAl Viro } 2056648fa861SAl Viro } 2057648fa861SAl Viro if (!IS_ERR(filp)) { 2058648fa861SAl Viro if (will_truncate) { 2059648fa861SAl Viro error = handle_truncate(&nd->path); 2060648fa861SAl Viro if (error) { 2061648fa861SAl Viro fput(filp); 2062648fa861SAl Viro filp = ERR_PTR(error); 2063648fa861SAl Viro } 2064648fa861SAl Viro } 2065648fa861SAl Viro } 2066648fa861SAl Viro /* 2067648fa861SAl Viro * It is now safe to drop the mnt write 2068648fa861SAl Viro * because the filp has had a write taken 2069648fa861SAl Viro * on its behalf. 2070648fa861SAl Viro */ 2071648fa861SAl Viro if (will_truncate) 2072648fa861SAl Viro mnt_drop_write(nd->path.mnt); 2073d893f1bcSAl Viro path_put(&nd->path); 2074648fa861SAl Viro return filp; 2075648fa861SAl Viro 2076648fa861SAl Viro exit: 2077648fa861SAl Viro if (!IS_ERR(nd->intent.open.file)) 2078648fa861SAl Viro release_open_intent(nd); 2079648fa861SAl Viro path_put(&nd->path); 2080648fa861SAl Viro return ERR_PTR(error); 2081648fa861SAl Viro } 2082648fa861SAl Viro 208331e6b01fSNick Piggin /* 208431e6b01fSNick Piggin * Handle O_CREAT case for do_filp_open 208531e6b01fSNick Piggin */ 2086fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path, 20875b369df8SAl Viro int open_flag, int acc_mode, 20883e297b61SAl Viro int mode, const char *pathname) 2089fb1cc555SAl Viro { 2090a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 2091fb1cc555SAl Viro struct file *filp; 20921f36f774SAl Viro int error = -EISDIR; 2093fb1cc555SAl Viro 20941f36f774SAl Viro switch (nd->last_type) { 20951f36f774SAl Viro case LAST_DOTDOT: 20961f36f774SAl Viro follow_dotdot(nd); 20971f36f774SAl Viro dir = nd->path.dentry; 2098176306f5SNeil Brown case LAST_DOT: 2099fb045adbSNick Piggin if (need_reval_dot(dir)) { 210034286d66SNick Piggin error = d_revalidate(nd->path.dentry, nd); 210134286d66SNick Piggin if (!error) 21021f36f774SAl Viro error = -ESTALE; 210334286d66SNick Piggin if (error < 0) 2104a2c36b45SAl Viro goto exit; 21051f36f774SAl Viro } 21061f36f774SAl Viro /* fallthrough */ 21071f36f774SAl Viro case LAST_ROOT: 21081f36f774SAl Viro goto exit; 21091f36f774SAl Viro case LAST_BIND: 21101f36f774SAl Viro audit_inode(pathname, dir); 21111f36f774SAl Viro goto ok; 21121f36f774SAl Viro } 2113a2c36b45SAl Viro 21141f36f774SAl Viro /* trailing slashes? */ 211531e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 21161f36f774SAl Viro goto exit; 21171f36f774SAl Viro 2118a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 2119a1e28038SAl Viro 2120a1e28038SAl Viro path->dentry = lookup_hash(nd); 2121a1e28038SAl Viro path->mnt = nd->path.mnt; 2122a1e28038SAl Viro 2123fb1cc555SAl Viro error = PTR_ERR(path->dentry); 2124fb1cc555SAl Viro if (IS_ERR(path->dentry)) { 2125fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2126fb1cc555SAl Viro goto exit; 2127fb1cc555SAl Viro } 2128fb1cc555SAl Viro 2129fb1cc555SAl Viro if (IS_ERR(nd->intent.open.file)) { 2130fb1cc555SAl Viro error = PTR_ERR(nd->intent.open.file); 2131fb1cc555SAl Viro goto exit_mutex_unlock; 2132fb1cc555SAl Viro } 2133fb1cc555SAl Viro 2134fb1cc555SAl Viro /* Negative dentry, just create the file */ 2135fb1cc555SAl Viro if (!path->dentry->d_inode) { 2136fb1cc555SAl Viro /* 2137fb1cc555SAl Viro * This write is needed to ensure that a 2138fb1cc555SAl Viro * ro->rw transition does not occur between 2139fb1cc555SAl Viro * the time when the file is created and when 2140fb1cc555SAl Viro * a permanent write count is taken through 2141fb1cc555SAl Viro * the 'struct file' in nameidata_to_filp(). 2142fb1cc555SAl Viro */ 2143fb1cc555SAl Viro error = mnt_want_write(nd->path.mnt); 2144fb1cc555SAl Viro if (error) 2145fb1cc555SAl Viro goto exit_mutex_unlock; 2146fb1cc555SAl Viro error = __open_namei_create(nd, path, open_flag, mode); 2147fb1cc555SAl Viro if (error) { 2148fb1cc555SAl Viro mnt_drop_write(nd->path.mnt); 2149fb1cc555SAl Viro goto exit; 2150fb1cc555SAl Viro } 2151fb1cc555SAl Viro filp = nameidata_to_filp(nd); 2152fb1cc555SAl Viro mnt_drop_write(nd->path.mnt); 2153d893f1bcSAl Viro path_put(&nd->path); 2154fb1cc555SAl Viro if (!IS_ERR(filp)) { 2155fb1cc555SAl Viro error = ima_file_check(filp, acc_mode); 2156fb1cc555SAl Viro if (error) { 2157fb1cc555SAl Viro fput(filp); 2158fb1cc555SAl Viro filp = ERR_PTR(error); 2159fb1cc555SAl Viro } 2160fb1cc555SAl Viro } 2161fb1cc555SAl Viro return filp; 2162fb1cc555SAl Viro } 2163fb1cc555SAl Viro 2164fb1cc555SAl Viro /* 2165fb1cc555SAl Viro * It already exists. 2166fb1cc555SAl Viro */ 2167fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2168fb1cc555SAl Viro audit_inode(pathname, path->dentry); 2169fb1cc555SAl Viro 2170fb1cc555SAl Viro error = -EEXIST; 21715b369df8SAl Viro if (open_flag & O_EXCL) 2172fb1cc555SAl Viro goto exit_dput; 2173fb1cc555SAl Viro 2174fb1cc555SAl Viro if (__follow_mount(path)) { 2175fb1cc555SAl Viro error = -ELOOP; 21765b369df8SAl Viro if (open_flag & O_NOFOLLOW) 2177fb1cc555SAl Viro goto exit_dput; 2178fb1cc555SAl Viro } 2179fb1cc555SAl Viro 2180fb1cc555SAl Viro error = -ENOENT; 2181fb1cc555SAl Viro if (!path->dentry->d_inode) 2182fb1cc555SAl Viro goto exit_dput; 21839e67f361SAl Viro 21849e67f361SAl Viro if (path->dentry->d_inode->i_op->follow_link) 2185fb1cc555SAl Viro return NULL; 2186fb1cc555SAl Viro 2187fb1cc555SAl Viro path_to_nameidata(path, nd); 218831e6b01fSNick Piggin nd->inode = path->dentry->d_inode; 2189fb1cc555SAl Viro error = -EISDIR; 219031e6b01fSNick Piggin if (S_ISDIR(nd->inode->i_mode)) 2191fb1cc555SAl Viro goto exit; 219267ee3ad2SAl Viro ok: 21939a66179eSAl Viro filp = finish_open(nd, open_flag, acc_mode); 2194fb1cc555SAl Viro return filp; 2195fb1cc555SAl Viro 2196fb1cc555SAl Viro exit_mutex_unlock: 2197fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2198fb1cc555SAl Viro exit_dput: 2199fb1cc555SAl Viro path_put_conditional(path, nd); 2200fb1cc555SAl Viro exit: 2201fb1cc555SAl Viro if (!IS_ERR(nd->intent.open.file)) 2202fb1cc555SAl Viro release_open_intent(nd); 2203fb1cc555SAl Viro path_put(&nd->path); 2204fb1cc555SAl Viro return ERR_PTR(error); 2205fb1cc555SAl Viro } 2206fb1cc555SAl Viro 22074a3fd211SDave Hansen /* 22084a3fd211SDave Hansen * Note that the low bits of the passed in "open_flag" 22094a3fd211SDave Hansen * are not the same as in the local variable "flag". See 22104a3fd211SDave Hansen * open_to_namei_flags() for more details. 22111da177e4SLinus Torvalds */ 2212a70e65dfSChristoph Hellwig struct file *do_filp_open(int dfd, const char *pathname, 22136e8341a1SAl Viro int open_flag, int mode, int acc_mode) 22141da177e4SLinus Torvalds { 22154a3fd211SDave Hansen struct file *filp; 2216a70e65dfSChristoph Hellwig struct nameidata nd; 22176e8341a1SAl Viro int error; 22189850c056SAl Viro struct path path; 22191da177e4SLinus Torvalds int count = 0; 2220d57999e1SDave Hansen int flag = open_to_namei_flags(open_flag); 222131e6b01fSNick Piggin int flags; 22221f36f774SAl Viro 22231f36f774SAl Viro if (!(open_flag & O_CREAT)) 22241f36f774SAl Viro mode = 0; 22251da177e4SLinus Torvalds 2226b1085ba8SLino Sanfilippo /* Must never be set by userspace */ 2227b1085ba8SLino Sanfilippo open_flag &= ~FMODE_NONOTIFY; 2228b1085ba8SLino Sanfilippo 22296b2f3d1fSChristoph Hellwig /* 22306b2f3d1fSChristoph Hellwig * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only 22316b2f3d1fSChristoph Hellwig * check for O_DSYNC if the need any syncing at all we enforce it's 22326b2f3d1fSChristoph Hellwig * always set instead of having to deal with possibly weird behaviour 22336b2f3d1fSChristoph Hellwig * for malicious applications setting only __O_SYNC. 22346b2f3d1fSChristoph Hellwig */ 22356b2f3d1fSChristoph Hellwig if (open_flag & __O_SYNC) 22366b2f3d1fSChristoph Hellwig open_flag |= O_DSYNC; 22376b2f3d1fSChristoph Hellwig 22386e8341a1SAl Viro if (!acc_mode) 22396d125529SAl Viro acc_mode = MAY_OPEN | ACC_MODE(open_flag); 22401da177e4SLinus Torvalds 2241834f2a4aSTrond Myklebust /* O_TRUNC implies we need access checks for write permissions */ 22424296e2cbSAl Viro if (open_flag & O_TRUNC) 2243834f2a4aSTrond Myklebust acc_mode |= MAY_WRITE; 2244834f2a4aSTrond Myklebust 22451da177e4SLinus Torvalds /* Allow the LSM permission hook to distinguish append 22461da177e4SLinus Torvalds access from general write access. */ 22474296e2cbSAl Viro if (open_flag & O_APPEND) 22481da177e4SLinus Torvalds acc_mode |= MAY_APPEND; 22491da177e4SLinus Torvalds 225031e6b01fSNick Piggin flags = LOOKUP_OPEN; 225131e6b01fSNick Piggin if (open_flag & O_CREAT) { 225231e6b01fSNick Piggin flags |= LOOKUP_CREATE; 225331e6b01fSNick Piggin if (open_flag & O_EXCL) 225431e6b01fSNick Piggin flags |= LOOKUP_EXCL; 2255654f562cSJ. R. Okajima } 225631e6b01fSNick Piggin if (open_flag & O_DIRECTORY) 225731e6b01fSNick Piggin flags |= LOOKUP_DIRECTORY; 225831e6b01fSNick Piggin if (!(open_flag & O_NOFOLLOW)) 225931e6b01fSNick Piggin flags |= LOOKUP_FOLLOW; 226031e6b01fSNick Piggin 226131e6b01fSNick Piggin filp = get_empty_filp(); 226231e6b01fSNick Piggin if (!filp) 226331e6b01fSNick Piggin return ERR_PTR(-ENFILE); 226431e6b01fSNick Piggin 226531e6b01fSNick Piggin filp->f_flags = open_flag; 226631e6b01fSNick Piggin nd.intent.open.file = filp; 226731e6b01fSNick Piggin nd.intent.open.flags = flag; 226831e6b01fSNick Piggin nd.intent.open.create_mode = mode; 226931e6b01fSNick Piggin 227031e6b01fSNick Piggin if (open_flag & O_CREAT) 227131e6b01fSNick Piggin goto creat; 227231e6b01fSNick Piggin 227331e6b01fSNick Piggin /* !O_CREAT, simple open */ 227431e6b01fSNick Piggin error = do_path_lookup(dfd, pathname, flags, &nd); 227531e6b01fSNick Piggin if (unlikely(error)) 227631e6b01fSNick Piggin goto out_filp; 227731e6b01fSNick Piggin error = -ELOOP; 227831e6b01fSNick Piggin if (!(nd.flags & LOOKUP_FOLLOW)) { 227931e6b01fSNick Piggin if (nd.inode->i_op->follow_link) 228031e6b01fSNick Piggin goto out_path; 228131e6b01fSNick Piggin } 228231e6b01fSNick Piggin error = -ENOTDIR; 228331e6b01fSNick Piggin if (nd.flags & LOOKUP_DIRECTORY) { 228431e6b01fSNick Piggin if (!nd.inode->i_op->lookup) 228531e6b01fSNick Piggin goto out_path; 228631e6b01fSNick Piggin } 228731e6b01fSNick Piggin audit_inode(pathname, nd.path.dentry); 228831e6b01fSNick Piggin filp = finish_open(&nd, open_flag, acc_mode); 228931e6b01fSNick Piggin return filp; 229031e6b01fSNick Piggin 229131e6b01fSNick Piggin creat: 229231e6b01fSNick Piggin /* OK, have to create the file. Find the parent. */ 229331e6b01fSNick Piggin error = path_init_rcu(dfd, pathname, 229431e6b01fSNick Piggin LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd); 229531e6b01fSNick Piggin if (error) 229631e6b01fSNick Piggin goto out_filp; 229731e6b01fSNick Piggin error = path_walk_rcu(pathname, &nd); 229831e6b01fSNick Piggin path_finish_rcu(&nd); 229931e6b01fSNick Piggin if (unlikely(error == -ECHILD || error == -ESTALE)) { 230031e6b01fSNick Piggin /* slower, locked walk */ 230131e6b01fSNick Piggin if (error == -ESTALE) { 230231e6b01fSNick Piggin reval: 230331e6b01fSNick Piggin flags |= LOOKUP_REVAL; 230431e6b01fSNick Piggin } 230531e6b01fSNick Piggin error = path_init(dfd, pathname, 230631e6b01fSNick Piggin LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd); 230731e6b01fSNick Piggin if (error) 230831e6b01fSNick Piggin goto out_filp; 230931e6b01fSNick Piggin 231031e6b01fSNick Piggin error = path_walk_simple(pathname, &nd); 231131e6b01fSNick Piggin } 231231e6b01fSNick Piggin if (unlikely(error)) 231331e6b01fSNick Piggin goto out_filp; 231431e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) 23159b4a9b14SAl Viro audit_inode(pathname, nd.path.dentry); 23161da177e4SLinus Torvalds 23171da177e4SLinus Torvalds /* 2318a2c36b45SAl Viro * We have the parent and last component. 23191da177e4SLinus Torvalds */ 232031e6b01fSNick Piggin nd.flags = flags; 23213e297b61SAl Viro filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname); 2322806b681cSAl Viro while (unlikely(!filp)) { /* trailing symlink */ 2323def4af30SAl Viro struct path holder; 2324def4af30SAl Viro void *cookie; 2325806b681cSAl Viro error = -ELOOP; 23261f36f774SAl Viro /* S_ISDIR part is a temporary automount kludge */ 232731e6b01fSNick Piggin if (!(nd.flags & LOOKUP_FOLLOW) && !S_ISDIR(nd.inode->i_mode)) 23281f36f774SAl Viro goto exit_dput; 23291f36f774SAl Viro if (count++ == 32) 2330806b681cSAl Viro goto exit_dput; 2331806b681cSAl Viro /* 2332806b681cSAl Viro * This is subtle. Instead of calling do_follow_link() we do 2333806b681cSAl Viro * the thing by hands. The reason is that this way we have zero 2334806b681cSAl Viro * link_count and path_walk() (called from ->follow_link) 2335806b681cSAl Viro * honoring LOOKUP_PARENT. After that we have the parent and 2336806b681cSAl Viro * last component, i.e. we are in the same situation as after 2337806b681cSAl Viro * the first path_walk(). Well, almost - if the last component 2338806b681cSAl Viro * is normal we get its copy stored in nd->last.name and we will 2339806b681cSAl Viro * have to putname() it when we are done. Procfs-like symlinks 2340806b681cSAl Viro * just set LAST_BIND. 2341806b681cSAl Viro */ 2342806b681cSAl Viro nd.flags |= LOOKUP_PARENT; 2343806b681cSAl Viro error = security_inode_follow_link(path.dentry, &nd); 2344806b681cSAl Viro if (error) 2345806b681cSAl Viro goto exit_dput; 2346def4af30SAl Viro error = __do_follow_link(&path, &nd, &cookie); 2347def4af30SAl Viro if (unlikely(error)) { 234831e6b01fSNick Piggin if (!IS_ERR(cookie) && nd.inode->i_op->put_link) 234931e6b01fSNick Piggin nd.inode->i_op->put_link(path.dentry, &nd, cookie); 2350806b681cSAl Viro /* nd.path had been dropped */ 235131e6b01fSNick Piggin nd.path = path; 235231e6b01fSNick Piggin goto out_path; 2353806b681cSAl Viro } 2354def4af30SAl Viro holder = path; 2355806b681cSAl Viro nd.flags &= ~LOOKUP_PARENT; 23563e297b61SAl Viro filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname); 235731e6b01fSNick Piggin if (nd.inode->i_op->put_link) 235831e6b01fSNick Piggin nd.inode->i_op->put_link(holder.dentry, &nd, cookie); 2359def4af30SAl Viro path_put(&holder); 2360806b681cSAl Viro } 236110fa8e62SAl Viro out: 23622a737871SAl Viro if (nd.root.mnt) 23632a737871SAl Viro path_put(&nd.root); 236431e6b01fSNick Piggin if (filp == ERR_PTR(-ESTALE) && !(flags & LOOKUP_REVAL)) 236510fa8e62SAl Viro goto reval; 236610fa8e62SAl Viro return filp; 23671da177e4SLinus Torvalds 2368806b681cSAl Viro exit_dput: 2369806b681cSAl Viro path_put_conditional(&path, &nd); 237031e6b01fSNick Piggin out_path: 237131e6b01fSNick Piggin path_put(&nd.path); 237231e6b01fSNick Piggin out_filp: 2373806b681cSAl Viro if (!IS_ERR(nd.intent.open.file)) 2374a70e65dfSChristoph Hellwig release_open_intent(&nd); 237510fa8e62SAl Viro filp = ERR_PTR(error); 237610fa8e62SAl Viro goto out; 2377de459215SKirill Korotaev } 23781da177e4SLinus Torvalds 23791da177e4SLinus Torvalds /** 2380a70e65dfSChristoph Hellwig * filp_open - open file and return file pointer 2381a70e65dfSChristoph Hellwig * 2382a70e65dfSChristoph Hellwig * @filename: path to open 2383a70e65dfSChristoph Hellwig * @flags: open flags as per the open(2) second argument 2384a70e65dfSChristoph Hellwig * @mode: mode for the new file if O_CREAT is set, else ignored 2385a70e65dfSChristoph Hellwig * 2386a70e65dfSChristoph Hellwig * This is the helper to open a file from kernelspace if you really 2387a70e65dfSChristoph Hellwig * have to. But in generally you should not do this, so please move 2388a70e65dfSChristoph Hellwig * along, nothing to see here.. 2389a70e65dfSChristoph Hellwig */ 2390a70e65dfSChristoph Hellwig struct file *filp_open(const char *filename, int flags, int mode) 2391a70e65dfSChristoph Hellwig { 23926e8341a1SAl Viro return do_filp_open(AT_FDCWD, filename, flags, mode, 0); 2393a70e65dfSChristoph Hellwig } 2394a70e65dfSChristoph Hellwig EXPORT_SYMBOL(filp_open); 2395a70e65dfSChristoph Hellwig 2396a70e65dfSChristoph Hellwig /** 23971da177e4SLinus Torvalds * lookup_create - lookup a dentry, creating it if it doesn't exist 23981da177e4SLinus Torvalds * @nd: nameidata info 23991da177e4SLinus Torvalds * @is_dir: directory flag 24001da177e4SLinus Torvalds * 24011da177e4SLinus Torvalds * Simple function to lookup and return a dentry and create it 24021da177e4SLinus Torvalds * if it doesn't exist. Is SMP-safe. 2403c663e5d8SChristoph Hellwig * 24044ac91378SJan Blunck * Returns with nd->path.dentry->d_inode->i_mutex locked. 24051da177e4SLinus Torvalds */ 24061da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir) 24071da177e4SLinus Torvalds { 2408c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 24091da177e4SLinus Torvalds 24104ac91378SJan Blunck mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 2411c663e5d8SChristoph Hellwig /* 2412c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 2413c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 2414c663e5d8SChristoph Hellwig */ 24151da177e4SLinus Torvalds if (nd->last_type != LAST_NORM) 24161da177e4SLinus Torvalds goto fail; 24171da177e4SLinus Torvalds nd->flags &= ~LOOKUP_PARENT; 24183516586aSAl Viro nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL; 2419a634904aSASANO Masahiro nd->intent.open.flags = O_EXCL; 2420c663e5d8SChristoph Hellwig 2421c663e5d8SChristoph Hellwig /* 2422c663e5d8SChristoph Hellwig * Do the final lookup. 2423c663e5d8SChristoph Hellwig */ 242449705b77SChristoph Hellwig dentry = lookup_hash(nd); 24251da177e4SLinus Torvalds if (IS_ERR(dentry)) 24261da177e4SLinus Torvalds goto fail; 2427c663e5d8SChristoph Hellwig 2428e9baf6e5SAl Viro if (dentry->d_inode) 2429e9baf6e5SAl Viro goto eexist; 2430c663e5d8SChristoph Hellwig /* 2431c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 2432c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 2433c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 2434c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 2435c663e5d8SChristoph Hellwig */ 2436e9baf6e5SAl Viro if (unlikely(!is_dir && nd->last.name[nd->last.len])) { 24371da177e4SLinus Torvalds dput(dentry); 24381da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 2439e9baf6e5SAl Viro } 2440e9baf6e5SAl Viro return dentry; 2441e9baf6e5SAl Viro eexist: 2442e9baf6e5SAl Viro dput(dentry); 2443e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 24441da177e4SLinus Torvalds fail: 24451da177e4SLinus Torvalds return dentry; 24461da177e4SLinus Torvalds } 2447f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create); 24481da177e4SLinus Torvalds 24491da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 24501da177e4SLinus Torvalds { 2451a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 24521da177e4SLinus Torvalds 24531da177e4SLinus Torvalds if (error) 24541da177e4SLinus Torvalds return error; 24551da177e4SLinus Torvalds 24561da177e4SLinus Torvalds if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) 24571da177e4SLinus Torvalds return -EPERM; 24581da177e4SLinus Torvalds 2459acfa4380SAl Viro if (!dir->i_op->mknod) 24601da177e4SLinus Torvalds return -EPERM; 24611da177e4SLinus Torvalds 246208ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 246308ce5f16SSerge E. Hallyn if (error) 246408ce5f16SSerge E. Hallyn return error; 246508ce5f16SSerge E. Hallyn 24661da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 24671da177e4SLinus Torvalds if (error) 24681da177e4SLinus Torvalds return error; 24691da177e4SLinus Torvalds 24701da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 2471a74574aaSStephen Smalley if (!error) 2472f38aa942SAmy Griffis fsnotify_create(dir, dentry); 24731da177e4SLinus Torvalds return error; 24741da177e4SLinus Torvalds } 24751da177e4SLinus Torvalds 2476463c3197SDave Hansen static int may_mknod(mode_t mode) 2477463c3197SDave Hansen { 2478463c3197SDave Hansen switch (mode & S_IFMT) { 2479463c3197SDave Hansen case S_IFREG: 2480463c3197SDave Hansen case S_IFCHR: 2481463c3197SDave Hansen case S_IFBLK: 2482463c3197SDave Hansen case S_IFIFO: 2483463c3197SDave Hansen case S_IFSOCK: 2484463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2485463c3197SDave Hansen return 0; 2486463c3197SDave Hansen case S_IFDIR: 2487463c3197SDave Hansen return -EPERM; 2488463c3197SDave Hansen default: 2489463c3197SDave Hansen return -EINVAL; 2490463c3197SDave Hansen } 2491463c3197SDave Hansen } 2492463c3197SDave Hansen 24932e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode, 24942e4d0924SHeiko Carstens unsigned, dev) 24951da177e4SLinus Torvalds { 24962ad94ae6SAl Viro int error; 24971da177e4SLinus Torvalds char *tmp; 24981da177e4SLinus Torvalds struct dentry *dentry; 24991da177e4SLinus Torvalds struct nameidata nd; 25001da177e4SLinus Torvalds 25011da177e4SLinus Torvalds if (S_ISDIR(mode)) 25021da177e4SLinus Torvalds return -EPERM; 25031da177e4SLinus Torvalds 25042ad94ae6SAl Viro error = user_path_parent(dfd, filename, &nd, &tmp); 25051da177e4SLinus Torvalds if (error) 25062ad94ae6SAl Viro return error; 25072ad94ae6SAl Viro 25081da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 2509463c3197SDave Hansen if (IS_ERR(dentry)) { 25101da177e4SLinus Torvalds error = PTR_ERR(dentry); 2511463c3197SDave Hansen goto out_unlock; 2512463c3197SDave Hansen } 25134ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2514ce3b0f8dSAl Viro mode &= ~current_umask(); 2515463c3197SDave Hansen error = may_mknod(mode); 2516463c3197SDave Hansen if (error) 2517463c3197SDave Hansen goto out_dput; 2518463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2519463c3197SDave Hansen if (error) 2520463c3197SDave Hansen goto out_dput; 2521be6d3e56SKentaro Takeda error = security_path_mknod(&nd.path, dentry, mode, dev); 2522be6d3e56SKentaro Takeda if (error) 2523be6d3e56SKentaro Takeda goto out_drop_write; 25241da177e4SLinus Torvalds switch (mode & S_IFMT) { 25251da177e4SLinus Torvalds case 0: case S_IFREG: 25264ac91378SJan Blunck error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd); 25271da177e4SLinus Torvalds break; 25281da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 25294ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode, 25301da177e4SLinus Torvalds new_decode_dev(dev)); 25311da177e4SLinus Torvalds break; 25321da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 25334ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0); 25341da177e4SLinus Torvalds break; 25351da177e4SLinus Torvalds } 2536be6d3e56SKentaro Takeda out_drop_write: 2537463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2538463c3197SDave Hansen out_dput: 25391da177e4SLinus Torvalds dput(dentry); 2540463c3197SDave Hansen out_unlock: 25414ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 25421d957f9bSJan Blunck path_put(&nd.path); 25431da177e4SLinus Torvalds putname(tmp); 25441da177e4SLinus Torvalds 25451da177e4SLinus Torvalds return error; 25461da177e4SLinus Torvalds } 25471da177e4SLinus Torvalds 25483480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev) 25495590ff0dSUlrich Drepper { 25505590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 25515590ff0dSUlrich Drepper } 25525590ff0dSUlrich Drepper 25531da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 25541da177e4SLinus Torvalds { 2555a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 25561da177e4SLinus Torvalds 25571da177e4SLinus Torvalds if (error) 25581da177e4SLinus Torvalds return error; 25591da177e4SLinus Torvalds 2560acfa4380SAl Viro if (!dir->i_op->mkdir) 25611da177e4SLinus Torvalds return -EPERM; 25621da177e4SLinus Torvalds 25631da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 25641da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 25651da177e4SLinus Torvalds if (error) 25661da177e4SLinus Torvalds return error; 25671da177e4SLinus Torvalds 25681da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2569a74574aaSStephen Smalley if (!error) 2570f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 25711da177e4SLinus Torvalds return error; 25721da177e4SLinus Torvalds } 25731da177e4SLinus Torvalds 25742e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode) 25751da177e4SLinus Torvalds { 25761da177e4SLinus Torvalds int error = 0; 25771da177e4SLinus Torvalds char * tmp; 25786902d925SDave Hansen struct dentry *dentry; 25796902d925SDave Hansen struct nameidata nd; 25801da177e4SLinus Torvalds 25812ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &tmp); 25822ad94ae6SAl Viro if (error) 25836902d925SDave Hansen goto out_err; 25841da177e4SLinus Torvalds 25851da177e4SLinus Torvalds dentry = lookup_create(&nd, 1); 25861da177e4SLinus Torvalds error = PTR_ERR(dentry); 25876902d925SDave Hansen if (IS_ERR(dentry)) 25886902d925SDave Hansen goto out_unlock; 25896902d925SDave Hansen 25904ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2591ce3b0f8dSAl Viro mode &= ~current_umask(); 2592463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2593463c3197SDave Hansen if (error) 2594463c3197SDave Hansen goto out_dput; 2595be6d3e56SKentaro Takeda error = security_path_mkdir(&nd.path, dentry, mode); 2596be6d3e56SKentaro Takeda if (error) 2597be6d3e56SKentaro Takeda goto out_drop_write; 25984ac91378SJan Blunck error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode); 2599be6d3e56SKentaro Takeda out_drop_write: 2600463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2601463c3197SDave Hansen out_dput: 26021da177e4SLinus Torvalds dput(dentry); 26036902d925SDave Hansen out_unlock: 26044ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 26051d957f9bSJan Blunck path_put(&nd.path); 26061da177e4SLinus Torvalds putname(tmp); 26076902d925SDave Hansen out_err: 26081da177e4SLinus Torvalds return error; 26091da177e4SLinus Torvalds } 26101da177e4SLinus Torvalds 26113cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode) 26125590ff0dSUlrich Drepper { 26135590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 26145590ff0dSUlrich Drepper } 26155590ff0dSUlrich Drepper 26161da177e4SLinus Torvalds /* 26171da177e4SLinus Torvalds * We try to drop the dentry early: we should have 26181da177e4SLinus Torvalds * a usage count of 2 if we're the only user of this 26191da177e4SLinus Torvalds * dentry, and if that is true (possibly after pruning 26201da177e4SLinus Torvalds * the dcache), then we drop the dentry now. 26211da177e4SLinus Torvalds * 26221da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 26231da177e4SLinus Torvalds * do a 26241da177e4SLinus Torvalds * 26251da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 26261da177e4SLinus Torvalds * return -EBUSY; 26271da177e4SLinus Torvalds * 26281da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 26291da177e4SLinus Torvalds * that is still in use by something else.. 26301da177e4SLinus Torvalds */ 26311da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 26321da177e4SLinus Torvalds { 26331da177e4SLinus Torvalds dget(dentry); 26341da177e4SLinus Torvalds shrink_dcache_parent(dentry); 26351da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 2636b7ab39f6SNick Piggin if (dentry->d_count == 2) 26371da177e4SLinus Torvalds __d_drop(dentry); 26381da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 26391da177e4SLinus Torvalds } 26401da177e4SLinus Torvalds 26411da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 26421da177e4SLinus Torvalds { 26431da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 26441da177e4SLinus Torvalds 26451da177e4SLinus Torvalds if (error) 26461da177e4SLinus Torvalds return error; 26471da177e4SLinus Torvalds 2648acfa4380SAl Viro if (!dir->i_op->rmdir) 26491da177e4SLinus Torvalds return -EPERM; 26501da177e4SLinus Torvalds 26511b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 26521da177e4SLinus Torvalds dentry_unhash(dentry); 26531da177e4SLinus Torvalds if (d_mountpoint(dentry)) 26541da177e4SLinus Torvalds error = -EBUSY; 26551da177e4SLinus Torvalds else { 26561da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 26571da177e4SLinus Torvalds if (!error) { 26581da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 2659d83c49f3SAl Viro if (!error) { 26601da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 2661d83c49f3SAl Viro dont_mount(dentry); 2662d83c49f3SAl Viro } 26631da177e4SLinus Torvalds } 26641da177e4SLinus Torvalds } 26651b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 26661da177e4SLinus Torvalds if (!error) { 26671da177e4SLinus Torvalds d_delete(dentry); 26681da177e4SLinus Torvalds } 26691da177e4SLinus Torvalds dput(dentry); 26701da177e4SLinus Torvalds 26711da177e4SLinus Torvalds return error; 26721da177e4SLinus Torvalds } 26731da177e4SLinus Torvalds 26745590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 26751da177e4SLinus Torvalds { 26761da177e4SLinus Torvalds int error = 0; 26771da177e4SLinus Torvalds char * name; 26781da177e4SLinus Torvalds struct dentry *dentry; 26791da177e4SLinus Torvalds struct nameidata nd; 26801da177e4SLinus Torvalds 26812ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 26821da177e4SLinus Torvalds if (error) 26832ad94ae6SAl Viro return error; 26841da177e4SLinus Torvalds 26851da177e4SLinus Torvalds switch(nd.last_type) { 26861da177e4SLinus Torvalds case LAST_DOTDOT: 26871da177e4SLinus Torvalds error = -ENOTEMPTY; 26881da177e4SLinus Torvalds goto exit1; 26891da177e4SLinus Torvalds case LAST_DOT: 26901da177e4SLinus Torvalds error = -EINVAL; 26911da177e4SLinus Torvalds goto exit1; 26921da177e4SLinus Torvalds case LAST_ROOT: 26931da177e4SLinus Torvalds error = -EBUSY; 26941da177e4SLinus Torvalds goto exit1; 26951da177e4SLinus Torvalds } 26960612d9fbSOGAWA Hirofumi 26970612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 26980612d9fbSOGAWA Hirofumi 26994ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 270049705b77SChristoph Hellwig dentry = lookup_hash(&nd); 27011da177e4SLinus Torvalds error = PTR_ERR(dentry); 27026902d925SDave Hansen if (IS_ERR(dentry)) 27036902d925SDave Hansen goto exit2; 27040622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 27050622753bSDave Hansen if (error) 27060622753bSDave Hansen goto exit3; 2707be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 2708be6d3e56SKentaro Takeda if (error) 2709be6d3e56SKentaro Takeda goto exit4; 27104ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 2711be6d3e56SKentaro Takeda exit4: 27120622753bSDave Hansen mnt_drop_write(nd.path.mnt); 27130622753bSDave Hansen exit3: 27141da177e4SLinus Torvalds dput(dentry); 27156902d925SDave Hansen exit2: 27164ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 27171da177e4SLinus Torvalds exit1: 27181d957f9bSJan Blunck path_put(&nd.path); 27191da177e4SLinus Torvalds putname(name); 27201da177e4SLinus Torvalds return error; 27211da177e4SLinus Torvalds } 27221da177e4SLinus Torvalds 27233cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 27245590ff0dSUlrich Drepper { 27255590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 27265590ff0dSUlrich Drepper } 27275590ff0dSUlrich Drepper 27281da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 27291da177e4SLinus Torvalds { 27301da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 27311da177e4SLinus Torvalds 27321da177e4SLinus Torvalds if (error) 27331da177e4SLinus Torvalds return error; 27341da177e4SLinus Torvalds 2735acfa4380SAl Viro if (!dir->i_op->unlink) 27361da177e4SLinus Torvalds return -EPERM; 27371da177e4SLinus Torvalds 27381b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 27391da177e4SLinus Torvalds if (d_mountpoint(dentry)) 27401da177e4SLinus Torvalds error = -EBUSY; 27411da177e4SLinus Torvalds else { 27421da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 2743bec1052eSAl Viro if (!error) { 27441da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 2745bec1052eSAl Viro if (!error) 2746d83c49f3SAl Viro dont_mount(dentry); 2747bec1052eSAl Viro } 27481da177e4SLinus Torvalds } 27491b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 27501da177e4SLinus Torvalds 27511da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 27521da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 2753ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 27541da177e4SLinus Torvalds d_delete(dentry); 27551da177e4SLinus Torvalds } 27560eeca283SRobert Love 27571da177e4SLinus Torvalds return error; 27581da177e4SLinus Torvalds } 27591da177e4SLinus Torvalds 27601da177e4SLinus Torvalds /* 27611da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 27621b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 27631da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 27641da177e4SLinus Torvalds * while waiting on the I/O. 27651da177e4SLinus Torvalds */ 27665590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 27671da177e4SLinus Torvalds { 27682ad94ae6SAl Viro int error; 27691da177e4SLinus Torvalds char *name; 27701da177e4SLinus Torvalds struct dentry *dentry; 27711da177e4SLinus Torvalds struct nameidata nd; 27721da177e4SLinus Torvalds struct inode *inode = NULL; 27731da177e4SLinus Torvalds 27742ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 27751da177e4SLinus Torvalds if (error) 27762ad94ae6SAl Viro return error; 27772ad94ae6SAl Viro 27781da177e4SLinus Torvalds error = -EISDIR; 27791da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 27801da177e4SLinus Torvalds goto exit1; 27810612d9fbSOGAWA Hirofumi 27820612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 27830612d9fbSOGAWA Hirofumi 27844ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 278549705b77SChristoph Hellwig dentry = lookup_hash(&nd); 27861da177e4SLinus Torvalds error = PTR_ERR(dentry); 27871da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 27881da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 27891da177e4SLinus Torvalds if (nd.last.name[nd.last.len]) 27901da177e4SLinus Torvalds goto slashes; 27911da177e4SLinus Torvalds inode = dentry->d_inode; 27921da177e4SLinus Torvalds if (inode) 27937de9c6eeSAl Viro ihold(inode); 27940622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 27950622753bSDave Hansen if (error) 27960622753bSDave Hansen goto exit2; 2797be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 2798be6d3e56SKentaro Takeda if (error) 2799be6d3e56SKentaro Takeda goto exit3; 28004ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 2801be6d3e56SKentaro Takeda exit3: 28020622753bSDave Hansen mnt_drop_write(nd.path.mnt); 28031da177e4SLinus Torvalds exit2: 28041da177e4SLinus Torvalds dput(dentry); 28051da177e4SLinus Torvalds } 28064ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 28071da177e4SLinus Torvalds if (inode) 28081da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 28091da177e4SLinus Torvalds exit1: 28101d957f9bSJan Blunck path_put(&nd.path); 28111da177e4SLinus Torvalds putname(name); 28121da177e4SLinus Torvalds return error; 28131da177e4SLinus Torvalds 28141da177e4SLinus Torvalds slashes: 28151da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 28161da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 28171da177e4SLinus Torvalds goto exit2; 28181da177e4SLinus Torvalds } 28191da177e4SLinus Torvalds 28202e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 28215590ff0dSUlrich Drepper { 28225590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 28235590ff0dSUlrich Drepper return -EINVAL; 28245590ff0dSUlrich Drepper 28255590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 28265590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 28275590ff0dSUlrich Drepper 28285590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 28295590ff0dSUlrich Drepper } 28305590ff0dSUlrich Drepper 28313480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 28325590ff0dSUlrich Drepper { 28335590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 28345590ff0dSUlrich Drepper } 28355590ff0dSUlrich Drepper 2836db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 28371da177e4SLinus Torvalds { 2838a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 28391da177e4SLinus Torvalds 28401da177e4SLinus Torvalds if (error) 28411da177e4SLinus Torvalds return error; 28421da177e4SLinus Torvalds 2843acfa4380SAl Viro if (!dir->i_op->symlink) 28441da177e4SLinus Torvalds return -EPERM; 28451da177e4SLinus Torvalds 28461da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 28471da177e4SLinus Torvalds if (error) 28481da177e4SLinus Torvalds return error; 28491da177e4SLinus Torvalds 28501da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 2851a74574aaSStephen Smalley if (!error) 2852f38aa942SAmy Griffis fsnotify_create(dir, dentry); 28531da177e4SLinus Torvalds return error; 28541da177e4SLinus Torvalds } 28551da177e4SLinus Torvalds 28562e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 28572e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 28581da177e4SLinus Torvalds { 28592ad94ae6SAl Viro int error; 28601da177e4SLinus Torvalds char *from; 28611da177e4SLinus Torvalds char *to; 28626902d925SDave Hansen struct dentry *dentry; 28636902d925SDave Hansen struct nameidata nd; 28641da177e4SLinus Torvalds 28651da177e4SLinus Torvalds from = getname(oldname); 28661da177e4SLinus Torvalds if (IS_ERR(from)) 28671da177e4SLinus Torvalds return PTR_ERR(from); 28682ad94ae6SAl Viro 28692ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 28702ad94ae6SAl Viro if (error) 28716902d925SDave Hansen goto out_putname; 28721da177e4SLinus Torvalds 28731da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 28741da177e4SLinus Torvalds error = PTR_ERR(dentry); 28756902d925SDave Hansen if (IS_ERR(dentry)) 28766902d925SDave Hansen goto out_unlock; 28776902d925SDave Hansen 287875c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 287975c3f29dSDave Hansen if (error) 288075c3f29dSDave Hansen goto out_dput; 2881be6d3e56SKentaro Takeda error = security_path_symlink(&nd.path, dentry, from); 2882be6d3e56SKentaro Takeda if (error) 2883be6d3e56SKentaro Takeda goto out_drop_write; 2884db2e747bSMiklos Szeredi error = vfs_symlink(nd.path.dentry->d_inode, dentry, from); 2885be6d3e56SKentaro Takeda out_drop_write: 288675c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 288775c3f29dSDave Hansen out_dput: 28881da177e4SLinus Torvalds dput(dentry); 28896902d925SDave Hansen out_unlock: 28904ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 28911d957f9bSJan Blunck path_put(&nd.path); 28921da177e4SLinus Torvalds putname(to); 28936902d925SDave Hansen out_putname: 28941da177e4SLinus Torvalds putname(from); 28951da177e4SLinus Torvalds return error; 28961da177e4SLinus Torvalds } 28971da177e4SLinus Torvalds 28983480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 28995590ff0dSUlrich Drepper { 29005590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 29015590ff0dSUlrich Drepper } 29025590ff0dSUlrich Drepper 29031da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 29041da177e4SLinus Torvalds { 29051da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 29061da177e4SLinus Torvalds int error; 29071da177e4SLinus Torvalds 29081da177e4SLinus Torvalds if (!inode) 29091da177e4SLinus Torvalds return -ENOENT; 29101da177e4SLinus Torvalds 2911a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 29121da177e4SLinus Torvalds if (error) 29131da177e4SLinus Torvalds return error; 29141da177e4SLinus Torvalds 29151da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 29161da177e4SLinus Torvalds return -EXDEV; 29171da177e4SLinus Torvalds 29181da177e4SLinus Torvalds /* 29191da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 29201da177e4SLinus Torvalds */ 29211da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 29221da177e4SLinus Torvalds return -EPERM; 2923acfa4380SAl Viro if (!dir->i_op->link) 29241da177e4SLinus Torvalds return -EPERM; 29257e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 29261da177e4SLinus Torvalds return -EPERM; 29271da177e4SLinus Torvalds 29281da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 29291da177e4SLinus Torvalds if (error) 29301da177e4SLinus Torvalds return error; 29311da177e4SLinus Torvalds 29327e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 29331da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 29347e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 2935e31e14ecSStephen Smalley if (!error) 29367e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 29371da177e4SLinus Torvalds return error; 29381da177e4SLinus Torvalds } 29391da177e4SLinus Torvalds 29401da177e4SLinus Torvalds /* 29411da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 29421da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 29431da177e4SLinus Torvalds * newname. --KAB 29441da177e4SLinus Torvalds * 29451da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 29461da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 29471da177e4SLinus Torvalds * and other special files. --ADM 29481da177e4SLinus Torvalds */ 29492e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 29502e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 29511da177e4SLinus Torvalds { 29521da177e4SLinus Torvalds struct dentry *new_dentry; 29532d8f3038SAl Viro struct nameidata nd; 29542d8f3038SAl Viro struct path old_path; 29551da177e4SLinus Torvalds int error; 29561da177e4SLinus Torvalds char *to; 29571da177e4SLinus Torvalds 295845c9b11aSUlrich Drepper if ((flags & ~AT_SYMLINK_FOLLOW) != 0) 2959c04030e1SUlrich Drepper return -EINVAL; 2960c04030e1SUlrich Drepper 29612d8f3038SAl Viro error = user_path_at(olddfd, oldname, 296245c9b11aSUlrich Drepper flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0, 29632d8f3038SAl Viro &old_path); 29641da177e4SLinus Torvalds if (error) 29652ad94ae6SAl Viro return error; 29662ad94ae6SAl Viro 29672ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 29681da177e4SLinus Torvalds if (error) 29691da177e4SLinus Torvalds goto out; 29701da177e4SLinus Torvalds error = -EXDEV; 29712d8f3038SAl Viro if (old_path.mnt != nd.path.mnt) 29721da177e4SLinus Torvalds goto out_release; 29731da177e4SLinus Torvalds new_dentry = lookup_create(&nd, 0); 29741da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 29756902d925SDave Hansen if (IS_ERR(new_dentry)) 29766902d925SDave Hansen goto out_unlock; 297775c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 297875c3f29dSDave Hansen if (error) 297975c3f29dSDave Hansen goto out_dput; 2980be6d3e56SKentaro Takeda error = security_path_link(old_path.dentry, &nd.path, new_dentry); 2981be6d3e56SKentaro Takeda if (error) 2982be6d3e56SKentaro Takeda goto out_drop_write; 29832d8f3038SAl Viro error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry); 2984be6d3e56SKentaro Takeda out_drop_write: 298575c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 298675c3f29dSDave Hansen out_dput: 29871da177e4SLinus Torvalds dput(new_dentry); 29886902d925SDave Hansen out_unlock: 29894ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 29901da177e4SLinus Torvalds out_release: 29911d957f9bSJan Blunck path_put(&nd.path); 29922ad94ae6SAl Viro putname(to); 29931da177e4SLinus Torvalds out: 29942d8f3038SAl Viro path_put(&old_path); 29951da177e4SLinus Torvalds 29961da177e4SLinus Torvalds return error; 29971da177e4SLinus Torvalds } 29981da177e4SLinus Torvalds 29993480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 30005590ff0dSUlrich Drepper { 3001c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 30025590ff0dSUlrich Drepper } 30035590ff0dSUlrich Drepper 30041da177e4SLinus Torvalds /* 30051da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 30061da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 30071da177e4SLinus Torvalds * Problems: 30081da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 30091da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 30101da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 3011a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 30121da177e4SLinus Torvalds * story. 30131da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 30141b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 30151da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 30161da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 3017a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 30181da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 30191da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 30201da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 3021a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 30221da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 30231da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 30241da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 30251da177e4SLinus Torvalds * d) some filesystems don't support opened-but-unlinked directories, 30261da177e4SLinus Torvalds * either because of layout or because they are not ready to deal with 30271da177e4SLinus Torvalds * all cases correctly. The latter will be fixed (taking this sort of 30281da177e4SLinus Torvalds * stuff into VFS), but the former is not going away. Solution: the same 30291da177e4SLinus Torvalds * trick as in rmdir(). 30301da177e4SLinus Torvalds * e) conversion from fhandle to dentry may come in the wrong moment - when 30311b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 30321da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 3033c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 30341da177e4SLinus Torvalds * locking]. 30351da177e4SLinus Torvalds */ 303675c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 30371da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30381da177e4SLinus Torvalds { 30391da177e4SLinus Torvalds int error = 0; 30401da177e4SLinus Torvalds struct inode *target; 30411da177e4SLinus Torvalds 30421da177e4SLinus Torvalds /* 30431da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 30441da177e4SLinus Torvalds * we'll need to flip '..'. 30451da177e4SLinus Torvalds */ 30461da177e4SLinus Torvalds if (new_dir != old_dir) { 3047f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 30481da177e4SLinus Torvalds if (error) 30491da177e4SLinus Torvalds return error; 30501da177e4SLinus Torvalds } 30511da177e4SLinus Torvalds 30521da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 30531da177e4SLinus Torvalds if (error) 30541da177e4SLinus Torvalds return error; 30551da177e4SLinus Torvalds 30561da177e4SLinus Torvalds target = new_dentry->d_inode; 3057d83c49f3SAl Viro if (target) 30581b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 30591da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 30601da177e4SLinus Torvalds error = -EBUSY; 3061d83c49f3SAl Viro else { 3062d83c49f3SAl Viro if (target) 3063d83c49f3SAl Viro dentry_unhash(new_dentry); 30641da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 3065d83c49f3SAl Viro } 30661da177e4SLinus Torvalds if (target) { 3067d83c49f3SAl Viro if (!error) { 30681da177e4SLinus Torvalds target->i_flags |= S_DEAD; 3069d83c49f3SAl Viro dont_mount(new_dentry); 3070d83c49f3SAl Viro } 30711b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 30721da177e4SLinus Torvalds if (d_unhashed(new_dentry)) 30731da177e4SLinus Torvalds d_rehash(new_dentry); 30741da177e4SLinus Torvalds dput(new_dentry); 30751da177e4SLinus Torvalds } 3076e31e14ecSStephen Smalley if (!error) 3077349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 30781da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 30791da177e4SLinus Torvalds return error; 30801da177e4SLinus Torvalds } 30811da177e4SLinus Torvalds 308275c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 30831da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30841da177e4SLinus Torvalds { 30851da177e4SLinus Torvalds struct inode *target; 30861da177e4SLinus Torvalds int error; 30871da177e4SLinus Torvalds 30881da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 30891da177e4SLinus Torvalds if (error) 30901da177e4SLinus Torvalds return error; 30911da177e4SLinus Torvalds 30921da177e4SLinus Torvalds dget(new_dentry); 30931da177e4SLinus Torvalds target = new_dentry->d_inode; 30941da177e4SLinus Torvalds if (target) 30951b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 30961da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 30971da177e4SLinus Torvalds error = -EBUSY; 30981da177e4SLinus Torvalds else 30991da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 31001da177e4SLinus Torvalds if (!error) { 3101bec1052eSAl Viro if (target) 3102d83c49f3SAl Viro dont_mount(new_dentry); 3103349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 31041da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 31051da177e4SLinus Torvalds } 31061da177e4SLinus Torvalds if (target) 31071b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 31081da177e4SLinus Torvalds dput(new_dentry); 31091da177e4SLinus Torvalds return error; 31101da177e4SLinus Torvalds } 31111da177e4SLinus Torvalds 31121da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 31131da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 31141da177e4SLinus Torvalds { 31151da177e4SLinus Torvalds int error; 31161da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 311759b0df21SEric Paris const unsigned char *old_name; 31181da177e4SLinus Torvalds 31191da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 31201da177e4SLinus Torvalds return 0; 31211da177e4SLinus Torvalds 31221da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 31231da177e4SLinus Torvalds if (error) 31241da177e4SLinus Torvalds return error; 31251da177e4SLinus Torvalds 31261da177e4SLinus Torvalds if (!new_dentry->d_inode) 3127a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 31281da177e4SLinus Torvalds else 31291da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 31301da177e4SLinus Torvalds if (error) 31311da177e4SLinus Torvalds return error; 31321da177e4SLinus Torvalds 3133acfa4380SAl Viro if (!old_dir->i_op->rename) 31341da177e4SLinus Torvalds return -EPERM; 31351da177e4SLinus Torvalds 31360eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 31370eeca283SRobert Love 31381da177e4SLinus Torvalds if (is_dir) 31391da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 31401da177e4SLinus Torvalds else 31411da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3142123df294SAl Viro if (!error) 3143123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 31445a190ae6SAl Viro new_dentry->d_inode, old_dentry); 31450eeca283SRobert Love fsnotify_oldname_free(old_name); 31460eeca283SRobert Love 31471da177e4SLinus Torvalds return error; 31481da177e4SLinus Torvalds } 31491da177e4SLinus Torvalds 31502e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 31512e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 31521da177e4SLinus Torvalds { 31531da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 31541da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 31551da177e4SLinus Torvalds struct dentry *trap; 31561da177e4SLinus Torvalds struct nameidata oldnd, newnd; 31572ad94ae6SAl Viro char *from; 31582ad94ae6SAl Viro char *to; 31592ad94ae6SAl Viro int error; 31601da177e4SLinus Torvalds 31612ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 31621da177e4SLinus Torvalds if (error) 31631da177e4SLinus Torvalds goto exit; 31641da177e4SLinus Torvalds 31652ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 31661da177e4SLinus Torvalds if (error) 31671da177e4SLinus Torvalds goto exit1; 31681da177e4SLinus Torvalds 31691da177e4SLinus Torvalds error = -EXDEV; 31704ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 31711da177e4SLinus Torvalds goto exit2; 31721da177e4SLinus Torvalds 31734ac91378SJan Blunck old_dir = oldnd.path.dentry; 31741da177e4SLinus Torvalds error = -EBUSY; 31751da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 31761da177e4SLinus Torvalds goto exit2; 31771da177e4SLinus Torvalds 31784ac91378SJan Blunck new_dir = newnd.path.dentry; 31791da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 31801da177e4SLinus Torvalds goto exit2; 31811da177e4SLinus Torvalds 31820612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 31830612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 31844e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 31850612d9fbSOGAWA Hirofumi 31861da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 31871da177e4SLinus Torvalds 318849705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 31891da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 31901da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 31911da177e4SLinus Torvalds goto exit3; 31921da177e4SLinus Torvalds /* source must exist */ 31931da177e4SLinus Torvalds error = -ENOENT; 31941da177e4SLinus Torvalds if (!old_dentry->d_inode) 31951da177e4SLinus Torvalds goto exit4; 31961da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 31971da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 31981da177e4SLinus Torvalds error = -ENOTDIR; 31991da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 32001da177e4SLinus Torvalds goto exit4; 32011da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 32021da177e4SLinus Torvalds goto exit4; 32031da177e4SLinus Torvalds } 32041da177e4SLinus Torvalds /* source should not be ancestor of target */ 32051da177e4SLinus Torvalds error = -EINVAL; 32061da177e4SLinus Torvalds if (old_dentry == trap) 32071da177e4SLinus Torvalds goto exit4; 320849705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 32091da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 32101da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 32111da177e4SLinus Torvalds goto exit4; 32121da177e4SLinus Torvalds /* target should not be an ancestor of source */ 32131da177e4SLinus Torvalds error = -ENOTEMPTY; 32141da177e4SLinus Torvalds if (new_dentry == trap) 32151da177e4SLinus Torvalds goto exit5; 32161da177e4SLinus Torvalds 32179079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 32189079b1ebSDave Hansen if (error) 32199079b1ebSDave Hansen goto exit5; 3220be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3221be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3222be6d3e56SKentaro Takeda if (error) 3223be6d3e56SKentaro Takeda goto exit6; 32241da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 32251da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 3226be6d3e56SKentaro Takeda exit6: 32279079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 32281da177e4SLinus Torvalds exit5: 32291da177e4SLinus Torvalds dput(new_dentry); 32301da177e4SLinus Torvalds exit4: 32311da177e4SLinus Torvalds dput(old_dentry); 32321da177e4SLinus Torvalds exit3: 32331da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 32341da177e4SLinus Torvalds exit2: 32351d957f9bSJan Blunck path_put(&newnd.path); 32362ad94ae6SAl Viro putname(to); 32371da177e4SLinus Torvalds exit1: 32381d957f9bSJan Blunck path_put(&oldnd.path); 32391da177e4SLinus Torvalds putname(from); 32402ad94ae6SAl Viro exit: 32411da177e4SLinus Torvalds return error; 32421da177e4SLinus Torvalds } 32431da177e4SLinus Torvalds 3244a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 32455590ff0dSUlrich Drepper { 32465590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 32475590ff0dSUlrich Drepper } 32485590ff0dSUlrich Drepper 32491da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 32501da177e4SLinus Torvalds { 32511da177e4SLinus Torvalds int len; 32521da177e4SLinus Torvalds 32531da177e4SLinus Torvalds len = PTR_ERR(link); 32541da177e4SLinus Torvalds if (IS_ERR(link)) 32551da177e4SLinus Torvalds goto out; 32561da177e4SLinus Torvalds 32571da177e4SLinus Torvalds len = strlen(link); 32581da177e4SLinus Torvalds if (len > (unsigned) buflen) 32591da177e4SLinus Torvalds len = buflen; 32601da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 32611da177e4SLinus Torvalds len = -EFAULT; 32621da177e4SLinus Torvalds out: 32631da177e4SLinus Torvalds return len; 32641da177e4SLinus Torvalds } 32651da177e4SLinus Torvalds 32661da177e4SLinus Torvalds /* 32671da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 32681da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 32691da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 32701da177e4SLinus Torvalds */ 32711da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 32721da177e4SLinus Torvalds { 32731da177e4SLinus Torvalds struct nameidata nd; 3274cc314eefSLinus Torvalds void *cookie; 3275694a1764SMarcin Slusarz int res; 3276cc314eefSLinus Torvalds 32771da177e4SLinus Torvalds nd.depth = 0; 3278cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3279694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3280694a1764SMarcin Slusarz return PTR_ERR(cookie); 3281694a1764SMarcin Slusarz 3282694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 32831da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3284cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3285694a1764SMarcin Slusarz return res; 32861da177e4SLinus Torvalds } 32871da177e4SLinus Torvalds 32881da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 32891da177e4SLinus Torvalds { 32901da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 32911da177e4SLinus Torvalds } 32921da177e4SLinus Torvalds 32931da177e4SLinus Torvalds /* get the link contents into pagecache */ 32941da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 32951da177e4SLinus Torvalds { 3296ebd09abbSDuane Griffin char *kaddr; 32971da177e4SLinus Torvalds struct page *page; 32981da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3299090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 33001da177e4SLinus Torvalds if (IS_ERR(page)) 33016fe6900eSNick Piggin return (char*)page; 33021da177e4SLinus Torvalds *ppage = page; 3303ebd09abbSDuane Griffin kaddr = kmap(page); 3304ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3305ebd09abbSDuane Griffin return kaddr; 33061da177e4SLinus Torvalds } 33071da177e4SLinus Torvalds 33081da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 33091da177e4SLinus Torvalds { 33101da177e4SLinus Torvalds struct page *page = NULL; 33111da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 33121da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 33131da177e4SLinus Torvalds if (page) { 33141da177e4SLinus Torvalds kunmap(page); 33151da177e4SLinus Torvalds page_cache_release(page); 33161da177e4SLinus Torvalds } 33171da177e4SLinus Torvalds return res; 33181da177e4SLinus Torvalds } 33191da177e4SLinus Torvalds 3320cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 33211da177e4SLinus Torvalds { 3322cc314eefSLinus Torvalds struct page *page = NULL; 33231da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 3324cc314eefSLinus Torvalds return page; 33251da177e4SLinus Torvalds } 33261da177e4SLinus Torvalds 3327cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 33281da177e4SLinus Torvalds { 3329cc314eefSLinus Torvalds struct page *page = cookie; 3330cc314eefSLinus Torvalds 3331cc314eefSLinus Torvalds if (page) { 33321da177e4SLinus Torvalds kunmap(page); 33331da177e4SLinus Torvalds page_cache_release(page); 33341da177e4SLinus Torvalds } 33351da177e4SLinus Torvalds } 33361da177e4SLinus Torvalds 333754566b2cSNick Piggin /* 333854566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 333954566b2cSNick Piggin */ 334054566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 33411da177e4SLinus Torvalds { 33421da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 33430adb25d2SKirill Korotaev struct page *page; 3344afddba49SNick Piggin void *fsdata; 3345beb497abSDmitriy Monakhov int err; 33461da177e4SLinus Torvalds char *kaddr; 334754566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 334854566b2cSNick Piggin if (nofs) 334954566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 33501da177e4SLinus Torvalds 33517e53cac4SNeilBrown retry: 3352afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 335354566b2cSNick Piggin flags, &page, &fsdata); 33541da177e4SLinus Torvalds if (err) 3355afddba49SNick Piggin goto fail; 3356afddba49SNick Piggin 33571da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 33581da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 33591da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 3360afddba49SNick Piggin 3361afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 3362afddba49SNick Piggin page, fsdata); 33631da177e4SLinus Torvalds if (err < 0) 33641da177e4SLinus Torvalds goto fail; 3365afddba49SNick Piggin if (err < len-1) 3366afddba49SNick Piggin goto retry; 3367afddba49SNick Piggin 33681da177e4SLinus Torvalds mark_inode_dirty(inode); 33691da177e4SLinus Torvalds return 0; 33701da177e4SLinus Torvalds fail: 33711da177e4SLinus Torvalds return err; 33721da177e4SLinus Torvalds } 33731da177e4SLinus Torvalds 33740adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 33750adb25d2SKirill Korotaev { 33760adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 337754566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 33780adb25d2SKirill Korotaev } 33790adb25d2SKirill Korotaev 338092e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 33811da177e4SLinus Torvalds .readlink = generic_readlink, 33821da177e4SLinus Torvalds .follow_link = page_follow_link_light, 33831da177e4SLinus Torvalds .put_link = page_put_link, 33841da177e4SLinus Torvalds }; 33851da177e4SLinus Torvalds 33862d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 33871da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 33881da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 33891da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 33901da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 33911da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 33921da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 33931da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 33941da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 33951da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 33960adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 33971da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 33981da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 33991da177e4SLinus Torvalds EXPORT_SYMBOL(path_lookup); 3400d1811465SAl Viro EXPORT_SYMBOL(kern_path); 340116f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 3402f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 34038c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission); 34041da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 34051da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 34061da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 34071da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 34081da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 34091da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 34101da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 34111da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 34121da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 34131da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 34141da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 34151da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 34161da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 34171da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 3418