11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/namei.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* 81da177e4SLinus Torvalds * Some corrections by tytso. 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname 121da177e4SLinus Torvalds * lookup logic. 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture. 151da177e4SLinus Torvalds */ 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #include <linux/init.h> 181da177e4SLinus Torvalds #include <linux/module.h> 191da177e4SLinus Torvalds #include <linux/slab.h> 201da177e4SLinus Torvalds #include <linux/fs.h> 211da177e4SLinus Torvalds #include <linux/namei.h> 221da177e4SLinus Torvalds #include <linux/pagemap.h> 230eeca283SRobert Love #include <linux/fsnotify.h> 241da177e4SLinus Torvalds #include <linux/personality.h> 251da177e4SLinus Torvalds #include <linux/security.h> 266146f0d5SMimi Zohar #include <linux/ima.h> 271da177e4SLinus Torvalds #include <linux/syscalls.h> 281da177e4SLinus Torvalds #include <linux/mount.h> 291da177e4SLinus Torvalds #include <linux/audit.h> 3016f7e0feSRandy Dunlap #include <linux/capability.h> 31834f2a4aSTrond Myklebust #include <linux/file.h> 325590ff0dSUlrich Drepper #include <linux/fcntl.h> 3308ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h> 345ad4e53bSAl Viro #include <linux/fs_struct.h> 351da177e4SLinus Torvalds #include <asm/uaccess.h> 361da177e4SLinus Torvalds 37e81e3f4dSEric Paris #include "internal.h" 38e81e3f4dSEric Paris 391da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer] 401da177e4SLinus Torvalds * Fundamental changes in the pathname lookup mechanisms (namei) 411da177e4SLinus Torvalds * were necessary because of omirr. The reason is that omirr needs 421da177e4SLinus Torvalds * to know the _real_ pathname, not the user-supplied one, in case 431da177e4SLinus Torvalds * of symlinks (and also when transname replacements occur). 441da177e4SLinus Torvalds * 451da177e4SLinus Torvalds * The new code replaces the old recursive symlink resolution with 461da177e4SLinus Torvalds * an iterative one (in case of non-nested symlink chains). It does 471da177e4SLinus Torvalds * this with calls to <fs>_follow_link(). 481da177e4SLinus Torvalds * As a side effect, dir_namei(), _namei() and follow_link() are now 491da177e4SLinus Torvalds * replaced with a single function lookup_dentry() that can handle all 501da177e4SLinus Torvalds * the special cases of the former code. 511da177e4SLinus Torvalds * 521da177e4SLinus Torvalds * With the new dcache, the pathname is stored at each inode, at least as 531da177e4SLinus Torvalds * long as the refcount of the inode is positive. As a side effect, the 541da177e4SLinus Torvalds * size of the dcache depends on the inode cache and thus is dynamic. 551da177e4SLinus Torvalds * 561da177e4SLinus Torvalds * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink 571da177e4SLinus Torvalds * resolution to correspond with current state of the code. 581da177e4SLinus Torvalds * 591da177e4SLinus Torvalds * Note that the symlink resolution is not *completely* iterative. 601da177e4SLinus Torvalds * There is still a significant amount of tail- and mid- recursion in 611da177e4SLinus Torvalds * the algorithm. Also, note that <fs>_readlink() is not used in 621da177e4SLinus Torvalds * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() 631da177e4SLinus Torvalds * may return different results than <fs>_follow_link(). Many virtual 641da177e4SLinus Torvalds * filesystems (including /proc) exhibit this behavior. 651da177e4SLinus Torvalds */ 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: 681da177e4SLinus Torvalds * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL 691da177e4SLinus Torvalds * and the name already exists in form of a symlink, try to create the new 701da177e4SLinus Torvalds * name indicated by the symlink. The old code always complained that the 711da177e4SLinus Torvalds * name already exists, due to not following the symlink even if its target 721da177e4SLinus Torvalds * is nonexistent. The new semantics affects also mknod() and link() when 731da177e4SLinus Torvalds * the name is a symlink pointing to a non-existant name. 741da177e4SLinus Torvalds * 751da177e4SLinus Torvalds * I don't know which semantics is the right one, since I have no access 761da177e4SLinus Torvalds * to standards. But I found by trial that HP-UX 9.0 has the full "new" 771da177e4SLinus Torvalds * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the 781da177e4SLinus Torvalds * "old" one. Personally, I think the new semantics is much more logical. 791da177e4SLinus Torvalds * Note that "ln old new" where "new" is a symlink pointing to a non-existing 801da177e4SLinus Torvalds * file does succeed in both HP-UX and SunOs, but not in Solaris 811da177e4SLinus Torvalds * and in the old Linux semantics. 821da177e4SLinus Torvalds */ 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink 851da177e4SLinus Torvalds * semantics. See the comments in "open_namei" and "do_link" below. 861da177e4SLinus Torvalds * 871da177e4SLinus Torvalds * [10-Sep-98 Alan Modra] Another symlink change. 881da177e4SLinus Torvalds */ 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: 911da177e4SLinus Torvalds * inside the path - always follow. 921da177e4SLinus Torvalds * in the last component in creation/removal/renaming - never follow. 931da177e4SLinus Torvalds * if LOOKUP_FOLLOW passed - follow. 941da177e4SLinus Torvalds * if the pathname has trailing slashes - follow. 951da177e4SLinus Torvalds * otherwise - don't follow. 961da177e4SLinus Torvalds * (applied in that order). 971da177e4SLinus Torvalds * 981da177e4SLinus Torvalds * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT 991da177e4SLinus Torvalds * restored for 2.4. This is the last surviving part of old 4.2BSD bug. 1001da177e4SLinus Torvalds * During the 2.4 we need to fix the userland stuff depending on it - 1011da177e4SLinus Torvalds * hopefully we will be able to get rid of that wart in 2.5. So far only 1021da177e4SLinus Torvalds * XEmacs seems to be relying on it... 1031da177e4SLinus Torvalds */ 1041da177e4SLinus Torvalds /* 1051da177e4SLinus Torvalds * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) 106a11f3a05SArjan van de Ven * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives 1071da177e4SLinus Torvalds * any extra contention... 1081da177e4SLinus Torvalds */ 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional 1111da177e4SLinus Torvalds * checking and hopefully speeding things up, we copy filenames to the 1121da177e4SLinus Torvalds * kernel data space before using them.. 1131da177e4SLinus Torvalds * 1141da177e4SLinus Torvalds * POSIX.1 2.4: an empty pathname is invalid (ENOENT). 1151da177e4SLinus Torvalds * PATH_MAX includes the nul terminator --RR. 1161da177e4SLinus Torvalds */ 117858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page) 1181da177e4SLinus Torvalds { 1191da177e4SLinus Torvalds int retval; 1201da177e4SLinus Torvalds unsigned long len = PATH_MAX; 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds if (!segment_eq(get_fs(), KERNEL_DS)) { 1231da177e4SLinus Torvalds if ((unsigned long) filename >= TASK_SIZE) 1241da177e4SLinus Torvalds return -EFAULT; 1251da177e4SLinus Torvalds if (TASK_SIZE - (unsigned long) filename < PATH_MAX) 1261da177e4SLinus Torvalds len = TASK_SIZE - (unsigned long) filename; 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds retval = strncpy_from_user(page, filename, len); 1301da177e4SLinus Torvalds if (retval > 0) { 1311da177e4SLinus Torvalds if (retval < len) 1321da177e4SLinus Torvalds return 0; 1331da177e4SLinus Torvalds return -ENAMETOOLONG; 1341da177e4SLinus Torvalds } else if (!retval) 1351da177e4SLinus Torvalds retval = -ENOENT; 1361da177e4SLinus Torvalds return retval; 1371da177e4SLinus Torvalds } 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds char * getname(const char __user * filename) 1401da177e4SLinus Torvalds { 1411da177e4SLinus Torvalds char *tmp, *result; 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds result = ERR_PTR(-ENOMEM); 1441da177e4SLinus Torvalds tmp = __getname(); 1451da177e4SLinus Torvalds if (tmp) { 1461da177e4SLinus Torvalds int retval = do_getname(filename, tmp); 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds result = tmp; 1491da177e4SLinus Torvalds if (retval < 0) { 1501da177e4SLinus Torvalds __putname(tmp); 1511da177e4SLinus Torvalds result = ERR_PTR(retval); 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds audit_getname(result); 1551da177e4SLinus Torvalds return result; 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 1591da177e4SLinus Torvalds void putname(const char *name) 1601da177e4SLinus Torvalds { 1615ac3a9c2SAl Viro if (unlikely(!audit_dummy_context())) 1621da177e4SLinus Torvalds audit_putname(name); 1631da177e4SLinus Torvalds else 1641da177e4SLinus Torvalds __putname(name); 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds EXPORT_SYMBOL(putname); 1671da177e4SLinus Torvalds #endif 1681da177e4SLinus Torvalds 1695909ccaaSLinus Torvalds /* 1705909ccaaSLinus Torvalds * This does basic POSIX ACL permission checking 1715909ccaaSLinus Torvalds */ 17231e6b01fSNick Piggin static inline int __acl_permission_check(struct inode *inode, int mask, 17331e6b01fSNick Piggin int (*check_acl)(struct inode *inode, int mask), int rcu) 1745909ccaaSLinus Torvalds { 1755909ccaaSLinus Torvalds umode_t mode = inode->i_mode; 1765909ccaaSLinus Torvalds 1775909ccaaSLinus Torvalds mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 1785909ccaaSLinus Torvalds 1795909ccaaSLinus Torvalds if (current_fsuid() == inode->i_uid) 1805909ccaaSLinus Torvalds mode >>= 6; 1815909ccaaSLinus Torvalds else { 1825909ccaaSLinus Torvalds if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) { 18331e6b01fSNick Piggin if (rcu) { 18431e6b01fSNick Piggin return -ECHILD; 18531e6b01fSNick Piggin } else { 1865909ccaaSLinus Torvalds int error = check_acl(inode, mask); 1875909ccaaSLinus Torvalds if (error != -EAGAIN) 1885909ccaaSLinus Torvalds return error; 1895909ccaaSLinus Torvalds } 19031e6b01fSNick Piggin } 1915909ccaaSLinus Torvalds 1925909ccaaSLinus Torvalds if (in_group_p(inode->i_gid)) 1935909ccaaSLinus Torvalds mode >>= 3; 1945909ccaaSLinus Torvalds } 1955909ccaaSLinus Torvalds 1965909ccaaSLinus Torvalds /* 1975909ccaaSLinus Torvalds * If the DACs are ok we don't need any capability check. 1985909ccaaSLinus Torvalds */ 1995909ccaaSLinus Torvalds if ((mask & ~mode) == 0) 2005909ccaaSLinus Torvalds return 0; 2015909ccaaSLinus Torvalds return -EACCES; 2025909ccaaSLinus Torvalds } 2031da177e4SLinus Torvalds 20431e6b01fSNick Piggin static inline int acl_permission_check(struct inode *inode, int mask, 20531e6b01fSNick Piggin int (*check_acl)(struct inode *inode, int mask)) 20631e6b01fSNick Piggin { 20731e6b01fSNick Piggin return __acl_permission_check(inode, mask, check_acl, 0); 20831e6b01fSNick Piggin } 20931e6b01fSNick Piggin 2101da177e4SLinus Torvalds /** 2111da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 2121da177e4SLinus Torvalds * @inode: inode to check access rights for 2131da177e4SLinus Torvalds * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 2141da177e4SLinus Torvalds * @check_acl: optional callback to check for Posix ACLs 2151da177e4SLinus Torvalds * 2161da177e4SLinus Torvalds * Used to check for read/write/execute permissions on a file. 2171da177e4SLinus Torvalds * We use "fsuid" for this, letting us set arbitrary permissions 2181da177e4SLinus Torvalds * for filesystem access without changing the "normal" uids which 2191da177e4SLinus Torvalds * are used for other things.. 2201da177e4SLinus Torvalds */ 2211da177e4SLinus Torvalds int generic_permission(struct inode *inode, int mask, 2221da177e4SLinus Torvalds int (*check_acl)(struct inode *inode, int mask)) 2231da177e4SLinus Torvalds { 2245909ccaaSLinus Torvalds int ret; 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds /* 2275909ccaaSLinus Torvalds * Do the basic POSIX ACL permission checks. 2281da177e4SLinus Torvalds */ 2295909ccaaSLinus Torvalds ret = acl_permission_check(inode, mask, check_acl); 2305909ccaaSLinus Torvalds if (ret != -EACCES) 2315909ccaaSLinus Torvalds return ret; 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds /* 2341da177e4SLinus Torvalds * Read/write DACs are always overridable. 2351da177e4SLinus Torvalds * Executable DACs are overridable if at least one exec bit is set. 2361da177e4SLinus Torvalds */ 237f696a365SMiklos Szeredi if (!(mask & MAY_EXEC) || execute_ok(inode)) 2381da177e4SLinus Torvalds if (capable(CAP_DAC_OVERRIDE)) 2391da177e4SLinus Torvalds return 0; 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds /* 2421da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 2431da177e4SLinus Torvalds */ 2447ea66001SSerge E. Hallyn mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 2451da177e4SLinus Torvalds if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE))) 2461da177e4SLinus Torvalds if (capable(CAP_DAC_READ_SEARCH)) 2471da177e4SLinus Torvalds return 0; 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds return -EACCES; 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds 252cb23beb5SChristoph Hellwig /** 253cb23beb5SChristoph Hellwig * inode_permission - check for access rights to a given inode 254cb23beb5SChristoph Hellwig * @inode: inode to check permission on 255cb23beb5SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 256cb23beb5SChristoph Hellwig * 257cb23beb5SChristoph Hellwig * Used to check for read/write/execute permissions on an inode. 258cb23beb5SChristoph Hellwig * We use "fsuid" for this, letting us set arbitrary permissions 259cb23beb5SChristoph Hellwig * for filesystem access without changing the "normal" uids which 260cb23beb5SChristoph Hellwig * are used for other things. 261cb23beb5SChristoph Hellwig */ 262f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask) 2631da177e4SLinus Torvalds { 264e6305c43SAl Viro int retval; 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds if (mask & MAY_WRITE) { 26722590e41SMiklos Szeredi umode_t mode = inode->i_mode; 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds /* 2701da177e4SLinus Torvalds * Nobody gets write access to a read-only fs. 2711da177e4SLinus Torvalds */ 2721da177e4SLinus Torvalds if (IS_RDONLY(inode) && 2731da177e4SLinus Torvalds (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 2741da177e4SLinus Torvalds return -EROFS; 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds /* 2771da177e4SLinus Torvalds * Nobody gets write access to an immutable file. 2781da177e4SLinus Torvalds */ 2791da177e4SLinus Torvalds if (IS_IMMUTABLE(inode)) 2801da177e4SLinus Torvalds return -EACCES; 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds 283acfa4380SAl Viro if (inode->i_op->permission) 284b77b0646SAl Viro retval = inode->i_op->permission(inode, mask); 285f696a365SMiklos Szeredi else 2865909ccaaSLinus Torvalds retval = generic_permission(inode, mask, inode->i_op->check_acl); 287f696a365SMiklos Szeredi 2881da177e4SLinus Torvalds if (retval) 2891da177e4SLinus Torvalds return retval; 2901da177e4SLinus Torvalds 29108ce5f16SSerge E. Hallyn retval = devcgroup_inode_permission(inode, mask); 29208ce5f16SSerge E. Hallyn if (retval) 29308ce5f16SSerge E. Hallyn return retval; 29408ce5f16SSerge E. Hallyn 295d09ca739SEric Paris return security_inode_permission(inode, mask); 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 298e4543eddSChristoph Hellwig /** 2998c744fb8SChristoph Hellwig * file_permission - check for additional access rights to a given file 3008c744fb8SChristoph Hellwig * @file: file to check access rights for 3018c744fb8SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 3028c744fb8SChristoph Hellwig * 3038c744fb8SChristoph Hellwig * Used to check for read/write/execute permissions on an already opened 3048c744fb8SChristoph Hellwig * file. 3058c744fb8SChristoph Hellwig * 3068c744fb8SChristoph Hellwig * Note: 3078c744fb8SChristoph Hellwig * Do not use this function in new code. All access checks should 308cb23beb5SChristoph Hellwig * be done using inode_permission(). 3098c744fb8SChristoph Hellwig */ 3108c744fb8SChristoph Hellwig int file_permission(struct file *file, int mask) 3118c744fb8SChristoph Hellwig { 312f419a2e3SAl Viro return inode_permission(file->f_path.dentry->d_inode, mask); 3138c744fb8SChristoph Hellwig } 3148c744fb8SChristoph Hellwig 3151da177e4SLinus Torvalds /* 3161da177e4SLinus Torvalds * get_write_access() gets write permission for a file. 3171da177e4SLinus Torvalds * put_write_access() releases this write permission. 3181da177e4SLinus Torvalds * This is used for regular files. 3191da177e4SLinus Torvalds * We cannot support write (and maybe mmap read-write shared) accesses and 3201da177e4SLinus Torvalds * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode 3211da177e4SLinus Torvalds * can have the following values: 3221da177e4SLinus Torvalds * 0: no writers, no VM_DENYWRITE mappings 3231da177e4SLinus Torvalds * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist 3241da177e4SLinus Torvalds * > 0: (i_writecount) users are writing to the file. 3251da177e4SLinus Torvalds * 3261da177e4SLinus Torvalds * Normally we operate on that counter with atomic_{inc,dec} and it's safe 3271da177e4SLinus Torvalds * except for the cases where we don't hold i_writecount yet. Then we need to 3281da177e4SLinus Torvalds * use {get,deny}_write_access() - these functions check the sign and refuse 3291da177e4SLinus Torvalds * to do the change if sign is wrong. Exclusion between them is provided by 3301da177e4SLinus Torvalds * the inode->i_lock spinlock. 3311da177e4SLinus Torvalds */ 3321da177e4SLinus Torvalds 3331da177e4SLinus Torvalds int get_write_access(struct inode * inode) 3341da177e4SLinus Torvalds { 3351da177e4SLinus Torvalds spin_lock(&inode->i_lock); 3361da177e4SLinus Torvalds if (atomic_read(&inode->i_writecount) < 0) { 3371da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3381da177e4SLinus Torvalds return -ETXTBSY; 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds atomic_inc(&inode->i_writecount); 3411da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds return 0; 3441da177e4SLinus Torvalds } 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds int deny_write_access(struct file * file) 3471da177e4SLinus Torvalds { 3480f7fc9e4SJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds spin_lock(&inode->i_lock); 3511da177e4SLinus Torvalds if (atomic_read(&inode->i_writecount) > 0) { 3521da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3531da177e4SLinus Torvalds return -ETXTBSY; 3541da177e4SLinus Torvalds } 3551da177e4SLinus Torvalds atomic_dec(&inode->i_writecount); 3561da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds return 0; 3591da177e4SLinus Torvalds } 3601da177e4SLinus Torvalds 3611d957f9bSJan Blunck /** 3625dd784d0SJan Blunck * path_get - get a reference to a path 3635dd784d0SJan Blunck * @path: path to get the reference to 3645dd784d0SJan Blunck * 3655dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 3665dd784d0SJan Blunck */ 3675dd784d0SJan Blunck void path_get(struct path *path) 3685dd784d0SJan Blunck { 3695dd784d0SJan Blunck mntget(path->mnt); 3705dd784d0SJan Blunck dget(path->dentry); 3715dd784d0SJan Blunck } 3725dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 3735dd784d0SJan Blunck 3745dd784d0SJan Blunck /** 3751d957f9bSJan Blunck * path_put - put a reference to a path 3761d957f9bSJan Blunck * @path: path to put the reference to 3771d957f9bSJan Blunck * 3781d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 3791d957f9bSJan Blunck */ 3801d957f9bSJan Blunck void path_put(struct path *path) 3811da177e4SLinus Torvalds { 3821d957f9bSJan Blunck dput(path->dentry); 3831d957f9bSJan Blunck mntput(path->mnt); 3841da177e4SLinus Torvalds } 3851d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 3861da177e4SLinus Torvalds 387834f2a4aSTrond Myklebust /** 38831e6b01fSNick Piggin * nameidata_drop_rcu - drop this nameidata out of rcu-walk 38931e6b01fSNick Piggin * @nd: nameidata pathwalk data to drop 39031e6b01fSNick Piggin * @Returns: 0 on success, -ECHLID on failure 39131e6b01fSNick Piggin * 39231e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 39331e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). __drop_rcu* functions attempt 39431e6b01fSNick Piggin * to drop out of rcu-walk mode and take normal reference counts on dentries 39531e6b01fSNick Piggin * and vfsmounts to transition to rcu-walk mode. __drop_rcu* functions take 39631e6b01fSNick Piggin * refcounts at the last known good point before rcu-walk got stuck, so 39731e6b01fSNick Piggin * ref-walk may continue from there. If this is not successful (eg. a seqcount 39831e6b01fSNick Piggin * has changed), then failure is returned and path walk restarts from the 39931e6b01fSNick Piggin * beginning in ref-walk mode. 40031e6b01fSNick Piggin * 40131e6b01fSNick Piggin * nameidata_drop_rcu attempts to drop the current nd->path and nd->root into 40231e6b01fSNick Piggin * ref-walk. Must be called from rcu-walk context. 40331e6b01fSNick Piggin */ 40431e6b01fSNick Piggin static int nameidata_drop_rcu(struct nameidata *nd) 40531e6b01fSNick Piggin { 40631e6b01fSNick Piggin struct fs_struct *fs = current->fs; 40731e6b01fSNick Piggin struct dentry *dentry = nd->path.dentry; 40831e6b01fSNick Piggin 40931e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 41031e6b01fSNick Piggin if (nd->root.mnt) { 41131e6b01fSNick Piggin spin_lock(&fs->lock); 41231e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 41331e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 41431e6b01fSNick Piggin goto err_root; 41531e6b01fSNick Piggin } 41631e6b01fSNick Piggin spin_lock(&dentry->d_lock); 41731e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 41831e6b01fSNick Piggin goto err; 41931e6b01fSNick Piggin BUG_ON(nd->inode != dentry->d_inode); 42031e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 42131e6b01fSNick Piggin if (nd->root.mnt) { 42231e6b01fSNick Piggin path_get(&nd->root); 42331e6b01fSNick Piggin spin_unlock(&fs->lock); 42431e6b01fSNick Piggin } 42531e6b01fSNick Piggin mntget(nd->path.mnt); 42631e6b01fSNick Piggin 42731e6b01fSNick Piggin rcu_read_unlock(); 42831e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 42931e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 43031e6b01fSNick Piggin return 0; 43131e6b01fSNick Piggin err: 43231e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 43331e6b01fSNick Piggin err_root: 43431e6b01fSNick Piggin if (nd->root.mnt) 43531e6b01fSNick Piggin spin_unlock(&fs->lock); 43631e6b01fSNick Piggin return -ECHILD; 43731e6b01fSNick Piggin } 43831e6b01fSNick Piggin 43931e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */ 44031e6b01fSNick Piggin static inline int nameidata_drop_rcu_maybe(struct nameidata *nd) 44131e6b01fSNick Piggin { 44231e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) 44331e6b01fSNick Piggin return nameidata_drop_rcu(nd); 44431e6b01fSNick Piggin return 0; 44531e6b01fSNick Piggin } 44631e6b01fSNick Piggin 44731e6b01fSNick Piggin /** 44831e6b01fSNick Piggin * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk 44931e6b01fSNick Piggin * @nd: nameidata pathwalk data to drop 45031e6b01fSNick Piggin * @dentry: dentry to drop 45131e6b01fSNick Piggin * @Returns: 0 on success, -ECHLID on failure 45231e6b01fSNick Piggin * 45331e6b01fSNick Piggin * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root, 45431e6b01fSNick Piggin * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on 45531e6b01fSNick Piggin * @nd. Must be called from rcu-walk context. 45631e6b01fSNick Piggin */ 45731e6b01fSNick Piggin static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry) 45831e6b01fSNick Piggin { 45931e6b01fSNick Piggin struct fs_struct *fs = current->fs; 46031e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 46131e6b01fSNick Piggin 46231e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 46331e6b01fSNick Piggin if (nd->root.mnt) { 46431e6b01fSNick Piggin spin_lock(&fs->lock); 46531e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 46631e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 46731e6b01fSNick Piggin goto err_root; 46831e6b01fSNick Piggin } 46931e6b01fSNick Piggin spin_lock(&parent->d_lock); 47031e6b01fSNick Piggin spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); 47131e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 47231e6b01fSNick Piggin goto err; 47331e6b01fSNick Piggin /* 47431e6b01fSNick Piggin * If the sequence check on the child dentry passed, then the child has 47531e6b01fSNick Piggin * not been removed from its parent. This means the parent dentry must 47631e6b01fSNick Piggin * be valid and able to take a reference at this point. 47731e6b01fSNick Piggin */ 47831e6b01fSNick Piggin BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); 47931e6b01fSNick Piggin BUG_ON(!parent->d_count); 48031e6b01fSNick Piggin parent->d_count++; 48131e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 48231e6b01fSNick Piggin spin_unlock(&parent->d_lock); 48331e6b01fSNick Piggin if (nd->root.mnt) { 48431e6b01fSNick Piggin path_get(&nd->root); 48531e6b01fSNick Piggin spin_unlock(&fs->lock); 48631e6b01fSNick Piggin } 48731e6b01fSNick Piggin mntget(nd->path.mnt); 48831e6b01fSNick Piggin 48931e6b01fSNick Piggin rcu_read_unlock(); 49031e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 49131e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 49231e6b01fSNick Piggin return 0; 49331e6b01fSNick Piggin err: 49431e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 49531e6b01fSNick Piggin spin_unlock(&parent->d_lock); 49631e6b01fSNick Piggin err_root: 49731e6b01fSNick Piggin if (nd->root.mnt) 49831e6b01fSNick Piggin spin_unlock(&fs->lock); 49931e6b01fSNick Piggin return -ECHILD; 50031e6b01fSNick Piggin } 50131e6b01fSNick Piggin 50231e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */ 50331e6b01fSNick Piggin static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry) 50431e6b01fSNick Piggin { 50531e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) 50631e6b01fSNick Piggin return nameidata_dentry_drop_rcu(nd, dentry); 50731e6b01fSNick Piggin return 0; 50831e6b01fSNick Piggin } 50931e6b01fSNick Piggin 51031e6b01fSNick Piggin /** 51131e6b01fSNick Piggin * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk 51231e6b01fSNick Piggin * @nd: nameidata pathwalk data to drop 51331e6b01fSNick Piggin * @Returns: 0 on success, -ECHLID on failure 51431e6b01fSNick Piggin * 51531e6b01fSNick Piggin * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk. 51631e6b01fSNick Piggin * nd->path should be the final element of the lookup, so nd->root is discarded. 51731e6b01fSNick Piggin * Must be called from rcu-walk context. 51831e6b01fSNick Piggin */ 51931e6b01fSNick Piggin static int nameidata_drop_rcu_last(struct nameidata *nd) 52031e6b01fSNick Piggin { 52131e6b01fSNick Piggin struct dentry *dentry = nd->path.dentry; 52231e6b01fSNick Piggin 52331e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 52431e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 52531e6b01fSNick Piggin nd->root.mnt = NULL; 52631e6b01fSNick Piggin spin_lock(&dentry->d_lock); 52731e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 52831e6b01fSNick Piggin goto err_unlock; 52931e6b01fSNick Piggin BUG_ON(nd->inode != dentry->d_inode); 53031e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 53131e6b01fSNick Piggin 53231e6b01fSNick Piggin mntget(nd->path.mnt); 53331e6b01fSNick Piggin 53431e6b01fSNick Piggin rcu_read_unlock(); 53531e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 53631e6b01fSNick Piggin 53731e6b01fSNick Piggin return 0; 53831e6b01fSNick Piggin 53931e6b01fSNick Piggin err_unlock: 54031e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 54131e6b01fSNick Piggin rcu_read_unlock(); 54231e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 54331e6b01fSNick Piggin return -ECHILD; 54431e6b01fSNick Piggin } 54531e6b01fSNick Piggin 54631e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */ 54731e6b01fSNick Piggin static inline int nameidata_drop_rcu_last_maybe(struct nameidata *nd) 54831e6b01fSNick Piggin { 54931e6b01fSNick Piggin if (likely(nd->flags & LOOKUP_RCU)) 55031e6b01fSNick Piggin return nameidata_drop_rcu_last(nd); 55131e6b01fSNick Piggin return 0; 55231e6b01fSNick Piggin } 55331e6b01fSNick Piggin 55431e6b01fSNick Piggin /** 555834f2a4aSTrond Myklebust * release_open_intent - free up open intent resources 556834f2a4aSTrond Myklebust * @nd: pointer to nameidata 557834f2a4aSTrond Myklebust */ 558834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd) 559834f2a4aSTrond Myklebust { 5600f7fc9e4SJosef "Jeff" Sipek if (nd->intent.open.file->f_path.dentry == NULL) 561834f2a4aSTrond Myklebust put_filp(nd->intent.open.file); 562834f2a4aSTrond Myklebust else 563834f2a4aSTrond Myklebust fput(nd->intent.open.file); 564834f2a4aSTrond Myklebust } 565834f2a4aSTrond Myklebust 566bcdc5e01SIan Kent static inline struct dentry * 567bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd) 568bcdc5e01SIan Kent { 569bcdc5e01SIan Kent int status = dentry->d_op->d_revalidate(dentry, nd); 570bcdc5e01SIan Kent if (unlikely(status <= 0)) { 571bcdc5e01SIan Kent /* 572bcdc5e01SIan Kent * The dentry failed validation. 573bcdc5e01SIan Kent * If d_revalidate returned 0 attempt to invalidate 574bcdc5e01SIan Kent * the dentry otherwise d_revalidate is asking us 575bcdc5e01SIan Kent * to return a fail status. 576bcdc5e01SIan Kent */ 577bcdc5e01SIan Kent if (!status) { 578bcdc5e01SIan Kent if (!d_invalidate(dentry)) { 579bcdc5e01SIan Kent dput(dentry); 580bcdc5e01SIan Kent dentry = NULL; 581bcdc5e01SIan Kent } 582bcdc5e01SIan Kent } else { 583bcdc5e01SIan Kent dput(dentry); 584bcdc5e01SIan Kent dentry = ERR_PTR(status); 585bcdc5e01SIan Kent } 586bcdc5e01SIan Kent } 587bcdc5e01SIan Kent return dentry; 588bcdc5e01SIan Kent } 589bcdc5e01SIan Kent 5901da177e4SLinus Torvalds /* 59139159de2SJeff Layton * force_reval_path - force revalidation of a dentry 59239159de2SJeff Layton * 59339159de2SJeff Layton * In some situations the path walking code will trust dentries without 59439159de2SJeff Layton * revalidating them. This causes problems for filesystems that depend on 59539159de2SJeff Layton * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set 59639159de2SJeff Layton * (which indicates that it's possible for the dentry to go stale), force 59739159de2SJeff Layton * a d_revalidate call before proceeding. 59839159de2SJeff Layton * 59939159de2SJeff Layton * Returns 0 if the revalidation was successful. If the revalidation fails, 60039159de2SJeff Layton * either return the error returned by d_revalidate or -ESTALE if the 60139159de2SJeff Layton * revalidation it just returned 0. If d_revalidate returns 0, we attempt to 60239159de2SJeff Layton * invalidate the dentry. It's up to the caller to handle putting references 60339159de2SJeff Layton * to the path if necessary. 60439159de2SJeff Layton */ 60539159de2SJeff Layton static int 60639159de2SJeff Layton force_reval_path(struct path *path, struct nameidata *nd) 60739159de2SJeff Layton { 60839159de2SJeff Layton int status; 60939159de2SJeff Layton struct dentry *dentry = path->dentry; 61039159de2SJeff Layton 61139159de2SJeff Layton /* 61239159de2SJeff Layton * only check on filesystems where it's possible for the dentry to 61339159de2SJeff Layton * become stale. It's assumed that if this flag is set then the 61439159de2SJeff Layton * d_revalidate op will also be defined. 61539159de2SJeff Layton */ 61639159de2SJeff Layton if (!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) 61739159de2SJeff Layton return 0; 61839159de2SJeff Layton 61939159de2SJeff Layton status = dentry->d_op->d_revalidate(dentry, nd); 62039159de2SJeff Layton if (status > 0) 62139159de2SJeff Layton return 0; 62239159de2SJeff Layton 62339159de2SJeff Layton if (!status) { 62439159de2SJeff Layton d_invalidate(dentry); 62539159de2SJeff Layton status = -ESTALE; 62639159de2SJeff Layton } 62739159de2SJeff Layton return status; 62839159de2SJeff Layton } 62939159de2SJeff Layton 63039159de2SJeff Layton /* 631b75b5086SAl Viro * Short-cut version of permission(), for calling on directories 632b75b5086SAl Viro * during pathname resolution. Combines parts of permission() 633b75b5086SAl Viro * and generic_permission(), and tests ONLY for MAY_EXEC permission. 6341da177e4SLinus Torvalds * 6351da177e4SLinus Torvalds * If appropriate, check DAC only. If not appropriate, or 636b75b5086SAl Viro * short-cut DAC fails, then call ->permission() to do more 6371da177e4SLinus Torvalds * complete permission check. 6381da177e4SLinus Torvalds */ 63931e6b01fSNick Piggin static inline int __exec_permission(struct inode *inode, int rcu) 6401da177e4SLinus Torvalds { 6415909ccaaSLinus Torvalds int ret; 6421da177e4SLinus Torvalds 643cb9179eaSLinus Torvalds if (inode->i_op->permission) { 64431e6b01fSNick Piggin if (rcu) 64531e6b01fSNick Piggin return -ECHILD; 6465909ccaaSLinus Torvalds ret = inode->i_op->permission(inode, MAY_EXEC); 647cb9179eaSLinus Torvalds if (!ret) 648cb9179eaSLinus Torvalds goto ok; 649cb9179eaSLinus Torvalds return ret; 650cb9179eaSLinus Torvalds } 65131e6b01fSNick Piggin ret = __acl_permission_check(inode, MAY_EXEC, inode->i_op->check_acl, rcu); 6525909ccaaSLinus Torvalds if (!ret) 6531da177e4SLinus Torvalds goto ok; 65431e6b01fSNick Piggin if (rcu && ret == -ECHILD) 65531e6b01fSNick Piggin return ret; 6561da177e4SLinus Torvalds 657f1ac9f6bSLinus Torvalds if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH)) 6581da177e4SLinus Torvalds goto ok; 6591da177e4SLinus Torvalds 6605909ccaaSLinus Torvalds return ret; 6611da177e4SLinus Torvalds ok: 66231e6b01fSNick Piggin return security_inode_exec_permission(inode, rcu); 66331e6b01fSNick Piggin } 66431e6b01fSNick Piggin 66531e6b01fSNick Piggin static int exec_permission(struct inode *inode) 66631e6b01fSNick Piggin { 66731e6b01fSNick Piggin return __exec_permission(inode, 0); 66831e6b01fSNick Piggin } 66931e6b01fSNick Piggin 67031e6b01fSNick Piggin static int exec_permission_rcu(struct inode *inode) 67131e6b01fSNick Piggin { 67231e6b01fSNick Piggin return __exec_permission(inode, 1); 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds 6752a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 6762a737871SAl Viro { 677f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 678f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 6792a737871SAl Viro } 6802a737871SAl Viro 6816de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 6826de88d72SAl Viro 68331e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 68431e6b01fSNick Piggin { 68531e6b01fSNick Piggin if (!nd->root.mnt) { 68631e6b01fSNick Piggin struct fs_struct *fs = current->fs; 68731e6b01fSNick Piggin spin_lock(&fs->lock); 68831e6b01fSNick Piggin nd->root = fs->root; 68931e6b01fSNick Piggin spin_unlock(&fs->lock); 69031e6b01fSNick Piggin } 69131e6b01fSNick Piggin } 69231e6b01fSNick Piggin 693f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 6941da177e4SLinus Torvalds { 69531e6b01fSNick Piggin int ret; 69631e6b01fSNick Piggin 6971da177e4SLinus Torvalds if (IS_ERR(link)) 6981da177e4SLinus Torvalds goto fail; 6991da177e4SLinus Torvalds 7001da177e4SLinus Torvalds if (*link == '/') { 7012a737871SAl Viro set_root(nd); 7021d957f9bSJan Blunck path_put(&nd->path); 7032a737871SAl Viro nd->path = nd->root; 7042a737871SAl Viro path_get(&nd->root); 7051da177e4SLinus Torvalds } 70631e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 707b4091d5fSChristoph Hellwig 70831e6b01fSNick Piggin ret = link_path_walk(link, nd); 70931e6b01fSNick Piggin return ret; 7101da177e4SLinus Torvalds fail: 7111d957f9bSJan Blunck path_put(&nd->path); 7121da177e4SLinus Torvalds return PTR_ERR(link); 7131da177e4SLinus Torvalds } 7141da177e4SLinus Torvalds 7151d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 716051d3812SIan Kent { 717051d3812SIan Kent dput(path->dentry); 7184ac91378SJan Blunck if (path->mnt != nd->path.mnt) 719051d3812SIan Kent mntput(path->mnt); 720051d3812SIan Kent } 721051d3812SIan Kent 722051d3812SIan Kent static inline void path_to_nameidata(struct path *path, struct nameidata *nd) 723051d3812SIan Kent { 72431e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 7254ac91378SJan Blunck dput(nd->path.dentry); 72631e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 7274ac91378SJan Blunck mntput(nd->path.mnt); 7289a229683SHuang Shijie } 72931e6b01fSNick Piggin nd->path.mnt = path->mnt; 7304ac91378SJan Blunck nd->path.dentry = path->dentry; 731051d3812SIan Kent } 732051d3812SIan Kent 733def4af30SAl Viro static __always_inline int 734def4af30SAl Viro __do_follow_link(struct path *path, struct nameidata *nd, void **p) 7351da177e4SLinus Torvalds { 7361da177e4SLinus Torvalds int error; 737cd4e91d3SAl Viro struct dentry *dentry = path->dentry; 7381da177e4SLinus Torvalds 739d671a1cbSAl Viro touch_atime(path->mnt, dentry); 7401da177e4SLinus Torvalds nd_set_link(nd, NULL); 741cd4e91d3SAl Viro 7424ac91378SJan Blunck if (path->mnt != nd->path.mnt) { 743051d3812SIan Kent path_to_nameidata(path, nd); 74431e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 745051d3812SIan Kent dget(dentry); 746051d3812SIan Kent } 747cd4e91d3SAl Viro mntget(path->mnt); 74831e6b01fSNick Piggin 74986acdca1SAl Viro nd->last_type = LAST_BIND; 750def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 751def4af30SAl Viro error = PTR_ERR(*p); 752def4af30SAl Viro if (!IS_ERR(*p)) { 7531da177e4SLinus Torvalds char *s = nd_get_link(nd); 754cc314eefSLinus Torvalds error = 0; 7551da177e4SLinus Torvalds if (s) 7561da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 75739159de2SJeff Layton else if (nd->last_type == LAST_BIND) { 75839159de2SJeff Layton error = force_reval_path(&nd->path, nd); 75939159de2SJeff Layton if (error) 76039159de2SJeff Layton path_put(&nd->path); 76139159de2SJeff Layton } 7621da177e4SLinus Torvalds } 7631da177e4SLinus Torvalds return error; 7641da177e4SLinus Torvalds } 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds /* 7671da177e4SLinus Torvalds * This limits recursive symlink follows to 8, while 7681da177e4SLinus Torvalds * limiting consecutive symlinks to 40. 7691da177e4SLinus Torvalds * 7701da177e4SLinus Torvalds * Without that kind of total limit, nasty chains of consecutive 7711da177e4SLinus Torvalds * symlinks can cause almost arbitrarily long lookups. 7721da177e4SLinus Torvalds */ 77390ebe565SAl Viro static inline int do_follow_link(struct path *path, struct nameidata *nd) 7741da177e4SLinus Torvalds { 775def4af30SAl Viro void *cookie; 7761da177e4SLinus Torvalds int err = -ELOOP; 7771da177e4SLinus Torvalds if (current->link_count >= MAX_NESTED_LINKS) 7781da177e4SLinus Torvalds goto loop; 7791da177e4SLinus Torvalds if (current->total_link_count >= 40) 7801da177e4SLinus Torvalds goto loop; 7811da177e4SLinus Torvalds BUG_ON(nd->depth >= MAX_NESTED_LINKS); 7821da177e4SLinus Torvalds cond_resched(); 78390ebe565SAl Viro err = security_inode_follow_link(path->dentry, nd); 7841da177e4SLinus Torvalds if (err) 7851da177e4SLinus Torvalds goto loop; 7861da177e4SLinus Torvalds current->link_count++; 7871da177e4SLinus Torvalds current->total_link_count++; 7881da177e4SLinus Torvalds nd->depth++; 789def4af30SAl Viro err = __do_follow_link(path, nd, &cookie); 790def4af30SAl Viro if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link) 791def4af30SAl Viro path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie); 792258fa999SAl Viro path_put(path); 7931da177e4SLinus Torvalds current->link_count--; 7941da177e4SLinus Torvalds nd->depth--; 7951da177e4SLinus Torvalds return err; 7961da177e4SLinus Torvalds loop: 7971d957f9bSJan Blunck path_put_conditional(path, nd); 7981d957f9bSJan Blunck path_put(&nd->path); 7991da177e4SLinus Torvalds return err; 8001da177e4SLinus Torvalds } 8011da177e4SLinus Torvalds 80231e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 80331e6b01fSNick Piggin { 80431e6b01fSNick Piggin struct vfsmount *parent; 80531e6b01fSNick Piggin struct dentry *mountpoint; 80631e6b01fSNick Piggin 80731e6b01fSNick Piggin parent = path->mnt->mnt_parent; 80831e6b01fSNick Piggin if (parent == path->mnt) 80931e6b01fSNick Piggin return 0; 81031e6b01fSNick Piggin mountpoint = path->mnt->mnt_mountpoint; 81131e6b01fSNick Piggin path->dentry = mountpoint; 81231e6b01fSNick Piggin path->mnt = parent; 81331e6b01fSNick Piggin return 1; 81431e6b01fSNick Piggin } 81531e6b01fSNick Piggin 816bab77ebfSAl Viro int follow_up(struct path *path) 8171da177e4SLinus Torvalds { 8181da177e4SLinus Torvalds struct vfsmount *parent; 8191da177e4SLinus Torvalds struct dentry *mountpoint; 82099b7db7bSNick Piggin 82199b7db7bSNick Piggin br_read_lock(vfsmount_lock); 822bab77ebfSAl Viro parent = path->mnt->mnt_parent; 823bab77ebfSAl Viro if (parent == path->mnt) { 82499b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 8251da177e4SLinus Torvalds return 0; 8261da177e4SLinus Torvalds } 8271da177e4SLinus Torvalds mntget(parent); 828bab77ebfSAl Viro mountpoint = dget(path->mnt->mnt_mountpoint); 82999b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 830bab77ebfSAl Viro dput(path->dentry); 831bab77ebfSAl Viro path->dentry = mountpoint; 832bab77ebfSAl Viro mntput(path->mnt); 833bab77ebfSAl Viro path->mnt = parent; 8341da177e4SLinus Torvalds return 1; 8351da177e4SLinus Torvalds } 8361da177e4SLinus Torvalds 837b5c84bf6SNick Piggin /* 838b5c84bf6SNick Piggin * serialization is taken care of in namespace.c 8391da177e4SLinus Torvalds */ 84031e6b01fSNick Piggin static void __follow_mount_rcu(struct nameidata *nd, struct path *path, 84131e6b01fSNick Piggin struct inode **inode) 84231e6b01fSNick Piggin { 84331e6b01fSNick Piggin while (d_mountpoint(path->dentry)) { 84431e6b01fSNick Piggin struct vfsmount *mounted; 84531e6b01fSNick Piggin mounted = __lookup_mnt(path->mnt, path->dentry, 1); 84631e6b01fSNick Piggin if (!mounted) 84731e6b01fSNick Piggin return; 84831e6b01fSNick Piggin path->mnt = mounted; 84931e6b01fSNick Piggin path->dentry = mounted->mnt_root; 85031e6b01fSNick Piggin nd->seq = read_seqcount_begin(&path->dentry->d_seq); 85131e6b01fSNick Piggin *inode = path->dentry->d_inode; 85231e6b01fSNick Piggin } 85331e6b01fSNick Piggin } 85431e6b01fSNick Piggin 855463ffb2eSAl Viro static int __follow_mount(struct path *path) 856463ffb2eSAl Viro { 857463ffb2eSAl Viro int res = 0; 858463ffb2eSAl Viro while (d_mountpoint(path->dentry)) { 8591c755af4SAl Viro struct vfsmount *mounted = lookup_mnt(path); 860463ffb2eSAl Viro if (!mounted) 861463ffb2eSAl Viro break; 862463ffb2eSAl Viro dput(path->dentry); 863463ffb2eSAl Viro if (res) 864463ffb2eSAl Viro mntput(path->mnt); 865463ffb2eSAl Viro path->mnt = mounted; 866463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 867463ffb2eSAl Viro res = 1; 868463ffb2eSAl Viro } 869463ffb2eSAl Viro return res; 870463ffb2eSAl Viro } 871463ffb2eSAl Viro 87279ed0226SAl Viro static void follow_mount(struct path *path) 8731da177e4SLinus Torvalds { 87479ed0226SAl Viro while (d_mountpoint(path->dentry)) { 8751c755af4SAl Viro struct vfsmount *mounted = lookup_mnt(path); 8761da177e4SLinus Torvalds if (!mounted) 8771da177e4SLinus Torvalds break; 87879ed0226SAl Viro dput(path->dentry); 87979ed0226SAl Viro mntput(path->mnt); 88079ed0226SAl Viro path->mnt = mounted; 88179ed0226SAl Viro path->dentry = dget(mounted->mnt_root); 8821da177e4SLinus Torvalds } 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds 8859393bd07SAl Viro int follow_down(struct path *path) 8861da177e4SLinus Torvalds { 8871da177e4SLinus Torvalds struct vfsmount *mounted; 8881da177e4SLinus Torvalds 8891c755af4SAl Viro mounted = lookup_mnt(path); 8901da177e4SLinus Torvalds if (mounted) { 8919393bd07SAl Viro dput(path->dentry); 8929393bd07SAl Viro mntput(path->mnt); 8939393bd07SAl Viro path->mnt = mounted; 8949393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 8951da177e4SLinus Torvalds return 1; 8961da177e4SLinus Torvalds } 8971da177e4SLinus Torvalds return 0; 8981da177e4SLinus Torvalds } 8991da177e4SLinus Torvalds 90031e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 90131e6b01fSNick Piggin { 90231e6b01fSNick Piggin struct inode *inode = nd->inode; 90331e6b01fSNick Piggin 90431e6b01fSNick Piggin set_root_rcu(nd); 90531e6b01fSNick Piggin 90631e6b01fSNick Piggin while(1) { 90731e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 90831e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 90931e6b01fSNick Piggin break; 91031e6b01fSNick Piggin } 91131e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 91231e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 91331e6b01fSNick Piggin struct dentry *parent = old->d_parent; 91431e6b01fSNick Piggin unsigned seq; 91531e6b01fSNick Piggin 91631e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 91731e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 91831e6b01fSNick Piggin return -ECHILD; 91931e6b01fSNick Piggin inode = parent->d_inode; 92031e6b01fSNick Piggin nd->path.dentry = parent; 92131e6b01fSNick Piggin nd->seq = seq; 92231e6b01fSNick Piggin break; 92331e6b01fSNick Piggin } 92431e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 92531e6b01fSNick Piggin break; 92631e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 92731e6b01fSNick Piggin inode = nd->path.dentry->d_inode; 92831e6b01fSNick Piggin } 92931e6b01fSNick Piggin __follow_mount_rcu(nd, &nd->path, &inode); 93031e6b01fSNick Piggin nd->inode = inode; 93131e6b01fSNick Piggin 93231e6b01fSNick Piggin return 0; 93331e6b01fSNick Piggin } 93431e6b01fSNick Piggin 93531e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 9361da177e4SLinus Torvalds { 9372a737871SAl Viro set_root(nd); 938e518ddb7SAndreas Mohr 9391da177e4SLinus Torvalds while(1) { 9404ac91378SJan Blunck struct dentry *old = nd->path.dentry; 9411da177e4SLinus Torvalds 9422a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 9432a737871SAl Viro nd->path.mnt == nd->root.mnt) { 9441da177e4SLinus Torvalds break; 9451da177e4SLinus Torvalds } 9464ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 9473088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 9483088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 9491da177e4SLinus Torvalds dput(old); 9501da177e4SLinus Torvalds break; 9511da177e4SLinus Torvalds } 9523088dd70SAl Viro if (!follow_up(&nd->path)) 9531da177e4SLinus Torvalds break; 9541da177e4SLinus Torvalds } 95579ed0226SAl Viro follow_mount(&nd->path); 95631e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds 9591da177e4SLinus Torvalds /* 960baa03890SNick Piggin * Allocate a dentry with name and parent, and perform a parent 961baa03890SNick Piggin * directory ->lookup on it. Returns the new dentry, or ERR_PTR 962baa03890SNick Piggin * on error. parent->d_inode->i_mutex must be held. d_lookup must 963baa03890SNick Piggin * have verified that no child exists while under i_mutex. 964baa03890SNick Piggin */ 965baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent, 966baa03890SNick Piggin struct qstr *name, struct nameidata *nd) 967baa03890SNick Piggin { 968baa03890SNick Piggin struct inode *inode = parent->d_inode; 969baa03890SNick Piggin struct dentry *dentry; 970baa03890SNick Piggin struct dentry *old; 971baa03890SNick Piggin 972baa03890SNick Piggin /* Don't create child dentry for a dead directory. */ 973baa03890SNick Piggin if (unlikely(IS_DEADDIR(inode))) 974baa03890SNick Piggin return ERR_PTR(-ENOENT); 975baa03890SNick Piggin 976baa03890SNick Piggin dentry = d_alloc(parent, name); 977baa03890SNick Piggin if (unlikely(!dentry)) 978baa03890SNick Piggin return ERR_PTR(-ENOMEM); 979baa03890SNick Piggin 980baa03890SNick Piggin old = inode->i_op->lookup(inode, dentry, nd); 981baa03890SNick Piggin if (unlikely(old)) { 982baa03890SNick Piggin dput(dentry); 983baa03890SNick Piggin dentry = old; 984baa03890SNick Piggin } 985baa03890SNick Piggin return dentry; 986baa03890SNick Piggin } 987baa03890SNick Piggin 988baa03890SNick Piggin /* 9891da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 9901da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 9911da177e4SLinus Torvalds * It _is_ time-critical. 9921da177e4SLinus Torvalds */ 9931da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name, 99431e6b01fSNick Piggin struct path *path, struct inode **inode) 9951da177e4SLinus Torvalds { 9964ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 99731e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 9986e6b1bd1SAl Viro struct inode *dir; 9993cac260aSAl Viro /* 10003cac260aSAl Viro * See if the low-level filesystem might want 10013cac260aSAl Viro * to use its own hash.. 10023cac260aSAl Viro */ 100331e6b01fSNick Piggin if (parent->d_op && parent->d_op->d_hash) { 100431e6b01fSNick Piggin int err = parent->d_op->d_hash(parent, nd->inode, name); 10053cac260aSAl Viro if (err < 0) 10063cac260aSAl Viro return err; 10073cac260aSAl Viro } 10081da177e4SLinus Torvalds 1009b04f784eSNick Piggin /* 1010b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1011b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1012b04f784eSNick Piggin * do the non-racy lookup, below. 1013b04f784eSNick Piggin */ 101431e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 101531e6b01fSNick Piggin unsigned seq; 101631e6b01fSNick Piggin 101731e6b01fSNick Piggin *inode = nd->inode; 101831e6b01fSNick Piggin dentry = __d_lookup_rcu(parent, name, &seq, inode); 101931e6b01fSNick Piggin if (!dentry) { 102031e6b01fSNick Piggin if (nameidata_drop_rcu(nd)) 102131e6b01fSNick Piggin return -ECHILD; 102231e6b01fSNick Piggin goto need_lookup; 102331e6b01fSNick Piggin } 102431e6b01fSNick Piggin /* Memory barrier in read_seqcount_begin of child is enough */ 102531e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 102631e6b01fSNick Piggin return -ECHILD; 102731e6b01fSNick Piggin 102831e6b01fSNick Piggin nd->seq = seq; 102931e6b01fSNick Piggin if (dentry->d_op && dentry->d_op->d_revalidate) { 103031e6b01fSNick Piggin /* We commonly drop rcu-walk here */ 103131e6b01fSNick Piggin if (nameidata_dentry_drop_rcu(nd, dentry)) 103231e6b01fSNick Piggin return -ECHILD; 103331e6b01fSNick Piggin goto need_revalidate; 103431e6b01fSNick Piggin } 103531e6b01fSNick Piggin path->mnt = mnt; 103631e6b01fSNick Piggin path->dentry = dentry; 103731e6b01fSNick Piggin __follow_mount_rcu(nd, path, inode); 103831e6b01fSNick Piggin } else { 103931e6b01fSNick Piggin dentry = __d_lookup(parent, name); 10401da177e4SLinus Torvalds if (!dentry) 10411da177e4SLinus Torvalds goto need_lookup; 10422e2e88eaSNick Piggin found: 10431da177e4SLinus Torvalds if (dentry->d_op && dentry->d_op->d_revalidate) 10441da177e4SLinus Torvalds goto need_revalidate; 10451da177e4SLinus Torvalds done: 10461da177e4SLinus Torvalds path->mnt = mnt; 10471da177e4SLinus Torvalds path->dentry = dentry; 1048634ee701SAl Viro __follow_mount(path); 104931e6b01fSNick Piggin *inode = path->dentry->d_inode; 105031e6b01fSNick Piggin } 10511da177e4SLinus Torvalds return 0; 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds need_lookup: 10546e6b1bd1SAl Viro dir = parent->d_inode; 105531e6b01fSNick Piggin BUG_ON(nd->inode != dir); 10566e6b1bd1SAl Viro 10576e6b1bd1SAl Viro mutex_lock(&dir->i_mutex); 10586e6b1bd1SAl Viro /* 10596e6b1bd1SAl Viro * First re-do the cached lookup just in case it was created 1060b04f784eSNick Piggin * while we waited for the directory semaphore, or the first 1061b04f784eSNick Piggin * lookup failed due to an unrelated rename. 10626e6b1bd1SAl Viro * 1063b04f784eSNick Piggin * This could use version numbering or similar to avoid unnecessary 1064b04f784eSNick Piggin * cache lookups, but then we'd have to do the first lookup in the 1065b04f784eSNick Piggin * non-racy way. However in the common case here, everything should 1066b04f784eSNick Piggin * be hot in cache, so would it be a big win? 10676e6b1bd1SAl Viro */ 10686e6b1bd1SAl Viro dentry = d_lookup(parent, name); 1069baa03890SNick Piggin if (likely(!dentry)) { 1070baa03890SNick Piggin dentry = d_alloc_and_lookup(parent, name, nd); 10716e6b1bd1SAl Viro mutex_unlock(&dir->i_mutex); 10726e6b1bd1SAl Viro if (IS_ERR(dentry)) 10736e6b1bd1SAl Viro goto fail; 10746e6b1bd1SAl Viro goto done; 10756e6b1bd1SAl Viro } 10766e6b1bd1SAl Viro /* 10776e6b1bd1SAl Viro * Uhhuh! Nasty case: the cache was re-populated while 10786e6b1bd1SAl Viro * we waited on the semaphore. Need to revalidate. 10796e6b1bd1SAl Viro */ 10806e6b1bd1SAl Viro mutex_unlock(&dir->i_mutex); 10812e2e88eaSNick Piggin goto found; 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds need_revalidate: 1084bcdc5e01SIan Kent dentry = do_revalidate(dentry, nd); 1085bcdc5e01SIan Kent if (!dentry) 10861da177e4SLinus Torvalds goto need_lookup; 1087bcdc5e01SIan Kent if (IS_ERR(dentry)) 1088bcdc5e01SIan Kent goto fail; 1089bcdc5e01SIan Kent goto done; 10901da177e4SLinus Torvalds 10911da177e4SLinus Torvalds fail: 10921da177e4SLinus Torvalds return PTR_ERR(dentry); 10931da177e4SLinus Torvalds } 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds /* 1096ac278a9cSAl Viro * This is a temporary kludge to deal with "automount" symlinks; proper 1097ac278a9cSAl Viro * solution is to trigger them on follow_mount(), so that do_lookup() 1098ac278a9cSAl Viro * would DTRT. To be killed before 2.6.34-final. 1099ac278a9cSAl Viro */ 1100ac278a9cSAl Viro static inline int follow_on_final(struct inode *inode, unsigned lookup_flags) 1101ac278a9cSAl Viro { 1102ac278a9cSAl Viro return inode && unlikely(inode->i_op->follow_link) && 1103ac278a9cSAl Viro ((lookup_flags & LOOKUP_FOLLOW) || S_ISDIR(inode->i_mode)); 1104ac278a9cSAl Viro } 1105ac278a9cSAl Viro 1106ac278a9cSAl Viro /* 11071da177e4SLinus Torvalds * Name resolution. 1108ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1109ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 11101da177e4SLinus Torvalds * 1111ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1112ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 11131da177e4SLinus Torvalds */ 11146de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 11151da177e4SLinus Torvalds { 11161da177e4SLinus Torvalds struct path next; 11171da177e4SLinus Torvalds int err; 11181da177e4SLinus Torvalds unsigned int lookup_flags = nd->flags; 11191da177e4SLinus Torvalds 11201da177e4SLinus Torvalds while (*name=='/') 11211da177e4SLinus Torvalds name++; 11221da177e4SLinus Torvalds if (!*name) 11231da177e4SLinus Torvalds goto return_reval; 11241da177e4SLinus Torvalds 11251da177e4SLinus Torvalds if (nd->depth) 1126f55eab82STrond Myklebust lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE); 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 11291da177e4SLinus Torvalds for(;;) { 113031e6b01fSNick Piggin struct inode *inode; 11311da177e4SLinus Torvalds unsigned long hash; 11321da177e4SLinus Torvalds struct qstr this; 11331da177e4SLinus Torvalds unsigned int c; 11341da177e4SLinus Torvalds 1135cdce5d6bSTrond Myklebust nd->flags |= LOOKUP_CONTINUE; 113631e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 113731e6b01fSNick Piggin err = exec_permission_rcu(nd->inode); 113831e6b01fSNick Piggin if (err == -ECHILD) { 113931e6b01fSNick Piggin if (nameidata_drop_rcu(nd)) 114031e6b01fSNick Piggin return -ECHILD; 114131e6b01fSNick Piggin goto exec_again; 114231e6b01fSNick Piggin } 114331e6b01fSNick Piggin } else { 114431e6b01fSNick Piggin exec_again: 114531e6b01fSNick Piggin err = exec_permission(nd->inode); 114631e6b01fSNick Piggin } 11471da177e4SLinus Torvalds if (err) 11481da177e4SLinus Torvalds break; 11491da177e4SLinus Torvalds 11501da177e4SLinus Torvalds this.name = name; 11511da177e4SLinus Torvalds c = *(const unsigned char *)name; 11521da177e4SLinus Torvalds 11531da177e4SLinus Torvalds hash = init_name_hash(); 11541da177e4SLinus Torvalds do { 11551da177e4SLinus Torvalds name++; 11561da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 11571da177e4SLinus Torvalds c = *(const unsigned char *)name; 11581da177e4SLinus Torvalds } while (c && (c != '/')); 11591da177e4SLinus Torvalds this.len = name - (const char *) this.name; 11601da177e4SLinus Torvalds this.hash = end_name_hash(hash); 11611da177e4SLinus Torvalds 11621da177e4SLinus Torvalds /* remove trailing slashes? */ 11631da177e4SLinus Torvalds if (!c) 11641da177e4SLinus Torvalds goto last_component; 11651da177e4SLinus Torvalds while (*++name == '/'); 11661da177e4SLinus Torvalds if (!*name) 11671da177e4SLinus Torvalds goto last_with_slashes; 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds /* 11701da177e4SLinus Torvalds * "." and ".." are special - ".." especially so because it has 11711da177e4SLinus Torvalds * to be able to know about the current root directory and 11721da177e4SLinus Torvalds * parent relationships. 11731da177e4SLinus Torvalds */ 11741da177e4SLinus Torvalds if (this.name[0] == '.') switch (this.len) { 11751da177e4SLinus Torvalds default: 11761da177e4SLinus Torvalds break; 11771da177e4SLinus Torvalds case 2: 11781da177e4SLinus Torvalds if (this.name[1] != '.') 11791da177e4SLinus Torvalds break; 118031e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 118131e6b01fSNick Piggin if (follow_dotdot_rcu(nd)) 118231e6b01fSNick Piggin return -ECHILD; 118331e6b01fSNick Piggin } else 118458c465ebSAl Viro follow_dotdot(nd); 11851da177e4SLinus Torvalds /* fallthrough */ 11861da177e4SLinus Torvalds case 1: 11871da177e4SLinus Torvalds continue; 11881da177e4SLinus Torvalds } 11891da177e4SLinus Torvalds /* This does the actual lookups.. */ 119031e6b01fSNick Piggin err = do_lookup(nd, &this, &next, &inode); 11911da177e4SLinus Torvalds if (err) 11921da177e4SLinus Torvalds break; 11931da177e4SLinus Torvalds err = -ENOENT; 11941da177e4SLinus Torvalds if (!inode) 11951da177e4SLinus Torvalds goto out_dput; 11961da177e4SLinus Torvalds 11971da177e4SLinus Torvalds if (inode->i_op->follow_link) { 119831e6b01fSNick Piggin /* We commonly drop rcu-walk here */ 119931e6b01fSNick Piggin if (nameidata_dentry_drop_rcu_maybe(nd, next.dentry)) 120031e6b01fSNick Piggin return -ECHILD; 120131e6b01fSNick Piggin BUG_ON(inode != next.dentry->d_inode); 120290ebe565SAl Viro err = do_follow_link(&next, nd); 12031da177e4SLinus Torvalds if (err) 12041da177e4SLinus Torvalds goto return_err; 120531e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 12061da177e4SLinus Torvalds err = -ENOENT; 120731e6b01fSNick Piggin if (!nd->inode) 12081da177e4SLinus Torvalds break; 120931e6b01fSNick Piggin } else { 121009dd17d3SMiklos Szeredi path_to_nameidata(&next, nd); 121131e6b01fSNick Piggin nd->inode = inode; 121231e6b01fSNick Piggin } 12131da177e4SLinus Torvalds err = -ENOTDIR; 121431e6b01fSNick Piggin if (!nd->inode->i_op->lookup) 12151da177e4SLinus Torvalds break; 12161da177e4SLinus Torvalds continue; 12171da177e4SLinus Torvalds /* here ends the main loop */ 12181da177e4SLinus Torvalds 12191da177e4SLinus Torvalds last_with_slashes: 12201da177e4SLinus Torvalds lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 12211da177e4SLinus Torvalds last_component: 1222f55eab82STrond Myklebust /* Clear LOOKUP_CONTINUE iff it was previously unset */ 1223f55eab82STrond Myklebust nd->flags &= lookup_flags | ~LOOKUP_CONTINUE; 12241da177e4SLinus Torvalds if (lookup_flags & LOOKUP_PARENT) 12251da177e4SLinus Torvalds goto lookup_parent; 12261da177e4SLinus Torvalds if (this.name[0] == '.') switch (this.len) { 12271da177e4SLinus Torvalds default: 12281da177e4SLinus Torvalds break; 12291da177e4SLinus Torvalds case 2: 12301da177e4SLinus Torvalds if (this.name[1] != '.') 12311da177e4SLinus Torvalds break; 123231e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 123331e6b01fSNick Piggin if (follow_dotdot_rcu(nd)) 123431e6b01fSNick Piggin return -ECHILD; 123531e6b01fSNick Piggin } else 123658c465ebSAl Viro follow_dotdot(nd); 12371da177e4SLinus Torvalds /* fallthrough */ 12381da177e4SLinus Torvalds case 1: 12391da177e4SLinus Torvalds goto return_reval; 12401da177e4SLinus Torvalds } 124131e6b01fSNick Piggin err = do_lookup(nd, &this, &next, &inode); 12421da177e4SLinus Torvalds if (err) 12431da177e4SLinus Torvalds break; 1244ac278a9cSAl Viro if (follow_on_final(inode, lookup_flags)) { 124531e6b01fSNick Piggin if (nameidata_dentry_drop_rcu_maybe(nd, next.dentry)) 124631e6b01fSNick Piggin return -ECHILD; 124731e6b01fSNick Piggin BUG_ON(inode != next.dentry->d_inode); 124890ebe565SAl Viro err = do_follow_link(&next, nd); 12491da177e4SLinus Torvalds if (err) 12501da177e4SLinus Torvalds goto return_err; 125131e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 125231e6b01fSNick Piggin } else { 125309dd17d3SMiklos Szeredi path_to_nameidata(&next, nd); 125431e6b01fSNick Piggin nd->inode = inode; 125531e6b01fSNick Piggin } 12561da177e4SLinus Torvalds err = -ENOENT; 125731e6b01fSNick Piggin if (!nd->inode) 12581da177e4SLinus Torvalds break; 12591da177e4SLinus Torvalds if (lookup_flags & LOOKUP_DIRECTORY) { 12601da177e4SLinus Torvalds err = -ENOTDIR; 126131e6b01fSNick Piggin if (!nd->inode->i_op->lookup) 12621da177e4SLinus Torvalds break; 12631da177e4SLinus Torvalds } 12641da177e4SLinus Torvalds goto return_base; 12651da177e4SLinus Torvalds lookup_parent: 12661da177e4SLinus Torvalds nd->last = this; 12671da177e4SLinus Torvalds nd->last_type = LAST_NORM; 12681da177e4SLinus Torvalds if (this.name[0] != '.') 12691da177e4SLinus Torvalds goto return_base; 12701da177e4SLinus Torvalds if (this.len == 1) 12711da177e4SLinus Torvalds nd->last_type = LAST_DOT; 12721da177e4SLinus Torvalds else if (this.len == 2 && this.name[1] == '.') 12731da177e4SLinus Torvalds nd->last_type = LAST_DOTDOT; 12741da177e4SLinus Torvalds else 12751da177e4SLinus Torvalds goto return_base; 12761da177e4SLinus Torvalds return_reval: 12771da177e4SLinus Torvalds /* 12781da177e4SLinus Torvalds * We bypassed the ordinary revalidation routines. 12791da177e4SLinus Torvalds * We may need to check the cached dentry for staleness. 12801da177e4SLinus Torvalds */ 12814ac91378SJan Blunck if (nd->path.dentry && nd->path.dentry->d_sb && 12824ac91378SJan Blunck (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) { 128331e6b01fSNick Piggin if (nameidata_drop_rcu_maybe(nd)) 128431e6b01fSNick Piggin return -ECHILD; 12851da177e4SLinus Torvalds err = -ESTALE; 12861da177e4SLinus Torvalds /* Note: we do not d_invalidate() */ 12874ac91378SJan Blunck if (!nd->path.dentry->d_op->d_revalidate( 12884ac91378SJan Blunck nd->path.dentry, nd)) 12891da177e4SLinus Torvalds break; 12901da177e4SLinus Torvalds } 12911da177e4SLinus Torvalds return_base: 129231e6b01fSNick Piggin if (nameidata_drop_rcu_last_maybe(nd)) 129331e6b01fSNick Piggin return -ECHILD; 12941da177e4SLinus Torvalds return 0; 12951da177e4SLinus Torvalds out_dput: 129631e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) 12971d957f9bSJan Blunck path_put_conditional(&next, nd); 12981da177e4SLinus Torvalds break; 12991da177e4SLinus Torvalds } 130031e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) 13011d957f9bSJan Blunck path_put(&nd->path); 13021da177e4SLinus Torvalds return_err: 13031da177e4SLinus Torvalds return err; 13041da177e4SLinus Torvalds } 13051da177e4SLinus Torvalds 130631e6b01fSNick Piggin static inline int path_walk_rcu(const char *name, struct nameidata *nd) 130731e6b01fSNick Piggin { 130831e6b01fSNick Piggin current->total_link_count = 0; 130931e6b01fSNick Piggin 131031e6b01fSNick Piggin return link_path_walk(name, nd); 131131e6b01fSNick Piggin } 131231e6b01fSNick Piggin 131331e6b01fSNick Piggin static inline int path_walk_simple(const char *name, struct nameidata *nd) 131431e6b01fSNick Piggin { 131531e6b01fSNick Piggin current->total_link_count = 0; 131631e6b01fSNick Piggin 131731e6b01fSNick Piggin return link_path_walk(name, nd); 131831e6b01fSNick Piggin } 131931e6b01fSNick Piggin 1320fc9b52cdSHarvey Harrison static int path_walk(const char *name, struct nameidata *nd) 13211da177e4SLinus Torvalds { 13226de88d72SAl Viro struct path save = nd->path; 13236de88d72SAl Viro int result; 13246de88d72SAl Viro 13251da177e4SLinus Torvalds current->total_link_count = 0; 13266de88d72SAl Viro 13276de88d72SAl Viro /* make sure the stuff we saved doesn't go away */ 13286de88d72SAl Viro path_get(&save); 13296de88d72SAl Viro 13306de88d72SAl Viro result = link_path_walk(name, nd); 13316de88d72SAl Viro if (result == -ESTALE) { 13326de88d72SAl Viro /* nd->path had been dropped */ 13336de88d72SAl Viro current->total_link_count = 0; 13346de88d72SAl Viro nd->path = save; 13356de88d72SAl Viro path_get(&nd->path); 13366de88d72SAl Viro nd->flags |= LOOKUP_REVAL; 13376de88d72SAl Viro result = link_path_walk(name, nd); 13386de88d72SAl Viro } 13396de88d72SAl Viro 13406de88d72SAl Viro path_put(&save); 13416de88d72SAl Viro 13426de88d72SAl Viro return result; 13431da177e4SLinus Torvalds } 13441da177e4SLinus Torvalds 134531e6b01fSNick Piggin static void path_finish_rcu(struct nameidata *nd) 134631e6b01fSNick Piggin { 134731e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 134831e6b01fSNick Piggin /* RCU dangling. Cancel it. */ 134931e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 135031e6b01fSNick Piggin nd->root.mnt = NULL; 135131e6b01fSNick Piggin rcu_read_unlock(); 135231e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 135331e6b01fSNick Piggin } 135431e6b01fSNick Piggin if (nd->file) 135531e6b01fSNick Piggin fput(nd->file); 135631e6b01fSNick Piggin } 135731e6b01fSNick Piggin 135831e6b01fSNick Piggin static int path_init_rcu(int dfd, const char *name, unsigned int flags, struct nameidata *nd) 135931e6b01fSNick Piggin { 136031e6b01fSNick Piggin int retval = 0; 136131e6b01fSNick Piggin int fput_needed; 136231e6b01fSNick Piggin struct file *file; 136331e6b01fSNick Piggin 136431e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 136531e6b01fSNick Piggin nd->flags = flags | LOOKUP_RCU; 136631e6b01fSNick Piggin nd->depth = 0; 136731e6b01fSNick Piggin nd->root.mnt = NULL; 136831e6b01fSNick Piggin nd->file = NULL; 136931e6b01fSNick Piggin 137031e6b01fSNick Piggin if (*name=='/') { 137131e6b01fSNick Piggin struct fs_struct *fs = current->fs; 137231e6b01fSNick Piggin 137331e6b01fSNick Piggin br_read_lock(vfsmount_lock); 137431e6b01fSNick Piggin rcu_read_lock(); 137531e6b01fSNick Piggin 137631e6b01fSNick Piggin spin_lock(&fs->lock); 137731e6b01fSNick Piggin nd->root = fs->root; 137831e6b01fSNick Piggin nd->path = nd->root; 137931e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 138031e6b01fSNick Piggin spin_unlock(&fs->lock); 138131e6b01fSNick Piggin 138231e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 138331e6b01fSNick Piggin struct fs_struct *fs = current->fs; 138431e6b01fSNick Piggin 138531e6b01fSNick Piggin br_read_lock(vfsmount_lock); 138631e6b01fSNick Piggin rcu_read_lock(); 138731e6b01fSNick Piggin 138831e6b01fSNick Piggin spin_lock(&fs->lock); 138931e6b01fSNick Piggin nd->path = fs->pwd; 139031e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 139131e6b01fSNick Piggin spin_unlock(&fs->lock); 139231e6b01fSNick Piggin } else { 139331e6b01fSNick Piggin struct dentry *dentry; 139431e6b01fSNick Piggin 139531e6b01fSNick Piggin file = fget_light(dfd, &fput_needed); 139631e6b01fSNick Piggin retval = -EBADF; 139731e6b01fSNick Piggin if (!file) 139831e6b01fSNick Piggin goto out_fail; 139931e6b01fSNick Piggin 140031e6b01fSNick Piggin dentry = file->f_path.dentry; 140131e6b01fSNick Piggin 140231e6b01fSNick Piggin retval = -ENOTDIR; 140331e6b01fSNick Piggin if (!S_ISDIR(dentry->d_inode->i_mode)) 140431e6b01fSNick Piggin goto fput_fail; 140531e6b01fSNick Piggin 140631e6b01fSNick Piggin retval = file_permission(file, MAY_EXEC); 140731e6b01fSNick Piggin if (retval) 140831e6b01fSNick Piggin goto fput_fail; 140931e6b01fSNick Piggin 141031e6b01fSNick Piggin nd->path = file->f_path; 141131e6b01fSNick Piggin if (fput_needed) 141231e6b01fSNick Piggin nd->file = file; 141331e6b01fSNick Piggin 141431e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 141531e6b01fSNick Piggin br_read_lock(vfsmount_lock); 141631e6b01fSNick Piggin rcu_read_lock(); 141731e6b01fSNick Piggin } 141831e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 141931e6b01fSNick Piggin return 0; 142031e6b01fSNick Piggin 142131e6b01fSNick Piggin fput_fail: 142231e6b01fSNick Piggin fput_light(file, fput_needed); 142331e6b01fSNick Piggin out_fail: 142431e6b01fSNick Piggin return retval; 142531e6b01fSNick Piggin } 142631e6b01fSNick Piggin 14279b4a9b14SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd) 14281da177e4SLinus Torvalds { 1429ea3834d9SPrasanna Meda int retval = 0; 1430170aa3d0SUlrich Drepper int fput_needed; 1431170aa3d0SUlrich Drepper struct file *file; 14321da177e4SLinus Torvalds 14331da177e4SLinus Torvalds nd->last_type = LAST_ROOT; /* if there are only slashes... */ 14341da177e4SLinus Torvalds nd->flags = flags; 14351da177e4SLinus Torvalds nd->depth = 0; 14362a737871SAl Viro nd->root.mnt = NULL; 14371da177e4SLinus Torvalds 14381da177e4SLinus Torvalds if (*name=='/') { 14392a737871SAl Viro set_root(nd); 14402a737871SAl Viro nd->path = nd->root; 14412a737871SAl Viro path_get(&nd->root); 14425590ff0dSUlrich Drepper } else if (dfd == AT_FDCWD) { 1443f7ad3c6bSMiklos Szeredi get_fs_pwd(current->fs, &nd->path); 14445590ff0dSUlrich Drepper } else { 14455590ff0dSUlrich Drepper struct dentry *dentry; 14465590ff0dSUlrich Drepper 14475590ff0dSUlrich Drepper file = fget_light(dfd, &fput_needed); 14485590ff0dSUlrich Drepper retval = -EBADF; 1449170aa3d0SUlrich Drepper if (!file) 14506d09bb62STrond Myklebust goto out_fail; 14515590ff0dSUlrich Drepper 14520f7fc9e4SJosef "Jeff" Sipek dentry = file->f_path.dentry; 14535590ff0dSUlrich Drepper 14545590ff0dSUlrich Drepper retval = -ENOTDIR; 1455170aa3d0SUlrich Drepper if (!S_ISDIR(dentry->d_inode->i_mode)) 14566d09bb62STrond Myklebust goto fput_fail; 14575590ff0dSUlrich Drepper 14585590ff0dSUlrich Drepper retval = file_permission(file, MAY_EXEC); 1459170aa3d0SUlrich Drepper if (retval) 14606d09bb62STrond Myklebust goto fput_fail; 14615590ff0dSUlrich Drepper 14625dd784d0SJan Blunck nd->path = file->f_path; 14635dd784d0SJan Blunck path_get(&file->f_path); 14645590ff0dSUlrich Drepper 14655590ff0dSUlrich Drepper fput_light(file, fput_needed); 14661da177e4SLinus Torvalds } 146731e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 14689b4a9b14SAl Viro return 0; 14692dfdd266SJosef 'Jeff' Sipek 14709b4a9b14SAl Viro fput_fail: 14719b4a9b14SAl Viro fput_light(file, fput_needed); 14729b4a9b14SAl Viro out_fail: 14739b4a9b14SAl Viro return retval; 14749b4a9b14SAl Viro } 14759b4a9b14SAl Viro 14769b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 14779b4a9b14SAl Viro static int do_path_lookup(int dfd, const char *name, 14789b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 14799b4a9b14SAl Viro { 148031e6b01fSNick Piggin int retval; 148131e6b01fSNick Piggin 148231e6b01fSNick Piggin /* 148331e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 148431e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 148531e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 148631e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 148731e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 148831e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 148931e6b01fSNick Piggin * analogue, foo_rcu(). 149031e6b01fSNick Piggin * 149131e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 149231e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 149331e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 149431e6b01fSNick Piggin * be able to complete). 149531e6b01fSNick Piggin */ 149631e6b01fSNick Piggin retval = path_init_rcu(dfd, name, flags, nd); 149731e6b01fSNick Piggin if (unlikely(retval)) 149831e6b01fSNick Piggin return retval; 149931e6b01fSNick Piggin retval = path_walk_rcu(name, nd); 150031e6b01fSNick Piggin path_finish_rcu(nd); 15012a737871SAl Viro if (nd->root.mnt) { 15022a737871SAl Viro path_put(&nd->root); 15032a737871SAl Viro nd->root.mnt = NULL; 15042a737871SAl Viro } 150531e6b01fSNick Piggin 150631e6b01fSNick Piggin if (unlikely(retval == -ECHILD || retval == -ESTALE)) { 150731e6b01fSNick Piggin /* slower, locked walk */ 150831e6b01fSNick Piggin if (retval == -ESTALE) 150931e6b01fSNick Piggin flags |= LOOKUP_REVAL; 151031e6b01fSNick Piggin retval = path_init(dfd, name, flags, nd); 151131e6b01fSNick Piggin if (unlikely(retval)) 151231e6b01fSNick Piggin return retval; 151331e6b01fSNick Piggin retval = path_walk(name, nd); 151431e6b01fSNick Piggin if (nd->root.mnt) { 151531e6b01fSNick Piggin path_put(&nd->root); 151631e6b01fSNick Piggin nd->root.mnt = NULL; 151731e6b01fSNick Piggin } 151831e6b01fSNick Piggin } 151931e6b01fSNick Piggin 152031e6b01fSNick Piggin if (likely(!retval)) { 152131e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) { 152231e6b01fSNick Piggin if (nd->path.dentry && nd->inode) 152331e6b01fSNick Piggin audit_inode(name, nd->path.dentry); 152431e6b01fSNick Piggin } 152531e6b01fSNick Piggin } 152631e6b01fSNick Piggin 1527170aa3d0SUlrich Drepper return retval; 15281da177e4SLinus Torvalds } 15291da177e4SLinus Torvalds 1530fc9b52cdSHarvey Harrison int path_lookup(const char *name, unsigned int flags, 15315590ff0dSUlrich Drepper struct nameidata *nd) 15325590ff0dSUlrich Drepper { 15335590ff0dSUlrich Drepper return do_path_lookup(AT_FDCWD, name, flags, nd); 15345590ff0dSUlrich Drepper } 15355590ff0dSUlrich Drepper 1536d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1537d1811465SAl Viro { 1538d1811465SAl Viro struct nameidata nd; 1539d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1540d1811465SAl Viro if (!res) 1541d1811465SAl Viro *path = nd.path; 1542d1811465SAl Viro return res; 1543d1811465SAl Viro } 1544d1811465SAl Viro 154516f18200SJosef 'Jeff' Sipek /** 154616f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 154716f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 154816f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 154916f18200SJosef 'Jeff' Sipek * @name: pointer to file name 155016f18200SJosef 'Jeff' Sipek * @flags: lookup flags 155116f18200SJosef 'Jeff' Sipek * @nd: pointer to nameidata 155216f18200SJosef 'Jeff' Sipek */ 155316f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 155416f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 155516f18200SJosef 'Jeff' Sipek struct nameidata *nd) 155616f18200SJosef 'Jeff' Sipek { 155716f18200SJosef 'Jeff' Sipek int retval; 155816f18200SJosef 'Jeff' Sipek 155916f18200SJosef 'Jeff' Sipek /* same as do_path_lookup */ 156016f18200SJosef 'Jeff' Sipek nd->last_type = LAST_ROOT; 156116f18200SJosef 'Jeff' Sipek nd->flags = flags; 156216f18200SJosef 'Jeff' Sipek nd->depth = 0; 156316f18200SJosef 'Jeff' Sipek 1564c8e7f449SJan Blunck nd->path.dentry = dentry; 1565c8e7f449SJan Blunck nd->path.mnt = mnt; 1566c8e7f449SJan Blunck path_get(&nd->path); 15675b857119SAl Viro nd->root = nd->path; 15685b857119SAl Viro path_get(&nd->root); 156931e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 157016f18200SJosef 'Jeff' Sipek 157116f18200SJosef 'Jeff' Sipek retval = path_walk(name, nd); 15724ac91378SJan Blunck if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry && 157331e6b01fSNick Piggin nd->inode)) 15744ac91378SJan Blunck audit_inode(name, nd->path.dentry); 157516f18200SJosef 'Jeff' Sipek 15762a737871SAl Viro path_put(&nd->root); 15772a737871SAl Viro nd->root.mnt = NULL; 157816f18200SJosef 'Jeff' Sipek 15792a737871SAl Viro return retval; 158016f18200SJosef 'Jeff' Sipek } 158116f18200SJosef 'Jeff' Sipek 1582eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name, 1583eead1911SChristoph Hellwig struct dentry *base, struct nameidata *nd) 15841da177e4SLinus Torvalds { 158581fca444SChristoph Hellwig struct inode *inode = base->d_inode; 15861da177e4SLinus Torvalds struct dentry *dentry; 15871da177e4SLinus Torvalds int err; 15881da177e4SLinus Torvalds 158981fca444SChristoph Hellwig err = exec_permission(inode); 159081fca444SChristoph Hellwig if (err) 159181fca444SChristoph Hellwig return ERR_PTR(err); 15921da177e4SLinus Torvalds 15931da177e4SLinus Torvalds /* 15941da177e4SLinus Torvalds * See if the low-level filesystem might want 15951da177e4SLinus Torvalds * to use its own hash.. 15961da177e4SLinus Torvalds */ 15971da177e4SLinus Torvalds if (base->d_op && base->d_op->d_hash) { 1598b1e6a015SNick Piggin err = base->d_op->d_hash(base, inode, name); 15991da177e4SLinus Torvalds dentry = ERR_PTR(err); 16001da177e4SLinus Torvalds if (err < 0) 16011da177e4SLinus Torvalds goto out; 16021da177e4SLinus Torvalds } 16031da177e4SLinus Torvalds 1604b04f784eSNick Piggin /* 1605b04f784eSNick Piggin * Don't bother with __d_lookup: callers are for creat as 1606b04f784eSNick Piggin * well as unlink, so a lot of the time it would cost 1607b04f784eSNick Piggin * a double lookup. 16086e6b1bd1SAl Viro */ 16096e6b1bd1SAl Viro dentry = d_lookup(base, name); 16106e6b1bd1SAl Viro 16116e6b1bd1SAl Viro if (dentry && dentry->d_op && dentry->d_op->d_revalidate) 16126e6b1bd1SAl Viro dentry = do_revalidate(dentry, nd); 16136e6b1bd1SAl Viro 16141da177e4SLinus Torvalds if (!dentry) 1615baa03890SNick Piggin dentry = d_alloc_and_lookup(base, name, nd); 16161da177e4SLinus Torvalds out: 16171da177e4SLinus Torvalds return dentry; 16181da177e4SLinus Torvalds } 16191da177e4SLinus Torvalds 1620057f6c01SJames Morris /* 1621057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1622057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1623057f6c01SJames Morris * SMP-safe. 1624057f6c01SJames Morris */ 1625a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 16261da177e4SLinus Torvalds { 16274ac91378SJan Blunck return __lookup_hash(&nd->last, nd->path.dentry, nd); 16281da177e4SLinus Torvalds } 16291da177e4SLinus Torvalds 1630eead1911SChristoph Hellwig static int __lookup_one_len(const char *name, struct qstr *this, 1631eead1911SChristoph Hellwig struct dentry *base, int len) 16321da177e4SLinus Torvalds { 16331da177e4SLinus Torvalds unsigned long hash; 16341da177e4SLinus Torvalds unsigned int c; 16351da177e4SLinus Torvalds 1636057f6c01SJames Morris this->name = name; 1637057f6c01SJames Morris this->len = len; 16381da177e4SLinus Torvalds if (!len) 1639057f6c01SJames Morris return -EACCES; 16401da177e4SLinus Torvalds 16411da177e4SLinus Torvalds hash = init_name_hash(); 16421da177e4SLinus Torvalds while (len--) { 16431da177e4SLinus Torvalds c = *(const unsigned char *)name++; 16441da177e4SLinus Torvalds if (c == '/' || c == '\0') 1645057f6c01SJames Morris return -EACCES; 16461da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 16471da177e4SLinus Torvalds } 1648057f6c01SJames Morris this->hash = end_name_hash(hash); 1649057f6c01SJames Morris return 0; 1650057f6c01SJames Morris } 16511da177e4SLinus Torvalds 1652eead1911SChristoph Hellwig /** 1653a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1654eead1911SChristoph Hellwig * @name: pathname component to lookup 1655eead1911SChristoph Hellwig * @base: base directory to lookup from 1656eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1657eead1911SChristoph Hellwig * 1658a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1659a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1660eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1661eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1662eead1911SChristoph Hellwig */ 1663057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1664057f6c01SJames Morris { 1665057f6c01SJames Morris int err; 1666057f6c01SJames Morris struct qstr this; 1667057f6c01SJames Morris 16682f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 16692f9092e1SDavid Woodhouse 1670057f6c01SJames Morris err = __lookup_one_len(name, &this, base, len); 1671057f6c01SJames Morris if (err) 1672057f6c01SJames Morris return ERR_PTR(err); 1673eead1911SChristoph Hellwig 167449705b77SChristoph Hellwig return __lookup_hash(&this, base, NULL); 1675057f6c01SJames Morris } 1676057f6c01SJames Morris 16772d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags, 16782d8f3038SAl Viro struct path *path) 16791da177e4SLinus Torvalds { 16802d8f3038SAl Viro struct nameidata nd; 16811da177e4SLinus Torvalds char *tmp = getname(name); 16821da177e4SLinus Torvalds int err = PTR_ERR(tmp); 16831da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 16842d8f3038SAl Viro 16852d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 16862d8f3038SAl Viro 16872d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 16881da177e4SLinus Torvalds putname(tmp); 16892d8f3038SAl Viro if (!err) 16902d8f3038SAl Viro *path = nd.path; 16911da177e4SLinus Torvalds } 16921da177e4SLinus Torvalds return err; 16931da177e4SLinus Torvalds } 16941da177e4SLinus Torvalds 16952ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 16962ad94ae6SAl Viro struct nameidata *nd, char **name) 16972ad94ae6SAl Viro { 16982ad94ae6SAl Viro char *s = getname(path); 16992ad94ae6SAl Viro int error; 17002ad94ae6SAl Viro 17012ad94ae6SAl Viro if (IS_ERR(s)) 17022ad94ae6SAl Viro return PTR_ERR(s); 17032ad94ae6SAl Viro 17042ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 17052ad94ae6SAl Viro if (error) 17062ad94ae6SAl Viro putname(s); 17072ad94ae6SAl Viro else 17082ad94ae6SAl Viro *name = s; 17092ad94ae6SAl Viro 17102ad94ae6SAl Viro return error; 17112ad94ae6SAl Viro } 17122ad94ae6SAl Viro 17131da177e4SLinus Torvalds /* 17141da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 17151da177e4SLinus Torvalds * minimal. 17161da177e4SLinus Torvalds */ 17171da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 17181da177e4SLinus Torvalds { 1719da9592edSDavid Howells uid_t fsuid = current_fsuid(); 1720da9592edSDavid Howells 17211da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 17221da177e4SLinus Torvalds return 0; 1723da9592edSDavid Howells if (inode->i_uid == fsuid) 17241da177e4SLinus Torvalds return 0; 1725da9592edSDavid Howells if (dir->i_uid == fsuid) 17261da177e4SLinus Torvalds return 0; 17271da177e4SLinus Torvalds return !capable(CAP_FOWNER); 17281da177e4SLinus Torvalds } 17291da177e4SLinus Torvalds 17301da177e4SLinus Torvalds /* 17311da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 17321da177e4SLinus Torvalds * whether the type of victim is right. 17331da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 17341da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 17351da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 17361da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 17371da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 17381da177e4SLinus Torvalds * a. be owner of dir, or 17391da177e4SLinus Torvalds * b. be owner of victim, or 17401da177e4SLinus Torvalds * c. have CAP_FOWNER capability 17411da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 17421da177e4SLinus Torvalds * links pointing to it. 17431da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 17441da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 17451da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 17461da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 17471da177e4SLinus Torvalds * nfs_async_unlink(). 17481da177e4SLinus Torvalds */ 1749858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 17501da177e4SLinus Torvalds { 17511da177e4SLinus Torvalds int error; 17521da177e4SLinus Torvalds 17531da177e4SLinus Torvalds if (!victim->d_inode) 17541da177e4SLinus Torvalds return -ENOENT; 17551da177e4SLinus Torvalds 17561da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 1757cccc6bbaSAl Viro audit_inode_child(victim, dir); 17581da177e4SLinus Torvalds 1759f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 17601da177e4SLinus Torvalds if (error) 17611da177e4SLinus Torvalds return error; 17621da177e4SLinus Torvalds if (IS_APPEND(dir)) 17631da177e4SLinus Torvalds return -EPERM; 17641da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 1765f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 17661da177e4SLinus Torvalds return -EPERM; 17671da177e4SLinus Torvalds if (isdir) { 17681da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 17691da177e4SLinus Torvalds return -ENOTDIR; 17701da177e4SLinus Torvalds if (IS_ROOT(victim)) 17711da177e4SLinus Torvalds return -EBUSY; 17721da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 17731da177e4SLinus Torvalds return -EISDIR; 17741da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 17751da177e4SLinus Torvalds return -ENOENT; 17761da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 17771da177e4SLinus Torvalds return -EBUSY; 17781da177e4SLinus Torvalds return 0; 17791da177e4SLinus Torvalds } 17801da177e4SLinus Torvalds 17811da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 17821da177e4SLinus Torvalds * dir. 17831da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 17841da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 17851da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 17861da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 17871da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 17881da177e4SLinus Torvalds */ 1789a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 17901da177e4SLinus Torvalds { 17911da177e4SLinus Torvalds if (child->d_inode) 17921da177e4SLinus Torvalds return -EEXIST; 17931da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 17941da177e4SLinus Torvalds return -ENOENT; 1795f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 17961da177e4SLinus Torvalds } 17971da177e4SLinus Torvalds 17981da177e4SLinus Torvalds /* 17991da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 18001da177e4SLinus Torvalds */ 18011da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 18021da177e4SLinus Torvalds { 18031da177e4SLinus Torvalds struct dentry *p; 18041da177e4SLinus Torvalds 18051da177e4SLinus Torvalds if (p1 == p2) { 1806f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 18071da177e4SLinus Torvalds return NULL; 18081da177e4SLinus Torvalds } 18091da177e4SLinus Torvalds 1810a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 18111da177e4SLinus Torvalds 1812e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 1813e2761a11SOGAWA Hirofumi if (p) { 1814f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 1815f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 18161da177e4SLinus Torvalds return p; 18171da177e4SLinus Torvalds } 18181da177e4SLinus Torvalds 1819e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 1820e2761a11SOGAWA Hirofumi if (p) { 1821f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1822f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 18231da177e4SLinus Torvalds return p; 18241da177e4SLinus Torvalds } 18251da177e4SLinus Torvalds 1826f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1827f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 18281da177e4SLinus Torvalds return NULL; 18291da177e4SLinus Torvalds } 18301da177e4SLinus Torvalds 18311da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 18321da177e4SLinus Torvalds { 18331b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 18341da177e4SLinus Torvalds if (p1 != p2) { 18351b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 1836a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 18371da177e4SLinus Torvalds } 18381da177e4SLinus Torvalds } 18391da177e4SLinus Torvalds 18401da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode, 18411da177e4SLinus Torvalds struct nameidata *nd) 18421da177e4SLinus Torvalds { 1843a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 18441da177e4SLinus Torvalds 18451da177e4SLinus Torvalds if (error) 18461da177e4SLinus Torvalds return error; 18471da177e4SLinus Torvalds 1848acfa4380SAl Viro if (!dir->i_op->create) 18491da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 18501da177e4SLinus Torvalds mode &= S_IALLUGO; 18511da177e4SLinus Torvalds mode |= S_IFREG; 18521da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 18531da177e4SLinus Torvalds if (error) 18541da177e4SLinus Torvalds return error; 18551da177e4SLinus Torvalds error = dir->i_op->create(dir, dentry, mode, nd); 1856a74574aaSStephen Smalley if (!error) 1857f38aa942SAmy Griffis fsnotify_create(dir, dentry); 18581da177e4SLinus Torvalds return error; 18591da177e4SLinus Torvalds } 18601da177e4SLinus Torvalds 18613fb64190SChristoph Hellwig int may_open(struct path *path, int acc_mode, int flag) 18621da177e4SLinus Torvalds { 18633fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 18641da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 18651da177e4SLinus Torvalds int error; 18661da177e4SLinus Torvalds 18671da177e4SLinus Torvalds if (!inode) 18681da177e4SLinus Torvalds return -ENOENT; 18691da177e4SLinus Torvalds 1870c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 1871c8fe8f30SChristoph Hellwig case S_IFLNK: 18721da177e4SLinus Torvalds return -ELOOP; 1873c8fe8f30SChristoph Hellwig case S_IFDIR: 1874c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 18751da177e4SLinus Torvalds return -EISDIR; 1876c8fe8f30SChristoph Hellwig break; 1877c8fe8f30SChristoph Hellwig case S_IFBLK: 1878c8fe8f30SChristoph Hellwig case S_IFCHR: 18793fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 18801da177e4SLinus Torvalds return -EACCES; 1881c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 1882c8fe8f30SChristoph Hellwig case S_IFIFO: 1883c8fe8f30SChristoph Hellwig case S_IFSOCK: 18841da177e4SLinus Torvalds flag &= ~O_TRUNC; 1885c8fe8f30SChristoph Hellwig break; 18864a3fd211SDave Hansen } 1887b41572e9SDave Hansen 18883fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 1889b41572e9SDave Hansen if (error) 1890b41572e9SDave Hansen return error; 18916146f0d5SMimi Zohar 18921da177e4SLinus Torvalds /* 18931da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 18941da177e4SLinus Torvalds */ 18951da177e4SLinus Torvalds if (IS_APPEND(inode)) { 18968737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 18977715b521SAl Viro return -EPERM; 18981da177e4SLinus Torvalds if (flag & O_TRUNC) 18997715b521SAl Viro return -EPERM; 19001da177e4SLinus Torvalds } 19011da177e4SLinus Torvalds 19021da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 19037715b521SAl Viro if (flag & O_NOATIME && !is_owner_or_cap(inode)) 19047715b521SAl Viro return -EPERM; 19051da177e4SLinus Torvalds 19061da177e4SLinus Torvalds /* 19071da177e4SLinus Torvalds * Ensure there are no outstanding leases on the file. 19081da177e4SLinus Torvalds */ 1909b65a9cfcSAl Viro return break_lease(inode, flag); 19107715b521SAl Viro } 19117715b521SAl Viro 19127715b521SAl Viro static int handle_truncate(struct path *path) 19137715b521SAl Viro { 19147715b521SAl Viro struct inode *inode = path->dentry->d_inode; 19157715b521SAl Viro int error = get_write_access(inode); 19161da177e4SLinus Torvalds if (error) 19177715b521SAl Viro return error; 19181da177e4SLinus Torvalds /* 19191da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 19201da177e4SLinus Torvalds */ 19211da177e4SLinus Torvalds error = locks_verify_locked(inode); 1922be6d3e56SKentaro Takeda if (!error) 1923ea0d3ab2STetsuo Handa error = security_path_truncate(path); 19241da177e4SLinus Torvalds if (!error) { 19257715b521SAl Viro error = do_truncate(path->dentry, 0, 1926d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 1927d139d7ffSMiklos Szeredi NULL); 19281da177e4SLinus Torvalds } 19291da177e4SLinus Torvalds put_write_access(inode); 1930acd0c935SMimi Zohar return error; 19311da177e4SLinus Torvalds } 19321da177e4SLinus Torvalds 1933d57999e1SDave Hansen /* 1934d57999e1SDave Hansen * Be careful about ever adding any more callers of this 1935d57999e1SDave Hansen * function. Its flags must be in the namei format, not 1936d57999e1SDave Hansen * what get passed to sys_open(). 1937d57999e1SDave Hansen */ 1938d57999e1SDave Hansen static int __open_namei_create(struct nameidata *nd, struct path *path, 19398737c930SAl Viro int open_flag, int mode) 1940aab520e2SDave Hansen { 1941aab520e2SDave Hansen int error; 19424ac91378SJan Blunck struct dentry *dir = nd->path.dentry; 1943aab520e2SDave Hansen 1944aab520e2SDave Hansen if (!IS_POSIXACL(dir->d_inode)) 1945ce3b0f8dSAl Viro mode &= ~current_umask(); 1946be6d3e56SKentaro Takeda error = security_path_mknod(&nd->path, path->dentry, mode, 0); 1947be6d3e56SKentaro Takeda if (error) 1948be6d3e56SKentaro Takeda goto out_unlock; 1949aab520e2SDave Hansen error = vfs_create(dir->d_inode, path->dentry, mode, nd); 1950be6d3e56SKentaro Takeda out_unlock: 1951aab520e2SDave Hansen mutex_unlock(&dir->d_inode->i_mutex); 19524ac91378SJan Blunck dput(nd->path.dentry); 19534ac91378SJan Blunck nd->path.dentry = path->dentry; 195431e6b01fSNick Piggin 1955aab520e2SDave Hansen if (error) 1956aab520e2SDave Hansen return error; 1957aab520e2SDave Hansen /* Don't check for write permission, don't truncate */ 19588737c930SAl Viro return may_open(&nd->path, 0, open_flag & ~O_TRUNC); 1959aab520e2SDave Hansen } 1960aab520e2SDave Hansen 19611da177e4SLinus Torvalds /* 1962d57999e1SDave Hansen * Note that while the flag value (low two bits) for sys_open means: 1963d57999e1SDave Hansen * 00 - read-only 1964d57999e1SDave Hansen * 01 - write-only 1965d57999e1SDave Hansen * 10 - read-write 1966d57999e1SDave Hansen * 11 - special 1967d57999e1SDave Hansen * it is changed into 1968d57999e1SDave Hansen * 00 - no permissions needed 1969d57999e1SDave Hansen * 01 - read-permission 1970d57999e1SDave Hansen * 10 - write-permission 1971d57999e1SDave Hansen * 11 - read-write 1972d57999e1SDave Hansen * for the internal routines (ie open_namei()/follow_link() etc) 1973d57999e1SDave Hansen * This is more logical, and also allows the 00 "no perm needed" 1974d57999e1SDave Hansen * to be used for symlinks (where the permissions are checked 1975d57999e1SDave Hansen * later). 1976d57999e1SDave Hansen * 1977d57999e1SDave Hansen */ 1978d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 1979d57999e1SDave Hansen { 1980d57999e1SDave Hansen if ((flag+1) & O_ACCMODE) 1981d57999e1SDave Hansen flag++; 1982d57999e1SDave Hansen return flag; 1983d57999e1SDave Hansen } 1984d57999e1SDave Hansen 19857715b521SAl Viro static int open_will_truncate(int flag, struct inode *inode) 19864a3fd211SDave Hansen { 1987d57999e1SDave Hansen /* 19884a3fd211SDave Hansen * We'll never write to the fs underlying 19894a3fd211SDave Hansen * a device file. 19904a3fd211SDave Hansen */ 19914a3fd211SDave Hansen if (special_file(inode->i_mode)) 19924a3fd211SDave Hansen return 0; 19934a3fd211SDave Hansen return (flag & O_TRUNC); 19944a3fd211SDave Hansen } 19954a3fd211SDave Hansen 1996648fa861SAl Viro static struct file *finish_open(struct nameidata *nd, 19979a66179eSAl Viro int open_flag, int acc_mode) 1998648fa861SAl Viro { 1999648fa861SAl Viro struct file *filp; 2000648fa861SAl Viro int will_truncate; 2001648fa861SAl Viro int error; 2002648fa861SAl Viro 20039a66179eSAl Viro will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode); 2004648fa861SAl Viro if (will_truncate) { 2005648fa861SAl Viro error = mnt_want_write(nd->path.mnt); 2006648fa861SAl Viro if (error) 2007648fa861SAl Viro goto exit; 2008648fa861SAl Viro } 2009648fa861SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2010648fa861SAl Viro if (error) { 2011648fa861SAl Viro if (will_truncate) 2012648fa861SAl Viro mnt_drop_write(nd->path.mnt); 2013648fa861SAl Viro goto exit; 2014648fa861SAl Viro } 2015648fa861SAl Viro filp = nameidata_to_filp(nd); 2016648fa861SAl Viro if (!IS_ERR(filp)) { 2017648fa861SAl Viro error = ima_file_check(filp, acc_mode); 2018648fa861SAl Viro if (error) { 2019648fa861SAl Viro fput(filp); 2020648fa861SAl Viro filp = ERR_PTR(error); 2021648fa861SAl Viro } 2022648fa861SAl Viro } 2023648fa861SAl Viro if (!IS_ERR(filp)) { 2024648fa861SAl Viro if (will_truncate) { 2025648fa861SAl Viro error = handle_truncate(&nd->path); 2026648fa861SAl Viro if (error) { 2027648fa861SAl Viro fput(filp); 2028648fa861SAl Viro filp = ERR_PTR(error); 2029648fa861SAl Viro } 2030648fa861SAl Viro } 2031648fa861SAl Viro } 2032648fa861SAl Viro /* 2033648fa861SAl Viro * It is now safe to drop the mnt write 2034648fa861SAl Viro * because the filp has had a write taken 2035648fa861SAl Viro * on its behalf. 2036648fa861SAl Viro */ 2037648fa861SAl Viro if (will_truncate) 2038648fa861SAl Viro mnt_drop_write(nd->path.mnt); 2039d893f1bcSAl Viro path_put(&nd->path); 2040648fa861SAl Viro return filp; 2041648fa861SAl Viro 2042648fa861SAl Viro exit: 2043648fa861SAl Viro if (!IS_ERR(nd->intent.open.file)) 2044648fa861SAl Viro release_open_intent(nd); 2045648fa861SAl Viro path_put(&nd->path); 2046648fa861SAl Viro return ERR_PTR(error); 2047648fa861SAl Viro } 2048648fa861SAl Viro 204931e6b01fSNick Piggin /* 205031e6b01fSNick Piggin * Handle O_CREAT case for do_filp_open 205131e6b01fSNick Piggin */ 2052fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path, 20535b369df8SAl Viro int open_flag, int acc_mode, 20543e297b61SAl Viro int mode, const char *pathname) 2055fb1cc555SAl Viro { 2056a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 2057fb1cc555SAl Viro struct file *filp; 20581f36f774SAl Viro int error = -EISDIR; 2059fb1cc555SAl Viro 20601f36f774SAl Viro switch (nd->last_type) { 20611f36f774SAl Viro case LAST_DOTDOT: 20621f36f774SAl Viro follow_dotdot(nd); 20631f36f774SAl Viro dir = nd->path.dentry; 2064176306f5SNeil Brown case LAST_DOT: 20651f36f774SAl Viro if (nd->path.mnt->mnt_sb->s_type->fs_flags & FS_REVAL_DOT) { 20661f36f774SAl Viro if (!dir->d_op->d_revalidate(dir, nd)) { 20671f36f774SAl Viro error = -ESTALE; 2068a2c36b45SAl Viro goto exit; 20691f36f774SAl Viro } 20701f36f774SAl Viro } 20711f36f774SAl Viro /* fallthrough */ 20721f36f774SAl Viro case LAST_ROOT: 20731f36f774SAl Viro goto exit; 20741f36f774SAl Viro case LAST_BIND: 20751f36f774SAl Viro audit_inode(pathname, dir); 20761f36f774SAl Viro goto ok; 20771f36f774SAl Viro } 2078a2c36b45SAl Viro 20791f36f774SAl Viro /* trailing slashes? */ 208031e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 20811f36f774SAl Viro goto exit; 20821f36f774SAl Viro 2083a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 2084a1e28038SAl Viro 2085a1e28038SAl Viro path->dentry = lookup_hash(nd); 2086a1e28038SAl Viro path->mnt = nd->path.mnt; 2087a1e28038SAl Viro 2088fb1cc555SAl Viro error = PTR_ERR(path->dentry); 2089fb1cc555SAl Viro if (IS_ERR(path->dentry)) { 2090fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2091fb1cc555SAl Viro goto exit; 2092fb1cc555SAl Viro } 2093fb1cc555SAl Viro 2094fb1cc555SAl Viro if (IS_ERR(nd->intent.open.file)) { 2095fb1cc555SAl Viro error = PTR_ERR(nd->intent.open.file); 2096fb1cc555SAl Viro goto exit_mutex_unlock; 2097fb1cc555SAl Viro } 2098fb1cc555SAl Viro 2099fb1cc555SAl Viro /* Negative dentry, just create the file */ 2100fb1cc555SAl Viro if (!path->dentry->d_inode) { 2101fb1cc555SAl Viro /* 2102fb1cc555SAl Viro * This write is needed to ensure that a 2103fb1cc555SAl Viro * ro->rw transition does not occur between 2104fb1cc555SAl Viro * the time when the file is created and when 2105fb1cc555SAl Viro * a permanent write count is taken through 2106fb1cc555SAl Viro * the 'struct file' in nameidata_to_filp(). 2107fb1cc555SAl Viro */ 2108fb1cc555SAl Viro error = mnt_want_write(nd->path.mnt); 2109fb1cc555SAl Viro if (error) 2110fb1cc555SAl Viro goto exit_mutex_unlock; 2111fb1cc555SAl Viro error = __open_namei_create(nd, path, open_flag, mode); 2112fb1cc555SAl Viro if (error) { 2113fb1cc555SAl Viro mnt_drop_write(nd->path.mnt); 2114fb1cc555SAl Viro goto exit; 2115fb1cc555SAl Viro } 2116fb1cc555SAl Viro filp = nameidata_to_filp(nd); 2117fb1cc555SAl Viro mnt_drop_write(nd->path.mnt); 2118d893f1bcSAl Viro path_put(&nd->path); 2119fb1cc555SAl Viro if (!IS_ERR(filp)) { 2120fb1cc555SAl Viro error = ima_file_check(filp, acc_mode); 2121fb1cc555SAl Viro if (error) { 2122fb1cc555SAl Viro fput(filp); 2123fb1cc555SAl Viro filp = ERR_PTR(error); 2124fb1cc555SAl Viro } 2125fb1cc555SAl Viro } 2126fb1cc555SAl Viro return filp; 2127fb1cc555SAl Viro } 2128fb1cc555SAl Viro 2129fb1cc555SAl Viro /* 2130fb1cc555SAl Viro * It already exists. 2131fb1cc555SAl Viro */ 2132fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2133fb1cc555SAl Viro audit_inode(pathname, path->dentry); 2134fb1cc555SAl Viro 2135fb1cc555SAl Viro error = -EEXIST; 21365b369df8SAl Viro if (open_flag & O_EXCL) 2137fb1cc555SAl Viro goto exit_dput; 2138fb1cc555SAl Viro 2139fb1cc555SAl Viro if (__follow_mount(path)) { 2140fb1cc555SAl Viro error = -ELOOP; 21415b369df8SAl Viro if (open_flag & O_NOFOLLOW) 2142fb1cc555SAl Viro goto exit_dput; 2143fb1cc555SAl Viro } 2144fb1cc555SAl Viro 2145fb1cc555SAl Viro error = -ENOENT; 2146fb1cc555SAl Viro if (!path->dentry->d_inode) 2147fb1cc555SAl Viro goto exit_dput; 21489e67f361SAl Viro 21499e67f361SAl Viro if (path->dentry->d_inode->i_op->follow_link) 2150fb1cc555SAl Viro return NULL; 2151fb1cc555SAl Viro 2152fb1cc555SAl Viro path_to_nameidata(path, nd); 215331e6b01fSNick Piggin nd->inode = path->dentry->d_inode; 2154fb1cc555SAl Viro error = -EISDIR; 215531e6b01fSNick Piggin if (S_ISDIR(nd->inode->i_mode)) 2156fb1cc555SAl Viro goto exit; 215767ee3ad2SAl Viro ok: 21589a66179eSAl Viro filp = finish_open(nd, open_flag, acc_mode); 2159fb1cc555SAl Viro return filp; 2160fb1cc555SAl Viro 2161fb1cc555SAl Viro exit_mutex_unlock: 2162fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2163fb1cc555SAl Viro exit_dput: 2164fb1cc555SAl Viro path_put_conditional(path, nd); 2165fb1cc555SAl Viro exit: 2166fb1cc555SAl Viro if (!IS_ERR(nd->intent.open.file)) 2167fb1cc555SAl Viro release_open_intent(nd); 2168fb1cc555SAl Viro path_put(&nd->path); 2169fb1cc555SAl Viro return ERR_PTR(error); 2170fb1cc555SAl Viro } 2171fb1cc555SAl Viro 21724a3fd211SDave Hansen /* 21734a3fd211SDave Hansen * Note that the low bits of the passed in "open_flag" 21744a3fd211SDave Hansen * are not the same as in the local variable "flag". See 21754a3fd211SDave Hansen * open_to_namei_flags() for more details. 21761da177e4SLinus Torvalds */ 2177a70e65dfSChristoph Hellwig struct file *do_filp_open(int dfd, const char *pathname, 21786e8341a1SAl Viro int open_flag, int mode, int acc_mode) 21791da177e4SLinus Torvalds { 21804a3fd211SDave Hansen struct file *filp; 2181a70e65dfSChristoph Hellwig struct nameidata nd; 21826e8341a1SAl Viro int error; 21839850c056SAl Viro struct path path; 21841da177e4SLinus Torvalds int count = 0; 2185d57999e1SDave Hansen int flag = open_to_namei_flags(open_flag); 218631e6b01fSNick Piggin int flags; 21871f36f774SAl Viro 21881f36f774SAl Viro if (!(open_flag & O_CREAT)) 21891f36f774SAl Viro mode = 0; 21901da177e4SLinus Torvalds 2191b1085ba8SLino Sanfilippo /* Must never be set by userspace */ 2192b1085ba8SLino Sanfilippo open_flag &= ~FMODE_NONOTIFY; 2193b1085ba8SLino Sanfilippo 21946b2f3d1fSChristoph Hellwig /* 21956b2f3d1fSChristoph Hellwig * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only 21966b2f3d1fSChristoph Hellwig * check for O_DSYNC if the need any syncing at all we enforce it's 21976b2f3d1fSChristoph Hellwig * always set instead of having to deal with possibly weird behaviour 21986b2f3d1fSChristoph Hellwig * for malicious applications setting only __O_SYNC. 21996b2f3d1fSChristoph Hellwig */ 22006b2f3d1fSChristoph Hellwig if (open_flag & __O_SYNC) 22016b2f3d1fSChristoph Hellwig open_flag |= O_DSYNC; 22026b2f3d1fSChristoph Hellwig 22036e8341a1SAl Viro if (!acc_mode) 22046d125529SAl Viro acc_mode = MAY_OPEN | ACC_MODE(open_flag); 22051da177e4SLinus Torvalds 2206834f2a4aSTrond Myklebust /* O_TRUNC implies we need access checks for write permissions */ 22074296e2cbSAl Viro if (open_flag & O_TRUNC) 2208834f2a4aSTrond Myklebust acc_mode |= MAY_WRITE; 2209834f2a4aSTrond Myklebust 22101da177e4SLinus Torvalds /* Allow the LSM permission hook to distinguish append 22111da177e4SLinus Torvalds access from general write access. */ 22124296e2cbSAl Viro if (open_flag & O_APPEND) 22131da177e4SLinus Torvalds acc_mode |= MAY_APPEND; 22141da177e4SLinus Torvalds 221531e6b01fSNick Piggin flags = LOOKUP_OPEN; 221631e6b01fSNick Piggin if (open_flag & O_CREAT) { 221731e6b01fSNick Piggin flags |= LOOKUP_CREATE; 221831e6b01fSNick Piggin if (open_flag & O_EXCL) 221931e6b01fSNick Piggin flags |= LOOKUP_EXCL; 2220654f562cSJ. R. Okajima } 222131e6b01fSNick Piggin if (open_flag & O_DIRECTORY) 222231e6b01fSNick Piggin flags |= LOOKUP_DIRECTORY; 222331e6b01fSNick Piggin if (!(open_flag & O_NOFOLLOW)) 222431e6b01fSNick Piggin flags |= LOOKUP_FOLLOW; 222531e6b01fSNick Piggin 222631e6b01fSNick Piggin filp = get_empty_filp(); 222731e6b01fSNick Piggin if (!filp) 222831e6b01fSNick Piggin return ERR_PTR(-ENFILE); 222931e6b01fSNick Piggin 223031e6b01fSNick Piggin filp->f_flags = open_flag; 223131e6b01fSNick Piggin nd.intent.open.file = filp; 223231e6b01fSNick Piggin nd.intent.open.flags = flag; 223331e6b01fSNick Piggin nd.intent.open.create_mode = mode; 223431e6b01fSNick Piggin 223531e6b01fSNick Piggin if (open_flag & O_CREAT) 223631e6b01fSNick Piggin goto creat; 223731e6b01fSNick Piggin 223831e6b01fSNick Piggin /* !O_CREAT, simple open */ 223931e6b01fSNick Piggin error = do_path_lookup(dfd, pathname, flags, &nd); 224031e6b01fSNick Piggin if (unlikely(error)) 224131e6b01fSNick Piggin goto out_filp; 224231e6b01fSNick Piggin error = -ELOOP; 224331e6b01fSNick Piggin if (!(nd.flags & LOOKUP_FOLLOW)) { 224431e6b01fSNick Piggin if (nd.inode->i_op->follow_link) 224531e6b01fSNick Piggin goto out_path; 224631e6b01fSNick Piggin } 224731e6b01fSNick Piggin error = -ENOTDIR; 224831e6b01fSNick Piggin if (nd.flags & LOOKUP_DIRECTORY) { 224931e6b01fSNick Piggin if (!nd.inode->i_op->lookup) 225031e6b01fSNick Piggin goto out_path; 225131e6b01fSNick Piggin } 225231e6b01fSNick Piggin audit_inode(pathname, nd.path.dentry); 225331e6b01fSNick Piggin filp = finish_open(&nd, open_flag, acc_mode); 225431e6b01fSNick Piggin return filp; 225531e6b01fSNick Piggin 225631e6b01fSNick Piggin creat: 225731e6b01fSNick Piggin /* OK, have to create the file. Find the parent. */ 225831e6b01fSNick Piggin error = path_init_rcu(dfd, pathname, 225931e6b01fSNick Piggin LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd); 226031e6b01fSNick Piggin if (error) 226131e6b01fSNick Piggin goto out_filp; 226231e6b01fSNick Piggin error = path_walk_rcu(pathname, &nd); 226331e6b01fSNick Piggin path_finish_rcu(&nd); 226431e6b01fSNick Piggin if (unlikely(error == -ECHILD || error == -ESTALE)) { 226531e6b01fSNick Piggin /* slower, locked walk */ 226631e6b01fSNick Piggin if (error == -ESTALE) { 226731e6b01fSNick Piggin reval: 226831e6b01fSNick Piggin flags |= LOOKUP_REVAL; 226931e6b01fSNick Piggin } 227031e6b01fSNick Piggin error = path_init(dfd, pathname, 227131e6b01fSNick Piggin LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd); 227231e6b01fSNick Piggin if (error) 227331e6b01fSNick Piggin goto out_filp; 227431e6b01fSNick Piggin 227531e6b01fSNick Piggin error = path_walk_simple(pathname, &nd); 227631e6b01fSNick Piggin } 227731e6b01fSNick Piggin if (unlikely(error)) 227831e6b01fSNick Piggin goto out_filp; 227931e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) 22809b4a9b14SAl Viro audit_inode(pathname, nd.path.dentry); 22811da177e4SLinus Torvalds 22821da177e4SLinus Torvalds /* 2283a2c36b45SAl Viro * We have the parent and last component. 22841da177e4SLinus Torvalds */ 228531e6b01fSNick Piggin nd.flags = flags; 22863e297b61SAl Viro filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname); 2287806b681cSAl Viro while (unlikely(!filp)) { /* trailing symlink */ 2288def4af30SAl Viro struct path holder; 2289def4af30SAl Viro void *cookie; 2290806b681cSAl Viro error = -ELOOP; 22911f36f774SAl Viro /* S_ISDIR part is a temporary automount kludge */ 229231e6b01fSNick Piggin if (!(nd.flags & LOOKUP_FOLLOW) && !S_ISDIR(nd.inode->i_mode)) 22931f36f774SAl Viro goto exit_dput; 22941f36f774SAl Viro if (count++ == 32) 2295806b681cSAl Viro goto exit_dput; 2296806b681cSAl Viro /* 2297806b681cSAl Viro * This is subtle. Instead of calling do_follow_link() we do 2298806b681cSAl Viro * the thing by hands. The reason is that this way we have zero 2299806b681cSAl Viro * link_count and path_walk() (called from ->follow_link) 2300806b681cSAl Viro * honoring LOOKUP_PARENT. After that we have the parent and 2301806b681cSAl Viro * last component, i.e. we are in the same situation as after 2302806b681cSAl Viro * the first path_walk(). Well, almost - if the last component 2303806b681cSAl Viro * is normal we get its copy stored in nd->last.name and we will 2304806b681cSAl Viro * have to putname() it when we are done. Procfs-like symlinks 2305806b681cSAl Viro * just set LAST_BIND. 2306806b681cSAl Viro */ 2307806b681cSAl Viro nd.flags |= LOOKUP_PARENT; 2308806b681cSAl Viro error = security_inode_follow_link(path.dentry, &nd); 2309806b681cSAl Viro if (error) 2310806b681cSAl Viro goto exit_dput; 2311def4af30SAl Viro error = __do_follow_link(&path, &nd, &cookie); 2312def4af30SAl Viro if (unlikely(error)) { 231331e6b01fSNick Piggin if (!IS_ERR(cookie) && nd.inode->i_op->put_link) 231431e6b01fSNick Piggin nd.inode->i_op->put_link(path.dentry, &nd, cookie); 2315806b681cSAl Viro /* nd.path had been dropped */ 231631e6b01fSNick Piggin nd.path = path; 231731e6b01fSNick Piggin goto out_path; 2318806b681cSAl Viro } 2319def4af30SAl Viro holder = path; 2320806b681cSAl Viro nd.flags &= ~LOOKUP_PARENT; 23213e297b61SAl Viro filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname); 232231e6b01fSNick Piggin if (nd.inode->i_op->put_link) 232331e6b01fSNick Piggin nd.inode->i_op->put_link(holder.dentry, &nd, cookie); 2324def4af30SAl Viro path_put(&holder); 2325806b681cSAl Viro } 232610fa8e62SAl Viro out: 23272a737871SAl Viro if (nd.root.mnt) 23282a737871SAl Viro path_put(&nd.root); 232931e6b01fSNick Piggin if (filp == ERR_PTR(-ESTALE) && !(flags & LOOKUP_REVAL)) 233010fa8e62SAl Viro goto reval; 233110fa8e62SAl Viro return filp; 23321da177e4SLinus Torvalds 2333806b681cSAl Viro exit_dput: 2334806b681cSAl Viro path_put_conditional(&path, &nd); 233531e6b01fSNick Piggin out_path: 233631e6b01fSNick Piggin path_put(&nd.path); 233731e6b01fSNick Piggin out_filp: 2338806b681cSAl Viro if (!IS_ERR(nd.intent.open.file)) 2339a70e65dfSChristoph Hellwig release_open_intent(&nd); 234010fa8e62SAl Viro filp = ERR_PTR(error); 234110fa8e62SAl Viro goto out; 2342de459215SKirill Korotaev } 23431da177e4SLinus Torvalds 23441da177e4SLinus Torvalds /** 2345a70e65dfSChristoph Hellwig * filp_open - open file and return file pointer 2346a70e65dfSChristoph Hellwig * 2347a70e65dfSChristoph Hellwig * @filename: path to open 2348a70e65dfSChristoph Hellwig * @flags: open flags as per the open(2) second argument 2349a70e65dfSChristoph Hellwig * @mode: mode for the new file if O_CREAT is set, else ignored 2350a70e65dfSChristoph Hellwig * 2351a70e65dfSChristoph Hellwig * This is the helper to open a file from kernelspace if you really 2352a70e65dfSChristoph Hellwig * have to. But in generally you should not do this, so please move 2353a70e65dfSChristoph Hellwig * along, nothing to see here.. 2354a70e65dfSChristoph Hellwig */ 2355a70e65dfSChristoph Hellwig struct file *filp_open(const char *filename, int flags, int mode) 2356a70e65dfSChristoph Hellwig { 23576e8341a1SAl Viro return do_filp_open(AT_FDCWD, filename, flags, mode, 0); 2358a70e65dfSChristoph Hellwig } 2359a70e65dfSChristoph Hellwig EXPORT_SYMBOL(filp_open); 2360a70e65dfSChristoph Hellwig 2361a70e65dfSChristoph Hellwig /** 23621da177e4SLinus Torvalds * lookup_create - lookup a dentry, creating it if it doesn't exist 23631da177e4SLinus Torvalds * @nd: nameidata info 23641da177e4SLinus Torvalds * @is_dir: directory flag 23651da177e4SLinus Torvalds * 23661da177e4SLinus Torvalds * Simple function to lookup and return a dentry and create it 23671da177e4SLinus Torvalds * if it doesn't exist. Is SMP-safe. 2368c663e5d8SChristoph Hellwig * 23694ac91378SJan Blunck * Returns with nd->path.dentry->d_inode->i_mutex locked. 23701da177e4SLinus Torvalds */ 23711da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir) 23721da177e4SLinus Torvalds { 2373c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 23741da177e4SLinus Torvalds 23754ac91378SJan Blunck mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 2376c663e5d8SChristoph Hellwig /* 2377c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 2378c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 2379c663e5d8SChristoph Hellwig */ 23801da177e4SLinus Torvalds if (nd->last_type != LAST_NORM) 23811da177e4SLinus Torvalds goto fail; 23821da177e4SLinus Torvalds nd->flags &= ~LOOKUP_PARENT; 23833516586aSAl Viro nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL; 2384a634904aSASANO Masahiro nd->intent.open.flags = O_EXCL; 2385c663e5d8SChristoph Hellwig 2386c663e5d8SChristoph Hellwig /* 2387c663e5d8SChristoph Hellwig * Do the final lookup. 2388c663e5d8SChristoph Hellwig */ 238949705b77SChristoph Hellwig dentry = lookup_hash(nd); 23901da177e4SLinus Torvalds if (IS_ERR(dentry)) 23911da177e4SLinus Torvalds goto fail; 2392c663e5d8SChristoph Hellwig 2393e9baf6e5SAl Viro if (dentry->d_inode) 2394e9baf6e5SAl Viro goto eexist; 2395c663e5d8SChristoph Hellwig /* 2396c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 2397c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 2398c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 2399c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 2400c663e5d8SChristoph Hellwig */ 2401e9baf6e5SAl Viro if (unlikely(!is_dir && nd->last.name[nd->last.len])) { 24021da177e4SLinus Torvalds dput(dentry); 24031da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 2404e9baf6e5SAl Viro } 2405e9baf6e5SAl Viro return dentry; 2406e9baf6e5SAl Viro eexist: 2407e9baf6e5SAl Viro dput(dentry); 2408e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 24091da177e4SLinus Torvalds fail: 24101da177e4SLinus Torvalds return dentry; 24111da177e4SLinus Torvalds } 2412f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create); 24131da177e4SLinus Torvalds 24141da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 24151da177e4SLinus Torvalds { 2416a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 24171da177e4SLinus Torvalds 24181da177e4SLinus Torvalds if (error) 24191da177e4SLinus Torvalds return error; 24201da177e4SLinus Torvalds 24211da177e4SLinus Torvalds if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) 24221da177e4SLinus Torvalds return -EPERM; 24231da177e4SLinus Torvalds 2424acfa4380SAl Viro if (!dir->i_op->mknod) 24251da177e4SLinus Torvalds return -EPERM; 24261da177e4SLinus Torvalds 242708ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 242808ce5f16SSerge E. Hallyn if (error) 242908ce5f16SSerge E. Hallyn return error; 243008ce5f16SSerge E. Hallyn 24311da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 24321da177e4SLinus Torvalds if (error) 24331da177e4SLinus Torvalds return error; 24341da177e4SLinus Torvalds 24351da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 2436a74574aaSStephen Smalley if (!error) 2437f38aa942SAmy Griffis fsnotify_create(dir, dentry); 24381da177e4SLinus Torvalds return error; 24391da177e4SLinus Torvalds } 24401da177e4SLinus Torvalds 2441463c3197SDave Hansen static int may_mknod(mode_t mode) 2442463c3197SDave Hansen { 2443463c3197SDave Hansen switch (mode & S_IFMT) { 2444463c3197SDave Hansen case S_IFREG: 2445463c3197SDave Hansen case S_IFCHR: 2446463c3197SDave Hansen case S_IFBLK: 2447463c3197SDave Hansen case S_IFIFO: 2448463c3197SDave Hansen case S_IFSOCK: 2449463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2450463c3197SDave Hansen return 0; 2451463c3197SDave Hansen case S_IFDIR: 2452463c3197SDave Hansen return -EPERM; 2453463c3197SDave Hansen default: 2454463c3197SDave Hansen return -EINVAL; 2455463c3197SDave Hansen } 2456463c3197SDave Hansen } 2457463c3197SDave Hansen 24582e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode, 24592e4d0924SHeiko Carstens unsigned, dev) 24601da177e4SLinus Torvalds { 24612ad94ae6SAl Viro int error; 24621da177e4SLinus Torvalds char *tmp; 24631da177e4SLinus Torvalds struct dentry *dentry; 24641da177e4SLinus Torvalds struct nameidata nd; 24651da177e4SLinus Torvalds 24661da177e4SLinus Torvalds if (S_ISDIR(mode)) 24671da177e4SLinus Torvalds return -EPERM; 24681da177e4SLinus Torvalds 24692ad94ae6SAl Viro error = user_path_parent(dfd, filename, &nd, &tmp); 24701da177e4SLinus Torvalds if (error) 24712ad94ae6SAl Viro return error; 24722ad94ae6SAl Viro 24731da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 2474463c3197SDave Hansen if (IS_ERR(dentry)) { 24751da177e4SLinus Torvalds error = PTR_ERR(dentry); 2476463c3197SDave Hansen goto out_unlock; 2477463c3197SDave Hansen } 24784ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2479ce3b0f8dSAl Viro mode &= ~current_umask(); 2480463c3197SDave Hansen error = may_mknod(mode); 2481463c3197SDave Hansen if (error) 2482463c3197SDave Hansen goto out_dput; 2483463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2484463c3197SDave Hansen if (error) 2485463c3197SDave Hansen goto out_dput; 2486be6d3e56SKentaro Takeda error = security_path_mknod(&nd.path, dentry, mode, dev); 2487be6d3e56SKentaro Takeda if (error) 2488be6d3e56SKentaro Takeda goto out_drop_write; 24891da177e4SLinus Torvalds switch (mode & S_IFMT) { 24901da177e4SLinus Torvalds case 0: case S_IFREG: 24914ac91378SJan Blunck error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd); 24921da177e4SLinus Torvalds break; 24931da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 24944ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode, 24951da177e4SLinus Torvalds new_decode_dev(dev)); 24961da177e4SLinus Torvalds break; 24971da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 24984ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0); 24991da177e4SLinus Torvalds break; 25001da177e4SLinus Torvalds } 2501be6d3e56SKentaro Takeda out_drop_write: 2502463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2503463c3197SDave Hansen out_dput: 25041da177e4SLinus Torvalds dput(dentry); 2505463c3197SDave Hansen out_unlock: 25064ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 25071d957f9bSJan Blunck path_put(&nd.path); 25081da177e4SLinus Torvalds putname(tmp); 25091da177e4SLinus Torvalds 25101da177e4SLinus Torvalds return error; 25111da177e4SLinus Torvalds } 25121da177e4SLinus Torvalds 25133480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev) 25145590ff0dSUlrich Drepper { 25155590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 25165590ff0dSUlrich Drepper } 25175590ff0dSUlrich Drepper 25181da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 25191da177e4SLinus Torvalds { 2520a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 25211da177e4SLinus Torvalds 25221da177e4SLinus Torvalds if (error) 25231da177e4SLinus Torvalds return error; 25241da177e4SLinus Torvalds 2525acfa4380SAl Viro if (!dir->i_op->mkdir) 25261da177e4SLinus Torvalds return -EPERM; 25271da177e4SLinus Torvalds 25281da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 25291da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 25301da177e4SLinus Torvalds if (error) 25311da177e4SLinus Torvalds return error; 25321da177e4SLinus Torvalds 25331da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2534a74574aaSStephen Smalley if (!error) 2535f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 25361da177e4SLinus Torvalds return error; 25371da177e4SLinus Torvalds } 25381da177e4SLinus Torvalds 25392e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode) 25401da177e4SLinus Torvalds { 25411da177e4SLinus Torvalds int error = 0; 25421da177e4SLinus Torvalds char * tmp; 25436902d925SDave Hansen struct dentry *dentry; 25446902d925SDave Hansen struct nameidata nd; 25451da177e4SLinus Torvalds 25462ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &tmp); 25472ad94ae6SAl Viro if (error) 25486902d925SDave Hansen goto out_err; 25491da177e4SLinus Torvalds 25501da177e4SLinus Torvalds dentry = lookup_create(&nd, 1); 25511da177e4SLinus Torvalds error = PTR_ERR(dentry); 25526902d925SDave Hansen if (IS_ERR(dentry)) 25536902d925SDave Hansen goto out_unlock; 25546902d925SDave Hansen 25554ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2556ce3b0f8dSAl Viro mode &= ~current_umask(); 2557463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2558463c3197SDave Hansen if (error) 2559463c3197SDave Hansen goto out_dput; 2560be6d3e56SKentaro Takeda error = security_path_mkdir(&nd.path, dentry, mode); 2561be6d3e56SKentaro Takeda if (error) 2562be6d3e56SKentaro Takeda goto out_drop_write; 25634ac91378SJan Blunck error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode); 2564be6d3e56SKentaro Takeda out_drop_write: 2565463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2566463c3197SDave Hansen out_dput: 25671da177e4SLinus Torvalds dput(dentry); 25686902d925SDave Hansen out_unlock: 25694ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 25701d957f9bSJan Blunck path_put(&nd.path); 25711da177e4SLinus Torvalds putname(tmp); 25726902d925SDave Hansen out_err: 25731da177e4SLinus Torvalds return error; 25741da177e4SLinus Torvalds } 25751da177e4SLinus Torvalds 25763cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode) 25775590ff0dSUlrich Drepper { 25785590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 25795590ff0dSUlrich Drepper } 25805590ff0dSUlrich Drepper 25811da177e4SLinus Torvalds /* 25821da177e4SLinus Torvalds * We try to drop the dentry early: we should have 25831da177e4SLinus Torvalds * a usage count of 2 if we're the only user of this 25841da177e4SLinus Torvalds * dentry, and if that is true (possibly after pruning 25851da177e4SLinus Torvalds * the dcache), then we drop the dentry now. 25861da177e4SLinus Torvalds * 25871da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 25881da177e4SLinus Torvalds * do a 25891da177e4SLinus Torvalds * 25901da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 25911da177e4SLinus Torvalds * return -EBUSY; 25921da177e4SLinus Torvalds * 25931da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 25941da177e4SLinus Torvalds * that is still in use by something else.. 25951da177e4SLinus Torvalds */ 25961da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 25971da177e4SLinus Torvalds { 25981da177e4SLinus Torvalds dget(dentry); 25991da177e4SLinus Torvalds shrink_dcache_parent(dentry); 26001da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 2601b7ab39f6SNick Piggin if (dentry->d_count == 2) 26021da177e4SLinus Torvalds __d_drop(dentry); 26031da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 26041da177e4SLinus Torvalds } 26051da177e4SLinus Torvalds 26061da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 26071da177e4SLinus Torvalds { 26081da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 26091da177e4SLinus Torvalds 26101da177e4SLinus Torvalds if (error) 26111da177e4SLinus Torvalds return error; 26121da177e4SLinus Torvalds 2613acfa4380SAl Viro if (!dir->i_op->rmdir) 26141da177e4SLinus Torvalds return -EPERM; 26151da177e4SLinus Torvalds 26161b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 26171da177e4SLinus Torvalds dentry_unhash(dentry); 26181da177e4SLinus Torvalds if (d_mountpoint(dentry)) 26191da177e4SLinus Torvalds error = -EBUSY; 26201da177e4SLinus Torvalds else { 26211da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 26221da177e4SLinus Torvalds if (!error) { 26231da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 2624d83c49f3SAl Viro if (!error) { 26251da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 2626d83c49f3SAl Viro dont_mount(dentry); 2627d83c49f3SAl Viro } 26281da177e4SLinus Torvalds } 26291da177e4SLinus Torvalds } 26301b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 26311da177e4SLinus Torvalds if (!error) { 26321da177e4SLinus Torvalds d_delete(dentry); 26331da177e4SLinus Torvalds } 26341da177e4SLinus Torvalds dput(dentry); 26351da177e4SLinus Torvalds 26361da177e4SLinus Torvalds return error; 26371da177e4SLinus Torvalds } 26381da177e4SLinus Torvalds 26395590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 26401da177e4SLinus Torvalds { 26411da177e4SLinus Torvalds int error = 0; 26421da177e4SLinus Torvalds char * name; 26431da177e4SLinus Torvalds struct dentry *dentry; 26441da177e4SLinus Torvalds struct nameidata nd; 26451da177e4SLinus Torvalds 26462ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 26471da177e4SLinus Torvalds if (error) 26482ad94ae6SAl Viro return error; 26491da177e4SLinus Torvalds 26501da177e4SLinus Torvalds switch(nd.last_type) { 26511da177e4SLinus Torvalds case LAST_DOTDOT: 26521da177e4SLinus Torvalds error = -ENOTEMPTY; 26531da177e4SLinus Torvalds goto exit1; 26541da177e4SLinus Torvalds case LAST_DOT: 26551da177e4SLinus Torvalds error = -EINVAL; 26561da177e4SLinus Torvalds goto exit1; 26571da177e4SLinus Torvalds case LAST_ROOT: 26581da177e4SLinus Torvalds error = -EBUSY; 26591da177e4SLinus Torvalds goto exit1; 26601da177e4SLinus Torvalds } 26610612d9fbSOGAWA Hirofumi 26620612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 26630612d9fbSOGAWA Hirofumi 26644ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 266549705b77SChristoph Hellwig dentry = lookup_hash(&nd); 26661da177e4SLinus Torvalds error = PTR_ERR(dentry); 26676902d925SDave Hansen if (IS_ERR(dentry)) 26686902d925SDave Hansen goto exit2; 26690622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 26700622753bSDave Hansen if (error) 26710622753bSDave Hansen goto exit3; 2672be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 2673be6d3e56SKentaro Takeda if (error) 2674be6d3e56SKentaro Takeda goto exit4; 26754ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 2676be6d3e56SKentaro Takeda exit4: 26770622753bSDave Hansen mnt_drop_write(nd.path.mnt); 26780622753bSDave Hansen exit3: 26791da177e4SLinus Torvalds dput(dentry); 26806902d925SDave Hansen exit2: 26814ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 26821da177e4SLinus Torvalds exit1: 26831d957f9bSJan Blunck path_put(&nd.path); 26841da177e4SLinus Torvalds putname(name); 26851da177e4SLinus Torvalds return error; 26861da177e4SLinus Torvalds } 26871da177e4SLinus Torvalds 26883cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 26895590ff0dSUlrich Drepper { 26905590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 26915590ff0dSUlrich Drepper } 26925590ff0dSUlrich Drepper 26931da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 26941da177e4SLinus Torvalds { 26951da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 26961da177e4SLinus Torvalds 26971da177e4SLinus Torvalds if (error) 26981da177e4SLinus Torvalds return error; 26991da177e4SLinus Torvalds 2700acfa4380SAl Viro if (!dir->i_op->unlink) 27011da177e4SLinus Torvalds return -EPERM; 27021da177e4SLinus Torvalds 27031b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 27041da177e4SLinus Torvalds if (d_mountpoint(dentry)) 27051da177e4SLinus Torvalds error = -EBUSY; 27061da177e4SLinus Torvalds else { 27071da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 2708bec1052eSAl Viro if (!error) { 27091da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 2710bec1052eSAl Viro if (!error) 2711d83c49f3SAl Viro dont_mount(dentry); 2712bec1052eSAl Viro } 27131da177e4SLinus Torvalds } 27141b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 27151da177e4SLinus Torvalds 27161da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 27171da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 2718ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 27191da177e4SLinus Torvalds d_delete(dentry); 27201da177e4SLinus Torvalds } 27210eeca283SRobert Love 27221da177e4SLinus Torvalds return error; 27231da177e4SLinus Torvalds } 27241da177e4SLinus Torvalds 27251da177e4SLinus Torvalds /* 27261da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 27271b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 27281da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 27291da177e4SLinus Torvalds * while waiting on the I/O. 27301da177e4SLinus Torvalds */ 27315590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 27321da177e4SLinus Torvalds { 27332ad94ae6SAl Viro int error; 27341da177e4SLinus Torvalds char *name; 27351da177e4SLinus Torvalds struct dentry *dentry; 27361da177e4SLinus Torvalds struct nameidata nd; 27371da177e4SLinus Torvalds struct inode *inode = NULL; 27381da177e4SLinus Torvalds 27392ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 27401da177e4SLinus Torvalds if (error) 27412ad94ae6SAl Viro return error; 27422ad94ae6SAl Viro 27431da177e4SLinus Torvalds error = -EISDIR; 27441da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 27451da177e4SLinus Torvalds goto exit1; 27460612d9fbSOGAWA Hirofumi 27470612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 27480612d9fbSOGAWA Hirofumi 27494ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 275049705b77SChristoph Hellwig dentry = lookup_hash(&nd); 27511da177e4SLinus Torvalds error = PTR_ERR(dentry); 27521da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 27531da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 27541da177e4SLinus Torvalds if (nd.last.name[nd.last.len]) 27551da177e4SLinus Torvalds goto slashes; 27561da177e4SLinus Torvalds inode = dentry->d_inode; 27571da177e4SLinus Torvalds if (inode) 27587de9c6eeSAl Viro ihold(inode); 27590622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 27600622753bSDave Hansen if (error) 27610622753bSDave Hansen goto exit2; 2762be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 2763be6d3e56SKentaro Takeda if (error) 2764be6d3e56SKentaro Takeda goto exit3; 27654ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 2766be6d3e56SKentaro Takeda exit3: 27670622753bSDave Hansen mnt_drop_write(nd.path.mnt); 27681da177e4SLinus Torvalds exit2: 27691da177e4SLinus Torvalds dput(dentry); 27701da177e4SLinus Torvalds } 27714ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 27721da177e4SLinus Torvalds if (inode) 27731da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 27741da177e4SLinus Torvalds exit1: 27751d957f9bSJan Blunck path_put(&nd.path); 27761da177e4SLinus Torvalds putname(name); 27771da177e4SLinus Torvalds return error; 27781da177e4SLinus Torvalds 27791da177e4SLinus Torvalds slashes: 27801da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 27811da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 27821da177e4SLinus Torvalds goto exit2; 27831da177e4SLinus Torvalds } 27841da177e4SLinus Torvalds 27852e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 27865590ff0dSUlrich Drepper { 27875590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 27885590ff0dSUlrich Drepper return -EINVAL; 27895590ff0dSUlrich Drepper 27905590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 27915590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 27925590ff0dSUlrich Drepper 27935590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 27945590ff0dSUlrich Drepper } 27955590ff0dSUlrich Drepper 27963480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 27975590ff0dSUlrich Drepper { 27985590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 27995590ff0dSUlrich Drepper } 28005590ff0dSUlrich Drepper 2801db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 28021da177e4SLinus Torvalds { 2803a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 28041da177e4SLinus Torvalds 28051da177e4SLinus Torvalds if (error) 28061da177e4SLinus Torvalds return error; 28071da177e4SLinus Torvalds 2808acfa4380SAl Viro if (!dir->i_op->symlink) 28091da177e4SLinus Torvalds return -EPERM; 28101da177e4SLinus Torvalds 28111da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 28121da177e4SLinus Torvalds if (error) 28131da177e4SLinus Torvalds return error; 28141da177e4SLinus Torvalds 28151da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 2816a74574aaSStephen Smalley if (!error) 2817f38aa942SAmy Griffis fsnotify_create(dir, dentry); 28181da177e4SLinus Torvalds return error; 28191da177e4SLinus Torvalds } 28201da177e4SLinus Torvalds 28212e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 28222e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 28231da177e4SLinus Torvalds { 28242ad94ae6SAl Viro int error; 28251da177e4SLinus Torvalds char *from; 28261da177e4SLinus Torvalds char *to; 28276902d925SDave Hansen struct dentry *dentry; 28286902d925SDave Hansen struct nameidata nd; 28291da177e4SLinus Torvalds 28301da177e4SLinus Torvalds from = getname(oldname); 28311da177e4SLinus Torvalds if (IS_ERR(from)) 28321da177e4SLinus Torvalds return PTR_ERR(from); 28332ad94ae6SAl Viro 28342ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 28352ad94ae6SAl Viro if (error) 28366902d925SDave Hansen goto out_putname; 28371da177e4SLinus Torvalds 28381da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 28391da177e4SLinus Torvalds error = PTR_ERR(dentry); 28406902d925SDave Hansen if (IS_ERR(dentry)) 28416902d925SDave Hansen goto out_unlock; 28426902d925SDave Hansen 284375c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 284475c3f29dSDave Hansen if (error) 284575c3f29dSDave Hansen goto out_dput; 2846be6d3e56SKentaro Takeda error = security_path_symlink(&nd.path, dentry, from); 2847be6d3e56SKentaro Takeda if (error) 2848be6d3e56SKentaro Takeda goto out_drop_write; 2849db2e747bSMiklos Szeredi error = vfs_symlink(nd.path.dentry->d_inode, dentry, from); 2850be6d3e56SKentaro Takeda out_drop_write: 285175c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 285275c3f29dSDave Hansen out_dput: 28531da177e4SLinus Torvalds dput(dentry); 28546902d925SDave Hansen out_unlock: 28554ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 28561d957f9bSJan Blunck path_put(&nd.path); 28571da177e4SLinus Torvalds putname(to); 28586902d925SDave Hansen out_putname: 28591da177e4SLinus Torvalds putname(from); 28601da177e4SLinus Torvalds return error; 28611da177e4SLinus Torvalds } 28621da177e4SLinus Torvalds 28633480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 28645590ff0dSUlrich Drepper { 28655590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 28665590ff0dSUlrich Drepper } 28675590ff0dSUlrich Drepper 28681da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 28691da177e4SLinus Torvalds { 28701da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 28711da177e4SLinus Torvalds int error; 28721da177e4SLinus Torvalds 28731da177e4SLinus Torvalds if (!inode) 28741da177e4SLinus Torvalds return -ENOENT; 28751da177e4SLinus Torvalds 2876a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 28771da177e4SLinus Torvalds if (error) 28781da177e4SLinus Torvalds return error; 28791da177e4SLinus Torvalds 28801da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 28811da177e4SLinus Torvalds return -EXDEV; 28821da177e4SLinus Torvalds 28831da177e4SLinus Torvalds /* 28841da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 28851da177e4SLinus Torvalds */ 28861da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 28871da177e4SLinus Torvalds return -EPERM; 2888acfa4380SAl Viro if (!dir->i_op->link) 28891da177e4SLinus Torvalds return -EPERM; 28907e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 28911da177e4SLinus Torvalds return -EPERM; 28921da177e4SLinus Torvalds 28931da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 28941da177e4SLinus Torvalds if (error) 28951da177e4SLinus Torvalds return error; 28961da177e4SLinus Torvalds 28977e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 28981da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 28997e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 2900e31e14ecSStephen Smalley if (!error) 29017e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 29021da177e4SLinus Torvalds return error; 29031da177e4SLinus Torvalds } 29041da177e4SLinus Torvalds 29051da177e4SLinus Torvalds /* 29061da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 29071da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 29081da177e4SLinus Torvalds * newname. --KAB 29091da177e4SLinus Torvalds * 29101da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 29111da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 29121da177e4SLinus Torvalds * and other special files. --ADM 29131da177e4SLinus Torvalds */ 29142e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 29152e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 29161da177e4SLinus Torvalds { 29171da177e4SLinus Torvalds struct dentry *new_dentry; 29182d8f3038SAl Viro struct nameidata nd; 29192d8f3038SAl Viro struct path old_path; 29201da177e4SLinus Torvalds int error; 29211da177e4SLinus Torvalds char *to; 29221da177e4SLinus Torvalds 292345c9b11aSUlrich Drepper if ((flags & ~AT_SYMLINK_FOLLOW) != 0) 2924c04030e1SUlrich Drepper return -EINVAL; 2925c04030e1SUlrich Drepper 29262d8f3038SAl Viro error = user_path_at(olddfd, oldname, 292745c9b11aSUlrich Drepper flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0, 29282d8f3038SAl Viro &old_path); 29291da177e4SLinus Torvalds if (error) 29302ad94ae6SAl Viro return error; 29312ad94ae6SAl Viro 29322ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 29331da177e4SLinus Torvalds if (error) 29341da177e4SLinus Torvalds goto out; 29351da177e4SLinus Torvalds error = -EXDEV; 29362d8f3038SAl Viro if (old_path.mnt != nd.path.mnt) 29371da177e4SLinus Torvalds goto out_release; 29381da177e4SLinus Torvalds new_dentry = lookup_create(&nd, 0); 29391da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 29406902d925SDave Hansen if (IS_ERR(new_dentry)) 29416902d925SDave Hansen goto out_unlock; 294275c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 294375c3f29dSDave Hansen if (error) 294475c3f29dSDave Hansen goto out_dput; 2945be6d3e56SKentaro Takeda error = security_path_link(old_path.dentry, &nd.path, new_dentry); 2946be6d3e56SKentaro Takeda if (error) 2947be6d3e56SKentaro Takeda goto out_drop_write; 29482d8f3038SAl Viro error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry); 2949be6d3e56SKentaro Takeda out_drop_write: 295075c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 295175c3f29dSDave Hansen out_dput: 29521da177e4SLinus Torvalds dput(new_dentry); 29536902d925SDave Hansen out_unlock: 29544ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 29551da177e4SLinus Torvalds out_release: 29561d957f9bSJan Blunck path_put(&nd.path); 29572ad94ae6SAl Viro putname(to); 29581da177e4SLinus Torvalds out: 29592d8f3038SAl Viro path_put(&old_path); 29601da177e4SLinus Torvalds 29611da177e4SLinus Torvalds return error; 29621da177e4SLinus Torvalds } 29631da177e4SLinus Torvalds 29643480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 29655590ff0dSUlrich Drepper { 2966c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 29675590ff0dSUlrich Drepper } 29685590ff0dSUlrich Drepper 29691da177e4SLinus Torvalds /* 29701da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 29711da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 29721da177e4SLinus Torvalds * Problems: 29731da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 29741da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 29751da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 2976a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 29771da177e4SLinus Torvalds * story. 29781da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 29791b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 29801da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 29811da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 2982a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 29831da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 29841da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 29851da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 2986a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 29871da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 29881da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 29891da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 29901da177e4SLinus Torvalds * d) some filesystems don't support opened-but-unlinked directories, 29911da177e4SLinus Torvalds * either because of layout or because they are not ready to deal with 29921da177e4SLinus Torvalds * all cases correctly. The latter will be fixed (taking this sort of 29931da177e4SLinus Torvalds * stuff into VFS), but the former is not going away. Solution: the same 29941da177e4SLinus Torvalds * trick as in rmdir(). 29951da177e4SLinus Torvalds * e) conversion from fhandle to dentry may come in the wrong moment - when 29961b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 29971da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 2998c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 29991da177e4SLinus Torvalds * locking]. 30001da177e4SLinus Torvalds */ 300175c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 30021da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30031da177e4SLinus Torvalds { 30041da177e4SLinus Torvalds int error = 0; 30051da177e4SLinus Torvalds struct inode *target; 30061da177e4SLinus Torvalds 30071da177e4SLinus Torvalds /* 30081da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 30091da177e4SLinus Torvalds * we'll need to flip '..'. 30101da177e4SLinus Torvalds */ 30111da177e4SLinus Torvalds if (new_dir != old_dir) { 3012f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 30131da177e4SLinus Torvalds if (error) 30141da177e4SLinus Torvalds return error; 30151da177e4SLinus Torvalds } 30161da177e4SLinus Torvalds 30171da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 30181da177e4SLinus Torvalds if (error) 30191da177e4SLinus Torvalds return error; 30201da177e4SLinus Torvalds 30211da177e4SLinus Torvalds target = new_dentry->d_inode; 3022d83c49f3SAl Viro if (target) 30231b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 30241da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 30251da177e4SLinus Torvalds error = -EBUSY; 3026d83c49f3SAl Viro else { 3027d83c49f3SAl Viro if (target) 3028d83c49f3SAl Viro dentry_unhash(new_dentry); 30291da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 3030d83c49f3SAl Viro } 30311da177e4SLinus Torvalds if (target) { 3032d83c49f3SAl Viro if (!error) { 30331da177e4SLinus Torvalds target->i_flags |= S_DEAD; 3034d83c49f3SAl Viro dont_mount(new_dentry); 3035d83c49f3SAl Viro } 30361b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 30371da177e4SLinus Torvalds if (d_unhashed(new_dentry)) 30381da177e4SLinus Torvalds d_rehash(new_dentry); 30391da177e4SLinus Torvalds dput(new_dentry); 30401da177e4SLinus Torvalds } 3041e31e14ecSStephen Smalley if (!error) 3042349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 30431da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 30441da177e4SLinus Torvalds return error; 30451da177e4SLinus Torvalds } 30461da177e4SLinus Torvalds 304775c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 30481da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30491da177e4SLinus Torvalds { 30501da177e4SLinus Torvalds struct inode *target; 30511da177e4SLinus Torvalds int error; 30521da177e4SLinus Torvalds 30531da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 30541da177e4SLinus Torvalds if (error) 30551da177e4SLinus Torvalds return error; 30561da177e4SLinus Torvalds 30571da177e4SLinus Torvalds dget(new_dentry); 30581da177e4SLinus Torvalds target = new_dentry->d_inode; 30591da177e4SLinus Torvalds if (target) 30601b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 30611da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 30621da177e4SLinus Torvalds error = -EBUSY; 30631da177e4SLinus Torvalds else 30641da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 30651da177e4SLinus Torvalds if (!error) { 3066bec1052eSAl Viro if (target) 3067d83c49f3SAl Viro dont_mount(new_dentry); 3068349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 30691da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 30701da177e4SLinus Torvalds } 30711da177e4SLinus Torvalds if (target) 30721b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 30731da177e4SLinus Torvalds dput(new_dentry); 30741da177e4SLinus Torvalds return error; 30751da177e4SLinus Torvalds } 30761da177e4SLinus Torvalds 30771da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 30781da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30791da177e4SLinus Torvalds { 30801da177e4SLinus Torvalds int error; 30811da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 308259b0df21SEric Paris const unsigned char *old_name; 30831da177e4SLinus Torvalds 30841da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 30851da177e4SLinus Torvalds return 0; 30861da177e4SLinus Torvalds 30871da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 30881da177e4SLinus Torvalds if (error) 30891da177e4SLinus Torvalds return error; 30901da177e4SLinus Torvalds 30911da177e4SLinus Torvalds if (!new_dentry->d_inode) 3092a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 30931da177e4SLinus Torvalds else 30941da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 30951da177e4SLinus Torvalds if (error) 30961da177e4SLinus Torvalds return error; 30971da177e4SLinus Torvalds 3098acfa4380SAl Viro if (!old_dir->i_op->rename) 30991da177e4SLinus Torvalds return -EPERM; 31001da177e4SLinus Torvalds 31010eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 31020eeca283SRobert Love 31031da177e4SLinus Torvalds if (is_dir) 31041da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 31051da177e4SLinus Torvalds else 31061da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3107123df294SAl Viro if (!error) 3108123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 31095a190ae6SAl Viro new_dentry->d_inode, old_dentry); 31100eeca283SRobert Love fsnotify_oldname_free(old_name); 31110eeca283SRobert Love 31121da177e4SLinus Torvalds return error; 31131da177e4SLinus Torvalds } 31141da177e4SLinus Torvalds 31152e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 31162e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 31171da177e4SLinus Torvalds { 31181da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 31191da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 31201da177e4SLinus Torvalds struct dentry *trap; 31211da177e4SLinus Torvalds struct nameidata oldnd, newnd; 31222ad94ae6SAl Viro char *from; 31232ad94ae6SAl Viro char *to; 31242ad94ae6SAl Viro int error; 31251da177e4SLinus Torvalds 31262ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 31271da177e4SLinus Torvalds if (error) 31281da177e4SLinus Torvalds goto exit; 31291da177e4SLinus Torvalds 31302ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 31311da177e4SLinus Torvalds if (error) 31321da177e4SLinus Torvalds goto exit1; 31331da177e4SLinus Torvalds 31341da177e4SLinus Torvalds error = -EXDEV; 31354ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 31361da177e4SLinus Torvalds goto exit2; 31371da177e4SLinus Torvalds 31384ac91378SJan Blunck old_dir = oldnd.path.dentry; 31391da177e4SLinus Torvalds error = -EBUSY; 31401da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 31411da177e4SLinus Torvalds goto exit2; 31421da177e4SLinus Torvalds 31434ac91378SJan Blunck new_dir = newnd.path.dentry; 31441da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 31451da177e4SLinus Torvalds goto exit2; 31461da177e4SLinus Torvalds 31470612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 31480612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 31494e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 31500612d9fbSOGAWA Hirofumi 31511da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 31521da177e4SLinus Torvalds 315349705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 31541da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 31551da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 31561da177e4SLinus Torvalds goto exit3; 31571da177e4SLinus Torvalds /* source must exist */ 31581da177e4SLinus Torvalds error = -ENOENT; 31591da177e4SLinus Torvalds if (!old_dentry->d_inode) 31601da177e4SLinus Torvalds goto exit4; 31611da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 31621da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 31631da177e4SLinus Torvalds error = -ENOTDIR; 31641da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 31651da177e4SLinus Torvalds goto exit4; 31661da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 31671da177e4SLinus Torvalds goto exit4; 31681da177e4SLinus Torvalds } 31691da177e4SLinus Torvalds /* source should not be ancestor of target */ 31701da177e4SLinus Torvalds error = -EINVAL; 31711da177e4SLinus Torvalds if (old_dentry == trap) 31721da177e4SLinus Torvalds goto exit4; 317349705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 31741da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 31751da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 31761da177e4SLinus Torvalds goto exit4; 31771da177e4SLinus Torvalds /* target should not be an ancestor of source */ 31781da177e4SLinus Torvalds error = -ENOTEMPTY; 31791da177e4SLinus Torvalds if (new_dentry == trap) 31801da177e4SLinus Torvalds goto exit5; 31811da177e4SLinus Torvalds 31829079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 31839079b1ebSDave Hansen if (error) 31849079b1ebSDave Hansen goto exit5; 3185be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3186be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3187be6d3e56SKentaro Takeda if (error) 3188be6d3e56SKentaro Takeda goto exit6; 31891da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 31901da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 3191be6d3e56SKentaro Takeda exit6: 31929079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 31931da177e4SLinus Torvalds exit5: 31941da177e4SLinus Torvalds dput(new_dentry); 31951da177e4SLinus Torvalds exit4: 31961da177e4SLinus Torvalds dput(old_dentry); 31971da177e4SLinus Torvalds exit3: 31981da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 31991da177e4SLinus Torvalds exit2: 32001d957f9bSJan Blunck path_put(&newnd.path); 32012ad94ae6SAl Viro putname(to); 32021da177e4SLinus Torvalds exit1: 32031d957f9bSJan Blunck path_put(&oldnd.path); 32041da177e4SLinus Torvalds putname(from); 32052ad94ae6SAl Viro exit: 32061da177e4SLinus Torvalds return error; 32071da177e4SLinus Torvalds } 32081da177e4SLinus Torvalds 3209a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 32105590ff0dSUlrich Drepper { 32115590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 32125590ff0dSUlrich Drepper } 32135590ff0dSUlrich Drepper 32141da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 32151da177e4SLinus Torvalds { 32161da177e4SLinus Torvalds int len; 32171da177e4SLinus Torvalds 32181da177e4SLinus Torvalds len = PTR_ERR(link); 32191da177e4SLinus Torvalds if (IS_ERR(link)) 32201da177e4SLinus Torvalds goto out; 32211da177e4SLinus Torvalds 32221da177e4SLinus Torvalds len = strlen(link); 32231da177e4SLinus Torvalds if (len > (unsigned) buflen) 32241da177e4SLinus Torvalds len = buflen; 32251da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 32261da177e4SLinus Torvalds len = -EFAULT; 32271da177e4SLinus Torvalds out: 32281da177e4SLinus Torvalds return len; 32291da177e4SLinus Torvalds } 32301da177e4SLinus Torvalds 32311da177e4SLinus Torvalds /* 32321da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 32331da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 32341da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 32351da177e4SLinus Torvalds */ 32361da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 32371da177e4SLinus Torvalds { 32381da177e4SLinus Torvalds struct nameidata nd; 3239cc314eefSLinus Torvalds void *cookie; 3240694a1764SMarcin Slusarz int res; 3241cc314eefSLinus Torvalds 32421da177e4SLinus Torvalds nd.depth = 0; 3243cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3244694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3245694a1764SMarcin Slusarz return PTR_ERR(cookie); 3246694a1764SMarcin Slusarz 3247694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 32481da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3249cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3250694a1764SMarcin Slusarz return res; 32511da177e4SLinus Torvalds } 32521da177e4SLinus Torvalds 32531da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 32541da177e4SLinus Torvalds { 32551da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 32561da177e4SLinus Torvalds } 32571da177e4SLinus Torvalds 32581da177e4SLinus Torvalds /* get the link contents into pagecache */ 32591da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 32601da177e4SLinus Torvalds { 3261ebd09abbSDuane Griffin char *kaddr; 32621da177e4SLinus Torvalds struct page *page; 32631da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3264090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 32651da177e4SLinus Torvalds if (IS_ERR(page)) 32666fe6900eSNick Piggin return (char*)page; 32671da177e4SLinus Torvalds *ppage = page; 3268ebd09abbSDuane Griffin kaddr = kmap(page); 3269ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3270ebd09abbSDuane Griffin return kaddr; 32711da177e4SLinus Torvalds } 32721da177e4SLinus Torvalds 32731da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 32741da177e4SLinus Torvalds { 32751da177e4SLinus Torvalds struct page *page = NULL; 32761da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 32771da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 32781da177e4SLinus Torvalds if (page) { 32791da177e4SLinus Torvalds kunmap(page); 32801da177e4SLinus Torvalds page_cache_release(page); 32811da177e4SLinus Torvalds } 32821da177e4SLinus Torvalds return res; 32831da177e4SLinus Torvalds } 32841da177e4SLinus Torvalds 3285cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 32861da177e4SLinus Torvalds { 3287cc314eefSLinus Torvalds struct page *page = NULL; 32881da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 3289cc314eefSLinus Torvalds return page; 32901da177e4SLinus Torvalds } 32911da177e4SLinus Torvalds 3292cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 32931da177e4SLinus Torvalds { 3294cc314eefSLinus Torvalds struct page *page = cookie; 3295cc314eefSLinus Torvalds 3296cc314eefSLinus Torvalds if (page) { 32971da177e4SLinus Torvalds kunmap(page); 32981da177e4SLinus Torvalds page_cache_release(page); 32991da177e4SLinus Torvalds } 33001da177e4SLinus Torvalds } 33011da177e4SLinus Torvalds 330254566b2cSNick Piggin /* 330354566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 330454566b2cSNick Piggin */ 330554566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 33061da177e4SLinus Torvalds { 33071da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 33080adb25d2SKirill Korotaev struct page *page; 3309afddba49SNick Piggin void *fsdata; 3310beb497abSDmitriy Monakhov int err; 33111da177e4SLinus Torvalds char *kaddr; 331254566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 331354566b2cSNick Piggin if (nofs) 331454566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 33151da177e4SLinus Torvalds 33167e53cac4SNeilBrown retry: 3317afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 331854566b2cSNick Piggin flags, &page, &fsdata); 33191da177e4SLinus Torvalds if (err) 3320afddba49SNick Piggin goto fail; 3321afddba49SNick Piggin 33221da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 33231da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 33241da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 3325afddba49SNick Piggin 3326afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 3327afddba49SNick Piggin page, fsdata); 33281da177e4SLinus Torvalds if (err < 0) 33291da177e4SLinus Torvalds goto fail; 3330afddba49SNick Piggin if (err < len-1) 3331afddba49SNick Piggin goto retry; 3332afddba49SNick Piggin 33331da177e4SLinus Torvalds mark_inode_dirty(inode); 33341da177e4SLinus Torvalds return 0; 33351da177e4SLinus Torvalds fail: 33361da177e4SLinus Torvalds return err; 33371da177e4SLinus Torvalds } 33381da177e4SLinus Torvalds 33390adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 33400adb25d2SKirill Korotaev { 33410adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 334254566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 33430adb25d2SKirill Korotaev } 33440adb25d2SKirill Korotaev 334592e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 33461da177e4SLinus Torvalds .readlink = generic_readlink, 33471da177e4SLinus Torvalds .follow_link = page_follow_link_light, 33481da177e4SLinus Torvalds .put_link = page_put_link, 33491da177e4SLinus Torvalds }; 33501da177e4SLinus Torvalds 33512d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 33521da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 33531da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 33541da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 33551da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 33561da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 33571da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 33581da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 33591da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 33601da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 33610adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 33621da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 33631da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 33641da177e4SLinus Torvalds EXPORT_SYMBOL(path_lookup); 3365d1811465SAl Viro EXPORT_SYMBOL(kern_path); 336616f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 3367f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 33688c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission); 33691da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 33701da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 33711da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 33721da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 33731da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 33741da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 33751da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 33761da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 33771da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 33781da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 33791da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 33801da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 33811da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 33821da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 3383