11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/namei.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* 81da177e4SLinus Torvalds * Some corrections by tytso. 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname 121da177e4SLinus Torvalds * lookup logic. 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture. 151da177e4SLinus Torvalds */ 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #include <linux/init.h> 181da177e4SLinus Torvalds #include <linux/module.h> 191da177e4SLinus Torvalds #include <linux/slab.h> 201da177e4SLinus Torvalds #include <linux/fs.h> 211da177e4SLinus Torvalds #include <linux/namei.h> 221da177e4SLinus Torvalds #include <linux/quotaops.h> 231da177e4SLinus Torvalds #include <linux/pagemap.h> 240eeca283SRobert Love #include <linux/fsnotify.h> 251da177e4SLinus Torvalds #include <linux/personality.h> 261da177e4SLinus Torvalds #include <linux/security.h> 276146f0d5SMimi Zohar #include <linux/ima.h> 281da177e4SLinus Torvalds #include <linux/syscalls.h> 291da177e4SLinus Torvalds #include <linux/mount.h> 301da177e4SLinus Torvalds #include <linux/audit.h> 3116f7e0feSRandy Dunlap #include <linux/capability.h> 32834f2a4aSTrond Myklebust #include <linux/file.h> 335590ff0dSUlrich Drepper #include <linux/fcntl.h> 3408ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h> 355ad4e53bSAl Viro #include <linux/fs_struct.h> 361da177e4SLinus Torvalds #include <asm/uaccess.h> 371da177e4SLinus Torvalds 38e81e3f4dSEric Paris #include "internal.h" 39e81e3f4dSEric Paris 401da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer] 411da177e4SLinus Torvalds * Fundamental changes in the pathname lookup mechanisms (namei) 421da177e4SLinus Torvalds * were necessary because of omirr. The reason is that omirr needs 431da177e4SLinus Torvalds * to know the _real_ pathname, not the user-supplied one, in case 441da177e4SLinus Torvalds * of symlinks (and also when transname replacements occur). 451da177e4SLinus Torvalds * 461da177e4SLinus Torvalds * The new code replaces the old recursive symlink resolution with 471da177e4SLinus Torvalds * an iterative one (in case of non-nested symlink chains). It does 481da177e4SLinus Torvalds * this with calls to <fs>_follow_link(). 491da177e4SLinus Torvalds * As a side effect, dir_namei(), _namei() and follow_link() are now 501da177e4SLinus Torvalds * replaced with a single function lookup_dentry() that can handle all 511da177e4SLinus Torvalds * the special cases of the former code. 521da177e4SLinus Torvalds * 531da177e4SLinus Torvalds * With the new dcache, the pathname is stored at each inode, at least as 541da177e4SLinus Torvalds * long as the refcount of the inode is positive. As a side effect, the 551da177e4SLinus Torvalds * size of the dcache depends on the inode cache and thus is dynamic. 561da177e4SLinus Torvalds * 571da177e4SLinus Torvalds * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink 581da177e4SLinus Torvalds * resolution to correspond with current state of the code. 591da177e4SLinus Torvalds * 601da177e4SLinus Torvalds * Note that the symlink resolution is not *completely* iterative. 611da177e4SLinus Torvalds * There is still a significant amount of tail- and mid- recursion in 621da177e4SLinus Torvalds * the algorithm. Also, note that <fs>_readlink() is not used in 631da177e4SLinus Torvalds * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() 641da177e4SLinus Torvalds * may return different results than <fs>_follow_link(). Many virtual 651da177e4SLinus Torvalds * filesystems (including /proc) exhibit this behavior. 661da177e4SLinus Torvalds */ 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: 691da177e4SLinus Torvalds * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL 701da177e4SLinus Torvalds * and the name already exists in form of a symlink, try to create the new 711da177e4SLinus Torvalds * name indicated by the symlink. The old code always complained that the 721da177e4SLinus Torvalds * name already exists, due to not following the symlink even if its target 731da177e4SLinus Torvalds * is nonexistent. The new semantics affects also mknod() and link() when 741da177e4SLinus Torvalds * the name is a symlink pointing to a non-existant name. 751da177e4SLinus Torvalds * 761da177e4SLinus Torvalds * I don't know which semantics is the right one, since I have no access 771da177e4SLinus Torvalds * to standards. But I found by trial that HP-UX 9.0 has the full "new" 781da177e4SLinus Torvalds * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the 791da177e4SLinus Torvalds * "old" one. Personally, I think the new semantics is much more logical. 801da177e4SLinus Torvalds * Note that "ln old new" where "new" is a symlink pointing to a non-existing 811da177e4SLinus Torvalds * file does succeed in both HP-UX and SunOs, but not in Solaris 821da177e4SLinus Torvalds * and in the old Linux semantics. 831da177e4SLinus Torvalds */ 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink 861da177e4SLinus Torvalds * semantics. See the comments in "open_namei" and "do_link" below. 871da177e4SLinus Torvalds * 881da177e4SLinus Torvalds * [10-Sep-98 Alan Modra] Another symlink change. 891da177e4SLinus Torvalds */ 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: 921da177e4SLinus Torvalds * inside the path - always follow. 931da177e4SLinus Torvalds * in the last component in creation/removal/renaming - never follow. 941da177e4SLinus Torvalds * if LOOKUP_FOLLOW passed - follow. 951da177e4SLinus Torvalds * if the pathname has trailing slashes - follow. 961da177e4SLinus Torvalds * otherwise - don't follow. 971da177e4SLinus Torvalds * (applied in that order). 981da177e4SLinus Torvalds * 991da177e4SLinus Torvalds * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT 1001da177e4SLinus Torvalds * restored for 2.4. This is the last surviving part of old 4.2BSD bug. 1011da177e4SLinus Torvalds * During the 2.4 we need to fix the userland stuff depending on it - 1021da177e4SLinus Torvalds * hopefully we will be able to get rid of that wart in 2.5. So far only 1031da177e4SLinus Torvalds * XEmacs seems to be relying on it... 1041da177e4SLinus Torvalds */ 1051da177e4SLinus Torvalds /* 1061da177e4SLinus Torvalds * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) 107a11f3a05SArjan van de Ven * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives 1081da177e4SLinus Torvalds * any extra contention... 1091da177e4SLinus Torvalds */ 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional 1121da177e4SLinus Torvalds * checking and hopefully speeding things up, we copy filenames to the 1131da177e4SLinus Torvalds * kernel data space before using them.. 1141da177e4SLinus Torvalds * 1151da177e4SLinus Torvalds * POSIX.1 2.4: an empty pathname is invalid (ENOENT). 1161da177e4SLinus Torvalds * PATH_MAX includes the nul terminator --RR. 1171da177e4SLinus Torvalds */ 118858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page) 1191da177e4SLinus Torvalds { 1201da177e4SLinus Torvalds int retval; 1211da177e4SLinus Torvalds unsigned long len = PATH_MAX; 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds if (!segment_eq(get_fs(), KERNEL_DS)) { 1241da177e4SLinus Torvalds if ((unsigned long) filename >= TASK_SIZE) 1251da177e4SLinus Torvalds return -EFAULT; 1261da177e4SLinus Torvalds if (TASK_SIZE - (unsigned long) filename < PATH_MAX) 1271da177e4SLinus Torvalds len = TASK_SIZE - (unsigned long) filename; 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds retval = strncpy_from_user(page, filename, len); 1311da177e4SLinus Torvalds if (retval > 0) { 1321da177e4SLinus Torvalds if (retval < len) 1331da177e4SLinus Torvalds return 0; 1341da177e4SLinus Torvalds return -ENAMETOOLONG; 1351da177e4SLinus Torvalds } else if (!retval) 1361da177e4SLinus Torvalds retval = -ENOENT; 1371da177e4SLinus Torvalds return retval; 1381da177e4SLinus Torvalds } 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds char * getname(const char __user * filename) 1411da177e4SLinus Torvalds { 1421da177e4SLinus Torvalds char *tmp, *result; 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds result = ERR_PTR(-ENOMEM); 1451da177e4SLinus Torvalds tmp = __getname(); 1461da177e4SLinus Torvalds if (tmp) { 1471da177e4SLinus Torvalds int retval = do_getname(filename, tmp); 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds result = tmp; 1501da177e4SLinus Torvalds if (retval < 0) { 1511da177e4SLinus Torvalds __putname(tmp); 1521da177e4SLinus Torvalds result = ERR_PTR(retval); 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds } 1551da177e4SLinus Torvalds audit_getname(result); 1561da177e4SLinus Torvalds return result; 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 1601da177e4SLinus Torvalds void putname(const char *name) 1611da177e4SLinus Torvalds { 1625ac3a9c2SAl Viro if (unlikely(!audit_dummy_context())) 1631da177e4SLinus Torvalds audit_putname(name); 1641da177e4SLinus Torvalds else 1651da177e4SLinus Torvalds __putname(name); 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds EXPORT_SYMBOL(putname); 1681da177e4SLinus Torvalds #endif 1691da177e4SLinus Torvalds 1705909ccaaSLinus Torvalds /* 1715909ccaaSLinus Torvalds * This does basic POSIX ACL permission checking 1725909ccaaSLinus Torvalds */ 1735909ccaaSLinus Torvalds static int acl_permission_check(struct inode *inode, int mask, 1745909ccaaSLinus Torvalds int (*check_acl)(struct inode *inode, int mask)) 1755909ccaaSLinus Torvalds { 1765909ccaaSLinus Torvalds umode_t mode = inode->i_mode; 1775909ccaaSLinus Torvalds 1785909ccaaSLinus Torvalds mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 1795909ccaaSLinus Torvalds 1805909ccaaSLinus Torvalds if (current_fsuid() == inode->i_uid) 1815909ccaaSLinus Torvalds mode >>= 6; 1825909ccaaSLinus Torvalds else { 1835909ccaaSLinus Torvalds if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) { 1845909ccaaSLinus Torvalds int error = check_acl(inode, mask); 1855909ccaaSLinus Torvalds if (error != -EAGAIN) 1865909ccaaSLinus Torvalds return error; 1875909ccaaSLinus Torvalds } 1885909ccaaSLinus Torvalds 1895909ccaaSLinus Torvalds if (in_group_p(inode->i_gid)) 1905909ccaaSLinus Torvalds mode >>= 3; 1915909ccaaSLinus Torvalds } 1925909ccaaSLinus Torvalds 1935909ccaaSLinus Torvalds /* 1945909ccaaSLinus Torvalds * If the DACs are ok we don't need any capability check. 1955909ccaaSLinus Torvalds */ 1965909ccaaSLinus Torvalds if ((mask & ~mode) == 0) 1975909ccaaSLinus Torvalds return 0; 1985909ccaaSLinus Torvalds return -EACCES; 1995909ccaaSLinus Torvalds } 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds /** 2021da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 2031da177e4SLinus Torvalds * @inode: inode to check access rights for 2041da177e4SLinus Torvalds * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 2051da177e4SLinus Torvalds * @check_acl: optional callback to check for Posix ACLs 2061da177e4SLinus Torvalds * 2071da177e4SLinus Torvalds * Used to check for read/write/execute permissions on a file. 2081da177e4SLinus Torvalds * We use "fsuid" for this, letting us set arbitrary permissions 2091da177e4SLinus Torvalds * for filesystem access without changing the "normal" uids which 2101da177e4SLinus Torvalds * are used for other things.. 2111da177e4SLinus Torvalds */ 2121da177e4SLinus Torvalds int generic_permission(struct inode *inode, int mask, 2131da177e4SLinus Torvalds int (*check_acl)(struct inode *inode, int mask)) 2141da177e4SLinus Torvalds { 2155909ccaaSLinus Torvalds int ret; 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds /* 2185909ccaaSLinus Torvalds * Do the basic POSIX ACL permission checks. 2191da177e4SLinus Torvalds */ 2205909ccaaSLinus Torvalds ret = acl_permission_check(inode, mask, check_acl); 2215909ccaaSLinus Torvalds if (ret != -EACCES) 2225909ccaaSLinus Torvalds return ret; 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds /* 2251da177e4SLinus Torvalds * Read/write DACs are always overridable. 2261da177e4SLinus Torvalds * Executable DACs are overridable if at least one exec bit is set. 2271da177e4SLinus Torvalds */ 228f696a365SMiklos Szeredi if (!(mask & MAY_EXEC) || execute_ok(inode)) 2291da177e4SLinus Torvalds if (capable(CAP_DAC_OVERRIDE)) 2301da177e4SLinus Torvalds return 0; 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds /* 2331da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 2341da177e4SLinus Torvalds */ 2357ea66001SSerge E. Hallyn mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 2361da177e4SLinus Torvalds if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE))) 2371da177e4SLinus Torvalds if (capable(CAP_DAC_READ_SEARCH)) 2381da177e4SLinus Torvalds return 0; 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds return -EACCES; 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds 243cb23beb5SChristoph Hellwig /** 244cb23beb5SChristoph Hellwig * inode_permission - check for access rights to a given inode 245cb23beb5SChristoph Hellwig * @inode: inode to check permission on 246cb23beb5SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 247cb23beb5SChristoph Hellwig * 248cb23beb5SChristoph Hellwig * Used to check for read/write/execute permissions on an inode. 249cb23beb5SChristoph Hellwig * We use "fsuid" for this, letting us set arbitrary permissions 250cb23beb5SChristoph Hellwig * for filesystem access without changing the "normal" uids which 251cb23beb5SChristoph Hellwig * are used for other things. 252cb23beb5SChristoph Hellwig */ 253f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask) 2541da177e4SLinus Torvalds { 255e6305c43SAl Viro int retval; 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds if (mask & MAY_WRITE) { 25822590e41SMiklos Szeredi umode_t mode = inode->i_mode; 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds /* 2611da177e4SLinus Torvalds * Nobody gets write access to a read-only fs. 2621da177e4SLinus Torvalds */ 2631da177e4SLinus Torvalds if (IS_RDONLY(inode) && 2641da177e4SLinus Torvalds (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 2651da177e4SLinus Torvalds return -EROFS; 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds /* 2681da177e4SLinus Torvalds * Nobody gets write access to an immutable file. 2691da177e4SLinus Torvalds */ 2701da177e4SLinus Torvalds if (IS_IMMUTABLE(inode)) 2711da177e4SLinus Torvalds return -EACCES; 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds 274acfa4380SAl Viro if (inode->i_op->permission) 275b77b0646SAl Viro retval = inode->i_op->permission(inode, mask); 276f696a365SMiklos Szeredi else 2775909ccaaSLinus Torvalds retval = generic_permission(inode, mask, inode->i_op->check_acl); 278f696a365SMiklos Szeredi 2791da177e4SLinus Torvalds if (retval) 2801da177e4SLinus Torvalds return retval; 2811da177e4SLinus Torvalds 28208ce5f16SSerge E. Hallyn retval = devcgroup_inode_permission(inode, mask); 28308ce5f16SSerge E. Hallyn if (retval) 28408ce5f16SSerge E. Hallyn return retval; 28508ce5f16SSerge E. Hallyn 286e6305c43SAl Viro return security_inode_permission(inode, 287f418b006SStephen Smalley mask & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND)); 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds 290e4543eddSChristoph Hellwig /** 2918c744fb8SChristoph Hellwig * file_permission - check for additional access rights to a given file 2928c744fb8SChristoph Hellwig * @file: file to check access rights for 2938c744fb8SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 2948c744fb8SChristoph Hellwig * 2958c744fb8SChristoph Hellwig * Used to check for read/write/execute permissions on an already opened 2968c744fb8SChristoph Hellwig * file. 2978c744fb8SChristoph Hellwig * 2988c744fb8SChristoph Hellwig * Note: 2998c744fb8SChristoph Hellwig * Do not use this function in new code. All access checks should 300cb23beb5SChristoph Hellwig * be done using inode_permission(). 3018c744fb8SChristoph Hellwig */ 3028c744fb8SChristoph Hellwig int file_permission(struct file *file, int mask) 3038c744fb8SChristoph Hellwig { 304f419a2e3SAl Viro return inode_permission(file->f_path.dentry->d_inode, mask); 3058c744fb8SChristoph Hellwig } 3068c744fb8SChristoph Hellwig 3071da177e4SLinus Torvalds /* 3081da177e4SLinus Torvalds * get_write_access() gets write permission for a file. 3091da177e4SLinus Torvalds * put_write_access() releases this write permission. 3101da177e4SLinus Torvalds * This is used for regular files. 3111da177e4SLinus Torvalds * We cannot support write (and maybe mmap read-write shared) accesses and 3121da177e4SLinus Torvalds * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode 3131da177e4SLinus Torvalds * can have the following values: 3141da177e4SLinus Torvalds * 0: no writers, no VM_DENYWRITE mappings 3151da177e4SLinus Torvalds * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist 3161da177e4SLinus Torvalds * > 0: (i_writecount) users are writing to the file. 3171da177e4SLinus Torvalds * 3181da177e4SLinus Torvalds * Normally we operate on that counter with atomic_{inc,dec} and it's safe 3191da177e4SLinus Torvalds * except for the cases where we don't hold i_writecount yet. Then we need to 3201da177e4SLinus Torvalds * use {get,deny}_write_access() - these functions check the sign and refuse 3211da177e4SLinus Torvalds * to do the change if sign is wrong. Exclusion between them is provided by 3221da177e4SLinus Torvalds * the inode->i_lock spinlock. 3231da177e4SLinus Torvalds */ 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds int get_write_access(struct inode * inode) 3261da177e4SLinus Torvalds { 3271da177e4SLinus Torvalds spin_lock(&inode->i_lock); 3281da177e4SLinus Torvalds if (atomic_read(&inode->i_writecount) < 0) { 3291da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3301da177e4SLinus Torvalds return -ETXTBSY; 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds atomic_inc(&inode->i_writecount); 3331da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds return 0; 3361da177e4SLinus Torvalds } 3371da177e4SLinus Torvalds 3381da177e4SLinus Torvalds int deny_write_access(struct file * file) 3391da177e4SLinus Torvalds { 3400f7fc9e4SJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds spin_lock(&inode->i_lock); 3431da177e4SLinus Torvalds if (atomic_read(&inode->i_writecount) > 0) { 3441da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3451da177e4SLinus Torvalds return -ETXTBSY; 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds atomic_dec(&inode->i_writecount); 3481da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds return 0; 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds 3531d957f9bSJan Blunck /** 3545dd784d0SJan Blunck * path_get - get a reference to a path 3555dd784d0SJan Blunck * @path: path to get the reference to 3565dd784d0SJan Blunck * 3575dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 3585dd784d0SJan Blunck */ 3595dd784d0SJan Blunck void path_get(struct path *path) 3605dd784d0SJan Blunck { 3615dd784d0SJan Blunck mntget(path->mnt); 3625dd784d0SJan Blunck dget(path->dentry); 3635dd784d0SJan Blunck } 3645dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 3655dd784d0SJan Blunck 3665dd784d0SJan Blunck /** 3671d957f9bSJan Blunck * path_put - put a reference to a path 3681d957f9bSJan Blunck * @path: path to put the reference to 3691d957f9bSJan Blunck * 3701d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 3711d957f9bSJan Blunck */ 3721d957f9bSJan Blunck void path_put(struct path *path) 3731da177e4SLinus Torvalds { 3741d957f9bSJan Blunck dput(path->dentry); 3751d957f9bSJan Blunck mntput(path->mnt); 3761da177e4SLinus Torvalds } 3771d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 3781da177e4SLinus Torvalds 379834f2a4aSTrond Myklebust /** 380834f2a4aSTrond Myklebust * release_open_intent - free up open intent resources 381834f2a4aSTrond Myklebust * @nd: pointer to nameidata 382834f2a4aSTrond Myklebust */ 383834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd) 384834f2a4aSTrond Myklebust { 3850f7fc9e4SJosef "Jeff" Sipek if (nd->intent.open.file->f_path.dentry == NULL) 386834f2a4aSTrond Myklebust put_filp(nd->intent.open.file); 387834f2a4aSTrond Myklebust else 388834f2a4aSTrond Myklebust fput(nd->intent.open.file); 389834f2a4aSTrond Myklebust } 390834f2a4aSTrond Myklebust 391bcdc5e01SIan Kent static inline struct dentry * 392bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd) 393bcdc5e01SIan Kent { 394bcdc5e01SIan Kent int status = dentry->d_op->d_revalidate(dentry, nd); 395bcdc5e01SIan Kent if (unlikely(status <= 0)) { 396bcdc5e01SIan Kent /* 397bcdc5e01SIan Kent * The dentry failed validation. 398bcdc5e01SIan Kent * If d_revalidate returned 0 attempt to invalidate 399bcdc5e01SIan Kent * the dentry otherwise d_revalidate is asking us 400bcdc5e01SIan Kent * to return a fail status. 401bcdc5e01SIan Kent */ 402bcdc5e01SIan Kent if (!status) { 403bcdc5e01SIan Kent if (!d_invalidate(dentry)) { 404bcdc5e01SIan Kent dput(dentry); 405bcdc5e01SIan Kent dentry = NULL; 406bcdc5e01SIan Kent } 407bcdc5e01SIan Kent } else { 408bcdc5e01SIan Kent dput(dentry); 409bcdc5e01SIan Kent dentry = ERR_PTR(status); 410bcdc5e01SIan Kent } 411bcdc5e01SIan Kent } 412bcdc5e01SIan Kent return dentry; 413bcdc5e01SIan Kent } 414bcdc5e01SIan Kent 4151da177e4SLinus Torvalds /* 41639159de2SJeff Layton * force_reval_path - force revalidation of a dentry 41739159de2SJeff Layton * 41839159de2SJeff Layton * In some situations the path walking code will trust dentries without 41939159de2SJeff Layton * revalidating them. This causes problems for filesystems that depend on 42039159de2SJeff Layton * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set 42139159de2SJeff Layton * (which indicates that it's possible for the dentry to go stale), force 42239159de2SJeff Layton * a d_revalidate call before proceeding. 42339159de2SJeff Layton * 42439159de2SJeff Layton * Returns 0 if the revalidation was successful. If the revalidation fails, 42539159de2SJeff Layton * either return the error returned by d_revalidate or -ESTALE if the 42639159de2SJeff Layton * revalidation it just returned 0. If d_revalidate returns 0, we attempt to 42739159de2SJeff Layton * invalidate the dentry. It's up to the caller to handle putting references 42839159de2SJeff Layton * to the path if necessary. 42939159de2SJeff Layton */ 43039159de2SJeff Layton static int 43139159de2SJeff Layton force_reval_path(struct path *path, struct nameidata *nd) 43239159de2SJeff Layton { 43339159de2SJeff Layton int status; 43439159de2SJeff Layton struct dentry *dentry = path->dentry; 43539159de2SJeff Layton 43639159de2SJeff Layton /* 43739159de2SJeff Layton * only check on filesystems where it's possible for the dentry to 43839159de2SJeff Layton * become stale. It's assumed that if this flag is set then the 43939159de2SJeff Layton * d_revalidate op will also be defined. 44039159de2SJeff Layton */ 44139159de2SJeff Layton if (!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) 44239159de2SJeff Layton return 0; 44339159de2SJeff Layton 44439159de2SJeff Layton status = dentry->d_op->d_revalidate(dentry, nd); 44539159de2SJeff Layton if (status > 0) 44639159de2SJeff Layton return 0; 44739159de2SJeff Layton 44839159de2SJeff Layton if (!status) { 44939159de2SJeff Layton d_invalidate(dentry); 45039159de2SJeff Layton status = -ESTALE; 45139159de2SJeff Layton } 45239159de2SJeff Layton return status; 45339159de2SJeff Layton } 45439159de2SJeff Layton 45539159de2SJeff Layton /* 456b75b5086SAl Viro * Short-cut version of permission(), for calling on directories 457b75b5086SAl Viro * during pathname resolution. Combines parts of permission() 458b75b5086SAl Viro * and generic_permission(), and tests ONLY for MAY_EXEC permission. 4591da177e4SLinus Torvalds * 4601da177e4SLinus Torvalds * If appropriate, check DAC only. If not appropriate, or 461b75b5086SAl Viro * short-cut DAC fails, then call ->permission() to do more 4621da177e4SLinus Torvalds * complete permission check. 4631da177e4SLinus Torvalds */ 464b75b5086SAl Viro static int exec_permission(struct inode *inode) 4651da177e4SLinus Torvalds { 4665909ccaaSLinus Torvalds int ret; 4671da177e4SLinus Torvalds 468cb9179eaSLinus Torvalds if (inode->i_op->permission) { 4695909ccaaSLinus Torvalds ret = inode->i_op->permission(inode, MAY_EXEC); 470cb9179eaSLinus Torvalds if (!ret) 471cb9179eaSLinus Torvalds goto ok; 472cb9179eaSLinus Torvalds return ret; 473cb9179eaSLinus Torvalds } 4745909ccaaSLinus Torvalds ret = acl_permission_check(inode, MAY_EXEC, inode->i_op->check_acl); 4755909ccaaSLinus Torvalds if (!ret) 4761da177e4SLinus Torvalds goto ok; 4771da177e4SLinus Torvalds 478f1ac9f6bSLinus Torvalds if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH)) 4791da177e4SLinus Torvalds goto ok; 4801da177e4SLinus Torvalds 4815909ccaaSLinus Torvalds return ret; 4821da177e4SLinus Torvalds ok: 483b77b0646SAl Viro return security_inode_permission(inode, MAY_EXEC); 4841da177e4SLinus Torvalds } 4851da177e4SLinus Torvalds 4862a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 4872a737871SAl Viro { 4882a737871SAl Viro if (!nd->root.mnt) { 4892a737871SAl Viro struct fs_struct *fs = current->fs; 4902a737871SAl Viro read_lock(&fs->lock); 4912a737871SAl Viro nd->root = fs->root; 4922a737871SAl Viro path_get(&nd->root); 4932a737871SAl Viro read_unlock(&fs->lock); 4942a737871SAl Viro } 4952a737871SAl Viro } 4962a737871SAl Viro 4976de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 4986de88d72SAl Viro 499f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 5001da177e4SLinus Torvalds { 5011da177e4SLinus Torvalds int res = 0; 5021da177e4SLinus Torvalds char *name; 5031da177e4SLinus Torvalds if (IS_ERR(link)) 5041da177e4SLinus Torvalds goto fail; 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds if (*link == '/') { 5072a737871SAl Viro set_root(nd); 5081d957f9bSJan Blunck path_put(&nd->path); 5092a737871SAl Viro nd->path = nd->root; 5102a737871SAl Viro path_get(&nd->root); 5111da177e4SLinus Torvalds } 512b4091d5fSChristoph Hellwig 5131da177e4SLinus Torvalds res = link_path_walk(link, nd); 5141da177e4SLinus Torvalds if (nd->depth || res || nd->last_type!=LAST_NORM) 5151da177e4SLinus Torvalds return res; 5161da177e4SLinus Torvalds /* 5171da177e4SLinus Torvalds * If it is an iterative symlinks resolution in open_namei() we 5181da177e4SLinus Torvalds * have to copy the last component. And all that crap because of 5191da177e4SLinus Torvalds * bloody create() on broken symlinks. Furrfu... 5201da177e4SLinus Torvalds */ 5211da177e4SLinus Torvalds name = __getname(); 5221da177e4SLinus Torvalds if (unlikely(!name)) { 5231d957f9bSJan Blunck path_put(&nd->path); 5241da177e4SLinus Torvalds return -ENOMEM; 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds strcpy(name, nd->last.name); 5271da177e4SLinus Torvalds nd->last.name = name; 5281da177e4SLinus Torvalds return 0; 5291da177e4SLinus Torvalds fail: 5301d957f9bSJan Blunck path_put(&nd->path); 5311da177e4SLinus Torvalds return PTR_ERR(link); 5321da177e4SLinus Torvalds } 5331da177e4SLinus Torvalds 5341d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 535051d3812SIan Kent { 536051d3812SIan Kent dput(path->dentry); 5374ac91378SJan Blunck if (path->mnt != nd->path.mnt) 538051d3812SIan Kent mntput(path->mnt); 539051d3812SIan Kent } 540051d3812SIan Kent 541051d3812SIan Kent static inline void path_to_nameidata(struct path *path, struct nameidata *nd) 542051d3812SIan Kent { 5434ac91378SJan Blunck dput(nd->path.dentry); 5444ac91378SJan Blunck if (nd->path.mnt != path->mnt) 5454ac91378SJan Blunck mntput(nd->path.mnt); 5464ac91378SJan Blunck nd->path.mnt = path->mnt; 5474ac91378SJan Blunck nd->path.dentry = path->dentry; 548051d3812SIan Kent } 549051d3812SIan Kent 550f1662356SArjan van de Ven static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd) 5511da177e4SLinus Torvalds { 5521da177e4SLinus Torvalds int error; 553cc314eefSLinus Torvalds void *cookie; 554cd4e91d3SAl Viro struct dentry *dentry = path->dentry; 5551da177e4SLinus Torvalds 556d671a1cbSAl Viro touch_atime(path->mnt, dentry); 5571da177e4SLinus Torvalds nd_set_link(nd, NULL); 558cd4e91d3SAl Viro 5594ac91378SJan Blunck if (path->mnt != nd->path.mnt) { 560051d3812SIan Kent path_to_nameidata(path, nd); 561051d3812SIan Kent dget(dentry); 562051d3812SIan Kent } 563cd4e91d3SAl Viro mntget(path->mnt); 56486acdca1SAl Viro nd->last_type = LAST_BIND; 565cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, nd); 566cc314eefSLinus Torvalds error = PTR_ERR(cookie); 567cc314eefSLinus Torvalds if (!IS_ERR(cookie)) { 5681da177e4SLinus Torvalds char *s = nd_get_link(nd); 569cc314eefSLinus Torvalds error = 0; 5701da177e4SLinus Torvalds if (s) 5711da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 57239159de2SJeff Layton else if (nd->last_type == LAST_BIND) { 57339159de2SJeff Layton error = force_reval_path(&nd->path, nd); 57439159de2SJeff Layton if (error) 57539159de2SJeff Layton path_put(&nd->path); 57639159de2SJeff Layton } 5771da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 578cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, nd, cookie); 5791da177e4SLinus Torvalds } 5801da177e4SLinus Torvalds return error; 5811da177e4SLinus Torvalds } 5821da177e4SLinus Torvalds 5831da177e4SLinus Torvalds /* 5841da177e4SLinus Torvalds * This limits recursive symlink follows to 8, while 5851da177e4SLinus Torvalds * limiting consecutive symlinks to 40. 5861da177e4SLinus Torvalds * 5871da177e4SLinus Torvalds * Without that kind of total limit, nasty chains of consecutive 5881da177e4SLinus Torvalds * symlinks can cause almost arbitrarily long lookups. 5891da177e4SLinus Torvalds */ 59090ebe565SAl Viro static inline int do_follow_link(struct path *path, struct nameidata *nd) 5911da177e4SLinus Torvalds { 5921da177e4SLinus Torvalds int err = -ELOOP; 5931da177e4SLinus Torvalds if (current->link_count >= MAX_NESTED_LINKS) 5941da177e4SLinus Torvalds goto loop; 5951da177e4SLinus Torvalds if (current->total_link_count >= 40) 5961da177e4SLinus Torvalds goto loop; 5971da177e4SLinus Torvalds BUG_ON(nd->depth >= MAX_NESTED_LINKS); 5981da177e4SLinus Torvalds cond_resched(); 59990ebe565SAl Viro err = security_inode_follow_link(path->dentry, nd); 6001da177e4SLinus Torvalds if (err) 6011da177e4SLinus Torvalds goto loop; 6021da177e4SLinus Torvalds current->link_count++; 6031da177e4SLinus Torvalds current->total_link_count++; 6041da177e4SLinus Torvalds nd->depth++; 605cd4e91d3SAl Viro err = __do_follow_link(path, nd); 606258fa999SAl Viro path_put(path); 6071da177e4SLinus Torvalds current->link_count--; 6081da177e4SLinus Torvalds nd->depth--; 6091da177e4SLinus Torvalds return err; 6101da177e4SLinus Torvalds loop: 6111d957f9bSJan Blunck path_put_conditional(path, nd); 6121d957f9bSJan Blunck path_put(&nd->path); 6131da177e4SLinus Torvalds return err; 6141da177e4SLinus Torvalds } 6151da177e4SLinus Torvalds 616bab77ebfSAl Viro int follow_up(struct path *path) 6171da177e4SLinus Torvalds { 6181da177e4SLinus Torvalds struct vfsmount *parent; 6191da177e4SLinus Torvalds struct dentry *mountpoint; 6201da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 621bab77ebfSAl Viro parent = path->mnt->mnt_parent; 622bab77ebfSAl Viro if (parent == path->mnt) { 6231da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 6241da177e4SLinus Torvalds return 0; 6251da177e4SLinus Torvalds } 6261da177e4SLinus Torvalds mntget(parent); 627bab77ebfSAl Viro mountpoint = dget(path->mnt->mnt_mountpoint); 6281da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 629bab77ebfSAl Viro dput(path->dentry); 630bab77ebfSAl Viro path->dentry = mountpoint; 631bab77ebfSAl Viro mntput(path->mnt); 632bab77ebfSAl Viro path->mnt = parent; 6331da177e4SLinus Torvalds return 1; 6341da177e4SLinus Torvalds } 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds /* no need for dcache_lock, as serialization is taken care in 6371da177e4SLinus Torvalds * namespace.c 6381da177e4SLinus Torvalds */ 639463ffb2eSAl Viro static int __follow_mount(struct path *path) 640463ffb2eSAl Viro { 641463ffb2eSAl Viro int res = 0; 642463ffb2eSAl Viro while (d_mountpoint(path->dentry)) { 6431c755af4SAl Viro struct vfsmount *mounted = lookup_mnt(path); 644463ffb2eSAl Viro if (!mounted) 645463ffb2eSAl Viro break; 646463ffb2eSAl Viro dput(path->dentry); 647463ffb2eSAl Viro if (res) 648463ffb2eSAl Viro mntput(path->mnt); 649463ffb2eSAl Viro path->mnt = mounted; 650463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 651463ffb2eSAl Viro res = 1; 652463ffb2eSAl Viro } 653463ffb2eSAl Viro return res; 654463ffb2eSAl Viro } 655463ffb2eSAl Viro 65679ed0226SAl Viro static void follow_mount(struct path *path) 6571da177e4SLinus Torvalds { 65879ed0226SAl Viro while (d_mountpoint(path->dentry)) { 6591c755af4SAl Viro struct vfsmount *mounted = lookup_mnt(path); 6601da177e4SLinus Torvalds if (!mounted) 6611da177e4SLinus Torvalds break; 66279ed0226SAl Viro dput(path->dentry); 66379ed0226SAl Viro mntput(path->mnt); 66479ed0226SAl Viro path->mnt = mounted; 66579ed0226SAl Viro path->dentry = dget(mounted->mnt_root); 6661da177e4SLinus Torvalds } 6671da177e4SLinus Torvalds } 6681da177e4SLinus Torvalds 6691da177e4SLinus Torvalds /* no need for dcache_lock, as serialization is taken care in 6701da177e4SLinus Torvalds * namespace.c 6711da177e4SLinus Torvalds */ 6729393bd07SAl Viro int follow_down(struct path *path) 6731da177e4SLinus Torvalds { 6741da177e4SLinus Torvalds struct vfsmount *mounted; 6751da177e4SLinus Torvalds 6761c755af4SAl Viro mounted = lookup_mnt(path); 6771da177e4SLinus Torvalds if (mounted) { 6789393bd07SAl Viro dput(path->dentry); 6799393bd07SAl Viro mntput(path->mnt); 6809393bd07SAl Viro path->mnt = mounted; 6819393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 6821da177e4SLinus Torvalds return 1; 6831da177e4SLinus Torvalds } 6841da177e4SLinus Torvalds return 0; 6851da177e4SLinus Torvalds } 6861da177e4SLinus Torvalds 687f1662356SArjan van de Ven static __always_inline void follow_dotdot(struct nameidata *nd) 6881da177e4SLinus Torvalds { 6892a737871SAl Viro set_root(nd); 690e518ddb7SAndreas Mohr 6911da177e4SLinus Torvalds while(1) { 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 } 6984ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 6993088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 7003088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 7011da177e4SLinus Torvalds dput(old); 7021da177e4SLinus Torvalds break; 7031da177e4SLinus Torvalds } 7043088dd70SAl Viro if (!follow_up(&nd->path)) 7051da177e4SLinus Torvalds break; 7061da177e4SLinus Torvalds } 70779ed0226SAl Viro follow_mount(&nd->path); 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds /* 7111da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 7121da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 7131da177e4SLinus Torvalds * It _is_ time-critical. 7141da177e4SLinus Torvalds */ 7151da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name, 7161da177e4SLinus Torvalds struct path *path) 7171da177e4SLinus Torvalds { 7184ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 7196e6b1bd1SAl Viro struct dentry *dentry, *parent; 7206e6b1bd1SAl Viro struct inode *dir; 7213cac260aSAl Viro /* 7223cac260aSAl Viro * See if the low-level filesystem might want 7233cac260aSAl Viro * to use its own hash.. 7243cac260aSAl Viro */ 7253cac260aSAl Viro if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) { 7263cac260aSAl Viro int err = nd->path.dentry->d_op->d_hash(nd->path.dentry, name); 7273cac260aSAl Viro if (err < 0) 7283cac260aSAl Viro return err; 7293cac260aSAl Viro } 7301da177e4SLinus Torvalds 7313cac260aSAl Viro dentry = __d_lookup(nd->path.dentry, name); 7321da177e4SLinus Torvalds if (!dentry) 7331da177e4SLinus Torvalds goto need_lookup; 7341da177e4SLinus Torvalds if (dentry->d_op && dentry->d_op->d_revalidate) 7351da177e4SLinus Torvalds goto need_revalidate; 7361da177e4SLinus Torvalds done: 7371da177e4SLinus Torvalds path->mnt = mnt; 7381da177e4SLinus Torvalds path->dentry = dentry; 739634ee701SAl Viro __follow_mount(path); 7401da177e4SLinus Torvalds return 0; 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds need_lookup: 7436e6b1bd1SAl Viro parent = nd->path.dentry; 7446e6b1bd1SAl Viro dir = parent->d_inode; 7456e6b1bd1SAl Viro 7466e6b1bd1SAl Viro mutex_lock(&dir->i_mutex); 7476e6b1bd1SAl Viro /* 7486e6b1bd1SAl Viro * First re-do the cached lookup just in case it was created 7496e6b1bd1SAl Viro * while we waited for the directory semaphore.. 7506e6b1bd1SAl Viro * 7516e6b1bd1SAl Viro * FIXME! This could use version numbering or similar to 7526e6b1bd1SAl Viro * avoid unnecessary cache lookups. 7536e6b1bd1SAl Viro * 7546e6b1bd1SAl Viro * The "dcache_lock" is purely to protect the RCU list walker 7556e6b1bd1SAl Viro * from concurrent renames at this point (we mustn't get false 7566e6b1bd1SAl Viro * negatives from the RCU list walk here, unlike the optimistic 7576e6b1bd1SAl Viro * fast walk). 7586e6b1bd1SAl Viro * 7596e6b1bd1SAl Viro * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup 7606e6b1bd1SAl Viro */ 7616e6b1bd1SAl Viro dentry = d_lookup(parent, name); 7626e6b1bd1SAl Viro if (!dentry) { 7636e6b1bd1SAl Viro struct dentry *new; 7646e6b1bd1SAl Viro 7656e6b1bd1SAl Viro /* Don't create child dentry for a dead directory. */ 7666e6b1bd1SAl Viro dentry = ERR_PTR(-ENOENT); 7676e6b1bd1SAl Viro if (IS_DEADDIR(dir)) 7686e6b1bd1SAl Viro goto out_unlock; 7696e6b1bd1SAl Viro 7706e6b1bd1SAl Viro new = d_alloc(parent, name); 7716e6b1bd1SAl Viro dentry = ERR_PTR(-ENOMEM); 7726e6b1bd1SAl Viro if (new) { 7736e6b1bd1SAl Viro dentry = dir->i_op->lookup(dir, new, nd); 7746e6b1bd1SAl Viro if (dentry) 7756e6b1bd1SAl Viro dput(new); 7766e6b1bd1SAl Viro else 7776e6b1bd1SAl Viro dentry = new; 7786e6b1bd1SAl Viro } 7796e6b1bd1SAl Viro out_unlock: 7806e6b1bd1SAl Viro mutex_unlock(&dir->i_mutex); 7816e6b1bd1SAl Viro if (IS_ERR(dentry)) 7826e6b1bd1SAl Viro goto fail; 7836e6b1bd1SAl Viro goto done; 7846e6b1bd1SAl Viro } 7856e6b1bd1SAl Viro 7866e6b1bd1SAl Viro /* 7876e6b1bd1SAl Viro * Uhhuh! Nasty case: the cache was re-populated while 7886e6b1bd1SAl Viro * we waited on the semaphore. Need to revalidate. 7896e6b1bd1SAl Viro */ 7906e6b1bd1SAl Viro mutex_unlock(&dir->i_mutex); 7916e6b1bd1SAl Viro if (dentry->d_op && dentry->d_op->d_revalidate) { 7926e6b1bd1SAl Viro dentry = do_revalidate(dentry, nd); 7936e6b1bd1SAl Viro if (!dentry) 7946e6b1bd1SAl Viro dentry = ERR_PTR(-ENOENT); 7956e6b1bd1SAl Viro } 7961da177e4SLinus Torvalds if (IS_ERR(dentry)) 7971da177e4SLinus Torvalds goto fail; 7981da177e4SLinus Torvalds goto done; 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds need_revalidate: 801bcdc5e01SIan Kent dentry = do_revalidate(dentry, nd); 802bcdc5e01SIan Kent if (!dentry) 8031da177e4SLinus Torvalds goto need_lookup; 804bcdc5e01SIan Kent if (IS_ERR(dentry)) 805bcdc5e01SIan Kent goto fail; 806bcdc5e01SIan Kent goto done; 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds fail: 8091da177e4SLinus Torvalds return PTR_ERR(dentry); 8101da177e4SLinus Torvalds } 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds /* 813ac278a9cSAl Viro * This is a temporary kludge to deal with "automount" symlinks; proper 814ac278a9cSAl Viro * solution is to trigger them on follow_mount(), so that do_lookup() 815ac278a9cSAl Viro * would DTRT. To be killed before 2.6.34-final. 816ac278a9cSAl Viro */ 817ac278a9cSAl Viro static inline int follow_on_final(struct inode *inode, unsigned lookup_flags) 818ac278a9cSAl Viro { 819ac278a9cSAl Viro return inode && unlikely(inode->i_op->follow_link) && 820ac278a9cSAl Viro ((lookup_flags & LOOKUP_FOLLOW) || S_ISDIR(inode->i_mode)); 821ac278a9cSAl Viro } 822ac278a9cSAl Viro 823ac278a9cSAl Viro /* 8241da177e4SLinus Torvalds * Name resolution. 825ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 826ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 8271da177e4SLinus Torvalds * 828ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 829ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 8301da177e4SLinus Torvalds */ 8316de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 8321da177e4SLinus Torvalds { 8331da177e4SLinus Torvalds struct path next; 8341da177e4SLinus Torvalds struct inode *inode; 8351da177e4SLinus Torvalds int err; 8361da177e4SLinus Torvalds unsigned int lookup_flags = nd->flags; 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds while (*name=='/') 8391da177e4SLinus Torvalds name++; 8401da177e4SLinus Torvalds if (!*name) 8411da177e4SLinus Torvalds goto return_reval; 8421da177e4SLinus Torvalds 8434ac91378SJan Blunck inode = nd->path.dentry->d_inode; 8441da177e4SLinus Torvalds if (nd->depth) 845f55eab82STrond Myklebust lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE); 8461da177e4SLinus Torvalds 8471da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 8481da177e4SLinus Torvalds for(;;) { 8491da177e4SLinus Torvalds unsigned long hash; 8501da177e4SLinus Torvalds struct qstr this; 8511da177e4SLinus Torvalds unsigned int c; 8521da177e4SLinus Torvalds 853cdce5d6bSTrond Myklebust nd->flags |= LOOKUP_CONTINUE; 854b75b5086SAl Viro err = exec_permission(inode); 8551da177e4SLinus Torvalds if (err) 8561da177e4SLinus Torvalds break; 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds this.name = name; 8591da177e4SLinus Torvalds c = *(const unsigned char *)name; 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds hash = init_name_hash(); 8621da177e4SLinus Torvalds do { 8631da177e4SLinus Torvalds name++; 8641da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 8651da177e4SLinus Torvalds c = *(const unsigned char *)name; 8661da177e4SLinus Torvalds } while (c && (c != '/')); 8671da177e4SLinus Torvalds this.len = name - (const char *) this.name; 8681da177e4SLinus Torvalds this.hash = end_name_hash(hash); 8691da177e4SLinus Torvalds 8701da177e4SLinus Torvalds /* remove trailing slashes? */ 8711da177e4SLinus Torvalds if (!c) 8721da177e4SLinus Torvalds goto last_component; 8731da177e4SLinus Torvalds while (*++name == '/'); 8741da177e4SLinus Torvalds if (!*name) 8751da177e4SLinus Torvalds goto last_with_slashes; 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds /* 8781da177e4SLinus Torvalds * "." and ".." are special - ".." especially so because it has 8791da177e4SLinus Torvalds * to be able to know about the current root directory and 8801da177e4SLinus Torvalds * parent relationships. 8811da177e4SLinus Torvalds */ 8821da177e4SLinus Torvalds if (this.name[0] == '.') switch (this.len) { 8831da177e4SLinus Torvalds default: 8841da177e4SLinus Torvalds break; 8851da177e4SLinus Torvalds case 2: 8861da177e4SLinus Torvalds if (this.name[1] != '.') 8871da177e4SLinus Torvalds break; 88858c465ebSAl Viro follow_dotdot(nd); 8894ac91378SJan Blunck inode = nd->path.dentry->d_inode; 8901da177e4SLinus Torvalds /* fallthrough */ 8911da177e4SLinus Torvalds case 1: 8921da177e4SLinus Torvalds continue; 8931da177e4SLinus Torvalds } 8941da177e4SLinus Torvalds /* This does the actual lookups.. */ 8951da177e4SLinus Torvalds err = do_lookup(nd, &this, &next); 8961da177e4SLinus Torvalds if (err) 8971da177e4SLinus Torvalds break; 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds err = -ENOENT; 9001da177e4SLinus Torvalds inode = next.dentry->d_inode; 9011da177e4SLinus Torvalds if (!inode) 9021da177e4SLinus Torvalds goto out_dput; 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds if (inode->i_op->follow_link) { 90590ebe565SAl Viro err = do_follow_link(&next, nd); 9061da177e4SLinus Torvalds if (err) 9071da177e4SLinus Torvalds goto return_err; 9081da177e4SLinus Torvalds err = -ENOENT; 9094ac91378SJan Blunck inode = nd->path.dentry->d_inode; 9101da177e4SLinus Torvalds if (!inode) 9111da177e4SLinus Torvalds break; 91209dd17d3SMiklos Szeredi } else 91309dd17d3SMiklos Szeredi path_to_nameidata(&next, nd); 9141da177e4SLinus Torvalds err = -ENOTDIR; 9151da177e4SLinus Torvalds if (!inode->i_op->lookup) 9161da177e4SLinus Torvalds break; 9171da177e4SLinus Torvalds continue; 9181da177e4SLinus Torvalds /* here ends the main loop */ 9191da177e4SLinus Torvalds 9201da177e4SLinus Torvalds last_with_slashes: 9211da177e4SLinus Torvalds lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 9221da177e4SLinus Torvalds last_component: 923f55eab82STrond Myklebust /* Clear LOOKUP_CONTINUE iff it was previously unset */ 924f55eab82STrond Myklebust nd->flags &= lookup_flags | ~LOOKUP_CONTINUE; 9251da177e4SLinus Torvalds if (lookup_flags & LOOKUP_PARENT) 9261da177e4SLinus Torvalds goto lookup_parent; 9271da177e4SLinus Torvalds if (this.name[0] == '.') switch (this.len) { 9281da177e4SLinus Torvalds default: 9291da177e4SLinus Torvalds break; 9301da177e4SLinus Torvalds case 2: 9311da177e4SLinus Torvalds if (this.name[1] != '.') 9321da177e4SLinus Torvalds break; 93358c465ebSAl Viro follow_dotdot(nd); 9344ac91378SJan Blunck inode = nd->path.dentry->d_inode; 9351da177e4SLinus Torvalds /* fallthrough */ 9361da177e4SLinus Torvalds case 1: 9371da177e4SLinus Torvalds goto return_reval; 9381da177e4SLinus Torvalds } 9391da177e4SLinus Torvalds err = do_lookup(nd, &this, &next); 9401da177e4SLinus Torvalds if (err) 9411da177e4SLinus Torvalds break; 9421da177e4SLinus Torvalds inode = next.dentry->d_inode; 943ac278a9cSAl Viro if (follow_on_final(inode, lookup_flags)) { 94490ebe565SAl Viro err = do_follow_link(&next, nd); 9451da177e4SLinus Torvalds if (err) 9461da177e4SLinus Torvalds goto return_err; 9474ac91378SJan Blunck inode = nd->path.dentry->d_inode; 94809dd17d3SMiklos Szeredi } else 94909dd17d3SMiklos Szeredi path_to_nameidata(&next, nd); 9501da177e4SLinus Torvalds err = -ENOENT; 9511da177e4SLinus Torvalds if (!inode) 9521da177e4SLinus Torvalds break; 9531da177e4SLinus Torvalds if (lookup_flags & LOOKUP_DIRECTORY) { 9541da177e4SLinus Torvalds err = -ENOTDIR; 955acfa4380SAl Viro if (!inode->i_op->lookup) 9561da177e4SLinus Torvalds break; 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds goto return_base; 9591da177e4SLinus Torvalds lookup_parent: 9601da177e4SLinus Torvalds nd->last = this; 9611da177e4SLinus Torvalds nd->last_type = LAST_NORM; 9621da177e4SLinus Torvalds if (this.name[0] != '.') 9631da177e4SLinus Torvalds goto return_base; 9641da177e4SLinus Torvalds if (this.len == 1) 9651da177e4SLinus Torvalds nd->last_type = LAST_DOT; 9661da177e4SLinus Torvalds else if (this.len == 2 && this.name[1] == '.') 9671da177e4SLinus Torvalds nd->last_type = LAST_DOTDOT; 9681da177e4SLinus Torvalds else 9691da177e4SLinus Torvalds goto return_base; 9701da177e4SLinus Torvalds return_reval: 9711da177e4SLinus Torvalds /* 9721da177e4SLinus Torvalds * We bypassed the ordinary revalidation routines. 9731da177e4SLinus Torvalds * We may need to check the cached dentry for staleness. 9741da177e4SLinus Torvalds */ 9754ac91378SJan Blunck if (nd->path.dentry && nd->path.dentry->d_sb && 9764ac91378SJan Blunck (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) { 9771da177e4SLinus Torvalds err = -ESTALE; 9781da177e4SLinus Torvalds /* Note: we do not d_invalidate() */ 9794ac91378SJan Blunck if (!nd->path.dentry->d_op->d_revalidate( 9804ac91378SJan Blunck nd->path.dentry, nd)) 9811da177e4SLinus Torvalds break; 9821da177e4SLinus Torvalds } 9831da177e4SLinus Torvalds return_base: 9841da177e4SLinus Torvalds return 0; 9851da177e4SLinus Torvalds out_dput: 9861d957f9bSJan Blunck path_put_conditional(&next, nd); 9871da177e4SLinus Torvalds break; 9881da177e4SLinus Torvalds } 9891d957f9bSJan Blunck path_put(&nd->path); 9901da177e4SLinus Torvalds return_err: 9911da177e4SLinus Torvalds return err; 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds 994fc9b52cdSHarvey Harrison static int path_walk(const char *name, struct nameidata *nd) 9951da177e4SLinus Torvalds { 9966de88d72SAl Viro struct path save = nd->path; 9976de88d72SAl Viro int result; 9986de88d72SAl Viro 9991da177e4SLinus Torvalds current->total_link_count = 0; 10006de88d72SAl Viro 10016de88d72SAl Viro /* make sure the stuff we saved doesn't go away */ 10026de88d72SAl Viro path_get(&save); 10036de88d72SAl Viro 10046de88d72SAl Viro result = link_path_walk(name, nd); 10056de88d72SAl Viro if (result == -ESTALE) { 10066de88d72SAl Viro /* nd->path had been dropped */ 10076de88d72SAl Viro current->total_link_count = 0; 10086de88d72SAl Viro nd->path = save; 10096de88d72SAl Viro path_get(&nd->path); 10106de88d72SAl Viro nd->flags |= LOOKUP_REVAL; 10116de88d72SAl Viro result = link_path_walk(name, nd); 10126de88d72SAl Viro } 10136de88d72SAl Viro 10146de88d72SAl Viro path_put(&save); 10156de88d72SAl Viro 10166de88d72SAl Viro return result; 10171da177e4SLinus Torvalds } 10181da177e4SLinus Torvalds 10199b4a9b14SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd) 10201da177e4SLinus Torvalds { 1021ea3834d9SPrasanna Meda int retval = 0; 1022170aa3d0SUlrich Drepper int fput_needed; 1023170aa3d0SUlrich Drepper struct file *file; 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds nd->last_type = LAST_ROOT; /* if there are only slashes... */ 10261da177e4SLinus Torvalds nd->flags = flags; 10271da177e4SLinus Torvalds nd->depth = 0; 10282a737871SAl Viro nd->root.mnt = NULL; 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds if (*name=='/') { 10312a737871SAl Viro set_root(nd); 10322a737871SAl Viro nd->path = nd->root; 10332a737871SAl Viro path_get(&nd->root); 10345590ff0dSUlrich Drepper } else if (dfd == AT_FDCWD) { 10352a737871SAl Viro struct fs_struct *fs = current->fs; 1036e518ddb7SAndreas Mohr read_lock(&fs->lock); 10376ac08c39SJan Blunck nd->path = fs->pwd; 10386ac08c39SJan Blunck path_get(&fs->pwd); 1039e518ddb7SAndreas Mohr read_unlock(&fs->lock); 10405590ff0dSUlrich Drepper } else { 10415590ff0dSUlrich Drepper struct dentry *dentry; 10425590ff0dSUlrich Drepper 10435590ff0dSUlrich Drepper file = fget_light(dfd, &fput_needed); 10445590ff0dSUlrich Drepper retval = -EBADF; 1045170aa3d0SUlrich Drepper if (!file) 10466d09bb62STrond Myklebust goto out_fail; 10475590ff0dSUlrich Drepper 10480f7fc9e4SJosef "Jeff" Sipek dentry = file->f_path.dentry; 10495590ff0dSUlrich Drepper 10505590ff0dSUlrich Drepper retval = -ENOTDIR; 1051170aa3d0SUlrich Drepper if (!S_ISDIR(dentry->d_inode->i_mode)) 10526d09bb62STrond Myklebust goto fput_fail; 10535590ff0dSUlrich Drepper 10545590ff0dSUlrich Drepper retval = file_permission(file, MAY_EXEC); 1055170aa3d0SUlrich Drepper if (retval) 10566d09bb62STrond Myklebust goto fput_fail; 10575590ff0dSUlrich Drepper 10585dd784d0SJan Blunck nd->path = file->f_path; 10595dd784d0SJan Blunck path_get(&file->f_path); 10605590ff0dSUlrich Drepper 10615590ff0dSUlrich Drepper fput_light(file, fput_needed); 10621da177e4SLinus Torvalds } 10639b4a9b14SAl Viro return 0; 10642dfdd266SJosef 'Jeff' Sipek 10659b4a9b14SAl Viro fput_fail: 10669b4a9b14SAl Viro fput_light(file, fput_needed); 10679b4a9b14SAl Viro out_fail: 10689b4a9b14SAl Viro return retval; 10699b4a9b14SAl Viro } 10709b4a9b14SAl Viro 10719b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 10729b4a9b14SAl Viro static int do_path_lookup(int dfd, const char *name, 10739b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 10749b4a9b14SAl Viro { 10759b4a9b14SAl Viro int retval = path_init(dfd, name, flags, nd); 10769b4a9b14SAl Viro if (!retval) 10772dfdd266SJosef 'Jeff' Sipek retval = path_walk(name, nd); 10784ac91378SJan Blunck if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry && 10794ac91378SJan Blunck nd->path.dentry->d_inode)) 10804ac91378SJan Blunck audit_inode(name, nd->path.dentry); 10812a737871SAl Viro if (nd->root.mnt) { 10822a737871SAl Viro path_put(&nd->root); 10832a737871SAl Viro nd->root.mnt = NULL; 10842a737871SAl Viro } 1085170aa3d0SUlrich Drepper return retval; 10861da177e4SLinus Torvalds } 10871da177e4SLinus Torvalds 1088fc9b52cdSHarvey Harrison int path_lookup(const char *name, unsigned int flags, 10895590ff0dSUlrich Drepper struct nameidata *nd) 10905590ff0dSUlrich Drepper { 10915590ff0dSUlrich Drepper return do_path_lookup(AT_FDCWD, name, flags, nd); 10925590ff0dSUlrich Drepper } 10935590ff0dSUlrich Drepper 1094d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1095d1811465SAl Viro { 1096d1811465SAl Viro struct nameidata nd; 1097d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1098d1811465SAl Viro if (!res) 1099d1811465SAl Viro *path = nd.path; 1100d1811465SAl Viro return res; 1101d1811465SAl Viro } 1102d1811465SAl Viro 110316f18200SJosef 'Jeff' Sipek /** 110416f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 110516f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 110616f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 110716f18200SJosef 'Jeff' Sipek * @name: pointer to file name 110816f18200SJosef 'Jeff' Sipek * @flags: lookup flags 110916f18200SJosef 'Jeff' Sipek * @nd: pointer to nameidata 111016f18200SJosef 'Jeff' Sipek */ 111116f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 111216f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 111316f18200SJosef 'Jeff' Sipek struct nameidata *nd) 111416f18200SJosef 'Jeff' Sipek { 111516f18200SJosef 'Jeff' Sipek int retval; 111616f18200SJosef 'Jeff' Sipek 111716f18200SJosef 'Jeff' Sipek /* same as do_path_lookup */ 111816f18200SJosef 'Jeff' Sipek nd->last_type = LAST_ROOT; 111916f18200SJosef 'Jeff' Sipek nd->flags = flags; 112016f18200SJosef 'Jeff' Sipek nd->depth = 0; 112116f18200SJosef 'Jeff' Sipek 1122c8e7f449SJan Blunck nd->path.dentry = dentry; 1123c8e7f449SJan Blunck nd->path.mnt = mnt; 1124c8e7f449SJan Blunck path_get(&nd->path); 11255b857119SAl Viro nd->root = nd->path; 11265b857119SAl Viro path_get(&nd->root); 112716f18200SJosef 'Jeff' Sipek 112816f18200SJosef 'Jeff' Sipek retval = path_walk(name, nd); 11294ac91378SJan Blunck if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry && 11304ac91378SJan Blunck nd->path.dentry->d_inode)) 11314ac91378SJan Blunck audit_inode(name, nd->path.dentry); 113216f18200SJosef 'Jeff' Sipek 11332a737871SAl Viro path_put(&nd->root); 11342a737871SAl Viro nd->root.mnt = NULL; 113516f18200SJosef 'Jeff' Sipek 11362a737871SAl Viro return retval; 113716f18200SJosef 'Jeff' Sipek } 113816f18200SJosef 'Jeff' Sipek 1139eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name, 1140eead1911SChristoph Hellwig struct dentry *base, struct nameidata *nd) 11411da177e4SLinus Torvalds { 11421da177e4SLinus Torvalds struct dentry *dentry; 11431da177e4SLinus Torvalds struct inode *inode; 11441da177e4SLinus Torvalds int err; 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds inode = base->d_inode; 11471da177e4SLinus Torvalds 11481da177e4SLinus Torvalds /* 11491da177e4SLinus Torvalds * See if the low-level filesystem might want 11501da177e4SLinus Torvalds * to use its own hash.. 11511da177e4SLinus Torvalds */ 11521da177e4SLinus Torvalds if (base->d_op && base->d_op->d_hash) { 11531da177e4SLinus Torvalds err = base->d_op->d_hash(base, name); 11541da177e4SLinus Torvalds dentry = ERR_PTR(err); 11551da177e4SLinus Torvalds if (err < 0) 11561da177e4SLinus Torvalds goto out; 11571da177e4SLinus Torvalds } 11581da177e4SLinus Torvalds 11596e6b1bd1SAl Viro dentry = __d_lookup(base, name); 11606e6b1bd1SAl Viro 11616e6b1bd1SAl Viro /* lockess __d_lookup may fail due to concurrent d_move() 11626e6b1bd1SAl Viro * in some unrelated directory, so try with d_lookup 11636e6b1bd1SAl Viro */ 11646e6b1bd1SAl Viro if (!dentry) 11656e6b1bd1SAl Viro dentry = d_lookup(base, name); 11666e6b1bd1SAl Viro 11676e6b1bd1SAl Viro if (dentry && dentry->d_op && dentry->d_op->d_revalidate) 11686e6b1bd1SAl Viro dentry = do_revalidate(dentry, nd); 11696e6b1bd1SAl Viro 11701da177e4SLinus Torvalds if (!dentry) { 1171d70b67c8SMiklos Szeredi struct dentry *new; 1172d70b67c8SMiklos Szeredi 1173d70b67c8SMiklos Szeredi /* Don't create child dentry for a dead directory. */ 1174d70b67c8SMiklos Szeredi dentry = ERR_PTR(-ENOENT); 1175d70b67c8SMiklos Szeredi if (IS_DEADDIR(inode)) 1176d70b67c8SMiklos Szeredi goto out; 1177d70b67c8SMiklos Szeredi 1178d70b67c8SMiklos Szeredi new = d_alloc(base, name); 11791da177e4SLinus Torvalds dentry = ERR_PTR(-ENOMEM); 11801da177e4SLinus Torvalds if (!new) 11811da177e4SLinus Torvalds goto out; 11821da177e4SLinus Torvalds dentry = inode->i_op->lookup(inode, new, nd); 11831da177e4SLinus Torvalds if (!dentry) 11841da177e4SLinus Torvalds dentry = new; 11851da177e4SLinus Torvalds else 11861da177e4SLinus Torvalds dput(new); 11871da177e4SLinus Torvalds } 11881da177e4SLinus Torvalds out: 11891da177e4SLinus Torvalds return dentry; 11901da177e4SLinus Torvalds } 11911da177e4SLinus Torvalds 1192057f6c01SJames Morris /* 1193057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1194057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1195057f6c01SJames Morris * SMP-safe. 1196057f6c01SJames Morris */ 1197a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 11981da177e4SLinus Torvalds { 1199eead1911SChristoph Hellwig int err; 1200eead1911SChristoph Hellwig 1201b75b5086SAl Viro err = exec_permission(nd->path.dentry->d_inode); 1202eead1911SChristoph Hellwig if (err) 1203eead1911SChristoph Hellwig return ERR_PTR(err); 12044ac91378SJan Blunck return __lookup_hash(&nd->last, nd->path.dentry, nd); 12051da177e4SLinus Torvalds } 12061da177e4SLinus Torvalds 1207eead1911SChristoph Hellwig static int __lookup_one_len(const char *name, struct qstr *this, 1208eead1911SChristoph Hellwig struct dentry *base, int len) 12091da177e4SLinus Torvalds { 12101da177e4SLinus Torvalds unsigned long hash; 12111da177e4SLinus Torvalds unsigned int c; 12121da177e4SLinus Torvalds 1213057f6c01SJames Morris this->name = name; 1214057f6c01SJames Morris this->len = len; 12151da177e4SLinus Torvalds if (!len) 1216057f6c01SJames Morris return -EACCES; 12171da177e4SLinus Torvalds 12181da177e4SLinus Torvalds hash = init_name_hash(); 12191da177e4SLinus Torvalds while (len--) { 12201da177e4SLinus Torvalds c = *(const unsigned char *)name++; 12211da177e4SLinus Torvalds if (c == '/' || c == '\0') 1222057f6c01SJames Morris return -EACCES; 12231da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 12241da177e4SLinus Torvalds } 1225057f6c01SJames Morris this->hash = end_name_hash(hash); 1226057f6c01SJames Morris return 0; 1227057f6c01SJames Morris } 12281da177e4SLinus Torvalds 1229eead1911SChristoph Hellwig /** 1230a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1231eead1911SChristoph Hellwig * @name: pathname component to lookup 1232eead1911SChristoph Hellwig * @base: base directory to lookup from 1233eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1234eead1911SChristoph Hellwig * 1235a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1236a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1237eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1238eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1239eead1911SChristoph Hellwig */ 1240057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1241057f6c01SJames Morris { 1242057f6c01SJames Morris int err; 1243057f6c01SJames Morris struct qstr this; 1244057f6c01SJames Morris 12452f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 12462f9092e1SDavid Woodhouse 1247057f6c01SJames Morris err = __lookup_one_len(name, &this, base, len); 1248057f6c01SJames Morris if (err) 1249057f6c01SJames Morris return ERR_PTR(err); 1250eead1911SChristoph Hellwig 1251b75b5086SAl Viro err = exec_permission(base->d_inode); 1252eead1911SChristoph Hellwig if (err) 1253eead1911SChristoph Hellwig return ERR_PTR(err); 125449705b77SChristoph Hellwig return __lookup_hash(&this, base, NULL); 1255057f6c01SJames Morris } 1256057f6c01SJames Morris 12572d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags, 12582d8f3038SAl Viro struct path *path) 12591da177e4SLinus Torvalds { 12602d8f3038SAl Viro struct nameidata nd; 12611da177e4SLinus Torvalds char *tmp = getname(name); 12621da177e4SLinus Torvalds int err = PTR_ERR(tmp); 12631da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 12642d8f3038SAl Viro 12652d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 12662d8f3038SAl Viro 12672d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 12681da177e4SLinus Torvalds putname(tmp); 12692d8f3038SAl Viro if (!err) 12702d8f3038SAl Viro *path = nd.path; 12711da177e4SLinus Torvalds } 12721da177e4SLinus Torvalds return err; 12731da177e4SLinus Torvalds } 12741da177e4SLinus Torvalds 12752ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 12762ad94ae6SAl Viro struct nameidata *nd, char **name) 12772ad94ae6SAl Viro { 12782ad94ae6SAl Viro char *s = getname(path); 12792ad94ae6SAl Viro int error; 12802ad94ae6SAl Viro 12812ad94ae6SAl Viro if (IS_ERR(s)) 12822ad94ae6SAl Viro return PTR_ERR(s); 12832ad94ae6SAl Viro 12842ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 12852ad94ae6SAl Viro if (error) 12862ad94ae6SAl Viro putname(s); 12872ad94ae6SAl Viro else 12882ad94ae6SAl Viro *name = s; 12892ad94ae6SAl Viro 12902ad94ae6SAl Viro return error; 12912ad94ae6SAl Viro } 12922ad94ae6SAl Viro 12931da177e4SLinus Torvalds /* 12941da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 12951da177e4SLinus Torvalds * minimal. 12961da177e4SLinus Torvalds */ 12971da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 12981da177e4SLinus Torvalds { 1299da9592edSDavid Howells uid_t fsuid = current_fsuid(); 1300da9592edSDavid Howells 13011da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 13021da177e4SLinus Torvalds return 0; 1303da9592edSDavid Howells if (inode->i_uid == fsuid) 13041da177e4SLinus Torvalds return 0; 1305da9592edSDavid Howells if (dir->i_uid == fsuid) 13061da177e4SLinus Torvalds return 0; 13071da177e4SLinus Torvalds return !capable(CAP_FOWNER); 13081da177e4SLinus Torvalds } 13091da177e4SLinus Torvalds 13101da177e4SLinus Torvalds /* 13111da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 13121da177e4SLinus Torvalds * whether the type of victim is right. 13131da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 13141da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 13151da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 13161da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 13171da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 13181da177e4SLinus Torvalds * a. be owner of dir, or 13191da177e4SLinus Torvalds * b. be owner of victim, or 13201da177e4SLinus Torvalds * c. have CAP_FOWNER capability 13211da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 13221da177e4SLinus Torvalds * links pointing to it. 13231da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 13241da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 13251da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 13261da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 13271da177e4SLinus Torvalds * nfs_async_unlink(). 13281da177e4SLinus Torvalds */ 1329858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 13301da177e4SLinus Torvalds { 13311da177e4SLinus Torvalds int error; 13321da177e4SLinus Torvalds 13331da177e4SLinus Torvalds if (!victim->d_inode) 13341da177e4SLinus Torvalds return -ENOENT; 13351da177e4SLinus Torvalds 13361da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 1337cccc6bbaSAl Viro audit_inode_child(victim, dir); 13381da177e4SLinus Torvalds 1339f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 13401da177e4SLinus Torvalds if (error) 13411da177e4SLinus Torvalds return error; 13421da177e4SLinus Torvalds if (IS_APPEND(dir)) 13431da177e4SLinus Torvalds return -EPERM; 13441da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 1345f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 13461da177e4SLinus Torvalds return -EPERM; 13471da177e4SLinus Torvalds if (isdir) { 13481da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 13491da177e4SLinus Torvalds return -ENOTDIR; 13501da177e4SLinus Torvalds if (IS_ROOT(victim)) 13511da177e4SLinus Torvalds return -EBUSY; 13521da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 13531da177e4SLinus Torvalds return -EISDIR; 13541da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 13551da177e4SLinus Torvalds return -ENOENT; 13561da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 13571da177e4SLinus Torvalds return -EBUSY; 13581da177e4SLinus Torvalds return 0; 13591da177e4SLinus Torvalds } 13601da177e4SLinus Torvalds 13611da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 13621da177e4SLinus Torvalds * dir. 13631da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 13641da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 13651da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 13661da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 13671da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 13681da177e4SLinus Torvalds */ 1369a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 13701da177e4SLinus Torvalds { 13711da177e4SLinus Torvalds if (child->d_inode) 13721da177e4SLinus Torvalds return -EEXIST; 13731da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 13741da177e4SLinus Torvalds return -ENOENT; 1375f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 13761da177e4SLinus Torvalds } 13771da177e4SLinus Torvalds 13781da177e4SLinus Torvalds /* 13791da177e4SLinus Torvalds * O_DIRECTORY translates into forcing a directory lookup. 13801da177e4SLinus Torvalds */ 13811da177e4SLinus Torvalds static inline int lookup_flags(unsigned int f) 13821da177e4SLinus Torvalds { 13831da177e4SLinus Torvalds unsigned long retval = LOOKUP_FOLLOW; 13841da177e4SLinus Torvalds 13851da177e4SLinus Torvalds if (f & O_NOFOLLOW) 13861da177e4SLinus Torvalds retval &= ~LOOKUP_FOLLOW; 13871da177e4SLinus Torvalds 13881da177e4SLinus Torvalds if (f & O_DIRECTORY) 13891da177e4SLinus Torvalds retval |= LOOKUP_DIRECTORY; 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds return retval; 13921da177e4SLinus Torvalds } 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds /* 13951da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 13961da177e4SLinus Torvalds */ 13971da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 13981da177e4SLinus Torvalds { 13991da177e4SLinus Torvalds struct dentry *p; 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds if (p1 == p2) { 1402f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 14031da177e4SLinus Torvalds return NULL; 14041da177e4SLinus Torvalds } 14051da177e4SLinus Torvalds 1406a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 14071da177e4SLinus Torvalds 1408e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 1409e2761a11SOGAWA Hirofumi if (p) { 1410f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 1411f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 14121da177e4SLinus Torvalds return p; 14131da177e4SLinus Torvalds } 14141da177e4SLinus Torvalds 1415e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 1416e2761a11SOGAWA Hirofumi if (p) { 1417f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1418f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 14191da177e4SLinus Torvalds return p; 14201da177e4SLinus Torvalds } 14211da177e4SLinus Torvalds 1422f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1423f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 14241da177e4SLinus Torvalds return NULL; 14251da177e4SLinus Torvalds } 14261da177e4SLinus Torvalds 14271da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 14281da177e4SLinus Torvalds { 14291b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 14301da177e4SLinus Torvalds if (p1 != p2) { 14311b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 1432a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 14331da177e4SLinus Torvalds } 14341da177e4SLinus Torvalds } 14351da177e4SLinus Torvalds 14361da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode, 14371da177e4SLinus Torvalds struct nameidata *nd) 14381da177e4SLinus Torvalds { 1439a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 14401da177e4SLinus Torvalds 14411da177e4SLinus Torvalds if (error) 14421da177e4SLinus Torvalds return error; 14431da177e4SLinus Torvalds 1444acfa4380SAl Viro if (!dir->i_op->create) 14451da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 14461da177e4SLinus Torvalds mode &= S_IALLUGO; 14471da177e4SLinus Torvalds mode |= S_IFREG; 14481da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 14491da177e4SLinus Torvalds if (error) 14501da177e4SLinus Torvalds return error; 14519e3509e2SJan Kara vfs_dq_init(dir); 14521da177e4SLinus Torvalds error = dir->i_op->create(dir, dentry, mode, nd); 1453a74574aaSStephen Smalley if (!error) 1454f38aa942SAmy Griffis fsnotify_create(dir, dentry); 14551da177e4SLinus Torvalds return error; 14561da177e4SLinus Torvalds } 14571da177e4SLinus Torvalds 14583fb64190SChristoph Hellwig int may_open(struct path *path, int acc_mode, int flag) 14591da177e4SLinus Torvalds { 14603fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 14611da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 14621da177e4SLinus Torvalds int error; 14631da177e4SLinus Torvalds 14641da177e4SLinus Torvalds if (!inode) 14651da177e4SLinus Torvalds return -ENOENT; 14661da177e4SLinus Torvalds 1467c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 1468c8fe8f30SChristoph Hellwig case S_IFLNK: 14691da177e4SLinus Torvalds return -ELOOP; 1470c8fe8f30SChristoph Hellwig case S_IFDIR: 1471c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 14721da177e4SLinus Torvalds return -EISDIR; 1473c8fe8f30SChristoph Hellwig break; 1474c8fe8f30SChristoph Hellwig case S_IFBLK: 1475c8fe8f30SChristoph Hellwig case S_IFCHR: 14763fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 14771da177e4SLinus Torvalds return -EACCES; 1478c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 1479c8fe8f30SChristoph Hellwig case S_IFIFO: 1480c8fe8f30SChristoph Hellwig case S_IFSOCK: 14811da177e4SLinus Torvalds flag &= ~O_TRUNC; 1482c8fe8f30SChristoph Hellwig break; 14834a3fd211SDave Hansen } 1484b41572e9SDave Hansen 14853fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 1486b41572e9SDave Hansen if (error) 1487b41572e9SDave Hansen return error; 14886146f0d5SMimi Zohar 14891da177e4SLinus Torvalds /* 14901da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 14911da177e4SLinus Torvalds */ 14921da177e4SLinus Torvalds if (IS_APPEND(inode)) { 14938737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 14947715b521SAl Viro return -EPERM; 14951da177e4SLinus Torvalds if (flag & O_TRUNC) 14967715b521SAl Viro return -EPERM; 14971da177e4SLinus Torvalds } 14981da177e4SLinus Torvalds 14991da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 15007715b521SAl Viro if (flag & O_NOATIME && !is_owner_or_cap(inode)) 15017715b521SAl Viro return -EPERM; 15021da177e4SLinus Torvalds 15031da177e4SLinus Torvalds /* 15041da177e4SLinus Torvalds * Ensure there are no outstanding leases on the file. 15051da177e4SLinus Torvalds */ 1506b65a9cfcSAl Viro return break_lease(inode, flag); 15077715b521SAl Viro } 15087715b521SAl Viro 15097715b521SAl Viro static int handle_truncate(struct path *path) 15107715b521SAl Viro { 15117715b521SAl Viro struct inode *inode = path->dentry->d_inode; 15127715b521SAl Viro int error = get_write_access(inode); 15131da177e4SLinus Torvalds if (error) 15147715b521SAl Viro return error; 15151da177e4SLinus Torvalds /* 15161da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 15171da177e4SLinus Torvalds */ 15181da177e4SLinus Torvalds error = locks_verify_locked(inode); 1519be6d3e56SKentaro Takeda if (!error) 15203fb64190SChristoph Hellwig error = security_path_truncate(path, 0, 1521be6d3e56SKentaro Takeda ATTR_MTIME|ATTR_CTIME|ATTR_OPEN); 15221da177e4SLinus Torvalds if (!error) { 15237715b521SAl Viro error = do_truncate(path->dentry, 0, 1524d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 1525d139d7ffSMiklos Szeredi NULL); 15261da177e4SLinus Torvalds } 15271da177e4SLinus Torvalds put_write_access(inode); 1528acd0c935SMimi Zohar return error; 15291da177e4SLinus Torvalds } 15301da177e4SLinus Torvalds 1531d57999e1SDave Hansen /* 1532d57999e1SDave Hansen * Be careful about ever adding any more callers of this 1533d57999e1SDave Hansen * function. Its flags must be in the namei format, not 1534d57999e1SDave Hansen * what get passed to sys_open(). 1535d57999e1SDave Hansen */ 1536d57999e1SDave Hansen static int __open_namei_create(struct nameidata *nd, struct path *path, 15378737c930SAl Viro int open_flag, int mode) 1538aab520e2SDave Hansen { 1539aab520e2SDave Hansen int error; 15404ac91378SJan Blunck struct dentry *dir = nd->path.dentry; 1541aab520e2SDave Hansen 1542aab520e2SDave Hansen if (!IS_POSIXACL(dir->d_inode)) 1543ce3b0f8dSAl Viro mode &= ~current_umask(); 1544be6d3e56SKentaro Takeda error = security_path_mknod(&nd->path, path->dentry, mode, 0); 1545be6d3e56SKentaro Takeda if (error) 1546be6d3e56SKentaro Takeda goto out_unlock; 1547aab520e2SDave Hansen error = vfs_create(dir->d_inode, path->dentry, mode, nd); 1548be6d3e56SKentaro Takeda out_unlock: 1549aab520e2SDave Hansen mutex_unlock(&dir->d_inode->i_mutex); 15504ac91378SJan Blunck dput(nd->path.dentry); 15514ac91378SJan Blunck nd->path.dentry = path->dentry; 1552aab520e2SDave Hansen if (error) 1553aab520e2SDave Hansen return error; 1554aab520e2SDave Hansen /* Don't check for write permission, don't truncate */ 15558737c930SAl Viro return may_open(&nd->path, 0, open_flag & ~O_TRUNC); 1556aab520e2SDave Hansen } 1557aab520e2SDave Hansen 15581da177e4SLinus Torvalds /* 1559d57999e1SDave Hansen * Note that while the flag value (low two bits) for sys_open means: 1560d57999e1SDave Hansen * 00 - read-only 1561d57999e1SDave Hansen * 01 - write-only 1562d57999e1SDave Hansen * 10 - read-write 1563d57999e1SDave Hansen * 11 - special 1564d57999e1SDave Hansen * it is changed into 1565d57999e1SDave Hansen * 00 - no permissions needed 1566d57999e1SDave Hansen * 01 - read-permission 1567d57999e1SDave Hansen * 10 - write-permission 1568d57999e1SDave Hansen * 11 - read-write 1569d57999e1SDave Hansen * for the internal routines (ie open_namei()/follow_link() etc) 1570d57999e1SDave Hansen * This is more logical, and also allows the 00 "no perm needed" 1571d57999e1SDave Hansen * to be used for symlinks (where the permissions are checked 1572d57999e1SDave Hansen * later). 1573d57999e1SDave Hansen * 1574d57999e1SDave Hansen */ 1575d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 1576d57999e1SDave Hansen { 1577d57999e1SDave Hansen if ((flag+1) & O_ACCMODE) 1578d57999e1SDave Hansen flag++; 1579d57999e1SDave Hansen return flag; 1580d57999e1SDave Hansen } 1581d57999e1SDave Hansen 15827715b521SAl Viro static int open_will_truncate(int flag, struct inode *inode) 15834a3fd211SDave Hansen { 1584d57999e1SDave Hansen /* 15854a3fd211SDave Hansen * We'll never write to the fs underlying 15864a3fd211SDave Hansen * a device file. 15874a3fd211SDave Hansen */ 15884a3fd211SDave Hansen if (special_file(inode->i_mode)) 15894a3fd211SDave Hansen return 0; 15904a3fd211SDave Hansen return (flag & O_TRUNC); 15914a3fd211SDave Hansen } 15924a3fd211SDave Hansen 1593648fa861SAl Viro static struct file *finish_open(struct nameidata *nd, 15949a66179eSAl Viro int open_flag, int acc_mode) 1595648fa861SAl Viro { 1596648fa861SAl Viro struct file *filp; 1597648fa861SAl Viro int will_truncate; 1598648fa861SAl Viro int error; 1599648fa861SAl Viro 16009a66179eSAl Viro will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode); 1601648fa861SAl Viro if (will_truncate) { 1602648fa861SAl Viro error = mnt_want_write(nd->path.mnt); 1603648fa861SAl Viro if (error) 1604648fa861SAl Viro goto exit; 1605648fa861SAl Viro } 1606648fa861SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 1607648fa861SAl Viro if (error) { 1608648fa861SAl Viro if (will_truncate) 1609648fa861SAl Viro mnt_drop_write(nd->path.mnt); 1610648fa861SAl Viro goto exit; 1611648fa861SAl Viro } 1612648fa861SAl Viro filp = nameidata_to_filp(nd); 1613648fa861SAl Viro if (!IS_ERR(filp)) { 1614648fa861SAl Viro error = ima_file_check(filp, acc_mode); 1615648fa861SAl Viro if (error) { 1616648fa861SAl Viro fput(filp); 1617648fa861SAl Viro filp = ERR_PTR(error); 1618648fa861SAl Viro } 1619648fa861SAl Viro } 1620648fa861SAl Viro if (!IS_ERR(filp)) { 1621648fa861SAl Viro if (acc_mode & MAY_WRITE) 1622648fa861SAl Viro vfs_dq_init(nd->path.dentry->d_inode); 1623648fa861SAl Viro 1624648fa861SAl Viro if (will_truncate) { 1625648fa861SAl Viro error = handle_truncate(&nd->path); 1626648fa861SAl Viro if (error) { 1627648fa861SAl Viro fput(filp); 1628648fa861SAl Viro filp = ERR_PTR(error); 1629648fa861SAl Viro } 1630648fa861SAl Viro } 1631648fa861SAl Viro } 1632648fa861SAl Viro /* 1633648fa861SAl Viro * It is now safe to drop the mnt write 1634648fa861SAl Viro * because the filp has had a write taken 1635648fa861SAl Viro * on its behalf. 1636648fa861SAl Viro */ 1637648fa861SAl Viro if (will_truncate) 1638648fa861SAl Viro mnt_drop_write(nd->path.mnt); 1639648fa861SAl Viro return filp; 1640648fa861SAl Viro 1641648fa861SAl Viro exit: 1642648fa861SAl Viro if (!IS_ERR(nd->intent.open.file)) 1643648fa861SAl Viro release_open_intent(nd); 1644648fa861SAl Viro path_put(&nd->path); 1645648fa861SAl Viro return ERR_PTR(error); 1646648fa861SAl Viro } 1647648fa861SAl Viro 1648fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path, 16495b369df8SAl Viro int open_flag, int acc_mode, 16509e67f361SAl Viro int mode, const char *pathname) 1651fb1cc555SAl Viro { 1652a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 1653fb1cc555SAl Viro struct file *filp; 1654fb1cc555SAl Viro int error; 1655fb1cc555SAl Viro 165667ee3ad2SAl Viro if (nd->last_type == LAST_BIND) 165767ee3ad2SAl Viro goto ok; 165867ee3ad2SAl Viro 1659a2c36b45SAl Viro error = -EISDIR; 1660a2c36b45SAl Viro if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len]) 1661a2c36b45SAl Viro goto exit; 1662a2c36b45SAl Viro 1663a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 1664a1e28038SAl Viro 1665a1e28038SAl Viro path->dentry = lookup_hash(nd); 1666a1e28038SAl Viro path->mnt = nd->path.mnt; 1667a1e28038SAl Viro 1668fb1cc555SAl Viro error = PTR_ERR(path->dentry); 1669fb1cc555SAl Viro if (IS_ERR(path->dentry)) { 1670fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 1671fb1cc555SAl Viro goto exit; 1672fb1cc555SAl Viro } 1673fb1cc555SAl Viro 1674fb1cc555SAl Viro if (IS_ERR(nd->intent.open.file)) { 1675fb1cc555SAl Viro error = PTR_ERR(nd->intent.open.file); 1676fb1cc555SAl Viro goto exit_mutex_unlock; 1677fb1cc555SAl Viro } 1678fb1cc555SAl Viro 1679fb1cc555SAl Viro /* Negative dentry, just create the file */ 1680fb1cc555SAl Viro if (!path->dentry->d_inode) { 1681fb1cc555SAl Viro /* 1682fb1cc555SAl Viro * This write is needed to ensure that a 1683fb1cc555SAl Viro * ro->rw transition does not occur between 1684fb1cc555SAl Viro * the time when the file is created and when 1685fb1cc555SAl Viro * a permanent write count is taken through 1686fb1cc555SAl Viro * the 'struct file' in nameidata_to_filp(). 1687fb1cc555SAl Viro */ 1688fb1cc555SAl Viro error = mnt_want_write(nd->path.mnt); 1689fb1cc555SAl Viro if (error) 1690fb1cc555SAl Viro goto exit_mutex_unlock; 1691fb1cc555SAl Viro error = __open_namei_create(nd, path, open_flag, mode); 1692fb1cc555SAl Viro if (error) { 1693fb1cc555SAl Viro mnt_drop_write(nd->path.mnt); 1694fb1cc555SAl Viro goto exit; 1695fb1cc555SAl Viro } 1696fb1cc555SAl Viro filp = nameidata_to_filp(nd); 1697fb1cc555SAl Viro mnt_drop_write(nd->path.mnt); 1698fb1cc555SAl Viro if (!IS_ERR(filp)) { 1699fb1cc555SAl Viro error = ima_file_check(filp, acc_mode); 1700fb1cc555SAl Viro if (error) { 1701fb1cc555SAl Viro fput(filp); 1702fb1cc555SAl Viro filp = ERR_PTR(error); 1703fb1cc555SAl Viro } 1704fb1cc555SAl Viro } 1705fb1cc555SAl Viro return filp; 1706fb1cc555SAl Viro } 1707fb1cc555SAl Viro 1708fb1cc555SAl Viro /* 1709fb1cc555SAl Viro * It already exists. 1710fb1cc555SAl Viro */ 1711fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 1712fb1cc555SAl Viro audit_inode(pathname, path->dentry); 1713fb1cc555SAl Viro 1714fb1cc555SAl Viro error = -EEXIST; 17155b369df8SAl Viro if (open_flag & O_EXCL) 1716fb1cc555SAl Viro goto exit_dput; 1717fb1cc555SAl Viro 1718fb1cc555SAl Viro if (__follow_mount(path)) { 1719fb1cc555SAl Viro error = -ELOOP; 17205b369df8SAl Viro if (open_flag & O_NOFOLLOW) 1721fb1cc555SAl Viro goto exit_dput; 1722fb1cc555SAl Viro } 1723fb1cc555SAl Viro 1724fb1cc555SAl Viro error = -ENOENT; 1725fb1cc555SAl Viro if (!path->dentry->d_inode) 1726fb1cc555SAl Viro goto exit_dput; 17279e67f361SAl Viro 17289e67f361SAl Viro if (path->dentry->d_inode->i_op->follow_link) 1729fb1cc555SAl Viro return NULL; 1730fb1cc555SAl Viro 1731fb1cc555SAl Viro path_to_nameidata(path, nd); 1732fb1cc555SAl Viro error = -EISDIR; 1733fb1cc555SAl Viro if (S_ISDIR(path->dentry->d_inode->i_mode)) 1734fb1cc555SAl Viro goto exit; 173567ee3ad2SAl Viro ok: 17369a66179eSAl Viro filp = finish_open(nd, open_flag, acc_mode); 1737fb1cc555SAl Viro return filp; 1738fb1cc555SAl Viro 1739fb1cc555SAl Viro exit_mutex_unlock: 1740fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 1741fb1cc555SAl Viro exit_dput: 1742fb1cc555SAl Viro path_put_conditional(path, nd); 1743fb1cc555SAl Viro exit: 1744fb1cc555SAl Viro if (!IS_ERR(nd->intent.open.file)) 1745fb1cc555SAl Viro release_open_intent(nd); 1746fb1cc555SAl Viro path_put(&nd->path); 1747fb1cc555SAl Viro return ERR_PTR(error); 1748fb1cc555SAl Viro } 1749fb1cc555SAl Viro 17504a3fd211SDave Hansen /* 17514a3fd211SDave Hansen * Note that the low bits of the passed in "open_flag" 17524a3fd211SDave Hansen * are not the same as in the local variable "flag". See 17534a3fd211SDave Hansen * open_to_namei_flags() for more details. 17541da177e4SLinus Torvalds */ 1755a70e65dfSChristoph Hellwig struct file *do_filp_open(int dfd, const char *pathname, 17566e8341a1SAl Viro int open_flag, int mode, int acc_mode) 17571da177e4SLinus Torvalds { 17584a3fd211SDave Hansen struct file *filp; 1759a70e65dfSChristoph Hellwig struct nameidata nd; 17606e8341a1SAl Viro int error; 17619850c056SAl Viro struct path path; 17621da177e4SLinus Torvalds int count = 0; 1763d57999e1SDave Hansen int flag = open_to_namei_flags(open_flag); 17649850c056SAl Viro int force_reval = 0; 17651da177e4SLinus Torvalds 17666b2f3d1fSChristoph Hellwig /* 17676b2f3d1fSChristoph Hellwig * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only 17686b2f3d1fSChristoph Hellwig * check for O_DSYNC if the need any syncing at all we enforce it's 17696b2f3d1fSChristoph Hellwig * always set instead of having to deal with possibly weird behaviour 17706b2f3d1fSChristoph Hellwig * for malicious applications setting only __O_SYNC. 17716b2f3d1fSChristoph Hellwig */ 17726b2f3d1fSChristoph Hellwig if (open_flag & __O_SYNC) 17736b2f3d1fSChristoph Hellwig open_flag |= O_DSYNC; 17746b2f3d1fSChristoph Hellwig 17756e8341a1SAl Viro if (!acc_mode) 17766d125529SAl Viro acc_mode = MAY_OPEN | ACC_MODE(open_flag); 17771da177e4SLinus Torvalds 1778834f2a4aSTrond Myklebust /* O_TRUNC implies we need access checks for write permissions */ 17794296e2cbSAl Viro if (open_flag & O_TRUNC) 1780834f2a4aSTrond Myklebust acc_mode |= MAY_WRITE; 1781834f2a4aSTrond Myklebust 17821da177e4SLinus Torvalds /* Allow the LSM permission hook to distinguish append 17831da177e4SLinus Torvalds access from general write access. */ 17844296e2cbSAl Viro if (open_flag & O_APPEND) 17851da177e4SLinus Torvalds acc_mode |= MAY_APPEND; 17861da177e4SLinus Torvalds 17871da177e4SLinus Torvalds /* 17881da177e4SLinus Torvalds * The simplest case - just a plain lookup. 17891da177e4SLinus Torvalds */ 17904296e2cbSAl Viro if (!(open_flag & O_CREAT)) { 17912dd6d1f4SAl Viro filp = get_empty_filp(); 17922dd6d1f4SAl Viro 17932dd6d1f4SAl Viro if (filp == NULL) 17942dd6d1f4SAl Viro return ERR_PTR(-ENFILE); 17952dd6d1f4SAl Viro nd.intent.open.file = filp; 1796482928d5SAl Viro filp->f_flags = open_flag; 17972dd6d1f4SAl Viro nd.intent.open.flags = flag; 17982dd6d1f4SAl Viro nd.intent.open.create_mode = 0; 17992dd6d1f4SAl Viro error = do_path_lookup(dfd, pathname, 18004296e2cbSAl Viro lookup_flags(open_flag)|LOOKUP_OPEN, &nd); 18012dd6d1f4SAl Viro if (IS_ERR(nd.intent.open.file)) { 18022dd6d1f4SAl Viro if (error == 0) { 18032dd6d1f4SAl Viro error = PTR_ERR(nd.intent.open.file); 18042dd6d1f4SAl Viro path_put(&nd.path); 18052dd6d1f4SAl Viro } 18062dd6d1f4SAl Viro } else if (error) 18072dd6d1f4SAl Viro release_open_intent(&nd); 18081da177e4SLinus Torvalds if (error) 1809a70e65dfSChristoph Hellwig return ERR_PTR(error); 181067ee3ad2SAl Viro return finish_open(&nd, open_flag, acc_mode); 18111da177e4SLinus Torvalds } 18121da177e4SLinus Torvalds 18131da177e4SLinus Torvalds /* 18141da177e4SLinus Torvalds * Create - we need to know the parent. 18151da177e4SLinus Torvalds */ 18169850c056SAl Viro reval: 18179b4a9b14SAl Viro error = path_init(dfd, pathname, LOOKUP_PARENT, &nd); 18181da177e4SLinus Torvalds if (error) 1819a70e65dfSChristoph Hellwig return ERR_PTR(error); 18209850c056SAl Viro if (force_reval) 18219850c056SAl Viro nd.flags |= LOOKUP_REVAL; 18223866248eSAl Viro 18233866248eSAl Viro current->total_link_count = 0; 18243866248eSAl Viro error = link_path_walk(pathname, &nd); 1825654f562cSJ. R. Okajima if (error) { 182610fa8e62SAl Viro filp = ERR_PTR(error); 182710fa8e62SAl Viro goto out; 1828654f562cSJ. R. Okajima } 18299b4a9b14SAl Viro if (unlikely(!audit_dummy_context())) 18309b4a9b14SAl Viro audit_inode(pathname, nd.path.dentry); 18311da177e4SLinus Torvalds 18321da177e4SLinus Torvalds /* 1833a2c36b45SAl Viro * We have the parent and last component. 18341da177e4SLinus Torvalds */ 18351da177e4SLinus Torvalds 18368737f3a1SAl Viro error = -ENFILE; 18378737f3a1SAl Viro filp = get_empty_filp(); 18388737f3a1SAl Viro if (filp == NULL) 18398737f3a1SAl Viro goto exit_parent; 18408737f3a1SAl Viro nd.intent.open.file = filp; 1841482928d5SAl Viro filp->f_flags = open_flag; 18428737f3a1SAl Viro nd.intent.open.flags = flag; 18438737f3a1SAl Viro nd.intent.open.create_mode = mode; 1844a70e65dfSChristoph Hellwig nd.flags &= ~LOOKUP_PARENT; 18458737f3a1SAl Viro nd.flags |= LOOKUP_CREATE | LOOKUP_OPEN; 18464296e2cbSAl Viro if (open_flag & O_EXCL) 18473516586aSAl Viro nd.flags |= LOOKUP_EXCL; 18489e67f361SAl Viro filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname); 1849806b681cSAl Viro while (unlikely(!filp)) { /* trailing symlink */ 1850806b681cSAl Viro error = -ELOOP; 1851806b681cSAl Viro if ((open_flag & O_NOFOLLOW) || count++ == 32) 1852806b681cSAl Viro goto exit_dput; 1853806b681cSAl Viro /* 1854806b681cSAl Viro * This is subtle. Instead of calling do_follow_link() we do 1855806b681cSAl Viro * the thing by hands. The reason is that this way we have zero 1856806b681cSAl Viro * link_count and path_walk() (called from ->follow_link) 1857806b681cSAl Viro * honoring LOOKUP_PARENT. After that we have the parent and 1858806b681cSAl Viro * last component, i.e. we are in the same situation as after 1859806b681cSAl Viro * the first path_walk(). Well, almost - if the last component 1860806b681cSAl Viro * is normal we get its copy stored in nd->last.name and we will 1861806b681cSAl Viro * have to putname() it when we are done. Procfs-like symlinks 1862806b681cSAl Viro * just set LAST_BIND. 1863806b681cSAl Viro */ 1864806b681cSAl Viro nd.flags |= LOOKUP_PARENT; 1865806b681cSAl Viro error = security_inode_follow_link(path.dentry, &nd); 1866806b681cSAl Viro if (error) 1867806b681cSAl Viro goto exit_dput; 1868806b681cSAl Viro error = __do_follow_link(&path, &nd); 1869806b681cSAl Viro path_put(&path); 1870806b681cSAl Viro if (error) { 1871806b681cSAl Viro /* nd.path had been dropped */ 1872a70e65dfSChristoph Hellwig release_open_intent(&nd); 187310fa8e62SAl Viro filp = ERR_PTR(error); 1874806b681cSAl Viro goto out; 1875806b681cSAl Viro } 1876806b681cSAl Viro nd.flags &= ~LOOKUP_PARENT; 1877806b681cSAl Viro filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname); 1878806b681cSAl Viro if (nd.last_type == LAST_NORM) 1879806b681cSAl Viro __putname(nd.last.name); 1880806b681cSAl Viro } 188110fa8e62SAl Viro out: 18822a737871SAl Viro if (nd.root.mnt) 18832a737871SAl Viro path_put(&nd.root); 188410fa8e62SAl Viro if (filp == ERR_PTR(-ESTALE) && !force_reval) { 188510fa8e62SAl Viro force_reval = 1; 188610fa8e62SAl Viro goto reval; 188710fa8e62SAl Viro } 188810fa8e62SAl Viro return filp; 18891da177e4SLinus Torvalds 1890806b681cSAl Viro exit_dput: 1891806b681cSAl Viro path_put_conditional(&path, &nd); 1892806b681cSAl Viro if (!IS_ERR(nd.intent.open.file)) 1893a70e65dfSChristoph Hellwig release_open_intent(&nd); 1894806b681cSAl Viro exit_parent: 1895806b681cSAl Viro path_put(&nd.path); 189610fa8e62SAl Viro filp = ERR_PTR(error); 189710fa8e62SAl Viro goto out; 1898de459215SKirill Korotaev } 18991da177e4SLinus Torvalds 19001da177e4SLinus Torvalds /** 1901a70e65dfSChristoph Hellwig * filp_open - open file and return file pointer 1902a70e65dfSChristoph Hellwig * 1903a70e65dfSChristoph Hellwig * @filename: path to open 1904a70e65dfSChristoph Hellwig * @flags: open flags as per the open(2) second argument 1905a70e65dfSChristoph Hellwig * @mode: mode for the new file if O_CREAT is set, else ignored 1906a70e65dfSChristoph Hellwig * 1907a70e65dfSChristoph Hellwig * This is the helper to open a file from kernelspace if you really 1908a70e65dfSChristoph Hellwig * have to. But in generally you should not do this, so please move 1909a70e65dfSChristoph Hellwig * along, nothing to see here.. 1910a70e65dfSChristoph Hellwig */ 1911a70e65dfSChristoph Hellwig struct file *filp_open(const char *filename, int flags, int mode) 1912a70e65dfSChristoph Hellwig { 19136e8341a1SAl Viro return do_filp_open(AT_FDCWD, filename, flags, mode, 0); 1914a70e65dfSChristoph Hellwig } 1915a70e65dfSChristoph Hellwig EXPORT_SYMBOL(filp_open); 1916a70e65dfSChristoph Hellwig 1917a70e65dfSChristoph Hellwig /** 19181da177e4SLinus Torvalds * lookup_create - lookup a dentry, creating it if it doesn't exist 19191da177e4SLinus Torvalds * @nd: nameidata info 19201da177e4SLinus Torvalds * @is_dir: directory flag 19211da177e4SLinus Torvalds * 19221da177e4SLinus Torvalds * Simple function to lookup and return a dentry and create it 19231da177e4SLinus Torvalds * if it doesn't exist. Is SMP-safe. 1924c663e5d8SChristoph Hellwig * 19254ac91378SJan Blunck * Returns with nd->path.dentry->d_inode->i_mutex locked. 19261da177e4SLinus Torvalds */ 19271da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir) 19281da177e4SLinus Torvalds { 1929c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 19301da177e4SLinus Torvalds 19314ac91378SJan Blunck mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 1932c663e5d8SChristoph Hellwig /* 1933c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 1934c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 1935c663e5d8SChristoph Hellwig */ 19361da177e4SLinus Torvalds if (nd->last_type != LAST_NORM) 19371da177e4SLinus Torvalds goto fail; 19381da177e4SLinus Torvalds nd->flags &= ~LOOKUP_PARENT; 19393516586aSAl Viro nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL; 1940a634904aSASANO Masahiro nd->intent.open.flags = O_EXCL; 1941c663e5d8SChristoph Hellwig 1942c663e5d8SChristoph Hellwig /* 1943c663e5d8SChristoph Hellwig * Do the final lookup. 1944c663e5d8SChristoph Hellwig */ 194549705b77SChristoph Hellwig dentry = lookup_hash(nd); 19461da177e4SLinus Torvalds if (IS_ERR(dentry)) 19471da177e4SLinus Torvalds goto fail; 1948c663e5d8SChristoph Hellwig 1949e9baf6e5SAl Viro if (dentry->d_inode) 1950e9baf6e5SAl Viro goto eexist; 1951c663e5d8SChristoph Hellwig /* 1952c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 1953c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 1954c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 1955c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 1956c663e5d8SChristoph Hellwig */ 1957e9baf6e5SAl Viro if (unlikely(!is_dir && nd->last.name[nd->last.len])) { 19581da177e4SLinus Torvalds dput(dentry); 19591da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 1960e9baf6e5SAl Viro } 1961e9baf6e5SAl Viro return dentry; 1962e9baf6e5SAl Viro eexist: 1963e9baf6e5SAl Viro dput(dentry); 1964e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 19651da177e4SLinus Torvalds fail: 19661da177e4SLinus Torvalds return dentry; 19671da177e4SLinus Torvalds } 1968f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create); 19691da177e4SLinus Torvalds 19701da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 19711da177e4SLinus Torvalds { 1972a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 19731da177e4SLinus Torvalds 19741da177e4SLinus Torvalds if (error) 19751da177e4SLinus Torvalds return error; 19761da177e4SLinus Torvalds 19771da177e4SLinus Torvalds if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) 19781da177e4SLinus Torvalds return -EPERM; 19791da177e4SLinus Torvalds 1980acfa4380SAl Viro if (!dir->i_op->mknod) 19811da177e4SLinus Torvalds return -EPERM; 19821da177e4SLinus Torvalds 198308ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 198408ce5f16SSerge E. Hallyn if (error) 198508ce5f16SSerge E. Hallyn return error; 198608ce5f16SSerge E. Hallyn 19871da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 19881da177e4SLinus Torvalds if (error) 19891da177e4SLinus Torvalds return error; 19901da177e4SLinus Torvalds 19919e3509e2SJan Kara vfs_dq_init(dir); 19921da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 1993a74574aaSStephen Smalley if (!error) 1994f38aa942SAmy Griffis fsnotify_create(dir, dentry); 19951da177e4SLinus Torvalds return error; 19961da177e4SLinus Torvalds } 19971da177e4SLinus Torvalds 1998463c3197SDave Hansen static int may_mknod(mode_t mode) 1999463c3197SDave Hansen { 2000463c3197SDave Hansen switch (mode & S_IFMT) { 2001463c3197SDave Hansen case S_IFREG: 2002463c3197SDave Hansen case S_IFCHR: 2003463c3197SDave Hansen case S_IFBLK: 2004463c3197SDave Hansen case S_IFIFO: 2005463c3197SDave Hansen case S_IFSOCK: 2006463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2007463c3197SDave Hansen return 0; 2008463c3197SDave Hansen case S_IFDIR: 2009463c3197SDave Hansen return -EPERM; 2010463c3197SDave Hansen default: 2011463c3197SDave Hansen return -EINVAL; 2012463c3197SDave Hansen } 2013463c3197SDave Hansen } 2014463c3197SDave Hansen 20152e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode, 20162e4d0924SHeiko Carstens unsigned, dev) 20171da177e4SLinus Torvalds { 20182ad94ae6SAl Viro int error; 20191da177e4SLinus Torvalds char *tmp; 20201da177e4SLinus Torvalds struct dentry *dentry; 20211da177e4SLinus Torvalds struct nameidata nd; 20221da177e4SLinus Torvalds 20231da177e4SLinus Torvalds if (S_ISDIR(mode)) 20241da177e4SLinus Torvalds return -EPERM; 20251da177e4SLinus Torvalds 20262ad94ae6SAl Viro error = user_path_parent(dfd, filename, &nd, &tmp); 20271da177e4SLinus Torvalds if (error) 20282ad94ae6SAl Viro return error; 20292ad94ae6SAl Viro 20301da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 2031463c3197SDave Hansen if (IS_ERR(dentry)) { 20321da177e4SLinus Torvalds error = PTR_ERR(dentry); 2033463c3197SDave Hansen goto out_unlock; 2034463c3197SDave Hansen } 20354ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2036ce3b0f8dSAl Viro mode &= ~current_umask(); 2037463c3197SDave Hansen error = may_mknod(mode); 2038463c3197SDave Hansen if (error) 2039463c3197SDave Hansen goto out_dput; 2040463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2041463c3197SDave Hansen if (error) 2042463c3197SDave Hansen goto out_dput; 2043be6d3e56SKentaro Takeda error = security_path_mknod(&nd.path, dentry, mode, dev); 2044be6d3e56SKentaro Takeda if (error) 2045be6d3e56SKentaro Takeda goto out_drop_write; 20461da177e4SLinus Torvalds switch (mode & S_IFMT) { 20471da177e4SLinus Torvalds case 0: case S_IFREG: 20484ac91378SJan Blunck error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd); 20491da177e4SLinus Torvalds break; 20501da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 20514ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode, 20521da177e4SLinus Torvalds new_decode_dev(dev)); 20531da177e4SLinus Torvalds break; 20541da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 20554ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0); 20561da177e4SLinus Torvalds break; 20571da177e4SLinus Torvalds } 2058be6d3e56SKentaro Takeda out_drop_write: 2059463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2060463c3197SDave Hansen out_dput: 20611da177e4SLinus Torvalds dput(dentry); 2062463c3197SDave Hansen out_unlock: 20634ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 20641d957f9bSJan Blunck path_put(&nd.path); 20651da177e4SLinus Torvalds putname(tmp); 20661da177e4SLinus Torvalds 20671da177e4SLinus Torvalds return error; 20681da177e4SLinus Torvalds } 20691da177e4SLinus Torvalds 20703480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev) 20715590ff0dSUlrich Drepper { 20725590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 20735590ff0dSUlrich Drepper } 20745590ff0dSUlrich Drepper 20751da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 20761da177e4SLinus Torvalds { 2077a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 20781da177e4SLinus Torvalds 20791da177e4SLinus Torvalds if (error) 20801da177e4SLinus Torvalds return error; 20811da177e4SLinus Torvalds 2082acfa4380SAl Viro if (!dir->i_op->mkdir) 20831da177e4SLinus Torvalds return -EPERM; 20841da177e4SLinus Torvalds 20851da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 20861da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 20871da177e4SLinus Torvalds if (error) 20881da177e4SLinus Torvalds return error; 20891da177e4SLinus Torvalds 20909e3509e2SJan Kara vfs_dq_init(dir); 20911da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2092a74574aaSStephen Smalley if (!error) 2093f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 20941da177e4SLinus Torvalds return error; 20951da177e4SLinus Torvalds } 20961da177e4SLinus Torvalds 20972e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode) 20981da177e4SLinus Torvalds { 20991da177e4SLinus Torvalds int error = 0; 21001da177e4SLinus Torvalds char * tmp; 21016902d925SDave Hansen struct dentry *dentry; 21026902d925SDave Hansen struct nameidata nd; 21031da177e4SLinus Torvalds 21042ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &tmp); 21052ad94ae6SAl Viro if (error) 21066902d925SDave Hansen goto out_err; 21071da177e4SLinus Torvalds 21081da177e4SLinus Torvalds dentry = lookup_create(&nd, 1); 21091da177e4SLinus Torvalds error = PTR_ERR(dentry); 21106902d925SDave Hansen if (IS_ERR(dentry)) 21116902d925SDave Hansen goto out_unlock; 21126902d925SDave Hansen 21134ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2114ce3b0f8dSAl Viro mode &= ~current_umask(); 2115463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2116463c3197SDave Hansen if (error) 2117463c3197SDave Hansen goto out_dput; 2118be6d3e56SKentaro Takeda error = security_path_mkdir(&nd.path, dentry, mode); 2119be6d3e56SKentaro Takeda if (error) 2120be6d3e56SKentaro Takeda goto out_drop_write; 21214ac91378SJan Blunck error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode); 2122be6d3e56SKentaro Takeda out_drop_write: 2123463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2124463c3197SDave Hansen out_dput: 21251da177e4SLinus Torvalds dput(dentry); 21266902d925SDave Hansen out_unlock: 21274ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 21281d957f9bSJan Blunck path_put(&nd.path); 21291da177e4SLinus Torvalds putname(tmp); 21306902d925SDave Hansen out_err: 21311da177e4SLinus Torvalds return error; 21321da177e4SLinus Torvalds } 21331da177e4SLinus Torvalds 21343cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode) 21355590ff0dSUlrich Drepper { 21365590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 21375590ff0dSUlrich Drepper } 21385590ff0dSUlrich Drepper 21391da177e4SLinus Torvalds /* 21401da177e4SLinus Torvalds * We try to drop the dentry early: we should have 21411da177e4SLinus Torvalds * a usage count of 2 if we're the only user of this 21421da177e4SLinus Torvalds * dentry, and if that is true (possibly after pruning 21431da177e4SLinus Torvalds * the dcache), then we drop the dentry now. 21441da177e4SLinus Torvalds * 21451da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 21461da177e4SLinus Torvalds * do a 21471da177e4SLinus Torvalds * 21481da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 21491da177e4SLinus Torvalds * return -EBUSY; 21501da177e4SLinus Torvalds * 21511da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 21521da177e4SLinus Torvalds * that is still in use by something else.. 21531da177e4SLinus Torvalds */ 21541da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 21551da177e4SLinus Torvalds { 21561da177e4SLinus Torvalds dget(dentry); 21571da177e4SLinus Torvalds shrink_dcache_parent(dentry); 21581da177e4SLinus Torvalds spin_lock(&dcache_lock); 21591da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 21601da177e4SLinus Torvalds if (atomic_read(&dentry->d_count) == 2) 21611da177e4SLinus Torvalds __d_drop(dentry); 21621da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 21631da177e4SLinus Torvalds spin_unlock(&dcache_lock); 21641da177e4SLinus Torvalds } 21651da177e4SLinus Torvalds 21661da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 21671da177e4SLinus Torvalds { 21681da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 21691da177e4SLinus Torvalds 21701da177e4SLinus Torvalds if (error) 21711da177e4SLinus Torvalds return error; 21721da177e4SLinus Torvalds 2173acfa4380SAl Viro if (!dir->i_op->rmdir) 21741da177e4SLinus Torvalds return -EPERM; 21751da177e4SLinus Torvalds 21769e3509e2SJan Kara vfs_dq_init(dir); 21771da177e4SLinus Torvalds 21781b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 21791da177e4SLinus Torvalds dentry_unhash(dentry); 21801da177e4SLinus Torvalds if (d_mountpoint(dentry)) 21811da177e4SLinus Torvalds error = -EBUSY; 21821da177e4SLinus Torvalds else { 21831da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 21841da177e4SLinus Torvalds if (!error) { 21851da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 21861da177e4SLinus Torvalds if (!error) 21871da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 21881da177e4SLinus Torvalds } 21891da177e4SLinus Torvalds } 21901b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 21911da177e4SLinus Torvalds if (!error) { 21921da177e4SLinus Torvalds d_delete(dentry); 21931da177e4SLinus Torvalds } 21941da177e4SLinus Torvalds dput(dentry); 21951da177e4SLinus Torvalds 21961da177e4SLinus Torvalds return error; 21971da177e4SLinus Torvalds } 21981da177e4SLinus Torvalds 21995590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 22001da177e4SLinus Torvalds { 22011da177e4SLinus Torvalds int error = 0; 22021da177e4SLinus Torvalds char * name; 22031da177e4SLinus Torvalds struct dentry *dentry; 22041da177e4SLinus Torvalds struct nameidata nd; 22051da177e4SLinus Torvalds 22062ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 22071da177e4SLinus Torvalds if (error) 22082ad94ae6SAl Viro return error; 22091da177e4SLinus Torvalds 22101da177e4SLinus Torvalds switch(nd.last_type) { 22111da177e4SLinus Torvalds case LAST_DOTDOT: 22121da177e4SLinus Torvalds error = -ENOTEMPTY; 22131da177e4SLinus Torvalds goto exit1; 22141da177e4SLinus Torvalds case LAST_DOT: 22151da177e4SLinus Torvalds error = -EINVAL; 22161da177e4SLinus Torvalds goto exit1; 22171da177e4SLinus Torvalds case LAST_ROOT: 22181da177e4SLinus Torvalds error = -EBUSY; 22191da177e4SLinus Torvalds goto exit1; 22201da177e4SLinus Torvalds } 22210612d9fbSOGAWA Hirofumi 22220612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 22230612d9fbSOGAWA Hirofumi 22244ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 222549705b77SChristoph Hellwig dentry = lookup_hash(&nd); 22261da177e4SLinus Torvalds error = PTR_ERR(dentry); 22276902d925SDave Hansen if (IS_ERR(dentry)) 22286902d925SDave Hansen goto exit2; 22290622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 22300622753bSDave Hansen if (error) 22310622753bSDave Hansen goto exit3; 2232be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 2233be6d3e56SKentaro Takeda if (error) 2234be6d3e56SKentaro Takeda goto exit4; 22354ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 2236be6d3e56SKentaro Takeda exit4: 22370622753bSDave Hansen mnt_drop_write(nd.path.mnt); 22380622753bSDave Hansen exit3: 22391da177e4SLinus Torvalds dput(dentry); 22406902d925SDave Hansen exit2: 22414ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 22421da177e4SLinus Torvalds exit1: 22431d957f9bSJan Blunck path_put(&nd.path); 22441da177e4SLinus Torvalds putname(name); 22451da177e4SLinus Torvalds return error; 22461da177e4SLinus Torvalds } 22471da177e4SLinus Torvalds 22483cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 22495590ff0dSUlrich Drepper { 22505590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 22515590ff0dSUlrich Drepper } 22525590ff0dSUlrich Drepper 22531da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 22541da177e4SLinus Torvalds { 22551da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 22561da177e4SLinus Torvalds 22571da177e4SLinus Torvalds if (error) 22581da177e4SLinus Torvalds return error; 22591da177e4SLinus Torvalds 2260acfa4380SAl Viro if (!dir->i_op->unlink) 22611da177e4SLinus Torvalds return -EPERM; 22621da177e4SLinus Torvalds 22639e3509e2SJan Kara vfs_dq_init(dir); 22641da177e4SLinus Torvalds 22651b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 22661da177e4SLinus Torvalds if (d_mountpoint(dentry)) 22671da177e4SLinus Torvalds error = -EBUSY; 22681da177e4SLinus Torvalds else { 22691da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 2270bec1052eSAl Viro if (!error) { 22711da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 2272bec1052eSAl Viro if (!error) 2273bec1052eSAl Viro dentry->d_inode->i_flags |= S_DEAD; 2274bec1052eSAl Viro } 22751da177e4SLinus Torvalds } 22761b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 22771da177e4SLinus Torvalds 22781da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 22791da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 2280ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 22811da177e4SLinus Torvalds d_delete(dentry); 22821da177e4SLinus Torvalds } 22830eeca283SRobert Love 22841da177e4SLinus Torvalds return error; 22851da177e4SLinus Torvalds } 22861da177e4SLinus Torvalds 22871da177e4SLinus Torvalds /* 22881da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 22891b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 22901da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 22911da177e4SLinus Torvalds * while waiting on the I/O. 22921da177e4SLinus Torvalds */ 22935590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 22941da177e4SLinus Torvalds { 22952ad94ae6SAl Viro int error; 22961da177e4SLinus Torvalds char *name; 22971da177e4SLinus Torvalds struct dentry *dentry; 22981da177e4SLinus Torvalds struct nameidata nd; 22991da177e4SLinus Torvalds struct inode *inode = NULL; 23001da177e4SLinus Torvalds 23012ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 23021da177e4SLinus Torvalds if (error) 23032ad94ae6SAl Viro return error; 23042ad94ae6SAl Viro 23051da177e4SLinus Torvalds error = -EISDIR; 23061da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 23071da177e4SLinus Torvalds goto exit1; 23080612d9fbSOGAWA Hirofumi 23090612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 23100612d9fbSOGAWA Hirofumi 23114ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 231249705b77SChristoph Hellwig dentry = lookup_hash(&nd); 23131da177e4SLinus Torvalds error = PTR_ERR(dentry); 23141da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 23151da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 23161da177e4SLinus Torvalds if (nd.last.name[nd.last.len]) 23171da177e4SLinus Torvalds goto slashes; 23181da177e4SLinus Torvalds inode = dentry->d_inode; 23191da177e4SLinus Torvalds if (inode) 23201da177e4SLinus Torvalds atomic_inc(&inode->i_count); 23210622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 23220622753bSDave Hansen if (error) 23230622753bSDave Hansen goto exit2; 2324be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 2325be6d3e56SKentaro Takeda if (error) 2326be6d3e56SKentaro Takeda goto exit3; 23274ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 2328be6d3e56SKentaro Takeda exit3: 23290622753bSDave Hansen mnt_drop_write(nd.path.mnt); 23301da177e4SLinus Torvalds exit2: 23311da177e4SLinus Torvalds dput(dentry); 23321da177e4SLinus Torvalds } 23334ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 23341da177e4SLinus Torvalds if (inode) 23351da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 23361da177e4SLinus Torvalds exit1: 23371d957f9bSJan Blunck path_put(&nd.path); 23381da177e4SLinus Torvalds putname(name); 23391da177e4SLinus Torvalds return error; 23401da177e4SLinus Torvalds 23411da177e4SLinus Torvalds slashes: 23421da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 23431da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 23441da177e4SLinus Torvalds goto exit2; 23451da177e4SLinus Torvalds } 23461da177e4SLinus Torvalds 23472e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 23485590ff0dSUlrich Drepper { 23495590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 23505590ff0dSUlrich Drepper return -EINVAL; 23515590ff0dSUlrich Drepper 23525590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 23535590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 23545590ff0dSUlrich Drepper 23555590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 23565590ff0dSUlrich Drepper } 23575590ff0dSUlrich Drepper 23583480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 23595590ff0dSUlrich Drepper { 23605590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 23615590ff0dSUlrich Drepper } 23625590ff0dSUlrich Drepper 2363db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 23641da177e4SLinus Torvalds { 2365a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 23661da177e4SLinus Torvalds 23671da177e4SLinus Torvalds if (error) 23681da177e4SLinus Torvalds return error; 23691da177e4SLinus Torvalds 2370acfa4380SAl Viro if (!dir->i_op->symlink) 23711da177e4SLinus Torvalds return -EPERM; 23721da177e4SLinus Torvalds 23731da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 23741da177e4SLinus Torvalds if (error) 23751da177e4SLinus Torvalds return error; 23761da177e4SLinus Torvalds 23779e3509e2SJan Kara vfs_dq_init(dir); 23781da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 2379a74574aaSStephen Smalley if (!error) 2380f38aa942SAmy Griffis fsnotify_create(dir, dentry); 23811da177e4SLinus Torvalds return error; 23821da177e4SLinus Torvalds } 23831da177e4SLinus Torvalds 23842e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 23852e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 23861da177e4SLinus Torvalds { 23872ad94ae6SAl Viro int error; 23881da177e4SLinus Torvalds char *from; 23891da177e4SLinus Torvalds char *to; 23906902d925SDave Hansen struct dentry *dentry; 23916902d925SDave Hansen struct nameidata nd; 23921da177e4SLinus Torvalds 23931da177e4SLinus Torvalds from = getname(oldname); 23941da177e4SLinus Torvalds if (IS_ERR(from)) 23951da177e4SLinus Torvalds return PTR_ERR(from); 23962ad94ae6SAl Viro 23972ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 23982ad94ae6SAl Viro if (error) 23996902d925SDave Hansen goto out_putname; 24001da177e4SLinus Torvalds 24011da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 24021da177e4SLinus Torvalds error = PTR_ERR(dentry); 24036902d925SDave Hansen if (IS_ERR(dentry)) 24046902d925SDave Hansen goto out_unlock; 24056902d925SDave Hansen 240675c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 240775c3f29dSDave Hansen if (error) 240875c3f29dSDave Hansen goto out_dput; 2409be6d3e56SKentaro Takeda error = security_path_symlink(&nd.path, dentry, from); 2410be6d3e56SKentaro Takeda if (error) 2411be6d3e56SKentaro Takeda goto out_drop_write; 2412db2e747bSMiklos Szeredi error = vfs_symlink(nd.path.dentry->d_inode, dentry, from); 2413be6d3e56SKentaro Takeda out_drop_write: 241475c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 241575c3f29dSDave Hansen out_dput: 24161da177e4SLinus Torvalds dput(dentry); 24176902d925SDave Hansen out_unlock: 24184ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 24191d957f9bSJan Blunck path_put(&nd.path); 24201da177e4SLinus Torvalds putname(to); 24216902d925SDave Hansen out_putname: 24221da177e4SLinus Torvalds putname(from); 24231da177e4SLinus Torvalds return error; 24241da177e4SLinus Torvalds } 24251da177e4SLinus Torvalds 24263480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 24275590ff0dSUlrich Drepper { 24285590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 24295590ff0dSUlrich Drepper } 24305590ff0dSUlrich Drepper 24311da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 24321da177e4SLinus Torvalds { 24331da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 24341da177e4SLinus Torvalds int error; 24351da177e4SLinus Torvalds 24361da177e4SLinus Torvalds if (!inode) 24371da177e4SLinus Torvalds return -ENOENT; 24381da177e4SLinus Torvalds 2439a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 24401da177e4SLinus Torvalds if (error) 24411da177e4SLinus Torvalds return error; 24421da177e4SLinus Torvalds 24431da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 24441da177e4SLinus Torvalds return -EXDEV; 24451da177e4SLinus Torvalds 24461da177e4SLinus Torvalds /* 24471da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 24481da177e4SLinus Torvalds */ 24491da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 24501da177e4SLinus Torvalds return -EPERM; 2451acfa4380SAl Viro if (!dir->i_op->link) 24521da177e4SLinus Torvalds return -EPERM; 24537e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 24541da177e4SLinus Torvalds return -EPERM; 24551da177e4SLinus Torvalds 24561da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 24571da177e4SLinus Torvalds if (error) 24581da177e4SLinus Torvalds return error; 24591da177e4SLinus Torvalds 24607e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 24619e3509e2SJan Kara vfs_dq_init(dir); 24621da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 24637e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 2464e31e14ecSStephen Smalley if (!error) 24657e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 24661da177e4SLinus Torvalds return error; 24671da177e4SLinus Torvalds } 24681da177e4SLinus Torvalds 24691da177e4SLinus Torvalds /* 24701da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 24711da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 24721da177e4SLinus Torvalds * newname. --KAB 24731da177e4SLinus Torvalds * 24741da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 24751da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 24761da177e4SLinus Torvalds * and other special files. --ADM 24771da177e4SLinus Torvalds */ 24782e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 24792e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 24801da177e4SLinus Torvalds { 24811da177e4SLinus Torvalds struct dentry *new_dentry; 24822d8f3038SAl Viro struct nameidata nd; 24832d8f3038SAl Viro struct path old_path; 24841da177e4SLinus Torvalds int error; 24851da177e4SLinus Torvalds char *to; 24861da177e4SLinus Torvalds 248745c9b11aSUlrich Drepper if ((flags & ~AT_SYMLINK_FOLLOW) != 0) 2488c04030e1SUlrich Drepper return -EINVAL; 2489c04030e1SUlrich Drepper 24902d8f3038SAl Viro error = user_path_at(olddfd, oldname, 249145c9b11aSUlrich Drepper flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0, 24922d8f3038SAl Viro &old_path); 24931da177e4SLinus Torvalds if (error) 24942ad94ae6SAl Viro return error; 24952ad94ae6SAl Viro 24962ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 24971da177e4SLinus Torvalds if (error) 24981da177e4SLinus Torvalds goto out; 24991da177e4SLinus Torvalds error = -EXDEV; 25002d8f3038SAl Viro if (old_path.mnt != nd.path.mnt) 25011da177e4SLinus Torvalds goto out_release; 25021da177e4SLinus Torvalds new_dentry = lookup_create(&nd, 0); 25031da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 25046902d925SDave Hansen if (IS_ERR(new_dentry)) 25056902d925SDave Hansen goto out_unlock; 250675c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 250775c3f29dSDave Hansen if (error) 250875c3f29dSDave Hansen goto out_dput; 2509be6d3e56SKentaro Takeda error = security_path_link(old_path.dentry, &nd.path, new_dentry); 2510be6d3e56SKentaro Takeda if (error) 2511be6d3e56SKentaro Takeda goto out_drop_write; 25122d8f3038SAl Viro error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry); 2513be6d3e56SKentaro Takeda out_drop_write: 251475c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 251575c3f29dSDave Hansen out_dput: 25161da177e4SLinus Torvalds dput(new_dentry); 25176902d925SDave Hansen out_unlock: 25184ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 25191da177e4SLinus Torvalds out_release: 25201d957f9bSJan Blunck path_put(&nd.path); 25212ad94ae6SAl Viro putname(to); 25221da177e4SLinus Torvalds out: 25232d8f3038SAl Viro path_put(&old_path); 25241da177e4SLinus Torvalds 25251da177e4SLinus Torvalds return error; 25261da177e4SLinus Torvalds } 25271da177e4SLinus Torvalds 25283480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 25295590ff0dSUlrich Drepper { 2530c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 25315590ff0dSUlrich Drepper } 25325590ff0dSUlrich Drepper 25331da177e4SLinus Torvalds /* 25341da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 25351da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 25361da177e4SLinus Torvalds * Problems: 25371da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 25381da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 25391da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 2540a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 25411da177e4SLinus Torvalds * story. 25421da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 25431b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 25441da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 25451da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 2546a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 25471da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 25481da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 25491da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 2550a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 25511da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 25521da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 25531da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 25541da177e4SLinus Torvalds * d) some filesystems don't support opened-but-unlinked directories, 25551da177e4SLinus Torvalds * either because of layout or because they are not ready to deal with 25561da177e4SLinus Torvalds * all cases correctly. The latter will be fixed (taking this sort of 25571da177e4SLinus Torvalds * stuff into VFS), but the former is not going away. Solution: the same 25581da177e4SLinus Torvalds * trick as in rmdir(). 25591da177e4SLinus Torvalds * e) conversion from fhandle to dentry may come in the wrong moment - when 25601b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 25611da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 25621b1dcc1bSJes Sorensen * ->i_mutex on parents, which works but leads to some truely excessive 25631da177e4SLinus Torvalds * locking]. 25641da177e4SLinus Torvalds */ 256575c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 25661da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 25671da177e4SLinus Torvalds { 25681da177e4SLinus Torvalds int error = 0; 25691da177e4SLinus Torvalds struct inode *target; 25701da177e4SLinus Torvalds 25711da177e4SLinus Torvalds /* 25721da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 25731da177e4SLinus Torvalds * we'll need to flip '..'. 25741da177e4SLinus Torvalds */ 25751da177e4SLinus Torvalds if (new_dir != old_dir) { 2576f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 25771da177e4SLinus Torvalds if (error) 25781da177e4SLinus Torvalds return error; 25791da177e4SLinus Torvalds } 25801da177e4SLinus Torvalds 25811da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 25821da177e4SLinus Torvalds if (error) 25831da177e4SLinus Torvalds return error; 25841da177e4SLinus Torvalds 25851da177e4SLinus Torvalds target = new_dentry->d_inode; 25861da177e4SLinus Torvalds if (target) { 25871b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 25881da177e4SLinus Torvalds dentry_unhash(new_dentry); 25891da177e4SLinus Torvalds } 25901da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 25911da177e4SLinus Torvalds error = -EBUSY; 25921da177e4SLinus Torvalds else 25931da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 25941da177e4SLinus Torvalds if (target) { 25951da177e4SLinus Torvalds if (!error) 25961da177e4SLinus Torvalds target->i_flags |= S_DEAD; 25971b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 25981da177e4SLinus Torvalds if (d_unhashed(new_dentry)) 25991da177e4SLinus Torvalds d_rehash(new_dentry); 26001da177e4SLinus Torvalds dput(new_dentry); 26011da177e4SLinus Torvalds } 2602e31e14ecSStephen Smalley if (!error) 2603349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 26041da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 26051da177e4SLinus Torvalds return error; 26061da177e4SLinus Torvalds } 26071da177e4SLinus Torvalds 260875c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 26091da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 26101da177e4SLinus Torvalds { 26111da177e4SLinus Torvalds struct inode *target; 26121da177e4SLinus Torvalds int error; 26131da177e4SLinus Torvalds 26141da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 26151da177e4SLinus Torvalds if (error) 26161da177e4SLinus Torvalds return error; 26171da177e4SLinus Torvalds 26181da177e4SLinus Torvalds dget(new_dentry); 26191da177e4SLinus Torvalds target = new_dentry->d_inode; 26201da177e4SLinus Torvalds if (target) 26211b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 26221da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 26231da177e4SLinus Torvalds error = -EBUSY; 26241da177e4SLinus Torvalds else 26251da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 26261da177e4SLinus Torvalds if (!error) { 2627bec1052eSAl Viro if (target) 2628bec1052eSAl Viro target->i_flags |= S_DEAD; 2629349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 26301da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 26311da177e4SLinus Torvalds } 26321da177e4SLinus Torvalds if (target) 26331b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 26341da177e4SLinus Torvalds dput(new_dentry); 26351da177e4SLinus Torvalds return error; 26361da177e4SLinus Torvalds } 26371da177e4SLinus Torvalds 26381da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 26391da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 26401da177e4SLinus Torvalds { 26411da177e4SLinus Torvalds int error; 26421da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 26430eeca283SRobert Love const char *old_name; 26441da177e4SLinus Torvalds 26451da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 26461da177e4SLinus Torvalds return 0; 26471da177e4SLinus Torvalds 26481da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 26491da177e4SLinus Torvalds if (error) 26501da177e4SLinus Torvalds return error; 26511da177e4SLinus Torvalds 26521da177e4SLinus Torvalds if (!new_dentry->d_inode) 2653a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 26541da177e4SLinus Torvalds else 26551da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 26561da177e4SLinus Torvalds if (error) 26571da177e4SLinus Torvalds return error; 26581da177e4SLinus Torvalds 2659acfa4380SAl Viro if (!old_dir->i_op->rename) 26601da177e4SLinus Torvalds return -EPERM; 26611da177e4SLinus Torvalds 26629e3509e2SJan Kara vfs_dq_init(old_dir); 26639e3509e2SJan Kara vfs_dq_init(new_dir); 26641da177e4SLinus Torvalds 26650eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 26660eeca283SRobert Love 26671da177e4SLinus Torvalds if (is_dir) 26681da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 26691da177e4SLinus Torvalds else 26701da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 2671123df294SAl Viro if (!error) 2672123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 26735a190ae6SAl Viro new_dentry->d_inode, old_dentry); 26740eeca283SRobert Love fsnotify_oldname_free(old_name); 26750eeca283SRobert Love 26761da177e4SLinus Torvalds return error; 26771da177e4SLinus Torvalds } 26781da177e4SLinus Torvalds 26792e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 26802e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 26811da177e4SLinus Torvalds { 26821da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 26831da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 26841da177e4SLinus Torvalds struct dentry *trap; 26851da177e4SLinus Torvalds struct nameidata oldnd, newnd; 26862ad94ae6SAl Viro char *from; 26872ad94ae6SAl Viro char *to; 26882ad94ae6SAl Viro int error; 26891da177e4SLinus Torvalds 26902ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 26911da177e4SLinus Torvalds if (error) 26921da177e4SLinus Torvalds goto exit; 26931da177e4SLinus Torvalds 26942ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 26951da177e4SLinus Torvalds if (error) 26961da177e4SLinus Torvalds goto exit1; 26971da177e4SLinus Torvalds 26981da177e4SLinus Torvalds error = -EXDEV; 26994ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 27001da177e4SLinus Torvalds goto exit2; 27011da177e4SLinus Torvalds 27024ac91378SJan Blunck old_dir = oldnd.path.dentry; 27031da177e4SLinus Torvalds error = -EBUSY; 27041da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 27051da177e4SLinus Torvalds goto exit2; 27061da177e4SLinus Torvalds 27074ac91378SJan Blunck new_dir = newnd.path.dentry; 27081da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 27091da177e4SLinus Torvalds goto exit2; 27101da177e4SLinus Torvalds 27110612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 27120612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 27134e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 27140612d9fbSOGAWA Hirofumi 27151da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 27161da177e4SLinus Torvalds 271749705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 27181da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 27191da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 27201da177e4SLinus Torvalds goto exit3; 27211da177e4SLinus Torvalds /* source must exist */ 27221da177e4SLinus Torvalds error = -ENOENT; 27231da177e4SLinus Torvalds if (!old_dentry->d_inode) 27241da177e4SLinus Torvalds goto exit4; 27251da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 27261da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 27271da177e4SLinus Torvalds error = -ENOTDIR; 27281da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 27291da177e4SLinus Torvalds goto exit4; 27301da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 27311da177e4SLinus Torvalds goto exit4; 27321da177e4SLinus Torvalds } 27331da177e4SLinus Torvalds /* source should not be ancestor of target */ 27341da177e4SLinus Torvalds error = -EINVAL; 27351da177e4SLinus Torvalds if (old_dentry == trap) 27361da177e4SLinus Torvalds goto exit4; 273749705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 27381da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 27391da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 27401da177e4SLinus Torvalds goto exit4; 27411da177e4SLinus Torvalds /* target should not be an ancestor of source */ 27421da177e4SLinus Torvalds error = -ENOTEMPTY; 27431da177e4SLinus Torvalds if (new_dentry == trap) 27441da177e4SLinus Torvalds goto exit5; 27451da177e4SLinus Torvalds 27469079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 27479079b1ebSDave Hansen if (error) 27489079b1ebSDave Hansen goto exit5; 2749be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 2750be6d3e56SKentaro Takeda &newnd.path, new_dentry); 2751be6d3e56SKentaro Takeda if (error) 2752be6d3e56SKentaro Takeda goto exit6; 27531da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 27541da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 2755be6d3e56SKentaro Takeda exit6: 27569079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 27571da177e4SLinus Torvalds exit5: 27581da177e4SLinus Torvalds dput(new_dentry); 27591da177e4SLinus Torvalds exit4: 27601da177e4SLinus Torvalds dput(old_dentry); 27611da177e4SLinus Torvalds exit3: 27621da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 27631da177e4SLinus Torvalds exit2: 27641d957f9bSJan Blunck path_put(&newnd.path); 27652ad94ae6SAl Viro putname(to); 27661da177e4SLinus Torvalds exit1: 27671d957f9bSJan Blunck path_put(&oldnd.path); 27681da177e4SLinus Torvalds putname(from); 27692ad94ae6SAl Viro exit: 27701da177e4SLinus Torvalds return error; 27711da177e4SLinus Torvalds } 27721da177e4SLinus Torvalds 2773a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 27745590ff0dSUlrich Drepper { 27755590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 27765590ff0dSUlrich Drepper } 27775590ff0dSUlrich Drepper 27781da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 27791da177e4SLinus Torvalds { 27801da177e4SLinus Torvalds int len; 27811da177e4SLinus Torvalds 27821da177e4SLinus Torvalds len = PTR_ERR(link); 27831da177e4SLinus Torvalds if (IS_ERR(link)) 27841da177e4SLinus Torvalds goto out; 27851da177e4SLinus Torvalds 27861da177e4SLinus Torvalds len = strlen(link); 27871da177e4SLinus Torvalds if (len > (unsigned) buflen) 27881da177e4SLinus Torvalds len = buflen; 27891da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 27901da177e4SLinus Torvalds len = -EFAULT; 27911da177e4SLinus Torvalds out: 27921da177e4SLinus Torvalds return len; 27931da177e4SLinus Torvalds } 27941da177e4SLinus Torvalds 27951da177e4SLinus Torvalds /* 27961da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 27971da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 27981da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 27991da177e4SLinus Torvalds */ 28001da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 28011da177e4SLinus Torvalds { 28021da177e4SLinus Torvalds struct nameidata nd; 2803cc314eefSLinus Torvalds void *cookie; 2804694a1764SMarcin Slusarz int res; 2805cc314eefSLinus Torvalds 28061da177e4SLinus Torvalds nd.depth = 0; 2807cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 2808694a1764SMarcin Slusarz if (IS_ERR(cookie)) 2809694a1764SMarcin Slusarz return PTR_ERR(cookie); 2810694a1764SMarcin Slusarz 2811694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 28121da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 2813cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 2814694a1764SMarcin Slusarz return res; 28151da177e4SLinus Torvalds } 28161da177e4SLinus Torvalds 28171da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 28181da177e4SLinus Torvalds { 28191da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 28201da177e4SLinus Torvalds } 28211da177e4SLinus Torvalds 28221da177e4SLinus Torvalds /* get the link contents into pagecache */ 28231da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 28241da177e4SLinus Torvalds { 2825ebd09abbSDuane Griffin char *kaddr; 28261da177e4SLinus Torvalds struct page *page; 28271da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 2828090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 28291da177e4SLinus Torvalds if (IS_ERR(page)) 28306fe6900eSNick Piggin return (char*)page; 28311da177e4SLinus Torvalds *ppage = page; 2832ebd09abbSDuane Griffin kaddr = kmap(page); 2833ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 2834ebd09abbSDuane Griffin return kaddr; 28351da177e4SLinus Torvalds } 28361da177e4SLinus Torvalds 28371da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 28381da177e4SLinus Torvalds { 28391da177e4SLinus Torvalds struct page *page = NULL; 28401da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 28411da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 28421da177e4SLinus Torvalds if (page) { 28431da177e4SLinus Torvalds kunmap(page); 28441da177e4SLinus Torvalds page_cache_release(page); 28451da177e4SLinus Torvalds } 28461da177e4SLinus Torvalds return res; 28471da177e4SLinus Torvalds } 28481da177e4SLinus Torvalds 2849cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 28501da177e4SLinus Torvalds { 2851cc314eefSLinus Torvalds struct page *page = NULL; 28521da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 2853cc314eefSLinus Torvalds return page; 28541da177e4SLinus Torvalds } 28551da177e4SLinus Torvalds 2856cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 28571da177e4SLinus Torvalds { 2858cc314eefSLinus Torvalds struct page *page = cookie; 2859cc314eefSLinus Torvalds 2860cc314eefSLinus Torvalds if (page) { 28611da177e4SLinus Torvalds kunmap(page); 28621da177e4SLinus Torvalds page_cache_release(page); 28631da177e4SLinus Torvalds } 28641da177e4SLinus Torvalds } 28651da177e4SLinus Torvalds 286654566b2cSNick Piggin /* 286754566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 286854566b2cSNick Piggin */ 286954566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 28701da177e4SLinus Torvalds { 28711da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 28720adb25d2SKirill Korotaev struct page *page; 2873afddba49SNick Piggin void *fsdata; 2874beb497abSDmitriy Monakhov int err; 28751da177e4SLinus Torvalds char *kaddr; 287654566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 287754566b2cSNick Piggin if (nofs) 287854566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 28791da177e4SLinus Torvalds 28807e53cac4SNeilBrown retry: 2881afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 288254566b2cSNick Piggin flags, &page, &fsdata); 28831da177e4SLinus Torvalds if (err) 2884afddba49SNick Piggin goto fail; 2885afddba49SNick Piggin 28861da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 28871da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 28881da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 2889afddba49SNick Piggin 2890afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 2891afddba49SNick Piggin page, fsdata); 28921da177e4SLinus Torvalds if (err < 0) 28931da177e4SLinus Torvalds goto fail; 2894afddba49SNick Piggin if (err < len-1) 2895afddba49SNick Piggin goto retry; 2896afddba49SNick Piggin 28971da177e4SLinus Torvalds mark_inode_dirty(inode); 28981da177e4SLinus Torvalds return 0; 28991da177e4SLinus Torvalds fail: 29001da177e4SLinus Torvalds return err; 29011da177e4SLinus Torvalds } 29021da177e4SLinus Torvalds 29030adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 29040adb25d2SKirill Korotaev { 29050adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 290654566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 29070adb25d2SKirill Korotaev } 29080adb25d2SKirill Korotaev 290992e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 29101da177e4SLinus Torvalds .readlink = generic_readlink, 29111da177e4SLinus Torvalds .follow_link = page_follow_link_light, 29121da177e4SLinus Torvalds .put_link = page_put_link, 29131da177e4SLinus Torvalds }; 29141da177e4SLinus Torvalds 29152d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 29161da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 29171da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 29181da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 29191da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 29201da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 29211da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 29221da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 29231da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 29241da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 29250adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 29261da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 29271da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 29281da177e4SLinus Torvalds EXPORT_SYMBOL(path_lookup); 2929d1811465SAl Viro EXPORT_SYMBOL(kern_path); 293016f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 2931f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 29328c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission); 29331da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 29341da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 29351da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 29361da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 29371da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 29381da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 29391da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 29401da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 29411da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 29421da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 29431da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 29441da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 29451da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 29461da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 2947