11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/namei.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* 81da177e4SLinus Torvalds * Some corrections by tytso. 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname 121da177e4SLinus Torvalds * lookup logic. 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture. 151da177e4SLinus Torvalds */ 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #include <linux/init.h> 181da177e4SLinus Torvalds #include <linux/module.h> 191da177e4SLinus Torvalds #include <linux/slab.h> 201da177e4SLinus Torvalds #include <linux/fs.h> 211da177e4SLinus Torvalds #include <linux/namei.h> 221da177e4SLinus Torvalds #include <linux/pagemap.h> 230eeca283SRobert Love #include <linux/fsnotify.h> 241da177e4SLinus Torvalds #include <linux/personality.h> 251da177e4SLinus Torvalds #include <linux/security.h> 266146f0d5SMimi Zohar #include <linux/ima.h> 271da177e4SLinus Torvalds #include <linux/syscalls.h> 281da177e4SLinus Torvalds #include <linux/mount.h> 291da177e4SLinus Torvalds #include <linux/audit.h> 3016f7e0feSRandy Dunlap #include <linux/capability.h> 31834f2a4aSTrond Myklebust #include <linux/file.h> 325590ff0dSUlrich Drepper #include <linux/fcntl.h> 3308ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h> 345ad4e53bSAl Viro #include <linux/fs_struct.h> 35e77819e5SLinus Torvalds #include <linux/posix_acl.h> 361da177e4SLinus Torvalds #include <asm/uaccess.h> 371da177e4SLinus Torvalds 38e81e3f4dSEric Paris #include "internal.h" 39e81e3f4dSEric Paris 401da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer] 411da177e4SLinus Torvalds * Fundamental changes in the pathname lookup mechanisms (namei) 421da177e4SLinus Torvalds * were necessary because of omirr. The reason is that omirr needs 431da177e4SLinus Torvalds * to know the _real_ pathname, not the user-supplied one, in case 441da177e4SLinus Torvalds * of symlinks (and also when transname replacements occur). 451da177e4SLinus Torvalds * 461da177e4SLinus Torvalds * The new code replaces the old recursive symlink resolution with 471da177e4SLinus Torvalds * an iterative one (in case of non-nested symlink chains). It does 481da177e4SLinus Torvalds * this with calls to <fs>_follow_link(). 491da177e4SLinus Torvalds * As a side effect, dir_namei(), _namei() and follow_link() are now 501da177e4SLinus Torvalds * replaced with a single function lookup_dentry() that can handle all 511da177e4SLinus Torvalds * the special cases of the former code. 521da177e4SLinus Torvalds * 531da177e4SLinus Torvalds * With the new dcache, the pathname is stored at each inode, at least as 541da177e4SLinus Torvalds * long as the refcount of the inode is positive. As a side effect, the 551da177e4SLinus Torvalds * size of the dcache depends on the inode cache and thus is dynamic. 561da177e4SLinus Torvalds * 571da177e4SLinus Torvalds * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink 581da177e4SLinus Torvalds * resolution to correspond with current state of the code. 591da177e4SLinus Torvalds * 601da177e4SLinus Torvalds * Note that the symlink resolution is not *completely* iterative. 611da177e4SLinus Torvalds * There is still a significant amount of tail- and mid- recursion in 621da177e4SLinus Torvalds * the algorithm. Also, note that <fs>_readlink() is not used in 631da177e4SLinus Torvalds * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() 641da177e4SLinus Torvalds * may return different results than <fs>_follow_link(). Many virtual 651da177e4SLinus Torvalds * filesystems (including /proc) exhibit this behavior. 661da177e4SLinus Torvalds */ 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: 691da177e4SLinus Torvalds * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL 701da177e4SLinus Torvalds * and the name already exists in form of a symlink, try to create the new 711da177e4SLinus Torvalds * name indicated by the symlink. The old code always complained that the 721da177e4SLinus Torvalds * name already exists, due to not following the symlink even if its target 731da177e4SLinus Torvalds * is nonexistent. The new semantics affects also mknod() and link() when 7425985edcSLucas De Marchi * the name is a symlink pointing to a non-existent name. 751da177e4SLinus Torvalds * 761da177e4SLinus Torvalds * I don't know which semantics is the right one, since I have no access 771da177e4SLinus Torvalds * to standards. But I found by trial that HP-UX 9.0 has the full "new" 781da177e4SLinus Torvalds * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the 791da177e4SLinus Torvalds * "old" one. Personally, I think the new semantics is much more logical. 801da177e4SLinus Torvalds * Note that "ln old new" where "new" is a symlink pointing to a non-existing 811da177e4SLinus Torvalds * file does succeed in both HP-UX and SunOs, but not in Solaris 821da177e4SLinus Torvalds * and in the old Linux semantics. 831da177e4SLinus Torvalds */ 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink 861da177e4SLinus Torvalds * semantics. See the comments in "open_namei" and "do_link" below. 871da177e4SLinus Torvalds * 881da177e4SLinus Torvalds * [10-Sep-98 Alan Modra] Another symlink change. 891da177e4SLinus Torvalds */ 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: 921da177e4SLinus Torvalds * inside the path - always follow. 931da177e4SLinus Torvalds * in the last component in creation/removal/renaming - never follow. 941da177e4SLinus Torvalds * if LOOKUP_FOLLOW passed - follow. 951da177e4SLinus Torvalds * if the pathname has trailing slashes - follow. 961da177e4SLinus Torvalds * otherwise - don't follow. 971da177e4SLinus Torvalds * (applied in that order). 981da177e4SLinus Torvalds * 991da177e4SLinus Torvalds * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT 1001da177e4SLinus Torvalds * restored for 2.4. This is the last surviving part of old 4.2BSD bug. 1011da177e4SLinus Torvalds * During the 2.4 we need to fix the userland stuff depending on it - 1021da177e4SLinus Torvalds * hopefully we will be able to get rid of that wart in 2.5. So far only 1031da177e4SLinus Torvalds * XEmacs seems to be relying on it... 1041da177e4SLinus Torvalds */ 1051da177e4SLinus Torvalds /* 1061da177e4SLinus Torvalds * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) 107a11f3a05SArjan van de Ven * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives 1081da177e4SLinus Torvalds * any extra contention... 1091da177e4SLinus Torvalds */ 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional 1121da177e4SLinus Torvalds * checking and hopefully speeding things up, we copy filenames to the 1131da177e4SLinus Torvalds * kernel data space before using them.. 1141da177e4SLinus Torvalds * 1151da177e4SLinus Torvalds * POSIX.1 2.4: an empty pathname is invalid (ENOENT). 1161da177e4SLinus Torvalds * PATH_MAX includes the nul terminator --RR. 1171da177e4SLinus Torvalds */ 118858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page) 1191da177e4SLinus Torvalds { 1201da177e4SLinus Torvalds int retval; 1211da177e4SLinus Torvalds unsigned long len = PATH_MAX; 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds if (!segment_eq(get_fs(), KERNEL_DS)) { 1241da177e4SLinus Torvalds if ((unsigned long) filename >= TASK_SIZE) 1251da177e4SLinus Torvalds return -EFAULT; 1261da177e4SLinus Torvalds if (TASK_SIZE - (unsigned long) filename < PATH_MAX) 1271da177e4SLinus Torvalds len = TASK_SIZE - (unsigned long) filename; 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds retval = strncpy_from_user(page, filename, len); 1311da177e4SLinus Torvalds if (retval > 0) { 1321da177e4SLinus Torvalds if (retval < len) 1331da177e4SLinus Torvalds return 0; 1341da177e4SLinus Torvalds return -ENAMETOOLONG; 1351da177e4SLinus Torvalds } else if (!retval) 1361da177e4SLinus Torvalds retval = -ENOENT; 1371da177e4SLinus Torvalds return retval; 1381da177e4SLinus Torvalds } 1391da177e4SLinus Torvalds 140f52e0c11SAl Viro static char *getname_flags(const char __user * filename, int flags) 1411da177e4SLinus Torvalds { 1421da177e4SLinus Torvalds char *tmp, *result; 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds result = ERR_PTR(-ENOMEM); 1451da177e4SLinus Torvalds tmp = __getname(); 1461da177e4SLinus Torvalds if (tmp) { 1471da177e4SLinus Torvalds int retval = do_getname(filename, tmp); 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds result = tmp; 1501da177e4SLinus Torvalds if (retval < 0) { 151f52e0c11SAl Viro if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) { 1521da177e4SLinus Torvalds __putname(tmp); 1531da177e4SLinus Torvalds result = ERR_PTR(retval); 1541da177e4SLinus Torvalds } 1551da177e4SLinus Torvalds } 156f52e0c11SAl Viro } 1571da177e4SLinus Torvalds audit_getname(result); 1581da177e4SLinus Torvalds return result; 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 161f52e0c11SAl Viro char *getname(const char __user * filename) 162f52e0c11SAl Viro { 163f52e0c11SAl Viro return getname_flags(filename, 0); 164f52e0c11SAl Viro } 165f52e0c11SAl Viro 1661da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 1671da177e4SLinus Torvalds void putname(const char *name) 1681da177e4SLinus Torvalds { 1695ac3a9c2SAl Viro if (unlikely(!audit_dummy_context())) 1701da177e4SLinus Torvalds audit_putname(name); 1711da177e4SLinus Torvalds else 1721da177e4SLinus Torvalds __putname(name); 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds EXPORT_SYMBOL(putname); 1751da177e4SLinus Torvalds #endif 1761da177e4SLinus Torvalds 177e77819e5SLinus Torvalds static int check_acl(struct inode *inode, int mask) 178e77819e5SLinus Torvalds { 17984635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL 180e77819e5SLinus Torvalds struct posix_acl *acl; 181e77819e5SLinus Torvalds 182e77819e5SLinus Torvalds /* 183e77819e5SLinus Torvalds * Under RCU walk, we cannot even do a "get_cached_acl()", 184e77819e5SLinus Torvalds * because that involves locking and getting a refcount on 185e77819e5SLinus Torvalds * a cached ACL. 186e77819e5SLinus Torvalds * 187e77819e5SLinus Torvalds * So the only case we handle during RCU walking is the 188e77819e5SLinus Torvalds * case of a cached "no ACL at all", which needs no locks 189e77819e5SLinus Torvalds * or refcounts. 190e77819e5SLinus Torvalds */ 191e77819e5SLinus Torvalds if (mask & MAY_NOT_BLOCK) { 192e77819e5SLinus Torvalds if (negative_cached_acl(inode, ACL_TYPE_ACCESS)) 193e77819e5SLinus Torvalds return -EAGAIN; 194e77819e5SLinus Torvalds return -ECHILD; 195e77819e5SLinus Torvalds } 196e77819e5SLinus Torvalds 197e77819e5SLinus Torvalds acl = get_cached_acl(inode, ACL_TYPE_ACCESS); 198e77819e5SLinus Torvalds 199e77819e5SLinus Torvalds /* 2004e34e719SChristoph Hellwig * A filesystem can force a ACL callback by just never filling the 2014e34e719SChristoph Hellwig * ACL cache. But normally you'd fill the cache either at inode 2024e34e719SChristoph Hellwig * instantiation time, or on the first ->get_acl call. 203e77819e5SLinus Torvalds * 2044e34e719SChristoph Hellwig * If the filesystem doesn't have a get_acl() function at all, we'll 2054e34e719SChristoph Hellwig * just create the negative cache entry. 206e77819e5SLinus Torvalds */ 207e77819e5SLinus Torvalds if (acl == ACL_NOT_CACHED) { 2084e34e719SChristoph Hellwig if (inode->i_op->get_acl) { 2094e34e719SChristoph Hellwig acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS); 2104e34e719SChristoph Hellwig if (IS_ERR(acl)) 2114e34e719SChristoph Hellwig return PTR_ERR(acl); 2124e34e719SChristoph Hellwig } else { 213e77819e5SLinus Torvalds set_cached_acl(inode, ACL_TYPE_ACCESS, NULL); 214e77819e5SLinus Torvalds return -EAGAIN; 215e77819e5SLinus Torvalds } 2164e34e719SChristoph Hellwig } 217e77819e5SLinus Torvalds 218e77819e5SLinus Torvalds if (acl) { 219e77819e5SLinus Torvalds int error = posix_acl_permission(inode, acl, mask); 220e77819e5SLinus Torvalds posix_acl_release(acl); 221e77819e5SLinus Torvalds return error; 222e77819e5SLinus Torvalds } 22384635d68SLinus Torvalds #endif 224e77819e5SLinus Torvalds 225e77819e5SLinus Torvalds return -EAGAIN; 226e77819e5SLinus Torvalds } 227e77819e5SLinus Torvalds 2285909ccaaSLinus Torvalds /* 2295909ccaaSLinus Torvalds * This does basic POSIX ACL permission checking 2305909ccaaSLinus Torvalds */ 2317e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask) 2325909ccaaSLinus Torvalds { 23326cf46beSLinus Torvalds unsigned int mode = inode->i_mode; 2345909ccaaSLinus Torvalds 2359c2c7039SAl Viro mask &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK; 2365909ccaaSLinus Torvalds 237e795b717SSerge E. Hallyn if (current_user_ns() != inode_userns(inode)) 238e795b717SSerge E. Hallyn goto other_perms; 239e795b717SSerge E. Hallyn 24014067ff5SLinus Torvalds if (likely(current_fsuid() == inode->i_uid)) 2415909ccaaSLinus Torvalds mode >>= 6; 2425909ccaaSLinus Torvalds else { 243e77819e5SLinus Torvalds if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { 2447e40145eSAl Viro int error = check_acl(inode, mask); 2455909ccaaSLinus Torvalds if (error != -EAGAIN) 2465909ccaaSLinus Torvalds return error; 2475909ccaaSLinus Torvalds } 2485909ccaaSLinus Torvalds 2495909ccaaSLinus Torvalds if (in_group_p(inode->i_gid)) 2505909ccaaSLinus Torvalds mode >>= 3; 2515909ccaaSLinus Torvalds } 2525909ccaaSLinus Torvalds 253e795b717SSerge E. Hallyn other_perms: 2545909ccaaSLinus Torvalds /* 2555909ccaaSLinus Torvalds * If the DACs are ok we don't need any capability check. 2565909ccaaSLinus Torvalds */ 2579c2c7039SAl Viro if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 2585909ccaaSLinus Torvalds return 0; 2595909ccaaSLinus Torvalds return -EACCES; 2605909ccaaSLinus Torvalds } 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds /** 2631da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 2641da177e4SLinus Torvalds * @inode: inode to check access rights for 2651da177e4SLinus Torvalds * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 2661da177e4SLinus Torvalds * 2671da177e4SLinus Torvalds * Used to check for read/write/execute permissions on a file. 2681da177e4SLinus Torvalds * We use "fsuid" for this, letting us set arbitrary permissions 2691da177e4SLinus Torvalds * for filesystem access without changing the "normal" uids which 270b74c79e9SNick Piggin * are used for other things. 271b74c79e9SNick Piggin * 272b74c79e9SNick Piggin * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk 273b74c79e9SNick Piggin * request cannot be satisfied (eg. requires blocking or too much complexity). 274b74c79e9SNick Piggin * It would then be called again in ref-walk mode. 2751da177e4SLinus Torvalds */ 2762830ba7fSAl Viro int generic_permission(struct inode *inode, int mask) 2771da177e4SLinus Torvalds { 2785909ccaaSLinus Torvalds int ret; 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds /* 2815909ccaaSLinus Torvalds * Do the basic POSIX ACL permission checks. 2821da177e4SLinus Torvalds */ 2837e40145eSAl Viro ret = acl_permission_check(inode, mask); 2845909ccaaSLinus Torvalds if (ret != -EACCES) 2855909ccaaSLinus Torvalds return ret; 2861da177e4SLinus Torvalds 287d594e7ecSAl Viro if (S_ISDIR(inode->i_mode)) { 288d594e7ecSAl Viro /* DACs are overridable for directories */ 289d594e7ecSAl Viro if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE)) 290d594e7ecSAl Viro return 0; 291d594e7ecSAl Viro if (!(mask & MAY_WRITE)) 292d594e7ecSAl Viro if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH)) 293d594e7ecSAl Viro return 0; 294d594e7ecSAl Viro return -EACCES; 295d594e7ecSAl Viro } 2961da177e4SLinus Torvalds /* 2971da177e4SLinus Torvalds * Read/write DACs are always overridable. 298d594e7ecSAl Viro * Executable DACs are overridable when there is 299d594e7ecSAl Viro * at least one exec bit set. 3001da177e4SLinus Torvalds */ 301d594e7ecSAl Viro if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) 302e795b717SSerge E. Hallyn if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE)) 3031da177e4SLinus Torvalds return 0; 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds /* 3061da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 3071da177e4SLinus Torvalds */ 3087ea66001SSerge E. Hallyn mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 309d594e7ecSAl Viro if (mask == MAY_READ) 310e795b717SSerge E. Hallyn if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH)) 3111da177e4SLinus Torvalds return 0; 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds return -EACCES; 3141da177e4SLinus Torvalds } 3151da177e4SLinus Torvalds 316cb23beb5SChristoph Hellwig /** 317cb23beb5SChristoph Hellwig * inode_permission - check for access rights to a given inode 318cb23beb5SChristoph Hellwig * @inode: inode to check permission on 319cb23beb5SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 320cb23beb5SChristoph Hellwig * 321cb23beb5SChristoph Hellwig * Used to check for read/write/execute permissions on an inode. 322cb23beb5SChristoph Hellwig * We use "fsuid" for this, letting us set arbitrary permissions 323cb23beb5SChristoph Hellwig * for filesystem access without changing the "normal" uids which 324cb23beb5SChristoph Hellwig * are used for other things. 325cb23beb5SChristoph Hellwig */ 326f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask) 3271da177e4SLinus Torvalds { 328e6305c43SAl Viro int retval; 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds if (mask & MAY_WRITE) { 33122590e41SMiklos Szeredi umode_t mode = inode->i_mode; 3321da177e4SLinus Torvalds 3331da177e4SLinus Torvalds /* 3341da177e4SLinus Torvalds * Nobody gets write access to a read-only fs. 3351da177e4SLinus Torvalds */ 3361da177e4SLinus Torvalds if (IS_RDONLY(inode) && 3371da177e4SLinus Torvalds (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 3381da177e4SLinus Torvalds return -EROFS; 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds /* 3411da177e4SLinus Torvalds * Nobody gets write access to an immutable file. 3421da177e4SLinus Torvalds */ 3431da177e4SLinus Torvalds if (IS_IMMUTABLE(inode)) 3441da177e4SLinus Torvalds return -EACCES; 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds 347acfa4380SAl Viro if (inode->i_op->permission) 34810556cb2SAl Viro retval = inode->i_op->permission(inode, mask); 349f696a365SMiklos Szeredi else 3502830ba7fSAl Viro retval = generic_permission(inode, mask); 351f696a365SMiklos Szeredi 3521da177e4SLinus Torvalds if (retval) 3531da177e4SLinus Torvalds return retval; 3541da177e4SLinus Torvalds 35508ce5f16SSerge E. Hallyn retval = devcgroup_inode_permission(inode, mask); 35608ce5f16SSerge E. Hallyn if (retval) 35708ce5f16SSerge E. Hallyn return retval; 35808ce5f16SSerge E. Hallyn 359d09ca739SEric Paris return security_inode_permission(inode, mask); 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds 362f4d6ff89SAl Viro /** 3635dd784d0SJan Blunck * path_get - get a reference to a path 3645dd784d0SJan Blunck * @path: path to get the reference to 3655dd784d0SJan Blunck * 3665dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 3675dd784d0SJan Blunck */ 3685dd784d0SJan Blunck void path_get(struct path *path) 3695dd784d0SJan Blunck { 3705dd784d0SJan Blunck mntget(path->mnt); 3715dd784d0SJan Blunck dget(path->dentry); 3725dd784d0SJan Blunck } 3735dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 3745dd784d0SJan Blunck 3755dd784d0SJan Blunck /** 3761d957f9bSJan Blunck * path_put - put a reference to a path 3771d957f9bSJan Blunck * @path: path to put the reference to 3781d957f9bSJan Blunck * 3791d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 3801d957f9bSJan Blunck */ 3811d957f9bSJan Blunck void path_put(struct path *path) 3821da177e4SLinus Torvalds { 3831d957f9bSJan Blunck dput(path->dentry); 3841d957f9bSJan Blunck mntput(path->mnt); 3851da177e4SLinus Torvalds } 3861d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 3871da177e4SLinus Torvalds 38819660af7SAl Viro /* 38931e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 39019660af7SAl Viro * Documentation/filesystems/path-lookup.txt). In situations when we can't 39119660af7SAl Viro * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab 39219660af7SAl Viro * normal reference counts on dentries and vfsmounts to transition to rcu-walk 39319660af7SAl Viro * mode. Refcounts are grabbed at the last known good point before rcu-walk 39419660af7SAl Viro * got stuck, so ref-walk may continue from there. If this is not successful 39519660af7SAl Viro * (eg. a seqcount has changed), then failure is returned and it's up to caller 39619660af7SAl Viro * to restart the path walk from the beginning in ref-walk mode. 39731e6b01fSNick Piggin */ 39831e6b01fSNick Piggin 39931e6b01fSNick Piggin /** 40019660af7SAl Viro * unlazy_walk - try to switch to ref-walk mode. 40119660af7SAl Viro * @nd: nameidata pathwalk data 40219660af7SAl Viro * @dentry: child of nd->path.dentry or NULL 40339191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 40431e6b01fSNick Piggin * 40519660af7SAl Viro * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry 40619660af7SAl Viro * for ref-walk mode. @dentry must be a path found by a do_lookup call on 40719660af7SAl Viro * @nd or NULL. Must be called from rcu-walk context. 40831e6b01fSNick Piggin */ 40919660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry) 41031e6b01fSNick Piggin { 41131e6b01fSNick Piggin struct fs_struct *fs = current->fs; 41231e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 4135b6ca027SAl Viro int want_root = 0; 41431e6b01fSNick Piggin 41531e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 4165b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 4175b6ca027SAl Viro want_root = 1; 41831e6b01fSNick Piggin spin_lock(&fs->lock); 41931e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 42031e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 42131e6b01fSNick Piggin goto err_root; 42231e6b01fSNick Piggin } 42331e6b01fSNick Piggin spin_lock(&parent->d_lock); 42419660af7SAl Viro if (!dentry) { 42519660af7SAl Viro if (!__d_rcu_to_refcount(parent, nd->seq)) 42619660af7SAl Viro goto err_parent; 42719660af7SAl Viro BUG_ON(nd->inode != parent->d_inode); 42819660af7SAl Viro } else { 42994c0d4ecSAl Viro if (dentry->d_parent != parent) 43094c0d4ecSAl Viro goto err_parent; 43131e6b01fSNick Piggin spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); 43231e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 43319660af7SAl Viro goto err_child; 43431e6b01fSNick Piggin /* 43519660af7SAl Viro * If the sequence check on the child dentry passed, then 43619660af7SAl Viro * the child has not been removed from its parent. This 43719660af7SAl Viro * means the parent dentry must be valid and able to take 43819660af7SAl Viro * a reference at this point. 43931e6b01fSNick Piggin */ 44031e6b01fSNick Piggin BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); 44131e6b01fSNick Piggin BUG_ON(!parent->d_count); 44231e6b01fSNick Piggin parent->d_count++; 44331e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 44419660af7SAl Viro } 44531e6b01fSNick Piggin spin_unlock(&parent->d_lock); 4465b6ca027SAl Viro if (want_root) { 44731e6b01fSNick Piggin path_get(&nd->root); 44831e6b01fSNick Piggin spin_unlock(&fs->lock); 44931e6b01fSNick Piggin } 45031e6b01fSNick Piggin mntget(nd->path.mnt); 45131e6b01fSNick Piggin 45231e6b01fSNick Piggin rcu_read_unlock(); 45331e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 45431e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 45531e6b01fSNick Piggin return 0; 45619660af7SAl Viro 45719660af7SAl Viro err_child: 45831e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 45919660af7SAl Viro err_parent: 46031e6b01fSNick Piggin spin_unlock(&parent->d_lock); 46131e6b01fSNick Piggin err_root: 4625b6ca027SAl Viro if (want_root) 46331e6b01fSNick Piggin spin_unlock(&fs->lock); 46431e6b01fSNick Piggin return -ECHILD; 46531e6b01fSNick Piggin } 46631e6b01fSNick Piggin 46731e6b01fSNick Piggin /** 468834f2a4aSTrond Myklebust * release_open_intent - free up open intent resources 469834f2a4aSTrond Myklebust * @nd: pointer to nameidata 470834f2a4aSTrond Myklebust */ 471834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd) 472834f2a4aSTrond Myklebust { 4732dab5974SLinus Torvalds struct file *file = nd->intent.open.file; 4742dab5974SLinus Torvalds 4752dab5974SLinus Torvalds if (file && !IS_ERR(file)) { 4762dab5974SLinus Torvalds if (file->f_path.dentry == NULL) 4772dab5974SLinus Torvalds put_filp(file); 478834f2a4aSTrond Myklebust else 4792dab5974SLinus Torvalds fput(file); 4802dab5974SLinus Torvalds } 481834f2a4aSTrond Myklebust } 482834f2a4aSTrond Myklebust 483f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd) 48434286d66SNick Piggin { 485f60aef7eSAl Viro return dentry->d_op->d_revalidate(dentry, nd); 48634286d66SNick Piggin } 48734286d66SNick Piggin 4889f1fafeeSAl Viro /** 4899f1fafeeSAl Viro * complete_walk - successful completion of path walk 4909f1fafeeSAl Viro * @nd: pointer nameidata 49139159de2SJeff Layton * 4929f1fafeeSAl Viro * If we had been in RCU mode, drop out of it and legitimize nd->path. 4939f1fafeeSAl Viro * Revalidate the final result, unless we'd already done that during 4949f1fafeeSAl Viro * the path walk or the filesystem doesn't ask for it. Return 0 on 4959f1fafeeSAl Viro * success, -error on failure. In case of failure caller does not 4969f1fafeeSAl Viro * need to drop nd->path. 49739159de2SJeff Layton */ 4989f1fafeeSAl Viro static int complete_walk(struct nameidata *nd) 49939159de2SJeff Layton { 50016c2cd71SAl Viro struct dentry *dentry = nd->path.dentry; 50139159de2SJeff Layton int status; 50239159de2SJeff Layton 5039f1fafeeSAl Viro if (nd->flags & LOOKUP_RCU) { 5049f1fafeeSAl Viro nd->flags &= ~LOOKUP_RCU; 5059f1fafeeSAl Viro if (!(nd->flags & LOOKUP_ROOT)) 5069f1fafeeSAl Viro nd->root.mnt = NULL; 5079f1fafeeSAl Viro spin_lock(&dentry->d_lock); 5089f1fafeeSAl Viro if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) { 5099f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 5109f1fafeeSAl Viro rcu_read_unlock(); 5119f1fafeeSAl Viro br_read_unlock(vfsmount_lock); 5129f1fafeeSAl Viro return -ECHILD; 5139f1fafeeSAl Viro } 5149f1fafeeSAl Viro BUG_ON(nd->inode != dentry->d_inode); 5159f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 5169f1fafeeSAl Viro mntget(nd->path.mnt); 5179f1fafeeSAl Viro rcu_read_unlock(); 5189f1fafeeSAl Viro br_read_unlock(vfsmount_lock); 5199f1fafeeSAl Viro } 5209f1fafeeSAl Viro 52116c2cd71SAl Viro if (likely(!(nd->flags & LOOKUP_JUMPED))) 52239159de2SJeff Layton return 0; 52339159de2SJeff Layton 52416c2cd71SAl Viro if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE))) 52516c2cd71SAl Viro return 0; 52616c2cd71SAl Viro 52716c2cd71SAl Viro if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))) 52816c2cd71SAl Viro return 0; 52916c2cd71SAl Viro 53016c2cd71SAl Viro /* Note: we do not d_invalidate() */ 53134286d66SNick Piggin status = d_revalidate(dentry, nd); 53239159de2SJeff Layton if (status > 0) 53339159de2SJeff Layton return 0; 53439159de2SJeff Layton 53516c2cd71SAl Viro if (!status) 53639159de2SJeff Layton status = -ESTALE; 53716c2cd71SAl Viro 5389f1fafeeSAl Viro path_put(&nd->path); 53939159de2SJeff Layton return status; 54039159de2SJeff Layton } 54139159de2SJeff Layton 5422a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 5432a737871SAl Viro { 544f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 545f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 5462a737871SAl Viro } 5472a737871SAl Viro 5486de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 5496de88d72SAl Viro 55031e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 55131e6b01fSNick Piggin { 55231e6b01fSNick Piggin if (!nd->root.mnt) { 55331e6b01fSNick Piggin struct fs_struct *fs = current->fs; 554c28cc364SNick Piggin unsigned seq; 555c28cc364SNick Piggin 556c28cc364SNick Piggin do { 557c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 55831e6b01fSNick Piggin nd->root = fs->root; 559c1530019STim Chen nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq); 560c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 56131e6b01fSNick Piggin } 56231e6b01fSNick Piggin } 56331e6b01fSNick Piggin 564f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 5651da177e4SLinus Torvalds { 56631e6b01fSNick Piggin int ret; 56731e6b01fSNick Piggin 5681da177e4SLinus Torvalds if (IS_ERR(link)) 5691da177e4SLinus Torvalds goto fail; 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds if (*link == '/') { 5722a737871SAl Viro set_root(nd); 5731d957f9bSJan Blunck path_put(&nd->path); 5742a737871SAl Viro nd->path = nd->root; 5752a737871SAl Viro path_get(&nd->root); 57616c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 5771da177e4SLinus Torvalds } 57831e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 579b4091d5fSChristoph Hellwig 58031e6b01fSNick Piggin ret = link_path_walk(link, nd); 58131e6b01fSNick Piggin return ret; 5821da177e4SLinus Torvalds fail: 5831d957f9bSJan Blunck path_put(&nd->path); 5841da177e4SLinus Torvalds return PTR_ERR(link); 5851da177e4SLinus Torvalds } 5861da177e4SLinus Torvalds 5871d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 588051d3812SIan Kent { 589051d3812SIan Kent dput(path->dentry); 5904ac91378SJan Blunck if (path->mnt != nd->path.mnt) 591051d3812SIan Kent mntput(path->mnt); 592051d3812SIan Kent } 593051d3812SIan Kent 5947b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path, 5957b9337aaSNick Piggin struct nameidata *nd) 596051d3812SIan Kent { 59731e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 5984ac91378SJan Blunck dput(nd->path.dentry); 59931e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 6004ac91378SJan Blunck mntput(nd->path.mnt); 6019a229683SHuang Shijie } 60231e6b01fSNick Piggin nd->path.mnt = path->mnt; 6034ac91378SJan Blunck nd->path.dentry = path->dentry; 604051d3812SIan Kent } 605051d3812SIan Kent 606574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie) 607574197e0SAl Viro { 608574197e0SAl Viro struct inode *inode = link->dentry->d_inode; 609574197e0SAl Viro if (!IS_ERR(cookie) && inode->i_op->put_link) 610574197e0SAl Viro inode->i_op->put_link(link->dentry, nd, cookie); 611574197e0SAl Viro path_put(link); 612574197e0SAl Viro } 613574197e0SAl Viro 614def4af30SAl Viro static __always_inline int 615574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p) 6161da177e4SLinus Torvalds { 6171da177e4SLinus Torvalds int error; 6187b9337aaSNick Piggin struct dentry *dentry = link->dentry; 6191da177e4SLinus Torvalds 620844a3917SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 621844a3917SAl Viro 6220e794589SAl Viro if (link->mnt == nd->path.mnt) 6230e794589SAl Viro mntget(link->mnt); 6240e794589SAl Viro 625574197e0SAl Viro if (unlikely(current->total_link_count >= 40)) { 626574197e0SAl Viro *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */ 627574197e0SAl Viro path_put(&nd->path); 628574197e0SAl Viro return -ELOOP; 629574197e0SAl Viro } 630574197e0SAl Viro cond_resched(); 631574197e0SAl Viro current->total_link_count++; 632574197e0SAl Viro 6337b9337aaSNick Piggin touch_atime(link->mnt, dentry); 6341da177e4SLinus Torvalds nd_set_link(nd, NULL); 635cd4e91d3SAl Viro 63636f3b4f6SAl Viro error = security_inode_follow_link(link->dentry, nd); 63736f3b4f6SAl Viro if (error) { 63836f3b4f6SAl Viro *p = ERR_PTR(error); /* no ->put_link(), please */ 63936f3b4f6SAl Viro path_put(&nd->path); 64036f3b4f6SAl Viro return error; 64136f3b4f6SAl Viro } 64236f3b4f6SAl Viro 64386acdca1SAl Viro nd->last_type = LAST_BIND; 644def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 645def4af30SAl Viro error = PTR_ERR(*p); 646def4af30SAl Viro if (!IS_ERR(*p)) { 6471da177e4SLinus Torvalds char *s = nd_get_link(nd); 648cc314eefSLinus Torvalds error = 0; 6491da177e4SLinus Torvalds if (s) 6501da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 651bcda7652SAl Viro else if (nd->last_type == LAST_BIND) { 65216c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 653b21041d0SAl Viro nd->inode = nd->path.dentry->d_inode; 654b21041d0SAl Viro if (nd->inode->i_op->follow_link) { 655bcda7652SAl Viro /* stepped on a _really_ weird one */ 656bcda7652SAl Viro path_put(&nd->path); 657bcda7652SAl Viro error = -ELOOP; 658bcda7652SAl Viro } 659bcda7652SAl Viro } 6601da177e4SLinus Torvalds } 6611da177e4SLinus Torvalds return error; 6621da177e4SLinus Torvalds } 6631da177e4SLinus Torvalds 66431e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 66531e6b01fSNick Piggin { 66631e6b01fSNick Piggin struct vfsmount *parent; 66731e6b01fSNick Piggin struct dentry *mountpoint; 66831e6b01fSNick Piggin 66931e6b01fSNick Piggin parent = path->mnt->mnt_parent; 67031e6b01fSNick Piggin if (parent == path->mnt) 67131e6b01fSNick Piggin return 0; 67231e6b01fSNick Piggin mountpoint = path->mnt->mnt_mountpoint; 67331e6b01fSNick Piggin path->dentry = mountpoint; 67431e6b01fSNick Piggin path->mnt = parent; 67531e6b01fSNick Piggin return 1; 67631e6b01fSNick Piggin } 67731e6b01fSNick Piggin 678bab77ebfSAl Viro int follow_up(struct path *path) 6791da177e4SLinus Torvalds { 6801da177e4SLinus Torvalds struct vfsmount *parent; 6811da177e4SLinus Torvalds struct dentry *mountpoint; 68299b7db7bSNick Piggin 68399b7db7bSNick Piggin br_read_lock(vfsmount_lock); 684bab77ebfSAl Viro parent = path->mnt->mnt_parent; 685bab77ebfSAl Viro if (parent == path->mnt) { 68699b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 6871da177e4SLinus Torvalds return 0; 6881da177e4SLinus Torvalds } 6891da177e4SLinus Torvalds mntget(parent); 690bab77ebfSAl Viro mountpoint = dget(path->mnt->mnt_mountpoint); 69199b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 692bab77ebfSAl Viro dput(path->dentry); 693bab77ebfSAl Viro path->dentry = mountpoint; 694bab77ebfSAl Viro mntput(path->mnt); 695bab77ebfSAl Viro path->mnt = parent; 6961da177e4SLinus Torvalds return 1; 6971da177e4SLinus Torvalds } 6981da177e4SLinus Torvalds 699b5c84bf6SNick Piggin /* 7009875cf80SDavid Howells * Perform an automount 7019875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 7029875cf80SDavid Howells * were called with. 7031da177e4SLinus Torvalds */ 7049875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags, 7059875cf80SDavid Howells bool *need_mntput) 70631e6b01fSNick Piggin { 7079875cf80SDavid Howells struct vfsmount *mnt; 708ea5b778aSDavid Howells int err; 7099875cf80SDavid Howells 7109875cf80SDavid Howells if (!path->dentry->d_op || !path->dentry->d_op->d_automount) 7119875cf80SDavid Howells return -EREMOTE; 7129875cf80SDavid Howells 7136f45b656SDavid Howells /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT 7146f45b656SDavid Howells * and this is the terminal part of the path. 7156f45b656SDavid Howells */ 71649084c3bSAl Viro if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_PARENT)) 7176f45b656SDavid Howells return -EISDIR; /* we actually want to stop here */ 7186f45b656SDavid Howells 7195a30d8a2SDavid Howells /* 7209875cf80SDavid Howells * We don't want to mount if someone's just doing a stat and they've 7219875cf80SDavid Howells * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and 7229875cf80SDavid Howells * appended a '/' to the name. 7239875cf80SDavid Howells */ 7245a30d8a2SDavid Howells if (!(flags & LOOKUP_FOLLOW)) { 7255a30d8a2SDavid Howells /* We do, however, want to mount if someone wants to open or 7265a30d8a2SDavid Howells * create a file of any type under the mountpoint, wants to 7275a30d8a2SDavid Howells * traverse through the mountpoint or wants to open the mounted 7285a30d8a2SDavid Howells * directory. 7295a30d8a2SDavid Howells * Also, autofs may mark negative dentries as being automount 7305a30d8a2SDavid Howells * points. These will need the attentions of the daemon to 7315a30d8a2SDavid Howells * instantiate them before they can be used. 7325a30d8a2SDavid Howells */ 7335a30d8a2SDavid Howells if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | 7345a30d8a2SDavid Howells LOOKUP_OPEN | LOOKUP_CREATE)) && 7355a30d8a2SDavid Howells path->dentry->d_inode) 7369875cf80SDavid Howells return -EISDIR; 7375a30d8a2SDavid Howells } 7389875cf80SDavid Howells current->total_link_count++; 7399875cf80SDavid Howells if (current->total_link_count >= 40) 7409875cf80SDavid Howells return -ELOOP; 7419875cf80SDavid Howells 7429875cf80SDavid Howells mnt = path->dentry->d_op->d_automount(path); 7439875cf80SDavid Howells if (IS_ERR(mnt)) { 7449875cf80SDavid Howells /* 7459875cf80SDavid Howells * The filesystem is allowed to return -EISDIR here to indicate 7469875cf80SDavid Howells * it doesn't want to automount. For instance, autofs would do 7479875cf80SDavid Howells * this so that its userspace daemon can mount on this dentry. 7489875cf80SDavid Howells * 7499875cf80SDavid Howells * However, we can only permit this if it's a terminal point in 7509875cf80SDavid Howells * the path being looked up; if it wasn't then the remainder of 7519875cf80SDavid Howells * the path is inaccessible and we should say so. 7529875cf80SDavid Howells */ 75349084c3bSAl Viro if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT)) 7549875cf80SDavid Howells return -EREMOTE; 7559875cf80SDavid Howells return PTR_ERR(mnt); 75631e6b01fSNick Piggin } 757ea5b778aSDavid Howells 7589875cf80SDavid Howells if (!mnt) /* mount collision */ 7599875cf80SDavid Howells return 0; 7609875cf80SDavid Howells 7618aef1884SAl Viro if (!*need_mntput) { 7628aef1884SAl Viro /* lock_mount() may release path->mnt on error */ 7638aef1884SAl Viro mntget(path->mnt); 7648aef1884SAl Viro *need_mntput = true; 7658aef1884SAl Viro } 76619a167afSAl Viro err = finish_automount(mnt, path); 767ea5b778aSDavid Howells 768ea5b778aSDavid Howells switch (err) { 769ea5b778aSDavid Howells case -EBUSY: 770ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 77119a167afSAl Viro return 0; 772ea5b778aSDavid Howells case 0: 7738aef1884SAl Viro path_put(path); 7749875cf80SDavid Howells path->mnt = mnt; 7759875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 7769875cf80SDavid Howells return 0; 77719a167afSAl Viro default: 77819a167afSAl Viro return err; 7799875cf80SDavid Howells } 78019a167afSAl Viro 781ea5b778aSDavid Howells } 7829875cf80SDavid Howells 7839875cf80SDavid Howells /* 7849875cf80SDavid Howells * Handle a dentry that is managed in some way. 785cc53ce53SDavid Howells * - Flagged for transit management (autofs) 7869875cf80SDavid Howells * - Flagged as mountpoint 7879875cf80SDavid Howells * - Flagged as automount point 7889875cf80SDavid Howells * 7899875cf80SDavid Howells * This may only be called in refwalk mode. 7909875cf80SDavid Howells * 7919875cf80SDavid Howells * Serialization is taken care of in namespace.c 7929875cf80SDavid Howells */ 7939875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 7949875cf80SDavid Howells { 7958aef1884SAl Viro struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */ 7969875cf80SDavid Howells unsigned managed; 7979875cf80SDavid Howells bool need_mntput = false; 7988aef1884SAl Viro int ret = 0; 7999875cf80SDavid Howells 8009875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 8019875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 8029875cf80SDavid Howells * the components of that value change under us */ 8039875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 8049875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 8059875cf80SDavid Howells unlikely(managed != 0)) { 806cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 807cc53ce53SDavid Howells * being held. */ 808cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 809cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 810cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 8111aed3e42SAl Viro ret = path->dentry->d_op->d_manage(path->dentry, false); 812cc53ce53SDavid Howells if (ret < 0) 8138aef1884SAl Viro break; 814cc53ce53SDavid Howells } 815cc53ce53SDavid Howells 8169875cf80SDavid Howells /* Transit to a mounted filesystem. */ 8179875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 8189875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 8199875cf80SDavid Howells if (mounted) { 8209875cf80SDavid Howells dput(path->dentry); 8219875cf80SDavid Howells if (need_mntput) 822463ffb2eSAl Viro mntput(path->mnt); 823463ffb2eSAl Viro path->mnt = mounted; 824463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 8259875cf80SDavid Howells need_mntput = true; 8269875cf80SDavid Howells continue; 827463ffb2eSAl Viro } 828463ffb2eSAl Viro 8299875cf80SDavid Howells /* Something is mounted on this dentry in another 8309875cf80SDavid Howells * namespace and/or whatever was mounted there in this 8319875cf80SDavid Howells * namespace got unmounted before we managed to get the 8329875cf80SDavid Howells * vfsmount_lock */ 8331da177e4SLinus Torvalds } 8349875cf80SDavid Howells 8359875cf80SDavid Howells /* Handle an automount point */ 8369875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 8379875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 8389875cf80SDavid Howells if (ret < 0) 8398aef1884SAl Viro break; 8409875cf80SDavid Howells continue; 8419875cf80SDavid Howells } 8429875cf80SDavid Howells 8439875cf80SDavid Howells /* We didn't change the current path point */ 8449875cf80SDavid Howells break; 8459875cf80SDavid Howells } 8468aef1884SAl Viro 8478aef1884SAl Viro if (need_mntput && path->mnt == mnt) 8488aef1884SAl Viro mntput(path->mnt); 8498aef1884SAl Viro if (ret == -EISDIR) 8508aef1884SAl Viro ret = 0; 8518aef1884SAl Viro return ret; 8521da177e4SLinus Torvalds } 8531da177e4SLinus Torvalds 854cc53ce53SDavid Howells int follow_down_one(struct path *path) 8551da177e4SLinus Torvalds { 8561da177e4SLinus Torvalds struct vfsmount *mounted; 8571da177e4SLinus Torvalds 8581c755af4SAl Viro mounted = lookup_mnt(path); 8591da177e4SLinus Torvalds if (mounted) { 8609393bd07SAl Viro dput(path->dentry); 8619393bd07SAl Viro mntput(path->mnt); 8629393bd07SAl Viro path->mnt = mounted; 8639393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 8641da177e4SLinus Torvalds return 1; 8651da177e4SLinus Torvalds } 8661da177e4SLinus Torvalds return 0; 8671da177e4SLinus Torvalds } 8681da177e4SLinus Torvalds 86962a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry) 87062a7375eSIan Kent { 87162a7375eSIan Kent return (dentry->d_flags & DCACHE_MANAGE_TRANSIT && 87262a7375eSIan Kent dentry->d_op->d_manage(dentry, true) < 0); 87362a7375eSIan Kent } 87462a7375eSIan Kent 8759875cf80SDavid Howells /* 876287548e4SAl Viro * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if 877287548e4SAl Viro * we meet a managed dentry that would need blocking. 8789875cf80SDavid Howells */ 8799875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 880287548e4SAl Viro struct inode **inode) 8819875cf80SDavid Howells { 88262a7375eSIan Kent for (;;) { 8839875cf80SDavid Howells struct vfsmount *mounted; 88462a7375eSIan Kent /* 88562a7375eSIan Kent * Don't forget we might have a non-mountpoint managed dentry 88662a7375eSIan Kent * that wants to block transit. 88762a7375eSIan Kent */ 888287548e4SAl Viro if (unlikely(managed_dentry_might_block(path->dentry))) 889ab90911fSDavid Howells return false; 89062a7375eSIan Kent 89162a7375eSIan Kent if (!d_mountpoint(path->dentry)) 89262a7375eSIan Kent break; 89362a7375eSIan Kent 8949875cf80SDavid Howells mounted = __lookup_mnt(path->mnt, path->dentry, 1); 8959875cf80SDavid Howells if (!mounted) 8969875cf80SDavid Howells break; 8979875cf80SDavid Howells path->mnt = mounted; 8989875cf80SDavid Howells path->dentry = mounted->mnt_root; 8999875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 90059430262SLinus Torvalds /* 90159430262SLinus Torvalds * Update the inode too. We don't need to re-check the 90259430262SLinus Torvalds * dentry sequence number here after this d_inode read, 90359430262SLinus Torvalds * because a mount-point is always pinned. 90459430262SLinus Torvalds */ 90559430262SLinus Torvalds *inode = path->dentry->d_inode; 9069875cf80SDavid Howells } 9079875cf80SDavid Howells return true; 9089875cf80SDavid Howells } 9099875cf80SDavid Howells 910dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd) 911287548e4SAl Viro { 912dea39376SAl Viro while (d_mountpoint(nd->path.dentry)) { 913287548e4SAl Viro struct vfsmount *mounted; 914dea39376SAl Viro mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1); 915287548e4SAl Viro if (!mounted) 916287548e4SAl Viro break; 917dea39376SAl Viro nd->path.mnt = mounted; 918dea39376SAl Viro nd->path.dentry = mounted->mnt_root; 919dea39376SAl Viro nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 920287548e4SAl Viro } 921287548e4SAl Viro } 922287548e4SAl Viro 92331e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 92431e6b01fSNick Piggin { 92531e6b01fSNick Piggin set_root_rcu(nd); 92631e6b01fSNick Piggin 92731e6b01fSNick Piggin while (1) { 92831e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 92931e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 93031e6b01fSNick Piggin break; 93131e6b01fSNick Piggin } 93231e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 93331e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 93431e6b01fSNick Piggin struct dentry *parent = old->d_parent; 93531e6b01fSNick Piggin unsigned seq; 93631e6b01fSNick Piggin 93731e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 93831e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 939ef7562d5SAl Viro goto failed; 94031e6b01fSNick Piggin nd->path.dentry = parent; 94131e6b01fSNick Piggin nd->seq = seq; 94231e6b01fSNick Piggin break; 94331e6b01fSNick Piggin } 94431e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 94531e6b01fSNick Piggin break; 94631e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 94731e6b01fSNick Piggin } 948dea39376SAl Viro follow_mount_rcu(nd); 949dea39376SAl Viro nd->inode = nd->path.dentry->d_inode; 95031e6b01fSNick Piggin return 0; 951ef7562d5SAl Viro 952ef7562d5SAl Viro failed: 953ef7562d5SAl Viro nd->flags &= ~LOOKUP_RCU; 9545b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 955ef7562d5SAl Viro nd->root.mnt = NULL; 956ef7562d5SAl Viro rcu_read_unlock(); 957ef7562d5SAl Viro br_read_unlock(vfsmount_lock); 958ef7562d5SAl Viro return -ECHILD; 95931e6b01fSNick Piggin } 96031e6b01fSNick Piggin 9619875cf80SDavid Howells /* 962cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 963cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 964cc53ce53SDavid Howells * caller is permitted to proceed or not. 965cc53ce53SDavid Howells */ 9667cc90cc3SAl Viro int follow_down(struct path *path) 967cc53ce53SDavid Howells { 968cc53ce53SDavid Howells unsigned managed; 969cc53ce53SDavid Howells int ret; 970cc53ce53SDavid Howells 971cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 972cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 973cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 974cc53ce53SDavid Howells * being held. 975cc53ce53SDavid Howells * 976cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 977cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 978cc53ce53SDavid Howells * other than its daemon the right to mount on its 979cc53ce53SDavid Howells * superstructure. 980cc53ce53SDavid Howells * 981cc53ce53SDavid Howells * The filesystem may sleep at this point. 982cc53ce53SDavid Howells */ 983cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 984cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 985cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 986ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 9871aed3e42SAl Viro path->dentry, false); 988cc53ce53SDavid Howells if (ret < 0) 989cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 990cc53ce53SDavid Howells } 991cc53ce53SDavid Howells 992cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 993cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 994cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 995cc53ce53SDavid Howells if (!mounted) 996cc53ce53SDavid Howells break; 997cc53ce53SDavid Howells dput(path->dentry); 998cc53ce53SDavid Howells mntput(path->mnt); 999cc53ce53SDavid Howells path->mnt = mounted; 1000cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 1001cc53ce53SDavid Howells continue; 1002cc53ce53SDavid Howells } 1003cc53ce53SDavid Howells 1004cc53ce53SDavid Howells /* Don't handle automount points here */ 1005cc53ce53SDavid Howells break; 1006cc53ce53SDavid Howells } 1007cc53ce53SDavid Howells return 0; 1008cc53ce53SDavid Howells } 1009cc53ce53SDavid Howells 1010cc53ce53SDavid Howells /* 10119875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 10129875cf80SDavid Howells */ 10139875cf80SDavid Howells static void follow_mount(struct path *path) 10149875cf80SDavid Howells { 10159875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 10169875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 10179875cf80SDavid Howells if (!mounted) 10189875cf80SDavid Howells break; 10199875cf80SDavid Howells dput(path->dentry); 10209875cf80SDavid Howells mntput(path->mnt); 10219875cf80SDavid Howells path->mnt = mounted; 10229875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 10239875cf80SDavid Howells } 10249875cf80SDavid Howells } 10259875cf80SDavid Howells 102631e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 10271da177e4SLinus Torvalds { 10282a737871SAl Viro set_root(nd); 1029e518ddb7SAndreas Mohr 10301da177e4SLinus Torvalds while(1) { 10314ac91378SJan Blunck struct dentry *old = nd->path.dentry; 10321da177e4SLinus Torvalds 10332a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 10342a737871SAl Viro nd->path.mnt == nd->root.mnt) { 10351da177e4SLinus Torvalds break; 10361da177e4SLinus Torvalds } 10374ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 10383088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 10393088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 10401da177e4SLinus Torvalds dput(old); 10411da177e4SLinus Torvalds break; 10421da177e4SLinus Torvalds } 10433088dd70SAl Viro if (!follow_up(&nd->path)) 10441da177e4SLinus Torvalds break; 10451da177e4SLinus Torvalds } 104679ed0226SAl Viro follow_mount(&nd->path); 104731e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 10481da177e4SLinus Torvalds } 10491da177e4SLinus Torvalds 10501da177e4SLinus Torvalds /* 1051baa03890SNick Piggin * Allocate a dentry with name and parent, and perform a parent 1052baa03890SNick Piggin * directory ->lookup on it. Returns the new dentry, or ERR_PTR 1053baa03890SNick Piggin * on error. parent->d_inode->i_mutex must be held. d_lookup must 1054baa03890SNick Piggin * have verified that no child exists while under i_mutex. 1055baa03890SNick Piggin */ 1056baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent, 1057baa03890SNick Piggin struct qstr *name, struct nameidata *nd) 1058baa03890SNick Piggin { 1059baa03890SNick Piggin struct inode *inode = parent->d_inode; 1060baa03890SNick Piggin struct dentry *dentry; 1061baa03890SNick Piggin struct dentry *old; 1062baa03890SNick Piggin 1063baa03890SNick Piggin /* Don't create child dentry for a dead directory. */ 1064baa03890SNick Piggin if (unlikely(IS_DEADDIR(inode))) 1065baa03890SNick Piggin return ERR_PTR(-ENOENT); 1066baa03890SNick Piggin 1067baa03890SNick Piggin dentry = d_alloc(parent, name); 1068baa03890SNick Piggin if (unlikely(!dentry)) 1069baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1070baa03890SNick Piggin 1071baa03890SNick Piggin old = inode->i_op->lookup(inode, dentry, nd); 1072baa03890SNick Piggin if (unlikely(old)) { 1073baa03890SNick Piggin dput(dentry); 1074baa03890SNick Piggin dentry = old; 1075baa03890SNick Piggin } 1076baa03890SNick Piggin return dentry; 1077baa03890SNick Piggin } 1078baa03890SNick Piggin 1079baa03890SNick Piggin /* 108044396f4bSJosef Bacik * We already have a dentry, but require a lookup to be performed on the parent 108144396f4bSJosef Bacik * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error. 108244396f4bSJosef Bacik * parent->d_inode->i_mutex must be held. d_lookup must have verified that no 108344396f4bSJosef Bacik * child exists while under i_mutex. 108444396f4bSJosef Bacik */ 108544396f4bSJosef Bacik static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry, 108644396f4bSJosef Bacik struct nameidata *nd) 108744396f4bSJosef Bacik { 108844396f4bSJosef Bacik struct inode *inode = parent->d_inode; 108944396f4bSJosef Bacik struct dentry *old; 109044396f4bSJosef Bacik 109144396f4bSJosef Bacik /* Don't create child dentry for a dead directory. */ 109244396f4bSJosef Bacik if (unlikely(IS_DEADDIR(inode))) 109344396f4bSJosef Bacik return ERR_PTR(-ENOENT); 109444396f4bSJosef Bacik 109544396f4bSJosef Bacik old = inode->i_op->lookup(inode, dentry, nd); 109644396f4bSJosef Bacik if (unlikely(old)) { 109744396f4bSJosef Bacik dput(dentry); 109844396f4bSJosef Bacik dentry = old; 109944396f4bSJosef Bacik } 110044396f4bSJosef Bacik return dentry; 110144396f4bSJosef Bacik } 110244396f4bSJosef Bacik 110344396f4bSJosef Bacik /* 11041da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 11051da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 11061da177e4SLinus Torvalds * It _is_ time-critical. 11071da177e4SLinus Torvalds */ 11081da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name, 110931e6b01fSNick Piggin struct path *path, struct inode **inode) 11101da177e4SLinus Torvalds { 11114ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 111231e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 11135a18fff2SAl Viro int need_reval = 1; 11145a18fff2SAl Viro int status = 1; 11159875cf80SDavid Howells int err; 11169875cf80SDavid Howells 11173cac260aSAl Viro /* 1118b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1119b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1120b04f784eSNick Piggin * do the non-racy lookup, below. 1121b04f784eSNick Piggin */ 112231e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 112331e6b01fSNick Piggin unsigned seq; 112431e6b01fSNick Piggin *inode = nd->inode; 112531e6b01fSNick Piggin dentry = __d_lookup_rcu(parent, name, &seq, inode); 11265a18fff2SAl Viro if (!dentry) 11275a18fff2SAl Viro goto unlazy; 11285a18fff2SAl Viro 112931e6b01fSNick Piggin /* Memory barrier in read_seqcount_begin of child is enough */ 113031e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 113131e6b01fSNick Piggin return -ECHILD; 113231e6b01fSNick Piggin nd->seq = seq; 11335a18fff2SAl Viro 113424643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 11355a18fff2SAl Viro status = d_revalidate(dentry, nd); 11365a18fff2SAl Viro if (unlikely(status <= 0)) { 11375a18fff2SAl Viro if (status != -ECHILD) 11385a18fff2SAl Viro need_reval = 0; 11395a18fff2SAl Viro goto unlazy; 11405a18fff2SAl Viro } 114124643087SAl Viro } 114244396f4bSJosef Bacik if (unlikely(d_need_lookup(dentry))) 114344396f4bSJosef Bacik goto unlazy; 114431e6b01fSNick Piggin path->mnt = mnt; 114531e6b01fSNick Piggin path->dentry = dentry; 1146d6e9bd25SAl Viro if (unlikely(!__follow_mount_rcu(nd, path, inode))) 1147d6e9bd25SAl Viro goto unlazy; 1148d6e9bd25SAl Viro if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 1149d6e9bd25SAl Viro goto unlazy; 11509875cf80SDavid Howells return 0; 11515a18fff2SAl Viro unlazy: 115219660af7SAl Viro if (unlazy_walk(nd, dentry)) 11535a18fff2SAl Viro return -ECHILD; 11545a18fff2SAl Viro } else { 115531e6b01fSNick Piggin dentry = __d_lookup(parent, name); 115624643087SAl Viro } 11575a18fff2SAl Viro 115844396f4bSJosef Bacik if (dentry && unlikely(d_need_lookup(dentry))) { 115944396f4bSJosef Bacik dput(dentry); 116044396f4bSJosef Bacik dentry = NULL; 116144396f4bSJosef Bacik } 11625a18fff2SAl Viro retry: 11635a18fff2SAl Viro if (unlikely(!dentry)) { 11645a18fff2SAl Viro struct inode *dir = parent->d_inode; 11655a18fff2SAl Viro BUG_ON(nd->inode != dir); 11665a18fff2SAl Viro 11675a18fff2SAl Viro mutex_lock(&dir->i_mutex); 11685a18fff2SAl Viro dentry = d_lookup(parent, name); 11695a18fff2SAl Viro if (likely(!dentry)) { 11705a18fff2SAl Viro dentry = d_alloc_and_lookup(parent, name, nd); 11715a18fff2SAl Viro if (IS_ERR(dentry)) { 11725a18fff2SAl Viro mutex_unlock(&dir->i_mutex); 11735a18fff2SAl Viro return PTR_ERR(dentry); 11745a18fff2SAl Viro } 11755a18fff2SAl Viro /* known good */ 11765a18fff2SAl Viro need_reval = 0; 11775a18fff2SAl Viro status = 1; 117844396f4bSJosef Bacik } else if (unlikely(d_need_lookup(dentry))) { 117944396f4bSJosef Bacik dentry = d_inode_lookup(parent, dentry, nd); 118044396f4bSJosef Bacik if (IS_ERR(dentry)) { 118144396f4bSJosef Bacik mutex_unlock(&dir->i_mutex); 118244396f4bSJosef Bacik return PTR_ERR(dentry); 118344396f4bSJosef Bacik } 118444396f4bSJosef Bacik /* known good */ 118544396f4bSJosef Bacik need_reval = 0; 118644396f4bSJosef Bacik status = 1; 11875a18fff2SAl Viro } 11885a18fff2SAl Viro mutex_unlock(&dir->i_mutex); 11895a18fff2SAl Viro } 11905a18fff2SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) 11915a18fff2SAl Viro status = d_revalidate(dentry, nd); 11925a18fff2SAl Viro if (unlikely(status <= 0)) { 11935a18fff2SAl Viro if (status < 0) { 11945a18fff2SAl Viro dput(dentry); 11955a18fff2SAl Viro return status; 11965a18fff2SAl Viro } 11975a18fff2SAl Viro if (!d_invalidate(dentry)) { 11985a18fff2SAl Viro dput(dentry); 11995a18fff2SAl Viro dentry = NULL; 12005a18fff2SAl Viro need_reval = 1; 12015a18fff2SAl Viro goto retry; 12025a18fff2SAl Viro } 12035a18fff2SAl Viro } 12045a18fff2SAl Viro 12051da177e4SLinus Torvalds path->mnt = mnt; 12061da177e4SLinus Torvalds path->dentry = dentry; 12079875cf80SDavid Howells err = follow_managed(path, nd->flags); 120889312214SIan Kent if (unlikely(err < 0)) { 120989312214SIan Kent path_put_conditional(path, nd); 12109875cf80SDavid Howells return err; 121189312214SIan Kent } 121231e6b01fSNick Piggin *inode = path->dentry->d_inode; 12131da177e4SLinus Torvalds return 0; 12141da177e4SLinus Torvalds } 12151da177e4SLinus Torvalds 121652094c8aSAl Viro static inline int may_lookup(struct nameidata *nd) 121752094c8aSAl Viro { 121852094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 12194ad5abb3SAl Viro int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); 122052094c8aSAl Viro if (err != -ECHILD) 122152094c8aSAl Viro return err; 122219660af7SAl Viro if (unlazy_walk(nd, NULL)) 122352094c8aSAl Viro return -ECHILD; 122452094c8aSAl Viro } 12254ad5abb3SAl Viro return inode_permission(nd->inode, MAY_EXEC); 122652094c8aSAl Viro } 122752094c8aSAl Viro 12289856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type) 12299856fa1bSAl Viro { 12309856fa1bSAl Viro if (type == LAST_DOTDOT) { 12319856fa1bSAl Viro if (nd->flags & LOOKUP_RCU) { 12329856fa1bSAl Viro if (follow_dotdot_rcu(nd)) 12339856fa1bSAl Viro return -ECHILD; 12349856fa1bSAl Viro } else 12359856fa1bSAl Viro follow_dotdot(nd); 12369856fa1bSAl Viro } 12379856fa1bSAl Viro return 0; 12389856fa1bSAl Viro } 12399856fa1bSAl Viro 1240951361f9SAl Viro static void terminate_walk(struct nameidata *nd) 1241951361f9SAl Viro { 1242951361f9SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1243951361f9SAl Viro path_put(&nd->path); 1244951361f9SAl Viro } else { 1245951361f9SAl Viro nd->flags &= ~LOOKUP_RCU; 12465b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1247951361f9SAl Viro nd->root.mnt = NULL; 1248951361f9SAl Viro rcu_read_unlock(); 1249951361f9SAl Viro br_read_unlock(vfsmount_lock); 1250951361f9SAl Viro } 1251951361f9SAl Viro } 1252951361f9SAl Viro 1253ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path, 1254ce57dfc1SAl Viro struct qstr *name, int type, int follow) 1255ce57dfc1SAl Viro { 1256ce57dfc1SAl Viro struct inode *inode; 1257ce57dfc1SAl Viro int err; 1258ce57dfc1SAl Viro /* 1259ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1260ce57dfc1SAl Viro * to be able to know about the current root directory and 1261ce57dfc1SAl Viro * parent relationships. 1262ce57dfc1SAl Viro */ 1263ce57dfc1SAl Viro if (unlikely(type != LAST_NORM)) 1264ce57dfc1SAl Viro return handle_dots(nd, type); 1265ce57dfc1SAl Viro err = do_lookup(nd, name, path, &inode); 1266ce57dfc1SAl Viro if (unlikely(err)) { 1267ce57dfc1SAl Viro terminate_walk(nd); 1268ce57dfc1SAl Viro return err; 1269ce57dfc1SAl Viro } 1270ce57dfc1SAl Viro if (!inode) { 1271ce57dfc1SAl Viro path_to_nameidata(path, nd); 1272ce57dfc1SAl Viro terminate_walk(nd); 1273ce57dfc1SAl Viro return -ENOENT; 1274ce57dfc1SAl Viro } 1275ce57dfc1SAl Viro if (unlikely(inode->i_op->follow_link) && follow) { 127619660af7SAl Viro if (nd->flags & LOOKUP_RCU) { 127719660af7SAl Viro if (unlikely(unlazy_walk(nd, path->dentry))) { 127819660af7SAl Viro terminate_walk(nd); 1279ce57dfc1SAl Viro return -ECHILD; 128019660af7SAl Viro } 128119660af7SAl Viro } 1282ce57dfc1SAl Viro BUG_ON(inode != path->dentry->d_inode); 1283ce57dfc1SAl Viro return 1; 1284ce57dfc1SAl Viro } 1285ce57dfc1SAl Viro path_to_nameidata(path, nd); 1286ce57dfc1SAl Viro nd->inode = inode; 1287ce57dfc1SAl Viro return 0; 1288ce57dfc1SAl Viro } 1289ce57dfc1SAl Viro 12901da177e4SLinus Torvalds /* 1291b356379aSAl Viro * This limits recursive symlink follows to 8, while 1292b356379aSAl Viro * limiting consecutive symlinks to 40. 1293b356379aSAl Viro * 1294b356379aSAl Viro * Without that kind of total limit, nasty chains of consecutive 1295b356379aSAl Viro * symlinks can cause almost arbitrarily long lookups. 1296b356379aSAl Viro */ 1297b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd) 1298b356379aSAl Viro { 1299b356379aSAl Viro int res; 1300b356379aSAl Viro 1301b356379aSAl Viro if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { 1302b356379aSAl Viro path_put_conditional(path, nd); 1303b356379aSAl Viro path_put(&nd->path); 1304b356379aSAl Viro return -ELOOP; 1305b356379aSAl Viro } 13061a4022f8SErez Zadok BUG_ON(nd->depth >= MAX_NESTED_LINKS); 1307b356379aSAl Viro 1308b356379aSAl Viro nd->depth++; 1309b356379aSAl Viro current->link_count++; 1310b356379aSAl Viro 1311b356379aSAl Viro do { 1312b356379aSAl Viro struct path link = *path; 1313b356379aSAl Viro void *cookie; 1314574197e0SAl Viro 1315574197e0SAl Viro res = follow_link(&link, nd, &cookie); 1316b356379aSAl Viro if (!res) 1317b356379aSAl Viro res = walk_component(nd, path, &nd->last, 1318b356379aSAl Viro nd->last_type, LOOKUP_FOLLOW); 1319574197e0SAl Viro put_link(nd, &link, cookie); 1320b356379aSAl Viro } while (res > 0); 1321b356379aSAl Viro 1322b356379aSAl Viro current->link_count--; 1323b356379aSAl Viro nd->depth--; 1324b356379aSAl Viro return res; 1325b356379aSAl Viro } 1326b356379aSAl Viro 1327b356379aSAl Viro /* 13281da177e4SLinus Torvalds * Name resolution. 1329ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1330ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 13311da177e4SLinus Torvalds * 1332ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1333ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 13341da177e4SLinus Torvalds */ 13356de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 13361da177e4SLinus Torvalds { 13371da177e4SLinus Torvalds struct path next; 13381da177e4SLinus Torvalds int err; 13391da177e4SLinus Torvalds 13401da177e4SLinus Torvalds while (*name=='/') 13411da177e4SLinus Torvalds name++; 13421da177e4SLinus Torvalds if (!*name) 1343086e183aSAl Viro return 0; 13441da177e4SLinus Torvalds 13451da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 13461da177e4SLinus Torvalds for(;;) { 13471da177e4SLinus Torvalds unsigned long hash; 13481da177e4SLinus Torvalds struct qstr this; 13491da177e4SLinus Torvalds unsigned int c; 1350fe479a58SAl Viro int type; 13511da177e4SLinus Torvalds 135252094c8aSAl Viro err = may_lookup(nd); 13531da177e4SLinus Torvalds if (err) 13541da177e4SLinus Torvalds break; 13551da177e4SLinus Torvalds 13561da177e4SLinus Torvalds this.name = name; 13571da177e4SLinus Torvalds c = *(const unsigned char *)name; 13581da177e4SLinus Torvalds 13591da177e4SLinus Torvalds hash = init_name_hash(); 13601da177e4SLinus Torvalds do { 13611da177e4SLinus Torvalds name++; 13621da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 13631da177e4SLinus Torvalds c = *(const unsigned char *)name; 13641da177e4SLinus Torvalds } while (c && (c != '/')); 13651da177e4SLinus Torvalds this.len = name - (const char *) this.name; 13661da177e4SLinus Torvalds this.hash = end_name_hash(hash); 13671da177e4SLinus Torvalds 1368fe479a58SAl Viro type = LAST_NORM; 1369fe479a58SAl Viro if (this.name[0] == '.') switch (this.len) { 1370fe479a58SAl Viro case 2: 137116c2cd71SAl Viro if (this.name[1] == '.') { 1372fe479a58SAl Viro type = LAST_DOTDOT; 137316c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 137416c2cd71SAl Viro } 1375fe479a58SAl Viro break; 1376fe479a58SAl Viro case 1: 1377fe479a58SAl Viro type = LAST_DOT; 1378fe479a58SAl Viro } 13795a202bcdSAl Viro if (likely(type == LAST_NORM)) { 13805a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 138116c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 13825a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 13835a202bcdSAl Viro err = parent->d_op->d_hash(parent, nd->inode, 13845a202bcdSAl Viro &this); 13855a202bcdSAl Viro if (err < 0) 13865a202bcdSAl Viro break; 13875a202bcdSAl Viro } 13885a202bcdSAl Viro } 1389fe479a58SAl Viro 13901da177e4SLinus Torvalds /* remove trailing slashes? */ 13911da177e4SLinus Torvalds if (!c) 13921da177e4SLinus Torvalds goto last_component; 13931da177e4SLinus Torvalds while (*++name == '/'); 13941da177e4SLinus Torvalds if (!*name) 1395b356379aSAl Viro goto last_component; 13961da177e4SLinus Torvalds 1397ce57dfc1SAl Viro err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW); 1398ce57dfc1SAl Viro if (err < 0) 1399ce57dfc1SAl Viro return err; 1400fe479a58SAl Viro 1401ce57dfc1SAl Viro if (err) { 1402b356379aSAl Viro err = nested_symlink(&next, nd); 14031da177e4SLinus Torvalds if (err) 1404a7472babSAl Viro return err; 140531e6b01fSNick Piggin } 14061da177e4SLinus Torvalds err = -ENOTDIR; 140731e6b01fSNick Piggin if (!nd->inode->i_op->lookup) 14081da177e4SLinus Torvalds break; 14091da177e4SLinus Torvalds continue; 14101da177e4SLinus Torvalds /* here ends the main loop */ 14111da177e4SLinus Torvalds 14121da177e4SLinus Torvalds last_component: 1413ce57dfc1SAl Viro nd->last = this; 1414ce57dfc1SAl Viro nd->last_type = type; 1415ce57dfc1SAl Viro return 0; 1416ce57dfc1SAl Viro } 1417951361f9SAl Viro terminate_walk(nd); 14181da177e4SLinus Torvalds return err; 14191da177e4SLinus Torvalds } 14201da177e4SLinus Torvalds 142170e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, 142270e9b357SAl Viro struct nameidata *nd, struct file **fp) 142331e6b01fSNick Piggin { 142431e6b01fSNick Piggin int retval = 0; 142531e6b01fSNick Piggin int fput_needed; 142631e6b01fSNick Piggin struct file *file; 142731e6b01fSNick Piggin 142831e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 142916c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 143031e6b01fSNick Piggin nd->depth = 0; 14315b6ca027SAl Viro if (flags & LOOKUP_ROOT) { 14325b6ca027SAl Viro struct inode *inode = nd->root.dentry->d_inode; 143373d049a4SAl Viro if (*name) { 14345b6ca027SAl Viro if (!inode->i_op->lookup) 14355b6ca027SAl Viro return -ENOTDIR; 14365b6ca027SAl Viro retval = inode_permission(inode, MAY_EXEC); 14375b6ca027SAl Viro if (retval) 14385b6ca027SAl Viro return retval; 143973d049a4SAl Viro } 14405b6ca027SAl Viro nd->path = nd->root; 14415b6ca027SAl Viro nd->inode = inode; 14425b6ca027SAl Viro if (flags & LOOKUP_RCU) { 14435b6ca027SAl Viro br_read_lock(vfsmount_lock); 14445b6ca027SAl Viro rcu_read_lock(); 14455b6ca027SAl Viro nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 14465b6ca027SAl Viro } else { 14475b6ca027SAl Viro path_get(&nd->path); 14485b6ca027SAl Viro } 14495b6ca027SAl Viro return 0; 14505b6ca027SAl Viro } 14515b6ca027SAl Viro 145231e6b01fSNick Piggin nd->root.mnt = NULL; 145331e6b01fSNick Piggin 145431e6b01fSNick Piggin if (*name=='/') { 1455e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 145631e6b01fSNick Piggin br_read_lock(vfsmount_lock); 145731e6b01fSNick Piggin rcu_read_lock(); 1458e41f7d4eSAl Viro set_root_rcu(nd); 1459e41f7d4eSAl Viro } else { 1460e41f7d4eSAl Viro set_root(nd); 1461e41f7d4eSAl Viro path_get(&nd->root); 1462e41f7d4eSAl Viro } 146331e6b01fSNick Piggin nd->path = nd->root; 146431e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 1465e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 146631e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1467c28cc364SNick Piggin unsigned seq; 146831e6b01fSNick Piggin 146931e6b01fSNick Piggin br_read_lock(vfsmount_lock); 147031e6b01fSNick Piggin rcu_read_lock(); 147131e6b01fSNick Piggin 1472c28cc364SNick Piggin do { 1473c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 147431e6b01fSNick Piggin nd->path = fs->pwd; 1475c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1476c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1477e41f7d4eSAl Viro } else { 1478e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 1479e41f7d4eSAl Viro } 148031e6b01fSNick Piggin } else { 148131e6b01fSNick Piggin struct dentry *dentry; 148231e6b01fSNick Piggin 14831abf0c71SAl Viro file = fget_raw_light(dfd, &fput_needed); 148431e6b01fSNick Piggin retval = -EBADF; 148531e6b01fSNick Piggin if (!file) 148631e6b01fSNick Piggin goto out_fail; 148731e6b01fSNick Piggin 148831e6b01fSNick Piggin dentry = file->f_path.dentry; 148931e6b01fSNick Piggin 1490f52e0c11SAl Viro if (*name) { 149131e6b01fSNick Piggin retval = -ENOTDIR; 149231e6b01fSNick Piggin if (!S_ISDIR(dentry->d_inode->i_mode)) 149331e6b01fSNick Piggin goto fput_fail; 149431e6b01fSNick Piggin 14954ad5abb3SAl Viro retval = inode_permission(dentry->d_inode, MAY_EXEC); 149631e6b01fSNick Piggin if (retval) 149731e6b01fSNick Piggin goto fput_fail; 1498f52e0c11SAl Viro } 149931e6b01fSNick Piggin 150031e6b01fSNick Piggin nd->path = file->f_path; 1501e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 150231e6b01fSNick Piggin if (fput_needed) 150370e9b357SAl Viro *fp = file; 1504c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 150531e6b01fSNick Piggin br_read_lock(vfsmount_lock); 150631e6b01fSNick Piggin rcu_read_lock(); 15075590ff0dSUlrich Drepper } else { 15085dd784d0SJan Blunck path_get(&file->f_path); 15095590ff0dSUlrich Drepper fput_light(file, fput_needed); 15101da177e4SLinus Torvalds } 1511e41f7d4eSAl Viro } 1512e41f7d4eSAl Viro 151331e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 15149b4a9b14SAl Viro return 0; 15152dfdd266SJosef 'Jeff' Sipek 15169b4a9b14SAl Viro fput_fail: 15179b4a9b14SAl Viro fput_light(file, fput_needed); 15189b4a9b14SAl Viro out_fail: 15199b4a9b14SAl Viro return retval; 15209b4a9b14SAl Viro } 15219b4a9b14SAl Viro 1522bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path) 1523bd92d7feSAl Viro { 1524bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 1525bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 1526bd92d7feSAl Viro 1527bd92d7feSAl Viro nd->flags &= ~LOOKUP_PARENT; 1528bd92d7feSAl Viro return walk_component(nd, path, &nd->last, nd->last_type, 1529bd92d7feSAl Viro nd->flags & LOOKUP_FOLLOW); 1530bd92d7feSAl Viro } 1531bd92d7feSAl Viro 15329b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1533ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name, 15349b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 15359b4a9b14SAl Viro { 153670e9b357SAl Viro struct file *base = NULL; 1537bd92d7feSAl Viro struct path path; 1538bd92d7feSAl Viro int err; 153931e6b01fSNick Piggin 154031e6b01fSNick Piggin /* 154131e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 154231e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 154331e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 154431e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 154531e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 154631e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 154731e6b01fSNick Piggin * analogue, foo_rcu(). 154831e6b01fSNick Piggin * 154931e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 155031e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 155131e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 155231e6b01fSNick Piggin * be able to complete). 155331e6b01fSNick Piggin */ 1554bd92d7feSAl Viro err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); 1555ee0827cdSAl Viro 1556bd92d7feSAl Viro if (unlikely(err)) 1557bd92d7feSAl Viro return err; 1558ee0827cdSAl Viro 1559ee0827cdSAl Viro current->total_link_count = 0; 1560bd92d7feSAl Viro err = link_path_walk(name, nd); 1561bd92d7feSAl Viro 1562bd92d7feSAl Viro if (!err && !(flags & LOOKUP_PARENT)) { 1563bd92d7feSAl Viro err = lookup_last(nd, &path); 1564bd92d7feSAl Viro while (err > 0) { 1565bd92d7feSAl Viro void *cookie; 1566bd92d7feSAl Viro struct path link = path; 1567bd92d7feSAl Viro nd->flags |= LOOKUP_PARENT; 1568574197e0SAl Viro err = follow_link(&link, nd, &cookie); 1569bd92d7feSAl Viro if (!err) 1570bd92d7feSAl Viro err = lookup_last(nd, &path); 1571574197e0SAl Viro put_link(nd, &link, cookie); 1572bd92d7feSAl Viro } 1573bd92d7feSAl Viro } 1574ee0827cdSAl Viro 15759f1fafeeSAl Viro if (!err) 15769f1fafeeSAl Viro err = complete_walk(nd); 1577bd92d7feSAl Viro 1578bd92d7feSAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) { 1579bd92d7feSAl Viro if (!nd->inode->i_op->lookup) { 1580bd92d7feSAl Viro path_put(&nd->path); 1581bd23a539SAl Viro err = -ENOTDIR; 1582bd92d7feSAl Viro } 1583bd92d7feSAl Viro } 158416c2cd71SAl Viro 158570e9b357SAl Viro if (base) 158670e9b357SAl Viro fput(base); 1587ee0827cdSAl Viro 15885b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 158931e6b01fSNick Piggin path_put(&nd->root); 159031e6b01fSNick Piggin nd->root.mnt = NULL; 159131e6b01fSNick Piggin } 1592bd92d7feSAl Viro return err; 159331e6b01fSNick Piggin } 159431e6b01fSNick Piggin 1595ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name, 1596ee0827cdSAl Viro unsigned int flags, struct nameidata *nd) 1597ee0827cdSAl Viro { 1598ee0827cdSAl Viro int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd); 1599ee0827cdSAl Viro if (unlikely(retval == -ECHILD)) 1600ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags, nd); 1601ee0827cdSAl Viro if (unlikely(retval == -ESTALE)) 1602ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd); 1603ee0827cdSAl Viro 160431e6b01fSNick Piggin if (likely(!retval)) { 160531e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) { 160631e6b01fSNick Piggin if (nd->path.dentry && nd->inode) 160731e6b01fSNick Piggin audit_inode(name, nd->path.dentry); 160831e6b01fSNick Piggin } 160931e6b01fSNick Piggin } 1610170aa3d0SUlrich Drepper return retval; 16111da177e4SLinus Torvalds } 16121da177e4SLinus Torvalds 1613c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd) 16145590ff0dSUlrich Drepper { 1615c9c6cac0SAl Viro return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd); 16165590ff0dSUlrich Drepper } 16175590ff0dSUlrich Drepper 1618d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1619d1811465SAl Viro { 1620d1811465SAl Viro struct nameidata nd; 1621d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1622d1811465SAl Viro if (!res) 1623d1811465SAl Viro *path = nd.path; 1624d1811465SAl Viro return res; 1625d1811465SAl Viro } 1626d1811465SAl Viro 162716f18200SJosef 'Jeff' Sipek /** 162816f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 162916f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 163016f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 163116f18200SJosef 'Jeff' Sipek * @name: pointer to file name 163216f18200SJosef 'Jeff' Sipek * @flags: lookup flags 1633e0a01249SAl Viro * @path: pointer to struct path to fill 163416f18200SJosef 'Jeff' Sipek */ 163516f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 163616f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 1637e0a01249SAl Viro struct path *path) 163816f18200SJosef 'Jeff' Sipek { 1639e0a01249SAl Viro struct nameidata nd; 1640e0a01249SAl Viro int err; 1641e0a01249SAl Viro nd.root.dentry = dentry; 1642e0a01249SAl Viro nd.root.mnt = mnt; 1643e0a01249SAl Viro BUG_ON(flags & LOOKUP_PARENT); 16445b6ca027SAl Viro /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ 1645e0a01249SAl Viro err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd); 1646e0a01249SAl Viro if (!err) 1647e0a01249SAl Viro *path = nd.path; 1648e0a01249SAl Viro return err; 164916f18200SJosef 'Jeff' Sipek } 165016f18200SJosef 'Jeff' Sipek 1651eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name, 1652eead1911SChristoph Hellwig struct dentry *base, struct nameidata *nd) 16531da177e4SLinus Torvalds { 165481fca444SChristoph Hellwig struct inode *inode = base->d_inode; 16551da177e4SLinus Torvalds struct dentry *dentry; 16561da177e4SLinus Torvalds int err; 16571da177e4SLinus Torvalds 16584ad5abb3SAl Viro err = inode_permission(inode, MAY_EXEC); 165981fca444SChristoph Hellwig if (err) 166081fca444SChristoph Hellwig return ERR_PTR(err); 16611da177e4SLinus Torvalds 16621da177e4SLinus Torvalds /* 1663b04f784eSNick Piggin * Don't bother with __d_lookup: callers are for creat as 1664b04f784eSNick Piggin * well as unlink, so a lot of the time it would cost 1665b04f784eSNick Piggin * a double lookup. 16666e6b1bd1SAl Viro */ 16676e6b1bd1SAl Viro dentry = d_lookup(base, name); 16686e6b1bd1SAl Viro 166944396f4bSJosef Bacik if (dentry && d_need_lookup(dentry)) { 167044396f4bSJosef Bacik /* 167144396f4bSJosef Bacik * __lookup_hash is called with the parent dir's i_mutex already 167244396f4bSJosef Bacik * held, so we are good to go here. 167344396f4bSJosef Bacik */ 167444396f4bSJosef Bacik dentry = d_inode_lookup(base, dentry, nd); 167544396f4bSJosef Bacik if (IS_ERR(dentry)) 167644396f4bSJosef Bacik return dentry; 167744396f4bSJosef Bacik } 167844396f4bSJosef Bacik 1679d2d9e9fbSAl Viro if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) { 1680d2d9e9fbSAl Viro int status = d_revalidate(dentry, nd); 1681d2d9e9fbSAl Viro if (unlikely(status <= 0)) { 1682d2d9e9fbSAl Viro /* 1683d2d9e9fbSAl Viro * The dentry failed validation. 1684d2d9e9fbSAl Viro * If d_revalidate returned 0 attempt to invalidate 1685d2d9e9fbSAl Viro * the dentry otherwise d_revalidate is asking us 1686d2d9e9fbSAl Viro * to return a fail status. 1687d2d9e9fbSAl Viro */ 1688d2d9e9fbSAl Viro if (status < 0) { 1689d2d9e9fbSAl Viro dput(dentry); 1690d2d9e9fbSAl Viro return ERR_PTR(status); 1691d2d9e9fbSAl Viro } else if (!d_invalidate(dentry)) { 1692d2d9e9fbSAl Viro dput(dentry); 1693d2d9e9fbSAl Viro dentry = NULL; 1694d2d9e9fbSAl Viro } 1695d2d9e9fbSAl Viro } 1696d2d9e9fbSAl Viro } 16976e6b1bd1SAl Viro 16981da177e4SLinus Torvalds if (!dentry) 1699baa03890SNick Piggin dentry = d_alloc_and_lookup(base, name, nd); 17005a202bcdSAl Viro 17011da177e4SLinus Torvalds return dentry; 17021da177e4SLinus Torvalds } 17031da177e4SLinus Torvalds 1704057f6c01SJames Morris /* 1705057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1706057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1707057f6c01SJames Morris * SMP-safe. 1708057f6c01SJames Morris */ 1709a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 17101da177e4SLinus Torvalds { 17114ac91378SJan Blunck return __lookup_hash(&nd->last, nd->path.dentry, nd); 17121da177e4SLinus Torvalds } 17131da177e4SLinus Torvalds 1714eead1911SChristoph Hellwig /** 1715a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1716eead1911SChristoph Hellwig * @name: pathname component to lookup 1717eead1911SChristoph Hellwig * @base: base directory to lookup from 1718eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1719eead1911SChristoph Hellwig * 1720a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1721a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1722eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1723eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1724eead1911SChristoph Hellwig */ 1725057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1726057f6c01SJames Morris { 1727057f6c01SJames Morris struct qstr this; 17286a96ba54SAl Viro unsigned long hash; 17296a96ba54SAl Viro unsigned int c; 1730057f6c01SJames Morris 17312f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 17322f9092e1SDavid Woodhouse 17336a96ba54SAl Viro this.name = name; 17346a96ba54SAl Viro this.len = len; 17356a96ba54SAl Viro if (!len) 17366a96ba54SAl Viro return ERR_PTR(-EACCES); 17376a96ba54SAl Viro 17386a96ba54SAl Viro hash = init_name_hash(); 17396a96ba54SAl Viro while (len--) { 17406a96ba54SAl Viro c = *(const unsigned char *)name++; 17416a96ba54SAl Viro if (c == '/' || c == '\0') 17426a96ba54SAl Viro return ERR_PTR(-EACCES); 17436a96ba54SAl Viro hash = partial_name_hash(c, hash); 17446a96ba54SAl Viro } 17456a96ba54SAl Viro this.hash = end_name_hash(hash); 17465a202bcdSAl Viro /* 17475a202bcdSAl Viro * See if the low-level filesystem might want 17485a202bcdSAl Viro * to use its own hash.. 17495a202bcdSAl Viro */ 17505a202bcdSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 17515a202bcdSAl Viro int err = base->d_op->d_hash(base, base->d_inode, &this); 17525a202bcdSAl Viro if (err < 0) 17535a202bcdSAl Viro return ERR_PTR(err); 17545a202bcdSAl Viro } 1755eead1911SChristoph Hellwig 175649705b77SChristoph Hellwig return __lookup_hash(&this, base, NULL); 1757057f6c01SJames Morris } 1758057f6c01SJames Morris 17592d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags, 17602d8f3038SAl Viro struct path *path) 17611da177e4SLinus Torvalds { 17622d8f3038SAl Viro struct nameidata nd; 1763f52e0c11SAl Viro char *tmp = getname_flags(name, flags); 17641da177e4SLinus Torvalds int err = PTR_ERR(tmp); 17651da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 17662d8f3038SAl Viro 17672d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 17682d8f3038SAl Viro 17692d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 17701da177e4SLinus Torvalds putname(tmp); 17712d8f3038SAl Viro if (!err) 17722d8f3038SAl Viro *path = nd.path; 17731da177e4SLinus Torvalds } 17741da177e4SLinus Torvalds return err; 17751da177e4SLinus Torvalds } 17761da177e4SLinus Torvalds 17772ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 17782ad94ae6SAl Viro struct nameidata *nd, char **name) 17792ad94ae6SAl Viro { 17802ad94ae6SAl Viro char *s = getname(path); 17812ad94ae6SAl Viro int error; 17822ad94ae6SAl Viro 17832ad94ae6SAl Viro if (IS_ERR(s)) 17842ad94ae6SAl Viro return PTR_ERR(s); 17852ad94ae6SAl Viro 17862ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 17872ad94ae6SAl Viro if (error) 17882ad94ae6SAl Viro putname(s); 17892ad94ae6SAl Viro else 17902ad94ae6SAl Viro *name = s; 17912ad94ae6SAl Viro 17922ad94ae6SAl Viro return error; 17932ad94ae6SAl Viro } 17942ad94ae6SAl Viro 17951da177e4SLinus Torvalds /* 17961da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 17971da177e4SLinus Torvalds * minimal. 17981da177e4SLinus Torvalds */ 17991da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 18001da177e4SLinus Torvalds { 1801da9592edSDavid Howells uid_t fsuid = current_fsuid(); 1802da9592edSDavid Howells 18031da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 18041da177e4SLinus Torvalds return 0; 1805e795b717SSerge E. Hallyn if (current_user_ns() != inode_userns(inode)) 1806e795b717SSerge E. Hallyn goto other_userns; 1807da9592edSDavid Howells if (inode->i_uid == fsuid) 18081da177e4SLinus Torvalds return 0; 1809da9592edSDavid Howells if (dir->i_uid == fsuid) 18101da177e4SLinus Torvalds return 0; 1811e795b717SSerge E. Hallyn 1812e795b717SSerge E. Hallyn other_userns: 1813e795b717SSerge E. Hallyn return !ns_capable(inode_userns(inode), CAP_FOWNER); 18141da177e4SLinus Torvalds } 18151da177e4SLinus Torvalds 18161da177e4SLinus Torvalds /* 18171da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 18181da177e4SLinus Torvalds * whether the type of victim is right. 18191da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 18201da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 18211da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 18221da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 18231da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 18241da177e4SLinus Torvalds * a. be owner of dir, or 18251da177e4SLinus Torvalds * b. be owner of victim, or 18261da177e4SLinus Torvalds * c. have CAP_FOWNER capability 18271da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 18281da177e4SLinus Torvalds * links pointing to it. 18291da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 18301da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 18311da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 18321da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 18331da177e4SLinus Torvalds * nfs_async_unlink(). 18341da177e4SLinus Torvalds */ 1835858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 18361da177e4SLinus Torvalds { 18371da177e4SLinus Torvalds int error; 18381da177e4SLinus Torvalds 18391da177e4SLinus Torvalds if (!victim->d_inode) 18401da177e4SLinus Torvalds return -ENOENT; 18411da177e4SLinus Torvalds 18421da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 1843cccc6bbaSAl Viro audit_inode_child(victim, dir); 18441da177e4SLinus Torvalds 1845f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 18461da177e4SLinus Torvalds if (error) 18471da177e4SLinus Torvalds return error; 18481da177e4SLinus Torvalds if (IS_APPEND(dir)) 18491da177e4SLinus Torvalds return -EPERM; 18501da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 1851f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 18521da177e4SLinus Torvalds return -EPERM; 18531da177e4SLinus Torvalds if (isdir) { 18541da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 18551da177e4SLinus Torvalds return -ENOTDIR; 18561da177e4SLinus Torvalds if (IS_ROOT(victim)) 18571da177e4SLinus Torvalds return -EBUSY; 18581da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 18591da177e4SLinus Torvalds return -EISDIR; 18601da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 18611da177e4SLinus Torvalds return -ENOENT; 18621da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 18631da177e4SLinus Torvalds return -EBUSY; 18641da177e4SLinus Torvalds return 0; 18651da177e4SLinus Torvalds } 18661da177e4SLinus Torvalds 18671da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 18681da177e4SLinus Torvalds * dir. 18691da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 18701da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 18711da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 18721da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 18731da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 18741da177e4SLinus Torvalds */ 1875a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 18761da177e4SLinus Torvalds { 18771da177e4SLinus Torvalds if (child->d_inode) 18781da177e4SLinus Torvalds return -EEXIST; 18791da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 18801da177e4SLinus Torvalds return -ENOENT; 1881f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 18821da177e4SLinus Torvalds } 18831da177e4SLinus Torvalds 18841da177e4SLinus Torvalds /* 18851da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 18861da177e4SLinus Torvalds */ 18871da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 18881da177e4SLinus Torvalds { 18891da177e4SLinus Torvalds struct dentry *p; 18901da177e4SLinus Torvalds 18911da177e4SLinus Torvalds if (p1 == p2) { 1892f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 18931da177e4SLinus Torvalds return NULL; 18941da177e4SLinus Torvalds } 18951da177e4SLinus Torvalds 1896a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 18971da177e4SLinus Torvalds 1898e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 1899e2761a11SOGAWA Hirofumi if (p) { 1900f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 1901f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 19021da177e4SLinus Torvalds return p; 19031da177e4SLinus Torvalds } 19041da177e4SLinus Torvalds 1905e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 1906e2761a11SOGAWA Hirofumi if (p) { 1907f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1908f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 19091da177e4SLinus Torvalds return p; 19101da177e4SLinus Torvalds } 19111da177e4SLinus Torvalds 1912f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1913f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 19141da177e4SLinus Torvalds return NULL; 19151da177e4SLinus Torvalds } 19161da177e4SLinus Torvalds 19171da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 19181da177e4SLinus Torvalds { 19191b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 19201da177e4SLinus Torvalds if (p1 != p2) { 19211b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 1922a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 19231da177e4SLinus Torvalds } 19241da177e4SLinus Torvalds } 19251da177e4SLinus Torvalds 19261da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode, 19271da177e4SLinus Torvalds struct nameidata *nd) 19281da177e4SLinus Torvalds { 1929a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 19301da177e4SLinus Torvalds 19311da177e4SLinus Torvalds if (error) 19321da177e4SLinus Torvalds return error; 19331da177e4SLinus Torvalds 1934acfa4380SAl Viro if (!dir->i_op->create) 19351da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 19361da177e4SLinus Torvalds mode &= S_IALLUGO; 19371da177e4SLinus Torvalds mode |= S_IFREG; 19381da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 19391da177e4SLinus Torvalds if (error) 19401da177e4SLinus Torvalds return error; 19411da177e4SLinus Torvalds error = dir->i_op->create(dir, dentry, mode, nd); 1942a74574aaSStephen Smalley if (!error) 1943f38aa942SAmy Griffis fsnotify_create(dir, dentry); 19441da177e4SLinus Torvalds return error; 19451da177e4SLinus Torvalds } 19461da177e4SLinus Torvalds 194773d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag) 19481da177e4SLinus Torvalds { 19493fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 19501da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 19511da177e4SLinus Torvalds int error; 19521da177e4SLinus Torvalds 1953bcda7652SAl Viro /* O_PATH? */ 1954bcda7652SAl Viro if (!acc_mode) 1955bcda7652SAl Viro return 0; 1956bcda7652SAl Viro 19571da177e4SLinus Torvalds if (!inode) 19581da177e4SLinus Torvalds return -ENOENT; 19591da177e4SLinus Torvalds 1960c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 1961c8fe8f30SChristoph Hellwig case S_IFLNK: 19621da177e4SLinus Torvalds return -ELOOP; 1963c8fe8f30SChristoph Hellwig case S_IFDIR: 1964c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 19651da177e4SLinus Torvalds return -EISDIR; 1966c8fe8f30SChristoph Hellwig break; 1967c8fe8f30SChristoph Hellwig case S_IFBLK: 1968c8fe8f30SChristoph Hellwig case S_IFCHR: 19693fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 19701da177e4SLinus Torvalds return -EACCES; 1971c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 1972c8fe8f30SChristoph Hellwig case S_IFIFO: 1973c8fe8f30SChristoph Hellwig case S_IFSOCK: 19741da177e4SLinus Torvalds flag &= ~O_TRUNC; 1975c8fe8f30SChristoph Hellwig break; 19764a3fd211SDave Hansen } 1977b41572e9SDave Hansen 19783fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 1979b41572e9SDave Hansen if (error) 1980b41572e9SDave Hansen return error; 19816146f0d5SMimi Zohar 19821da177e4SLinus Torvalds /* 19831da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 19841da177e4SLinus Torvalds */ 19851da177e4SLinus Torvalds if (IS_APPEND(inode)) { 19868737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 19877715b521SAl Viro return -EPERM; 19881da177e4SLinus Torvalds if (flag & O_TRUNC) 19897715b521SAl Viro return -EPERM; 19901da177e4SLinus Torvalds } 19911da177e4SLinus Torvalds 19921da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 19932e149670SSerge E. Hallyn if (flag & O_NOATIME && !inode_owner_or_capable(inode)) 19947715b521SAl Viro return -EPERM; 19951da177e4SLinus Torvalds 19961da177e4SLinus Torvalds /* 19971da177e4SLinus Torvalds * Ensure there are no outstanding leases on the file. 19981da177e4SLinus Torvalds */ 1999b65a9cfcSAl Viro return break_lease(inode, flag); 20007715b521SAl Viro } 20017715b521SAl Viro 2002e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 20037715b521SAl Viro { 2004e1181ee6SJeff Layton struct path *path = &filp->f_path; 20057715b521SAl Viro struct inode *inode = path->dentry->d_inode; 20067715b521SAl Viro int error = get_write_access(inode); 20071da177e4SLinus Torvalds if (error) 20087715b521SAl Viro return error; 20091da177e4SLinus Torvalds /* 20101da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 20111da177e4SLinus Torvalds */ 20121da177e4SLinus Torvalds error = locks_verify_locked(inode); 2013be6d3e56SKentaro Takeda if (!error) 2014ea0d3ab2STetsuo Handa error = security_path_truncate(path); 20151da177e4SLinus Torvalds if (!error) { 20167715b521SAl Viro error = do_truncate(path->dentry, 0, 2017d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 2018e1181ee6SJeff Layton filp); 20191da177e4SLinus Torvalds } 20201da177e4SLinus Torvalds put_write_access(inode); 2021acd0c935SMimi Zohar return error; 20221da177e4SLinus Torvalds } 20231da177e4SLinus Torvalds 2024d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2025d57999e1SDave Hansen { 20268a5e929dSAl Viro if ((flag & O_ACCMODE) == 3) 20278a5e929dSAl Viro flag--; 2028d57999e1SDave Hansen return flag; 2029d57999e1SDave Hansen } 2030d57999e1SDave Hansen 203131e6b01fSNick Piggin /* 2032fe2d35ffSAl Viro * Handle the last step of open() 203331e6b01fSNick Piggin */ 2034fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path, 2035c3e380b0SAl Viro const struct open_flags *op, const char *pathname) 2036fb1cc555SAl Viro { 2037a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 20386c0d46c4SAl Viro struct dentry *dentry; 2039ca344a89SAl Viro int open_flag = op->open_flag; 20406c0d46c4SAl Viro int will_truncate = open_flag & O_TRUNC; 2041ca344a89SAl Viro int want_write = 0; 2042bcda7652SAl Viro int acc_mode = op->acc_mode; 2043fb1cc555SAl Viro struct file *filp; 204416c2cd71SAl Viro int error; 2045fb1cc555SAl Viro 2046c3e380b0SAl Viro nd->flags &= ~LOOKUP_PARENT; 2047c3e380b0SAl Viro nd->flags |= op->intent; 2048c3e380b0SAl Viro 20491f36f774SAl Viro switch (nd->last_type) { 20501f36f774SAl Viro case LAST_DOTDOT: 2051176306f5SNeil Brown case LAST_DOT: 2052fe2d35ffSAl Viro error = handle_dots(nd, nd->last_type); 2053fe2d35ffSAl Viro if (error) 2054fe2d35ffSAl Viro return ERR_PTR(error); 20551f36f774SAl Viro /* fallthrough */ 20561f36f774SAl Viro case LAST_ROOT: 20579f1fafeeSAl Viro error = complete_walk(nd); 205816c2cd71SAl Viro if (error) 20599f1fafeeSAl Viro return ERR_PTR(error); 2060fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2061ca344a89SAl Viro if (open_flag & O_CREAT) { 206216c2cd71SAl Viro error = -EISDIR; 20631f36f774SAl Viro goto exit; 2064fe2d35ffSAl Viro } 2065fe2d35ffSAl Viro goto ok; 20661f36f774SAl Viro case LAST_BIND: 20679f1fafeeSAl Viro error = complete_walk(nd); 206816c2cd71SAl Viro if (error) 20699f1fafeeSAl Viro return ERR_PTR(error); 20701f36f774SAl Viro audit_inode(pathname, dir); 20711f36f774SAl Viro goto ok; 20721f36f774SAl Viro } 2073a2c36b45SAl Viro 2074ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 2075bcda7652SAl Viro int symlink_ok = 0; 2076fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 2077fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2078bcda7652SAl Viro if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) 2079bcda7652SAl Viro symlink_ok = 1; 2080fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 2081ce57dfc1SAl Viro error = walk_component(nd, path, &nd->last, LAST_NORM, 2082ce57dfc1SAl Viro !symlink_ok); 2083ce57dfc1SAl Viro if (error < 0) 2084fe2d35ffSAl Viro return ERR_PTR(error); 2085ce57dfc1SAl Viro if (error) /* symlink */ 2086fe2d35ffSAl Viro return NULL; 2087fe2d35ffSAl Viro /* sayonara */ 20889f1fafeeSAl Viro error = complete_walk(nd); 20899f1fafeeSAl Viro if (error) 2090fe2d35ffSAl Viro return ERR_PTR(-ECHILD); 2091fe2d35ffSAl Viro 2092fe2d35ffSAl Viro error = -ENOTDIR; 2093fe2d35ffSAl Viro if (nd->flags & LOOKUP_DIRECTORY) { 2094ce57dfc1SAl Viro if (!nd->inode->i_op->lookup) 2095fe2d35ffSAl Viro goto exit; 2096fe2d35ffSAl Viro } 2097fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2098fe2d35ffSAl Viro goto ok; 2099fe2d35ffSAl Viro } 2100fe2d35ffSAl Viro 2101fe2d35ffSAl Viro /* create side of things */ 21029f1fafeeSAl Viro error = complete_walk(nd); 21039f1fafeeSAl Viro if (error) 21049f1fafeeSAl Viro return ERR_PTR(error); 2105fe2d35ffSAl Viro 2106fe2d35ffSAl Viro audit_inode(pathname, dir); 210716c2cd71SAl Viro error = -EISDIR; 21081f36f774SAl Viro /* trailing slashes? */ 210931e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 21101f36f774SAl Viro goto exit; 21111f36f774SAl Viro 2112a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 2113a1e28038SAl Viro 21146c0d46c4SAl Viro dentry = lookup_hash(nd); 21156c0d46c4SAl Viro error = PTR_ERR(dentry); 21166c0d46c4SAl Viro if (IS_ERR(dentry)) { 2117fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2118fb1cc555SAl Viro goto exit; 2119fb1cc555SAl Viro } 2120fb1cc555SAl Viro 21216c0d46c4SAl Viro path->dentry = dentry; 21226c0d46c4SAl Viro path->mnt = nd->path.mnt; 21236c0d46c4SAl Viro 2124fb1cc555SAl Viro /* Negative dentry, just create the file */ 21256c0d46c4SAl Viro if (!dentry->d_inode) { 21266c0d46c4SAl Viro int mode = op->mode; 21276c0d46c4SAl Viro if (!IS_POSIXACL(dir->d_inode)) 21286c0d46c4SAl Viro mode &= ~current_umask(); 2129fb1cc555SAl Viro /* 2130fb1cc555SAl Viro * This write is needed to ensure that a 21316c0d46c4SAl Viro * rw->ro transition does not occur between 2132fb1cc555SAl Viro * the time when the file is created and when 2133fb1cc555SAl Viro * a permanent write count is taken through 2134fb1cc555SAl Viro * the 'struct file' in nameidata_to_filp(). 2135fb1cc555SAl Viro */ 2136fb1cc555SAl Viro error = mnt_want_write(nd->path.mnt); 2137fb1cc555SAl Viro if (error) 2138fb1cc555SAl Viro goto exit_mutex_unlock; 2139ca344a89SAl Viro want_write = 1; 21409b44f1b3SAl Viro /* Don't check for write permission, don't truncate */ 2141ca344a89SAl Viro open_flag &= ~O_TRUNC; 21426c0d46c4SAl Viro will_truncate = 0; 2143bcda7652SAl Viro acc_mode = MAY_OPEN; 21446c0d46c4SAl Viro error = security_path_mknod(&nd->path, dentry, mode, 0); 21456c0d46c4SAl Viro if (error) 21466c0d46c4SAl Viro goto exit_mutex_unlock; 21476c0d46c4SAl Viro error = vfs_create(dir->d_inode, dentry, mode, nd); 21486c0d46c4SAl Viro if (error) 21496c0d46c4SAl Viro goto exit_mutex_unlock; 21506c0d46c4SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 21516c0d46c4SAl Viro dput(nd->path.dentry); 21526c0d46c4SAl Viro nd->path.dentry = dentry; 2153ca344a89SAl Viro goto common; 2154fb1cc555SAl Viro } 2155fb1cc555SAl Viro 2156fb1cc555SAl Viro /* 2157fb1cc555SAl Viro * It already exists. 2158fb1cc555SAl Viro */ 2159fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2160fb1cc555SAl Viro audit_inode(pathname, path->dentry); 2161fb1cc555SAl Viro 2162fb1cc555SAl Viro error = -EEXIST; 2163ca344a89SAl Viro if (open_flag & O_EXCL) 2164fb1cc555SAl Viro goto exit_dput; 2165fb1cc555SAl Viro 21669875cf80SDavid Howells error = follow_managed(path, nd->flags); 21679875cf80SDavid Howells if (error < 0) 2168fb1cc555SAl Viro goto exit_dput; 2169fb1cc555SAl Viro 2170fb1cc555SAl Viro error = -ENOENT; 2171fb1cc555SAl Viro if (!path->dentry->d_inode) 2172fb1cc555SAl Viro goto exit_dput; 21739e67f361SAl Viro 21749e67f361SAl Viro if (path->dentry->d_inode->i_op->follow_link) 2175fb1cc555SAl Viro return NULL; 2176fb1cc555SAl Viro 2177fb1cc555SAl Viro path_to_nameidata(path, nd); 217831e6b01fSNick Piggin nd->inode = path->dentry->d_inode; 2179fb1cc555SAl Viro error = -EISDIR; 218031e6b01fSNick Piggin if (S_ISDIR(nd->inode->i_mode)) 2181fb1cc555SAl Viro goto exit; 218267ee3ad2SAl Viro ok: 21836c0d46c4SAl Viro if (!S_ISREG(nd->inode->i_mode)) 21846c0d46c4SAl Viro will_truncate = 0; 21856c0d46c4SAl Viro 21860f9d1a10SAl Viro if (will_truncate) { 21870f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 21880f9d1a10SAl Viro if (error) 21890f9d1a10SAl Viro goto exit; 2190ca344a89SAl Viro want_write = 1; 21910f9d1a10SAl Viro } 2192ca344a89SAl Viro common: 2193bcda7652SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2194ca344a89SAl Viro if (error) 21950f9d1a10SAl Viro goto exit; 21960f9d1a10SAl Viro filp = nameidata_to_filp(nd); 21970f9d1a10SAl Viro if (!IS_ERR(filp)) { 21980f9d1a10SAl Viro error = ima_file_check(filp, op->acc_mode); 21990f9d1a10SAl Viro if (error) { 22000f9d1a10SAl Viro fput(filp); 22010f9d1a10SAl Viro filp = ERR_PTR(error); 22020f9d1a10SAl Viro } 22030f9d1a10SAl Viro } 22040f9d1a10SAl Viro if (!IS_ERR(filp)) { 22050f9d1a10SAl Viro if (will_truncate) { 22060f9d1a10SAl Viro error = handle_truncate(filp); 22070f9d1a10SAl Viro if (error) { 22080f9d1a10SAl Viro fput(filp); 22090f9d1a10SAl Viro filp = ERR_PTR(error); 22100f9d1a10SAl Viro } 22110f9d1a10SAl Viro } 22120f9d1a10SAl Viro } 2213ca344a89SAl Viro out: 2214ca344a89SAl Viro if (want_write) 22150f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 22160f9d1a10SAl Viro path_put(&nd->path); 2217fb1cc555SAl Viro return filp; 2218fb1cc555SAl Viro 2219fb1cc555SAl Viro exit_mutex_unlock: 2220fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2221fb1cc555SAl Viro exit_dput: 2222fb1cc555SAl Viro path_put_conditional(path, nd); 2223fb1cc555SAl Viro exit: 2224ca344a89SAl Viro filp = ERR_PTR(error); 2225ca344a89SAl Viro goto out; 2226fb1cc555SAl Viro } 2227fb1cc555SAl Viro 222813aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname, 222973d049a4SAl Viro struct nameidata *nd, const struct open_flags *op, int flags) 22301da177e4SLinus Torvalds { 2231fe2d35ffSAl Viro struct file *base = NULL; 22324a3fd211SDave Hansen struct file *filp; 22339850c056SAl Viro struct path path; 223413aab428SAl Viro int error; 223531e6b01fSNick Piggin 223631e6b01fSNick Piggin filp = get_empty_filp(); 223731e6b01fSNick Piggin if (!filp) 223831e6b01fSNick Piggin return ERR_PTR(-ENFILE); 223931e6b01fSNick Piggin 224047c805dcSAl Viro filp->f_flags = op->open_flag; 224173d049a4SAl Viro nd->intent.open.file = filp; 224273d049a4SAl Viro nd->intent.open.flags = open_to_namei_flags(op->open_flag); 224373d049a4SAl Viro nd->intent.open.create_mode = op->mode; 224431e6b01fSNick Piggin 224573d049a4SAl Viro error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base); 224631e6b01fSNick Piggin if (unlikely(error)) 224713aab428SAl Viro goto out_filp; 224831e6b01fSNick Piggin 2249fe2d35ffSAl Viro current->total_link_count = 0; 225073d049a4SAl Viro error = link_path_walk(pathname, nd); 225131e6b01fSNick Piggin if (unlikely(error)) 225231e6b01fSNick Piggin goto out_filp; 22531da177e4SLinus Torvalds 225473d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2255806b681cSAl Viro while (unlikely(!filp)) { /* trailing symlink */ 22567b9337aaSNick Piggin struct path link = path; 2257def4af30SAl Viro void *cookie; 2258574197e0SAl Viro if (!(nd->flags & LOOKUP_FOLLOW)) { 225973d049a4SAl Viro path_put_conditional(&path, nd); 226073d049a4SAl Viro path_put(&nd->path); 226140b39136SAl Viro filp = ERR_PTR(-ELOOP); 226240b39136SAl Viro break; 226340b39136SAl Viro } 226473d049a4SAl Viro nd->flags |= LOOKUP_PARENT; 226573d049a4SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 2266574197e0SAl Viro error = follow_link(&link, nd, &cookie); 2267c3e380b0SAl Viro if (unlikely(error)) 2268f1afe9efSAl Viro filp = ERR_PTR(error); 2269c3e380b0SAl Viro else 227073d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2271574197e0SAl Viro put_link(nd, &link, cookie); 2272806b681cSAl Viro } 227310fa8e62SAl Viro out: 227473d049a4SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) 227573d049a4SAl Viro path_put(&nd->root); 2276fe2d35ffSAl Viro if (base) 2277fe2d35ffSAl Viro fput(base); 227873d049a4SAl Viro release_open_intent(nd); 227910fa8e62SAl Viro return filp; 22801da177e4SLinus Torvalds 228131e6b01fSNick Piggin out_filp: 228210fa8e62SAl Viro filp = ERR_PTR(error); 228310fa8e62SAl Viro goto out; 2284de459215SKirill Korotaev } 22851da177e4SLinus Torvalds 228613aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname, 228713aab428SAl Viro const struct open_flags *op, int flags) 228813aab428SAl Viro { 228973d049a4SAl Viro struct nameidata nd; 229013aab428SAl Viro struct file *filp; 229113aab428SAl Viro 229273d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); 229313aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 229473d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags); 229513aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 229673d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); 229713aab428SAl Viro return filp; 229813aab428SAl Viro } 229913aab428SAl Viro 230073d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, 230173d049a4SAl Viro const char *name, const struct open_flags *op, int flags) 230273d049a4SAl Viro { 230373d049a4SAl Viro struct nameidata nd; 230473d049a4SAl Viro struct file *file; 230573d049a4SAl Viro 230673d049a4SAl Viro nd.root.mnt = mnt; 230773d049a4SAl Viro nd.root.dentry = dentry; 230873d049a4SAl Viro 230973d049a4SAl Viro flags |= LOOKUP_ROOT; 231073d049a4SAl Viro 2311bcda7652SAl Viro if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN) 231273d049a4SAl Viro return ERR_PTR(-ELOOP); 231373d049a4SAl Viro 231473d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU); 231573d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 231673d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags); 231773d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 231873d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL); 231973d049a4SAl Viro return file; 232073d049a4SAl Viro } 232173d049a4SAl Viro 2322ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir) 23231da177e4SLinus Torvalds { 2324c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 2325ed75e95dSAl Viro struct nameidata nd; 2326ed75e95dSAl Viro int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd); 2327ed75e95dSAl Viro if (error) 2328ed75e95dSAl Viro return ERR_PTR(error); 23291da177e4SLinus Torvalds 2330c663e5d8SChristoph Hellwig /* 2331c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 2332c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 2333c663e5d8SChristoph Hellwig */ 2334ed75e95dSAl Viro if (nd.last_type != LAST_NORM) 2335ed75e95dSAl Viro goto out; 2336ed75e95dSAl Viro nd.flags &= ~LOOKUP_PARENT; 2337ed75e95dSAl Viro nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL; 2338ed75e95dSAl Viro nd.intent.open.flags = O_EXCL; 2339c663e5d8SChristoph Hellwig 2340c663e5d8SChristoph Hellwig /* 2341c663e5d8SChristoph Hellwig * Do the final lookup. 2342c663e5d8SChristoph Hellwig */ 2343ed75e95dSAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 2344ed75e95dSAl Viro dentry = lookup_hash(&nd); 23451da177e4SLinus Torvalds if (IS_ERR(dentry)) 23461da177e4SLinus Torvalds goto fail; 2347c663e5d8SChristoph Hellwig 2348e9baf6e5SAl Viro if (dentry->d_inode) 2349e9baf6e5SAl Viro goto eexist; 2350c663e5d8SChristoph Hellwig /* 2351c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 2352c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 2353c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 2354c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 2355c663e5d8SChristoph Hellwig */ 2356ed75e95dSAl Viro if (unlikely(!is_dir && nd.last.name[nd.last.len])) { 23571da177e4SLinus Torvalds dput(dentry); 23581da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 2359ed75e95dSAl Viro goto fail; 2360e9baf6e5SAl Viro } 2361ed75e95dSAl Viro *path = nd.path; 2362e9baf6e5SAl Viro return dentry; 2363e9baf6e5SAl Viro eexist: 2364e9baf6e5SAl Viro dput(dentry); 2365e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 23661da177e4SLinus Torvalds fail: 2367dae6ad8fSAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 2368ed75e95dSAl Viro out: 2369dae6ad8fSAl Viro path_put(&nd.path); 2370ed75e95dSAl Viro return dentry; 2371dae6ad8fSAl Viro } 2372dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create); 2373dae6ad8fSAl Viro 2374dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir) 2375dae6ad8fSAl Viro { 2376dae6ad8fSAl Viro char *tmp = getname(pathname); 2377dae6ad8fSAl Viro struct dentry *res; 2378dae6ad8fSAl Viro if (IS_ERR(tmp)) 2379dae6ad8fSAl Viro return ERR_CAST(tmp); 2380dae6ad8fSAl Viro res = kern_path_create(dfd, tmp, path, is_dir); 2381dae6ad8fSAl Viro putname(tmp); 2382dae6ad8fSAl Viro return res; 2383dae6ad8fSAl Viro } 2384dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create); 2385dae6ad8fSAl Viro 23861da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 23871da177e4SLinus Torvalds { 2388a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 23891da177e4SLinus Torvalds 23901da177e4SLinus Torvalds if (error) 23911da177e4SLinus Torvalds return error; 23921da177e4SLinus Torvalds 2393e795b717SSerge E. Hallyn if ((S_ISCHR(mode) || S_ISBLK(mode)) && 2394e795b717SSerge E. Hallyn !ns_capable(inode_userns(dir), CAP_MKNOD)) 23951da177e4SLinus Torvalds return -EPERM; 23961da177e4SLinus Torvalds 2397acfa4380SAl Viro if (!dir->i_op->mknod) 23981da177e4SLinus Torvalds return -EPERM; 23991da177e4SLinus Torvalds 240008ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 240108ce5f16SSerge E. Hallyn if (error) 240208ce5f16SSerge E. Hallyn return error; 240308ce5f16SSerge E. Hallyn 24041da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 24051da177e4SLinus Torvalds if (error) 24061da177e4SLinus Torvalds return error; 24071da177e4SLinus Torvalds 24081da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 2409a74574aaSStephen Smalley if (!error) 2410f38aa942SAmy Griffis fsnotify_create(dir, dentry); 24111da177e4SLinus Torvalds return error; 24121da177e4SLinus Torvalds } 24131da177e4SLinus Torvalds 2414463c3197SDave Hansen static int may_mknod(mode_t mode) 2415463c3197SDave Hansen { 2416463c3197SDave Hansen switch (mode & S_IFMT) { 2417463c3197SDave Hansen case S_IFREG: 2418463c3197SDave Hansen case S_IFCHR: 2419463c3197SDave Hansen case S_IFBLK: 2420463c3197SDave Hansen case S_IFIFO: 2421463c3197SDave Hansen case S_IFSOCK: 2422463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2423463c3197SDave Hansen return 0; 2424463c3197SDave Hansen case S_IFDIR: 2425463c3197SDave Hansen return -EPERM; 2426463c3197SDave Hansen default: 2427463c3197SDave Hansen return -EINVAL; 2428463c3197SDave Hansen } 2429463c3197SDave Hansen } 2430463c3197SDave Hansen 24312e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode, 24322e4d0924SHeiko Carstens unsigned, dev) 24331da177e4SLinus Torvalds { 24341da177e4SLinus Torvalds struct dentry *dentry; 2435dae6ad8fSAl Viro struct path path; 2436dae6ad8fSAl Viro int error; 24371da177e4SLinus Torvalds 24381da177e4SLinus Torvalds if (S_ISDIR(mode)) 24391da177e4SLinus Torvalds return -EPERM; 24401da177e4SLinus Torvalds 2441dae6ad8fSAl Viro dentry = user_path_create(dfd, filename, &path, 0); 2442dae6ad8fSAl Viro if (IS_ERR(dentry)) 2443dae6ad8fSAl Viro return PTR_ERR(dentry); 24442ad94ae6SAl Viro 2445dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 2446ce3b0f8dSAl Viro mode &= ~current_umask(); 2447463c3197SDave Hansen error = may_mknod(mode); 2448463c3197SDave Hansen if (error) 2449463c3197SDave Hansen goto out_dput; 2450dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 2451463c3197SDave Hansen if (error) 2452463c3197SDave Hansen goto out_dput; 2453dae6ad8fSAl Viro error = security_path_mknod(&path, dentry, mode, dev); 2454be6d3e56SKentaro Takeda if (error) 2455be6d3e56SKentaro Takeda goto out_drop_write; 24561da177e4SLinus Torvalds switch (mode & S_IFMT) { 24571da177e4SLinus Torvalds case 0: case S_IFREG: 2458dae6ad8fSAl Viro error = vfs_create(path.dentry->d_inode,dentry,mode,NULL); 24591da177e4SLinus Torvalds break; 24601da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 2461dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode, 24621da177e4SLinus Torvalds new_decode_dev(dev)); 24631da177e4SLinus Torvalds break; 24641da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 2465dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); 24661da177e4SLinus Torvalds break; 24671da177e4SLinus Torvalds } 2468be6d3e56SKentaro Takeda out_drop_write: 2469dae6ad8fSAl Viro mnt_drop_write(path.mnt); 2470463c3197SDave Hansen out_dput: 24711da177e4SLinus Torvalds dput(dentry); 2472dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2473dae6ad8fSAl Viro path_put(&path); 24741da177e4SLinus Torvalds 24751da177e4SLinus Torvalds return error; 24761da177e4SLinus Torvalds } 24771da177e4SLinus Torvalds 24783480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev) 24795590ff0dSUlrich Drepper { 24805590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 24815590ff0dSUlrich Drepper } 24825590ff0dSUlrich Drepper 24831da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 24841da177e4SLinus Torvalds { 2485a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 24861da177e4SLinus Torvalds 24871da177e4SLinus Torvalds if (error) 24881da177e4SLinus Torvalds return error; 24891da177e4SLinus Torvalds 2490acfa4380SAl Viro if (!dir->i_op->mkdir) 24911da177e4SLinus Torvalds return -EPERM; 24921da177e4SLinus Torvalds 24931da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 24941da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 24951da177e4SLinus Torvalds if (error) 24961da177e4SLinus Torvalds return error; 24971da177e4SLinus Torvalds 24981da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2499a74574aaSStephen Smalley if (!error) 2500f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 25011da177e4SLinus Torvalds return error; 25021da177e4SLinus Torvalds } 25031da177e4SLinus Torvalds 25042e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode) 25051da177e4SLinus Torvalds { 25066902d925SDave Hansen struct dentry *dentry; 2507dae6ad8fSAl Viro struct path path; 2508dae6ad8fSAl Viro int error; 25091da177e4SLinus Torvalds 2510dae6ad8fSAl Viro dentry = user_path_create(dfd, pathname, &path, 1); 25116902d925SDave Hansen if (IS_ERR(dentry)) 2512dae6ad8fSAl Viro return PTR_ERR(dentry); 25136902d925SDave Hansen 2514dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 2515ce3b0f8dSAl Viro mode &= ~current_umask(); 2516dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 2517463c3197SDave Hansen if (error) 2518463c3197SDave Hansen goto out_dput; 2519dae6ad8fSAl Viro error = security_path_mkdir(&path, dentry, mode); 2520be6d3e56SKentaro Takeda if (error) 2521be6d3e56SKentaro Takeda goto out_drop_write; 2522dae6ad8fSAl Viro error = vfs_mkdir(path.dentry->d_inode, dentry, mode); 2523be6d3e56SKentaro Takeda out_drop_write: 2524dae6ad8fSAl Viro mnt_drop_write(path.mnt); 2525463c3197SDave Hansen out_dput: 25261da177e4SLinus Torvalds dput(dentry); 2527dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2528dae6ad8fSAl Viro path_put(&path); 25291da177e4SLinus Torvalds return error; 25301da177e4SLinus Torvalds } 25311da177e4SLinus Torvalds 25323cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode) 25335590ff0dSUlrich Drepper { 25345590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 25355590ff0dSUlrich Drepper } 25365590ff0dSUlrich Drepper 25371da177e4SLinus Torvalds /* 2538a71905f0SSage Weil * The dentry_unhash() helper will try to drop the dentry early: we 2539a71905f0SSage Weil * should have a usage count of 2 if we're the only user of this 2540a71905f0SSage Weil * dentry, and if that is true (possibly after pruning the dcache), 2541a71905f0SSage Weil * then we drop the dentry now. 25421da177e4SLinus Torvalds * 25431da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 25441da177e4SLinus Torvalds * do a 25451da177e4SLinus Torvalds * 25461da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 25471da177e4SLinus Torvalds * return -EBUSY; 25481da177e4SLinus Torvalds * 25491da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 25501da177e4SLinus Torvalds * that is still in use by something else.. 25511da177e4SLinus Torvalds */ 25521da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 25531da177e4SLinus Torvalds { 25541da177e4SLinus Torvalds shrink_dcache_parent(dentry); 25551da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 255664252c75SSage Weil if (dentry->d_count == 1) 25571da177e4SLinus Torvalds __d_drop(dentry); 25581da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 25591da177e4SLinus Torvalds } 25601da177e4SLinus Torvalds 25611da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 25621da177e4SLinus Torvalds { 25631da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 25641da177e4SLinus Torvalds 25651da177e4SLinus Torvalds if (error) 25661da177e4SLinus Torvalds return error; 25671da177e4SLinus Torvalds 2568acfa4380SAl Viro if (!dir->i_op->rmdir) 25691da177e4SLinus Torvalds return -EPERM; 25701da177e4SLinus Torvalds 25711b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 2572912dbc15SSage Weil 25731da177e4SLinus Torvalds error = -EBUSY; 2574912dbc15SSage Weil if (d_mountpoint(dentry)) 2575912dbc15SSage Weil goto out; 2576912dbc15SSage Weil 25771da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 2578912dbc15SSage Weil if (error) 2579912dbc15SSage Weil goto out; 2580912dbc15SSage Weil 25813cebde24SSage Weil shrink_dcache_parent(dentry); 25821da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 2583912dbc15SSage Weil if (error) 2584912dbc15SSage Weil goto out; 2585912dbc15SSage Weil 25861da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 2587d83c49f3SAl Viro dont_mount(dentry); 25881da177e4SLinus Torvalds 2589912dbc15SSage Weil out: 2590912dbc15SSage Weil mutex_unlock(&dentry->d_inode->i_mutex); 2591912dbc15SSage Weil if (!error) 2592912dbc15SSage Weil d_delete(dentry); 25931da177e4SLinus Torvalds return error; 25941da177e4SLinus Torvalds } 25951da177e4SLinus Torvalds 25965590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 25971da177e4SLinus Torvalds { 25981da177e4SLinus Torvalds int error = 0; 25991da177e4SLinus Torvalds char * name; 26001da177e4SLinus Torvalds struct dentry *dentry; 26011da177e4SLinus Torvalds struct nameidata nd; 26021da177e4SLinus Torvalds 26032ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 26041da177e4SLinus Torvalds if (error) 26052ad94ae6SAl Viro return error; 26061da177e4SLinus Torvalds 26071da177e4SLinus Torvalds switch(nd.last_type) { 26081da177e4SLinus Torvalds case LAST_DOTDOT: 26091da177e4SLinus Torvalds error = -ENOTEMPTY; 26101da177e4SLinus Torvalds goto exit1; 26111da177e4SLinus Torvalds case LAST_DOT: 26121da177e4SLinus Torvalds error = -EINVAL; 26131da177e4SLinus Torvalds goto exit1; 26141da177e4SLinus Torvalds case LAST_ROOT: 26151da177e4SLinus Torvalds error = -EBUSY; 26161da177e4SLinus Torvalds goto exit1; 26171da177e4SLinus Torvalds } 26180612d9fbSOGAWA Hirofumi 26190612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 26200612d9fbSOGAWA Hirofumi 26214ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 262249705b77SChristoph Hellwig dentry = lookup_hash(&nd); 26231da177e4SLinus Torvalds error = PTR_ERR(dentry); 26246902d925SDave Hansen if (IS_ERR(dentry)) 26256902d925SDave Hansen goto exit2; 2626e6bc45d6STheodore Ts'o if (!dentry->d_inode) { 2627e6bc45d6STheodore Ts'o error = -ENOENT; 2628e6bc45d6STheodore Ts'o goto exit3; 2629e6bc45d6STheodore Ts'o } 26300622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 26310622753bSDave Hansen if (error) 26320622753bSDave Hansen goto exit3; 2633be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 2634be6d3e56SKentaro Takeda if (error) 2635be6d3e56SKentaro Takeda goto exit4; 26364ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 2637be6d3e56SKentaro Takeda exit4: 26380622753bSDave Hansen mnt_drop_write(nd.path.mnt); 26390622753bSDave Hansen exit3: 26401da177e4SLinus Torvalds dput(dentry); 26416902d925SDave Hansen exit2: 26424ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 26431da177e4SLinus Torvalds exit1: 26441d957f9bSJan Blunck path_put(&nd.path); 26451da177e4SLinus Torvalds putname(name); 26461da177e4SLinus Torvalds return error; 26471da177e4SLinus Torvalds } 26481da177e4SLinus Torvalds 26493cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 26505590ff0dSUlrich Drepper { 26515590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 26525590ff0dSUlrich Drepper } 26535590ff0dSUlrich Drepper 26541da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 26551da177e4SLinus Torvalds { 26561da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 26571da177e4SLinus Torvalds 26581da177e4SLinus Torvalds if (error) 26591da177e4SLinus Torvalds return error; 26601da177e4SLinus Torvalds 2661acfa4380SAl Viro if (!dir->i_op->unlink) 26621da177e4SLinus Torvalds return -EPERM; 26631da177e4SLinus Torvalds 26641b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 26651da177e4SLinus Torvalds if (d_mountpoint(dentry)) 26661da177e4SLinus Torvalds error = -EBUSY; 26671da177e4SLinus Torvalds else { 26681da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 2669bec1052eSAl Viro if (!error) { 26701da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 2671bec1052eSAl Viro if (!error) 2672d83c49f3SAl Viro dont_mount(dentry); 2673bec1052eSAl Viro } 26741da177e4SLinus Torvalds } 26751b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 26761da177e4SLinus Torvalds 26771da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 26781da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 2679ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 26801da177e4SLinus Torvalds d_delete(dentry); 26811da177e4SLinus Torvalds } 26820eeca283SRobert Love 26831da177e4SLinus Torvalds return error; 26841da177e4SLinus Torvalds } 26851da177e4SLinus Torvalds 26861da177e4SLinus Torvalds /* 26871da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 26881b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 26891da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 26901da177e4SLinus Torvalds * while waiting on the I/O. 26911da177e4SLinus Torvalds */ 26925590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 26931da177e4SLinus Torvalds { 26942ad94ae6SAl Viro int error; 26951da177e4SLinus Torvalds char *name; 26961da177e4SLinus Torvalds struct dentry *dentry; 26971da177e4SLinus Torvalds struct nameidata nd; 26981da177e4SLinus Torvalds struct inode *inode = NULL; 26991da177e4SLinus Torvalds 27002ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 27011da177e4SLinus Torvalds if (error) 27022ad94ae6SAl Viro return error; 27032ad94ae6SAl Viro 27041da177e4SLinus Torvalds error = -EISDIR; 27051da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 27061da177e4SLinus Torvalds goto exit1; 27070612d9fbSOGAWA Hirofumi 27080612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 27090612d9fbSOGAWA Hirofumi 27104ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 271149705b77SChristoph Hellwig dentry = lookup_hash(&nd); 27121da177e4SLinus Torvalds error = PTR_ERR(dentry); 27131da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 27141da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 271550338b88STörök Edwin if (nd.last.name[nd.last.len]) 271650338b88STörök Edwin goto slashes; 27171da177e4SLinus Torvalds inode = dentry->d_inode; 271850338b88STörök Edwin if (!inode) 2719e6bc45d6STheodore Ts'o goto slashes; 27207de9c6eeSAl Viro ihold(inode); 27210622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 27220622753bSDave Hansen if (error) 27230622753bSDave Hansen goto exit2; 2724be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 2725be6d3e56SKentaro Takeda if (error) 2726be6d3e56SKentaro Takeda goto exit3; 27274ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 2728be6d3e56SKentaro Takeda exit3: 27290622753bSDave Hansen mnt_drop_write(nd.path.mnt); 27301da177e4SLinus Torvalds exit2: 27311da177e4SLinus Torvalds dput(dentry); 27321da177e4SLinus Torvalds } 27334ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 27341da177e4SLinus Torvalds if (inode) 27351da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 27361da177e4SLinus Torvalds exit1: 27371d957f9bSJan Blunck path_put(&nd.path); 27381da177e4SLinus Torvalds putname(name); 27391da177e4SLinus Torvalds return error; 27401da177e4SLinus Torvalds 27411da177e4SLinus Torvalds slashes: 27421da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 27431da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 27441da177e4SLinus Torvalds goto exit2; 27451da177e4SLinus Torvalds } 27461da177e4SLinus Torvalds 27472e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 27485590ff0dSUlrich Drepper { 27495590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 27505590ff0dSUlrich Drepper return -EINVAL; 27515590ff0dSUlrich Drepper 27525590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 27535590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 27545590ff0dSUlrich Drepper 27555590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 27565590ff0dSUlrich Drepper } 27575590ff0dSUlrich Drepper 27583480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 27595590ff0dSUlrich Drepper { 27605590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 27615590ff0dSUlrich Drepper } 27625590ff0dSUlrich Drepper 2763db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 27641da177e4SLinus Torvalds { 2765a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 27661da177e4SLinus Torvalds 27671da177e4SLinus Torvalds if (error) 27681da177e4SLinus Torvalds return error; 27691da177e4SLinus Torvalds 2770acfa4380SAl Viro if (!dir->i_op->symlink) 27711da177e4SLinus Torvalds return -EPERM; 27721da177e4SLinus Torvalds 27731da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 27741da177e4SLinus Torvalds if (error) 27751da177e4SLinus Torvalds return error; 27761da177e4SLinus Torvalds 27771da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 2778a74574aaSStephen Smalley if (!error) 2779f38aa942SAmy Griffis fsnotify_create(dir, dentry); 27801da177e4SLinus Torvalds return error; 27811da177e4SLinus Torvalds } 27821da177e4SLinus Torvalds 27832e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 27842e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 27851da177e4SLinus Torvalds { 27862ad94ae6SAl Viro int error; 27871da177e4SLinus Torvalds char *from; 27886902d925SDave Hansen struct dentry *dentry; 2789dae6ad8fSAl Viro struct path path; 27901da177e4SLinus Torvalds 27911da177e4SLinus Torvalds from = getname(oldname); 27921da177e4SLinus Torvalds if (IS_ERR(from)) 27931da177e4SLinus Torvalds return PTR_ERR(from); 27942ad94ae6SAl Viro 2795dae6ad8fSAl Viro dentry = user_path_create(newdfd, newname, &path, 0); 27961da177e4SLinus Torvalds error = PTR_ERR(dentry); 27976902d925SDave Hansen if (IS_ERR(dentry)) 2798dae6ad8fSAl Viro goto out_putname; 27996902d925SDave Hansen 2800dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 280175c3f29dSDave Hansen if (error) 280275c3f29dSDave Hansen goto out_dput; 2803dae6ad8fSAl Viro error = security_path_symlink(&path, dentry, from); 2804be6d3e56SKentaro Takeda if (error) 2805be6d3e56SKentaro Takeda goto out_drop_write; 2806dae6ad8fSAl Viro error = vfs_symlink(path.dentry->d_inode, dentry, from); 2807be6d3e56SKentaro Takeda out_drop_write: 2808dae6ad8fSAl Viro mnt_drop_write(path.mnt); 280975c3f29dSDave Hansen out_dput: 28101da177e4SLinus Torvalds dput(dentry); 2811dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2812dae6ad8fSAl Viro path_put(&path); 28136902d925SDave Hansen out_putname: 28141da177e4SLinus Torvalds putname(from); 28151da177e4SLinus Torvalds return error; 28161da177e4SLinus Torvalds } 28171da177e4SLinus Torvalds 28183480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 28195590ff0dSUlrich Drepper { 28205590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 28215590ff0dSUlrich Drepper } 28225590ff0dSUlrich Drepper 28231da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 28241da177e4SLinus Torvalds { 28251da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 28261da177e4SLinus Torvalds int error; 28271da177e4SLinus Torvalds 28281da177e4SLinus Torvalds if (!inode) 28291da177e4SLinus Torvalds return -ENOENT; 28301da177e4SLinus Torvalds 2831a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 28321da177e4SLinus Torvalds if (error) 28331da177e4SLinus Torvalds return error; 28341da177e4SLinus Torvalds 28351da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 28361da177e4SLinus Torvalds return -EXDEV; 28371da177e4SLinus Torvalds 28381da177e4SLinus Torvalds /* 28391da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 28401da177e4SLinus Torvalds */ 28411da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 28421da177e4SLinus Torvalds return -EPERM; 2843acfa4380SAl Viro if (!dir->i_op->link) 28441da177e4SLinus Torvalds return -EPERM; 28457e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 28461da177e4SLinus Torvalds return -EPERM; 28471da177e4SLinus Torvalds 28481da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 28491da177e4SLinus Torvalds if (error) 28501da177e4SLinus Torvalds return error; 28511da177e4SLinus Torvalds 28527e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 2853aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 2854aae8a97dSAneesh Kumar K.V if (inode->i_nlink == 0) 2855aae8a97dSAneesh Kumar K.V error = -ENOENT; 2856aae8a97dSAneesh Kumar K.V else 28571da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 28587e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 2859e31e14ecSStephen Smalley if (!error) 28607e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 28611da177e4SLinus Torvalds return error; 28621da177e4SLinus Torvalds } 28631da177e4SLinus Torvalds 28641da177e4SLinus Torvalds /* 28651da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 28661da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 28671da177e4SLinus Torvalds * newname. --KAB 28681da177e4SLinus Torvalds * 28691da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 28701da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 28711da177e4SLinus Torvalds * and other special files. --ADM 28721da177e4SLinus Torvalds */ 28732e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 28742e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 28751da177e4SLinus Torvalds { 28761da177e4SLinus Torvalds struct dentry *new_dentry; 2877dae6ad8fSAl Viro struct path old_path, new_path; 287811a7b371SAneesh Kumar K.V int how = 0; 28791da177e4SLinus Torvalds int error; 28801da177e4SLinus Torvalds 288111a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 2882c04030e1SUlrich Drepper return -EINVAL; 288311a7b371SAneesh Kumar K.V /* 288411a7b371SAneesh Kumar K.V * To use null names we require CAP_DAC_READ_SEARCH 288511a7b371SAneesh Kumar K.V * This ensures that not everyone will be able to create 288611a7b371SAneesh Kumar K.V * handlink using the passed filedescriptor. 288711a7b371SAneesh Kumar K.V */ 288811a7b371SAneesh Kumar K.V if (flags & AT_EMPTY_PATH) { 288911a7b371SAneesh Kumar K.V if (!capable(CAP_DAC_READ_SEARCH)) 289011a7b371SAneesh Kumar K.V return -ENOENT; 289111a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 289211a7b371SAneesh Kumar K.V } 2893c04030e1SUlrich Drepper 289411a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 289511a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 289611a7b371SAneesh Kumar K.V 289711a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 28981da177e4SLinus Torvalds if (error) 28992ad94ae6SAl Viro return error; 29002ad94ae6SAl Viro 2901dae6ad8fSAl Viro new_dentry = user_path_create(newdfd, newname, &new_path, 0); 29021da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 29036902d925SDave Hansen if (IS_ERR(new_dentry)) 2904dae6ad8fSAl Viro goto out; 2905dae6ad8fSAl Viro 2906dae6ad8fSAl Viro error = -EXDEV; 2907dae6ad8fSAl Viro if (old_path.mnt != new_path.mnt) 2908dae6ad8fSAl Viro goto out_dput; 2909dae6ad8fSAl Viro error = mnt_want_write(new_path.mnt); 291075c3f29dSDave Hansen if (error) 291175c3f29dSDave Hansen goto out_dput; 2912dae6ad8fSAl Viro error = security_path_link(old_path.dentry, &new_path, new_dentry); 2913be6d3e56SKentaro Takeda if (error) 2914be6d3e56SKentaro Takeda goto out_drop_write; 2915dae6ad8fSAl Viro error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry); 2916be6d3e56SKentaro Takeda out_drop_write: 2917dae6ad8fSAl Viro mnt_drop_write(new_path.mnt); 291875c3f29dSDave Hansen out_dput: 29191da177e4SLinus Torvalds dput(new_dentry); 2920dae6ad8fSAl Viro mutex_unlock(&new_path.dentry->d_inode->i_mutex); 2921dae6ad8fSAl Viro path_put(&new_path); 29221da177e4SLinus Torvalds out: 29232d8f3038SAl Viro path_put(&old_path); 29241da177e4SLinus Torvalds 29251da177e4SLinus Torvalds return error; 29261da177e4SLinus Torvalds } 29271da177e4SLinus Torvalds 29283480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 29295590ff0dSUlrich Drepper { 2930c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 29315590ff0dSUlrich Drepper } 29325590ff0dSUlrich Drepper 29331da177e4SLinus Torvalds /* 29341da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 29351da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 29361da177e4SLinus Torvalds * Problems: 29371da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 29381da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 29391da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 2940a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 29411da177e4SLinus Torvalds * story. 29421da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 29431b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 29441da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 29451da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 2946a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 29471da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 29481da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 29491da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 2950a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 29511da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 29521da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 29531da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 2954e4eaac06SSage Weil * d) conversion from fhandle to dentry may come in the wrong moment - when 29551b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 29561da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 2957c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 29581da177e4SLinus Torvalds * locking]. 29591da177e4SLinus Torvalds */ 296075c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 29611da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 29621da177e4SLinus Torvalds { 29631da177e4SLinus Torvalds int error = 0; 29649055cba7SSage Weil struct inode *target = new_dentry->d_inode; 29651da177e4SLinus Torvalds 29661da177e4SLinus Torvalds /* 29671da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 29681da177e4SLinus Torvalds * we'll need to flip '..'. 29691da177e4SLinus Torvalds */ 29701da177e4SLinus Torvalds if (new_dir != old_dir) { 2971f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 29721da177e4SLinus Torvalds if (error) 29731da177e4SLinus Torvalds return error; 29741da177e4SLinus Torvalds } 29751da177e4SLinus Torvalds 29761da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 29771da177e4SLinus Torvalds if (error) 29781da177e4SLinus Torvalds return error; 29791da177e4SLinus Torvalds 2980d83c49f3SAl Viro if (target) 29811b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 29829055cba7SSage Weil 29831da177e4SLinus Torvalds error = -EBUSY; 29849055cba7SSage Weil if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) 29859055cba7SSage Weil goto out; 29869055cba7SSage Weil 29873cebde24SSage Weil if (target) 29883cebde24SSage Weil shrink_dcache_parent(new_dentry); 29891da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 29909055cba7SSage Weil if (error) 29919055cba7SSage Weil goto out; 29929055cba7SSage Weil 29931da177e4SLinus Torvalds if (target) { 29941da177e4SLinus Torvalds target->i_flags |= S_DEAD; 2995d83c49f3SAl Viro dont_mount(new_dentry); 2996d83c49f3SAl Viro } 29979055cba7SSage Weil out: 29989055cba7SSage Weil if (target) 29991b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 3000e31e14ecSStephen Smalley if (!error) 3001349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 30021da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 30031da177e4SLinus Torvalds return error; 30041da177e4SLinus Torvalds } 30051da177e4SLinus Torvalds 300675c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 30071da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30081da177e4SLinus Torvalds { 300951892bbbSSage Weil struct inode *target = new_dentry->d_inode; 30101da177e4SLinus Torvalds int error; 30111da177e4SLinus Torvalds 30121da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 30131da177e4SLinus Torvalds if (error) 30141da177e4SLinus Torvalds return error; 30151da177e4SLinus Torvalds 30161da177e4SLinus Torvalds dget(new_dentry); 30171da177e4SLinus Torvalds if (target) 30181b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 301951892bbbSSage Weil 30201da177e4SLinus Torvalds error = -EBUSY; 302151892bbbSSage Weil if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 302251892bbbSSage Weil goto out; 302351892bbbSSage Weil 30241da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 302551892bbbSSage Weil if (error) 302651892bbbSSage Weil goto out; 302751892bbbSSage Weil 3028bec1052eSAl Viro if (target) 3029d83c49f3SAl Viro dont_mount(new_dentry); 3030349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 30311da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 303251892bbbSSage Weil out: 30331da177e4SLinus Torvalds if (target) 30341b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 30351da177e4SLinus Torvalds dput(new_dentry); 30361da177e4SLinus Torvalds return error; 30371da177e4SLinus Torvalds } 30381da177e4SLinus Torvalds 30391da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 30401da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30411da177e4SLinus Torvalds { 30421da177e4SLinus Torvalds int error; 30431da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 304459b0df21SEric Paris const unsigned char *old_name; 30451da177e4SLinus Torvalds 30461da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 30471da177e4SLinus Torvalds return 0; 30481da177e4SLinus Torvalds 30491da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 30501da177e4SLinus Torvalds if (error) 30511da177e4SLinus Torvalds return error; 30521da177e4SLinus Torvalds 30531da177e4SLinus Torvalds if (!new_dentry->d_inode) 3054a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 30551da177e4SLinus Torvalds else 30561da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 30571da177e4SLinus Torvalds if (error) 30581da177e4SLinus Torvalds return error; 30591da177e4SLinus Torvalds 3060acfa4380SAl Viro if (!old_dir->i_op->rename) 30611da177e4SLinus Torvalds return -EPERM; 30621da177e4SLinus Torvalds 30630eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 30640eeca283SRobert Love 30651da177e4SLinus Torvalds if (is_dir) 30661da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 30671da177e4SLinus Torvalds else 30681da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3069123df294SAl Viro if (!error) 3070123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 30715a190ae6SAl Viro new_dentry->d_inode, old_dentry); 30720eeca283SRobert Love fsnotify_oldname_free(old_name); 30730eeca283SRobert Love 30741da177e4SLinus Torvalds return error; 30751da177e4SLinus Torvalds } 30761da177e4SLinus Torvalds 30772e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 30782e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 30791da177e4SLinus Torvalds { 30801da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 30811da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 30821da177e4SLinus Torvalds struct dentry *trap; 30831da177e4SLinus Torvalds struct nameidata oldnd, newnd; 30842ad94ae6SAl Viro char *from; 30852ad94ae6SAl Viro char *to; 30862ad94ae6SAl Viro int error; 30871da177e4SLinus Torvalds 30882ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 30891da177e4SLinus Torvalds if (error) 30901da177e4SLinus Torvalds goto exit; 30911da177e4SLinus Torvalds 30922ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 30931da177e4SLinus Torvalds if (error) 30941da177e4SLinus Torvalds goto exit1; 30951da177e4SLinus Torvalds 30961da177e4SLinus Torvalds error = -EXDEV; 30974ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 30981da177e4SLinus Torvalds goto exit2; 30991da177e4SLinus Torvalds 31004ac91378SJan Blunck old_dir = oldnd.path.dentry; 31011da177e4SLinus Torvalds error = -EBUSY; 31021da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 31031da177e4SLinus Torvalds goto exit2; 31041da177e4SLinus Torvalds 31054ac91378SJan Blunck new_dir = newnd.path.dentry; 31061da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 31071da177e4SLinus Torvalds goto exit2; 31081da177e4SLinus Torvalds 31090612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 31100612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 31114e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 31120612d9fbSOGAWA Hirofumi 31131da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 31141da177e4SLinus Torvalds 311549705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 31161da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 31171da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 31181da177e4SLinus Torvalds goto exit3; 31191da177e4SLinus Torvalds /* source must exist */ 31201da177e4SLinus Torvalds error = -ENOENT; 31211da177e4SLinus Torvalds if (!old_dentry->d_inode) 31221da177e4SLinus Torvalds goto exit4; 31231da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 31241da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 31251da177e4SLinus Torvalds error = -ENOTDIR; 31261da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 31271da177e4SLinus Torvalds goto exit4; 31281da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 31291da177e4SLinus Torvalds goto exit4; 31301da177e4SLinus Torvalds } 31311da177e4SLinus Torvalds /* source should not be ancestor of target */ 31321da177e4SLinus Torvalds error = -EINVAL; 31331da177e4SLinus Torvalds if (old_dentry == trap) 31341da177e4SLinus Torvalds goto exit4; 313549705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 31361da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 31371da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 31381da177e4SLinus Torvalds goto exit4; 31391da177e4SLinus Torvalds /* target should not be an ancestor of source */ 31401da177e4SLinus Torvalds error = -ENOTEMPTY; 31411da177e4SLinus Torvalds if (new_dentry == trap) 31421da177e4SLinus Torvalds goto exit5; 31431da177e4SLinus Torvalds 31449079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 31459079b1ebSDave Hansen if (error) 31469079b1ebSDave Hansen goto exit5; 3147be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3148be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3149be6d3e56SKentaro Takeda if (error) 3150be6d3e56SKentaro Takeda goto exit6; 31511da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 31521da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 3153be6d3e56SKentaro Takeda exit6: 31549079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 31551da177e4SLinus Torvalds exit5: 31561da177e4SLinus Torvalds dput(new_dentry); 31571da177e4SLinus Torvalds exit4: 31581da177e4SLinus Torvalds dput(old_dentry); 31591da177e4SLinus Torvalds exit3: 31601da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 31611da177e4SLinus Torvalds exit2: 31621d957f9bSJan Blunck path_put(&newnd.path); 31632ad94ae6SAl Viro putname(to); 31641da177e4SLinus Torvalds exit1: 31651d957f9bSJan Blunck path_put(&oldnd.path); 31661da177e4SLinus Torvalds putname(from); 31672ad94ae6SAl Viro exit: 31681da177e4SLinus Torvalds return error; 31691da177e4SLinus Torvalds } 31701da177e4SLinus Torvalds 3171a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 31725590ff0dSUlrich Drepper { 31735590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 31745590ff0dSUlrich Drepper } 31755590ff0dSUlrich Drepper 31761da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 31771da177e4SLinus Torvalds { 31781da177e4SLinus Torvalds int len; 31791da177e4SLinus Torvalds 31801da177e4SLinus Torvalds len = PTR_ERR(link); 31811da177e4SLinus Torvalds if (IS_ERR(link)) 31821da177e4SLinus Torvalds goto out; 31831da177e4SLinus Torvalds 31841da177e4SLinus Torvalds len = strlen(link); 31851da177e4SLinus Torvalds if (len > (unsigned) buflen) 31861da177e4SLinus Torvalds len = buflen; 31871da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 31881da177e4SLinus Torvalds len = -EFAULT; 31891da177e4SLinus Torvalds out: 31901da177e4SLinus Torvalds return len; 31911da177e4SLinus Torvalds } 31921da177e4SLinus Torvalds 31931da177e4SLinus Torvalds /* 31941da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 31951da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 31961da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 31971da177e4SLinus Torvalds */ 31981da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 31991da177e4SLinus Torvalds { 32001da177e4SLinus Torvalds struct nameidata nd; 3201cc314eefSLinus Torvalds void *cookie; 3202694a1764SMarcin Slusarz int res; 3203cc314eefSLinus Torvalds 32041da177e4SLinus Torvalds nd.depth = 0; 3205cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3206694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3207694a1764SMarcin Slusarz return PTR_ERR(cookie); 3208694a1764SMarcin Slusarz 3209694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 32101da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3211cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3212694a1764SMarcin Slusarz return res; 32131da177e4SLinus Torvalds } 32141da177e4SLinus Torvalds 32151da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 32161da177e4SLinus Torvalds { 32171da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 32181da177e4SLinus Torvalds } 32191da177e4SLinus Torvalds 32201da177e4SLinus Torvalds /* get the link contents into pagecache */ 32211da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 32221da177e4SLinus Torvalds { 3223ebd09abbSDuane Griffin char *kaddr; 32241da177e4SLinus Torvalds struct page *page; 32251da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3226090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 32271da177e4SLinus Torvalds if (IS_ERR(page)) 32286fe6900eSNick Piggin return (char*)page; 32291da177e4SLinus Torvalds *ppage = page; 3230ebd09abbSDuane Griffin kaddr = kmap(page); 3231ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3232ebd09abbSDuane Griffin return kaddr; 32331da177e4SLinus Torvalds } 32341da177e4SLinus Torvalds 32351da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 32361da177e4SLinus Torvalds { 32371da177e4SLinus Torvalds struct page *page = NULL; 32381da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 32391da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 32401da177e4SLinus Torvalds if (page) { 32411da177e4SLinus Torvalds kunmap(page); 32421da177e4SLinus Torvalds page_cache_release(page); 32431da177e4SLinus Torvalds } 32441da177e4SLinus Torvalds return res; 32451da177e4SLinus Torvalds } 32461da177e4SLinus Torvalds 3247cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 32481da177e4SLinus Torvalds { 3249cc314eefSLinus Torvalds struct page *page = NULL; 32501da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 3251cc314eefSLinus Torvalds return page; 32521da177e4SLinus Torvalds } 32531da177e4SLinus Torvalds 3254cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 32551da177e4SLinus Torvalds { 3256cc314eefSLinus Torvalds struct page *page = cookie; 3257cc314eefSLinus Torvalds 3258cc314eefSLinus Torvalds if (page) { 32591da177e4SLinus Torvalds kunmap(page); 32601da177e4SLinus Torvalds page_cache_release(page); 32611da177e4SLinus Torvalds } 32621da177e4SLinus Torvalds } 32631da177e4SLinus Torvalds 326454566b2cSNick Piggin /* 326554566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 326654566b2cSNick Piggin */ 326754566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 32681da177e4SLinus Torvalds { 32691da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 32700adb25d2SKirill Korotaev struct page *page; 3271afddba49SNick Piggin void *fsdata; 3272beb497abSDmitriy Monakhov int err; 32731da177e4SLinus Torvalds char *kaddr; 327454566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 327554566b2cSNick Piggin if (nofs) 327654566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 32771da177e4SLinus Torvalds 32787e53cac4SNeilBrown retry: 3279afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 328054566b2cSNick Piggin flags, &page, &fsdata); 32811da177e4SLinus Torvalds if (err) 3282afddba49SNick Piggin goto fail; 3283afddba49SNick Piggin 32841da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 32851da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 32861da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 3287afddba49SNick Piggin 3288afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 3289afddba49SNick Piggin page, fsdata); 32901da177e4SLinus Torvalds if (err < 0) 32911da177e4SLinus Torvalds goto fail; 3292afddba49SNick Piggin if (err < len-1) 3293afddba49SNick Piggin goto retry; 3294afddba49SNick Piggin 32951da177e4SLinus Torvalds mark_inode_dirty(inode); 32961da177e4SLinus Torvalds return 0; 32971da177e4SLinus Torvalds fail: 32981da177e4SLinus Torvalds return err; 32991da177e4SLinus Torvalds } 33001da177e4SLinus Torvalds 33010adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 33020adb25d2SKirill Korotaev { 33030adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 330454566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 33050adb25d2SKirill Korotaev } 33060adb25d2SKirill Korotaev 330792e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 33081da177e4SLinus Torvalds .readlink = generic_readlink, 33091da177e4SLinus Torvalds .follow_link = page_follow_link_light, 33101da177e4SLinus Torvalds .put_link = page_put_link, 33111da177e4SLinus Torvalds }; 33121da177e4SLinus Torvalds 33132d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 3314cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one); 33151da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 33161da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 33171da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 33181da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 33191da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 33201da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 33211da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 33221da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 33231da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 33240adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 33251da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 33261da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 3327d1811465SAl Viro EXPORT_SYMBOL(kern_path); 332816f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 3329f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 33301da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 33311da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 33321da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 33331da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 33341da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 33351da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 33361da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 33371da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 33381da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 33391da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 33401da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 33411da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 33421da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 33431da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 3344