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 /** 3711d957f9bSJan Blunck * path_put - put a reference to a path 3721d957f9bSJan Blunck * @path: path to put the reference to 3731d957f9bSJan Blunck * 3741d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 3751d957f9bSJan Blunck */ 3761d957f9bSJan Blunck void path_put(struct path *path) 3771da177e4SLinus Torvalds { 3781d957f9bSJan Blunck dput(path->dentry); 3791d957f9bSJan Blunck mntput(path->mnt); 3801da177e4SLinus Torvalds } 3811d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 3821da177e4SLinus Torvalds 383834f2a4aSTrond Myklebust /** 38431e6b01fSNick Piggin * nameidata_drop_rcu - drop this nameidata out of rcu-walk 38531e6b01fSNick Piggin * @nd: nameidata pathwalk data to drop 38639191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 38731e6b01fSNick Piggin * 38831e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 38931e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). __drop_rcu* functions attempt 39031e6b01fSNick Piggin * to drop out of rcu-walk mode and take normal reference counts on dentries 39131e6b01fSNick Piggin * and vfsmounts to transition to rcu-walk mode. __drop_rcu* functions take 39231e6b01fSNick Piggin * refcounts at the last known good point before rcu-walk got stuck, so 39331e6b01fSNick Piggin * ref-walk may continue from there. If this is not successful (eg. a seqcount 39431e6b01fSNick Piggin * has changed), then failure is returned and path walk restarts from the 39531e6b01fSNick Piggin * beginning in ref-walk mode. 39631e6b01fSNick Piggin * 39731e6b01fSNick Piggin * nameidata_drop_rcu attempts to drop the current nd->path and nd->root into 39831e6b01fSNick Piggin * ref-walk. Must be called from rcu-walk context. 39931e6b01fSNick Piggin */ 40031e6b01fSNick Piggin static int nameidata_drop_rcu(struct nameidata *nd) 40131e6b01fSNick Piggin { 40231e6b01fSNick Piggin struct fs_struct *fs = current->fs; 40331e6b01fSNick Piggin struct dentry *dentry = nd->path.dentry; 40431e6b01fSNick Piggin 40531e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 40631e6b01fSNick Piggin if (nd->root.mnt) { 40731e6b01fSNick Piggin spin_lock(&fs->lock); 40831e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 40931e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 41031e6b01fSNick Piggin goto err_root; 41131e6b01fSNick Piggin } 41231e6b01fSNick Piggin spin_lock(&dentry->d_lock); 41331e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 41431e6b01fSNick Piggin goto err; 41531e6b01fSNick Piggin BUG_ON(nd->inode != dentry->d_inode); 41631e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 41731e6b01fSNick Piggin if (nd->root.mnt) { 41831e6b01fSNick Piggin path_get(&nd->root); 41931e6b01fSNick Piggin spin_unlock(&fs->lock); 42031e6b01fSNick Piggin } 42131e6b01fSNick Piggin mntget(nd->path.mnt); 42231e6b01fSNick Piggin 42331e6b01fSNick Piggin rcu_read_unlock(); 42431e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 42531e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 42631e6b01fSNick Piggin return 0; 42731e6b01fSNick Piggin err: 42831e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 42931e6b01fSNick Piggin err_root: 43031e6b01fSNick Piggin if (nd->root.mnt) 43131e6b01fSNick Piggin spin_unlock(&fs->lock); 43231e6b01fSNick Piggin return -ECHILD; 43331e6b01fSNick Piggin } 43431e6b01fSNick Piggin 43531e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */ 43631e6b01fSNick Piggin static inline int nameidata_drop_rcu_maybe(struct nameidata *nd) 43731e6b01fSNick Piggin { 43831e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) 43931e6b01fSNick Piggin return nameidata_drop_rcu(nd); 44031e6b01fSNick Piggin return 0; 44131e6b01fSNick Piggin } 44231e6b01fSNick Piggin 44331e6b01fSNick Piggin /** 44431e6b01fSNick Piggin * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk 44531e6b01fSNick Piggin * @nd: nameidata pathwalk data to drop 44631e6b01fSNick Piggin * @dentry: dentry to drop 44739191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 44831e6b01fSNick Piggin * 44931e6b01fSNick Piggin * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root, 45031e6b01fSNick Piggin * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on 45131e6b01fSNick Piggin * @nd. Must be called from rcu-walk context. 45231e6b01fSNick Piggin */ 45331e6b01fSNick Piggin static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry) 45431e6b01fSNick Piggin { 45531e6b01fSNick Piggin struct fs_struct *fs = current->fs; 45631e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 45731e6b01fSNick Piggin 45890dbb77bSNick Piggin /* 45990dbb77bSNick Piggin * It can be possible to revalidate the dentry that we started 46090dbb77bSNick Piggin * the path walk with. force_reval_path may also revalidate the 46190dbb77bSNick Piggin * dentry already committed to the nameidata. 46290dbb77bSNick Piggin */ 46390dbb77bSNick Piggin if (unlikely(parent == dentry)) 46490dbb77bSNick Piggin return nameidata_drop_rcu(nd); 46590dbb77bSNick Piggin 46631e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 46731e6b01fSNick Piggin if (nd->root.mnt) { 46831e6b01fSNick Piggin spin_lock(&fs->lock); 46931e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 47031e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 47131e6b01fSNick Piggin goto err_root; 47231e6b01fSNick Piggin } 47331e6b01fSNick Piggin spin_lock(&parent->d_lock); 47431e6b01fSNick Piggin spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); 47531e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 47631e6b01fSNick Piggin goto err; 47731e6b01fSNick Piggin /* 47831e6b01fSNick Piggin * If the sequence check on the child dentry passed, then the child has 47931e6b01fSNick Piggin * not been removed from its parent. This means the parent dentry must 48031e6b01fSNick Piggin * be valid and able to take a reference at this point. 48131e6b01fSNick Piggin */ 48231e6b01fSNick Piggin BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); 48331e6b01fSNick Piggin BUG_ON(!parent->d_count); 48431e6b01fSNick Piggin parent->d_count++; 48531e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 48631e6b01fSNick Piggin spin_unlock(&parent->d_lock); 48731e6b01fSNick Piggin if (nd->root.mnt) { 48831e6b01fSNick Piggin path_get(&nd->root); 48931e6b01fSNick Piggin spin_unlock(&fs->lock); 49031e6b01fSNick Piggin } 49131e6b01fSNick Piggin mntget(nd->path.mnt); 49231e6b01fSNick Piggin 49331e6b01fSNick Piggin rcu_read_unlock(); 49431e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 49531e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 49631e6b01fSNick Piggin return 0; 49731e6b01fSNick Piggin err: 49831e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 49931e6b01fSNick Piggin spin_unlock(&parent->d_lock); 50031e6b01fSNick Piggin err_root: 50131e6b01fSNick Piggin if (nd->root.mnt) 50231e6b01fSNick Piggin spin_unlock(&fs->lock); 50331e6b01fSNick Piggin return -ECHILD; 50431e6b01fSNick Piggin } 50531e6b01fSNick Piggin 50631e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */ 50731e6b01fSNick Piggin static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry) 50831e6b01fSNick Piggin { 50931e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) 51031e6b01fSNick Piggin return nameidata_dentry_drop_rcu(nd, dentry); 51131e6b01fSNick Piggin return 0; 51231e6b01fSNick Piggin } 51331e6b01fSNick Piggin 51431e6b01fSNick Piggin /** 51531e6b01fSNick Piggin * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk 51631e6b01fSNick Piggin * @nd: nameidata pathwalk data to drop 51739191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 51831e6b01fSNick Piggin * 51931e6b01fSNick Piggin * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk. 52031e6b01fSNick Piggin * nd->path should be the final element of the lookup, so nd->root is discarded. 52131e6b01fSNick Piggin * Must be called from rcu-walk context. 52231e6b01fSNick Piggin */ 52331e6b01fSNick Piggin static int nameidata_drop_rcu_last(struct nameidata *nd) 52431e6b01fSNick Piggin { 52531e6b01fSNick Piggin struct dentry *dentry = nd->path.dentry; 52631e6b01fSNick Piggin 52731e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 52831e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 52931e6b01fSNick Piggin nd->root.mnt = NULL; 53031e6b01fSNick Piggin spin_lock(&dentry->d_lock); 53131e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 53231e6b01fSNick Piggin goto err_unlock; 53331e6b01fSNick Piggin BUG_ON(nd->inode != dentry->d_inode); 53431e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 53531e6b01fSNick Piggin 53631e6b01fSNick Piggin mntget(nd->path.mnt); 53731e6b01fSNick Piggin 53831e6b01fSNick Piggin rcu_read_unlock(); 53931e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 54031e6b01fSNick Piggin 54131e6b01fSNick Piggin return 0; 54231e6b01fSNick Piggin 54331e6b01fSNick Piggin err_unlock: 54431e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 54531e6b01fSNick Piggin rcu_read_unlock(); 54631e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 54731e6b01fSNick Piggin return -ECHILD; 54831e6b01fSNick Piggin } 54931e6b01fSNick Piggin 55031e6b01fSNick Piggin /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */ 55131e6b01fSNick Piggin static inline int nameidata_drop_rcu_last_maybe(struct nameidata *nd) 55231e6b01fSNick Piggin { 55331e6b01fSNick Piggin if (likely(nd->flags & LOOKUP_RCU)) 55431e6b01fSNick Piggin return nameidata_drop_rcu_last(nd); 55531e6b01fSNick Piggin return 0; 55631e6b01fSNick Piggin } 55731e6b01fSNick Piggin 55831e6b01fSNick Piggin /** 559834f2a4aSTrond Myklebust * release_open_intent - free up open intent resources 560834f2a4aSTrond Myklebust * @nd: pointer to nameidata 561834f2a4aSTrond Myklebust */ 562834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd) 563834f2a4aSTrond Myklebust { 5640f7fc9e4SJosef "Jeff" Sipek if (nd->intent.open.file->f_path.dentry == NULL) 565834f2a4aSTrond Myklebust put_filp(nd->intent.open.file); 566834f2a4aSTrond Myklebust else 567834f2a4aSTrond Myklebust fput(nd->intent.open.file); 568834f2a4aSTrond Myklebust } 569834f2a4aSTrond Myklebust 570bb20c18dSNick Piggin /* 571bb20c18dSNick Piggin * Call d_revalidate and handle filesystems that request rcu-walk 572bb20c18dSNick Piggin * to be dropped. This may be called and return in rcu-walk mode, 573bb20c18dSNick Piggin * regardless of success or error. If -ECHILD is returned, the caller 574bb20c18dSNick Piggin * must return -ECHILD back up the path walk stack so path walk may 575bb20c18dSNick Piggin * be restarted in ref-walk mode. 576bb20c18dSNick Piggin */ 57734286d66SNick Piggin static int d_revalidate(struct dentry *dentry, struct nameidata *nd) 57834286d66SNick Piggin { 57934286d66SNick Piggin int status; 58034286d66SNick Piggin 58134286d66SNick Piggin status = dentry->d_op->d_revalidate(dentry, nd); 58234286d66SNick Piggin if (status == -ECHILD) { 58334286d66SNick Piggin if (nameidata_dentry_drop_rcu(nd, dentry)) 58434286d66SNick Piggin return status; 58534286d66SNick Piggin status = dentry->d_op->d_revalidate(dentry, nd); 58634286d66SNick Piggin } 58734286d66SNick Piggin 58834286d66SNick Piggin return status; 58934286d66SNick Piggin } 59034286d66SNick Piggin 591bcdc5e01SIan Kent static inline struct dentry * 592bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd) 593bcdc5e01SIan Kent { 59434286d66SNick Piggin int status; 59534286d66SNick Piggin 59634286d66SNick Piggin status = d_revalidate(dentry, nd); 597bcdc5e01SIan Kent if (unlikely(status <= 0)) { 598bcdc5e01SIan Kent /* 599bcdc5e01SIan Kent * The dentry failed validation. 600bcdc5e01SIan Kent * If d_revalidate returned 0 attempt to invalidate 601bcdc5e01SIan Kent * the dentry otherwise d_revalidate is asking us 602bcdc5e01SIan Kent * to return a fail status. 603bcdc5e01SIan Kent */ 60434286d66SNick Piggin if (status < 0) { 60534286d66SNick Piggin /* If we're in rcu-walk, we don't have a ref */ 60634286d66SNick Piggin if (!(nd->flags & LOOKUP_RCU)) 60734286d66SNick Piggin dput(dentry); 60834286d66SNick Piggin dentry = ERR_PTR(status); 60934286d66SNick Piggin 61034286d66SNick Piggin } else { 61134286d66SNick Piggin /* Don't d_invalidate in rcu-walk mode */ 61234286d66SNick Piggin if (nameidata_dentry_drop_rcu_maybe(nd, dentry)) 61334286d66SNick Piggin return ERR_PTR(-ECHILD); 614bcdc5e01SIan Kent if (!d_invalidate(dentry)) { 615bcdc5e01SIan Kent dput(dentry); 616bcdc5e01SIan Kent dentry = NULL; 617bcdc5e01SIan Kent } 618bcdc5e01SIan Kent } 619bcdc5e01SIan Kent } 620bcdc5e01SIan Kent return dentry; 621bcdc5e01SIan Kent } 622bcdc5e01SIan Kent 623fb045adbSNick Piggin static inline int need_reval_dot(struct dentry *dentry) 624fb045adbSNick Piggin { 625fb045adbSNick Piggin if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE))) 626fb045adbSNick Piggin return 0; 627fb045adbSNick Piggin 628fb045adbSNick Piggin if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))) 629fb045adbSNick Piggin return 0; 630fb045adbSNick Piggin 631fb045adbSNick Piggin return 1; 632fb045adbSNick Piggin } 633fb045adbSNick Piggin 6341da177e4SLinus Torvalds /* 63539159de2SJeff Layton * force_reval_path - force revalidation of a dentry 63639159de2SJeff Layton * 63739159de2SJeff Layton * In some situations the path walking code will trust dentries without 63839159de2SJeff Layton * revalidating them. This causes problems for filesystems that depend on 63939159de2SJeff Layton * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set 64039159de2SJeff Layton * (which indicates that it's possible for the dentry to go stale), force 64139159de2SJeff Layton * a d_revalidate call before proceeding. 64239159de2SJeff Layton * 64339159de2SJeff Layton * Returns 0 if the revalidation was successful. If the revalidation fails, 64439159de2SJeff Layton * either return the error returned by d_revalidate or -ESTALE if the 64539159de2SJeff Layton * revalidation it just returned 0. If d_revalidate returns 0, we attempt to 64639159de2SJeff Layton * invalidate the dentry. It's up to the caller to handle putting references 64739159de2SJeff Layton * to the path if necessary. 64839159de2SJeff Layton */ 64939159de2SJeff Layton static int 65039159de2SJeff Layton force_reval_path(struct path *path, struct nameidata *nd) 65139159de2SJeff Layton { 65239159de2SJeff Layton int status; 65339159de2SJeff Layton struct dentry *dentry = path->dentry; 65439159de2SJeff Layton 65539159de2SJeff Layton /* 65639159de2SJeff Layton * only check on filesystems where it's possible for the dentry to 657fb045adbSNick Piggin * become stale. 65839159de2SJeff Layton */ 659fb045adbSNick Piggin if (!need_reval_dot(dentry)) 66039159de2SJeff Layton return 0; 66139159de2SJeff Layton 66234286d66SNick Piggin status = d_revalidate(dentry, nd); 66339159de2SJeff Layton if (status > 0) 66439159de2SJeff Layton return 0; 66539159de2SJeff Layton 66639159de2SJeff Layton if (!status) { 667bb20c18dSNick Piggin /* Don't d_invalidate in rcu-walk mode */ 668bb20c18dSNick Piggin if (nameidata_drop_rcu(nd)) 669bb20c18dSNick Piggin return -ECHILD; 67039159de2SJeff Layton d_invalidate(dentry); 67139159de2SJeff Layton status = -ESTALE; 67239159de2SJeff Layton } 67339159de2SJeff Layton return status; 67439159de2SJeff Layton } 67539159de2SJeff Layton 67639159de2SJeff Layton /* 677b75b5086SAl Viro * Short-cut version of permission(), for calling on directories 678b75b5086SAl Viro * during pathname resolution. Combines parts of permission() 679b75b5086SAl Viro * and generic_permission(), and tests ONLY for MAY_EXEC permission. 6801da177e4SLinus Torvalds * 6811da177e4SLinus Torvalds * If appropriate, check DAC only. If not appropriate, or 682b75b5086SAl Viro * short-cut DAC fails, then call ->permission() to do more 6831da177e4SLinus Torvalds * complete permission check. 6841da177e4SLinus Torvalds */ 685b74c79e9SNick Piggin static inline int exec_permission(struct inode *inode, unsigned int flags) 6861da177e4SLinus Torvalds { 6875909ccaaSLinus Torvalds int ret; 6881da177e4SLinus Torvalds 689cb9179eaSLinus Torvalds if (inode->i_op->permission) { 690b74c79e9SNick Piggin ret = inode->i_op->permission(inode, MAY_EXEC, flags); 691b74c79e9SNick Piggin } else { 692b74c79e9SNick Piggin ret = acl_permission_check(inode, MAY_EXEC, flags, 693b74c79e9SNick Piggin inode->i_op->check_acl); 694cb9179eaSLinus Torvalds } 695b74c79e9SNick Piggin if (likely(!ret)) 6961da177e4SLinus Torvalds goto ok; 697b74c79e9SNick Piggin if (ret == -ECHILD) 69831e6b01fSNick Piggin return ret; 6991da177e4SLinus Torvalds 700f1ac9f6bSLinus Torvalds if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH)) 7011da177e4SLinus Torvalds goto ok; 7021da177e4SLinus Torvalds 7035909ccaaSLinus Torvalds return ret; 7041da177e4SLinus Torvalds ok: 705b74c79e9SNick Piggin return security_inode_exec_permission(inode, flags); 7061da177e4SLinus Torvalds } 7071da177e4SLinus Torvalds 7082a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 7092a737871SAl Viro { 710f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 711f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 7122a737871SAl Viro } 7132a737871SAl Viro 7146de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 7156de88d72SAl Viro 71631e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 71731e6b01fSNick Piggin { 71831e6b01fSNick Piggin if (!nd->root.mnt) { 71931e6b01fSNick Piggin struct fs_struct *fs = current->fs; 720c28cc364SNick Piggin unsigned seq; 721c28cc364SNick Piggin 722c28cc364SNick Piggin do { 723c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 72431e6b01fSNick Piggin nd->root = fs->root; 725c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 72631e6b01fSNick Piggin } 72731e6b01fSNick Piggin } 72831e6b01fSNick Piggin 729f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 7301da177e4SLinus Torvalds { 73131e6b01fSNick Piggin int ret; 73231e6b01fSNick Piggin 7331da177e4SLinus Torvalds if (IS_ERR(link)) 7341da177e4SLinus Torvalds goto fail; 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds if (*link == '/') { 7372a737871SAl Viro set_root(nd); 7381d957f9bSJan Blunck path_put(&nd->path); 7392a737871SAl Viro nd->path = nd->root; 7402a737871SAl Viro path_get(&nd->root); 7411da177e4SLinus Torvalds } 74231e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 743b4091d5fSChristoph Hellwig 74431e6b01fSNick Piggin ret = link_path_walk(link, nd); 74531e6b01fSNick Piggin return ret; 7461da177e4SLinus Torvalds fail: 7471d957f9bSJan Blunck path_put(&nd->path); 7481da177e4SLinus Torvalds return PTR_ERR(link); 7491da177e4SLinus Torvalds } 7501da177e4SLinus Torvalds 7511d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 752051d3812SIan Kent { 753051d3812SIan Kent dput(path->dentry); 7544ac91378SJan Blunck if (path->mnt != nd->path.mnt) 755051d3812SIan Kent mntput(path->mnt); 756051d3812SIan Kent } 757051d3812SIan Kent 7587b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path, 7597b9337aaSNick Piggin struct nameidata *nd) 760051d3812SIan Kent { 76131e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 7624ac91378SJan Blunck dput(nd->path.dentry); 76331e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 7644ac91378SJan Blunck mntput(nd->path.mnt); 7659a229683SHuang Shijie } 76631e6b01fSNick Piggin nd->path.mnt = path->mnt; 7674ac91378SJan Blunck nd->path.dentry = path->dentry; 768051d3812SIan Kent } 769051d3812SIan Kent 770def4af30SAl Viro static __always_inline int 7717b9337aaSNick Piggin __do_follow_link(const struct path *link, struct nameidata *nd, void **p) 7721da177e4SLinus Torvalds { 7731da177e4SLinus Torvalds int error; 7747b9337aaSNick Piggin struct dentry *dentry = link->dentry; 7751da177e4SLinus Torvalds 7767b9337aaSNick Piggin touch_atime(link->mnt, dentry); 7771da177e4SLinus Torvalds nd_set_link(nd, NULL); 778cd4e91d3SAl Viro 77987556ef1SDavid Howells if (link->mnt == nd->path.mnt) 7807b9337aaSNick Piggin mntget(link->mnt); 78131e6b01fSNick Piggin 78286acdca1SAl Viro nd->last_type = LAST_BIND; 783def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 784def4af30SAl Viro error = PTR_ERR(*p); 785def4af30SAl Viro if (!IS_ERR(*p)) { 7861da177e4SLinus Torvalds char *s = nd_get_link(nd); 787cc314eefSLinus Torvalds error = 0; 7881da177e4SLinus Torvalds if (s) 7891da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 79039159de2SJeff Layton else if (nd->last_type == LAST_BIND) { 79139159de2SJeff Layton error = force_reval_path(&nd->path, nd); 79239159de2SJeff Layton if (error) 79339159de2SJeff Layton path_put(&nd->path); 79439159de2SJeff Layton } 7951da177e4SLinus Torvalds } 7961da177e4SLinus Torvalds return error; 7971da177e4SLinus Torvalds } 7981da177e4SLinus Torvalds 7991da177e4SLinus Torvalds /* 8001da177e4SLinus Torvalds * This limits recursive symlink follows to 8, while 8011da177e4SLinus Torvalds * limiting consecutive symlinks to 40. 8021da177e4SLinus Torvalds * 8031da177e4SLinus Torvalds * Without that kind of total limit, nasty chains of consecutive 8041da177e4SLinus Torvalds * symlinks can cause almost arbitrarily long lookups. 8051da177e4SLinus Torvalds */ 80690ebe565SAl Viro static inline int do_follow_link(struct path *path, struct nameidata *nd) 8071da177e4SLinus Torvalds { 808def4af30SAl Viro void *cookie; 8091da177e4SLinus Torvalds int err = -ELOOP; 8101da177e4SLinus Torvalds if (current->link_count >= MAX_NESTED_LINKS) 8111da177e4SLinus Torvalds goto loop; 8121da177e4SLinus Torvalds if (current->total_link_count >= 40) 8131da177e4SLinus Torvalds goto loop; 8141da177e4SLinus Torvalds BUG_ON(nd->depth >= MAX_NESTED_LINKS); 8151da177e4SLinus Torvalds cond_resched(); 81690ebe565SAl Viro err = security_inode_follow_link(path->dentry, nd); 8171da177e4SLinus Torvalds if (err) 8181da177e4SLinus Torvalds goto loop; 8191da177e4SLinus Torvalds current->link_count++; 8201da177e4SLinus Torvalds current->total_link_count++; 8211da177e4SLinus Torvalds nd->depth++; 822def4af30SAl Viro err = __do_follow_link(path, nd, &cookie); 823def4af30SAl Viro if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link) 824def4af30SAl Viro path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie); 825258fa999SAl Viro path_put(path); 8261da177e4SLinus Torvalds current->link_count--; 8271da177e4SLinus Torvalds nd->depth--; 8281da177e4SLinus Torvalds return err; 8291da177e4SLinus Torvalds loop: 8301d957f9bSJan Blunck path_put_conditional(path, nd); 8311d957f9bSJan Blunck path_put(&nd->path); 8321da177e4SLinus Torvalds return err; 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds 83531e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 83631e6b01fSNick Piggin { 83731e6b01fSNick Piggin struct vfsmount *parent; 83831e6b01fSNick Piggin struct dentry *mountpoint; 83931e6b01fSNick Piggin 84031e6b01fSNick Piggin parent = path->mnt->mnt_parent; 84131e6b01fSNick Piggin if (parent == path->mnt) 84231e6b01fSNick Piggin return 0; 84331e6b01fSNick Piggin mountpoint = path->mnt->mnt_mountpoint; 84431e6b01fSNick Piggin path->dentry = mountpoint; 84531e6b01fSNick Piggin path->mnt = parent; 84631e6b01fSNick Piggin return 1; 84731e6b01fSNick Piggin } 84831e6b01fSNick Piggin 849bab77ebfSAl Viro int follow_up(struct path *path) 8501da177e4SLinus Torvalds { 8511da177e4SLinus Torvalds struct vfsmount *parent; 8521da177e4SLinus Torvalds struct dentry *mountpoint; 85399b7db7bSNick Piggin 85499b7db7bSNick Piggin br_read_lock(vfsmount_lock); 855bab77ebfSAl Viro parent = path->mnt->mnt_parent; 856bab77ebfSAl Viro if (parent == path->mnt) { 85799b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 8581da177e4SLinus Torvalds return 0; 8591da177e4SLinus Torvalds } 8601da177e4SLinus Torvalds mntget(parent); 861bab77ebfSAl Viro mountpoint = dget(path->mnt->mnt_mountpoint); 86299b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 863bab77ebfSAl Viro dput(path->dentry); 864bab77ebfSAl Viro path->dentry = mountpoint; 865bab77ebfSAl Viro mntput(path->mnt); 866bab77ebfSAl Viro path->mnt = parent; 8671da177e4SLinus Torvalds return 1; 8681da177e4SLinus Torvalds } 8691da177e4SLinus Torvalds 870b5c84bf6SNick Piggin /* 8719875cf80SDavid Howells * Perform an automount 8729875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 8739875cf80SDavid Howells * were called with. 8741da177e4SLinus Torvalds */ 8759875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags, 8769875cf80SDavid Howells bool *need_mntput) 87731e6b01fSNick Piggin { 8789875cf80SDavid Howells struct vfsmount *mnt; 879ea5b778aSDavid Howells int err; 8809875cf80SDavid Howells 8819875cf80SDavid Howells if (!path->dentry->d_op || !path->dentry->d_op->d_automount) 8829875cf80SDavid Howells return -EREMOTE; 8839875cf80SDavid Howells 8846f45b656SDavid Howells /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT 8856f45b656SDavid Howells * and this is the terminal part of the path. 8866f45b656SDavid Howells */ 8876f45b656SDavid Howells if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE)) 8886f45b656SDavid Howells return -EISDIR; /* we actually want to stop here */ 8896f45b656SDavid Howells 8909875cf80SDavid Howells /* We want to mount if someone is trying to open/create a file of any 8919875cf80SDavid Howells * type under the mountpoint, wants to traverse through the mountpoint 8929875cf80SDavid Howells * or wants to open the mounted directory. 8939875cf80SDavid Howells * 8949875cf80SDavid Howells * We don't want to mount if someone's just doing a stat and they've 8959875cf80SDavid Howells * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and 8969875cf80SDavid Howells * appended a '/' to the name. 8979875cf80SDavid Howells */ 8989875cf80SDavid Howells if (!(flags & LOOKUP_FOLLOW) && 8999875cf80SDavid Howells !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY | 9009875cf80SDavid Howells LOOKUP_OPEN | LOOKUP_CREATE))) 9019875cf80SDavid Howells return -EISDIR; 9029875cf80SDavid Howells 9039875cf80SDavid Howells current->total_link_count++; 9049875cf80SDavid Howells if (current->total_link_count >= 40) 9059875cf80SDavid Howells return -ELOOP; 9069875cf80SDavid Howells 9079875cf80SDavid Howells mnt = path->dentry->d_op->d_automount(path); 9089875cf80SDavid Howells if (IS_ERR(mnt)) { 9099875cf80SDavid Howells /* 9109875cf80SDavid Howells * The filesystem is allowed to return -EISDIR here to indicate 9119875cf80SDavid Howells * it doesn't want to automount. For instance, autofs would do 9129875cf80SDavid Howells * this so that its userspace daemon can mount on this dentry. 9139875cf80SDavid Howells * 9149875cf80SDavid Howells * However, we can only permit this if it's a terminal point in 9159875cf80SDavid Howells * the path being looked up; if it wasn't then the remainder of 9169875cf80SDavid Howells * the path is inaccessible and we should say so. 9179875cf80SDavid Howells */ 9189875cf80SDavid Howells if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE)) 9199875cf80SDavid Howells return -EREMOTE; 9209875cf80SDavid Howells return PTR_ERR(mnt); 92131e6b01fSNick Piggin } 922ea5b778aSDavid Howells 9239875cf80SDavid Howells if (!mnt) /* mount collision */ 9249875cf80SDavid Howells return 0; 9259875cf80SDavid Howells 92619a167afSAl Viro err = finish_automount(mnt, path); 927ea5b778aSDavid Howells 928ea5b778aSDavid Howells switch (err) { 929ea5b778aSDavid Howells case -EBUSY: 930ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 93119a167afSAl Viro return 0; 932ea5b778aSDavid Howells case 0: 933463ffb2eSAl Viro dput(path->dentry); 9349875cf80SDavid Howells if (*need_mntput) 9359875cf80SDavid Howells mntput(path->mnt); 9369875cf80SDavid Howells path->mnt = mnt; 9379875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 9389875cf80SDavid Howells *need_mntput = true; 9399875cf80SDavid Howells return 0; 94019a167afSAl Viro default: 94119a167afSAl Viro return err; 9429875cf80SDavid Howells } 94319a167afSAl Viro 944ea5b778aSDavid Howells } 9459875cf80SDavid Howells 9469875cf80SDavid Howells /* 9479875cf80SDavid Howells * Handle a dentry that is managed in some way. 948cc53ce53SDavid Howells * - Flagged for transit management (autofs) 9499875cf80SDavid Howells * - Flagged as mountpoint 9509875cf80SDavid Howells * - Flagged as automount point 9519875cf80SDavid Howells * 9529875cf80SDavid Howells * This may only be called in refwalk mode. 9539875cf80SDavid Howells * 9549875cf80SDavid Howells * Serialization is taken care of in namespace.c 9559875cf80SDavid Howells */ 9569875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 9579875cf80SDavid Howells { 9589875cf80SDavid Howells unsigned managed; 9599875cf80SDavid Howells bool need_mntput = false; 9609875cf80SDavid Howells int ret; 9619875cf80SDavid Howells 9629875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 9639875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 9649875cf80SDavid Howells * the components of that value change under us */ 9659875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 9669875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 9679875cf80SDavid Howells unlikely(managed != 0)) { 968cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 969cc53ce53SDavid Howells * being held. */ 970cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 971cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 972cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 973ab90911fSDavid Howells ret = path->dentry->d_op->d_manage(path->dentry, 974ab90911fSDavid Howells false, false); 975cc53ce53SDavid Howells if (ret < 0) 976cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 977cc53ce53SDavid Howells } 978cc53ce53SDavid Howells 9799875cf80SDavid Howells /* Transit to a mounted filesystem. */ 9809875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 9819875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 9829875cf80SDavid Howells if (mounted) { 9839875cf80SDavid Howells dput(path->dentry); 9849875cf80SDavid Howells if (need_mntput) 985463ffb2eSAl Viro mntput(path->mnt); 986463ffb2eSAl Viro path->mnt = mounted; 987463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 9889875cf80SDavid Howells need_mntput = true; 9899875cf80SDavid Howells continue; 990463ffb2eSAl Viro } 991463ffb2eSAl Viro 9929875cf80SDavid Howells /* Something is mounted on this dentry in another 9939875cf80SDavid Howells * namespace and/or whatever was mounted there in this 9949875cf80SDavid Howells * namespace got unmounted before we managed to get the 9959875cf80SDavid Howells * vfsmount_lock */ 9961da177e4SLinus Torvalds } 9979875cf80SDavid Howells 9989875cf80SDavid Howells /* Handle an automount point */ 9999875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 10009875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 10019875cf80SDavid Howells if (ret < 0) 10029875cf80SDavid Howells return ret == -EISDIR ? 0 : ret; 10039875cf80SDavid Howells continue; 10049875cf80SDavid Howells } 10059875cf80SDavid Howells 10069875cf80SDavid Howells /* We didn't change the current path point */ 10079875cf80SDavid Howells break; 10089875cf80SDavid Howells } 10099875cf80SDavid Howells return 0; 10101da177e4SLinus Torvalds } 10111da177e4SLinus Torvalds 1012cc53ce53SDavid Howells int follow_down_one(struct path *path) 10131da177e4SLinus Torvalds { 10141da177e4SLinus Torvalds struct vfsmount *mounted; 10151da177e4SLinus Torvalds 10161c755af4SAl Viro mounted = lookup_mnt(path); 10171da177e4SLinus Torvalds if (mounted) { 10189393bd07SAl Viro dput(path->dentry); 10199393bd07SAl Viro mntput(path->mnt); 10209393bd07SAl Viro path->mnt = mounted; 10219393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 10221da177e4SLinus Torvalds return 1; 10231da177e4SLinus Torvalds } 10241da177e4SLinus Torvalds return 0; 10251da177e4SLinus Torvalds } 10261da177e4SLinus Torvalds 10279875cf80SDavid Howells /* 10289875cf80SDavid Howells * Skip to top of mountpoint pile in rcuwalk mode. We abort the rcu-walk if we 1029cc53ce53SDavid Howells * meet a managed dentry and we're not walking to "..". True is returned to 10309875cf80SDavid Howells * continue, false to abort. 10319875cf80SDavid Howells */ 10329875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 10339875cf80SDavid Howells struct inode **inode, bool reverse_transit) 10349875cf80SDavid Howells { 10359875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 10369875cf80SDavid Howells struct vfsmount *mounted; 1037ab90911fSDavid Howells if (unlikely(path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) && 1038ab90911fSDavid Howells !reverse_transit && 1039ab90911fSDavid Howells path->dentry->d_op->d_manage(path->dentry, false, true) < 0) 1040ab90911fSDavid Howells return false; 10419875cf80SDavid Howells mounted = __lookup_mnt(path->mnt, path->dentry, 1); 10429875cf80SDavid Howells if (!mounted) 10439875cf80SDavid Howells break; 10449875cf80SDavid Howells path->mnt = mounted; 10459875cf80SDavid Howells path->dentry = mounted->mnt_root; 10469875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 10479875cf80SDavid Howells *inode = path->dentry->d_inode; 10489875cf80SDavid Howells } 10499875cf80SDavid Howells 10509875cf80SDavid Howells if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 10519875cf80SDavid Howells return reverse_transit; 10529875cf80SDavid Howells return true; 10539875cf80SDavid Howells } 10549875cf80SDavid Howells 105531e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 105631e6b01fSNick Piggin { 105731e6b01fSNick Piggin struct inode *inode = nd->inode; 105831e6b01fSNick Piggin 105931e6b01fSNick Piggin set_root_rcu(nd); 106031e6b01fSNick Piggin 106131e6b01fSNick Piggin while (1) { 106231e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 106331e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 106431e6b01fSNick Piggin break; 106531e6b01fSNick Piggin } 106631e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 106731e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 106831e6b01fSNick Piggin struct dentry *parent = old->d_parent; 106931e6b01fSNick Piggin unsigned seq; 107031e6b01fSNick Piggin 107131e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 107231e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 107331e6b01fSNick Piggin return -ECHILD; 107431e6b01fSNick Piggin inode = parent->d_inode; 107531e6b01fSNick Piggin nd->path.dentry = parent; 107631e6b01fSNick Piggin nd->seq = seq; 107731e6b01fSNick Piggin break; 107831e6b01fSNick Piggin } 107931e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 108031e6b01fSNick Piggin break; 108131e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 108231e6b01fSNick Piggin inode = nd->path.dentry->d_inode; 108331e6b01fSNick Piggin } 10849875cf80SDavid Howells __follow_mount_rcu(nd, &nd->path, &inode, true); 108531e6b01fSNick Piggin nd->inode = inode; 108631e6b01fSNick Piggin 108731e6b01fSNick Piggin return 0; 108831e6b01fSNick Piggin } 108931e6b01fSNick Piggin 10909875cf80SDavid Howells /* 1091cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 1092cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 1093cc53ce53SDavid Howells * caller is permitted to proceed or not. 1094cc53ce53SDavid Howells * 1095cc53ce53SDavid Howells * Care must be taken as namespace_sem may be held (indicated by mounting_here 1096cc53ce53SDavid Howells * being true). 1097cc53ce53SDavid Howells */ 1098cc53ce53SDavid Howells int follow_down(struct path *path, bool mounting_here) 1099cc53ce53SDavid Howells { 1100cc53ce53SDavid Howells unsigned managed; 1101cc53ce53SDavid Howells int ret; 1102cc53ce53SDavid Howells 1103cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 1104cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 1105cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 1106cc53ce53SDavid Howells * being held. 1107cc53ce53SDavid Howells * 1108cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 1109cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 1110cc53ce53SDavid Howells * other than its daemon the right to mount on its 1111cc53ce53SDavid Howells * superstructure. 1112cc53ce53SDavid Howells * 1113cc53ce53SDavid Howells * The filesystem may sleep at this point. 1114cc53ce53SDavid Howells */ 1115cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 1116cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 1117cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 1118ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 1119ab90911fSDavid Howells path->dentry, mounting_here, false); 1120cc53ce53SDavid Howells if (ret < 0) 1121cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 1122cc53ce53SDavid Howells } 1123cc53ce53SDavid Howells 1124cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 1125cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 1126cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 1127cc53ce53SDavid Howells if (!mounted) 1128cc53ce53SDavid Howells break; 1129cc53ce53SDavid Howells dput(path->dentry); 1130cc53ce53SDavid Howells mntput(path->mnt); 1131cc53ce53SDavid Howells path->mnt = mounted; 1132cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 1133cc53ce53SDavid Howells continue; 1134cc53ce53SDavid Howells } 1135cc53ce53SDavid Howells 1136cc53ce53SDavid Howells /* Don't handle automount points here */ 1137cc53ce53SDavid Howells break; 1138cc53ce53SDavid Howells } 1139cc53ce53SDavid Howells return 0; 1140cc53ce53SDavid Howells } 1141cc53ce53SDavid Howells 1142cc53ce53SDavid Howells /* 11439875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 11449875cf80SDavid Howells */ 11459875cf80SDavid Howells static void follow_mount(struct path *path) 11469875cf80SDavid Howells { 11479875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 11489875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 11499875cf80SDavid Howells if (!mounted) 11509875cf80SDavid Howells break; 11519875cf80SDavid Howells dput(path->dentry); 11529875cf80SDavid Howells mntput(path->mnt); 11539875cf80SDavid Howells path->mnt = mounted; 11549875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 11559875cf80SDavid Howells } 11569875cf80SDavid Howells } 11579875cf80SDavid Howells 115831e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 11591da177e4SLinus Torvalds { 11602a737871SAl Viro set_root(nd); 1161e518ddb7SAndreas Mohr 11621da177e4SLinus Torvalds while(1) { 11634ac91378SJan Blunck struct dentry *old = nd->path.dentry; 11641da177e4SLinus Torvalds 11652a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 11662a737871SAl Viro nd->path.mnt == nd->root.mnt) { 11671da177e4SLinus Torvalds break; 11681da177e4SLinus Torvalds } 11694ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 11703088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 11713088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 11721da177e4SLinus Torvalds dput(old); 11731da177e4SLinus Torvalds break; 11741da177e4SLinus Torvalds } 11753088dd70SAl Viro if (!follow_up(&nd->path)) 11761da177e4SLinus Torvalds break; 11771da177e4SLinus Torvalds } 117879ed0226SAl Viro follow_mount(&nd->path); 117931e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 11801da177e4SLinus Torvalds } 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds /* 1183baa03890SNick Piggin * Allocate a dentry with name and parent, and perform a parent 1184baa03890SNick Piggin * directory ->lookup on it. Returns the new dentry, or ERR_PTR 1185baa03890SNick Piggin * on error. parent->d_inode->i_mutex must be held. d_lookup must 1186baa03890SNick Piggin * have verified that no child exists while under i_mutex. 1187baa03890SNick Piggin */ 1188baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent, 1189baa03890SNick Piggin struct qstr *name, struct nameidata *nd) 1190baa03890SNick Piggin { 1191baa03890SNick Piggin struct inode *inode = parent->d_inode; 1192baa03890SNick Piggin struct dentry *dentry; 1193baa03890SNick Piggin struct dentry *old; 1194baa03890SNick Piggin 1195baa03890SNick Piggin /* Don't create child dentry for a dead directory. */ 1196baa03890SNick Piggin if (unlikely(IS_DEADDIR(inode))) 1197baa03890SNick Piggin return ERR_PTR(-ENOENT); 1198baa03890SNick Piggin 1199baa03890SNick Piggin dentry = d_alloc(parent, name); 1200baa03890SNick Piggin if (unlikely(!dentry)) 1201baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1202baa03890SNick Piggin 1203baa03890SNick Piggin old = inode->i_op->lookup(inode, dentry, nd); 1204baa03890SNick Piggin if (unlikely(old)) { 1205baa03890SNick Piggin dput(dentry); 1206baa03890SNick Piggin dentry = old; 1207baa03890SNick Piggin } 1208baa03890SNick Piggin return dentry; 1209baa03890SNick Piggin } 1210baa03890SNick Piggin 1211baa03890SNick Piggin /* 12121da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 12131da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 12141da177e4SLinus Torvalds * It _is_ time-critical. 12151da177e4SLinus Torvalds */ 12161da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name, 121731e6b01fSNick Piggin struct path *path, struct inode **inode) 12181da177e4SLinus Torvalds { 12194ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 122031e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 12216e6b1bd1SAl Viro struct inode *dir; 12229875cf80SDavid Howells int err; 12239875cf80SDavid Howells 12243cac260aSAl Viro /* 12253cac260aSAl Viro * See if the low-level filesystem might want 12263cac260aSAl Viro * to use its own hash.. 12273cac260aSAl Viro */ 1228fb045adbSNick Piggin if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 12299875cf80SDavid Howells err = parent->d_op->d_hash(parent, nd->inode, name); 12303cac260aSAl Viro if (err < 0) 12313cac260aSAl Viro return err; 12323cac260aSAl Viro } 12331da177e4SLinus Torvalds 1234b04f784eSNick Piggin /* 1235b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1236b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1237b04f784eSNick Piggin * do the non-racy lookup, below. 1238b04f784eSNick Piggin */ 123931e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 124031e6b01fSNick Piggin unsigned seq; 124131e6b01fSNick Piggin 124231e6b01fSNick Piggin *inode = nd->inode; 124331e6b01fSNick Piggin dentry = __d_lookup_rcu(parent, name, &seq, inode); 124431e6b01fSNick Piggin if (!dentry) { 124531e6b01fSNick Piggin if (nameidata_drop_rcu(nd)) 124631e6b01fSNick Piggin return -ECHILD; 124731e6b01fSNick Piggin goto need_lookup; 124831e6b01fSNick Piggin } 124931e6b01fSNick Piggin /* Memory barrier in read_seqcount_begin of child is enough */ 125031e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 125131e6b01fSNick Piggin return -ECHILD; 125231e6b01fSNick Piggin 125331e6b01fSNick Piggin nd->seq = seq; 125434286d66SNick Piggin if (dentry->d_flags & DCACHE_OP_REVALIDATE) 125531e6b01fSNick Piggin goto need_revalidate; 12561a8edf40SAl Viro done2: 125731e6b01fSNick Piggin path->mnt = mnt; 125831e6b01fSNick Piggin path->dentry = dentry; 12599875cf80SDavid Howells if (likely(__follow_mount_rcu(nd, path, inode, false))) 12609875cf80SDavid Howells return 0; 12619875cf80SDavid Howells if (nameidata_drop_rcu(nd)) 12629875cf80SDavid Howells return -ECHILD; 12639875cf80SDavid Howells /* fallthru */ 12649875cf80SDavid Howells } 126531e6b01fSNick Piggin dentry = __d_lookup(parent, name); 12661da177e4SLinus Torvalds if (!dentry) 12671da177e4SLinus Torvalds goto need_lookup; 12682e2e88eaSNick Piggin found: 1269fb045adbSNick Piggin if (dentry->d_flags & DCACHE_OP_REVALIDATE) 12701da177e4SLinus Torvalds goto need_revalidate; 12711da177e4SLinus Torvalds done: 12721da177e4SLinus Torvalds path->mnt = mnt; 12731da177e4SLinus Torvalds path->dentry = dentry; 12749875cf80SDavid Howells err = follow_managed(path, nd->flags); 127589312214SIan Kent if (unlikely(err < 0)) { 127689312214SIan Kent path_put_conditional(path, nd); 12779875cf80SDavid Howells return err; 127889312214SIan Kent } 127931e6b01fSNick Piggin *inode = path->dentry->d_inode; 12801da177e4SLinus Torvalds return 0; 12811da177e4SLinus Torvalds 12821da177e4SLinus Torvalds need_lookup: 12836e6b1bd1SAl Viro dir = parent->d_inode; 128431e6b01fSNick Piggin BUG_ON(nd->inode != dir); 12856e6b1bd1SAl Viro 12866e6b1bd1SAl Viro mutex_lock(&dir->i_mutex); 12876e6b1bd1SAl Viro /* 12886e6b1bd1SAl Viro * First re-do the cached lookup just in case it was created 1289b04f784eSNick Piggin * while we waited for the directory semaphore, or the first 1290b04f784eSNick Piggin * lookup failed due to an unrelated rename. 12916e6b1bd1SAl Viro * 1292b04f784eSNick Piggin * This could use version numbering or similar to avoid unnecessary 1293b04f784eSNick Piggin * cache lookups, but then we'd have to do the first lookup in the 1294b04f784eSNick Piggin * non-racy way. However in the common case here, everything should 1295b04f784eSNick Piggin * be hot in cache, so would it be a big win? 12966e6b1bd1SAl Viro */ 12976e6b1bd1SAl Viro dentry = d_lookup(parent, name); 1298baa03890SNick Piggin if (likely(!dentry)) { 1299baa03890SNick Piggin dentry = d_alloc_and_lookup(parent, name, nd); 13006e6b1bd1SAl Viro mutex_unlock(&dir->i_mutex); 13016e6b1bd1SAl Viro if (IS_ERR(dentry)) 13026e6b1bd1SAl Viro goto fail; 13036e6b1bd1SAl Viro goto done; 13046e6b1bd1SAl Viro } 13056e6b1bd1SAl Viro /* 13066e6b1bd1SAl Viro * Uhhuh! Nasty case: the cache was re-populated while 13076e6b1bd1SAl Viro * we waited on the semaphore. Need to revalidate. 13086e6b1bd1SAl Viro */ 13096e6b1bd1SAl Viro mutex_unlock(&dir->i_mutex); 13102e2e88eaSNick Piggin goto found; 13111da177e4SLinus Torvalds 13121da177e4SLinus Torvalds need_revalidate: 1313bcdc5e01SIan Kent dentry = do_revalidate(dentry, nd); 1314bcdc5e01SIan Kent if (!dentry) 13151da177e4SLinus Torvalds goto need_lookup; 1316bcdc5e01SIan Kent if (IS_ERR(dentry)) 1317bcdc5e01SIan Kent goto fail; 13181a8edf40SAl Viro if (nd->flags & LOOKUP_RCU) 13191a8edf40SAl Viro goto done2; 1320bcdc5e01SIan Kent goto done; 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds fail: 13231da177e4SLinus Torvalds return PTR_ERR(dentry); 13241da177e4SLinus Torvalds } 13251da177e4SLinus Torvalds 13261da177e4SLinus Torvalds /* 13271da177e4SLinus Torvalds * Name resolution. 1328ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1329ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 13301da177e4SLinus Torvalds * 1331ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1332ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 13331da177e4SLinus Torvalds */ 13346de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 13351da177e4SLinus Torvalds { 13361da177e4SLinus Torvalds struct path next; 13371da177e4SLinus Torvalds int err; 13381da177e4SLinus Torvalds unsigned int lookup_flags = nd->flags; 13391da177e4SLinus Torvalds 13401da177e4SLinus Torvalds while (*name=='/') 13411da177e4SLinus Torvalds name++; 13421da177e4SLinus Torvalds if (!*name) 13431da177e4SLinus Torvalds goto return_reval; 13441da177e4SLinus Torvalds 13451da177e4SLinus Torvalds if (nd->depth) 1346f55eab82STrond Myklebust lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE); 13471da177e4SLinus Torvalds 13481da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 13491da177e4SLinus Torvalds for(;;) { 135031e6b01fSNick Piggin struct inode *inode; 13511da177e4SLinus Torvalds unsigned long hash; 13521da177e4SLinus Torvalds struct qstr this; 13531da177e4SLinus Torvalds unsigned int c; 13541da177e4SLinus Torvalds 1355cdce5d6bSTrond Myklebust nd->flags |= LOOKUP_CONTINUE; 135631e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 1357b74c79e9SNick Piggin err = exec_permission(nd->inode, IPERM_FLAG_RCU); 135831e6b01fSNick Piggin if (err == -ECHILD) { 135931e6b01fSNick Piggin if (nameidata_drop_rcu(nd)) 136031e6b01fSNick Piggin return -ECHILD; 136131e6b01fSNick Piggin goto exec_again; 136231e6b01fSNick Piggin } 136331e6b01fSNick Piggin } else { 136431e6b01fSNick Piggin exec_again: 1365b74c79e9SNick Piggin err = exec_permission(nd->inode, 0); 136631e6b01fSNick Piggin } 13671da177e4SLinus Torvalds if (err) 13681da177e4SLinus Torvalds break; 13691da177e4SLinus Torvalds 13701da177e4SLinus Torvalds this.name = name; 13711da177e4SLinus Torvalds c = *(const unsigned char *)name; 13721da177e4SLinus Torvalds 13731da177e4SLinus Torvalds hash = init_name_hash(); 13741da177e4SLinus Torvalds do { 13751da177e4SLinus Torvalds name++; 13761da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 13771da177e4SLinus Torvalds c = *(const unsigned char *)name; 13781da177e4SLinus Torvalds } while (c && (c != '/')); 13791da177e4SLinus Torvalds this.len = name - (const char *) this.name; 13801da177e4SLinus Torvalds this.hash = end_name_hash(hash); 13811da177e4SLinus Torvalds 13821da177e4SLinus Torvalds /* remove trailing slashes? */ 13831da177e4SLinus Torvalds if (!c) 13841da177e4SLinus Torvalds goto last_component; 13851da177e4SLinus Torvalds while (*++name == '/'); 13861da177e4SLinus Torvalds if (!*name) 13871da177e4SLinus Torvalds goto last_with_slashes; 13881da177e4SLinus Torvalds 13891da177e4SLinus Torvalds /* 13901da177e4SLinus Torvalds * "." and ".." are special - ".." especially so because it has 13911da177e4SLinus Torvalds * to be able to know about the current root directory and 13921da177e4SLinus Torvalds * parent relationships. 13931da177e4SLinus Torvalds */ 13941da177e4SLinus Torvalds if (this.name[0] == '.') switch (this.len) { 13951da177e4SLinus Torvalds default: 13961da177e4SLinus Torvalds break; 13971da177e4SLinus Torvalds case 2: 13981da177e4SLinus Torvalds if (this.name[1] != '.') 13991da177e4SLinus Torvalds break; 140031e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 140131e6b01fSNick Piggin if (follow_dotdot_rcu(nd)) 140231e6b01fSNick Piggin return -ECHILD; 140331e6b01fSNick Piggin } else 140458c465ebSAl Viro follow_dotdot(nd); 14051da177e4SLinus Torvalds /* fallthrough */ 14061da177e4SLinus Torvalds case 1: 14071da177e4SLinus Torvalds continue; 14081da177e4SLinus Torvalds } 14091da177e4SLinus Torvalds /* This does the actual lookups.. */ 141031e6b01fSNick Piggin err = do_lookup(nd, &this, &next, &inode); 14111da177e4SLinus Torvalds if (err) 14121da177e4SLinus Torvalds break; 14131da177e4SLinus Torvalds err = -ENOENT; 14141da177e4SLinus Torvalds if (!inode) 14151da177e4SLinus Torvalds goto out_dput; 14161da177e4SLinus Torvalds 14171da177e4SLinus Torvalds if (inode->i_op->follow_link) { 141831e6b01fSNick Piggin /* We commonly drop rcu-walk here */ 141931e6b01fSNick Piggin if (nameidata_dentry_drop_rcu_maybe(nd, next.dentry)) 142031e6b01fSNick Piggin return -ECHILD; 142131e6b01fSNick Piggin BUG_ON(inode != next.dentry->d_inode); 142290ebe565SAl Viro err = do_follow_link(&next, nd); 14231da177e4SLinus Torvalds if (err) 14241da177e4SLinus Torvalds goto return_err; 142531e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 14261da177e4SLinus Torvalds err = -ENOENT; 142731e6b01fSNick Piggin if (!nd->inode) 14281da177e4SLinus Torvalds break; 142931e6b01fSNick Piggin } else { 143009dd17d3SMiklos Szeredi path_to_nameidata(&next, nd); 143131e6b01fSNick Piggin nd->inode = inode; 143231e6b01fSNick Piggin } 14331da177e4SLinus Torvalds err = -ENOTDIR; 143431e6b01fSNick Piggin if (!nd->inode->i_op->lookup) 14351da177e4SLinus Torvalds break; 14361da177e4SLinus Torvalds continue; 14371da177e4SLinus Torvalds /* here ends the main loop */ 14381da177e4SLinus Torvalds 14391da177e4SLinus Torvalds last_with_slashes: 14401da177e4SLinus Torvalds lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 14411da177e4SLinus Torvalds last_component: 1442f55eab82STrond Myklebust /* Clear LOOKUP_CONTINUE iff it was previously unset */ 1443f55eab82STrond Myklebust nd->flags &= lookup_flags | ~LOOKUP_CONTINUE; 14441da177e4SLinus Torvalds if (lookup_flags & LOOKUP_PARENT) 14451da177e4SLinus Torvalds goto lookup_parent; 14461da177e4SLinus Torvalds if (this.name[0] == '.') switch (this.len) { 14471da177e4SLinus Torvalds default: 14481da177e4SLinus Torvalds break; 14491da177e4SLinus Torvalds case 2: 14501da177e4SLinus Torvalds if (this.name[1] != '.') 14511da177e4SLinus Torvalds break; 145231e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 145331e6b01fSNick Piggin if (follow_dotdot_rcu(nd)) 145431e6b01fSNick Piggin return -ECHILD; 145531e6b01fSNick Piggin } else 145658c465ebSAl Viro follow_dotdot(nd); 14571da177e4SLinus Torvalds /* fallthrough */ 14581da177e4SLinus Torvalds case 1: 14591da177e4SLinus Torvalds goto return_reval; 14601da177e4SLinus Torvalds } 146131e6b01fSNick Piggin err = do_lookup(nd, &this, &next, &inode); 14621da177e4SLinus Torvalds if (err) 14631da177e4SLinus Torvalds break; 1464db372915SDavid Howells if (inode && unlikely(inode->i_op->follow_link) && 1465db372915SDavid Howells (lookup_flags & LOOKUP_FOLLOW)) { 146631e6b01fSNick Piggin if (nameidata_dentry_drop_rcu_maybe(nd, next.dentry)) 146731e6b01fSNick Piggin return -ECHILD; 146831e6b01fSNick Piggin BUG_ON(inode != next.dentry->d_inode); 146990ebe565SAl Viro err = do_follow_link(&next, nd); 14701da177e4SLinus Torvalds if (err) 14711da177e4SLinus Torvalds goto return_err; 147231e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 147331e6b01fSNick Piggin } else { 147409dd17d3SMiklos Szeredi path_to_nameidata(&next, nd); 147531e6b01fSNick Piggin nd->inode = inode; 147631e6b01fSNick Piggin } 14771da177e4SLinus Torvalds err = -ENOENT; 147831e6b01fSNick Piggin if (!nd->inode) 14791da177e4SLinus Torvalds break; 14801da177e4SLinus Torvalds if (lookup_flags & LOOKUP_DIRECTORY) { 14811da177e4SLinus Torvalds err = -ENOTDIR; 148231e6b01fSNick Piggin if (!nd->inode->i_op->lookup) 14831da177e4SLinus Torvalds break; 14841da177e4SLinus Torvalds } 14851da177e4SLinus Torvalds goto return_base; 14861da177e4SLinus Torvalds lookup_parent: 14871da177e4SLinus Torvalds nd->last = this; 14881da177e4SLinus Torvalds nd->last_type = LAST_NORM; 14891da177e4SLinus Torvalds if (this.name[0] != '.') 14901da177e4SLinus Torvalds goto return_base; 14911da177e4SLinus Torvalds if (this.len == 1) 14921da177e4SLinus Torvalds nd->last_type = LAST_DOT; 14931da177e4SLinus Torvalds else if (this.len == 2 && this.name[1] == '.') 14941da177e4SLinus Torvalds nd->last_type = LAST_DOTDOT; 14951da177e4SLinus Torvalds else 14961da177e4SLinus Torvalds goto return_base; 14971da177e4SLinus Torvalds return_reval: 14981da177e4SLinus Torvalds /* 14991da177e4SLinus Torvalds * We bypassed the ordinary revalidation routines. 15001da177e4SLinus Torvalds * We may need to check the cached dentry for staleness. 15011da177e4SLinus Torvalds */ 1502fb045adbSNick Piggin if (need_reval_dot(nd->path.dentry)) { 15031da177e4SLinus Torvalds /* Note: we do not d_invalidate() */ 150434286d66SNick Piggin err = d_revalidate(nd->path.dentry, nd); 150534286d66SNick Piggin if (!err) 150634286d66SNick Piggin err = -ESTALE; 150734286d66SNick Piggin if (err < 0) 15081da177e4SLinus Torvalds break; 15091da177e4SLinus Torvalds } 15101da177e4SLinus Torvalds return_base: 151131e6b01fSNick Piggin if (nameidata_drop_rcu_last_maybe(nd)) 151231e6b01fSNick Piggin return -ECHILD; 15131da177e4SLinus Torvalds return 0; 15141da177e4SLinus Torvalds out_dput: 151531e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) 15161d957f9bSJan Blunck path_put_conditional(&next, nd); 15171da177e4SLinus Torvalds break; 15181da177e4SLinus Torvalds } 151931e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) 15201d957f9bSJan Blunck path_put(&nd->path); 15211da177e4SLinus Torvalds return_err: 15221da177e4SLinus Torvalds return err; 15231da177e4SLinus Torvalds } 15241da177e4SLinus Torvalds 152531e6b01fSNick Piggin static inline int path_walk_rcu(const char *name, struct nameidata *nd) 152631e6b01fSNick Piggin { 152731e6b01fSNick Piggin current->total_link_count = 0; 152831e6b01fSNick Piggin 152931e6b01fSNick Piggin return link_path_walk(name, nd); 153031e6b01fSNick Piggin } 153131e6b01fSNick Piggin 153231e6b01fSNick Piggin static inline int path_walk_simple(const char *name, struct nameidata *nd) 153331e6b01fSNick Piggin { 153431e6b01fSNick Piggin current->total_link_count = 0; 153531e6b01fSNick Piggin 153631e6b01fSNick Piggin return link_path_walk(name, nd); 153731e6b01fSNick Piggin } 153831e6b01fSNick Piggin 1539fc9b52cdSHarvey Harrison static int path_walk(const char *name, struct nameidata *nd) 15401da177e4SLinus Torvalds { 15416de88d72SAl Viro struct path save = nd->path; 15426de88d72SAl Viro int result; 15436de88d72SAl Viro 15441da177e4SLinus Torvalds current->total_link_count = 0; 15456de88d72SAl Viro 15466de88d72SAl Viro /* make sure the stuff we saved doesn't go away */ 15476de88d72SAl Viro path_get(&save); 15486de88d72SAl Viro 15496de88d72SAl Viro result = link_path_walk(name, nd); 15506de88d72SAl Viro if (result == -ESTALE) { 15516de88d72SAl Viro /* nd->path had been dropped */ 15526de88d72SAl Viro current->total_link_count = 0; 15536de88d72SAl Viro nd->path = save; 15546de88d72SAl Viro path_get(&nd->path); 15556de88d72SAl Viro nd->flags |= LOOKUP_REVAL; 15566de88d72SAl Viro result = link_path_walk(name, nd); 15576de88d72SAl Viro } 15586de88d72SAl Viro 15596de88d72SAl Viro path_put(&save); 15606de88d72SAl Viro 15616de88d72SAl Viro return result; 15621da177e4SLinus Torvalds } 15631da177e4SLinus Torvalds 156431e6b01fSNick Piggin static void path_finish_rcu(struct nameidata *nd) 156531e6b01fSNick Piggin { 156631e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 156731e6b01fSNick Piggin /* RCU dangling. Cancel it. */ 156831e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 156931e6b01fSNick Piggin nd->root.mnt = NULL; 157031e6b01fSNick Piggin rcu_read_unlock(); 157131e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 157231e6b01fSNick Piggin } 157331e6b01fSNick Piggin if (nd->file) 157431e6b01fSNick Piggin fput(nd->file); 157531e6b01fSNick Piggin } 157631e6b01fSNick Piggin 157731e6b01fSNick Piggin static int path_init_rcu(int dfd, const char *name, unsigned int flags, struct nameidata *nd) 157831e6b01fSNick Piggin { 157931e6b01fSNick Piggin int retval = 0; 158031e6b01fSNick Piggin int fput_needed; 158131e6b01fSNick Piggin struct file *file; 158231e6b01fSNick Piggin 158331e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 158431e6b01fSNick Piggin nd->flags = flags | LOOKUP_RCU; 158531e6b01fSNick Piggin nd->depth = 0; 158631e6b01fSNick Piggin nd->root.mnt = NULL; 158731e6b01fSNick Piggin nd->file = NULL; 158831e6b01fSNick Piggin 158931e6b01fSNick Piggin if (*name=='/') { 159031e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1591c28cc364SNick Piggin unsigned seq; 159231e6b01fSNick Piggin 159331e6b01fSNick Piggin br_read_lock(vfsmount_lock); 159431e6b01fSNick Piggin rcu_read_lock(); 159531e6b01fSNick Piggin 1596c28cc364SNick Piggin do { 1597c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 159831e6b01fSNick Piggin nd->root = fs->root; 159931e6b01fSNick Piggin nd->path = nd->root; 1600c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1601c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 160231e6b01fSNick Piggin 160331e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 160431e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1605c28cc364SNick Piggin unsigned seq; 160631e6b01fSNick Piggin 160731e6b01fSNick Piggin br_read_lock(vfsmount_lock); 160831e6b01fSNick Piggin rcu_read_lock(); 160931e6b01fSNick Piggin 1610c28cc364SNick Piggin do { 1611c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 161231e6b01fSNick Piggin nd->path = fs->pwd; 1613c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1614c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1615c28cc364SNick Piggin 161631e6b01fSNick Piggin } else { 161731e6b01fSNick Piggin struct dentry *dentry; 161831e6b01fSNick Piggin 161931e6b01fSNick Piggin file = fget_light(dfd, &fput_needed); 162031e6b01fSNick Piggin retval = -EBADF; 162131e6b01fSNick Piggin if (!file) 162231e6b01fSNick Piggin goto out_fail; 162331e6b01fSNick Piggin 162431e6b01fSNick Piggin dentry = file->f_path.dentry; 162531e6b01fSNick Piggin 162631e6b01fSNick Piggin retval = -ENOTDIR; 162731e6b01fSNick Piggin if (!S_ISDIR(dentry->d_inode->i_mode)) 162831e6b01fSNick Piggin goto fput_fail; 162931e6b01fSNick Piggin 163031e6b01fSNick Piggin retval = file_permission(file, MAY_EXEC); 163131e6b01fSNick Piggin if (retval) 163231e6b01fSNick Piggin goto fput_fail; 163331e6b01fSNick Piggin 163431e6b01fSNick Piggin nd->path = file->f_path; 163531e6b01fSNick Piggin if (fput_needed) 163631e6b01fSNick Piggin nd->file = file; 163731e6b01fSNick Piggin 1638c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 163931e6b01fSNick Piggin br_read_lock(vfsmount_lock); 164031e6b01fSNick Piggin rcu_read_lock(); 164131e6b01fSNick Piggin } 164231e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 164331e6b01fSNick Piggin return 0; 164431e6b01fSNick Piggin 164531e6b01fSNick Piggin fput_fail: 164631e6b01fSNick Piggin fput_light(file, fput_needed); 164731e6b01fSNick Piggin out_fail: 164831e6b01fSNick Piggin return retval; 164931e6b01fSNick Piggin } 165031e6b01fSNick Piggin 16519b4a9b14SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd) 16521da177e4SLinus Torvalds { 1653ea3834d9SPrasanna Meda int retval = 0; 1654170aa3d0SUlrich Drepper int fput_needed; 1655170aa3d0SUlrich Drepper struct file *file; 16561da177e4SLinus Torvalds 16571da177e4SLinus Torvalds nd->last_type = LAST_ROOT; /* if there are only slashes... */ 16581da177e4SLinus Torvalds nd->flags = flags; 16591da177e4SLinus Torvalds nd->depth = 0; 16602a737871SAl Viro nd->root.mnt = NULL; 16611da177e4SLinus Torvalds 16621da177e4SLinus Torvalds if (*name=='/') { 16632a737871SAl Viro set_root(nd); 16642a737871SAl Viro nd->path = nd->root; 16652a737871SAl Viro path_get(&nd->root); 16665590ff0dSUlrich Drepper } else if (dfd == AT_FDCWD) { 1667f7ad3c6bSMiklos Szeredi get_fs_pwd(current->fs, &nd->path); 16685590ff0dSUlrich Drepper } else { 16695590ff0dSUlrich Drepper struct dentry *dentry; 16705590ff0dSUlrich Drepper 16715590ff0dSUlrich Drepper file = fget_light(dfd, &fput_needed); 16725590ff0dSUlrich Drepper retval = -EBADF; 1673170aa3d0SUlrich Drepper if (!file) 16746d09bb62STrond Myklebust goto out_fail; 16755590ff0dSUlrich Drepper 16760f7fc9e4SJosef "Jeff" Sipek dentry = file->f_path.dentry; 16775590ff0dSUlrich Drepper 16785590ff0dSUlrich Drepper retval = -ENOTDIR; 1679170aa3d0SUlrich Drepper if (!S_ISDIR(dentry->d_inode->i_mode)) 16806d09bb62STrond Myklebust goto fput_fail; 16815590ff0dSUlrich Drepper 16825590ff0dSUlrich Drepper retval = file_permission(file, MAY_EXEC); 1683170aa3d0SUlrich Drepper if (retval) 16846d09bb62STrond Myklebust goto fput_fail; 16855590ff0dSUlrich Drepper 16865dd784d0SJan Blunck nd->path = file->f_path; 16875dd784d0SJan Blunck path_get(&file->f_path); 16885590ff0dSUlrich Drepper 16895590ff0dSUlrich Drepper fput_light(file, fput_needed); 16901da177e4SLinus Torvalds } 169131e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 16929b4a9b14SAl Viro return 0; 16932dfdd266SJosef 'Jeff' Sipek 16949b4a9b14SAl Viro fput_fail: 16959b4a9b14SAl Viro fput_light(file, fput_needed); 16969b4a9b14SAl Viro out_fail: 16979b4a9b14SAl Viro return retval; 16989b4a9b14SAl Viro } 16999b4a9b14SAl Viro 17009b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 17019b4a9b14SAl Viro static int do_path_lookup(int dfd, const char *name, 17029b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 17039b4a9b14SAl Viro { 170431e6b01fSNick Piggin int retval; 170531e6b01fSNick Piggin 170631e6b01fSNick Piggin /* 170731e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 170831e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 170931e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 171031e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 171131e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 171231e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 171331e6b01fSNick Piggin * analogue, foo_rcu(). 171431e6b01fSNick Piggin * 171531e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 171631e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 171731e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 171831e6b01fSNick Piggin * be able to complete). 171931e6b01fSNick Piggin */ 172031e6b01fSNick Piggin retval = path_init_rcu(dfd, name, flags, nd); 172131e6b01fSNick Piggin if (unlikely(retval)) 172231e6b01fSNick Piggin return retval; 172331e6b01fSNick Piggin retval = path_walk_rcu(name, nd); 172431e6b01fSNick Piggin path_finish_rcu(nd); 17252a737871SAl Viro if (nd->root.mnt) { 17262a737871SAl Viro path_put(&nd->root); 17272a737871SAl Viro nd->root.mnt = NULL; 17282a737871SAl Viro } 172931e6b01fSNick Piggin 173031e6b01fSNick Piggin if (unlikely(retval == -ECHILD || retval == -ESTALE)) { 173131e6b01fSNick Piggin /* slower, locked walk */ 173231e6b01fSNick Piggin if (retval == -ESTALE) 173331e6b01fSNick Piggin flags |= LOOKUP_REVAL; 173431e6b01fSNick Piggin retval = path_init(dfd, name, flags, nd); 173531e6b01fSNick Piggin if (unlikely(retval)) 173631e6b01fSNick Piggin return retval; 173731e6b01fSNick Piggin retval = path_walk(name, nd); 173831e6b01fSNick Piggin if (nd->root.mnt) { 173931e6b01fSNick Piggin path_put(&nd->root); 174031e6b01fSNick Piggin nd->root.mnt = NULL; 174131e6b01fSNick Piggin } 174231e6b01fSNick Piggin } 174331e6b01fSNick Piggin 174431e6b01fSNick Piggin if (likely(!retval)) { 174531e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) { 174631e6b01fSNick Piggin if (nd->path.dentry && nd->inode) 174731e6b01fSNick Piggin audit_inode(name, nd->path.dentry); 174831e6b01fSNick Piggin } 174931e6b01fSNick Piggin } 175031e6b01fSNick Piggin 1751170aa3d0SUlrich Drepper return retval; 17521da177e4SLinus Torvalds } 17531da177e4SLinus Torvalds 1754fc9b52cdSHarvey Harrison int path_lookup(const char *name, unsigned int flags, 17555590ff0dSUlrich Drepper struct nameidata *nd) 17565590ff0dSUlrich Drepper { 17575590ff0dSUlrich Drepper return do_path_lookup(AT_FDCWD, name, flags, nd); 17585590ff0dSUlrich Drepper } 17595590ff0dSUlrich Drepper 1760d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1761d1811465SAl Viro { 1762d1811465SAl Viro struct nameidata nd; 1763d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1764d1811465SAl Viro if (!res) 1765d1811465SAl Viro *path = nd.path; 1766d1811465SAl Viro return res; 1767d1811465SAl Viro } 1768d1811465SAl Viro 176916f18200SJosef 'Jeff' Sipek /** 177016f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 177116f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 177216f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 177316f18200SJosef 'Jeff' Sipek * @name: pointer to file name 177416f18200SJosef 'Jeff' Sipek * @flags: lookup flags 177516f18200SJosef 'Jeff' Sipek * @nd: pointer to nameidata 177616f18200SJosef 'Jeff' Sipek */ 177716f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 177816f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 177916f18200SJosef 'Jeff' Sipek struct nameidata *nd) 178016f18200SJosef 'Jeff' Sipek { 178116f18200SJosef 'Jeff' Sipek int retval; 178216f18200SJosef 'Jeff' Sipek 178316f18200SJosef 'Jeff' Sipek /* same as do_path_lookup */ 178416f18200SJosef 'Jeff' Sipek nd->last_type = LAST_ROOT; 178516f18200SJosef 'Jeff' Sipek nd->flags = flags; 178616f18200SJosef 'Jeff' Sipek nd->depth = 0; 178716f18200SJosef 'Jeff' Sipek 1788c8e7f449SJan Blunck nd->path.dentry = dentry; 1789c8e7f449SJan Blunck nd->path.mnt = mnt; 1790c8e7f449SJan Blunck path_get(&nd->path); 17915b857119SAl Viro nd->root = nd->path; 17925b857119SAl Viro path_get(&nd->root); 179331e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 179416f18200SJosef 'Jeff' Sipek 179516f18200SJosef 'Jeff' Sipek retval = path_walk(name, nd); 17964ac91378SJan Blunck if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry && 179731e6b01fSNick Piggin nd->inode)) 17984ac91378SJan Blunck audit_inode(name, nd->path.dentry); 179916f18200SJosef 'Jeff' Sipek 18002a737871SAl Viro path_put(&nd->root); 18012a737871SAl Viro nd->root.mnt = NULL; 180216f18200SJosef 'Jeff' Sipek 18032a737871SAl Viro return retval; 180416f18200SJosef 'Jeff' Sipek } 180516f18200SJosef 'Jeff' Sipek 1806eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name, 1807eead1911SChristoph Hellwig struct dentry *base, struct nameidata *nd) 18081da177e4SLinus Torvalds { 180981fca444SChristoph Hellwig struct inode *inode = base->d_inode; 18101da177e4SLinus Torvalds struct dentry *dentry; 18111da177e4SLinus Torvalds int err; 18121da177e4SLinus Torvalds 1813b74c79e9SNick Piggin err = exec_permission(inode, 0); 181481fca444SChristoph Hellwig if (err) 181581fca444SChristoph Hellwig return ERR_PTR(err); 18161da177e4SLinus Torvalds 18171da177e4SLinus Torvalds /* 18181da177e4SLinus Torvalds * See if the low-level filesystem might want 18191da177e4SLinus Torvalds * to use its own hash.. 18201da177e4SLinus Torvalds */ 1821fb045adbSNick Piggin if (base->d_flags & DCACHE_OP_HASH) { 1822b1e6a015SNick Piggin err = base->d_op->d_hash(base, inode, name); 18231da177e4SLinus Torvalds dentry = ERR_PTR(err); 18241da177e4SLinus Torvalds if (err < 0) 18251da177e4SLinus Torvalds goto out; 18261da177e4SLinus Torvalds } 18271da177e4SLinus Torvalds 1828b04f784eSNick Piggin /* 1829b04f784eSNick Piggin * Don't bother with __d_lookup: callers are for creat as 1830b04f784eSNick Piggin * well as unlink, so a lot of the time it would cost 1831b04f784eSNick Piggin * a double lookup. 18326e6b1bd1SAl Viro */ 18336e6b1bd1SAl Viro dentry = d_lookup(base, name); 18346e6b1bd1SAl Viro 1835fb045adbSNick Piggin if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) 18366e6b1bd1SAl Viro dentry = do_revalidate(dentry, nd); 18376e6b1bd1SAl Viro 18381da177e4SLinus Torvalds if (!dentry) 1839baa03890SNick Piggin dentry = d_alloc_and_lookup(base, name, nd); 18401da177e4SLinus Torvalds out: 18411da177e4SLinus Torvalds return dentry; 18421da177e4SLinus Torvalds } 18431da177e4SLinus Torvalds 1844057f6c01SJames Morris /* 1845057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1846057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1847057f6c01SJames Morris * SMP-safe. 1848057f6c01SJames Morris */ 1849a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 18501da177e4SLinus Torvalds { 18514ac91378SJan Blunck return __lookup_hash(&nd->last, nd->path.dentry, nd); 18521da177e4SLinus Torvalds } 18531da177e4SLinus Torvalds 1854eead1911SChristoph Hellwig static int __lookup_one_len(const char *name, struct qstr *this, 1855eead1911SChristoph Hellwig struct dentry *base, int len) 18561da177e4SLinus Torvalds { 18571da177e4SLinus Torvalds unsigned long hash; 18581da177e4SLinus Torvalds unsigned int c; 18591da177e4SLinus Torvalds 1860057f6c01SJames Morris this->name = name; 1861057f6c01SJames Morris this->len = len; 18621da177e4SLinus Torvalds if (!len) 1863057f6c01SJames Morris return -EACCES; 18641da177e4SLinus Torvalds 18651da177e4SLinus Torvalds hash = init_name_hash(); 18661da177e4SLinus Torvalds while (len--) { 18671da177e4SLinus Torvalds c = *(const unsigned char *)name++; 18681da177e4SLinus Torvalds if (c == '/' || c == '\0') 1869057f6c01SJames Morris return -EACCES; 18701da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 18711da177e4SLinus Torvalds } 1872057f6c01SJames Morris this->hash = end_name_hash(hash); 1873057f6c01SJames Morris return 0; 1874057f6c01SJames Morris } 18751da177e4SLinus Torvalds 1876eead1911SChristoph Hellwig /** 1877a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1878eead1911SChristoph Hellwig * @name: pathname component to lookup 1879eead1911SChristoph Hellwig * @base: base directory to lookup from 1880eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1881eead1911SChristoph Hellwig * 1882a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1883a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1884eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1885eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1886eead1911SChristoph Hellwig */ 1887057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1888057f6c01SJames Morris { 1889057f6c01SJames Morris int err; 1890057f6c01SJames Morris struct qstr this; 1891057f6c01SJames Morris 18922f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 18932f9092e1SDavid Woodhouse 1894057f6c01SJames Morris err = __lookup_one_len(name, &this, base, len); 1895057f6c01SJames Morris if (err) 1896057f6c01SJames Morris return ERR_PTR(err); 1897eead1911SChristoph Hellwig 189849705b77SChristoph Hellwig return __lookup_hash(&this, base, NULL); 1899057f6c01SJames Morris } 1900057f6c01SJames Morris 19012d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags, 19022d8f3038SAl Viro struct path *path) 19031da177e4SLinus Torvalds { 19042d8f3038SAl Viro struct nameidata nd; 19051da177e4SLinus Torvalds char *tmp = getname(name); 19061da177e4SLinus Torvalds int err = PTR_ERR(tmp); 19071da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 19082d8f3038SAl Viro 19092d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 19102d8f3038SAl Viro 19112d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 19121da177e4SLinus Torvalds putname(tmp); 19132d8f3038SAl Viro if (!err) 19142d8f3038SAl Viro *path = nd.path; 19151da177e4SLinus Torvalds } 19161da177e4SLinus Torvalds return err; 19171da177e4SLinus Torvalds } 19181da177e4SLinus Torvalds 19192ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 19202ad94ae6SAl Viro struct nameidata *nd, char **name) 19212ad94ae6SAl Viro { 19222ad94ae6SAl Viro char *s = getname(path); 19232ad94ae6SAl Viro int error; 19242ad94ae6SAl Viro 19252ad94ae6SAl Viro if (IS_ERR(s)) 19262ad94ae6SAl Viro return PTR_ERR(s); 19272ad94ae6SAl Viro 19282ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 19292ad94ae6SAl Viro if (error) 19302ad94ae6SAl Viro putname(s); 19312ad94ae6SAl Viro else 19322ad94ae6SAl Viro *name = s; 19332ad94ae6SAl Viro 19342ad94ae6SAl Viro return error; 19352ad94ae6SAl Viro } 19362ad94ae6SAl Viro 19371da177e4SLinus Torvalds /* 19381da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 19391da177e4SLinus Torvalds * minimal. 19401da177e4SLinus Torvalds */ 19411da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 19421da177e4SLinus Torvalds { 1943da9592edSDavid Howells uid_t fsuid = current_fsuid(); 1944da9592edSDavid Howells 19451da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 19461da177e4SLinus Torvalds return 0; 1947da9592edSDavid Howells if (inode->i_uid == fsuid) 19481da177e4SLinus Torvalds return 0; 1949da9592edSDavid Howells if (dir->i_uid == fsuid) 19501da177e4SLinus Torvalds return 0; 19511da177e4SLinus Torvalds return !capable(CAP_FOWNER); 19521da177e4SLinus Torvalds } 19531da177e4SLinus Torvalds 19541da177e4SLinus Torvalds /* 19551da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 19561da177e4SLinus Torvalds * whether the type of victim is right. 19571da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 19581da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 19591da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 19601da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 19611da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 19621da177e4SLinus Torvalds * a. be owner of dir, or 19631da177e4SLinus Torvalds * b. be owner of victim, or 19641da177e4SLinus Torvalds * c. have CAP_FOWNER capability 19651da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 19661da177e4SLinus Torvalds * links pointing to it. 19671da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 19681da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 19691da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 19701da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 19711da177e4SLinus Torvalds * nfs_async_unlink(). 19721da177e4SLinus Torvalds */ 1973858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 19741da177e4SLinus Torvalds { 19751da177e4SLinus Torvalds int error; 19761da177e4SLinus Torvalds 19771da177e4SLinus Torvalds if (!victim->d_inode) 19781da177e4SLinus Torvalds return -ENOENT; 19791da177e4SLinus Torvalds 19801da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 1981cccc6bbaSAl Viro audit_inode_child(victim, dir); 19821da177e4SLinus Torvalds 1983f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 19841da177e4SLinus Torvalds if (error) 19851da177e4SLinus Torvalds return error; 19861da177e4SLinus Torvalds if (IS_APPEND(dir)) 19871da177e4SLinus Torvalds return -EPERM; 19881da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 1989f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 19901da177e4SLinus Torvalds return -EPERM; 19911da177e4SLinus Torvalds if (isdir) { 19921da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 19931da177e4SLinus Torvalds return -ENOTDIR; 19941da177e4SLinus Torvalds if (IS_ROOT(victim)) 19951da177e4SLinus Torvalds return -EBUSY; 19961da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 19971da177e4SLinus Torvalds return -EISDIR; 19981da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 19991da177e4SLinus Torvalds return -ENOENT; 20001da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 20011da177e4SLinus Torvalds return -EBUSY; 20021da177e4SLinus Torvalds return 0; 20031da177e4SLinus Torvalds } 20041da177e4SLinus Torvalds 20051da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 20061da177e4SLinus Torvalds * dir. 20071da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 20081da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 20091da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 20101da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 20111da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 20121da177e4SLinus Torvalds */ 2013a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 20141da177e4SLinus Torvalds { 20151da177e4SLinus Torvalds if (child->d_inode) 20161da177e4SLinus Torvalds return -EEXIST; 20171da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 20181da177e4SLinus Torvalds return -ENOENT; 2019f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 20201da177e4SLinus Torvalds } 20211da177e4SLinus Torvalds 20221da177e4SLinus Torvalds /* 20231da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 20241da177e4SLinus Torvalds */ 20251da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 20261da177e4SLinus Torvalds { 20271da177e4SLinus Torvalds struct dentry *p; 20281da177e4SLinus Torvalds 20291da177e4SLinus Torvalds if (p1 == p2) { 2030f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 20311da177e4SLinus Torvalds return NULL; 20321da177e4SLinus Torvalds } 20331da177e4SLinus Torvalds 2034a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 20351da177e4SLinus Torvalds 2036e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 2037e2761a11SOGAWA Hirofumi if (p) { 2038f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 2039f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 20401da177e4SLinus Torvalds return p; 20411da177e4SLinus Torvalds } 20421da177e4SLinus Torvalds 2043e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 2044e2761a11SOGAWA Hirofumi if (p) { 2045f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2046f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 20471da177e4SLinus Torvalds return p; 20481da177e4SLinus Torvalds } 20491da177e4SLinus Torvalds 2050f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2051f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 20521da177e4SLinus Torvalds return NULL; 20531da177e4SLinus Torvalds } 20541da177e4SLinus Torvalds 20551da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 20561da177e4SLinus Torvalds { 20571b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 20581da177e4SLinus Torvalds if (p1 != p2) { 20591b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 2060a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 20611da177e4SLinus Torvalds } 20621da177e4SLinus Torvalds } 20631da177e4SLinus Torvalds 20641da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode, 20651da177e4SLinus Torvalds struct nameidata *nd) 20661da177e4SLinus Torvalds { 2067a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 20681da177e4SLinus Torvalds 20691da177e4SLinus Torvalds if (error) 20701da177e4SLinus Torvalds return error; 20711da177e4SLinus Torvalds 2072acfa4380SAl Viro if (!dir->i_op->create) 20731da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 20741da177e4SLinus Torvalds mode &= S_IALLUGO; 20751da177e4SLinus Torvalds mode |= S_IFREG; 20761da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 20771da177e4SLinus Torvalds if (error) 20781da177e4SLinus Torvalds return error; 20791da177e4SLinus Torvalds error = dir->i_op->create(dir, dentry, mode, nd); 2080a74574aaSStephen Smalley if (!error) 2081f38aa942SAmy Griffis fsnotify_create(dir, dentry); 20821da177e4SLinus Torvalds return error; 20831da177e4SLinus Torvalds } 20841da177e4SLinus Torvalds 20853fb64190SChristoph Hellwig int may_open(struct path *path, int acc_mode, int flag) 20861da177e4SLinus Torvalds { 20873fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 20881da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 20891da177e4SLinus Torvalds int error; 20901da177e4SLinus Torvalds 20911da177e4SLinus Torvalds if (!inode) 20921da177e4SLinus Torvalds return -ENOENT; 20931da177e4SLinus Torvalds 2094c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2095c8fe8f30SChristoph Hellwig case S_IFLNK: 20961da177e4SLinus Torvalds return -ELOOP; 2097c8fe8f30SChristoph Hellwig case S_IFDIR: 2098c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 20991da177e4SLinus Torvalds return -EISDIR; 2100c8fe8f30SChristoph Hellwig break; 2101c8fe8f30SChristoph Hellwig case S_IFBLK: 2102c8fe8f30SChristoph Hellwig case S_IFCHR: 21033fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 21041da177e4SLinus Torvalds return -EACCES; 2105c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 2106c8fe8f30SChristoph Hellwig case S_IFIFO: 2107c8fe8f30SChristoph Hellwig case S_IFSOCK: 21081da177e4SLinus Torvalds flag &= ~O_TRUNC; 2109c8fe8f30SChristoph Hellwig break; 21104a3fd211SDave Hansen } 2111b41572e9SDave Hansen 21123fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 2113b41572e9SDave Hansen if (error) 2114b41572e9SDave Hansen return error; 21156146f0d5SMimi Zohar 21161da177e4SLinus Torvalds /* 21171da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 21181da177e4SLinus Torvalds */ 21191da177e4SLinus Torvalds if (IS_APPEND(inode)) { 21208737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 21217715b521SAl Viro return -EPERM; 21221da177e4SLinus Torvalds if (flag & O_TRUNC) 21237715b521SAl Viro return -EPERM; 21241da177e4SLinus Torvalds } 21251da177e4SLinus Torvalds 21261da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 21277715b521SAl Viro if (flag & O_NOATIME && !is_owner_or_cap(inode)) 21287715b521SAl Viro return -EPERM; 21291da177e4SLinus Torvalds 21301da177e4SLinus Torvalds /* 21311da177e4SLinus Torvalds * Ensure there are no outstanding leases on the file. 21321da177e4SLinus Torvalds */ 2133b65a9cfcSAl Viro return break_lease(inode, flag); 21347715b521SAl Viro } 21357715b521SAl Viro 2136e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 21377715b521SAl Viro { 2138e1181ee6SJeff Layton struct path *path = &filp->f_path; 21397715b521SAl Viro struct inode *inode = path->dentry->d_inode; 21407715b521SAl Viro int error = get_write_access(inode); 21411da177e4SLinus Torvalds if (error) 21427715b521SAl Viro return error; 21431da177e4SLinus Torvalds /* 21441da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 21451da177e4SLinus Torvalds */ 21461da177e4SLinus Torvalds error = locks_verify_locked(inode); 2147be6d3e56SKentaro Takeda if (!error) 2148ea0d3ab2STetsuo Handa error = security_path_truncate(path); 21491da177e4SLinus Torvalds if (!error) { 21507715b521SAl Viro error = do_truncate(path->dentry, 0, 2151d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 2152e1181ee6SJeff Layton filp); 21531da177e4SLinus Torvalds } 21541da177e4SLinus Torvalds put_write_access(inode); 2155acd0c935SMimi Zohar return error; 21561da177e4SLinus Torvalds } 21571da177e4SLinus Torvalds 2158d57999e1SDave Hansen /* 2159d57999e1SDave Hansen * Be careful about ever adding any more callers of this 2160d57999e1SDave Hansen * function. Its flags must be in the namei format, not 2161d57999e1SDave Hansen * what get passed to sys_open(). 2162d57999e1SDave Hansen */ 2163d57999e1SDave Hansen static int __open_namei_create(struct nameidata *nd, struct path *path, 21648737c930SAl Viro int open_flag, int mode) 2165aab520e2SDave Hansen { 2166aab520e2SDave Hansen int error; 21674ac91378SJan Blunck struct dentry *dir = nd->path.dentry; 2168aab520e2SDave Hansen 2169aab520e2SDave Hansen if (!IS_POSIXACL(dir->d_inode)) 2170ce3b0f8dSAl Viro mode &= ~current_umask(); 2171be6d3e56SKentaro Takeda error = security_path_mknod(&nd->path, path->dentry, mode, 0); 2172be6d3e56SKentaro Takeda if (error) 2173be6d3e56SKentaro Takeda goto out_unlock; 2174aab520e2SDave Hansen error = vfs_create(dir->d_inode, path->dentry, mode, nd); 2175be6d3e56SKentaro Takeda out_unlock: 2176aab520e2SDave Hansen mutex_unlock(&dir->d_inode->i_mutex); 21774ac91378SJan Blunck dput(nd->path.dentry); 21784ac91378SJan Blunck nd->path.dentry = path->dentry; 217931e6b01fSNick Piggin 2180aab520e2SDave Hansen if (error) 2181aab520e2SDave Hansen return error; 2182aab520e2SDave Hansen /* Don't check for write permission, don't truncate */ 21838737c930SAl Viro return may_open(&nd->path, 0, open_flag & ~O_TRUNC); 2184aab520e2SDave Hansen } 2185aab520e2SDave Hansen 21861da177e4SLinus Torvalds /* 2187d57999e1SDave Hansen * Note that while the flag value (low two bits) for sys_open means: 2188d57999e1SDave Hansen * 00 - read-only 2189d57999e1SDave Hansen * 01 - write-only 2190d57999e1SDave Hansen * 10 - read-write 2191d57999e1SDave Hansen * 11 - special 2192d57999e1SDave Hansen * it is changed into 2193d57999e1SDave Hansen * 00 - no permissions needed 2194d57999e1SDave Hansen * 01 - read-permission 2195d57999e1SDave Hansen * 10 - write-permission 2196d57999e1SDave Hansen * 11 - read-write 2197d57999e1SDave Hansen * for the internal routines (ie open_namei()/follow_link() etc) 2198d57999e1SDave Hansen * This is more logical, and also allows the 00 "no perm needed" 2199d57999e1SDave Hansen * to be used for symlinks (where the permissions are checked 2200d57999e1SDave Hansen * later). 2201d57999e1SDave Hansen * 2202d57999e1SDave Hansen */ 2203d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2204d57999e1SDave Hansen { 2205d57999e1SDave Hansen if ((flag+1) & O_ACCMODE) 2206d57999e1SDave Hansen flag++; 2207d57999e1SDave Hansen return flag; 2208d57999e1SDave Hansen } 2209d57999e1SDave Hansen 22107715b521SAl Viro static int open_will_truncate(int flag, struct inode *inode) 22114a3fd211SDave Hansen { 2212d57999e1SDave Hansen /* 22134a3fd211SDave Hansen * We'll never write to the fs underlying 22144a3fd211SDave Hansen * a device file. 22154a3fd211SDave Hansen */ 22164a3fd211SDave Hansen if (special_file(inode->i_mode)) 22174a3fd211SDave Hansen return 0; 22184a3fd211SDave Hansen return (flag & O_TRUNC); 22194a3fd211SDave Hansen } 22204a3fd211SDave Hansen 2221648fa861SAl Viro static struct file *finish_open(struct nameidata *nd, 22229a66179eSAl Viro int open_flag, int acc_mode) 2223648fa861SAl Viro { 2224648fa861SAl Viro struct file *filp; 2225648fa861SAl Viro int will_truncate; 2226648fa861SAl Viro int error; 2227648fa861SAl Viro 22289a66179eSAl Viro will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode); 2229648fa861SAl Viro if (will_truncate) { 2230648fa861SAl Viro error = mnt_want_write(nd->path.mnt); 2231648fa861SAl Viro if (error) 2232648fa861SAl Viro goto exit; 2233648fa861SAl Viro } 2234648fa861SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2235648fa861SAl Viro if (error) { 2236648fa861SAl Viro if (will_truncate) 2237648fa861SAl Viro mnt_drop_write(nd->path.mnt); 2238648fa861SAl Viro goto exit; 2239648fa861SAl Viro } 2240648fa861SAl Viro filp = nameidata_to_filp(nd); 2241648fa861SAl Viro if (!IS_ERR(filp)) { 2242648fa861SAl Viro error = ima_file_check(filp, acc_mode); 2243648fa861SAl Viro if (error) { 2244648fa861SAl Viro fput(filp); 2245648fa861SAl Viro filp = ERR_PTR(error); 2246648fa861SAl Viro } 2247648fa861SAl Viro } 2248648fa861SAl Viro if (!IS_ERR(filp)) { 2249648fa861SAl Viro if (will_truncate) { 2250e1181ee6SJeff Layton error = handle_truncate(filp); 2251648fa861SAl Viro if (error) { 2252648fa861SAl Viro fput(filp); 2253648fa861SAl Viro filp = ERR_PTR(error); 2254648fa861SAl Viro } 2255648fa861SAl Viro } 2256648fa861SAl Viro } 2257648fa861SAl Viro /* 2258648fa861SAl Viro * It is now safe to drop the mnt write 2259648fa861SAl Viro * because the filp has had a write taken 2260648fa861SAl Viro * on its behalf. 2261648fa861SAl Viro */ 2262648fa861SAl Viro if (will_truncate) 2263648fa861SAl Viro mnt_drop_write(nd->path.mnt); 2264d893f1bcSAl Viro path_put(&nd->path); 2265648fa861SAl Viro return filp; 2266648fa861SAl Viro 2267648fa861SAl Viro exit: 2268648fa861SAl Viro if (!IS_ERR(nd->intent.open.file)) 2269648fa861SAl Viro release_open_intent(nd); 2270648fa861SAl Viro path_put(&nd->path); 2271648fa861SAl Viro return ERR_PTR(error); 2272648fa861SAl Viro } 2273648fa861SAl Viro 227431e6b01fSNick Piggin /* 227531e6b01fSNick Piggin * Handle O_CREAT case for do_filp_open 227631e6b01fSNick Piggin */ 2277fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path, 22785b369df8SAl Viro int open_flag, int acc_mode, 22793e297b61SAl Viro int mode, const char *pathname) 2280fb1cc555SAl Viro { 2281a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 2282fb1cc555SAl Viro struct file *filp; 22831f36f774SAl Viro int error = -EISDIR; 2284fb1cc555SAl Viro 22851f36f774SAl Viro switch (nd->last_type) { 22861f36f774SAl Viro case LAST_DOTDOT: 22871f36f774SAl Viro follow_dotdot(nd); 22881f36f774SAl Viro dir = nd->path.dentry; 2289176306f5SNeil Brown case LAST_DOT: 2290fb045adbSNick Piggin if (need_reval_dot(dir)) { 2291f20877d9SJ. R. Okajima int status = d_revalidate(nd->path.dentry, nd); 2292f20877d9SJ. R. Okajima if (!status) 2293f20877d9SJ. R. Okajima status = -ESTALE; 2294f20877d9SJ. R. Okajima if (status < 0) { 2295f20877d9SJ. R. Okajima error = status; 2296a2c36b45SAl Viro goto exit; 22971f36f774SAl Viro } 2298f20877d9SJ. R. Okajima } 22991f36f774SAl Viro /* fallthrough */ 23001f36f774SAl Viro case LAST_ROOT: 23011f36f774SAl Viro goto exit; 23021f36f774SAl Viro case LAST_BIND: 23031f36f774SAl Viro audit_inode(pathname, dir); 23041f36f774SAl Viro goto ok; 23051f36f774SAl Viro } 2306a2c36b45SAl Viro 23071f36f774SAl Viro /* trailing slashes? */ 230831e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 23091f36f774SAl Viro goto exit; 23101f36f774SAl Viro 2311a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 2312a1e28038SAl Viro 2313a1e28038SAl Viro path->dentry = lookup_hash(nd); 2314a1e28038SAl Viro path->mnt = nd->path.mnt; 2315a1e28038SAl Viro 2316fb1cc555SAl Viro error = PTR_ERR(path->dentry); 2317fb1cc555SAl Viro if (IS_ERR(path->dentry)) { 2318fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2319fb1cc555SAl Viro goto exit; 2320fb1cc555SAl Viro } 2321fb1cc555SAl Viro 2322fb1cc555SAl Viro if (IS_ERR(nd->intent.open.file)) { 2323fb1cc555SAl Viro error = PTR_ERR(nd->intent.open.file); 2324fb1cc555SAl Viro goto exit_mutex_unlock; 2325fb1cc555SAl Viro } 2326fb1cc555SAl Viro 2327fb1cc555SAl Viro /* Negative dentry, just create the file */ 2328fb1cc555SAl Viro if (!path->dentry->d_inode) { 2329fb1cc555SAl Viro /* 2330fb1cc555SAl Viro * This write is needed to ensure that a 2331fb1cc555SAl Viro * ro->rw transition does not occur between 2332fb1cc555SAl Viro * the time when the file is created and when 2333fb1cc555SAl Viro * a permanent write count is taken through 2334fb1cc555SAl Viro * the 'struct file' in nameidata_to_filp(). 2335fb1cc555SAl Viro */ 2336fb1cc555SAl Viro error = mnt_want_write(nd->path.mnt); 2337fb1cc555SAl Viro if (error) 2338fb1cc555SAl Viro goto exit_mutex_unlock; 2339fb1cc555SAl Viro error = __open_namei_create(nd, path, open_flag, mode); 2340fb1cc555SAl Viro if (error) { 2341fb1cc555SAl Viro mnt_drop_write(nd->path.mnt); 2342fb1cc555SAl Viro goto exit; 2343fb1cc555SAl Viro } 2344fb1cc555SAl Viro filp = nameidata_to_filp(nd); 2345fb1cc555SAl Viro mnt_drop_write(nd->path.mnt); 2346d893f1bcSAl Viro path_put(&nd->path); 2347fb1cc555SAl Viro if (!IS_ERR(filp)) { 2348fb1cc555SAl Viro error = ima_file_check(filp, acc_mode); 2349fb1cc555SAl Viro if (error) { 2350fb1cc555SAl Viro fput(filp); 2351fb1cc555SAl Viro filp = ERR_PTR(error); 2352fb1cc555SAl Viro } 2353fb1cc555SAl Viro } 2354fb1cc555SAl Viro return filp; 2355fb1cc555SAl Viro } 2356fb1cc555SAl Viro 2357fb1cc555SAl Viro /* 2358fb1cc555SAl Viro * It already exists. 2359fb1cc555SAl Viro */ 2360fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2361fb1cc555SAl Viro audit_inode(pathname, path->dentry); 2362fb1cc555SAl Viro 2363fb1cc555SAl Viro error = -EEXIST; 23645b369df8SAl Viro if (open_flag & O_EXCL) 2365fb1cc555SAl Viro goto exit_dput; 2366fb1cc555SAl Viro 23679875cf80SDavid Howells error = follow_managed(path, nd->flags); 23689875cf80SDavid Howells if (error < 0) 2369fb1cc555SAl Viro goto exit_dput; 2370fb1cc555SAl Viro 2371fb1cc555SAl Viro error = -ENOENT; 2372fb1cc555SAl Viro if (!path->dentry->d_inode) 2373fb1cc555SAl Viro goto exit_dput; 23749e67f361SAl Viro 23759e67f361SAl Viro if (path->dentry->d_inode->i_op->follow_link) 2376fb1cc555SAl Viro return NULL; 2377fb1cc555SAl Viro 2378fb1cc555SAl Viro path_to_nameidata(path, nd); 237931e6b01fSNick Piggin nd->inode = path->dentry->d_inode; 2380fb1cc555SAl Viro error = -EISDIR; 238131e6b01fSNick Piggin if (S_ISDIR(nd->inode->i_mode)) 2382fb1cc555SAl Viro goto exit; 238367ee3ad2SAl Viro ok: 23849a66179eSAl Viro filp = finish_open(nd, open_flag, acc_mode); 2385fb1cc555SAl Viro return filp; 2386fb1cc555SAl Viro 2387fb1cc555SAl Viro exit_mutex_unlock: 2388fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2389fb1cc555SAl Viro exit_dput: 2390fb1cc555SAl Viro path_put_conditional(path, nd); 2391fb1cc555SAl Viro exit: 2392fb1cc555SAl Viro if (!IS_ERR(nd->intent.open.file)) 2393fb1cc555SAl Viro release_open_intent(nd); 2394fb1cc555SAl Viro path_put(&nd->path); 2395fb1cc555SAl Viro return ERR_PTR(error); 2396fb1cc555SAl Viro } 2397fb1cc555SAl Viro 23984a3fd211SDave Hansen /* 23994a3fd211SDave Hansen * Note that the low bits of the passed in "open_flag" 24004a3fd211SDave Hansen * are not the same as in the local variable "flag". See 24014a3fd211SDave Hansen * open_to_namei_flags() for more details. 24021da177e4SLinus Torvalds */ 2403a70e65dfSChristoph Hellwig struct file *do_filp_open(int dfd, const char *pathname, 24046e8341a1SAl Viro int open_flag, int mode, int acc_mode) 24051da177e4SLinus Torvalds { 24064a3fd211SDave Hansen struct file *filp; 2407a70e65dfSChristoph Hellwig struct nameidata nd; 24086e8341a1SAl Viro int error; 24099850c056SAl Viro struct path path; 24101da177e4SLinus Torvalds int count = 0; 2411d57999e1SDave Hansen int flag = open_to_namei_flags(open_flag); 241231e6b01fSNick Piggin int flags; 24131f36f774SAl Viro 24141f36f774SAl Viro if (!(open_flag & O_CREAT)) 24151f36f774SAl Viro mode = 0; 24161da177e4SLinus Torvalds 2417b1085ba8SLino Sanfilippo /* Must never be set by userspace */ 2418b1085ba8SLino Sanfilippo open_flag &= ~FMODE_NONOTIFY; 2419b1085ba8SLino Sanfilippo 24206b2f3d1fSChristoph Hellwig /* 24216b2f3d1fSChristoph Hellwig * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only 24226b2f3d1fSChristoph Hellwig * check for O_DSYNC if the need any syncing at all we enforce it's 24236b2f3d1fSChristoph Hellwig * always set instead of having to deal with possibly weird behaviour 24246b2f3d1fSChristoph Hellwig * for malicious applications setting only __O_SYNC. 24256b2f3d1fSChristoph Hellwig */ 24266b2f3d1fSChristoph Hellwig if (open_flag & __O_SYNC) 24276b2f3d1fSChristoph Hellwig open_flag |= O_DSYNC; 24286b2f3d1fSChristoph Hellwig 24296e8341a1SAl Viro if (!acc_mode) 24306d125529SAl Viro acc_mode = MAY_OPEN | ACC_MODE(open_flag); 24311da177e4SLinus Torvalds 2432834f2a4aSTrond Myklebust /* O_TRUNC implies we need access checks for write permissions */ 24334296e2cbSAl Viro if (open_flag & O_TRUNC) 2434834f2a4aSTrond Myklebust acc_mode |= MAY_WRITE; 2435834f2a4aSTrond Myklebust 24361da177e4SLinus Torvalds /* Allow the LSM permission hook to distinguish append 24371da177e4SLinus Torvalds access from general write access. */ 24384296e2cbSAl Viro if (open_flag & O_APPEND) 24391da177e4SLinus Torvalds acc_mode |= MAY_APPEND; 24401da177e4SLinus Torvalds 244131e6b01fSNick Piggin flags = LOOKUP_OPEN; 244231e6b01fSNick Piggin if (open_flag & O_CREAT) { 244331e6b01fSNick Piggin flags |= LOOKUP_CREATE; 244431e6b01fSNick Piggin if (open_flag & O_EXCL) 244531e6b01fSNick Piggin flags |= LOOKUP_EXCL; 2446654f562cSJ. R. Okajima } 244731e6b01fSNick Piggin if (open_flag & O_DIRECTORY) 244831e6b01fSNick Piggin flags |= LOOKUP_DIRECTORY; 244931e6b01fSNick Piggin if (!(open_flag & O_NOFOLLOW)) 245031e6b01fSNick Piggin flags |= LOOKUP_FOLLOW; 245131e6b01fSNick Piggin 245231e6b01fSNick Piggin filp = get_empty_filp(); 245331e6b01fSNick Piggin if (!filp) 245431e6b01fSNick Piggin return ERR_PTR(-ENFILE); 245531e6b01fSNick Piggin 245631e6b01fSNick Piggin filp->f_flags = open_flag; 245731e6b01fSNick Piggin nd.intent.open.file = filp; 245831e6b01fSNick Piggin nd.intent.open.flags = flag; 245931e6b01fSNick Piggin nd.intent.open.create_mode = mode; 246031e6b01fSNick Piggin 246131e6b01fSNick Piggin if (open_flag & O_CREAT) 246231e6b01fSNick Piggin goto creat; 246331e6b01fSNick Piggin 246431e6b01fSNick Piggin /* !O_CREAT, simple open */ 246531e6b01fSNick Piggin error = do_path_lookup(dfd, pathname, flags, &nd); 246631e6b01fSNick Piggin if (unlikely(error)) 246731e6b01fSNick Piggin goto out_filp; 246831e6b01fSNick Piggin error = -ELOOP; 246931e6b01fSNick Piggin if (!(nd.flags & LOOKUP_FOLLOW)) { 247031e6b01fSNick Piggin if (nd.inode->i_op->follow_link) 247131e6b01fSNick Piggin goto out_path; 247231e6b01fSNick Piggin } 247331e6b01fSNick Piggin error = -ENOTDIR; 247431e6b01fSNick Piggin if (nd.flags & LOOKUP_DIRECTORY) { 247531e6b01fSNick Piggin if (!nd.inode->i_op->lookup) 247631e6b01fSNick Piggin goto out_path; 247731e6b01fSNick Piggin } 247831e6b01fSNick Piggin audit_inode(pathname, nd.path.dentry); 247931e6b01fSNick Piggin filp = finish_open(&nd, open_flag, acc_mode); 248031e6b01fSNick Piggin return filp; 248131e6b01fSNick Piggin 248231e6b01fSNick Piggin creat: 248331e6b01fSNick Piggin /* OK, have to create the file. Find the parent. */ 248431e6b01fSNick Piggin error = path_init_rcu(dfd, pathname, 248531e6b01fSNick Piggin LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd); 248631e6b01fSNick Piggin if (error) 248731e6b01fSNick Piggin goto out_filp; 248831e6b01fSNick Piggin error = path_walk_rcu(pathname, &nd); 248931e6b01fSNick Piggin path_finish_rcu(&nd); 249031e6b01fSNick Piggin if (unlikely(error == -ECHILD || error == -ESTALE)) { 249131e6b01fSNick Piggin /* slower, locked walk */ 249231e6b01fSNick Piggin if (error == -ESTALE) { 249331e6b01fSNick Piggin reval: 249431e6b01fSNick Piggin flags |= LOOKUP_REVAL; 249531e6b01fSNick Piggin } 249631e6b01fSNick Piggin error = path_init(dfd, pathname, 249731e6b01fSNick Piggin LOOKUP_PARENT | (flags & LOOKUP_REVAL), &nd); 249831e6b01fSNick Piggin if (error) 249931e6b01fSNick Piggin goto out_filp; 250031e6b01fSNick Piggin 250131e6b01fSNick Piggin error = path_walk_simple(pathname, &nd); 250231e6b01fSNick Piggin } 250331e6b01fSNick Piggin if (unlikely(error)) 250431e6b01fSNick Piggin goto out_filp; 250531e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) 25069b4a9b14SAl Viro audit_inode(pathname, nd.path.dentry); 25071da177e4SLinus Torvalds 25081da177e4SLinus Torvalds /* 2509a2c36b45SAl Viro * We have the parent and last component. 25101da177e4SLinus Torvalds */ 251131e6b01fSNick Piggin nd.flags = flags; 25123e297b61SAl Viro filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname); 2513806b681cSAl Viro while (unlikely(!filp)) { /* trailing symlink */ 25147b9337aaSNick Piggin struct path link = path; 25157b9337aaSNick Piggin struct inode *linki = link.dentry->d_inode; 2516def4af30SAl Viro void *cookie; 2517806b681cSAl Viro error = -ELOOP; 2518db372915SDavid Howells if (!(nd.flags & LOOKUP_FOLLOW)) 25191f36f774SAl Viro goto exit_dput; 25201f36f774SAl Viro if (count++ == 32) 2521806b681cSAl Viro goto exit_dput; 2522806b681cSAl Viro /* 2523806b681cSAl Viro * This is subtle. Instead of calling do_follow_link() we do 2524806b681cSAl Viro * the thing by hands. The reason is that this way we have zero 2525806b681cSAl Viro * link_count and path_walk() (called from ->follow_link) 2526806b681cSAl Viro * honoring LOOKUP_PARENT. After that we have the parent and 2527806b681cSAl Viro * last component, i.e. we are in the same situation as after 2528806b681cSAl Viro * the first path_walk(). Well, almost - if the last component 2529806b681cSAl Viro * is normal we get its copy stored in nd->last.name and we will 2530806b681cSAl Viro * have to putname() it when we are done. Procfs-like symlinks 2531806b681cSAl Viro * just set LAST_BIND. 2532806b681cSAl Viro */ 2533806b681cSAl Viro nd.flags |= LOOKUP_PARENT; 25347b9337aaSNick Piggin error = security_inode_follow_link(link.dentry, &nd); 2535806b681cSAl Viro if (error) 2536806b681cSAl Viro goto exit_dput; 25377b9337aaSNick Piggin error = __do_follow_link(&link, &nd, &cookie); 2538def4af30SAl Viro if (unlikely(error)) { 25397b9337aaSNick Piggin if (!IS_ERR(cookie) && linki->i_op->put_link) 25407b9337aaSNick Piggin linki->i_op->put_link(link.dentry, &nd, cookie); 2541806b681cSAl Viro /* nd.path had been dropped */ 25427b9337aaSNick Piggin nd.path = link; 254331e6b01fSNick Piggin goto out_path; 2544806b681cSAl Viro } 2545806b681cSAl Viro nd.flags &= ~LOOKUP_PARENT; 25463e297b61SAl Viro filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname); 25477b9337aaSNick Piggin if (linki->i_op->put_link) 25487b9337aaSNick Piggin linki->i_op->put_link(link.dentry, &nd, cookie); 25497b9337aaSNick Piggin path_put(&link); 2550806b681cSAl Viro } 255110fa8e62SAl Viro out: 25522a737871SAl Viro if (nd.root.mnt) 25532a737871SAl Viro path_put(&nd.root); 255431e6b01fSNick Piggin if (filp == ERR_PTR(-ESTALE) && !(flags & LOOKUP_REVAL)) 255510fa8e62SAl Viro goto reval; 255610fa8e62SAl Viro return filp; 25571da177e4SLinus Torvalds 2558806b681cSAl Viro exit_dput: 2559806b681cSAl Viro path_put_conditional(&path, &nd); 256031e6b01fSNick Piggin out_path: 256131e6b01fSNick Piggin path_put(&nd.path); 256231e6b01fSNick Piggin out_filp: 2563806b681cSAl Viro if (!IS_ERR(nd.intent.open.file)) 2564a70e65dfSChristoph Hellwig release_open_intent(&nd); 256510fa8e62SAl Viro filp = ERR_PTR(error); 256610fa8e62SAl Viro goto out; 2567de459215SKirill Korotaev } 25681da177e4SLinus Torvalds 25691da177e4SLinus Torvalds /** 2570a70e65dfSChristoph Hellwig * filp_open - open file and return file pointer 2571a70e65dfSChristoph Hellwig * 2572a70e65dfSChristoph Hellwig * @filename: path to open 2573a70e65dfSChristoph Hellwig * @flags: open flags as per the open(2) second argument 2574a70e65dfSChristoph Hellwig * @mode: mode for the new file if O_CREAT is set, else ignored 2575a70e65dfSChristoph Hellwig * 2576a70e65dfSChristoph Hellwig * This is the helper to open a file from kernelspace if you really 2577a70e65dfSChristoph Hellwig * have to. But in generally you should not do this, so please move 2578a70e65dfSChristoph Hellwig * along, nothing to see here.. 2579a70e65dfSChristoph Hellwig */ 2580a70e65dfSChristoph Hellwig struct file *filp_open(const char *filename, int flags, int mode) 2581a70e65dfSChristoph Hellwig { 25826e8341a1SAl Viro return do_filp_open(AT_FDCWD, filename, flags, mode, 0); 2583a70e65dfSChristoph Hellwig } 2584a70e65dfSChristoph Hellwig EXPORT_SYMBOL(filp_open); 2585a70e65dfSChristoph Hellwig 2586a70e65dfSChristoph Hellwig /** 25871da177e4SLinus Torvalds * lookup_create - lookup a dentry, creating it if it doesn't exist 25881da177e4SLinus Torvalds * @nd: nameidata info 25891da177e4SLinus Torvalds * @is_dir: directory flag 25901da177e4SLinus Torvalds * 25911da177e4SLinus Torvalds * Simple function to lookup and return a dentry and create it 25921da177e4SLinus Torvalds * if it doesn't exist. Is SMP-safe. 2593c663e5d8SChristoph Hellwig * 25944ac91378SJan Blunck * Returns with nd->path.dentry->d_inode->i_mutex locked. 25951da177e4SLinus Torvalds */ 25961da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir) 25971da177e4SLinus Torvalds { 2598c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 25991da177e4SLinus Torvalds 26004ac91378SJan Blunck mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 2601c663e5d8SChristoph Hellwig /* 2602c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 2603c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 2604c663e5d8SChristoph Hellwig */ 26051da177e4SLinus Torvalds if (nd->last_type != LAST_NORM) 26061da177e4SLinus Torvalds goto fail; 26071da177e4SLinus Torvalds nd->flags &= ~LOOKUP_PARENT; 26083516586aSAl Viro nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL; 2609a634904aSASANO Masahiro nd->intent.open.flags = O_EXCL; 2610c663e5d8SChristoph Hellwig 2611c663e5d8SChristoph Hellwig /* 2612c663e5d8SChristoph Hellwig * Do the final lookup. 2613c663e5d8SChristoph Hellwig */ 261449705b77SChristoph Hellwig dentry = lookup_hash(nd); 26151da177e4SLinus Torvalds if (IS_ERR(dentry)) 26161da177e4SLinus Torvalds goto fail; 2617c663e5d8SChristoph Hellwig 2618e9baf6e5SAl Viro if (dentry->d_inode) 2619e9baf6e5SAl Viro goto eexist; 2620c663e5d8SChristoph Hellwig /* 2621c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 2622c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 2623c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 2624c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 2625c663e5d8SChristoph Hellwig */ 2626e9baf6e5SAl Viro if (unlikely(!is_dir && nd->last.name[nd->last.len])) { 26271da177e4SLinus Torvalds dput(dentry); 26281da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 2629e9baf6e5SAl Viro } 2630e9baf6e5SAl Viro return dentry; 2631e9baf6e5SAl Viro eexist: 2632e9baf6e5SAl Viro dput(dentry); 2633e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 26341da177e4SLinus Torvalds fail: 26351da177e4SLinus Torvalds return dentry; 26361da177e4SLinus Torvalds } 2637f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create); 26381da177e4SLinus Torvalds 26391da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 26401da177e4SLinus Torvalds { 2641a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 26421da177e4SLinus Torvalds 26431da177e4SLinus Torvalds if (error) 26441da177e4SLinus Torvalds return error; 26451da177e4SLinus Torvalds 26461da177e4SLinus Torvalds if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) 26471da177e4SLinus Torvalds return -EPERM; 26481da177e4SLinus Torvalds 2649acfa4380SAl Viro if (!dir->i_op->mknod) 26501da177e4SLinus Torvalds return -EPERM; 26511da177e4SLinus Torvalds 265208ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 265308ce5f16SSerge E. Hallyn if (error) 265408ce5f16SSerge E. Hallyn return error; 265508ce5f16SSerge E. Hallyn 26561da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 26571da177e4SLinus Torvalds if (error) 26581da177e4SLinus Torvalds return error; 26591da177e4SLinus Torvalds 26601da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 2661a74574aaSStephen Smalley if (!error) 2662f38aa942SAmy Griffis fsnotify_create(dir, dentry); 26631da177e4SLinus Torvalds return error; 26641da177e4SLinus Torvalds } 26651da177e4SLinus Torvalds 2666463c3197SDave Hansen static int may_mknod(mode_t mode) 2667463c3197SDave Hansen { 2668463c3197SDave Hansen switch (mode & S_IFMT) { 2669463c3197SDave Hansen case S_IFREG: 2670463c3197SDave Hansen case S_IFCHR: 2671463c3197SDave Hansen case S_IFBLK: 2672463c3197SDave Hansen case S_IFIFO: 2673463c3197SDave Hansen case S_IFSOCK: 2674463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2675463c3197SDave Hansen return 0; 2676463c3197SDave Hansen case S_IFDIR: 2677463c3197SDave Hansen return -EPERM; 2678463c3197SDave Hansen default: 2679463c3197SDave Hansen return -EINVAL; 2680463c3197SDave Hansen } 2681463c3197SDave Hansen } 2682463c3197SDave Hansen 26832e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode, 26842e4d0924SHeiko Carstens unsigned, dev) 26851da177e4SLinus Torvalds { 26862ad94ae6SAl Viro int error; 26871da177e4SLinus Torvalds char *tmp; 26881da177e4SLinus Torvalds struct dentry *dentry; 26891da177e4SLinus Torvalds struct nameidata nd; 26901da177e4SLinus Torvalds 26911da177e4SLinus Torvalds if (S_ISDIR(mode)) 26921da177e4SLinus Torvalds return -EPERM; 26931da177e4SLinus Torvalds 26942ad94ae6SAl Viro error = user_path_parent(dfd, filename, &nd, &tmp); 26951da177e4SLinus Torvalds if (error) 26962ad94ae6SAl Viro return error; 26972ad94ae6SAl Viro 26981da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 2699463c3197SDave Hansen if (IS_ERR(dentry)) { 27001da177e4SLinus Torvalds error = PTR_ERR(dentry); 2701463c3197SDave Hansen goto out_unlock; 2702463c3197SDave Hansen } 27034ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2704ce3b0f8dSAl Viro mode &= ~current_umask(); 2705463c3197SDave Hansen error = may_mknod(mode); 2706463c3197SDave Hansen if (error) 2707463c3197SDave Hansen goto out_dput; 2708463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2709463c3197SDave Hansen if (error) 2710463c3197SDave Hansen goto out_dput; 2711be6d3e56SKentaro Takeda error = security_path_mknod(&nd.path, dentry, mode, dev); 2712be6d3e56SKentaro Takeda if (error) 2713be6d3e56SKentaro Takeda goto out_drop_write; 27141da177e4SLinus Torvalds switch (mode & S_IFMT) { 27151da177e4SLinus Torvalds case 0: case S_IFREG: 27164ac91378SJan Blunck error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd); 27171da177e4SLinus Torvalds break; 27181da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 27194ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode, 27201da177e4SLinus Torvalds new_decode_dev(dev)); 27211da177e4SLinus Torvalds break; 27221da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 27234ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0); 27241da177e4SLinus Torvalds break; 27251da177e4SLinus Torvalds } 2726be6d3e56SKentaro Takeda out_drop_write: 2727463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2728463c3197SDave Hansen out_dput: 27291da177e4SLinus Torvalds dput(dentry); 2730463c3197SDave Hansen out_unlock: 27314ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 27321d957f9bSJan Blunck path_put(&nd.path); 27331da177e4SLinus Torvalds putname(tmp); 27341da177e4SLinus Torvalds 27351da177e4SLinus Torvalds return error; 27361da177e4SLinus Torvalds } 27371da177e4SLinus Torvalds 27383480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev) 27395590ff0dSUlrich Drepper { 27405590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 27415590ff0dSUlrich Drepper } 27425590ff0dSUlrich Drepper 27431da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 27441da177e4SLinus Torvalds { 2745a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 27461da177e4SLinus Torvalds 27471da177e4SLinus Torvalds if (error) 27481da177e4SLinus Torvalds return error; 27491da177e4SLinus Torvalds 2750acfa4380SAl Viro if (!dir->i_op->mkdir) 27511da177e4SLinus Torvalds return -EPERM; 27521da177e4SLinus Torvalds 27531da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 27541da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 27551da177e4SLinus Torvalds if (error) 27561da177e4SLinus Torvalds return error; 27571da177e4SLinus Torvalds 27581da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2759a74574aaSStephen Smalley if (!error) 2760f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 27611da177e4SLinus Torvalds return error; 27621da177e4SLinus Torvalds } 27631da177e4SLinus Torvalds 27642e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode) 27651da177e4SLinus Torvalds { 27661da177e4SLinus Torvalds int error = 0; 27671da177e4SLinus Torvalds char * tmp; 27686902d925SDave Hansen struct dentry *dentry; 27696902d925SDave Hansen struct nameidata nd; 27701da177e4SLinus Torvalds 27712ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &tmp); 27722ad94ae6SAl Viro if (error) 27736902d925SDave Hansen goto out_err; 27741da177e4SLinus Torvalds 27751da177e4SLinus Torvalds dentry = lookup_create(&nd, 1); 27761da177e4SLinus Torvalds error = PTR_ERR(dentry); 27776902d925SDave Hansen if (IS_ERR(dentry)) 27786902d925SDave Hansen goto out_unlock; 27796902d925SDave Hansen 27804ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2781ce3b0f8dSAl Viro mode &= ~current_umask(); 2782463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2783463c3197SDave Hansen if (error) 2784463c3197SDave Hansen goto out_dput; 2785be6d3e56SKentaro Takeda error = security_path_mkdir(&nd.path, dentry, mode); 2786be6d3e56SKentaro Takeda if (error) 2787be6d3e56SKentaro Takeda goto out_drop_write; 27884ac91378SJan Blunck error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode); 2789be6d3e56SKentaro Takeda out_drop_write: 2790463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2791463c3197SDave Hansen out_dput: 27921da177e4SLinus Torvalds dput(dentry); 27936902d925SDave Hansen out_unlock: 27944ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 27951d957f9bSJan Blunck path_put(&nd.path); 27961da177e4SLinus Torvalds putname(tmp); 27976902d925SDave Hansen out_err: 27981da177e4SLinus Torvalds return error; 27991da177e4SLinus Torvalds } 28001da177e4SLinus Torvalds 28013cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode) 28025590ff0dSUlrich Drepper { 28035590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 28045590ff0dSUlrich Drepper } 28055590ff0dSUlrich Drepper 28061da177e4SLinus Torvalds /* 28071da177e4SLinus Torvalds * We try to drop the dentry early: we should have 28081da177e4SLinus Torvalds * a usage count of 2 if we're the only user of this 28091da177e4SLinus Torvalds * dentry, and if that is true (possibly after pruning 28101da177e4SLinus Torvalds * the dcache), then we drop the dentry now. 28111da177e4SLinus Torvalds * 28121da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 28131da177e4SLinus Torvalds * do a 28141da177e4SLinus Torvalds * 28151da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 28161da177e4SLinus Torvalds * return -EBUSY; 28171da177e4SLinus Torvalds * 28181da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 28191da177e4SLinus Torvalds * that is still in use by something else.. 28201da177e4SLinus Torvalds */ 28211da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 28221da177e4SLinus Torvalds { 28231da177e4SLinus Torvalds dget(dentry); 28241da177e4SLinus Torvalds shrink_dcache_parent(dentry); 28251da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 2826b7ab39f6SNick Piggin if (dentry->d_count == 2) 28271da177e4SLinus Torvalds __d_drop(dentry); 28281da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 28291da177e4SLinus Torvalds } 28301da177e4SLinus Torvalds 28311da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 28321da177e4SLinus Torvalds { 28331da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 28341da177e4SLinus Torvalds 28351da177e4SLinus Torvalds if (error) 28361da177e4SLinus Torvalds return error; 28371da177e4SLinus Torvalds 2838acfa4380SAl Viro if (!dir->i_op->rmdir) 28391da177e4SLinus Torvalds return -EPERM; 28401da177e4SLinus Torvalds 28411b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 28421da177e4SLinus Torvalds dentry_unhash(dentry); 28431da177e4SLinus Torvalds if (d_mountpoint(dentry)) 28441da177e4SLinus Torvalds error = -EBUSY; 28451da177e4SLinus Torvalds else { 28461da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 28471da177e4SLinus Torvalds if (!error) { 28481da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 2849d83c49f3SAl Viro if (!error) { 28501da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 2851d83c49f3SAl Viro dont_mount(dentry); 2852d83c49f3SAl Viro } 28531da177e4SLinus Torvalds } 28541da177e4SLinus Torvalds } 28551b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 28561da177e4SLinus Torvalds if (!error) { 28571da177e4SLinus Torvalds d_delete(dentry); 28581da177e4SLinus Torvalds } 28591da177e4SLinus Torvalds dput(dentry); 28601da177e4SLinus Torvalds 28611da177e4SLinus Torvalds return error; 28621da177e4SLinus Torvalds } 28631da177e4SLinus Torvalds 28645590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 28651da177e4SLinus Torvalds { 28661da177e4SLinus Torvalds int error = 0; 28671da177e4SLinus Torvalds char * name; 28681da177e4SLinus Torvalds struct dentry *dentry; 28691da177e4SLinus Torvalds struct nameidata nd; 28701da177e4SLinus Torvalds 28712ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 28721da177e4SLinus Torvalds if (error) 28732ad94ae6SAl Viro return error; 28741da177e4SLinus Torvalds 28751da177e4SLinus Torvalds switch(nd.last_type) { 28761da177e4SLinus Torvalds case LAST_DOTDOT: 28771da177e4SLinus Torvalds error = -ENOTEMPTY; 28781da177e4SLinus Torvalds goto exit1; 28791da177e4SLinus Torvalds case LAST_DOT: 28801da177e4SLinus Torvalds error = -EINVAL; 28811da177e4SLinus Torvalds goto exit1; 28821da177e4SLinus Torvalds case LAST_ROOT: 28831da177e4SLinus Torvalds error = -EBUSY; 28841da177e4SLinus Torvalds goto exit1; 28851da177e4SLinus Torvalds } 28860612d9fbSOGAWA Hirofumi 28870612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 28880612d9fbSOGAWA Hirofumi 28894ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 289049705b77SChristoph Hellwig dentry = lookup_hash(&nd); 28911da177e4SLinus Torvalds error = PTR_ERR(dentry); 28926902d925SDave Hansen if (IS_ERR(dentry)) 28936902d925SDave Hansen goto exit2; 28940622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 28950622753bSDave Hansen if (error) 28960622753bSDave Hansen goto exit3; 2897be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 2898be6d3e56SKentaro Takeda if (error) 2899be6d3e56SKentaro Takeda goto exit4; 29004ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 2901be6d3e56SKentaro Takeda exit4: 29020622753bSDave Hansen mnt_drop_write(nd.path.mnt); 29030622753bSDave Hansen exit3: 29041da177e4SLinus Torvalds dput(dentry); 29056902d925SDave Hansen exit2: 29064ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 29071da177e4SLinus Torvalds exit1: 29081d957f9bSJan Blunck path_put(&nd.path); 29091da177e4SLinus Torvalds putname(name); 29101da177e4SLinus Torvalds return error; 29111da177e4SLinus Torvalds } 29121da177e4SLinus Torvalds 29133cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 29145590ff0dSUlrich Drepper { 29155590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 29165590ff0dSUlrich Drepper } 29175590ff0dSUlrich Drepper 29181da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 29191da177e4SLinus Torvalds { 29201da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 29211da177e4SLinus Torvalds 29221da177e4SLinus Torvalds if (error) 29231da177e4SLinus Torvalds return error; 29241da177e4SLinus Torvalds 2925acfa4380SAl Viro if (!dir->i_op->unlink) 29261da177e4SLinus Torvalds return -EPERM; 29271da177e4SLinus Torvalds 29281b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 29291da177e4SLinus Torvalds if (d_mountpoint(dentry)) 29301da177e4SLinus Torvalds error = -EBUSY; 29311da177e4SLinus Torvalds else { 29321da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 2933bec1052eSAl Viro if (!error) { 29341da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 2935bec1052eSAl Viro if (!error) 2936d83c49f3SAl Viro dont_mount(dentry); 2937bec1052eSAl Viro } 29381da177e4SLinus Torvalds } 29391b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 29401da177e4SLinus Torvalds 29411da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 29421da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 2943ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 29441da177e4SLinus Torvalds d_delete(dentry); 29451da177e4SLinus Torvalds } 29460eeca283SRobert Love 29471da177e4SLinus Torvalds return error; 29481da177e4SLinus Torvalds } 29491da177e4SLinus Torvalds 29501da177e4SLinus Torvalds /* 29511da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 29521b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 29531da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 29541da177e4SLinus Torvalds * while waiting on the I/O. 29551da177e4SLinus Torvalds */ 29565590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 29571da177e4SLinus Torvalds { 29582ad94ae6SAl Viro int error; 29591da177e4SLinus Torvalds char *name; 29601da177e4SLinus Torvalds struct dentry *dentry; 29611da177e4SLinus Torvalds struct nameidata nd; 29621da177e4SLinus Torvalds struct inode *inode = NULL; 29631da177e4SLinus Torvalds 29642ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 29651da177e4SLinus Torvalds if (error) 29662ad94ae6SAl Viro return error; 29672ad94ae6SAl Viro 29681da177e4SLinus Torvalds error = -EISDIR; 29691da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 29701da177e4SLinus Torvalds goto exit1; 29710612d9fbSOGAWA Hirofumi 29720612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 29730612d9fbSOGAWA Hirofumi 29744ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 297549705b77SChristoph Hellwig dentry = lookup_hash(&nd); 29761da177e4SLinus Torvalds error = PTR_ERR(dentry); 29771da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 29781da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 29791da177e4SLinus Torvalds if (nd.last.name[nd.last.len]) 29801da177e4SLinus Torvalds goto slashes; 29811da177e4SLinus Torvalds inode = dentry->d_inode; 29821da177e4SLinus Torvalds if (inode) 29837de9c6eeSAl Viro ihold(inode); 29840622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 29850622753bSDave Hansen if (error) 29860622753bSDave Hansen goto exit2; 2987be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 2988be6d3e56SKentaro Takeda if (error) 2989be6d3e56SKentaro Takeda goto exit3; 29904ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 2991be6d3e56SKentaro Takeda exit3: 29920622753bSDave Hansen mnt_drop_write(nd.path.mnt); 29931da177e4SLinus Torvalds exit2: 29941da177e4SLinus Torvalds dput(dentry); 29951da177e4SLinus Torvalds } 29964ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 29971da177e4SLinus Torvalds if (inode) 29981da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 29991da177e4SLinus Torvalds exit1: 30001d957f9bSJan Blunck path_put(&nd.path); 30011da177e4SLinus Torvalds putname(name); 30021da177e4SLinus Torvalds return error; 30031da177e4SLinus Torvalds 30041da177e4SLinus Torvalds slashes: 30051da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 30061da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 30071da177e4SLinus Torvalds goto exit2; 30081da177e4SLinus Torvalds } 30091da177e4SLinus Torvalds 30102e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 30115590ff0dSUlrich Drepper { 30125590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 30135590ff0dSUlrich Drepper return -EINVAL; 30145590ff0dSUlrich Drepper 30155590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 30165590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 30175590ff0dSUlrich Drepper 30185590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 30195590ff0dSUlrich Drepper } 30205590ff0dSUlrich Drepper 30213480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 30225590ff0dSUlrich Drepper { 30235590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 30245590ff0dSUlrich Drepper } 30255590ff0dSUlrich Drepper 3026db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 30271da177e4SLinus Torvalds { 3028a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 30291da177e4SLinus Torvalds 30301da177e4SLinus Torvalds if (error) 30311da177e4SLinus Torvalds return error; 30321da177e4SLinus Torvalds 3033acfa4380SAl Viro if (!dir->i_op->symlink) 30341da177e4SLinus Torvalds return -EPERM; 30351da177e4SLinus Torvalds 30361da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 30371da177e4SLinus Torvalds if (error) 30381da177e4SLinus Torvalds return error; 30391da177e4SLinus Torvalds 30401da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 3041a74574aaSStephen Smalley if (!error) 3042f38aa942SAmy Griffis fsnotify_create(dir, dentry); 30431da177e4SLinus Torvalds return error; 30441da177e4SLinus Torvalds } 30451da177e4SLinus Torvalds 30462e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 30472e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 30481da177e4SLinus Torvalds { 30492ad94ae6SAl Viro int error; 30501da177e4SLinus Torvalds char *from; 30511da177e4SLinus Torvalds char *to; 30526902d925SDave Hansen struct dentry *dentry; 30536902d925SDave Hansen struct nameidata nd; 30541da177e4SLinus Torvalds 30551da177e4SLinus Torvalds from = getname(oldname); 30561da177e4SLinus Torvalds if (IS_ERR(from)) 30571da177e4SLinus Torvalds return PTR_ERR(from); 30582ad94ae6SAl Viro 30592ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 30602ad94ae6SAl Viro if (error) 30616902d925SDave Hansen goto out_putname; 30621da177e4SLinus Torvalds 30631da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 30641da177e4SLinus Torvalds error = PTR_ERR(dentry); 30656902d925SDave Hansen if (IS_ERR(dentry)) 30666902d925SDave Hansen goto out_unlock; 30676902d925SDave Hansen 306875c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 306975c3f29dSDave Hansen if (error) 307075c3f29dSDave Hansen goto out_dput; 3071be6d3e56SKentaro Takeda error = security_path_symlink(&nd.path, dentry, from); 3072be6d3e56SKentaro Takeda if (error) 3073be6d3e56SKentaro Takeda goto out_drop_write; 3074db2e747bSMiklos Szeredi error = vfs_symlink(nd.path.dentry->d_inode, dentry, from); 3075be6d3e56SKentaro Takeda out_drop_write: 307675c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 307775c3f29dSDave Hansen out_dput: 30781da177e4SLinus Torvalds dput(dentry); 30796902d925SDave Hansen out_unlock: 30804ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 30811d957f9bSJan Blunck path_put(&nd.path); 30821da177e4SLinus Torvalds putname(to); 30836902d925SDave Hansen out_putname: 30841da177e4SLinus Torvalds putname(from); 30851da177e4SLinus Torvalds return error; 30861da177e4SLinus Torvalds } 30871da177e4SLinus Torvalds 30883480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 30895590ff0dSUlrich Drepper { 30905590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 30915590ff0dSUlrich Drepper } 30925590ff0dSUlrich Drepper 30931da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 30941da177e4SLinus Torvalds { 30951da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 30961da177e4SLinus Torvalds int error; 30971da177e4SLinus Torvalds 30981da177e4SLinus Torvalds if (!inode) 30991da177e4SLinus Torvalds return -ENOENT; 31001da177e4SLinus Torvalds 3101a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 31021da177e4SLinus Torvalds if (error) 31031da177e4SLinus Torvalds return error; 31041da177e4SLinus Torvalds 31051da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 31061da177e4SLinus Torvalds return -EXDEV; 31071da177e4SLinus Torvalds 31081da177e4SLinus Torvalds /* 31091da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 31101da177e4SLinus Torvalds */ 31111da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 31121da177e4SLinus Torvalds return -EPERM; 3113acfa4380SAl Viro if (!dir->i_op->link) 31141da177e4SLinus Torvalds return -EPERM; 31157e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 31161da177e4SLinus Torvalds return -EPERM; 31171da177e4SLinus Torvalds 31181da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 31191da177e4SLinus Torvalds if (error) 31201da177e4SLinus Torvalds return error; 31211da177e4SLinus Torvalds 31227e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 31231da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 31247e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 3125e31e14ecSStephen Smalley if (!error) 31267e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 31271da177e4SLinus Torvalds return error; 31281da177e4SLinus Torvalds } 31291da177e4SLinus Torvalds 31301da177e4SLinus Torvalds /* 31311da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 31321da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 31331da177e4SLinus Torvalds * newname. --KAB 31341da177e4SLinus Torvalds * 31351da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 31361da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 31371da177e4SLinus Torvalds * and other special files. --ADM 31381da177e4SLinus Torvalds */ 31392e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 31402e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 31411da177e4SLinus Torvalds { 31421da177e4SLinus Torvalds struct dentry *new_dentry; 31432d8f3038SAl Viro struct nameidata nd; 31442d8f3038SAl Viro struct path old_path; 31451da177e4SLinus Torvalds int error; 31461da177e4SLinus Torvalds char *to; 31471da177e4SLinus Torvalds 314845c9b11aSUlrich Drepper if ((flags & ~AT_SYMLINK_FOLLOW) != 0) 3149c04030e1SUlrich Drepper return -EINVAL; 3150c04030e1SUlrich Drepper 31512d8f3038SAl Viro error = user_path_at(olddfd, oldname, 315245c9b11aSUlrich Drepper flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0, 31532d8f3038SAl Viro &old_path); 31541da177e4SLinus Torvalds if (error) 31552ad94ae6SAl Viro return error; 31562ad94ae6SAl Viro 31572ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 31581da177e4SLinus Torvalds if (error) 31591da177e4SLinus Torvalds goto out; 31601da177e4SLinus Torvalds error = -EXDEV; 31612d8f3038SAl Viro if (old_path.mnt != nd.path.mnt) 31621da177e4SLinus Torvalds goto out_release; 31631da177e4SLinus Torvalds new_dentry = lookup_create(&nd, 0); 31641da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 31656902d925SDave Hansen if (IS_ERR(new_dentry)) 31666902d925SDave Hansen goto out_unlock; 316775c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 316875c3f29dSDave Hansen if (error) 316975c3f29dSDave Hansen goto out_dput; 3170be6d3e56SKentaro Takeda error = security_path_link(old_path.dentry, &nd.path, new_dentry); 3171be6d3e56SKentaro Takeda if (error) 3172be6d3e56SKentaro Takeda goto out_drop_write; 31732d8f3038SAl Viro error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry); 3174be6d3e56SKentaro Takeda out_drop_write: 317575c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 317675c3f29dSDave Hansen out_dput: 31771da177e4SLinus Torvalds dput(new_dentry); 31786902d925SDave Hansen out_unlock: 31794ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 31801da177e4SLinus Torvalds out_release: 31811d957f9bSJan Blunck path_put(&nd.path); 31822ad94ae6SAl Viro putname(to); 31831da177e4SLinus Torvalds out: 31842d8f3038SAl Viro path_put(&old_path); 31851da177e4SLinus Torvalds 31861da177e4SLinus Torvalds return error; 31871da177e4SLinus Torvalds } 31881da177e4SLinus Torvalds 31893480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 31905590ff0dSUlrich Drepper { 3191c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 31925590ff0dSUlrich Drepper } 31935590ff0dSUlrich Drepper 31941da177e4SLinus Torvalds /* 31951da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 31961da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 31971da177e4SLinus Torvalds * Problems: 31981da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 31991da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 32001da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 3201a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 32021da177e4SLinus Torvalds * story. 32031da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 32041b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 32051da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 32061da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 3207a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 32081da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 32091da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 32101da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 3211a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 32121da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 32131da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 32141da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 32151da177e4SLinus Torvalds * d) some filesystems don't support opened-but-unlinked directories, 32161da177e4SLinus Torvalds * either because of layout or because they are not ready to deal with 32171da177e4SLinus Torvalds * all cases correctly. The latter will be fixed (taking this sort of 32181da177e4SLinus Torvalds * stuff into VFS), but the former is not going away. Solution: the same 32191da177e4SLinus Torvalds * trick as in rmdir(). 32201da177e4SLinus Torvalds * e) conversion from fhandle to dentry may come in the wrong moment - when 32211b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 32221da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 3223c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 32241da177e4SLinus Torvalds * locking]. 32251da177e4SLinus Torvalds */ 322675c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 32271da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 32281da177e4SLinus Torvalds { 32291da177e4SLinus Torvalds int error = 0; 32301da177e4SLinus Torvalds struct inode *target; 32311da177e4SLinus Torvalds 32321da177e4SLinus Torvalds /* 32331da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 32341da177e4SLinus Torvalds * we'll need to flip '..'. 32351da177e4SLinus Torvalds */ 32361da177e4SLinus Torvalds if (new_dir != old_dir) { 3237f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 32381da177e4SLinus Torvalds if (error) 32391da177e4SLinus Torvalds return error; 32401da177e4SLinus Torvalds } 32411da177e4SLinus Torvalds 32421da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 32431da177e4SLinus Torvalds if (error) 32441da177e4SLinus Torvalds return error; 32451da177e4SLinus Torvalds 32461da177e4SLinus Torvalds target = new_dentry->d_inode; 3247d83c49f3SAl Viro if (target) 32481b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 32491da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 32501da177e4SLinus Torvalds error = -EBUSY; 3251d83c49f3SAl Viro else { 3252d83c49f3SAl Viro if (target) 3253d83c49f3SAl Viro dentry_unhash(new_dentry); 32541da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 3255d83c49f3SAl Viro } 32561da177e4SLinus Torvalds if (target) { 3257d83c49f3SAl Viro if (!error) { 32581da177e4SLinus Torvalds target->i_flags |= S_DEAD; 3259d83c49f3SAl Viro dont_mount(new_dentry); 3260d83c49f3SAl Viro } 32611b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 32621da177e4SLinus Torvalds if (d_unhashed(new_dentry)) 32631da177e4SLinus Torvalds d_rehash(new_dentry); 32641da177e4SLinus Torvalds dput(new_dentry); 32651da177e4SLinus Torvalds } 3266e31e14ecSStephen Smalley if (!error) 3267349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 32681da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 32691da177e4SLinus Torvalds return error; 32701da177e4SLinus Torvalds } 32711da177e4SLinus Torvalds 327275c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 32731da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 32741da177e4SLinus Torvalds { 32751da177e4SLinus Torvalds struct inode *target; 32761da177e4SLinus Torvalds int error; 32771da177e4SLinus Torvalds 32781da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 32791da177e4SLinus Torvalds if (error) 32801da177e4SLinus Torvalds return error; 32811da177e4SLinus Torvalds 32821da177e4SLinus Torvalds dget(new_dentry); 32831da177e4SLinus Torvalds target = new_dentry->d_inode; 32841da177e4SLinus Torvalds if (target) 32851b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 32861da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 32871da177e4SLinus Torvalds error = -EBUSY; 32881da177e4SLinus Torvalds else 32891da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 32901da177e4SLinus Torvalds if (!error) { 3291bec1052eSAl Viro if (target) 3292d83c49f3SAl Viro dont_mount(new_dentry); 3293349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 32941da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 32951da177e4SLinus Torvalds } 32961da177e4SLinus Torvalds if (target) 32971b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 32981da177e4SLinus Torvalds dput(new_dentry); 32991da177e4SLinus Torvalds return error; 33001da177e4SLinus Torvalds } 33011da177e4SLinus Torvalds 33021da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 33031da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 33041da177e4SLinus Torvalds { 33051da177e4SLinus Torvalds int error; 33061da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 330759b0df21SEric Paris const unsigned char *old_name; 33081da177e4SLinus Torvalds 33091da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 33101da177e4SLinus Torvalds return 0; 33111da177e4SLinus Torvalds 33121da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 33131da177e4SLinus Torvalds if (error) 33141da177e4SLinus Torvalds return error; 33151da177e4SLinus Torvalds 33161da177e4SLinus Torvalds if (!new_dentry->d_inode) 3317a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 33181da177e4SLinus Torvalds else 33191da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 33201da177e4SLinus Torvalds if (error) 33211da177e4SLinus Torvalds return error; 33221da177e4SLinus Torvalds 3323acfa4380SAl Viro if (!old_dir->i_op->rename) 33241da177e4SLinus Torvalds return -EPERM; 33251da177e4SLinus Torvalds 33260eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 33270eeca283SRobert Love 33281da177e4SLinus Torvalds if (is_dir) 33291da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 33301da177e4SLinus Torvalds else 33311da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3332123df294SAl Viro if (!error) 3333123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 33345a190ae6SAl Viro new_dentry->d_inode, old_dentry); 33350eeca283SRobert Love fsnotify_oldname_free(old_name); 33360eeca283SRobert Love 33371da177e4SLinus Torvalds return error; 33381da177e4SLinus Torvalds } 33391da177e4SLinus Torvalds 33402e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 33412e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 33421da177e4SLinus Torvalds { 33431da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 33441da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 33451da177e4SLinus Torvalds struct dentry *trap; 33461da177e4SLinus Torvalds struct nameidata oldnd, newnd; 33472ad94ae6SAl Viro char *from; 33482ad94ae6SAl Viro char *to; 33492ad94ae6SAl Viro int error; 33501da177e4SLinus Torvalds 33512ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 33521da177e4SLinus Torvalds if (error) 33531da177e4SLinus Torvalds goto exit; 33541da177e4SLinus Torvalds 33552ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 33561da177e4SLinus Torvalds if (error) 33571da177e4SLinus Torvalds goto exit1; 33581da177e4SLinus Torvalds 33591da177e4SLinus Torvalds error = -EXDEV; 33604ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 33611da177e4SLinus Torvalds goto exit2; 33621da177e4SLinus Torvalds 33634ac91378SJan Blunck old_dir = oldnd.path.dentry; 33641da177e4SLinus Torvalds error = -EBUSY; 33651da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 33661da177e4SLinus Torvalds goto exit2; 33671da177e4SLinus Torvalds 33684ac91378SJan Blunck new_dir = newnd.path.dentry; 33691da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 33701da177e4SLinus Torvalds goto exit2; 33711da177e4SLinus Torvalds 33720612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 33730612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 33744e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 33750612d9fbSOGAWA Hirofumi 33761da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 33771da177e4SLinus Torvalds 337849705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 33791da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 33801da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 33811da177e4SLinus Torvalds goto exit3; 33821da177e4SLinus Torvalds /* source must exist */ 33831da177e4SLinus Torvalds error = -ENOENT; 33841da177e4SLinus Torvalds if (!old_dentry->d_inode) 33851da177e4SLinus Torvalds goto exit4; 33861da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 33871da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 33881da177e4SLinus Torvalds error = -ENOTDIR; 33891da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 33901da177e4SLinus Torvalds goto exit4; 33911da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 33921da177e4SLinus Torvalds goto exit4; 33931da177e4SLinus Torvalds } 33941da177e4SLinus Torvalds /* source should not be ancestor of target */ 33951da177e4SLinus Torvalds error = -EINVAL; 33961da177e4SLinus Torvalds if (old_dentry == trap) 33971da177e4SLinus Torvalds goto exit4; 339849705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 33991da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 34001da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 34011da177e4SLinus Torvalds goto exit4; 34021da177e4SLinus Torvalds /* target should not be an ancestor of source */ 34031da177e4SLinus Torvalds error = -ENOTEMPTY; 34041da177e4SLinus Torvalds if (new_dentry == trap) 34051da177e4SLinus Torvalds goto exit5; 34061da177e4SLinus Torvalds 34079079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 34089079b1ebSDave Hansen if (error) 34099079b1ebSDave Hansen goto exit5; 3410be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3411be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3412be6d3e56SKentaro Takeda if (error) 3413be6d3e56SKentaro Takeda goto exit6; 34141da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 34151da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 3416be6d3e56SKentaro Takeda exit6: 34179079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 34181da177e4SLinus Torvalds exit5: 34191da177e4SLinus Torvalds dput(new_dentry); 34201da177e4SLinus Torvalds exit4: 34211da177e4SLinus Torvalds dput(old_dentry); 34221da177e4SLinus Torvalds exit3: 34231da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 34241da177e4SLinus Torvalds exit2: 34251d957f9bSJan Blunck path_put(&newnd.path); 34262ad94ae6SAl Viro putname(to); 34271da177e4SLinus Torvalds exit1: 34281d957f9bSJan Blunck path_put(&oldnd.path); 34291da177e4SLinus Torvalds putname(from); 34302ad94ae6SAl Viro exit: 34311da177e4SLinus Torvalds return error; 34321da177e4SLinus Torvalds } 34331da177e4SLinus Torvalds 3434a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 34355590ff0dSUlrich Drepper { 34365590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 34375590ff0dSUlrich Drepper } 34385590ff0dSUlrich Drepper 34391da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 34401da177e4SLinus Torvalds { 34411da177e4SLinus Torvalds int len; 34421da177e4SLinus Torvalds 34431da177e4SLinus Torvalds len = PTR_ERR(link); 34441da177e4SLinus Torvalds if (IS_ERR(link)) 34451da177e4SLinus Torvalds goto out; 34461da177e4SLinus Torvalds 34471da177e4SLinus Torvalds len = strlen(link); 34481da177e4SLinus Torvalds if (len > (unsigned) buflen) 34491da177e4SLinus Torvalds len = buflen; 34501da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 34511da177e4SLinus Torvalds len = -EFAULT; 34521da177e4SLinus Torvalds out: 34531da177e4SLinus Torvalds return len; 34541da177e4SLinus Torvalds } 34551da177e4SLinus Torvalds 34561da177e4SLinus Torvalds /* 34571da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 34581da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 34591da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 34601da177e4SLinus Torvalds */ 34611da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 34621da177e4SLinus Torvalds { 34631da177e4SLinus Torvalds struct nameidata nd; 3464cc314eefSLinus Torvalds void *cookie; 3465694a1764SMarcin Slusarz int res; 3466cc314eefSLinus Torvalds 34671da177e4SLinus Torvalds nd.depth = 0; 3468cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3469694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3470694a1764SMarcin Slusarz return PTR_ERR(cookie); 3471694a1764SMarcin Slusarz 3472694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 34731da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3474cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3475694a1764SMarcin Slusarz return res; 34761da177e4SLinus Torvalds } 34771da177e4SLinus Torvalds 34781da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 34791da177e4SLinus Torvalds { 34801da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 34811da177e4SLinus Torvalds } 34821da177e4SLinus Torvalds 34831da177e4SLinus Torvalds /* get the link contents into pagecache */ 34841da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 34851da177e4SLinus Torvalds { 3486ebd09abbSDuane Griffin char *kaddr; 34871da177e4SLinus Torvalds struct page *page; 34881da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3489090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 34901da177e4SLinus Torvalds if (IS_ERR(page)) 34916fe6900eSNick Piggin return (char*)page; 34921da177e4SLinus Torvalds *ppage = page; 3493ebd09abbSDuane Griffin kaddr = kmap(page); 3494ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3495ebd09abbSDuane Griffin return kaddr; 34961da177e4SLinus Torvalds } 34971da177e4SLinus Torvalds 34981da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 34991da177e4SLinus Torvalds { 35001da177e4SLinus Torvalds struct page *page = NULL; 35011da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 35021da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 35031da177e4SLinus Torvalds if (page) { 35041da177e4SLinus Torvalds kunmap(page); 35051da177e4SLinus Torvalds page_cache_release(page); 35061da177e4SLinus Torvalds } 35071da177e4SLinus Torvalds return res; 35081da177e4SLinus Torvalds } 35091da177e4SLinus Torvalds 3510cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 35111da177e4SLinus Torvalds { 3512cc314eefSLinus Torvalds struct page *page = NULL; 35131da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 3514cc314eefSLinus Torvalds return page; 35151da177e4SLinus Torvalds } 35161da177e4SLinus Torvalds 3517cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 35181da177e4SLinus Torvalds { 3519cc314eefSLinus Torvalds struct page *page = cookie; 3520cc314eefSLinus Torvalds 3521cc314eefSLinus Torvalds if (page) { 35221da177e4SLinus Torvalds kunmap(page); 35231da177e4SLinus Torvalds page_cache_release(page); 35241da177e4SLinus Torvalds } 35251da177e4SLinus Torvalds } 35261da177e4SLinus Torvalds 352754566b2cSNick Piggin /* 352854566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 352954566b2cSNick Piggin */ 353054566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 35311da177e4SLinus Torvalds { 35321da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 35330adb25d2SKirill Korotaev struct page *page; 3534afddba49SNick Piggin void *fsdata; 3535beb497abSDmitriy Monakhov int err; 35361da177e4SLinus Torvalds char *kaddr; 353754566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 353854566b2cSNick Piggin if (nofs) 353954566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 35401da177e4SLinus Torvalds 35417e53cac4SNeilBrown retry: 3542afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 354354566b2cSNick Piggin flags, &page, &fsdata); 35441da177e4SLinus Torvalds if (err) 3545afddba49SNick Piggin goto fail; 3546afddba49SNick Piggin 35471da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 35481da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 35491da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 3550afddba49SNick Piggin 3551afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 3552afddba49SNick Piggin page, fsdata); 35531da177e4SLinus Torvalds if (err < 0) 35541da177e4SLinus Torvalds goto fail; 3555afddba49SNick Piggin if (err < len-1) 3556afddba49SNick Piggin goto retry; 3557afddba49SNick Piggin 35581da177e4SLinus Torvalds mark_inode_dirty(inode); 35591da177e4SLinus Torvalds return 0; 35601da177e4SLinus Torvalds fail: 35611da177e4SLinus Torvalds return err; 35621da177e4SLinus Torvalds } 35631da177e4SLinus Torvalds 35640adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 35650adb25d2SKirill Korotaev { 35660adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 356754566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 35680adb25d2SKirill Korotaev } 35690adb25d2SKirill Korotaev 357092e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 35711da177e4SLinus Torvalds .readlink = generic_readlink, 35721da177e4SLinus Torvalds .follow_link = page_follow_link_light, 35731da177e4SLinus Torvalds .put_link = page_put_link, 35741da177e4SLinus Torvalds }; 35751da177e4SLinus Torvalds 35762d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 3577cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one); 35781da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 35791da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 35801da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 35811da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 35821da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 35831da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 35841da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 35851da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 35861da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 35870adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 35881da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 35891da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 35901da177e4SLinus Torvalds EXPORT_SYMBOL(path_lookup); 3591d1811465SAl Viro EXPORT_SYMBOL(kern_path); 359216f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 3593f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 35948c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission); 35951da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 35961da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 35971da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 35981da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 35991da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 36001da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 36011da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 36021da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 36031da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 36041da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 36051da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 36061da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 36071da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 36081da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 3609