11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/namei.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* 81da177e4SLinus Torvalds * Some corrections by tytso. 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname 121da177e4SLinus Torvalds * lookup logic. 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture. 151da177e4SLinus Torvalds */ 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #include <linux/init.h> 181da177e4SLinus Torvalds #include <linux/module.h> 191da177e4SLinus Torvalds #include <linux/slab.h> 201da177e4SLinus Torvalds #include <linux/fs.h> 211da177e4SLinus Torvalds #include <linux/namei.h> 221da177e4SLinus Torvalds #include <linux/pagemap.h> 230eeca283SRobert Love #include <linux/fsnotify.h> 241da177e4SLinus Torvalds #include <linux/personality.h> 251da177e4SLinus Torvalds #include <linux/security.h> 266146f0d5SMimi Zohar #include <linux/ima.h> 271da177e4SLinus Torvalds #include <linux/syscalls.h> 281da177e4SLinus Torvalds #include <linux/mount.h> 291da177e4SLinus Torvalds #include <linux/audit.h> 3016f7e0feSRandy Dunlap #include <linux/capability.h> 31834f2a4aSTrond Myklebust #include <linux/file.h> 325590ff0dSUlrich Drepper #include <linux/fcntl.h> 3308ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h> 345ad4e53bSAl Viro #include <linux/fs_struct.h> 351da177e4SLinus Torvalds #include <asm/uaccess.h> 361da177e4SLinus Torvalds 37e81e3f4dSEric Paris #include "internal.h" 38e81e3f4dSEric Paris 391da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer] 401da177e4SLinus Torvalds * Fundamental changes in the pathname lookup mechanisms (namei) 411da177e4SLinus Torvalds * were necessary because of omirr. The reason is that omirr needs 421da177e4SLinus Torvalds * to know the _real_ pathname, not the user-supplied one, in case 431da177e4SLinus Torvalds * of symlinks (and also when transname replacements occur). 441da177e4SLinus Torvalds * 451da177e4SLinus Torvalds * The new code replaces the old recursive symlink resolution with 461da177e4SLinus Torvalds * an iterative one (in case of non-nested symlink chains). It does 471da177e4SLinus Torvalds * this with calls to <fs>_follow_link(). 481da177e4SLinus Torvalds * As a side effect, dir_namei(), _namei() and follow_link() are now 491da177e4SLinus Torvalds * replaced with a single function lookup_dentry() that can handle all 501da177e4SLinus Torvalds * the special cases of the former code. 511da177e4SLinus Torvalds * 521da177e4SLinus Torvalds * With the new dcache, the pathname is stored at each inode, at least as 531da177e4SLinus Torvalds * long as the refcount of the inode is positive. As a side effect, the 541da177e4SLinus Torvalds * size of the dcache depends on the inode cache and thus is dynamic. 551da177e4SLinus Torvalds * 561da177e4SLinus Torvalds * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink 571da177e4SLinus Torvalds * resolution to correspond with current state of the code. 581da177e4SLinus Torvalds * 591da177e4SLinus Torvalds * Note that the symlink resolution is not *completely* iterative. 601da177e4SLinus Torvalds * There is still a significant amount of tail- and mid- recursion in 611da177e4SLinus Torvalds * the algorithm. Also, note that <fs>_readlink() is not used in 621da177e4SLinus Torvalds * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() 631da177e4SLinus Torvalds * may return different results than <fs>_follow_link(). Many virtual 641da177e4SLinus Torvalds * filesystems (including /proc) exhibit this behavior. 651da177e4SLinus Torvalds */ 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: 681da177e4SLinus Torvalds * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL 691da177e4SLinus Torvalds * and the name already exists in form of a symlink, try to create the new 701da177e4SLinus Torvalds * name indicated by the symlink. The old code always complained that the 711da177e4SLinus Torvalds * name already exists, due to not following the symlink even if its target 721da177e4SLinus Torvalds * is nonexistent. The new semantics affects also mknod() and link() when 731da177e4SLinus Torvalds * the name is a symlink pointing to a non-existant name. 741da177e4SLinus Torvalds * 751da177e4SLinus Torvalds * I don't know which semantics is the right one, since I have no access 761da177e4SLinus Torvalds * to standards. But I found by trial that HP-UX 9.0 has the full "new" 771da177e4SLinus Torvalds * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the 781da177e4SLinus Torvalds * "old" one. Personally, I think the new semantics is much more logical. 791da177e4SLinus Torvalds * Note that "ln old new" where "new" is a symlink pointing to a non-existing 801da177e4SLinus Torvalds * file does succeed in both HP-UX and SunOs, but not in Solaris 811da177e4SLinus Torvalds * and in the old Linux semantics. 821da177e4SLinus Torvalds */ 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink 851da177e4SLinus Torvalds * semantics. See the comments in "open_namei" and "do_link" below. 861da177e4SLinus Torvalds * 871da177e4SLinus Torvalds * [10-Sep-98 Alan Modra] Another symlink change. 881da177e4SLinus Torvalds */ 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: 911da177e4SLinus Torvalds * inside the path - always follow. 921da177e4SLinus Torvalds * in the last component in creation/removal/renaming - never follow. 931da177e4SLinus Torvalds * if LOOKUP_FOLLOW passed - follow. 941da177e4SLinus Torvalds * if the pathname has trailing slashes - follow. 951da177e4SLinus Torvalds * otherwise - don't follow. 961da177e4SLinus Torvalds * (applied in that order). 971da177e4SLinus Torvalds * 981da177e4SLinus Torvalds * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT 991da177e4SLinus Torvalds * restored for 2.4. This is the last surviving part of old 4.2BSD bug. 1001da177e4SLinus Torvalds * During the 2.4 we need to fix the userland stuff depending on it - 1011da177e4SLinus Torvalds * hopefully we will be able to get rid of that wart in 2.5. So far only 1021da177e4SLinus Torvalds * XEmacs seems to be relying on it... 1031da177e4SLinus Torvalds */ 1041da177e4SLinus Torvalds /* 1051da177e4SLinus Torvalds * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) 106a11f3a05SArjan van de Ven * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives 1071da177e4SLinus Torvalds * any extra contention... 1081da177e4SLinus Torvalds */ 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional 1111da177e4SLinus Torvalds * checking and hopefully speeding things up, we copy filenames to the 1121da177e4SLinus Torvalds * kernel data space before using them.. 1131da177e4SLinus Torvalds * 1141da177e4SLinus Torvalds * POSIX.1 2.4: an empty pathname is invalid (ENOENT). 1151da177e4SLinus Torvalds * PATH_MAX includes the nul terminator --RR. 1161da177e4SLinus Torvalds */ 117858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page) 1181da177e4SLinus Torvalds { 1191da177e4SLinus Torvalds int retval; 1201da177e4SLinus Torvalds unsigned long len = PATH_MAX; 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds if (!segment_eq(get_fs(), KERNEL_DS)) { 1231da177e4SLinus Torvalds if ((unsigned long) filename >= TASK_SIZE) 1241da177e4SLinus Torvalds return -EFAULT; 1251da177e4SLinus Torvalds if (TASK_SIZE - (unsigned long) filename < PATH_MAX) 1261da177e4SLinus Torvalds len = TASK_SIZE - (unsigned long) filename; 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds retval = strncpy_from_user(page, filename, len); 1301da177e4SLinus Torvalds if (retval > 0) { 1311da177e4SLinus Torvalds if (retval < len) 1321da177e4SLinus Torvalds return 0; 1331da177e4SLinus Torvalds return -ENAMETOOLONG; 1341da177e4SLinus Torvalds } else if (!retval) 1351da177e4SLinus Torvalds retval = -ENOENT; 1361da177e4SLinus Torvalds return retval; 1371da177e4SLinus Torvalds } 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds char * getname(const char __user * filename) 1401da177e4SLinus Torvalds { 1411da177e4SLinus Torvalds char *tmp, *result; 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds result = ERR_PTR(-ENOMEM); 1441da177e4SLinus Torvalds tmp = __getname(); 1451da177e4SLinus Torvalds if (tmp) { 1461da177e4SLinus Torvalds int retval = do_getname(filename, tmp); 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds result = tmp; 1491da177e4SLinus Torvalds if (retval < 0) { 1501da177e4SLinus Torvalds __putname(tmp); 1511da177e4SLinus Torvalds result = ERR_PTR(retval); 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds audit_getname(result); 1551da177e4SLinus Torvalds return result; 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 1591da177e4SLinus Torvalds void putname(const char *name) 1601da177e4SLinus Torvalds { 1615ac3a9c2SAl Viro if (unlikely(!audit_dummy_context())) 1621da177e4SLinus Torvalds audit_putname(name); 1631da177e4SLinus Torvalds else 1641da177e4SLinus Torvalds __putname(name); 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds EXPORT_SYMBOL(putname); 1671da177e4SLinus Torvalds #endif 1681da177e4SLinus Torvalds 1695909ccaaSLinus Torvalds /* 1705909ccaaSLinus Torvalds * This does basic POSIX ACL permission checking 1715909ccaaSLinus Torvalds */ 172b74c79e9SNick Piggin static int acl_permission_check(struct inode *inode, int mask, unsigned int flags, 173b74c79e9SNick Piggin int (*check_acl)(struct inode *inode, int mask, unsigned int flags)) 1745909ccaaSLinus Torvalds { 1755909ccaaSLinus Torvalds umode_t mode = inode->i_mode; 1765909ccaaSLinus Torvalds 1775909ccaaSLinus Torvalds mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 1785909ccaaSLinus Torvalds 1795909ccaaSLinus Torvalds if (current_fsuid() == inode->i_uid) 1805909ccaaSLinus Torvalds mode >>= 6; 1815909ccaaSLinus Torvalds else { 1825909ccaaSLinus Torvalds if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) { 183b74c79e9SNick Piggin int error = check_acl(inode, mask, flags); 1845909ccaaSLinus Torvalds if (error != -EAGAIN) 1855909ccaaSLinus Torvalds return error; 1865909ccaaSLinus Torvalds } 1875909ccaaSLinus Torvalds 1885909ccaaSLinus Torvalds if (in_group_p(inode->i_gid)) 1895909ccaaSLinus Torvalds mode >>= 3; 1905909ccaaSLinus Torvalds } 1915909ccaaSLinus Torvalds 1925909ccaaSLinus Torvalds /* 1935909ccaaSLinus Torvalds * If the DACs are ok we don't need any capability check. 1945909ccaaSLinus Torvalds */ 1955909ccaaSLinus Torvalds if ((mask & ~mode) == 0) 1965909ccaaSLinus Torvalds return 0; 1975909ccaaSLinus Torvalds return -EACCES; 1985909ccaaSLinus Torvalds } 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds /** 2011da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 2021da177e4SLinus Torvalds * @inode: inode to check access rights for 2031da177e4SLinus Torvalds * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 2041da177e4SLinus Torvalds * @check_acl: optional callback to check for Posix ACLs 20539191628SRandy Dunlap * @flags: IPERM_FLAG_ flags. 2061da177e4SLinus Torvalds * 2071da177e4SLinus Torvalds * Used to check for read/write/execute permissions on a file. 2081da177e4SLinus Torvalds * We use "fsuid" for this, letting us set arbitrary permissions 2091da177e4SLinus Torvalds * for filesystem access without changing the "normal" uids which 210b74c79e9SNick Piggin * are used for other things. 211b74c79e9SNick Piggin * 212b74c79e9SNick Piggin * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk 213b74c79e9SNick Piggin * request cannot be satisfied (eg. requires blocking or too much complexity). 214b74c79e9SNick Piggin * It would then be called again in ref-walk mode. 2151da177e4SLinus Torvalds */ 216b74c79e9SNick Piggin int generic_permission(struct inode *inode, int mask, unsigned int flags, 217b74c79e9SNick Piggin int (*check_acl)(struct inode *inode, int mask, unsigned int flags)) 2181da177e4SLinus Torvalds { 2195909ccaaSLinus Torvalds int ret; 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds /* 2225909ccaaSLinus Torvalds * Do the basic POSIX ACL permission checks. 2231da177e4SLinus Torvalds */ 224b74c79e9SNick Piggin ret = acl_permission_check(inode, mask, flags, check_acl); 2255909ccaaSLinus Torvalds if (ret != -EACCES) 2265909ccaaSLinus Torvalds return ret; 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds /* 2291da177e4SLinus Torvalds * Read/write DACs are always overridable. 2301da177e4SLinus Torvalds * Executable DACs are overridable if at least one exec bit is set. 2311da177e4SLinus Torvalds */ 232f696a365SMiklos Szeredi if (!(mask & MAY_EXEC) || execute_ok(inode)) 2331da177e4SLinus Torvalds if (capable(CAP_DAC_OVERRIDE)) 2341da177e4SLinus Torvalds return 0; 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds /* 2371da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 2381da177e4SLinus Torvalds */ 2397ea66001SSerge E. Hallyn mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 2401da177e4SLinus Torvalds if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE))) 2411da177e4SLinus Torvalds if (capable(CAP_DAC_READ_SEARCH)) 2421da177e4SLinus Torvalds return 0; 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds return -EACCES; 2451da177e4SLinus Torvalds } 2461da177e4SLinus Torvalds 247cb23beb5SChristoph Hellwig /** 248cb23beb5SChristoph Hellwig * inode_permission - check for access rights to a given inode 249cb23beb5SChristoph Hellwig * @inode: inode to check permission on 250cb23beb5SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 251cb23beb5SChristoph Hellwig * 252cb23beb5SChristoph Hellwig * Used to check for read/write/execute permissions on an inode. 253cb23beb5SChristoph Hellwig * We use "fsuid" for this, letting us set arbitrary permissions 254cb23beb5SChristoph Hellwig * for filesystem access without changing the "normal" uids which 255cb23beb5SChristoph Hellwig * are used for other things. 256cb23beb5SChristoph Hellwig */ 257f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask) 2581da177e4SLinus Torvalds { 259e6305c43SAl Viro int retval; 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds if (mask & MAY_WRITE) { 26222590e41SMiklos Szeredi umode_t mode = inode->i_mode; 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds /* 2651da177e4SLinus Torvalds * Nobody gets write access to a read-only fs. 2661da177e4SLinus Torvalds */ 2671da177e4SLinus Torvalds if (IS_RDONLY(inode) && 2681da177e4SLinus Torvalds (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 2691da177e4SLinus Torvalds return -EROFS; 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds /* 2721da177e4SLinus Torvalds * Nobody gets write access to an immutable file. 2731da177e4SLinus Torvalds */ 2741da177e4SLinus Torvalds if (IS_IMMUTABLE(inode)) 2751da177e4SLinus Torvalds return -EACCES; 2761da177e4SLinus Torvalds } 2771da177e4SLinus Torvalds 278acfa4380SAl Viro if (inode->i_op->permission) 279b74c79e9SNick Piggin retval = inode->i_op->permission(inode, mask, 0); 280f696a365SMiklos Szeredi else 281b74c79e9SNick Piggin retval = generic_permission(inode, mask, 0, 282b74c79e9SNick Piggin inode->i_op->check_acl); 283f696a365SMiklos Szeredi 2841da177e4SLinus Torvalds if (retval) 2851da177e4SLinus Torvalds return retval; 2861da177e4SLinus Torvalds 28708ce5f16SSerge E. Hallyn retval = devcgroup_inode_permission(inode, mask); 28808ce5f16SSerge E. Hallyn if (retval) 28908ce5f16SSerge E. Hallyn return retval; 29008ce5f16SSerge E. Hallyn 291d09ca739SEric Paris return security_inode_permission(inode, mask); 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds 294e4543eddSChristoph Hellwig /** 2958c744fb8SChristoph Hellwig * file_permission - check for additional access rights to a given file 2968c744fb8SChristoph Hellwig * @file: file to check access rights for 2978c744fb8SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 2988c744fb8SChristoph Hellwig * 2998c744fb8SChristoph Hellwig * Used to check for read/write/execute permissions on an already opened 3008c744fb8SChristoph Hellwig * file. 3018c744fb8SChristoph Hellwig * 3028c744fb8SChristoph Hellwig * Note: 3038c744fb8SChristoph Hellwig * Do not use this function in new code. All access checks should 304cb23beb5SChristoph Hellwig * be done using inode_permission(). 3058c744fb8SChristoph Hellwig */ 3068c744fb8SChristoph Hellwig int file_permission(struct file *file, int mask) 3078c744fb8SChristoph Hellwig { 308f419a2e3SAl Viro return inode_permission(file->f_path.dentry->d_inode, mask); 3098c744fb8SChristoph Hellwig } 3108c744fb8SChristoph Hellwig 3111da177e4SLinus Torvalds /* 3121da177e4SLinus Torvalds * get_write_access() gets write permission for a file. 3131da177e4SLinus Torvalds * put_write_access() releases this write permission. 3141da177e4SLinus Torvalds * This is used for regular files. 3151da177e4SLinus Torvalds * We cannot support write (and maybe mmap read-write shared) accesses and 3161da177e4SLinus Torvalds * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode 3171da177e4SLinus Torvalds * can have the following values: 3181da177e4SLinus Torvalds * 0: no writers, no VM_DENYWRITE mappings 3191da177e4SLinus Torvalds * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist 3201da177e4SLinus Torvalds * > 0: (i_writecount) users are writing to the file. 3211da177e4SLinus Torvalds * 3221da177e4SLinus Torvalds * Normally we operate on that counter with atomic_{inc,dec} and it's safe 3231da177e4SLinus Torvalds * except for the cases where we don't hold i_writecount yet. Then we need to 3241da177e4SLinus Torvalds * use {get,deny}_write_access() - these functions check the sign and refuse 3251da177e4SLinus Torvalds * to do the change if sign is wrong. Exclusion between them is provided by 3261da177e4SLinus Torvalds * the inode->i_lock spinlock. 3271da177e4SLinus Torvalds */ 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds int get_write_access(struct inode * inode) 3301da177e4SLinus Torvalds { 3311da177e4SLinus Torvalds spin_lock(&inode->i_lock); 3321da177e4SLinus Torvalds if (atomic_read(&inode->i_writecount) < 0) { 3331da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3341da177e4SLinus Torvalds return -ETXTBSY; 3351da177e4SLinus Torvalds } 3361da177e4SLinus Torvalds atomic_inc(&inode->i_writecount); 3371da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds return 0; 3401da177e4SLinus Torvalds } 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds int deny_write_access(struct file * file) 3431da177e4SLinus Torvalds { 3440f7fc9e4SJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds spin_lock(&inode->i_lock); 3471da177e4SLinus Torvalds if (atomic_read(&inode->i_writecount) > 0) { 3481da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3491da177e4SLinus Torvalds return -ETXTBSY; 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds atomic_dec(&inode->i_writecount); 3521da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds return 0; 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds 3571d957f9bSJan Blunck /** 3585dd784d0SJan Blunck * path_get - get a reference to a path 3595dd784d0SJan Blunck * @path: path to get the reference to 3605dd784d0SJan Blunck * 3615dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 3625dd784d0SJan Blunck */ 3635dd784d0SJan Blunck void path_get(struct path *path) 3645dd784d0SJan Blunck { 3655dd784d0SJan Blunck mntget(path->mnt); 3665dd784d0SJan Blunck dget(path->dentry); 3675dd784d0SJan Blunck } 3685dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 3695dd784d0SJan Blunck 3705dd784d0SJan Blunck /** 371b3e19d92SNick Piggin * path_get_long - get a long reference to a path 372b3e19d92SNick Piggin * @path: path to get the reference to 373b3e19d92SNick Piggin * 374b3e19d92SNick Piggin * Given a path increment the reference count to the dentry and the vfsmount. 375b3e19d92SNick Piggin */ 376b3e19d92SNick Piggin void path_get_long(struct path *path) 377b3e19d92SNick Piggin { 378b3e19d92SNick Piggin mntget_long(path->mnt); 379b3e19d92SNick Piggin dget(path->dentry); 380b3e19d92SNick Piggin } 381b3e19d92SNick Piggin 382b3e19d92SNick Piggin /** 3831d957f9bSJan Blunck * path_put - put a reference to a path 3841d957f9bSJan Blunck * @path: path to put the reference to 3851d957f9bSJan Blunck * 3861d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 3871d957f9bSJan Blunck */ 3881d957f9bSJan Blunck void path_put(struct path *path) 3891da177e4SLinus Torvalds { 3901d957f9bSJan Blunck dput(path->dentry); 3911d957f9bSJan Blunck mntput(path->mnt); 3921da177e4SLinus Torvalds } 3931d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 3941da177e4SLinus Torvalds 395834f2a4aSTrond Myklebust /** 396b3e19d92SNick Piggin * path_put_long - put a long reference to a path 397b3e19d92SNick Piggin * @path: path to put the reference to 398b3e19d92SNick Piggin * 399b3e19d92SNick Piggin * Given a path decrement the reference count to the dentry and the vfsmount. 400b3e19d92SNick Piggin */ 401b3e19d92SNick Piggin void path_put_long(struct path *path) 402b3e19d92SNick Piggin { 403b3e19d92SNick Piggin dput(path->dentry); 404b3e19d92SNick Piggin mntput_long(path->mnt); 405b3e19d92SNick Piggin } 406b3e19d92SNick Piggin 407b3e19d92SNick Piggin /** 40831e6b01fSNick Piggin * nameidata_drop_rcu - drop this nameidata out of rcu-walk 40931e6b01fSNick Piggin * @nd: nameidata pathwalk data to drop 41039191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 41131e6b01fSNick Piggin * 41231e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 41331e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). __drop_rcu* functions attempt 41431e6b01fSNick Piggin * to drop out of rcu-walk mode and take normal reference counts on dentries 41531e6b01fSNick Piggin * and vfsmounts to transition to rcu-walk mode. __drop_rcu* functions take 41631e6b01fSNick Piggin * refcounts at the last known good point before rcu-walk got stuck, so 41731e6b01fSNick Piggin * ref-walk may continue from there. If this is not successful (eg. a seqcount 41831e6b01fSNick Piggin * has changed), then failure is returned and path walk restarts from the 41931e6b01fSNick Piggin * beginning in ref-walk mode. 42031e6b01fSNick Piggin * 42131e6b01fSNick Piggin * nameidata_drop_rcu attempts to drop the current nd->path and nd->root into 42231e6b01fSNick Piggin * ref-walk. Must be called from rcu-walk context. 42331e6b01fSNick Piggin */ 42431e6b01fSNick Piggin static int nameidata_drop_rcu(struct nameidata *nd) 42531e6b01fSNick Piggin { 42631e6b01fSNick Piggin struct fs_struct *fs = current->fs; 42731e6b01fSNick Piggin struct dentry *dentry = nd->path.dentry; 42831e6b01fSNick Piggin 42931e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 43031e6b01fSNick Piggin if (nd->root.mnt) { 43131e6b01fSNick Piggin spin_lock(&fs->lock); 43231e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 43331e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 43431e6b01fSNick Piggin goto err_root; 43531e6b01fSNick Piggin } 43631e6b01fSNick Piggin spin_lock(&dentry->d_lock); 43731e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 43831e6b01fSNick Piggin goto err; 43931e6b01fSNick Piggin BUG_ON(nd->inode != dentry->d_inode); 44031e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 44131e6b01fSNick Piggin if (nd->root.mnt) { 44231e6b01fSNick Piggin path_get(&nd->root); 44331e6b01fSNick Piggin spin_unlock(&fs->lock); 44431e6b01fSNick Piggin } 44531e6b01fSNick Piggin mntget(nd->path.mnt); 44631e6b01fSNick Piggin 44731e6b01fSNick Piggin rcu_read_unlock(); 44831e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 44931e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 45031e6b01fSNick Piggin return 0; 45131e6b01fSNick Piggin err: 45231e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 45331e6b01fSNick Piggin err_root: 45431e6b01fSNick Piggin if (nd->root.mnt) 45531e6b01fSNick Piggin spin_unlock(&fs->lock); 45631e6b01fSNick Piggin return -ECHILD; 45731e6b01fSNick Piggin } 45831e6b01fSNick Piggin 45931e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */ 46031e6b01fSNick Piggin static inline int nameidata_drop_rcu_maybe(struct nameidata *nd) 46131e6b01fSNick Piggin { 46231e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) 46331e6b01fSNick Piggin return nameidata_drop_rcu(nd); 46431e6b01fSNick Piggin return 0; 46531e6b01fSNick Piggin } 46631e6b01fSNick Piggin 46731e6b01fSNick Piggin /** 46831e6b01fSNick Piggin * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk 46931e6b01fSNick Piggin * @nd: nameidata pathwalk data to drop 47031e6b01fSNick Piggin * @dentry: dentry to drop 47139191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 47231e6b01fSNick Piggin * 47331e6b01fSNick Piggin * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root, 47431e6b01fSNick Piggin * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on 47531e6b01fSNick Piggin * @nd. Must be called from rcu-walk context. 47631e6b01fSNick Piggin */ 47731e6b01fSNick Piggin static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry) 47831e6b01fSNick Piggin { 47931e6b01fSNick Piggin struct fs_struct *fs = current->fs; 48031e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 48131e6b01fSNick Piggin 48290dbb77bSNick Piggin /* 48390dbb77bSNick Piggin * It can be possible to revalidate the dentry that we started 48490dbb77bSNick Piggin * the path walk with. force_reval_path may also revalidate the 48590dbb77bSNick Piggin * dentry already committed to the nameidata. 48690dbb77bSNick Piggin */ 48790dbb77bSNick Piggin if (unlikely(parent == dentry)) 48890dbb77bSNick Piggin return nameidata_drop_rcu(nd); 48990dbb77bSNick Piggin 49031e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 49131e6b01fSNick Piggin if (nd->root.mnt) { 49231e6b01fSNick Piggin spin_lock(&fs->lock); 49331e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 49431e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 49531e6b01fSNick Piggin goto err_root; 49631e6b01fSNick Piggin } 49731e6b01fSNick Piggin spin_lock(&parent->d_lock); 49831e6b01fSNick Piggin spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); 49931e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 50031e6b01fSNick Piggin goto err; 50131e6b01fSNick Piggin /* 50231e6b01fSNick Piggin * If the sequence check on the child dentry passed, then the child has 50331e6b01fSNick Piggin * not been removed from its parent. This means the parent dentry must 50431e6b01fSNick Piggin * be valid and able to take a reference at this point. 50531e6b01fSNick Piggin */ 50631e6b01fSNick Piggin BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); 50731e6b01fSNick Piggin BUG_ON(!parent->d_count); 50831e6b01fSNick Piggin parent->d_count++; 50931e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 51031e6b01fSNick Piggin spin_unlock(&parent->d_lock); 51131e6b01fSNick Piggin if (nd->root.mnt) { 51231e6b01fSNick Piggin path_get(&nd->root); 51331e6b01fSNick Piggin spin_unlock(&fs->lock); 51431e6b01fSNick Piggin } 51531e6b01fSNick Piggin mntget(nd->path.mnt); 51631e6b01fSNick Piggin 51731e6b01fSNick Piggin rcu_read_unlock(); 51831e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 51931e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 52031e6b01fSNick Piggin return 0; 52131e6b01fSNick Piggin err: 52231e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 52331e6b01fSNick Piggin spin_unlock(&parent->d_lock); 52431e6b01fSNick Piggin err_root: 52531e6b01fSNick Piggin if (nd->root.mnt) 52631e6b01fSNick Piggin spin_unlock(&fs->lock); 52731e6b01fSNick Piggin return -ECHILD; 52831e6b01fSNick Piggin } 52931e6b01fSNick Piggin 53031e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */ 53131e6b01fSNick Piggin static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry) 53231e6b01fSNick Piggin { 53331e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) 53431e6b01fSNick Piggin return nameidata_dentry_drop_rcu(nd, dentry); 53531e6b01fSNick Piggin return 0; 53631e6b01fSNick Piggin } 53731e6b01fSNick Piggin 53831e6b01fSNick Piggin /** 53931e6b01fSNick Piggin * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk 54031e6b01fSNick Piggin * @nd: nameidata pathwalk data to drop 54139191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 54231e6b01fSNick Piggin * 54331e6b01fSNick Piggin * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk. 54431e6b01fSNick Piggin * nd->path should be the final element of the lookup, so nd->root is discarded. 54531e6b01fSNick Piggin * Must be called from rcu-walk context. 54631e6b01fSNick Piggin */ 54731e6b01fSNick Piggin static int nameidata_drop_rcu_last(struct nameidata *nd) 54831e6b01fSNick Piggin { 54931e6b01fSNick Piggin struct dentry *dentry = nd->path.dentry; 55031e6b01fSNick Piggin 55131e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 55231e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 55331e6b01fSNick Piggin nd->root.mnt = NULL; 55431e6b01fSNick Piggin spin_lock(&dentry->d_lock); 55531e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 55631e6b01fSNick Piggin goto err_unlock; 55731e6b01fSNick Piggin BUG_ON(nd->inode != dentry->d_inode); 55831e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 55931e6b01fSNick Piggin 56031e6b01fSNick Piggin mntget(nd->path.mnt); 56131e6b01fSNick Piggin 56231e6b01fSNick Piggin rcu_read_unlock(); 56331e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 56431e6b01fSNick Piggin 56531e6b01fSNick Piggin return 0; 56631e6b01fSNick Piggin 56731e6b01fSNick Piggin err_unlock: 56831e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 56931e6b01fSNick Piggin rcu_read_unlock(); 57031e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 57131e6b01fSNick Piggin return -ECHILD; 57231e6b01fSNick Piggin } 57331e6b01fSNick Piggin 57431e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */ 57531e6b01fSNick Piggin static inline int nameidata_drop_rcu_last_maybe(struct nameidata *nd) 57631e6b01fSNick Piggin { 57731e6b01fSNick Piggin if (likely(nd->flags & LOOKUP_RCU)) 57831e6b01fSNick Piggin return nameidata_drop_rcu_last(nd); 57931e6b01fSNick Piggin return 0; 58031e6b01fSNick Piggin } 58131e6b01fSNick Piggin 58231e6b01fSNick Piggin /** 583834f2a4aSTrond Myklebust * release_open_intent - free up open intent resources 584834f2a4aSTrond Myklebust * @nd: pointer to nameidata 585834f2a4aSTrond Myklebust */ 586834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd) 587834f2a4aSTrond Myklebust { 5880f7fc9e4SJosef "Jeff" Sipek if (nd->intent.open.file->f_path.dentry == NULL) 589834f2a4aSTrond Myklebust put_filp(nd->intent.open.file); 590834f2a4aSTrond Myklebust else 591834f2a4aSTrond Myklebust fput(nd->intent.open.file); 592834f2a4aSTrond Myklebust } 593834f2a4aSTrond Myklebust 594bb20c18dSNick Piggin /* 595bb20c18dSNick Piggin * Call d_revalidate and handle filesystems that request rcu-walk 596bb20c18dSNick Piggin * to be dropped. This may be called and return in rcu-walk mode, 597bb20c18dSNick Piggin * regardless of success or error. If -ECHILD is returned, the caller 598bb20c18dSNick Piggin * must return -ECHILD back up the path walk stack so path walk may 599bb20c18dSNick Piggin * be restarted in ref-walk mode. 600bb20c18dSNick Piggin */ 60134286d66SNick Piggin static int d_revalidate(struct dentry *dentry, struct nameidata *nd) 60234286d66SNick Piggin { 60334286d66SNick Piggin int status; 60434286d66SNick Piggin 60534286d66SNick Piggin status = dentry->d_op->d_revalidate(dentry, nd); 60634286d66SNick Piggin if (status == -ECHILD) { 60734286d66SNick Piggin if (nameidata_dentry_drop_rcu(nd, dentry)) 60834286d66SNick Piggin return status; 60934286d66SNick Piggin status = dentry->d_op->d_revalidate(dentry, nd); 61034286d66SNick Piggin } 61134286d66SNick Piggin 61234286d66SNick Piggin return status; 61334286d66SNick Piggin } 61434286d66SNick Piggin 615bcdc5e01SIan Kent static inline struct dentry * 616bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd) 617bcdc5e01SIan Kent { 61834286d66SNick Piggin int status; 61934286d66SNick Piggin 62034286d66SNick Piggin status = d_revalidate(dentry, nd); 621bcdc5e01SIan Kent if (unlikely(status <= 0)) { 622bcdc5e01SIan Kent /* 623bcdc5e01SIan Kent * The dentry failed validation. 624bcdc5e01SIan Kent * If d_revalidate returned 0 attempt to invalidate 625bcdc5e01SIan Kent * the dentry otherwise d_revalidate is asking us 626bcdc5e01SIan Kent * to return a fail status. 627bcdc5e01SIan Kent */ 62834286d66SNick Piggin if (status < 0) { 62934286d66SNick Piggin /* If we're in rcu-walk, we don't have a ref */ 63034286d66SNick Piggin if (!(nd->flags & LOOKUP_RCU)) 63134286d66SNick Piggin dput(dentry); 63234286d66SNick Piggin dentry = ERR_PTR(status); 63334286d66SNick Piggin 63434286d66SNick Piggin } else { 63534286d66SNick Piggin /* Don't d_invalidate in rcu-walk mode */ 63634286d66SNick Piggin if (nameidata_dentry_drop_rcu_maybe(nd, dentry)) 63734286d66SNick Piggin return ERR_PTR(-ECHILD); 638bcdc5e01SIan Kent if (!d_invalidate(dentry)) { 639bcdc5e01SIan Kent dput(dentry); 640bcdc5e01SIan Kent dentry = NULL; 641bcdc5e01SIan Kent } 642bcdc5e01SIan Kent } 643bcdc5e01SIan Kent } 644bcdc5e01SIan Kent return dentry; 645bcdc5e01SIan Kent } 646bcdc5e01SIan Kent 647fb045adbSNick Piggin static inline int need_reval_dot(struct dentry *dentry) 648fb045adbSNick Piggin { 649fb045adbSNick Piggin if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE))) 650fb045adbSNick Piggin return 0; 651fb045adbSNick Piggin 652fb045adbSNick Piggin if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))) 653fb045adbSNick Piggin return 0; 654fb045adbSNick Piggin 655fb045adbSNick Piggin return 1; 656fb045adbSNick Piggin } 657fb045adbSNick Piggin 6581da177e4SLinus Torvalds /* 65939159de2SJeff Layton * force_reval_path - force revalidation of a dentry 66039159de2SJeff Layton * 66139159de2SJeff Layton * In some situations the path walking code will trust dentries without 66239159de2SJeff Layton * revalidating them. This causes problems for filesystems that depend on 66339159de2SJeff Layton * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set 66439159de2SJeff Layton * (which indicates that it's possible for the dentry to go stale), force 66539159de2SJeff Layton * a d_revalidate call before proceeding. 66639159de2SJeff Layton * 66739159de2SJeff Layton * Returns 0 if the revalidation was successful. If the revalidation fails, 66839159de2SJeff Layton * either return the error returned by d_revalidate or -ESTALE if the 66939159de2SJeff Layton * revalidation it just returned 0. If d_revalidate returns 0, we attempt to 67039159de2SJeff Layton * invalidate the dentry. It's up to the caller to handle putting references 67139159de2SJeff Layton * to the path if necessary. 67239159de2SJeff Layton */ 67339159de2SJeff Layton static int 67439159de2SJeff Layton force_reval_path(struct path *path, struct nameidata *nd) 67539159de2SJeff Layton { 67639159de2SJeff Layton int status; 67739159de2SJeff Layton struct dentry *dentry = path->dentry; 67839159de2SJeff Layton 67939159de2SJeff Layton /* 68039159de2SJeff Layton * only check on filesystems where it's possible for the dentry to 681fb045adbSNick Piggin * become stale. 68239159de2SJeff Layton */ 683fb045adbSNick Piggin if (!need_reval_dot(dentry)) 68439159de2SJeff Layton return 0; 68539159de2SJeff Layton 68634286d66SNick Piggin status = d_revalidate(dentry, nd); 68739159de2SJeff Layton if (status > 0) 68839159de2SJeff Layton return 0; 68939159de2SJeff Layton 69039159de2SJeff Layton if (!status) { 691bb20c18dSNick Piggin /* Don't d_invalidate in rcu-walk mode */ 692bb20c18dSNick Piggin if (nameidata_drop_rcu(nd)) 693bb20c18dSNick Piggin return -ECHILD; 69439159de2SJeff Layton d_invalidate(dentry); 69539159de2SJeff Layton status = -ESTALE; 69639159de2SJeff Layton } 69739159de2SJeff Layton return status; 69839159de2SJeff Layton } 69939159de2SJeff Layton 70039159de2SJeff Layton /* 701b75b5086SAl Viro * Short-cut version of permission(), for calling on directories 702b75b5086SAl Viro * during pathname resolution. Combines parts of permission() 703b75b5086SAl Viro * and generic_permission(), and tests ONLY for MAY_EXEC permission. 7041da177e4SLinus Torvalds * 7051da177e4SLinus Torvalds * If appropriate, check DAC only. If not appropriate, or 706b75b5086SAl Viro * short-cut DAC fails, then call ->permission() to do more 7071da177e4SLinus Torvalds * complete permission check. 7081da177e4SLinus Torvalds */ 709b74c79e9SNick Piggin static inline int exec_permission(struct inode *inode, unsigned int flags) 7101da177e4SLinus Torvalds { 7115909ccaaSLinus Torvalds int ret; 7121da177e4SLinus Torvalds 713cb9179eaSLinus Torvalds if (inode->i_op->permission) { 714b74c79e9SNick Piggin ret = inode->i_op->permission(inode, MAY_EXEC, flags); 715b74c79e9SNick Piggin } else { 716b74c79e9SNick Piggin ret = acl_permission_check(inode, MAY_EXEC, flags, 717b74c79e9SNick Piggin inode->i_op->check_acl); 718cb9179eaSLinus Torvalds } 719b74c79e9SNick Piggin if (likely(!ret)) 7201da177e4SLinus Torvalds goto ok; 721b74c79e9SNick Piggin if (ret == -ECHILD) 72231e6b01fSNick Piggin return ret; 7231da177e4SLinus Torvalds 724f1ac9f6bSLinus Torvalds if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH)) 7251da177e4SLinus Torvalds goto ok; 7261da177e4SLinus Torvalds 7275909ccaaSLinus Torvalds return ret; 7281da177e4SLinus Torvalds ok: 729b74c79e9SNick Piggin return security_inode_exec_permission(inode, flags); 7301da177e4SLinus Torvalds } 7311da177e4SLinus Torvalds 7322a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 7332a737871SAl Viro { 734f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 735f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 7362a737871SAl Viro } 7372a737871SAl Viro 7386de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 7396de88d72SAl Viro 74031e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 74131e6b01fSNick Piggin { 74231e6b01fSNick Piggin if (!nd->root.mnt) { 74331e6b01fSNick Piggin struct fs_struct *fs = current->fs; 744c28cc364SNick Piggin unsigned seq; 745c28cc364SNick Piggin 746c28cc364SNick Piggin do { 747c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 74831e6b01fSNick Piggin nd->root = fs->root; 749c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 75031e6b01fSNick Piggin } 75131e6b01fSNick Piggin } 75231e6b01fSNick Piggin 753f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 7541da177e4SLinus Torvalds { 75531e6b01fSNick Piggin int ret; 75631e6b01fSNick Piggin 7571da177e4SLinus Torvalds if (IS_ERR(link)) 7581da177e4SLinus Torvalds goto fail; 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds if (*link == '/') { 7612a737871SAl Viro set_root(nd); 7621d957f9bSJan Blunck path_put(&nd->path); 7632a737871SAl Viro nd->path = nd->root; 7642a737871SAl Viro path_get(&nd->root); 7651da177e4SLinus Torvalds } 76631e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 767b4091d5fSChristoph Hellwig 76831e6b01fSNick Piggin ret = link_path_walk(link, nd); 76931e6b01fSNick Piggin return ret; 7701da177e4SLinus Torvalds fail: 7711d957f9bSJan Blunck path_put(&nd->path); 7721da177e4SLinus Torvalds return PTR_ERR(link); 7731da177e4SLinus Torvalds } 7741da177e4SLinus Torvalds 7751d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 776051d3812SIan Kent { 777051d3812SIan Kent dput(path->dentry); 7784ac91378SJan Blunck if (path->mnt != nd->path.mnt) 779051d3812SIan Kent mntput(path->mnt); 780051d3812SIan Kent } 781051d3812SIan Kent 7827b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path, 7837b9337aaSNick Piggin struct nameidata *nd) 784051d3812SIan Kent { 78531e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 7864ac91378SJan Blunck dput(nd->path.dentry); 78731e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 7884ac91378SJan Blunck mntput(nd->path.mnt); 7899a229683SHuang Shijie } 79031e6b01fSNick Piggin nd->path.mnt = path->mnt; 7914ac91378SJan Blunck nd->path.dentry = path->dentry; 792051d3812SIan Kent } 793051d3812SIan Kent 794def4af30SAl Viro static __always_inline int 7957b9337aaSNick Piggin __do_follow_link(const struct path *link, struct nameidata *nd, void **p) 7961da177e4SLinus Torvalds { 7971da177e4SLinus Torvalds int error; 7987b9337aaSNick Piggin struct dentry *dentry = link->dentry; 7991da177e4SLinus Torvalds 8007b9337aaSNick Piggin touch_atime(link->mnt, dentry); 8011da177e4SLinus Torvalds nd_set_link(nd, NULL); 802cd4e91d3SAl Viro 80387556ef1SDavid Howells if (link->mnt == nd->path.mnt) 8047b9337aaSNick Piggin mntget(link->mnt); 80531e6b01fSNick Piggin 80686acdca1SAl Viro nd->last_type = LAST_BIND; 807def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 808def4af30SAl Viro error = PTR_ERR(*p); 809def4af30SAl Viro if (!IS_ERR(*p)) { 8101da177e4SLinus Torvalds char *s = nd_get_link(nd); 811cc314eefSLinus Torvalds error = 0; 8121da177e4SLinus Torvalds if (s) 8131da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 81439159de2SJeff Layton else if (nd->last_type == LAST_BIND) { 81539159de2SJeff Layton error = force_reval_path(&nd->path, nd); 81639159de2SJeff Layton if (error) 81739159de2SJeff Layton path_put(&nd->path); 81839159de2SJeff Layton } 8191da177e4SLinus Torvalds } 8201da177e4SLinus Torvalds return error; 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds 8231da177e4SLinus Torvalds /* 8241da177e4SLinus Torvalds * This limits recursive symlink follows to 8, while 8251da177e4SLinus Torvalds * limiting consecutive symlinks to 40. 8261da177e4SLinus Torvalds * 8271da177e4SLinus Torvalds * Without that kind of total limit, nasty chains of consecutive 8281da177e4SLinus Torvalds * symlinks can cause almost arbitrarily long lookups. 8291da177e4SLinus Torvalds */ 83090ebe565SAl Viro static inline int do_follow_link(struct path *path, struct nameidata *nd) 8311da177e4SLinus Torvalds { 832def4af30SAl Viro void *cookie; 8331da177e4SLinus Torvalds int err = -ELOOP; 8341da177e4SLinus Torvalds if (current->link_count >= MAX_NESTED_LINKS) 8351da177e4SLinus Torvalds goto loop; 8361da177e4SLinus Torvalds if (current->total_link_count >= 40) 8371da177e4SLinus Torvalds goto loop; 8381da177e4SLinus Torvalds BUG_ON(nd->depth >= MAX_NESTED_LINKS); 8391da177e4SLinus Torvalds cond_resched(); 84090ebe565SAl Viro err = security_inode_follow_link(path->dentry, nd); 8411da177e4SLinus Torvalds if (err) 8421da177e4SLinus Torvalds goto loop; 8431da177e4SLinus Torvalds current->link_count++; 8441da177e4SLinus Torvalds current->total_link_count++; 8451da177e4SLinus Torvalds nd->depth++; 846def4af30SAl Viro err = __do_follow_link(path, nd, &cookie); 847def4af30SAl Viro if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link) 848def4af30SAl Viro path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie); 849258fa999SAl Viro path_put(path); 8501da177e4SLinus Torvalds current->link_count--; 8511da177e4SLinus Torvalds nd->depth--; 8521da177e4SLinus Torvalds return err; 8531da177e4SLinus Torvalds loop: 8541d957f9bSJan Blunck path_put_conditional(path, nd); 8551d957f9bSJan Blunck path_put(&nd->path); 8561da177e4SLinus Torvalds return err; 8571da177e4SLinus Torvalds } 8581da177e4SLinus Torvalds 85931e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 86031e6b01fSNick Piggin { 86131e6b01fSNick Piggin struct vfsmount *parent; 86231e6b01fSNick Piggin struct dentry *mountpoint; 86331e6b01fSNick Piggin 86431e6b01fSNick Piggin parent = path->mnt->mnt_parent; 86531e6b01fSNick Piggin if (parent == path->mnt) 86631e6b01fSNick Piggin return 0; 86731e6b01fSNick Piggin mountpoint = path->mnt->mnt_mountpoint; 86831e6b01fSNick Piggin path->dentry = mountpoint; 86931e6b01fSNick Piggin path->mnt = parent; 87031e6b01fSNick Piggin return 1; 87131e6b01fSNick Piggin } 87231e6b01fSNick Piggin 873bab77ebfSAl Viro int follow_up(struct path *path) 8741da177e4SLinus Torvalds { 8751da177e4SLinus Torvalds struct vfsmount *parent; 8761da177e4SLinus Torvalds struct dentry *mountpoint; 87799b7db7bSNick Piggin 87899b7db7bSNick Piggin br_read_lock(vfsmount_lock); 879bab77ebfSAl Viro parent = path->mnt->mnt_parent; 880bab77ebfSAl Viro if (parent == path->mnt) { 88199b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 8821da177e4SLinus Torvalds return 0; 8831da177e4SLinus Torvalds } 8841da177e4SLinus Torvalds mntget(parent); 885bab77ebfSAl Viro mountpoint = dget(path->mnt->mnt_mountpoint); 88699b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 887bab77ebfSAl Viro dput(path->dentry); 888bab77ebfSAl Viro path->dentry = mountpoint; 889bab77ebfSAl Viro mntput(path->mnt); 890bab77ebfSAl Viro path->mnt = parent; 8911da177e4SLinus Torvalds return 1; 8921da177e4SLinus Torvalds } 8931da177e4SLinus Torvalds 894b5c84bf6SNick Piggin /* 8959875cf80SDavid Howells * Perform an automount 8969875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 8979875cf80SDavid Howells * were called with. 8981da177e4SLinus Torvalds */ 8999875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags, 9009875cf80SDavid Howells bool *need_mntput) 90131e6b01fSNick Piggin { 9029875cf80SDavid Howells struct vfsmount *mnt; 903ea5b778aSDavid Howells int err; 9049875cf80SDavid Howells 9059875cf80SDavid Howells if (!path->dentry->d_op || !path->dentry->d_op->d_automount) 9069875cf80SDavid Howells return -EREMOTE; 9079875cf80SDavid Howells 9086f45b656SDavid Howells /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT 9096f45b656SDavid Howells * and this is the terminal part of the path. 9106f45b656SDavid Howells */ 9116f45b656SDavid Howells if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE)) 9126f45b656SDavid Howells return -EISDIR; /* we actually want to stop here */ 9136f45b656SDavid Howells 9149875cf80SDavid Howells /* We want to mount if someone is trying to open/create a file of any 9159875cf80SDavid Howells * type under the mountpoint, wants to traverse through the mountpoint 9169875cf80SDavid Howells * or wants to open the mounted directory. 9179875cf80SDavid Howells * 9189875cf80SDavid Howells * We don't want to mount if someone's just doing a stat and they've 9199875cf80SDavid Howells * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and 9209875cf80SDavid Howells * appended a '/' to the name. 9219875cf80SDavid Howells */ 9229875cf80SDavid Howells if (!(flags & LOOKUP_FOLLOW) && 9239875cf80SDavid Howells !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY | 9249875cf80SDavid Howells LOOKUP_OPEN | LOOKUP_CREATE))) 9259875cf80SDavid Howells return -EISDIR; 9269875cf80SDavid Howells 9279875cf80SDavid Howells current->total_link_count++; 9289875cf80SDavid Howells if (current->total_link_count >= 40) 9299875cf80SDavid Howells return -ELOOP; 9309875cf80SDavid Howells 9319875cf80SDavid Howells mnt = path->dentry->d_op->d_automount(path); 9329875cf80SDavid Howells if (IS_ERR(mnt)) { 9339875cf80SDavid Howells /* 9349875cf80SDavid Howells * The filesystem is allowed to return -EISDIR here to indicate 9359875cf80SDavid Howells * it doesn't want to automount. For instance, autofs would do 9369875cf80SDavid Howells * this so that its userspace daemon can mount on this dentry. 9379875cf80SDavid Howells * 9389875cf80SDavid Howells * However, we can only permit this if it's a terminal point in 9399875cf80SDavid Howells * the path being looked up; if it wasn't then the remainder of 9409875cf80SDavid Howells * the path is inaccessible and we should say so. 9419875cf80SDavid Howells */ 9429875cf80SDavid Howells if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE)) 9439875cf80SDavid Howells return -EREMOTE; 9449875cf80SDavid Howells return PTR_ERR(mnt); 94531e6b01fSNick Piggin } 946ea5b778aSDavid Howells 9479875cf80SDavid Howells if (!mnt) /* mount collision */ 9489875cf80SDavid Howells return 0; 9499875cf80SDavid Howells 950ea5b778aSDavid Howells /* The new mount record should have at least 2 refs to prevent it being 951ea5b778aSDavid Howells * expired before we get a chance to add it 952ea5b778aSDavid Howells */ 953ea5b778aSDavid Howells BUG_ON(mnt_get_count(mnt) < 2); 954ea5b778aSDavid Howells 9559875cf80SDavid Howells if (mnt->mnt_sb == path->mnt->mnt_sb && 9569875cf80SDavid Howells mnt->mnt_root == path->dentry) { 957ea5b778aSDavid Howells mnt_clear_expiry(mnt); 958ea5b778aSDavid Howells mntput(mnt); 9599875cf80SDavid Howells mntput(mnt); 9609875cf80SDavid Howells return -ELOOP; 96131e6b01fSNick Piggin } 96231e6b01fSNick Piggin 963ea5b778aSDavid Howells /* We need to add the mountpoint to the parent. The filesystem may 964ea5b778aSDavid Howells * have placed it on an expiry list, and so we need to make sure it 965ea5b778aSDavid Howells * won't be expired under us if do_add_mount() fails (do_add_mount() 966ea5b778aSDavid Howells * will eat a reference unconditionally). 967ea5b778aSDavid Howells */ 968ea5b778aSDavid Howells mntget(mnt); 969ea5b778aSDavid Howells err = do_add_mount(mnt, path, path->mnt->mnt_flags | MNT_SHRINKABLE); 970ea5b778aSDavid Howells switch (err) { 971ea5b778aSDavid Howells case -EBUSY: 972ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 973ea5b778aSDavid Howells err = 0; 974ea5b778aSDavid Howells default: 975ea5b778aSDavid Howells mnt_clear_expiry(mnt); 976ea5b778aSDavid Howells mntput(mnt); 977ea5b778aSDavid Howells mntput(mnt); 978ea5b778aSDavid Howells return err; 979ea5b778aSDavid Howells case 0: 980ea5b778aSDavid Howells mntput(mnt); 981463ffb2eSAl Viro dput(path->dentry); 9829875cf80SDavid Howells if (*need_mntput) 9839875cf80SDavid Howells mntput(path->mnt); 9849875cf80SDavid Howells path->mnt = mnt; 9859875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 9869875cf80SDavid Howells *need_mntput = true; 9879875cf80SDavid Howells return 0; 9889875cf80SDavid Howells } 989ea5b778aSDavid Howells } 9909875cf80SDavid Howells 9919875cf80SDavid Howells /* 9929875cf80SDavid Howells * Handle a dentry that is managed in some way. 993cc53ce53SDavid Howells * - Flagged for transit management (autofs) 9949875cf80SDavid Howells * - Flagged as mountpoint 9959875cf80SDavid Howells * - Flagged as automount point 9969875cf80SDavid Howells * 9979875cf80SDavid Howells * This may only be called in refwalk mode. 9989875cf80SDavid Howells * 9999875cf80SDavid Howells * Serialization is taken care of in namespace.c 10009875cf80SDavid Howells */ 10019875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 10029875cf80SDavid Howells { 10039875cf80SDavid Howells unsigned managed; 10049875cf80SDavid Howells bool need_mntput = false; 10059875cf80SDavid Howells int ret; 10069875cf80SDavid Howells 10079875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 10089875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 10099875cf80SDavid Howells * the components of that value change under us */ 10109875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 10119875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 10129875cf80SDavid Howells unlikely(managed != 0)) { 1013cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 1014cc53ce53SDavid Howells * being held. */ 1015cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 1016cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 1017cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 1018ab90911fSDavid Howells ret = path->dentry->d_op->d_manage(path->dentry, 1019ab90911fSDavid Howells false, false); 1020cc53ce53SDavid Howells if (ret < 0) 1021cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 1022cc53ce53SDavid Howells } 1023cc53ce53SDavid Howells 10249875cf80SDavid Howells /* Transit to a mounted filesystem. */ 10259875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 10269875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 10279875cf80SDavid Howells if (mounted) { 10289875cf80SDavid Howells dput(path->dentry); 10299875cf80SDavid Howells if (need_mntput) 1030463ffb2eSAl Viro mntput(path->mnt); 1031463ffb2eSAl Viro path->mnt = mounted; 1032463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 10339875cf80SDavid Howells need_mntput = true; 10349875cf80SDavid Howells continue; 1035463ffb2eSAl Viro } 1036463ffb2eSAl Viro 10379875cf80SDavid Howells /* Something is mounted on this dentry in another 10389875cf80SDavid Howells * namespace and/or whatever was mounted there in this 10399875cf80SDavid Howells * namespace got unmounted before we managed to get the 10409875cf80SDavid Howells * vfsmount_lock */ 10411da177e4SLinus Torvalds } 10429875cf80SDavid Howells 10439875cf80SDavid Howells /* Handle an automount point */ 10449875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 10459875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 10469875cf80SDavid Howells if (ret < 0) 10479875cf80SDavid Howells return ret == -EISDIR ? 0 : ret; 10489875cf80SDavid Howells continue; 10499875cf80SDavid Howells } 10509875cf80SDavid Howells 10519875cf80SDavid Howells /* We didn't change the current path point */ 10529875cf80SDavid Howells break; 10539875cf80SDavid Howells } 10549875cf80SDavid Howells return 0; 10551da177e4SLinus Torvalds } 10561da177e4SLinus Torvalds 1057cc53ce53SDavid Howells int follow_down_one(struct path *path) 10581da177e4SLinus Torvalds { 10591da177e4SLinus Torvalds struct vfsmount *mounted; 10601da177e4SLinus Torvalds 10611c755af4SAl Viro mounted = lookup_mnt(path); 10621da177e4SLinus Torvalds if (mounted) { 10639393bd07SAl Viro dput(path->dentry); 10649393bd07SAl Viro mntput(path->mnt); 10659393bd07SAl Viro path->mnt = mounted; 10669393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 10671da177e4SLinus Torvalds return 1; 10681da177e4SLinus Torvalds } 10691da177e4SLinus Torvalds return 0; 10701da177e4SLinus Torvalds } 10711da177e4SLinus Torvalds 10729875cf80SDavid Howells /* 10739875cf80SDavid Howells * Skip to top of mountpoint pile in rcuwalk mode. We abort the rcu-walk if we 1074cc53ce53SDavid Howells * meet a managed dentry and we're not walking to "..". True is returned to 10759875cf80SDavid Howells * continue, false to abort. 10769875cf80SDavid Howells */ 10779875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 10789875cf80SDavid Howells struct inode **inode, bool reverse_transit) 10799875cf80SDavid Howells { 10809875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 10819875cf80SDavid Howells struct vfsmount *mounted; 1082ab90911fSDavid Howells if (unlikely(path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) && 1083ab90911fSDavid Howells !reverse_transit && 1084ab90911fSDavid Howells path->dentry->d_op->d_manage(path->dentry, false, true) < 0) 1085ab90911fSDavid Howells return false; 10869875cf80SDavid Howells mounted = __lookup_mnt(path->mnt, path->dentry, 1); 10879875cf80SDavid Howells if (!mounted) 10889875cf80SDavid Howells break; 10899875cf80SDavid Howells path->mnt = mounted; 10909875cf80SDavid Howells path->dentry = mounted->mnt_root; 10919875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 10929875cf80SDavid Howells *inode = path->dentry->d_inode; 10939875cf80SDavid Howells } 10949875cf80SDavid Howells 10959875cf80SDavid Howells if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 10969875cf80SDavid Howells return reverse_transit; 10979875cf80SDavid Howells return true; 10989875cf80SDavid Howells } 10999875cf80SDavid Howells 110031e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 110131e6b01fSNick Piggin { 110231e6b01fSNick Piggin struct inode *inode = nd->inode; 110331e6b01fSNick Piggin 110431e6b01fSNick Piggin set_root_rcu(nd); 110531e6b01fSNick Piggin 110631e6b01fSNick Piggin while (1) { 110731e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 110831e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 110931e6b01fSNick Piggin break; 111031e6b01fSNick Piggin } 111131e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 111231e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 111331e6b01fSNick Piggin struct dentry *parent = old->d_parent; 111431e6b01fSNick Piggin unsigned seq; 111531e6b01fSNick Piggin 111631e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 111731e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 111831e6b01fSNick Piggin return -ECHILD; 111931e6b01fSNick Piggin inode = parent->d_inode; 112031e6b01fSNick Piggin nd->path.dentry = parent; 112131e6b01fSNick Piggin nd->seq = seq; 112231e6b01fSNick Piggin break; 112331e6b01fSNick Piggin } 112431e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 112531e6b01fSNick Piggin break; 112631e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 112731e6b01fSNick Piggin inode = nd->path.dentry->d_inode; 112831e6b01fSNick Piggin } 11299875cf80SDavid Howells __follow_mount_rcu(nd, &nd->path, &inode, true); 113031e6b01fSNick Piggin nd->inode = inode; 113131e6b01fSNick Piggin 113231e6b01fSNick Piggin return 0; 113331e6b01fSNick Piggin } 113431e6b01fSNick Piggin 11359875cf80SDavid Howells /* 1136cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 1137cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 1138cc53ce53SDavid Howells * caller is permitted to proceed or not. 1139cc53ce53SDavid Howells * 1140cc53ce53SDavid Howells * Care must be taken as namespace_sem may be held (indicated by mounting_here 1141cc53ce53SDavid Howells * being true). 1142cc53ce53SDavid Howells */ 1143cc53ce53SDavid Howells int follow_down(struct path *path, bool mounting_here) 1144cc53ce53SDavid Howells { 1145cc53ce53SDavid Howells unsigned managed; 1146cc53ce53SDavid Howells int ret; 1147cc53ce53SDavid Howells 1148cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 1149cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 1150cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 1151cc53ce53SDavid Howells * being held. 1152cc53ce53SDavid Howells * 1153cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 1154cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 1155cc53ce53SDavid Howells * other than its daemon the right to mount on its 1156cc53ce53SDavid Howells * superstructure. 1157cc53ce53SDavid Howells * 1158cc53ce53SDavid Howells * The filesystem may sleep at this point. 1159cc53ce53SDavid Howells */ 1160cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 1161cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 1162cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 1163ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 1164ab90911fSDavid Howells path->dentry, mounting_here, false); 1165cc53ce53SDavid Howells if (ret < 0) 1166cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 1167cc53ce53SDavid Howells } 1168cc53ce53SDavid Howells 1169cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 1170cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 1171cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 1172cc53ce53SDavid Howells if (!mounted) 1173cc53ce53SDavid Howells break; 1174cc53ce53SDavid Howells dput(path->dentry); 1175cc53ce53SDavid Howells mntput(path->mnt); 1176cc53ce53SDavid Howells path->mnt = mounted; 1177cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 1178cc53ce53SDavid Howells continue; 1179cc53ce53SDavid Howells } 1180cc53ce53SDavid Howells 1181cc53ce53SDavid Howells /* Don't handle automount points here */ 1182cc53ce53SDavid Howells break; 1183cc53ce53SDavid Howells } 1184cc53ce53SDavid Howells return 0; 1185cc53ce53SDavid Howells } 1186cc53ce53SDavid Howells 1187cc53ce53SDavid Howells /* 11889875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 11899875cf80SDavid Howells */ 11909875cf80SDavid Howells static void follow_mount(struct path *path) 11919875cf80SDavid Howells { 11929875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 11939875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 11949875cf80SDavid Howells if (!mounted) 11959875cf80SDavid Howells break; 11969875cf80SDavid Howells dput(path->dentry); 11979875cf80SDavid Howells mntput(path->mnt); 11989875cf80SDavid Howells path->mnt = mounted; 11999875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 12009875cf80SDavid Howells } 12019875cf80SDavid Howells } 12029875cf80SDavid Howells 120331e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 12041da177e4SLinus Torvalds { 12052a737871SAl Viro set_root(nd); 1206e518ddb7SAndreas Mohr 12071da177e4SLinus Torvalds while(1) { 12084ac91378SJan Blunck struct dentry *old = nd->path.dentry; 12091da177e4SLinus Torvalds 12102a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 12112a737871SAl Viro nd->path.mnt == nd->root.mnt) { 12121da177e4SLinus Torvalds break; 12131da177e4SLinus Torvalds } 12144ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 12153088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 12163088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 12171da177e4SLinus Torvalds dput(old); 12181da177e4SLinus Torvalds break; 12191da177e4SLinus Torvalds } 12203088dd70SAl Viro if (!follow_up(&nd->path)) 12211da177e4SLinus Torvalds break; 12221da177e4SLinus Torvalds } 122379ed0226SAl Viro follow_mount(&nd->path); 122431e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 12251da177e4SLinus Torvalds } 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds /* 1228baa03890SNick Piggin * Allocate a dentry with name and parent, and perform a parent 1229baa03890SNick Piggin * directory ->lookup on it. Returns the new dentry, or ERR_PTR 1230baa03890SNick Piggin * on error. parent->d_inode->i_mutex must be held. d_lookup must 1231baa03890SNick Piggin * have verified that no child exists while under i_mutex. 1232baa03890SNick Piggin */ 1233baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent, 1234baa03890SNick Piggin struct qstr *name, struct nameidata *nd) 1235baa03890SNick Piggin { 1236baa03890SNick Piggin struct inode *inode = parent->d_inode; 1237baa03890SNick Piggin struct dentry *dentry; 1238baa03890SNick Piggin struct dentry *old; 1239baa03890SNick Piggin 1240baa03890SNick Piggin /* Don't create child dentry for a dead directory. */ 1241baa03890SNick Piggin if (unlikely(IS_DEADDIR(inode))) 1242baa03890SNick Piggin return ERR_PTR(-ENOENT); 1243baa03890SNick Piggin 1244baa03890SNick Piggin dentry = d_alloc(parent, name); 1245baa03890SNick Piggin if (unlikely(!dentry)) 1246baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1247baa03890SNick Piggin 1248baa03890SNick Piggin old = inode->i_op->lookup(inode, dentry, nd); 1249baa03890SNick Piggin if (unlikely(old)) { 1250baa03890SNick Piggin dput(dentry); 1251baa03890SNick Piggin dentry = old; 1252baa03890SNick Piggin } 1253baa03890SNick Piggin return dentry; 1254baa03890SNick Piggin } 1255baa03890SNick Piggin 1256baa03890SNick Piggin /* 12571da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 12581da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 12591da177e4SLinus Torvalds * It _is_ time-critical. 12601da177e4SLinus Torvalds */ 12611da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name, 126231e6b01fSNick Piggin struct path *path, struct inode **inode) 12631da177e4SLinus Torvalds { 12644ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 126531e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 12666e6b1bd1SAl Viro struct inode *dir; 12679875cf80SDavid Howells int err; 12689875cf80SDavid Howells 12693cac260aSAl Viro /* 12703cac260aSAl Viro * See if the low-level filesystem might want 12713cac260aSAl Viro * to use its own hash.. 12723cac260aSAl Viro */ 1273fb045adbSNick Piggin if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 12749875cf80SDavid Howells err = parent->d_op->d_hash(parent, nd->inode, name); 12753cac260aSAl Viro if (err < 0) 12763cac260aSAl Viro return err; 12773cac260aSAl Viro } 12781da177e4SLinus Torvalds 1279b04f784eSNick Piggin /* 1280b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1281b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1282b04f784eSNick Piggin * do the non-racy lookup, below. 1283b04f784eSNick Piggin */ 128431e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 128531e6b01fSNick Piggin unsigned seq; 128631e6b01fSNick Piggin 128731e6b01fSNick Piggin *inode = nd->inode; 128831e6b01fSNick Piggin dentry = __d_lookup_rcu(parent, name, &seq, inode); 128931e6b01fSNick Piggin if (!dentry) { 129031e6b01fSNick Piggin if (nameidata_drop_rcu(nd)) 129131e6b01fSNick Piggin return -ECHILD; 129231e6b01fSNick Piggin goto need_lookup; 129331e6b01fSNick Piggin } 129431e6b01fSNick Piggin /* Memory barrier in read_seqcount_begin of child is enough */ 129531e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 129631e6b01fSNick Piggin return -ECHILD; 129731e6b01fSNick Piggin 129831e6b01fSNick Piggin nd->seq = seq; 129934286d66SNick Piggin if (dentry->d_flags & DCACHE_OP_REVALIDATE) 130031e6b01fSNick Piggin goto need_revalidate; 13011a8edf40SAl Viro done2: 130231e6b01fSNick Piggin path->mnt = mnt; 130331e6b01fSNick Piggin path->dentry = dentry; 13049875cf80SDavid Howells if (likely(__follow_mount_rcu(nd, path, inode, false))) 13059875cf80SDavid Howells return 0; 13069875cf80SDavid Howells if (nameidata_drop_rcu(nd)) 13079875cf80SDavid Howells return -ECHILD; 13089875cf80SDavid Howells /* fallthru */ 13099875cf80SDavid Howells } 131031e6b01fSNick Piggin dentry = __d_lookup(parent, name); 13111da177e4SLinus Torvalds if (!dentry) 13121da177e4SLinus Torvalds goto need_lookup; 13132e2e88eaSNick Piggin found: 1314fb045adbSNick Piggin if (dentry->d_flags & DCACHE_OP_REVALIDATE) 13151da177e4SLinus Torvalds goto need_revalidate; 13161da177e4SLinus Torvalds done: 13171da177e4SLinus Torvalds path->mnt = mnt; 13181da177e4SLinus Torvalds path->dentry = dentry; 13199875cf80SDavid Howells err = follow_managed(path, nd->flags); 13209875cf80SDavid Howells if (unlikely(err < 0)) 13219875cf80SDavid Howells return err; 132231e6b01fSNick Piggin *inode = path->dentry->d_inode; 13231da177e4SLinus Torvalds return 0; 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds need_lookup: 13266e6b1bd1SAl Viro dir = parent->d_inode; 132731e6b01fSNick Piggin BUG_ON(nd->inode != dir); 13286e6b1bd1SAl Viro 13296e6b1bd1SAl Viro mutex_lock(&dir->i_mutex); 13306e6b1bd1SAl Viro /* 13316e6b1bd1SAl Viro * First re-do the cached lookup just in case it was created 1332b04f784eSNick Piggin * while we waited for the directory semaphore, or the first 1333b04f784eSNick Piggin * lookup failed due to an unrelated rename. 13346e6b1bd1SAl Viro * 1335b04f784eSNick Piggin * This could use version numbering or similar to avoid unnecessary 1336b04f784eSNick Piggin * cache lookups, but then we'd have to do the first lookup in the 1337b04f784eSNick Piggin * non-racy way. However in the common case here, everything should 1338b04f784eSNick Piggin * be hot in cache, so would it be a big win? 13396e6b1bd1SAl Viro */ 13406e6b1bd1SAl Viro dentry = d_lookup(parent, name); 1341baa03890SNick Piggin if (likely(!dentry)) { 1342baa03890SNick Piggin dentry = d_alloc_and_lookup(parent, name, nd); 13436e6b1bd1SAl Viro mutex_unlock(&dir->i_mutex); 13446e6b1bd1SAl Viro if (IS_ERR(dentry)) 13456e6b1bd1SAl Viro goto fail; 13466e6b1bd1SAl Viro goto done; 13476e6b1bd1SAl Viro } 13486e6b1bd1SAl Viro /* 13496e6b1bd1SAl Viro * Uhhuh! Nasty case: the cache was re-populated while 13506e6b1bd1SAl Viro * we waited on the semaphore. Need to revalidate. 13516e6b1bd1SAl Viro */ 13526e6b1bd1SAl Viro mutex_unlock(&dir->i_mutex); 13532e2e88eaSNick Piggin goto found; 13541da177e4SLinus Torvalds 13551da177e4SLinus Torvalds need_revalidate: 1356bcdc5e01SIan Kent dentry = do_revalidate(dentry, nd); 1357bcdc5e01SIan Kent if (!dentry) 13581da177e4SLinus Torvalds goto need_lookup; 1359bcdc5e01SIan Kent if (IS_ERR(dentry)) 1360bcdc5e01SIan Kent goto fail; 13611a8edf40SAl Viro if (nd->flags & LOOKUP_RCU) 13621a8edf40SAl Viro goto done2; 1363bcdc5e01SIan Kent goto done; 13641da177e4SLinus Torvalds 13651da177e4SLinus Torvalds fail: 13661da177e4SLinus Torvalds return PTR_ERR(dentry); 13671da177e4SLinus Torvalds } 13681da177e4SLinus Torvalds 13691da177e4SLinus Torvalds /* 13701da177e4SLinus Torvalds * Name resolution. 1371ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1372ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 13731da177e4SLinus Torvalds * 1374ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1375ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 13761da177e4SLinus Torvalds */ 13776de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 13781da177e4SLinus Torvalds { 13791da177e4SLinus Torvalds struct path next; 13801da177e4SLinus Torvalds int err; 13811da177e4SLinus Torvalds unsigned int lookup_flags = nd->flags; 13821da177e4SLinus Torvalds 13831da177e4SLinus Torvalds while (*name=='/') 13841da177e4SLinus Torvalds name++; 13851da177e4SLinus Torvalds if (!*name) 13861da177e4SLinus Torvalds goto return_reval; 13871da177e4SLinus Torvalds 13881da177e4SLinus Torvalds if (nd->depth) 1389f55eab82STrond Myklebust lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE); 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 13921da177e4SLinus Torvalds for(;;) { 139331e6b01fSNick Piggin struct inode *inode; 13941da177e4SLinus Torvalds unsigned long hash; 13951da177e4SLinus Torvalds struct qstr this; 13961da177e4SLinus Torvalds unsigned int c; 13971da177e4SLinus Torvalds 1398cdce5d6bSTrond Myklebust nd->flags |= LOOKUP_CONTINUE; 139931e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 1400b74c79e9SNick Piggin err = exec_permission(nd->inode, IPERM_FLAG_RCU); 140131e6b01fSNick Piggin if (err == -ECHILD) { 140231e6b01fSNick Piggin if (nameidata_drop_rcu(nd)) 140331e6b01fSNick Piggin return -ECHILD; 140431e6b01fSNick Piggin goto exec_again; 140531e6b01fSNick Piggin } 140631e6b01fSNick Piggin } else { 140731e6b01fSNick Piggin exec_again: 1408b74c79e9SNick Piggin err = exec_permission(nd->inode, 0); 140931e6b01fSNick Piggin } 14101da177e4SLinus Torvalds if (err) 14111da177e4SLinus Torvalds break; 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds this.name = name; 14141da177e4SLinus Torvalds c = *(const unsigned char *)name; 14151da177e4SLinus Torvalds 14161da177e4SLinus Torvalds hash = init_name_hash(); 14171da177e4SLinus Torvalds do { 14181da177e4SLinus Torvalds name++; 14191da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 14201da177e4SLinus Torvalds c = *(const unsigned char *)name; 14211da177e4SLinus Torvalds } while (c && (c != '/')); 14221da177e4SLinus Torvalds this.len = name - (const char *) this.name; 14231da177e4SLinus Torvalds this.hash = end_name_hash(hash); 14241da177e4SLinus Torvalds 14251da177e4SLinus Torvalds /* remove trailing slashes? */ 14261da177e4SLinus Torvalds if (!c) 14271da177e4SLinus Torvalds goto last_component; 14281da177e4SLinus Torvalds while (*++name == '/'); 14291da177e4SLinus Torvalds if (!*name) 14301da177e4SLinus Torvalds goto last_with_slashes; 14311da177e4SLinus Torvalds 14321da177e4SLinus Torvalds /* 14331da177e4SLinus Torvalds * "." and ".." are special - ".." especially so because it has 14341da177e4SLinus Torvalds * to be able to know about the current root directory and 14351da177e4SLinus Torvalds * parent relationships. 14361da177e4SLinus Torvalds */ 14371da177e4SLinus Torvalds if (this.name[0] == '.') switch (this.len) { 14381da177e4SLinus Torvalds default: 14391da177e4SLinus Torvalds break; 14401da177e4SLinus Torvalds case 2: 14411da177e4SLinus Torvalds if (this.name[1] != '.') 14421da177e4SLinus Torvalds break; 144331e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 144431e6b01fSNick Piggin if (follow_dotdot_rcu(nd)) 144531e6b01fSNick Piggin return -ECHILD; 144631e6b01fSNick Piggin } else 144758c465ebSAl Viro follow_dotdot(nd); 14481da177e4SLinus Torvalds /* fallthrough */ 14491da177e4SLinus Torvalds case 1: 14501da177e4SLinus Torvalds continue; 14511da177e4SLinus Torvalds } 14521da177e4SLinus Torvalds /* This does the actual lookups.. */ 145331e6b01fSNick Piggin err = do_lookup(nd, &this, &next, &inode); 14541da177e4SLinus Torvalds if (err) 14551da177e4SLinus Torvalds break; 14561da177e4SLinus Torvalds err = -ENOENT; 14571da177e4SLinus Torvalds if (!inode) 14581da177e4SLinus Torvalds goto out_dput; 14591da177e4SLinus Torvalds 14601da177e4SLinus Torvalds if (inode->i_op->follow_link) { 146131e6b01fSNick Piggin /* We commonly drop rcu-walk here */ 146231e6b01fSNick Piggin if (nameidata_dentry_drop_rcu_maybe(nd, next.dentry)) 146331e6b01fSNick Piggin return -ECHILD; 146431e6b01fSNick Piggin BUG_ON(inode != next.dentry->d_inode); 146590ebe565SAl Viro err = do_follow_link(&next, nd); 14661da177e4SLinus Torvalds if (err) 14671da177e4SLinus Torvalds goto return_err; 146831e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 14691da177e4SLinus Torvalds err = -ENOENT; 147031e6b01fSNick Piggin if (!nd->inode) 14711da177e4SLinus Torvalds break; 147231e6b01fSNick Piggin } else { 147309dd17d3SMiklos Szeredi path_to_nameidata(&next, nd); 147431e6b01fSNick Piggin nd->inode = inode; 147531e6b01fSNick Piggin } 14761da177e4SLinus Torvalds err = -ENOTDIR; 147731e6b01fSNick Piggin if (!nd->inode->i_op->lookup) 14781da177e4SLinus Torvalds break; 14791da177e4SLinus Torvalds continue; 14801da177e4SLinus Torvalds /* here ends the main loop */ 14811da177e4SLinus Torvalds 14821da177e4SLinus Torvalds last_with_slashes: 14831da177e4SLinus Torvalds lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 14841da177e4SLinus Torvalds last_component: 1485f55eab82STrond Myklebust /* Clear LOOKUP_CONTINUE iff it was previously unset */ 1486f55eab82STrond Myklebust nd->flags &= lookup_flags | ~LOOKUP_CONTINUE; 14871da177e4SLinus Torvalds if (lookup_flags & LOOKUP_PARENT) 14881da177e4SLinus Torvalds goto lookup_parent; 14891da177e4SLinus Torvalds if (this.name[0] == '.') switch (this.len) { 14901da177e4SLinus Torvalds default: 14911da177e4SLinus Torvalds break; 14921da177e4SLinus Torvalds case 2: 14931da177e4SLinus Torvalds if (this.name[1] != '.') 14941da177e4SLinus Torvalds break; 149531e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 149631e6b01fSNick Piggin if (follow_dotdot_rcu(nd)) 149731e6b01fSNick Piggin return -ECHILD; 149831e6b01fSNick Piggin } else 149958c465ebSAl Viro follow_dotdot(nd); 15001da177e4SLinus Torvalds /* fallthrough */ 15011da177e4SLinus Torvalds case 1: 15021da177e4SLinus Torvalds goto return_reval; 15031da177e4SLinus Torvalds } 150431e6b01fSNick Piggin err = do_lookup(nd, &this, &next, &inode); 15051da177e4SLinus Torvalds if (err) 15061da177e4SLinus Torvalds break; 1507db372915SDavid Howells if (inode && unlikely(inode->i_op->follow_link) && 1508db372915SDavid Howells (lookup_flags & LOOKUP_FOLLOW)) { 150931e6b01fSNick Piggin if (nameidata_dentry_drop_rcu_maybe(nd, next.dentry)) 151031e6b01fSNick Piggin return -ECHILD; 151131e6b01fSNick Piggin BUG_ON(inode != next.dentry->d_inode); 151290ebe565SAl Viro err = do_follow_link(&next, nd); 15131da177e4SLinus Torvalds if (err) 15141da177e4SLinus Torvalds goto return_err; 151531e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 151631e6b01fSNick Piggin } else { 151709dd17d3SMiklos Szeredi path_to_nameidata(&next, nd); 151831e6b01fSNick Piggin nd->inode = inode; 151931e6b01fSNick Piggin } 15201da177e4SLinus Torvalds err = -ENOENT; 152131e6b01fSNick Piggin if (!nd->inode) 15221da177e4SLinus Torvalds break; 15231da177e4SLinus Torvalds if (lookup_flags & LOOKUP_DIRECTORY) { 15241da177e4SLinus Torvalds err = -ENOTDIR; 152531e6b01fSNick Piggin if (!nd->inode->i_op->lookup) 15261da177e4SLinus Torvalds break; 15271da177e4SLinus Torvalds } 15281da177e4SLinus Torvalds goto return_base; 15291da177e4SLinus Torvalds lookup_parent: 15301da177e4SLinus Torvalds nd->last = this; 15311da177e4SLinus Torvalds nd->last_type = LAST_NORM; 15321da177e4SLinus Torvalds if (this.name[0] != '.') 15331da177e4SLinus Torvalds goto return_base; 15341da177e4SLinus Torvalds if (this.len == 1) 15351da177e4SLinus Torvalds nd->last_type = LAST_DOT; 15361da177e4SLinus Torvalds else if (this.len == 2 && this.name[1] == '.') 15371da177e4SLinus Torvalds nd->last_type = LAST_DOTDOT; 15381da177e4SLinus Torvalds else 15391da177e4SLinus Torvalds goto return_base; 15401da177e4SLinus Torvalds return_reval: 15411da177e4SLinus Torvalds /* 15421da177e4SLinus Torvalds * We bypassed the ordinary revalidation routines. 15431da177e4SLinus Torvalds * We may need to check the cached dentry for staleness. 15441da177e4SLinus Torvalds */ 1545fb045adbSNick Piggin if (need_reval_dot(nd->path.dentry)) { 15461da177e4SLinus Torvalds /* Note: we do not d_invalidate() */ 154734286d66SNick Piggin err = d_revalidate(nd->path.dentry, nd); 154834286d66SNick Piggin if (!err) 154934286d66SNick Piggin err = -ESTALE; 155034286d66SNick Piggin if (err < 0) 15511da177e4SLinus Torvalds break; 15521da177e4SLinus Torvalds } 15531da177e4SLinus Torvalds return_base: 155431e6b01fSNick Piggin if (nameidata_drop_rcu_last_maybe(nd)) 155531e6b01fSNick Piggin return -ECHILD; 15561da177e4SLinus Torvalds return 0; 15571da177e4SLinus Torvalds out_dput: 155831e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) 15591d957f9bSJan Blunck path_put_conditional(&next, nd); 15601da177e4SLinus Torvalds break; 15611da177e4SLinus Torvalds } 156231e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) 15631d957f9bSJan Blunck path_put(&nd->path); 15641da177e4SLinus Torvalds return_err: 15651da177e4SLinus Torvalds return err; 15661da177e4SLinus Torvalds } 15671da177e4SLinus Torvalds 156831e6b01fSNick Piggin static inline int path_walk_rcu(const char *name, struct nameidata *nd) 156931e6b01fSNick Piggin { 157031e6b01fSNick Piggin current->total_link_count = 0; 157131e6b01fSNick Piggin 157231e6b01fSNick Piggin return link_path_walk(name, nd); 157331e6b01fSNick Piggin } 157431e6b01fSNick Piggin 157531e6b01fSNick Piggin static inline int path_walk_simple(const char *name, struct nameidata *nd) 157631e6b01fSNick Piggin { 157731e6b01fSNick Piggin current->total_link_count = 0; 157831e6b01fSNick Piggin 157931e6b01fSNick Piggin return link_path_walk(name, nd); 158031e6b01fSNick Piggin } 158131e6b01fSNick Piggin 1582fc9b52cdSHarvey Harrison static int path_walk(const char *name, struct nameidata *nd) 15831da177e4SLinus Torvalds { 15846de88d72SAl Viro struct path save = nd->path; 15856de88d72SAl Viro int result; 15866de88d72SAl Viro 15871da177e4SLinus Torvalds current->total_link_count = 0; 15886de88d72SAl Viro 15896de88d72SAl Viro /* make sure the stuff we saved doesn't go away */ 15906de88d72SAl Viro path_get(&save); 15916de88d72SAl Viro 15926de88d72SAl Viro result = link_path_walk(name, nd); 15936de88d72SAl Viro if (result == -ESTALE) { 15946de88d72SAl Viro /* nd->path had been dropped */ 15956de88d72SAl Viro current->total_link_count = 0; 15966de88d72SAl Viro nd->path = save; 15976de88d72SAl Viro path_get(&nd->path); 15986de88d72SAl Viro nd->flags |= LOOKUP_REVAL; 15996de88d72SAl Viro result = link_path_walk(name, nd); 16006de88d72SAl Viro } 16016de88d72SAl Viro 16026de88d72SAl Viro path_put(&save); 16036de88d72SAl Viro 16046de88d72SAl Viro return result; 16051da177e4SLinus Torvalds } 16061da177e4SLinus Torvalds 160731e6b01fSNick Piggin static void path_finish_rcu(struct nameidata *nd) 160831e6b01fSNick Piggin { 160931e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 161031e6b01fSNick Piggin /* RCU dangling. Cancel it. */ 161131e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 161231e6b01fSNick Piggin nd->root.mnt = NULL; 161331e6b01fSNick Piggin rcu_read_unlock(); 161431e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 161531e6b01fSNick Piggin } 161631e6b01fSNick Piggin if (nd->file) 161731e6b01fSNick Piggin fput(nd->file); 161831e6b01fSNick Piggin } 161931e6b01fSNick Piggin 162031e6b01fSNick Piggin static int path_init_rcu(int dfd, const char *name, unsigned int flags, struct nameidata *nd) 162131e6b01fSNick Piggin { 162231e6b01fSNick Piggin int retval = 0; 162331e6b01fSNick Piggin int fput_needed; 162431e6b01fSNick Piggin struct file *file; 162531e6b01fSNick Piggin 162631e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 162731e6b01fSNick Piggin nd->flags = flags | LOOKUP_RCU; 162831e6b01fSNick Piggin nd->depth = 0; 162931e6b01fSNick Piggin nd->root.mnt = NULL; 163031e6b01fSNick Piggin nd->file = NULL; 163131e6b01fSNick Piggin 163231e6b01fSNick Piggin if (*name=='/') { 163331e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1634c28cc364SNick Piggin unsigned seq; 163531e6b01fSNick Piggin 163631e6b01fSNick Piggin br_read_lock(vfsmount_lock); 163731e6b01fSNick Piggin rcu_read_lock(); 163831e6b01fSNick Piggin 1639c28cc364SNick Piggin do { 1640c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 164131e6b01fSNick Piggin nd->root = fs->root; 164231e6b01fSNick Piggin nd->path = nd->root; 1643c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1644c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 164531e6b01fSNick Piggin 164631e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 164731e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1648c28cc364SNick Piggin unsigned seq; 164931e6b01fSNick Piggin 165031e6b01fSNick Piggin br_read_lock(vfsmount_lock); 165131e6b01fSNick Piggin rcu_read_lock(); 165231e6b01fSNick Piggin 1653c28cc364SNick Piggin do { 1654c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 165531e6b01fSNick Piggin nd->path = fs->pwd; 1656c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1657c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1658c28cc364SNick Piggin 165931e6b01fSNick Piggin } else { 166031e6b01fSNick Piggin struct dentry *dentry; 166131e6b01fSNick Piggin 166231e6b01fSNick Piggin file = fget_light(dfd, &fput_needed); 166331e6b01fSNick Piggin retval = -EBADF; 166431e6b01fSNick Piggin if (!file) 166531e6b01fSNick Piggin goto out_fail; 166631e6b01fSNick Piggin 166731e6b01fSNick Piggin dentry = file->f_path.dentry; 166831e6b01fSNick Piggin 166931e6b01fSNick Piggin retval = -ENOTDIR; 167031e6b01fSNick Piggin if (!S_ISDIR(dentry->d_inode->i_mode)) 167131e6b01fSNick Piggin goto fput_fail; 167231e6b01fSNick Piggin 167331e6b01fSNick Piggin retval = file_permission(file, MAY_EXEC); 167431e6b01fSNick Piggin if (retval) 167531e6b01fSNick Piggin goto fput_fail; 167631e6b01fSNick Piggin 167731e6b01fSNick Piggin nd->path = file->f_path; 167831e6b01fSNick Piggin if (fput_needed) 167931e6b01fSNick Piggin nd->file = file; 168031e6b01fSNick Piggin 1681c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 168231e6b01fSNick Piggin br_read_lock(vfsmount_lock); 168331e6b01fSNick Piggin rcu_read_lock(); 168431e6b01fSNick Piggin } 168531e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 168631e6b01fSNick Piggin return 0; 168731e6b01fSNick Piggin 168831e6b01fSNick Piggin fput_fail: 168931e6b01fSNick Piggin fput_light(file, fput_needed); 169031e6b01fSNick Piggin out_fail: 169131e6b01fSNick Piggin return retval; 169231e6b01fSNick Piggin } 169331e6b01fSNick Piggin 16949b4a9b14SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd) 16951da177e4SLinus Torvalds { 1696ea3834d9SPrasanna Meda int retval = 0; 1697170aa3d0SUlrich Drepper int fput_needed; 1698170aa3d0SUlrich Drepper struct file *file; 16991da177e4SLinus Torvalds 17001da177e4SLinus Torvalds nd->last_type = LAST_ROOT; /* if there are only slashes... */ 17011da177e4SLinus Torvalds nd->flags = flags; 17021da177e4SLinus Torvalds nd->depth = 0; 17032a737871SAl Viro nd->root.mnt = NULL; 17041da177e4SLinus Torvalds 17051da177e4SLinus Torvalds if (*name=='/') { 17062a737871SAl Viro set_root(nd); 17072a737871SAl Viro nd->path = nd->root; 17082a737871SAl Viro path_get(&nd->root); 17095590ff0dSUlrich Drepper } else if (dfd == AT_FDCWD) { 1710f7ad3c6bSMiklos Szeredi get_fs_pwd(current->fs, &nd->path); 17115590ff0dSUlrich Drepper } else { 17125590ff0dSUlrich Drepper struct dentry *dentry; 17135590ff0dSUlrich Drepper 17145590ff0dSUlrich Drepper file = fget_light(dfd, &fput_needed); 17155590ff0dSUlrich Drepper retval = -EBADF; 1716170aa3d0SUlrich Drepper if (!file) 17176d09bb62STrond Myklebust goto out_fail; 17185590ff0dSUlrich Drepper 17190f7fc9e4SJosef "Jeff" Sipek dentry = file->f_path.dentry; 17205590ff0dSUlrich Drepper 17215590ff0dSUlrich Drepper retval = -ENOTDIR; 1722170aa3d0SUlrich Drepper if (!S_ISDIR(dentry->d_inode->i_mode)) 17236d09bb62STrond Myklebust goto fput_fail; 17245590ff0dSUlrich Drepper 17255590ff0dSUlrich Drepper retval = file_permission(file, MAY_EXEC); 1726170aa3d0SUlrich Drepper if (retval) 17276d09bb62STrond Myklebust goto fput_fail; 17285590ff0dSUlrich Drepper 17295dd784d0SJan Blunck nd->path = file->f_path; 17305dd784d0SJan Blunck path_get(&file->f_path); 17315590ff0dSUlrich Drepper 17325590ff0dSUlrich Drepper fput_light(file, fput_needed); 17331da177e4SLinus Torvalds } 173431e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 17359b4a9b14SAl Viro return 0; 17362dfdd266SJosef 'Jeff' Sipek 17379b4a9b14SAl Viro fput_fail: 17389b4a9b14SAl Viro fput_light(file, fput_needed); 17399b4a9b14SAl Viro out_fail: 17409b4a9b14SAl Viro return retval; 17419b4a9b14SAl Viro } 17429b4a9b14SAl Viro 17439b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 17449b4a9b14SAl Viro static int do_path_lookup(int dfd, const char *name, 17459b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 17469b4a9b14SAl Viro { 174731e6b01fSNick Piggin int retval; 174831e6b01fSNick Piggin 174931e6b01fSNick Piggin /* 175031e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 175131e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 175231e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 175331e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 175431e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 175531e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 175631e6b01fSNick Piggin * analogue, foo_rcu(). 175731e6b01fSNick Piggin * 175831e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 175931e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 176031e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 176131e6b01fSNick Piggin * be able to complete). 176231e6b01fSNick Piggin */ 176331e6b01fSNick Piggin retval = path_init_rcu(dfd, name, flags, nd); 176431e6b01fSNick Piggin if (unlikely(retval)) 176531e6b01fSNick Piggin return retval; 176631e6b01fSNick Piggin retval = path_walk_rcu(name, nd); 176731e6b01fSNick Piggin path_finish_rcu(nd); 17682a737871SAl Viro if (nd->root.mnt) { 17692a737871SAl Viro path_put(&nd->root); 17702a737871SAl Viro nd->root.mnt = NULL; 17712a737871SAl Viro } 177231e6b01fSNick Piggin 177331e6b01fSNick Piggin if (unlikely(retval == -ECHILD || retval == -ESTALE)) { 177431e6b01fSNick Piggin /* slower, locked walk */ 177531e6b01fSNick Piggin if (retval == -ESTALE) 177631e6b01fSNick Piggin flags |= LOOKUP_REVAL; 177731e6b01fSNick Piggin retval = path_init(dfd, name, flags, nd); 177831e6b01fSNick Piggin if (unlikely(retval)) 177931e6b01fSNick Piggin return retval; 178031e6b01fSNick Piggin retval = path_walk(name, nd); 178131e6b01fSNick Piggin if (nd->root.mnt) { 178231e6b01fSNick Piggin path_put(&nd->root); 178331e6b01fSNick Piggin nd->root.mnt = NULL; 178431e6b01fSNick Piggin } 178531e6b01fSNick Piggin } 178631e6b01fSNick Piggin 178731e6b01fSNick Piggin if (likely(!retval)) { 178831e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) { 178931e6b01fSNick Piggin if (nd->path.dentry && nd->inode) 179031e6b01fSNick Piggin audit_inode(name, nd->path.dentry); 179131e6b01fSNick Piggin } 179231e6b01fSNick Piggin } 179331e6b01fSNick Piggin 1794170aa3d0SUlrich Drepper return retval; 17951da177e4SLinus Torvalds } 17961da177e4SLinus Torvalds 1797fc9b52cdSHarvey Harrison int path_lookup(const char *name, unsigned int flags, 17985590ff0dSUlrich Drepper struct nameidata *nd) 17995590ff0dSUlrich Drepper { 18005590ff0dSUlrich Drepper return do_path_lookup(AT_FDCWD, name, flags, nd); 18015590ff0dSUlrich Drepper } 18025590ff0dSUlrich Drepper 1803d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1804d1811465SAl Viro { 1805d1811465SAl Viro struct nameidata nd; 1806d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1807d1811465SAl Viro if (!res) 1808d1811465SAl Viro *path = nd.path; 1809d1811465SAl Viro return res; 1810d1811465SAl Viro } 1811d1811465SAl Viro 181216f18200SJosef 'Jeff' Sipek /** 181316f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 181416f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 181516f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 181616f18200SJosef 'Jeff' Sipek * @name: pointer to file name 181716f18200SJosef 'Jeff' Sipek * @flags: lookup flags 181816f18200SJosef 'Jeff' Sipek * @nd: pointer to nameidata 181916f18200SJosef 'Jeff' Sipek */ 182016f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 182116f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 182216f18200SJosef 'Jeff' Sipek struct nameidata *nd) 182316f18200SJosef 'Jeff' Sipek { 182416f18200SJosef 'Jeff' Sipek int retval; 182516f18200SJosef 'Jeff' Sipek 182616f18200SJosef 'Jeff' Sipek /* same as do_path_lookup */ 182716f18200SJosef 'Jeff' Sipek nd->last_type = LAST_ROOT; 182816f18200SJosef 'Jeff' Sipek nd->flags = flags; 182916f18200SJosef 'Jeff' Sipek nd->depth = 0; 183016f18200SJosef 'Jeff' Sipek 1831c8e7f449SJan Blunck nd->path.dentry = dentry; 1832c8e7f449SJan Blunck nd->path.mnt = mnt; 1833c8e7f449SJan Blunck path_get(&nd->path); 18345b857119SAl Viro nd->root = nd->path; 18355b857119SAl Viro path_get(&nd->root); 183631e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 183716f18200SJosef 'Jeff' Sipek 183816f18200SJosef 'Jeff' Sipek retval = path_walk(name, nd); 18394ac91378SJan Blunck if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry && 184031e6b01fSNick Piggin nd->inode)) 18414ac91378SJan Blunck audit_inode(name, nd->path.dentry); 184216f18200SJosef 'Jeff' Sipek 18432a737871SAl Viro path_put(&nd->root); 18442a737871SAl Viro nd->root.mnt = NULL; 184516f18200SJosef 'Jeff' Sipek 18462a737871SAl Viro return retval; 184716f18200SJosef 'Jeff' Sipek } 184816f18200SJosef 'Jeff' Sipek 1849eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name, 1850eead1911SChristoph Hellwig struct dentry *base, struct nameidata *nd) 18511da177e4SLinus Torvalds { 185281fca444SChristoph Hellwig struct inode *inode = base->d_inode; 18531da177e4SLinus Torvalds struct dentry *dentry; 18541da177e4SLinus Torvalds int err; 18551da177e4SLinus Torvalds 1856b74c79e9SNick Piggin err = exec_permission(inode, 0); 185781fca444SChristoph Hellwig if (err) 185881fca444SChristoph Hellwig return ERR_PTR(err); 18591da177e4SLinus Torvalds 18601da177e4SLinus Torvalds /* 18611da177e4SLinus Torvalds * See if the low-level filesystem might want 18621da177e4SLinus Torvalds * to use its own hash.. 18631da177e4SLinus Torvalds */ 1864fb045adbSNick Piggin if (base->d_flags & DCACHE_OP_HASH) { 1865b1e6a015SNick Piggin err = base->d_op->d_hash(base, inode, name); 18661da177e4SLinus Torvalds dentry = ERR_PTR(err); 18671da177e4SLinus Torvalds if (err < 0) 18681da177e4SLinus Torvalds goto out; 18691da177e4SLinus Torvalds } 18701da177e4SLinus Torvalds 1871b04f784eSNick Piggin /* 1872b04f784eSNick Piggin * Don't bother with __d_lookup: callers are for creat as 1873b04f784eSNick Piggin * well as unlink, so a lot of the time it would cost 1874b04f784eSNick Piggin * a double lookup. 18756e6b1bd1SAl Viro */ 18766e6b1bd1SAl Viro dentry = d_lookup(base, name); 18776e6b1bd1SAl Viro 1878fb045adbSNick Piggin if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) 18796e6b1bd1SAl Viro dentry = do_revalidate(dentry, nd); 18806e6b1bd1SAl Viro 18811da177e4SLinus Torvalds if (!dentry) 1882baa03890SNick Piggin dentry = d_alloc_and_lookup(base, name, nd); 18831da177e4SLinus Torvalds out: 18841da177e4SLinus Torvalds return dentry; 18851da177e4SLinus Torvalds } 18861da177e4SLinus Torvalds 1887057f6c01SJames Morris /* 1888057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1889057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1890057f6c01SJames Morris * SMP-safe. 1891057f6c01SJames Morris */ 1892a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 18931da177e4SLinus Torvalds { 18944ac91378SJan Blunck return __lookup_hash(&nd->last, nd->path.dentry, nd); 18951da177e4SLinus Torvalds } 18961da177e4SLinus Torvalds 1897eead1911SChristoph Hellwig static int __lookup_one_len(const char *name, struct qstr *this, 1898eead1911SChristoph Hellwig struct dentry *base, int len) 18991da177e4SLinus Torvalds { 19001da177e4SLinus Torvalds unsigned long hash; 19011da177e4SLinus Torvalds unsigned int c; 19021da177e4SLinus Torvalds 1903057f6c01SJames Morris this->name = name; 1904057f6c01SJames Morris this->len = len; 19051da177e4SLinus Torvalds if (!len) 1906057f6c01SJames Morris return -EACCES; 19071da177e4SLinus Torvalds 19081da177e4SLinus Torvalds hash = init_name_hash(); 19091da177e4SLinus Torvalds while (len--) { 19101da177e4SLinus Torvalds c = *(const unsigned char *)name++; 19111da177e4SLinus Torvalds if (c == '/' || c == '\0') 1912057f6c01SJames Morris return -EACCES; 19131da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 19141da177e4SLinus Torvalds } 1915057f6c01SJames Morris this->hash = end_name_hash(hash); 1916057f6c01SJames Morris return 0; 1917057f6c01SJames Morris } 19181da177e4SLinus Torvalds 1919eead1911SChristoph Hellwig /** 1920a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1921eead1911SChristoph Hellwig * @name: pathname component to lookup 1922eead1911SChristoph Hellwig * @base: base directory to lookup from 1923eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1924eead1911SChristoph Hellwig * 1925a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1926a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1927eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1928eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1929eead1911SChristoph Hellwig */ 1930057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1931057f6c01SJames Morris { 1932057f6c01SJames Morris int err; 1933057f6c01SJames Morris struct qstr this; 1934057f6c01SJames Morris 19352f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 19362f9092e1SDavid Woodhouse 1937057f6c01SJames Morris err = __lookup_one_len(name, &this, base, len); 1938057f6c01SJames Morris if (err) 1939057f6c01SJames Morris return ERR_PTR(err); 1940eead1911SChristoph Hellwig 194149705b77SChristoph Hellwig return __lookup_hash(&this, base, NULL); 1942057f6c01SJames Morris } 1943057f6c01SJames Morris 19442d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags, 19452d8f3038SAl Viro struct path *path) 19461da177e4SLinus Torvalds { 19472d8f3038SAl Viro struct nameidata nd; 19481da177e4SLinus Torvalds char *tmp = getname(name); 19491da177e4SLinus Torvalds int err = PTR_ERR(tmp); 19501da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 19512d8f3038SAl Viro 19522d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 19532d8f3038SAl Viro 19542d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 19551da177e4SLinus Torvalds putname(tmp); 19562d8f3038SAl Viro if (!err) 19572d8f3038SAl Viro *path = nd.path; 19581da177e4SLinus Torvalds } 19591da177e4SLinus Torvalds return err; 19601da177e4SLinus Torvalds } 19611da177e4SLinus Torvalds 19622ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 19632ad94ae6SAl Viro struct nameidata *nd, char **name) 19642ad94ae6SAl Viro { 19652ad94ae6SAl Viro char *s = getname(path); 19662ad94ae6SAl Viro int error; 19672ad94ae6SAl Viro 19682ad94ae6SAl Viro if (IS_ERR(s)) 19692ad94ae6SAl Viro return PTR_ERR(s); 19702ad94ae6SAl Viro 19712ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 19722ad94ae6SAl Viro if (error) 19732ad94ae6SAl Viro putname(s); 19742ad94ae6SAl Viro else 19752ad94ae6SAl Viro *name = s; 19762ad94ae6SAl Viro 19772ad94ae6SAl Viro return error; 19782ad94ae6SAl Viro } 19792ad94ae6SAl Viro 19801da177e4SLinus Torvalds /* 19811da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 19821da177e4SLinus Torvalds * minimal. 19831da177e4SLinus Torvalds */ 19841da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 19851da177e4SLinus Torvalds { 1986da9592edSDavid Howells uid_t fsuid = current_fsuid(); 1987da9592edSDavid Howells 19881da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 19891da177e4SLinus Torvalds return 0; 1990da9592edSDavid Howells if (inode->i_uid == fsuid) 19911da177e4SLinus Torvalds return 0; 1992da9592edSDavid Howells if (dir->i_uid == fsuid) 19931da177e4SLinus Torvalds return 0; 19941da177e4SLinus Torvalds return !capable(CAP_FOWNER); 19951da177e4SLinus Torvalds } 19961da177e4SLinus Torvalds 19971da177e4SLinus Torvalds /* 19981da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 19991da177e4SLinus Torvalds * whether the type of victim is right. 20001da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 20011da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 20021da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 20031da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 20041da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 20051da177e4SLinus Torvalds * a. be owner of dir, or 20061da177e4SLinus Torvalds * b. be owner of victim, or 20071da177e4SLinus Torvalds * c. have CAP_FOWNER capability 20081da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 20091da177e4SLinus Torvalds * links pointing to it. 20101da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 20111da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 20121da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 20131da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 20141da177e4SLinus Torvalds * nfs_async_unlink(). 20151da177e4SLinus Torvalds */ 2016858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 20171da177e4SLinus Torvalds { 20181da177e4SLinus Torvalds int error; 20191da177e4SLinus Torvalds 20201da177e4SLinus Torvalds if (!victim->d_inode) 20211da177e4SLinus Torvalds return -ENOENT; 20221da177e4SLinus Torvalds 20231da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 2024cccc6bbaSAl Viro audit_inode_child(victim, dir); 20251da177e4SLinus Torvalds 2026f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 20271da177e4SLinus Torvalds if (error) 20281da177e4SLinus Torvalds return error; 20291da177e4SLinus Torvalds if (IS_APPEND(dir)) 20301da177e4SLinus Torvalds return -EPERM; 20311da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 2032f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 20331da177e4SLinus Torvalds return -EPERM; 20341da177e4SLinus Torvalds if (isdir) { 20351da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 20361da177e4SLinus Torvalds return -ENOTDIR; 20371da177e4SLinus Torvalds if (IS_ROOT(victim)) 20381da177e4SLinus Torvalds return -EBUSY; 20391da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 20401da177e4SLinus Torvalds return -EISDIR; 20411da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 20421da177e4SLinus Torvalds return -ENOENT; 20431da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 20441da177e4SLinus Torvalds return -EBUSY; 20451da177e4SLinus Torvalds return 0; 20461da177e4SLinus Torvalds } 20471da177e4SLinus Torvalds 20481da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 20491da177e4SLinus Torvalds * dir. 20501da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 20511da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 20521da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 20531da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 20541da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 20551da177e4SLinus Torvalds */ 2056a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 20571da177e4SLinus Torvalds { 20581da177e4SLinus Torvalds if (child->d_inode) 20591da177e4SLinus Torvalds return -EEXIST; 20601da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 20611da177e4SLinus Torvalds return -ENOENT; 2062f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 20631da177e4SLinus Torvalds } 20641da177e4SLinus Torvalds 20651da177e4SLinus Torvalds /* 20661da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 20671da177e4SLinus Torvalds */ 20681da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 20691da177e4SLinus Torvalds { 20701da177e4SLinus Torvalds struct dentry *p; 20711da177e4SLinus Torvalds 20721da177e4SLinus Torvalds if (p1 == p2) { 2073f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 20741da177e4SLinus Torvalds return NULL; 20751da177e4SLinus Torvalds } 20761da177e4SLinus Torvalds 2077a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 20781da177e4SLinus Torvalds 2079e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 2080e2761a11SOGAWA Hirofumi if (p) { 2081f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 2082f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 20831da177e4SLinus Torvalds return p; 20841da177e4SLinus Torvalds } 20851da177e4SLinus Torvalds 2086e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 2087e2761a11SOGAWA Hirofumi if (p) { 2088f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2089f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 20901da177e4SLinus Torvalds return p; 20911da177e4SLinus Torvalds } 20921da177e4SLinus Torvalds 2093f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2094f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 20951da177e4SLinus Torvalds return NULL; 20961da177e4SLinus Torvalds } 20971da177e4SLinus Torvalds 20981da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 20991da177e4SLinus Torvalds { 21001b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 21011da177e4SLinus Torvalds if (p1 != p2) { 21021b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 2103a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 21041da177e4SLinus Torvalds } 21051da177e4SLinus Torvalds } 21061da177e4SLinus Torvalds 21071da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode, 21081da177e4SLinus Torvalds struct nameidata *nd) 21091da177e4SLinus Torvalds { 2110a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 21111da177e4SLinus Torvalds 21121da177e4SLinus Torvalds if (error) 21131da177e4SLinus Torvalds return error; 21141da177e4SLinus Torvalds 2115acfa4380SAl Viro if (!dir->i_op->create) 21161da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 21171da177e4SLinus Torvalds mode &= S_IALLUGO; 21181da177e4SLinus Torvalds mode |= S_IFREG; 21191da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 21201da177e4SLinus Torvalds if (error) 21211da177e4SLinus Torvalds return error; 21221da177e4SLinus Torvalds error = dir->i_op->create(dir, dentry, mode, nd); 2123a74574aaSStephen Smalley if (!error) 2124f38aa942SAmy Griffis fsnotify_create(dir, dentry); 21251da177e4SLinus Torvalds return error; 21261da177e4SLinus Torvalds } 21271da177e4SLinus Torvalds 21283fb64190SChristoph Hellwig int may_open(struct path *path, int acc_mode, int flag) 21291da177e4SLinus Torvalds { 21303fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 21311da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 21321da177e4SLinus Torvalds int error; 21331da177e4SLinus Torvalds 21341da177e4SLinus Torvalds if (!inode) 21351da177e4SLinus Torvalds return -ENOENT; 21361da177e4SLinus Torvalds 2137c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2138c8fe8f30SChristoph Hellwig case S_IFLNK: 21391da177e4SLinus Torvalds return -ELOOP; 2140c8fe8f30SChristoph Hellwig case S_IFDIR: 2141c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 21421da177e4SLinus Torvalds return -EISDIR; 2143c8fe8f30SChristoph Hellwig break; 2144c8fe8f30SChristoph Hellwig case S_IFBLK: 2145c8fe8f30SChristoph Hellwig case S_IFCHR: 21463fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 21471da177e4SLinus Torvalds return -EACCES; 2148c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 2149c8fe8f30SChristoph Hellwig case S_IFIFO: 2150c8fe8f30SChristoph Hellwig case S_IFSOCK: 21511da177e4SLinus Torvalds flag &= ~O_TRUNC; 2152c8fe8f30SChristoph Hellwig break; 21534a3fd211SDave Hansen } 2154b41572e9SDave Hansen 21553fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 2156b41572e9SDave Hansen if (error) 2157b41572e9SDave Hansen return error; 21586146f0d5SMimi Zohar 21591da177e4SLinus Torvalds /* 21601da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 21611da177e4SLinus Torvalds */ 21621da177e4SLinus Torvalds if (IS_APPEND(inode)) { 21638737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 21647715b521SAl Viro return -EPERM; 21651da177e4SLinus Torvalds if (flag & O_TRUNC) 21667715b521SAl Viro return -EPERM; 21671da177e4SLinus Torvalds } 21681da177e4SLinus Torvalds 21691da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 21707715b521SAl Viro if (flag & O_NOATIME && !is_owner_or_cap(inode)) 21717715b521SAl Viro return -EPERM; 21721da177e4SLinus Torvalds 21731da177e4SLinus Torvalds /* 21741da177e4SLinus Torvalds * Ensure there are no outstanding leases on the file. 21751da177e4SLinus Torvalds */ 2176b65a9cfcSAl Viro return break_lease(inode, flag); 21777715b521SAl Viro } 21787715b521SAl Viro 2179e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 21807715b521SAl Viro { 2181e1181ee6SJeff Layton struct path *path = &filp->f_path; 21827715b521SAl Viro struct inode *inode = path->dentry->d_inode; 21837715b521SAl Viro int error = get_write_access(inode); 21841da177e4SLinus Torvalds if (error) 21857715b521SAl Viro return error; 21861da177e4SLinus Torvalds /* 21871da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 21881da177e4SLinus Torvalds */ 21891da177e4SLinus Torvalds error = locks_verify_locked(inode); 2190be6d3e56SKentaro Takeda if (!error) 2191ea0d3ab2STetsuo Handa error = security_path_truncate(path); 21921da177e4SLinus Torvalds if (!error) { 21937715b521SAl Viro error = do_truncate(path->dentry, 0, 2194d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 2195e1181ee6SJeff Layton filp); 21961da177e4SLinus Torvalds } 21971da177e4SLinus Torvalds put_write_access(inode); 2198acd0c935SMimi Zohar return error; 21991da177e4SLinus Torvalds } 22001da177e4SLinus Torvalds 2201d57999e1SDave Hansen /* 2202d57999e1SDave Hansen * Be careful about ever adding any more callers of this 2203d57999e1SDave Hansen * function. Its flags must be in the namei format, not 2204d57999e1SDave Hansen * what get passed to sys_open(). 2205d57999e1SDave Hansen */ 2206d57999e1SDave Hansen static int __open_namei_create(struct nameidata *nd, struct path *path, 22078737c930SAl Viro int open_flag, int mode) 2208aab520e2SDave Hansen { 2209aab520e2SDave Hansen int error; 22104ac91378SJan Blunck struct dentry *dir = nd->path.dentry; 2211aab520e2SDave Hansen 2212aab520e2SDave Hansen if (!IS_POSIXACL(dir->d_inode)) 2213ce3b0f8dSAl Viro mode &= ~current_umask(); 2214be6d3e56SKentaro Takeda error = security_path_mknod(&nd->path, path->dentry, mode, 0); 2215be6d3e56SKentaro Takeda if (error) 2216be6d3e56SKentaro Takeda goto out_unlock; 2217aab520e2SDave Hansen error = vfs_create(dir->d_inode, path->dentry, mode, nd); 2218be6d3e56SKentaro Takeda out_unlock: 2219aab520e2SDave Hansen mutex_unlock(&dir->d_inode->i_mutex); 22204ac91378SJan Blunck dput(nd->path.dentry); 22214ac91378SJan Blunck nd->path.dentry = path->dentry; 222231e6b01fSNick Piggin 2223aab520e2SDave Hansen if (error) 2224aab520e2SDave Hansen return error; 2225aab520e2SDave Hansen /* Don't check for write permission, don't truncate */ 22268737c930SAl Viro return may_open(&nd->path, 0, open_flag & ~O_TRUNC); 2227aab520e2SDave Hansen } 2228aab520e2SDave Hansen 22291da177e4SLinus Torvalds /* 2230d57999e1SDave Hansen * Note that while the flag value (low two bits) for sys_open means: 2231d57999e1SDave Hansen * 00 - read-only 2232d57999e1SDave Hansen * 01 - write-only 2233d57999e1SDave Hansen * 10 - read-write 2234d57999e1SDave Hansen * 11 - special 2235d57999e1SDave Hansen * it is changed into 2236d57999e1SDave Hansen * 00 - no permissions needed 2237d57999e1SDave Hansen * 01 - read-permission 2238d57999e1SDave Hansen * 10 - write-permission 2239d57999e1SDave Hansen * 11 - read-write 2240d57999e1SDave Hansen * for the internal routines (ie open_namei()/follow_link() etc) 2241d57999e1SDave Hansen * This is more logical, and also allows the 00 "no perm needed" 2242d57999e1SDave Hansen * to be used for symlinks (where the permissions are checked 2243d57999e1SDave Hansen * later). 2244d57999e1SDave Hansen * 2245d57999e1SDave Hansen */ 2246d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2247d57999e1SDave Hansen { 2248d57999e1SDave Hansen if ((flag+1) & O_ACCMODE) 2249d57999e1SDave Hansen flag++; 2250d57999e1SDave Hansen return flag; 2251d57999e1SDave Hansen } 2252d57999e1SDave Hansen 22537715b521SAl Viro static int open_will_truncate(int flag, struct inode *inode) 22544a3fd211SDave Hansen { 2255d57999e1SDave Hansen /* 22564a3fd211SDave Hansen * We'll never write to the fs underlying 22574a3fd211SDave Hansen * a device file. 22584a3fd211SDave Hansen */ 22594a3fd211SDave Hansen if (special_file(inode->i_mode)) 22604a3fd211SDave Hansen return 0; 22614a3fd211SDave Hansen return (flag & O_TRUNC); 22624a3fd211SDave Hansen } 22634a3fd211SDave Hansen 2264648fa861SAl Viro static struct file *finish_open(struct nameidata *nd, 22659a66179eSAl Viro int open_flag, int acc_mode) 2266648fa861SAl Viro { 2267648fa861SAl Viro struct file *filp; 2268648fa861SAl Viro int will_truncate; 2269648fa861SAl Viro int error; 2270648fa861SAl Viro 22719a66179eSAl Viro will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode); 2272648fa861SAl Viro if (will_truncate) { 2273648fa861SAl Viro error = mnt_want_write(nd->path.mnt); 2274648fa861SAl Viro if (error) 2275648fa861SAl Viro goto exit; 2276648fa861SAl Viro } 2277648fa861SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2278648fa861SAl Viro if (error) { 2279648fa861SAl Viro if (will_truncate) 2280648fa861SAl Viro mnt_drop_write(nd->path.mnt); 2281648fa861SAl Viro goto exit; 2282648fa861SAl Viro } 2283648fa861SAl Viro filp = nameidata_to_filp(nd); 2284648fa861SAl Viro if (!IS_ERR(filp)) { 2285648fa861SAl Viro error = ima_file_check(filp, acc_mode); 2286648fa861SAl Viro if (error) { 2287648fa861SAl Viro fput(filp); 2288648fa861SAl Viro filp = ERR_PTR(error); 2289648fa861SAl Viro } 2290648fa861SAl Viro } 2291648fa861SAl Viro if (!IS_ERR(filp)) { 2292648fa861SAl Viro if (will_truncate) { 2293e1181ee6SJeff Layton error = handle_truncate(filp); 2294648fa861SAl Viro if (error) { 2295648fa861SAl Viro fput(filp); 2296648fa861SAl Viro filp = ERR_PTR(error); 2297648fa861SAl Viro } 2298648fa861SAl Viro } 2299648fa861SAl Viro } 2300648fa861SAl Viro /* 2301648fa861SAl Viro * It is now safe to drop the mnt write 2302648fa861SAl Viro * because the filp has had a write taken 2303648fa861SAl Viro * on its behalf. 2304648fa861SAl Viro */ 2305648fa861SAl Viro if (will_truncate) 2306648fa861SAl Viro mnt_drop_write(nd->path.mnt); 2307d893f1bcSAl Viro path_put(&nd->path); 2308648fa861SAl Viro return filp; 2309648fa861SAl Viro 2310648fa861SAl Viro exit: 2311648fa861SAl Viro if (!IS_ERR(nd->intent.open.file)) 2312648fa861SAl Viro release_open_intent(nd); 2313648fa861SAl Viro path_put(&nd->path); 2314648fa861SAl Viro return ERR_PTR(error); 2315648fa861SAl Viro } 2316648fa861SAl Viro 231731e6b01fSNick Piggin /* 231831e6b01fSNick Piggin * Handle O_CREAT case for do_filp_open 231931e6b01fSNick Piggin */ 2320fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path, 23215b369df8SAl Viro int open_flag, int acc_mode, 23223e297b61SAl Viro int mode, const char *pathname) 2323fb1cc555SAl Viro { 2324a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 2325fb1cc555SAl Viro struct file *filp; 23261f36f774SAl Viro int error = -EISDIR; 2327fb1cc555SAl Viro 23281f36f774SAl Viro switch (nd->last_type) { 23291f36f774SAl Viro case LAST_DOTDOT: 23301f36f774SAl Viro follow_dotdot(nd); 23311f36f774SAl Viro dir = nd->path.dentry; 2332176306f5SNeil Brown case LAST_DOT: 2333fb045adbSNick Piggin if (need_reval_dot(dir)) { 2334f20877d9SJ. R. Okajima int status = d_revalidate(nd->path.dentry, nd); 2335f20877d9SJ. R. Okajima if (!status) 2336f20877d9SJ. R. Okajima status = -ESTALE; 2337f20877d9SJ. R. Okajima if (status < 0) { 2338f20877d9SJ. R. Okajima error = status; 2339a2c36b45SAl Viro goto exit; 23401f36f774SAl Viro } 2341f20877d9SJ. R. Okajima } 23421f36f774SAl Viro /* fallthrough */ 23431f36f774SAl Viro case LAST_ROOT: 23441f36f774SAl Viro goto exit; 23451f36f774SAl Viro case LAST_BIND: 23461f36f774SAl Viro audit_inode(pathname, dir); 23471f36f774SAl Viro goto ok; 23481f36f774SAl Viro } 2349a2c36b45SAl Viro 23501f36f774SAl Viro /* trailing slashes? */ 235131e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 23521f36f774SAl Viro goto exit; 23531f36f774SAl Viro 2354a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 2355a1e28038SAl Viro 2356a1e28038SAl Viro path->dentry = lookup_hash(nd); 2357a1e28038SAl Viro path->mnt = nd->path.mnt; 2358a1e28038SAl Viro 2359fb1cc555SAl Viro error = PTR_ERR(path->dentry); 2360fb1cc555SAl Viro if (IS_ERR(path->dentry)) { 2361fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2362fb1cc555SAl Viro goto exit; 2363fb1cc555SAl Viro } 2364fb1cc555SAl Viro 2365fb1cc555SAl Viro if (IS_ERR(nd->intent.open.file)) { 2366fb1cc555SAl Viro error = PTR_ERR(nd->intent.open.file); 2367fb1cc555SAl Viro goto exit_mutex_unlock; 2368fb1cc555SAl Viro } 2369fb1cc555SAl Viro 2370fb1cc555SAl Viro /* Negative dentry, just create the file */ 2371fb1cc555SAl Viro if (!path->dentry->d_inode) { 2372fb1cc555SAl Viro /* 2373fb1cc555SAl Viro * This write is needed to ensure that a 2374fb1cc555SAl Viro * ro->rw transition does not occur between 2375fb1cc555SAl Viro * the time when the file is created and when 2376fb1cc555SAl Viro * a permanent write count is taken through 2377fb1cc555SAl Viro * the 'struct file' in nameidata_to_filp(). 2378fb1cc555SAl Viro */ 2379fb1cc555SAl Viro error = mnt_want_write(nd->path.mnt); 2380fb1cc555SAl Viro if (error) 2381fb1cc555SAl Viro goto exit_mutex_unlock; 2382fb1cc555SAl Viro error = __open_namei_create(nd, path, open_flag, mode); 2383fb1cc555SAl Viro if (error) { 2384fb1cc555SAl Viro mnt_drop_write(nd->path.mnt); 2385fb1cc555SAl Viro goto exit; 2386fb1cc555SAl Viro } 2387fb1cc555SAl Viro filp = nameidata_to_filp(nd); 2388fb1cc555SAl Viro mnt_drop_write(nd->path.mnt); 2389d893f1bcSAl Viro path_put(&nd->path); 2390fb1cc555SAl Viro if (!IS_ERR(filp)) { 2391fb1cc555SAl Viro error = ima_file_check(filp, acc_mode); 2392fb1cc555SAl Viro if (error) { 2393fb1cc555SAl Viro fput(filp); 2394fb1cc555SAl Viro filp = ERR_PTR(error); 2395fb1cc555SAl Viro } 2396fb1cc555SAl Viro } 2397fb1cc555SAl Viro return filp; 2398fb1cc555SAl Viro } 2399fb1cc555SAl Viro 2400fb1cc555SAl Viro /* 2401fb1cc555SAl Viro * It already exists. 2402fb1cc555SAl Viro */ 2403fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2404fb1cc555SAl Viro audit_inode(pathname, path->dentry); 2405fb1cc555SAl Viro 2406fb1cc555SAl Viro error = -EEXIST; 24075b369df8SAl Viro if (open_flag & O_EXCL) 2408fb1cc555SAl Viro goto exit_dput; 2409fb1cc555SAl Viro 24109875cf80SDavid Howells error = follow_managed(path, nd->flags); 24119875cf80SDavid Howells if (error < 0) 2412fb1cc555SAl Viro goto exit_dput; 2413fb1cc555SAl Viro 2414fb1cc555SAl Viro error = -ENOENT; 2415fb1cc555SAl Viro if (!path->dentry->d_inode) 2416fb1cc555SAl Viro goto exit_dput; 24179e67f361SAl Viro 24189e67f361SAl Viro if (path->dentry->d_inode->i_op->follow_link) 2419fb1cc555SAl Viro return NULL; 2420fb1cc555SAl Viro 2421fb1cc555SAl Viro path_to_nameidata(path, nd); 242231e6b01fSNick Piggin nd->inode = path->dentry->d_inode; 2423fb1cc555SAl Viro error = -EISDIR; 242431e6b01fSNick Piggin if (S_ISDIR(nd->inode->i_mode)) 2425fb1cc555SAl Viro goto exit; 242667ee3ad2SAl Viro ok: 24279a66179eSAl Viro filp = finish_open(nd, open_flag, acc_mode); 2428fb1cc555SAl Viro return filp; 2429fb1cc555SAl Viro 2430fb1cc555SAl Viro exit_mutex_unlock: 2431fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2432fb1cc555SAl Viro exit_dput: 2433fb1cc555SAl Viro path_put_conditional(path, nd); 2434fb1cc555SAl Viro exit: 2435fb1cc555SAl Viro if (!IS_ERR(nd->intent.open.file)) 2436fb1cc555SAl Viro release_open_intent(nd); 2437fb1cc555SAl Viro path_put(&nd->path); 2438fb1cc555SAl Viro return ERR_PTR(error); 2439fb1cc555SAl Viro } 2440fb1cc555SAl Viro 24414a3fd211SDave Hansen /* 24424a3fd211SDave Hansen * Note that the low bits of the passed in "open_flag" 24434a3fd211SDave Hansen * are not the same as in the local variable "flag". See 24444a3fd211SDave Hansen * open_to_namei_flags() for more details. 24451da177e4SLinus Torvalds */ 2446a70e65dfSChristoph Hellwig struct file *do_filp_open(int dfd, const char *pathname, 24476e8341a1SAl Viro int open_flag, int mode, int acc_mode) 24481da177e4SLinus Torvalds { 24494a3fd211SDave Hansen struct file *filp; 2450a70e65dfSChristoph Hellwig struct nameidata nd; 24516e8341a1SAl Viro int error; 24529850c056SAl Viro struct path path; 24531da177e4SLinus Torvalds int count = 0; 2454d57999e1SDave Hansen int flag = open_to_namei_flags(open_flag); 245531e6b01fSNick Piggin int flags; 24561f36f774SAl Viro 24571f36f774SAl Viro if (!(open_flag & O_CREAT)) 24581f36f774SAl Viro mode = 0; 24591da177e4SLinus Torvalds 2460b1085ba8SLino Sanfilippo /* Must never be set by userspace */ 2461b1085ba8SLino Sanfilippo open_flag &= ~FMODE_NONOTIFY; 2462b1085ba8SLino Sanfilippo 24636b2f3d1fSChristoph Hellwig /* 24646b2f3d1fSChristoph Hellwig * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only 24656b2f3d1fSChristoph Hellwig * check for O_DSYNC if the need any syncing at all we enforce it's 24666b2f3d1fSChristoph Hellwig * always set instead of having to deal with possibly weird behaviour 24676b2f3d1fSChristoph Hellwig * for malicious applications setting only __O_SYNC. 24686b2f3d1fSChristoph Hellwig */ 24696b2f3d1fSChristoph Hellwig if (open_flag & __O_SYNC) 24706b2f3d1fSChristoph Hellwig open_flag |= O_DSYNC; 24716b2f3d1fSChristoph Hellwig 24726e8341a1SAl Viro if (!acc_mode) 24736d125529SAl Viro acc_mode = MAY_OPEN | ACC_MODE(open_flag); 24741da177e4SLinus Torvalds 2475834f2a4aSTrond Myklebust /* O_TRUNC implies we need access checks for write permissions */ 24764296e2cbSAl Viro if (open_flag & O_TRUNC) 2477834f2a4aSTrond Myklebust acc_mode |= MAY_WRITE; 2478834f2a4aSTrond Myklebust 24791da177e4SLinus Torvalds /* Allow the LSM permission hook to distinguish append 24801da177e4SLinus Torvalds access from general write access. */ 24814296e2cbSAl Viro if (open_flag & O_APPEND) 24821da177e4SLinus Torvalds acc_mode |= MAY_APPEND; 24831da177e4SLinus Torvalds 248431e6b01fSNick Piggin flags = LOOKUP_OPEN; 248531e6b01fSNick Piggin if (open_flag & O_CREAT) { 248631e6b01fSNick Piggin flags |= LOOKUP_CREATE; 248731e6b01fSNick Piggin if (open_flag & O_EXCL) 248831e6b01fSNick Piggin flags |= LOOKUP_EXCL; 2489654f562cSJ. R. Okajima } 249031e6b01fSNick Piggin if (open_flag & O_DIRECTORY) 249131e6b01fSNick Piggin flags |= LOOKUP_DIRECTORY; 249231e6b01fSNick Piggin if (!(open_flag & O_NOFOLLOW)) 249331e6b01fSNick Piggin flags |= LOOKUP_FOLLOW; 249431e6b01fSNick Piggin 249531e6b01fSNick Piggin filp = get_empty_filp(); 249631e6b01fSNick Piggin if (!filp) 249731e6b01fSNick Piggin return ERR_PTR(-ENFILE); 249831e6b01fSNick Piggin 249931e6b01fSNick Piggin filp->f_flags = open_flag; 250031e6b01fSNick Piggin nd.intent.open.file = filp; 250131e6b01fSNick Piggin nd.intent.open.flags = flag; 250231e6b01fSNick Piggin nd.intent.open.create_mode = mode; 250331e6b01fSNick Piggin 250431e6b01fSNick Piggin if (open_flag & O_CREAT) 250531e6b01fSNick Piggin goto creat; 250631e6b01fSNick Piggin 250731e6b01fSNick Piggin /* !O_CREAT, simple open */ 250831e6b01fSNick Piggin error = do_path_lookup(dfd, pathname, flags, &nd); 250931e6b01fSNick Piggin if (unlikely(error)) 251031e6b01fSNick Piggin goto out_filp; 251131e6b01fSNick Piggin error = -ELOOP; 251231e6b01fSNick Piggin if (!(nd.flags & LOOKUP_FOLLOW)) { 251331e6b01fSNick Piggin if (nd.inode->i_op->follow_link) 251431e6b01fSNick Piggin goto out_path; 251531e6b01fSNick Piggin } 251631e6b01fSNick Piggin error = -ENOTDIR; 251731e6b01fSNick Piggin if (nd.flags & LOOKUP_DIRECTORY) { 251831e6b01fSNick Piggin if (!nd.inode->i_op->lookup) 251931e6b01fSNick Piggin goto out_path; 252031e6b01fSNick Piggin } 252131e6b01fSNick Piggin audit_inode(pathname, nd.path.dentry); 252231e6b01fSNick Piggin filp = finish_open(&nd, open_flag, acc_mode); 252331e6b01fSNick Piggin return filp; 252431e6b01fSNick Piggin 252531e6b01fSNick Piggin creat: 252631e6b01fSNick Piggin /* OK, have to create the file. Find the parent. */ 252731e6b01fSNick Piggin error = path_init_rcu(dfd, pathname, 252831e6b01fSNick Piggin LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd); 252931e6b01fSNick Piggin if (error) 253031e6b01fSNick Piggin goto out_filp; 253131e6b01fSNick Piggin error = path_walk_rcu(pathname, &nd); 253231e6b01fSNick Piggin path_finish_rcu(&nd); 253331e6b01fSNick Piggin if (unlikely(error == -ECHILD || error == -ESTALE)) { 253431e6b01fSNick Piggin /* slower, locked walk */ 253531e6b01fSNick Piggin if (error == -ESTALE) { 253631e6b01fSNick Piggin reval: 253731e6b01fSNick Piggin flags |= LOOKUP_REVAL; 253831e6b01fSNick Piggin } 253931e6b01fSNick Piggin error = path_init(dfd, pathname, 254031e6b01fSNick Piggin LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd); 254131e6b01fSNick Piggin if (error) 254231e6b01fSNick Piggin goto out_filp; 254331e6b01fSNick Piggin 254431e6b01fSNick Piggin error = path_walk_simple(pathname, &nd); 254531e6b01fSNick Piggin } 254631e6b01fSNick Piggin if (unlikely(error)) 254731e6b01fSNick Piggin goto out_filp; 254831e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) 25499b4a9b14SAl Viro audit_inode(pathname, nd.path.dentry); 25501da177e4SLinus Torvalds 25511da177e4SLinus Torvalds /* 2552a2c36b45SAl Viro * We have the parent and last component. 25531da177e4SLinus Torvalds */ 255431e6b01fSNick Piggin nd.flags = flags; 25553e297b61SAl Viro filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname); 2556806b681cSAl Viro while (unlikely(!filp)) { /* trailing symlink */ 25577b9337aaSNick Piggin struct path link = path; 25587b9337aaSNick Piggin struct inode *linki = link.dentry->d_inode; 2559def4af30SAl Viro void *cookie; 2560806b681cSAl Viro error = -ELOOP; 2561db372915SDavid Howells if (!(nd.flags & LOOKUP_FOLLOW)) 25621f36f774SAl Viro goto exit_dput; 25631f36f774SAl Viro if (count++ == 32) 2564806b681cSAl Viro goto exit_dput; 2565806b681cSAl Viro /* 2566806b681cSAl Viro * This is subtle. Instead of calling do_follow_link() we do 2567806b681cSAl Viro * the thing by hands. The reason is that this way we have zero 2568806b681cSAl Viro * link_count and path_walk() (called from ->follow_link) 2569806b681cSAl Viro * honoring LOOKUP_PARENT. After that we have the parent and 2570806b681cSAl Viro * last component, i.e. we are in the same situation as after 2571806b681cSAl Viro * the first path_walk(). Well, almost - if the last component 2572806b681cSAl Viro * is normal we get its copy stored in nd->last.name and we will 2573806b681cSAl Viro * have to putname() it when we are done. Procfs-like symlinks 2574806b681cSAl Viro * just set LAST_BIND. 2575806b681cSAl Viro */ 2576806b681cSAl Viro nd.flags |= LOOKUP_PARENT; 25777b9337aaSNick Piggin error = security_inode_follow_link(link.dentry, &nd); 2578806b681cSAl Viro if (error) 2579806b681cSAl Viro goto exit_dput; 25807b9337aaSNick Piggin error = __do_follow_link(&link, &nd, &cookie); 2581def4af30SAl Viro if (unlikely(error)) { 25827b9337aaSNick Piggin if (!IS_ERR(cookie) && linki->i_op->put_link) 25837b9337aaSNick Piggin linki->i_op->put_link(link.dentry, &nd, cookie); 2584806b681cSAl Viro /* nd.path had been dropped */ 25857b9337aaSNick Piggin nd.path = link; 258631e6b01fSNick Piggin goto out_path; 2587806b681cSAl Viro } 2588806b681cSAl Viro nd.flags &= ~LOOKUP_PARENT; 25893e297b61SAl Viro filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname); 25907b9337aaSNick Piggin if (linki->i_op->put_link) 25917b9337aaSNick Piggin linki->i_op->put_link(link.dentry, &nd, cookie); 25927b9337aaSNick Piggin path_put(&link); 2593806b681cSAl Viro } 259410fa8e62SAl Viro out: 25952a737871SAl Viro if (nd.root.mnt) 25962a737871SAl Viro path_put(&nd.root); 259731e6b01fSNick Piggin if (filp == ERR_PTR(-ESTALE) && !(flags & LOOKUP_REVAL)) 259810fa8e62SAl Viro goto reval; 259910fa8e62SAl Viro return filp; 26001da177e4SLinus Torvalds 2601806b681cSAl Viro exit_dput: 2602806b681cSAl Viro path_put_conditional(&path, &nd); 260331e6b01fSNick Piggin out_path: 260431e6b01fSNick Piggin path_put(&nd.path); 260531e6b01fSNick Piggin out_filp: 2606806b681cSAl Viro if (!IS_ERR(nd.intent.open.file)) 2607a70e65dfSChristoph Hellwig release_open_intent(&nd); 260810fa8e62SAl Viro filp = ERR_PTR(error); 260910fa8e62SAl Viro goto out; 2610de459215SKirill Korotaev } 26111da177e4SLinus Torvalds 26121da177e4SLinus Torvalds /** 2613a70e65dfSChristoph Hellwig * filp_open - open file and return file pointer 2614a70e65dfSChristoph Hellwig * 2615a70e65dfSChristoph Hellwig * @filename: path to open 2616a70e65dfSChristoph Hellwig * @flags: open flags as per the open(2) second argument 2617a70e65dfSChristoph Hellwig * @mode: mode for the new file if O_CREAT is set, else ignored 2618a70e65dfSChristoph Hellwig * 2619a70e65dfSChristoph Hellwig * This is the helper to open a file from kernelspace if you really 2620a70e65dfSChristoph Hellwig * have to. But in generally you should not do this, so please move 2621a70e65dfSChristoph Hellwig * along, nothing to see here.. 2622a70e65dfSChristoph Hellwig */ 2623a70e65dfSChristoph Hellwig struct file *filp_open(const char *filename, int flags, int mode) 2624a70e65dfSChristoph Hellwig { 26256e8341a1SAl Viro return do_filp_open(AT_FDCWD, filename, flags, mode, 0); 2626a70e65dfSChristoph Hellwig } 2627a70e65dfSChristoph Hellwig EXPORT_SYMBOL(filp_open); 2628a70e65dfSChristoph Hellwig 2629a70e65dfSChristoph Hellwig /** 26301da177e4SLinus Torvalds * lookup_create - lookup a dentry, creating it if it doesn't exist 26311da177e4SLinus Torvalds * @nd: nameidata info 26321da177e4SLinus Torvalds * @is_dir: directory flag 26331da177e4SLinus Torvalds * 26341da177e4SLinus Torvalds * Simple function to lookup and return a dentry and create it 26351da177e4SLinus Torvalds * if it doesn't exist. Is SMP-safe. 2636c663e5d8SChristoph Hellwig * 26374ac91378SJan Blunck * Returns with nd->path.dentry->d_inode->i_mutex locked. 26381da177e4SLinus Torvalds */ 26391da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir) 26401da177e4SLinus Torvalds { 2641c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 26421da177e4SLinus Torvalds 26434ac91378SJan Blunck mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 2644c663e5d8SChristoph Hellwig /* 2645c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 2646c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 2647c663e5d8SChristoph Hellwig */ 26481da177e4SLinus Torvalds if (nd->last_type != LAST_NORM) 26491da177e4SLinus Torvalds goto fail; 26501da177e4SLinus Torvalds nd->flags &= ~LOOKUP_PARENT; 26513516586aSAl Viro nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL; 2652a634904aSASANO Masahiro nd->intent.open.flags = O_EXCL; 2653c663e5d8SChristoph Hellwig 2654c663e5d8SChristoph Hellwig /* 2655c663e5d8SChristoph Hellwig * Do the final lookup. 2656c663e5d8SChristoph Hellwig */ 265749705b77SChristoph Hellwig dentry = lookup_hash(nd); 26581da177e4SLinus Torvalds if (IS_ERR(dentry)) 26591da177e4SLinus Torvalds goto fail; 2660c663e5d8SChristoph Hellwig 2661e9baf6e5SAl Viro if (dentry->d_inode) 2662e9baf6e5SAl Viro goto eexist; 2663c663e5d8SChristoph Hellwig /* 2664c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 2665c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 2666c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 2667c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 2668c663e5d8SChristoph Hellwig */ 2669e9baf6e5SAl Viro if (unlikely(!is_dir && nd->last.name[nd->last.len])) { 26701da177e4SLinus Torvalds dput(dentry); 26711da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 2672e9baf6e5SAl Viro } 2673e9baf6e5SAl Viro return dentry; 2674e9baf6e5SAl Viro eexist: 2675e9baf6e5SAl Viro dput(dentry); 2676e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 26771da177e4SLinus Torvalds fail: 26781da177e4SLinus Torvalds return dentry; 26791da177e4SLinus Torvalds } 2680f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create); 26811da177e4SLinus Torvalds 26821da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 26831da177e4SLinus Torvalds { 2684a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 26851da177e4SLinus Torvalds 26861da177e4SLinus Torvalds if (error) 26871da177e4SLinus Torvalds return error; 26881da177e4SLinus Torvalds 26891da177e4SLinus Torvalds if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) 26901da177e4SLinus Torvalds return -EPERM; 26911da177e4SLinus Torvalds 2692acfa4380SAl Viro if (!dir->i_op->mknod) 26931da177e4SLinus Torvalds return -EPERM; 26941da177e4SLinus Torvalds 269508ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 269608ce5f16SSerge E. Hallyn if (error) 269708ce5f16SSerge E. Hallyn return error; 269808ce5f16SSerge E. Hallyn 26991da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 27001da177e4SLinus Torvalds if (error) 27011da177e4SLinus Torvalds return error; 27021da177e4SLinus Torvalds 27031da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 2704a74574aaSStephen Smalley if (!error) 2705f38aa942SAmy Griffis fsnotify_create(dir, dentry); 27061da177e4SLinus Torvalds return error; 27071da177e4SLinus Torvalds } 27081da177e4SLinus Torvalds 2709463c3197SDave Hansen static int may_mknod(mode_t mode) 2710463c3197SDave Hansen { 2711463c3197SDave Hansen switch (mode & S_IFMT) { 2712463c3197SDave Hansen case S_IFREG: 2713463c3197SDave Hansen case S_IFCHR: 2714463c3197SDave Hansen case S_IFBLK: 2715463c3197SDave Hansen case S_IFIFO: 2716463c3197SDave Hansen case S_IFSOCK: 2717463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2718463c3197SDave Hansen return 0; 2719463c3197SDave Hansen case S_IFDIR: 2720463c3197SDave Hansen return -EPERM; 2721463c3197SDave Hansen default: 2722463c3197SDave Hansen return -EINVAL; 2723463c3197SDave Hansen } 2724463c3197SDave Hansen } 2725463c3197SDave Hansen 27262e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode, 27272e4d0924SHeiko Carstens unsigned, dev) 27281da177e4SLinus Torvalds { 27292ad94ae6SAl Viro int error; 27301da177e4SLinus Torvalds char *tmp; 27311da177e4SLinus Torvalds struct dentry *dentry; 27321da177e4SLinus Torvalds struct nameidata nd; 27331da177e4SLinus Torvalds 27341da177e4SLinus Torvalds if (S_ISDIR(mode)) 27351da177e4SLinus Torvalds return -EPERM; 27361da177e4SLinus Torvalds 27372ad94ae6SAl Viro error = user_path_parent(dfd, filename, &nd, &tmp); 27381da177e4SLinus Torvalds if (error) 27392ad94ae6SAl Viro return error; 27402ad94ae6SAl Viro 27411da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 2742463c3197SDave Hansen if (IS_ERR(dentry)) { 27431da177e4SLinus Torvalds error = PTR_ERR(dentry); 2744463c3197SDave Hansen goto out_unlock; 2745463c3197SDave Hansen } 27464ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2747ce3b0f8dSAl Viro mode &= ~current_umask(); 2748463c3197SDave Hansen error = may_mknod(mode); 2749463c3197SDave Hansen if (error) 2750463c3197SDave Hansen goto out_dput; 2751463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2752463c3197SDave Hansen if (error) 2753463c3197SDave Hansen goto out_dput; 2754be6d3e56SKentaro Takeda error = security_path_mknod(&nd.path, dentry, mode, dev); 2755be6d3e56SKentaro Takeda if (error) 2756be6d3e56SKentaro Takeda goto out_drop_write; 27571da177e4SLinus Torvalds switch (mode & S_IFMT) { 27581da177e4SLinus Torvalds case 0: case S_IFREG: 27594ac91378SJan Blunck error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd); 27601da177e4SLinus Torvalds break; 27611da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 27624ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode, 27631da177e4SLinus Torvalds new_decode_dev(dev)); 27641da177e4SLinus Torvalds break; 27651da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 27664ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0); 27671da177e4SLinus Torvalds break; 27681da177e4SLinus Torvalds } 2769be6d3e56SKentaro Takeda out_drop_write: 2770463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2771463c3197SDave Hansen out_dput: 27721da177e4SLinus Torvalds dput(dentry); 2773463c3197SDave Hansen out_unlock: 27744ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 27751d957f9bSJan Blunck path_put(&nd.path); 27761da177e4SLinus Torvalds putname(tmp); 27771da177e4SLinus Torvalds 27781da177e4SLinus Torvalds return error; 27791da177e4SLinus Torvalds } 27801da177e4SLinus Torvalds 27813480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev) 27825590ff0dSUlrich Drepper { 27835590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 27845590ff0dSUlrich Drepper } 27855590ff0dSUlrich Drepper 27861da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 27871da177e4SLinus Torvalds { 2788a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 27891da177e4SLinus Torvalds 27901da177e4SLinus Torvalds if (error) 27911da177e4SLinus Torvalds return error; 27921da177e4SLinus Torvalds 2793acfa4380SAl Viro if (!dir->i_op->mkdir) 27941da177e4SLinus Torvalds return -EPERM; 27951da177e4SLinus Torvalds 27961da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 27971da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 27981da177e4SLinus Torvalds if (error) 27991da177e4SLinus Torvalds return error; 28001da177e4SLinus Torvalds 28011da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2802a74574aaSStephen Smalley if (!error) 2803f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 28041da177e4SLinus Torvalds return error; 28051da177e4SLinus Torvalds } 28061da177e4SLinus Torvalds 28072e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode) 28081da177e4SLinus Torvalds { 28091da177e4SLinus Torvalds int error = 0; 28101da177e4SLinus Torvalds char * tmp; 28116902d925SDave Hansen struct dentry *dentry; 28126902d925SDave Hansen struct nameidata nd; 28131da177e4SLinus Torvalds 28142ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &tmp); 28152ad94ae6SAl Viro if (error) 28166902d925SDave Hansen goto out_err; 28171da177e4SLinus Torvalds 28181da177e4SLinus Torvalds dentry = lookup_create(&nd, 1); 28191da177e4SLinus Torvalds error = PTR_ERR(dentry); 28206902d925SDave Hansen if (IS_ERR(dentry)) 28216902d925SDave Hansen goto out_unlock; 28226902d925SDave Hansen 28234ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2824ce3b0f8dSAl Viro mode &= ~current_umask(); 2825463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2826463c3197SDave Hansen if (error) 2827463c3197SDave Hansen goto out_dput; 2828be6d3e56SKentaro Takeda error = security_path_mkdir(&nd.path, dentry, mode); 2829be6d3e56SKentaro Takeda if (error) 2830be6d3e56SKentaro Takeda goto out_drop_write; 28314ac91378SJan Blunck error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode); 2832be6d3e56SKentaro Takeda out_drop_write: 2833463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2834463c3197SDave Hansen out_dput: 28351da177e4SLinus Torvalds dput(dentry); 28366902d925SDave Hansen out_unlock: 28374ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 28381d957f9bSJan Blunck path_put(&nd.path); 28391da177e4SLinus Torvalds putname(tmp); 28406902d925SDave Hansen out_err: 28411da177e4SLinus Torvalds return error; 28421da177e4SLinus Torvalds } 28431da177e4SLinus Torvalds 28443cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode) 28455590ff0dSUlrich Drepper { 28465590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 28475590ff0dSUlrich Drepper } 28485590ff0dSUlrich Drepper 28491da177e4SLinus Torvalds /* 28501da177e4SLinus Torvalds * We try to drop the dentry early: we should have 28511da177e4SLinus Torvalds * a usage count of 2 if we're the only user of this 28521da177e4SLinus Torvalds * dentry, and if that is true (possibly after pruning 28531da177e4SLinus Torvalds * the dcache), then we drop the dentry now. 28541da177e4SLinus Torvalds * 28551da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 28561da177e4SLinus Torvalds * do a 28571da177e4SLinus Torvalds * 28581da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 28591da177e4SLinus Torvalds * return -EBUSY; 28601da177e4SLinus Torvalds * 28611da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 28621da177e4SLinus Torvalds * that is still in use by something else.. 28631da177e4SLinus Torvalds */ 28641da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 28651da177e4SLinus Torvalds { 28661da177e4SLinus Torvalds dget(dentry); 28671da177e4SLinus Torvalds shrink_dcache_parent(dentry); 28681da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 2869b7ab39f6SNick Piggin if (dentry->d_count == 2) 28701da177e4SLinus Torvalds __d_drop(dentry); 28711da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 28721da177e4SLinus Torvalds } 28731da177e4SLinus Torvalds 28741da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 28751da177e4SLinus Torvalds { 28761da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 28771da177e4SLinus Torvalds 28781da177e4SLinus Torvalds if (error) 28791da177e4SLinus Torvalds return error; 28801da177e4SLinus Torvalds 2881acfa4380SAl Viro if (!dir->i_op->rmdir) 28821da177e4SLinus Torvalds return -EPERM; 28831da177e4SLinus Torvalds 28841b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 28851da177e4SLinus Torvalds dentry_unhash(dentry); 28861da177e4SLinus Torvalds if (d_mountpoint(dentry)) 28871da177e4SLinus Torvalds error = -EBUSY; 28881da177e4SLinus Torvalds else { 28891da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 28901da177e4SLinus Torvalds if (!error) { 28911da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 2892d83c49f3SAl Viro if (!error) { 28931da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 2894d83c49f3SAl Viro dont_mount(dentry); 2895d83c49f3SAl Viro } 28961da177e4SLinus Torvalds } 28971da177e4SLinus Torvalds } 28981b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 28991da177e4SLinus Torvalds if (!error) { 29001da177e4SLinus Torvalds d_delete(dentry); 29011da177e4SLinus Torvalds } 29021da177e4SLinus Torvalds dput(dentry); 29031da177e4SLinus Torvalds 29041da177e4SLinus Torvalds return error; 29051da177e4SLinus Torvalds } 29061da177e4SLinus Torvalds 29075590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 29081da177e4SLinus Torvalds { 29091da177e4SLinus Torvalds int error = 0; 29101da177e4SLinus Torvalds char * name; 29111da177e4SLinus Torvalds struct dentry *dentry; 29121da177e4SLinus Torvalds struct nameidata nd; 29131da177e4SLinus Torvalds 29142ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 29151da177e4SLinus Torvalds if (error) 29162ad94ae6SAl Viro return error; 29171da177e4SLinus Torvalds 29181da177e4SLinus Torvalds switch(nd.last_type) { 29191da177e4SLinus Torvalds case LAST_DOTDOT: 29201da177e4SLinus Torvalds error = -ENOTEMPTY; 29211da177e4SLinus Torvalds goto exit1; 29221da177e4SLinus Torvalds case LAST_DOT: 29231da177e4SLinus Torvalds error = -EINVAL; 29241da177e4SLinus Torvalds goto exit1; 29251da177e4SLinus Torvalds case LAST_ROOT: 29261da177e4SLinus Torvalds error = -EBUSY; 29271da177e4SLinus Torvalds goto exit1; 29281da177e4SLinus Torvalds } 29290612d9fbSOGAWA Hirofumi 29300612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 29310612d9fbSOGAWA Hirofumi 29324ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 293349705b77SChristoph Hellwig dentry = lookup_hash(&nd); 29341da177e4SLinus Torvalds error = PTR_ERR(dentry); 29356902d925SDave Hansen if (IS_ERR(dentry)) 29366902d925SDave Hansen goto exit2; 29370622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 29380622753bSDave Hansen if (error) 29390622753bSDave Hansen goto exit3; 2940be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 2941be6d3e56SKentaro Takeda if (error) 2942be6d3e56SKentaro Takeda goto exit4; 29434ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 2944be6d3e56SKentaro Takeda exit4: 29450622753bSDave Hansen mnt_drop_write(nd.path.mnt); 29460622753bSDave Hansen exit3: 29471da177e4SLinus Torvalds dput(dentry); 29486902d925SDave Hansen exit2: 29494ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 29501da177e4SLinus Torvalds exit1: 29511d957f9bSJan Blunck path_put(&nd.path); 29521da177e4SLinus Torvalds putname(name); 29531da177e4SLinus Torvalds return error; 29541da177e4SLinus Torvalds } 29551da177e4SLinus Torvalds 29563cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 29575590ff0dSUlrich Drepper { 29585590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 29595590ff0dSUlrich Drepper } 29605590ff0dSUlrich Drepper 29611da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 29621da177e4SLinus Torvalds { 29631da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 29641da177e4SLinus Torvalds 29651da177e4SLinus Torvalds if (error) 29661da177e4SLinus Torvalds return error; 29671da177e4SLinus Torvalds 2968acfa4380SAl Viro if (!dir->i_op->unlink) 29691da177e4SLinus Torvalds return -EPERM; 29701da177e4SLinus Torvalds 29711b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 29721da177e4SLinus Torvalds if (d_mountpoint(dentry)) 29731da177e4SLinus Torvalds error = -EBUSY; 29741da177e4SLinus Torvalds else { 29751da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 2976bec1052eSAl Viro if (!error) { 29771da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 2978bec1052eSAl Viro if (!error) 2979d83c49f3SAl Viro dont_mount(dentry); 2980bec1052eSAl Viro } 29811da177e4SLinus Torvalds } 29821b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 29831da177e4SLinus Torvalds 29841da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 29851da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 2986ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 29871da177e4SLinus Torvalds d_delete(dentry); 29881da177e4SLinus Torvalds } 29890eeca283SRobert Love 29901da177e4SLinus Torvalds return error; 29911da177e4SLinus Torvalds } 29921da177e4SLinus Torvalds 29931da177e4SLinus Torvalds /* 29941da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 29951b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 29961da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 29971da177e4SLinus Torvalds * while waiting on the I/O. 29981da177e4SLinus Torvalds */ 29995590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 30001da177e4SLinus Torvalds { 30012ad94ae6SAl Viro int error; 30021da177e4SLinus Torvalds char *name; 30031da177e4SLinus Torvalds struct dentry *dentry; 30041da177e4SLinus Torvalds struct nameidata nd; 30051da177e4SLinus Torvalds struct inode *inode = NULL; 30061da177e4SLinus Torvalds 30072ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 30081da177e4SLinus Torvalds if (error) 30092ad94ae6SAl Viro return error; 30102ad94ae6SAl Viro 30111da177e4SLinus Torvalds error = -EISDIR; 30121da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 30131da177e4SLinus Torvalds goto exit1; 30140612d9fbSOGAWA Hirofumi 30150612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 30160612d9fbSOGAWA Hirofumi 30174ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 301849705b77SChristoph Hellwig dentry = lookup_hash(&nd); 30191da177e4SLinus Torvalds error = PTR_ERR(dentry); 30201da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 30211da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 30221da177e4SLinus Torvalds if (nd.last.name[nd.last.len]) 30231da177e4SLinus Torvalds goto slashes; 30241da177e4SLinus Torvalds inode = dentry->d_inode; 30251da177e4SLinus Torvalds if (inode) 30267de9c6eeSAl Viro ihold(inode); 30270622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 30280622753bSDave Hansen if (error) 30290622753bSDave Hansen goto exit2; 3030be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 3031be6d3e56SKentaro Takeda if (error) 3032be6d3e56SKentaro Takeda goto exit3; 30334ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 3034be6d3e56SKentaro Takeda exit3: 30350622753bSDave Hansen mnt_drop_write(nd.path.mnt); 30361da177e4SLinus Torvalds exit2: 30371da177e4SLinus Torvalds dput(dentry); 30381da177e4SLinus Torvalds } 30394ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 30401da177e4SLinus Torvalds if (inode) 30411da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 30421da177e4SLinus Torvalds exit1: 30431d957f9bSJan Blunck path_put(&nd.path); 30441da177e4SLinus Torvalds putname(name); 30451da177e4SLinus Torvalds return error; 30461da177e4SLinus Torvalds 30471da177e4SLinus Torvalds slashes: 30481da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 30491da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 30501da177e4SLinus Torvalds goto exit2; 30511da177e4SLinus Torvalds } 30521da177e4SLinus Torvalds 30532e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 30545590ff0dSUlrich Drepper { 30555590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 30565590ff0dSUlrich Drepper return -EINVAL; 30575590ff0dSUlrich Drepper 30585590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 30595590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 30605590ff0dSUlrich Drepper 30615590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 30625590ff0dSUlrich Drepper } 30635590ff0dSUlrich Drepper 30643480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 30655590ff0dSUlrich Drepper { 30665590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 30675590ff0dSUlrich Drepper } 30685590ff0dSUlrich Drepper 3069db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 30701da177e4SLinus Torvalds { 3071a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 30721da177e4SLinus Torvalds 30731da177e4SLinus Torvalds if (error) 30741da177e4SLinus Torvalds return error; 30751da177e4SLinus Torvalds 3076acfa4380SAl Viro if (!dir->i_op->symlink) 30771da177e4SLinus Torvalds return -EPERM; 30781da177e4SLinus Torvalds 30791da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 30801da177e4SLinus Torvalds if (error) 30811da177e4SLinus Torvalds return error; 30821da177e4SLinus Torvalds 30831da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 3084a74574aaSStephen Smalley if (!error) 3085f38aa942SAmy Griffis fsnotify_create(dir, dentry); 30861da177e4SLinus Torvalds return error; 30871da177e4SLinus Torvalds } 30881da177e4SLinus Torvalds 30892e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 30902e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 30911da177e4SLinus Torvalds { 30922ad94ae6SAl Viro int error; 30931da177e4SLinus Torvalds char *from; 30941da177e4SLinus Torvalds char *to; 30956902d925SDave Hansen struct dentry *dentry; 30966902d925SDave Hansen struct nameidata nd; 30971da177e4SLinus Torvalds 30981da177e4SLinus Torvalds from = getname(oldname); 30991da177e4SLinus Torvalds if (IS_ERR(from)) 31001da177e4SLinus Torvalds return PTR_ERR(from); 31012ad94ae6SAl Viro 31022ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 31032ad94ae6SAl Viro if (error) 31046902d925SDave Hansen goto out_putname; 31051da177e4SLinus Torvalds 31061da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 31071da177e4SLinus Torvalds error = PTR_ERR(dentry); 31086902d925SDave Hansen if (IS_ERR(dentry)) 31096902d925SDave Hansen goto out_unlock; 31106902d925SDave Hansen 311175c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 311275c3f29dSDave Hansen if (error) 311375c3f29dSDave Hansen goto out_dput; 3114be6d3e56SKentaro Takeda error = security_path_symlink(&nd.path, dentry, from); 3115be6d3e56SKentaro Takeda if (error) 3116be6d3e56SKentaro Takeda goto out_drop_write; 3117db2e747bSMiklos Szeredi error = vfs_symlink(nd.path.dentry->d_inode, dentry, from); 3118be6d3e56SKentaro Takeda out_drop_write: 311975c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 312075c3f29dSDave Hansen out_dput: 31211da177e4SLinus Torvalds dput(dentry); 31226902d925SDave Hansen out_unlock: 31234ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 31241d957f9bSJan Blunck path_put(&nd.path); 31251da177e4SLinus Torvalds putname(to); 31266902d925SDave Hansen out_putname: 31271da177e4SLinus Torvalds putname(from); 31281da177e4SLinus Torvalds return error; 31291da177e4SLinus Torvalds } 31301da177e4SLinus Torvalds 31313480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 31325590ff0dSUlrich Drepper { 31335590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 31345590ff0dSUlrich Drepper } 31355590ff0dSUlrich Drepper 31361da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 31371da177e4SLinus Torvalds { 31381da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 31391da177e4SLinus Torvalds int error; 31401da177e4SLinus Torvalds 31411da177e4SLinus Torvalds if (!inode) 31421da177e4SLinus Torvalds return -ENOENT; 31431da177e4SLinus Torvalds 3144a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 31451da177e4SLinus Torvalds if (error) 31461da177e4SLinus Torvalds return error; 31471da177e4SLinus Torvalds 31481da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 31491da177e4SLinus Torvalds return -EXDEV; 31501da177e4SLinus Torvalds 31511da177e4SLinus Torvalds /* 31521da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 31531da177e4SLinus Torvalds */ 31541da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 31551da177e4SLinus Torvalds return -EPERM; 3156acfa4380SAl Viro if (!dir->i_op->link) 31571da177e4SLinus Torvalds return -EPERM; 31587e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 31591da177e4SLinus Torvalds return -EPERM; 31601da177e4SLinus Torvalds 31611da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 31621da177e4SLinus Torvalds if (error) 31631da177e4SLinus Torvalds return error; 31641da177e4SLinus Torvalds 31657e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 31661da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 31677e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 3168e31e14ecSStephen Smalley if (!error) 31697e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 31701da177e4SLinus Torvalds return error; 31711da177e4SLinus Torvalds } 31721da177e4SLinus Torvalds 31731da177e4SLinus Torvalds /* 31741da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 31751da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 31761da177e4SLinus Torvalds * newname. --KAB 31771da177e4SLinus Torvalds * 31781da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 31791da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 31801da177e4SLinus Torvalds * and other special files. --ADM 31811da177e4SLinus Torvalds */ 31822e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 31832e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 31841da177e4SLinus Torvalds { 31851da177e4SLinus Torvalds struct dentry *new_dentry; 31862d8f3038SAl Viro struct nameidata nd; 31872d8f3038SAl Viro struct path old_path; 31881da177e4SLinus Torvalds int error; 31891da177e4SLinus Torvalds char *to; 31901da177e4SLinus Torvalds 319145c9b11aSUlrich Drepper if ((flags & ~AT_SYMLINK_FOLLOW) != 0) 3192c04030e1SUlrich Drepper return -EINVAL; 3193c04030e1SUlrich Drepper 31942d8f3038SAl Viro error = user_path_at(olddfd, oldname, 319545c9b11aSUlrich Drepper flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0, 31962d8f3038SAl Viro &old_path); 31971da177e4SLinus Torvalds if (error) 31982ad94ae6SAl Viro return error; 31992ad94ae6SAl Viro 32002ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 32011da177e4SLinus Torvalds if (error) 32021da177e4SLinus Torvalds goto out; 32031da177e4SLinus Torvalds error = -EXDEV; 32042d8f3038SAl Viro if (old_path.mnt != nd.path.mnt) 32051da177e4SLinus Torvalds goto out_release; 32061da177e4SLinus Torvalds new_dentry = lookup_create(&nd, 0); 32071da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 32086902d925SDave Hansen if (IS_ERR(new_dentry)) 32096902d925SDave Hansen goto out_unlock; 321075c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 321175c3f29dSDave Hansen if (error) 321275c3f29dSDave Hansen goto out_dput; 3213be6d3e56SKentaro Takeda error = security_path_link(old_path.dentry, &nd.path, new_dentry); 3214be6d3e56SKentaro Takeda if (error) 3215be6d3e56SKentaro Takeda goto out_drop_write; 32162d8f3038SAl Viro error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry); 3217be6d3e56SKentaro Takeda out_drop_write: 321875c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 321975c3f29dSDave Hansen out_dput: 32201da177e4SLinus Torvalds dput(new_dentry); 32216902d925SDave Hansen out_unlock: 32224ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 32231da177e4SLinus Torvalds out_release: 32241d957f9bSJan Blunck path_put(&nd.path); 32252ad94ae6SAl Viro putname(to); 32261da177e4SLinus Torvalds out: 32272d8f3038SAl Viro path_put(&old_path); 32281da177e4SLinus Torvalds 32291da177e4SLinus Torvalds return error; 32301da177e4SLinus Torvalds } 32311da177e4SLinus Torvalds 32323480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 32335590ff0dSUlrich Drepper { 3234c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 32355590ff0dSUlrich Drepper } 32365590ff0dSUlrich Drepper 32371da177e4SLinus Torvalds /* 32381da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 32391da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 32401da177e4SLinus Torvalds * Problems: 32411da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 32421da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 32431da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 3244a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 32451da177e4SLinus Torvalds * story. 32461da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 32471b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 32481da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 32491da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 3250a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 32511da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 32521da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 32531da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 3254a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 32551da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 32561da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 32571da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 32581da177e4SLinus Torvalds * d) some filesystems don't support opened-but-unlinked directories, 32591da177e4SLinus Torvalds * either because of layout or because they are not ready to deal with 32601da177e4SLinus Torvalds * all cases correctly. The latter will be fixed (taking this sort of 32611da177e4SLinus Torvalds * stuff into VFS), but the former is not going away. Solution: the same 32621da177e4SLinus Torvalds * trick as in rmdir(). 32631da177e4SLinus Torvalds * e) conversion from fhandle to dentry may come in the wrong moment - when 32641b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 32651da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 3266c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 32671da177e4SLinus Torvalds * locking]. 32681da177e4SLinus Torvalds */ 326975c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 32701da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 32711da177e4SLinus Torvalds { 32721da177e4SLinus Torvalds int error = 0; 32731da177e4SLinus Torvalds struct inode *target; 32741da177e4SLinus Torvalds 32751da177e4SLinus Torvalds /* 32761da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 32771da177e4SLinus Torvalds * we'll need to flip '..'. 32781da177e4SLinus Torvalds */ 32791da177e4SLinus Torvalds if (new_dir != old_dir) { 3280f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 32811da177e4SLinus Torvalds if (error) 32821da177e4SLinus Torvalds return error; 32831da177e4SLinus Torvalds } 32841da177e4SLinus Torvalds 32851da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 32861da177e4SLinus Torvalds if (error) 32871da177e4SLinus Torvalds return error; 32881da177e4SLinus Torvalds 32891da177e4SLinus Torvalds target = new_dentry->d_inode; 3290d83c49f3SAl Viro if (target) 32911b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 32921da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 32931da177e4SLinus Torvalds error = -EBUSY; 3294d83c49f3SAl Viro else { 3295d83c49f3SAl Viro if (target) 3296d83c49f3SAl Viro dentry_unhash(new_dentry); 32971da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 3298d83c49f3SAl Viro } 32991da177e4SLinus Torvalds if (target) { 3300d83c49f3SAl Viro if (!error) { 33011da177e4SLinus Torvalds target->i_flags |= S_DEAD; 3302d83c49f3SAl Viro dont_mount(new_dentry); 3303d83c49f3SAl Viro } 33041b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 33051da177e4SLinus Torvalds if (d_unhashed(new_dentry)) 33061da177e4SLinus Torvalds d_rehash(new_dentry); 33071da177e4SLinus Torvalds dput(new_dentry); 33081da177e4SLinus Torvalds } 3309e31e14ecSStephen Smalley if (!error) 3310349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 33111da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 33121da177e4SLinus Torvalds return error; 33131da177e4SLinus Torvalds } 33141da177e4SLinus Torvalds 331575c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 33161da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 33171da177e4SLinus Torvalds { 33181da177e4SLinus Torvalds struct inode *target; 33191da177e4SLinus Torvalds int error; 33201da177e4SLinus Torvalds 33211da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 33221da177e4SLinus Torvalds if (error) 33231da177e4SLinus Torvalds return error; 33241da177e4SLinus Torvalds 33251da177e4SLinus Torvalds dget(new_dentry); 33261da177e4SLinus Torvalds target = new_dentry->d_inode; 33271da177e4SLinus Torvalds if (target) 33281b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 33291da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 33301da177e4SLinus Torvalds error = -EBUSY; 33311da177e4SLinus Torvalds else 33321da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 33331da177e4SLinus Torvalds if (!error) { 3334bec1052eSAl Viro if (target) 3335d83c49f3SAl Viro dont_mount(new_dentry); 3336349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 33371da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 33381da177e4SLinus Torvalds } 33391da177e4SLinus Torvalds if (target) 33401b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 33411da177e4SLinus Torvalds dput(new_dentry); 33421da177e4SLinus Torvalds return error; 33431da177e4SLinus Torvalds } 33441da177e4SLinus Torvalds 33451da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 33461da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 33471da177e4SLinus Torvalds { 33481da177e4SLinus Torvalds int error; 33491da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 335059b0df21SEric Paris const unsigned char *old_name; 33511da177e4SLinus Torvalds 33521da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 33531da177e4SLinus Torvalds return 0; 33541da177e4SLinus Torvalds 33551da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 33561da177e4SLinus Torvalds if (error) 33571da177e4SLinus Torvalds return error; 33581da177e4SLinus Torvalds 33591da177e4SLinus Torvalds if (!new_dentry->d_inode) 3360a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 33611da177e4SLinus Torvalds else 33621da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 33631da177e4SLinus Torvalds if (error) 33641da177e4SLinus Torvalds return error; 33651da177e4SLinus Torvalds 3366acfa4380SAl Viro if (!old_dir->i_op->rename) 33671da177e4SLinus Torvalds return -EPERM; 33681da177e4SLinus Torvalds 33690eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 33700eeca283SRobert Love 33711da177e4SLinus Torvalds if (is_dir) 33721da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 33731da177e4SLinus Torvalds else 33741da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3375123df294SAl Viro if (!error) 3376123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 33775a190ae6SAl Viro new_dentry->d_inode, old_dentry); 33780eeca283SRobert Love fsnotify_oldname_free(old_name); 33790eeca283SRobert Love 33801da177e4SLinus Torvalds return error; 33811da177e4SLinus Torvalds } 33821da177e4SLinus Torvalds 33832e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 33842e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 33851da177e4SLinus Torvalds { 33861da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 33871da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 33881da177e4SLinus Torvalds struct dentry *trap; 33891da177e4SLinus Torvalds struct nameidata oldnd, newnd; 33902ad94ae6SAl Viro char *from; 33912ad94ae6SAl Viro char *to; 33922ad94ae6SAl Viro int error; 33931da177e4SLinus Torvalds 33942ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 33951da177e4SLinus Torvalds if (error) 33961da177e4SLinus Torvalds goto exit; 33971da177e4SLinus Torvalds 33982ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 33991da177e4SLinus Torvalds if (error) 34001da177e4SLinus Torvalds goto exit1; 34011da177e4SLinus Torvalds 34021da177e4SLinus Torvalds error = -EXDEV; 34034ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 34041da177e4SLinus Torvalds goto exit2; 34051da177e4SLinus Torvalds 34064ac91378SJan Blunck old_dir = oldnd.path.dentry; 34071da177e4SLinus Torvalds error = -EBUSY; 34081da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 34091da177e4SLinus Torvalds goto exit2; 34101da177e4SLinus Torvalds 34114ac91378SJan Blunck new_dir = newnd.path.dentry; 34121da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 34131da177e4SLinus Torvalds goto exit2; 34141da177e4SLinus Torvalds 34150612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 34160612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 34174e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 34180612d9fbSOGAWA Hirofumi 34191da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 34201da177e4SLinus Torvalds 342149705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 34221da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 34231da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 34241da177e4SLinus Torvalds goto exit3; 34251da177e4SLinus Torvalds /* source must exist */ 34261da177e4SLinus Torvalds error = -ENOENT; 34271da177e4SLinus Torvalds if (!old_dentry->d_inode) 34281da177e4SLinus Torvalds goto exit4; 34291da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 34301da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 34311da177e4SLinus Torvalds error = -ENOTDIR; 34321da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 34331da177e4SLinus Torvalds goto exit4; 34341da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 34351da177e4SLinus Torvalds goto exit4; 34361da177e4SLinus Torvalds } 34371da177e4SLinus Torvalds /* source should not be ancestor of target */ 34381da177e4SLinus Torvalds error = -EINVAL; 34391da177e4SLinus Torvalds if (old_dentry == trap) 34401da177e4SLinus Torvalds goto exit4; 344149705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 34421da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 34431da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 34441da177e4SLinus Torvalds goto exit4; 34451da177e4SLinus Torvalds /* target should not be an ancestor of source */ 34461da177e4SLinus Torvalds error = -ENOTEMPTY; 34471da177e4SLinus Torvalds if (new_dentry == trap) 34481da177e4SLinus Torvalds goto exit5; 34491da177e4SLinus Torvalds 34509079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 34519079b1ebSDave Hansen if (error) 34529079b1ebSDave Hansen goto exit5; 3453be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3454be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3455be6d3e56SKentaro Takeda if (error) 3456be6d3e56SKentaro Takeda goto exit6; 34571da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 34581da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 3459be6d3e56SKentaro Takeda exit6: 34609079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 34611da177e4SLinus Torvalds exit5: 34621da177e4SLinus Torvalds dput(new_dentry); 34631da177e4SLinus Torvalds exit4: 34641da177e4SLinus Torvalds dput(old_dentry); 34651da177e4SLinus Torvalds exit3: 34661da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 34671da177e4SLinus Torvalds exit2: 34681d957f9bSJan Blunck path_put(&newnd.path); 34692ad94ae6SAl Viro putname(to); 34701da177e4SLinus Torvalds exit1: 34711d957f9bSJan Blunck path_put(&oldnd.path); 34721da177e4SLinus Torvalds putname(from); 34732ad94ae6SAl Viro exit: 34741da177e4SLinus Torvalds return error; 34751da177e4SLinus Torvalds } 34761da177e4SLinus Torvalds 3477a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 34785590ff0dSUlrich Drepper { 34795590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 34805590ff0dSUlrich Drepper } 34815590ff0dSUlrich Drepper 34821da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 34831da177e4SLinus Torvalds { 34841da177e4SLinus Torvalds int len; 34851da177e4SLinus Torvalds 34861da177e4SLinus Torvalds len = PTR_ERR(link); 34871da177e4SLinus Torvalds if (IS_ERR(link)) 34881da177e4SLinus Torvalds goto out; 34891da177e4SLinus Torvalds 34901da177e4SLinus Torvalds len = strlen(link); 34911da177e4SLinus Torvalds if (len > (unsigned) buflen) 34921da177e4SLinus Torvalds len = buflen; 34931da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 34941da177e4SLinus Torvalds len = -EFAULT; 34951da177e4SLinus Torvalds out: 34961da177e4SLinus Torvalds return len; 34971da177e4SLinus Torvalds } 34981da177e4SLinus Torvalds 34991da177e4SLinus Torvalds /* 35001da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 35011da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 35021da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 35031da177e4SLinus Torvalds */ 35041da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 35051da177e4SLinus Torvalds { 35061da177e4SLinus Torvalds struct nameidata nd; 3507cc314eefSLinus Torvalds void *cookie; 3508694a1764SMarcin Slusarz int res; 3509cc314eefSLinus Torvalds 35101da177e4SLinus Torvalds nd.depth = 0; 3511cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3512694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3513694a1764SMarcin Slusarz return PTR_ERR(cookie); 3514694a1764SMarcin Slusarz 3515694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 35161da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3517cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3518694a1764SMarcin Slusarz return res; 35191da177e4SLinus Torvalds } 35201da177e4SLinus Torvalds 35211da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 35221da177e4SLinus Torvalds { 35231da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 35241da177e4SLinus Torvalds } 35251da177e4SLinus Torvalds 35261da177e4SLinus Torvalds /* get the link contents into pagecache */ 35271da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 35281da177e4SLinus Torvalds { 3529ebd09abbSDuane Griffin char *kaddr; 35301da177e4SLinus Torvalds struct page *page; 35311da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3532090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 35331da177e4SLinus Torvalds if (IS_ERR(page)) 35346fe6900eSNick Piggin return (char*)page; 35351da177e4SLinus Torvalds *ppage = page; 3536ebd09abbSDuane Griffin kaddr = kmap(page); 3537ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3538ebd09abbSDuane Griffin return kaddr; 35391da177e4SLinus Torvalds } 35401da177e4SLinus Torvalds 35411da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 35421da177e4SLinus Torvalds { 35431da177e4SLinus Torvalds struct page *page = NULL; 35441da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 35451da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 35461da177e4SLinus Torvalds if (page) { 35471da177e4SLinus Torvalds kunmap(page); 35481da177e4SLinus Torvalds page_cache_release(page); 35491da177e4SLinus Torvalds } 35501da177e4SLinus Torvalds return res; 35511da177e4SLinus Torvalds } 35521da177e4SLinus Torvalds 3553cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 35541da177e4SLinus Torvalds { 3555cc314eefSLinus Torvalds struct page *page = NULL; 35561da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 3557cc314eefSLinus Torvalds return page; 35581da177e4SLinus Torvalds } 35591da177e4SLinus Torvalds 3560cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 35611da177e4SLinus Torvalds { 3562cc314eefSLinus Torvalds struct page *page = cookie; 3563cc314eefSLinus Torvalds 3564cc314eefSLinus Torvalds if (page) { 35651da177e4SLinus Torvalds kunmap(page); 35661da177e4SLinus Torvalds page_cache_release(page); 35671da177e4SLinus Torvalds } 35681da177e4SLinus Torvalds } 35691da177e4SLinus Torvalds 357054566b2cSNick Piggin /* 357154566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 357254566b2cSNick Piggin */ 357354566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 35741da177e4SLinus Torvalds { 35751da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 35760adb25d2SKirill Korotaev struct page *page; 3577afddba49SNick Piggin void *fsdata; 3578beb497abSDmitriy Monakhov int err; 35791da177e4SLinus Torvalds char *kaddr; 358054566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 358154566b2cSNick Piggin if (nofs) 358254566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 35831da177e4SLinus Torvalds 35847e53cac4SNeilBrown retry: 3585afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 358654566b2cSNick Piggin flags, &page, &fsdata); 35871da177e4SLinus Torvalds if (err) 3588afddba49SNick Piggin goto fail; 3589afddba49SNick Piggin 35901da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 35911da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 35921da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 3593afddba49SNick Piggin 3594afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 3595afddba49SNick Piggin page, fsdata); 35961da177e4SLinus Torvalds if (err < 0) 35971da177e4SLinus Torvalds goto fail; 3598afddba49SNick Piggin if (err < len-1) 3599afddba49SNick Piggin goto retry; 3600afddba49SNick Piggin 36011da177e4SLinus Torvalds mark_inode_dirty(inode); 36021da177e4SLinus Torvalds return 0; 36031da177e4SLinus Torvalds fail: 36041da177e4SLinus Torvalds return err; 36051da177e4SLinus Torvalds } 36061da177e4SLinus Torvalds 36070adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 36080adb25d2SKirill Korotaev { 36090adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 361054566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 36110adb25d2SKirill Korotaev } 36120adb25d2SKirill Korotaev 361392e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 36141da177e4SLinus Torvalds .readlink = generic_readlink, 36151da177e4SLinus Torvalds .follow_link = page_follow_link_light, 36161da177e4SLinus Torvalds .put_link = page_put_link, 36171da177e4SLinus Torvalds }; 36181da177e4SLinus Torvalds 36192d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 3620cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one); 36211da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 36221da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 36231da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 36241da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 36251da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 36261da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 36271da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 36281da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 36291da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 36300adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 36311da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 36321da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 36331da177e4SLinus Torvalds EXPORT_SYMBOL(path_lookup); 3634d1811465SAl Viro EXPORT_SYMBOL(kern_path); 363516f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 3636f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 36378c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission); 36381da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 36391da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 36401da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 36411da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 36421da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 36431da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 36441da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 36451da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 36461da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 36471da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 36481da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 36491da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 36501da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 36511da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 3652