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/quotaops.h> 231da177e4SLinus Torvalds #include <linux/pagemap.h> 240eeca283SRobert Love #include <linux/fsnotify.h> 251da177e4SLinus Torvalds #include <linux/personality.h> 261da177e4SLinus Torvalds #include <linux/security.h> 276146f0d5SMimi Zohar #include <linux/ima.h> 281da177e4SLinus Torvalds #include <linux/syscalls.h> 291da177e4SLinus Torvalds #include <linux/mount.h> 301da177e4SLinus Torvalds #include <linux/audit.h> 3116f7e0feSRandy Dunlap #include <linux/capability.h> 32834f2a4aSTrond Myklebust #include <linux/file.h> 335590ff0dSUlrich Drepper #include <linux/fcntl.h> 3408ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h> 355ad4e53bSAl Viro #include <linux/fs_struct.h> 361da177e4SLinus Torvalds #include <asm/uaccess.h> 371da177e4SLinus Torvalds 38e81e3f4dSEric Paris #include "internal.h" 39e81e3f4dSEric Paris 401da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer] 411da177e4SLinus Torvalds * Fundamental changes in the pathname lookup mechanisms (namei) 421da177e4SLinus Torvalds * were necessary because of omirr. The reason is that omirr needs 431da177e4SLinus Torvalds * to know the _real_ pathname, not the user-supplied one, in case 441da177e4SLinus Torvalds * of symlinks (and also when transname replacements occur). 451da177e4SLinus Torvalds * 461da177e4SLinus Torvalds * The new code replaces the old recursive symlink resolution with 471da177e4SLinus Torvalds * an iterative one (in case of non-nested symlink chains). It does 481da177e4SLinus Torvalds * this with calls to <fs>_follow_link(). 491da177e4SLinus Torvalds * As a side effect, dir_namei(), _namei() and follow_link() are now 501da177e4SLinus Torvalds * replaced with a single function lookup_dentry() that can handle all 511da177e4SLinus Torvalds * the special cases of the former code. 521da177e4SLinus Torvalds * 531da177e4SLinus Torvalds * With the new dcache, the pathname is stored at each inode, at least as 541da177e4SLinus Torvalds * long as the refcount of the inode is positive. As a side effect, the 551da177e4SLinus Torvalds * size of the dcache depends on the inode cache and thus is dynamic. 561da177e4SLinus Torvalds * 571da177e4SLinus Torvalds * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink 581da177e4SLinus Torvalds * resolution to correspond with current state of the code. 591da177e4SLinus Torvalds * 601da177e4SLinus Torvalds * Note that the symlink resolution is not *completely* iterative. 611da177e4SLinus Torvalds * There is still a significant amount of tail- and mid- recursion in 621da177e4SLinus Torvalds * the algorithm. Also, note that <fs>_readlink() is not used in 631da177e4SLinus Torvalds * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() 641da177e4SLinus Torvalds * may return different results than <fs>_follow_link(). Many virtual 651da177e4SLinus Torvalds * filesystems (including /proc) exhibit this behavior. 661da177e4SLinus Torvalds */ 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: 691da177e4SLinus Torvalds * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL 701da177e4SLinus Torvalds * and the name already exists in form of a symlink, try to create the new 711da177e4SLinus Torvalds * name indicated by the symlink. The old code always complained that the 721da177e4SLinus Torvalds * name already exists, due to not following the symlink even if its target 731da177e4SLinus Torvalds * is nonexistent. The new semantics affects also mknod() and link() when 741da177e4SLinus Torvalds * the name is a symlink pointing to a non-existant name. 751da177e4SLinus Torvalds * 761da177e4SLinus Torvalds * I don't know which semantics is the right one, since I have no access 771da177e4SLinus Torvalds * to standards. But I found by trial that HP-UX 9.0 has the full "new" 781da177e4SLinus Torvalds * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the 791da177e4SLinus Torvalds * "old" one. Personally, I think the new semantics is much more logical. 801da177e4SLinus Torvalds * Note that "ln old new" where "new" is a symlink pointing to a non-existing 811da177e4SLinus Torvalds * file does succeed in both HP-UX and SunOs, but not in Solaris 821da177e4SLinus Torvalds * and in the old Linux semantics. 831da177e4SLinus Torvalds */ 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink 861da177e4SLinus Torvalds * semantics. See the comments in "open_namei" and "do_link" below. 871da177e4SLinus Torvalds * 881da177e4SLinus Torvalds * [10-Sep-98 Alan Modra] Another symlink change. 891da177e4SLinus Torvalds */ 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: 921da177e4SLinus Torvalds * inside the path - always follow. 931da177e4SLinus Torvalds * in the last component in creation/removal/renaming - never follow. 941da177e4SLinus Torvalds * if LOOKUP_FOLLOW passed - follow. 951da177e4SLinus Torvalds * if the pathname has trailing slashes - follow. 961da177e4SLinus Torvalds * otherwise - don't follow. 971da177e4SLinus Torvalds * (applied in that order). 981da177e4SLinus Torvalds * 991da177e4SLinus Torvalds * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT 1001da177e4SLinus Torvalds * restored for 2.4. This is the last surviving part of old 4.2BSD bug. 1011da177e4SLinus Torvalds * During the 2.4 we need to fix the userland stuff depending on it - 1021da177e4SLinus Torvalds * hopefully we will be able to get rid of that wart in 2.5. So far only 1031da177e4SLinus Torvalds * XEmacs seems to be relying on it... 1041da177e4SLinus Torvalds */ 1051da177e4SLinus Torvalds /* 1061da177e4SLinus Torvalds * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) 107a11f3a05SArjan van de Ven * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives 1081da177e4SLinus Torvalds * any extra contention... 1091da177e4SLinus Torvalds */ 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional 1121da177e4SLinus Torvalds * checking and hopefully speeding things up, we copy filenames to the 1131da177e4SLinus Torvalds * kernel data space before using them.. 1141da177e4SLinus Torvalds * 1151da177e4SLinus Torvalds * POSIX.1 2.4: an empty pathname is invalid (ENOENT). 1161da177e4SLinus Torvalds * PATH_MAX includes the nul terminator --RR. 1171da177e4SLinus Torvalds */ 118858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page) 1191da177e4SLinus Torvalds { 1201da177e4SLinus Torvalds int retval; 1211da177e4SLinus Torvalds unsigned long len = PATH_MAX; 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds if (!segment_eq(get_fs(), KERNEL_DS)) { 1241da177e4SLinus Torvalds if ((unsigned long) filename >= TASK_SIZE) 1251da177e4SLinus Torvalds return -EFAULT; 1261da177e4SLinus Torvalds if (TASK_SIZE - (unsigned long) filename < PATH_MAX) 1271da177e4SLinus Torvalds len = TASK_SIZE - (unsigned long) filename; 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds retval = strncpy_from_user(page, filename, len); 1311da177e4SLinus Torvalds if (retval > 0) { 1321da177e4SLinus Torvalds if (retval < len) 1331da177e4SLinus Torvalds return 0; 1341da177e4SLinus Torvalds return -ENAMETOOLONG; 1351da177e4SLinus Torvalds } else if (!retval) 1361da177e4SLinus Torvalds retval = -ENOENT; 1371da177e4SLinus Torvalds return retval; 1381da177e4SLinus Torvalds } 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds char * getname(const char __user * filename) 1411da177e4SLinus Torvalds { 1421da177e4SLinus Torvalds char *tmp, *result; 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds result = ERR_PTR(-ENOMEM); 1451da177e4SLinus Torvalds tmp = __getname(); 1461da177e4SLinus Torvalds if (tmp) { 1471da177e4SLinus Torvalds int retval = do_getname(filename, tmp); 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds result = tmp; 1501da177e4SLinus Torvalds if (retval < 0) { 1511da177e4SLinus Torvalds __putname(tmp); 1521da177e4SLinus Torvalds result = ERR_PTR(retval); 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds } 1551da177e4SLinus Torvalds audit_getname(result); 1561da177e4SLinus Torvalds return result; 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 1601da177e4SLinus Torvalds void putname(const char *name) 1611da177e4SLinus Torvalds { 1625ac3a9c2SAl Viro if (unlikely(!audit_dummy_context())) 1631da177e4SLinus Torvalds audit_putname(name); 1641da177e4SLinus Torvalds else 1651da177e4SLinus Torvalds __putname(name); 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds EXPORT_SYMBOL(putname); 1681da177e4SLinus Torvalds #endif 1691da177e4SLinus Torvalds 1705909ccaaSLinus Torvalds /* 1715909ccaaSLinus Torvalds * This does basic POSIX ACL permission checking 1725909ccaaSLinus Torvalds */ 1735909ccaaSLinus Torvalds static int acl_permission_check(struct inode *inode, int mask, 1745909ccaaSLinus Torvalds int (*check_acl)(struct inode *inode, int mask)) 1755909ccaaSLinus Torvalds { 1765909ccaaSLinus Torvalds umode_t mode = inode->i_mode; 1775909ccaaSLinus Torvalds 1785909ccaaSLinus Torvalds mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 1795909ccaaSLinus Torvalds 1805909ccaaSLinus Torvalds if (current_fsuid() == inode->i_uid) 1815909ccaaSLinus Torvalds mode >>= 6; 1825909ccaaSLinus Torvalds else { 1835909ccaaSLinus Torvalds if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) { 1845909ccaaSLinus Torvalds int error = check_acl(inode, mask); 1855909ccaaSLinus Torvalds if (error != -EAGAIN) 1865909ccaaSLinus Torvalds return error; 1875909ccaaSLinus Torvalds } 1885909ccaaSLinus Torvalds 1895909ccaaSLinus Torvalds if (in_group_p(inode->i_gid)) 1905909ccaaSLinus Torvalds mode >>= 3; 1915909ccaaSLinus Torvalds } 1925909ccaaSLinus Torvalds 1935909ccaaSLinus Torvalds /* 1945909ccaaSLinus Torvalds * If the DACs are ok we don't need any capability check. 1955909ccaaSLinus Torvalds */ 1965909ccaaSLinus Torvalds if ((mask & ~mode) == 0) 1975909ccaaSLinus Torvalds return 0; 1985909ccaaSLinus Torvalds return -EACCES; 1995909ccaaSLinus Torvalds } 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds /** 2021da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 2031da177e4SLinus Torvalds * @inode: inode to check access rights for 2041da177e4SLinus Torvalds * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 2051da177e4SLinus Torvalds * @check_acl: optional callback to check for Posix ACLs 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 2101da177e4SLinus Torvalds * are used for other things.. 2111da177e4SLinus Torvalds */ 2121da177e4SLinus Torvalds int generic_permission(struct inode *inode, int mask, 2131da177e4SLinus Torvalds int (*check_acl)(struct inode *inode, int mask)) 2141da177e4SLinus Torvalds { 2155909ccaaSLinus Torvalds int ret; 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds /* 2185909ccaaSLinus Torvalds * Do the basic POSIX ACL permission checks. 2191da177e4SLinus Torvalds */ 2205909ccaaSLinus Torvalds ret = acl_permission_check(inode, mask, check_acl); 2215909ccaaSLinus Torvalds if (ret != -EACCES) 2225909ccaaSLinus Torvalds return ret; 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds /* 2251da177e4SLinus Torvalds * Read/write DACs are always overridable. 2261da177e4SLinus Torvalds * Executable DACs are overridable if at least one exec bit is set. 2271da177e4SLinus Torvalds */ 228f696a365SMiklos Szeredi if (!(mask & MAY_EXEC) || execute_ok(inode)) 2291da177e4SLinus Torvalds if (capable(CAP_DAC_OVERRIDE)) 2301da177e4SLinus Torvalds return 0; 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds /* 2331da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 2341da177e4SLinus Torvalds */ 2357ea66001SSerge E. Hallyn mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 2361da177e4SLinus Torvalds if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE))) 2371da177e4SLinus Torvalds if (capable(CAP_DAC_READ_SEARCH)) 2381da177e4SLinus Torvalds return 0; 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds return -EACCES; 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds 243cb23beb5SChristoph Hellwig /** 244cb23beb5SChristoph Hellwig * inode_permission - check for access rights to a given inode 245cb23beb5SChristoph Hellwig * @inode: inode to check permission on 246cb23beb5SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 247cb23beb5SChristoph Hellwig * 248cb23beb5SChristoph Hellwig * Used to check for read/write/execute permissions on an inode. 249cb23beb5SChristoph Hellwig * We use "fsuid" for this, letting us set arbitrary permissions 250cb23beb5SChristoph Hellwig * for filesystem access without changing the "normal" uids which 251cb23beb5SChristoph Hellwig * are used for other things. 252cb23beb5SChristoph Hellwig */ 253f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask) 2541da177e4SLinus Torvalds { 255e6305c43SAl Viro int retval; 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds if (mask & MAY_WRITE) { 25822590e41SMiklos Szeredi umode_t mode = inode->i_mode; 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds /* 2611da177e4SLinus Torvalds * Nobody gets write access to a read-only fs. 2621da177e4SLinus Torvalds */ 2631da177e4SLinus Torvalds if (IS_RDONLY(inode) && 2641da177e4SLinus Torvalds (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 2651da177e4SLinus Torvalds return -EROFS; 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds /* 2681da177e4SLinus Torvalds * Nobody gets write access to an immutable file. 2691da177e4SLinus Torvalds */ 2701da177e4SLinus Torvalds if (IS_IMMUTABLE(inode)) 2711da177e4SLinus Torvalds return -EACCES; 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds 274acfa4380SAl Viro if (inode->i_op->permission) 275b77b0646SAl Viro retval = inode->i_op->permission(inode, mask); 276f696a365SMiklos Szeredi else 2775909ccaaSLinus Torvalds retval = generic_permission(inode, mask, inode->i_op->check_acl); 278f696a365SMiklos Szeredi 2791da177e4SLinus Torvalds if (retval) 2801da177e4SLinus Torvalds return retval; 2811da177e4SLinus Torvalds 28208ce5f16SSerge E. Hallyn retval = devcgroup_inode_permission(inode, mask); 28308ce5f16SSerge E. Hallyn if (retval) 28408ce5f16SSerge E. Hallyn return retval; 28508ce5f16SSerge E. Hallyn 286e6305c43SAl Viro return security_inode_permission(inode, 287f418b006SStephen Smalley mask & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND)); 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds 290e4543eddSChristoph Hellwig /** 2918c744fb8SChristoph Hellwig * file_permission - check for additional access rights to a given file 2928c744fb8SChristoph Hellwig * @file: file to check access rights for 2938c744fb8SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 2948c744fb8SChristoph Hellwig * 2958c744fb8SChristoph Hellwig * Used to check for read/write/execute permissions on an already opened 2968c744fb8SChristoph Hellwig * file. 2978c744fb8SChristoph Hellwig * 2988c744fb8SChristoph Hellwig * Note: 2998c744fb8SChristoph Hellwig * Do not use this function in new code. All access checks should 300cb23beb5SChristoph Hellwig * be done using inode_permission(). 3018c744fb8SChristoph Hellwig */ 3028c744fb8SChristoph Hellwig int file_permission(struct file *file, int mask) 3038c744fb8SChristoph Hellwig { 304f419a2e3SAl Viro return inode_permission(file->f_path.dentry->d_inode, mask); 3058c744fb8SChristoph Hellwig } 3068c744fb8SChristoph Hellwig 3071da177e4SLinus Torvalds /* 3081da177e4SLinus Torvalds * get_write_access() gets write permission for a file. 3091da177e4SLinus Torvalds * put_write_access() releases this write permission. 3101da177e4SLinus Torvalds * This is used for regular files. 3111da177e4SLinus Torvalds * We cannot support write (and maybe mmap read-write shared) accesses and 3121da177e4SLinus Torvalds * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode 3131da177e4SLinus Torvalds * can have the following values: 3141da177e4SLinus Torvalds * 0: no writers, no VM_DENYWRITE mappings 3151da177e4SLinus Torvalds * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist 3161da177e4SLinus Torvalds * > 0: (i_writecount) users are writing to the file. 3171da177e4SLinus Torvalds * 3181da177e4SLinus Torvalds * Normally we operate on that counter with atomic_{inc,dec} and it's safe 3191da177e4SLinus Torvalds * except for the cases where we don't hold i_writecount yet. Then we need to 3201da177e4SLinus Torvalds * use {get,deny}_write_access() - these functions check the sign and refuse 3211da177e4SLinus Torvalds * to do the change if sign is wrong. Exclusion between them is provided by 3221da177e4SLinus Torvalds * the inode->i_lock spinlock. 3231da177e4SLinus Torvalds */ 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds int get_write_access(struct inode * inode) 3261da177e4SLinus Torvalds { 3271da177e4SLinus Torvalds spin_lock(&inode->i_lock); 3281da177e4SLinus Torvalds if (atomic_read(&inode->i_writecount) < 0) { 3291da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3301da177e4SLinus Torvalds return -ETXTBSY; 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds atomic_inc(&inode->i_writecount); 3331da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds return 0; 3361da177e4SLinus Torvalds } 3371da177e4SLinus Torvalds 3381da177e4SLinus Torvalds int deny_write_access(struct file * file) 3391da177e4SLinus Torvalds { 3400f7fc9e4SJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds spin_lock(&inode->i_lock); 3431da177e4SLinus Torvalds if (atomic_read(&inode->i_writecount) > 0) { 3441da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3451da177e4SLinus Torvalds return -ETXTBSY; 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds atomic_dec(&inode->i_writecount); 3481da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds return 0; 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds 3531d957f9bSJan Blunck /** 3545dd784d0SJan Blunck * path_get - get a reference to a path 3555dd784d0SJan Blunck * @path: path to get the reference to 3565dd784d0SJan Blunck * 3575dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 3585dd784d0SJan Blunck */ 3595dd784d0SJan Blunck void path_get(struct path *path) 3605dd784d0SJan Blunck { 3615dd784d0SJan Blunck mntget(path->mnt); 3625dd784d0SJan Blunck dget(path->dentry); 3635dd784d0SJan Blunck } 3645dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 3655dd784d0SJan Blunck 3665dd784d0SJan Blunck /** 3671d957f9bSJan Blunck * path_put - put a reference to a path 3681d957f9bSJan Blunck * @path: path to put the reference to 3691d957f9bSJan Blunck * 3701d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 3711d957f9bSJan Blunck */ 3721d957f9bSJan Blunck void path_put(struct path *path) 3731da177e4SLinus Torvalds { 3741d957f9bSJan Blunck dput(path->dentry); 3751d957f9bSJan Blunck mntput(path->mnt); 3761da177e4SLinus Torvalds } 3771d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 3781da177e4SLinus Torvalds 379834f2a4aSTrond Myklebust /** 380834f2a4aSTrond Myklebust * release_open_intent - free up open intent resources 381834f2a4aSTrond Myklebust * @nd: pointer to nameidata 382834f2a4aSTrond Myklebust */ 383834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd) 384834f2a4aSTrond Myklebust { 3850f7fc9e4SJosef "Jeff" Sipek if (nd->intent.open.file->f_path.dentry == NULL) 386834f2a4aSTrond Myklebust put_filp(nd->intent.open.file); 387834f2a4aSTrond Myklebust else 388834f2a4aSTrond Myklebust fput(nd->intent.open.file); 389834f2a4aSTrond Myklebust } 390834f2a4aSTrond Myklebust 391bcdc5e01SIan Kent static inline struct dentry * 392bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd) 393bcdc5e01SIan Kent { 394bcdc5e01SIan Kent int status = dentry->d_op->d_revalidate(dentry, nd); 395bcdc5e01SIan Kent if (unlikely(status <= 0)) { 396bcdc5e01SIan Kent /* 397bcdc5e01SIan Kent * The dentry failed validation. 398bcdc5e01SIan Kent * If d_revalidate returned 0 attempt to invalidate 399bcdc5e01SIan Kent * the dentry otherwise d_revalidate is asking us 400bcdc5e01SIan Kent * to return a fail status. 401bcdc5e01SIan Kent */ 402bcdc5e01SIan Kent if (!status) { 403bcdc5e01SIan Kent if (!d_invalidate(dentry)) { 404bcdc5e01SIan Kent dput(dentry); 405bcdc5e01SIan Kent dentry = NULL; 406bcdc5e01SIan Kent } 407bcdc5e01SIan Kent } else { 408bcdc5e01SIan Kent dput(dentry); 409bcdc5e01SIan Kent dentry = ERR_PTR(status); 410bcdc5e01SIan Kent } 411bcdc5e01SIan Kent } 412bcdc5e01SIan Kent return dentry; 413bcdc5e01SIan Kent } 414bcdc5e01SIan Kent 4151da177e4SLinus Torvalds /* 41639159de2SJeff Layton * force_reval_path - force revalidation of a dentry 41739159de2SJeff Layton * 41839159de2SJeff Layton * In some situations the path walking code will trust dentries without 41939159de2SJeff Layton * revalidating them. This causes problems for filesystems that depend on 42039159de2SJeff Layton * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set 42139159de2SJeff Layton * (which indicates that it's possible for the dentry to go stale), force 42239159de2SJeff Layton * a d_revalidate call before proceeding. 42339159de2SJeff Layton * 42439159de2SJeff Layton * Returns 0 if the revalidation was successful. If the revalidation fails, 42539159de2SJeff Layton * either return the error returned by d_revalidate or -ESTALE if the 42639159de2SJeff Layton * revalidation it just returned 0. If d_revalidate returns 0, we attempt to 42739159de2SJeff Layton * invalidate the dentry. It's up to the caller to handle putting references 42839159de2SJeff Layton * to the path if necessary. 42939159de2SJeff Layton */ 43039159de2SJeff Layton static int 43139159de2SJeff Layton force_reval_path(struct path *path, struct nameidata *nd) 43239159de2SJeff Layton { 43339159de2SJeff Layton int status; 43439159de2SJeff Layton struct dentry *dentry = path->dentry; 43539159de2SJeff Layton 43639159de2SJeff Layton /* 43739159de2SJeff Layton * only check on filesystems where it's possible for the dentry to 43839159de2SJeff Layton * become stale. It's assumed that if this flag is set then the 43939159de2SJeff Layton * d_revalidate op will also be defined. 44039159de2SJeff Layton */ 44139159de2SJeff Layton if (!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) 44239159de2SJeff Layton return 0; 44339159de2SJeff Layton 44439159de2SJeff Layton status = dentry->d_op->d_revalidate(dentry, nd); 44539159de2SJeff Layton if (status > 0) 44639159de2SJeff Layton return 0; 44739159de2SJeff Layton 44839159de2SJeff Layton if (!status) { 44939159de2SJeff Layton d_invalidate(dentry); 45039159de2SJeff Layton status = -ESTALE; 45139159de2SJeff Layton } 45239159de2SJeff Layton return status; 45339159de2SJeff Layton } 45439159de2SJeff Layton 45539159de2SJeff Layton /* 456b75b5086SAl Viro * Short-cut version of permission(), for calling on directories 457b75b5086SAl Viro * during pathname resolution. Combines parts of permission() 458b75b5086SAl Viro * and generic_permission(), and tests ONLY for MAY_EXEC permission. 4591da177e4SLinus Torvalds * 4601da177e4SLinus Torvalds * If appropriate, check DAC only. If not appropriate, or 461b75b5086SAl Viro * short-cut DAC fails, then call ->permission() to do more 4621da177e4SLinus Torvalds * complete permission check. 4631da177e4SLinus Torvalds */ 464b75b5086SAl Viro static int exec_permission(struct inode *inode) 4651da177e4SLinus Torvalds { 4665909ccaaSLinus Torvalds int ret; 4671da177e4SLinus Torvalds 468cb9179eaSLinus Torvalds if (inode->i_op->permission) { 4695909ccaaSLinus Torvalds ret = inode->i_op->permission(inode, MAY_EXEC); 470cb9179eaSLinus Torvalds if (!ret) 471cb9179eaSLinus Torvalds goto ok; 472cb9179eaSLinus Torvalds return ret; 473cb9179eaSLinus Torvalds } 4745909ccaaSLinus Torvalds ret = acl_permission_check(inode, MAY_EXEC, inode->i_op->check_acl); 4755909ccaaSLinus Torvalds if (!ret) 4761da177e4SLinus Torvalds goto ok; 4771da177e4SLinus Torvalds 478f1ac9f6bSLinus Torvalds if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH)) 4791da177e4SLinus Torvalds goto ok; 4801da177e4SLinus Torvalds 4815909ccaaSLinus Torvalds return ret; 4821da177e4SLinus Torvalds ok: 483b77b0646SAl Viro return security_inode_permission(inode, MAY_EXEC); 4841da177e4SLinus Torvalds } 4851da177e4SLinus Torvalds 4862a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 4872a737871SAl Viro { 4882a737871SAl Viro if (!nd->root.mnt) { 4892a737871SAl Viro struct fs_struct *fs = current->fs; 4902a737871SAl Viro read_lock(&fs->lock); 4912a737871SAl Viro nd->root = fs->root; 4922a737871SAl Viro path_get(&nd->root); 4932a737871SAl Viro read_unlock(&fs->lock); 4942a737871SAl Viro } 4952a737871SAl Viro } 4962a737871SAl Viro 4976de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 4986de88d72SAl Viro 499f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 5001da177e4SLinus Torvalds { 5011da177e4SLinus Torvalds int res = 0; 5021da177e4SLinus Torvalds char *name; 5031da177e4SLinus Torvalds if (IS_ERR(link)) 5041da177e4SLinus Torvalds goto fail; 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds if (*link == '/') { 5072a737871SAl Viro set_root(nd); 5081d957f9bSJan Blunck path_put(&nd->path); 5092a737871SAl Viro nd->path = nd->root; 5102a737871SAl Viro path_get(&nd->root); 5111da177e4SLinus Torvalds } 512b4091d5fSChristoph Hellwig 5131da177e4SLinus Torvalds res = link_path_walk(link, nd); 5141da177e4SLinus Torvalds if (nd->depth || res || nd->last_type!=LAST_NORM) 5151da177e4SLinus Torvalds return res; 5161da177e4SLinus Torvalds /* 5171da177e4SLinus Torvalds * If it is an iterative symlinks resolution in open_namei() we 5181da177e4SLinus Torvalds * have to copy the last component. And all that crap because of 5191da177e4SLinus Torvalds * bloody create() on broken symlinks. Furrfu... 5201da177e4SLinus Torvalds */ 5211da177e4SLinus Torvalds name = __getname(); 5221da177e4SLinus Torvalds if (unlikely(!name)) { 5231d957f9bSJan Blunck path_put(&nd->path); 5241da177e4SLinus Torvalds return -ENOMEM; 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds strcpy(name, nd->last.name); 5271da177e4SLinus Torvalds nd->last.name = name; 5281da177e4SLinus Torvalds return 0; 5291da177e4SLinus Torvalds fail: 5301d957f9bSJan Blunck path_put(&nd->path); 5311da177e4SLinus Torvalds return PTR_ERR(link); 5321da177e4SLinus Torvalds } 5331da177e4SLinus Torvalds 5341d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 535051d3812SIan Kent { 536051d3812SIan Kent dput(path->dentry); 5374ac91378SJan Blunck if (path->mnt != nd->path.mnt) 538051d3812SIan Kent mntput(path->mnt); 539051d3812SIan Kent } 540051d3812SIan Kent 541051d3812SIan Kent static inline void path_to_nameidata(struct path *path, struct nameidata *nd) 542051d3812SIan Kent { 5434ac91378SJan Blunck dput(nd->path.dentry); 5444ac91378SJan Blunck if (nd->path.mnt != path->mnt) 5454ac91378SJan Blunck mntput(nd->path.mnt); 5464ac91378SJan Blunck nd->path.mnt = path->mnt; 5474ac91378SJan Blunck nd->path.dentry = path->dentry; 548051d3812SIan Kent } 549051d3812SIan Kent 550f1662356SArjan van de Ven static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd) 5511da177e4SLinus Torvalds { 5521da177e4SLinus Torvalds int error; 553cc314eefSLinus Torvalds void *cookie; 554cd4e91d3SAl Viro struct dentry *dentry = path->dentry; 5551da177e4SLinus Torvalds 556d671a1cbSAl Viro touch_atime(path->mnt, dentry); 5571da177e4SLinus Torvalds nd_set_link(nd, NULL); 558cd4e91d3SAl Viro 5594ac91378SJan Blunck if (path->mnt != nd->path.mnt) { 560051d3812SIan Kent path_to_nameidata(path, nd); 561051d3812SIan Kent dget(dentry); 562051d3812SIan Kent } 563cd4e91d3SAl Viro mntget(path->mnt); 56486acdca1SAl Viro nd->last_type = LAST_BIND; 565cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, nd); 566cc314eefSLinus Torvalds error = PTR_ERR(cookie); 567cc314eefSLinus Torvalds if (!IS_ERR(cookie)) { 5681da177e4SLinus Torvalds char *s = nd_get_link(nd); 569cc314eefSLinus Torvalds error = 0; 5701da177e4SLinus Torvalds if (s) 5711da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 57239159de2SJeff Layton else if (nd->last_type == LAST_BIND) { 57339159de2SJeff Layton error = force_reval_path(&nd->path, nd); 57439159de2SJeff Layton if (error) 57539159de2SJeff Layton path_put(&nd->path); 57639159de2SJeff Layton } 5771da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 578cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, nd, cookie); 5791da177e4SLinus Torvalds } 5801da177e4SLinus Torvalds return error; 5811da177e4SLinus Torvalds } 5821da177e4SLinus Torvalds 5831da177e4SLinus Torvalds /* 5841da177e4SLinus Torvalds * This limits recursive symlink follows to 8, while 5851da177e4SLinus Torvalds * limiting consecutive symlinks to 40. 5861da177e4SLinus Torvalds * 5871da177e4SLinus Torvalds * Without that kind of total limit, nasty chains of consecutive 5881da177e4SLinus Torvalds * symlinks can cause almost arbitrarily long lookups. 5891da177e4SLinus Torvalds */ 59090ebe565SAl Viro static inline int do_follow_link(struct path *path, struct nameidata *nd) 5911da177e4SLinus Torvalds { 5921da177e4SLinus Torvalds int err = -ELOOP; 5931da177e4SLinus Torvalds if (current->link_count >= MAX_NESTED_LINKS) 5941da177e4SLinus Torvalds goto loop; 5951da177e4SLinus Torvalds if (current->total_link_count >= 40) 5961da177e4SLinus Torvalds goto loop; 5971da177e4SLinus Torvalds BUG_ON(nd->depth >= MAX_NESTED_LINKS); 5981da177e4SLinus Torvalds cond_resched(); 59990ebe565SAl Viro err = security_inode_follow_link(path->dentry, nd); 6001da177e4SLinus Torvalds if (err) 6011da177e4SLinus Torvalds goto loop; 6021da177e4SLinus Torvalds current->link_count++; 6031da177e4SLinus Torvalds current->total_link_count++; 6041da177e4SLinus Torvalds nd->depth++; 605cd4e91d3SAl Viro err = __do_follow_link(path, nd); 606258fa999SAl Viro path_put(path); 6071da177e4SLinus Torvalds current->link_count--; 6081da177e4SLinus Torvalds nd->depth--; 6091da177e4SLinus Torvalds return err; 6101da177e4SLinus Torvalds loop: 6111d957f9bSJan Blunck path_put_conditional(path, nd); 6121d957f9bSJan Blunck path_put(&nd->path); 6131da177e4SLinus Torvalds return err; 6141da177e4SLinus Torvalds } 6151da177e4SLinus Torvalds 616bab77ebfSAl Viro int follow_up(struct path *path) 6171da177e4SLinus Torvalds { 6181da177e4SLinus Torvalds struct vfsmount *parent; 6191da177e4SLinus Torvalds struct dentry *mountpoint; 6201da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 621bab77ebfSAl Viro parent = path->mnt->mnt_parent; 622bab77ebfSAl Viro if (parent == path->mnt) { 6231da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 6241da177e4SLinus Torvalds return 0; 6251da177e4SLinus Torvalds } 6261da177e4SLinus Torvalds mntget(parent); 627bab77ebfSAl Viro mountpoint = dget(path->mnt->mnt_mountpoint); 6281da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 629bab77ebfSAl Viro dput(path->dentry); 630bab77ebfSAl Viro path->dentry = mountpoint; 631bab77ebfSAl Viro mntput(path->mnt); 632bab77ebfSAl Viro path->mnt = parent; 6331da177e4SLinus Torvalds return 1; 6341da177e4SLinus Torvalds } 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds /* no need for dcache_lock, as serialization is taken care in 6371da177e4SLinus Torvalds * namespace.c 6381da177e4SLinus Torvalds */ 639463ffb2eSAl Viro static int __follow_mount(struct path *path) 640463ffb2eSAl Viro { 641463ffb2eSAl Viro int res = 0; 642463ffb2eSAl Viro while (d_mountpoint(path->dentry)) { 6431c755af4SAl Viro struct vfsmount *mounted = lookup_mnt(path); 644463ffb2eSAl Viro if (!mounted) 645463ffb2eSAl Viro break; 646463ffb2eSAl Viro dput(path->dentry); 647463ffb2eSAl Viro if (res) 648463ffb2eSAl Viro mntput(path->mnt); 649463ffb2eSAl Viro path->mnt = mounted; 650463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 651463ffb2eSAl Viro res = 1; 652463ffb2eSAl Viro } 653463ffb2eSAl Viro return res; 654463ffb2eSAl Viro } 655463ffb2eSAl Viro 65679ed0226SAl Viro static void follow_mount(struct path *path) 6571da177e4SLinus Torvalds { 65879ed0226SAl Viro while (d_mountpoint(path->dentry)) { 6591c755af4SAl Viro struct vfsmount *mounted = lookup_mnt(path); 6601da177e4SLinus Torvalds if (!mounted) 6611da177e4SLinus Torvalds break; 66279ed0226SAl Viro dput(path->dentry); 66379ed0226SAl Viro mntput(path->mnt); 66479ed0226SAl Viro path->mnt = mounted; 66579ed0226SAl Viro path->dentry = dget(mounted->mnt_root); 6661da177e4SLinus Torvalds } 6671da177e4SLinus Torvalds } 6681da177e4SLinus Torvalds 6691da177e4SLinus Torvalds /* no need for dcache_lock, as serialization is taken care in 6701da177e4SLinus Torvalds * namespace.c 6711da177e4SLinus Torvalds */ 6729393bd07SAl Viro int follow_down(struct path *path) 6731da177e4SLinus Torvalds { 6741da177e4SLinus Torvalds struct vfsmount *mounted; 6751da177e4SLinus Torvalds 6761c755af4SAl Viro mounted = lookup_mnt(path); 6771da177e4SLinus Torvalds if (mounted) { 6789393bd07SAl Viro dput(path->dentry); 6799393bd07SAl Viro mntput(path->mnt); 6809393bd07SAl Viro path->mnt = mounted; 6819393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 6821da177e4SLinus Torvalds return 1; 6831da177e4SLinus Torvalds } 6841da177e4SLinus Torvalds return 0; 6851da177e4SLinus Torvalds } 6861da177e4SLinus Torvalds 687f1662356SArjan van de Ven static __always_inline void follow_dotdot(struct nameidata *nd) 6881da177e4SLinus Torvalds { 6892a737871SAl Viro set_root(nd); 690e518ddb7SAndreas Mohr 6911da177e4SLinus Torvalds while(1) { 6921da177e4SLinus Torvalds struct vfsmount *parent; 6934ac91378SJan Blunck struct dentry *old = nd->path.dentry; 6941da177e4SLinus Torvalds 6952a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 6962a737871SAl Viro nd->path.mnt == nd->root.mnt) { 6971da177e4SLinus Torvalds break; 6981da177e4SLinus Torvalds } 6991da177e4SLinus Torvalds spin_lock(&dcache_lock); 7004ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 7014ac91378SJan Blunck nd->path.dentry = dget(nd->path.dentry->d_parent); 7021da177e4SLinus Torvalds spin_unlock(&dcache_lock); 7031da177e4SLinus Torvalds dput(old); 7041da177e4SLinus Torvalds break; 7051da177e4SLinus Torvalds } 7061da177e4SLinus Torvalds spin_unlock(&dcache_lock); 7071da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 7084ac91378SJan Blunck parent = nd->path.mnt->mnt_parent; 7094ac91378SJan Blunck if (parent == nd->path.mnt) { 7101da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 7111da177e4SLinus Torvalds break; 7121da177e4SLinus Torvalds } 7131da177e4SLinus Torvalds mntget(parent); 7144ac91378SJan Blunck nd->path.dentry = dget(nd->path.mnt->mnt_mountpoint); 7151da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 7161da177e4SLinus Torvalds dput(old); 7174ac91378SJan Blunck mntput(nd->path.mnt); 7184ac91378SJan Blunck nd->path.mnt = parent; 7191da177e4SLinus Torvalds } 72079ed0226SAl Viro follow_mount(&nd->path); 7211da177e4SLinus Torvalds } 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds /* 7241da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 7251da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 7261da177e4SLinus Torvalds * It _is_ time-critical. 7271da177e4SLinus Torvalds */ 7281da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name, 7291da177e4SLinus Torvalds struct path *path) 7301da177e4SLinus Torvalds { 7314ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 7326e6b1bd1SAl Viro struct dentry *dentry, *parent; 7336e6b1bd1SAl Viro struct inode *dir; 7343cac260aSAl Viro /* 7353cac260aSAl Viro * See if the low-level filesystem might want 7363cac260aSAl Viro * to use its own hash.. 7373cac260aSAl Viro */ 7383cac260aSAl Viro if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) { 7393cac260aSAl Viro int err = nd->path.dentry->d_op->d_hash(nd->path.dentry, name); 7403cac260aSAl Viro if (err < 0) 7413cac260aSAl Viro return err; 7423cac260aSAl Viro } 7431da177e4SLinus Torvalds 7443cac260aSAl Viro dentry = __d_lookup(nd->path.dentry, name); 7451da177e4SLinus Torvalds if (!dentry) 7461da177e4SLinus Torvalds goto need_lookup; 7471da177e4SLinus Torvalds if (dentry->d_op && dentry->d_op->d_revalidate) 7481da177e4SLinus Torvalds goto need_revalidate; 7491da177e4SLinus Torvalds done: 7501da177e4SLinus Torvalds path->mnt = mnt; 7511da177e4SLinus Torvalds path->dentry = dentry; 752634ee701SAl Viro __follow_mount(path); 7531da177e4SLinus Torvalds return 0; 7541da177e4SLinus Torvalds 7551da177e4SLinus Torvalds need_lookup: 7566e6b1bd1SAl Viro parent = nd->path.dentry; 7576e6b1bd1SAl Viro dir = parent->d_inode; 7586e6b1bd1SAl Viro 7596e6b1bd1SAl Viro mutex_lock(&dir->i_mutex); 7606e6b1bd1SAl Viro /* 7616e6b1bd1SAl Viro * First re-do the cached lookup just in case it was created 7626e6b1bd1SAl Viro * while we waited for the directory semaphore.. 7636e6b1bd1SAl Viro * 7646e6b1bd1SAl Viro * FIXME! This could use version numbering or similar to 7656e6b1bd1SAl Viro * avoid unnecessary cache lookups. 7666e6b1bd1SAl Viro * 7676e6b1bd1SAl Viro * The "dcache_lock" is purely to protect the RCU list walker 7686e6b1bd1SAl Viro * from concurrent renames at this point (we mustn't get false 7696e6b1bd1SAl Viro * negatives from the RCU list walk here, unlike the optimistic 7706e6b1bd1SAl Viro * fast walk). 7716e6b1bd1SAl Viro * 7726e6b1bd1SAl Viro * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup 7736e6b1bd1SAl Viro */ 7746e6b1bd1SAl Viro dentry = d_lookup(parent, name); 7756e6b1bd1SAl Viro if (!dentry) { 7766e6b1bd1SAl Viro struct dentry *new; 7776e6b1bd1SAl Viro 7786e6b1bd1SAl Viro /* Don't create child dentry for a dead directory. */ 7796e6b1bd1SAl Viro dentry = ERR_PTR(-ENOENT); 7806e6b1bd1SAl Viro if (IS_DEADDIR(dir)) 7816e6b1bd1SAl Viro goto out_unlock; 7826e6b1bd1SAl Viro 7836e6b1bd1SAl Viro new = d_alloc(parent, name); 7846e6b1bd1SAl Viro dentry = ERR_PTR(-ENOMEM); 7856e6b1bd1SAl Viro if (new) { 7866e6b1bd1SAl Viro dentry = dir->i_op->lookup(dir, new, nd); 7876e6b1bd1SAl Viro if (dentry) 7886e6b1bd1SAl Viro dput(new); 7896e6b1bd1SAl Viro else 7906e6b1bd1SAl Viro dentry = new; 7916e6b1bd1SAl Viro } 7926e6b1bd1SAl Viro out_unlock: 7936e6b1bd1SAl Viro mutex_unlock(&dir->i_mutex); 7946e6b1bd1SAl Viro if (IS_ERR(dentry)) 7956e6b1bd1SAl Viro goto fail; 7966e6b1bd1SAl Viro goto done; 7976e6b1bd1SAl Viro } 7986e6b1bd1SAl Viro 7996e6b1bd1SAl Viro /* 8006e6b1bd1SAl Viro * Uhhuh! Nasty case: the cache was re-populated while 8016e6b1bd1SAl Viro * we waited on the semaphore. Need to revalidate. 8026e6b1bd1SAl Viro */ 8036e6b1bd1SAl Viro mutex_unlock(&dir->i_mutex); 8046e6b1bd1SAl Viro if (dentry->d_op && dentry->d_op->d_revalidate) { 8056e6b1bd1SAl Viro dentry = do_revalidate(dentry, nd); 8066e6b1bd1SAl Viro if (!dentry) 8076e6b1bd1SAl Viro dentry = ERR_PTR(-ENOENT); 8086e6b1bd1SAl Viro } 8091da177e4SLinus Torvalds if (IS_ERR(dentry)) 8101da177e4SLinus Torvalds goto fail; 8111da177e4SLinus Torvalds goto done; 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds need_revalidate: 814bcdc5e01SIan Kent dentry = do_revalidate(dentry, nd); 815bcdc5e01SIan Kent if (!dentry) 8161da177e4SLinus Torvalds goto need_lookup; 817bcdc5e01SIan Kent if (IS_ERR(dentry)) 818bcdc5e01SIan Kent goto fail; 819bcdc5e01SIan Kent goto done; 8201da177e4SLinus Torvalds 8211da177e4SLinus Torvalds fail: 8221da177e4SLinus Torvalds return PTR_ERR(dentry); 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds /* 8261da177e4SLinus Torvalds * Name resolution. 827ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 828ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 8291da177e4SLinus Torvalds * 830ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 831ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 8321da177e4SLinus Torvalds */ 8336de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 8341da177e4SLinus Torvalds { 8351da177e4SLinus Torvalds struct path next; 8361da177e4SLinus Torvalds struct inode *inode; 8371da177e4SLinus Torvalds int err; 8381da177e4SLinus Torvalds unsigned int lookup_flags = nd->flags; 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds while (*name=='/') 8411da177e4SLinus Torvalds name++; 8421da177e4SLinus Torvalds if (!*name) 8431da177e4SLinus Torvalds goto return_reval; 8441da177e4SLinus Torvalds 8454ac91378SJan Blunck inode = nd->path.dentry->d_inode; 8461da177e4SLinus Torvalds if (nd->depth) 847f55eab82STrond Myklebust lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE); 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 8501da177e4SLinus Torvalds for(;;) { 8511da177e4SLinus Torvalds unsigned long hash; 8521da177e4SLinus Torvalds struct qstr this; 8531da177e4SLinus Torvalds unsigned int c; 8541da177e4SLinus Torvalds 855cdce5d6bSTrond Myklebust nd->flags |= LOOKUP_CONTINUE; 856b75b5086SAl Viro err = exec_permission(inode); 8571da177e4SLinus Torvalds if (err) 8581da177e4SLinus Torvalds break; 8591da177e4SLinus Torvalds 8601da177e4SLinus Torvalds this.name = name; 8611da177e4SLinus Torvalds c = *(const unsigned char *)name; 8621da177e4SLinus Torvalds 8631da177e4SLinus Torvalds hash = init_name_hash(); 8641da177e4SLinus Torvalds do { 8651da177e4SLinus Torvalds name++; 8661da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 8671da177e4SLinus Torvalds c = *(const unsigned char *)name; 8681da177e4SLinus Torvalds } while (c && (c != '/')); 8691da177e4SLinus Torvalds this.len = name - (const char *) this.name; 8701da177e4SLinus Torvalds this.hash = end_name_hash(hash); 8711da177e4SLinus Torvalds 8721da177e4SLinus Torvalds /* remove trailing slashes? */ 8731da177e4SLinus Torvalds if (!c) 8741da177e4SLinus Torvalds goto last_component; 8751da177e4SLinus Torvalds while (*++name == '/'); 8761da177e4SLinus Torvalds if (!*name) 8771da177e4SLinus Torvalds goto last_with_slashes; 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds /* 8801da177e4SLinus Torvalds * "." and ".." are special - ".." especially so because it has 8811da177e4SLinus Torvalds * to be able to know about the current root directory and 8821da177e4SLinus Torvalds * parent relationships. 8831da177e4SLinus Torvalds */ 8841da177e4SLinus Torvalds if (this.name[0] == '.') switch (this.len) { 8851da177e4SLinus Torvalds default: 8861da177e4SLinus Torvalds break; 8871da177e4SLinus Torvalds case 2: 8881da177e4SLinus Torvalds if (this.name[1] != '.') 8891da177e4SLinus Torvalds break; 89058c465ebSAl Viro follow_dotdot(nd); 8914ac91378SJan Blunck inode = nd->path.dentry->d_inode; 8921da177e4SLinus Torvalds /* fallthrough */ 8931da177e4SLinus Torvalds case 1: 8941da177e4SLinus Torvalds continue; 8951da177e4SLinus Torvalds } 8961da177e4SLinus Torvalds /* This does the actual lookups.. */ 8971da177e4SLinus Torvalds err = do_lookup(nd, &this, &next); 8981da177e4SLinus Torvalds if (err) 8991da177e4SLinus Torvalds break; 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds err = -ENOENT; 9021da177e4SLinus Torvalds inode = next.dentry->d_inode; 9031da177e4SLinus Torvalds if (!inode) 9041da177e4SLinus Torvalds goto out_dput; 9051da177e4SLinus Torvalds 9061da177e4SLinus Torvalds if (inode->i_op->follow_link) { 90790ebe565SAl Viro err = do_follow_link(&next, nd); 9081da177e4SLinus Torvalds if (err) 9091da177e4SLinus Torvalds goto return_err; 9101da177e4SLinus Torvalds err = -ENOENT; 9114ac91378SJan Blunck inode = nd->path.dentry->d_inode; 9121da177e4SLinus Torvalds if (!inode) 9131da177e4SLinus Torvalds break; 91409dd17d3SMiklos Szeredi } else 91509dd17d3SMiklos Szeredi path_to_nameidata(&next, nd); 9161da177e4SLinus Torvalds err = -ENOTDIR; 9171da177e4SLinus Torvalds if (!inode->i_op->lookup) 9181da177e4SLinus Torvalds break; 9191da177e4SLinus Torvalds continue; 9201da177e4SLinus Torvalds /* here ends the main loop */ 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds last_with_slashes: 9231da177e4SLinus Torvalds lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 9241da177e4SLinus Torvalds last_component: 925f55eab82STrond Myklebust /* Clear LOOKUP_CONTINUE iff it was previously unset */ 926f55eab82STrond Myklebust nd->flags &= lookup_flags | ~LOOKUP_CONTINUE; 9271da177e4SLinus Torvalds if (lookup_flags & LOOKUP_PARENT) 9281da177e4SLinus Torvalds goto lookup_parent; 9291da177e4SLinus Torvalds if (this.name[0] == '.') switch (this.len) { 9301da177e4SLinus Torvalds default: 9311da177e4SLinus Torvalds break; 9321da177e4SLinus Torvalds case 2: 9331da177e4SLinus Torvalds if (this.name[1] != '.') 9341da177e4SLinus Torvalds break; 93558c465ebSAl Viro follow_dotdot(nd); 9364ac91378SJan Blunck inode = nd->path.dentry->d_inode; 9371da177e4SLinus Torvalds /* fallthrough */ 9381da177e4SLinus Torvalds case 1: 9391da177e4SLinus Torvalds goto return_reval; 9401da177e4SLinus Torvalds } 9411da177e4SLinus Torvalds err = do_lookup(nd, &this, &next); 9421da177e4SLinus Torvalds if (err) 9431da177e4SLinus Torvalds break; 9441da177e4SLinus Torvalds inode = next.dentry->d_inode; 9451da177e4SLinus Torvalds if ((lookup_flags & LOOKUP_FOLLOW) 946acfa4380SAl Viro && inode && inode->i_op->follow_link) { 94790ebe565SAl Viro err = do_follow_link(&next, nd); 9481da177e4SLinus Torvalds if (err) 9491da177e4SLinus Torvalds goto return_err; 9504ac91378SJan Blunck inode = nd->path.dentry->d_inode; 95109dd17d3SMiklos Szeredi } else 95209dd17d3SMiklos Szeredi path_to_nameidata(&next, nd); 9531da177e4SLinus Torvalds err = -ENOENT; 9541da177e4SLinus Torvalds if (!inode) 9551da177e4SLinus Torvalds break; 9561da177e4SLinus Torvalds if (lookup_flags & LOOKUP_DIRECTORY) { 9571da177e4SLinus Torvalds err = -ENOTDIR; 958acfa4380SAl Viro if (!inode->i_op->lookup) 9591da177e4SLinus Torvalds break; 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds goto return_base; 9621da177e4SLinus Torvalds lookup_parent: 9631da177e4SLinus Torvalds nd->last = this; 9641da177e4SLinus Torvalds nd->last_type = LAST_NORM; 9651da177e4SLinus Torvalds if (this.name[0] != '.') 9661da177e4SLinus Torvalds goto return_base; 9671da177e4SLinus Torvalds if (this.len == 1) 9681da177e4SLinus Torvalds nd->last_type = LAST_DOT; 9691da177e4SLinus Torvalds else if (this.len == 2 && this.name[1] == '.') 9701da177e4SLinus Torvalds nd->last_type = LAST_DOTDOT; 9711da177e4SLinus Torvalds else 9721da177e4SLinus Torvalds goto return_base; 9731da177e4SLinus Torvalds return_reval: 9741da177e4SLinus Torvalds /* 9751da177e4SLinus Torvalds * We bypassed the ordinary revalidation routines. 9761da177e4SLinus Torvalds * We may need to check the cached dentry for staleness. 9771da177e4SLinus Torvalds */ 9784ac91378SJan Blunck if (nd->path.dentry && nd->path.dentry->d_sb && 9794ac91378SJan Blunck (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) { 9801da177e4SLinus Torvalds err = -ESTALE; 9811da177e4SLinus Torvalds /* Note: we do not d_invalidate() */ 9824ac91378SJan Blunck if (!nd->path.dentry->d_op->d_revalidate( 9834ac91378SJan Blunck nd->path.dentry, nd)) 9841da177e4SLinus Torvalds break; 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds return_base: 9871da177e4SLinus Torvalds return 0; 9881da177e4SLinus Torvalds out_dput: 9891d957f9bSJan Blunck path_put_conditional(&next, nd); 9901da177e4SLinus Torvalds break; 9911da177e4SLinus Torvalds } 9921d957f9bSJan Blunck path_put(&nd->path); 9931da177e4SLinus Torvalds return_err: 9941da177e4SLinus Torvalds return err; 9951da177e4SLinus Torvalds } 9961da177e4SLinus Torvalds 997fc9b52cdSHarvey Harrison static int path_walk(const char *name, struct nameidata *nd) 9981da177e4SLinus Torvalds { 9996de88d72SAl Viro struct path save = nd->path; 10006de88d72SAl Viro int result; 10016de88d72SAl Viro 10021da177e4SLinus Torvalds current->total_link_count = 0; 10036de88d72SAl Viro 10046de88d72SAl Viro /* make sure the stuff we saved doesn't go away */ 10056de88d72SAl Viro path_get(&save); 10066de88d72SAl Viro 10076de88d72SAl Viro result = link_path_walk(name, nd); 10086de88d72SAl Viro if (result == -ESTALE) { 10096de88d72SAl Viro /* nd->path had been dropped */ 10106de88d72SAl Viro current->total_link_count = 0; 10116de88d72SAl Viro nd->path = save; 10126de88d72SAl Viro path_get(&nd->path); 10136de88d72SAl Viro nd->flags |= LOOKUP_REVAL; 10146de88d72SAl Viro result = link_path_walk(name, nd); 10156de88d72SAl Viro } 10166de88d72SAl Viro 10176de88d72SAl Viro path_put(&save); 10186de88d72SAl Viro 10196de88d72SAl Viro return result; 10201da177e4SLinus Torvalds } 10211da177e4SLinus Torvalds 10229b4a9b14SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd) 10231da177e4SLinus Torvalds { 1024ea3834d9SPrasanna Meda int retval = 0; 1025170aa3d0SUlrich Drepper int fput_needed; 1026170aa3d0SUlrich Drepper struct file *file; 10271da177e4SLinus Torvalds 10281da177e4SLinus Torvalds nd->last_type = LAST_ROOT; /* if there are only slashes... */ 10291da177e4SLinus Torvalds nd->flags = flags; 10301da177e4SLinus Torvalds nd->depth = 0; 10312a737871SAl Viro nd->root.mnt = NULL; 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds if (*name=='/') { 10342a737871SAl Viro set_root(nd); 10352a737871SAl Viro nd->path = nd->root; 10362a737871SAl Viro path_get(&nd->root); 10375590ff0dSUlrich Drepper } else if (dfd == AT_FDCWD) { 10382a737871SAl Viro struct fs_struct *fs = current->fs; 1039e518ddb7SAndreas Mohr read_lock(&fs->lock); 10406ac08c39SJan Blunck nd->path = fs->pwd; 10416ac08c39SJan Blunck path_get(&fs->pwd); 1042e518ddb7SAndreas Mohr read_unlock(&fs->lock); 10435590ff0dSUlrich Drepper } else { 10445590ff0dSUlrich Drepper struct dentry *dentry; 10455590ff0dSUlrich Drepper 10465590ff0dSUlrich Drepper file = fget_light(dfd, &fput_needed); 10475590ff0dSUlrich Drepper retval = -EBADF; 1048170aa3d0SUlrich Drepper if (!file) 10496d09bb62STrond Myklebust goto out_fail; 10505590ff0dSUlrich Drepper 10510f7fc9e4SJosef "Jeff" Sipek dentry = file->f_path.dentry; 10525590ff0dSUlrich Drepper 10535590ff0dSUlrich Drepper retval = -ENOTDIR; 1054170aa3d0SUlrich Drepper if (!S_ISDIR(dentry->d_inode->i_mode)) 10556d09bb62STrond Myklebust goto fput_fail; 10565590ff0dSUlrich Drepper 10575590ff0dSUlrich Drepper retval = file_permission(file, MAY_EXEC); 1058170aa3d0SUlrich Drepper if (retval) 10596d09bb62STrond Myklebust goto fput_fail; 10605590ff0dSUlrich Drepper 10615dd784d0SJan Blunck nd->path = file->f_path; 10625dd784d0SJan Blunck path_get(&file->f_path); 10635590ff0dSUlrich Drepper 10645590ff0dSUlrich Drepper fput_light(file, fput_needed); 10651da177e4SLinus Torvalds } 10669b4a9b14SAl Viro return 0; 10672dfdd266SJosef 'Jeff' Sipek 10689b4a9b14SAl Viro fput_fail: 10699b4a9b14SAl Viro fput_light(file, fput_needed); 10709b4a9b14SAl Viro out_fail: 10719b4a9b14SAl Viro return retval; 10729b4a9b14SAl Viro } 10739b4a9b14SAl Viro 10749b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 10759b4a9b14SAl Viro static int do_path_lookup(int dfd, const char *name, 10769b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 10779b4a9b14SAl Viro { 10789b4a9b14SAl Viro int retval = path_init(dfd, name, flags, nd); 10799b4a9b14SAl Viro if (!retval) 10802dfdd266SJosef 'Jeff' Sipek retval = path_walk(name, nd); 10814ac91378SJan Blunck if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry && 10824ac91378SJan Blunck nd->path.dentry->d_inode)) 10834ac91378SJan Blunck audit_inode(name, nd->path.dentry); 10842a737871SAl Viro if (nd->root.mnt) { 10852a737871SAl Viro path_put(&nd->root); 10862a737871SAl Viro nd->root.mnt = NULL; 10872a737871SAl Viro } 1088170aa3d0SUlrich Drepper return retval; 10891da177e4SLinus Torvalds } 10901da177e4SLinus Torvalds 1091fc9b52cdSHarvey Harrison int path_lookup(const char *name, unsigned int flags, 10925590ff0dSUlrich Drepper struct nameidata *nd) 10935590ff0dSUlrich Drepper { 10945590ff0dSUlrich Drepper return do_path_lookup(AT_FDCWD, name, flags, nd); 10955590ff0dSUlrich Drepper } 10965590ff0dSUlrich Drepper 1097d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1098d1811465SAl Viro { 1099d1811465SAl Viro struct nameidata nd; 1100d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1101d1811465SAl Viro if (!res) 1102d1811465SAl Viro *path = nd.path; 1103d1811465SAl Viro return res; 1104d1811465SAl Viro } 1105d1811465SAl Viro 110616f18200SJosef 'Jeff' Sipek /** 110716f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 110816f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 110916f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 111016f18200SJosef 'Jeff' Sipek * @name: pointer to file name 111116f18200SJosef 'Jeff' Sipek * @flags: lookup flags 111216f18200SJosef 'Jeff' Sipek * @nd: pointer to nameidata 111316f18200SJosef 'Jeff' Sipek */ 111416f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 111516f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 111616f18200SJosef 'Jeff' Sipek struct nameidata *nd) 111716f18200SJosef 'Jeff' Sipek { 111816f18200SJosef 'Jeff' Sipek int retval; 111916f18200SJosef 'Jeff' Sipek 112016f18200SJosef 'Jeff' Sipek /* same as do_path_lookup */ 112116f18200SJosef 'Jeff' Sipek nd->last_type = LAST_ROOT; 112216f18200SJosef 'Jeff' Sipek nd->flags = flags; 112316f18200SJosef 'Jeff' Sipek nd->depth = 0; 112416f18200SJosef 'Jeff' Sipek 1125c8e7f449SJan Blunck nd->path.dentry = dentry; 1126c8e7f449SJan Blunck nd->path.mnt = mnt; 1127c8e7f449SJan Blunck path_get(&nd->path); 11285b857119SAl Viro nd->root = nd->path; 11295b857119SAl Viro path_get(&nd->root); 113016f18200SJosef 'Jeff' Sipek 113116f18200SJosef 'Jeff' Sipek retval = path_walk(name, nd); 11324ac91378SJan Blunck if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry && 11334ac91378SJan Blunck nd->path.dentry->d_inode)) 11344ac91378SJan Blunck audit_inode(name, nd->path.dentry); 113516f18200SJosef 'Jeff' Sipek 11362a737871SAl Viro path_put(&nd->root); 11372a737871SAl Viro nd->root.mnt = NULL; 113816f18200SJosef 'Jeff' Sipek 11392a737871SAl Viro return retval; 114016f18200SJosef 'Jeff' Sipek } 114116f18200SJosef 'Jeff' Sipek 1142eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name, 1143eead1911SChristoph Hellwig struct dentry *base, struct nameidata *nd) 11441da177e4SLinus Torvalds { 11451da177e4SLinus Torvalds struct dentry *dentry; 11461da177e4SLinus Torvalds struct inode *inode; 11471da177e4SLinus Torvalds int err; 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds inode = base->d_inode; 11501da177e4SLinus Torvalds 11511da177e4SLinus Torvalds /* 11521da177e4SLinus Torvalds * See if the low-level filesystem might want 11531da177e4SLinus Torvalds * to use its own hash.. 11541da177e4SLinus Torvalds */ 11551da177e4SLinus Torvalds if (base->d_op && base->d_op->d_hash) { 11561da177e4SLinus Torvalds err = base->d_op->d_hash(base, name); 11571da177e4SLinus Torvalds dentry = ERR_PTR(err); 11581da177e4SLinus Torvalds if (err < 0) 11591da177e4SLinus Torvalds goto out; 11601da177e4SLinus Torvalds } 11611da177e4SLinus Torvalds 11626e6b1bd1SAl Viro dentry = __d_lookup(base, name); 11636e6b1bd1SAl Viro 11646e6b1bd1SAl Viro /* lockess __d_lookup may fail due to concurrent d_move() 11656e6b1bd1SAl Viro * in some unrelated directory, so try with d_lookup 11666e6b1bd1SAl Viro */ 11676e6b1bd1SAl Viro if (!dentry) 11686e6b1bd1SAl Viro dentry = d_lookup(base, name); 11696e6b1bd1SAl Viro 11706e6b1bd1SAl Viro if (dentry && dentry->d_op && dentry->d_op->d_revalidate) 11716e6b1bd1SAl Viro dentry = do_revalidate(dentry, nd); 11726e6b1bd1SAl Viro 11731da177e4SLinus Torvalds if (!dentry) { 1174d70b67c8SMiklos Szeredi struct dentry *new; 1175d70b67c8SMiklos Szeredi 1176d70b67c8SMiklos Szeredi /* Don't create child dentry for a dead directory. */ 1177d70b67c8SMiklos Szeredi dentry = ERR_PTR(-ENOENT); 1178d70b67c8SMiklos Szeredi if (IS_DEADDIR(inode)) 1179d70b67c8SMiklos Szeredi goto out; 1180d70b67c8SMiklos Szeredi 1181d70b67c8SMiklos Szeredi new = d_alloc(base, name); 11821da177e4SLinus Torvalds dentry = ERR_PTR(-ENOMEM); 11831da177e4SLinus Torvalds if (!new) 11841da177e4SLinus Torvalds goto out; 11851da177e4SLinus Torvalds dentry = inode->i_op->lookup(inode, new, nd); 11861da177e4SLinus Torvalds if (!dentry) 11871da177e4SLinus Torvalds dentry = new; 11881da177e4SLinus Torvalds else 11891da177e4SLinus Torvalds dput(new); 11901da177e4SLinus Torvalds } 11911da177e4SLinus Torvalds out: 11921da177e4SLinus Torvalds return dentry; 11931da177e4SLinus Torvalds } 11941da177e4SLinus Torvalds 1195057f6c01SJames Morris /* 1196057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1197057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1198057f6c01SJames Morris * SMP-safe. 1199057f6c01SJames Morris */ 1200a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 12011da177e4SLinus Torvalds { 1202eead1911SChristoph Hellwig int err; 1203eead1911SChristoph Hellwig 1204b75b5086SAl Viro err = exec_permission(nd->path.dentry->d_inode); 1205eead1911SChristoph Hellwig if (err) 1206eead1911SChristoph Hellwig return ERR_PTR(err); 12074ac91378SJan Blunck return __lookup_hash(&nd->last, nd->path.dentry, nd); 12081da177e4SLinus Torvalds } 12091da177e4SLinus Torvalds 1210eead1911SChristoph Hellwig static int __lookup_one_len(const char *name, struct qstr *this, 1211eead1911SChristoph Hellwig struct dentry *base, int len) 12121da177e4SLinus Torvalds { 12131da177e4SLinus Torvalds unsigned long hash; 12141da177e4SLinus Torvalds unsigned int c; 12151da177e4SLinus Torvalds 1216057f6c01SJames Morris this->name = name; 1217057f6c01SJames Morris this->len = len; 12181da177e4SLinus Torvalds if (!len) 1219057f6c01SJames Morris return -EACCES; 12201da177e4SLinus Torvalds 12211da177e4SLinus Torvalds hash = init_name_hash(); 12221da177e4SLinus Torvalds while (len--) { 12231da177e4SLinus Torvalds c = *(const unsigned char *)name++; 12241da177e4SLinus Torvalds if (c == '/' || c == '\0') 1225057f6c01SJames Morris return -EACCES; 12261da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 12271da177e4SLinus Torvalds } 1228057f6c01SJames Morris this->hash = end_name_hash(hash); 1229057f6c01SJames Morris return 0; 1230057f6c01SJames Morris } 12311da177e4SLinus Torvalds 1232eead1911SChristoph Hellwig /** 1233a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1234eead1911SChristoph Hellwig * @name: pathname component to lookup 1235eead1911SChristoph Hellwig * @base: base directory to lookup from 1236eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1237eead1911SChristoph Hellwig * 1238a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1239a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1240eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1241eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1242eead1911SChristoph Hellwig */ 1243057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1244057f6c01SJames Morris { 1245057f6c01SJames Morris int err; 1246057f6c01SJames Morris struct qstr this; 1247057f6c01SJames Morris 12482f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 12492f9092e1SDavid Woodhouse 1250057f6c01SJames Morris err = __lookup_one_len(name, &this, base, len); 1251057f6c01SJames Morris if (err) 1252057f6c01SJames Morris return ERR_PTR(err); 1253eead1911SChristoph Hellwig 1254b75b5086SAl Viro err = exec_permission(base->d_inode); 1255eead1911SChristoph Hellwig if (err) 1256eead1911SChristoph Hellwig return ERR_PTR(err); 125749705b77SChristoph Hellwig return __lookup_hash(&this, base, NULL); 1258057f6c01SJames Morris } 1259057f6c01SJames Morris 12602d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags, 12612d8f3038SAl Viro struct path *path) 12621da177e4SLinus Torvalds { 12632d8f3038SAl Viro struct nameidata nd; 12641da177e4SLinus Torvalds char *tmp = getname(name); 12651da177e4SLinus Torvalds int err = PTR_ERR(tmp); 12661da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 12672d8f3038SAl Viro 12682d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 12692d8f3038SAl Viro 12702d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 12711da177e4SLinus Torvalds putname(tmp); 12722d8f3038SAl Viro if (!err) 12732d8f3038SAl Viro *path = nd.path; 12741da177e4SLinus Torvalds } 12751da177e4SLinus Torvalds return err; 12761da177e4SLinus Torvalds } 12771da177e4SLinus Torvalds 12782ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 12792ad94ae6SAl Viro struct nameidata *nd, char **name) 12802ad94ae6SAl Viro { 12812ad94ae6SAl Viro char *s = getname(path); 12822ad94ae6SAl Viro int error; 12832ad94ae6SAl Viro 12842ad94ae6SAl Viro if (IS_ERR(s)) 12852ad94ae6SAl Viro return PTR_ERR(s); 12862ad94ae6SAl Viro 12872ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 12882ad94ae6SAl Viro if (error) 12892ad94ae6SAl Viro putname(s); 12902ad94ae6SAl Viro else 12912ad94ae6SAl Viro *name = s; 12922ad94ae6SAl Viro 12932ad94ae6SAl Viro return error; 12942ad94ae6SAl Viro } 12952ad94ae6SAl Viro 12961da177e4SLinus Torvalds /* 12971da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 12981da177e4SLinus Torvalds * minimal. 12991da177e4SLinus Torvalds */ 13001da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 13011da177e4SLinus Torvalds { 1302da9592edSDavid Howells uid_t fsuid = current_fsuid(); 1303da9592edSDavid Howells 13041da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 13051da177e4SLinus Torvalds return 0; 1306da9592edSDavid Howells if (inode->i_uid == fsuid) 13071da177e4SLinus Torvalds return 0; 1308da9592edSDavid Howells if (dir->i_uid == fsuid) 13091da177e4SLinus Torvalds return 0; 13101da177e4SLinus Torvalds return !capable(CAP_FOWNER); 13111da177e4SLinus Torvalds } 13121da177e4SLinus Torvalds 13131da177e4SLinus Torvalds /* 13141da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 13151da177e4SLinus Torvalds * whether the type of victim is right. 13161da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 13171da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 13181da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 13191da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 13201da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 13211da177e4SLinus Torvalds * a. be owner of dir, or 13221da177e4SLinus Torvalds * b. be owner of victim, or 13231da177e4SLinus Torvalds * c. have CAP_FOWNER capability 13241da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 13251da177e4SLinus Torvalds * links pointing to it. 13261da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 13271da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 13281da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 13291da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 13301da177e4SLinus Torvalds * nfs_async_unlink(). 13311da177e4SLinus Torvalds */ 1332858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 13331da177e4SLinus Torvalds { 13341da177e4SLinus Torvalds int error; 13351da177e4SLinus Torvalds 13361da177e4SLinus Torvalds if (!victim->d_inode) 13371da177e4SLinus Torvalds return -ENOENT; 13381da177e4SLinus Torvalds 13391da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 13405a190ae6SAl Viro audit_inode_child(victim->d_name.name, victim, dir); 13411da177e4SLinus Torvalds 1342f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 13431da177e4SLinus Torvalds if (error) 13441da177e4SLinus Torvalds return error; 13451da177e4SLinus Torvalds if (IS_APPEND(dir)) 13461da177e4SLinus Torvalds return -EPERM; 13471da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 1348f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 13491da177e4SLinus Torvalds return -EPERM; 13501da177e4SLinus Torvalds if (isdir) { 13511da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 13521da177e4SLinus Torvalds return -ENOTDIR; 13531da177e4SLinus Torvalds if (IS_ROOT(victim)) 13541da177e4SLinus Torvalds return -EBUSY; 13551da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 13561da177e4SLinus Torvalds return -EISDIR; 13571da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 13581da177e4SLinus Torvalds return -ENOENT; 13591da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 13601da177e4SLinus Torvalds return -EBUSY; 13611da177e4SLinus Torvalds return 0; 13621da177e4SLinus Torvalds } 13631da177e4SLinus Torvalds 13641da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 13651da177e4SLinus Torvalds * dir. 13661da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 13671da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 13681da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 13691da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 13701da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 13711da177e4SLinus Torvalds */ 1372a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 13731da177e4SLinus Torvalds { 13741da177e4SLinus Torvalds if (child->d_inode) 13751da177e4SLinus Torvalds return -EEXIST; 13761da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 13771da177e4SLinus Torvalds return -ENOENT; 1378f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 13791da177e4SLinus Torvalds } 13801da177e4SLinus Torvalds 13811da177e4SLinus Torvalds /* 13821da177e4SLinus Torvalds * O_DIRECTORY translates into forcing a directory lookup. 13831da177e4SLinus Torvalds */ 13841da177e4SLinus Torvalds static inline int lookup_flags(unsigned int f) 13851da177e4SLinus Torvalds { 13861da177e4SLinus Torvalds unsigned long retval = LOOKUP_FOLLOW; 13871da177e4SLinus Torvalds 13881da177e4SLinus Torvalds if (f & O_NOFOLLOW) 13891da177e4SLinus Torvalds retval &= ~LOOKUP_FOLLOW; 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds if (f & O_DIRECTORY) 13921da177e4SLinus Torvalds retval |= LOOKUP_DIRECTORY; 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds return retval; 13951da177e4SLinus Torvalds } 13961da177e4SLinus Torvalds 13971da177e4SLinus Torvalds /* 13981da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 13991da177e4SLinus Torvalds */ 14001da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 14011da177e4SLinus Torvalds { 14021da177e4SLinus Torvalds struct dentry *p; 14031da177e4SLinus Torvalds 14041da177e4SLinus Torvalds if (p1 == p2) { 1405f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 14061da177e4SLinus Torvalds return NULL; 14071da177e4SLinus Torvalds } 14081da177e4SLinus Torvalds 1409a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 14101da177e4SLinus Torvalds 1411e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 1412e2761a11SOGAWA Hirofumi if (p) { 1413f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 1414f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 14151da177e4SLinus Torvalds return p; 14161da177e4SLinus Torvalds } 14171da177e4SLinus Torvalds 1418e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 1419e2761a11SOGAWA Hirofumi if (p) { 1420f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1421f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 14221da177e4SLinus Torvalds return p; 14231da177e4SLinus Torvalds } 14241da177e4SLinus Torvalds 1425f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1426f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 14271da177e4SLinus Torvalds return NULL; 14281da177e4SLinus Torvalds } 14291da177e4SLinus Torvalds 14301da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 14311da177e4SLinus Torvalds { 14321b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 14331da177e4SLinus Torvalds if (p1 != p2) { 14341b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 1435a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 14361da177e4SLinus Torvalds } 14371da177e4SLinus Torvalds } 14381da177e4SLinus Torvalds 14391da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode, 14401da177e4SLinus Torvalds struct nameidata *nd) 14411da177e4SLinus Torvalds { 1442a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 14431da177e4SLinus Torvalds 14441da177e4SLinus Torvalds if (error) 14451da177e4SLinus Torvalds return error; 14461da177e4SLinus Torvalds 1447acfa4380SAl Viro if (!dir->i_op->create) 14481da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 14491da177e4SLinus Torvalds mode &= S_IALLUGO; 14501da177e4SLinus Torvalds mode |= S_IFREG; 14511da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 14521da177e4SLinus Torvalds if (error) 14531da177e4SLinus Torvalds return error; 14549e3509e2SJan Kara vfs_dq_init(dir); 14551da177e4SLinus Torvalds error = dir->i_op->create(dir, dentry, mode, nd); 1456a74574aaSStephen Smalley if (!error) 1457f38aa942SAmy Griffis fsnotify_create(dir, dentry); 14581da177e4SLinus Torvalds return error; 14591da177e4SLinus Torvalds } 14601da177e4SLinus Torvalds 14613fb64190SChristoph Hellwig int may_open(struct path *path, int acc_mode, int flag) 14621da177e4SLinus Torvalds { 14633fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 14641da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 14651da177e4SLinus Torvalds int error; 14661da177e4SLinus Torvalds 14671da177e4SLinus Torvalds if (!inode) 14681da177e4SLinus Torvalds return -ENOENT; 14691da177e4SLinus Torvalds 1470c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 1471c8fe8f30SChristoph Hellwig case S_IFLNK: 14721da177e4SLinus Torvalds return -ELOOP; 1473c8fe8f30SChristoph Hellwig case S_IFDIR: 1474c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 14751da177e4SLinus Torvalds return -EISDIR; 1476c8fe8f30SChristoph Hellwig break; 1477c8fe8f30SChristoph Hellwig case S_IFBLK: 1478c8fe8f30SChristoph Hellwig case S_IFCHR: 14793fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 14801da177e4SLinus Torvalds return -EACCES; 1481c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 1482c8fe8f30SChristoph Hellwig case S_IFIFO: 1483c8fe8f30SChristoph Hellwig case S_IFSOCK: 14841da177e4SLinus Torvalds flag &= ~O_TRUNC; 1485c8fe8f30SChristoph Hellwig break; 14864a3fd211SDave Hansen } 1487b41572e9SDave Hansen 14883fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 1489b41572e9SDave Hansen if (error) 1490b41572e9SDave Hansen return error; 14916146f0d5SMimi Zohar 14921da177e4SLinus Torvalds /* 14931da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 14941da177e4SLinus Torvalds */ 14951da177e4SLinus Torvalds if (IS_APPEND(inode)) { 14961da177e4SLinus Torvalds if ((flag & FMODE_WRITE) && !(flag & O_APPEND)) 14977715b521SAl Viro return -EPERM; 14981da177e4SLinus Torvalds if (flag & O_TRUNC) 14997715b521SAl Viro return -EPERM; 15001da177e4SLinus Torvalds } 15011da177e4SLinus Torvalds 15021da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 15037715b521SAl Viro if (flag & O_NOATIME && !is_owner_or_cap(inode)) 15047715b521SAl Viro return -EPERM; 15051da177e4SLinus Torvalds 15061da177e4SLinus Torvalds /* 15071da177e4SLinus Torvalds * Ensure there are no outstanding leases on the file. 15081da177e4SLinus Torvalds */ 1509b65a9cfcSAl Viro return break_lease(inode, flag); 15107715b521SAl Viro } 15117715b521SAl Viro 15127715b521SAl Viro static int handle_truncate(struct path *path) 15137715b521SAl Viro { 15147715b521SAl Viro struct inode *inode = path->dentry->d_inode; 15157715b521SAl Viro int error = get_write_access(inode); 15161da177e4SLinus Torvalds if (error) 15177715b521SAl Viro return error; 15181da177e4SLinus Torvalds /* 15191da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 15201da177e4SLinus Torvalds */ 15211da177e4SLinus Torvalds error = locks_verify_locked(inode); 1522be6d3e56SKentaro Takeda if (!error) 15233fb64190SChristoph Hellwig error = security_path_truncate(path, 0, 1524be6d3e56SKentaro Takeda ATTR_MTIME|ATTR_CTIME|ATTR_OPEN); 15251da177e4SLinus Torvalds if (!error) { 15267715b521SAl Viro error = do_truncate(path->dentry, 0, 1527d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 1528d139d7ffSMiklos Szeredi NULL); 15291da177e4SLinus Torvalds } 15301da177e4SLinus Torvalds put_write_access(inode); 1531acd0c935SMimi Zohar return error; 15321da177e4SLinus Torvalds } 15331da177e4SLinus Torvalds 1534d57999e1SDave Hansen /* 1535d57999e1SDave Hansen * Be careful about ever adding any more callers of this 1536d57999e1SDave Hansen * function. Its flags must be in the namei format, not 1537d57999e1SDave Hansen * what get passed to sys_open(). 1538d57999e1SDave Hansen */ 1539d57999e1SDave Hansen static int __open_namei_create(struct nameidata *nd, struct path *path, 1540aab520e2SDave Hansen int flag, int mode) 1541aab520e2SDave Hansen { 1542aab520e2SDave Hansen int error; 15434ac91378SJan Blunck struct dentry *dir = nd->path.dentry; 1544aab520e2SDave Hansen 1545aab520e2SDave Hansen if (!IS_POSIXACL(dir->d_inode)) 1546ce3b0f8dSAl Viro mode &= ~current_umask(); 1547be6d3e56SKentaro Takeda error = security_path_mknod(&nd->path, path->dentry, mode, 0); 1548be6d3e56SKentaro Takeda if (error) 1549be6d3e56SKentaro Takeda goto out_unlock; 1550aab520e2SDave Hansen error = vfs_create(dir->d_inode, path->dentry, mode, nd); 1551be6d3e56SKentaro Takeda out_unlock: 1552aab520e2SDave Hansen mutex_unlock(&dir->d_inode->i_mutex); 15534ac91378SJan Blunck dput(nd->path.dentry); 15544ac91378SJan Blunck nd->path.dentry = path->dentry; 1555aab520e2SDave Hansen if (error) 1556aab520e2SDave Hansen return error; 1557aab520e2SDave Hansen /* Don't check for write permission, don't truncate */ 15583fb64190SChristoph Hellwig return may_open(&nd->path, 0, flag & ~O_TRUNC); 1559aab520e2SDave Hansen } 1560aab520e2SDave Hansen 15611da177e4SLinus Torvalds /* 1562d57999e1SDave Hansen * Note that while the flag value (low two bits) for sys_open means: 1563d57999e1SDave Hansen * 00 - read-only 1564d57999e1SDave Hansen * 01 - write-only 1565d57999e1SDave Hansen * 10 - read-write 1566d57999e1SDave Hansen * 11 - special 1567d57999e1SDave Hansen * it is changed into 1568d57999e1SDave Hansen * 00 - no permissions needed 1569d57999e1SDave Hansen * 01 - read-permission 1570d57999e1SDave Hansen * 10 - write-permission 1571d57999e1SDave Hansen * 11 - read-write 1572d57999e1SDave Hansen * for the internal routines (ie open_namei()/follow_link() etc) 1573d57999e1SDave Hansen * This is more logical, and also allows the 00 "no perm needed" 1574d57999e1SDave Hansen * to be used for symlinks (where the permissions are checked 1575d57999e1SDave Hansen * later). 1576d57999e1SDave Hansen * 1577d57999e1SDave Hansen */ 1578d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 1579d57999e1SDave Hansen { 1580d57999e1SDave Hansen if ((flag+1) & O_ACCMODE) 1581d57999e1SDave Hansen flag++; 1582d57999e1SDave Hansen return flag; 1583d57999e1SDave Hansen } 1584d57999e1SDave Hansen 15857715b521SAl Viro static int open_will_truncate(int flag, struct inode *inode) 15864a3fd211SDave Hansen { 1587d57999e1SDave Hansen /* 15884a3fd211SDave Hansen * We'll never write to the fs underlying 15894a3fd211SDave Hansen * a device file. 15904a3fd211SDave Hansen */ 15914a3fd211SDave Hansen if (special_file(inode->i_mode)) 15924a3fd211SDave Hansen return 0; 15934a3fd211SDave Hansen return (flag & O_TRUNC); 15944a3fd211SDave Hansen } 15954a3fd211SDave Hansen 15964a3fd211SDave Hansen /* 15974a3fd211SDave Hansen * Note that the low bits of the passed in "open_flag" 15984a3fd211SDave Hansen * are not the same as in the local variable "flag". See 15994a3fd211SDave Hansen * open_to_namei_flags() for more details. 16001da177e4SLinus Torvalds */ 1601a70e65dfSChristoph Hellwig struct file *do_filp_open(int dfd, const char *pathname, 16026e8341a1SAl Viro int open_flag, int mode, int acc_mode) 16031da177e4SLinus Torvalds { 16044a3fd211SDave Hansen struct file *filp; 1605a70e65dfSChristoph Hellwig struct nameidata nd; 16066e8341a1SAl Viro int error; 16079850c056SAl Viro struct path path; 16081da177e4SLinus Torvalds struct dentry *dir; 16091da177e4SLinus Torvalds int count = 0; 16107715b521SAl Viro int will_truncate; 1611d57999e1SDave Hansen int flag = open_to_namei_flags(open_flag); 16129850c056SAl Viro int force_reval = 0; 16131da177e4SLinus Torvalds 16146b2f3d1fSChristoph Hellwig /* 16156b2f3d1fSChristoph Hellwig * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only 16166b2f3d1fSChristoph Hellwig * check for O_DSYNC if the need any syncing at all we enforce it's 16176b2f3d1fSChristoph Hellwig * always set instead of having to deal with possibly weird behaviour 16186b2f3d1fSChristoph Hellwig * for malicious applications setting only __O_SYNC. 16196b2f3d1fSChristoph Hellwig */ 16206b2f3d1fSChristoph Hellwig if (open_flag & __O_SYNC) 16216b2f3d1fSChristoph Hellwig open_flag |= O_DSYNC; 16226b2f3d1fSChristoph Hellwig 16236e8341a1SAl Viro if (!acc_mode) 16246d125529SAl Viro acc_mode = MAY_OPEN | ACC_MODE(open_flag); 16251da177e4SLinus Torvalds 1626834f2a4aSTrond Myklebust /* O_TRUNC implies we need access checks for write permissions */ 1627834f2a4aSTrond Myklebust if (flag & O_TRUNC) 1628834f2a4aSTrond Myklebust acc_mode |= MAY_WRITE; 1629834f2a4aSTrond Myklebust 16301da177e4SLinus Torvalds /* Allow the LSM permission hook to distinguish append 16311da177e4SLinus Torvalds access from general write access. */ 16321da177e4SLinus Torvalds if (flag & O_APPEND) 16331da177e4SLinus Torvalds acc_mode |= MAY_APPEND; 16341da177e4SLinus Torvalds 16351da177e4SLinus Torvalds /* 16361da177e4SLinus Torvalds * The simplest case - just a plain lookup. 16371da177e4SLinus Torvalds */ 16381da177e4SLinus Torvalds if (!(flag & O_CREAT)) { 16392dd6d1f4SAl Viro filp = get_empty_filp(); 16402dd6d1f4SAl Viro 16412dd6d1f4SAl Viro if (filp == NULL) 16422dd6d1f4SAl Viro return ERR_PTR(-ENFILE); 16432dd6d1f4SAl Viro nd.intent.open.file = filp; 1644482928d5SAl Viro filp->f_flags = open_flag; 16452dd6d1f4SAl Viro nd.intent.open.flags = flag; 16462dd6d1f4SAl Viro nd.intent.open.create_mode = 0; 16472dd6d1f4SAl Viro error = do_path_lookup(dfd, pathname, 16482dd6d1f4SAl Viro lookup_flags(flag)|LOOKUP_OPEN, &nd); 16492dd6d1f4SAl Viro if (IS_ERR(nd.intent.open.file)) { 16502dd6d1f4SAl Viro if (error == 0) { 16512dd6d1f4SAl Viro error = PTR_ERR(nd.intent.open.file); 16522dd6d1f4SAl Viro path_put(&nd.path); 16532dd6d1f4SAl Viro } 16542dd6d1f4SAl Viro } else if (error) 16552dd6d1f4SAl Viro release_open_intent(&nd); 16561da177e4SLinus Torvalds if (error) 1657a70e65dfSChristoph Hellwig return ERR_PTR(error); 16581da177e4SLinus Torvalds goto ok; 16591da177e4SLinus Torvalds } 16601da177e4SLinus Torvalds 16611da177e4SLinus Torvalds /* 16621da177e4SLinus Torvalds * Create - we need to know the parent. 16631da177e4SLinus Torvalds */ 16649850c056SAl Viro reval: 16659b4a9b14SAl Viro error = path_init(dfd, pathname, LOOKUP_PARENT, &nd); 16661da177e4SLinus Torvalds if (error) 1667a70e65dfSChristoph Hellwig return ERR_PTR(error); 16689850c056SAl Viro if (force_reval) 16699850c056SAl Viro nd.flags |= LOOKUP_REVAL; 16709b4a9b14SAl Viro error = path_walk(pathname, &nd); 1671654f562cSJ. R. Okajima if (error) { 1672654f562cSJ. R. Okajima if (nd.root.mnt) 1673654f562cSJ. R. Okajima path_put(&nd.root); 16749b4a9b14SAl Viro return ERR_PTR(error); 1675654f562cSJ. R. Okajima } 16769b4a9b14SAl Viro if (unlikely(!audit_dummy_context())) 16779b4a9b14SAl Viro audit_inode(pathname, nd.path.dentry); 16781da177e4SLinus Torvalds 16791da177e4SLinus Torvalds /* 16801da177e4SLinus Torvalds * We have the parent and last component. First of all, check 16811da177e4SLinus Torvalds * that we are not asked to creat(2) an obvious directory - that 16821da177e4SLinus Torvalds * will not do. 16831da177e4SLinus Torvalds */ 16841da177e4SLinus Torvalds error = -EISDIR; 1685a70e65dfSChristoph Hellwig if (nd.last_type != LAST_NORM || nd.last.name[nd.last.len]) 16868737f3a1SAl Viro goto exit_parent; 16871da177e4SLinus Torvalds 16888737f3a1SAl Viro error = -ENFILE; 16898737f3a1SAl Viro filp = get_empty_filp(); 16908737f3a1SAl Viro if (filp == NULL) 16918737f3a1SAl Viro goto exit_parent; 16928737f3a1SAl Viro nd.intent.open.file = filp; 1693482928d5SAl Viro filp->f_flags = open_flag; 16948737f3a1SAl Viro nd.intent.open.flags = flag; 16958737f3a1SAl Viro nd.intent.open.create_mode = mode; 1696a70e65dfSChristoph Hellwig dir = nd.path.dentry; 1697a70e65dfSChristoph Hellwig nd.flags &= ~LOOKUP_PARENT; 16988737f3a1SAl Viro nd.flags |= LOOKUP_CREATE | LOOKUP_OPEN; 16993516586aSAl Viro if (flag & O_EXCL) 17003516586aSAl Viro nd.flags |= LOOKUP_EXCL; 17011b1dcc1bSJes Sorensen mutex_lock(&dir->d_inode->i_mutex); 1702a70e65dfSChristoph Hellwig path.dentry = lookup_hash(&nd); 1703a70e65dfSChristoph Hellwig path.mnt = nd.path.mnt; 17041da177e4SLinus Torvalds 17051da177e4SLinus Torvalds do_last: 17064e7506e4SAl Viro error = PTR_ERR(path.dentry); 17074e7506e4SAl Viro if (IS_ERR(path.dentry)) { 17081b1dcc1bSJes Sorensen mutex_unlock(&dir->d_inode->i_mutex); 17091da177e4SLinus Torvalds goto exit; 17101da177e4SLinus Torvalds } 17111da177e4SLinus Torvalds 1712a70e65dfSChristoph Hellwig if (IS_ERR(nd.intent.open.file)) { 1713a70e65dfSChristoph Hellwig error = PTR_ERR(nd.intent.open.file); 17144a3fd211SDave Hansen goto exit_mutex_unlock; 17154af4c52fSOleg Drokin } 17164af4c52fSOleg Drokin 17171da177e4SLinus Torvalds /* Negative dentry, just create the file */ 17184e7506e4SAl Viro if (!path.dentry->d_inode) { 17194a3fd211SDave Hansen /* 17204a3fd211SDave Hansen * This write is needed to ensure that a 17214a3fd211SDave Hansen * ro->rw transition does not occur between 17224a3fd211SDave Hansen * the time when the file is created and when 17234a3fd211SDave Hansen * a permanent write count is taken through 17244a3fd211SDave Hansen * the 'struct file' in nameidata_to_filp(). 17254a3fd211SDave Hansen */ 17264a3fd211SDave Hansen error = mnt_want_write(nd.path.mnt); 17271da177e4SLinus Torvalds if (error) 17284a3fd211SDave Hansen goto exit_mutex_unlock; 17294a3fd211SDave Hansen error = __open_namei_create(&nd, &path, flag, mode); 17304a3fd211SDave Hansen if (error) { 17314a3fd211SDave Hansen mnt_drop_write(nd.path.mnt); 17321da177e4SLinus Torvalds goto exit; 17334a3fd211SDave Hansen } 1734482928d5SAl Viro filp = nameidata_to_filp(&nd); 17354a3fd211SDave Hansen mnt_drop_write(nd.path.mnt); 1736654f562cSJ. R. Okajima if (nd.root.mnt) 1737654f562cSJ. R. Okajima path_put(&nd.root); 1738b65a9cfcSAl Viro if (!IS_ERR(filp)) { 1739b65a9cfcSAl Viro error = ima_path_check(&filp->f_path, filp->f_mode & 17401429b3ecSAl Viro (MAY_READ | MAY_WRITE | MAY_EXEC)); 1741b65a9cfcSAl Viro if (error) { 1742b65a9cfcSAl Viro fput(filp); 1743b65a9cfcSAl Viro filp = ERR_PTR(error); 1744b65a9cfcSAl Viro } 1745b65a9cfcSAl Viro } 17464a3fd211SDave Hansen return filp; 17471da177e4SLinus Torvalds } 17481da177e4SLinus Torvalds 17491da177e4SLinus Torvalds /* 17501da177e4SLinus Torvalds * It already exists. 17511da177e4SLinus Torvalds */ 17521b1dcc1bSJes Sorensen mutex_unlock(&dir->d_inode->i_mutex); 17535a190ae6SAl Viro audit_inode(pathname, path.dentry); 17541da177e4SLinus Torvalds 17551da177e4SLinus Torvalds error = -EEXIST; 17561da177e4SLinus Torvalds if (flag & O_EXCL) 17571da177e4SLinus Torvalds goto exit_dput; 17581da177e4SLinus Torvalds 1759e13b210fSAl Viro if (__follow_mount(&path)) { 17601da177e4SLinus Torvalds error = -ELOOP; 1761ba7a4c1aSAl Viro if (flag & O_NOFOLLOW) 1762ba7a4c1aSAl Viro goto exit_dput; 17631da177e4SLinus Torvalds } 17643e2efce0SAmy Griffis 17651da177e4SLinus Torvalds error = -ENOENT; 17664e7506e4SAl Viro if (!path.dentry->d_inode) 17671da177e4SLinus Torvalds goto exit_dput; 1768acfa4380SAl Viro if (path.dentry->d_inode->i_op->follow_link) 17691da177e4SLinus Torvalds goto do_link; 17701da177e4SLinus Torvalds 1771a70e65dfSChristoph Hellwig path_to_nameidata(&path, &nd); 17721da177e4SLinus Torvalds error = -EISDIR; 1773cb59861fSJeff Layton if (S_ISDIR(path.dentry->d_inode->i_mode)) 17741da177e4SLinus Torvalds goto exit; 17751da177e4SLinus Torvalds ok: 17764a3fd211SDave Hansen /* 17774a3fd211SDave Hansen * Consider: 17784a3fd211SDave Hansen * 1. may_open() truncates a file 17794a3fd211SDave Hansen * 2. a rw->ro mount transition occurs 17804a3fd211SDave Hansen * 3. nameidata_to_filp() fails due to 17814a3fd211SDave Hansen * the ro mount. 17824a3fd211SDave Hansen * That would be inconsistent, and should 17834a3fd211SDave Hansen * be avoided. Taking this mnt write here 17844a3fd211SDave Hansen * ensures that (2) can not occur. 17854a3fd211SDave Hansen */ 17867715b521SAl Viro will_truncate = open_will_truncate(flag, nd.path.dentry->d_inode); 17877715b521SAl Viro if (will_truncate) { 17884a3fd211SDave Hansen error = mnt_want_write(nd.path.mnt); 17891da177e4SLinus Torvalds if (error) 17901da177e4SLinus Torvalds goto exit; 17914a3fd211SDave Hansen } 17923fb64190SChristoph Hellwig error = may_open(&nd.path, acc_mode, flag); 17934a3fd211SDave Hansen if (error) { 17947715b521SAl Viro if (will_truncate) 17954a3fd211SDave Hansen mnt_drop_write(nd.path.mnt); 17964a3fd211SDave Hansen goto exit; 17974a3fd211SDave Hansen } 1798482928d5SAl Viro filp = nameidata_to_filp(&nd); 1799b65a9cfcSAl Viro if (!IS_ERR(filp)) { 1800b65a9cfcSAl Viro error = ima_path_check(&filp->f_path, filp->f_mode & 18011429b3ecSAl Viro (MAY_READ | MAY_WRITE | MAY_EXEC)); 1802b65a9cfcSAl Viro if (error) { 1803b65a9cfcSAl Viro fput(filp); 1804b65a9cfcSAl Viro filp = ERR_PTR(error); 18057715b521SAl Viro } 1806b65a9cfcSAl Viro } 1807b65a9cfcSAl Viro if (!IS_ERR(filp)) { 18087715b521SAl Viro if (acc_mode & MAY_WRITE) 18097715b521SAl Viro vfs_dq_init(nd.path.dentry->d_inode); 18107715b521SAl Viro 18117715b521SAl Viro if (will_truncate) { 18127715b521SAl Viro error = handle_truncate(&nd.path); 18137715b521SAl Viro if (error) { 18147715b521SAl Viro fput(filp); 1815b65a9cfcSAl Viro filp = ERR_PTR(error); 1816b65a9cfcSAl Viro } 18177715b521SAl Viro } 18187715b521SAl Viro } 18194a3fd211SDave Hansen /* 18204a3fd211SDave Hansen * It is now safe to drop the mnt write 18214a3fd211SDave Hansen * because the filp has had a write taken 18224a3fd211SDave Hansen * on its behalf. 18234a3fd211SDave Hansen */ 18247715b521SAl Viro if (will_truncate) 18254a3fd211SDave Hansen mnt_drop_write(nd.path.mnt); 1826654f562cSJ. R. Okajima if (nd.root.mnt) 1827654f562cSJ. R. Okajima path_put(&nd.root); 18284a3fd211SDave Hansen return filp; 18291da177e4SLinus Torvalds 18304a3fd211SDave Hansen exit_mutex_unlock: 18314a3fd211SDave Hansen mutex_unlock(&dir->d_inode->i_mutex); 18321da177e4SLinus Torvalds exit_dput: 1833a70e65dfSChristoph Hellwig path_put_conditional(&path, &nd); 18341da177e4SLinus Torvalds exit: 1835a70e65dfSChristoph Hellwig if (!IS_ERR(nd.intent.open.file)) 1836a70e65dfSChristoph Hellwig release_open_intent(&nd); 18378737f3a1SAl Viro exit_parent: 18382a737871SAl Viro if (nd.root.mnt) 18392a737871SAl Viro path_put(&nd.root); 1840a70e65dfSChristoph Hellwig path_put(&nd.path); 1841a70e65dfSChristoph Hellwig return ERR_PTR(error); 18421da177e4SLinus Torvalds 18431da177e4SLinus Torvalds do_link: 18441da177e4SLinus Torvalds error = -ELOOP; 18451da177e4SLinus Torvalds if (flag & O_NOFOLLOW) 18461da177e4SLinus Torvalds goto exit_dput; 18471da177e4SLinus Torvalds /* 18481da177e4SLinus Torvalds * This is subtle. Instead of calling do_follow_link() we do the 18491da177e4SLinus Torvalds * thing by hands. The reason is that this way we have zero link_count 18501da177e4SLinus Torvalds * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT. 18511da177e4SLinus Torvalds * After that we have the parent and last component, i.e. 18521da177e4SLinus Torvalds * we are in the same situation as after the first path_walk(). 18531da177e4SLinus Torvalds * Well, almost - if the last component is normal we get its copy 18541da177e4SLinus Torvalds * stored in nd->last.name and we will have to putname() it when we 18551da177e4SLinus Torvalds * are done. Procfs-like symlinks just set LAST_BIND. 18561da177e4SLinus Torvalds */ 1857a70e65dfSChristoph Hellwig nd.flags |= LOOKUP_PARENT; 1858a70e65dfSChristoph Hellwig error = security_inode_follow_link(path.dentry, &nd); 18591da177e4SLinus Torvalds if (error) 18601da177e4SLinus Torvalds goto exit_dput; 1861a70e65dfSChristoph Hellwig error = __do_follow_link(&path, &nd); 1862258fa999SAl Viro path_put(&path); 1863de459215SKirill Korotaev if (error) { 1864de459215SKirill Korotaev /* Does someone understand code flow here? Or it is only 1865de459215SKirill Korotaev * me so stupid? Anathema to whoever designed this non-sense 1866de459215SKirill Korotaev * with "intent.open". 1867de459215SKirill Korotaev */ 1868a70e65dfSChristoph Hellwig release_open_intent(&nd); 1869654f562cSJ. R. Okajima if (nd.root.mnt) 1870654f562cSJ. R. Okajima path_put(&nd.root); 18719850c056SAl Viro if (error == -ESTALE && !force_reval) { 18729850c056SAl Viro force_reval = 1; 18739850c056SAl Viro goto reval; 18749850c056SAl Viro } 1875a70e65dfSChristoph Hellwig return ERR_PTR(error); 1876de459215SKirill Korotaev } 1877a70e65dfSChristoph Hellwig nd.flags &= ~LOOKUP_PARENT; 1878a70e65dfSChristoph Hellwig if (nd.last_type == LAST_BIND) 18791da177e4SLinus Torvalds goto ok; 18801da177e4SLinus Torvalds error = -EISDIR; 1881a70e65dfSChristoph Hellwig if (nd.last_type != LAST_NORM) 18821da177e4SLinus Torvalds goto exit; 1883a70e65dfSChristoph Hellwig if (nd.last.name[nd.last.len]) { 1884a70e65dfSChristoph Hellwig __putname(nd.last.name); 18851da177e4SLinus Torvalds goto exit; 18861da177e4SLinus Torvalds } 18871da177e4SLinus Torvalds error = -ELOOP; 18881da177e4SLinus Torvalds if (count++==32) { 1889a70e65dfSChristoph Hellwig __putname(nd.last.name); 18901da177e4SLinus Torvalds goto exit; 18911da177e4SLinus Torvalds } 1892a70e65dfSChristoph Hellwig dir = nd.path.dentry; 18931b1dcc1bSJes Sorensen mutex_lock(&dir->d_inode->i_mutex); 1894a70e65dfSChristoph Hellwig path.dentry = lookup_hash(&nd); 1895a70e65dfSChristoph Hellwig path.mnt = nd.path.mnt; 1896a70e65dfSChristoph Hellwig __putname(nd.last.name); 18971da177e4SLinus Torvalds goto do_last; 18981da177e4SLinus Torvalds } 18991da177e4SLinus Torvalds 19001da177e4SLinus Torvalds /** 1901a70e65dfSChristoph Hellwig * filp_open - open file and return file pointer 1902a70e65dfSChristoph Hellwig * 1903a70e65dfSChristoph Hellwig * @filename: path to open 1904a70e65dfSChristoph Hellwig * @flags: open flags as per the open(2) second argument 1905a70e65dfSChristoph Hellwig * @mode: mode for the new file if O_CREAT is set, else ignored 1906a70e65dfSChristoph Hellwig * 1907a70e65dfSChristoph Hellwig * This is the helper to open a file from kernelspace if you really 1908a70e65dfSChristoph Hellwig * have to. But in generally you should not do this, so please move 1909a70e65dfSChristoph Hellwig * along, nothing to see here.. 1910a70e65dfSChristoph Hellwig */ 1911a70e65dfSChristoph Hellwig struct file *filp_open(const char *filename, int flags, int mode) 1912a70e65dfSChristoph Hellwig { 19136e8341a1SAl Viro return do_filp_open(AT_FDCWD, filename, flags, mode, 0); 1914a70e65dfSChristoph Hellwig } 1915a70e65dfSChristoph Hellwig EXPORT_SYMBOL(filp_open); 1916a70e65dfSChristoph Hellwig 1917a70e65dfSChristoph Hellwig /** 19181da177e4SLinus Torvalds * lookup_create - lookup a dentry, creating it if it doesn't exist 19191da177e4SLinus Torvalds * @nd: nameidata info 19201da177e4SLinus Torvalds * @is_dir: directory flag 19211da177e4SLinus Torvalds * 19221da177e4SLinus Torvalds * Simple function to lookup and return a dentry and create it 19231da177e4SLinus Torvalds * if it doesn't exist. Is SMP-safe. 1924c663e5d8SChristoph Hellwig * 19254ac91378SJan Blunck * Returns with nd->path.dentry->d_inode->i_mutex locked. 19261da177e4SLinus Torvalds */ 19271da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir) 19281da177e4SLinus Torvalds { 1929c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 19301da177e4SLinus Torvalds 19314ac91378SJan Blunck mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 1932c663e5d8SChristoph Hellwig /* 1933c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 1934c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 1935c663e5d8SChristoph Hellwig */ 19361da177e4SLinus Torvalds if (nd->last_type != LAST_NORM) 19371da177e4SLinus Torvalds goto fail; 19381da177e4SLinus Torvalds nd->flags &= ~LOOKUP_PARENT; 19393516586aSAl Viro nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL; 1940a634904aSASANO Masahiro nd->intent.open.flags = O_EXCL; 1941c663e5d8SChristoph Hellwig 1942c663e5d8SChristoph Hellwig /* 1943c663e5d8SChristoph Hellwig * Do the final lookup. 1944c663e5d8SChristoph Hellwig */ 194549705b77SChristoph Hellwig dentry = lookup_hash(nd); 19461da177e4SLinus Torvalds if (IS_ERR(dentry)) 19471da177e4SLinus Torvalds goto fail; 1948c663e5d8SChristoph Hellwig 1949e9baf6e5SAl Viro if (dentry->d_inode) 1950e9baf6e5SAl Viro goto eexist; 1951c663e5d8SChristoph Hellwig /* 1952c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 1953c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 1954c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 1955c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 1956c663e5d8SChristoph Hellwig */ 1957e9baf6e5SAl Viro if (unlikely(!is_dir && nd->last.name[nd->last.len])) { 19581da177e4SLinus Torvalds dput(dentry); 19591da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 1960e9baf6e5SAl Viro } 1961e9baf6e5SAl Viro return dentry; 1962e9baf6e5SAl Viro eexist: 1963e9baf6e5SAl Viro dput(dentry); 1964e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 19651da177e4SLinus Torvalds fail: 19661da177e4SLinus Torvalds return dentry; 19671da177e4SLinus Torvalds } 1968f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create); 19691da177e4SLinus Torvalds 19701da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 19711da177e4SLinus Torvalds { 1972a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 19731da177e4SLinus Torvalds 19741da177e4SLinus Torvalds if (error) 19751da177e4SLinus Torvalds return error; 19761da177e4SLinus Torvalds 19771da177e4SLinus Torvalds if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) 19781da177e4SLinus Torvalds return -EPERM; 19791da177e4SLinus Torvalds 1980acfa4380SAl Viro if (!dir->i_op->mknod) 19811da177e4SLinus Torvalds return -EPERM; 19821da177e4SLinus Torvalds 198308ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 198408ce5f16SSerge E. Hallyn if (error) 198508ce5f16SSerge E. Hallyn return error; 198608ce5f16SSerge E. Hallyn 19871da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 19881da177e4SLinus Torvalds if (error) 19891da177e4SLinus Torvalds return error; 19901da177e4SLinus Torvalds 19919e3509e2SJan Kara vfs_dq_init(dir); 19921da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 1993a74574aaSStephen Smalley if (!error) 1994f38aa942SAmy Griffis fsnotify_create(dir, dentry); 19951da177e4SLinus Torvalds return error; 19961da177e4SLinus Torvalds } 19971da177e4SLinus Torvalds 1998463c3197SDave Hansen static int may_mknod(mode_t mode) 1999463c3197SDave Hansen { 2000463c3197SDave Hansen switch (mode & S_IFMT) { 2001463c3197SDave Hansen case S_IFREG: 2002463c3197SDave Hansen case S_IFCHR: 2003463c3197SDave Hansen case S_IFBLK: 2004463c3197SDave Hansen case S_IFIFO: 2005463c3197SDave Hansen case S_IFSOCK: 2006463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2007463c3197SDave Hansen return 0; 2008463c3197SDave Hansen case S_IFDIR: 2009463c3197SDave Hansen return -EPERM; 2010463c3197SDave Hansen default: 2011463c3197SDave Hansen return -EINVAL; 2012463c3197SDave Hansen } 2013463c3197SDave Hansen } 2014463c3197SDave Hansen 20152e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode, 20162e4d0924SHeiko Carstens unsigned, dev) 20171da177e4SLinus Torvalds { 20182ad94ae6SAl Viro int error; 20191da177e4SLinus Torvalds char *tmp; 20201da177e4SLinus Torvalds struct dentry *dentry; 20211da177e4SLinus Torvalds struct nameidata nd; 20221da177e4SLinus Torvalds 20231da177e4SLinus Torvalds if (S_ISDIR(mode)) 20241da177e4SLinus Torvalds return -EPERM; 20251da177e4SLinus Torvalds 20262ad94ae6SAl Viro error = user_path_parent(dfd, filename, &nd, &tmp); 20271da177e4SLinus Torvalds if (error) 20282ad94ae6SAl Viro return error; 20292ad94ae6SAl Viro 20301da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 2031463c3197SDave Hansen if (IS_ERR(dentry)) { 20321da177e4SLinus Torvalds error = PTR_ERR(dentry); 2033463c3197SDave Hansen goto out_unlock; 2034463c3197SDave Hansen } 20354ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2036ce3b0f8dSAl Viro mode &= ~current_umask(); 2037463c3197SDave Hansen error = may_mknod(mode); 2038463c3197SDave Hansen if (error) 2039463c3197SDave Hansen goto out_dput; 2040463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2041463c3197SDave Hansen if (error) 2042463c3197SDave Hansen goto out_dput; 2043be6d3e56SKentaro Takeda error = security_path_mknod(&nd.path, dentry, mode, dev); 2044be6d3e56SKentaro Takeda if (error) 2045be6d3e56SKentaro Takeda goto out_drop_write; 20461da177e4SLinus Torvalds switch (mode & S_IFMT) { 20471da177e4SLinus Torvalds case 0: case S_IFREG: 20484ac91378SJan Blunck error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd); 20491da177e4SLinus Torvalds break; 20501da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 20514ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode, 20521da177e4SLinus Torvalds new_decode_dev(dev)); 20531da177e4SLinus Torvalds break; 20541da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 20554ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0); 20561da177e4SLinus Torvalds break; 20571da177e4SLinus Torvalds } 2058be6d3e56SKentaro Takeda out_drop_write: 2059463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2060463c3197SDave Hansen out_dput: 20611da177e4SLinus Torvalds dput(dentry); 2062463c3197SDave Hansen out_unlock: 20634ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 20641d957f9bSJan Blunck path_put(&nd.path); 20651da177e4SLinus Torvalds putname(tmp); 20661da177e4SLinus Torvalds 20671da177e4SLinus Torvalds return error; 20681da177e4SLinus Torvalds } 20691da177e4SLinus Torvalds 20703480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev) 20715590ff0dSUlrich Drepper { 20725590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 20735590ff0dSUlrich Drepper } 20745590ff0dSUlrich Drepper 20751da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 20761da177e4SLinus Torvalds { 2077a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 20781da177e4SLinus Torvalds 20791da177e4SLinus Torvalds if (error) 20801da177e4SLinus Torvalds return error; 20811da177e4SLinus Torvalds 2082acfa4380SAl Viro if (!dir->i_op->mkdir) 20831da177e4SLinus Torvalds return -EPERM; 20841da177e4SLinus Torvalds 20851da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 20861da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 20871da177e4SLinus Torvalds if (error) 20881da177e4SLinus Torvalds return error; 20891da177e4SLinus Torvalds 20909e3509e2SJan Kara vfs_dq_init(dir); 20911da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2092a74574aaSStephen Smalley if (!error) 2093f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 20941da177e4SLinus Torvalds return error; 20951da177e4SLinus Torvalds } 20961da177e4SLinus Torvalds 20972e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode) 20981da177e4SLinus Torvalds { 20991da177e4SLinus Torvalds int error = 0; 21001da177e4SLinus Torvalds char * tmp; 21016902d925SDave Hansen struct dentry *dentry; 21026902d925SDave Hansen struct nameidata nd; 21031da177e4SLinus Torvalds 21042ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &tmp); 21052ad94ae6SAl Viro if (error) 21066902d925SDave Hansen goto out_err; 21071da177e4SLinus Torvalds 21081da177e4SLinus Torvalds dentry = lookup_create(&nd, 1); 21091da177e4SLinus Torvalds error = PTR_ERR(dentry); 21106902d925SDave Hansen if (IS_ERR(dentry)) 21116902d925SDave Hansen goto out_unlock; 21126902d925SDave Hansen 21134ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2114ce3b0f8dSAl Viro mode &= ~current_umask(); 2115463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2116463c3197SDave Hansen if (error) 2117463c3197SDave Hansen goto out_dput; 2118be6d3e56SKentaro Takeda error = security_path_mkdir(&nd.path, dentry, mode); 2119be6d3e56SKentaro Takeda if (error) 2120be6d3e56SKentaro Takeda goto out_drop_write; 21214ac91378SJan Blunck error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode); 2122be6d3e56SKentaro Takeda out_drop_write: 2123463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2124463c3197SDave Hansen out_dput: 21251da177e4SLinus Torvalds dput(dentry); 21266902d925SDave Hansen out_unlock: 21274ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 21281d957f9bSJan Blunck path_put(&nd.path); 21291da177e4SLinus Torvalds putname(tmp); 21306902d925SDave Hansen out_err: 21311da177e4SLinus Torvalds return error; 21321da177e4SLinus Torvalds } 21331da177e4SLinus Torvalds 21343cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode) 21355590ff0dSUlrich Drepper { 21365590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 21375590ff0dSUlrich Drepper } 21385590ff0dSUlrich Drepper 21391da177e4SLinus Torvalds /* 21401da177e4SLinus Torvalds * We try to drop the dentry early: we should have 21411da177e4SLinus Torvalds * a usage count of 2 if we're the only user of this 21421da177e4SLinus Torvalds * dentry, and if that is true (possibly after pruning 21431da177e4SLinus Torvalds * the dcache), then we drop the dentry now. 21441da177e4SLinus Torvalds * 21451da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 21461da177e4SLinus Torvalds * do a 21471da177e4SLinus Torvalds * 21481da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 21491da177e4SLinus Torvalds * return -EBUSY; 21501da177e4SLinus Torvalds * 21511da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 21521da177e4SLinus Torvalds * that is still in use by something else.. 21531da177e4SLinus Torvalds */ 21541da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 21551da177e4SLinus Torvalds { 21561da177e4SLinus Torvalds dget(dentry); 21571da177e4SLinus Torvalds shrink_dcache_parent(dentry); 21581da177e4SLinus Torvalds spin_lock(&dcache_lock); 21591da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 21601da177e4SLinus Torvalds if (atomic_read(&dentry->d_count) == 2) 21611da177e4SLinus Torvalds __d_drop(dentry); 21621da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 21631da177e4SLinus Torvalds spin_unlock(&dcache_lock); 21641da177e4SLinus Torvalds } 21651da177e4SLinus Torvalds 21661da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 21671da177e4SLinus Torvalds { 21681da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 21691da177e4SLinus Torvalds 21701da177e4SLinus Torvalds if (error) 21711da177e4SLinus Torvalds return error; 21721da177e4SLinus Torvalds 2173acfa4380SAl Viro if (!dir->i_op->rmdir) 21741da177e4SLinus Torvalds return -EPERM; 21751da177e4SLinus Torvalds 21769e3509e2SJan Kara vfs_dq_init(dir); 21771da177e4SLinus Torvalds 21781b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 21791da177e4SLinus Torvalds dentry_unhash(dentry); 21801da177e4SLinus Torvalds if (d_mountpoint(dentry)) 21811da177e4SLinus Torvalds error = -EBUSY; 21821da177e4SLinus Torvalds else { 21831da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 21841da177e4SLinus Torvalds if (!error) { 21851da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 21861da177e4SLinus Torvalds if (!error) 21871da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 21881da177e4SLinus Torvalds } 21891da177e4SLinus Torvalds } 21901b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 21911da177e4SLinus Torvalds if (!error) { 21921da177e4SLinus Torvalds d_delete(dentry); 21931da177e4SLinus Torvalds } 21941da177e4SLinus Torvalds dput(dentry); 21951da177e4SLinus Torvalds 21961da177e4SLinus Torvalds return error; 21971da177e4SLinus Torvalds } 21981da177e4SLinus Torvalds 21995590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 22001da177e4SLinus Torvalds { 22011da177e4SLinus Torvalds int error = 0; 22021da177e4SLinus Torvalds char * name; 22031da177e4SLinus Torvalds struct dentry *dentry; 22041da177e4SLinus Torvalds struct nameidata nd; 22051da177e4SLinus Torvalds 22062ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 22071da177e4SLinus Torvalds if (error) 22082ad94ae6SAl Viro return error; 22091da177e4SLinus Torvalds 22101da177e4SLinus Torvalds switch(nd.last_type) { 22111da177e4SLinus Torvalds case LAST_DOTDOT: 22121da177e4SLinus Torvalds error = -ENOTEMPTY; 22131da177e4SLinus Torvalds goto exit1; 22141da177e4SLinus Torvalds case LAST_DOT: 22151da177e4SLinus Torvalds error = -EINVAL; 22161da177e4SLinus Torvalds goto exit1; 22171da177e4SLinus Torvalds case LAST_ROOT: 22181da177e4SLinus Torvalds error = -EBUSY; 22191da177e4SLinus Torvalds goto exit1; 22201da177e4SLinus Torvalds } 22210612d9fbSOGAWA Hirofumi 22220612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 22230612d9fbSOGAWA Hirofumi 22244ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 222549705b77SChristoph Hellwig dentry = lookup_hash(&nd); 22261da177e4SLinus Torvalds error = PTR_ERR(dentry); 22276902d925SDave Hansen if (IS_ERR(dentry)) 22286902d925SDave Hansen goto exit2; 22290622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 22300622753bSDave Hansen if (error) 22310622753bSDave Hansen goto exit3; 2232be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 2233be6d3e56SKentaro Takeda if (error) 2234be6d3e56SKentaro Takeda goto exit4; 22354ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 2236be6d3e56SKentaro Takeda exit4: 22370622753bSDave Hansen mnt_drop_write(nd.path.mnt); 22380622753bSDave Hansen exit3: 22391da177e4SLinus Torvalds dput(dentry); 22406902d925SDave Hansen exit2: 22414ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 22421da177e4SLinus Torvalds exit1: 22431d957f9bSJan Blunck path_put(&nd.path); 22441da177e4SLinus Torvalds putname(name); 22451da177e4SLinus Torvalds return error; 22461da177e4SLinus Torvalds } 22471da177e4SLinus Torvalds 22483cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 22495590ff0dSUlrich Drepper { 22505590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 22515590ff0dSUlrich Drepper } 22525590ff0dSUlrich Drepper 22531da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 22541da177e4SLinus Torvalds { 22551da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 22561da177e4SLinus Torvalds 22571da177e4SLinus Torvalds if (error) 22581da177e4SLinus Torvalds return error; 22591da177e4SLinus Torvalds 2260acfa4380SAl Viro if (!dir->i_op->unlink) 22611da177e4SLinus Torvalds return -EPERM; 22621da177e4SLinus Torvalds 22639e3509e2SJan Kara vfs_dq_init(dir); 22641da177e4SLinus Torvalds 22651b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 22661da177e4SLinus Torvalds if (d_mountpoint(dentry)) 22671da177e4SLinus Torvalds error = -EBUSY; 22681da177e4SLinus Torvalds else { 22691da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 22701da177e4SLinus Torvalds if (!error) 22711da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 22721da177e4SLinus Torvalds } 22731b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 22741da177e4SLinus Torvalds 22751da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 22761da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 2277ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 22781da177e4SLinus Torvalds d_delete(dentry); 22791da177e4SLinus Torvalds } 22800eeca283SRobert Love 22811da177e4SLinus Torvalds return error; 22821da177e4SLinus Torvalds } 22831da177e4SLinus Torvalds 22841da177e4SLinus Torvalds /* 22851da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 22861b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 22871da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 22881da177e4SLinus Torvalds * while waiting on the I/O. 22891da177e4SLinus Torvalds */ 22905590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 22911da177e4SLinus Torvalds { 22922ad94ae6SAl Viro int error; 22931da177e4SLinus Torvalds char *name; 22941da177e4SLinus Torvalds struct dentry *dentry; 22951da177e4SLinus Torvalds struct nameidata nd; 22961da177e4SLinus Torvalds struct inode *inode = NULL; 22971da177e4SLinus Torvalds 22982ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 22991da177e4SLinus Torvalds if (error) 23002ad94ae6SAl Viro return error; 23012ad94ae6SAl Viro 23021da177e4SLinus Torvalds error = -EISDIR; 23031da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 23041da177e4SLinus Torvalds goto exit1; 23050612d9fbSOGAWA Hirofumi 23060612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 23070612d9fbSOGAWA Hirofumi 23084ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 230949705b77SChristoph Hellwig dentry = lookup_hash(&nd); 23101da177e4SLinus Torvalds error = PTR_ERR(dentry); 23111da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 23121da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 23131da177e4SLinus Torvalds if (nd.last.name[nd.last.len]) 23141da177e4SLinus Torvalds goto slashes; 23151da177e4SLinus Torvalds inode = dentry->d_inode; 23161da177e4SLinus Torvalds if (inode) 23171da177e4SLinus Torvalds atomic_inc(&inode->i_count); 23180622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 23190622753bSDave Hansen if (error) 23200622753bSDave Hansen goto exit2; 2321be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 2322be6d3e56SKentaro Takeda if (error) 2323be6d3e56SKentaro Takeda goto exit3; 23244ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 2325be6d3e56SKentaro Takeda exit3: 23260622753bSDave Hansen mnt_drop_write(nd.path.mnt); 23271da177e4SLinus Torvalds exit2: 23281da177e4SLinus Torvalds dput(dentry); 23291da177e4SLinus Torvalds } 23304ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 23311da177e4SLinus Torvalds if (inode) 23321da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 23331da177e4SLinus Torvalds exit1: 23341d957f9bSJan Blunck path_put(&nd.path); 23351da177e4SLinus Torvalds putname(name); 23361da177e4SLinus Torvalds return error; 23371da177e4SLinus Torvalds 23381da177e4SLinus Torvalds slashes: 23391da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 23401da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 23411da177e4SLinus Torvalds goto exit2; 23421da177e4SLinus Torvalds } 23431da177e4SLinus Torvalds 23442e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 23455590ff0dSUlrich Drepper { 23465590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 23475590ff0dSUlrich Drepper return -EINVAL; 23485590ff0dSUlrich Drepper 23495590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 23505590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 23515590ff0dSUlrich Drepper 23525590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 23535590ff0dSUlrich Drepper } 23545590ff0dSUlrich Drepper 23553480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 23565590ff0dSUlrich Drepper { 23575590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 23585590ff0dSUlrich Drepper } 23595590ff0dSUlrich Drepper 2360db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 23611da177e4SLinus Torvalds { 2362a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 23631da177e4SLinus Torvalds 23641da177e4SLinus Torvalds if (error) 23651da177e4SLinus Torvalds return error; 23661da177e4SLinus Torvalds 2367acfa4380SAl Viro if (!dir->i_op->symlink) 23681da177e4SLinus Torvalds return -EPERM; 23691da177e4SLinus Torvalds 23701da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 23711da177e4SLinus Torvalds if (error) 23721da177e4SLinus Torvalds return error; 23731da177e4SLinus Torvalds 23749e3509e2SJan Kara vfs_dq_init(dir); 23751da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 2376a74574aaSStephen Smalley if (!error) 2377f38aa942SAmy Griffis fsnotify_create(dir, dentry); 23781da177e4SLinus Torvalds return error; 23791da177e4SLinus Torvalds } 23801da177e4SLinus Torvalds 23812e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 23822e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 23831da177e4SLinus Torvalds { 23842ad94ae6SAl Viro int error; 23851da177e4SLinus Torvalds char *from; 23861da177e4SLinus Torvalds char *to; 23876902d925SDave Hansen struct dentry *dentry; 23886902d925SDave Hansen struct nameidata nd; 23891da177e4SLinus Torvalds 23901da177e4SLinus Torvalds from = getname(oldname); 23911da177e4SLinus Torvalds if (IS_ERR(from)) 23921da177e4SLinus Torvalds return PTR_ERR(from); 23932ad94ae6SAl Viro 23942ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 23952ad94ae6SAl Viro if (error) 23966902d925SDave Hansen goto out_putname; 23971da177e4SLinus Torvalds 23981da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 23991da177e4SLinus Torvalds error = PTR_ERR(dentry); 24006902d925SDave Hansen if (IS_ERR(dentry)) 24016902d925SDave Hansen goto out_unlock; 24026902d925SDave Hansen 240375c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 240475c3f29dSDave Hansen if (error) 240575c3f29dSDave Hansen goto out_dput; 2406be6d3e56SKentaro Takeda error = security_path_symlink(&nd.path, dentry, from); 2407be6d3e56SKentaro Takeda if (error) 2408be6d3e56SKentaro Takeda goto out_drop_write; 2409db2e747bSMiklos Szeredi error = vfs_symlink(nd.path.dentry->d_inode, dentry, from); 2410be6d3e56SKentaro Takeda out_drop_write: 241175c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 241275c3f29dSDave Hansen out_dput: 24131da177e4SLinus Torvalds dput(dentry); 24146902d925SDave Hansen out_unlock: 24154ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 24161d957f9bSJan Blunck path_put(&nd.path); 24171da177e4SLinus Torvalds putname(to); 24186902d925SDave Hansen out_putname: 24191da177e4SLinus Torvalds putname(from); 24201da177e4SLinus Torvalds return error; 24211da177e4SLinus Torvalds } 24221da177e4SLinus Torvalds 24233480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 24245590ff0dSUlrich Drepper { 24255590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 24265590ff0dSUlrich Drepper } 24275590ff0dSUlrich Drepper 24281da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 24291da177e4SLinus Torvalds { 24301da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 24311da177e4SLinus Torvalds int error; 24321da177e4SLinus Torvalds 24331da177e4SLinus Torvalds if (!inode) 24341da177e4SLinus Torvalds return -ENOENT; 24351da177e4SLinus Torvalds 2436a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 24371da177e4SLinus Torvalds if (error) 24381da177e4SLinus Torvalds return error; 24391da177e4SLinus Torvalds 24401da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 24411da177e4SLinus Torvalds return -EXDEV; 24421da177e4SLinus Torvalds 24431da177e4SLinus Torvalds /* 24441da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 24451da177e4SLinus Torvalds */ 24461da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 24471da177e4SLinus Torvalds return -EPERM; 2448acfa4380SAl Viro if (!dir->i_op->link) 24491da177e4SLinus Torvalds return -EPERM; 24507e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 24511da177e4SLinus Torvalds return -EPERM; 24521da177e4SLinus Torvalds 24531da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 24541da177e4SLinus Torvalds if (error) 24551da177e4SLinus Torvalds return error; 24561da177e4SLinus Torvalds 24577e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 24589e3509e2SJan Kara vfs_dq_init(dir); 24591da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 24607e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 2461e31e14ecSStephen Smalley if (!error) 24627e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 24631da177e4SLinus Torvalds return error; 24641da177e4SLinus Torvalds } 24651da177e4SLinus Torvalds 24661da177e4SLinus Torvalds /* 24671da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 24681da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 24691da177e4SLinus Torvalds * newname. --KAB 24701da177e4SLinus Torvalds * 24711da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 24721da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 24731da177e4SLinus Torvalds * and other special files. --ADM 24741da177e4SLinus Torvalds */ 24752e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 24762e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 24771da177e4SLinus Torvalds { 24781da177e4SLinus Torvalds struct dentry *new_dentry; 24792d8f3038SAl Viro struct nameidata nd; 24802d8f3038SAl Viro struct path old_path; 24811da177e4SLinus Torvalds int error; 24821da177e4SLinus Torvalds char *to; 24831da177e4SLinus Torvalds 248445c9b11aSUlrich Drepper if ((flags & ~AT_SYMLINK_FOLLOW) != 0) 2485c04030e1SUlrich Drepper return -EINVAL; 2486c04030e1SUlrich Drepper 24872d8f3038SAl Viro error = user_path_at(olddfd, oldname, 248845c9b11aSUlrich Drepper flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0, 24892d8f3038SAl Viro &old_path); 24901da177e4SLinus Torvalds if (error) 24912ad94ae6SAl Viro return error; 24922ad94ae6SAl Viro 24932ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 24941da177e4SLinus Torvalds if (error) 24951da177e4SLinus Torvalds goto out; 24961da177e4SLinus Torvalds error = -EXDEV; 24972d8f3038SAl Viro if (old_path.mnt != nd.path.mnt) 24981da177e4SLinus Torvalds goto out_release; 24991da177e4SLinus Torvalds new_dentry = lookup_create(&nd, 0); 25001da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 25016902d925SDave Hansen if (IS_ERR(new_dentry)) 25026902d925SDave Hansen goto out_unlock; 250375c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 250475c3f29dSDave Hansen if (error) 250575c3f29dSDave Hansen goto out_dput; 2506be6d3e56SKentaro Takeda error = security_path_link(old_path.dentry, &nd.path, new_dentry); 2507be6d3e56SKentaro Takeda if (error) 2508be6d3e56SKentaro Takeda goto out_drop_write; 25092d8f3038SAl Viro error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry); 2510be6d3e56SKentaro Takeda out_drop_write: 251175c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 251275c3f29dSDave Hansen out_dput: 25131da177e4SLinus Torvalds dput(new_dentry); 25146902d925SDave Hansen out_unlock: 25154ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 25161da177e4SLinus Torvalds out_release: 25171d957f9bSJan Blunck path_put(&nd.path); 25182ad94ae6SAl Viro putname(to); 25191da177e4SLinus Torvalds out: 25202d8f3038SAl Viro path_put(&old_path); 25211da177e4SLinus Torvalds 25221da177e4SLinus Torvalds return error; 25231da177e4SLinus Torvalds } 25241da177e4SLinus Torvalds 25253480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 25265590ff0dSUlrich Drepper { 2527c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 25285590ff0dSUlrich Drepper } 25295590ff0dSUlrich Drepper 25301da177e4SLinus Torvalds /* 25311da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 25321da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 25331da177e4SLinus Torvalds * Problems: 25341da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 25351da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 25361da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 2537a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 25381da177e4SLinus Torvalds * story. 25391da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 25401b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 25411da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 25421da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 2543a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 25441da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 25451da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 25461da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 2547a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 25481da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 25491da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 25501da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 25511da177e4SLinus Torvalds * d) some filesystems don't support opened-but-unlinked directories, 25521da177e4SLinus Torvalds * either because of layout or because they are not ready to deal with 25531da177e4SLinus Torvalds * all cases correctly. The latter will be fixed (taking this sort of 25541da177e4SLinus Torvalds * stuff into VFS), but the former is not going away. Solution: the same 25551da177e4SLinus Torvalds * trick as in rmdir(). 25561da177e4SLinus Torvalds * e) conversion from fhandle to dentry may come in the wrong moment - when 25571b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 25581da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 2559c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 25601da177e4SLinus Torvalds * locking]. 25611da177e4SLinus Torvalds */ 256275c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 25631da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 25641da177e4SLinus Torvalds { 25651da177e4SLinus Torvalds int error = 0; 25661da177e4SLinus Torvalds struct inode *target; 25671da177e4SLinus Torvalds 25681da177e4SLinus Torvalds /* 25691da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 25701da177e4SLinus Torvalds * we'll need to flip '..'. 25711da177e4SLinus Torvalds */ 25721da177e4SLinus Torvalds if (new_dir != old_dir) { 2573f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 25741da177e4SLinus Torvalds if (error) 25751da177e4SLinus Torvalds return error; 25761da177e4SLinus Torvalds } 25771da177e4SLinus Torvalds 25781da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 25791da177e4SLinus Torvalds if (error) 25801da177e4SLinus Torvalds return error; 25811da177e4SLinus Torvalds 25821da177e4SLinus Torvalds target = new_dentry->d_inode; 25831da177e4SLinus Torvalds if (target) { 25841b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 25851da177e4SLinus Torvalds dentry_unhash(new_dentry); 25861da177e4SLinus Torvalds } 25871da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 25881da177e4SLinus Torvalds error = -EBUSY; 25891da177e4SLinus Torvalds else 25901da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 25911da177e4SLinus Torvalds if (target) { 25921da177e4SLinus Torvalds if (!error) 25931da177e4SLinus Torvalds target->i_flags |= S_DEAD; 25941b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 25951da177e4SLinus Torvalds if (d_unhashed(new_dentry)) 25961da177e4SLinus Torvalds d_rehash(new_dentry); 25971da177e4SLinus Torvalds dput(new_dentry); 25981da177e4SLinus Torvalds } 2599e31e14ecSStephen Smalley if (!error) 2600349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 26011da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 26021da177e4SLinus Torvalds return error; 26031da177e4SLinus Torvalds } 26041da177e4SLinus Torvalds 260575c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 26061da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 26071da177e4SLinus Torvalds { 26081da177e4SLinus Torvalds struct inode *target; 26091da177e4SLinus Torvalds int error; 26101da177e4SLinus Torvalds 26111da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 26121da177e4SLinus Torvalds if (error) 26131da177e4SLinus Torvalds return error; 26141da177e4SLinus Torvalds 26151da177e4SLinus Torvalds dget(new_dentry); 26161da177e4SLinus Torvalds target = new_dentry->d_inode; 26171da177e4SLinus Torvalds if (target) 26181b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 26191da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 26201da177e4SLinus Torvalds error = -EBUSY; 26211da177e4SLinus Torvalds else 26221da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 26231da177e4SLinus Torvalds if (!error) { 2624349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 26251da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 26261da177e4SLinus Torvalds } 26271da177e4SLinus Torvalds if (target) 26281b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 26291da177e4SLinus Torvalds dput(new_dentry); 26301da177e4SLinus Torvalds return error; 26311da177e4SLinus Torvalds } 26321da177e4SLinus Torvalds 26331da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 26341da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 26351da177e4SLinus Torvalds { 26361da177e4SLinus Torvalds int error; 26371da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 26380eeca283SRobert Love const char *old_name; 26391da177e4SLinus Torvalds 26401da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 26411da177e4SLinus Torvalds return 0; 26421da177e4SLinus Torvalds 26431da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 26441da177e4SLinus Torvalds if (error) 26451da177e4SLinus Torvalds return error; 26461da177e4SLinus Torvalds 26471da177e4SLinus Torvalds if (!new_dentry->d_inode) 2648a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 26491da177e4SLinus Torvalds else 26501da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 26511da177e4SLinus Torvalds if (error) 26521da177e4SLinus Torvalds return error; 26531da177e4SLinus Torvalds 2654acfa4380SAl Viro if (!old_dir->i_op->rename) 26551da177e4SLinus Torvalds return -EPERM; 26561da177e4SLinus Torvalds 26579e3509e2SJan Kara vfs_dq_init(old_dir); 26589e3509e2SJan Kara vfs_dq_init(new_dir); 26591da177e4SLinus Torvalds 26600eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 26610eeca283SRobert Love 26621da177e4SLinus Torvalds if (is_dir) 26631da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 26641da177e4SLinus Torvalds else 26651da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 26661da177e4SLinus Torvalds if (!error) { 26670eeca283SRobert Love const char *new_name = old_dentry->d_name.name; 266889204c40SJohn McCutchan fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir, 26695a190ae6SAl Viro new_dentry->d_inode, old_dentry); 26701da177e4SLinus Torvalds } 26710eeca283SRobert Love fsnotify_oldname_free(old_name); 26720eeca283SRobert Love 26731da177e4SLinus Torvalds return error; 26741da177e4SLinus Torvalds } 26751da177e4SLinus Torvalds 26762e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 26772e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 26781da177e4SLinus Torvalds { 26791da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 26801da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 26811da177e4SLinus Torvalds struct dentry *trap; 26821da177e4SLinus Torvalds struct nameidata oldnd, newnd; 26832ad94ae6SAl Viro char *from; 26842ad94ae6SAl Viro char *to; 26852ad94ae6SAl Viro int error; 26861da177e4SLinus Torvalds 26872ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 26881da177e4SLinus Torvalds if (error) 26891da177e4SLinus Torvalds goto exit; 26901da177e4SLinus Torvalds 26912ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 26921da177e4SLinus Torvalds if (error) 26931da177e4SLinus Torvalds goto exit1; 26941da177e4SLinus Torvalds 26951da177e4SLinus Torvalds error = -EXDEV; 26964ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 26971da177e4SLinus Torvalds goto exit2; 26981da177e4SLinus Torvalds 26994ac91378SJan Blunck old_dir = oldnd.path.dentry; 27001da177e4SLinus Torvalds error = -EBUSY; 27011da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 27021da177e4SLinus Torvalds goto exit2; 27031da177e4SLinus Torvalds 27044ac91378SJan Blunck new_dir = newnd.path.dentry; 27051da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 27061da177e4SLinus Torvalds goto exit2; 27071da177e4SLinus Torvalds 27080612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 27090612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 27104e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 27110612d9fbSOGAWA Hirofumi 27121da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 27131da177e4SLinus Torvalds 271449705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 27151da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 27161da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 27171da177e4SLinus Torvalds goto exit3; 27181da177e4SLinus Torvalds /* source must exist */ 27191da177e4SLinus Torvalds error = -ENOENT; 27201da177e4SLinus Torvalds if (!old_dentry->d_inode) 27211da177e4SLinus Torvalds goto exit4; 27221da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 27231da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 27241da177e4SLinus Torvalds error = -ENOTDIR; 27251da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 27261da177e4SLinus Torvalds goto exit4; 27271da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 27281da177e4SLinus Torvalds goto exit4; 27291da177e4SLinus Torvalds } 27301da177e4SLinus Torvalds /* source should not be ancestor of target */ 27311da177e4SLinus Torvalds error = -EINVAL; 27321da177e4SLinus Torvalds if (old_dentry == trap) 27331da177e4SLinus Torvalds goto exit4; 273449705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 27351da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 27361da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 27371da177e4SLinus Torvalds goto exit4; 27381da177e4SLinus Torvalds /* target should not be an ancestor of source */ 27391da177e4SLinus Torvalds error = -ENOTEMPTY; 27401da177e4SLinus Torvalds if (new_dentry == trap) 27411da177e4SLinus Torvalds goto exit5; 27421da177e4SLinus Torvalds 27439079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 27449079b1ebSDave Hansen if (error) 27459079b1ebSDave Hansen goto exit5; 2746be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 2747be6d3e56SKentaro Takeda &newnd.path, new_dentry); 2748be6d3e56SKentaro Takeda if (error) 2749be6d3e56SKentaro Takeda goto exit6; 27501da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 27511da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 2752be6d3e56SKentaro Takeda exit6: 27539079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 27541da177e4SLinus Torvalds exit5: 27551da177e4SLinus Torvalds dput(new_dentry); 27561da177e4SLinus Torvalds exit4: 27571da177e4SLinus Torvalds dput(old_dentry); 27581da177e4SLinus Torvalds exit3: 27591da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 27601da177e4SLinus Torvalds exit2: 27611d957f9bSJan Blunck path_put(&newnd.path); 27622ad94ae6SAl Viro putname(to); 27631da177e4SLinus Torvalds exit1: 27641d957f9bSJan Blunck path_put(&oldnd.path); 27651da177e4SLinus Torvalds putname(from); 27662ad94ae6SAl Viro exit: 27671da177e4SLinus Torvalds return error; 27681da177e4SLinus Torvalds } 27691da177e4SLinus Torvalds 2770a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 27715590ff0dSUlrich Drepper { 27725590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 27735590ff0dSUlrich Drepper } 27745590ff0dSUlrich Drepper 27751da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 27761da177e4SLinus Torvalds { 27771da177e4SLinus Torvalds int len; 27781da177e4SLinus Torvalds 27791da177e4SLinus Torvalds len = PTR_ERR(link); 27801da177e4SLinus Torvalds if (IS_ERR(link)) 27811da177e4SLinus Torvalds goto out; 27821da177e4SLinus Torvalds 27831da177e4SLinus Torvalds len = strlen(link); 27841da177e4SLinus Torvalds if (len > (unsigned) buflen) 27851da177e4SLinus Torvalds len = buflen; 27861da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 27871da177e4SLinus Torvalds len = -EFAULT; 27881da177e4SLinus Torvalds out: 27891da177e4SLinus Torvalds return len; 27901da177e4SLinus Torvalds } 27911da177e4SLinus Torvalds 27921da177e4SLinus Torvalds /* 27931da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 27941da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 27951da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 27961da177e4SLinus Torvalds */ 27971da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 27981da177e4SLinus Torvalds { 27991da177e4SLinus Torvalds struct nameidata nd; 2800cc314eefSLinus Torvalds void *cookie; 2801694a1764SMarcin Slusarz int res; 2802cc314eefSLinus Torvalds 28031da177e4SLinus Torvalds nd.depth = 0; 2804cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 2805694a1764SMarcin Slusarz if (IS_ERR(cookie)) 2806694a1764SMarcin Slusarz return PTR_ERR(cookie); 2807694a1764SMarcin Slusarz 2808694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 28091da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 2810cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 2811694a1764SMarcin Slusarz return res; 28121da177e4SLinus Torvalds } 28131da177e4SLinus Torvalds 28141da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 28151da177e4SLinus Torvalds { 28161da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 28171da177e4SLinus Torvalds } 28181da177e4SLinus Torvalds 28191da177e4SLinus Torvalds /* get the link contents into pagecache */ 28201da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 28211da177e4SLinus Torvalds { 2822ebd09abbSDuane Griffin char *kaddr; 28231da177e4SLinus Torvalds struct page *page; 28241da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 2825090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 28261da177e4SLinus Torvalds if (IS_ERR(page)) 28276fe6900eSNick Piggin return (char*)page; 28281da177e4SLinus Torvalds *ppage = page; 2829ebd09abbSDuane Griffin kaddr = kmap(page); 2830ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 2831ebd09abbSDuane Griffin return kaddr; 28321da177e4SLinus Torvalds } 28331da177e4SLinus Torvalds 28341da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 28351da177e4SLinus Torvalds { 28361da177e4SLinus Torvalds struct page *page = NULL; 28371da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 28381da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 28391da177e4SLinus Torvalds if (page) { 28401da177e4SLinus Torvalds kunmap(page); 28411da177e4SLinus Torvalds page_cache_release(page); 28421da177e4SLinus Torvalds } 28431da177e4SLinus Torvalds return res; 28441da177e4SLinus Torvalds } 28451da177e4SLinus Torvalds 2846cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 28471da177e4SLinus Torvalds { 2848cc314eefSLinus Torvalds struct page *page = NULL; 28491da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 2850cc314eefSLinus Torvalds return page; 28511da177e4SLinus Torvalds } 28521da177e4SLinus Torvalds 2853cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 28541da177e4SLinus Torvalds { 2855cc314eefSLinus Torvalds struct page *page = cookie; 2856cc314eefSLinus Torvalds 2857cc314eefSLinus Torvalds if (page) { 28581da177e4SLinus Torvalds kunmap(page); 28591da177e4SLinus Torvalds page_cache_release(page); 28601da177e4SLinus Torvalds } 28611da177e4SLinus Torvalds } 28621da177e4SLinus Torvalds 286354566b2cSNick Piggin /* 286454566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 286554566b2cSNick Piggin */ 286654566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 28671da177e4SLinus Torvalds { 28681da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 28690adb25d2SKirill Korotaev struct page *page; 2870afddba49SNick Piggin void *fsdata; 2871beb497abSDmitriy Monakhov int err; 28721da177e4SLinus Torvalds char *kaddr; 287354566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 287454566b2cSNick Piggin if (nofs) 287554566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 28761da177e4SLinus Torvalds 28777e53cac4SNeilBrown retry: 2878afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 287954566b2cSNick Piggin flags, &page, &fsdata); 28801da177e4SLinus Torvalds if (err) 2881afddba49SNick Piggin goto fail; 2882afddba49SNick Piggin 28831da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 28841da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 28851da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 2886afddba49SNick Piggin 2887afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 2888afddba49SNick Piggin page, fsdata); 28891da177e4SLinus Torvalds if (err < 0) 28901da177e4SLinus Torvalds goto fail; 2891afddba49SNick Piggin if (err < len-1) 2892afddba49SNick Piggin goto retry; 2893afddba49SNick Piggin 28941da177e4SLinus Torvalds mark_inode_dirty(inode); 28951da177e4SLinus Torvalds return 0; 28961da177e4SLinus Torvalds fail: 28971da177e4SLinus Torvalds return err; 28981da177e4SLinus Torvalds } 28991da177e4SLinus Torvalds 29000adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 29010adb25d2SKirill Korotaev { 29020adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 290354566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 29040adb25d2SKirill Korotaev } 29050adb25d2SKirill Korotaev 290692e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 29071da177e4SLinus Torvalds .readlink = generic_readlink, 29081da177e4SLinus Torvalds .follow_link = page_follow_link_light, 29091da177e4SLinus Torvalds .put_link = page_put_link, 29101da177e4SLinus Torvalds }; 29111da177e4SLinus Torvalds 29122d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 29131da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 29141da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 29151da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 29161da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 29171da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 29181da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 29191da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 29201da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 29211da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 29220adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 29231da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 29241da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 29251da177e4SLinus Torvalds EXPORT_SYMBOL(path_lookup); 2926d1811465SAl Viro EXPORT_SYMBOL(kern_path); 292716f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 2928f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 29298c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission); 29301da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 29311da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 29321da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 29331da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 29341da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 29351da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 29361da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 29371da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 29381da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 29391da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 29401da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 29411da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 29421da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 29431da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 2944