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 #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE]) 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer] 431da177e4SLinus Torvalds * Fundamental changes in the pathname lookup mechanisms (namei) 441da177e4SLinus Torvalds * were necessary because of omirr. The reason is that omirr needs 451da177e4SLinus Torvalds * to know the _real_ pathname, not the user-supplied one, in case 461da177e4SLinus Torvalds * of symlinks (and also when transname replacements occur). 471da177e4SLinus Torvalds * 481da177e4SLinus Torvalds * The new code replaces the old recursive symlink resolution with 491da177e4SLinus Torvalds * an iterative one (in case of non-nested symlink chains). It does 501da177e4SLinus Torvalds * this with calls to <fs>_follow_link(). 511da177e4SLinus Torvalds * As a side effect, dir_namei(), _namei() and follow_link() are now 521da177e4SLinus Torvalds * replaced with a single function lookup_dentry() that can handle all 531da177e4SLinus Torvalds * the special cases of the former code. 541da177e4SLinus Torvalds * 551da177e4SLinus Torvalds * With the new dcache, the pathname is stored at each inode, at least as 561da177e4SLinus Torvalds * long as the refcount of the inode is positive. As a side effect, the 571da177e4SLinus Torvalds * size of the dcache depends on the inode cache and thus is dynamic. 581da177e4SLinus Torvalds * 591da177e4SLinus Torvalds * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink 601da177e4SLinus Torvalds * resolution to correspond with current state of the code. 611da177e4SLinus Torvalds * 621da177e4SLinus Torvalds * Note that the symlink resolution is not *completely* iterative. 631da177e4SLinus Torvalds * There is still a significant amount of tail- and mid- recursion in 641da177e4SLinus Torvalds * the algorithm. Also, note that <fs>_readlink() is not used in 651da177e4SLinus Torvalds * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() 661da177e4SLinus Torvalds * may return different results than <fs>_follow_link(). Many virtual 671da177e4SLinus Torvalds * filesystems (including /proc) exhibit this behavior. 681da177e4SLinus Torvalds */ 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: 711da177e4SLinus Torvalds * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL 721da177e4SLinus Torvalds * and the name already exists in form of a symlink, try to create the new 731da177e4SLinus Torvalds * name indicated by the symlink. The old code always complained that the 741da177e4SLinus Torvalds * name already exists, due to not following the symlink even if its target 751da177e4SLinus Torvalds * is nonexistent. The new semantics affects also mknod() and link() when 761da177e4SLinus Torvalds * the name is a symlink pointing to a non-existant name. 771da177e4SLinus Torvalds * 781da177e4SLinus Torvalds * I don't know which semantics is the right one, since I have no access 791da177e4SLinus Torvalds * to standards. But I found by trial that HP-UX 9.0 has the full "new" 801da177e4SLinus Torvalds * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the 811da177e4SLinus Torvalds * "old" one. Personally, I think the new semantics is much more logical. 821da177e4SLinus Torvalds * Note that "ln old new" where "new" is a symlink pointing to a non-existing 831da177e4SLinus Torvalds * file does succeed in both HP-UX and SunOs, but not in Solaris 841da177e4SLinus Torvalds * and in the old Linux semantics. 851da177e4SLinus Torvalds */ 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink 881da177e4SLinus Torvalds * semantics. See the comments in "open_namei" and "do_link" below. 891da177e4SLinus Torvalds * 901da177e4SLinus Torvalds * [10-Sep-98 Alan Modra] Another symlink change. 911da177e4SLinus Torvalds */ 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: 941da177e4SLinus Torvalds * inside the path - always follow. 951da177e4SLinus Torvalds * in the last component in creation/removal/renaming - never follow. 961da177e4SLinus Torvalds * if LOOKUP_FOLLOW passed - follow. 971da177e4SLinus Torvalds * if the pathname has trailing slashes - follow. 981da177e4SLinus Torvalds * otherwise - don't follow. 991da177e4SLinus Torvalds * (applied in that order). 1001da177e4SLinus Torvalds * 1011da177e4SLinus Torvalds * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT 1021da177e4SLinus Torvalds * restored for 2.4. This is the last surviving part of old 4.2BSD bug. 1031da177e4SLinus Torvalds * During the 2.4 we need to fix the userland stuff depending on it - 1041da177e4SLinus Torvalds * hopefully we will be able to get rid of that wart in 2.5. So far only 1051da177e4SLinus Torvalds * XEmacs seems to be relying on it... 1061da177e4SLinus Torvalds */ 1071da177e4SLinus Torvalds /* 1081da177e4SLinus Torvalds * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) 109a11f3a05SArjan van de Ven * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives 1101da177e4SLinus Torvalds * any extra contention... 1111da177e4SLinus Torvalds */ 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional 1141da177e4SLinus Torvalds * checking and hopefully speeding things up, we copy filenames to the 1151da177e4SLinus Torvalds * kernel data space before using them.. 1161da177e4SLinus Torvalds * 1171da177e4SLinus Torvalds * POSIX.1 2.4: an empty pathname is invalid (ENOENT). 1181da177e4SLinus Torvalds * PATH_MAX includes the nul terminator --RR. 1191da177e4SLinus Torvalds */ 120858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page) 1211da177e4SLinus Torvalds { 1221da177e4SLinus Torvalds int retval; 1231da177e4SLinus Torvalds unsigned long len = PATH_MAX; 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds if (!segment_eq(get_fs(), KERNEL_DS)) { 1261da177e4SLinus Torvalds if ((unsigned long) filename >= TASK_SIZE) 1271da177e4SLinus Torvalds return -EFAULT; 1281da177e4SLinus Torvalds if (TASK_SIZE - (unsigned long) filename < PATH_MAX) 1291da177e4SLinus Torvalds len = TASK_SIZE - (unsigned long) filename; 1301da177e4SLinus Torvalds } 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds retval = strncpy_from_user(page, filename, len); 1331da177e4SLinus Torvalds if (retval > 0) { 1341da177e4SLinus Torvalds if (retval < len) 1351da177e4SLinus Torvalds return 0; 1361da177e4SLinus Torvalds return -ENAMETOOLONG; 1371da177e4SLinus Torvalds } else if (!retval) 1381da177e4SLinus Torvalds retval = -ENOENT; 1391da177e4SLinus Torvalds return retval; 1401da177e4SLinus Torvalds } 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds char * getname(const char __user * filename) 1431da177e4SLinus Torvalds { 1441da177e4SLinus Torvalds char *tmp, *result; 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds result = ERR_PTR(-ENOMEM); 1471da177e4SLinus Torvalds tmp = __getname(); 1481da177e4SLinus Torvalds if (tmp) { 1491da177e4SLinus Torvalds int retval = do_getname(filename, tmp); 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds result = tmp; 1521da177e4SLinus Torvalds if (retval < 0) { 1531da177e4SLinus Torvalds __putname(tmp); 1541da177e4SLinus Torvalds result = ERR_PTR(retval); 1551da177e4SLinus Torvalds } 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds audit_getname(result); 1581da177e4SLinus Torvalds return result; 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 1621da177e4SLinus Torvalds void putname(const char *name) 1631da177e4SLinus Torvalds { 1645ac3a9c2SAl Viro if (unlikely(!audit_dummy_context())) 1651da177e4SLinus Torvalds audit_putname(name); 1661da177e4SLinus Torvalds else 1671da177e4SLinus Torvalds __putname(name); 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds EXPORT_SYMBOL(putname); 1701da177e4SLinus Torvalds #endif 1711da177e4SLinus Torvalds 1725909ccaaSLinus Torvalds /* 1735909ccaaSLinus Torvalds * This does basic POSIX ACL permission checking 1745909ccaaSLinus Torvalds */ 1755909ccaaSLinus Torvalds static int acl_permission_check(struct inode *inode, int mask, 1765909ccaaSLinus Torvalds int (*check_acl)(struct inode *inode, int mask)) 1775909ccaaSLinus Torvalds { 1785909ccaaSLinus Torvalds umode_t mode = inode->i_mode; 1795909ccaaSLinus Torvalds 1805909ccaaSLinus Torvalds mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 1815909ccaaSLinus Torvalds 1825909ccaaSLinus Torvalds if (current_fsuid() == inode->i_uid) 1835909ccaaSLinus Torvalds mode >>= 6; 1845909ccaaSLinus Torvalds else { 1855909ccaaSLinus Torvalds if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) { 1865909ccaaSLinus Torvalds int error = check_acl(inode, mask); 1875909ccaaSLinus Torvalds if (error != -EAGAIN) 1885909ccaaSLinus Torvalds return error; 1895909ccaaSLinus Torvalds } 1905909ccaaSLinus Torvalds 1915909ccaaSLinus Torvalds if (in_group_p(inode->i_gid)) 1925909ccaaSLinus Torvalds mode >>= 3; 1935909ccaaSLinus Torvalds } 1945909ccaaSLinus Torvalds 1955909ccaaSLinus Torvalds /* 1965909ccaaSLinus Torvalds * If the DACs are ok we don't need any capability check. 1975909ccaaSLinus Torvalds */ 1985909ccaaSLinus Torvalds if ((mask & ~mode) == 0) 1995909ccaaSLinus Torvalds return 0; 2005909ccaaSLinus Torvalds return -EACCES; 2015909ccaaSLinus Torvalds } 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds /** 2041da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 2051da177e4SLinus Torvalds * @inode: inode to check access rights for 2061da177e4SLinus Torvalds * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 2071da177e4SLinus Torvalds * @check_acl: optional callback to check for Posix ACLs 2081da177e4SLinus Torvalds * 2091da177e4SLinus Torvalds * Used to check for read/write/execute permissions on a file. 2101da177e4SLinus Torvalds * We use "fsuid" for this, letting us set arbitrary permissions 2111da177e4SLinus Torvalds * for filesystem access without changing the "normal" uids which 2121da177e4SLinus Torvalds * are used for other things.. 2131da177e4SLinus Torvalds */ 2141da177e4SLinus Torvalds int generic_permission(struct inode *inode, int mask, 2151da177e4SLinus Torvalds int (*check_acl)(struct inode *inode, int mask)) 2161da177e4SLinus Torvalds { 2175909ccaaSLinus Torvalds int ret; 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds /* 2205909ccaaSLinus Torvalds * Do the basic POSIX ACL permission checks. 2211da177e4SLinus Torvalds */ 2225909ccaaSLinus Torvalds ret = acl_permission_check(inode, mask, check_acl); 2235909ccaaSLinus Torvalds if (ret != -EACCES) 2245909ccaaSLinus Torvalds return ret; 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds /* 2271da177e4SLinus Torvalds * Read/write DACs are always overridable. 2281da177e4SLinus Torvalds * Executable DACs are overridable if at least one exec bit is set. 2291da177e4SLinus Torvalds */ 230f696a365SMiklos Szeredi if (!(mask & MAY_EXEC) || execute_ok(inode)) 2311da177e4SLinus Torvalds if (capable(CAP_DAC_OVERRIDE)) 2321da177e4SLinus Torvalds return 0; 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds /* 2351da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 2361da177e4SLinus Torvalds */ 2371da177e4SLinus Torvalds if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE))) 2381da177e4SLinus Torvalds if (capable(CAP_DAC_READ_SEARCH)) 2391da177e4SLinus Torvalds return 0; 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds return -EACCES; 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds 244cb23beb5SChristoph Hellwig /** 245cb23beb5SChristoph Hellwig * inode_permission - check for access rights to a given inode 246cb23beb5SChristoph Hellwig * @inode: inode to check permission on 247cb23beb5SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 248cb23beb5SChristoph Hellwig * 249cb23beb5SChristoph Hellwig * Used to check for read/write/execute permissions on an inode. 250cb23beb5SChristoph Hellwig * We use "fsuid" for this, letting us set arbitrary permissions 251cb23beb5SChristoph Hellwig * for filesystem access without changing the "normal" uids which 252cb23beb5SChristoph Hellwig * are used for other things. 253cb23beb5SChristoph Hellwig */ 254f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask) 2551da177e4SLinus Torvalds { 256e6305c43SAl Viro int retval; 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds if (mask & MAY_WRITE) { 25922590e41SMiklos Szeredi umode_t mode = inode->i_mode; 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds /* 2621da177e4SLinus Torvalds * Nobody gets write access to a read-only fs. 2631da177e4SLinus Torvalds */ 2641da177e4SLinus Torvalds if (IS_RDONLY(inode) && 2651da177e4SLinus Torvalds (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 2661da177e4SLinus Torvalds return -EROFS; 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds /* 2691da177e4SLinus Torvalds * Nobody gets write access to an immutable file. 2701da177e4SLinus Torvalds */ 2711da177e4SLinus Torvalds if (IS_IMMUTABLE(inode)) 2721da177e4SLinus Torvalds return -EACCES; 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds 275acfa4380SAl Viro if (inode->i_op->permission) 276b77b0646SAl Viro retval = inode->i_op->permission(inode, mask); 277f696a365SMiklos Szeredi else 2785909ccaaSLinus Torvalds retval = generic_permission(inode, mask, inode->i_op->check_acl); 279f696a365SMiklos Szeredi 2801da177e4SLinus Torvalds if (retval) 2811da177e4SLinus Torvalds return retval; 2821da177e4SLinus Torvalds 28308ce5f16SSerge E. Hallyn retval = devcgroup_inode_permission(inode, mask); 28408ce5f16SSerge E. Hallyn if (retval) 28508ce5f16SSerge E. Hallyn return retval; 28608ce5f16SSerge E. Hallyn 287e6305c43SAl Viro return security_inode_permission(inode, 288f418b006SStephen Smalley mask & (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND)); 2891da177e4SLinus Torvalds } 2901da177e4SLinus Torvalds 291e4543eddSChristoph Hellwig /** 2928c744fb8SChristoph Hellwig * file_permission - check for additional access rights to a given file 2938c744fb8SChristoph Hellwig * @file: file to check access rights for 2948c744fb8SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 2958c744fb8SChristoph Hellwig * 2968c744fb8SChristoph Hellwig * Used to check for read/write/execute permissions on an already opened 2978c744fb8SChristoph Hellwig * file. 2988c744fb8SChristoph Hellwig * 2998c744fb8SChristoph Hellwig * Note: 3008c744fb8SChristoph Hellwig * Do not use this function in new code. All access checks should 301cb23beb5SChristoph Hellwig * be done using inode_permission(). 3028c744fb8SChristoph Hellwig */ 3038c744fb8SChristoph Hellwig int file_permission(struct file *file, int mask) 3048c744fb8SChristoph Hellwig { 305f419a2e3SAl Viro return inode_permission(file->f_path.dentry->d_inode, mask); 3068c744fb8SChristoph Hellwig } 3078c744fb8SChristoph Hellwig 3081da177e4SLinus Torvalds /* 3091da177e4SLinus Torvalds * get_write_access() gets write permission for a file. 3101da177e4SLinus Torvalds * put_write_access() releases this write permission. 3111da177e4SLinus Torvalds * This is used for regular files. 3121da177e4SLinus Torvalds * We cannot support write (and maybe mmap read-write shared) accesses and 3131da177e4SLinus Torvalds * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode 3141da177e4SLinus Torvalds * can have the following values: 3151da177e4SLinus Torvalds * 0: no writers, no VM_DENYWRITE mappings 3161da177e4SLinus Torvalds * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist 3171da177e4SLinus Torvalds * > 0: (i_writecount) users are writing to the file. 3181da177e4SLinus Torvalds * 3191da177e4SLinus Torvalds * Normally we operate on that counter with atomic_{inc,dec} and it's safe 3201da177e4SLinus Torvalds * except for the cases where we don't hold i_writecount yet. Then we need to 3211da177e4SLinus Torvalds * use {get,deny}_write_access() - these functions check the sign and refuse 3221da177e4SLinus Torvalds * to do the change if sign is wrong. Exclusion between them is provided by 3231da177e4SLinus Torvalds * the inode->i_lock spinlock. 3241da177e4SLinus Torvalds */ 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds int get_write_access(struct inode * inode) 3271da177e4SLinus Torvalds { 3281da177e4SLinus Torvalds spin_lock(&inode->i_lock); 3291da177e4SLinus Torvalds if (atomic_read(&inode->i_writecount) < 0) { 3301da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3311da177e4SLinus Torvalds return -ETXTBSY; 3321da177e4SLinus Torvalds } 3331da177e4SLinus Torvalds atomic_inc(&inode->i_writecount); 3341da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds return 0; 3371da177e4SLinus Torvalds } 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds int deny_write_access(struct file * file) 3401da177e4SLinus Torvalds { 3410f7fc9e4SJosef "Jeff" Sipek struct inode *inode = file->f_path.dentry->d_inode; 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds spin_lock(&inode->i_lock); 3441da177e4SLinus Torvalds if (atomic_read(&inode->i_writecount) > 0) { 3451da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3461da177e4SLinus Torvalds return -ETXTBSY; 3471da177e4SLinus Torvalds } 3481da177e4SLinus Torvalds atomic_dec(&inode->i_writecount); 3491da177e4SLinus Torvalds spin_unlock(&inode->i_lock); 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds return 0; 3521da177e4SLinus Torvalds } 3531da177e4SLinus Torvalds 3541d957f9bSJan Blunck /** 3555dd784d0SJan Blunck * path_get - get a reference to a path 3565dd784d0SJan Blunck * @path: path to get the reference to 3575dd784d0SJan Blunck * 3585dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 3595dd784d0SJan Blunck */ 3605dd784d0SJan Blunck void path_get(struct path *path) 3615dd784d0SJan Blunck { 3625dd784d0SJan Blunck mntget(path->mnt); 3635dd784d0SJan Blunck dget(path->dentry); 3645dd784d0SJan Blunck } 3655dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 3665dd784d0SJan Blunck 3675dd784d0SJan Blunck /** 3681d957f9bSJan Blunck * path_put - put a reference to a path 3691d957f9bSJan Blunck * @path: path to put the reference to 3701d957f9bSJan Blunck * 3711d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 3721d957f9bSJan Blunck */ 3731d957f9bSJan Blunck void path_put(struct path *path) 3741da177e4SLinus Torvalds { 3751d957f9bSJan Blunck dput(path->dentry); 3761d957f9bSJan Blunck mntput(path->mnt); 3771da177e4SLinus Torvalds } 3781d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 3791da177e4SLinus Torvalds 380834f2a4aSTrond Myklebust /** 381834f2a4aSTrond Myklebust * release_open_intent - free up open intent resources 382834f2a4aSTrond Myklebust * @nd: pointer to nameidata 383834f2a4aSTrond Myklebust */ 384834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd) 385834f2a4aSTrond Myklebust { 3860f7fc9e4SJosef "Jeff" Sipek if (nd->intent.open.file->f_path.dentry == NULL) 387834f2a4aSTrond Myklebust put_filp(nd->intent.open.file); 388834f2a4aSTrond Myklebust else 389834f2a4aSTrond Myklebust fput(nd->intent.open.file); 390834f2a4aSTrond Myklebust } 391834f2a4aSTrond Myklebust 392bcdc5e01SIan Kent static inline struct dentry * 393bcdc5e01SIan Kent do_revalidate(struct dentry *dentry, struct nameidata *nd) 394bcdc5e01SIan Kent { 395bcdc5e01SIan Kent int status = dentry->d_op->d_revalidate(dentry, nd); 396bcdc5e01SIan Kent if (unlikely(status <= 0)) { 397bcdc5e01SIan Kent /* 398bcdc5e01SIan Kent * The dentry failed validation. 399bcdc5e01SIan Kent * If d_revalidate returned 0 attempt to invalidate 400bcdc5e01SIan Kent * the dentry otherwise d_revalidate is asking us 401bcdc5e01SIan Kent * to return a fail status. 402bcdc5e01SIan Kent */ 403bcdc5e01SIan Kent if (!status) { 404bcdc5e01SIan Kent if (!d_invalidate(dentry)) { 405bcdc5e01SIan Kent dput(dentry); 406bcdc5e01SIan Kent dentry = NULL; 407bcdc5e01SIan Kent } 408bcdc5e01SIan Kent } else { 409bcdc5e01SIan Kent dput(dentry); 410bcdc5e01SIan Kent dentry = ERR_PTR(status); 411bcdc5e01SIan Kent } 412bcdc5e01SIan Kent } 413bcdc5e01SIan Kent return dentry; 414bcdc5e01SIan Kent } 415bcdc5e01SIan Kent 4161da177e4SLinus Torvalds /* 417b75b5086SAl Viro * Short-cut version of permission(), for calling on directories 418b75b5086SAl Viro * during pathname resolution. Combines parts of permission() 419b75b5086SAl Viro * and generic_permission(), and tests ONLY for MAY_EXEC permission. 4201da177e4SLinus Torvalds * 4211da177e4SLinus Torvalds * If appropriate, check DAC only. If not appropriate, or 422b75b5086SAl Viro * short-cut DAC fails, then call ->permission() to do more 4231da177e4SLinus Torvalds * complete permission check. 4241da177e4SLinus Torvalds */ 425b75b5086SAl Viro static int exec_permission(struct inode *inode) 4261da177e4SLinus Torvalds { 4275909ccaaSLinus Torvalds int ret; 4281da177e4SLinus Torvalds 429cb9179eaSLinus Torvalds if (inode->i_op->permission) { 4305909ccaaSLinus Torvalds ret = inode->i_op->permission(inode, MAY_EXEC); 431cb9179eaSLinus Torvalds if (!ret) 432cb9179eaSLinus Torvalds goto ok; 433cb9179eaSLinus Torvalds return ret; 434cb9179eaSLinus Torvalds } 4355909ccaaSLinus Torvalds ret = acl_permission_check(inode, MAY_EXEC, inode->i_op->check_acl); 4365909ccaaSLinus Torvalds if (!ret) 4371da177e4SLinus Torvalds goto ok; 4381da177e4SLinus Torvalds 439f1ac9f6bSLinus Torvalds if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH)) 4401da177e4SLinus Torvalds goto ok; 4411da177e4SLinus Torvalds 4425909ccaaSLinus Torvalds return ret; 4431da177e4SLinus Torvalds ok: 444b77b0646SAl Viro return security_inode_permission(inode, MAY_EXEC); 4451da177e4SLinus Torvalds } 4461da177e4SLinus Torvalds 4472a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 4482a737871SAl Viro { 4492a737871SAl Viro if (!nd->root.mnt) { 4502a737871SAl Viro struct fs_struct *fs = current->fs; 4512a737871SAl Viro read_lock(&fs->lock); 4522a737871SAl Viro nd->root = fs->root; 4532a737871SAl Viro path_get(&nd->root); 4542a737871SAl Viro read_unlock(&fs->lock); 4552a737871SAl Viro } 4562a737871SAl Viro } 4572a737871SAl Viro 4586de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 4596de88d72SAl Viro 460f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 4611da177e4SLinus Torvalds { 4621da177e4SLinus Torvalds int res = 0; 4631da177e4SLinus Torvalds char *name; 4641da177e4SLinus Torvalds if (IS_ERR(link)) 4651da177e4SLinus Torvalds goto fail; 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds if (*link == '/') { 4682a737871SAl Viro set_root(nd); 4691d957f9bSJan Blunck path_put(&nd->path); 4702a737871SAl Viro nd->path = nd->root; 4712a737871SAl Viro path_get(&nd->root); 4721da177e4SLinus Torvalds } 473b4091d5fSChristoph Hellwig 4741da177e4SLinus Torvalds res = link_path_walk(link, nd); 4751da177e4SLinus Torvalds if (nd->depth || res || nd->last_type!=LAST_NORM) 4761da177e4SLinus Torvalds return res; 4771da177e4SLinus Torvalds /* 4781da177e4SLinus Torvalds * If it is an iterative symlinks resolution in open_namei() we 4791da177e4SLinus Torvalds * have to copy the last component. And all that crap because of 4801da177e4SLinus Torvalds * bloody create() on broken symlinks. Furrfu... 4811da177e4SLinus Torvalds */ 4821da177e4SLinus Torvalds name = __getname(); 4831da177e4SLinus Torvalds if (unlikely(!name)) { 4841d957f9bSJan Blunck path_put(&nd->path); 4851da177e4SLinus Torvalds return -ENOMEM; 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds strcpy(name, nd->last.name); 4881da177e4SLinus Torvalds nd->last.name = name; 4891da177e4SLinus Torvalds return 0; 4901da177e4SLinus Torvalds fail: 4911d957f9bSJan Blunck path_put(&nd->path); 4921da177e4SLinus Torvalds return PTR_ERR(link); 4931da177e4SLinus Torvalds } 4941da177e4SLinus Torvalds 4951d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 496051d3812SIan Kent { 497051d3812SIan Kent dput(path->dentry); 4984ac91378SJan Blunck if (path->mnt != nd->path.mnt) 499051d3812SIan Kent mntput(path->mnt); 500051d3812SIan Kent } 501051d3812SIan Kent 502051d3812SIan Kent static inline void path_to_nameidata(struct path *path, struct nameidata *nd) 503051d3812SIan Kent { 5044ac91378SJan Blunck dput(nd->path.dentry); 5054ac91378SJan Blunck if (nd->path.mnt != path->mnt) 5064ac91378SJan Blunck mntput(nd->path.mnt); 5074ac91378SJan Blunck nd->path.mnt = path->mnt; 5084ac91378SJan Blunck nd->path.dentry = path->dentry; 509051d3812SIan Kent } 510051d3812SIan Kent 511f1662356SArjan van de Ven static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd) 5121da177e4SLinus Torvalds { 5131da177e4SLinus Torvalds int error; 514cc314eefSLinus Torvalds void *cookie; 515cd4e91d3SAl Viro struct dentry *dentry = path->dentry; 5161da177e4SLinus Torvalds 517d671a1cbSAl Viro touch_atime(path->mnt, dentry); 5181da177e4SLinus Torvalds nd_set_link(nd, NULL); 519cd4e91d3SAl Viro 5204ac91378SJan Blunck if (path->mnt != nd->path.mnt) { 521051d3812SIan Kent path_to_nameidata(path, nd); 522051d3812SIan Kent dget(dentry); 523051d3812SIan Kent } 524cd4e91d3SAl Viro mntget(path->mnt); 525cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, nd); 526cc314eefSLinus Torvalds error = PTR_ERR(cookie); 527cc314eefSLinus Torvalds if (!IS_ERR(cookie)) { 5281da177e4SLinus Torvalds char *s = nd_get_link(nd); 529cc314eefSLinus Torvalds error = 0; 5301da177e4SLinus Torvalds if (s) 5311da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 5321da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 533cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, nd, cookie); 5341da177e4SLinus Torvalds } 5351da177e4SLinus Torvalds return error; 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds /* 5391da177e4SLinus Torvalds * This limits recursive symlink follows to 8, while 5401da177e4SLinus Torvalds * limiting consecutive symlinks to 40. 5411da177e4SLinus Torvalds * 5421da177e4SLinus Torvalds * Without that kind of total limit, nasty chains of consecutive 5431da177e4SLinus Torvalds * symlinks can cause almost arbitrarily long lookups. 5441da177e4SLinus Torvalds */ 54590ebe565SAl Viro static inline int do_follow_link(struct path *path, struct nameidata *nd) 5461da177e4SLinus Torvalds { 5471da177e4SLinus Torvalds int err = -ELOOP; 5481da177e4SLinus Torvalds if (current->link_count >= MAX_NESTED_LINKS) 5491da177e4SLinus Torvalds goto loop; 5501da177e4SLinus Torvalds if (current->total_link_count >= 40) 5511da177e4SLinus Torvalds goto loop; 5521da177e4SLinus Torvalds BUG_ON(nd->depth >= MAX_NESTED_LINKS); 5531da177e4SLinus Torvalds cond_resched(); 55490ebe565SAl Viro err = security_inode_follow_link(path->dentry, nd); 5551da177e4SLinus Torvalds if (err) 5561da177e4SLinus Torvalds goto loop; 5571da177e4SLinus Torvalds current->link_count++; 5581da177e4SLinus Torvalds current->total_link_count++; 5591da177e4SLinus Torvalds nd->depth++; 560cd4e91d3SAl Viro err = __do_follow_link(path, nd); 561258fa999SAl Viro path_put(path); 5621da177e4SLinus Torvalds current->link_count--; 5631da177e4SLinus Torvalds nd->depth--; 5641da177e4SLinus Torvalds return err; 5651da177e4SLinus Torvalds loop: 5661d957f9bSJan Blunck path_put_conditional(path, nd); 5671d957f9bSJan Blunck path_put(&nd->path); 5681da177e4SLinus Torvalds return err; 5691da177e4SLinus Torvalds } 5701da177e4SLinus Torvalds 571bab77ebfSAl Viro int follow_up(struct path *path) 5721da177e4SLinus Torvalds { 5731da177e4SLinus Torvalds struct vfsmount *parent; 5741da177e4SLinus Torvalds struct dentry *mountpoint; 5751da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 576bab77ebfSAl Viro parent = path->mnt->mnt_parent; 577bab77ebfSAl Viro if (parent == path->mnt) { 5781da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 5791da177e4SLinus Torvalds return 0; 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds mntget(parent); 582bab77ebfSAl Viro mountpoint = dget(path->mnt->mnt_mountpoint); 5831da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 584bab77ebfSAl Viro dput(path->dentry); 585bab77ebfSAl Viro path->dentry = mountpoint; 586bab77ebfSAl Viro mntput(path->mnt); 587bab77ebfSAl Viro path->mnt = parent; 5881da177e4SLinus Torvalds return 1; 5891da177e4SLinus Torvalds } 5901da177e4SLinus Torvalds 5911da177e4SLinus Torvalds /* no need for dcache_lock, as serialization is taken care in 5921da177e4SLinus Torvalds * namespace.c 5931da177e4SLinus Torvalds */ 594463ffb2eSAl Viro static int __follow_mount(struct path *path) 595463ffb2eSAl Viro { 596463ffb2eSAl Viro int res = 0; 597463ffb2eSAl Viro while (d_mountpoint(path->dentry)) { 5981c755af4SAl Viro struct vfsmount *mounted = lookup_mnt(path); 599463ffb2eSAl Viro if (!mounted) 600463ffb2eSAl Viro break; 601463ffb2eSAl Viro dput(path->dentry); 602463ffb2eSAl Viro if (res) 603463ffb2eSAl Viro mntput(path->mnt); 604463ffb2eSAl Viro path->mnt = mounted; 605463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 606463ffb2eSAl Viro res = 1; 607463ffb2eSAl Viro } 608463ffb2eSAl Viro return res; 609463ffb2eSAl Viro } 610463ffb2eSAl Viro 61179ed0226SAl Viro static void follow_mount(struct path *path) 6121da177e4SLinus Torvalds { 61379ed0226SAl Viro while (d_mountpoint(path->dentry)) { 6141c755af4SAl Viro struct vfsmount *mounted = lookup_mnt(path); 6151da177e4SLinus Torvalds if (!mounted) 6161da177e4SLinus Torvalds break; 61779ed0226SAl Viro dput(path->dentry); 61879ed0226SAl Viro mntput(path->mnt); 61979ed0226SAl Viro path->mnt = mounted; 62079ed0226SAl Viro path->dentry = dget(mounted->mnt_root); 6211da177e4SLinus Torvalds } 6221da177e4SLinus Torvalds } 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds /* no need for dcache_lock, as serialization is taken care in 6251da177e4SLinus Torvalds * namespace.c 6261da177e4SLinus Torvalds */ 6279393bd07SAl Viro int follow_down(struct path *path) 6281da177e4SLinus Torvalds { 6291da177e4SLinus Torvalds struct vfsmount *mounted; 6301da177e4SLinus Torvalds 6311c755af4SAl Viro mounted = lookup_mnt(path); 6321da177e4SLinus Torvalds if (mounted) { 6339393bd07SAl Viro dput(path->dentry); 6349393bd07SAl Viro mntput(path->mnt); 6359393bd07SAl Viro path->mnt = mounted; 6369393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 6371da177e4SLinus Torvalds return 1; 6381da177e4SLinus Torvalds } 6391da177e4SLinus Torvalds return 0; 6401da177e4SLinus Torvalds } 6411da177e4SLinus Torvalds 642f1662356SArjan van de Ven static __always_inline void follow_dotdot(struct nameidata *nd) 6431da177e4SLinus Torvalds { 6442a737871SAl Viro set_root(nd); 645e518ddb7SAndreas Mohr 6461da177e4SLinus Torvalds while(1) { 6471da177e4SLinus Torvalds struct vfsmount *parent; 6484ac91378SJan Blunck struct dentry *old = nd->path.dentry; 6491da177e4SLinus Torvalds 6502a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 6512a737871SAl Viro nd->path.mnt == nd->root.mnt) { 6521da177e4SLinus Torvalds break; 6531da177e4SLinus Torvalds } 6541da177e4SLinus Torvalds spin_lock(&dcache_lock); 6554ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 6564ac91378SJan Blunck nd->path.dentry = dget(nd->path.dentry->d_parent); 6571da177e4SLinus Torvalds spin_unlock(&dcache_lock); 6581da177e4SLinus Torvalds dput(old); 6591da177e4SLinus Torvalds break; 6601da177e4SLinus Torvalds } 6611da177e4SLinus Torvalds spin_unlock(&dcache_lock); 6621da177e4SLinus Torvalds spin_lock(&vfsmount_lock); 6634ac91378SJan Blunck parent = nd->path.mnt->mnt_parent; 6644ac91378SJan Blunck if (parent == nd->path.mnt) { 6651da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 6661da177e4SLinus Torvalds break; 6671da177e4SLinus Torvalds } 6681da177e4SLinus Torvalds mntget(parent); 6694ac91378SJan Blunck nd->path.dentry = dget(nd->path.mnt->mnt_mountpoint); 6701da177e4SLinus Torvalds spin_unlock(&vfsmount_lock); 6711da177e4SLinus Torvalds dput(old); 6724ac91378SJan Blunck mntput(nd->path.mnt); 6734ac91378SJan Blunck nd->path.mnt = parent; 6741da177e4SLinus Torvalds } 67579ed0226SAl Viro follow_mount(&nd->path); 6761da177e4SLinus Torvalds } 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds /* 6791da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 6801da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 6811da177e4SLinus Torvalds * It _is_ time-critical. 6821da177e4SLinus Torvalds */ 6831da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name, 6841da177e4SLinus Torvalds struct path *path) 6851da177e4SLinus Torvalds { 6864ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 6876e6b1bd1SAl Viro struct dentry *dentry, *parent; 6886e6b1bd1SAl Viro struct inode *dir; 6893cac260aSAl Viro /* 6903cac260aSAl Viro * See if the low-level filesystem might want 6913cac260aSAl Viro * to use its own hash.. 6923cac260aSAl Viro */ 6933cac260aSAl Viro if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) { 6943cac260aSAl Viro int err = nd->path.dentry->d_op->d_hash(nd->path.dentry, name); 6953cac260aSAl Viro if (err < 0) 6963cac260aSAl Viro return err; 6973cac260aSAl Viro } 6981da177e4SLinus Torvalds 6993cac260aSAl Viro dentry = __d_lookup(nd->path.dentry, name); 7001da177e4SLinus Torvalds if (!dentry) 7011da177e4SLinus Torvalds goto need_lookup; 7021da177e4SLinus Torvalds if (dentry->d_op && dentry->d_op->d_revalidate) 7031da177e4SLinus Torvalds goto need_revalidate; 7041da177e4SLinus Torvalds done: 7051da177e4SLinus Torvalds path->mnt = mnt; 7061da177e4SLinus Torvalds path->dentry = dentry; 707634ee701SAl Viro __follow_mount(path); 7081da177e4SLinus Torvalds return 0; 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds need_lookup: 7116e6b1bd1SAl Viro parent = nd->path.dentry; 7126e6b1bd1SAl Viro dir = parent->d_inode; 7136e6b1bd1SAl Viro 7146e6b1bd1SAl Viro mutex_lock(&dir->i_mutex); 7156e6b1bd1SAl Viro /* 7166e6b1bd1SAl Viro * First re-do the cached lookup just in case it was created 7176e6b1bd1SAl Viro * while we waited for the directory semaphore.. 7186e6b1bd1SAl Viro * 7196e6b1bd1SAl Viro * FIXME! This could use version numbering or similar to 7206e6b1bd1SAl Viro * avoid unnecessary cache lookups. 7216e6b1bd1SAl Viro * 7226e6b1bd1SAl Viro * The "dcache_lock" is purely to protect the RCU list walker 7236e6b1bd1SAl Viro * from concurrent renames at this point (we mustn't get false 7246e6b1bd1SAl Viro * negatives from the RCU list walk here, unlike the optimistic 7256e6b1bd1SAl Viro * fast walk). 7266e6b1bd1SAl Viro * 7276e6b1bd1SAl Viro * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup 7286e6b1bd1SAl Viro */ 7296e6b1bd1SAl Viro dentry = d_lookup(parent, name); 7306e6b1bd1SAl Viro if (!dentry) { 7316e6b1bd1SAl Viro struct dentry *new; 7326e6b1bd1SAl Viro 7336e6b1bd1SAl Viro /* Don't create child dentry for a dead directory. */ 7346e6b1bd1SAl Viro dentry = ERR_PTR(-ENOENT); 7356e6b1bd1SAl Viro if (IS_DEADDIR(dir)) 7366e6b1bd1SAl Viro goto out_unlock; 7376e6b1bd1SAl Viro 7386e6b1bd1SAl Viro new = d_alloc(parent, name); 7396e6b1bd1SAl Viro dentry = ERR_PTR(-ENOMEM); 7406e6b1bd1SAl Viro if (new) { 7416e6b1bd1SAl Viro dentry = dir->i_op->lookup(dir, new, nd); 7426e6b1bd1SAl Viro if (dentry) 7436e6b1bd1SAl Viro dput(new); 7446e6b1bd1SAl Viro else 7456e6b1bd1SAl Viro dentry = new; 7466e6b1bd1SAl Viro } 7476e6b1bd1SAl Viro out_unlock: 7486e6b1bd1SAl Viro mutex_unlock(&dir->i_mutex); 7496e6b1bd1SAl Viro if (IS_ERR(dentry)) 7506e6b1bd1SAl Viro goto fail; 7516e6b1bd1SAl Viro goto done; 7526e6b1bd1SAl Viro } 7536e6b1bd1SAl Viro 7546e6b1bd1SAl Viro /* 7556e6b1bd1SAl Viro * Uhhuh! Nasty case: the cache was re-populated while 7566e6b1bd1SAl Viro * we waited on the semaphore. Need to revalidate. 7576e6b1bd1SAl Viro */ 7586e6b1bd1SAl Viro mutex_unlock(&dir->i_mutex); 7596e6b1bd1SAl Viro if (dentry->d_op && dentry->d_op->d_revalidate) { 7606e6b1bd1SAl Viro dentry = do_revalidate(dentry, nd); 7616e6b1bd1SAl Viro if (!dentry) 7626e6b1bd1SAl Viro dentry = ERR_PTR(-ENOENT); 7636e6b1bd1SAl Viro } 7641da177e4SLinus Torvalds if (IS_ERR(dentry)) 7651da177e4SLinus Torvalds goto fail; 7661da177e4SLinus Torvalds goto done; 7671da177e4SLinus Torvalds 7681da177e4SLinus Torvalds need_revalidate: 769bcdc5e01SIan Kent dentry = do_revalidate(dentry, nd); 770bcdc5e01SIan Kent if (!dentry) 7711da177e4SLinus Torvalds goto need_lookup; 772bcdc5e01SIan Kent if (IS_ERR(dentry)) 773bcdc5e01SIan Kent goto fail; 774bcdc5e01SIan Kent goto done; 7751da177e4SLinus Torvalds 7761da177e4SLinus Torvalds fail: 7771da177e4SLinus Torvalds return PTR_ERR(dentry); 7781da177e4SLinus Torvalds } 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds /* 7811da177e4SLinus Torvalds * Name resolution. 782ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 783ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 7841da177e4SLinus Torvalds * 785ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 786ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 7871da177e4SLinus Torvalds */ 7886de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 7891da177e4SLinus Torvalds { 7901da177e4SLinus Torvalds struct path next; 7911da177e4SLinus Torvalds struct inode *inode; 7921da177e4SLinus Torvalds int err; 7931da177e4SLinus Torvalds unsigned int lookup_flags = nd->flags; 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds while (*name=='/') 7961da177e4SLinus Torvalds name++; 7971da177e4SLinus Torvalds if (!*name) 7981da177e4SLinus Torvalds goto return_reval; 7991da177e4SLinus Torvalds 8004ac91378SJan Blunck inode = nd->path.dentry->d_inode; 8011da177e4SLinus Torvalds if (nd->depth) 802f55eab82STrond Myklebust lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE); 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 8051da177e4SLinus Torvalds for(;;) { 8061da177e4SLinus Torvalds unsigned long hash; 8071da177e4SLinus Torvalds struct qstr this; 8081da177e4SLinus Torvalds unsigned int c; 8091da177e4SLinus Torvalds 810cdce5d6bSTrond Myklebust nd->flags |= LOOKUP_CONTINUE; 811b75b5086SAl Viro err = exec_permission(inode); 8121da177e4SLinus Torvalds if (err) 8131da177e4SLinus Torvalds break; 8141da177e4SLinus Torvalds 8151da177e4SLinus Torvalds this.name = name; 8161da177e4SLinus Torvalds c = *(const unsigned char *)name; 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds hash = init_name_hash(); 8191da177e4SLinus Torvalds do { 8201da177e4SLinus Torvalds name++; 8211da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 8221da177e4SLinus Torvalds c = *(const unsigned char *)name; 8231da177e4SLinus Torvalds } while (c && (c != '/')); 8241da177e4SLinus Torvalds this.len = name - (const char *) this.name; 8251da177e4SLinus Torvalds this.hash = end_name_hash(hash); 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds /* remove trailing slashes? */ 8281da177e4SLinus Torvalds if (!c) 8291da177e4SLinus Torvalds goto last_component; 8301da177e4SLinus Torvalds while (*++name == '/'); 8311da177e4SLinus Torvalds if (!*name) 8321da177e4SLinus Torvalds goto last_with_slashes; 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds /* 8351da177e4SLinus Torvalds * "." and ".." are special - ".." especially so because it has 8361da177e4SLinus Torvalds * to be able to know about the current root directory and 8371da177e4SLinus Torvalds * parent relationships. 8381da177e4SLinus Torvalds */ 8391da177e4SLinus Torvalds if (this.name[0] == '.') switch (this.len) { 8401da177e4SLinus Torvalds default: 8411da177e4SLinus Torvalds break; 8421da177e4SLinus Torvalds case 2: 8431da177e4SLinus Torvalds if (this.name[1] != '.') 8441da177e4SLinus Torvalds break; 84558c465ebSAl Viro follow_dotdot(nd); 8464ac91378SJan Blunck inode = nd->path.dentry->d_inode; 8471da177e4SLinus Torvalds /* fallthrough */ 8481da177e4SLinus Torvalds case 1: 8491da177e4SLinus Torvalds continue; 8501da177e4SLinus Torvalds } 8511da177e4SLinus Torvalds /* This does the actual lookups.. */ 8521da177e4SLinus Torvalds err = do_lookup(nd, &this, &next); 8531da177e4SLinus Torvalds if (err) 8541da177e4SLinus Torvalds break; 8551da177e4SLinus Torvalds 8561da177e4SLinus Torvalds err = -ENOENT; 8571da177e4SLinus Torvalds inode = next.dentry->d_inode; 8581da177e4SLinus Torvalds if (!inode) 8591da177e4SLinus Torvalds goto out_dput; 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds if (inode->i_op->follow_link) { 86290ebe565SAl Viro err = do_follow_link(&next, nd); 8631da177e4SLinus Torvalds if (err) 8641da177e4SLinus Torvalds goto return_err; 8651da177e4SLinus Torvalds err = -ENOENT; 8664ac91378SJan Blunck inode = nd->path.dentry->d_inode; 8671da177e4SLinus Torvalds if (!inode) 8681da177e4SLinus Torvalds break; 86909dd17d3SMiklos Szeredi } else 87009dd17d3SMiklos Szeredi path_to_nameidata(&next, nd); 8711da177e4SLinus Torvalds err = -ENOTDIR; 8721da177e4SLinus Torvalds if (!inode->i_op->lookup) 8731da177e4SLinus Torvalds break; 8741da177e4SLinus Torvalds continue; 8751da177e4SLinus Torvalds /* here ends the main loop */ 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds last_with_slashes: 8781da177e4SLinus Torvalds lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 8791da177e4SLinus Torvalds last_component: 880f55eab82STrond Myklebust /* Clear LOOKUP_CONTINUE iff it was previously unset */ 881f55eab82STrond Myklebust nd->flags &= lookup_flags | ~LOOKUP_CONTINUE; 8821da177e4SLinus Torvalds if (lookup_flags & LOOKUP_PARENT) 8831da177e4SLinus Torvalds goto lookup_parent; 8841da177e4SLinus Torvalds if (this.name[0] == '.') switch (this.len) { 8851da177e4SLinus Torvalds default: 8861da177e4SLinus Torvalds break; 8871da177e4SLinus Torvalds case 2: 8881da177e4SLinus Torvalds if (this.name[1] != '.') 8891da177e4SLinus Torvalds break; 89058c465ebSAl Viro follow_dotdot(nd); 8914ac91378SJan Blunck inode = nd->path.dentry->d_inode; 8921da177e4SLinus Torvalds /* fallthrough */ 8931da177e4SLinus Torvalds case 1: 8941da177e4SLinus Torvalds goto return_reval; 8951da177e4SLinus Torvalds } 8961da177e4SLinus Torvalds err = do_lookup(nd, &this, &next); 8971da177e4SLinus Torvalds if (err) 8981da177e4SLinus Torvalds break; 8991da177e4SLinus Torvalds inode = next.dentry->d_inode; 9001da177e4SLinus Torvalds if ((lookup_flags & LOOKUP_FOLLOW) 901acfa4380SAl Viro && inode && inode->i_op->follow_link) { 90290ebe565SAl Viro err = do_follow_link(&next, nd); 9031da177e4SLinus Torvalds if (err) 9041da177e4SLinus Torvalds goto return_err; 9054ac91378SJan Blunck inode = nd->path.dentry->d_inode; 90609dd17d3SMiklos Szeredi } else 90709dd17d3SMiklos Szeredi path_to_nameidata(&next, nd); 9081da177e4SLinus Torvalds err = -ENOENT; 9091da177e4SLinus Torvalds if (!inode) 9101da177e4SLinus Torvalds break; 9111da177e4SLinus Torvalds if (lookup_flags & LOOKUP_DIRECTORY) { 9121da177e4SLinus Torvalds err = -ENOTDIR; 913acfa4380SAl Viro if (!inode->i_op->lookup) 9141da177e4SLinus Torvalds break; 9151da177e4SLinus Torvalds } 9161da177e4SLinus Torvalds goto return_base; 9171da177e4SLinus Torvalds lookup_parent: 9181da177e4SLinus Torvalds nd->last = this; 9191da177e4SLinus Torvalds nd->last_type = LAST_NORM; 9201da177e4SLinus Torvalds if (this.name[0] != '.') 9211da177e4SLinus Torvalds goto return_base; 9221da177e4SLinus Torvalds if (this.len == 1) 9231da177e4SLinus Torvalds nd->last_type = LAST_DOT; 9241da177e4SLinus Torvalds else if (this.len == 2 && this.name[1] == '.') 9251da177e4SLinus Torvalds nd->last_type = LAST_DOTDOT; 9261da177e4SLinus Torvalds else 9271da177e4SLinus Torvalds goto return_base; 9281da177e4SLinus Torvalds return_reval: 9291da177e4SLinus Torvalds /* 9301da177e4SLinus Torvalds * We bypassed the ordinary revalidation routines. 9311da177e4SLinus Torvalds * We may need to check the cached dentry for staleness. 9321da177e4SLinus Torvalds */ 9334ac91378SJan Blunck if (nd->path.dentry && nd->path.dentry->d_sb && 9344ac91378SJan Blunck (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) { 9351da177e4SLinus Torvalds err = -ESTALE; 9361da177e4SLinus Torvalds /* Note: we do not d_invalidate() */ 9374ac91378SJan Blunck if (!nd->path.dentry->d_op->d_revalidate( 9384ac91378SJan Blunck nd->path.dentry, nd)) 9391da177e4SLinus Torvalds break; 9401da177e4SLinus Torvalds } 9411da177e4SLinus Torvalds return_base: 9421da177e4SLinus Torvalds return 0; 9431da177e4SLinus Torvalds out_dput: 9441d957f9bSJan Blunck path_put_conditional(&next, nd); 9451da177e4SLinus Torvalds break; 9461da177e4SLinus Torvalds } 9471d957f9bSJan Blunck path_put(&nd->path); 9481da177e4SLinus Torvalds return_err: 9491da177e4SLinus Torvalds return err; 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds 952fc9b52cdSHarvey Harrison static int path_walk(const char *name, struct nameidata *nd) 9531da177e4SLinus Torvalds { 9546de88d72SAl Viro struct path save = nd->path; 9556de88d72SAl Viro int result; 9566de88d72SAl Viro 9571da177e4SLinus Torvalds current->total_link_count = 0; 9586de88d72SAl Viro 9596de88d72SAl Viro /* make sure the stuff we saved doesn't go away */ 9606de88d72SAl Viro path_get(&save); 9616de88d72SAl Viro 9626de88d72SAl Viro result = link_path_walk(name, nd); 9636de88d72SAl Viro if (result == -ESTALE) { 9646de88d72SAl Viro /* nd->path had been dropped */ 9656de88d72SAl Viro current->total_link_count = 0; 9666de88d72SAl Viro nd->path = save; 9676de88d72SAl Viro path_get(&nd->path); 9686de88d72SAl Viro nd->flags |= LOOKUP_REVAL; 9696de88d72SAl Viro result = link_path_walk(name, nd); 9706de88d72SAl Viro } 9716de88d72SAl Viro 9726de88d72SAl Viro path_put(&save); 9736de88d72SAl Viro 9746de88d72SAl Viro return result; 9751da177e4SLinus Torvalds } 9761da177e4SLinus Torvalds 9779b4a9b14SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd) 9781da177e4SLinus Torvalds { 979ea3834d9SPrasanna Meda int retval = 0; 980170aa3d0SUlrich Drepper int fput_needed; 981170aa3d0SUlrich Drepper struct file *file; 9821da177e4SLinus Torvalds 9831da177e4SLinus Torvalds nd->last_type = LAST_ROOT; /* if there are only slashes... */ 9841da177e4SLinus Torvalds nd->flags = flags; 9851da177e4SLinus Torvalds nd->depth = 0; 9862a737871SAl Viro nd->root.mnt = NULL; 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds if (*name=='/') { 9892a737871SAl Viro set_root(nd); 9902a737871SAl Viro nd->path = nd->root; 9912a737871SAl Viro path_get(&nd->root); 9925590ff0dSUlrich Drepper } else if (dfd == AT_FDCWD) { 9932a737871SAl Viro struct fs_struct *fs = current->fs; 994e518ddb7SAndreas Mohr read_lock(&fs->lock); 9956ac08c39SJan Blunck nd->path = fs->pwd; 9966ac08c39SJan Blunck path_get(&fs->pwd); 997e518ddb7SAndreas Mohr read_unlock(&fs->lock); 9985590ff0dSUlrich Drepper } else { 9995590ff0dSUlrich Drepper struct dentry *dentry; 10005590ff0dSUlrich Drepper 10015590ff0dSUlrich Drepper file = fget_light(dfd, &fput_needed); 10025590ff0dSUlrich Drepper retval = -EBADF; 1003170aa3d0SUlrich Drepper if (!file) 10046d09bb62STrond Myklebust goto out_fail; 10055590ff0dSUlrich Drepper 10060f7fc9e4SJosef "Jeff" Sipek dentry = file->f_path.dentry; 10075590ff0dSUlrich Drepper 10085590ff0dSUlrich Drepper retval = -ENOTDIR; 1009170aa3d0SUlrich Drepper if (!S_ISDIR(dentry->d_inode->i_mode)) 10106d09bb62STrond Myklebust goto fput_fail; 10115590ff0dSUlrich Drepper 10125590ff0dSUlrich Drepper retval = file_permission(file, MAY_EXEC); 1013170aa3d0SUlrich Drepper if (retval) 10146d09bb62STrond Myklebust goto fput_fail; 10155590ff0dSUlrich Drepper 10165dd784d0SJan Blunck nd->path = file->f_path; 10175dd784d0SJan Blunck path_get(&file->f_path); 10185590ff0dSUlrich Drepper 10195590ff0dSUlrich Drepper fput_light(file, fput_needed); 10201da177e4SLinus Torvalds } 10219b4a9b14SAl Viro return 0; 10222dfdd266SJosef 'Jeff' Sipek 10239b4a9b14SAl Viro fput_fail: 10249b4a9b14SAl Viro fput_light(file, fput_needed); 10259b4a9b14SAl Viro out_fail: 10269b4a9b14SAl Viro return retval; 10279b4a9b14SAl Viro } 10289b4a9b14SAl Viro 10299b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 10309b4a9b14SAl Viro static int do_path_lookup(int dfd, const char *name, 10319b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 10329b4a9b14SAl Viro { 10339b4a9b14SAl Viro int retval = path_init(dfd, name, flags, nd); 10349b4a9b14SAl Viro if (!retval) 10352dfdd266SJosef 'Jeff' Sipek retval = path_walk(name, nd); 10364ac91378SJan Blunck if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry && 10374ac91378SJan Blunck nd->path.dentry->d_inode)) 10384ac91378SJan Blunck audit_inode(name, nd->path.dentry); 10392a737871SAl Viro if (nd->root.mnt) { 10402a737871SAl Viro path_put(&nd->root); 10412a737871SAl Viro nd->root.mnt = NULL; 10422a737871SAl Viro } 1043170aa3d0SUlrich Drepper return retval; 10441da177e4SLinus Torvalds } 10451da177e4SLinus Torvalds 1046fc9b52cdSHarvey Harrison int path_lookup(const char *name, unsigned int flags, 10475590ff0dSUlrich Drepper struct nameidata *nd) 10485590ff0dSUlrich Drepper { 10495590ff0dSUlrich Drepper return do_path_lookup(AT_FDCWD, name, flags, nd); 10505590ff0dSUlrich Drepper } 10515590ff0dSUlrich Drepper 1052d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1053d1811465SAl Viro { 1054d1811465SAl Viro struct nameidata nd; 1055d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1056d1811465SAl Viro if (!res) 1057d1811465SAl Viro *path = nd.path; 1058d1811465SAl Viro return res; 1059d1811465SAl Viro } 1060d1811465SAl Viro 106116f18200SJosef 'Jeff' Sipek /** 106216f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 106316f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 106416f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 106516f18200SJosef 'Jeff' Sipek * @name: pointer to file name 106616f18200SJosef 'Jeff' Sipek * @flags: lookup flags 106716f18200SJosef 'Jeff' Sipek * @nd: pointer to nameidata 106816f18200SJosef 'Jeff' Sipek */ 106916f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 107016f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 107116f18200SJosef 'Jeff' Sipek struct nameidata *nd) 107216f18200SJosef 'Jeff' Sipek { 107316f18200SJosef 'Jeff' Sipek int retval; 107416f18200SJosef 'Jeff' Sipek 107516f18200SJosef 'Jeff' Sipek /* same as do_path_lookup */ 107616f18200SJosef 'Jeff' Sipek nd->last_type = LAST_ROOT; 107716f18200SJosef 'Jeff' Sipek nd->flags = flags; 107816f18200SJosef 'Jeff' Sipek nd->depth = 0; 107916f18200SJosef 'Jeff' Sipek 1080c8e7f449SJan Blunck nd->path.dentry = dentry; 1081c8e7f449SJan Blunck nd->path.mnt = mnt; 1082c8e7f449SJan Blunck path_get(&nd->path); 10835b857119SAl Viro nd->root = nd->path; 10845b857119SAl Viro path_get(&nd->root); 108516f18200SJosef 'Jeff' Sipek 108616f18200SJosef 'Jeff' Sipek retval = path_walk(name, nd); 10874ac91378SJan Blunck if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry && 10884ac91378SJan Blunck nd->path.dentry->d_inode)) 10894ac91378SJan Blunck audit_inode(name, nd->path.dentry); 109016f18200SJosef 'Jeff' Sipek 10912a737871SAl Viro path_put(&nd->root); 10922a737871SAl Viro nd->root.mnt = NULL; 109316f18200SJosef 'Jeff' Sipek 10942a737871SAl Viro return retval; 109516f18200SJosef 'Jeff' Sipek } 109616f18200SJosef 'Jeff' Sipek 1097eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name, 1098eead1911SChristoph Hellwig struct dentry *base, struct nameidata *nd) 10991da177e4SLinus Torvalds { 11001da177e4SLinus Torvalds struct dentry *dentry; 11011da177e4SLinus Torvalds struct inode *inode; 11021da177e4SLinus Torvalds int err; 11031da177e4SLinus Torvalds 11041da177e4SLinus Torvalds inode = base->d_inode; 11051da177e4SLinus Torvalds 11061da177e4SLinus Torvalds /* 11071da177e4SLinus Torvalds * See if the low-level filesystem might want 11081da177e4SLinus Torvalds * to use its own hash.. 11091da177e4SLinus Torvalds */ 11101da177e4SLinus Torvalds if (base->d_op && base->d_op->d_hash) { 11111da177e4SLinus Torvalds err = base->d_op->d_hash(base, name); 11121da177e4SLinus Torvalds dentry = ERR_PTR(err); 11131da177e4SLinus Torvalds if (err < 0) 11141da177e4SLinus Torvalds goto out; 11151da177e4SLinus Torvalds } 11161da177e4SLinus Torvalds 11176e6b1bd1SAl Viro dentry = __d_lookup(base, name); 11186e6b1bd1SAl Viro 11196e6b1bd1SAl Viro /* lockess __d_lookup may fail due to concurrent d_move() 11206e6b1bd1SAl Viro * in some unrelated directory, so try with d_lookup 11216e6b1bd1SAl Viro */ 11226e6b1bd1SAl Viro if (!dentry) 11236e6b1bd1SAl Viro dentry = d_lookup(base, name); 11246e6b1bd1SAl Viro 11256e6b1bd1SAl Viro if (dentry && dentry->d_op && dentry->d_op->d_revalidate) 11266e6b1bd1SAl Viro dentry = do_revalidate(dentry, nd); 11276e6b1bd1SAl Viro 11281da177e4SLinus Torvalds if (!dentry) { 1129d70b67c8SMiklos Szeredi struct dentry *new; 1130d70b67c8SMiklos Szeredi 1131d70b67c8SMiklos Szeredi /* Don't create child dentry for a dead directory. */ 1132d70b67c8SMiklos Szeredi dentry = ERR_PTR(-ENOENT); 1133d70b67c8SMiklos Szeredi if (IS_DEADDIR(inode)) 1134d70b67c8SMiklos Szeredi goto out; 1135d70b67c8SMiklos Szeredi 1136d70b67c8SMiklos Szeredi new = d_alloc(base, name); 11371da177e4SLinus Torvalds dentry = ERR_PTR(-ENOMEM); 11381da177e4SLinus Torvalds if (!new) 11391da177e4SLinus Torvalds goto out; 11401da177e4SLinus Torvalds dentry = inode->i_op->lookup(inode, new, nd); 11411da177e4SLinus Torvalds if (!dentry) 11421da177e4SLinus Torvalds dentry = new; 11431da177e4SLinus Torvalds else 11441da177e4SLinus Torvalds dput(new); 11451da177e4SLinus Torvalds } 11461da177e4SLinus Torvalds out: 11471da177e4SLinus Torvalds return dentry; 11481da177e4SLinus Torvalds } 11491da177e4SLinus Torvalds 1150057f6c01SJames Morris /* 1151057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1152057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1153057f6c01SJames Morris * SMP-safe. 1154057f6c01SJames Morris */ 1155a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 11561da177e4SLinus Torvalds { 1157eead1911SChristoph Hellwig int err; 1158eead1911SChristoph Hellwig 1159b75b5086SAl Viro err = exec_permission(nd->path.dentry->d_inode); 1160eead1911SChristoph Hellwig if (err) 1161eead1911SChristoph Hellwig return ERR_PTR(err); 11624ac91378SJan Blunck return __lookup_hash(&nd->last, nd->path.dentry, nd); 11631da177e4SLinus Torvalds } 11641da177e4SLinus Torvalds 1165eead1911SChristoph Hellwig static int __lookup_one_len(const char *name, struct qstr *this, 1166eead1911SChristoph Hellwig struct dentry *base, int len) 11671da177e4SLinus Torvalds { 11681da177e4SLinus Torvalds unsigned long hash; 11691da177e4SLinus Torvalds unsigned int c; 11701da177e4SLinus Torvalds 1171057f6c01SJames Morris this->name = name; 1172057f6c01SJames Morris this->len = len; 11731da177e4SLinus Torvalds if (!len) 1174057f6c01SJames Morris return -EACCES; 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds hash = init_name_hash(); 11771da177e4SLinus Torvalds while (len--) { 11781da177e4SLinus Torvalds c = *(const unsigned char *)name++; 11791da177e4SLinus Torvalds if (c == '/' || c == '\0') 1180057f6c01SJames Morris return -EACCES; 11811da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 11821da177e4SLinus Torvalds } 1183057f6c01SJames Morris this->hash = end_name_hash(hash); 1184057f6c01SJames Morris return 0; 1185057f6c01SJames Morris } 11861da177e4SLinus Torvalds 1187eead1911SChristoph Hellwig /** 1188a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1189eead1911SChristoph Hellwig * @name: pathname component to lookup 1190eead1911SChristoph Hellwig * @base: base directory to lookup from 1191eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1192eead1911SChristoph Hellwig * 1193a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1194a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1195eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1196eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1197eead1911SChristoph Hellwig */ 1198057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1199057f6c01SJames Morris { 1200057f6c01SJames Morris int err; 1201057f6c01SJames Morris struct qstr this; 1202057f6c01SJames Morris 12032f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 12042f9092e1SDavid Woodhouse 1205057f6c01SJames Morris err = __lookup_one_len(name, &this, base, len); 1206057f6c01SJames Morris if (err) 1207057f6c01SJames Morris return ERR_PTR(err); 1208eead1911SChristoph Hellwig 1209b75b5086SAl Viro err = exec_permission(base->d_inode); 1210eead1911SChristoph Hellwig if (err) 1211eead1911SChristoph Hellwig return ERR_PTR(err); 121249705b77SChristoph Hellwig return __lookup_hash(&this, base, NULL); 1213057f6c01SJames Morris } 1214057f6c01SJames Morris 12152d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags, 12162d8f3038SAl Viro struct path *path) 12171da177e4SLinus Torvalds { 12182d8f3038SAl Viro struct nameidata nd; 12191da177e4SLinus Torvalds char *tmp = getname(name); 12201da177e4SLinus Torvalds int err = PTR_ERR(tmp); 12211da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 12222d8f3038SAl Viro 12232d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 12242d8f3038SAl Viro 12252d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 12261da177e4SLinus Torvalds putname(tmp); 12272d8f3038SAl Viro if (!err) 12282d8f3038SAl Viro *path = nd.path; 12291da177e4SLinus Torvalds } 12301da177e4SLinus Torvalds return err; 12311da177e4SLinus Torvalds } 12321da177e4SLinus Torvalds 12332ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 12342ad94ae6SAl Viro struct nameidata *nd, char **name) 12352ad94ae6SAl Viro { 12362ad94ae6SAl Viro char *s = getname(path); 12372ad94ae6SAl Viro int error; 12382ad94ae6SAl Viro 12392ad94ae6SAl Viro if (IS_ERR(s)) 12402ad94ae6SAl Viro return PTR_ERR(s); 12412ad94ae6SAl Viro 12422ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 12432ad94ae6SAl Viro if (error) 12442ad94ae6SAl Viro putname(s); 12452ad94ae6SAl Viro else 12462ad94ae6SAl Viro *name = s; 12472ad94ae6SAl Viro 12482ad94ae6SAl Viro return error; 12492ad94ae6SAl Viro } 12502ad94ae6SAl Viro 12511da177e4SLinus Torvalds /* 12521da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 12531da177e4SLinus Torvalds * minimal. 12541da177e4SLinus Torvalds */ 12551da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 12561da177e4SLinus Torvalds { 1257da9592edSDavid Howells uid_t fsuid = current_fsuid(); 1258da9592edSDavid Howells 12591da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 12601da177e4SLinus Torvalds return 0; 1261da9592edSDavid Howells if (inode->i_uid == fsuid) 12621da177e4SLinus Torvalds return 0; 1263da9592edSDavid Howells if (dir->i_uid == fsuid) 12641da177e4SLinus Torvalds return 0; 12651da177e4SLinus Torvalds return !capable(CAP_FOWNER); 12661da177e4SLinus Torvalds } 12671da177e4SLinus Torvalds 12681da177e4SLinus Torvalds /* 12691da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 12701da177e4SLinus Torvalds * whether the type of victim is right. 12711da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 12721da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 12731da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 12741da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 12751da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 12761da177e4SLinus Torvalds * a. be owner of dir, or 12771da177e4SLinus Torvalds * b. be owner of victim, or 12781da177e4SLinus Torvalds * c. have CAP_FOWNER capability 12791da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 12801da177e4SLinus Torvalds * links pointing to it. 12811da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 12821da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 12831da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 12841da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 12851da177e4SLinus Torvalds * nfs_async_unlink(). 12861da177e4SLinus Torvalds */ 1287858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 12881da177e4SLinus Torvalds { 12891da177e4SLinus Torvalds int error; 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds if (!victim->d_inode) 12921da177e4SLinus Torvalds return -ENOENT; 12931da177e4SLinus Torvalds 12941da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 12955a190ae6SAl Viro audit_inode_child(victim->d_name.name, victim, dir); 12961da177e4SLinus Torvalds 1297f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 12981da177e4SLinus Torvalds if (error) 12991da177e4SLinus Torvalds return error; 13001da177e4SLinus Torvalds if (IS_APPEND(dir)) 13011da177e4SLinus Torvalds return -EPERM; 13021da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 1303f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 13041da177e4SLinus Torvalds return -EPERM; 13051da177e4SLinus Torvalds if (isdir) { 13061da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 13071da177e4SLinus Torvalds return -ENOTDIR; 13081da177e4SLinus Torvalds if (IS_ROOT(victim)) 13091da177e4SLinus Torvalds return -EBUSY; 13101da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 13111da177e4SLinus Torvalds return -EISDIR; 13121da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 13131da177e4SLinus Torvalds return -ENOENT; 13141da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 13151da177e4SLinus Torvalds return -EBUSY; 13161da177e4SLinus Torvalds return 0; 13171da177e4SLinus Torvalds } 13181da177e4SLinus Torvalds 13191da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 13201da177e4SLinus Torvalds * dir. 13211da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 13221da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 13231da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 13241da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 13251da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 13261da177e4SLinus Torvalds */ 1327a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 13281da177e4SLinus Torvalds { 13291da177e4SLinus Torvalds if (child->d_inode) 13301da177e4SLinus Torvalds return -EEXIST; 13311da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 13321da177e4SLinus Torvalds return -ENOENT; 1333f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 13341da177e4SLinus Torvalds } 13351da177e4SLinus Torvalds 13361da177e4SLinus Torvalds /* 13371da177e4SLinus Torvalds * O_DIRECTORY translates into forcing a directory lookup. 13381da177e4SLinus Torvalds */ 13391da177e4SLinus Torvalds static inline int lookup_flags(unsigned int f) 13401da177e4SLinus Torvalds { 13411da177e4SLinus Torvalds unsigned long retval = LOOKUP_FOLLOW; 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds if (f & O_NOFOLLOW) 13441da177e4SLinus Torvalds retval &= ~LOOKUP_FOLLOW; 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds if (f & O_DIRECTORY) 13471da177e4SLinus Torvalds retval |= LOOKUP_DIRECTORY; 13481da177e4SLinus Torvalds 13491da177e4SLinus Torvalds return retval; 13501da177e4SLinus Torvalds } 13511da177e4SLinus Torvalds 13521da177e4SLinus Torvalds /* 13531da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 13541da177e4SLinus Torvalds */ 13551da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 13561da177e4SLinus Torvalds { 13571da177e4SLinus Torvalds struct dentry *p; 13581da177e4SLinus Torvalds 13591da177e4SLinus Torvalds if (p1 == p2) { 1360f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 13611da177e4SLinus Torvalds return NULL; 13621da177e4SLinus Torvalds } 13631da177e4SLinus Torvalds 1364a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 13651da177e4SLinus Torvalds 1366e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 1367e2761a11SOGAWA Hirofumi if (p) { 1368f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 1369f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 13701da177e4SLinus Torvalds return p; 13711da177e4SLinus Torvalds } 13721da177e4SLinus Torvalds 1373e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 1374e2761a11SOGAWA Hirofumi if (p) { 1375f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1376f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 13771da177e4SLinus Torvalds return p; 13781da177e4SLinus Torvalds } 13791da177e4SLinus Torvalds 1380f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1381f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 13821da177e4SLinus Torvalds return NULL; 13831da177e4SLinus Torvalds } 13841da177e4SLinus Torvalds 13851da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 13861da177e4SLinus Torvalds { 13871b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 13881da177e4SLinus Torvalds if (p1 != p2) { 13891b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 1390a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 13911da177e4SLinus Torvalds } 13921da177e4SLinus Torvalds } 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode, 13951da177e4SLinus Torvalds struct nameidata *nd) 13961da177e4SLinus Torvalds { 1397a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 13981da177e4SLinus Torvalds 13991da177e4SLinus Torvalds if (error) 14001da177e4SLinus Torvalds return error; 14011da177e4SLinus Torvalds 1402acfa4380SAl Viro if (!dir->i_op->create) 14031da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 14041da177e4SLinus Torvalds mode &= S_IALLUGO; 14051da177e4SLinus Torvalds mode |= S_IFREG; 14061da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 14071da177e4SLinus Torvalds if (error) 14081da177e4SLinus Torvalds return error; 14099e3509e2SJan Kara vfs_dq_init(dir); 14101da177e4SLinus Torvalds error = dir->i_op->create(dir, dentry, mode, nd); 1411a74574aaSStephen Smalley if (!error) 1412f38aa942SAmy Griffis fsnotify_create(dir, dentry); 14131da177e4SLinus Torvalds return error; 14141da177e4SLinus Torvalds } 14151da177e4SLinus Torvalds 14163fb64190SChristoph Hellwig int may_open(struct path *path, int acc_mode, int flag) 14171da177e4SLinus Torvalds { 14183fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 14191da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 14201da177e4SLinus Torvalds int error; 14211da177e4SLinus Torvalds 14221da177e4SLinus Torvalds if (!inode) 14231da177e4SLinus Torvalds return -ENOENT; 14241da177e4SLinus Torvalds 1425c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 1426c8fe8f30SChristoph Hellwig case S_IFLNK: 14271da177e4SLinus Torvalds return -ELOOP; 1428c8fe8f30SChristoph Hellwig case S_IFDIR: 1429c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 14301da177e4SLinus Torvalds return -EISDIR; 1431c8fe8f30SChristoph Hellwig break; 1432c8fe8f30SChristoph Hellwig case S_IFBLK: 1433c8fe8f30SChristoph Hellwig case S_IFCHR: 14343fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 14351da177e4SLinus Torvalds return -EACCES; 1436c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 1437c8fe8f30SChristoph Hellwig case S_IFIFO: 1438c8fe8f30SChristoph Hellwig case S_IFSOCK: 14391da177e4SLinus Torvalds flag &= ~O_TRUNC; 1440c8fe8f30SChristoph Hellwig break; 14414a3fd211SDave Hansen } 1442b41572e9SDave Hansen 14433fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 1444b41572e9SDave Hansen if (error) 1445b41572e9SDave Hansen return error; 14466146f0d5SMimi Zohar 14476c1488fdSMimi Zohar error = ima_path_check(path, acc_mode ? 14486c1488fdSMimi Zohar acc_mode & (MAY_READ | MAY_WRITE | MAY_EXEC) : 14496c1488fdSMimi Zohar ACC_MODE(flag) & (MAY_READ | MAY_WRITE), 1450b9fc745dSMimi Zohar IMA_COUNT_UPDATE); 14516c1488fdSMimi Zohar 14526146f0d5SMimi Zohar if (error) 14536146f0d5SMimi Zohar return error; 14541da177e4SLinus Torvalds /* 14551da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 14561da177e4SLinus Torvalds */ 14571da177e4SLinus Torvalds if (IS_APPEND(inode)) { 1458acd0c935SMimi Zohar error = -EPERM; 14591da177e4SLinus Torvalds if ((flag & FMODE_WRITE) && !(flag & O_APPEND)) 1460acd0c935SMimi Zohar goto err_out; 14611da177e4SLinus Torvalds if (flag & O_TRUNC) 1462acd0c935SMimi Zohar goto err_out; 14631da177e4SLinus Torvalds } 14641da177e4SLinus Torvalds 14651da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 14661da177e4SLinus Torvalds if (flag & O_NOATIME) 1467acd0c935SMimi Zohar if (!is_owner_or_cap(inode)) { 1468acd0c935SMimi Zohar error = -EPERM; 1469acd0c935SMimi Zohar goto err_out; 1470acd0c935SMimi Zohar } 14711da177e4SLinus Torvalds 14721da177e4SLinus Torvalds /* 14731da177e4SLinus Torvalds * Ensure there are no outstanding leases on the file. 14741da177e4SLinus Torvalds */ 14751da177e4SLinus Torvalds error = break_lease(inode, flag); 14761da177e4SLinus Torvalds if (error) 1477acd0c935SMimi Zohar goto err_out; 14781da177e4SLinus Torvalds 14791da177e4SLinus Torvalds if (flag & O_TRUNC) { 14801da177e4SLinus Torvalds error = get_write_access(inode); 14811da177e4SLinus Torvalds if (error) 1482acd0c935SMimi Zohar goto err_out; 14831da177e4SLinus Torvalds 14841da177e4SLinus Torvalds /* 14851da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 14861da177e4SLinus Torvalds */ 14871da177e4SLinus Torvalds error = locks_verify_locked(inode); 1488be6d3e56SKentaro Takeda if (!error) 14893fb64190SChristoph Hellwig error = security_path_truncate(path, 0, 1490be6d3e56SKentaro Takeda ATTR_MTIME|ATTR_CTIME|ATTR_OPEN); 14911da177e4SLinus Torvalds if (!error) { 14929e3509e2SJan Kara vfs_dq_init(inode); 14931da177e4SLinus Torvalds 1494d139d7ffSMiklos Szeredi error = do_truncate(dentry, 0, 1495d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 1496d139d7ffSMiklos Szeredi NULL); 14971da177e4SLinus Torvalds } 14981da177e4SLinus Torvalds put_write_access(inode); 14991da177e4SLinus Torvalds if (error) 1500acd0c935SMimi Zohar goto err_out; 15011da177e4SLinus Torvalds } else 15021da177e4SLinus Torvalds if (flag & FMODE_WRITE) 15039e3509e2SJan Kara vfs_dq_init(inode); 15041da177e4SLinus Torvalds 15051da177e4SLinus Torvalds return 0; 1506acd0c935SMimi Zohar err_out: 1507acd0c935SMimi Zohar ima_counts_put(path, acc_mode ? 1508acd0c935SMimi Zohar acc_mode & (MAY_READ | MAY_WRITE | MAY_EXEC) : 1509acd0c935SMimi Zohar ACC_MODE(flag) & (MAY_READ | MAY_WRITE)); 1510acd0c935SMimi Zohar return error; 15111da177e4SLinus Torvalds } 15121da177e4SLinus Torvalds 1513d57999e1SDave Hansen /* 1514d57999e1SDave Hansen * Be careful about ever adding any more callers of this 1515d57999e1SDave Hansen * function. Its flags must be in the namei format, not 1516d57999e1SDave Hansen * what get passed to sys_open(). 1517d57999e1SDave Hansen */ 1518d57999e1SDave Hansen static int __open_namei_create(struct nameidata *nd, struct path *path, 1519aab520e2SDave Hansen int flag, int mode) 1520aab520e2SDave Hansen { 1521aab520e2SDave Hansen int error; 15224ac91378SJan Blunck struct dentry *dir = nd->path.dentry; 1523aab520e2SDave Hansen 1524aab520e2SDave Hansen if (!IS_POSIXACL(dir->d_inode)) 1525ce3b0f8dSAl Viro mode &= ~current_umask(); 1526be6d3e56SKentaro Takeda error = security_path_mknod(&nd->path, path->dentry, mode, 0); 1527be6d3e56SKentaro Takeda if (error) 1528be6d3e56SKentaro Takeda goto out_unlock; 1529aab520e2SDave Hansen error = vfs_create(dir->d_inode, path->dentry, mode, nd); 1530be6d3e56SKentaro Takeda out_unlock: 1531aab520e2SDave Hansen mutex_unlock(&dir->d_inode->i_mutex); 15324ac91378SJan Blunck dput(nd->path.dentry); 15334ac91378SJan Blunck nd->path.dentry = path->dentry; 1534aab520e2SDave Hansen if (error) 1535aab520e2SDave Hansen return error; 1536aab520e2SDave Hansen /* Don't check for write permission, don't truncate */ 15373fb64190SChristoph Hellwig return may_open(&nd->path, 0, flag & ~O_TRUNC); 1538aab520e2SDave Hansen } 1539aab520e2SDave Hansen 15401da177e4SLinus Torvalds /* 1541d57999e1SDave Hansen * Note that while the flag value (low two bits) for sys_open means: 1542d57999e1SDave Hansen * 00 - read-only 1543d57999e1SDave Hansen * 01 - write-only 1544d57999e1SDave Hansen * 10 - read-write 1545d57999e1SDave Hansen * 11 - special 1546d57999e1SDave Hansen * it is changed into 1547d57999e1SDave Hansen * 00 - no permissions needed 1548d57999e1SDave Hansen * 01 - read-permission 1549d57999e1SDave Hansen * 10 - write-permission 1550d57999e1SDave Hansen * 11 - read-write 1551d57999e1SDave Hansen * for the internal routines (ie open_namei()/follow_link() etc) 1552d57999e1SDave Hansen * This is more logical, and also allows the 00 "no perm needed" 1553d57999e1SDave Hansen * to be used for symlinks (where the permissions are checked 1554d57999e1SDave Hansen * later). 1555d57999e1SDave Hansen * 1556d57999e1SDave Hansen */ 1557d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 1558d57999e1SDave Hansen { 1559d57999e1SDave Hansen if ((flag+1) & O_ACCMODE) 1560d57999e1SDave Hansen flag++; 1561d57999e1SDave Hansen return flag; 1562d57999e1SDave Hansen } 1563d57999e1SDave Hansen 15644a3fd211SDave Hansen static int open_will_write_to_fs(int flag, struct inode *inode) 15654a3fd211SDave Hansen { 1566d57999e1SDave Hansen /* 15674a3fd211SDave Hansen * We'll never write to the fs underlying 15684a3fd211SDave Hansen * a device file. 15694a3fd211SDave Hansen */ 15704a3fd211SDave Hansen if (special_file(inode->i_mode)) 15714a3fd211SDave Hansen return 0; 15724a3fd211SDave Hansen return (flag & O_TRUNC); 15734a3fd211SDave Hansen } 15744a3fd211SDave Hansen 15754a3fd211SDave Hansen /* 15764a3fd211SDave Hansen * Note that the low bits of the passed in "open_flag" 15774a3fd211SDave Hansen * are not the same as in the local variable "flag". See 15784a3fd211SDave Hansen * open_to_namei_flags() for more details. 15791da177e4SLinus Torvalds */ 1580a70e65dfSChristoph Hellwig struct file *do_filp_open(int dfd, const char *pathname, 15816e8341a1SAl Viro int open_flag, int mode, int acc_mode) 15821da177e4SLinus Torvalds { 15834a3fd211SDave Hansen struct file *filp; 1584a70e65dfSChristoph Hellwig struct nameidata nd; 15856e8341a1SAl Viro int error; 15866de88d72SAl Viro struct path path, save; 15871da177e4SLinus Torvalds struct dentry *dir; 15881da177e4SLinus Torvalds int count = 0; 15894a3fd211SDave Hansen int will_write; 1590d57999e1SDave Hansen int flag = open_to_namei_flags(open_flag); 15911da177e4SLinus Torvalds 15926b2f3d1fSChristoph Hellwig /* 15936b2f3d1fSChristoph Hellwig * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only 15946b2f3d1fSChristoph Hellwig * check for O_DSYNC if the need any syncing at all we enforce it's 15956b2f3d1fSChristoph Hellwig * always set instead of having to deal with possibly weird behaviour 15966b2f3d1fSChristoph Hellwig * for malicious applications setting only __O_SYNC. 15976b2f3d1fSChristoph Hellwig */ 15986b2f3d1fSChristoph Hellwig if (open_flag & __O_SYNC) 15996b2f3d1fSChristoph Hellwig open_flag |= O_DSYNC; 16006b2f3d1fSChristoph Hellwig 16016e8341a1SAl Viro if (!acc_mode) 1602b77b0646SAl Viro acc_mode = MAY_OPEN | ACC_MODE(flag); 16031da177e4SLinus Torvalds 1604834f2a4aSTrond Myklebust /* O_TRUNC implies we need access checks for write permissions */ 1605834f2a4aSTrond Myklebust if (flag & O_TRUNC) 1606834f2a4aSTrond Myklebust acc_mode |= MAY_WRITE; 1607834f2a4aSTrond Myklebust 16081da177e4SLinus Torvalds /* Allow the LSM permission hook to distinguish append 16091da177e4SLinus Torvalds access from general write access. */ 16101da177e4SLinus Torvalds if (flag & O_APPEND) 16111da177e4SLinus Torvalds acc_mode |= MAY_APPEND; 16121da177e4SLinus Torvalds 16131da177e4SLinus Torvalds /* 16141da177e4SLinus Torvalds * The simplest case - just a plain lookup. 16151da177e4SLinus Torvalds */ 16161da177e4SLinus Torvalds if (!(flag & O_CREAT)) { 16172dd6d1f4SAl Viro filp = get_empty_filp(); 16182dd6d1f4SAl Viro 16192dd6d1f4SAl Viro if (filp == NULL) 16202dd6d1f4SAl Viro return ERR_PTR(-ENFILE); 16212dd6d1f4SAl Viro nd.intent.open.file = filp; 16222dd6d1f4SAl Viro nd.intent.open.flags = flag; 16232dd6d1f4SAl Viro nd.intent.open.create_mode = 0; 16242dd6d1f4SAl Viro error = do_path_lookup(dfd, pathname, 16252dd6d1f4SAl Viro lookup_flags(flag)|LOOKUP_OPEN, &nd); 16262dd6d1f4SAl Viro if (IS_ERR(nd.intent.open.file)) { 16272dd6d1f4SAl Viro if (error == 0) { 16282dd6d1f4SAl Viro error = PTR_ERR(nd.intent.open.file); 16292dd6d1f4SAl Viro path_put(&nd.path); 16302dd6d1f4SAl Viro } 16312dd6d1f4SAl Viro } else if (error) 16322dd6d1f4SAl Viro release_open_intent(&nd); 16331da177e4SLinus Torvalds if (error) 1634a70e65dfSChristoph Hellwig return ERR_PTR(error); 16351da177e4SLinus Torvalds goto ok; 16361da177e4SLinus Torvalds } 16371da177e4SLinus Torvalds 16381da177e4SLinus Torvalds /* 16391da177e4SLinus Torvalds * Create - we need to know the parent. 16401da177e4SLinus Torvalds */ 16419b4a9b14SAl Viro error = path_init(dfd, pathname, LOOKUP_PARENT, &nd); 16421da177e4SLinus Torvalds if (error) 1643a70e65dfSChristoph Hellwig return ERR_PTR(error); 16449b4a9b14SAl Viro error = path_walk(pathname, &nd); 1645654f562cSJ. R. Okajima if (error) { 1646654f562cSJ. R. Okajima if (nd.root.mnt) 1647654f562cSJ. R. Okajima path_put(&nd.root); 16489b4a9b14SAl Viro return ERR_PTR(error); 1649654f562cSJ. R. Okajima } 16509b4a9b14SAl Viro if (unlikely(!audit_dummy_context())) 16519b4a9b14SAl Viro audit_inode(pathname, nd.path.dentry); 16521da177e4SLinus Torvalds 16531da177e4SLinus Torvalds /* 16541da177e4SLinus Torvalds * We have the parent and last component. First of all, check 16551da177e4SLinus Torvalds * that we are not asked to creat(2) an obvious directory - that 16561da177e4SLinus Torvalds * will not do. 16571da177e4SLinus Torvalds */ 16581da177e4SLinus Torvalds error = -EISDIR; 1659a70e65dfSChristoph Hellwig if (nd.last_type != LAST_NORM || nd.last.name[nd.last.len]) 16608737f3a1SAl Viro goto exit_parent; 16611da177e4SLinus Torvalds 16628737f3a1SAl Viro error = -ENFILE; 16638737f3a1SAl Viro filp = get_empty_filp(); 16648737f3a1SAl Viro if (filp == NULL) 16658737f3a1SAl Viro goto exit_parent; 16668737f3a1SAl Viro nd.intent.open.file = filp; 16678737f3a1SAl Viro nd.intent.open.flags = flag; 16688737f3a1SAl Viro nd.intent.open.create_mode = mode; 1669a70e65dfSChristoph Hellwig dir = nd.path.dentry; 1670a70e65dfSChristoph Hellwig nd.flags &= ~LOOKUP_PARENT; 16718737f3a1SAl Viro nd.flags |= LOOKUP_CREATE | LOOKUP_OPEN; 16723516586aSAl Viro if (flag & O_EXCL) 16733516586aSAl Viro nd.flags |= LOOKUP_EXCL; 16741b1dcc1bSJes Sorensen mutex_lock(&dir->d_inode->i_mutex); 1675a70e65dfSChristoph Hellwig path.dentry = lookup_hash(&nd); 1676a70e65dfSChristoph Hellwig path.mnt = nd.path.mnt; 16771da177e4SLinus Torvalds 16781da177e4SLinus Torvalds do_last: 16794e7506e4SAl Viro error = PTR_ERR(path.dentry); 16804e7506e4SAl Viro if (IS_ERR(path.dentry)) { 16811b1dcc1bSJes Sorensen mutex_unlock(&dir->d_inode->i_mutex); 16821da177e4SLinus Torvalds goto exit; 16831da177e4SLinus Torvalds } 16841da177e4SLinus Torvalds 1685a70e65dfSChristoph Hellwig if (IS_ERR(nd.intent.open.file)) { 1686a70e65dfSChristoph Hellwig error = PTR_ERR(nd.intent.open.file); 16874a3fd211SDave Hansen goto exit_mutex_unlock; 16884af4c52fSOleg Drokin } 16894af4c52fSOleg Drokin 16901da177e4SLinus Torvalds /* Negative dentry, just create the file */ 16914e7506e4SAl Viro if (!path.dentry->d_inode) { 16924a3fd211SDave Hansen /* 16934a3fd211SDave Hansen * This write is needed to ensure that a 16944a3fd211SDave Hansen * ro->rw transition does not occur between 16954a3fd211SDave Hansen * the time when the file is created and when 16964a3fd211SDave Hansen * a permanent write count is taken through 16974a3fd211SDave Hansen * the 'struct file' in nameidata_to_filp(). 16984a3fd211SDave Hansen */ 16994a3fd211SDave Hansen error = mnt_want_write(nd.path.mnt); 17001da177e4SLinus Torvalds if (error) 17014a3fd211SDave Hansen goto exit_mutex_unlock; 17024a3fd211SDave Hansen error = __open_namei_create(&nd, &path, flag, mode); 17034a3fd211SDave Hansen if (error) { 17044a3fd211SDave Hansen mnt_drop_write(nd.path.mnt); 17051da177e4SLinus Torvalds goto exit; 17064a3fd211SDave Hansen } 17074a3fd211SDave Hansen filp = nameidata_to_filp(&nd, open_flag); 170894e5d714SMimi Zohar if (IS_ERR(filp)) 170994e5d714SMimi Zohar ima_counts_put(&nd.path, 171094e5d714SMimi Zohar acc_mode & (MAY_READ | MAY_WRITE | 171194e5d714SMimi Zohar MAY_EXEC)); 17124a3fd211SDave Hansen mnt_drop_write(nd.path.mnt); 1713654f562cSJ. R. Okajima if (nd.root.mnt) 1714654f562cSJ. R. Okajima path_put(&nd.root); 17154a3fd211SDave Hansen return filp; 17161da177e4SLinus Torvalds } 17171da177e4SLinus Torvalds 17181da177e4SLinus Torvalds /* 17191da177e4SLinus Torvalds * It already exists. 17201da177e4SLinus Torvalds */ 17211b1dcc1bSJes Sorensen mutex_unlock(&dir->d_inode->i_mutex); 17225a190ae6SAl Viro audit_inode(pathname, path.dentry); 17231da177e4SLinus Torvalds 17241da177e4SLinus Torvalds error = -EEXIST; 17251da177e4SLinus Torvalds if (flag & O_EXCL) 17261da177e4SLinus Torvalds goto exit_dput; 17271da177e4SLinus Torvalds 1728e13b210fSAl Viro if (__follow_mount(&path)) { 17291da177e4SLinus Torvalds error = -ELOOP; 1730ba7a4c1aSAl Viro if (flag & O_NOFOLLOW) 1731ba7a4c1aSAl Viro goto exit_dput; 17321da177e4SLinus Torvalds } 17333e2efce0SAmy Griffis 17341da177e4SLinus Torvalds error = -ENOENT; 17354e7506e4SAl Viro if (!path.dentry->d_inode) 17361da177e4SLinus Torvalds goto exit_dput; 1737acfa4380SAl Viro if (path.dentry->d_inode->i_op->follow_link) 17381da177e4SLinus Torvalds goto do_link; 17391da177e4SLinus Torvalds 1740a70e65dfSChristoph Hellwig path_to_nameidata(&path, &nd); 17411da177e4SLinus Torvalds error = -EISDIR; 17424e7506e4SAl Viro if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode)) 17431da177e4SLinus Torvalds goto exit; 17441da177e4SLinus Torvalds ok: 17454a3fd211SDave Hansen /* 17464a3fd211SDave Hansen * Consider: 17474a3fd211SDave Hansen * 1. may_open() truncates a file 17484a3fd211SDave Hansen * 2. a rw->ro mount transition occurs 17494a3fd211SDave Hansen * 3. nameidata_to_filp() fails due to 17504a3fd211SDave Hansen * the ro mount. 17514a3fd211SDave Hansen * That would be inconsistent, and should 17524a3fd211SDave Hansen * be avoided. Taking this mnt write here 17534a3fd211SDave Hansen * ensures that (2) can not occur. 17544a3fd211SDave Hansen */ 17554a3fd211SDave Hansen will_write = open_will_write_to_fs(flag, nd.path.dentry->d_inode); 17564a3fd211SDave Hansen if (will_write) { 17574a3fd211SDave Hansen error = mnt_want_write(nd.path.mnt); 17581da177e4SLinus Torvalds if (error) 17591da177e4SLinus Torvalds goto exit; 17604a3fd211SDave Hansen } 17613fb64190SChristoph Hellwig error = may_open(&nd.path, acc_mode, flag); 17624a3fd211SDave Hansen if (error) { 17634a3fd211SDave Hansen if (will_write) 17644a3fd211SDave Hansen mnt_drop_write(nd.path.mnt); 17654a3fd211SDave Hansen goto exit; 17664a3fd211SDave Hansen } 17674a3fd211SDave Hansen filp = nameidata_to_filp(&nd, open_flag); 176894e5d714SMimi Zohar if (IS_ERR(filp)) 176994e5d714SMimi Zohar ima_counts_put(&nd.path, 177094e5d714SMimi Zohar acc_mode & (MAY_READ | MAY_WRITE | MAY_EXEC)); 17714a3fd211SDave Hansen /* 17724a3fd211SDave Hansen * It is now safe to drop the mnt write 17734a3fd211SDave Hansen * because the filp has had a write taken 17744a3fd211SDave Hansen * on its behalf. 17754a3fd211SDave Hansen */ 17764a3fd211SDave Hansen if (will_write) 17774a3fd211SDave Hansen mnt_drop_write(nd.path.mnt); 1778654f562cSJ. R. Okajima if (nd.root.mnt) 1779654f562cSJ. R. Okajima path_put(&nd.root); 17804a3fd211SDave Hansen return filp; 17811da177e4SLinus Torvalds 17824a3fd211SDave Hansen exit_mutex_unlock: 17834a3fd211SDave Hansen mutex_unlock(&dir->d_inode->i_mutex); 17841da177e4SLinus Torvalds exit_dput: 1785a70e65dfSChristoph Hellwig path_put_conditional(&path, &nd); 17861da177e4SLinus Torvalds exit: 1787a70e65dfSChristoph Hellwig if (!IS_ERR(nd.intent.open.file)) 1788a70e65dfSChristoph Hellwig release_open_intent(&nd); 17898737f3a1SAl Viro exit_parent: 17902a737871SAl Viro if (nd.root.mnt) 17912a737871SAl Viro path_put(&nd.root); 1792a70e65dfSChristoph Hellwig path_put(&nd.path); 1793a70e65dfSChristoph Hellwig return ERR_PTR(error); 17941da177e4SLinus Torvalds 17951da177e4SLinus Torvalds do_link: 17961da177e4SLinus Torvalds error = -ELOOP; 17971da177e4SLinus Torvalds if (flag & O_NOFOLLOW) 17981da177e4SLinus Torvalds goto exit_dput; 17991da177e4SLinus Torvalds /* 18001da177e4SLinus Torvalds * This is subtle. Instead of calling do_follow_link() we do the 18011da177e4SLinus Torvalds * thing by hands. The reason is that this way we have zero link_count 18021da177e4SLinus Torvalds * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT. 18031da177e4SLinus Torvalds * After that we have the parent and last component, i.e. 18041da177e4SLinus Torvalds * we are in the same situation as after the first path_walk(). 18051da177e4SLinus Torvalds * Well, almost - if the last component is normal we get its copy 18061da177e4SLinus Torvalds * stored in nd->last.name and we will have to putname() it when we 18071da177e4SLinus Torvalds * are done. Procfs-like symlinks just set LAST_BIND. 18081da177e4SLinus Torvalds */ 1809a70e65dfSChristoph Hellwig nd.flags |= LOOKUP_PARENT; 1810a70e65dfSChristoph Hellwig error = security_inode_follow_link(path.dentry, &nd); 18111da177e4SLinus Torvalds if (error) 18121da177e4SLinus Torvalds goto exit_dput; 18136de88d72SAl Viro save = nd.path; 18146de88d72SAl Viro path_get(&save); 1815a70e65dfSChristoph Hellwig error = __do_follow_link(&path, &nd); 18166de88d72SAl Viro if (error == -ESTALE) { 18176de88d72SAl Viro /* nd.path had been dropped */ 18186de88d72SAl Viro nd.path = save; 18196de88d72SAl Viro path_get(&nd.path); 18206de88d72SAl Viro nd.flags |= LOOKUP_REVAL; 18216de88d72SAl Viro error = __do_follow_link(&path, &nd); 18226de88d72SAl Viro } 18236de88d72SAl Viro path_put(&save); 1824258fa999SAl Viro path_put(&path); 1825de459215SKirill Korotaev if (error) { 1826de459215SKirill Korotaev /* Does someone understand code flow here? Or it is only 1827de459215SKirill Korotaev * me so stupid? Anathema to whoever designed this non-sense 1828de459215SKirill Korotaev * with "intent.open". 1829de459215SKirill Korotaev */ 1830a70e65dfSChristoph Hellwig release_open_intent(&nd); 1831654f562cSJ. R. Okajima if (nd.root.mnt) 1832654f562cSJ. R. Okajima path_put(&nd.root); 1833a70e65dfSChristoph Hellwig return ERR_PTR(error); 1834de459215SKirill Korotaev } 1835a70e65dfSChristoph Hellwig nd.flags &= ~LOOKUP_PARENT; 1836a70e65dfSChristoph Hellwig if (nd.last_type == LAST_BIND) 18371da177e4SLinus Torvalds goto ok; 18381da177e4SLinus Torvalds error = -EISDIR; 1839a70e65dfSChristoph Hellwig if (nd.last_type != LAST_NORM) 18401da177e4SLinus Torvalds goto exit; 1841a70e65dfSChristoph Hellwig if (nd.last.name[nd.last.len]) { 1842a70e65dfSChristoph Hellwig __putname(nd.last.name); 18431da177e4SLinus Torvalds goto exit; 18441da177e4SLinus Torvalds } 18451da177e4SLinus Torvalds error = -ELOOP; 18461da177e4SLinus Torvalds if (count++==32) { 1847a70e65dfSChristoph Hellwig __putname(nd.last.name); 18481da177e4SLinus Torvalds goto exit; 18491da177e4SLinus Torvalds } 1850a70e65dfSChristoph Hellwig dir = nd.path.dentry; 18511b1dcc1bSJes Sorensen mutex_lock(&dir->d_inode->i_mutex); 1852a70e65dfSChristoph Hellwig path.dentry = lookup_hash(&nd); 1853a70e65dfSChristoph Hellwig path.mnt = nd.path.mnt; 1854a70e65dfSChristoph Hellwig __putname(nd.last.name); 18551da177e4SLinus Torvalds goto do_last; 18561da177e4SLinus Torvalds } 18571da177e4SLinus Torvalds 18581da177e4SLinus Torvalds /** 1859a70e65dfSChristoph Hellwig * filp_open - open file and return file pointer 1860a70e65dfSChristoph Hellwig * 1861a70e65dfSChristoph Hellwig * @filename: path to open 1862a70e65dfSChristoph Hellwig * @flags: open flags as per the open(2) second argument 1863a70e65dfSChristoph Hellwig * @mode: mode for the new file if O_CREAT is set, else ignored 1864a70e65dfSChristoph Hellwig * 1865a70e65dfSChristoph Hellwig * This is the helper to open a file from kernelspace if you really 1866a70e65dfSChristoph Hellwig * have to. But in generally you should not do this, so please move 1867a70e65dfSChristoph Hellwig * along, nothing to see here.. 1868a70e65dfSChristoph Hellwig */ 1869a70e65dfSChristoph Hellwig struct file *filp_open(const char *filename, int flags, int mode) 1870a70e65dfSChristoph Hellwig { 18716e8341a1SAl Viro return do_filp_open(AT_FDCWD, filename, flags, mode, 0); 1872a70e65dfSChristoph Hellwig } 1873a70e65dfSChristoph Hellwig EXPORT_SYMBOL(filp_open); 1874a70e65dfSChristoph Hellwig 1875a70e65dfSChristoph Hellwig /** 18761da177e4SLinus Torvalds * lookup_create - lookup a dentry, creating it if it doesn't exist 18771da177e4SLinus Torvalds * @nd: nameidata info 18781da177e4SLinus Torvalds * @is_dir: directory flag 18791da177e4SLinus Torvalds * 18801da177e4SLinus Torvalds * Simple function to lookup and return a dentry and create it 18811da177e4SLinus Torvalds * if it doesn't exist. Is SMP-safe. 1882c663e5d8SChristoph Hellwig * 18834ac91378SJan Blunck * Returns with nd->path.dentry->d_inode->i_mutex locked. 18841da177e4SLinus Torvalds */ 18851da177e4SLinus Torvalds struct dentry *lookup_create(struct nameidata *nd, int is_dir) 18861da177e4SLinus Torvalds { 1887c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 18881da177e4SLinus Torvalds 18894ac91378SJan Blunck mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 1890c663e5d8SChristoph Hellwig /* 1891c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 1892c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 1893c663e5d8SChristoph Hellwig */ 18941da177e4SLinus Torvalds if (nd->last_type != LAST_NORM) 18951da177e4SLinus Torvalds goto fail; 18961da177e4SLinus Torvalds nd->flags &= ~LOOKUP_PARENT; 18973516586aSAl Viro nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL; 1898a634904aSASANO Masahiro nd->intent.open.flags = O_EXCL; 1899c663e5d8SChristoph Hellwig 1900c663e5d8SChristoph Hellwig /* 1901c663e5d8SChristoph Hellwig * Do the final lookup. 1902c663e5d8SChristoph Hellwig */ 190349705b77SChristoph Hellwig dentry = lookup_hash(nd); 19041da177e4SLinus Torvalds if (IS_ERR(dentry)) 19051da177e4SLinus Torvalds goto fail; 1906c663e5d8SChristoph Hellwig 1907e9baf6e5SAl Viro if (dentry->d_inode) 1908e9baf6e5SAl Viro goto eexist; 1909c663e5d8SChristoph Hellwig /* 1910c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 1911c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 1912c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 1913c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 1914c663e5d8SChristoph Hellwig */ 1915e9baf6e5SAl Viro if (unlikely(!is_dir && nd->last.name[nd->last.len])) { 19161da177e4SLinus Torvalds dput(dentry); 19171da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 1918e9baf6e5SAl Viro } 1919e9baf6e5SAl Viro return dentry; 1920e9baf6e5SAl Viro eexist: 1921e9baf6e5SAl Viro dput(dentry); 1922e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 19231da177e4SLinus Torvalds fail: 19241da177e4SLinus Torvalds return dentry; 19251da177e4SLinus Torvalds } 1926f81a0bffSChristoph Hellwig EXPORT_SYMBOL_GPL(lookup_create); 19271da177e4SLinus Torvalds 19281da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 19291da177e4SLinus Torvalds { 1930a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 19311da177e4SLinus Torvalds 19321da177e4SLinus Torvalds if (error) 19331da177e4SLinus Torvalds return error; 19341da177e4SLinus Torvalds 19351da177e4SLinus Torvalds if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) 19361da177e4SLinus Torvalds return -EPERM; 19371da177e4SLinus Torvalds 1938acfa4380SAl Viro if (!dir->i_op->mknod) 19391da177e4SLinus Torvalds return -EPERM; 19401da177e4SLinus Torvalds 194108ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 194208ce5f16SSerge E. Hallyn if (error) 194308ce5f16SSerge E. Hallyn return error; 194408ce5f16SSerge E. Hallyn 19451da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 19461da177e4SLinus Torvalds if (error) 19471da177e4SLinus Torvalds return error; 19481da177e4SLinus Torvalds 19499e3509e2SJan Kara vfs_dq_init(dir); 19501da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 1951a74574aaSStephen Smalley if (!error) 1952f38aa942SAmy Griffis fsnotify_create(dir, dentry); 19531da177e4SLinus Torvalds return error; 19541da177e4SLinus Torvalds } 19551da177e4SLinus Torvalds 1956463c3197SDave Hansen static int may_mknod(mode_t mode) 1957463c3197SDave Hansen { 1958463c3197SDave Hansen switch (mode & S_IFMT) { 1959463c3197SDave Hansen case S_IFREG: 1960463c3197SDave Hansen case S_IFCHR: 1961463c3197SDave Hansen case S_IFBLK: 1962463c3197SDave Hansen case S_IFIFO: 1963463c3197SDave Hansen case S_IFSOCK: 1964463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 1965463c3197SDave Hansen return 0; 1966463c3197SDave Hansen case S_IFDIR: 1967463c3197SDave Hansen return -EPERM; 1968463c3197SDave Hansen default: 1969463c3197SDave Hansen return -EINVAL; 1970463c3197SDave Hansen } 1971463c3197SDave Hansen } 1972463c3197SDave Hansen 19732e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode, 19742e4d0924SHeiko Carstens unsigned, dev) 19751da177e4SLinus Torvalds { 19762ad94ae6SAl Viro int error; 19771da177e4SLinus Torvalds char *tmp; 19781da177e4SLinus Torvalds struct dentry *dentry; 19791da177e4SLinus Torvalds struct nameidata nd; 19801da177e4SLinus Torvalds 19811da177e4SLinus Torvalds if (S_ISDIR(mode)) 19821da177e4SLinus Torvalds return -EPERM; 19831da177e4SLinus Torvalds 19842ad94ae6SAl Viro error = user_path_parent(dfd, filename, &nd, &tmp); 19851da177e4SLinus Torvalds if (error) 19862ad94ae6SAl Viro return error; 19872ad94ae6SAl Viro 19881da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 1989463c3197SDave Hansen if (IS_ERR(dentry)) { 19901da177e4SLinus Torvalds error = PTR_ERR(dentry); 1991463c3197SDave Hansen goto out_unlock; 1992463c3197SDave Hansen } 19934ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 1994ce3b0f8dSAl Viro mode &= ~current_umask(); 1995463c3197SDave Hansen error = may_mknod(mode); 1996463c3197SDave Hansen if (error) 1997463c3197SDave Hansen goto out_dput; 1998463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 1999463c3197SDave Hansen if (error) 2000463c3197SDave Hansen goto out_dput; 2001be6d3e56SKentaro Takeda error = security_path_mknod(&nd.path, dentry, mode, dev); 2002be6d3e56SKentaro Takeda if (error) 2003be6d3e56SKentaro Takeda goto out_drop_write; 20041da177e4SLinus Torvalds switch (mode & S_IFMT) { 20051da177e4SLinus Torvalds case 0: case S_IFREG: 20064ac91378SJan Blunck error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd); 20071da177e4SLinus Torvalds break; 20081da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 20094ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode, 20101da177e4SLinus Torvalds new_decode_dev(dev)); 20111da177e4SLinus Torvalds break; 20121da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 20134ac91378SJan Blunck error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0); 20141da177e4SLinus Torvalds break; 20151da177e4SLinus Torvalds } 2016be6d3e56SKentaro Takeda out_drop_write: 2017463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2018463c3197SDave Hansen out_dput: 20191da177e4SLinus Torvalds dput(dentry); 2020463c3197SDave Hansen out_unlock: 20214ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 20221d957f9bSJan Blunck path_put(&nd.path); 20231da177e4SLinus Torvalds putname(tmp); 20241da177e4SLinus Torvalds 20251da177e4SLinus Torvalds return error; 20261da177e4SLinus Torvalds } 20271da177e4SLinus Torvalds 20283480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev) 20295590ff0dSUlrich Drepper { 20305590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 20315590ff0dSUlrich Drepper } 20325590ff0dSUlrich Drepper 20331da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 20341da177e4SLinus Torvalds { 2035a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 20361da177e4SLinus Torvalds 20371da177e4SLinus Torvalds if (error) 20381da177e4SLinus Torvalds return error; 20391da177e4SLinus Torvalds 2040acfa4380SAl Viro if (!dir->i_op->mkdir) 20411da177e4SLinus Torvalds return -EPERM; 20421da177e4SLinus Torvalds 20431da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 20441da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 20451da177e4SLinus Torvalds if (error) 20461da177e4SLinus Torvalds return error; 20471da177e4SLinus Torvalds 20489e3509e2SJan Kara vfs_dq_init(dir); 20491da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2050a74574aaSStephen Smalley if (!error) 2051f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 20521da177e4SLinus Torvalds return error; 20531da177e4SLinus Torvalds } 20541da177e4SLinus Torvalds 20552e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode) 20561da177e4SLinus Torvalds { 20571da177e4SLinus Torvalds int error = 0; 20581da177e4SLinus Torvalds char * tmp; 20596902d925SDave Hansen struct dentry *dentry; 20606902d925SDave Hansen struct nameidata nd; 20611da177e4SLinus Torvalds 20622ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &tmp); 20632ad94ae6SAl Viro if (error) 20646902d925SDave Hansen goto out_err; 20651da177e4SLinus Torvalds 20661da177e4SLinus Torvalds dentry = lookup_create(&nd, 1); 20671da177e4SLinus Torvalds error = PTR_ERR(dentry); 20686902d925SDave Hansen if (IS_ERR(dentry)) 20696902d925SDave Hansen goto out_unlock; 20706902d925SDave Hansen 20714ac91378SJan Blunck if (!IS_POSIXACL(nd.path.dentry->d_inode)) 2072ce3b0f8dSAl Viro mode &= ~current_umask(); 2073463c3197SDave Hansen error = mnt_want_write(nd.path.mnt); 2074463c3197SDave Hansen if (error) 2075463c3197SDave Hansen goto out_dput; 2076be6d3e56SKentaro Takeda error = security_path_mkdir(&nd.path, dentry, mode); 2077be6d3e56SKentaro Takeda if (error) 2078be6d3e56SKentaro Takeda goto out_drop_write; 20794ac91378SJan Blunck error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode); 2080be6d3e56SKentaro Takeda out_drop_write: 2081463c3197SDave Hansen mnt_drop_write(nd.path.mnt); 2082463c3197SDave Hansen out_dput: 20831da177e4SLinus Torvalds dput(dentry); 20846902d925SDave Hansen out_unlock: 20854ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 20861d957f9bSJan Blunck path_put(&nd.path); 20871da177e4SLinus Torvalds putname(tmp); 20886902d925SDave Hansen out_err: 20891da177e4SLinus Torvalds return error; 20901da177e4SLinus Torvalds } 20911da177e4SLinus Torvalds 20923cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode) 20935590ff0dSUlrich Drepper { 20945590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 20955590ff0dSUlrich Drepper } 20965590ff0dSUlrich Drepper 20971da177e4SLinus Torvalds /* 20981da177e4SLinus Torvalds * We try to drop the dentry early: we should have 20991da177e4SLinus Torvalds * a usage count of 2 if we're the only user of this 21001da177e4SLinus Torvalds * dentry, and if that is true (possibly after pruning 21011da177e4SLinus Torvalds * the dcache), then we drop the dentry now. 21021da177e4SLinus Torvalds * 21031da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 21041da177e4SLinus Torvalds * do a 21051da177e4SLinus Torvalds * 21061da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 21071da177e4SLinus Torvalds * return -EBUSY; 21081da177e4SLinus Torvalds * 21091da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 21101da177e4SLinus Torvalds * that is still in use by something else.. 21111da177e4SLinus Torvalds */ 21121da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 21131da177e4SLinus Torvalds { 21141da177e4SLinus Torvalds dget(dentry); 21151da177e4SLinus Torvalds shrink_dcache_parent(dentry); 21161da177e4SLinus Torvalds spin_lock(&dcache_lock); 21171da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 21181da177e4SLinus Torvalds if (atomic_read(&dentry->d_count) == 2) 21191da177e4SLinus Torvalds __d_drop(dentry); 21201da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 21211da177e4SLinus Torvalds spin_unlock(&dcache_lock); 21221da177e4SLinus Torvalds } 21231da177e4SLinus Torvalds 21241da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 21251da177e4SLinus Torvalds { 21261da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 21271da177e4SLinus Torvalds 21281da177e4SLinus Torvalds if (error) 21291da177e4SLinus Torvalds return error; 21301da177e4SLinus Torvalds 2131acfa4380SAl Viro if (!dir->i_op->rmdir) 21321da177e4SLinus Torvalds return -EPERM; 21331da177e4SLinus Torvalds 21349e3509e2SJan Kara vfs_dq_init(dir); 21351da177e4SLinus Torvalds 21361b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 21371da177e4SLinus Torvalds dentry_unhash(dentry); 21381da177e4SLinus Torvalds if (d_mountpoint(dentry)) 21391da177e4SLinus Torvalds error = -EBUSY; 21401da177e4SLinus Torvalds else { 21411da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 21421da177e4SLinus Torvalds if (!error) { 21431da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 21441da177e4SLinus Torvalds if (!error) 21451da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 21461da177e4SLinus Torvalds } 21471da177e4SLinus Torvalds } 21481b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 21491da177e4SLinus Torvalds if (!error) { 21501da177e4SLinus Torvalds d_delete(dentry); 21511da177e4SLinus Torvalds } 21521da177e4SLinus Torvalds dput(dentry); 21531da177e4SLinus Torvalds 21541da177e4SLinus Torvalds return error; 21551da177e4SLinus Torvalds } 21561da177e4SLinus Torvalds 21575590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 21581da177e4SLinus Torvalds { 21591da177e4SLinus Torvalds int error = 0; 21601da177e4SLinus Torvalds char * name; 21611da177e4SLinus Torvalds struct dentry *dentry; 21621da177e4SLinus Torvalds struct nameidata nd; 21631da177e4SLinus Torvalds 21642ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 21651da177e4SLinus Torvalds if (error) 21662ad94ae6SAl Viro return error; 21671da177e4SLinus Torvalds 21681da177e4SLinus Torvalds switch(nd.last_type) { 21691da177e4SLinus Torvalds case LAST_DOTDOT: 21701da177e4SLinus Torvalds error = -ENOTEMPTY; 21711da177e4SLinus Torvalds goto exit1; 21721da177e4SLinus Torvalds case LAST_DOT: 21731da177e4SLinus Torvalds error = -EINVAL; 21741da177e4SLinus Torvalds goto exit1; 21751da177e4SLinus Torvalds case LAST_ROOT: 21761da177e4SLinus Torvalds error = -EBUSY; 21771da177e4SLinus Torvalds goto exit1; 21781da177e4SLinus Torvalds } 21790612d9fbSOGAWA Hirofumi 21800612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 21810612d9fbSOGAWA Hirofumi 21824ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 218349705b77SChristoph Hellwig dentry = lookup_hash(&nd); 21841da177e4SLinus Torvalds error = PTR_ERR(dentry); 21856902d925SDave Hansen if (IS_ERR(dentry)) 21866902d925SDave Hansen goto exit2; 21870622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 21880622753bSDave Hansen if (error) 21890622753bSDave Hansen goto exit3; 2190be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 2191be6d3e56SKentaro Takeda if (error) 2192be6d3e56SKentaro Takeda goto exit4; 21934ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 2194be6d3e56SKentaro Takeda exit4: 21950622753bSDave Hansen mnt_drop_write(nd.path.mnt); 21960622753bSDave Hansen exit3: 21971da177e4SLinus Torvalds dput(dentry); 21986902d925SDave Hansen exit2: 21994ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 22001da177e4SLinus Torvalds exit1: 22011d957f9bSJan Blunck path_put(&nd.path); 22021da177e4SLinus Torvalds putname(name); 22031da177e4SLinus Torvalds return error; 22041da177e4SLinus Torvalds } 22051da177e4SLinus Torvalds 22063cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 22075590ff0dSUlrich Drepper { 22085590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 22095590ff0dSUlrich Drepper } 22105590ff0dSUlrich Drepper 22111da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 22121da177e4SLinus Torvalds { 22131da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 22141da177e4SLinus Torvalds 22151da177e4SLinus Torvalds if (error) 22161da177e4SLinus Torvalds return error; 22171da177e4SLinus Torvalds 2218acfa4380SAl Viro if (!dir->i_op->unlink) 22191da177e4SLinus Torvalds return -EPERM; 22201da177e4SLinus Torvalds 22219e3509e2SJan Kara vfs_dq_init(dir); 22221da177e4SLinus Torvalds 22231b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 22241da177e4SLinus Torvalds if (d_mountpoint(dentry)) 22251da177e4SLinus Torvalds error = -EBUSY; 22261da177e4SLinus Torvalds else { 22271da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 22281da177e4SLinus Torvalds if (!error) 22291da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 22301da177e4SLinus Torvalds } 22311b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 22321da177e4SLinus Torvalds 22331da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 22341da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 2235ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 22361da177e4SLinus Torvalds d_delete(dentry); 22371da177e4SLinus Torvalds } 22380eeca283SRobert Love 22391da177e4SLinus Torvalds return error; 22401da177e4SLinus Torvalds } 22411da177e4SLinus Torvalds 22421da177e4SLinus Torvalds /* 22431da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 22441b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 22451da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 22461da177e4SLinus Torvalds * while waiting on the I/O. 22471da177e4SLinus Torvalds */ 22485590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 22491da177e4SLinus Torvalds { 22502ad94ae6SAl Viro int error; 22511da177e4SLinus Torvalds char *name; 22521da177e4SLinus Torvalds struct dentry *dentry; 22531da177e4SLinus Torvalds struct nameidata nd; 22541da177e4SLinus Torvalds struct inode *inode = NULL; 22551da177e4SLinus Torvalds 22562ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 22571da177e4SLinus Torvalds if (error) 22582ad94ae6SAl Viro return error; 22592ad94ae6SAl Viro 22601da177e4SLinus Torvalds error = -EISDIR; 22611da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 22621da177e4SLinus Torvalds goto exit1; 22630612d9fbSOGAWA Hirofumi 22640612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 22650612d9fbSOGAWA Hirofumi 22664ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 226749705b77SChristoph Hellwig dentry = lookup_hash(&nd); 22681da177e4SLinus Torvalds error = PTR_ERR(dentry); 22691da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 22701da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 22711da177e4SLinus Torvalds if (nd.last.name[nd.last.len]) 22721da177e4SLinus Torvalds goto slashes; 22731da177e4SLinus Torvalds inode = dentry->d_inode; 22741da177e4SLinus Torvalds if (inode) 22751da177e4SLinus Torvalds atomic_inc(&inode->i_count); 22760622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 22770622753bSDave Hansen if (error) 22780622753bSDave Hansen goto exit2; 2279be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 2280be6d3e56SKentaro Takeda if (error) 2281be6d3e56SKentaro Takeda goto exit3; 22824ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 2283be6d3e56SKentaro Takeda exit3: 22840622753bSDave Hansen mnt_drop_write(nd.path.mnt); 22851da177e4SLinus Torvalds exit2: 22861da177e4SLinus Torvalds dput(dentry); 22871da177e4SLinus Torvalds } 22884ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 22891da177e4SLinus Torvalds if (inode) 22901da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 22911da177e4SLinus Torvalds exit1: 22921d957f9bSJan Blunck path_put(&nd.path); 22931da177e4SLinus Torvalds putname(name); 22941da177e4SLinus Torvalds return error; 22951da177e4SLinus Torvalds 22961da177e4SLinus Torvalds slashes: 22971da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 22981da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 22991da177e4SLinus Torvalds goto exit2; 23001da177e4SLinus Torvalds } 23011da177e4SLinus Torvalds 23022e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 23035590ff0dSUlrich Drepper { 23045590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 23055590ff0dSUlrich Drepper return -EINVAL; 23065590ff0dSUlrich Drepper 23075590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 23085590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 23095590ff0dSUlrich Drepper 23105590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 23115590ff0dSUlrich Drepper } 23125590ff0dSUlrich Drepper 23133480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 23145590ff0dSUlrich Drepper { 23155590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 23165590ff0dSUlrich Drepper } 23175590ff0dSUlrich Drepper 2318db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 23191da177e4SLinus Torvalds { 2320a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 23211da177e4SLinus Torvalds 23221da177e4SLinus Torvalds if (error) 23231da177e4SLinus Torvalds return error; 23241da177e4SLinus Torvalds 2325acfa4380SAl Viro if (!dir->i_op->symlink) 23261da177e4SLinus Torvalds return -EPERM; 23271da177e4SLinus Torvalds 23281da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 23291da177e4SLinus Torvalds if (error) 23301da177e4SLinus Torvalds return error; 23311da177e4SLinus Torvalds 23329e3509e2SJan Kara vfs_dq_init(dir); 23331da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 2334a74574aaSStephen Smalley if (!error) 2335f38aa942SAmy Griffis fsnotify_create(dir, dentry); 23361da177e4SLinus Torvalds return error; 23371da177e4SLinus Torvalds } 23381da177e4SLinus Torvalds 23392e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 23402e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 23411da177e4SLinus Torvalds { 23422ad94ae6SAl Viro int error; 23431da177e4SLinus Torvalds char *from; 23441da177e4SLinus Torvalds char *to; 23456902d925SDave Hansen struct dentry *dentry; 23466902d925SDave Hansen struct nameidata nd; 23471da177e4SLinus Torvalds 23481da177e4SLinus Torvalds from = getname(oldname); 23491da177e4SLinus Torvalds if (IS_ERR(from)) 23501da177e4SLinus Torvalds return PTR_ERR(from); 23512ad94ae6SAl Viro 23522ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 23532ad94ae6SAl Viro if (error) 23546902d925SDave Hansen goto out_putname; 23551da177e4SLinus Torvalds 23561da177e4SLinus Torvalds dentry = lookup_create(&nd, 0); 23571da177e4SLinus Torvalds error = PTR_ERR(dentry); 23586902d925SDave Hansen if (IS_ERR(dentry)) 23596902d925SDave Hansen goto out_unlock; 23606902d925SDave Hansen 236175c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 236275c3f29dSDave Hansen if (error) 236375c3f29dSDave Hansen goto out_dput; 2364be6d3e56SKentaro Takeda error = security_path_symlink(&nd.path, dentry, from); 2365be6d3e56SKentaro Takeda if (error) 2366be6d3e56SKentaro Takeda goto out_drop_write; 2367db2e747bSMiklos Szeredi error = vfs_symlink(nd.path.dentry->d_inode, dentry, from); 2368be6d3e56SKentaro Takeda out_drop_write: 236975c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 237075c3f29dSDave Hansen out_dput: 23711da177e4SLinus Torvalds dput(dentry); 23726902d925SDave Hansen out_unlock: 23734ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 23741d957f9bSJan Blunck path_put(&nd.path); 23751da177e4SLinus Torvalds putname(to); 23766902d925SDave Hansen out_putname: 23771da177e4SLinus Torvalds putname(from); 23781da177e4SLinus Torvalds return error; 23791da177e4SLinus Torvalds } 23801da177e4SLinus Torvalds 23813480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 23825590ff0dSUlrich Drepper { 23835590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 23845590ff0dSUlrich Drepper } 23855590ff0dSUlrich Drepper 23861da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 23871da177e4SLinus Torvalds { 23881da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 23891da177e4SLinus Torvalds int error; 23901da177e4SLinus Torvalds 23911da177e4SLinus Torvalds if (!inode) 23921da177e4SLinus Torvalds return -ENOENT; 23931da177e4SLinus Torvalds 2394a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 23951da177e4SLinus Torvalds if (error) 23961da177e4SLinus Torvalds return error; 23971da177e4SLinus Torvalds 23981da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 23991da177e4SLinus Torvalds return -EXDEV; 24001da177e4SLinus Torvalds 24011da177e4SLinus Torvalds /* 24021da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 24031da177e4SLinus Torvalds */ 24041da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 24051da177e4SLinus Torvalds return -EPERM; 2406acfa4380SAl Viro if (!dir->i_op->link) 24071da177e4SLinus Torvalds return -EPERM; 24087e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 24091da177e4SLinus Torvalds return -EPERM; 24101da177e4SLinus Torvalds 24111da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 24121da177e4SLinus Torvalds if (error) 24131da177e4SLinus Torvalds return error; 24141da177e4SLinus Torvalds 24157e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 24169e3509e2SJan Kara vfs_dq_init(dir); 24171da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 24187e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 2419e31e14ecSStephen Smalley if (!error) 24207e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 24211da177e4SLinus Torvalds return error; 24221da177e4SLinus Torvalds } 24231da177e4SLinus Torvalds 24241da177e4SLinus Torvalds /* 24251da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 24261da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 24271da177e4SLinus Torvalds * newname. --KAB 24281da177e4SLinus Torvalds * 24291da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 24301da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 24311da177e4SLinus Torvalds * and other special files. --ADM 24321da177e4SLinus Torvalds */ 24332e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 24342e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 24351da177e4SLinus Torvalds { 24361da177e4SLinus Torvalds struct dentry *new_dentry; 24372d8f3038SAl Viro struct nameidata nd; 24382d8f3038SAl Viro struct path old_path; 24391da177e4SLinus Torvalds int error; 24401da177e4SLinus Torvalds char *to; 24411da177e4SLinus Torvalds 244245c9b11aSUlrich Drepper if ((flags & ~AT_SYMLINK_FOLLOW) != 0) 2443c04030e1SUlrich Drepper return -EINVAL; 2444c04030e1SUlrich Drepper 24452d8f3038SAl Viro error = user_path_at(olddfd, oldname, 244645c9b11aSUlrich Drepper flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0, 24472d8f3038SAl Viro &old_path); 24481da177e4SLinus Torvalds if (error) 24492ad94ae6SAl Viro return error; 24502ad94ae6SAl Viro 24512ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &nd, &to); 24521da177e4SLinus Torvalds if (error) 24531da177e4SLinus Torvalds goto out; 24541da177e4SLinus Torvalds error = -EXDEV; 24552d8f3038SAl Viro if (old_path.mnt != nd.path.mnt) 24561da177e4SLinus Torvalds goto out_release; 24571da177e4SLinus Torvalds new_dentry = lookup_create(&nd, 0); 24581da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 24596902d925SDave Hansen if (IS_ERR(new_dentry)) 24606902d925SDave Hansen goto out_unlock; 246175c3f29dSDave Hansen error = mnt_want_write(nd.path.mnt); 246275c3f29dSDave Hansen if (error) 246375c3f29dSDave Hansen goto out_dput; 2464be6d3e56SKentaro Takeda error = security_path_link(old_path.dentry, &nd.path, new_dentry); 2465be6d3e56SKentaro Takeda if (error) 2466be6d3e56SKentaro Takeda goto out_drop_write; 24672d8f3038SAl Viro error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry); 2468be6d3e56SKentaro Takeda out_drop_write: 246975c3f29dSDave Hansen mnt_drop_write(nd.path.mnt); 247075c3f29dSDave Hansen out_dput: 24711da177e4SLinus Torvalds dput(new_dentry); 24726902d925SDave Hansen out_unlock: 24734ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 24741da177e4SLinus Torvalds out_release: 24751d957f9bSJan Blunck path_put(&nd.path); 24762ad94ae6SAl Viro putname(to); 24771da177e4SLinus Torvalds out: 24782d8f3038SAl Viro path_put(&old_path); 24791da177e4SLinus Torvalds 24801da177e4SLinus Torvalds return error; 24811da177e4SLinus Torvalds } 24821da177e4SLinus Torvalds 24833480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 24845590ff0dSUlrich Drepper { 2485c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 24865590ff0dSUlrich Drepper } 24875590ff0dSUlrich Drepper 24881da177e4SLinus Torvalds /* 24891da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 24901da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 24911da177e4SLinus Torvalds * Problems: 24921da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 24931da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 24941da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 2495a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 24961da177e4SLinus Torvalds * story. 24971da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 24981b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 24991da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 25001da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 2501a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 25021da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 25031da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 25041da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 2505a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 25061da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 25071da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 25081da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 25091da177e4SLinus Torvalds * d) some filesystems don't support opened-but-unlinked directories, 25101da177e4SLinus Torvalds * either because of layout or because they are not ready to deal with 25111da177e4SLinus Torvalds * all cases correctly. The latter will be fixed (taking this sort of 25121da177e4SLinus Torvalds * stuff into VFS), but the former is not going away. Solution: the same 25131da177e4SLinus Torvalds * trick as in rmdir(). 25141da177e4SLinus Torvalds * e) conversion from fhandle to dentry may come in the wrong moment - when 25151b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 25161da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 25171b1dcc1bSJes Sorensen * ->i_mutex on parents, which works but leads to some truely excessive 25181da177e4SLinus Torvalds * locking]. 25191da177e4SLinus Torvalds */ 252075c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 25211da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 25221da177e4SLinus Torvalds { 25231da177e4SLinus Torvalds int error = 0; 25241da177e4SLinus Torvalds struct inode *target; 25251da177e4SLinus Torvalds 25261da177e4SLinus Torvalds /* 25271da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 25281da177e4SLinus Torvalds * we'll need to flip '..'. 25291da177e4SLinus Torvalds */ 25301da177e4SLinus Torvalds if (new_dir != old_dir) { 2531f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 25321da177e4SLinus Torvalds if (error) 25331da177e4SLinus Torvalds return error; 25341da177e4SLinus Torvalds } 25351da177e4SLinus Torvalds 25361da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 25371da177e4SLinus Torvalds if (error) 25381da177e4SLinus Torvalds return error; 25391da177e4SLinus Torvalds 25401da177e4SLinus Torvalds target = new_dentry->d_inode; 25411da177e4SLinus Torvalds if (target) { 25421b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 25431da177e4SLinus Torvalds dentry_unhash(new_dentry); 25441da177e4SLinus Torvalds } 25451da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 25461da177e4SLinus Torvalds error = -EBUSY; 25471da177e4SLinus Torvalds else 25481da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 25491da177e4SLinus Torvalds if (target) { 25501da177e4SLinus Torvalds if (!error) 25511da177e4SLinus Torvalds target->i_flags |= S_DEAD; 25521b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 25531da177e4SLinus Torvalds if (d_unhashed(new_dentry)) 25541da177e4SLinus Torvalds d_rehash(new_dentry); 25551da177e4SLinus Torvalds dput(new_dentry); 25561da177e4SLinus Torvalds } 2557e31e14ecSStephen Smalley if (!error) 2558349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 25591da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 25601da177e4SLinus Torvalds return error; 25611da177e4SLinus Torvalds } 25621da177e4SLinus Torvalds 256375c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 25641da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 25651da177e4SLinus Torvalds { 25661da177e4SLinus Torvalds struct inode *target; 25671da177e4SLinus Torvalds int error; 25681da177e4SLinus Torvalds 25691da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 25701da177e4SLinus Torvalds if (error) 25711da177e4SLinus Torvalds return error; 25721da177e4SLinus Torvalds 25731da177e4SLinus Torvalds dget(new_dentry); 25741da177e4SLinus Torvalds target = new_dentry->d_inode; 25751da177e4SLinus Torvalds if (target) 25761b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 25771da177e4SLinus Torvalds if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 25781da177e4SLinus Torvalds error = -EBUSY; 25791da177e4SLinus Torvalds else 25801da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 25811da177e4SLinus Torvalds if (!error) { 2582349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 25831da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 25841da177e4SLinus Torvalds } 25851da177e4SLinus Torvalds if (target) 25861b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 25871da177e4SLinus Torvalds dput(new_dentry); 25881da177e4SLinus Torvalds return error; 25891da177e4SLinus Torvalds } 25901da177e4SLinus Torvalds 25911da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 25921da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 25931da177e4SLinus Torvalds { 25941da177e4SLinus Torvalds int error; 25951da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 25960eeca283SRobert Love const char *old_name; 25971da177e4SLinus Torvalds 25981da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 25991da177e4SLinus Torvalds return 0; 26001da177e4SLinus Torvalds 26011da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 26021da177e4SLinus Torvalds if (error) 26031da177e4SLinus Torvalds return error; 26041da177e4SLinus Torvalds 26051da177e4SLinus Torvalds if (!new_dentry->d_inode) 2606a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 26071da177e4SLinus Torvalds else 26081da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 26091da177e4SLinus Torvalds if (error) 26101da177e4SLinus Torvalds return error; 26111da177e4SLinus Torvalds 2612acfa4380SAl Viro if (!old_dir->i_op->rename) 26131da177e4SLinus Torvalds return -EPERM; 26141da177e4SLinus Torvalds 26159e3509e2SJan Kara vfs_dq_init(old_dir); 26169e3509e2SJan Kara vfs_dq_init(new_dir); 26171da177e4SLinus Torvalds 26180eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 26190eeca283SRobert Love 26201da177e4SLinus Torvalds if (is_dir) 26211da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 26221da177e4SLinus Torvalds else 26231da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 26241da177e4SLinus Torvalds if (!error) { 26250eeca283SRobert Love const char *new_name = old_dentry->d_name.name; 262689204c40SJohn McCutchan fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir, 26275a190ae6SAl Viro new_dentry->d_inode, old_dentry); 26281da177e4SLinus Torvalds } 26290eeca283SRobert Love fsnotify_oldname_free(old_name); 26300eeca283SRobert Love 26311da177e4SLinus Torvalds return error; 26321da177e4SLinus Torvalds } 26331da177e4SLinus Torvalds 26342e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 26352e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 26361da177e4SLinus Torvalds { 26371da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 26381da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 26391da177e4SLinus Torvalds struct dentry *trap; 26401da177e4SLinus Torvalds struct nameidata oldnd, newnd; 26412ad94ae6SAl Viro char *from; 26422ad94ae6SAl Viro char *to; 26432ad94ae6SAl Viro int error; 26441da177e4SLinus Torvalds 26452ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 26461da177e4SLinus Torvalds if (error) 26471da177e4SLinus Torvalds goto exit; 26481da177e4SLinus Torvalds 26492ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 26501da177e4SLinus Torvalds if (error) 26511da177e4SLinus Torvalds goto exit1; 26521da177e4SLinus Torvalds 26531da177e4SLinus Torvalds error = -EXDEV; 26544ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 26551da177e4SLinus Torvalds goto exit2; 26561da177e4SLinus Torvalds 26574ac91378SJan Blunck old_dir = oldnd.path.dentry; 26581da177e4SLinus Torvalds error = -EBUSY; 26591da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 26601da177e4SLinus Torvalds goto exit2; 26611da177e4SLinus Torvalds 26624ac91378SJan Blunck new_dir = newnd.path.dentry; 26631da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 26641da177e4SLinus Torvalds goto exit2; 26651da177e4SLinus Torvalds 26660612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 26670612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 26684e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 26690612d9fbSOGAWA Hirofumi 26701da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 26711da177e4SLinus Torvalds 267249705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 26731da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 26741da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 26751da177e4SLinus Torvalds goto exit3; 26761da177e4SLinus Torvalds /* source must exist */ 26771da177e4SLinus Torvalds error = -ENOENT; 26781da177e4SLinus Torvalds if (!old_dentry->d_inode) 26791da177e4SLinus Torvalds goto exit4; 26801da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 26811da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 26821da177e4SLinus Torvalds error = -ENOTDIR; 26831da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 26841da177e4SLinus Torvalds goto exit4; 26851da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 26861da177e4SLinus Torvalds goto exit4; 26871da177e4SLinus Torvalds } 26881da177e4SLinus Torvalds /* source should not be ancestor of target */ 26891da177e4SLinus Torvalds error = -EINVAL; 26901da177e4SLinus Torvalds if (old_dentry == trap) 26911da177e4SLinus Torvalds goto exit4; 269249705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 26931da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 26941da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 26951da177e4SLinus Torvalds goto exit4; 26961da177e4SLinus Torvalds /* target should not be an ancestor of source */ 26971da177e4SLinus Torvalds error = -ENOTEMPTY; 26981da177e4SLinus Torvalds if (new_dentry == trap) 26991da177e4SLinus Torvalds goto exit5; 27001da177e4SLinus Torvalds 27019079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 27029079b1ebSDave Hansen if (error) 27039079b1ebSDave Hansen goto exit5; 2704be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 2705be6d3e56SKentaro Takeda &newnd.path, new_dentry); 2706be6d3e56SKentaro Takeda if (error) 2707be6d3e56SKentaro Takeda goto exit6; 27081da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 27091da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 2710be6d3e56SKentaro Takeda exit6: 27119079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 27121da177e4SLinus Torvalds exit5: 27131da177e4SLinus Torvalds dput(new_dentry); 27141da177e4SLinus Torvalds exit4: 27151da177e4SLinus Torvalds dput(old_dentry); 27161da177e4SLinus Torvalds exit3: 27171da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 27181da177e4SLinus Torvalds exit2: 27191d957f9bSJan Blunck path_put(&newnd.path); 27202ad94ae6SAl Viro putname(to); 27211da177e4SLinus Torvalds exit1: 27221d957f9bSJan Blunck path_put(&oldnd.path); 27231da177e4SLinus Torvalds putname(from); 27242ad94ae6SAl Viro exit: 27251da177e4SLinus Torvalds return error; 27261da177e4SLinus Torvalds } 27271da177e4SLinus Torvalds 2728a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 27295590ff0dSUlrich Drepper { 27305590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 27315590ff0dSUlrich Drepper } 27325590ff0dSUlrich Drepper 27331da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 27341da177e4SLinus Torvalds { 27351da177e4SLinus Torvalds int len; 27361da177e4SLinus Torvalds 27371da177e4SLinus Torvalds len = PTR_ERR(link); 27381da177e4SLinus Torvalds if (IS_ERR(link)) 27391da177e4SLinus Torvalds goto out; 27401da177e4SLinus Torvalds 27411da177e4SLinus Torvalds len = strlen(link); 27421da177e4SLinus Torvalds if (len > (unsigned) buflen) 27431da177e4SLinus Torvalds len = buflen; 27441da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 27451da177e4SLinus Torvalds len = -EFAULT; 27461da177e4SLinus Torvalds out: 27471da177e4SLinus Torvalds return len; 27481da177e4SLinus Torvalds } 27491da177e4SLinus Torvalds 27501da177e4SLinus Torvalds /* 27511da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 27521da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 27531da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 27541da177e4SLinus Torvalds */ 27551da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 27561da177e4SLinus Torvalds { 27571da177e4SLinus Torvalds struct nameidata nd; 2758cc314eefSLinus Torvalds void *cookie; 2759694a1764SMarcin Slusarz int res; 2760cc314eefSLinus Torvalds 27611da177e4SLinus Torvalds nd.depth = 0; 2762cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 2763694a1764SMarcin Slusarz if (IS_ERR(cookie)) 2764694a1764SMarcin Slusarz return PTR_ERR(cookie); 2765694a1764SMarcin Slusarz 2766694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 27671da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 2768cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 2769694a1764SMarcin Slusarz return res; 27701da177e4SLinus Torvalds } 27711da177e4SLinus Torvalds 27721da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 27731da177e4SLinus Torvalds { 27741da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 27751da177e4SLinus Torvalds } 27761da177e4SLinus Torvalds 27771da177e4SLinus Torvalds /* get the link contents into pagecache */ 27781da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 27791da177e4SLinus Torvalds { 2780ebd09abbSDuane Griffin char *kaddr; 27811da177e4SLinus Torvalds struct page *page; 27821da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 2783090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 27841da177e4SLinus Torvalds if (IS_ERR(page)) 27856fe6900eSNick Piggin return (char*)page; 27861da177e4SLinus Torvalds *ppage = page; 2787ebd09abbSDuane Griffin kaddr = kmap(page); 2788ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 2789ebd09abbSDuane Griffin return kaddr; 27901da177e4SLinus Torvalds } 27911da177e4SLinus Torvalds 27921da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 27931da177e4SLinus Torvalds { 27941da177e4SLinus Torvalds struct page *page = NULL; 27951da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 27961da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 27971da177e4SLinus Torvalds if (page) { 27981da177e4SLinus Torvalds kunmap(page); 27991da177e4SLinus Torvalds page_cache_release(page); 28001da177e4SLinus Torvalds } 28011da177e4SLinus Torvalds return res; 28021da177e4SLinus Torvalds } 28031da177e4SLinus Torvalds 2804cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 28051da177e4SLinus Torvalds { 2806cc314eefSLinus Torvalds struct page *page = NULL; 28071da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 2808cc314eefSLinus Torvalds return page; 28091da177e4SLinus Torvalds } 28101da177e4SLinus Torvalds 2811cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 28121da177e4SLinus Torvalds { 2813cc314eefSLinus Torvalds struct page *page = cookie; 2814cc314eefSLinus Torvalds 2815cc314eefSLinus Torvalds if (page) { 28161da177e4SLinus Torvalds kunmap(page); 28171da177e4SLinus Torvalds page_cache_release(page); 28181da177e4SLinus Torvalds } 28191da177e4SLinus Torvalds } 28201da177e4SLinus Torvalds 282154566b2cSNick Piggin /* 282254566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 282354566b2cSNick Piggin */ 282454566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 28251da177e4SLinus Torvalds { 28261da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 28270adb25d2SKirill Korotaev struct page *page; 2828afddba49SNick Piggin void *fsdata; 2829beb497abSDmitriy Monakhov int err; 28301da177e4SLinus Torvalds char *kaddr; 283154566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 283254566b2cSNick Piggin if (nofs) 283354566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 28341da177e4SLinus Torvalds 28357e53cac4SNeilBrown retry: 2836afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 283754566b2cSNick Piggin flags, &page, &fsdata); 28381da177e4SLinus Torvalds if (err) 2839afddba49SNick Piggin goto fail; 2840afddba49SNick Piggin 28411da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 28421da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 28431da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 2844afddba49SNick Piggin 2845afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 2846afddba49SNick Piggin page, fsdata); 28471da177e4SLinus Torvalds if (err < 0) 28481da177e4SLinus Torvalds goto fail; 2849afddba49SNick Piggin if (err < len-1) 2850afddba49SNick Piggin goto retry; 2851afddba49SNick Piggin 28521da177e4SLinus Torvalds mark_inode_dirty(inode); 28531da177e4SLinus Torvalds return 0; 28541da177e4SLinus Torvalds fail: 28551da177e4SLinus Torvalds return err; 28561da177e4SLinus Torvalds } 28571da177e4SLinus Torvalds 28580adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 28590adb25d2SKirill Korotaev { 28600adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 286154566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 28620adb25d2SKirill Korotaev } 28630adb25d2SKirill Korotaev 286492e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 28651da177e4SLinus Torvalds .readlink = generic_readlink, 28661da177e4SLinus Torvalds .follow_link = page_follow_link_light, 28671da177e4SLinus Torvalds .put_link = page_put_link, 28681da177e4SLinus Torvalds }; 28691da177e4SLinus Torvalds 28702d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 28711da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 28721da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 28731da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 28741da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 28751da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 28761da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 28771da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 28781da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 28791da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 28800adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 28811da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 28821da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 28831da177e4SLinus Torvalds EXPORT_SYMBOL(path_lookup); 2884d1811465SAl Viro EXPORT_SYMBOL(kern_path); 288516f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 2886f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 28878c744fb8SChristoph Hellwig EXPORT_SYMBOL(file_permission); 28881da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 28891da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 28901da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 28911da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 28921da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 28931da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 28941da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 28951da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 28961da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 28971da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 28981da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 28991da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 29001da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 29011da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 2902