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 { 179e77819e5SLinus Torvalds struct posix_acl *acl; 180e77819e5SLinus Torvalds 181e77819e5SLinus Torvalds /* 182e77819e5SLinus Torvalds * Under RCU walk, we cannot even do a "get_cached_acl()", 183e77819e5SLinus Torvalds * because that involves locking and getting a refcount on 184e77819e5SLinus Torvalds * a cached ACL. 185e77819e5SLinus Torvalds * 186e77819e5SLinus Torvalds * So the only case we handle during RCU walking is the 187e77819e5SLinus Torvalds * case of a cached "no ACL at all", which needs no locks 188e77819e5SLinus Torvalds * or refcounts. 189e77819e5SLinus Torvalds */ 190e77819e5SLinus Torvalds if (mask & MAY_NOT_BLOCK) { 191e77819e5SLinus Torvalds if (negative_cached_acl(inode, ACL_TYPE_ACCESS)) 192e77819e5SLinus Torvalds return -EAGAIN; 193e77819e5SLinus Torvalds return -ECHILD; 194e77819e5SLinus Torvalds } 195e77819e5SLinus Torvalds 196e77819e5SLinus Torvalds acl = get_cached_acl(inode, ACL_TYPE_ACCESS); 197e77819e5SLinus Torvalds 198e77819e5SLinus Torvalds /* 1994e34e719SChristoph Hellwig * A filesystem can force a ACL callback by just never filling the 2004e34e719SChristoph Hellwig * ACL cache. But normally you'd fill the cache either at inode 2014e34e719SChristoph Hellwig * instantiation time, or on the first ->get_acl call. 202e77819e5SLinus Torvalds * 2034e34e719SChristoph Hellwig * If the filesystem doesn't have a get_acl() function at all, we'll 2044e34e719SChristoph Hellwig * just create the negative cache entry. 205e77819e5SLinus Torvalds */ 206e77819e5SLinus Torvalds if (acl == ACL_NOT_CACHED) { 2074e34e719SChristoph Hellwig if (inode->i_op->get_acl) { 2084e34e719SChristoph Hellwig acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS); 2094e34e719SChristoph Hellwig if (IS_ERR(acl)) 2104e34e719SChristoph Hellwig return PTR_ERR(acl); 2114e34e719SChristoph Hellwig } else { 212e77819e5SLinus Torvalds set_cached_acl(inode, ACL_TYPE_ACCESS, NULL); 213e77819e5SLinus Torvalds return -EAGAIN; 214e77819e5SLinus Torvalds } 2154e34e719SChristoph Hellwig } 216e77819e5SLinus Torvalds 217e77819e5SLinus Torvalds if (acl) { 218e77819e5SLinus Torvalds int error = posix_acl_permission(inode, acl, mask); 219e77819e5SLinus Torvalds posix_acl_release(acl); 220e77819e5SLinus Torvalds return error; 221e77819e5SLinus Torvalds } 222e77819e5SLinus Torvalds 223e77819e5SLinus Torvalds return -EAGAIN; 224e77819e5SLinus Torvalds } 225e77819e5SLinus Torvalds 2265909ccaaSLinus Torvalds /* 2275909ccaaSLinus Torvalds * This does basic POSIX ACL permission checking 2285909ccaaSLinus Torvalds */ 2297e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask) 2305909ccaaSLinus Torvalds { 23126cf46beSLinus Torvalds unsigned int mode = inode->i_mode; 2325909ccaaSLinus Torvalds 2339c2c7039SAl Viro mask &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK; 2345909ccaaSLinus Torvalds 235e795b717SSerge E. Hallyn if (current_user_ns() != inode_userns(inode)) 236e795b717SSerge E. Hallyn goto other_perms; 237e795b717SSerge E. Hallyn 2385909ccaaSLinus Torvalds if (current_fsuid() == inode->i_uid) 2395909ccaaSLinus Torvalds mode >>= 6; 2405909ccaaSLinus Torvalds else { 241e77819e5SLinus Torvalds if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { 2427e40145eSAl Viro int error = check_acl(inode, mask); 2435909ccaaSLinus Torvalds if (error != -EAGAIN) 2445909ccaaSLinus Torvalds return error; 2455909ccaaSLinus Torvalds } 2465909ccaaSLinus Torvalds 2475909ccaaSLinus Torvalds if (in_group_p(inode->i_gid)) 2485909ccaaSLinus Torvalds mode >>= 3; 2495909ccaaSLinus Torvalds } 2505909ccaaSLinus Torvalds 251e795b717SSerge E. Hallyn other_perms: 2525909ccaaSLinus Torvalds /* 2535909ccaaSLinus Torvalds * If the DACs are ok we don't need any capability check. 2545909ccaaSLinus Torvalds */ 2559c2c7039SAl Viro if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 2565909ccaaSLinus Torvalds return 0; 2575909ccaaSLinus Torvalds return -EACCES; 2585909ccaaSLinus Torvalds } 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds /** 2611da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 2621da177e4SLinus Torvalds * @inode: inode to check access rights for 2631da177e4SLinus Torvalds * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 2641da177e4SLinus Torvalds * 2651da177e4SLinus Torvalds * Used to check for read/write/execute permissions on a file. 2661da177e4SLinus Torvalds * We use "fsuid" for this, letting us set arbitrary permissions 2671da177e4SLinus Torvalds * for filesystem access without changing the "normal" uids which 268b74c79e9SNick Piggin * are used for other things. 269b74c79e9SNick Piggin * 270b74c79e9SNick Piggin * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk 271b74c79e9SNick Piggin * request cannot be satisfied (eg. requires blocking or too much complexity). 272b74c79e9SNick Piggin * It would then be called again in ref-walk mode. 2731da177e4SLinus Torvalds */ 2742830ba7fSAl Viro int generic_permission(struct inode *inode, int mask) 2751da177e4SLinus Torvalds { 2765909ccaaSLinus Torvalds int ret; 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds /* 2795909ccaaSLinus Torvalds * Do the basic POSIX ACL permission checks. 2801da177e4SLinus Torvalds */ 2817e40145eSAl Viro ret = acl_permission_check(inode, mask); 2825909ccaaSLinus Torvalds if (ret != -EACCES) 2835909ccaaSLinus Torvalds return ret; 2841da177e4SLinus Torvalds 285d594e7ecSAl Viro if (S_ISDIR(inode->i_mode)) { 286d594e7ecSAl Viro /* DACs are overridable for directories */ 287d594e7ecSAl Viro if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE)) 288d594e7ecSAl Viro return 0; 289d594e7ecSAl Viro if (!(mask & MAY_WRITE)) 290d594e7ecSAl Viro if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH)) 291d594e7ecSAl Viro return 0; 292d594e7ecSAl Viro return -EACCES; 293d594e7ecSAl Viro } 2941da177e4SLinus Torvalds /* 2951da177e4SLinus Torvalds * Read/write DACs are always overridable. 296d594e7ecSAl Viro * Executable DACs are overridable when there is 297d594e7ecSAl Viro * at least one exec bit set. 2981da177e4SLinus Torvalds */ 299d594e7ecSAl Viro if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) 300e795b717SSerge E. Hallyn if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE)) 3011da177e4SLinus Torvalds return 0; 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds /* 3041da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 3051da177e4SLinus Torvalds */ 3067ea66001SSerge E. Hallyn mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 307d594e7ecSAl Viro if (mask == MAY_READ) 308e795b717SSerge E. Hallyn if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH)) 3091da177e4SLinus Torvalds return 0; 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds return -EACCES; 3121da177e4SLinus Torvalds } 3131da177e4SLinus Torvalds 314cb23beb5SChristoph Hellwig /** 315cb23beb5SChristoph Hellwig * inode_permission - check for access rights to a given inode 316cb23beb5SChristoph Hellwig * @inode: inode to check permission on 317cb23beb5SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 318cb23beb5SChristoph Hellwig * 319cb23beb5SChristoph Hellwig * Used to check for read/write/execute permissions on an inode. 320cb23beb5SChristoph Hellwig * We use "fsuid" for this, letting us set arbitrary permissions 321cb23beb5SChristoph Hellwig * for filesystem access without changing the "normal" uids which 322cb23beb5SChristoph Hellwig * are used for other things. 323cb23beb5SChristoph Hellwig */ 324f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask) 3251da177e4SLinus Torvalds { 326e6305c43SAl Viro int retval; 3271da177e4SLinus Torvalds 3281da177e4SLinus Torvalds if (mask & MAY_WRITE) { 32922590e41SMiklos Szeredi umode_t mode = inode->i_mode; 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds /* 3321da177e4SLinus Torvalds * Nobody gets write access to a read-only fs. 3331da177e4SLinus Torvalds */ 3341da177e4SLinus Torvalds if (IS_RDONLY(inode) && 3351da177e4SLinus Torvalds (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 3361da177e4SLinus Torvalds return -EROFS; 3371da177e4SLinus Torvalds 3381da177e4SLinus Torvalds /* 3391da177e4SLinus Torvalds * Nobody gets write access to an immutable file. 3401da177e4SLinus Torvalds */ 3411da177e4SLinus Torvalds if (IS_IMMUTABLE(inode)) 3421da177e4SLinus Torvalds return -EACCES; 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds 345acfa4380SAl Viro if (inode->i_op->permission) 34610556cb2SAl Viro retval = inode->i_op->permission(inode, mask); 347f696a365SMiklos Szeredi else 3482830ba7fSAl Viro retval = generic_permission(inode, mask); 349f696a365SMiklos Szeredi 3501da177e4SLinus Torvalds if (retval) 3511da177e4SLinus Torvalds return retval; 3521da177e4SLinus Torvalds 35308ce5f16SSerge E. Hallyn retval = devcgroup_inode_permission(inode, mask); 35408ce5f16SSerge E. Hallyn if (retval) 35508ce5f16SSerge E. Hallyn return retval; 35608ce5f16SSerge E. Hallyn 357d09ca739SEric Paris return security_inode_permission(inode, mask); 3581da177e4SLinus Torvalds } 3591da177e4SLinus Torvalds 360f4d6ff89SAl Viro /** 3615dd784d0SJan Blunck * path_get - get a reference to a path 3625dd784d0SJan Blunck * @path: path to get the reference to 3635dd784d0SJan Blunck * 3645dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 3655dd784d0SJan Blunck */ 3665dd784d0SJan Blunck void path_get(struct path *path) 3675dd784d0SJan Blunck { 3685dd784d0SJan Blunck mntget(path->mnt); 3695dd784d0SJan Blunck dget(path->dentry); 3705dd784d0SJan Blunck } 3715dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 3725dd784d0SJan Blunck 3735dd784d0SJan Blunck /** 3741d957f9bSJan Blunck * path_put - put a reference to a path 3751d957f9bSJan Blunck * @path: path to put the reference to 3761d957f9bSJan Blunck * 3771d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 3781d957f9bSJan Blunck */ 3791d957f9bSJan Blunck void path_put(struct path *path) 3801da177e4SLinus Torvalds { 3811d957f9bSJan Blunck dput(path->dentry); 3821d957f9bSJan Blunck mntput(path->mnt); 3831da177e4SLinus Torvalds } 3841d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 3851da177e4SLinus Torvalds 38619660af7SAl Viro /* 38731e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 38819660af7SAl Viro * Documentation/filesystems/path-lookup.txt). In situations when we can't 38919660af7SAl Viro * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab 39019660af7SAl Viro * normal reference counts on dentries and vfsmounts to transition to rcu-walk 39119660af7SAl Viro * mode. Refcounts are grabbed at the last known good point before rcu-walk 39219660af7SAl Viro * got stuck, so ref-walk may continue from there. If this is not successful 39319660af7SAl Viro * (eg. a seqcount has changed), then failure is returned and it's up to caller 39419660af7SAl Viro * to restart the path walk from the beginning in ref-walk mode. 39531e6b01fSNick Piggin */ 39631e6b01fSNick Piggin 39731e6b01fSNick Piggin /** 39819660af7SAl Viro * unlazy_walk - try to switch to ref-walk mode. 39919660af7SAl Viro * @nd: nameidata pathwalk data 40019660af7SAl Viro * @dentry: child of nd->path.dentry or NULL 40139191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 40231e6b01fSNick Piggin * 40319660af7SAl Viro * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry 40419660af7SAl Viro * for ref-walk mode. @dentry must be a path found by a do_lookup call on 40519660af7SAl Viro * @nd or NULL. Must be called from rcu-walk context. 40631e6b01fSNick Piggin */ 40719660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry) 40831e6b01fSNick Piggin { 40931e6b01fSNick Piggin struct fs_struct *fs = current->fs; 41031e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 4115b6ca027SAl Viro int want_root = 0; 41231e6b01fSNick Piggin 41331e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 4145b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 4155b6ca027SAl Viro want_root = 1; 41631e6b01fSNick Piggin spin_lock(&fs->lock); 41731e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 41831e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 41931e6b01fSNick Piggin goto err_root; 42031e6b01fSNick Piggin } 42131e6b01fSNick Piggin spin_lock(&parent->d_lock); 42219660af7SAl Viro if (!dentry) { 42319660af7SAl Viro if (!__d_rcu_to_refcount(parent, nd->seq)) 42419660af7SAl Viro goto err_parent; 42519660af7SAl Viro BUG_ON(nd->inode != parent->d_inode); 42619660af7SAl Viro } else { 42794c0d4ecSAl Viro if (dentry->d_parent != parent) 42894c0d4ecSAl Viro goto err_parent; 42931e6b01fSNick Piggin spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); 43031e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 43119660af7SAl Viro goto err_child; 43231e6b01fSNick Piggin /* 43319660af7SAl Viro * If the sequence check on the child dentry passed, then 43419660af7SAl Viro * the child has not been removed from its parent. This 43519660af7SAl Viro * means the parent dentry must be valid and able to take 43619660af7SAl Viro * a reference at this point. 43731e6b01fSNick Piggin */ 43831e6b01fSNick Piggin BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); 43931e6b01fSNick Piggin BUG_ON(!parent->d_count); 44031e6b01fSNick Piggin parent->d_count++; 44131e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 44219660af7SAl Viro } 44331e6b01fSNick Piggin spin_unlock(&parent->d_lock); 4445b6ca027SAl Viro if (want_root) { 44531e6b01fSNick Piggin path_get(&nd->root); 44631e6b01fSNick Piggin spin_unlock(&fs->lock); 44731e6b01fSNick Piggin } 44831e6b01fSNick Piggin mntget(nd->path.mnt); 44931e6b01fSNick Piggin 45031e6b01fSNick Piggin rcu_read_unlock(); 45131e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 45231e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 45331e6b01fSNick Piggin return 0; 45419660af7SAl Viro 45519660af7SAl Viro err_child: 45631e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 45719660af7SAl Viro err_parent: 45831e6b01fSNick Piggin spin_unlock(&parent->d_lock); 45931e6b01fSNick Piggin err_root: 4605b6ca027SAl Viro if (want_root) 46131e6b01fSNick Piggin spin_unlock(&fs->lock); 46231e6b01fSNick Piggin return -ECHILD; 46331e6b01fSNick Piggin } 46431e6b01fSNick Piggin 46531e6b01fSNick Piggin /** 466834f2a4aSTrond Myklebust * release_open_intent - free up open intent resources 467834f2a4aSTrond Myklebust * @nd: pointer to nameidata 468834f2a4aSTrond Myklebust */ 469834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd) 470834f2a4aSTrond Myklebust { 4712dab5974SLinus Torvalds struct file *file = nd->intent.open.file; 4722dab5974SLinus Torvalds 4732dab5974SLinus Torvalds if (file && !IS_ERR(file)) { 4742dab5974SLinus Torvalds if (file->f_path.dentry == NULL) 4752dab5974SLinus Torvalds put_filp(file); 476834f2a4aSTrond Myklebust else 4772dab5974SLinus Torvalds fput(file); 4782dab5974SLinus Torvalds } 479834f2a4aSTrond Myklebust } 480834f2a4aSTrond Myklebust 481f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd) 48234286d66SNick Piggin { 483f60aef7eSAl Viro return dentry->d_op->d_revalidate(dentry, nd); 48434286d66SNick Piggin } 48534286d66SNick Piggin 4869f1fafeeSAl Viro /** 4879f1fafeeSAl Viro * complete_walk - successful completion of path walk 4889f1fafeeSAl Viro * @nd: pointer nameidata 48939159de2SJeff Layton * 4909f1fafeeSAl Viro * If we had been in RCU mode, drop out of it and legitimize nd->path. 4919f1fafeeSAl Viro * Revalidate the final result, unless we'd already done that during 4929f1fafeeSAl Viro * the path walk or the filesystem doesn't ask for it. Return 0 on 4939f1fafeeSAl Viro * success, -error on failure. In case of failure caller does not 4949f1fafeeSAl Viro * need to drop nd->path. 49539159de2SJeff Layton */ 4969f1fafeeSAl Viro static int complete_walk(struct nameidata *nd) 49739159de2SJeff Layton { 49816c2cd71SAl Viro struct dentry *dentry = nd->path.dentry; 49939159de2SJeff Layton int status; 50039159de2SJeff Layton 5019f1fafeeSAl Viro if (nd->flags & LOOKUP_RCU) { 5029f1fafeeSAl Viro nd->flags &= ~LOOKUP_RCU; 5039f1fafeeSAl Viro if (!(nd->flags & LOOKUP_ROOT)) 5049f1fafeeSAl Viro nd->root.mnt = NULL; 5059f1fafeeSAl Viro spin_lock(&dentry->d_lock); 5069f1fafeeSAl Viro if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) { 5079f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 5089f1fafeeSAl Viro rcu_read_unlock(); 5099f1fafeeSAl Viro br_read_unlock(vfsmount_lock); 5109f1fafeeSAl Viro return -ECHILD; 5119f1fafeeSAl Viro } 5129f1fafeeSAl Viro BUG_ON(nd->inode != dentry->d_inode); 5139f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 5149f1fafeeSAl Viro mntget(nd->path.mnt); 5159f1fafeeSAl Viro rcu_read_unlock(); 5169f1fafeeSAl Viro br_read_unlock(vfsmount_lock); 5179f1fafeeSAl Viro } 5189f1fafeeSAl Viro 51916c2cd71SAl Viro if (likely(!(nd->flags & LOOKUP_JUMPED))) 52039159de2SJeff Layton return 0; 52139159de2SJeff Layton 52216c2cd71SAl Viro if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE))) 52316c2cd71SAl Viro return 0; 52416c2cd71SAl Viro 52516c2cd71SAl Viro if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))) 52616c2cd71SAl Viro return 0; 52716c2cd71SAl Viro 52816c2cd71SAl Viro /* Note: we do not d_invalidate() */ 52934286d66SNick Piggin status = d_revalidate(dentry, nd); 53039159de2SJeff Layton if (status > 0) 53139159de2SJeff Layton return 0; 53239159de2SJeff Layton 53316c2cd71SAl Viro if (!status) 53439159de2SJeff Layton status = -ESTALE; 53516c2cd71SAl Viro 5369f1fafeeSAl Viro path_put(&nd->path); 53739159de2SJeff Layton return status; 53839159de2SJeff Layton } 53939159de2SJeff Layton 5402a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 5412a737871SAl Viro { 542f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 543f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 5442a737871SAl Viro } 5452a737871SAl Viro 5466de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 5476de88d72SAl Viro 54831e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 54931e6b01fSNick Piggin { 55031e6b01fSNick Piggin if (!nd->root.mnt) { 55131e6b01fSNick Piggin struct fs_struct *fs = current->fs; 552c28cc364SNick Piggin unsigned seq; 553c28cc364SNick Piggin 554c28cc364SNick Piggin do { 555c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 55631e6b01fSNick Piggin nd->root = fs->root; 557c1530019STim Chen nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq); 558c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 55931e6b01fSNick Piggin } 56031e6b01fSNick Piggin } 56131e6b01fSNick Piggin 562f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 5631da177e4SLinus Torvalds { 56431e6b01fSNick Piggin int ret; 56531e6b01fSNick Piggin 5661da177e4SLinus Torvalds if (IS_ERR(link)) 5671da177e4SLinus Torvalds goto fail; 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds if (*link == '/') { 5702a737871SAl Viro set_root(nd); 5711d957f9bSJan Blunck path_put(&nd->path); 5722a737871SAl Viro nd->path = nd->root; 5732a737871SAl Viro path_get(&nd->root); 57416c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 5751da177e4SLinus Torvalds } 57631e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 577b4091d5fSChristoph Hellwig 57831e6b01fSNick Piggin ret = link_path_walk(link, nd); 57931e6b01fSNick Piggin return ret; 5801da177e4SLinus Torvalds fail: 5811d957f9bSJan Blunck path_put(&nd->path); 5821da177e4SLinus Torvalds return PTR_ERR(link); 5831da177e4SLinus Torvalds } 5841da177e4SLinus Torvalds 5851d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 586051d3812SIan Kent { 587051d3812SIan Kent dput(path->dentry); 5884ac91378SJan Blunck if (path->mnt != nd->path.mnt) 589051d3812SIan Kent mntput(path->mnt); 590051d3812SIan Kent } 591051d3812SIan Kent 5927b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path, 5937b9337aaSNick Piggin struct nameidata *nd) 594051d3812SIan Kent { 59531e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 5964ac91378SJan Blunck dput(nd->path.dentry); 59731e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 5984ac91378SJan Blunck mntput(nd->path.mnt); 5999a229683SHuang Shijie } 60031e6b01fSNick Piggin nd->path.mnt = path->mnt; 6014ac91378SJan Blunck nd->path.dentry = path->dentry; 602051d3812SIan Kent } 603051d3812SIan Kent 604574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie) 605574197e0SAl Viro { 606574197e0SAl Viro struct inode *inode = link->dentry->d_inode; 607574197e0SAl Viro if (!IS_ERR(cookie) && inode->i_op->put_link) 608574197e0SAl Viro inode->i_op->put_link(link->dentry, nd, cookie); 609574197e0SAl Viro path_put(link); 610574197e0SAl Viro } 611574197e0SAl Viro 612def4af30SAl Viro static __always_inline int 613574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p) 6141da177e4SLinus Torvalds { 6151da177e4SLinus Torvalds int error; 6167b9337aaSNick Piggin struct dentry *dentry = link->dentry; 6171da177e4SLinus Torvalds 618844a3917SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 619844a3917SAl Viro 6200e794589SAl Viro if (link->mnt == nd->path.mnt) 6210e794589SAl Viro mntget(link->mnt); 6220e794589SAl Viro 623574197e0SAl Viro if (unlikely(current->total_link_count >= 40)) { 624574197e0SAl Viro *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */ 625574197e0SAl Viro path_put(&nd->path); 626574197e0SAl Viro return -ELOOP; 627574197e0SAl Viro } 628574197e0SAl Viro cond_resched(); 629574197e0SAl Viro current->total_link_count++; 630574197e0SAl Viro 6317b9337aaSNick Piggin touch_atime(link->mnt, dentry); 6321da177e4SLinus Torvalds nd_set_link(nd, NULL); 633cd4e91d3SAl Viro 63436f3b4f6SAl Viro error = security_inode_follow_link(link->dentry, nd); 63536f3b4f6SAl Viro if (error) { 63636f3b4f6SAl Viro *p = ERR_PTR(error); /* no ->put_link(), please */ 63736f3b4f6SAl Viro path_put(&nd->path); 63836f3b4f6SAl Viro return error; 63936f3b4f6SAl Viro } 64036f3b4f6SAl Viro 64186acdca1SAl Viro nd->last_type = LAST_BIND; 642def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 643def4af30SAl Viro error = PTR_ERR(*p); 644def4af30SAl Viro if (!IS_ERR(*p)) { 6451da177e4SLinus Torvalds char *s = nd_get_link(nd); 646cc314eefSLinus Torvalds error = 0; 6471da177e4SLinus Torvalds if (s) 6481da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 649bcda7652SAl Viro else if (nd->last_type == LAST_BIND) { 65016c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 651b21041d0SAl Viro nd->inode = nd->path.dentry->d_inode; 652b21041d0SAl Viro if (nd->inode->i_op->follow_link) { 653bcda7652SAl Viro /* stepped on a _really_ weird one */ 654bcda7652SAl Viro path_put(&nd->path); 655bcda7652SAl Viro error = -ELOOP; 656bcda7652SAl Viro } 657bcda7652SAl Viro } 6581da177e4SLinus Torvalds } 6591da177e4SLinus Torvalds return error; 6601da177e4SLinus Torvalds } 6611da177e4SLinus Torvalds 66231e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 66331e6b01fSNick Piggin { 66431e6b01fSNick Piggin struct vfsmount *parent; 66531e6b01fSNick Piggin struct dentry *mountpoint; 66631e6b01fSNick Piggin 66731e6b01fSNick Piggin parent = path->mnt->mnt_parent; 66831e6b01fSNick Piggin if (parent == path->mnt) 66931e6b01fSNick Piggin return 0; 67031e6b01fSNick Piggin mountpoint = path->mnt->mnt_mountpoint; 67131e6b01fSNick Piggin path->dentry = mountpoint; 67231e6b01fSNick Piggin path->mnt = parent; 67331e6b01fSNick Piggin return 1; 67431e6b01fSNick Piggin } 67531e6b01fSNick Piggin 676bab77ebfSAl Viro int follow_up(struct path *path) 6771da177e4SLinus Torvalds { 6781da177e4SLinus Torvalds struct vfsmount *parent; 6791da177e4SLinus Torvalds struct dentry *mountpoint; 68099b7db7bSNick Piggin 68199b7db7bSNick Piggin br_read_lock(vfsmount_lock); 682bab77ebfSAl Viro parent = path->mnt->mnt_parent; 683bab77ebfSAl Viro if (parent == path->mnt) { 68499b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 6851da177e4SLinus Torvalds return 0; 6861da177e4SLinus Torvalds } 6871da177e4SLinus Torvalds mntget(parent); 688bab77ebfSAl Viro mountpoint = dget(path->mnt->mnt_mountpoint); 68999b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 690bab77ebfSAl Viro dput(path->dentry); 691bab77ebfSAl Viro path->dentry = mountpoint; 692bab77ebfSAl Viro mntput(path->mnt); 693bab77ebfSAl Viro path->mnt = parent; 6941da177e4SLinus Torvalds return 1; 6951da177e4SLinus Torvalds } 6961da177e4SLinus Torvalds 697b5c84bf6SNick Piggin /* 6989875cf80SDavid Howells * Perform an automount 6999875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 7009875cf80SDavid Howells * were called with. 7011da177e4SLinus Torvalds */ 7029875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags, 7039875cf80SDavid Howells bool *need_mntput) 70431e6b01fSNick Piggin { 7059875cf80SDavid Howells struct vfsmount *mnt; 706ea5b778aSDavid Howells int err; 7079875cf80SDavid Howells 7089875cf80SDavid Howells if (!path->dentry->d_op || !path->dentry->d_op->d_automount) 7099875cf80SDavid Howells return -EREMOTE; 7109875cf80SDavid Howells 7116f45b656SDavid Howells /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT 7126f45b656SDavid Howells * and this is the terminal part of the path. 7136f45b656SDavid Howells */ 71449084c3bSAl Viro if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_PARENT)) 7156f45b656SDavid Howells return -EISDIR; /* we actually want to stop here */ 7166f45b656SDavid Howells 7179875cf80SDavid Howells /* We want to mount if someone is trying to open/create a file of any 7189875cf80SDavid Howells * type under the mountpoint, wants to traverse through the mountpoint 7199875cf80SDavid Howells * or wants to open the mounted directory. 7209875cf80SDavid Howells * 7219875cf80SDavid Howells * We don't want to mount if someone's just doing a stat and they've 7229875cf80SDavid Howells * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and 7239875cf80SDavid Howells * appended a '/' to the name. 7249875cf80SDavid Howells */ 7259875cf80SDavid Howells if (!(flags & LOOKUP_FOLLOW) && 72649084c3bSAl Viro !(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | 7279875cf80SDavid Howells LOOKUP_OPEN | LOOKUP_CREATE))) 7289875cf80SDavid Howells return -EISDIR; 7299875cf80SDavid Howells 7309875cf80SDavid Howells current->total_link_count++; 7319875cf80SDavid Howells if (current->total_link_count >= 40) 7329875cf80SDavid Howells return -ELOOP; 7339875cf80SDavid Howells 7349875cf80SDavid Howells mnt = path->dentry->d_op->d_automount(path); 7359875cf80SDavid Howells if (IS_ERR(mnt)) { 7369875cf80SDavid Howells /* 7379875cf80SDavid Howells * The filesystem is allowed to return -EISDIR here to indicate 7389875cf80SDavid Howells * it doesn't want to automount. For instance, autofs would do 7399875cf80SDavid Howells * this so that its userspace daemon can mount on this dentry. 7409875cf80SDavid Howells * 7419875cf80SDavid Howells * However, we can only permit this if it's a terminal point in 7429875cf80SDavid Howells * the path being looked up; if it wasn't then the remainder of 7439875cf80SDavid Howells * the path is inaccessible and we should say so. 7449875cf80SDavid Howells */ 74549084c3bSAl Viro if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT)) 7469875cf80SDavid Howells return -EREMOTE; 7479875cf80SDavid Howells return PTR_ERR(mnt); 74831e6b01fSNick Piggin } 749ea5b778aSDavid Howells 7509875cf80SDavid Howells if (!mnt) /* mount collision */ 7519875cf80SDavid Howells return 0; 7529875cf80SDavid Howells 7538aef1884SAl Viro if (!*need_mntput) { 7548aef1884SAl Viro /* lock_mount() may release path->mnt on error */ 7558aef1884SAl Viro mntget(path->mnt); 7568aef1884SAl Viro *need_mntput = true; 7578aef1884SAl Viro } 75819a167afSAl Viro err = finish_automount(mnt, path); 759ea5b778aSDavid Howells 760ea5b778aSDavid Howells switch (err) { 761ea5b778aSDavid Howells case -EBUSY: 762ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 76319a167afSAl Viro return 0; 764ea5b778aSDavid Howells case 0: 7658aef1884SAl Viro path_put(path); 7669875cf80SDavid Howells path->mnt = mnt; 7679875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 7689875cf80SDavid Howells return 0; 76919a167afSAl Viro default: 77019a167afSAl Viro return err; 7719875cf80SDavid Howells } 77219a167afSAl Viro 773ea5b778aSDavid Howells } 7749875cf80SDavid Howells 7759875cf80SDavid Howells /* 7769875cf80SDavid Howells * Handle a dentry that is managed in some way. 777cc53ce53SDavid Howells * - Flagged for transit management (autofs) 7789875cf80SDavid Howells * - Flagged as mountpoint 7799875cf80SDavid Howells * - Flagged as automount point 7809875cf80SDavid Howells * 7819875cf80SDavid Howells * This may only be called in refwalk mode. 7829875cf80SDavid Howells * 7839875cf80SDavid Howells * Serialization is taken care of in namespace.c 7849875cf80SDavid Howells */ 7859875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 7869875cf80SDavid Howells { 7878aef1884SAl Viro struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */ 7889875cf80SDavid Howells unsigned managed; 7899875cf80SDavid Howells bool need_mntput = false; 7908aef1884SAl Viro int ret = 0; 7919875cf80SDavid Howells 7929875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 7939875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 7949875cf80SDavid Howells * the components of that value change under us */ 7959875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 7969875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 7979875cf80SDavid Howells unlikely(managed != 0)) { 798cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 799cc53ce53SDavid Howells * being held. */ 800cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 801cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 802cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 8031aed3e42SAl Viro ret = path->dentry->d_op->d_manage(path->dentry, false); 804cc53ce53SDavid Howells if (ret < 0) 8058aef1884SAl Viro break; 806cc53ce53SDavid Howells } 807cc53ce53SDavid Howells 8089875cf80SDavid Howells /* Transit to a mounted filesystem. */ 8099875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 8109875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 8119875cf80SDavid Howells if (mounted) { 8129875cf80SDavid Howells dput(path->dentry); 8139875cf80SDavid Howells if (need_mntput) 814463ffb2eSAl Viro mntput(path->mnt); 815463ffb2eSAl Viro path->mnt = mounted; 816463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 8179875cf80SDavid Howells need_mntput = true; 8189875cf80SDavid Howells continue; 819463ffb2eSAl Viro } 820463ffb2eSAl Viro 8219875cf80SDavid Howells /* Something is mounted on this dentry in another 8229875cf80SDavid Howells * namespace and/or whatever was mounted there in this 8239875cf80SDavid Howells * namespace got unmounted before we managed to get the 8249875cf80SDavid Howells * vfsmount_lock */ 8251da177e4SLinus Torvalds } 8269875cf80SDavid Howells 8279875cf80SDavid Howells /* Handle an automount point */ 8289875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 8299875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 8309875cf80SDavid Howells if (ret < 0) 8318aef1884SAl Viro break; 8329875cf80SDavid Howells continue; 8339875cf80SDavid Howells } 8349875cf80SDavid Howells 8359875cf80SDavid Howells /* We didn't change the current path point */ 8369875cf80SDavid Howells break; 8379875cf80SDavid Howells } 8388aef1884SAl Viro 8398aef1884SAl Viro if (need_mntput && path->mnt == mnt) 8408aef1884SAl Viro mntput(path->mnt); 8418aef1884SAl Viro if (ret == -EISDIR) 8428aef1884SAl Viro ret = 0; 8438aef1884SAl Viro return ret; 8441da177e4SLinus Torvalds } 8451da177e4SLinus Torvalds 846cc53ce53SDavid Howells int follow_down_one(struct path *path) 8471da177e4SLinus Torvalds { 8481da177e4SLinus Torvalds struct vfsmount *mounted; 8491da177e4SLinus Torvalds 8501c755af4SAl Viro mounted = lookup_mnt(path); 8511da177e4SLinus Torvalds if (mounted) { 8529393bd07SAl Viro dput(path->dentry); 8539393bd07SAl Viro mntput(path->mnt); 8549393bd07SAl Viro path->mnt = mounted; 8559393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 8561da177e4SLinus Torvalds return 1; 8571da177e4SLinus Torvalds } 8581da177e4SLinus Torvalds return 0; 8591da177e4SLinus Torvalds } 8601da177e4SLinus Torvalds 86162a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry) 86262a7375eSIan Kent { 86362a7375eSIan Kent return (dentry->d_flags & DCACHE_MANAGE_TRANSIT && 86462a7375eSIan Kent dentry->d_op->d_manage(dentry, true) < 0); 86562a7375eSIan Kent } 86662a7375eSIan Kent 8679875cf80SDavid Howells /* 868287548e4SAl Viro * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if 869287548e4SAl Viro * we meet a managed dentry that would need blocking. 8709875cf80SDavid Howells */ 8719875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 872287548e4SAl Viro struct inode **inode) 8739875cf80SDavid Howells { 87462a7375eSIan Kent for (;;) { 8759875cf80SDavid Howells struct vfsmount *mounted; 87662a7375eSIan Kent /* 87762a7375eSIan Kent * Don't forget we might have a non-mountpoint managed dentry 87862a7375eSIan Kent * that wants to block transit. 87962a7375eSIan Kent */ 880287548e4SAl Viro if (unlikely(managed_dentry_might_block(path->dentry))) 881ab90911fSDavid Howells return false; 88262a7375eSIan Kent 88362a7375eSIan Kent if (!d_mountpoint(path->dentry)) 88462a7375eSIan Kent break; 88562a7375eSIan Kent 8869875cf80SDavid Howells mounted = __lookup_mnt(path->mnt, path->dentry, 1); 8879875cf80SDavid Howells if (!mounted) 8889875cf80SDavid Howells break; 8899875cf80SDavid Howells path->mnt = mounted; 8909875cf80SDavid Howells path->dentry = mounted->mnt_root; 8919875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 89259430262SLinus Torvalds /* 89359430262SLinus Torvalds * Update the inode too. We don't need to re-check the 89459430262SLinus Torvalds * dentry sequence number here after this d_inode read, 89559430262SLinus Torvalds * because a mount-point is always pinned. 89659430262SLinus Torvalds */ 89759430262SLinus Torvalds *inode = path->dentry->d_inode; 8989875cf80SDavid Howells } 8999875cf80SDavid Howells return true; 9009875cf80SDavid Howells } 9019875cf80SDavid Howells 902dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd) 903287548e4SAl Viro { 904dea39376SAl Viro while (d_mountpoint(nd->path.dentry)) { 905287548e4SAl Viro struct vfsmount *mounted; 906dea39376SAl Viro mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1); 907287548e4SAl Viro if (!mounted) 908287548e4SAl Viro break; 909dea39376SAl Viro nd->path.mnt = mounted; 910dea39376SAl Viro nd->path.dentry = mounted->mnt_root; 911dea39376SAl Viro nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 912287548e4SAl Viro } 913287548e4SAl Viro } 914287548e4SAl Viro 91531e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 91631e6b01fSNick Piggin { 91731e6b01fSNick Piggin set_root_rcu(nd); 91831e6b01fSNick Piggin 91931e6b01fSNick Piggin while (1) { 92031e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 92131e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 92231e6b01fSNick Piggin break; 92331e6b01fSNick Piggin } 92431e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 92531e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 92631e6b01fSNick Piggin struct dentry *parent = old->d_parent; 92731e6b01fSNick Piggin unsigned seq; 92831e6b01fSNick Piggin 92931e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 93031e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 931ef7562d5SAl Viro goto failed; 93231e6b01fSNick Piggin nd->path.dentry = parent; 93331e6b01fSNick Piggin nd->seq = seq; 93431e6b01fSNick Piggin break; 93531e6b01fSNick Piggin } 93631e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 93731e6b01fSNick Piggin break; 93831e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 93931e6b01fSNick Piggin } 940dea39376SAl Viro follow_mount_rcu(nd); 941dea39376SAl Viro nd->inode = nd->path.dentry->d_inode; 94231e6b01fSNick Piggin return 0; 943ef7562d5SAl Viro 944ef7562d5SAl Viro failed: 945ef7562d5SAl Viro nd->flags &= ~LOOKUP_RCU; 9465b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 947ef7562d5SAl Viro nd->root.mnt = NULL; 948ef7562d5SAl Viro rcu_read_unlock(); 949ef7562d5SAl Viro br_read_unlock(vfsmount_lock); 950ef7562d5SAl Viro return -ECHILD; 95131e6b01fSNick Piggin } 95231e6b01fSNick Piggin 9539875cf80SDavid Howells /* 954cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 955cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 956cc53ce53SDavid Howells * caller is permitted to proceed or not. 957cc53ce53SDavid Howells */ 9587cc90cc3SAl Viro int follow_down(struct path *path) 959cc53ce53SDavid Howells { 960cc53ce53SDavid Howells unsigned managed; 961cc53ce53SDavid Howells int ret; 962cc53ce53SDavid Howells 963cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 964cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 965cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 966cc53ce53SDavid Howells * being held. 967cc53ce53SDavid Howells * 968cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 969cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 970cc53ce53SDavid Howells * other than its daemon the right to mount on its 971cc53ce53SDavid Howells * superstructure. 972cc53ce53SDavid Howells * 973cc53ce53SDavid Howells * The filesystem may sleep at this point. 974cc53ce53SDavid Howells */ 975cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 976cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 977cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 978ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 9791aed3e42SAl Viro path->dentry, false); 980cc53ce53SDavid Howells if (ret < 0) 981cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 982cc53ce53SDavid Howells } 983cc53ce53SDavid Howells 984cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 985cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 986cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 987cc53ce53SDavid Howells if (!mounted) 988cc53ce53SDavid Howells break; 989cc53ce53SDavid Howells dput(path->dentry); 990cc53ce53SDavid Howells mntput(path->mnt); 991cc53ce53SDavid Howells path->mnt = mounted; 992cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 993cc53ce53SDavid Howells continue; 994cc53ce53SDavid Howells } 995cc53ce53SDavid Howells 996cc53ce53SDavid Howells /* Don't handle automount points here */ 997cc53ce53SDavid Howells break; 998cc53ce53SDavid Howells } 999cc53ce53SDavid Howells return 0; 1000cc53ce53SDavid Howells } 1001cc53ce53SDavid Howells 1002cc53ce53SDavid Howells /* 10039875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 10049875cf80SDavid Howells */ 10059875cf80SDavid Howells static void follow_mount(struct path *path) 10069875cf80SDavid Howells { 10079875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 10089875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 10099875cf80SDavid Howells if (!mounted) 10109875cf80SDavid Howells break; 10119875cf80SDavid Howells dput(path->dentry); 10129875cf80SDavid Howells mntput(path->mnt); 10139875cf80SDavid Howells path->mnt = mounted; 10149875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 10159875cf80SDavid Howells } 10169875cf80SDavid Howells } 10179875cf80SDavid Howells 101831e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 10191da177e4SLinus Torvalds { 10202a737871SAl Viro set_root(nd); 1021e518ddb7SAndreas Mohr 10221da177e4SLinus Torvalds while(1) { 10234ac91378SJan Blunck struct dentry *old = nd->path.dentry; 10241da177e4SLinus Torvalds 10252a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 10262a737871SAl Viro nd->path.mnt == nd->root.mnt) { 10271da177e4SLinus Torvalds break; 10281da177e4SLinus Torvalds } 10294ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 10303088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 10313088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 10321da177e4SLinus Torvalds dput(old); 10331da177e4SLinus Torvalds break; 10341da177e4SLinus Torvalds } 10353088dd70SAl Viro if (!follow_up(&nd->path)) 10361da177e4SLinus Torvalds break; 10371da177e4SLinus Torvalds } 103879ed0226SAl Viro follow_mount(&nd->path); 103931e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 10401da177e4SLinus Torvalds } 10411da177e4SLinus Torvalds 10421da177e4SLinus Torvalds /* 1043baa03890SNick Piggin * Allocate a dentry with name and parent, and perform a parent 1044baa03890SNick Piggin * directory ->lookup on it. Returns the new dentry, or ERR_PTR 1045baa03890SNick Piggin * on error. parent->d_inode->i_mutex must be held. d_lookup must 1046baa03890SNick Piggin * have verified that no child exists while under i_mutex. 1047baa03890SNick Piggin */ 1048baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent, 1049baa03890SNick Piggin struct qstr *name, struct nameidata *nd) 1050baa03890SNick Piggin { 1051baa03890SNick Piggin struct inode *inode = parent->d_inode; 1052baa03890SNick Piggin struct dentry *dentry; 1053baa03890SNick Piggin struct dentry *old; 1054baa03890SNick Piggin 1055baa03890SNick Piggin /* Don't create child dentry for a dead directory. */ 1056baa03890SNick Piggin if (unlikely(IS_DEADDIR(inode))) 1057baa03890SNick Piggin return ERR_PTR(-ENOENT); 1058baa03890SNick Piggin 1059baa03890SNick Piggin dentry = d_alloc(parent, name); 1060baa03890SNick Piggin if (unlikely(!dentry)) 1061baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1062baa03890SNick Piggin 1063baa03890SNick Piggin old = inode->i_op->lookup(inode, dentry, nd); 1064baa03890SNick Piggin if (unlikely(old)) { 1065baa03890SNick Piggin dput(dentry); 1066baa03890SNick Piggin dentry = old; 1067baa03890SNick Piggin } 1068baa03890SNick Piggin return dentry; 1069baa03890SNick Piggin } 1070baa03890SNick Piggin 1071baa03890SNick Piggin /* 107244396f4bSJosef Bacik * We already have a dentry, but require a lookup to be performed on the parent 107344396f4bSJosef Bacik * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error. 107444396f4bSJosef Bacik * parent->d_inode->i_mutex must be held. d_lookup must have verified that no 107544396f4bSJosef Bacik * child exists while under i_mutex. 107644396f4bSJosef Bacik */ 107744396f4bSJosef Bacik static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry, 107844396f4bSJosef Bacik struct nameidata *nd) 107944396f4bSJosef Bacik { 108044396f4bSJosef Bacik struct inode *inode = parent->d_inode; 108144396f4bSJosef Bacik struct dentry *old; 108244396f4bSJosef Bacik 108344396f4bSJosef Bacik /* Don't create child dentry for a dead directory. */ 108444396f4bSJosef Bacik if (unlikely(IS_DEADDIR(inode))) 108544396f4bSJosef Bacik return ERR_PTR(-ENOENT); 108644396f4bSJosef Bacik 108744396f4bSJosef Bacik old = inode->i_op->lookup(inode, dentry, nd); 108844396f4bSJosef Bacik if (unlikely(old)) { 108944396f4bSJosef Bacik dput(dentry); 109044396f4bSJosef Bacik dentry = old; 109144396f4bSJosef Bacik } 109244396f4bSJosef Bacik return dentry; 109344396f4bSJosef Bacik } 109444396f4bSJosef Bacik 109544396f4bSJosef Bacik /* 10961da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 10971da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 10981da177e4SLinus Torvalds * It _is_ time-critical. 10991da177e4SLinus Torvalds */ 11001da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name, 110131e6b01fSNick Piggin struct path *path, struct inode **inode) 11021da177e4SLinus Torvalds { 11034ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 110431e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 11055a18fff2SAl Viro int need_reval = 1; 11065a18fff2SAl Viro int status = 1; 11079875cf80SDavid Howells int err; 11089875cf80SDavid Howells 11093cac260aSAl Viro /* 1110b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1111b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1112b04f784eSNick Piggin * do the non-racy lookup, below. 1113b04f784eSNick Piggin */ 111431e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 111531e6b01fSNick Piggin unsigned seq; 111631e6b01fSNick Piggin *inode = nd->inode; 111731e6b01fSNick Piggin dentry = __d_lookup_rcu(parent, name, &seq, inode); 11185a18fff2SAl Viro if (!dentry) 11195a18fff2SAl Viro goto unlazy; 11205a18fff2SAl Viro 112131e6b01fSNick Piggin /* Memory barrier in read_seqcount_begin of child is enough */ 112231e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 112331e6b01fSNick Piggin return -ECHILD; 112431e6b01fSNick Piggin nd->seq = seq; 11255a18fff2SAl Viro 112624643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 11275a18fff2SAl Viro status = d_revalidate(dentry, nd); 11285a18fff2SAl Viro if (unlikely(status <= 0)) { 11295a18fff2SAl Viro if (status != -ECHILD) 11305a18fff2SAl Viro need_reval = 0; 11315a18fff2SAl Viro goto unlazy; 11325a18fff2SAl Viro } 113324643087SAl Viro } 113444396f4bSJosef Bacik if (unlikely(d_need_lookup(dentry))) 113544396f4bSJosef Bacik goto unlazy; 113631e6b01fSNick Piggin path->mnt = mnt; 113731e6b01fSNick Piggin path->dentry = dentry; 1138d6e9bd25SAl Viro if (unlikely(!__follow_mount_rcu(nd, path, inode))) 1139d6e9bd25SAl Viro goto unlazy; 1140d6e9bd25SAl Viro if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 1141d6e9bd25SAl Viro goto unlazy; 11429875cf80SDavid Howells return 0; 11435a18fff2SAl Viro unlazy: 114419660af7SAl Viro if (unlazy_walk(nd, dentry)) 11455a18fff2SAl Viro return -ECHILD; 11465a18fff2SAl Viro } else { 114731e6b01fSNick Piggin dentry = __d_lookup(parent, name); 114824643087SAl Viro } 11495a18fff2SAl Viro 115044396f4bSJosef Bacik if (dentry && unlikely(d_need_lookup(dentry))) { 115144396f4bSJosef Bacik dput(dentry); 115244396f4bSJosef Bacik dentry = NULL; 115344396f4bSJosef Bacik } 11545a18fff2SAl Viro retry: 11555a18fff2SAl Viro if (unlikely(!dentry)) { 11565a18fff2SAl Viro struct inode *dir = parent->d_inode; 11575a18fff2SAl Viro BUG_ON(nd->inode != dir); 11585a18fff2SAl Viro 11595a18fff2SAl Viro mutex_lock(&dir->i_mutex); 11605a18fff2SAl Viro dentry = d_lookup(parent, name); 11615a18fff2SAl Viro if (likely(!dentry)) { 11625a18fff2SAl Viro dentry = d_alloc_and_lookup(parent, name, nd); 11635a18fff2SAl Viro if (IS_ERR(dentry)) { 11645a18fff2SAl Viro mutex_unlock(&dir->i_mutex); 11655a18fff2SAl Viro return PTR_ERR(dentry); 11665a18fff2SAl Viro } 11675a18fff2SAl Viro /* known good */ 11685a18fff2SAl Viro need_reval = 0; 11695a18fff2SAl Viro status = 1; 117044396f4bSJosef Bacik } else if (unlikely(d_need_lookup(dentry))) { 117144396f4bSJosef Bacik dentry = d_inode_lookup(parent, dentry, nd); 117244396f4bSJosef Bacik if (IS_ERR(dentry)) { 117344396f4bSJosef Bacik mutex_unlock(&dir->i_mutex); 117444396f4bSJosef Bacik return PTR_ERR(dentry); 117544396f4bSJosef Bacik } 117644396f4bSJosef Bacik /* known good */ 117744396f4bSJosef Bacik need_reval = 0; 117844396f4bSJosef Bacik status = 1; 11795a18fff2SAl Viro } 11805a18fff2SAl Viro mutex_unlock(&dir->i_mutex); 11815a18fff2SAl Viro } 11825a18fff2SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) 11835a18fff2SAl Viro status = d_revalidate(dentry, nd); 11845a18fff2SAl Viro if (unlikely(status <= 0)) { 11855a18fff2SAl Viro if (status < 0) { 11865a18fff2SAl Viro dput(dentry); 11875a18fff2SAl Viro return status; 11885a18fff2SAl Viro } 11895a18fff2SAl Viro if (!d_invalidate(dentry)) { 11905a18fff2SAl Viro dput(dentry); 11915a18fff2SAl Viro dentry = NULL; 11925a18fff2SAl Viro need_reval = 1; 11935a18fff2SAl Viro goto retry; 11945a18fff2SAl Viro } 11955a18fff2SAl Viro } 11965a18fff2SAl Viro 11971da177e4SLinus Torvalds path->mnt = mnt; 11981da177e4SLinus Torvalds path->dentry = dentry; 11999875cf80SDavid Howells err = follow_managed(path, nd->flags); 120089312214SIan Kent if (unlikely(err < 0)) { 120189312214SIan Kent path_put_conditional(path, nd); 12029875cf80SDavid Howells return err; 120389312214SIan Kent } 120431e6b01fSNick Piggin *inode = path->dentry->d_inode; 12051da177e4SLinus Torvalds return 0; 12061da177e4SLinus Torvalds } 12071da177e4SLinus Torvalds 120852094c8aSAl Viro static inline int may_lookup(struct nameidata *nd) 120952094c8aSAl Viro { 121052094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 12114ad5abb3SAl Viro int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); 121252094c8aSAl Viro if (err != -ECHILD) 121352094c8aSAl Viro return err; 121419660af7SAl Viro if (unlazy_walk(nd, NULL)) 121552094c8aSAl Viro return -ECHILD; 121652094c8aSAl Viro } 12174ad5abb3SAl Viro return inode_permission(nd->inode, MAY_EXEC); 121852094c8aSAl Viro } 121952094c8aSAl Viro 12209856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type) 12219856fa1bSAl Viro { 12229856fa1bSAl Viro if (type == LAST_DOTDOT) { 12239856fa1bSAl Viro if (nd->flags & LOOKUP_RCU) { 12249856fa1bSAl Viro if (follow_dotdot_rcu(nd)) 12259856fa1bSAl Viro return -ECHILD; 12269856fa1bSAl Viro } else 12279856fa1bSAl Viro follow_dotdot(nd); 12289856fa1bSAl Viro } 12299856fa1bSAl Viro return 0; 12309856fa1bSAl Viro } 12319856fa1bSAl Viro 1232951361f9SAl Viro static void terminate_walk(struct nameidata *nd) 1233951361f9SAl Viro { 1234951361f9SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1235951361f9SAl Viro path_put(&nd->path); 1236951361f9SAl Viro } else { 1237951361f9SAl Viro nd->flags &= ~LOOKUP_RCU; 12385b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1239951361f9SAl Viro nd->root.mnt = NULL; 1240951361f9SAl Viro rcu_read_unlock(); 1241951361f9SAl Viro br_read_unlock(vfsmount_lock); 1242951361f9SAl Viro } 1243951361f9SAl Viro } 1244951361f9SAl Viro 1245ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path, 1246ce57dfc1SAl Viro struct qstr *name, int type, int follow) 1247ce57dfc1SAl Viro { 1248ce57dfc1SAl Viro struct inode *inode; 1249ce57dfc1SAl Viro int err; 1250ce57dfc1SAl Viro /* 1251ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1252ce57dfc1SAl Viro * to be able to know about the current root directory and 1253ce57dfc1SAl Viro * parent relationships. 1254ce57dfc1SAl Viro */ 1255ce57dfc1SAl Viro if (unlikely(type != LAST_NORM)) 1256ce57dfc1SAl Viro return handle_dots(nd, type); 1257ce57dfc1SAl Viro err = do_lookup(nd, name, path, &inode); 1258ce57dfc1SAl Viro if (unlikely(err)) { 1259ce57dfc1SAl Viro terminate_walk(nd); 1260ce57dfc1SAl Viro return err; 1261ce57dfc1SAl Viro } 1262ce57dfc1SAl Viro if (!inode) { 1263ce57dfc1SAl Viro path_to_nameidata(path, nd); 1264ce57dfc1SAl Viro terminate_walk(nd); 1265ce57dfc1SAl Viro return -ENOENT; 1266ce57dfc1SAl Viro } 1267ce57dfc1SAl Viro if (unlikely(inode->i_op->follow_link) && follow) { 126819660af7SAl Viro if (nd->flags & LOOKUP_RCU) { 126919660af7SAl Viro if (unlikely(unlazy_walk(nd, path->dentry))) { 127019660af7SAl Viro terminate_walk(nd); 1271ce57dfc1SAl Viro return -ECHILD; 127219660af7SAl Viro } 127319660af7SAl Viro } 1274ce57dfc1SAl Viro BUG_ON(inode != path->dentry->d_inode); 1275ce57dfc1SAl Viro return 1; 1276ce57dfc1SAl Viro } 1277ce57dfc1SAl Viro path_to_nameidata(path, nd); 1278ce57dfc1SAl Viro nd->inode = inode; 1279ce57dfc1SAl Viro return 0; 1280ce57dfc1SAl Viro } 1281ce57dfc1SAl Viro 12821da177e4SLinus Torvalds /* 1283b356379aSAl Viro * This limits recursive symlink follows to 8, while 1284b356379aSAl Viro * limiting consecutive symlinks to 40. 1285b356379aSAl Viro * 1286b356379aSAl Viro * Without that kind of total limit, nasty chains of consecutive 1287b356379aSAl Viro * symlinks can cause almost arbitrarily long lookups. 1288b356379aSAl Viro */ 1289b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd) 1290b356379aSAl Viro { 1291b356379aSAl Viro int res; 1292b356379aSAl Viro 1293b356379aSAl Viro if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { 1294b356379aSAl Viro path_put_conditional(path, nd); 1295b356379aSAl Viro path_put(&nd->path); 1296b356379aSAl Viro return -ELOOP; 1297b356379aSAl Viro } 12981a4022f8SErez Zadok BUG_ON(nd->depth >= MAX_NESTED_LINKS); 1299b356379aSAl Viro 1300b356379aSAl Viro nd->depth++; 1301b356379aSAl Viro current->link_count++; 1302b356379aSAl Viro 1303b356379aSAl Viro do { 1304b356379aSAl Viro struct path link = *path; 1305b356379aSAl Viro void *cookie; 1306574197e0SAl Viro 1307574197e0SAl Viro res = follow_link(&link, nd, &cookie); 1308b356379aSAl Viro if (!res) 1309b356379aSAl Viro res = walk_component(nd, path, &nd->last, 1310b356379aSAl Viro nd->last_type, LOOKUP_FOLLOW); 1311574197e0SAl Viro put_link(nd, &link, cookie); 1312b356379aSAl Viro } while (res > 0); 1313b356379aSAl Viro 1314b356379aSAl Viro current->link_count--; 1315b356379aSAl Viro nd->depth--; 1316b356379aSAl Viro return res; 1317b356379aSAl Viro } 1318b356379aSAl Viro 1319b356379aSAl Viro /* 13201da177e4SLinus Torvalds * Name resolution. 1321ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1322ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 13231da177e4SLinus Torvalds * 1324ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1325ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 13261da177e4SLinus Torvalds */ 13276de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 13281da177e4SLinus Torvalds { 13291da177e4SLinus Torvalds struct path next; 13301da177e4SLinus Torvalds int err; 13311da177e4SLinus Torvalds 13321da177e4SLinus Torvalds while (*name=='/') 13331da177e4SLinus Torvalds name++; 13341da177e4SLinus Torvalds if (!*name) 1335086e183aSAl Viro return 0; 13361da177e4SLinus Torvalds 13371da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 13381da177e4SLinus Torvalds for(;;) { 13391da177e4SLinus Torvalds unsigned long hash; 13401da177e4SLinus Torvalds struct qstr this; 13411da177e4SLinus Torvalds unsigned int c; 1342fe479a58SAl Viro int type; 13431da177e4SLinus Torvalds 134452094c8aSAl Viro err = may_lookup(nd); 13451da177e4SLinus Torvalds if (err) 13461da177e4SLinus Torvalds break; 13471da177e4SLinus Torvalds 13481da177e4SLinus Torvalds this.name = name; 13491da177e4SLinus Torvalds c = *(const unsigned char *)name; 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds hash = init_name_hash(); 13521da177e4SLinus Torvalds do { 13531da177e4SLinus Torvalds name++; 13541da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 13551da177e4SLinus Torvalds c = *(const unsigned char *)name; 13561da177e4SLinus Torvalds } while (c && (c != '/')); 13571da177e4SLinus Torvalds this.len = name - (const char *) this.name; 13581da177e4SLinus Torvalds this.hash = end_name_hash(hash); 13591da177e4SLinus Torvalds 1360fe479a58SAl Viro type = LAST_NORM; 1361fe479a58SAl Viro if (this.name[0] == '.') switch (this.len) { 1362fe479a58SAl Viro case 2: 136316c2cd71SAl Viro if (this.name[1] == '.') { 1364fe479a58SAl Viro type = LAST_DOTDOT; 136516c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 136616c2cd71SAl Viro } 1367fe479a58SAl Viro break; 1368fe479a58SAl Viro case 1: 1369fe479a58SAl Viro type = LAST_DOT; 1370fe479a58SAl Viro } 13715a202bcdSAl Viro if (likely(type == LAST_NORM)) { 13725a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 137316c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 13745a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 13755a202bcdSAl Viro err = parent->d_op->d_hash(parent, nd->inode, 13765a202bcdSAl Viro &this); 13775a202bcdSAl Viro if (err < 0) 13785a202bcdSAl Viro break; 13795a202bcdSAl Viro } 13805a202bcdSAl Viro } 1381fe479a58SAl Viro 13821da177e4SLinus Torvalds /* remove trailing slashes? */ 13831da177e4SLinus Torvalds if (!c) 13841da177e4SLinus Torvalds goto last_component; 13851da177e4SLinus Torvalds while (*++name == '/'); 13861da177e4SLinus Torvalds if (!*name) 1387b356379aSAl Viro goto last_component; 13881da177e4SLinus Torvalds 1389ce57dfc1SAl Viro err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW); 1390ce57dfc1SAl Viro if (err < 0) 1391ce57dfc1SAl Viro return err; 1392fe479a58SAl Viro 1393ce57dfc1SAl Viro if (err) { 1394b356379aSAl Viro err = nested_symlink(&next, nd); 13951da177e4SLinus Torvalds if (err) 1396a7472babSAl Viro return err; 139731e6b01fSNick Piggin } 13981da177e4SLinus Torvalds err = -ENOTDIR; 139931e6b01fSNick Piggin if (!nd->inode->i_op->lookup) 14001da177e4SLinus Torvalds break; 14011da177e4SLinus Torvalds continue; 14021da177e4SLinus Torvalds /* here ends the main loop */ 14031da177e4SLinus Torvalds 14041da177e4SLinus Torvalds last_component: 1405ce57dfc1SAl Viro nd->last = this; 1406ce57dfc1SAl Viro nd->last_type = type; 1407ce57dfc1SAl Viro return 0; 1408ce57dfc1SAl Viro } 1409951361f9SAl Viro terminate_walk(nd); 14101da177e4SLinus Torvalds return err; 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds 141370e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, 141470e9b357SAl Viro struct nameidata *nd, struct file **fp) 141531e6b01fSNick Piggin { 141631e6b01fSNick Piggin int retval = 0; 141731e6b01fSNick Piggin int fput_needed; 141831e6b01fSNick Piggin struct file *file; 141931e6b01fSNick Piggin 142031e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 142116c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 142231e6b01fSNick Piggin nd->depth = 0; 14235b6ca027SAl Viro if (flags & LOOKUP_ROOT) { 14245b6ca027SAl Viro struct inode *inode = nd->root.dentry->d_inode; 142573d049a4SAl Viro if (*name) { 14265b6ca027SAl Viro if (!inode->i_op->lookup) 14275b6ca027SAl Viro return -ENOTDIR; 14285b6ca027SAl Viro retval = inode_permission(inode, MAY_EXEC); 14295b6ca027SAl Viro if (retval) 14305b6ca027SAl Viro return retval; 143173d049a4SAl Viro } 14325b6ca027SAl Viro nd->path = nd->root; 14335b6ca027SAl Viro nd->inode = inode; 14345b6ca027SAl Viro if (flags & LOOKUP_RCU) { 14355b6ca027SAl Viro br_read_lock(vfsmount_lock); 14365b6ca027SAl Viro rcu_read_lock(); 14375b6ca027SAl Viro nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 14385b6ca027SAl Viro } else { 14395b6ca027SAl Viro path_get(&nd->path); 14405b6ca027SAl Viro } 14415b6ca027SAl Viro return 0; 14425b6ca027SAl Viro } 14435b6ca027SAl Viro 144431e6b01fSNick Piggin nd->root.mnt = NULL; 144531e6b01fSNick Piggin 144631e6b01fSNick Piggin if (*name=='/') { 1447e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 144831e6b01fSNick Piggin br_read_lock(vfsmount_lock); 144931e6b01fSNick Piggin rcu_read_lock(); 1450e41f7d4eSAl Viro set_root_rcu(nd); 1451e41f7d4eSAl Viro } else { 1452e41f7d4eSAl Viro set_root(nd); 1453e41f7d4eSAl Viro path_get(&nd->root); 1454e41f7d4eSAl Viro } 145531e6b01fSNick Piggin nd->path = nd->root; 145631e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 1457e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 145831e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1459c28cc364SNick Piggin unsigned seq; 146031e6b01fSNick Piggin 146131e6b01fSNick Piggin br_read_lock(vfsmount_lock); 146231e6b01fSNick Piggin rcu_read_lock(); 146331e6b01fSNick Piggin 1464c28cc364SNick Piggin do { 1465c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 146631e6b01fSNick Piggin nd->path = fs->pwd; 1467c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1468c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1469e41f7d4eSAl Viro } else { 1470e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 1471e41f7d4eSAl Viro } 147231e6b01fSNick Piggin } else { 147331e6b01fSNick Piggin struct dentry *dentry; 147431e6b01fSNick Piggin 14751abf0c71SAl Viro file = fget_raw_light(dfd, &fput_needed); 147631e6b01fSNick Piggin retval = -EBADF; 147731e6b01fSNick Piggin if (!file) 147831e6b01fSNick Piggin goto out_fail; 147931e6b01fSNick Piggin 148031e6b01fSNick Piggin dentry = file->f_path.dentry; 148131e6b01fSNick Piggin 1482f52e0c11SAl Viro if (*name) { 148331e6b01fSNick Piggin retval = -ENOTDIR; 148431e6b01fSNick Piggin if (!S_ISDIR(dentry->d_inode->i_mode)) 148531e6b01fSNick Piggin goto fput_fail; 148631e6b01fSNick Piggin 14874ad5abb3SAl Viro retval = inode_permission(dentry->d_inode, MAY_EXEC); 148831e6b01fSNick Piggin if (retval) 148931e6b01fSNick Piggin goto fput_fail; 1490f52e0c11SAl Viro } 149131e6b01fSNick Piggin 149231e6b01fSNick Piggin nd->path = file->f_path; 1493e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 149431e6b01fSNick Piggin if (fput_needed) 149570e9b357SAl Viro *fp = file; 1496c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 149731e6b01fSNick Piggin br_read_lock(vfsmount_lock); 149831e6b01fSNick Piggin rcu_read_lock(); 14995590ff0dSUlrich Drepper } else { 15005dd784d0SJan Blunck path_get(&file->f_path); 15015590ff0dSUlrich Drepper fput_light(file, fput_needed); 15021da177e4SLinus Torvalds } 1503e41f7d4eSAl Viro } 1504e41f7d4eSAl Viro 150531e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 15069b4a9b14SAl Viro return 0; 15072dfdd266SJosef 'Jeff' Sipek 15089b4a9b14SAl Viro fput_fail: 15099b4a9b14SAl Viro fput_light(file, fput_needed); 15109b4a9b14SAl Viro out_fail: 15119b4a9b14SAl Viro return retval; 15129b4a9b14SAl Viro } 15139b4a9b14SAl Viro 1514bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path) 1515bd92d7feSAl Viro { 1516bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 1517bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 1518bd92d7feSAl Viro 1519bd92d7feSAl Viro nd->flags &= ~LOOKUP_PARENT; 1520bd92d7feSAl Viro return walk_component(nd, path, &nd->last, nd->last_type, 1521bd92d7feSAl Viro nd->flags & LOOKUP_FOLLOW); 1522bd92d7feSAl Viro } 1523bd92d7feSAl Viro 15249b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1525ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name, 15269b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 15279b4a9b14SAl Viro { 152870e9b357SAl Viro struct file *base = NULL; 1529bd92d7feSAl Viro struct path path; 1530bd92d7feSAl Viro int err; 153131e6b01fSNick Piggin 153231e6b01fSNick Piggin /* 153331e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 153431e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 153531e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 153631e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 153731e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 153831e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 153931e6b01fSNick Piggin * analogue, foo_rcu(). 154031e6b01fSNick Piggin * 154131e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 154231e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 154331e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 154431e6b01fSNick Piggin * be able to complete). 154531e6b01fSNick Piggin */ 1546bd92d7feSAl Viro err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); 1547ee0827cdSAl Viro 1548bd92d7feSAl Viro if (unlikely(err)) 1549bd92d7feSAl Viro return err; 1550ee0827cdSAl Viro 1551ee0827cdSAl Viro current->total_link_count = 0; 1552bd92d7feSAl Viro err = link_path_walk(name, nd); 1553bd92d7feSAl Viro 1554bd92d7feSAl Viro if (!err && !(flags & LOOKUP_PARENT)) { 1555bd92d7feSAl Viro err = lookup_last(nd, &path); 1556bd92d7feSAl Viro while (err > 0) { 1557bd92d7feSAl Viro void *cookie; 1558bd92d7feSAl Viro struct path link = path; 1559bd92d7feSAl Viro nd->flags |= LOOKUP_PARENT; 1560574197e0SAl Viro err = follow_link(&link, nd, &cookie); 1561bd92d7feSAl Viro if (!err) 1562bd92d7feSAl Viro err = lookup_last(nd, &path); 1563574197e0SAl Viro put_link(nd, &link, cookie); 1564bd92d7feSAl Viro } 1565bd92d7feSAl Viro } 1566ee0827cdSAl Viro 15679f1fafeeSAl Viro if (!err) 15689f1fafeeSAl Viro err = complete_walk(nd); 1569bd92d7feSAl Viro 1570bd92d7feSAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) { 1571bd92d7feSAl Viro if (!nd->inode->i_op->lookup) { 1572bd92d7feSAl Viro path_put(&nd->path); 1573bd23a539SAl Viro err = -ENOTDIR; 1574bd92d7feSAl Viro } 1575bd92d7feSAl Viro } 157616c2cd71SAl Viro 157770e9b357SAl Viro if (base) 157870e9b357SAl Viro fput(base); 1579ee0827cdSAl Viro 15805b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 158131e6b01fSNick Piggin path_put(&nd->root); 158231e6b01fSNick Piggin nd->root.mnt = NULL; 158331e6b01fSNick Piggin } 1584bd92d7feSAl Viro return err; 158531e6b01fSNick Piggin } 158631e6b01fSNick Piggin 1587ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name, 1588ee0827cdSAl Viro unsigned int flags, struct nameidata *nd) 1589ee0827cdSAl Viro { 1590ee0827cdSAl Viro int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd); 1591ee0827cdSAl Viro if (unlikely(retval == -ECHILD)) 1592ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags, nd); 1593ee0827cdSAl Viro if (unlikely(retval == -ESTALE)) 1594ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd); 1595ee0827cdSAl Viro 159631e6b01fSNick Piggin if (likely(!retval)) { 159731e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) { 159831e6b01fSNick Piggin if (nd->path.dentry && nd->inode) 159931e6b01fSNick Piggin audit_inode(name, nd->path.dentry); 160031e6b01fSNick Piggin } 160131e6b01fSNick Piggin } 1602170aa3d0SUlrich Drepper return retval; 16031da177e4SLinus Torvalds } 16041da177e4SLinus Torvalds 1605c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd) 16065590ff0dSUlrich Drepper { 1607c9c6cac0SAl Viro return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd); 16085590ff0dSUlrich Drepper } 16095590ff0dSUlrich Drepper 1610d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1611d1811465SAl Viro { 1612d1811465SAl Viro struct nameidata nd; 1613d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1614d1811465SAl Viro if (!res) 1615d1811465SAl Viro *path = nd.path; 1616d1811465SAl Viro return res; 1617d1811465SAl Viro } 1618d1811465SAl Viro 161916f18200SJosef 'Jeff' Sipek /** 162016f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 162116f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 162216f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 162316f18200SJosef 'Jeff' Sipek * @name: pointer to file name 162416f18200SJosef 'Jeff' Sipek * @flags: lookup flags 1625e0a01249SAl Viro * @path: pointer to struct path to fill 162616f18200SJosef 'Jeff' Sipek */ 162716f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 162816f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 1629e0a01249SAl Viro struct path *path) 163016f18200SJosef 'Jeff' Sipek { 1631e0a01249SAl Viro struct nameidata nd; 1632e0a01249SAl Viro int err; 1633e0a01249SAl Viro nd.root.dentry = dentry; 1634e0a01249SAl Viro nd.root.mnt = mnt; 1635e0a01249SAl Viro BUG_ON(flags & LOOKUP_PARENT); 16365b6ca027SAl Viro /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ 1637e0a01249SAl Viro err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd); 1638e0a01249SAl Viro if (!err) 1639e0a01249SAl Viro *path = nd.path; 1640e0a01249SAl Viro return err; 164116f18200SJosef 'Jeff' Sipek } 164216f18200SJosef 'Jeff' Sipek 1643eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name, 1644eead1911SChristoph Hellwig struct dentry *base, struct nameidata *nd) 16451da177e4SLinus Torvalds { 164681fca444SChristoph Hellwig struct inode *inode = base->d_inode; 16471da177e4SLinus Torvalds struct dentry *dentry; 16481da177e4SLinus Torvalds int err; 16491da177e4SLinus Torvalds 16504ad5abb3SAl Viro err = inode_permission(inode, MAY_EXEC); 165181fca444SChristoph Hellwig if (err) 165281fca444SChristoph Hellwig return ERR_PTR(err); 16531da177e4SLinus Torvalds 16541da177e4SLinus Torvalds /* 1655b04f784eSNick Piggin * Don't bother with __d_lookup: callers are for creat as 1656b04f784eSNick Piggin * well as unlink, so a lot of the time it would cost 1657b04f784eSNick Piggin * a double lookup. 16586e6b1bd1SAl Viro */ 16596e6b1bd1SAl Viro dentry = d_lookup(base, name); 16606e6b1bd1SAl Viro 166144396f4bSJosef Bacik if (dentry && d_need_lookup(dentry)) { 166244396f4bSJosef Bacik /* 166344396f4bSJosef Bacik * __lookup_hash is called with the parent dir's i_mutex already 166444396f4bSJosef Bacik * held, so we are good to go here. 166544396f4bSJosef Bacik */ 166644396f4bSJosef Bacik dentry = d_inode_lookup(base, dentry, nd); 166744396f4bSJosef Bacik if (IS_ERR(dentry)) 166844396f4bSJosef Bacik return dentry; 166944396f4bSJosef Bacik } 167044396f4bSJosef Bacik 1671d2d9e9fbSAl Viro if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) { 1672d2d9e9fbSAl Viro int status = d_revalidate(dentry, nd); 1673d2d9e9fbSAl Viro if (unlikely(status <= 0)) { 1674d2d9e9fbSAl Viro /* 1675d2d9e9fbSAl Viro * The dentry failed validation. 1676d2d9e9fbSAl Viro * If d_revalidate returned 0 attempt to invalidate 1677d2d9e9fbSAl Viro * the dentry otherwise d_revalidate is asking us 1678d2d9e9fbSAl Viro * to return a fail status. 1679d2d9e9fbSAl Viro */ 1680d2d9e9fbSAl Viro if (status < 0) { 1681d2d9e9fbSAl Viro dput(dentry); 1682d2d9e9fbSAl Viro return ERR_PTR(status); 1683d2d9e9fbSAl Viro } else if (!d_invalidate(dentry)) { 1684d2d9e9fbSAl Viro dput(dentry); 1685d2d9e9fbSAl Viro dentry = NULL; 1686d2d9e9fbSAl Viro } 1687d2d9e9fbSAl Viro } 1688d2d9e9fbSAl Viro } 16896e6b1bd1SAl Viro 16901da177e4SLinus Torvalds if (!dentry) 1691baa03890SNick Piggin dentry = d_alloc_and_lookup(base, name, nd); 16925a202bcdSAl Viro 16931da177e4SLinus Torvalds return dentry; 16941da177e4SLinus Torvalds } 16951da177e4SLinus Torvalds 1696057f6c01SJames Morris /* 1697057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1698057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1699057f6c01SJames Morris * SMP-safe. 1700057f6c01SJames Morris */ 1701a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 17021da177e4SLinus Torvalds { 17034ac91378SJan Blunck return __lookup_hash(&nd->last, nd->path.dentry, nd); 17041da177e4SLinus Torvalds } 17051da177e4SLinus Torvalds 1706eead1911SChristoph Hellwig /** 1707a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1708eead1911SChristoph Hellwig * @name: pathname component to lookup 1709eead1911SChristoph Hellwig * @base: base directory to lookup from 1710eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1711eead1911SChristoph Hellwig * 1712a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1713a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1714eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1715eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1716eead1911SChristoph Hellwig */ 1717057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1718057f6c01SJames Morris { 1719057f6c01SJames Morris struct qstr this; 17206a96ba54SAl Viro unsigned long hash; 17216a96ba54SAl Viro unsigned int c; 1722057f6c01SJames Morris 17232f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 17242f9092e1SDavid Woodhouse 17256a96ba54SAl Viro this.name = name; 17266a96ba54SAl Viro this.len = len; 17276a96ba54SAl Viro if (!len) 17286a96ba54SAl Viro return ERR_PTR(-EACCES); 17296a96ba54SAl Viro 17306a96ba54SAl Viro hash = init_name_hash(); 17316a96ba54SAl Viro while (len--) { 17326a96ba54SAl Viro c = *(const unsigned char *)name++; 17336a96ba54SAl Viro if (c == '/' || c == '\0') 17346a96ba54SAl Viro return ERR_PTR(-EACCES); 17356a96ba54SAl Viro hash = partial_name_hash(c, hash); 17366a96ba54SAl Viro } 17376a96ba54SAl Viro this.hash = end_name_hash(hash); 17385a202bcdSAl Viro /* 17395a202bcdSAl Viro * See if the low-level filesystem might want 17405a202bcdSAl Viro * to use its own hash.. 17415a202bcdSAl Viro */ 17425a202bcdSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 17435a202bcdSAl Viro int err = base->d_op->d_hash(base, base->d_inode, &this); 17445a202bcdSAl Viro if (err < 0) 17455a202bcdSAl Viro return ERR_PTR(err); 17465a202bcdSAl Viro } 1747eead1911SChristoph Hellwig 174849705b77SChristoph Hellwig return __lookup_hash(&this, base, NULL); 1749057f6c01SJames Morris } 1750057f6c01SJames Morris 17512d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags, 17522d8f3038SAl Viro struct path *path) 17531da177e4SLinus Torvalds { 17542d8f3038SAl Viro struct nameidata nd; 1755f52e0c11SAl Viro char *tmp = getname_flags(name, flags); 17561da177e4SLinus Torvalds int err = PTR_ERR(tmp); 17571da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 17582d8f3038SAl Viro 17592d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 17602d8f3038SAl Viro 17612d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 17621da177e4SLinus Torvalds putname(tmp); 17632d8f3038SAl Viro if (!err) 17642d8f3038SAl Viro *path = nd.path; 17651da177e4SLinus Torvalds } 17661da177e4SLinus Torvalds return err; 17671da177e4SLinus Torvalds } 17681da177e4SLinus Torvalds 17692ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 17702ad94ae6SAl Viro struct nameidata *nd, char **name) 17712ad94ae6SAl Viro { 17722ad94ae6SAl Viro char *s = getname(path); 17732ad94ae6SAl Viro int error; 17742ad94ae6SAl Viro 17752ad94ae6SAl Viro if (IS_ERR(s)) 17762ad94ae6SAl Viro return PTR_ERR(s); 17772ad94ae6SAl Viro 17782ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 17792ad94ae6SAl Viro if (error) 17802ad94ae6SAl Viro putname(s); 17812ad94ae6SAl Viro else 17822ad94ae6SAl Viro *name = s; 17832ad94ae6SAl Viro 17842ad94ae6SAl Viro return error; 17852ad94ae6SAl Viro } 17862ad94ae6SAl Viro 17871da177e4SLinus Torvalds /* 17881da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 17891da177e4SLinus Torvalds * minimal. 17901da177e4SLinus Torvalds */ 17911da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 17921da177e4SLinus Torvalds { 1793da9592edSDavid Howells uid_t fsuid = current_fsuid(); 1794da9592edSDavid Howells 17951da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 17961da177e4SLinus Torvalds return 0; 1797e795b717SSerge E. Hallyn if (current_user_ns() != inode_userns(inode)) 1798e795b717SSerge E. Hallyn goto other_userns; 1799da9592edSDavid Howells if (inode->i_uid == fsuid) 18001da177e4SLinus Torvalds return 0; 1801da9592edSDavid Howells if (dir->i_uid == fsuid) 18021da177e4SLinus Torvalds return 0; 1803e795b717SSerge E. Hallyn 1804e795b717SSerge E. Hallyn other_userns: 1805e795b717SSerge E. Hallyn return !ns_capable(inode_userns(inode), CAP_FOWNER); 18061da177e4SLinus Torvalds } 18071da177e4SLinus Torvalds 18081da177e4SLinus Torvalds /* 18091da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 18101da177e4SLinus Torvalds * whether the type of victim is right. 18111da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 18121da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 18131da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 18141da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 18151da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 18161da177e4SLinus Torvalds * a. be owner of dir, or 18171da177e4SLinus Torvalds * b. be owner of victim, or 18181da177e4SLinus Torvalds * c. have CAP_FOWNER capability 18191da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 18201da177e4SLinus Torvalds * links pointing to it. 18211da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 18221da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 18231da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 18241da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 18251da177e4SLinus Torvalds * nfs_async_unlink(). 18261da177e4SLinus Torvalds */ 1827858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 18281da177e4SLinus Torvalds { 18291da177e4SLinus Torvalds int error; 18301da177e4SLinus Torvalds 18311da177e4SLinus Torvalds if (!victim->d_inode) 18321da177e4SLinus Torvalds return -ENOENT; 18331da177e4SLinus Torvalds 18341da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 1835cccc6bbaSAl Viro audit_inode_child(victim, dir); 18361da177e4SLinus Torvalds 1837f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 18381da177e4SLinus Torvalds if (error) 18391da177e4SLinus Torvalds return error; 18401da177e4SLinus Torvalds if (IS_APPEND(dir)) 18411da177e4SLinus Torvalds return -EPERM; 18421da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 1843f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 18441da177e4SLinus Torvalds return -EPERM; 18451da177e4SLinus Torvalds if (isdir) { 18461da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 18471da177e4SLinus Torvalds return -ENOTDIR; 18481da177e4SLinus Torvalds if (IS_ROOT(victim)) 18491da177e4SLinus Torvalds return -EBUSY; 18501da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 18511da177e4SLinus Torvalds return -EISDIR; 18521da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 18531da177e4SLinus Torvalds return -ENOENT; 18541da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 18551da177e4SLinus Torvalds return -EBUSY; 18561da177e4SLinus Torvalds return 0; 18571da177e4SLinus Torvalds } 18581da177e4SLinus Torvalds 18591da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 18601da177e4SLinus Torvalds * dir. 18611da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 18621da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 18631da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 18641da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 18651da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 18661da177e4SLinus Torvalds */ 1867a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 18681da177e4SLinus Torvalds { 18691da177e4SLinus Torvalds if (child->d_inode) 18701da177e4SLinus Torvalds return -EEXIST; 18711da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 18721da177e4SLinus Torvalds return -ENOENT; 1873f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 18741da177e4SLinus Torvalds } 18751da177e4SLinus Torvalds 18761da177e4SLinus Torvalds /* 18771da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 18781da177e4SLinus Torvalds */ 18791da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 18801da177e4SLinus Torvalds { 18811da177e4SLinus Torvalds struct dentry *p; 18821da177e4SLinus Torvalds 18831da177e4SLinus Torvalds if (p1 == p2) { 1884f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 18851da177e4SLinus Torvalds return NULL; 18861da177e4SLinus Torvalds } 18871da177e4SLinus Torvalds 1888a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 18891da177e4SLinus Torvalds 1890e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 1891e2761a11SOGAWA Hirofumi if (p) { 1892f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 1893f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 18941da177e4SLinus Torvalds return p; 18951da177e4SLinus Torvalds } 18961da177e4SLinus Torvalds 1897e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 1898e2761a11SOGAWA Hirofumi if (p) { 1899f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1900f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 19011da177e4SLinus Torvalds return p; 19021da177e4SLinus Torvalds } 19031da177e4SLinus Torvalds 1904f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1905f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 19061da177e4SLinus Torvalds return NULL; 19071da177e4SLinus Torvalds } 19081da177e4SLinus Torvalds 19091da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 19101da177e4SLinus Torvalds { 19111b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 19121da177e4SLinus Torvalds if (p1 != p2) { 19131b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 1914a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 19151da177e4SLinus Torvalds } 19161da177e4SLinus Torvalds } 19171da177e4SLinus Torvalds 19181da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode, 19191da177e4SLinus Torvalds struct nameidata *nd) 19201da177e4SLinus Torvalds { 1921a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 19221da177e4SLinus Torvalds 19231da177e4SLinus Torvalds if (error) 19241da177e4SLinus Torvalds return error; 19251da177e4SLinus Torvalds 1926acfa4380SAl Viro if (!dir->i_op->create) 19271da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 19281da177e4SLinus Torvalds mode &= S_IALLUGO; 19291da177e4SLinus Torvalds mode |= S_IFREG; 19301da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 19311da177e4SLinus Torvalds if (error) 19321da177e4SLinus Torvalds return error; 19331da177e4SLinus Torvalds error = dir->i_op->create(dir, dentry, mode, nd); 1934a74574aaSStephen Smalley if (!error) 1935f38aa942SAmy Griffis fsnotify_create(dir, dentry); 19361da177e4SLinus Torvalds return error; 19371da177e4SLinus Torvalds } 19381da177e4SLinus Torvalds 193973d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag) 19401da177e4SLinus Torvalds { 19413fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 19421da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 19431da177e4SLinus Torvalds int error; 19441da177e4SLinus Torvalds 1945bcda7652SAl Viro /* O_PATH? */ 1946bcda7652SAl Viro if (!acc_mode) 1947bcda7652SAl Viro return 0; 1948bcda7652SAl Viro 19491da177e4SLinus Torvalds if (!inode) 19501da177e4SLinus Torvalds return -ENOENT; 19511da177e4SLinus Torvalds 1952c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 1953c8fe8f30SChristoph Hellwig case S_IFLNK: 19541da177e4SLinus Torvalds return -ELOOP; 1955c8fe8f30SChristoph Hellwig case S_IFDIR: 1956c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 19571da177e4SLinus Torvalds return -EISDIR; 1958c8fe8f30SChristoph Hellwig break; 1959c8fe8f30SChristoph Hellwig case S_IFBLK: 1960c8fe8f30SChristoph Hellwig case S_IFCHR: 19613fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 19621da177e4SLinus Torvalds return -EACCES; 1963c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 1964c8fe8f30SChristoph Hellwig case S_IFIFO: 1965c8fe8f30SChristoph Hellwig case S_IFSOCK: 19661da177e4SLinus Torvalds flag &= ~O_TRUNC; 1967c8fe8f30SChristoph Hellwig break; 19684a3fd211SDave Hansen } 1969b41572e9SDave Hansen 19703fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 1971b41572e9SDave Hansen if (error) 1972b41572e9SDave Hansen return error; 19736146f0d5SMimi Zohar 19741da177e4SLinus Torvalds /* 19751da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 19761da177e4SLinus Torvalds */ 19771da177e4SLinus Torvalds if (IS_APPEND(inode)) { 19788737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 19797715b521SAl Viro return -EPERM; 19801da177e4SLinus Torvalds if (flag & O_TRUNC) 19817715b521SAl Viro return -EPERM; 19821da177e4SLinus Torvalds } 19831da177e4SLinus Torvalds 19841da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 19852e149670SSerge E. Hallyn if (flag & O_NOATIME && !inode_owner_or_capable(inode)) 19867715b521SAl Viro return -EPERM; 19871da177e4SLinus Torvalds 19881da177e4SLinus Torvalds /* 19891da177e4SLinus Torvalds * Ensure there are no outstanding leases on the file. 19901da177e4SLinus Torvalds */ 1991b65a9cfcSAl Viro return break_lease(inode, flag); 19927715b521SAl Viro } 19937715b521SAl Viro 1994e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 19957715b521SAl Viro { 1996e1181ee6SJeff Layton struct path *path = &filp->f_path; 19977715b521SAl Viro struct inode *inode = path->dentry->d_inode; 19987715b521SAl Viro int error = get_write_access(inode); 19991da177e4SLinus Torvalds if (error) 20007715b521SAl Viro return error; 20011da177e4SLinus Torvalds /* 20021da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 20031da177e4SLinus Torvalds */ 20041da177e4SLinus Torvalds error = locks_verify_locked(inode); 2005be6d3e56SKentaro Takeda if (!error) 2006ea0d3ab2STetsuo Handa error = security_path_truncate(path); 20071da177e4SLinus Torvalds if (!error) { 20087715b521SAl Viro error = do_truncate(path->dentry, 0, 2009d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 2010e1181ee6SJeff Layton filp); 20111da177e4SLinus Torvalds } 20121da177e4SLinus Torvalds put_write_access(inode); 2013acd0c935SMimi Zohar return error; 20141da177e4SLinus Torvalds } 20151da177e4SLinus Torvalds 2016d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2017d57999e1SDave Hansen { 20188a5e929dSAl Viro if ((flag & O_ACCMODE) == 3) 20198a5e929dSAl Viro flag--; 2020d57999e1SDave Hansen return flag; 2021d57999e1SDave Hansen } 2022d57999e1SDave Hansen 202331e6b01fSNick Piggin /* 2024fe2d35ffSAl Viro * Handle the last step of open() 202531e6b01fSNick Piggin */ 2026fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path, 2027c3e380b0SAl Viro const struct open_flags *op, const char *pathname) 2028fb1cc555SAl Viro { 2029a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 20306c0d46c4SAl Viro struct dentry *dentry; 2031ca344a89SAl Viro int open_flag = op->open_flag; 20326c0d46c4SAl Viro int will_truncate = open_flag & O_TRUNC; 2033ca344a89SAl Viro int want_write = 0; 2034bcda7652SAl Viro int acc_mode = op->acc_mode; 2035fb1cc555SAl Viro struct file *filp; 203616c2cd71SAl Viro int error; 2037fb1cc555SAl Viro 2038c3e380b0SAl Viro nd->flags &= ~LOOKUP_PARENT; 2039c3e380b0SAl Viro nd->flags |= op->intent; 2040c3e380b0SAl Viro 20411f36f774SAl Viro switch (nd->last_type) { 20421f36f774SAl Viro case LAST_DOTDOT: 2043176306f5SNeil Brown case LAST_DOT: 2044fe2d35ffSAl Viro error = handle_dots(nd, nd->last_type); 2045fe2d35ffSAl Viro if (error) 2046fe2d35ffSAl Viro return ERR_PTR(error); 20471f36f774SAl Viro /* fallthrough */ 20481f36f774SAl Viro case LAST_ROOT: 20499f1fafeeSAl Viro error = complete_walk(nd); 205016c2cd71SAl Viro if (error) 20519f1fafeeSAl Viro return ERR_PTR(error); 2052fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2053ca344a89SAl Viro if (open_flag & O_CREAT) { 205416c2cd71SAl Viro error = -EISDIR; 20551f36f774SAl Viro goto exit; 2056fe2d35ffSAl Viro } 2057fe2d35ffSAl Viro goto ok; 20581f36f774SAl Viro case LAST_BIND: 20599f1fafeeSAl Viro error = complete_walk(nd); 206016c2cd71SAl Viro if (error) 20619f1fafeeSAl Viro return ERR_PTR(error); 20621f36f774SAl Viro audit_inode(pathname, dir); 20631f36f774SAl Viro goto ok; 20641f36f774SAl Viro } 2065a2c36b45SAl Viro 2066ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 2067bcda7652SAl Viro int symlink_ok = 0; 2068fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 2069fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2070bcda7652SAl Viro if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) 2071bcda7652SAl Viro symlink_ok = 1; 2072fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 2073ce57dfc1SAl Viro error = walk_component(nd, path, &nd->last, LAST_NORM, 2074ce57dfc1SAl Viro !symlink_ok); 2075ce57dfc1SAl Viro if (error < 0) 2076fe2d35ffSAl Viro return ERR_PTR(error); 2077ce57dfc1SAl Viro if (error) /* symlink */ 2078fe2d35ffSAl Viro return NULL; 2079fe2d35ffSAl Viro /* sayonara */ 20809f1fafeeSAl Viro error = complete_walk(nd); 20819f1fafeeSAl Viro if (error) 2082fe2d35ffSAl Viro return ERR_PTR(-ECHILD); 2083fe2d35ffSAl Viro 2084fe2d35ffSAl Viro error = -ENOTDIR; 2085fe2d35ffSAl Viro if (nd->flags & LOOKUP_DIRECTORY) { 2086ce57dfc1SAl Viro if (!nd->inode->i_op->lookup) 2087fe2d35ffSAl Viro goto exit; 2088fe2d35ffSAl Viro } 2089fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2090fe2d35ffSAl Viro goto ok; 2091fe2d35ffSAl Viro } 2092fe2d35ffSAl Viro 2093fe2d35ffSAl Viro /* create side of things */ 20949f1fafeeSAl Viro error = complete_walk(nd); 20959f1fafeeSAl Viro if (error) 20969f1fafeeSAl Viro return ERR_PTR(error); 2097fe2d35ffSAl Viro 2098fe2d35ffSAl Viro audit_inode(pathname, dir); 209916c2cd71SAl Viro error = -EISDIR; 21001f36f774SAl Viro /* trailing slashes? */ 210131e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 21021f36f774SAl Viro goto exit; 21031f36f774SAl Viro 2104a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 2105a1e28038SAl Viro 21066c0d46c4SAl Viro dentry = lookup_hash(nd); 21076c0d46c4SAl Viro error = PTR_ERR(dentry); 21086c0d46c4SAl Viro if (IS_ERR(dentry)) { 2109fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2110fb1cc555SAl Viro goto exit; 2111fb1cc555SAl Viro } 2112fb1cc555SAl Viro 21136c0d46c4SAl Viro path->dentry = dentry; 21146c0d46c4SAl Viro path->mnt = nd->path.mnt; 21156c0d46c4SAl Viro 2116fb1cc555SAl Viro /* Negative dentry, just create the file */ 21176c0d46c4SAl Viro if (!dentry->d_inode) { 21186c0d46c4SAl Viro int mode = op->mode; 21196c0d46c4SAl Viro if (!IS_POSIXACL(dir->d_inode)) 21206c0d46c4SAl Viro mode &= ~current_umask(); 2121fb1cc555SAl Viro /* 2122fb1cc555SAl Viro * This write is needed to ensure that a 21236c0d46c4SAl Viro * rw->ro transition does not occur between 2124fb1cc555SAl Viro * the time when the file is created and when 2125fb1cc555SAl Viro * a permanent write count is taken through 2126fb1cc555SAl Viro * the 'struct file' in nameidata_to_filp(). 2127fb1cc555SAl Viro */ 2128fb1cc555SAl Viro error = mnt_want_write(nd->path.mnt); 2129fb1cc555SAl Viro if (error) 2130fb1cc555SAl Viro goto exit_mutex_unlock; 2131ca344a89SAl Viro want_write = 1; 21329b44f1b3SAl Viro /* Don't check for write permission, don't truncate */ 2133ca344a89SAl Viro open_flag &= ~O_TRUNC; 21346c0d46c4SAl Viro will_truncate = 0; 2135bcda7652SAl Viro acc_mode = MAY_OPEN; 21366c0d46c4SAl Viro error = security_path_mknod(&nd->path, dentry, mode, 0); 21376c0d46c4SAl Viro if (error) 21386c0d46c4SAl Viro goto exit_mutex_unlock; 21396c0d46c4SAl Viro error = vfs_create(dir->d_inode, dentry, mode, nd); 21406c0d46c4SAl Viro if (error) 21416c0d46c4SAl Viro goto exit_mutex_unlock; 21426c0d46c4SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 21436c0d46c4SAl Viro dput(nd->path.dentry); 21446c0d46c4SAl Viro nd->path.dentry = dentry; 2145ca344a89SAl Viro goto common; 2146fb1cc555SAl Viro } 2147fb1cc555SAl Viro 2148fb1cc555SAl Viro /* 2149fb1cc555SAl Viro * It already exists. 2150fb1cc555SAl Viro */ 2151fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2152fb1cc555SAl Viro audit_inode(pathname, path->dentry); 2153fb1cc555SAl Viro 2154fb1cc555SAl Viro error = -EEXIST; 2155ca344a89SAl Viro if (open_flag & O_EXCL) 2156fb1cc555SAl Viro goto exit_dput; 2157fb1cc555SAl Viro 21589875cf80SDavid Howells error = follow_managed(path, nd->flags); 21599875cf80SDavid Howells if (error < 0) 2160fb1cc555SAl Viro goto exit_dput; 2161fb1cc555SAl Viro 2162fb1cc555SAl Viro error = -ENOENT; 2163fb1cc555SAl Viro if (!path->dentry->d_inode) 2164fb1cc555SAl Viro goto exit_dput; 21659e67f361SAl Viro 21669e67f361SAl Viro if (path->dentry->d_inode->i_op->follow_link) 2167fb1cc555SAl Viro return NULL; 2168fb1cc555SAl Viro 2169fb1cc555SAl Viro path_to_nameidata(path, nd); 217031e6b01fSNick Piggin nd->inode = path->dentry->d_inode; 2171fb1cc555SAl Viro error = -EISDIR; 217231e6b01fSNick Piggin if (S_ISDIR(nd->inode->i_mode)) 2173fb1cc555SAl Viro goto exit; 217467ee3ad2SAl Viro ok: 21756c0d46c4SAl Viro if (!S_ISREG(nd->inode->i_mode)) 21766c0d46c4SAl Viro will_truncate = 0; 21776c0d46c4SAl Viro 21780f9d1a10SAl Viro if (will_truncate) { 21790f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 21800f9d1a10SAl Viro if (error) 21810f9d1a10SAl Viro goto exit; 2182ca344a89SAl Viro want_write = 1; 21830f9d1a10SAl Viro } 2184ca344a89SAl Viro common: 2185bcda7652SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2186ca344a89SAl Viro if (error) 21870f9d1a10SAl Viro goto exit; 21880f9d1a10SAl Viro filp = nameidata_to_filp(nd); 21890f9d1a10SAl Viro if (!IS_ERR(filp)) { 21900f9d1a10SAl Viro error = ima_file_check(filp, op->acc_mode); 21910f9d1a10SAl Viro if (error) { 21920f9d1a10SAl Viro fput(filp); 21930f9d1a10SAl Viro filp = ERR_PTR(error); 21940f9d1a10SAl Viro } 21950f9d1a10SAl Viro } 21960f9d1a10SAl Viro if (!IS_ERR(filp)) { 21970f9d1a10SAl Viro if (will_truncate) { 21980f9d1a10SAl Viro error = handle_truncate(filp); 21990f9d1a10SAl Viro if (error) { 22000f9d1a10SAl Viro fput(filp); 22010f9d1a10SAl Viro filp = ERR_PTR(error); 22020f9d1a10SAl Viro } 22030f9d1a10SAl Viro } 22040f9d1a10SAl Viro } 2205ca344a89SAl Viro out: 2206ca344a89SAl Viro if (want_write) 22070f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 22080f9d1a10SAl Viro path_put(&nd->path); 2209fb1cc555SAl Viro return filp; 2210fb1cc555SAl Viro 2211fb1cc555SAl Viro exit_mutex_unlock: 2212fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2213fb1cc555SAl Viro exit_dput: 2214fb1cc555SAl Viro path_put_conditional(path, nd); 2215fb1cc555SAl Viro exit: 2216ca344a89SAl Viro filp = ERR_PTR(error); 2217ca344a89SAl Viro goto out; 2218fb1cc555SAl Viro } 2219fb1cc555SAl Viro 222013aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname, 222173d049a4SAl Viro struct nameidata *nd, const struct open_flags *op, int flags) 22221da177e4SLinus Torvalds { 2223fe2d35ffSAl Viro struct file *base = NULL; 22244a3fd211SDave Hansen struct file *filp; 22259850c056SAl Viro struct path path; 222613aab428SAl Viro int error; 222731e6b01fSNick Piggin 222831e6b01fSNick Piggin filp = get_empty_filp(); 222931e6b01fSNick Piggin if (!filp) 223031e6b01fSNick Piggin return ERR_PTR(-ENFILE); 223131e6b01fSNick Piggin 223247c805dcSAl Viro filp->f_flags = op->open_flag; 223373d049a4SAl Viro nd->intent.open.file = filp; 223473d049a4SAl Viro nd->intent.open.flags = open_to_namei_flags(op->open_flag); 223573d049a4SAl Viro nd->intent.open.create_mode = op->mode; 223631e6b01fSNick Piggin 223773d049a4SAl Viro error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base); 223831e6b01fSNick Piggin if (unlikely(error)) 223913aab428SAl Viro goto out_filp; 224031e6b01fSNick Piggin 2241fe2d35ffSAl Viro current->total_link_count = 0; 224273d049a4SAl Viro error = link_path_walk(pathname, nd); 224331e6b01fSNick Piggin if (unlikely(error)) 224431e6b01fSNick Piggin goto out_filp; 22451da177e4SLinus Torvalds 224673d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2247806b681cSAl Viro while (unlikely(!filp)) { /* trailing symlink */ 22487b9337aaSNick Piggin struct path link = path; 2249def4af30SAl Viro void *cookie; 2250574197e0SAl Viro if (!(nd->flags & LOOKUP_FOLLOW)) { 225173d049a4SAl Viro path_put_conditional(&path, nd); 225273d049a4SAl Viro path_put(&nd->path); 225340b39136SAl Viro filp = ERR_PTR(-ELOOP); 225440b39136SAl Viro break; 225540b39136SAl Viro } 225673d049a4SAl Viro nd->flags |= LOOKUP_PARENT; 225773d049a4SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 2258574197e0SAl Viro error = follow_link(&link, nd, &cookie); 2259c3e380b0SAl Viro if (unlikely(error)) 2260f1afe9efSAl Viro filp = ERR_PTR(error); 2261c3e380b0SAl Viro else 226273d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2263574197e0SAl Viro put_link(nd, &link, cookie); 2264806b681cSAl Viro } 226510fa8e62SAl Viro out: 226673d049a4SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) 226773d049a4SAl Viro path_put(&nd->root); 2268fe2d35ffSAl Viro if (base) 2269fe2d35ffSAl Viro fput(base); 227073d049a4SAl Viro release_open_intent(nd); 227110fa8e62SAl Viro return filp; 22721da177e4SLinus Torvalds 227331e6b01fSNick Piggin out_filp: 227410fa8e62SAl Viro filp = ERR_PTR(error); 227510fa8e62SAl Viro goto out; 2276de459215SKirill Korotaev } 22771da177e4SLinus Torvalds 227813aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname, 227913aab428SAl Viro const struct open_flags *op, int flags) 228013aab428SAl Viro { 228173d049a4SAl Viro struct nameidata nd; 228213aab428SAl Viro struct file *filp; 228313aab428SAl Viro 228473d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); 228513aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 228673d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags); 228713aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 228873d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); 228913aab428SAl Viro return filp; 229013aab428SAl Viro } 229113aab428SAl Viro 229273d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, 229373d049a4SAl Viro const char *name, const struct open_flags *op, int flags) 229473d049a4SAl Viro { 229573d049a4SAl Viro struct nameidata nd; 229673d049a4SAl Viro struct file *file; 229773d049a4SAl Viro 229873d049a4SAl Viro nd.root.mnt = mnt; 229973d049a4SAl Viro nd.root.dentry = dentry; 230073d049a4SAl Viro 230173d049a4SAl Viro flags |= LOOKUP_ROOT; 230273d049a4SAl Viro 2303bcda7652SAl Viro if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN) 230473d049a4SAl Viro return ERR_PTR(-ELOOP); 230573d049a4SAl Viro 230673d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU); 230773d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 230873d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags); 230973d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 231073d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL); 231173d049a4SAl Viro return file; 231273d049a4SAl Viro } 231373d049a4SAl Viro 2314ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir) 23151da177e4SLinus Torvalds { 2316c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 2317ed75e95dSAl Viro struct nameidata nd; 2318ed75e95dSAl Viro int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd); 2319ed75e95dSAl Viro if (error) 2320ed75e95dSAl Viro return ERR_PTR(error); 23211da177e4SLinus Torvalds 2322c663e5d8SChristoph Hellwig /* 2323c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 2324c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 2325c663e5d8SChristoph Hellwig */ 2326ed75e95dSAl Viro if (nd.last_type != LAST_NORM) 2327ed75e95dSAl Viro goto out; 2328ed75e95dSAl Viro nd.flags &= ~LOOKUP_PARENT; 2329ed75e95dSAl Viro nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL; 2330ed75e95dSAl Viro nd.intent.open.flags = O_EXCL; 2331c663e5d8SChristoph Hellwig 2332c663e5d8SChristoph Hellwig /* 2333c663e5d8SChristoph Hellwig * Do the final lookup. 2334c663e5d8SChristoph Hellwig */ 2335ed75e95dSAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 2336ed75e95dSAl Viro dentry = lookup_hash(&nd); 23371da177e4SLinus Torvalds if (IS_ERR(dentry)) 23381da177e4SLinus Torvalds goto fail; 2339c663e5d8SChristoph Hellwig 2340e9baf6e5SAl Viro if (dentry->d_inode) 2341e9baf6e5SAl Viro goto eexist; 2342c663e5d8SChristoph Hellwig /* 2343c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 2344c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 2345c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 2346c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 2347c663e5d8SChristoph Hellwig */ 2348ed75e95dSAl Viro if (unlikely(!is_dir && nd.last.name[nd.last.len])) { 23491da177e4SLinus Torvalds dput(dentry); 23501da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 2351ed75e95dSAl Viro goto fail; 2352e9baf6e5SAl Viro } 2353ed75e95dSAl Viro *path = nd.path; 2354e9baf6e5SAl Viro return dentry; 2355e9baf6e5SAl Viro eexist: 2356e9baf6e5SAl Viro dput(dentry); 2357e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 23581da177e4SLinus Torvalds fail: 2359dae6ad8fSAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 2360ed75e95dSAl Viro out: 2361dae6ad8fSAl Viro path_put(&nd.path); 2362ed75e95dSAl Viro return dentry; 2363dae6ad8fSAl Viro } 2364dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create); 2365dae6ad8fSAl Viro 2366dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir) 2367dae6ad8fSAl Viro { 2368dae6ad8fSAl Viro char *tmp = getname(pathname); 2369dae6ad8fSAl Viro struct dentry *res; 2370dae6ad8fSAl Viro if (IS_ERR(tmp)) 2371dae6ad8fSAl Viro return ERR_CAST(tmp); 2372dae6ad8fSAl Viro res = kern_path_create(dfd, tmp, path, is_dir); 2373dae6ad8fSAl Viro putname(tmp); 2374dae6ad8fSAl Viro return res; 2375dae6ad8fSAl Viro } 2376dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create); 2377dae6ad8fSAl Viro 23781da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 23791da177e4SLinus Torvalds { 2380a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 23811da177e4SLinus Torvalds 23821da177e4SLinus Torvalds if (error) 23831da177e4SLinus Torvalds return error; 23841da177e4SLinus Torvalds 2385e795b717SSerge E. Hallyn if ((S_ISCHR(mode) || S_ISBLK(mode)) && 2386e795b717SSerge E. Hallyn !ns_capable(inode_userns(dir), CAP_MKNOD)) 23871da177e4SLinus Torvalds return -EPERM; 23881da177e4SLinus Torvalds 2389acfa4380SAl Viro if (!dir->i_op->mknod) 23901da177e4SLinus Torvalds return -EPERM; 23911da177e4SLinus Torvalds 239208ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 239308ce5f16SSerge E. Hallyn if (error) 239408ce5f16SSerge E. Hallyn return error; 239508ce5f16SSerge E. Hallyn 23961da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 23971da177e4SLinus Torvalds if (error) 23981da177e4SLinus Torvalds return error; 23991da177e4SLinus Torvalds 24001da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 2401a74574aaSStephen Smalley if (!error) 2402f38aa942SAmy Griffis fsnotify_create(dir, dentry); 24031da177e4SLinus Torvalds return error; 24041da177e4SLinus Torvalds } 24051da177e4SLinus Torvalds 2406463c3197SDave Hansen static int may_mknod(mode_t mode) 2407463c3197SDave Hansen { 2408463c3197SDave Hansen switch (mode & S_IFMT) { 2409463c3197SDave Hansen case S_IFREG: 2410463c3197SDave Hansen case S_IFCHR: 2411463c3197SDave Hansen case S_IFBLK: 2412463c3197SDave Hansen case S_IFIFO: 2413463c3197SDave Hansen case S_IFSOCK: 2414463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2415463c3197SDave Hansen return 0; 2416463c3197SDave Hansen case S_IFDIR: 2417463c3197SDave Hansen return -EPERM; 2418463c3197SDave Hansen default: 2419463c3197SDave Hansen return -EINVAL; 2420463c3197SDave Hansen } 2421463c3197SDave Hansen } 2422463c3197SDave Hansen 24232e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode, 24242e4d0924SHeiko Carstens unsigned, dev) 24251da177e4SLinus Torvalds { 24261da177e4SLinus Torvalds struct dentry *dentry; 2427dae6ad8fSAl Viro struct path path; 2428dae6ad8fSAl Viro int error; 24291da177e4SLinus Torvalds 24301da177e4SLinus Torvalds if (S_ISDIR(mode)) 24311da177e4SLinus Torvalds return -EPERM; 24321da177e4SLinus Torvalds 2433dae6ad8fSAl Viro dentry = user_path_create(dfd, filename, &path, 0); 2434dae6ad8fSAl Viro if (IS_ERR(dentry)) 2435dae6ad8fSAl Viro return PTR_ERR(dentry); 24362ad94ae6SAl Viro 2437dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 2438ce3b0f8dSAl Viro mode &= ~current_umask(); 2439463c3197SDave Hansen error = may_mknod(mode); 2440463c3197SDave Hansen if (error) 2441463c3197SDave Hansen goto out_dput; 2442dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 2443463c3197SDave Hansen if (error) 2444463c3197SDave Hansen goto out_dput; 2445dae6ad8fSAl Viro error = security_path_mknod(&path, dentry, mode, dev); 2446be6d3e56SKentaro Takeda if (error) 2447be6d3e56SKentaro Takeda goto out_drop_write; 24481da177e4SLinus Torvalds switch (mode & S_IFMT) { 24491da177e4SLinus Torvalds case 0: case S_IFREG: 2450dae6ad8fSAl Viro error = vfs_create(path.dentry->d_inode,dentry,mode,NULL); 24511da177e4SLinus Torvalds break; 24521da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 2453dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode, 24541da177e4SLinus Torvalds new_decode_dev(dev)); 24551da177e4SLinus Torvalds break; 24561da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 2457dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); 24581da177e4SLinus Torvalds break; 24591da177e4SLinus Torvalds } 2460be6d3e56SKentaro Takeda out_drop_write: 2461dae6ad8fSAl Viro mnt_drop_write(path.mnt); 2462463c3197SDave Hansen out_dput: 24631da177e4SLinus Torvalds dput(dentry); 2464dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2465dae6ad8fSAl Viro path_put(&path); 24661da177e4SLinus Torvalds 24671da177e4SLinus Torvalds return error; 24681da177e4SLinus Torvalds } 24691da177e4SLinus Torvalds 24703480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev) 24715590ff0dSUlrich Drepper { 24725590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 24735590ff0dSUlrich Drepper } 24745590ff0dSUlrich Drepper 24751da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 24761da177e4SLinus Torvalds { 2477a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 24781da177e4SLinus Torvalds 24791da177e4SLinus Torvalds if (error) 24801da177e4SLinus Torvalds return error; 24811da177e4SLinus Torvalds 2482acfa4380SAl Viro if (!dir->i_op->mkdir) 24831da177e4SLinus Torvalds return -EPERM; 24841da177e4SLinus Torvalds 24851da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 24861da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 24871da177e4SLinus Torvalds if (error) 24881da177e4SLinus Torvalds return error; 24891da177e4SLinus Torvalds 24901da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2491a74574aaSStephen Smalley if (!error) 2492f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 24931da177e4SLinus Torvalds return error; 24941da177e4SLinus Torvalds } 24951da177e4SLinus Torvalds 24962e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode) 24971da177e4SLinus Torvalds { 24986902d925SDave Hansen struct dentry *dentry; 2499dae6ad8fSAl Viro struct path path; 2500dae6ad8fSAl Viro int error; 25011da177e4SLinus Torvalds 2502dae6ad8fSAl Viro dentry = user_path_create(dfd, pathname, &path, 1); 25036902d925SDave Hansen if (IS_ERR(dentry)) 2504dae6ad8fSAl Viro return PTR_ERR(dentry); 25056902d925SDave Hansen 2506dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 2507ce3b0f8dSAl Viro mode &= ~current_umask(); 2508dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 2509463c3197SDave Hansen if (error) 2510463c3197SDave Hansen goto out_dput; 2511dae6ad8fSAl Viro error = security_path_mkdir(&path, dentry, mode); 2512be6d3e56SKentaro Takeda if (error) 2513be6d3e56SKentaro Takeda goto out_drop_write; 2514dae6ad8fSAl Viro error = vfs_mkdir(path.dentry->d_inode, dentry, mode); 2515be6d3e56SKentaro Takeda out_drop_write: 2516dae6ad8fSAl Viro mnt_drop_write(path.mnt); 2517463c3197SDave Hansen out_dput: 25181da177e4SLinus Torvalds dput(dentry); 2519dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2520dae6ad8fSAl Viro path_put(&path); 25211da177e4SLinus Torvalds return error; 25221da177e4SLinus Torvalds } 25231da177e4SLinus Torvalds 25243cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode) 25255590ff0dSUlrich Drepper { 25265590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 25275590ff0dSUlrich Drepper } 25285590ff0dSUlrich Drepper 25291da177e4SLinus Torvalds /* 2530a71905f0SSage Weil * The dentry_unhash() helper will try to drop the dentry early: we 2531a71905f0SSage Weil * should have a usage count of 2 if we're the only user of this 2532a71905f0SSage Weil * dentry, and if that is true (possibly after pruning the dcache), 2533a71905f0SSage Weil * then we drop the dentry now. 25341da177e4SLinus Torvalds * 25351da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 25361da177e4SLinus Torvalds * do a 25371da177e4SLinus Torvalds * 25381da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 25391da177e4SLinus Torvalds * return -EBUSY; 25401da177e4SLinus Torvalds * 25411da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 25421da177e4SLinus Torvalds * that is still in use by something else.. 25431da177e4SLinus Torvalds */ 25441da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 25451da177e4SLinus Torvalds { 25461da177e4SLinus Torvalds shrink_dcache_parent(dentry); 25471da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 254864252c75SSage Weil if (dentry->d_count == 1) 25491da177e4SLinus Torvalds __d_drop(dentry); 25501da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 25511da177e4SLinus Torvalds } 25521da177e4SLinus Torvalds 25531da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 25541da177e4SLinus Torvalds { 25551da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 25561da177e4SLinus Torvalds 25571da177e4SLinus Torvalds if (error) 25581da177e4SLinus Torvalds return error; 25591da177e4SLinus Torvalds 2560acfa4380SAl Viro if (!dir->i_op->rmdir) 25611da177e4SLinus Torvalds return -EPERM; 25621da177e4SLinus Torvalds 25631b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 2564912dbc15SSage Weil 25651da177e4SLinus Torvalds error = -EBUSY; 2566912dbc15SSage Weil if (d_mountpoint(dentry)) 2567912dbc15SSage Weil goto out; 2568912dbc15SSage Weil 25691da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 2570912dbc15SSage Weil if (error) 2571912dbc15SSage Weil goto out; 2572912dbc15SSage Weil 25733cebde24SSage Weil shrink_dcache_parent(dentry); 25741da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 2575912dbc15SSage Weil if (error) 2576912dbc15SSage Weil goto out; 2577912dbc15SSage Weil 25781da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 2579d83c49f3SAl Viro dont_mount(dentry); 25801da177e4SLinus Torvalds 2581912dbc15SSage Weil out: 2582912dbc15SSage Weil mutex_unlock(&dentry->d_inode->i_mutex); 2583912dbc15SSage Weil if (!error) 2584912dbc15SSage Weil d_delete(dentry); 25851da177e4SLinus Torvalds return error; 25861da177e4SLinus Torvalds } 25871da177e4SLinus Torvalds 25885590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 25891da177e4SLinus Torvalds { 25901da177e4SLinus Torvalds int error = 0; 25911da177e4SLinus Torvalds char * name; 25921da177e4SLinus Torvalds struct dentry *dentry; 25931da177e4SLinus Torvalds struct nameidata nd; 25941da177e4SLinus Torvalds 25952ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 25961da177e4SLinus Torvalds if (error) 25972ad94ae6SAl Viro return error; 25981da177e4SLinus Torvalds 25991da177e4SLinus Torvalds switch(nd.last_type) { 26001da177e4SLinus Torvalds case LAST_DOTDOT: 26011da177e4SLinus Torvalds error = -ENOTEMPTY; 26021da177e4SLinus Torvalds goto exit1; 26031da177e4SLinus Torvalds case LAST_DOT: 26041da177e4SLinus Torvalds error = -EINVAL; 26051da177e4SLinus Torvalds goto exit1; 26061da177e4SLinus Torvalds case LAST_ROOT: 26071da177e4SLinus Torvalds error = -EBUSY; 26081da177e4SLinus Torvalds goto exit1; 26091da177e4SLinus Torvalds } 26100612d9fbSOGAWA Hirofumi 26110612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 26120612d9fbSOGAWA Hirofumi 26134ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 261449705b77SChristoph Hellwig dentry = lookup_hash(&nd); 26151da177e4SLinus Torvalds error = PTR_ERR(dentry); 26166902d925SDave Hansen if (IS_ERR(dentry)) 26176902d925SDave Hansen goto exit2; 2618e6bc45d6STheodore Ts'o if (!dentry->d_inode) { 2619e6bc45d6STheodore Ts'o error = -ENOENT; 2620e6bc45d6STheodore Ts'o goto exit3; 2621e6bc45d6STheodore Ts'o } 26220622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 26230622753bSDave Hansen if (error) 26240622753bSDave Hansen goto exit3; 2625be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 2626be6d3e56SKentaro Takeda if (error) 2627be6d3e56SKentaro Takeda goto exit4; 26284ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 2629be6d3e56SKentaro Takeda exit4: 26300622753bSDave Hansen mnt_drop_write(nd.path.mnt); 26310622753bSDave Hansen exit3: 26321da177e4SLinus Torvalds dput(dentry); 26336902d925SDave Hansen exit2: 26344ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 26351da177e4SLinus Torvalds exit1: 26361d957f9bSJan Blunck path_put(&nd.path); 26371da177e4SLinus Torvalds putname(name); 26381da177e4SLinus Torvalds return error; 26391da177e4SLinus Torvalds } 26401da177e4SLinus Torvalds 26413cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 26425590ff0dSUlrich Drepper { 26435590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 26445590ff0dSUlrich Drepper } 26455590ff0dSUlrich Drepper 26461da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 26471da177e4SLinus Torvalds { 26481da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 26491da177e4SLinus Torvalds 26501da177e4SLinus Torvalds if (error) 26511da177e4SLinus Torvalds return error; 26521da177e4SLinus Torvalds 2653acfa4380SAl Viro if (!dir->i_op->unlink) 26541da177e4SLinus Torvalds return -EPERM; 26551da177e4SLinus Torvalds 26561b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 26571da177e4SLinus Torvalds if (d_mountpoint(dentry)) 26581da177e4SLinus Torvalds error = -EBUSY; 26591da177e4SLinus Torvalds else { 26601da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 2661bec1052eSAl Viro if (!error) { 26621da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 2663bec1052eSAl Viro if (!error) 2664d83c49f3SAl Viro dont_mount(dentry); 2665bec1052eSAl Viro } 26661da177e4SLinus Torvalds } 26671b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 26681da177e4SLinus Torvalds 26691da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 26701da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 2671ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 26721da177e4SLinus Torvalds d_delete(dentry); 26731da177e4SLinus Torvalds } 26740eeca283SRobert Love 26751da177e4SLinus Torvalds return error; 26761da177e4SLinus Torvalds } 26771da177e4SLinus Torvalds 26781da177e4SLinus Torvalds /* 26791da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 26801b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 26811da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 26821da177e4SLinus Torvalds * while waiting on the I/O. 26831da177e4SLinus Torvalds */ 26845590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 26851da177e4SLinus Torvalds { 26862ad94ae6SAl Viro int error; 26871da177e4SLinus Torvalds char *name; 26881da177e4SLinus Torvalds struct dentry *dentry; 26891da177e4SLinus Torvalds struct nameidata nd; 26901da177e4SLinus Torvalds struct inode *inode = NULL; 26911da177e4SLinus Torvalds 26922ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 26931da177e4SLinus Torvalds if (error) 26942ad94ae6SAl Viro return error; 26952ad94ae6SAl Viro 26961da177e4SLinus Torvalds error = -EISDIR; 26971da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 26981da177e4SLinus Torvalds goto exit1; 26990612d9fbSOGAWA Hirofumi 27000612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 27010612d9fbSOGAWA Hirofumi 27024ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 270349705b77SChristoph Hellwig dentry = lookup_hash(&nd); 27041da177e4SLinus Torvalds error = PTR_ERR(dentry); 27051da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 27061da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 270750338b88STörök Edwin if (nd.last.name[nd.last.len]) 270850338b88STörök Edwin goto slashes; 27091da177e4SLinus Torvalds inode = dentry->d_inode; 271050338b88STörök Edwin if (!inode) 2711e6bc45d6STheodore Ts'o goto slashes; 27127de9c6eeSAl Viro ihold(inode); 27130622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 27140622753bSDave Hansen if (error) 27150622753bSDave Hansen goto exit2; 2716be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 2717be6d3e56SKentaro Takeda if (error) 2718be6d3e56SKentaro Takeda goto exit3; 27194ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 2720be6d3e56SKentaro Takeda exit3: 27210622753bSDave Hansen mnt_drop_write(nd.path.mnt); 27221da177e4SLinus Torvalds exit2: 27231da177e4SLinus Torvalds dput(dentry); 27241da177e4SLinus Torvalds } 27254ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 27261da177e4SLinus Torvalds if (inode) 27271da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 27281da177e4SLinus Torvalds exit1: 27291d957f9bSJan Blunck path_put(&nd.path); 27301da177e4SLinus Torvalds putname(name); 27311da177e4SLinus Torvalds return error; 27321da177e4SLinus Torvalds 27331da177e4SLinus Torvalds slashes: 27341da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 27351da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 27361da177e4SLinus Torvalds goto exit2; 27371da177e4SLinus Torvalds } 27381da177e4SLinus Torvalds 27392e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 27405590ff0dSUlrich Drepper { 27415590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 27425590ff0dSUlrich Drepper return -EINVAL; 27435590ff0dSUlrich Drepper 27445590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 27455590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 27465590ff0dSUlrich Drepper 27475590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 27485590ff0dSUlrich Drepper } 27495590ff0dSUlrich Drepper 27503480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 27515590ff0dSUlrich Drepper { 27525590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 27535590ff0dSUlrich Drepper } 27545590ff0dSUlrich Drepper 2755db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 27561da177e4SLinus Torvalds { 2757a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 27581da177e4SLinus Torvalds 27591da177e4SLinus Torvalds if (error) 27601da177e4SLinus Torvalds return error; 27611da177e4SLinus Torvalds 2762acfa4380SAl Viro if (!dir->i_op->symlink) 27631da177e4SLinus Torvalds return -EPERM; 27641da177e4SLinus Torvalds 27651da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 27661da177e4SLinus Torvalds if (error) 27671da177e4SLinus Torvalds return error; 27681da177e4SLinus Torvalds 27691da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 2770a74574aaSStephen Smalley if (!error) 2771f38aa942SAmy Griffis fsnotify_create(dir, dentry); 27721da177e4SLinus Torvalds return error; 27731da177e4SLinus Torvalds } 27741da177e4SLinus Torvalds 27752e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 27762e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 27771da177e4SLinus Torvalds { 27782ad94ae6SAl Viro int error; 27791da177e4SLinus Torvalds char *from; 27806902d925SDave Hansen struct dentry *dentry; 2781dae6ad8fSAl Viro struct path path; 27821da177e4SLinus Torvalds 27831da177e4SLinus Torvalds from = getname(oldname); 27841da177e4SLinus Torvalds if (IS_ERR(from)) 27851da177e4SLinus Torvalds return PTR_ERR(from); 27862ad94ae6SAl Viro 2787dae6ad8fSAl Viro dentry = user_path_create(newdfd, newname, &path, 0); 27881da177e4SLinus Torvalds error = PTR_ERR(dentry); 27896902d925SDave Hansen if (IS_ERR(dentry)) 2790dae6ad8fSAl Viro goto out_putname; 27916902d925SDave Hansen 2792dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 279375c3f29dSDave Hansen if (error) 279475c3f29dSDave Hansen goto out_dput; 2795dae6ad8fSAl Viro error = security_path_symlink(&path, dentry, from); 2796be6d3e56SKentaro Takeda if (error) 2797be6d3e56SKentaro Takeda goto out_drop_write; 2798dae6ad8fSAl Viro error = vfs_symlink(path.dentry->d_inode, dentry, from); 2799be6d3e56SKentaro Takeda out_drop_write: 2800dae6ad8fSAl Viro mnt_drop_write(path.mnt); 280175c3f29dSDave Hansen out_dput: 28021da177e4SLinus Torvalds dput(dentry); 2803dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2804dae6ad8fSAl Viro path_put(&path); 28056902d925SDave Hansen out_putname: 28061da177e4SLinus Torvalds putname(from); 28071da177e4SLinus Torvalds return error; 28081da177e4SLinus Torvalds } 28091da177e4SLinus Torvalds 28103480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 28115590ff0dSUlrich Drepper { 28125590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 28135590ff0dSUlrich Drepper } 28145590ff0dSUlrich Drepper 28151da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 28161da177e4SLinus Torvalds { 28171da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 28181da177e4SLinus Torvalds int error; 28191da177e4SLinus Torvalds 28201da177e4SLinus Torvalds if (!inode) 28211da177e4SLinus Torvalds return -ENOENT; 28221da177e4SLinus Torvalds 2823a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 28241da177e4SLinus Torvalds if (error) 28251da177e4SLinus Torvalds return error; 28261da177e4SLinus Torvalds 28271da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 28281da177e4SLinus Torvalds return -EXDEV; 28291da177e4SLinus Torvalds 28301da177e4SLinus Torvalds /* 28311da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 28321da177e4SLinus Torvalds */ 28331da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 28341da177e4SLinus Torvalds return -EPERM; 2835acfa4380SAl Viro if (!dir->i_op->link) 28361da177e4SLinus Torvalds return -EPERM; 28377e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 28381da177e4SLinus Torvalds return -EPERM; 28391da177e4SLinus Torvalds 28401da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 28411da177e4SLinus Torvalds if (error) 28421da177e4SLinus Torvalds return error; 28431da177e4SLinus Torvalds 28447e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 2845aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 2846aae8a97dSAneesh Kumar K.V if (inode->i_nlink == 0) 2847aae8a97dSAneesh Kumar K.V error = -ENOENT; 2848aae8a97dSAneesh Kumar K.V else 28491da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 28507e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 2851e31e14ecSStephen Smalley if (!error) 28527e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 28531da177e4SLinus Torvalds return error; 28541da177e4SLinus Torvalds } 28551da177e4SLinus Torvalds 28561da177e4SLinus Torvalds /* 28571da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 28581da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 28591da177e4SLinus Torvalds * newname. --KAB 28601da177e4SLinus Torvalds * 28611da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 28621da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 28631da177e4SLinus Torvalds * and other special files. --ADM 28641da177e4SLinus Torvalds */ 28652e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 28662e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 28671da177e4SLinus Torvalds { 28681da177e4SLinus Torvalds struct dentry *new_dentry; 2869dae6ad8fSAl Viro struct path old_path, new_path; 287011a7b371SAneesh Kumar K.V int how = 0; 28711da177e4SLinus Torvalds int error; 28721da177e4SLinus Torvalds 287311a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 2874c04030e1SUlrich Drepper return -EINVAL; 287511a7b371SAneesh Kumar K.V /* 287611a7b371SAneesh Kumar K.V * To use null names we require CAP_DAC_READ_SEARCH 287711a7b371SAneesh Kumar K.V * This ensures that not everyone will be able to create 287811a7b371SAneesh Kumar K.V * handlink using the passed filedescriptor. 287911a7b371SAneesh Kumar K.V */ 288011a7b371SAneesh Kumar K.V if (flags & AT_EMPTY_PATH) { 288111a7b371SAneesh Kumar K.V if (!capable(CAP_DAC_READ_SEARCH)) 288211a7b371SAneesh Kumar K.V return -ENOENT; 288311a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 288411a7b371SAneesh Kumar K.V } 2885c04030e1SUlrich Drepper 288611a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 288711a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 288811a7b371SAneesh Kumar K.V 288911a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 28901da177e4SLinus Torvalds if (error) 28912ad94ae6SAl Viro return error; 28922ad94ae6SAl Viro 2893dae6ad8fSAl Viro new_dentry = user_path_create(newdfd, newname, &new_path, 0); 28941da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 28956902d925SDave Hansen if (IS_ERR(new_dentry)) 2896dae6ad8fSAl Viro goto out; 2897dae6ad8fSAl Viro 2898dae6ad8fSAl Viro error = -EXDEV; 2899dae6ad8fSAl Viro if (old_path.mnt != new_path.mnt) 2900dae6ad8fSAl Viro goto out_dput; 2901dae6ad8fSAl Viro error = mnt_want_write(new_path.mnt); 290275c3f29dSDave Hansen if (error) 290375c3f29dSDave Hansen goto out_dput; 2904dae6ad8fSAl Viro error = security_path_link(old_path.dentry, &new_path, new_dentry); 2905be6d3e56SKentaro Takeda if (error) 2906be6d3e56SKentaro Takeda goto out_drop_write; 2907dae6ad8fSAl Viro error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry); 2908be6d3e56SKentaro Takeda out_drop_write: 2909dae6ad8fSAl Viro mnt_drop_write(new_path.mnt); 291075c3f29dSDave Hansen out_dput: 29111da177e4SLinus Torvalds dput(new_dentry); 2912dae6ad8fSAl Viro mutex_unlock(&new_path.dentry->d_inode->i_mutex); 2913dae6ad8fSAl Viro path_put(&new_path); 29141da177e4SLinus Torvalds out: 29152d8f3038SAl Viro path_put(&old_path); 29161da177e4SLinus Torvalds 29171da177e4SLinus Torvalds return error; 29181da177e4SLinus Torvalds } 29191da177e4SLinus Torvalds 29203480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 29215590ff0dSUlrich Drepper { 2922c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 29235590ff0dSUlrich Drepper } 29245590ff0dSUlrich Drepper 29251da177e4SLinus Torvalds /* 29261da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 29271da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 29281da177e4SLinus Torvalds * Problems: 29291da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 29301da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 29311da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 2932a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 29331da177e4SLinus Torvalds * story. 29341da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 29351b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 29361da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 29371da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 2938a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 29391da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 29401da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 29411da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 2942a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 29431da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 29441da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 29451da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 2946e4eaac06SSage Weil * d) conversion from fhandle to dentry may come in the wrong moment - when 29471b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 29481da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 2949c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 29501da177e4SLinus Torvalds * locking]. 29511da177e4SLinus Torvalds */ 295275c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 29531da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 29541da177e4SLinus Torvalds { 29551da177e4SLinus Torvalds int error = 0; 29569055cba7SSage Weil struct inode *target = new_dentry->d_inode; 29571da177e4SLinus Torvalds 29581da177e4SLinus Torvalds /* 29591da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 29601da177e4SLinus Torvalds * we'll need to flip '..'. 29611da177e4SLinus Torvalds */ 29621da177e4SLinus Torvalds if (new_dir != old_dir) { 2963f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 29641da177e4SLinus Torvalds if (error) 29651da177e4SLinus Torvalds return error; 29661da177e4SLinus Torvalds } 29671da177e4SLinus Torvalds 29681da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 29691da177e4SLinus Torvalds if (error) 29701da177e4SLinus Torvalds return error; 29711da177e4SLinus Torvalds 2972d83c49f3SAl Viro if (target) 29731b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 29749055cba7SSage Weil 29751da177e4SLinus Torvalds error = -EBUSY; 29769055cba7SSage Weil if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) 29779055cba7SSage Weil goto out; 29789055cba7SSage Weil 29793cebde24SSage Weil if (target) 29803cebde24SSage Weil shrink_dcache_parent(new_dentry); 29811da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 29829055cba7SSage Weil if (error) 29839055cba7SSage Weil goto out; 29849055cba7SSage Weil 29851da177e4SLinus Torvalds if (target) { 29861da177e4SLinus Torvalds target->i_flags |= S_DEAD; 2987d83c49f3SAl Viro dont_mount(new_dentry); 2988d83c49f3SAl Viro } 29899055cba7SSage Weil out: 29909055cba7SSage Weil if (target) 29911b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 2992e31e14ecSStephen Smalley if (!error) 2993349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 29941da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 29951da177e4SLinus Torvalds return error; 29961da177e4SLinus Torvalds } 29971da177e4SLinus Torvalds 299875c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 29991da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30001da177e4SLinus Torvalds { 300151892bbbSSage Weil struct inode *target = new_dentry->d_inode; 30021da177e4SLinus Torvalds int error; 30031da177e4SLinus Torvalds 30041da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 30051da177e4SLinus Torvalds if (error) 30061da177e4SLinus Torvalds return error; 30071da177e4SLinus Torvalds 30081da177e4SLinus Torvalds dget(new_dentry); 30091da177e4SLinus Torvalds if (target) 30101b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 301151892bbbSSage Weil 30121da177e4SLinus Torvalds error = -EBUSY; 301351892bbbSSage Weil if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 301451892bbbSSage Weil goto out; 301551892bbbSSage Weil 30161da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 301751892bbbSSage Weil if (error) 301851892bbbSSage Weil goto out; 301951892bbbSSage Weil 3020bec1052eSAl Viro if (target) 3021d83c49f3SAl Viro dont_mount(new_dentry); 3022349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 30231da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 302451892bbbSSage Weil out: 30251da177e4SLinus Torvalds if (target) 30261b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 30271da177e4SLinus Torvalds dput(new_dentry); 30281da177e4SLinus Torvalds return error; 30291da177e4SLinus Torvalds } 30301da177e4SLinus Torvalds 30311da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 30321da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30331da177e4SLinus Torvalds { 30341da177e4SLinus Torvalds int error; 30351da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 303659b0df21SEric Paris const unsigned char *old_name; 30371da177e4SLinus Torvalds 30381da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 30391da177e4SLinus Torvalds return 0; 30401da177e4SLinus Torvalds 30411da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 30421da177e4SLinus Torvalds if (error) 30431da177e4SLinus Torvalds return error; 30441da177e4SLinus Torvalds 30451da177e4SLinus Torvalds if (!new_dentry->d_inode) 3046a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 30471da177e4SLinus Torvalds else 30481da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 30491da177e4SLinus Torvalds if (error) 30501da177e4SLinus Torvalds return error; 30511da177e4SLinus Torvalds 3052acfa4380SAl Viro if (!old_dir->i_op->rename) 30531da177e4SLinus Torvalds return -EPERM; 30541da177e4SLinus Torvalds 30550eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 30560eeca283SRobert Love 30571da177e4SLinus Torvalds if (is_dir) 30581da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 30591da177e4SLinus Torvalds else 30601da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3061123df294SAl Viro if (!error) 3062123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 30635a190ae6SAl Viro new_dentry->d_inode, old_dentry); 30640eeca283SRobert Love fsnotify_oldname_free(old_name); 30650eeca283SRobert Love 30661da177e4SLinus Torvalds return error; 30671da177e4SLinus Torvalds } 30681da177e4SLinus Torvalds 30692e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 30702e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 30711da177e4SLinus Torvalds { 30721da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 30731da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 30741da177e4SLinus Torvalds struct dentry *trap; 30751da177e4SLinus Torvalds struct nameidata oldnd, newnd; 30762ad94ae6SAl Viro char *from; 30772ad94ae6SAl Viro char *to; 30782ad94ae6SAl Viro int error; 30791da177e4SLinus Torvalds 30802ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 30811da177e4SLinus Torvalds if (error) 30821da177e4SLinus Torvalds goto exit; 30831da177e4SLinus Torvalds 30842ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 30851da177e4SLinus Torvalds if (error) 30861da177e4SLinus Torvalds goto exit1; 30871da177e4SLinus Torvalds 30881da177e4SLinus Torvalds error = -EXDEV; 30894ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 30901da177e4SLinus Torvalds goto exit2; 30911da177e4SLinus Torvalds 30924ac91378SJan Blunck old_dir = oldnd.path.dentry; 30931da177e4SLinus Torvalds error = -EBUSY; 30941da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 30951da177e4SLinus Torvalds goto exit2; 30961da177e4SLinus Torvalds 30974ac91378SJan Blunck new_dir = newnd.path.dentry; 30981da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 30991da177e4SLinus Torvalds goto exit2; 31001da177e4SLinus Torvalds 31010612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 31020612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 31034e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 31040612d9fbSOGAWA Hirofumi 31051da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 31061da177e4SLinus Torvalds 310749705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 31081da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 31091da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 31101da177e4SLinus Torvalds goto exit3; 31111da177e4SLinus Torvalds /* source must exist */ 31121da177e4SLinus Torvalds error = -ENOENT; 31131da177e4SLinus Torvalds if (!old_dentry->d_inode) 31141da177e4SLinus Torvalds goto exit4; 31151da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 31161da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 31171da177e4SLinus Torvalds error = -ENOTDIR; 31181da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 31191da177e4SLinus Torvalds goto exit4; 31201da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 31211da177e4SLinus Torvalds goto exit4; 31221da177e4SLinus Torvalds } 31231da177e4SLinus Torvalds /* source should not be ancestor of target */ 31241da177e4SLinus Torvalds error = -EINVAL; 31251da177e4SLinus Torvalds if (old_dentry == trap) 31261da177e4SLinus Torvalds goto exit4; 312749705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 31281da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 31291da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 31301da177e4SLinus Torvalds goto exit4; 31311da177e4SLinus Torvalds /* target should not be an ancestor of source */ 31321da177e4SLinus Torvalds error = -ENOTEMPTY; 31331da177e4SLinus Torvalds if (new_dentry == trap) 31341da177e4SLinus Torvalds goto exit5; 31351da177e4SLinus Torvalds 31369079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 31379079b1ebSDave Hansen if (error) 31389079b1ebSDave Hansen goto exit5; 3139be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3140be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3141be6d3e56SKentaro Takeda if (error) 3142be6d3e56SKentaro Takeda goto exit6; 31431da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 31441da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 3145be6d3e56SKentaro Takeda exit6: 31469079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 31471da177e4SLinus Torvalds exit5: 31481da177e4SLinus Torvalds dput(new_dentry); 31491da177e4SLinus Torvalds exit4: 31501da177e4SLinus Torvalds dput(old_dentry); 31511da177e4SLinus Torvalds exit3: 31521da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 31531da177e4SLinus Torvalds exit2: 31541d957f9bSJan Blunck path_put(&newnd.path); 31552ad94ae6SAl Viro putname(to); 31561da177e4SLinus Torvalds exit1: 31571d957f9bSJan Blunck path_put(&oldnd.path); 31581da177e4SLinus Torvalds putname(from); 31592ad94ae6SAl Viro exit: 31601da177e4SLinus Torvalds return error; 31611da177e4SLinus Torvalds } 31621da177e4SLinus Torvalds 3163a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 31645590ff0dSUlrich Drepper { 31655590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 31665590ff0dSUlrich Drepper } 31675590ff0dSUlrich Drepper 31681da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 31691da177e4SLinus Torvalds { 31701da177e4SLinus Torvalds int len; 31711da177e4SLinus Torvalds 31721da177e4SLinus Torvalds len = PTR_ERR(link); 31731da177e4SLinus Torvalds if (IS_ERR(link)) 31741da177e4SLinus Torvalds goto out; 31751da177e4SLinus Torvalds 31761da177e4SLinus Torvalds len = strlen(link); 31771da177e4SLinus Torvalds if (len > (unsigned) buflen) 31781da177e4SLinus Torvalds len = buflen; 31791da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 31801da177e4SLinus Torvalds len = -EFAULT; 31811da177e4SLinus Torvalds out: 31821da177e4SLinus Torvalds return len; 31831da177e4SLinus Torvalds } 31841da177e4SLinus Torvalds 31851da177e4SLinus Torvalds /* 31861da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 31871da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 31881da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 31891da177e4SLinus Torvalds */ 31901da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 31911da177e4SLinus Torvalds { 31921da177e4SLinus Torvalds struct nameidata nd; 3193cc314eefSLinus Torvalds void *cookie; 3194694a1764SMarcin Slusarz int res; 3195cc314eefSLinus Torvalds 31961da177e4SLinus Torvalds nd.depth = 0; 3197cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3198694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3199694a1764SMarcin Slusarz return PTR_ERR(cookie); 3200694a1764SMarcin Slusarz 3201694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 32021da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3203cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3204694a1764SMarcin Slusarz return res; 32051da177e4SLinus Torvalds } 32061da177e4SLinus Torvalds 32071da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 32081da177e4SLinus Torvalds { 32091da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 32101da177e4SLinus Torvalds } 32111da177e4SLinus Torvalds 32121da177e4SLinus Torvalds /* get the link contents into pagecache */ 32131da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 32141da177e4SLinus Torvalds { 3215ebd09abbSDuane Griffin char *kaddr; 32161da177e4SLinus Torvalds struct page *page; 32171da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3218090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 32191da177e4SLinus Torvalds if (IS_ERR(page)) 32206fe6900eSNick Piggin return (char*)page; 32211da177e4SLinus Torvalds *ppage = page; 3222ebd09abbSDuane Griffin kaddr = kmap(page); 3223ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3224ebd09abbSDuane Griffin return kaddr; 32251da177e4SLinus Torvalds } 32261da177e4SLinus Torvalds 32271da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 32281da177e4SLinus Torvalds { 32291da177e4SLinus Torvalds struct page *page = NULL; 32301da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 32311da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 32321da177e4SLinus Torvalds if (page) { 32331da177e4SLinus Torvalds kunmap(page); 32341da177e4SLinus Torvalds page_cache_release(page); 32351da177e4SLinus Torvalds } 32361da177e4SLinus Torvalds return res; 32371da177e4SLinus Torvalds } 32381da177e4SLinus Torvalds 3239cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 32401da177e4SLinus Torvalds { 3241cc314eefSLinus Torvalds struct page *page = NULL; 32421da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 3243cc314eefSLinus Torvalds return page; 32441da177e4SLinus Torvalds } 32451da177e4SLinus Torvalds 3246cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 32471da177e4SLinus Torvalds { 3248cc314eefSLinus Torvalds struct page *page = cookie; 3249cc314eefSLinus Torvalds 3250cc314eefSLinus Torvalds if (page) { 32511da177e4SLinus Torvalds kunmap(page); 32521da177e4SLinus Torvalds page_cache_release(page); 32531da177e4SLinus Torvalds } 32541da177e4SLinus Torvalds } 32551da177e4SLinus Torvalds 325654566b2cSNick Piggin /* 325754566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 325854566b2cSNick Piggin */ 325954566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 32601da177e4SLinus Torvalds { 32611da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 32620adb25d2SKirill Korotaev struct page *page; 3263afddba49SNick Piggin void *fsdata; 3264beb497abSDmitriy Monakhov int err; 32651da177e4SLinus Torvalds char *kaddr; 326654566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 326754566b2cSNick Piggin if (nofs) 326854566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 32691da177e4SLinus Torvalds 32707e53cac4SNeilBrown retry: 3271afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 327254566b2cSNick Piggin flags, &page, &fsdata); 32731da177e4SLinus Torvalds if (err) 3274afddba49SNick Piggin goto fail; 3275afddba49SNick Piggin 32761da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 32771da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 32781da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 3279afddba49SNick Piggin 3280afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 3281afddba49SNick Piggin page, fsdata); 32821da177e4SLinus Torvalds if (err < 0) 32831da177e4SLinus Torvalds goto fail; 3284afddba49SNick Piggin if (err < len-1) 3285afddba49SNick Piggin goto retry; 3286afddba49SNick Piggin 32871da177e4SLinus Torvalds mark_inode_dirty(inode); 32881da177e4SLinus Torvalds return 0; 32891da177e4SLinus Torvalds fail: 32901da177e4SLinus Torvalds return err; 32911da177e4SLinus Torvalds } 32921da177e4SLinus Torvalds 32930adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 32940adb25d2SKirill Korotaev { 32950adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 329654566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 32970adb25d2SKirill Korotaev } 32980adb25d2SKirill Korotaev 329992e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 33001da177e4SLinus Torvalds .readlink = generic_readlink, 33011da177e4SLinus Torvalds .follow_link = page_follow_link_light, 33021da177e4SLinus Torvalds .put_link = page_put_link, 33031da177e4SLinus Torvalds }; 33041da177e4SLinus Torvalds 33052d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 3306cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one); 33071da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 33081da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 33091da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 33101da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 33111da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 33121da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 33131da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 33141da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 33151da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 33160adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 33171da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 33181da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 3319d1811465SAl Viro EXPORT_SYMBOL(kern_path); 332016f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 3321f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 33221da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 33231da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 33241da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 33251da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 33261da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 33271da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 33281da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 33291da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 33301da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 33311da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 33321da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 33331da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 33341da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 33351da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 3336