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); 564cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, nd); 565cc314eefSLinus Torvalds error = PTR_ERR(cookie); 566cc314eefSLinus Torvalds if (!IS_ERR(cookie)) { 5671da177e4SLinus Torvalds char *s = nd_get_link(nd); 568cc314eefSLinus Torvalds error = 0; 5691da177e4SLinus Torvalds if (s) 5701da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 57139159de2SJeff Layton else if (nd->last_type == LAST_BIND) { 57239159de2SJeff Layton error = force_reval_path(&nd->path, nd); 57339159de2SJeff Layton if (error) 57439159de2SJeff Layton path_put(&nd->path); 57539159de2SJeff Layton } 5761da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 577cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, nd, cookie); 5781da177e4SLinus Torvalds } 5791da177e4SLinus Torvalds return error; 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds /* 5831da177e4SLinus Torvalds * This limits recursive symlink follows to 8, while 5841da177e4SLinus Torvalds * limiting consecutive symlinks to 40. 5851da177e4SLinus Torvalds * 5861da177e4SLinus Torvalds * Without that kind of total limit, nasty chains of consecutive 5871da177e4SLinus Torvalds * symlinks can cause almost arbitrarily long lookups. 5881da177e4SLinus Torvalds */ 58990ebe565SAl Viro static inline int do_follow_link(struct path *path, struct nameidata *nd) 5901da177e4SLinus Torvalds { 5911da177e4SLinus Torvalds int err = -ELOOP; 5921da177e4SLinus Torvalds if (current->link_count >= MAX_NESTED_LINKS) 5931da177e4SLinus Torvalds goto loop; 5941da177e4SLinus Torvalds if (current->total_link_count >= 40) 5951da177e4SLinus Torvalds goto loop; 5961da177e4SLinus Torvalds BUG_ON(nd->depth >= MAX_NESTED_LINKS); 5971da177e4SLinus Torvalds cond_resched(); 59890ebe565SAl Viro err = security_inode_follow_link(path->dentry, nd); 5991da177e4SLinus Torvalds if (err) 6001da177e4SLinus Torvalds goto loop; 6011da177e4SLinus Torvalds current->link_count++; 6021da177e4SLinus Torvalds current->total_link_count++; 6031da177e4SLinus Torvalds nd->depth++; 604cd4e91d3SAl Viro err = __do_follow_link(path, nd); 605258fa999SAl Viro path_put(path); 6061da177e4SLinus Torvalds current->link_count--; 6071da177e4SLinus Torvalds nd->depth--; 6081da177e4SLinus Torvalds return err; 6091da177e4SLinus Torvalds loop: 6101d957f9bSJan Blunck path_put_conditional(path, nd); 6111d957f9bSJan Blunck path_put(&nd->path); 6121da177e4SLinus Torvalds return err; 6131da177e4SLinus Torvalds } 6141da177e4SLinus Torvalds 615bab77ebfSAl Viro int follow_up(struct path *path) 6161da177e4SLinus Torvalds { 6171da177e4SLinus Torvalds struct vfsmount *parent; 6181da177e4SLinus Torvalds struct dentry *mountpoint; 6191da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 620bab77ebfSAl Viro parent = path->mnt->mnt_parent; 621bab77ebfSAl Viro if (parent == path->mnt) { 6221da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 6231da177e4SLinus Torvalds return 0; 6241da177e4SLinus Torvalds } 6251da177e4SLinus Torvalds mntget(parent); 626bab77ebfSAl Viro mountpoint = dget(path->mnt->mnt_mountpoint); 6271da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 628bab77ebfSAl Viro dput(path->dentry); 629bab77ebfSAl Viro path->dentry = mountpoint; 630bab77ebfSAl Viro mntput(path->mnt); 631bab77ebfSAl Viro path->mnt = parent; 6321da177e4SLinus Torvalds return 1; 6331da177e4SLinus Torvalds } 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds /* no need for dcache_lock, as serialization is taken care in 6361da177e4SLinus Torvalds * namespace.c 6371da177e4SLinus Torvalds */ 638463ffb2eSAl Viro static int __follow_mount(struct path *path) 639463ffb2eSAl Viro { 640463ffb2eSAl Viro int res = 0; 641463ffb2eSAl Viro while (d_mountpoint(path->dentry)) { 6421c755af4SAl Viro struct vfsmount *mounted = lookup_mnt(path); 643463ffb2eSAl Viro if (!mounted) 644463ffb2eSAl Viro break; 645463ffb2eSAl Viro dput(path->dentry); 646463ffb2eSAl Viro if (res) 647463ffb2eSAl Viro mntput(path->mnt); 648463ffb2eSAl Viro path->mnt = mounted; 649463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 650463ffb2eSAl Viro res = 1; 651463ffb2eSAl Viro } 652463ffb2eSAl Viro return res; 653463ffb2eSAl Viro } 654463ffb2eSAl Viro 65579ed0226SAl Viro static void follow_mount(struct path *path) 6561da177e4SLinus Torvalds { 65779ed0226SAl Viro while (d_mountpoint(path->dentry)) { 6581c755af4SAl Viro struct vfsmount *mounted = lookup_mnt(path); 6591da177e4SLinus Torvalds if (!mounted) 6601da177e4SLinus Torvalds break; 66179ed0226SAl Viro dput(path->dentry); 66279ed0226SAl Viro mntput(path->mnt); 66379ed0226SAl Viro path->mnt = mounted; 66479ed0226SAl Viro path->dentry = dget(mounted->mnt_root); 6651da177e4SLinus Torvalds } 6661da177e4SLinus Torvalds } 6671da177e4SLinus Torvalds 6681da177e4SLinus Torvalds /* no need for dcache_lock, as serialization is taken care in 6691da177e4SLinus Torvalds * namespace.c 6701da177e4SLinus Torvalds */ 6719393bd07SAl Viro int follow_down(struct path *path) 6721da177e4SLinus Torvalds { 6731da177e4SLinus Torvalds struct vfsmount *mounted; 6741da177e4SLinus Torvalds 6751c755af4SAl Viro mounted = lookup_mnt(path); 6761da177e4SLinus Torvalds if (mounted) { 6779393bd07SAl Viro dput(path->dentry); 6789393bd07SAl Viro mntput(path->mnt); 6799393bd07SAl Viro path->mnt = mounted; 6809393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 6811da177e4SLinus Torvalds return 1; 6821da177e4SLinus Torvalds } 6831da177e4SLinus Torvalds return 0; 6841da177e4SLinus Torvalds } 6851da177e4SLinus Torvalds 686f1662356SArjan van de Ven static __always_inline void follow_dotdot(struct nameidata *nd) 6871da177e4SLinus Torvalds { 6882a737871SAl Viro set_root(nd); 689e518ddb7SAndreas Mohr 6901da177e4SLinus Torvalds while(1) { 6911da177e4SLinus Torvalds struct vfsmount *parent; 6924ac91378SJan Blunck struct dentry *old = nd->path.dentry; 6931da177e4SLinus Torvalds 6942a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 6952a737871SAl Viro nd->path.mnt == nd->root.mnt) { 6961da177e4SLinus Torvalds break; 6971da177e4SLinus Torvalds } 6981da177e4SLinus Torvalds spin_lock(&dcache_lock); 6994ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 7004ac91378SJan Blunck nd->path.dentry = dget(nd->path.dentry->d_parent); 7011da177e4SLinus Torvalds spin_unlock(&dcache_lock); 7021da177e4SLinus Torvalds dput(old); 7031da177e4SLinus Torvalds break; 7041da177e4SLinus Torvalds } 7051da177e4SLinus Torvalds spin_unlock(&dcache_lock); 7061da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 7074ac91378SJan Blunck parent = nd->path.mnt->mnt_parent; 7084ac91378SJan Blunck if (parent == nd->path.mnt) { 7091da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 7101da177e4SLinus Torvalds break; 7111da177e4SLinus Torvalds } 7121da177e4SLinus Torvalds mntget(parent); 7134ac91378SJan Blunck nd->path.dentry = dget(nd->path.mnt->mnt_mountpoint); 7141da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 7151da177e4SLinus Torvalds dput(old); 7164ac91378SJan Blunck mntput(nd->path.mnt); 7174ac91378SJan Blunck nd->path.mnt = parent; 7181da177e4SLinus Torvalds } 71979ed0226SAl Viro follow_mount(&nd->path); 7201da177e4SLinus Torvalds } 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds /* 7231da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 7241da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 7251da177e4SLinus Torvalds * It _is_ time-critical. 7261da177e4SLinus Torvalds */ 7271da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name, 7281da177e4SLinus Torvalds struct path *path) 7291da177e4SLinus Torvalds { 7304ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 7316e6b1bd1SAl Viro struct dentry *dentry, *parent; 7326e6b1bd1SAl Viro struct inode *dir; 7333cac260aSAl Viro /* 7343cac260aSAl Viro * See if the low-level filesystem might want 7353cac260aSAl Viro * to use its own hash.. 7363cac260aSAl Viro */ 7373cac260aSAl Viro if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) { 7383cac260aSAl Viro int err = nd->path.dentry->d_op->d_hash(nd->path.dentry, name); 7393cac260aSAl Viro if (err < 0) 7403cac260aSAl Viro return err; 7413cac260aSAl Viro } 7421da177e4SLinus Torvalds 7433cac260aSAl Viro dentry = __d_lookup(nd->path.dentry, name); 7441da177e4SLinus Torvalds if (!dentry) 7451da177e4SLinus Torvalds goto need_lookup; 7461da177e4SLinus Torvalds if (dentry->d_op && dentry->d_op->d_revalidate) 7471da177e4SLinus Torvalds goto need_revalidate; 7481da177e4SLinus Torvalds done: 7491da177e4SLinus Torvalds path->mnt = mnt; 7501da177e4SLinus Torvalds path->dentry = dentry; 751634ee701SAl Viro __follow_mount(path); 7521da177e4SLinus Torvalds return 0; 7531da177e4SLinus Torvalds 7541da177e4SLinus Torvalds need_lookup: 7556e6b1bd1SAl Viro parent = nd->path.dentry; 7566e6b1bd1SAl Viro dir = parent->d_inode; 7576e6b1bd1SAl Viro 7586e6b1bd1SAl Viro mutex_lock(&dir->i_mutex); 7596e6b1bd1SAl Viro /* 7606e6b1bd1SAl Viro * First re-do the cached lookup just in case it was created 7616e6b1bd1SAl Viro * while we waited for the directory semaphore.. 7626e6b1bd1SAl Viro * 7636e6b1bd1SAl Viro * FIXME! This could use version numbering or similar to 7646e6b1bd1SAl Viro * avoid unnecessary cache lookups. 7656e6b1bd1SAl Viro * 7666e6b1bd1SAl Viro * The "dcache_lock" is purely to protect the RCU list walker 7676e6b1bd1SAl Viro * from concurrent renames at this point (we mustn't get false 7686e6b1bd1SAl Viro * negatives from the RCU list walk here, unlike the optimistic 7696e6b1bd1SAl Viro * fast walk). 7706e6b1bd1SAl Viro * 7716e6b1bd1SAl Viro * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup 7726e6b1bd1SAl Viro */ 7736e6b1bd1SAl Viro dentry = d_lookup(parent, name); 7746e6b1bd1SAl Viro if (!dentry) { 7756e6b1bd1SAl Viro struct dentry *new; 7766e6b1bd1SAl Viro 7776e6b1bd1SAl Viro /* Don't create child dentry for a dead directory. */ 7786e6b1bd1SAl Viro dentry = ERR_PTR(-ENOENT); 7796e6b1bd1SAl Viro if (IS_DEADDIR(dir)) 7806e6b1bd1SAl Viro goto out_unlock; 7816e6b1bd1SAl Viro 7826e6b1bd1SAl Viro new = d_alloc(parent, name); 7836e6b1bd1SAl Viro dentry = ERR_PTR(-ENOMEM); 7846e6b1bd1SAl Viro if (new) { 7856e6b1bd1SAl Viro dentry = dir->i_op->lookup(dir, new, nd); 7866e6b1bd1SAl Viro if (dentry) 7876e6b1bd1SAl Viro dput(new); 7886e6b1bd1SAl Viro else 7896e6b1bd1SAl Viro dentry = new; 7906e6b1bd1SAl Viro } 7916e6b1bd1SAl Viro out_unlock: 7926e6b1bd1SAl Viro mutex_unlock(&dir->i_mutex); 7936e6b1bd1SAl Viro if (IS_ERR(dentry)) 7946e6b1bd1SAl Viro goto fail; 7956e6b1bd1SAl Viro goto done; 7966e6b1bd1SAl Viro } 7976e6b1bd1SAl Viro 7986e6b1bd1SAl Viro /* 7996e6b1bd1SAl Viro * Uhhuh! Nasty case: the cache was re-populated while 8006e6b1bd1SAl Viro * we waited on the semaphore. Need to revalidate. 8016e6b1bd1SAl Viro */ 8026e6b1bd1SAl Viro mutex_unlock(&dir->i_mutex); 8036e6b1bd1SAl Viro if (dentry->d_op && dentry->d_op->d_revalidate) { 8046e6b1bd1SAl Viro dentry = do_revalidate(dentry, nd); 8056e6b1bd1SAl Viro if (!dentry) 8066e6b1bd1SAl Viro dentry = ERR_PTR(-ENOENT); 8076e6b1bd1SAl Viro } 8081da177e4SLinus Torvalds if (IS_ERR(dentry)) 8091da177e4SLinus Torvalds goto fail; 8101da177e4SLinus Torvalds goto done; 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds need_revalidate: 813bcdc5e01SIan Kent dentry = do_revalidate(dentry, nd); 814bcdc5e01SIan Kent if (!dentry) 8151da177e4SLinus Torvalds goto need_lookup; 816bcdc5e01SIan Kent if (IS_ERR(dentry)) 817bcdc5e01SIan Kent goto fail; 818bcdc5e01SIan Kent goto done; 8191da177e4SLinus Torvalds 8201da177e4SLinus Torvalds fail: 8211da177e4SLinus Torvalds return PTR_ERR(dentry); 8221da177e4SLinus Torvalds } 8231da177e4SLinus Torvalds 8241da177e4SLinus Torvalds /* 8251da177e4SLinus Torvalds * Name resolution. 826ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 827ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 8281da177e4SLinus Torvalds * 829ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 830ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 8311da177e4SLinus Torvalds */ 8326de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 8331da177e4SLinus Torvalds { 8341da177e4SLinus Torvalds struct path next; 8351da177e4SLinus Torvalds struct inode *inode; 8361da177e4SLinus Torvalds int err; 8371da177e4SLinus Torvalds unsigned int lookup_flags = nd->flags; 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds while (*name=='/') 8401da177e4SLinus Torvalds name++; 8411da177e4SLinus Torvalds if (!*name) 8421da177e4SLinus Torvalds goto return_reval; 8431da177e4SLinus Torvalds 8444ac91378SJan Blunck inode = nd->path.dentry->d_inode; 8451da177e4SLinus Torvalds if (nd->depth) 846f55eab82STrond Myklebust lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE); 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 8491da177e4SLinus Torvalds for(;;) { 8501da177e4SLinus Torvalds unsigned long hash; 8511da177e4SLinus Torvalds struct qstr this; 8521da177e4SLinus Torvalds unsigned int c; 8531da177e4SLinus Torvalds 854cdce5d6bSTrond Myklebust nd->flags |= LOOKUP_CONTINUE; 855b75b5086SAl Viro err = exec_permission(inode); 8561da177e4SLinus Torvalds if (err) 8571da177e4SLinus Torvalds break; 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds this.name = name; 8601da177e4SLinus Torvalds c = *(const unsigned char *)name; 8611da177e4SLinus Torvalds 8621da177e4SLinus Torvalds hash = init_name_hash(); 8631da177e4SLinus Torvalds do { 8641da177e4SLinus Torvalds name++; 8651da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 8661da177e4SLinus Torvalds c = *(const unsigned char *)name; 8671da177e4SLinus Torvalds } while (c && (c != '/')); 8681da177e4SLinus Torvalds this.len = name - (const char *) this.name; 8691da177e4SLinus Torvalds this.hash = end_name_hash(hash); 8701da177e4SLinus Torvalds 8711da177e4SLinus Torvalds /* remove trailing slashes? */ 8721da177e4SLinus Torvalds if (!c) 8731da177e4SLinus Torvalds goto last_component; 8741da177e4SLinus Torvalds while (*++name == '/'); 8751da177e4SLinus Torvalds if (!*name) 8761da177e4SLinus Torvalds goto last_with_slashes; 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds /* 8791da177e4SLinus Torvalds * "." and ".." are special - ".." especially so because it has 8801da177e4SLinus Torvalds * to be able to know about the current root directory and 8811da177e4SLinus Torvalds * parent relationships. 8821da177e4SLinus Torvalds */ 8831da177e4SLinus Torvalds if (this.name[0] == '.') switch (this.len) { 8841da177e4SLinus Torvalds default: 8851da177e4SLinus Torvalds break; 8861da177e4SLinus Torvalds case 2: 8871da177e4SLinus Torvalds if (this.name[1] != '.') 8881da177e4SLinus Torvalds break; 88958c465ebSAl Viro follow_dotdot(nd); 8904ac91378SJan Blunck inode = nd->path.dentry->d_inode; 8911da177e4SLinus Torvalds /* fallthrough */ 8921da177e4SLinus Torvalds case 1: 8931da177e4SLinus Torvalds continue; 8941da177e4SLinus Torvalds } 8951da177e4SLinus Torvalds /* This does the actual lookups.. */ 8961da177e4SLinus Torvalds err = do_lookup(nd, &this, &next); 8971da177e4SLinus Torvalds if (err) 8981da177e4SLinus Torvalds break; 8991da177e4SLinus Torvalds 9001da177e4SLinus Torvalds err = -ENOENT; 9011da177e4SLinus Torvalds inode = next.dentry->d_inode; 9021da177e4SLinus Torvalds if (!inode) 9031da177e4SLinus Torvalds goto out_dput; 9041da177e4SLinus Torvalds 9051da177e4SLinus Torvalds if (inode->i_op->follow_link) { 90690ebe565SAl Viro err = do_follow_link(&next, nd); 9071da177e4SLinus Torvalds if (err) 9081da177e4SLinus Torvalds goto return_err; 9091da177e4SLinus Torvalds err = -ENOENT; 9104ac91378SJan Blunck inode = nd->path.dentry->d_inode; 9111da177e4SLinus Torvalds if (!inode) 9121da177e4SLinus Torvalds break; 91309dd17d3SMiklos Szeredi } else 91409dd17d3SMiklos Szeredi path_to_nameidata(&next, nd); 9151da177e4SLinus Torvalds err = -ENOTDIR; 9161da177e4SLinus Torvalds if (!inode->i_op->lookup) 9171da177e4SLinus Torvalds break; 9181da177e4SLinus Torvalds continue; 9191da177e4SLinus Torvalds /* here ends the main loop */ 9201da177e4SLinus Torvalds 9211da177e4SLinus Torvalds last_with_slashes: 9221da177e4SLinus Torvalds lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 9231da177e4SLinus Torvalds last_component: 924f55eab82STrond Myklebust /* Clear LOOKUP_CONTINUE iff it was previously unset */ 925f55eab82STrond Myklebust nd->flags &= lookup_flags | ~LOOKUP_CONTINUE; 9261da177e4SLinus Torvalds if (lookup_flags & LOOKUP_PARENT) 9271da177e4SLinus Torvalds goto lookup_parent; 9281da177e4SLinus Torvalds if (this.name[0] == '.') switch (this.len) { 9291da177e4SLinus Torvalds default: 9301da177e4SLinus Torvalds break; 9311da177e4SLinus Torvalds case 2: 9321da177e4SLinus Torvalds if (this.name[1] != '.') 9331da177e4SLinus Torvalds break; 93458c465ebSAl Viro follow_dotdot(nd); 9354ac91378SJan Blunck inode = nd->path.dentry->d_inode; 9361da177e4SLinus Torvalds /* fallthrough */ 9371da177e4SLinus Torvalds case 1: 9381da177e4SLinus Torvalds goto return_reval; 9391da177e4SLinus Torvalds } 9401da177e4SLinus Torvalds err = do_lookup(nd, &this, &next); 9411da177e4SLinus Torvalds if (err) 9421da177e4SLinus Torvalds break; 9431da177e4SLinus Torvalds inode = next.dentry->d_inode; 9441da177e4SLinus Torvalds if ((lookup_flags & LOOKUP_FOLLOW) 945acfa4380SAl Viro && inode && inode->i_op->follow_link) { 94690ebe565SAl Viro err = do_follow_link(&next, nd); 9471da177e4SLinus Torvalds if (err) 9481da177e4SLinus Torvalds goto return_err; 9494ac91378SJan Blunck inode = nd->path.dentry->d_inode; 95009dd17d3SMiklos Szeredi } else 95109dd17d3SMiklos Szeredi path_to_nameidata(&next, nd); 9521da177e4SLinus Torvalds err = -ENOENT; 9531da177e4SLinus Torvalds if (!inode) 9541da177e4SLinus Torvalds break; 9551da177e4SLinus Torvalds if (lookup_flags & LOOKUP_DIRECTORY) { 9561da177e4SLinus Torvalds err = -ENOTDIR; 957acfa4380SAl Viro if (!inode->i_op->lookup) 9581da177e4SLinus Torvalds break; 9591da177e4SLinus Torvalds } 9601da177e4SLinus Torvalds goto return_base; 9611da177e4SLinus Torvalds lookup_parent: 9621da177e4SLinus Torvalds nd->last = this; 9631da177e4SLinus Torvalds nd->last_type = LAST_NORM; 9641da177e4SLinus Torvalds if (this.name[0] != '.') 9651da177e4SLinus Torvalds goto return_base; 9661da177e4SLinus Torvalds if (this.len == 1) 9671da177e4SLinus Torvalds nd->last_type = LAST_DOT; 9681da177e4SLinus Torvalds else if (this.len == 2 && this.name[1] == '.') 9691da177e4SLinus Torvalds nd->last_type = LAST_DOTDOT; 9701da177e4SLinus Torvalds else 9711da177e4SLinus Torvalds goto return_base; 9721da177e4SLinus Torvalds return_reval: 9731da177e4SLinus Torvalds /* 9741da177e4SLinus Torvalds * We bypassed the ordinary revalidation routines. 9751da177e4SLinus Torvalds * We may need to check the cached dentry for staleness. 9761da177e4SLinus Torvalds */ 9774ac91378SJan Blunck if (nd->path.dentry && nd->path.dentry->d_sb && 9784ac91378SJan Blunck (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) { 9791da177e4SLinus Torvalds err = -ESTALE; 9801da177e4SLinus Torvalds /* Note: we do not d_invalidate() */ 9814ac91378SJan Blunck if (!nd->path.dentry->d_op->d_revalidate( 9824ac91378SJan Blunck nd->path.dentry, nd)) 9831da177e4SLinus Torvalds break; 9841da177e4SLinus Torvalds } 9851da177e4SLinus Torvalds return_base: 9861da177e4SLinus Torvalds return 0; 9871da177e4SLinus Torvalds out_dput: 9881d957f9bSJan Blunck path_put_conditional(&next, nd); 9891da177e4SLinus Torvalds break; 9901da177e4SLinus Torvalds } 9911d957f9bSJan Blunck path_put(&nd->path); 9921da177e4SLinus Torvalds return_err: 9931da177e4SLinus Torvalds return err; 9941da177e4SLinus Torvalds } 9951da177e4SLinus Torvalds 996fc9b52cdSHarvey Harrison static int path_walk(const char *name, struct nameidata *nd) 9971da177e4SLinus Torvalds { 9986de88d72SAl Viro struct path save = nd->path; 9996de88d72SAl Viro int result; 10006de88d72SAl Viro 10011da177e4SLinus Torvalds current->total_link_count = 0; 10026de88d72SAl Viro 10036de88d72SAl Viro /* make sure the stuff we saved doesn't go away */ 10046de88d72SAl Viro path_get(&save); 10056de88d72SAl Viro 10066de88d72SAl Viro result = link_path_walk(name, nd); 10076de88d72SAl Viro if (result == -ESTALE) { 10086de88d72SAl Viro /* nd->path had been dropped */ 10096de88d72SAl Viro current->total_link_count = 0; 10106de88d72SAl Viro nd->path = save; 10116de88d72SAl Viro path_get(&nd->path); 10126de88d72SAl Viro nd->flags |= LOOKUP_REVAL; 10136de88d72SAl Viro result = link_path_walk(name, nd); 10146de88d72SAl Viro } 10156de88d72SAl Viro 10166de88d72SAl Viro path_put(&save); 10176de88d72SAl Viro 10186de88d72SAl Viro return result; 10191da177e4SLinus Torvalds } 10201da177e4SLinus Torvalds 10219b4a9b14SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd) 10221da177e4SLinus Torvalds { 1023ea3834d9SPrasanna Meda int retval = 0; 1024170aa3d0SUlrich Drepper int fput_needed; 1025170aa3d0SUlrich Drepper struct file *file; 10261da177e4SLinus Torvalds 10271da177e4SLinus Torvalds nd->last_type = LAST_ROOT; /* if there are only slashes... */ 10281da177e4SLinus Torvalds nd->flags = flags; 10291da177e4SLinus Torvalds nd->depth = 0; 10302a737871SAl Viro nd->root.mnt = NULL; 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds if (*name=='/') { 10332a737871SAl Viro set_root(nd); 10342a737871SAl Viro nd->path = nd->root; 10352a737871SAl Viro path_get(&nd->root); 10365590ff0dSUlrich Drepper } else if (dfd == AT_FDCWD) { 10372a737871SAl Viro struct fs_struct *fs = current->fs; 1038e518ddb7SAndreas Mohr read_lock(&fs->lock); 10396ac08c39SJan Blunck nd->path = fs->pwd; 10406ac08c39SJan Blunck path_get(&fs->pwd); 1041e518ddb7SAndreas Mohr read_unlock(&fs->lock); 10425590ff0dSUlrich Drepper } else { 10435590ff0dSUlrich Drepper struct dentry *dentry; 10445590ff0dSUlrich Drepper 10455590ff0dSUlrich Drepper file = fget_light(dfd, &fput_needed); 10465590ff0dSUlrich Drepper retval = -EBADF; 1047170aa3d0SUlrich Drepper if (!file) 10486d09bb62STrond Myklebust goto out_fail; 10495590ff0dSUlrich Drepper 10500f7fc9e4SJosef "Jeff" Sipek dentry = file->f_path.dentry; 10515590ff0dSUlrich Drepper 10525590ff0dSUlrich Drepper retval = -ENOTDIR; 1053170aa3d0SUlrich Drepper if (!S_ISDIR(dentry->d_inode->i_mode)) 10546d09bb62STrond Myklebust goto fput_fail; 10555590ff0dSUlrich Drepper 10565590ff0dSUlrich Drepper retval = file_permission(file, MAY_EXEC); 1057170aa3d0SUlrich Drepper if (retval) 10586d09bb62STrond Myklebust goto fput_fail; 10595590ff0dSUlrich Drepper 10605dd784d0SJan Blunck nd->path = file->f_path; 10615dd784d0SJan Blunck path_get(&file->f_path); 10625590ff0dSUlrich Drepper 10635590ff0dSUlrich Drepper fput_light(file, fput_needed); 10641da177e4SLinus Torvalds } 10659b4a9b14SAl Viro return 0; 10662dfdd266SJosef 'Jeff' Sipek 10679b4a9b14SAl Viro fput_fail: 10689b4a9b14SAl Viro fput_light(file, fput_needed); 10699b4a9b14SAl Viro out_fail: 10709b4a9b14SAl Viro return retval; 10719b4a9b14SAl Viro } 10729b4a9b14SAl Viro 10739b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 10749b4a9b14SAl Viro static int do_path_lookup(int dfd, const char *name, 10759b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 10769b4a9b14SAl Viro { 10779b4a9b14SAl Viro int retval = path_init(dfd, name, flags, nd); 10789b4a9b14SAl Viro if (!retval) 10792dfdd266SJosef 'Jeff' Sipek retval = path_walk(name, nd); 10804ac91378SJan Blunck if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry && 10814ac91378SJan Blunck nd->path.dentry->d_inode)) 10824ac91378SJan Blunck audit_inode(name, nd->path.dentry); 10832a737871SAl Viro if (nd->root.mnt) { 10842a737871SAl Viro path_put(&nd->root); 10852a737871SAl Viro nd->root.mnt = NULL; 10862a737871SAl Viro } 1087170aa3d0SUlrich Drepper return retval; 10881da177e4SLinus Torvalds } 10891da177e4SLinus Torvalds 1090fc9b52cdSHarvey Harrison int path_lookup(const char *name, unsigned int flags, 10915590ff0dSUlrich Drepper struct nameidata *nd) 10925590ff0dSUlrich Drepper { 10935590ff0dSUlrich Drepper return do_path_lookup(AT_FDCWD, name, flags, nd); 10945590ff0dSUlrich Drepper } 10955590ff0dSUlrich Drepper 1096d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1097d1811465SAl Viro { 1098d1811465SAl Viro struct nameidata nd; 1099d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1100d1811465SAl Viro if (!res) 1101d1811465SAl Viro *path = nd.path; 1102d1811465SAl Viro return res; 1103d1811465SAl Viro } 1104d1811465SAl Viro 110516f18200SJosef 'Jeff' Sipek /** 110616f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 110716f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 110816f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 110916f18200SJosef 'Jeff' Sipek * @name: pointer to file name 111016f18200SJosef 'Jeff' Sipek * @flags: lookup flags 111116f18200SJosef 'Jeff' Sipek * @nd: pointer to nameidata 111216f18200SJosef 'Jeff' Sipek */ 111316f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 111416f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 111516f18200SJosef 'Jeff' Sipek struct nameidata *nd) 111616f18200SJosef 'Jeff' Sipek { 111716f18200SJosef 'Jeff' Sipek int retval; 111816f18200SJosef 'Jeff' Sipek 111916f18200SJosef 'Jeff' Sipek /* same as do_path_lookup */ 112016f18200SJosef 'Jeff' Sipek nd->last_type = LAST_ROOT; 112116f18200SJosef 'Jeff' Sipek nd->flags = flags; 112216f18200SJosef 'Jeff' Sipek nd->depth = 0; 112316f18200SJosef 'Jeff' Sipek 1124c8e7f449SJan Blunck nd->path.dentry = dentry; 1125c8e7f449SJan Blunck nd->path.mnt = mnt; 1126c8e7f449SJan Blunck path_get(&nd->path); 11275b857119SAl Viro nd->root = nd->path; 11285b857119SAl Viro path_get(&nd->root); 112916f18200SJosef 'Jeff' Sipek 113016f18200SJosef 'Jeff' Sipek retval = path_walk(name, nd); 11314ac91378SJan Blunck if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry && 11324ac91378SJan Blunck nd->path.dentry->d_inode)) 11334ac91378SJan Blunck audit_inode(name, nd->path.dentry); 113416f18200SJosef 'Jeff' Sipek 11352a737871SAl Viro path_put(&nd->root); 11362a737871SAl Viro nd->root.mnt = NULL; 113716f18200SJosef 'Jeff' Sipek 11382a737871SAl Viro return retval; 113916f18200SJosef 'Jeff' Sipek } 114016f18200SJosef 'Jeff' Sipek 1141eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name, 1142eead1911SChristoph Hellwig struct dentry *base, struct nameidata *nd) 11431da177e4SLinus Torvalds { 11441da177e4SLinus Torvalds struct dentry *dentry; 11451da177e4SLinus Torvalds struct inode *inode; 11461da177e4SLinus Torvalds int err; 11471da177e4SLinus Torvalds 11481da177e4SLinus Torvalds inode = base->d_inode; 11491da177e4SLinus Torvalds 11501da177e4SLinus Torvalds /* 11511da177e4SLinus Torvalds * See if the low-level filesystem might want 11521da177e4SLinus Torvalds * to use its own hash.. 11531da177e4SLinus Torvalds */ 11541da177e4SLinus Torvalds if (base->d_op && base->d_op->d_hash) { 11551da177e4SLinus Torvalds err = base->d_op->d_hash(base, name); 11561da177e4SLinus Torvalds dentry = ERR_PTR(err); 11571da177e4SLinus Torvalds if (err < 0) 11581da177e4SLinus Torvalds goto out; 11591da177e4SLinus Torvalds } 11601da177e4SLinus Torvalds 11616e6b1bd1SAl Viro dentry = __d_lookup(base, name); 11626e6b1bd1SAl Viro 11636e6b1bd1SAl Viro /* lockess __d_lookup may fail due to concurrent d_move() 11646e6b1bd1SAl Viro * in some unrelated directory, so try with d_lookup 11656e6b1bd1SAl Viro */ 11666e6b1bd1SAl Viro if (!dentry) 11676e6b1bd1SAl Viro dentry = d_lookup(base, name); 11686e6b1bd1SAl Viro 11696e6b1bd1SAl Viro if (dentry && dentry->d_op && dentry->d_op->d_revalidate) 11706e6b1bd1SAl Viro dentry = do_revalidate(dentry, nd); 11716e6b1bd1SAl Viro 11721da177e4SLinus Torvalds if (!dentry) { 1173d70b67c8SMiklos Szeredi struct dentry *new; 1174d70b67c8SMiklos Szeredi 1175d70b67c8SMiklos Szeredi /* Don't create child dentry for a dead directory. */ 1176d70b67c8SMiklos Szeredi dentry = ERR_PTR(-ENOENT); 1177d70b67c8SMiklos Szeredi if (IS_DEADDIR(inode)) 1178d70b67c8SMiklos Szeredi goto out; 1179d70b67c8SMiklos Szeredi 1180d70b67c8SMiklos Szeredi new = d_alloc(base, name); 11811da177e4SLinus Torvalds dentry = ERR_PTR(-ENOMEM); 11821da177e4SLinus Torvalds if (!new) 11831da177e4SLinus Torvalds goto out; 11841da177e4SLinus Torvalds dentry = inode->i_op->lookup(inode, new, nd); 11851da177e4SLinus Torvalds if (!dentry) 11861da177e4SLinus Torvalds dentry = new; 11871da177e4SLinus Torvalds else 11881da177e4SLinus Torvalds dput(new); 11891da177e4SLinus Torvalds } 11901da177e4SLinus Torvalds out: 11911da177e4SLinus Torvalds return dentry; 11921da177e4SLinus Torvalds } 11931da177e4SLinus Torvalds 1194057f6c01SJames Morris /* 1195057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1196057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1197057f6c01SJames Morris * SMP-safe. 1198057f6c01SJames Morris */ 1199a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 12001da177e4SLinus Torvalds { 1201eead1911SChristoph Hellwig int err; 1202eead1911SChristoph Hellwig 1203b75b5086SAl Viro err = exec_permission(nd->path.dentry->d_inode); 1204eead1911SChristoph Hellwig if (err) 1205eead1911SChristoph Hellwig return ERR_PTR(err); 12064ac91378SJan Blunck return __lookup_hash(&nd->last, nd->path.dentry, nd); 12071da177e4SLinus Torvalds } 12081da177e4SLinus Torvalds 1209eead1911SChristoph Hellwig static int __lookup_one_len(const char *name, struct qstr *this, 1210eead1911SChristoph Hellwig struct dentry *base, int len) 12111da177e4SLinus Torvalds { 12121da177e4SLinus Torvalds unsigned long hash; 12131da177e4SLinus Torvalds unsigned int c; 12141da177e4SLinus Torvalds 1215057f6c01SJames Morris this->name = name; 1216057f6c01SJames Morris this->len = len; 12171da177e4SLinus Torvalds if (!len) 1218057f6c01SJames Morris return -EACCES; 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds hash = init_name_hash(); 12211da177e4SLinus Torvalds while (len--) { 12221da177e4SLinus Torvalds c = *(const unsigned char *)name++; 12231da177e4SLinus Torvalds if (c == '/' || c == '\0') 1224057f6c01SJames Morris return -EACCES; 12251da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 12261da177e4SLinus Torvalds } 1227057f6c01SJames Morris this->hash = end_name_hash(hash); 1228057f6c01SJames Morris return 0; 1229057f6c01SJames Morris } 12301da177e4SLinus Torvalds 1231eead1911SChristoph Hellwig /** 1232a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1233eead1911SChristoph Hellwig * @name: pathname component to lookup 1234eead1911SChristoph Hellwig * @base: base directory to lookup from 1235eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1236eead1911SChristoph Hellwig * 1237a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1238a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1239eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1240eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1241eead1911SChristoph Hellwig */ 1242057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1243057f6c01SJames Morris { 1244057f6c01SJames Morris int err; 1245057f6c01SJames Morris struct qstr this; 1246057f6c01SJames Morris 12472f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 12482f9092e1SDavid Woodhouse 1249057f6c01SJames Morris err = __lookup_one_len(name, &this, base, len); 1250057f6c01SJames Morris if (err) 1251057f6c01SJames Morris return ERR_PTR(err); 1252eead1911SChristoph Hellwig 1253b75b5086SAl Viro err = exec_permission(base->d_inode); 1254eead1911SChristoph Hellwig if (err) 1255eead1911SChristoph Hellwig return ERR_PTR(err); 125649705b77SChristoph Hellwig return __lookup_hash(&this, base, NULL); 1257057f6c01SJames Morris } 1258057f6c01SJames Morris 12592d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags, 12602d8f3038SAl Viro struct path *path) 12611da177e4SLinus Torvalds { 12622d8f3038SAl Viro struct nameidata nd; 12631da177e4SLinus Torvalds char *tmp = getname(name); 12641da177e4SLinus Torvalds int err = PTR_ERR(tmp); 12651da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 12662d8f3038SAl Viro 12672d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 12682d8f3038SAl Viro 12692d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 12701da177e4SLinus Torvalds putname(tmp); 12712d8f3038SAl Viro if (!err) 12722d8f3038SAl Viro *path = nd.path; 12731da177e4SLinus Torvalds } 12741da177e4SLinus Torvalds return err; 12751da177e4SLinus Torvalds } 12761da177e4SLinus Torvalds 12772ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 12782ad94ae6SAl Viro struct nameidata *nd, char **name) 12792ad94ae6SAl Viro { 12802ad94ae6SAl Viro char *s = getname(path); 12812ad94ae6SAl Viro int error; 12822ad94ae6SAl Viro 12832ad94ae6SAl Viro if (IS_ERR(s)) 12842ad94ae6SAl Viro return PTR_ERR(s); 12852ad94ae6SAl Viro 12862ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 12872ad94ae6SAl Viro if (error) 12882ad94ae6SAl Viro putname(s); 12892ad94ae6SAl Viro else 12902ad94ae6SAl Viro *name = s; 12912ad94ae6SAl Viro 12922ad94ae6SAl Viro return error; 12932ad94ae6SAl Viro } 12942ad94ae6SAl Viro 12951da177e4SLinus Torvalds /* 12961da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 12971da177e4SLinus Torvalds * minimal. 12981da177e4SLinus Torvalds */ 12991da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 13001da177e4SLinus Torvalds { 1301da9592edSDavid Howells uid_t fsuid = current_fsuid(); 1302da9592edSDavid Howells 13031da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 13041da177e4SLinus Torvalds return 0; 1305da9592edSDavid Howells if (inode->i_uid == fsuid) 13061da177e4SLinus Torvalds return 0; 1307da9592edSDavid Howells if (dir->i_uid == fsuid) 13081da177e4SLinus Torvalds return 0; 13091da177e4SLinus Torvalds return !capable(CAP_FOWNER); 13101da177e4SLinus Torvalds } 13111da177e4SLinus Torvalds 13121da177e4SLinus Torvalds /* 13131da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 13141da177e4SLinus Torvalds * whether the type of victim is right. 13151da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 13161da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 13171da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 13181da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 13191da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 13201da177e4SLinus Torvalds * a. be owner of dir, or 13211da177e4SLinus Torvalds * b. be owner of victim, or 13221da177e4SLinus Torvalds * c. have CAP_FOWNER capability 13231da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 13241da177e4SLinus Torvalds * links pointing to it. 13251da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 13261da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 13271da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 13281da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 13291da177e4SLinus Torvalds * nfs_async_unlink(). 13301da177e4SLinus Torvalds */ 1331858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 13321da177e4SLinus Torvalds { 13331da177e4SLinus Torvalds int error; 13341da177e4SLinus Torvalds 13351da177e4SLinus Torvalds if (!victim->d_inode) 13361da177e4SLinus Torvalds return -ENOENT; 13371da177e4SLinus Torvalds 13381da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 13395a190ae6SAl Viro audit_inode_child(victim->d_name.name, victim, dir); 13401da177e4SLinus Torvalds 1341f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 13421da177e4SLinus Torvalds if (error) 13431da177e4SLinus Torvalds return error; 13441da177e4SLinus Torvalds if (IS_APPEND(dir)) 13451da177e4SLinus Torvalds return -EPERM; 13461da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 1347f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 13481da177e4SLinus Torvalds return -EPERM; 13491da177e4SLinus Torvalds if (isdir) { 13501da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 13511da177e4SLinus Torvalds return -ENOTDIR; 13521da177e4SLinus Torvalds if (IS_ROOT(victim)) 13531da177e4SLinus Torvalds return -EBUSY; 13541da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 13551da177e4SLinus Torvalds return -EISDIR; 13561da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 13571da177e4SLinus Torvalds return -ENOENT; 13581da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 13591da177e4SLinus Torvalds return -EBUSY; 13601da177e4SLinus Torvalds return 0; 13611da177e4SLinus Torvalds } 13621da177e4SLinus Torvalds 13631da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 13641da177e4SLinus Torvalds * dir. 13651da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 13661da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 13671da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 13681da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 13691da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 13701da177e4SLinus Torvalds */ 1371a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 13721da177e4SLinus Torvalds { 13731da177e4SLinus Torvalds if (child->d_inode) 13741da177e4SLinus Torvalds return -EEXIST; 13751da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 13761da177e4SLinus Torvalds return -ENOENT; 1377f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 13781da177e4SLinus Torvalds } 13791da177e4SLinus Torvalds 13801da177e4SLinus Torvalds /* 13811da177e4SLinus Torvalds * O_DIRECTORY translates into forcing a directory lookup. 13821da177e4SLinus Torvalds */ 13831da177e4SLinus Torvalds static inline int lookup_flags(unsigned int f) 13841da177e4SLinus Torvalds { 13851da177e4SLinus Torvalds unsigned long retval = LOOKUP_FOLLOW; 13861da177e4SLinus Torvalds 13871da177e4SLinus Torvalds if (f & O_NOFOLLOW) 13881da177e4SLinus Torvalds retval &= ~LOOKUP_FOLLOW; 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds if (f & O_DIRECTORY) 13911da177e4SLinus Torvalds retval |= LOOKUP_DIRECTORY; 13921da177e4SLinus Torvalds 13931da177e4SLinus Torvalds return retval; 13941da177e4SLinus Torvalds } 13951da177e4SLinus Torvalds 13961da177e4SLinus Torvalds /* 13971da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 13981da177e4SLinus Torvalds */ 13991da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 14001da177e4SLinus Torvalds { 14011da177e4SLinus Torvalds struct dentry *p; 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds if (p1 == p2) { 1404f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 14051da177e4SLinus Torvalds return NULL; 14061da177e4SLinus Torvalds } 14071da177e4SLinus Torvalds 1408a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 14091da177e4SLinus Torvalds 1410e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 1411e2761a11SOGAWA Hirofumi if (p) { 1412f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 1413f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 14141da177e4SLinus Torvalds return p; 14151da177e4SLinus Torvalds } 14161da177e4SLinus Torvalds 1417e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 1418e2761a11SOGAWA Hirofumi if (p) { 1419f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1420f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 14211da177e4SLinus Torvalds return p; 14221da177e4SLinus Torvalds } 14231da177e4SLinus Torvalds 1424f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1425f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 14261da177e4SLinus Torvalds return NULL; 14271da177e4SLinus Torvalds } 14281da177e4SLinus Torvalds 14291da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 14301da177e4SLinus Torvalds { 14311b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 14321da177e4SLinus Torvalds if (p1 != p2) { 14331b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 1434a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 14351da177e4SLinus Torvalds } 14361da177e4SLinus Torvalds } 14371da177e4SLinus Torvalds 14381da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode, 14391da177e4SLinus Torvalds struct nameidata *nd) 14401da177e4SLinus Torvalds { 1441a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds if (error) 14441da177e4SLinus Torvalds return error; 14451da177e4SLinus Torvalds 1446acfa4380SAl Viro if (!dir->i_op->create) 14471da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 14481da177e4SLinus Torvalds mode &= S_IALLUGO; 14491da177e4SLinus Torvalds mode |= S_IFREG; 14501da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 14511da177e4SLinus Torvalds if (error) 14521da177e4SLinus Torvalds return error; 14539e3509e2SJan Kara vfs_dq_init(dir); 14541da177e4SLinus Torvalds error = dir->i_op->create(dir, dentry, mode, nd); 1455a74574aaSStephen Smalley if (!error) 1456f38aa942SAmy Griffis fsnotify_create(dir, dentry); 14571da177e4SLinus Torvalds return error; 14581da177e4SLinus Torvalds } 14591da177e4SLinus Torvalds 14603fb64190SChristoph Hellwig int may_open(struct path *path, int acc_mode, int flag) 14611da177e4SLinus Torvalds { 14623fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 14631da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 14641da177e4SLinus Torvalds int error; 14651da177e4SLinus Torvalds 14661da177e4SLinus Torvalds if (!inode) 14671da177e4SLinus Torvalds return -ENOENT; 14681da177e4SLinus Torvalds 1469c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 1470c8fe8f30SChristoph Hellwig case S_IFLNK: 14711da177e4SLinus Torvalds return -ELOOP; 1472c8fe8f30SChristoph Hellwig case S_IFDIR: 1473c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 14741da177e4SLinus Torvalds return -EISDIR; 1475c8fe8f30SChristoph Hellwig break; 1476c8fe8f30SChristoph Hellwig case S_IFBLK: 1477c8fe8f30SChristoph Hellwig case S_IFCHR: 14783fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 14791da177e4SLinus Torvalds return -EACCES; 1480c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 1481c8fe8f30SChristoph Hellwig case S_IFIFO: 1482c8fe8f30SChristoph Hellwig case S_IFSOCK: 14831da177e4SLinus Torvalds flag &= ~O_TRUNC; 1484c8fe8f30SChristoph Hellwig break; 14854a3fd211SDave Hansen } 1486b41572e9SDave Hansen 14873fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 1488b41572e9SDave Hansen if (error) 1489b41572e9SDave Hansen return error; 14906146f0d5SMimi Zohar 14911da177e4SLinus Torvalds /* 14921da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 14931da177e4SLinus Torvalds */ 14941da177e4SLinus Torvalds if (IS_APPEND(inode)) { 14951da177e4SLinus Torvalds if ((flag & FMODE_WRITE) && !(flag & O_APPEND)) 14967715b521SAl Viro return -EPERM; 14971da177e4SLinus Torvalds if (flag & O_TRUNC) 14987715b521SAl Viro return -EPERM; 14991da177e4SLinus Torvalds } 15001da177e4SLinus Torvalds 15011da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 15027715b521SAl Viro if (flag & O_NOATIME && !is_owner_or_cap(inode)) 15037715b521SAl Viro return -EPERM; 15041da177e4SLinus Torvalds 15051da177e4SLinus Torvalds /* 15061da177e4SLinus Torvalds * Ensure there are no outstanding leases on the file. 15071da177e4SLinus Torvalds */ 1508b65a9cfcSAl Viro return break_lease(inode, flag); 15097715b521SAl Viro } 15107715b521SAl Viro 15117715b521SAl Viro static int handle_truncate(struct path *path) 15127715b521SAl Viro { 15137715b521SAl Viro struct inode *inode = path->dentry->d_inode; 15147715b521SAl Viro int error = get_write_access(inode); 15151da177e4SLinus Torvalds if (error) 15167715b521SAl Viro return error; 15171da177e4SLinus Torvalds /* 15181da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 15191da177e4SLinus Torvalds */ 15201da177e4SLinus Torvalds error = locks_verify_locked(inode); 1521be6d3e56SKentaro Takeda if (!error) 15223fb64190SChristoph Hellwig error = security_path_truncate(path, 0, 1523be6d3e56SKentaro Takeda ATTR_MTIME|ATTR_CTIME|ATTR_OPEN); 15241da177e4SLinus Torvalds if (!error) { 15257715b521SAl Viro error = do_truncate(path->dentry, 0, 1526d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 1527d139d7ffSMiklos Szeredi NULL); 15281da177e4SLinus Torvalds } 15291da177e4SLinus Torvalds put_write_access(inode); 1530acd0c935SMimi Zohar return error; 15311da177e4SLinus Torvalds } 15321da177e4SLinus Torvalds 1533d57999e1SDave Hansen /* 1534d57999e1SDave Hansen * Be careful about ever adding any more callers of this 1535d57999e1SDave Hansen * function. Its flags must be in the namei format, not 1536d57999e1SDave Hansen * what get passed to sys_open(). 1537d57999e1SDave Hansen */ 1538d57999e1SDave Hansen static int __open_namei_create(struct nameidata *nd, struct path *path, 1539aab520e2SDave Hansen int flag, int mode) 1540aab520e2SDave Hansen { 1541aab520e2SDave Hansen int error; 15424ac91378SJan Blunck struct dentry *dir = nd->path.dentry; 1543aab520e2SDave Hansen 1544aab520e2SDave Hansen if (!IS_POSIXACL(dir->d_inode)) 1545ce3b0f8dSAl Viro mode &= ~current_umask(); 1546be6d3e56SKentaro Takeda error = security_path_mknod(&nd->path, path->dentry, mode, 0); 1547be6d3e56SKentaro Takeda if (error) 1548be6d3e56SKentaro Takeda goto out_unlock; 1549aab520e2SDave Hansen error = vfs_create(dir->d_inode, path->dentry, mode, nd); 1550be6d3e56SKentaro Takeda out_unlock: 1551aab520e2SDave Hansen mutex_unlock(&dir->d_inode->i_mutex); 15524ac91378SJan Blunck dput(nd->path.dentry); 15534ac91378SJan Blunck nd->path.dentry = path->dentry; 1554aab520e2SDave Hansen if (error) 1555aab520e2SDave Hansen return error; 1556aab520e2SDave Hansen /* Don't check for write permission, don't truncate */ 15573fb64190SChristoph Hellwig return may_open(&nd->path, 0, flag & ~O_TRUNC); 1558aab520e2SDave Hansen } 1559aab520e2SDave Hansen 15601da177e4SLinus Torvalds /* 1561d57999e1SDave Hansen * Note that while the flag value (low two bits) for sys_open means: 1562d57999e1SDave Hansen * 00 - read-only 1563d57999e1SDave Hansen * 01 - write-only 1564d57999e1SDave Hansen * 10 - read-write 1565d57999e1SDave Hansen * 11 - special 1566d57999e1SDave Hansen * it is changed into 1567d57999e1SDave Hansen * 00 - no permissions needed 1568d57999e1SDave Hansen * 01 - read-permission 1569d57999e1SDave Hansen * 10 - write-permission 1570d57999e1SDave Hansen * 11 - read-write 1571d57999e1SDave Hansen * for the internal routines (ie open_namei()/follow_link() etc) 1572d57999e1SDave Hansen * This is more logical, and also allows the 00 "no perm needed" 1573d57999e1SDave Hansen * to be used for symlinks (where the permissions are checked 1574d57999e1SDave Hansen * later). 1575d57999e1SDave Hansen * 1576d57999e1SDave Hansen */ 1577d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 1578d57999e1SDave Hansen { 1579d57999e1SDave Hansen if ((flag+1) & O_ACCMODE) 1580d57999e1SDave Hansen flag++; 1581d57999e1SDave Hansen return flag; 1582d57999e1SDave Hansen } 1583d57999e1SDave Hansen 15847715b521SAl Viro static int open_will_truncate(int flag, struct inode *inode) 15854a3fd211SDave Hansen { 1586d57999e1SDave Hansen /* 15874a3fd211SDave Hansen * We'll never write to the fs underlying 15884a3fd211SDave Hansen * a device file. 15894a3fd211SDave Hansen */ 15904a3fd211SDave Hansen if (special_file(inode->i_mode)) 15914a3fd211SDave Hansen return 0; 15924a3fd211SDave Hansen return (flag & O_TRUNC); 15934a3fd211SDave Hansen } 15944a3fd211SDave Hansen 15954a3fd211SDave Hansen /* 15964a3fd211SDave Hansen * Note that the low bits of the passed in "open_flag" 15974a3fd211SDave Hansen * are not the same as in the local variable "flag". See 15984a3fd211SDave Hansen * open_to_namei_flags() for more details. 15991da177e4SLinus Torvalds */ 1600a70e65dfSChristoph Hellwig struct file *do_filp_open(int dfd, const char *pathname, 16016e8341a1SAl Viro int open_flag, int mode, int acc_mode) 16021da177e4SLinus Torvalds { 16034a3fd211SDave Hansen struct file *filp; 1604a70e65dfSChristoph Hellwig struct nameidata nd; 16056e8341a1SAl Viro int error; 16066de88d72SAl Viro struct path path, save; 16071da177e4SLinus Torvalds struct dentry *dir; 16081da177e4SLinus Torvalds int count = 0; 16097715b521SAl Viro int will_truncate; 1610d57999e1SDave Hansen int flag = open_to_namei_flags(open_flag); 16111da177e4SLinus Torvalds 16126b2f3d1fSChristoph Hellwig /* 16136b2f3d1fSChristoph Hellwig * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only 16146b2f3d1fSChristoph Hellwig * check for O_DSYNC if the need any syncing at all we enforce it's 16156b2f3d1fSChristoph Hellwig * always set instead of having to deal with possibly weird behaviour 16166b2f3d1fSChristoph Hellwig * for malicious applications setting only __O_SYNC. 16176b2f3d1fSChristoph Hellwig */ 16186b2f3d1fSChristoph Hellwig if (open_flag & __O_SYNC) 16196b2f3d1fSChristoph Hellwig open_flag |= O_DSYNC; 16206b2f3d1fSChristoph Hellwig 16216e8341a1SAl Viro if (!acc_mode) 1622b77b0646SAl Viro acc_mode = MAY_OPEN | ACC_MODE(flag); 16231da177e4SLinus Torvalds 1624834f2a4aSTrond Myklebust /* O_TRUNC implies we need access checks for write permissions */ 1625834f2a4aSTrond Myklebust if (flag & O_TRUNC) 1626834f2a4aSTrond Myklebust acc_mode |= MAY_WRITE; 1627834f2a4aSTrond Myklebust 16281da177e4SLinus Torvalds /* Allow the LSM permission hook to distinguish append 16291da177e4SLinus Torvalds access from general write access. */ 16301da177e4SLinus Torvalds if (flag & O_APPEND) 16311da177e4SLinus Torvalds acc_mode |= MAY_APPEND; 16321da177e4SLinus Torvalds 16331da177e4SLinus Torvalds /* 16341da177e4SLinus Torvalds * The simplest case - just a plain lookup. 16351da177e4SLinus Torvalds */ 16361da177e4SLinus Torvalds if (!(flag & O_CREAT)) { 16372dd6d1f4SAl Viro filp = get_empty_filp(); 16382dd6d1f4SAl Viro 16392dd6d1f4SAl Viro if (filp == NULL) 16402dd6d1f4SAl Viro return ERR_PTR(-ENFILE); 16412dd6d1f4SAl Viro nd.intent.open.file = filp; 1642482928d5SAl Viro filp->f_flags = open_flag; 16432dd6d1f4SAl Viro nd.intent.open.flags = flag; 16442dd6d1f4SAl Viro nd.intent.open.create_mode = 0; 16452dd6d1f4SAl Viro error = do_path_lookup(dfd, pathname, 16462dd6d1f4SAl Viro lookup_flags(flag)|LOOKUP_OPEN, &nd); 16472dd6d1f4SAl Viro if (IS_ERR(nd.intent.open.file)) { 16482dd6d1f4SAl Viro if (error == 0) { 16492dd6d1f4SAl Viro error = PTR_ERR(nd.intent.open.file); 16502dd6d1f4SAl Viro path_put(&nd.path); 16512dd6d1f4SAl Viro } 16522dd6d1f4SAl Viro } else if (error) 16532dd6d1f4SAl Viro release_open_intent(&nd); 16541da177e4SLinus Torvalds if (error) 1655a70e65dfSChristoph Hellwig return ERR_PTR(error); 16561da177e4SLinus Torvalds goto ok; 16571da177e4SLinus Torvalds } 16581da177e4SLinus Torvalds 16591da177e4SLinus Torvalds /* 16601da177e4SLinus Torvalds * Create - we need to know the parent. 16611da177e4SLinus Torvalds */ 16629b4a9b14SAl Viro error = path_init(dfd, pathname, LOOKUP_PARENT, &nd); 16631da177e4SLinus Torvalds if (error) 1664a70e65dfSChristoph Hellwig return ERR_PTR(error); 16659b4a9b14SAl Viro error = path_walk(pathname, &nd); 1666654f562cSJ. R. Okajima if (error) { 1667654f562cSJ. R. Okajima if (nd.root.mnt) 1668654f562cSJ. R. Okajima path_put(&nd.root); 16699b4a9b14SAl Viro return ERR_PTR(error); 1670654f562cSJ. R. Okajima } 16719b4a9b14SAl Viro if (unlikely(!audit_dummy_context())) 16729b4a9b14SAl Viro audit_inode(pathname, nd.path.dentry); 16731da177e4SLinus Torvalds 16741da177e4SLinus Torvalds /* 16751da177e4SLinus Torvalds * We have the parent and last component. First of all, check 16761da177e4SLinus Torvalds * that we are not asked to creat(2) an obvious directory - that 16771da177e4SLinus Torvalds * will not do. 16781da177e4SLinus Torvalds */ 16791da177e4SLinus Torvalds error = -EISDIR; 1680a70e65dfSChristoph Hellwig if (nd.last_type != LAST_NORM || nd.last.name[nd.last.len]) 16818737f3a1SAl Viro goto exit_parent; 16821da177e4SLinus Torvalds 16838737f3a1SAl Viro error = -ENFILE; 16848737f3a1SAl Viro filp = get_empty_filp(); 16858737f3a1SAl Viro if (filp == NULL) 16868737f3a1SAl Viro goto exit_parent; 16878737f3a1SAl Viro nd.intent.open.file = filp; 1688482928d5SAl Viro filp->f_flags = open_flag; 16898737f3a1SAl Viro nd.intent.open.flags = flag; 16908737f3a1SAl Viro nd.intent.open.create_mode = mode; 1691a70e65dfSChristoph Hellwig dir = nd.path.dentry; 1692a70e65dfSChristoph Hellwig nd.flags &= ~LOOKUP_PARENT; 16938737f3a1SAl Viro nd.flags |= LOOKUP_CREATE | LOOKUP_OPEN; 16943516586aSAl Viro if (flag & O_EXCL) 16953516586aSAl Viro nd.flags |= LOOKUP_EXCL; 16961b1dcc1bSJes Sorensen mutex_lock(&dir->d_inode->i_mutex); 1697a70e65dfSChristoph Hellwig path.dentry = lookup_hash(&nd); 1698a70e65dfSChristoph Hellwig path.mnt = nd.path.mnt; 16991da177e4SLinus Torvalds 17001da177e4SLinus Torvalds do_last: 17014e7506e4SAl Viro error = PTR_ERR(path.dentry); 17024e7506e4SAl Viro if (IS_ERR(path.dentry)) { 17031b1dcc1bSJes Sorensen mutex_unlock(&dir->d_inode->i_mutex); 17041da177e4SLinus Torvalds goto exit; 17051da177e4SLinus Torvalds } 17061da177e4SLinus Torvalds 1707a70e65dfSChristoph Hellwig if (IS_ERR(nd.intent.open.file)) { 1708a70e65dfSChristoph Hellwig error = PTR_ERR(nd.intent.open.file); 17094a3fd211SDave Hansen goto exit_mutex_unlock; 17104af4c52fSOleg Drokin } 17114af4c52fSOleg Drokin 17121da177e4SLinus Torvalds /* Negative dentry, just create the file */ 17134e7506e4SAl Viro if (!path.dentry->d_inode) { 17144a3fd211SDave Hansen /* 17154a3fd211SDave Hansen * This write is needed to ensure that a 17164a3fd211SDave Hansen * ro->rw transition does not occur between 17174a3fd211SDave Hansen * the time when the file is created and when 17184a3fd211SDave Hansen * a permanent write count is taken through 17194a3fd211SDave Hansen * the 'struct file' in nameidata_to_filp(). 17204a3fd211SDave Hansen */ 17214a3fd211SDave Hansen error = mnt_want_write(nd.path.mnt); 17221da177e4SLinus Torvalds if (error) 17234a3fd211SDave Hansen goto exit_mutex_unlock; 17244a3fd211SDave Hansen error = __open_namei_create(&nd, &path, flag, mode); 17254a3fd211SDave Hansen if (error) { 17264a3fd211SDave Hansen mnt_drop_write(nd.path.mnt); 17271da177e4SLinus Torvalds goto exit; 17284a3fd211SDave Hansen } 1729482928d5SAl Viro filp = nameidata_to_filp(&nd); 17304a3fd211SDave Hansen mnt_drop_write(nd.path.mnt); 1731654f562cSJ. R. Okajima if (nd.root.mnt) 1732654f562cSJ. R. Okajima path_put(&nd.root); 1733b65a9cfcSAl Viro if (!IS_ERR(filp)) { 1734b65a9cfcSAl Viro error = ima_path_check(&filp->f_path, filp->f_mode & 17351429b3ecSAl Viro (MAY_READ | MAY_WRITE | MAY_EXEC)); 1736b65a9cfcSAl Viro if (error) { 1737b65a9cfcSAl Viro fput(filp); 1738b65a9cfcSAl Viro filp = ERR_PTR(error); 1739b65a9cfcSAl Viro } 1740b65a9cfcSAl Viro } 17414a3fd211SDave Hansen return filp; 17421da177e4SLinus Torvalds } 17431da177e4SLinus Torvalds 17441da177e4SLinus Torvalds /* 17451da177e4SLinus Torvalds * It already exists. 17461da177e4SLinus Torvalds */ 17471b1dcc1bSJes Sorensen mutex_unlock(&dir->d_inode->i_mutex); 17485a190ae6SAl Viro audit_inode(pathname, path.dentry); 17491da177e4SLinus Torvalds 17501da177e4SLinus Torvalds error = -EEXIST; 17511da177e4SLinus Torvalds if (flag & O_EXCL) 17521da177e4SLinus Torvalds goto exit_dput; 17531da177e4SLinus Torvalds 1754e13b210fSAl Viro if (__follow_mount(&path)) { 17551da177e4SLinus Torvalds error = -ELOOP; 1756ba7a4c1aSAl Viro if (flag & O_NOFOLLOW) 1757ba7a4c1aSAl Viro goto exit_dput; 17581da177e4SLinus Torvalds } 17593e2efce0SAmy Griffis 17601da177e4SLinus Torvalds error = -ENOENT; 17614e7506e4SAl Viro if (!path.dentry->d_inode) 17621da177e4SLinus Torvalds goto exit_dput; 1763acfa4380SAl Viro if (path.dentry->d_inode->i_op->follow_link) 17641da177e4SLinus Torvalds goto do_link; 17651da177e4SLinus Torvalds 1766a70e65dfSChristoph Hellwig path_to_nameidata(&path, &nd); 17671da177e4SLinus Torvalds error = -EISDIR; 1768cb59861fSJeff Layton if (S_ISDIR(path.dentry->d_inode->i_mode)) 17691da177e4SLinus Torvalds goto exit; 17701da177e4SLinus Torvalds ok: 17714a3fd211SDave Hansen /* 17724a3fd211SDave Hansen * Consider: 17734a3fd211SDave Hansen * 1. may_open() truncates a file 17744a3fd211SDave Hansen * 2. a rw->ro mount transition occurs 17754a3fd211SDave Hansen * 3. nameidata_to_filp() fails due to 17764a3fd211SDave Hansen * the ro mount. 17774a3fd211SDave Hansen * That would be inconsistent, and should 17784a3fd211SDave Hansen * be avoided. Taking this mnt write here 17794a3fd211SDave Hansen * ensures that (2) can not occur. 17804a3fd211SDave Hansen */ 17817715b521SAl Viro will_truncate = open_will_truncate(flag, nd.path.dentry->d_inode); 17827715b521SAl Viro if (will_truncate) { 17834a3fd211SDave Hansen error = mnt_want_write(nd.path.mnt); 17841da177e4SLinus Torvalds if (error) 17851da177e4SLinus Torvalds goto exit; 17864a3fd211SDave Hansen } 17873fb64190SChristoph Hellwig error = may_open(&nd.path, acc_mode, flag); 17884a3fd211SDave Hansen if (error) { 17897715b521SAl Viro if (will_truncate) 17904a3fd211SDave Hansen mnt_drop_write(nd.path.mnt); 17914a3fd211SDave Hansen goto exit; 17924a3fd211SDave Hansen } 1793482928d5SAl Viro filp = nameidata_to_filp(&nd); 1794b65a9cfcSAl Viro if (!IS_ERR(filp)) { 1795b65a9cfcSAl Viro error = ima_path_check(&filp->f_path, filp->f_mode & 17961429b3ecSAl Viro (MAY_READ | MAY_WRITE | MAY_EXEC)); 1797b65a9cfcSAl Viro if (error) { 1798b65a9cfcSAl Viro fput(filp); 1799b65a9cfcSAl Viro filp = ERR_PTR(error); 18007715b521SAl Viro } 1801b65a9cfcSAl Viro } 1802b65a9cfcSAl Viro if (!IS_ERR(filp)) { 18037715b521SAl Viro if (acc_mode & MAY_WRITE) 18047715b521SAl Viro vfs_dq_init(nd.path.dentry->d_inode); 18057715b521SAl Viro 18067715b521SAl Viro if (will_truncate) { 18077715b521SAl Viro error = handle_truncate(&nd.path); 18087715b521SAl Viro if (error) { 18097715b521SAl Viro fput(filp); 1810b65a9cfcSAl Viro filp = ERR_PTR(error); 1811b65a9cfcSAl Viro } 18127715b521SAl Viro } 18137715b521SAl Viro } 18144a3fd211SDave Hansen /* 18154a3fd211SDave Hansen * It is now safe to drop the mnt write 18164a3fd211SDave Hansen * because the filp has had a write taken 18174a3fd211SDave Hansen * on its behalf. 18184a3fd211SDave Hansen */ 18197715b521SAl Viro if (will_truncate) 18204a3fd211SDave Hansen mnt_drop_write(nd.path.mnt); 1821654f562cSJ. R. Okajima if (nd.root.mnt) 1822654f562cSJ. R. Okajima path_put(&nd.root); 18234a3fd211SDave Hansen return filp; 18241da177e4SLinus Torvalds 18254a3fd211SDave Hansen exit_mutex_unlock: 18264a3fd211SDave Hansen mutex_unlock(&dir->d_inode->i_mutex); 18271da177e4SLinus Torvalds exit_dput: 1828a70e65dfSChristoph Hellwig path_put_conditional(&path, &nd); 18291da177e4SLinus Torvalds exit: 1830a70e65dfSChristoph Hellwig if (!IS_ERR(nd.intent.open.file)) 1831a70e65dfSChristoph Hellwig release_open_intent(&nd); 18328737f3a1SAl Viro exit_parent: 18332a737871SAl Viro if (nd.root.mnt) 18342a737871SAl Viro path_put(&nd.root); 1835a70e65dfSChristoph Hellwig path_put(&nd.path); 1836a70e65dfSChristoph Hellwig return ERR_PTR(error); 18371da177e4SLinus Torvalds 18381da177e4SLinus Torvalds do_link: 18391da177e4SLinus Torvalds error = -ELOOP; 18401da177e4SLinus Torvalds if (flag & O_NOFOLLOW) 18411da177e4SLinus Torvalds goto exit_dput; 18421da177e4SLinus Torvalds /* 18431da177e4SLinus Torvalds * This is subtle. Instead of calling do_follow_link() we do the 18441da177e4SLinus Torvalds * thing by hands. The reason is that this way we have zero link_count 18451da177e4SLinus Torvalds * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT. 18461da177e4SLinus Torvalds * After that we have the parent and last component, i.e. 18471da177e4SLinus Torvalds * we are in the same situation as after the first path_walk(). 18481da177e4SLinus Torvalds * Well, almost - if the last component is normal we get its copy 18491da177e4SLinus Torvalds * stored in nd->last.name and we will have to putname() it when we 18501da177e4SLinus Torvalds * are done. Procfs-like symlinks just set LAST_BIND. 18511da177e4SLinus Torvalds */ 1852a70e65dfSChristoph Hellwig nd.flags |= LOOKUP_PARENT; 1853a70e65dfSChristoph Hellwig error = security_inode_follow_link(path.dentry, &nd); 18541da177e4SLinus Torvalds if (error) 18551da177e4SLinus Torvalds goto exit_dput; 18566de88d72SAl Viro save = nd.path; 18576de88d72SAl Viro path_get(&save); 1858a70e65dfSChristoph Hellwig error = __do_follow_link(&path, &nd); 18596de88d72SAl Viro if (error == -ESTALE) { 18606de88d72SAl Viro /* nd.path had been dropped */ 18616de88d72SAl Viro nd.path = save; 18626de88d72SAl Viro path_get(&nd.path); 18636de88d72SAl Viro nd.flags |= LOOKUP_REVAL; 18646de88d72SAl Viro error = __do_follow_link(&path, &nd); 18656de88d72SAl Viro } 18666de88d72SAl Viro path_put(&save); 1867258fa999SAl Viro path_put(&path); 1868de459215SKirill Korotaev if (error) { 1869de459215SKirill Korotaev /* Does someone understand code flow here? Or it is only 1870de459215SKirill Korotaev * me so stupid? Anathema to whoever designed this non-sense 1871de459215SKirill Korotaev * with "intent.open". 1872de459215SKirill Korotaev */ 1873a70e65dfSChristoph Hellwig release_open_intent(&nd); 1874654f562cSJ. R. Okajima if (nd.root.mnt) 1875654f562cSJ. R. Okajima path_put(&nd.root); 1876a70e65dfSChristoph Hellwig return ERR_PTR(error); 1877de459215SKirill Korotaev } 1878a70e65dfSChristoph Hellwig nd.flags &= ~LOOKUP_PARENT; 1879a70e65dfSChristoph Hellwig if (nd.last_type == LAST_BIND) 18801da177e4SLinus Torvalds goto ok; 18811da177e4SLinus Torvalds error = -EISDIR; 1882a70e65dfSChristoph Hellwig if (nd.last_type != LAST_NORM) 18831da177e4SLinus Torvalds goto exit; 1884a70e65dfSChristoph Hellwig if (nd.last.name[nd.last.len]) { 1885a70e65dfSChristoph Hellwig __putname(nd.last.name); 18861da177e4SLinus Torvalds goto exit; 18871da177e4SLinus Torvalds } 18881da177e4SLinus Torvalds error = -ELOOP; 18891da177e4SLinus Torvalds if (count++==32) { 1890a70e65dfSChristoph Hellwig __putname(nd.last.name); 18911da177e4SLinus Torvalds goto exit; 18921da177e4SLinus Torvalds } 1893a70e65dfSChristoph Hellwig dir = nd.path.dentry; 18941b1dcc1bSJes Sorensen mutex_lock(&dir->d_inode->i_mutex); 1895a70e65dfSChristoph Hellwig path.dentry = lookup_hash(&nd); 1896a70e65dfSChristoph Hellwig path.mnt = nd.path.mnt; 1897a70e65dfSChristoph Hellwig __putname(nd.last.name); 18981da177e4SLinus Torvalds goto do_last; 18991da177e4SLinus Torvalds } 19001da177e4SLinus Torvalds 19011da177e4SLinus Torvalds /** 1902a70e65dfSChristoph Hellwig * filp_open - open file and return file pointer 1903a70e65dfSChristoph Hellwig * 1904a70e65dfSChristoph Hellwig * @filename: path to open 1905a70e65dfSChristoph Hellwig * @flags: open flags as per the open(2) second argument 1906a70e65dfSChristoph Hellwig * @mode: mode for the new file if O_CREAT is set, else ignored 1907a70e65dfSChristoph Hellwig * 1908a70e65dfSChristoph Hellwig * This is the helper to open a file from kernelspace if you really 1909a70e65dfSChristoph Hellwig * have to. But in generally you should not do this, so please move 1910a70e65dfSChristoph Hellwig * along, nothing to see here.. 1911a70e65dfSChristoph Hellwig */ 1912a70e65dfSChristoph Hellwig struct file *filp_open(const char *filename, int flags, int mode) 1913a70e65dfSChristoph Hellwig { 19146e8341a1SAl Viro return do_filp_open(AT_FDCWD, filename, flags, mode, 0); 1915a70e65dfSChristoph Hellwig } 1916a70e65dfSChristoph Hellwig EXPORT_SYMBOL(filp_open); 1917a70e65dfSChristoph Hellwig 1918a70e65dfSChristoph Hellwig /** 19191da177e4SLinus Torvalds * lookup_create - lookup a dentry, creating it if it doesn't exist 19201da177e4SLinus Torvalds * @nd: nameidata info 19211da177e4SLinus Torvalds * @is_dir: directory flag 19221da177e4SLinus Torvalds * 19231da177e4SLinus Torvalds * Simple function to lookup and return a dentry and create it 19241da177e4SLinus Torvalds * if it doesn't exist. Is SMP-safe. 1925c663e5d8SChristoph Hellwig * 19264ac91378SJan Blunck * Returns with nd->path.dentry->d_inode->i_mutex locked. 19271da177e4SLinus Torvalds */ 19281da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir) 19291da177e4SLinus Torvalds { 1930c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 19311da177e4SLinus Torvalds 19324ac91378SJan Blunck mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 1933c663e5d8SChristoph Hellwig /* 1934c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 1935c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 1936c663e5d8SChristoph Hellwig */ 19371da177e4SLinus Torvalds if (nd->last_type != LAST_NORM) 19381da177e4SLinus Torvalds goto fail; 19391da177e4SLinus Torvalds nd->flags &= ~LOOKUP_PARENT; 19403516586aSAl Viro nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL; 1941a634904aSASANO Masahiro nd->intent.open.flags = O_EXCL; 1942c663e5d8SChristoph Hellwig 1943c663e5d8SChristoph Hellwig /* 1944c663e5d8SChristoph Hellwig * Do the final lookup. 1945c663e5d8SChristoph Hellwig */ 194649705b77SChristoph Hellwig dentry = lookup_hash(nd); 19471da177e4SLinus Torvalds if (IS_ERR(dentry)) 19481da177e4SLinus Torvalds goto fail; 1949c663e5d8SChristoph Hellwig 1950e9baf6e5SAl Viro if (dentry->d_inode) 1951e9baf6e5SAl Viro goto eexist; 1952c663e5d8SChristoph Hellwig /* 1953c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 1954c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 1955c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 1956c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 1957c663e5d8SChristoph Hellwig */ 1958e9baf6e5SAl Viro if (unlikely(!is_dir && nd->last.name[nd->last.len])) { 19591da177e4SLinus Torvalds dput(dentry); 19601da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 1961e9baf6e5SAl Viro } 1962e9baf6e5SAl Viro return dentry; 1963e9baf6e5SAl Viro eexist: 1964e9baf6e5SAl Viro dput(dentry); 1965e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 19661da177e4SLinus Torvalds fail: 19671da177e4SLinus Torvalds return dentry; 19681da177e4SLinus Torvalds } 1969f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create); 19701da177e4SLinus Torvalds 19711da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 19721da177e4SLinus Torvalds { 1973a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 19741da177e4SLinus Torvalds 19751da177e4SLinus Torvalds if (error) 19761da177e4SLinus Torvalds return error; 19771da177e4SLinus Torvalds 19781da177e4SLinus Torvalds if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) 19791da177e4SLinus Torvalds return -EPERM; 19801da177e4SLinus Torvalds 1981acfa4380SAl Viro if (!dir->i_op->mknod) 19821da177e4SLinus Torvalds return -EPERM; 19831da177e4SLinus Torvalds 198408ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 198508ce5f16SSerge E. Hallyn if (error) 198608ce5f16SSerge E. Hallyn return error; 198708ce5f16SSerge E. Hallyn 19881da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 19891da177e4SLinus Torvalds if (error) 19901da177e4SLinus Torvalds return error; 19911da177e4SLinus Torvalds 19929e3509e2SJan Kara vfs_dq_init(dir); 19931da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 1994a74574aaSStephen Smalley if (!error) 1995f38aa942SAmy Griffis fsnotify_create(dir, dentry); 19961da177e4SLinus Torvalds return error; 19971da177e4SLinus Torvalds } 19981da177e4SLinus Torvalds 1999463c3197SDave Hansen static int may_mknod(mode_t mode) 2000463c3197SDave Hansen { 2001463c3197SDave Hansen switch (mode & S_IFMT) { 2002463c3197SDave Hansen case S_IFREG: 2003463c3197SDave Hansen case S_IFCHR: 2004463c3197SDave Hansen case S_IFBLK: 2005463c3197SDave Hansen case S_IFIFO: 2006463c3197SDave Hansen case S_IFSOCK: 2007463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2008463c3197SDave Hansen return 0; 2009463c3197SDave Hansen case S_IFDIR: 2010463c3197SDave Hansen return -EPERM; 2011463c3197SDave Hansen default: 2012463c3197SDave Hansen return -EINVAL; 2013463c3197SDave Hansen } 2014463c3197SDave Hansen } 2015463c3197SDave Hansen 20162e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode, 20172e4d0924SHeiko Carstens unsigned, dev) 20181da177e4SLinus Torvalds { 20192ad94ae6SAl Viro int error; 20201da177e4SLinus Torvalds char *tmp; 20211da177e4SLinus Torvalds struct dentry *dentry; 20221da177e4SLinus Torvalds struct nameidata nd; 20231da177e4SLinus Torvalds 20241da177e4SLinus Torvalds if (S_ISDIR(mode)) 20251da177e4SLinus Torvalds return -EPERM; 20261da177e4SLinus Torvalds 20272ad94ae6SAl Viro error = user_path_parent(dfd, filename, &nd, &tmp); 20281da177e4SLinus Torvalds if (error) 20292ad94ae6SAl Viro return error; 20302ad94ae6SAl Viro 20311da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 2032463c3197SDave Hansen if (IS_ERR(dentry)) { 20331da177e4SLinus Torvalds error = PTR_ERR(dentry); 2034463c3197SDave Hansen goto out_unlock; 2035463c3197SDave Hansen } 20364ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2037ce3b0f8dSAl Viro mode &= ~current_umask(); 2038463c3197SDave Hansen error = may_mknod(mode); 2039463c3197SDave Hansen if (error) 2040463c3197SDave Hansen goto out_dput; 2041463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2042463c3197SDave Hansen if (error) 2043463c3197SDave Hansen goto out_dput; 2044be6d3e56SKentaro Takeda error = security_path_mknod(&nd.path, dentry, mode, dev); 2045be6d3e56SKentaro Takeda if (error) 2046be6d3e56SKentaro Takeda goto out_drop_write; 20471da177e4SLinus Torvalds switch (mode & S_IFMT) { 20481da177e4SLinus Torvalds case 0: case S_IFREG: 20494ac91378SJan Blunck error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd); 20501da177e4SLinus Torvalds break; 20511da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 20524ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode, 20531da177e4SLinus Torvalds new_decode_dev(dev)); 20541da177e4SLinus Torvalds break; 20551da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 20564ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0); 20571da177e4SLinus Torvalds break; 20581da177e4SLinus Torvalds } 2059be6d3e56SKentaro Takeda out_drop_write: 2060463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2061463c3197SDave Hansen out_dput: 20621da177e4SLinus Torvalds dput(dentry); 2063463c3197SDave Hansen out_unlock: 20644ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 20651d957f9bSJan Blunck path_put(&nd.path); 20661da177e4SLinus Torvalds putname(tmp); 20671da177e4SLinus Torvalds 20681da177e4SLinus Torvalds return error; 20691da177e4SLinus Torvalds } 20701da177e4SLinus Torvalds 20713480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev) 20725590ff0dSUlrich Drepper { 20735590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 20745590ff0dSUlrich Drepper } 20755590ff0dSUlrich Drepper 20761da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 20771da177e4SLinus Torvalds { 2078a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 20791da177e4SLinus Torvalds 20801da177e4SLinus Torvalds if (error) 20811da177e4SLinus Torvalds return error; 20821da177e4SLinus Torvalds 2083acfa4380SAl Viro if (!dir->i_op->mkdir) 20841da177e4SLinus Torvalds return -EPERM; 20851da177e4SLinus Torvalds 20861da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 20871da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 20881da177e4SLinus Torvalds if (error) 20891da177e4SLinus Torvalds return error; 20901da177e4SLinus Torvalds 20919e3509e2SJan Kara vfs_dq_init(dir); 20921da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2093a74574aaSStephen Smalley if (!error) 2094f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 20951da177e4SLinus Torvalds return error; 20961da177e4SLinus Torvalds } 20971da177e4SLinus Torvalds 20982e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode) 20991da177e4SLinus Torvalds { 21001da177e4SLinus Torvalds int error = 0; 21011da177e4SLinus Torvalds char * tmp; 21026902d925SDave Hansen struct dentry *dentry; 21036902d925SDave Hansen struct nameidata nd; 21041da177e4SLinus Torvalds 21052ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &tmp); 21062ad94ae6SAl Viro if (error) 21076902d925SDave Hansen goto out_err; 21081da177e4SLinus Torvalds 21091da177e4SLinus Torvalds dentry = lookup_create(&nd, 1); 21101da177e4SLinus Torvalds error = PTR_ERR(dentry); 21116902d925SDave Hansen if (IS_ERR(dentry)) 21126902d925SDave Hansen goto out_unlock; 21136902d925SDave Hansen 21144ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2115ce3b0f8dSAl Viro mode &= ~current_umask(); 2116463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2117463c3197SDave Hansen if (error) 2118463c3197SDave Hansen goto out_dput; 2119be6d3e56SKentaro Takeda error = security_path_mkdir(&nd.path, dentry, mode); 2120be6d3e56SKentaro Takeda if (error) 2121be6d3e56SKentaro Takeda goto out_drop_write; 21224ac91378SJan Blunck error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode); 2123be6d3e56SKentaro Takeda out_drop_write: 2124463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2125463c3197SDave Hansen out_dput: 21261da177e4SLinus Torvalds dput(dentry); 21276902d925SDave Hansen out_unlock: 21284ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 21291d957f9bSJan Blunck path_put(&nd.path); 21301da177e4SLinus Torvalds putname(tmp); 21316902d925SDave Hansen out_err: 21321da177e4SLinus Torvalds return error; 21331da177e4SLinus Torvalds } 21341da177e4SLinus Torvalds 21353cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode) 21365590ff0dSUlrich Drepper { 21375590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 21385590ff0dSUlrich Drepper } 21395590ff0dSUlrich Drepper 21401da177e4SLinus Torvalds /* 21411da177e4SLinus Torvalds * We try to drop the dentry early: we should have 21421da177e4SLinus Torvalds * a usage count of 2 if we're the only user of this 21431da177e4SLinus Torvalds * dentry, and if that is true (possibly after pruning 21441da177e4SLinus Torvalds * the dcache), then we drop the dentry now. 21451da177e4SLinus Torvalds * 21461da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 21471da177e4SLinus Torvalds * do a 21481da177e4SLinus Torvalds * 21491da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 21501da177e4SLinus Torvalds * return -EBUSY; 21511da177e4SLinus Torvalds * 21521da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 21531da177e4SLinus Torvalds * that is still in use by something else.. 21541da177e4SLinus Torvalds */ 21551da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 21561da177e4SLinus Torvalds { 21571da177e4SLinus Torvalds dget(dentry); 21581da177e4SLinus Torvalds shrink_dcache_parent(dentry); 21591da177e4SLinus Torvalds spin_lock(&dcache_lock); 21601da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 21611da177e4SLinus Torvalds if (atomic_read(&dentry->d_count) == 2) 21621da177e4SLinus Torvalds __d_drop(dentry); 21631da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 21641da177e4SLinus Torvalds spin_unlock(&dcache_lock); 21651da177e4SLinus Torvalds } 21661da177e4SLinus Torvalds 21671da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 21681da177e4SLinus Torvalds { 21691da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 21701da177e4SLinus Torvalds 21711da177e4SLinus Torvalds if (error) 21721da177e4SLinus Torvalds return error; 21731da177e4SLinus Torvalds 2174acfa4380SAl Viro if (!dir->i_op->rmdir) 21751da177e4SLinus Torvalds return -EPERM; 21761da177e4SLinus Torvalds 21779e3509e2SJan Kara vfs_dq_init(dir); 21781da177e4SLinus Torvalds 21791b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 21801da177e4SLinus Torvalds dentry_unhash(dentry); 21811da177e4SLinus Torvalds if (d_mountpoint(dentry)) 21821da177e4SLinus Torvalds error = -EBUSY; 21831da177e4SLinus Torvalds else { 21841da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 21851da177e4SLinus Torvalds if (!error) { 21861da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 21871da177e4SLinus Torvalds if (!error) 21881da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 21891da177e4SLinus Torvalds } 21901da177e4SLinus Torvalds } 21911b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 21921da177e4SLinus Torvalds if (!error) { 21931da177e4SLinus Torvalds d_delete(dentry); 21941da177e4SLinus Torvalds } 21951da177e4SLinus Torvalds dput(dentry); 21961da177e4SLinus Torvalds 21971da177e4SLinus Torvalds return error; 21981da177e4SLinus Torvalds } 21991da177e4SLinus Torvalds 22005590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 22011da177e4SLinus Torvalds { 22021da177e4SLinus Torvalds int error = 0; 22031da177e4SLinus Torvalds char * name; 22041da177e4SLinus Torvalds struct dentry *dentry; 22051da177e4SLinus Torvalds struct nameidata nd; 22061da177e4SLinus Torvalds 22072ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 22081da177e4SLinus Torvalds if (error) 22092ad94ae6SAl Viro return error; 22101da177e4SLinus Torvalds 22111da177e4SLinus Torvalds switch(nd.last_type) { 22121da177e4SLinus Torvalds case LAST_DOTDOT: 22131da177e4SLinus Torvalds error = -ENOTEMPTY; 22141da177e4SLinus Torvalds goto exit1; 22151da177e4SLinus Torvalds case LAST_DOT: 22161da177e4SLinus Torvalds error = -EINVAL; 22171da177e4SLinus Torvalds goto exit1; 22181da177e4SLinus Torvalds case LAST_ROOT: 22191da177e4SLinus Torvalds error = -EBUSY; 22201da177e4SLinus Torvalds goto exit1; 22211da177e4SLinus Torvalds } 22220612d9fbSOGAWA Hirofumi 22230612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 22240612d9fbSOGAWA Hirofumi 22254ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 222649705b77SChristoph Hellwig dentry = lookup_hash(&nd); 22271da177e4SLinus Torvalds error = PTR_ERR(dentry); 22286902d925SDave Hansen if (IS_ERR(dentry)) 22296902d925SDave Hansen goto exit2; 22300622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 22310622753bSDave Hansen if (error) 22320622753bSDave Hansen goto exit3; 2233be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 2234be6d3e56SKentaro Takeda if (error) 2235be6d3e56SKentaro Takeda goto exit4; 22364ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 2237be6d3e56SKentaro Takeda exit4: 22380622753bSDave Hansen mnt_drop_write(nd.path.mnt); 22390622753bSDave Hansen exit3: 22401da177e4SLinus Torvalds dput(dentry); 22416902d925SDave Hansen exit2: 22424ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 22431da177e4SLinus Torvalds exit1: 22441d957f9bSJan Blunck path_put(&nd.path); 22451da177e4SLinus Torvalds putname(name); 22461da177e4SLinus Torvalds return error; 22471da177e4SLinus Torvalds } 22481da177e4SLinus Torvalds 22493cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 22505590ff0dSUlrich Drepper { 22515590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 22525590ff0dSUlrich Drepper } 22535590ff0dSUlrich Drepper 22541da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 22551da177e4SLinus Torvalds { 22561da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 22571da177e4SLinus Torvalds 22581da177e4SLinus Torvalds if (error) 22591da177e4SLinus Torvalds return error; 22601da177e4SLinus Torvalds 2261acfa4380SAl Viro if (!dir->i_op->unlink) 22621da177e4SLinus Torvalds return -EPERM; 22631da177e4SLinus Torvalds 22649e3509e2SJan Kara vfs_dq_init(dir); 22651da177e4SLinus Torvalds 22661b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 22671da177e4SLinus Torvalds if (d_mountpoint(dentry)) 22681da177e4SLinus Torvalds error = -EBUSY; 22691da177e4SLinus Torvalds else { 22701da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 22711da177e4SLinus Torvalds if (!error) 22721da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 22731da177e4SLinus Torvalds } 22741b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 22751da177e4SLinus Torvalds 22761da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 22771da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 2278ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 22791da177e4SLinus Torvalds d_delete(dentry); 22801da177e4SLinus Torvalds } 22810eeca283SRobert Love 22821da177e4SLinus Torvalds return error; 22831da177e4SLinus Torvalds } 22841da177e4SLinus Torvalds 22851da177e4SLinus Torvalds /* 22861da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 22871b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 22881da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 22891da177e4SLinus Torvalds * while waiting on the I/O. 22901da177e4SLinus Torvalds */ 22915590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 22921da177e4SLinus Torvalds { 22932ad94ae6SAl Viro int error; 22941da177e4SLinus Torvalds char *name; 22951da177e4SLinus Torvalds struct dentry *dentry; 22961da177e4SLinus Torvalds struct nameidata nd; 22971da177e4SLinus Torvalds struct inode *inode = NULL; 22981da177e4SLinus Torvalds 22992ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 23001da177e4SLinus Torvalds if (error) 23012ad94ae6SAl Viro return error; 23022ad94ae6SAl Viro 23031da177e4SLinus Torvalds error = -EISDIR; 23041da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 23051da177e4SLinus Torvalds goto exit1; 23060612d9fbSOGAWA Hirofumi 23070612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 23080612d9fbSOGAWA Hirofumi 23094ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 231049705b77SChristoph Hellwig dentry = lookup_hash(&nd); 23111da177e4SLinus Torvalds error = PTR_ERR(dentry); 23121da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 23131da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 23141da177e4SLinus Torvalds if (nd.last.name[nd.last.len]) 23151da177e4SLinus Torvalds goto slashes; 23161da177e4SLinus Torvalds inode = dentry->d_inode; 23171da177e4SLinus Torvalds if (inode) 23181da177e4SLinus Torvalds atomic_inc(&inode->i_count); 23190622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 23200622753bSDave Hansen if (error) 23210622753bSDave Hansen goto exit2; 2322be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 2323be6d3e56SKentaro Takeda if (error) 2324be6d3e56SKentaro Takeda goto exit3; 23254ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 2326be6d3e56SKentaro Takeda exit3: 23270622753bSDave Hansen mnt_drop_write(nd.path.mnt); 23281da177e4SLinus Torvalds exit2: 23291da177e4SLinus Torvalds dput(dentry); 23301da177e4SLinus Torvalds } 23314ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 23321da177e4SLinus Torvalds if (inode) 23331da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 23341da177e4SLinus Torvalds exit1: 23351d957f9bSJan Blunck path_put(&nd.path); 23361da177e4SLinus Torvalds putname(name); 23371da177e4SLinus Torvalds return error; 23381da177e4SLinus Torvalds 23391da177e4SLinus Torvalds slashes: 23401da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 23411da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 23421da177e4SLinus Torvalds goto exit2; 23431da177e4SLinus Torvalds } 23441da177e4SLinus Torvalds 23452e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 23465590ff0dSUlrich Drepper { 23475590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 23485590ff0dSUlrich Drepper return -EINVAL; 23495590ff0dSUlrich Drepper 23505590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 23515590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 23525590ff0dSUlrich Drepper 23535590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 23545590ff0dSUlrich Drepper } 23555590ff0dSUlrich Drepper 23563480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 23575590ff0dSUlrich Drepper { 23585590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 23595590ff0dSUlrich Drepper } 23605590ff0dSUlrich Drepper 2361db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 23621da177e4SLinus Torvalds { 2363a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 23641da177e4SLinus Torvalds 23651da177e4SLinus Torvalds if (error) 23661da177e4SLinus Torvalds return error; 23671da177e4SLinus Torvalds 2368acfa4380SAl Viro if (!dir->i_op->symlink) 23691da177e4SLinus Torvalds return -EPERM; 23701da177e4SLinus Torvalds 23711da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 23721da177e4SLinus Torvalds if (error) 23731da177e4SLinus Torvalds return error; 23741da177e4SLinus Torvalds 23759e3509e2SJan Kara vfs_dq_init(dir); 23761da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 2377a74574aaSStephen Smalley if (!error) 2378f38aa942SAmy Griffis fsnotify_create(dir, dentry); 23791da177e4SLinus Torvalds return error; 23801da177e4SLinus Torvalds } 23811da177e4SLinus Torvalds 23822e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 23832e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 23841da177e4SLinus Torvalds { 23852ad94ae6SAl Viro int error; 23861da177e4SLinus Torvalds char *from; 23871da177e4SLinus Torvalds char *to; 23886902d925SDave Hansen struct dentry *dentry; 23896902d925SDave Hansen struct nameidata nd; 23901da177e4SLinus Torvalds 23911da177e4SLinus Torvalds from = getname(oldname); 23921da177e4SLinus Torvalds if (IS_ERR(from)) 23931da177e4SLinus Torvalds return PTR_ERR(from); 23942ad94ae6SAl Viro 23952ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 23962ad94ae6SAl Viro if (error) 23976902d925SDave Hansen goto out_putname; 23981da177e4SLinus Torvalds 23991da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 24001da177e4SLinus Torvalds error = PTR_ERR(dentry); 24016902d925SDave Hansen if (IS_ERR(dentry)) 24026902d925SDave Hansen goto out_unlock; 24036902d925SDave Hansen 240475c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 240575c3f29dSDave Hansen if (error) 240675c3f29dSDave Hansen goto out_dput; 2407be6d3e56SKentaro Takeda error = security_path_symlink(&nd.path, dentry, from); 2408be6d3e56SKentaro Takeda if (error) 2409be6d3e56SKentaro Takeda goto out_drop_write; 2410db2e747bSMiklos Szeredi error = vfs_symlink(nd.path.dentry->d_inode, dentry, from); 2411be6d3e56SKentaro Takeda out_drop_write: 241275c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 241375c3f29dSDave Hansen out_dput: 24141da177e4SLinus Torvalds dput(dentry); 24156902d925SDave Hansen out_unlock: 24164ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 24171d957f9bSJan Blunck path_put(&nd.path); 24181da177e4SLinus Torvalds putname(to); 24196902d925SDave Hansen out_putname: 24201da177e4SLinus Torvalds putname(from); 24211da177e4SLinus Torvalds return error; 24221da177e4SLinus Torvalds } 24231da177e4SLinus Torvalds 24243480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 24255590ff0dSUlrich Drepper { 24265590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 24275590ff0dSUlrich Drepper } 24285590ff0dSUlrich Drepper 24291da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 24301da177e4SLinus Torvalds { 24311da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 24321da177e4SLinus Torvalds int error; 24331da177e4SLinus Torvalds 24341da177e4SLinus Torvalds if (!inode) 24351da177e4SLinus Torvalds return -ENOENT; 24361da177e4SLinus Torvalds 2437a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 24381da177e4SLinus Torvalds if (error) 24391da177e4SLinus Torvalds return error; 24401da177e4SLinus Torvalds 24411da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 24421da177e4SLinus Torvalds return -EXDEV; 24431da177e4SLinus Torvalds 24441da177e4SLinus Torvalds /* 24451da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 24461da177e4SLinus Torvalds */ 24471da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 24481da177e4SLinus Torvalds return -EPERM; 2449acfa4380SAl Viro if (!dir->i_op->link) 24501da177e4SLinus Torvalds return -EPERM; 24517e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 24521da177e4SLinus Torvalds return -EPERM; 24531da177e4SLinus Torvalds 24541da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 24551da177e4SLinus Torvalds if (error) 24561da177e4SLinus Torvalds return error; 24571da177e4SLinus Torvalds 24587e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 24599e3509e2SJan Kara vfs_dq_init(dir); 24601da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 24617e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 2462e31e14ecSStephen Smalley if (!error) 24637e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 24641da177e4SLinus Torvalds return error; 24651da177e4SLinus Torvalds } 24661da177e4SLinus Torvalds 24671da177e4SLinus Torvalds /* 24681da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 24691da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 24701da177e4SLinus Torvalds * newname. --KAB 24711da177e4SLinus Torvalds * 24721da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 24731da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 24741da177e4SLinus Torvalds * and other special files. --ADM 24751da177e4SLinus Torvalds */ 24762e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 24772e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 24781da177e4SLinus Torvalds { 24791da177e4SLinus Torvalds struct dentry *new_dentry; 24802d8f3038SAl Viro struct nameidata nd; 24812d8f3038SAl Viro struct path old_path; 24821da177e4SLinus Torvalds int error; 24831da177e4SLinus Torvalds char *to; 24841da177e4SLinus Torvalds 248545c9b11aSUlrich Drepper if ((flags & ~AT_SYMLINK_FOLLOW) != 0) 2486c04030e1SUlrich Drepper return -EINVAL; 2487c04030e1SUlrich Drepper 24882d8f3038SAl Viro error = user_path_at(olddfd, oldname, 248945c9b11aSUlrich Drepper flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0, 24902d8f3038SAl Viro &old_path); 24911da177e4SLinus Torvalds if (error) 24922ad94ae6SAl Viro return error; 24932ad94ae6SAl Viro 24942ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 24951da177e4SLinus Torvalds if (error) 24961da177e4SLinus Torvalds goto out; 24971da177e4SLinus Torvalds error = -EXDEV; 24982d8f3038SAl Viro if (old_path.mnt != nd.path.mnt) 24991da177e4SLinus Torvalds goto out_release; 25001da177e4SLinus Torvalds new_dentry = lookup_create(&nd, 0); 25011da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 25026902d925SDave Hansen if (IS_ERR(new_dentry)) 25036902d925SDave Hansen goto out_unlock; 250475c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 250575c3f29dSDave Hansen if (error) 250675c3f29dSDave Hansen goto out_dput; 2507be6d3e56SKentaro Takeda error = security_path_link(old_path.dentry, &nd.path, new_dentry); 2508be6d3e56SKentaro Takeda if (error) 2509be6d3e56SKentaro Takeda goto out_drop_write; 25102d8f3038SAl Viro error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry); 2511be6d3e56SKentaro Takeda out_drop_write: 251275c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 251375c3f29dSDave Hansen out_dput: 25141da177e4SLinus Torvalds dput(new_dentry); 25156902d925SDave Hansen out_unlock: 25164ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 25171da177e4SLinus Torvalds out_release: 25181d957f9bSJan Blunck path_put(&nd.path); 25192ad94ae6SAl Viro putname(to); 25201da177e4SLinus Torvalds out: 25212d8f3038SAl Viro path_put(&old_path); 25221da177e4SLinus Torvalds 25231da177e4SLinus Torvalds return error; 25241da177e4SLinus Torvalds } 25251da177e4SLinus Torvalds 25263480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 25275590ff0dSUlrich Drepper { 2528c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 25295590ff0dSUlrich Drepper } 25305590ff0dSUlrich Drepper 25311da177e4SLinus Torvalds /* 25321da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 25331da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 25341da177e4SLinus Torvalds * Problems: 25351da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 25361da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 25371da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 2538a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 25391da177e4SLinus Torvalds * story. 25401da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 25411b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 25421da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 25431da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 2544a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 25451da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 25461da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 25471da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 2548a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 25491da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 25501da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 25511da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 25521da177e4SLinus Torvalds * d) some filesystems don't support opened-but-unlinked directories, 25531da177e4SLinus Torvalds * either because of layout or because they are not ready to deal with 25541da177e4SLinus Torvalds * all cases correctly. The latter will be fixed (taking this sort of 25551da177e4SLinus Torvalds * stuff into VFS), but the former is not going away. Solution: the same 25561da177e4SLinus Torvalds * trick as in rmdir(). 25571da177e4SLinus Torvalds * e) conversion from fhandle to dentry may come in the wrong moment - when 25581b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 25591da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 25601b1dcc1bSJes Sorensen * ->i_mutex on parents, which works but leads to some truely excessive 25611da177e4SLinus Torvalds * locking]. 25621da177e4SLinus Torvalds */ 256375c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 25641da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 25651da177e4SLinus Torvalds { 25661da177e4SLinus Torvalds int error = 0; 25671da177e4SLinus Torvalds struct inode *target; 25681da177e4SLinus Torvalds 25691da177e4SLinus Torvalds /* 25701da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 25711da177e4SLinus Torvalds * we'll need to flip '..'. 25721da177e4SLinus Torvalds */ 25731da177e4SLinus Torvalds if (new_dir != old_dir) { 2574f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 25751da177e4SLinus Torvalds if (error) 25761da177e4SLinus Torvalds return error; 25771da177e4SLinus Torvalds } 25781da177e4SLinus Torvalds 25791da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 25801da177e4SLinus Torvalds if (error) 25811da177e4SLinus Torvalds return error; 25821da177e4SLinus Torvalds 25831da177e4SLinus Torvalds target = new_dentry->d_inode; 25841da177e4SLinus Torvalds if (target) { 25851b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 25861da177e4SLinus Torvalds dentry_unhash(new_dentry); 25871da177e4SLinus Torvalds } 25881da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 25891da177e4SLinus Torvalds error = -EBUSY; 25901da177e4SLinus Torvalds else 25911da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 25921da177e4SLinus Torvalds if (target) { 25931da177e4SLinus Torvalds if (!error) 25941da177e4SLinus Torvalds target->i_flags |= S_DEAD; 25951b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 25961da177e4SLinus Torvalds if (d_unhashed(new_dentry)) 25971da177e4SLinus Torvalds d_rehash(new_dentry); 25981da177e4SLinus Torvalds dput(new_dentry); 25991da177e4SLinus Torvalds } 2600e31e14ecSStephen Smalley if (!error) 2601349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 26021da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 26031da177e4SLinus Torvalds return error; 26041da177e4SLinus Torvalds } 26051da177e4SLinus Torvalds 260675c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 26071da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 26081da177e4SLinus Torvalds { 26091da177e4SLinus Torvalds struct inode *target; 26101da177e4SLinus Torvalds int error; 26111da177e4SLinus Torvalds 26121da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 26131da177e4SLinus Torvalds if (error) 26141da177e4SLinus Torvalds return error; 26151da177e4SLinus Torvalds 26161da177e4SLinus Torvalds dget(new_dentry); 26171da177e4SLinus Torvalds target = new_dentry->d_inode; 26181da177e4SLinus Torvalds if (target) 26191b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 26201da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 26211da177e4SLinus Torvalds error = -EBUSY; 26221da177e4SLinus Torvalds else 26231da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 26241da177e4SLinus Torvalds if (!error) { 2625349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 26261da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 26271da177e4SLinus Torvalds } 26281da177e4SLinus Torvalds if (target) 26291b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 26301da177e4SLinus Torvalds dput(new_dentry); 26311da177e4SLinus Torvalds return error; 26321da177e4SLinus Torvalds } 26331da177e4SLinus Torvalds 26341da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 26351da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 26361da177e4SLinus Torvalds { 26371da177e4SLinus Torvalds int error; 26381da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 26390eeca283SRobert Love const char *old_name; 26401da177e4SLinus Torvalds 26411da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 26421da177e4SLinus Torvalds return 0; 26431da177e4SLinus Torvalds 26441da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 26451da177e4SLinus Torvalds if (error) 26461da177e4SLinus Torvalds return error; 26471da177e4SLinus Torvalds 26481da177e4SLinus Torvalds if (!new_dentry->d_inode) 2649a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 26501da177e4SLinus Torvalds else 26511da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 26521da177e4SLinus Torvalds if (error) 26531da177e4SLinus Torvalds return error; 26541da177e4SLinus Torvalds 2655acfa4380SAl Viro if (!old_dir->i_op->rename) 26561da177e4SLinus Torvalds return -EPERM; 26571da177e4SLinus Torvalds 26589e3509e2SJan Kara vfs_dq_init(old_dir); 26599e3509e2SJan Kara vfs_dq_init(new_dir); 26601da177e4SLinus Torvalds 26610eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 26620eeca283SRobert Love 26631da177e4SLinus Torvalds if (is_dir) 26641da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 26651da177e4SLinus Torvalds else 26661da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 26671da177e4SLinus Torvalds if (!error) { 26680eeca283SRobert Love const char *new_name = old_dentry->d_name.name; 266989204c40SJohn McCutchan fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir, 26705a190ae6SAl Viro new_dentry->d_inode, old_dentry); 26711da177e4SLinus Torvalds } 26720eeca283SRobert Love fsnotify_oldname_free(old_name); 26730eeca283SRobert Love 26741da177e4SLinus Torvalds return error; 26751da177e4SLinus Torvalds } 26761da177e4SLinus Torvalds 26772e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 26782e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 26791da177e4SLinus Torvalds { 26801da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 26811da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 26821da177e4SLinus Torvalds struct dentry *trap; 26831da177e4SLinus Torvalds struct nameidata oldnd, newnd; 26842ad94ae6SAl Viro char *from; 26852ad94ae6SAl Viro char *to; 26862ad94ae6SAl Viro int error; 26871da177e4SLinus Torvalds 26882ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 26891da177e4SLinus Torvalds if (error) 26901da177e4SLinus Torvalds goto exit; 26911da177e4SLinus Torvalds 26922ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 26931da177e4SLinus Torvalds if (error) 26941da177e4SLinus Torvalds goto exit1; 26951da177e4SLinus Torvalds 26961da177e4SLinus Torvalds error = -EXDEV; 26974ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 26981da177e4SLinus Torvalds goto exit2; 26991da177e4SLinus Torvalds 27004ac91378SJan Blunck old_dir = oldnd.path.dentry; 27011da177e4SLinus Torvalds error = -EBUSY; 27021da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 27031da177e4SLinus Torvalds goto exit2; 27041da177e4SLinus Torvalds 27054ac91378SJan Blunck new_dir = newnd.path.dentry; 27061da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 27071da177e4SLinus Torvalds goto exit2; 27081da177e4SLinus Torvalds 27090612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 27100612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 27114e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 27120612d9fbSOGAWA Hirofumi 27131da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 27141da177e4SLinus Torvalds 271549705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 27161da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 27171da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 27181da177e4SLinus Torvalds goto exit3; 27191da177e4SLinus Torvalds /* source must exist */ 27201da177e4SLinus Torvalds error = -ENOENT; 27211da177e4SLinus Torvalds if (!old_dentry->d_inode) 27221da177e4SLinus Torvalds goto exit4; 27231da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 27241da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 27251da177e4SLinus Torvalds error = -ENOTDIR; 27261da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 27271da177e4SLinus Torvalds goto exit4; 27281da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 27291da177e4SLinus Torvalds goto exit4; 27301da177e4SLinus Torvalds } 27311da177e4SLinus Torvalds /* source should not be ancestor of target */ 27321da177e4SLinus Torvalds error = -EINVAL; 27331da177e4SLinus Torvalds if (old_dentry == trap) 27341da177e4SLinus Torvalds goto exit4; 273549705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 27361da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 27371da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 27381da177e4SLinus Torvalds goto exit4; 27391da177e4SLinus Torvalds /* target should not be an ancestor of source */ 27401da177e4SLinus Torvalds error = -ENOTEMPTY; 27411da177e4SLinus Torvalds if (new_dentry == trap) 27421da177e4SLinus Torvalds goto exit5; 27431da177e4SLinus Torvalds 27449079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 27459079b1ebSDave Hansen if (error) 27469079b1ebSDave Hansen goto exit5; 2747be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 2748be6d3e56SKentaro Takeda &newnd.path, new_dentry); 2749be6d3e56SKentaro Takeda if (error) 2750be6d3e56SKentaro Takeda goto exit6; 27511da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 27521da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 2753be6d3e56SKentaro Takeda exit6: 27549079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 27551da177e4SLinus Torvalds exit5: 27561da177e4SLinus Torvalds dput(new_dentry); 27571da177e4SLinus Torvalds exit4: 27581da177e4SLinus Torvalds dput(old_dentry); 27591da177e4SLinus Torvalds exit3: 27601da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 27611da177e4SLinus Torvalds exit2: 27621d957f9bSJan Blunck path_put(&newnd.path); 27632ad94ae6SAl Viro putname(to); 27641da177e4SLinus Torvalds exit1: 27651d957f9bSJan Blunck path_put(&oldnd.path); 27661da177e4SLinus Torvalds putname(from); 27672ad94ae6SAl Viro exit: 27681da177e4SLinus Torvalds return error; 27691da177e4SLinus Torvalds } 27701da177e4SLinus Torvalds 2771a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 27725590ff0dSUlrich Drepper { 27735590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 27745590ff0dSUlrich Drepper } 27755590ff0dSUlrich Drepper 27761da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 27771da177e4SLinus Torvalds { 27781da177e4SLinus Torvalds int len; 27791da177e4SLinus Torvalds 27801da177e4SLinus Torvalds len = PTR_ERR(link); 27811da177e4SLinus Torvalds if (IS_ERR(link)) 27821da177e4SLinus Torvalds goto out; 27831da177e4SLinus Torvalds 27841da177e4SLinus Torvalds len = strlen(link); 27851da177e4SLinus Torvalds if (len > (unsigned) buflen) 27861da177e4SLinus Torvalds len = buflen; 27871da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 27881da177e4SLinus Torvalds len = -EFAULT; 27891da177e4SLinus Torvalds out: 27901da177e4SLinus Torvalds return len; 27911da177e4SLinus Torvalds } 27921da177e4SLinus Torvalds 27931da177e4SLinus Torvalds /* 27941da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 27951da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 27961da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 27971da177e4SLinus Torvalds */ 27981da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 27991da177e4SLinus Torvalds { 28001da177e4SLinus Torvalds struct nameidata nd; 2801cc314eefSLinus Torvalds void *cookie; 2802694a1764SMarcin Slusarz int res; 2803cc314eefSLinus Torvalds 28041da177e4SLinus Torvalds nd.depth = 0; 2805cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 2806694a1764SMarcin Slusarz if (IS_ERR(cookie)) 2807694a1764SMarcin Slusarz return PTR_ERR(cookie); 2808694a1764SMarcin Slusarz 2809694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 28101da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 2811cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 2812694a1764SMarcin Slusarz return res; 28131da177e4SLinus Torvalds } 28141da177e4SLinus Torvalds 28151da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 28161da177e4SLinus Torvalds { 28171da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 28181da177e4SLinus Torvalds } 28191da177e4SLinus Torvalds 28201da177e4SLinus Torvalds /* get the link contents into pagecache */ 28211da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 28221da177e4SLinus Torvalds { 2823ebd09abbSDuane Griffin char *kaddr; 28241da177e4SLinus Torvalds struct page *page; 28251da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 2826090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 28271da177e4SLinus Torvalds if (IS_ERR(page)) 28286fe6900eSNick Piggin return (char*)page; 28291da177e4SLinus Torvalds *ppage = page; 2830ebd09abbSDuane Griffin kaddr = kmap(page); 2831ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 2832ebd09abbSDuane Griffin return kaddr; 28331da177e4SLinus Torvalds } 28341da177e4SLinus Torvalds 28351da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 28361da177e4SLinus Torvalds { 28371da177e4SLinus Torvalds struct page *page = NULL; 28381da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 28391da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 28401da177e4SLinus Torvalds if (page) { 28411da177e4SLinus Torvalds kunmap(page); 28421da177e4SLinus Torvalds page_cache_release(page); 28431da177e4SLinus Torvalds } 28441da177e4SLinus Torvalds return res; 28451da177e4SLinus Torvalds } 28461da177e4SLinus Torvalds 2847cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 28481da177e4SLinus Torvalds { 2849cc314eefSLinus Torvalds struct page *page = NULL; 28501da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 2851cc314eefSLinus Torvalds return page; 28521da177e4SLinus Torvalds } 28531da177e4SLinus Torvalds 2854cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 28551da177e4SLinus Torvalds { 2856cc314eefSLinus Torvalds struct page *page = cookie; 2857cc314eefSLinus Torvalds 2858cc314eefSLinus Torvalds if (page) { 28591da177e4SLinus Torvalds kunmap(page); 28601da177e4SLinus Torvalds page_cache_release(page); 28611da177e4SLinus Torvalds } 28621da177e4SLinus Torvalds } 28631da177e4SLinus Torvalds 286454566b2cSNick Piggin /* 286554566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 286654566b2cSNick Piggin */ 286754566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 28681da177e4SLinus Torvalds { 28691da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 28700adb25d2SKirill Korotaev struct page *page; 2871afddba49SNick Piggin void *fsdata; 2872beb497abSDmitriy Monakhov int err; 28731da177e4SLinus Torvalds char *kaddr; 287454566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 287554566b2cSNick Piggin if (nofs) 287654566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 28771da177e4SLinus Torvalds 28787e53cac4SNeilBrown retry: 2879afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 288054566b2cSNick Piggin flags, &page, &fsdata); 28811da177e4SLinus Torvalds if (err) 2882afddba49SNick Piggin goto fail; 2883afddba49SNick Piggin 28841da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 28851da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 28861da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 2887afddba49SNick Piggin 2888afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 2889afddba49SNick Piggin page, fsdata); 28901da177e4SLinus Torvalds if (err < 0) 28911da177e4SLinus Torvalds goto fail; 2892afddba49SNick Piggin if (err < len-1) 2893afddba49SNick Piggin goto retry; 2894afddba49SNick Piggin 28951da177e4SLinus Torvalds mark_inode_dirty(inode); 28961da177e4SLinus Torvalds return 0; 28971da177e4SLinus Torvalds fail: 28981da177e4SLinus Torvalds return err; 28991da177e4SLinus Torvalds } 29001da177e4SLinus Torvalds 29010adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 29020adb25d2SKirill Korotaev { 29030adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 290454566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 29050adb25d2SKirill Korotaev } 29060adb25d2SKirill Korotaev 290792e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 29081da177e4SLinus Torvalds .readlink = generic_readlink, 29091da177e4SLinus Torvalds .follow_link = page_follow_link_light, 29101da177e4SLinus Torvalds .put_link = page_put_link, 29111da177e4SLinus Torvalds }; 29121da177e4SLinus Torvalds 29132d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 29141da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 29151da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 29161da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 29171da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 29181da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 29191da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 29201da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 29211da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 29221da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 29230adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 29241da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 29251da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 29261da177e4SLinus Torvalds EXPORT_SYMBOL(path_lookup); 2927d1811465SAl Viro EXPORT_SYMBOL(kern_path); 292816f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 2929f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 29308c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission); 29311da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 29321da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 29331da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 29341da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 29351da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 29361da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 29371da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 29381da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 29391da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 29401da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 29411da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 29421da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 29431da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 29441da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 2945