11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/namei.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* 81da177e4SLinus Torvalds * Some corrections by tytso. 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname 121da177e4SLinus Torvalds * lookup logic. 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture. 151da177e4SLinus Torvalds */ 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #include <linux/init.h> 181da177e4SLinus Torvalds #include <linux/module.h> 191da177e4SLinus Torvalds #include <linux/slab.h> 201da177e4SLinus Torvalds #include <linux/fs.h> 211da177e4SLinus Torvalds #include <linux/namei.h> 221da177e4SLinus Torvalds #include <linux/pagemap.h> 230eeca283SRobert Love #include <linux/fsnotify.h> 241da177e4SLinus Torvalds #include <linux/personality.h> 251da177e4SLinus Torvalds #include <linux/security.h> 266146f0d5SMimi Zohar #include <linux/ima.h> 271da177e4SLinus Torvalds #include <linux/syscalls.h> 281da177e4SLinus Torvalds #include <linux/mount.h> 291da177e4SLinus Torvalds #include <linux/audit.h> 3016f7e0feSRandy Dunlap #include <linux/capability.h> 31834f2a4aSTrond Myklebust #include <linux/file.h> 325590ff0dSUlrich Drepper #include <linux/fcntl.h> 3308ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h> 345ad4e53bSAl Viro #include <linux/fs_struct.h> 35e77819e5SLinus Torvalds #include <linux/posix_acl.h> 361da177e4SLinus Torvalds #include <asm/uaccess.h> 371da177e4SLinus Torvalds 38e81e3f4dSEric Paris #include "internal.h" 39e81e3f4dSEric Paris 401da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer] 411da177e4SLinus Torvalds * Fundamental changes in the pathname lookup mechanisms (namei) 421da177e4SLinus Torvalds * were necessary because of omirr. The reason is that omirr needs 431da177e4SLinus Torvalds * to know the _real_ pathname, not the user-supplied one, in case 441da177e4SLinus Torvalds * of symlinks (and also when transname replacements occur). 451da177e4SLinus Torvalds * 461da177e4SLinus Torvalds * The new code replaces the old recursive symlink resolution with 471da177e4SLinus Torvalds * an iterative one (in case of non-nested symlink chains). It does 481da177e4SLinus Torvalds * this with calls to <fs>_follow_link(). 491da177e4SLinus Torvalds * As a side effect, dir_namei(), _namei() and follow_link() are now 501da177e4SLinus Torvalds * replaced with a single function lookup_dentry() that can handle all 511da177e4SLinus Torvalds * the special cases of the former code. 521da177e4SLinus Torvalds * 531da177e4SLinus Torvalds * With the new dcache, the pathname is stored at each inode, at least as 541da177e4SLinus Torvalds * long as the refcount of the inode is positive. As a side effect, the 551da177e4SLinus Torvalds * size of the dcache depends on the inode cache and thus is dynamic. 561da177e4SLinus Torvalds * 571da177e4SLinus Torvalds * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink 581da177e4SLinus Torvalds * resolution to correspond with current state of the code. 591da177e4SLinus Torvalds * 601da177e4SLinus Torvalds * Note that the symlink resolution is not *completely* iterative. 611da177e4SLinus Torvalds * There is still a significant amount of tail- and mid- recursion in 621da177e4SLinus Torvalds * the algorithm. Also, note that <fs>_readlink() is not used in 631da177e4SLinus Torvalds * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() 641da177e4SLinus Torvalds * may return different results than <fs>_follow_link(). Many virtual 651da177e4SLinus Torvalds * filesystems (including /proc) exhibit this behavior. 661da177e4SLinus Torvalds */ 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: 691da177e4SLinus Torvalds * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL 701da177e4SLinus Torvalds * and the name already exists in form of a symlink, try to create the new 711da177e4SLinus Torvalds * name indicated by the symlink. The old code always complained that the 721da177e4SLinus Torvalds * name already exists, due to not following the symlink even if its target 731da177e4SLinus Torvalds * is nonexistent. The new semantics affects also mknod() and link() when 7425985edcSLucas De Marchi * the name is a symlink pointing to a non-existent name. 751da177e4SLinus Torvalds * 761da177e4SLinus Torvalds * I don't know which semantics is the right one, since I have no access 771da177e4SLinus Torvalds * to standards. But I found by trial that HP-UX 9.0 has the full "new" 781da177e4SLinus Torvalds * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the 791da177e4SLinus Torvalds * "old" one. Personally, I think the new semantics is much more logical. 801da177e4SLinus Torvalds * Note that "ln old new" where "new" is a symlink pointing to a non-existing 811da177e4SLinus Torvalds * file does succeed in both HP-UX and SunOs, but not in Solaris 821da177e4SLinus Torvalds * and in the old Linux semantics. 831da177e4SLinus Torvalds */ 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink 861da177e4SLinus Torvalds * semantics. See the comments in "open_namei" and "do_link" below. 871da177e4SLinus Torvalds * 881da177e4SLinus Torvalds * [10-Sep-98 Alan Modra] Another symlink change. 891da177e4SLinus Torvalds */ 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: 921da177e4SLinus Torvalds * inside the path - always follow. 931da177e4SLinus Torvalds * in the last component in creation/removal/renaming - never follow. 941da177e4SLinus Torvalds * if LOOKUP_FOLLOW passed - follow. 951da177e4SLinus Torvalds * if the pathname has trailing slashes - follow. 961da177e4SLinus Torvalds * otherwise - don't follow. 971da177e4SLinus Torvalds * (applied in that order). 981da177e4SLinus Torvalds * 991da177e4SLinus Torvalds * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT 1001da177e4SLinus Torvalds * restored for 2.4. This is the last surviving part of old 4.2BSD bug. 1011da177e4SLinus Torvalds * During the 2.4 we need to fix the userland stuff depending on it - 1021da177e4SLinus Torvalds * hopefully we will be able to get rid of that wart in 2.5. So far only 1031da177e4SLinus Torvalds * XEmacs seems to be relying on it... 1041da177e4SLinus Torvalds */ 1051da177e4SLinus Torvalds /* 1061da177e4SLinus Torvalds * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) 107a11f3a05SArjan van de Ven * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives 1081da177e4SLinus Torvalds * any extra contention... 1091da177e4SLinus Torvalds */ 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional 1121da177e4SLinus Torvalds * checking and hopefully speeding things up, we copy filenames to the 1131da177e4SLinus Torvalds * kernel data space before using them.. 1141da177e4SLinus Torvalds * 1151da177e4SLinus Torvalds * POSIX.1 2.4: an empty pathname is invalid (ENOENT). 1161da177e4SLinus Torvalds * PATH_MAX includes the nul terminator --RR. 1171da177e4SLinus Torvalds */ 118858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page) 1191da177e4SLinus Torvalds { 1201da177e4SLinus Torvalds int retval; 1211da177e4SLinus Torvalds unsigned long len = PATH_MAX; 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds if (!segment_eq(get_fs(), KERNEL_DS)) { 1241da177e4SLinus Torvalds if ((unsigned long) filename >= TASK_SIZE) 1251da177e4SLinus Torvalds return -EFAULT; 1261da177e4SLinus Torvalds if (TASK_SIZE - (unsigned long) filename < PATH_MAX) 1271da177e4SLinus Torvalds len = TASK_SIZE - (unsigned long) filename; 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds retval = strncpy_from_user(page, filename, len); 1311da177e4SLinus Torvalds if (retval > 0) { 1321da177e4SLinus Torvalds if (retval < len) 1331da177e4SLinus Torvalds return 0; 1341da177e4SLinus Torvalds return -ENAMETOOLONG; 1351da177e4SLinus Torvalds } else if (!retval) 1361da177e4SLinus Torvalds retval = -ENOENT; 1371da177e4SLinus Torvalds return retval; 1381da177e4SLinus Torvalds } 1391da177e4SLinus Torvalds 140f52e0c11SAl Viro static char *getname_flags(const char __user * filename, int flags) 1411da177e4SLinus Torvalds { 1421da177e4SLinus Torvalds char *tmp, *result; 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds result = ERR_PTR(-ENOMEM); 1451da177e4SLinus Torvalds tmp = __getname(); 1461da177e4SLinus Torvalds if (tmp) { 1471da177e4SLinus Torvalds int retval = do_getname(filename, tmp); 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds result = tmp; 1501da177e4SLinus Torvalds if (retval < 0) { 151f52e0c11SAl Viro if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) { 1521da177e4SLinus Torvalds __putname(tmp); 1531da177e4SLinus Torvalds result = ERR_PTR(retval); 1541da177e4SLinus Torvalds } 1551da177e4SLinus Torvalds } 156f52e0c11SAl Viro } 1571da177e4SLinus Torvalds audit_getname(result); 1581da177e4SLinus Torvalds return result; 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 161f52e0c11SAl Viro char *getname(const char __user * filename) 162f52e0c11SAl Viro { 163f52e0c11SAl Viro return getname_flags(filename, 0); 164f52e0c11SAl Viro } 165f52e0c11SAl Viro 1661da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 1671da177e4SLinus Torvalds void putname(const char *name) 1681da177e4SLinus Torvalds { 1695ac3a9c2SAl Viro if (unlikely(!audit_dummy_context())) 1701da177e4SLinus Torvalds audit_putname(name); 1711da177e4SLinus Torvalds else 1721da177e4SLinus Torvalds __putname(name); 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds EXPORT_SYMBOL(putname); 1751da177e4SLinus Torvalds #endif 1761da177e4SLinus Torvalds 177e77819e5SLinus Torvalds static int check_acl(struct inode *inode, int mask) 178e77819e5SLinus Torvalds { 17984635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL 180e77819e5SLinus Torvalds struct posix_acl *acl; 181e77819e5SLinus Torvalds 182e77819e5SLinus Torvalds if (mask & MAY_NOT_BLOCK) { 1833567866bSAl Viro acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS); 1843567866bSAl Viro if (!acl) 185e77819e5SLinus Torvalds return -EAGAIN; 1863567866bSAl Viro /* no ->get_acl() calls in RCU mode... */ 1873567866bSAl Viro if (acl == ACL_NOT_CACHED) 188e77819e5SLinus Torvalds return -ECHILD; 1893567866bSAl Viro return posix_acl_permission(inode, acl, mask); 190e77819e5SLinus Torvalds } 191e77819e5SLinus Torvalds 192e77819e5SLinus Torvalds acl = get_cached_acl(inode, ACL_TYPE_ACCESS); 193e77819e5SLinus Torvalds 194e77819e5SLinus Torvalds /* 1954e34e719SChristoph Hellwig * A filesystem can force a ACL callback by just never filling the 1964e34e719SChristoph Hellwig * ACL cache. But normally you'd fill the cache either at inode 1974e34e719SChristoph Hellwig * instantiation time, or on the first ->get_acl call. 198e77819e5SLinus Torvalds * 1994e34e719SChristoph Hellwig * If the filesystem doesn't have a get_acl() function at all, we'll 2004e34e719SChristoph Hellwig * just create the negative cache entry. 201e77819e5SLinus Torvalds */ 202e77819e5SLinus Torvalds if (acl == ACL_NOT_CACHED) { 2034e34e719SChristoph Hellwig if (inode->i_op->get_acl) { 2044e34e719SChristoph Hellwig acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS); 2054e34e719SChristoph Hellwig if (IS_ERR(acl)) 2064e34e719SChristoph Hellwig return PTR_ERR(acl); 2074e34e719SChristoph Hellwig } else { 208e77819e5SLinus Torvalds set_cached_acl(inode, ACL_TYPE_ACCESS, NULL); 209e77819e5SLinus Torvalds return -EAGAIN; 210e77819e5SLinus Torvalds } 2114e34e719SChristoph Hellwig } 212e77819e5SLinus Torvalds 213e77819e5SLinus Torvalds if (acl) { 214e77819e5SLinus Torvalds int error = posix_acl_permission(inode, acl, mask); 215e77819e5SLinus Torvalds posix_acl_release(acl); 216e77819e5SLinus Torvalds return error; 217e77819e5SLinus Torvalds } 21884635d68SLinus Torvalds #endif 219e77819e5SLinus Torvalds 220e77819e5SLinus Torvalds return -EAGAIN; 221e77819e5SLinus Torvalds } 222e77819e5SLinus Torvalds 2235909ccaaSLinus Torvalds /* 2245909ccaaSLinus Torvalds * This does basic POSIX ACL permission checking 2255909ccaaSLinus Torvalds */ 2267e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask) 2275909ccaaSLinus Torvalds { 22826cf46beSLinus Torvalds unsigned int mode = inode->i_mode; 2295909ccaaSLinus Torvalds 2309c2c7039SAl Viro mask &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK; 2315909ccaaSLinus Torvalds 232e795b717SSerge E. Hallyn if (current_user_ns() != inode_userns(inode)) 233e795b717SSerge E. Hallyn goto other_perms; 234e795b717SSerge E. Hallyn 23514067ff5SLinus Torvalds if (likely(current_fsuid() == inode->i_uid)) 2365909ccaaSLinus Torvalds mode >>= 6; 2375909ccaaSLinus Torvalds else { 238e77819e5SLinus Torvalds if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { 2397e40145eSAl Viro int error = check_acl(inode, mask); 2405909ccaaSLinus Torvalds if (error != -EAGAIN) 2415909ccaaSLinus Torvalds return error; 2425909ccaaSLinus Torvalds } 2435909ccaaSLinus Torvalds 2445909ccaaSLinus Torvalds if (in_group_p(inode->i_gid)) 2455909ccaaSLinus Torvalds mode >>= 3; 2465909ccaaSLinus Torvalds } 2475909ccaaSLinus Torvalds 248e795b717SSerge E. Hallyn other_perms: 2495909ccaaSLinus Torvalds /* 2505909ccaaSLinus Torvalds * If the DACs are ok we don't need any capability check. 2515909ccaaSLinus Torvalds */ 2529c2c7039SAl Viro if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 2535909ccaaSLinus Torvalds return 0; 2545909ccaaSLinus Torvalds return -EACCES; 2555909ccaaSLinus Torvalds } 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds /** 2581da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 2591da177e4SLinus Torvalds * @inode: inode to check access rights for 2601da177e4SLinus Torvalds * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 2611da177e4SLinus Torvalds * 2621da177e4SLinus Torvalds * Used to check for read/write/execute permissions on a file. 2631da177e4SLinus Torvalds * We use "fsuid" for this, letting us set arbitrary permissions 2641da177e4SLinus Torvalds * for filesystem access without changing the "normal" uids which 265b74c79e9SNick Piggin * are used for other things. 266b74c79e9SNick Piggin * 267b74c79e9SNick Piggin * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk 268b74c79e9SNick Piggin * request cannot be satisfied (eg. requires blocking or too much complexity). 269b74c79e9SNick Piggin * It would then be called again in ref-walk mode. 2701da177e4SLinus Torvalds */ 2712830ba7fSAl Viro int generic_permission(struct inode *inode, int mask) 2721da177e4SLinus Torvalds { 2735909ccaaSLinus Torvalds int ret; 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds /* 2765909ccaaSLinus Torvalds * Do the basic POSIX ACL permission checks. 2771da177e4SLinus Torvalds */ 2787e40145eSAl Viro ret = acl_permission_check(inode, mask); 2795909ccaaSLinus Torvalds if (ret != -EACCES) 2805909ccaaSLinus Torvalds return ret; 2811da177e4SLinus Torvalds 282d594e7ecSAl Viro if (S_ISDIR(inode->i_mode)) { 283d594e7ecSAl Viro /* DACs are overridable for directories */ 284d594e7ecSAl Viro if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE)) 285d594e7ecSAl Viro return 0; 286d594e7ecSAl Viro if (!(mask & MAY_WRITE)) 287d594e7ecSAl Viro if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH)) 288d594e7ecSAl Viro return 0; 289d594e7ecSAl Viro return -EACCES; 290d594e7ecSAl Viro } 2911da177e4SLinus Torvalds /* 2921da177e4SLinus Torvalds * Read/write DACs are always overridable. 293d594e7ecSAl Viro * Executable DACs are overridable when there is 294d594e7ecSAl Viro * at least one exec bit set. 2951da177e4SLinus Torvalds */ 296d594e7ecSAl Viro if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) 297e795b717SSerge E. Hallyn if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE)) 2981da177e4SLinus Torvalds return 0; 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds /* 3011da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 3021da177e4SLinus Torvalds */ 3037ea66001SSerge E. Hallyn mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 304d594e7ecSAl Viro if (mask == MAY_READ) 305e795b717SSerge E. Hallyn if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH)) 3061da177e4SLinus Torvalds return 0; 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds return -EACCES; 3091da177e4SLinus Torvalds } 3101da177e4SLinus Torvalds 311cb23beb5SChristoph Hellwig /** 312cb23beb5SChristoph Hellwig * inode_permission - check for access rights to a given inode 313cb23beb5SChristoph Hellwig * @inode: inode to check permission on 314cb23beb5SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 315cb23beb5SChristoph Hellwig * 316cb23beb5SChristoph Hellwig * Used to check for read/write/execute permissions on an inode. 317cb23beb5SChristoph Hellwig * We use "fsuid" for this, letting us set arbitrary permissions 318cb23beb5SChristoph Hellwig * for filesystem access without changing the "normal" uids which 319cb23beb5SChristoph Hellwig * are used for other things. 320cb23beb5SChristoph Hellwig */ 321f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask) 3221da177e4SLinus Torvalds { 323e6305c43SAl Viro int retval; 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds if (mask & MAY_WRITE) { 32622590e41SMiklos Szeredi umode_t mode = inode->i_mode; 3271da177e4SLinus Torvalds 3281da177e4SLinus Torvalds /* 3291da177e4SLinus Torvalds * Nobody gets write access to a read-only fs. 3301da177e4SLinus Torvalds */ 3311da177e4SLinus Torvalds if (IS_RDONLY(inode) && 3321da177e4SLinus Torvalds (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 3331da177e4SLinus Torvalds return -EROFS; 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds /* 3361da177e4SLinus Torvalds * Nobody gets write access to an immutable file. 3371da177e4SLinus Torvalds */ 3381da177e4SLinus Torvalds if (IS_IMMUTABLE(inode)) 3391da177e4SLinus Torvalds return -EACCES; 3401da177e4SLinus Torvalds } 3411da177e4SLinus Torvalds 342acfa4380SAl Viro if (inode->i_op->permission) 34310556cb2SAl Viro retval = inode->i_op->permission(inode, mask); 344f696a365SMiklos Szeredi else 3452830ba7fSAl Viro retval = generic_permission(inode, mask); 346f696a365SMiklos Szeredi 3471da177e4SLinus Torvalds if (retval) 3481da177e4SLinus Torvalds return retval; 3491da177e4SLinus Torvalds 35008ce5f16SSerge E. Hallyn retval = devcgroup_inode_permission(inode, mask); 35108ce5f16SSerge E. Hallyn if (retval) 35208ce5f16SSerge E. Hallyn return retval; 35308ce5f16SSerge E. Hallyn 354d09ca739SEric Paris return security_inode_permission(inode, mask); 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds 357f4d6ff89SAl Viro /** 3585dd784d0SJan Blunck * path_get - get a reference to a path 3595dd784d0SJan Blunck * @path: path to get the reference to 3605dd784d0SJan Blunck * 3615dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 3625dd784d0SJan Blunck */ 3635dd784d0SJan Blunck void path_get(struct path *path) 3645dd784d0SJan Blunck { 3655dd784d0SJan Blunck mntget(path->mnt); 3665dd784d0SJan Blunck dget(path->dentry); 3675dd784d0SJan Blunck } 3685dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 3695dd784d0SJan Blunck 3705dd784d0SJan Blunck /** 3711d957f9bSJan Blunck * path_put - put a reference to a path 3721d957f9bSJan Blunck * @path: path to put the reference to 3731d957f9bSJan Blunck * 3741d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 3751d957f9bSJan Blunck */ 3761d957f9bSJan Blunck void path_put(struct path *path) 3771da177e4SLinus Torvalds { 3781d957f9bSJan Blunck dput(path->dentry); 3791d957f9bSJan Blunck mntput(path->mnt); 3801da177e4SLinus Torvalds } 3811d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 3821da177e4SLinus Torvalds 38319660af7SAl Viro /* 38431e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 38519660af7SAl Viro * Documentation/filesystems/path-lookup.txt). In situations when we can't 38619660af7SAl Viro * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab 38719660af7SAl Viro * normal reference counts on dentries and vfsmounts to transition to rcu-walk 38819660af7SAl Viro * mode. Refcounts are grabbed at the last known good point before rcu-walk 38919660af7SAl Viro * got stuck, so ref-walk may continue from there. If this is not successful 39019660af7SAl Viro * (eg. a seqcount has changed), then failure is returned and it's up to caller 39119660af7SAl Viro * to restart the path walk from the beginning in ref-walk mode. 39231e6b01fSNick Piggin */ 39331e6b01fSNick Piggin 39431e6b01fSNick Piggin /** 39519660af7SAl Viro * unlazy_walk - try to switch to ref-walk mode. 39619660af7SAl Viro * @nd: nameidata pathwalk data 39719660af7SAl Viro * @dentry: child of nd->path.dentry or NULL 39839191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 39931e6b01fSNick Piggin * 40019660af7SAl Viro * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry 40119660af7SAl Viro * for ref-walk mode. @dentry must be a path found by a do_lookup call on 40219660af7SAl Viro * @nd or NULL. Must be called from rcu-walk context. 40331e6b01fSNick Piggin */ 40419660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry) 40531e6b01fSNick Piggin { 40631e6b01fSNick Piggin struct fs_struct *fs = current->fs; 40731e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 4085b6ca027SAl Viro int want_root = 0; 40931e6b01fSNick Piggin 41031e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 4115b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 4125b6ca027SAl Viro want_root = 1; 41331e6b01fSNick Piggin spin_lock(&fs->lock); 41431e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 41531e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 41631e6b01fSNick Piggin goto err_root; 41731e6b01fSNick Piggin } 41831e6b01fSNick Piggin spin_lock(&parent->d_lock); 41919660af7SAl Viro if (!dentry) { 42019660af7SAl Viro if (!__d_rcu_to_refcount(parent, nd->seq)) 42119660af7SAl Viro goto err_parent; 42219660af7SAl Viro BUG_ON(nd->inode != parent->d_inode); 42319660af7SAl Viro } else { 42494c0d4ecSAl Viro if (dentry->d_parent != parent) 42594c0d4ecSAl Viro goto err_parent; 42631e6b01fSNick Piggin spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); 42731e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 42819660af7SAl Viro goto err_child; 42931e6b01fSNick Piggin /* 43019660af7SAl Viro * If the sequence check on the child dentry passed, then 43119660af7SAl Viro * the child has not been removed from its parent. This 43219660af7SAl Viro * means the parent dentry must be valid and able to take 43319660af7SAl Viro * a reference at this point. 43431e6b01fSNick Piggin */ 43531e6b01fSNick Piggin BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); 43631e6b01fSNick Piggin BUG_ON(!parent->d_count); 43731e6b01fSNick Piggin parent->d_count++; 43831e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 43919660af7SAl Viro } 44031e6b01fSNick Piggin spin_unlock(&parent->d_lock); 4415b6ca027SAl Viro if (want_root) { 44231e6b01fSNick Piggin path_get(&nd->root); 44331e6b01fSNick Piggin spin_unlock(&fs->lock); 44431e6b01fSNick Piggin } 44531e6b01fSNick Piggin mntget(nd->path.mnt); 44631e6b01fSNick Piggin 44731e6b01fSNick Piggin rcu_read_unlock(); 44831e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 44931e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 45031e6b01fSNick Piggin return 0; 45119660af7SAl Viro 45219660af7SAl Viro err_child: 45331e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 45419660af7SAl Viro err_parent: 45531e6b01fSNick Piggin spin_unlock(&parent->d_lock); 45631e6b01fSNick Piggin err_root: 4575b6ca027SAl Viro if (want_root) 45831e6b01fSNick Piggin spin_unlock(&fs->lock); 45931e6b01fSNick Piggin return -ECHILD; 46031e6b01fSNick Piggin } 46131e6b01fSNick Piggin 46231e6b01fSNick Piggin /** 463834f2a4aSTrond Myklebust * release_open_intent - free up open intent resources 464834f2a4aSTrond Myklebust * @nd: pointer to nameidata 465834f2a4aSTrond Myklebust */ 466834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd) 467834f2a4aSTrond Myklebust { 4682dab5974SLinus Torvalds struct file *file = nd->intent.open.file; 4692dab5974SLinus Torvalds 4702dab5974SLinus Torvalds if (file && !IS_ERR(file)) { 4712dab5974SLinus Torvalds if (file->f_path.dentry == NULL) 4722dab5974SLinus Torvalds put_filp(file); 473834f2a4aSTrond Myklebust else 4742dab5974SLinus Torvalds fput(file); 4752dab5974SLinus Torvalds } 476834f2a4aSTrond Myklebust } 477834f2a4aSTrond Myklebust 478f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd) 47934286d66SNick Piggin { 480f60aef7eSAl Viro return dentry->d_op->d_revalidate(dentry, nd); 48134286d66SNick Piggin } 48234286d66SNick Piggin 4839f1fafeeSAl Viro /** 4849f1fafeeSAl Viro * complete_walk - successful completion of path walk 4859f1fafeeSAl Viro * @nd: pointer nameidata 48639159de2SJeff Layton * 4879f1fafeeSAl Viro * If we had been in RCU mode, drop out of it and legitimize nd->path. 4889f1fafeeSAl Viro * Revalidate the final result, unless we'd already done that during 4899f1fafeeSAl Viro * the path walk or the filesystem doesn't ask for it. Return 0 on 4909f1fafeeSAl Viro * success, -error on failure. In case of failure caller does not 4919f1fafeeSAl Viro * need to drop nd->path. 49239159de2SJeff Layton */ 4939f1fafeeSAl Viro static int complete_walk(struct nameidata *nd) 49439159de2SJeff Layton { 49516c2cd71SAl Viro struct dentry *dentry = nd->path.dentry; 49639159de2SJeff Layton int status; 49739159de2SJeff Layton 4989f1fafeeSAl Viro if (nd->flags & LOOKUP_RCU) { 4999f1fafeeSAl Viro nd->flags &= ~LOOKUP_RCU; 5009f1fafeeSAl Viro if (!(nd->flags & LOOKUP_ROOT)) 5019f1fafeeSAl Viro nd->root.mnt = NULL; 5029f1fafeeSAl Viro spin_lock(&dentry->d_lock); 5039f1fafeeSAl Viro if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) { 5049f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 5059f1fafeeSAl Viro rcu_read_unlock(); 5069f1fafeeSAl Viro br_read_unlock(vfsmount_lock); 5079f1fafeeSAl Viro return -ECHILD; 5089f1fafeeSAl Viro } 5099f1fafeeSAl Viro BUG_ON(nd->inode != dentry->d_inode); 5109f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 5119f1fafeeSAl Viro mntget(nd->path.mnt); 5129f1fafeeSAl Viro rcu_read_unlock(); 5139f1fafeeSAl Viro br_read_unlock(vfsmount_lock); 5149f1fafeeSAl Viro } 5159f1fafeeSAl Viro 51616c2cd71SAl Viro if (likely(!(nd->flags & LOOKUP_JUMPED))) 51739159de2SJeff Layton return 0; 51839159de2SJeff Layton 51916c2cd71SAl Viro if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE))) 52016c2cd71SAl Viro return 0; 52116c2cd71SAl Viro 52216c2cd71SAl Viro if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))) 52316c2cd71SAl Viro return 0; 52416c2cd71SAl Viro 52516c2cd71SAl Viro /* Note: we do not d_invalidate() */ 52634286d66SNick Piggin status = d_revalidate(dentry, nd); 52739159de2SJeff Layton if (status > 0) 52839159de2SJeff Layton return 0; 52939159de2SJeff Layton 53016c2cd71SAl Viro if (!status) 53139159de2SJeff Layton status = -ESTALE; 53216c2cd71SAl Viro 5339f1fafeeSAl Viro path_put(&nd->path); 53439159de2SJeff Layton return status; 53539159de2SJeff Layton } 53639159de2SJeff Layton 5372a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 5382a737871SAl Viro { 539f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 540f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 5412a737871SAl Viro } 5422a737871SAl Viro 5436de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 5446de88d72SAl Viro 54531e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 54631e6b01fSNick Piggin { 54731e6b01fSNick Piggin if (!nd->root.mnt) { 54831e6b01fSNick Piggin struct fs_struct *fs = current->fs; 549c28cc364SNick Piggin unsigned seq; 550c28cc364SNick Piggin 551c28cc364SNick Piggin do { 552c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 55331e6b01fSNick Piggin nd->root = fs->root; 554c1530019STim Chen nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq); 555c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 55631e6b01fSNick Piggin } 55731e6b01fSNick Piggin } 55831e6b01fSNick Piggin 559f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 5601da177e4SLinus Torvalds { 56131e6b01fSNick Piggin int ret; 56231e6b01fSNick Piggin 5631da177e4SLinus Torvalds if (IS_ERR(link)) 5641da177e4SLinus Torvalds goto fail; 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds if (*link == '/') { 5672a737871SAl Viro set_root(nd); 5681d957f9bSJan Blunck path_put(&nd->path); 5692a737871SAl Viro nd->path = nd->root; 5702a737871SAl Viro path_get(&nd->root); 57116c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 5721da177e4SLinus Torvalds } 57331e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 574b4091d5fSChristoph Hellwig 57531e6b01fSNick Piggin ret = link_path_walk(link, nd); 57631e6b01fSNick Piggin return ret; 5771da177e4SLinus Torvalds fail: 5781d957f9bSJan Blunck path_put(&nd->path); 5791da177e4SLinus Torvalds return PTR_ERR(link); 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds 5821d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 583051d3812SIan Kent { 584051d3812SIan Kent dput(path->dentry); 5854ac91378SJan Blunck if (path->mnt != nd->path.mnt) 586051d3812SIan Kent mntput(path->mnt); 587051d3812SIan Kent } 588051d3812SIan Kent 5897b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path, 5907b9337aaSNick Piggin struct nameidata *nd) 591051d3812SIan Kent { 59231e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 5934ac91378SJan Blunck dput(nd->path.dentry); 59431e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 5954ac91378SJan Blunck mntput(nd->path.mnt); 5969a229683SHuang Shijie } 59731e6b01fSNick Piggin nd->path.mnt = path->mnt; 5984ac91378SJan Blunck nd->path.dentry = path->dentry; 599051d3812SIan Kent } 600051d3812SIan Kent 601574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie) 602574197e0SAl Viro { 603574197e0SAl Viro struct inode *inode = link->dentry->d_inode; 604574197e0SAl Viro if (!IS_ERR(cookie) && inode->i_op->put_link) 605574197e0SAl Viro inode->i_op->put_link(link->dentry, nd, cookie); 606574197e0SAl Viro path_put(link); 607574197e0SAl Viro } 608574197e0SAl Viro 609def4af30SAl Viro static __always_inline int 610574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p) 6111da177e4SLinus Torvalds { 6121da177e4SLinus Torvalds int error; 6137b9337aaSNick Piggin struct dentry *dentry = link->dentry; 6141da177e4SLinus Torvalds 615844a3917SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 616844a3917SAl Viro 6170e794589SAl Viro if (link->mnt == nd->path.mnt) 6180e794589SAl Viro mntget(link->mnt); 6190e794589SAl Viro 620574197e0SAl Viro if (unlikely(current->total_link_count >= 40)) { 621574197e0SAl Viro *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */ 622574197e0SAl Viro path_put(&nd->path); 623574197e0SAl Viro return -ELOOP; 624574197e0SAl Viro } 625574197e0SAl Viro cond_resched(); 626574197e0SAl Viro current->total_link_count++; 627574197e0SAl Viro 6287b9337aaSNick Piggin touch_atime(link->mnt, dentry); 6291da177e4SLinus Torvalds nd_set_link(nd, NULL); 630cd4e91d3SAl Viro 63136f3b4f6SAl Viro error = security_inode_follow_link(link->dentry, nd); 63236f3b4f6SAl Viro if (error) { 63336f3b4f6SAl Viro *p = ERR_PTR(error); /* no ->put_link(), please */ 63436f3b4f6SAl Viro path_put(&nd->path); 63536f3b4f6SAl Viro return error; 63636f3b4f6SAl Viro } 63736f3b4f6SAl Viro 63886acdca1SAl Viro nd->last_type = LAST_BIND; 639def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 640def4af30SAl Viro error = PTR_ERR(*p); 641def4af30SAl Viro if (!IS_ERR(*p)) { 6421da177e4SLinus Torvalds char *s = nd_get_link(nd); 643cc314eefSLinus Torvalds error = 0; 6441da177e4SLinus Torvalds if (s) 6451da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 646bcda7652SAl Viro else if (nd->last_type == LAST_BIND) { 64716c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 648b21041d0SAl Viro nd->inode = nd->path.dentry->d_inode; 649b21041d0SAl Viro if (nd->inode->i_op->follow_link) { 650bcda7652SAl Viro /* stepped on a _really_ weird one */ 651bcda7652SAl Viro path_put(&nd->path); 652bcda7652SAl Viro error = -ELOOP; 653bcda7652SAl Viro } 654bcda7652SAl Viro } 6551da177e4SLinus Torvalds } 6561da177e4SLinus Torvalds return error; 6571da177e4SLinus Torvalds } 6581da177e4SLinus Torvalds 65931e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 66031e6b01fSNick Piggin { 66131e6b01fSNick Piggin struct vfsmount *parent; 66231e6b01fSNick Piggin struct dentry *mountpoint; 66331e6b01fSNick Piggin 66431e6b01fSNick Piggin parent = path->mnt->mnt_parent; 66531e6b01fSNick Piggin if (parent == path->mnt) 66631e6b01fSNick Piggin return 0; 66731e6b01fSNick Piggin mountpoint = path->mnt->mnt_mountpoint; 66831e6b01fSNick Piggin path->dentry = mountpoint; 66931e6b01fSNick Piggin path->mnt = parent; 67031e6b01fSNick Piggin return 1; 67131e6b01fSNick Piggin } 67231e6b01fSNick Piggin 673bab77ebfSAl Viro int follow_up(struct path *path) 6741da177e4SLinus Torvalds { 6751da177e4SLinus Torvalds struct vfsmount *parent; 6761da177e4SLinus Torvalds struct dentry *mountpoint; 67799b7db7bSNick Piggin 67899b7db7bSNick Piggin br_read_lock(vfsmount_lock); 679bab77ebfSAl Viro parent = path->mnt->mnt_parent; 680bab77ebfSAl Viro if (parent == path->mnt) { 68199b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 6821da177e4SLinus Torvalds return 0; 6831da177e4SLinus Torvalds } 6841da177e4SLinus Torvalds mntget(parent); 685bab77ebfSAl Viro mountpoint = dget(path->mnt->mnt_mountpoint); 68699b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 687bab77ebfSAl Viro dput(path->dentry); 688bab77ebfSAl Viro path->dentry = mountpoint; 689bab77ebfSAl Viro mntput(path->mnt); 690bab77ebfSAl Viro path->mnt = parent; 6911da177e4SLinus Torvalds return 1; 6921da177e4SLinus Torvalds } 6931da177e4SLinus Torvalds 694b5c84bf6SNick Piggin /* 6959875cf80SDavid Howells * Perform an automount 6969875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 6979875cf80SDavid Howells * were called with. 6981da177e4SLinus Torvalds */ 6999875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags, 7009875cf80SDavid Howells bool *need_mntput) 70131e6b01fSNick Piggin { 7029875cf80SDavid Howells struct vfsmount *mnt; 703ea5b778aSDavid Howells int err; 7049875cf80SDavid Howells 7059875cf80SDavid Howells if (!path->dentry->d_op || !path->dentry->d_op->d_automount) 7069875cf80SDavid Howells return -EREMOTE; 7079875cf80SDavid Howells 7086f45b656SDavid Howells /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT 7096f45b656SDavid Howells * and this is the terminal part of the path. 7106f45b656SDavid Howells */ 71149084c3bSAl Viro if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_PARENT)) 7126f45b656SDavid Howells return -EISDIR; /* we actually want to stop here */ 7136f45b656SDavid Howells 7145a30d8a2SDavid Howells /* 7159875cf80SDavid Howells * We don't want to mount if someone's just doing a stat and they've 7169875cf80SDavid Howells * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and 7179875cf80SDavid Howells * appended a '/' to the name. 7189875cf80SDavid Howells */ 7195a30d8a2SDavid Howells if (!(flags & LOOKUP_FOLLOW)) { 7205a30d8a2SDavid Howells /* We do, however, want to mount if someone wants to open or 7215a30d8a2SDavid Howells * create a file of any type under the mountpoint, wants to 7225a30d8a2SDavid Howells * traverse through the mountpoint or wants to open the mounted 7235a30d8a2SDavid Howells * directory. 7245a30d8a2SDavid Howells * Also, autofs may mark negative dentries as being automount 7255a30d8a2SDavid Howells * points. These will need the attentions of the daemon to 7265a30d8a2SDavid Howells * instantiate them before they can be used. 7275a30d8a2SDavid Howells */ 7285a30d8a2SDavid Howells if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | 7295a30d8a2SDavid Howells LOOKUP_OPEN | LOOKUP_CREATE)) && 7305a30d8a2SDavid Howells path->dentry->d_inode) 7319875cf80SDavid Howells return -EISDIR; 7325a30d8a2SDavid Howells } 7339875cf80SDavid Howells current->total_link_count++; 7349875cf80SDavid Howells if (current->total_link_count >= 40) 7359875cf80SDavid Howells return -ELOOP; 7369875cf80SDavid Howells 7379875cf80SDavid Howells mnt = path->dentry->d_op->d_automount(path); 7389875cf80SDavid Howells if (IS_ERR(mnt)) { 7399875cf80SDavid Howells /* 7409875cf80SDavid Howells * The filesystem is allowed to return -EISDIR here to indicate 7419875cf80SDavid Howells * it doesn't want to automount. For instance, autofs would do 7429875cf80SDavid Howells * this so that its userspace daemon can mount on this dentry. 7439875cf80SDavid Howells * 7449875cf80SDavid Howells * However, we can only permit this if it's a terminal point in 7459875cf80SDavid Howells * the path being looked up; if it wasn't then the remainder of 7469875cf80SDavid Howells * the path is inaccessible and we should say so. 7479875cf80SDavid Howells */ 74849084c3bSAl Viro if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT)) 7499875cf80SDavid Howells return -EREMOTE; 7509875cf80SDavid Howells return PTR_ERR(mnt); 75131e6b01fSNick Piggin } 752ea5b778aSDavid Howells 7539875cf80SDavid Howells if (!mnt) /* mount collision */ 7549875cf80SDavid Howells return 0; 7559875cf80SDavid Howells 7568aef1884SAl Viro if (!*need_mntput) { 7578aef1884SAl Viro /* lock_mount() may release path->mnt on error */ 7588aef1884SAl Viro mntget(path->mnt); 7598aef1884SAl Viro *need_mntput = true; 7608aef1884SAl Viro } 76119a167afSAl Viro err = finish_automount(mnt, path); 762ea5b778aSDavid Howells 763ea5b778aSDavid Howells switch (err) { 764ea5b778aSDavid Howells case -EBUSY: 765ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 76619a167afSAl Viro return 0; 767ea5b778aSDavid Howells case 0: 7688aef1884SAl Viro path_put(path); 7699875cf80SDavid Howells path->mnt = mnt; 7709875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 7719875cf80SDavid Howells return 0; 77219a167afSAl Viro default: 77319a167afSAl Viro return err; 7749875cf80SDavid Howells } 77519a167afSAl Viro 776ea5b778aSDavid Howells } 7779875cf80SDavid Howells 7789875cf80SDavid Howells /* 7799875cf80SDavid Howells * Handle a dentry that is managed in some way. 780cc53ce53SDavid Howells * - Flagged for transit management (autofs) 7819875cf80SDavid Howells * - Flagged as mountpoint 7829875cf80SDavid Howells * - Flagged as automount point 7839875cf80SDavid Howells * 7849875cf80SDavid Howells * This may only be called in refwalk mode. 7859875cf80SDavid Howells * 7869875cf80SDavid Howells * Serialization is taken care of in namespace.c 7879875cf80SDavid Howells */ 7889875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 7899875cf80SDavid Howells { 7908aef1884SAl Viro struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */ 7919875cf80SDavid Howells unsigned managed; 7929875cf80SDavid Howells bool need_mntput = false; 7938aef1884SAl Viro int ret = 0; 7949875cf80SDavid Howells 7959875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 7969875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 7979875cf80SDavid Howells * the components of that value change under us */ 7989875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 7999875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 8009875cf80SDavid Howells unlikely(managed != 0)) { 801cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 802cc53ce53SDavid Howells * being held. */ 803cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 804cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 805cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 8061aed3e42SAl Viro ret = path->dentry->d_op->d_manage(path->dentry, false); 807cc53ce53SDavid Howells if (ret < 0) 8088aef1884SAl Viro break; 809cc53ce53SDavid Howells } 810cc53ce53SDavid Howells 8119875cf80SDavid Howells /* Transit to a mounted filesystem. */ 8129875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 8139875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 8149875cf80SDavid Howells if (mounted) { 8159875cf80SDavid Howells dput(path->dentry); 8169875cf80SDavid Howells if (need_mntput) 817463ffb2eSAl Viro mntput(path->mnt); 818463ffb2eSAl Viro path->mnt = mounted; 819463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 8209875cf80SDavid Howells need_mntput = true; 8219875cf80SDavid Howells continue; 822463ffb2eSAl Viro } 823463ffb2eSAl Viro 8249875cf80SDavid Howells /* Something is mounted on this dentry in another 8259875cf80SDavid Howells * namespace and/or whatever was mounted there in this 8269875cf80SDavid Howells * namespace got unmounted before we managed to get the 8279875cf80SDavid Howells * vfsmount_lock */ 8281da177e4SLinus Torvalds } 8299875cf80SDavid Howells 8309875cf80SDavid Howells /* Handle an automount point */ 8319875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 8329875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 8339875cf80SDavid Howells if (ret < 0) 8348aef1884SAl Viro break; 8359875cf80SDavid Howells continue; 8369875cf80SDavid Howells } 8379875cf80SDavid Howells 8389875cf80SDavid Howells /* We didn't change the current path point */ 8399875cf80SDavid Howells break; 8409875cf80SDavid Howells } 8418aef1884SAl Viro 8428aef1884SAl Viro if (need_mntput && path->mnt == mnt) 8438aef1884SAl Viro mntput(path->mnt); 8448aef1884SAl Viro if (ret == -EISDIR) 8458aef1884SAl Viro ret = 0; 8468aef1884SAl Viro return ret; 8471da177e4SLinus Torvalds } 8481da177e4SLinus Torvalds 849cc53ce53SDavid Howells int follow_down_one(struct path *path) 8501da177e4SLinus Torvalds { 8511da177e4SLinus Torvalds struct vfsmount *mounted; 8521da177e4SLinus Torvalds 8531c755af4SAl Viro mounted = lookup_mnt(path); 8541da177e4SLinus Torvalds if (mounted) { 8559393bd07SAl Viro dput(path->dentry); 8569393bd07SAl Viro mntput(path->mnt); 8579393bd07SAl Viro path->mnt = mounted; 8589393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 8591da177e4SLinus Torvalds return 1; 8601da177e4SLinus Torvalds } 8611da177e4SLinus Torvalds return 0; 8621da177e4SLinus Torvalds } 8631da177e4SLinus Torvalds 86462a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry) 86562a7375eSIan Kent { 86662a7375eSIan Kent return (dentry->d_flags & DCACHE_MANAGE_TRANSIT && 86762a7375eSIan Kent dentry->d_op->d_manage(dentry, true) < 0); 86862a7375eSIan Kent } 86962a7375eSIan Kent 8709875cf80SDavid Howells /* 871287548e4SAl Viro * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if 872287548e4SAl Viro * we meet a managed dentry that would need blocking. 8739875cf80SDavid Howells */ 8749875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 875287548e4SAl Viro struct inode **inode) 8769875cf80SDavid Howells { 87762a7375eSIan Kent for (;;) { 8789875cf80SDavid Howells struct vfsmount *mounted; 87962a7375eSIan Kent /* 88062a7375eSIan Kent * Don't forget we might have a non-mountpoint managed dentry 88162a7375eSIan Kent * that wants to block transit. 88262a7375eSIan Kent */ 883287548e4SAl Viro if (unlikely(managed_dentry_might_block(path->dentry))) 884ab90911fSDavid Howells return false; 88562a7375eSIan Kent 88662a7375eSIan Kent if (!d_mountpoint(path->dentry)) 88762a7375eSIan Kent break; 88862a7375eSIan Kent 8899875cf80SDavid Howells mounted = __lookup_mnt(path->mnt, path->dentry, 1); 8909875cf80SDavid Howells if (!mounted) 8919875cf80SDavid Howells break; 8929875cf80SDavid Howells path->mnt = mounted; 8939875cf80SDavid Howells path->dentry = mounted->mnt_root; 8949875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 89559430262SLinus Torvalds /* 89659430262SLinus Torvalds * Update the inode too. We don't need to re-check the 89759430262SLinus Torvalds * dentry sequence number here after this d_inode read, 89859430262SLinus Torvalds * because a mount-point is always pinned. 89959430262SLinus Torvalds */ 90059430262SLinus Torvalds *inode = path->dentry->d_inode; 9019875cf80SDavid Howells } 9029875cf80SDavid Howells return true; 9039875cf80SDavid Howells } 9049875cf80SDavid Howells 905dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd) 906287548e4SAl Viro { 907dea39376SAl Viro while (d_mountpoint(nd->path.dentry)) { 908287548e4SAl Viro struct vfsmount *mounted; 909dea39376SAl Viro mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1); 910287548e4SAl Viro if (!mounted) 911287548e4SAl Viro break; 912dea39376SAl Viro nd->path.mnt = mounted; 913dea39376SAl Viro nd->path.dentry = mounted->mnt_root; 914dea39376SAl Viro nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 915287548e4SAl Viro } 916287548e4SAl Viro } 917287548e4SAl Viro 91831e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 91931e6b01fSNick Piggin { 92031e6b01fSNick Piggin set_root_rcu(nd); 92131e6b01fSNick Piggin 92231e6b01fSNick Piggin while (1) { 92331e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 92431e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 92531e6b01fSNick Piggin break; 92631e6b01fSNick Piggin } 92731e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 92831e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 92931e6b01fSNick Piggin struct dentry *parent = old->d_parent; 93031e6b01fSNick Piggin unsigned seq; 93131e6b01fSNick Piggin 93231e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 93331e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 934ef7562d5SAl Viro goto failed; 93531e6b01fSNick Piggin nd->path.dentry = parent; 93631e6b01fSNick Piggin nd->seq = seq; 93731e6b01fSNick Piggin break; 93831e6b01fSNick Piggin } 93931e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 94031e6b01fSNick Piggin break; 94131e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 94231e6b01fSNick Piggin } 943dea39376SAl Viro follow_mount_rcu(nd); 944dea39376SAl Viro nd->inode = nd->path.dentry->d_inode; 94531e6b01fSNick Piggin return 0; 946ef7562d5SAl Viro 947ef7562d5SAl Viro failed: 948ef7562d5SAl Viro nd->flags &= ~LOOKUP_RCU; 9495b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 950ef7562d5SAl Viro nd->root.mnt = NULL; 951ef7562d5SAl Viro rcu_read_unlock(); 952ef7562d5SAl Viro br_read_unlock(vfsmount_lock); 953ef7562d5SAl Viro return -ECHILD; 95431e6b01fSNick Piggin } 95531e6b01fSNick Piggin 9569875cf80SDavid Howells /* 957cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 958cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 959cc53ce53SDavid Howells * caller is permitted to proceed or not. 960cc53ce53SDavid Howells */ 9617cc90cc3SAl Viro int follow_down(struct path *path) 962cc53ce53SDavid Howells { 963cc53ce53SDavid Howells unsigned managed; 964cc53ce53SDavid Howells int ret; 965cc53ce53SDavid Howells 966cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 967cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 968cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 969cc53ce53SDavid Howells * being held. 970cc53ce53SDavid Howells * 971cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 972cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 973cc53ce53SDavid Howells * other than its daemon the right to mount on its 974cc53ce53SDavid Howells * superstructure. 975cc53ce53SDavid Howells * 976cc53ce53SDavid Howells * The filesystem may sleep at this point. 977cc53ce53SDavid Howells */ 978cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 979cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 980cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 981ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 9821aed3e42SAl Viro path->dentry, false); 983cc53ce53SDavid Howells if (ret < 0) 984cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 985cc53ce53SDavid Howells } 986cc53ce53SDavid Howells 987cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 988cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 989cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 990cc53ce53SDavid Howells if (!mounted) 991cc53ce53SDavid Howells break; 992cc53ce53SDavid Howells dput(path->dentry); 993cc53ce53SDavid Howells mntput(path->mnt); 994cc53ce53SDavid Howells path->mnt = mounted; 995cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 996cc53ce53SDavid Howells continue; 997cc53ce53SDavid Howells } 998cc53ce53SDavid Howells 999cc53ce53SDavid Howells /* Don't handle automount points here */ 1000cc53ce53SDavid Howells break; 1001cc53ce53SDavid Howells } 1002cc53ce53SDavid Howells return 0; 1003cc53ce53SDavid Howells } 1004cc53ce53SDavid Howells 1005cc53ce53SDavid Howells /* 10069875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 10079875cf80SDavid Howells */ 10089875cf80SDavid Howells static void follow_mount(struct path *path) 10099875cf80SDavid Howells { 10109875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 10119875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 10129875cf80SDavid Howells if (!mounted) 10139875cf80SDavid Howells break; 10149875cf80SDavid Howells dput(path->dentry); 10159875cf80SDavid Howells mntput(path->mnt); 10169875cf80SDavid Howells path->mnt = mounted; 10179875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 10189875cf80SDavid Howells } 10199875cf80SDavid Howells } 10209875cf80SDavid Howells 102131e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 10221da177e4SLinus Torvalds { 10232a737871SAl Viro set_root(nd); 1024e518ddb7SAndreas Mohr 10251da177e4SLinus Torvalds while(1) { 10264ac91378SJan Blunck struct dentry *old = nd->path.dentry; 10271da177e4SLinus Torvalds 10282a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 10292a737871SAl Viro nd->path.mnt == nd->root.mnt) { 10301da177e4SLinus Torvalds break; 10311da177e4SLinus Torvalds } 10324ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 10333088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 10343088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 10351da177e4SLinus Torvalds dput(old); 10361da177e4SLinus Torvalds break; 10371da177e4SLinus Torvalds } 10383088dd70SAl Viro if (!follow_up(&nd->path)) 10391da177e4SLinus Torvalds break; 10401da177e4SLinus Torvalds } 104179ed0226SAl Viro follow_mount(&nd->path); 104231e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 10431da177e4SLinus Torvalds } 10441da177e4SLinus Torvalds 10451da177e4SLinus Torvalds /* 1046baa03890SNick Piggin * Allocate a dentry with name and parent, and perform a parent 1047baa03890SNick Piggin * directory ->lookup on it. Returns the new dentry, or ERR_PTR 1048baa03890SNick Piggin * on error. parent->d_inode->i_mutex must be held. d_lookup must 1049baa03890SNick Piggin * have verified that no child exists while under i_mutex. 1050baa03890SNick Piggin */ 1051baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent, 1052baa03890SNick Piggin struct qstr *name, struct nameidata *nd) 1053baa03890SNick Piggin { 1054baa03890SNick Piggin struct inode *inode = parent->d_inode; 1055baa03890SNick Piggin struct dentry *dentry; 1056baa03890SNick Piggin struct dentry *old; 1057baa03890SNick Piggin 1058baa03890SNick Piggin /* Don't create child dentry for a dead directory. */ 1059baa03890SNick Piggin if (unlikely(IS_DEADDIR(inode))) 1060baa03890SNick Piggin return ERR_PTR(-ENOENT); 1061baa03890SNick Piggin 1062baa03890SNick Piggin dentry = d_alloc(parent, name); 1063baa03890SNick Piggin if (unlikely(!dentry)) 1064baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1065baa03890SNick Piggin 1066baa03890SNick Piggin old = inode->i_op->lookup(inode, dentry, nd); 1067baa03890SNick Piggin if (unlikely(old)) { 1068baa03890SNick Piggin dput(dentry); 1069baa03890SNick Piggin dentry = old; 1070baa03890SNick Piggin } 1071baa03890SNick Piggin return dentry; 1072baa03890SNick Piggin } 1073baa03890SNick Piggin 1074baa03890SNick Piggin /* 107544396f4bSJosef Bacik * We already have a dentry, but require a lookup to be performed on the parent 107644396f4bSJosef Bacik * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error. 107744396f4bSJosef Bacik * parent->d_inode->i_mutex must be held. d_lookup must have verified that no 107844396f4bSJosef Bacik * child exists while under i_mutex. 107944396f4bSJosef Bacik */ 108044396f4bSJosef Bacik static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry, 108144396f4bSJosef Bacik struct nameidata *nd) 108244396f4bSJosef Bacik { 108344396f4bSJosef Bacik struct inode *inode = parent->d_inode; 108444396f4bSJosef Bacik struct dentry *old; 108544396f4bSJosef Bacik 108644396f4bSJosef Bacik /* Don't create child dentry for a dead directory. */ 108744396f4bSJosef Bacik if (unlikely(IS_DEADDIR(inode))) 108844396f4bSJosef Bacik return ERR_PTR(-ENOENT); 108944396f4bSJosef Bacik 109044396f4bSJosef Bacik old = inode->i_op->lookup(inode, dentry, nd); 109144396f4bSJosef Bacik if (unlikely(old)) { 109244396f4bSJosef Bacik dput(dentry); 109344396f4bSJosef Bacik dentry = old; 109444396f4bSJosef Bacik } 109544396f4bSJosef Bacik return dentry; 109644396f4bSJosef Bacik } 109744396f4bSJosef Bacik 109844396f4bSJosef Bacik /* 10991da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 11001da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 11011da177e4SLinus Torvalds * It _is_ time-critical. 11021da177e4SLinus Torvalds */ 11031da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name, 110431e6b01fSNick Piggin struct path *path, struct inode **inode) 11051da177e4SLinus Torvalds { 11064ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 110731e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 11085a18fff2SAl Viro int need_reval = 1; 11095a18fff2SAl Viro int status = 1; 11109875cf80SDavid Howells int err; 11119875cf80SDavid Howells 11123cac260aSAl Viro /* 1113b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1114b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1115b04f784eSNick Piggin * do the non-racy lookup, below. 1116b04f784eSNick Piggin */ 111731e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 111831e6b01fSNick Piggin unsigned seq; 111931e6b01fSNick Piggin *inode = nd->inode; 112031e6b01fSNick Piggin dentry = __d_lookup_rcu(parent, name, &seq, inode); 11215a18fff2SAl Viro if (!dentry) 11225a18fff2SAl Viro goto unlazy; 11235a18fff2SAl Viro 112431e6b01fSNick Piggin /* Memory barrier in read_seqcount_begin of child is enough */ 112531e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 112631e6b01fSNick Piggin return -ECHILD; 112731e6b01fSNick Piggin nd->seq = seq; 11285a18fff2SAl Viro 112924643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 11305a18fff2SAl Viro status = d_revalidate(dentry, nd); 11315a18fff2SAl Viro if (unlikely(status <= 0)) { 11325a18fff2SAl Viro if (status != -ECHILD) 11335a18fff2SAl Viro need_reval = 0; 11345a18fff2SAl Viro goto unlazy; 11355a18fff2SAl Viro } 113624643087SAl Viro } 113744396f4bSJosef Bacik if (unlikely(d_need_lookup(dentry))) 113844396f4bSJosef Bacik goto unlazy; 113931e6b01fSNick Piggin path->mnt = mnt; 114031e6b01fSNick Piggin path->dentry = dentry; 1141d6e9bd25SAl Viro if (unlikely(!__follow_mount_rcu(nd, path, inode))) 1142d6e9bd25SAl Viro goto unlazy; 1143d6e9bd25SAl Viro if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 1144d6e9bd25SAl Viro goto unlazy; 11459875cf80SDavid Howells return 0; 11465a18fff2SAl Viro unlazy: 114719660af7SAl Viro if (unlazy_walk(nd, dentry)) 11485a18fff2SAl Viro return -ECHILD; 11495a18fff2SAl Viro } else { 115031e6b01fSNick Piggin dentry = __d_lookup(parent, name); 115124643087SAl Viro } 11525a18fff2SAl Viro 115344396f4bSJosef Bacik if (dentry && unlikely(d_need_lookup(dentry))) { 115444396f4bSJosef Bacik dput(dentry); 115544396f4bSJosef Bacik dentry = NULL; 115644396f4bSJosef Bacik } 11575a18fff2SAl Viro retry: 11585a18fff2SAl Viro if (unlikely(!dentry)) { 11595a18fff2SAl Viro struct inode *dir = parent->d_inode; 11605a18fff2SAl Viro BUG_ON(nd->inode != dir); 11615a18fff2SAl Viro 11625a18fff2SAl Viro mutex_lock(&dir->i_mutex); 11635a18fff2SAl Viro dentry = d_lookup(parent, name); 11645a18fff2SAl Viro if (likely(!dentry)) { 11655a18fff2SAl Viro dentry = d_alloc_and_lookup(parent, name, nd); 11665a18fff2SAl Viro if (IS_ERR(dentry)) { 11675a18fff2SAl Viro mutex_unlock(&dir->i_mutex); 11685a18fff2SAl Viro return PTR_ERR(dentry); 11695a18fff2SAl Viro } 11705a18fff2SAl Viro /* known good */ 11715a18fff2SAl Viro need_reval = 0; 11725a18fff2SAl Viro status = 1; 117344396f4bSJosef Bacik } else if (unlikely(d_need_lookup(dentry))) { 117444396f4bSJosef Bacik dentry = d_inode_lookup(parent, dentry, nd); 117544396f4bSJosef Bacik if (IS_ERR(dentry)) { 117644396f4bSJosef Bacik mutex_unlock(&dir->i_mutex); 117744396f4bSJosef Bacik return PTR_ERR(dentry); 117844396f4bSJosef Bacik } 117944396f4bSJosef Bacik /* known good */ 118044396f4bSJosef Bacik need_reval = 0; 118144396f4bSJosef Bacik status = 1; 11825a18fff2SAl Viro } 11835a18fff2SAl Viro mutex_unlock(&dir->i_mutex); 11845a18fff2SAl Viro } 11855a18fff2SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) 11865a18fff2SAl Viro status = d_revalidate(dentry, nd); 11875a18fff2SAl Viro if (unlikely(status <= 0)) { 11885a18fff2SAl Viro if (status < 0) { 11895a18fff2SAl Viro dput(dentry); 11905a18fff2SAl Viro return status; 11915a18fff2SAl Viro } 11925a18fff2SAl Viro if (!d_invalidate(dentry)) { 11935a18fff2SAl Viro dput(dentry); 11945a18fff2SAl Viro dentry = NULL; 11955a18fff2SAl Viro need_reval = 1; 11965a18fff2SAl Viro goto retry; 11975a18fff2SAl Viro } 11985a18fff2SAl Viro } 11995a18fff2SAl Viro 12001da177e4SLinus Torvalds path->mnt = mnt; 12011da177e4SLinus Torvalds path->dentry = dentry; 12029875cf80SDavid Howells err = follow_managed(path, nd->flags); 120389312214SIan Kent if (unlikely(err < 0)) { 120489312214SIan Kent path_put_conditional(path, nd); 12059875cf80SDavid Howells return err; 120689312214SIan Kent } 120731e6b01fSNick Piggin *inode = path->dentry->d_inode; 12081da177e4SLinus Torvalds return 0; 12091da177e4SLinus Torvalds } 12101da177e4SLinus Torvalds 121152094c8aSAl Viro static inline int may_lookup(struct nameidata *nd) 121252094c8aSAl Viro { 121352094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 12144ad5abb3SAl Viro int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); 121552094c8aSAl Viro if (err != -ECHILD) 121652094c8aSAl Viro return err; 121719660af7SAl Viro if (unlazy_walk(nd, NULL)) 121852094c8aSAl Viro return -ECHILD; 121952094c8aSAl Viro } 12204ad5abb3SAl Viro return inode_permission(nd->inode, MAY_EXEC); 122152094c8aSAl Viro } 122252094c8aSAl Viro 12239856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type) 12249856fa1bSAl Viro { 12259856fa1bSAl Viro if (type == LAST_DOTDOT) { 12269856fa1bSAl Viro if (nd->flags & LOOKUP_RCU) { 12279856fa1bSAl Viro if (follow_dotdot_rcu(nd)) 12289856fa1bSAl Viro return -ECHILD; 12299856fa1bSAl Viro } else 12309856fa1bSAl Viro follow_dotdot(nd); 12319856fa1bSAl Viro } 12329856fa1bSAl Viro return 0; 12339856fa1bSAl Viro } 12349856fa1bSAl Viro 1235951361f9SAl Viro static void terminate_walk(struct nameidata *nd) 1236951361f9SAl Viro { 1237951361f9SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1238951361f9SAl Viro path_put(&nd->path); 1239951361f9SAl Viro } else { 1240951361f9SAl Viro nd->flags &= ~LOOKUP_RCU; 12415b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1242951361f9SAl Viro nd->root.mnt = NULL; 1243951361f9SAl Viro rcu_read_unlock(); 1244951361f9SAl Viro br_read_unlock(vfsmount_lock); 1245951361f9SAl Viro } 1246951361f9SAl Viro } 1247951361f9SAl Viro 1248ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path, 1249ce57dfc1SAl Viro struct qstr *name, int type, int follow) 1250ce57dfc1SAl Viro { 1251ce57dfc1SAl Viro struct inode *inode; 1252ce57dfc1SAl Viro int err; 1253ce57dfc1SAl Viro /* 1254ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1255ce57dfc1SAl Viro * to be able to know about the current root directory and 1256ce57dfc1SAl Viro * parent relationships. 1257ce57dfc1SAl Viro */ 1258ce57dfc1SAl Viro if (unlikely(type != LAST_NORM)) 1259ce57dfc1SAl Viro return handle_dots(nd, type); 1260ce57dfc1SAl Viro err = do_lookup(nd, name, path, &inode); 1261ce57dfc1SAl Viro if (unlikely(err)) { 1262ce57dfc1SAl Viro terminate_walk(nd); 1263ce57dfc1SAl Viro return err; 1264ce57dfc1SAl Viro } 1265ce57dfc1SAl Viro if (!inode) { 1266ce57dfc1SAl Viro path_to_nameidata(path, nd); 1267ce57dfc1SAl Viro terminate_walk(nd); 1268ce57dfc1SAl Viro return -ENOENT; 1269ce57dfc1SAl Viro } 1270ce57dfc1SAl Viro if (unlikely(inode->i_op->follow_link) && follow) { 127119660af7SAl Viro if (nd->flags & LOOKUP_RCU) { 127219660af7SAl Viro if (unlikely(unlazy_walk(nd, path->dentry))) { 127319660af7SAl Viro terminate_walk(nd); 1274ce57dfc1SAl Viro return -ECHILD; 127519660af7SAl Viro } 127619660af7SAl Viro } 1277ce57dfc1SAl Viro BUG_ON(inode != path->dentry->d_inode); 1278ce57dfc1SAl Viro return 1; 1279ce57dfc1SAl Viro } 1280ce57dfc1SAl Viro path_to_nameidata(path, nd); 1281ce57dfc1SAl Viro nd->inode = inode; 1282ce57dfc1SAl Viro return 0; 1283ce57dfc1SAl Viro } 1284ce57dfc1SAl Viro 12851da177e4SLinus Torvalds /* 1286b356379aSAl Viro * This limits recursive symlink follows to 8, while 1287b356379aSAl Viro * limiting consecutive symlinks to 40. 1288b356379aSAl Viro * 1289b356379aSAl Viro * Without that kind of total limit, nasty chains of consecutive 1290b356379aSAl Viro * symlinks can cause almost arbitrarily long lookups. 1291b356379aSAl Viro */ 1292b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd) 1293b356379aSAl Viro { 1294b356379aSAl Viro int res; 1295b356379aSAl Viro 1296b356379aSAl Viro if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { 1297b356379aSAl Viro path_put_conditional(path, nd); 1298b356379aSAl Viro path_put(&nd->path); 1299b356379aSAl Viro return -ELOOP; 1300b356379aSAl Viro } 13011a4022f8SErez Zadok BUG_ON(nd->depth >= MAX_NESTED_LINKS); 1302b356379aSAl Viro 1303b356379aSAl Viro nd->depth++; 1304b356379aSAl Viro current->link_count++; 1305b356379aSAl Viro 1306b356379aSAl Viro do { 1307b356379aSAl Viro struct path link = *path; 1308b356379aSAl Viro void *cookie; 1309574197e0SAl Viro 1310574197e0SAl Viro res = follow_link(&link, nd, &cookie); 1311b356379aSAl Viro if (!res) 1312b356379aSAl Viro res = walk_component(nd, path, &nd->last, 1313b356379aSAl Viro nd->last_type, LOOKUP_FOLLOW); 1314574197e0SAl Viro put_link(nd, &link, cookie); 1315b356379aSAl Viro } while (res > 0); 1316b356379aSAl Viro 1317b356379aSAl Viro current->link_count--; 1318b356379aSAl Viro nd->depth--; 1319b356379aSAl Viro return res; 1320b356379aSAl Viro } 1321b356379aSAl Viro 1322b356379aSAl Viro /* 13231da177e4SLinus Torvalds * Name resolution. 1324ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1325ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 13261da177e4SLinus Torvalds * 1327ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1328ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 13291da177e4SLinus Torvalds */ 13306de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 13311da177e4SLinus Torvalds { 13321da177e4SLinus Torvalds struct path next; 13331da177e4SLinus Torvalds int err; 13341da177e4SLinus Torvalds 13351da177e4SLinus Torvalds while (*name=='/') 13361da177e4SLinus Torvalds name++; 13371da177e4SLinus Torvalds if (!*name) 1338086e183aSAl Viro return 0; 13391da177e4SLinus Torvalds 13401da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 13411da177e4SLinus Torvalds for(;;) { 13421da177e4SLinus Torvalds unsigned long hash; 13431da177e4SLinus Torvalds struct qstr this; 13441da177e4SLinus Torvalds unsigned int c; 1345fe479a58SAl Viro int type; 13461da177e4SLinus Torvalds 134752094c8aSAl Viro err = may_lookup(nd); 13481da177e4SLinus Torvalds if (err) 13491da177e4SLinus Torvalds break; 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds this.name = name; 13521da177e4SLinus Torvalds c = *(const unsigned char *)name; 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds hash = init_name_hash(); 13551da177e4SLinus Torvalds do { 13561da177e4SLinus Torvalds name++; 13571da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 13581da177e4SLinus Torvalds c = *(const unsigned char *)name; 13591da177e4SLinus Torvalds } while (c && (c != '/')); 13601da177e4SLinus Torvalds this.len = name - (const char *) this.name; 13611da177e4SLinus Torvalds this.hash = end_name_hash(hash); 13621da177e4SLinus Torvalds 1363fe479a58SAl Viro type = LAST_NORM; 1364fe479a58SAl Viro if (this.name[0] == '.') switch (this.len) { 1365fe479a58SAl Viro case 2: 136616c2cd71SAl Viro if (this.name[1] == '.') { 1367fe479a58SAl Viro type = LAST_DOTDOT; 136816c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 136916c2cd71SAl Viro } 1370fe479a58SAl Viro break; 1371fe479a58SAl Viro case 1: 1372fe479a58SAl Viro type = LAST_DOT; 1373fe479a58SAl Viro } 13745a202bcdSAl Viro if (likely(type == LAST_NORM)) { 13755a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 137616c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 13775a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 13785a202bcdSAl Viro err = parent->d_op->d_hash(parent, nd->inode, 13795a202bcdSAl Viro &this); 13805a202bcdSAl Viro if (err < 0) 13815a202bcdSAl Viro break; 13825a202bcdSAl Viro } 13835a202bcdSAl Viro } 1384fe479a58SAl Viro 13851da177e4SLinus Torvalds /* remove trailing slashes? */ 13861da177e4SLinus Torvalds if (!c) 13871da177e4SLinus Torvalds goto last_component; 13881da177e4SLinus Torvalds while (*++name == '/'); 13891da177e4SLinus Torvalds if (!*name) 1390b356379aSAl Viro goto last_component; 13911da177e4SLinus Torvalds 1392ce57dfc1SAl Viro err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW); 1393ce57dfc1SAl Viro if (err < 0) 1394ce57dfc1SAl Viro return err; 1395fe479a58SAl Viro 1396ce57dfc1SAl Viro if (err) { 1397b356379aSAl Viro err = nested_symlink(&next, nd); 13981da177e4SLinus Torvalds if (err) 1399a7472babSAl Viro return err; 140031e6b01fSNick Piggin } 14011da177e4SLinus Torvalds err = -ENOTDIR; 140231e6b01fSNick Piggin if (!nd->inode->i_op->lookup) 14031da177e4SLinus Torvalds break; 14041da177e4SLinus Torvalds continue; 14051da177e4SLinus Torvalds /* here ends the main loop */ 14061da177e4SLinus Torvalds 14071da177e4SLinus Torvalds last_component: 1408ce57dfc1SAl Viro nd->last = this; 1409ce57dfc1SAl Viro nd->last_type = type; 1410ce57dfc1SAl Viro return 0; 1411ce57dfc1SAl Viro } 1412951361f9SAl Viro terminate_walk(nd); 14131da177e4SLinus Torvalds return err; 14141da177e4SLinus Torvalds } 14151da177e4SLinus Torvalds 141670e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, 141770e9b357SAl Viro struct nameidata *nd, struct file **fp) 141831e6b01fSNick Piggin { 141931e6b01fSNick Piggin int retval = 0; 142031e6b01fSNick Piggin int fput_needed; 142131e6b01fSNick Piggin struct file *file; 142231e6b01fSNick Piggin 142331e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 142416c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 142531e6b01fSNick Piggin nd->depth = 0; 14265b6ca027SAl Viro if (flags & LOOKUP_ROOT) { 14275b6ca027SAl Viro struct inode *inode = nd->root.dentry->d_inode; 142873d049a4SAl Viro if (*name) { 14295b6ca027SAl Viro if (!inode->i_op->lookup) 14305b6ca027SAl Viro return -ENOTDIR; 14315b6ca027SAl Viro retval = inode_permission(inode, MAY_EXEC); 14325b6ca027SAl Viro if (retval) 14335b6ca027SAl Viro return retval; 143473d049a4SAl Viro } 14355b6ca027SAl Viro nd->path = nd->root; 14365b6ca027SAl Viro nd->inode = inode; 14375b6ca027SAl Viro if (flags & LOOKUP_RCU) { 14385b6ca027SAl Viro br_read_lock(vfsmount_lock); 14395b6ca027SAl Viro rcu_read_lock(); 14405b6ca027SAl Viro nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 14415b6ca027SAl Viro } else { 14425b6ca027SAl Viro path_get(&nd->path); 14435b6ca027SAl Viro } 14445b6ca027SAl Viro return 0; 14455b6ca027SAl Viro } 14465b6ca027SAl Viro 144731e6b01fSNick Piggin nd->root.mnt = NULL; 144831e6b01fSNick Piggin 144931e6b01fSNick Piggin if (*name=='/') { 1450e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 145131e6b01fSNick Piggin br_read_lock(vfsmount_lock); 145231e6b01fSNick Piggin rcu_read_lock(); 1453e41f7d4eSAl Viro set_root_rcu(nd); 1454e41f7d4eSAl Viro } else { 1455e41f7d4eSAl Viro set_root(nd); 1456e41f7d4eSAl Viro path_get(&nd->root); 1457e41f7d4eSAl Viro } 145831e6b01fSNick Piggin nd->path = nd->root; 145931e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 1460e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 146131e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1462c28cc364SNick Piggin unsigned seq; 146331e6b01fSNick Piggin 146431e6b01fSNick Piggin br_read_lock(vfsmount_lock); 146531e6b01fSNick Piggin rcu_read_lock(); 146631e6b01fSNick Piggin 1467c28cc364SNick Piggin do { 1468c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 146931e6b01fSNick Piggin nd->path = fs->pwd; 1470c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1471c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1472e41f7d4eSAl Viro } else { 1473e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 1474e41f7d4eSAl Viro } 147531e6b01fSNick Piggin } else { 147631e6b01fSNick Piggin struct dentry *dentry; 147731e6b01fSNick Piggin 14781abf0c71SAl Viro file = fget_raw_light(dfd, &fput_needed); 147931e6b01fSNick Piggin retval = -EBADF; 148031e6b01fSNick Piggin if (!file) 148131e6b01fSNick Piggin goto out_fail; 148231e6b01fSNick Piggin 148331e6b01fSNick Piggin dentry = file->f_path.dentry; 148431e6b01fSNick Piggin 1485f52e0c11SAl Viro if (*name) { 148631e6b01fSNick Piggin retval = -ENOTDIR; 148731e6b01fSNick Piggin if (!S_ISDIR(dentry->d_inode->i_mode)) 148831e6b01fSNick Piggin goto fput_fail; 148931e6b01fSNick Piggin 14904ad5abb3SAl Viro retval = inode_permission(dentry->d_inode, MAY_EXEC); 149131e6b01fSNick Piggin if (retval) 149231e6b01fSNick Piggin goto fput_fail; 1493f52e0c11SAl Viro } 149431e6b01fSNick Piggin 149531e6b01fSNick Piggin nd->path = file->f_path; 1496e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 149731e6b01fSNick Piggin if (fput_needed) 149870e9b357SAl Viro *fp = file; 1499c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 150031e6b01fSNick Piggin br_read_lock(vfsmount_lock); 150131e6b01fSNick Piggin rcu_read_lock(); 15025590ff0dSUlrich Drepper } else { 15035dd784d0SJan Blunck path_get(&file->f_path); 15045590ff0dSUlrich Drepper fput_light(file, fput_needed); 15051da177e4SLinus Torvalds } 1506e41f7d4eSAl Viro } 1507e41f7d4eSAl Viro 150831e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 15099b4a9b14SAl Viro return 0; 15102dfdd266SJosef 'Jeff' Sipek 15119b4a9b14SAl Viro fput_fail: 15129b4a9b14SAl Viro fput_light(file, fput_needed); 15139b4a9b14SAl Viro out_fail: 15149b4a9b14SAl Viro return retval; 15159b4a9b14SAl Viro } 15169b4a9b14SAl Viro 1517bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path) 1518bd92d7feSAl Viro { 1519bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 1520bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 1521bd92d7feSAl Viro 1522bd92d7feSAl Viro nd->flags &= ~LOOKUP_PARENT; 1523bd92d7feSAl Viro return walk_component(nd, path, &nd->last, nd->last_type, 1524bd92d7feSAl Viro nd->flags & LOOKUP_FOLLOW); 1525bd92d7feSAl Viro } 1526bd92d7feSAl Viro 15279b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1528ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name, 15299b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 15309b4a9b14SAl Viro { 153170e9b357SAl Viro struct file *base = NULL; 1532bd92d7feSAl Viro struct path path; 1533bd92d7feSAl Viro int err; 153431e6b01fSNick Piggin 153531e6b01fSNick Piggin /* 153631e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 153731e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 153831e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 153931e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 154031e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 154131e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 154231e6b01fSNick Piggin * analogue, foo_rcu(). 154331e6b01fSNick Piggin * 154431e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 154531e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 154631e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 154731e6b01fSNick Piggin * be able to complete). 154831e6b01fSNick Piggin */ 1549bd92d7feSAl Viro err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); 1550ee0827cdSAl Viro 1551bd92d7feSAl Viro if (unlikely(err)) 1552bd92d7feSAl Viro return err; 1553ee0827cdSAl Viro 1554ee0827cdSAl Viro current->total_link_count = 0; 1555bd92d7feSAl Viro err = link_path_walk(name, nd); 1556bd92d7feSAl Viro 1557bd92d7feSAl Viro if (!err && !(flags & LOOKUP_PARENT)) { 1558bd92d7feSAl Viro err = lookup_last(nd, &path); 1559bd92d7feSAl Viro while (err > 0) { 1560bd92d7feSAl Viro void *cookie; 1561bd92d7feSAl Viro struct path link = path; 1562bd92d7feSAl Viro nd->flags |= LOOKUP_PARENT; 1563574197e0SAl Viro err = follow_link(&link, nd, &cookie); 1564bd92d7feSAl Viro if (!err) 1565bd92d7feSAl Viro err = lookup_last(nd, &path); 1566574197e0SAl Viro put_link(nd, &link, cookie); 1567bd92d7feSAl Viro } 1568bd92d7feSAl Viro } 1569ee0827cdSAl Viro 15709f1fafeeSAl Viro if (!err) 15719f1fafeeSAl Viro err = complete_walk(nd); 1572bd92d7feSAl Viro 1573bd92d7feSAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) { 1574bd92d7feSAl Viro if (!nd->inode->i_op->lookup) { 1575bd92d7feSAl Viro path_put(&nd->path); 1576bd23a539SAl Viro err = -ENOTDIR; 1577bd92d7feSAl Viro } 1578bd92d7feSAl Viro } 157916c2cd71SAl Viro 158070e9b357SAl Viro if (base) 158170e9b357SAl Viro fput(base); 1582ee0827cdSAl Viro 15835b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 158431e6b01fSNick Piggin path_put(&nd->root); 158531e6b01fSNick Piggin nd->root.mnt = NULL; 158631e6b01fSNick Piggin } 1587bd92d7feSAl Viro return err; 158831e6b01fSNick Piggin } 158931e6b01fSNick Piggin 1590ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name, 1591ee0827cdSAl Viro unsigned int flags, struct nameidata *nd) 1592ee0827cdSAl Viro { 1593ee0827cdSAl Viro int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd); 1594ee0827cdSAl Viro if (unlikely(retval == -ECHILD)) 1595ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags, nd); 1596ee0827cdSAl Viro if (unlikely(retval == -ESTALE)) 1597ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd); 1598ee0827cdSAl Viro 159931e6b01fSNick Piggin if (likely(!retval)) { 160031e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) { 160131e6b01fSNick Piggin if (nd->path.dentry && nd->inode) 160231e6b01fSNick Piggin audit_inode(name, nd->path.dentry); 160331e6b01fSNick Piggin } 160431e6b01fSNick Piggin } 1605170aa3d0SUlrich Drepper return retval; 16061da177e4SLinus Torvalds } 16071da177e4SLinus Torvalds 1608c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd) 16095590ff0dSUlrich Drepper { 1610c9c6cac0SAl Viro return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd); 16115590ff0dSUlrich Drepper } 16125590ff0dSUlrich Drepper 1613d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1614d1811465SAl Viro { 1615d1811465SAl Viro struct nameidata nd; 1616d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1617d1811465SAl Viro if (!res) 1618d1811465SAl Viro *path = nd.path; 1619d1811465SAl Viro return res; 1620d1811465SAl Viro } 1621d1811465SAl Viro 162216f18200SJosef 'Jeff' Sipek /** 162316f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 162416f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 162516f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 162616f18200SJosef 'Jeff' Sipek * @name: pointer to file name 162716f18200SJosef 'Jeff' Sipek * @flags: lookup flags 1628e0a01249SAl Viro * @path: pointer to struct path to fill 162916f18200SJosef 'Jeff' Sipek */ 163016f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 163116f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 1632e0a01249SAl Viro struct path *path) 163316f18200SJosef 'Jeff' Sipek { 1634e0a01249SAl Viro struct nameidata nd; 1635e0a01249SAl Viro int err; 1636e0a01249SAl Viro nd.root.dentry = dentry; 1637e0a01249SAl Viro nd.root.mnt = mnt; 1638e0a01249SAl Viro BUG_ON(flags & LOOKUP_PARENT); 16395b6ca027SAl Viro /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ 1640e0a01249SAl Viro err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd); 1641e0a01249SAl Viro if (!err) 1642e0a01249SAl Viro *path = nd.path; 1643e0a01249SAl Viro return err; 164416f18200SJosef 'Jeff' Sipek } 164516f18200SJosef 'Jeff' Sipek 1646eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name, 1647eead1911SChristoph Hellwig struct dentry *base, struct nameidata *nd) 16481da177e4SLinus Torvalds { 164981fca444SChristoph Hellwig struct inode *inode = base->d_inode; 16501da177e4SLinus Torvalds struct dentry *dentry; 16511da177e4SLinus Torvalds int err; 16521da177e4SLinus Torvalds 16534ad5abb3SAl Viro err = inode_permission(inode, MAY_EXEC); 165481fca444SChristoph Hellwig if (err) 165581fca444SChristoph Hellwig return ERR_PTR(err); 16561da177e4SLinus Torvalds 16571da177e4SLinus Torvalds /* 1658b04f784eSNick Piggin * Don't bother with __d_lookup: callers are for creat as 1659b04f784eSNick Piggin * well as unlink, so a lot of the time it would cost 1660b04f784eSNick Piggin * a double lookup. 16616e6b1bd1SAl Viro */ 16626e6b1bd1SAl Viro dentry = d_lookup(base, name); 16636e6b1bd1SAl Viro 166444396f4bSJosef Bacik if (dentry && d_need_lookup(dentry)) { 166544396f4bSJosef Bacik /* 166644396f4bSJosef Bacik * __lookup_hash is called with the parent dir's i_mutex already 166744396f4bSJosef Bacik * held, so we are good to go here. 166844396f4bSJosef Bacik */ 166944396f4bSJosef Bacik dentry = d_inode_lookup(base, dentry, nd); 167044396f4bSJosef Bacik if (IS_ERR(dentry)) 167144396f4bSJosef Bacik return dentry; 167244396f4bSJosef Bacik } 167344396f4bSJosef Bacik 1674d2d9e9fbSAl Viro if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) { 1675d2d9e9fbSAl Viro int status = d_revalidate(dentry, nd); 1676d2d9e9fbSAl Viro if (unlikely(status <= 0)) { 1677d2d9e9fbSAl Viro /* 1678d2d9e9fbSAl Viro * The dentry failed validation. 1679d2d9e9fbSAl Viro * If d_revalidate returned 0 attempt to invalidate 1680d2d9e9fbSAl Viro * the dentry otherwise d_revalidate is asking us 1681d2d9e9fbSAl Viro * to return a fail status. 1682d2d9e9fbSAl Viro */ 1683d2d9e9fbSAl Viro if (status < 0) { 1684d2d9e9fbSAl Viro dput(dentry); 1685d2d9e9fbSAl Viro return ERR_PTR(status); 1686d2d9e9fbSAl Viro } else if (!d_invalidate(dentry)) { 1687d2d9e9fbSAl Viro dput(dentry); 1688d2d9e9fbSAl Viro dentry = NULL; 1689d2d9e9fbSAl Viro } 1690d2d9e9fbSAl Viro } 1691d2d9e9fbSAl Viro } 16926e6b1bd1SAl Viro 16931da177e4SLinus Torvalds if (!dentry) 1694baa03890SNick Piggin dentry = d_alloc_and_lookup(base, name, nd); 16955a202bcdSAl Viro 16961da177e4SLinus Torvalds return dentry; 16971da177e4SLinus Torvalds } 16981da177e4SLinus Torvalds 1699057f6c01SJames Morris /* 1700057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1701057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1702057f6c01SJames Morris * SMP-safe. 1703057f6c01SJames Morris */ 1704a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 17051da177e4SLinus Torvalds { 17064ac91378SJan Blunck return __lookup_hash(&nd->last, nd->path.dentry, nd); 17071da177e4SLinus Torvalds } 17081da177e4SLinus Torvalds 1709eead1911SChristoph Hellwig /** 1710a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1711eead1911SChristoph Hellwig * @name: pathname component to lookup 1712eead1911SChristoph Hellwig * @base: base directory to lookup from 1713eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1714eead1911SChristoph Hellwig * 1715a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1716a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1717eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1718eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1719eead1911SChristoph Hellwig */ 1720057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1721057f6c01SJames Morris { 1722057f6c01SJames Morris struct qstr this; 17236a96ba54SAl Viro unsigned long hash; 17246a96ba54SAl Viro unsigned int c; 1725057f6c01SJames Morris 17262f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 17272f9092e1SDavid Woodhouse 17286a96ba54SAl Viro this.name = name; 17296a96ba54SAl Viro this.len = len; 17306a96ba54SAl Viro if (!len) 17316a96ba54SAl Viro return ERR_PTR(-EACCES); 17326a96ba54SAl Viro 17336a96ba54SAl Viro hash = init_name_hash(); 17346a96ba54SAl Viro while (len--) { 17356a96ba54SAl Viro c = *(const unsigned char *)name++; 17366a96ba54SAl Viro if (c == '/' || c == '\0') 17376a96ba54SAl Viro return ERR_PTR(-EACCES); 17386a96ba54SAl Viro hash = partial_name_hash(c, hash); 17396a96ba54SAl Viro } 17406a96ba54SAl Viro this.hash = end_name_hash(hash); 17415a202bcdSAl Viro /* 17425a202bcdSAl Viro * See if the low-level filesystem might want 17435a202bcdSAl Viro * to use its own hash.. 17445a202bcdSAl Viro */ 17455a202bcdSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 17465a202bcdSAl Viro int err = base->d_op->d_hash(base, base->d_inode, &this); 17475a202bcdSAl Viro if (err < 0) 17485a202bcdSAl Viro return ERR_PTR(err); 17495a202bcdSAl Viro } 1750eead1911SChristoph Hellwig 175149705b77SChristoph Hellwig return __lookup_hash(&this, base, NULL); 1752057f6c01SJames Morris } 1753057f6c01SJames Morris 17542d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags, 17552d8f3038SAl Viro struct path *path) 17561da177e4SLinus Torvalds { 17572d8f3038SAl Viro struct nameidata nd; 1758f52e0c11SAl Viro char *tmp = getname_flags(name, flags); 17591da177e4SLinus Torvalds int err = PTR_ERR(tmp); 17601da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 17612d8f3038SAl Viro 17622d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 17632d8f3038SAl Viro 17642d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 17651da177e4SLinus Torvalds putname(tmp); 17662d8f3038SAl Viro if (!err) 17672d8f3038SAl Viro *path = nd.path; 17681da177e4SLinus Torvalds } 17691da177e4SLinus Torvalds return err; 17701da177e4SLinus Torvalds } 17711da177e4SLinus Torvalds 17722ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 17732ad94ae6SAl Viro struct nameidata *nd, char **name) 17742ad94ae6SAl Viro { 17752ad94ae6SAl Viro char *s = getname(path); 17762ad94ae6SAl Viro int error; 17772ad94ae6SAl Viro 17782ad94ae6SAl Viro if (IS_ERR(s)) 17792ad94ae6SAl Viro return PTR_ERR(s); 17802ad94ae6SAl Viro 17812ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 17822ad94ae6SAl Viro if (error) 17832ad94ae6SAl Viro putname(s); 17842ad94ae6SAl Viro else 17852ad94ae6SAl Viro *name = s; 17862ad94ae6SAl Viro 17872ad94ae6SAl Viro return error; 17882ad94ae6SAl Viro } 17892ad94ae6SAl Viro 17901da177e4SLinus Torvalds /* 17911da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 17921da177e4SLinus Torvalds * minimal. 17931da177e4SLinus Torvalds */ 17941da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 17951da177e4SLinus Torvalds { 1796da9592edSDavid Howells uid_t fsuid = current_fsuid(); 1797da9592edSDavid Howells 17981da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 17991da177e4SLinus Torvalds return 0; 1800e795b717SSerge E. Hallyn if (current_user_ns() != inode_userns(inode)) 1801e795b717SSerge E. Hallyn goto other_userns; 1802da9592edSDavid Howells if (inode->i_uid == fsuid) 18031da177e4SLinus Torvalds return 0; 1804da9592edSDavid Howells if (dir->i_uid == fsuid) 18051da177e4SLinus Torvalds return 0; 1806e795b717SSerge E. Hallyn 1807e795b717SSerge E. Hallyn other_userns: 1808e795b717SSerge E. Hallyn return !ns_capable(inode_userns(inode), CAP_FOWNER); 18091da177e4SLinus Torvalds } 18101da177e4SLinus Torvalds 18111da177e4SLinus Torvalds /* 18121da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 18131da177e4SLinus Torvalds * whether the type of victim is right. 18141da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 18151da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 18161da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 18171da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 18181da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 18191da177e4SLinus Torvalds * a. be owner of dir, or 18201da177e4SLinus Torvalds * b. be owner of victim, or 18211da177e4SLinus Torvalds * c. have CAP_FOWNER capability 18221da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 18231da177e4SLinus Torvalds * links pointing to it. 18241da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 18251da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 18261da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 18271da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 18281da177e4SLinus Torvalds * nfs_async_unlink(). 18291da177e4SLinus Torvalds */ 1830858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 18311da177e4SLinus Torvalds { 18321da177e4SLinus Torvalds int error; 18331da177e4SLinus Torvalds 18341da177e4SLinus Torvalds if (!victim->d_inode) 18351da177e4SLinus Torvalds return -ENOENT; 18361da177e4SLinus Torvalds 18371da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 1838cccc6bbaSAl Viro audit_inode_child(victim, dir); 18391da177e4SLinus Torvalds 1840f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 18411da177e4SLinus Torvalds if (error) 18421da177e4SLinus Torvalds return error; 18431da177e4SLinus Torvalds if (IS_APPEND(dir)) 18441da177e4SLinus Torvalds return -EPERM; 18451da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 1846f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 18471da177e4SLinus Torvalds return -EPERM; 18481da177e4SLinus Torvalds if (isdir) { 18491da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 18501da177e4SLinus Torvalds return -ENOTDIR; 18511da177e4SLinus Torvalds if (IS_ROOT(victim)) 18521da177e4SLinus Torvalds return -EBUSY; 18531da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 18541da177e4SLinus Torvalds return -EISDIR; 18551da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 18561da177e4SLinus Torvalds return -ENOENT; 18571da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 18581da177e4SLinus Torvalds return -EBUSY; 18591da177e4SLinus Torvalds return 0; 18601da177e4SLinus Torvalds } 18611da177e4SLinus Torvalds 18621da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 18631da177e4SLinus Torvalds * dir. 18641da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 18651da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 18661da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 18671da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 18681da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 18691da177e4SLinus Torvalds */ 1870a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 18711da177e4SLinus Torvalds { 18721da177e4SLinus Torvalds if (child->d_inode) 18731da177e4SLinus Torvalds return -EEXIST; 18741da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 18751da177e4SLinus Torvalds return -ENOENT; 1876f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 18771da177e4SLinus Torvalds } 18781da177e4SLinus Torvalds 18791da177e4SLinus Torvalds /* 18801da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 18811da177e4SLinus Torvalds */ 18821da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 18831da177e4SLinus Torvalds { 18841da177e4SLinus Torvalds struct dentry *p; 18851da177e4SLinus Torvalds 18861da177e4SLinus Torvalds if (p1 == p2) { 1887f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 18881da177e4SLinus Torvalds return NULL; 18891da177e4SLinus Torvalds } 18901da177e4SLinus Torvalds 1891a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 18921da177e4SLinus Torvalds 1893e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 1894e2761a11SOGAWA Hirofumi if (p) { 1895f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 1896f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 18971da177e4SLinus Torvalds return p; 18981da177e4SLinus Torvalds } 18991da177e4SLinus Torvalds 1900e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 1901e2761a11SOGAWA Hirofumi if (p) { 1902f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1903f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 19041da177e4SLinus Torvalds return p; 19051da177e4SLinus Torvalds } 19061da177e4SLinus Torvalds 1907f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1908f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 19091da177e4SLinus Torvalds return NULL; 19101da177e4SLinus Torvalds } 19111da177e4SLinus Torvalds 19121da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 19131da177e4SLinus Torvalds { 19141b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 19151da177e4SLinus Torvalds if (p1 != p2) { 19161b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 1917a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 19181da177e4SLinus Torvalds } 19191da177e4SLinus Torvalds } 19201da177e4SLinus Torvalds 19211da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode, 19221da177e4SLinus Torvalds struct nameidata *nd) 19231da177e4SLinus Torvalds { 1924a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 19251da177e4SLinus Torvalds 19261da177e4SLinus Torvalds if (error) 19271da177e4SLinus Torvalds return error; 19281da177e4SLinus Torvalds 1929acfa4380SAl Viro if (!dir->i_op->create) 19301da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 19311da177e4SLinus Torvalds mode &= S_IALLUGO; 19321da177e4SLinus Torvalds mode |= S_IFREG; 19331da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 19341da177e4SLinus Torvalds if (error) 19351da177e4SLinus Torvalds return error; 19361da177e4SLinus Torvalds error = dir->i_op->create(dir, dentry, mode, nd); 1937a74574aaSStephen Smalley if (!error) 1938f38aa942SAmy Griffis fsnotify_create(dir, dentry); 19391da177e4SLinus Torvalds return error; 19401da177e4SLinus Torvalds } 19411da177e4SLinus Torvalds 194273d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag) 19431da177e4SLinus Torvalds { 19443fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 19451da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 19461da177e4SLinus Torvalds int error; 19471da177e4SLinus Torvalds 1948bcda7652SAl Viro /* O_PATH? */ 1949bcda7652SAl Viro if (!acc_mode) 1950bcda7652SAl Viro return 0; 1951bcda7652SAl Viro 19521da177e4SLinus Torvalds if (!inode) 19531da177e4SLinus Torvalds return -ENOENT; 19541da177e4SLinus Torvalds 1955c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 1956c8fe8f30SChristoph Hellwig case S_IFLNK: 19571da177e4SLinus Torvalds return -ELOOP; 1958c8fe8f30SChristoph Hellwig case S_IFDIR: 1959c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 19601da177e4SLinus Torvalds return -EISDIR; 1961c8fe8f30SChristoph Hellwig break; 1962c8fe8f30SChristoph Hellwig case S_IFBLK: 1963c8fe8f30SChristoph Hellwig case S_IFCHR: 19643fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 19651da177e4SLinus Torvalds return -EACCES; 1966c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 1967c8fe8f30SChristoph Hellwig case S_IFIFO: 1968c8fe8f30SChristoph Hellwig case S_IFSOCK: 19691da177e4SLinus Torvalds flag &= ~O_TRUNC; 1970c8fe8f30SChristoph Hellwig break; 19714a3fd211SDave Hansen } 1972b41572e9SDave Hansen 19733fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 1974b41572e9SDave Hansen if (error) 1975b41572e9SDave Hansen return error; 19766146f0d5SMimi Zohar 19771da177e4SLinus Torvalds /* 19781da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 19791da177e4SLinus Torvalds */ 19801da177e4SLinus Torvalds if (IS_APPEND(inode)) { 19818737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 19827715b521SAl Viro return -EPERM; 19831da177e4SLinus Torvalds if (flag & O_TRUNC) 19847715b521SAl Viro return -EPERM; 19851da177e4SLinus Torvalds } 19861da177e4SLinus Torvalds 19871da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 19882e149670SSerge E. Hallyn if (flag & O_NOATIME && !inode_owner_or_capable(inode)) 19897715b521SAl Viro return -EPERM; 19901da177e4SLinus Torvalds 19911da177e4SLinus Torvalds /* 19921da177e4SLinus Torvalds * Ensure there are no outstanding leases on the file. 19931da177e4SLinus Torvalds */ 1994b65a9cfcSAl Viro return break_lease(inode, flag); 19957715b521SAl Viro } 19967715b521SAl Viro 1997e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 19987715b521SAl Viro { 1999e1181ee6SJeff Layton struct path *path = &filp->f_path; 20007715b521SAl Viro struct inode *inode = path->dentry->d_inode; 20017715b521SAl Viro int error = get_write_access(inode); 20021da177e4SLinus Torvalds if (error) 20037715b521SAl Viro return error; 20041da177e4SLinus Torvalds /* 20051da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 20061da177e4SLinus Torvalds */ 20071da177e4SLinus Torvalds error = locks_verify_locked(inode); 2008be6d3e56SKentaro Takeda if (!error) 2009ea0d3ab2STetsuo Handa error = security_path_truncate(path); 20101da177e4SLinus Torvalds if (!error) { 20117715b521SAl Viro error = do_truncate(path->dentry, 0, 2012d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 2013e1181ee6SJeff Layton filp); 20141da177e4SLinus Torvalds } 20151da177e4SLinus Torvalds put_write_access(inode); 2016acd0c935SMimi Zohar return error; 20171da177e4SLinus Torvalds } 20181da177e4SLinus Torvalds 2019d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2020d57999e1SDave Hansen { 20218a5e929dSAl Viro if ((flag & O_ACCMODE) == 3) 20228a5e929dSAl Viro flag--; 2023d57999e1SDave Hansen return flag; 2024d57999e1SDave Hansen } 2025d57999e1SDave Hansen 202631e6b01fSNick Piggin /* 2027fe2d35ffSAl Viro * Handle the last step of open() 202831e6b01fSNick Piggin */ 2029fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path, 2030c3e380b0SAl Viro const struct open_flags *op, const char *pathname) 2031fb1cc555SAl Viro { 2032a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 20336c0d46c4SAl Viro struct dentry *dentry; 2034ca344a89SAl Viro int open_flag = op->open_flag; 20356c0d46c4SAl Viro int will_truncate = open_flag & O_TRUNC; 2036ca344a89SAl Viro int want_write = 0; 2037bcda7652SAl Viro int acc_mode = op->acc_mode; 2038fb1cc555SAl Viro struct file *filp; 203916c2cd71SAl Viro int error; 2040fb1cc555SAl Viro 2041c3e380b0SAl Viro nd->flags &= ~LOOKUP_PARENT; 2042c3e380b0SAl Viro nd->flags |= op->intent; 2043c3e380b0SAl Viro 20441f36f774SAl Viro switch (nd->last_type) { 20451f36f774SAl Viro case LAST_DOTDOT: 2046176306f5SNeil Brown case LAST_DOT: 2047fe2d35ffSAl Viro error = handle_dots(nd, nd->last_type); 2048fe2d35ffSAl Viro if (error) 2049fe2d35ffSAl Viro return ERR_PTR(error); 20501f36f774SAl Viro /* fallthrough */ 20511f36f774SAl Viro case LAST_ROOT: 20529f1fafeeSAl Viro error = complete_walk(nd); 205316c2cd71SAl Viro if (error) 20549f1fafeeSAl Viro return ERR_PTR(error); 2055fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2056ca344a89SAl Viro if (open_flag & O_CREAT) { 205716c2cd71SAl Viro error = -EISDIR; 20581f36f774SAl Viro goto exit; 2059fe2d35ffSAl Viro } 2060fe2d35ffSAl Viro goto ok; 20611f36f774SAl Viro case LAST_BIND: 20629f1fafeeSAl Viro error = complete_walk(nd); 206316c2cd71SAl Viro if (error) 20649f1fafeeSAl Viro return ERR_PTR(error); 20651f36f774SAl Viro audit_inode(pathname, dir); 20661f36f774SAl Viro goto ok; 20671f36f774SAl Viro } 2068a2c36b45SAl Viro 2069ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 2070bcda7652SAl Viro int symlink_ok = 0; 2071fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 2072fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2073bcda7652SAl Viro if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) 2074bcda7652SAl Viro symlink_ok = 1; 2075fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 2076ce57dfc1SAl Viro error = walk_component(nd, path, &nd->last, LAST_NORM, 2077ce57dfc1SAl Viro !symlink_ok); 2078ce57dfc1SAl Viro if (error < 0) 2079fe2d35ffSAl Viro return ERR_PTR(error); 2080ce57dfc1SAl Viro if (error) /* symlink */ 2081fe2d35ffSAl Viro return NULL; 2082fe2d35ffSAl Viro /* sayonara */ 20839f1fafeeSAl Viro error = complete_walk(nd); 20849f1fafeeSAl Viro if (error) 2085fe2d35ffSAl Viro return ERR_PTR(-ECHILD); 2086fe2d35ffSAl Viro 2087fe2d35ffSAl Viro error = -ENOTDIR; 2088fe2d35ffSAl Viro if (nd->flags & LOOKUP_DIRECTORY) { 2089ce57dfc1SAl Viro if (!nd->inode->i_op->lookup) 2090fe2d35ffSAl Viro goto exit; 2091fe2d35ffSAl Viro } 2092fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2093fe2d35ffSAl Viro goto ok; 2094fe2d35ffSAl Viro } 2095fe2d35ffSAl Viro 2096fe2d35ffSAl Viro /* create side of things */ 20979f1fafeeSAl Viro error = complete_walk(nd); 20989f1fafeeSAl Viro if (error) 20999f1fafeeSAl Viro return ERR_PTR(error); 2100fe2d35ffSAl Viro 2101fe2d35ffSAl Viro audit_inode(pathname, dir); 210216c2cd71SAl Viro error = -EISDIR; 21031f36f774SAl Viro /* trailing slashes? */ 210431e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 21051f36f774SAl Viro goto exit; 21061f36f774SAl Viro 2107a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 2108a1e28038SAl Viro 21096c0d46c4SAl Viro dentry = lookup_hash(nd); 21106c0d46c4SAl Viro error = PTR_ERR(dentry); 21116c0d46c4SAl Viro if (IS_ERR(dentry)) { 2112fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2113fb1cc555SAl Viro goto exit; 2114fb1cc555SAl Viro } 2115fb1cc555SAl Viro 21166c0d46c4SAl Viro path->dentry = dentry; 21176c0d46c4SAl Viro path->mnt = nd->path.mnt; 21186c0d46c4SAl Viro 2119fb1cc555SAl Viro /* Negative dentry, just create the file */ 21206c0d46c4SAl Viro if (!dentry->d_inode) { 21216c0d46c4SAl Viro int mode = op->mode; 21226c0d46c4SAl Viro if (!IS_POSIXACL(dir->d_inode)) 21236c0d46c4SAl Viro mode &= ~current_umask(); 2124fb1cc555SAl Viro /* 2125fb1cc555SAl Viro * This write is needed to ensure that a 21266c0d46c4SAl Viro * rw->ro transition does not occur between 2127fb1cc555SAl Viro * the time when the file is created and when 2128fb1cc555SAl Viro * a permanent write count is taken through 2129fb1cc555SAl Viro * the 'struct file' in nameidata_to_filp(). 2130fb1cc555SAl Viro */ 2131fb1cc555SAl Viro error = mnt_want_write(nd->path.mnt); 2132fb1cc555SAl Viro if (error) 2133fb1cc555SAl Viro goto exit_mutex_unlock; 2134ca344a89SAl Viro want_write = 1; 21359b44f1b3SAl Viro /* Don't check for write permission, don't truncate */ 2136ca344a89SAl Viro open_flag &= ~O_TRUNC; 21376c0d46c4SAl Viro will_truncate = 0; 2138bcda7652SAl Viro acc_mode = MAY_OPEN; 21396c0d46c4SAl Viro error = security_path_mknod(&nd->path, dentry, mode, 0); 21406c0d46c4SAl Viro if (error) 21416c0d46c4SAl Viro goto exit_mutex_unlock; 21426c0d46c4SAl Viro error = vfs_create(dir->d_inode, dentry, mode, nd); 21436c0d46c4SAl Viro if (error) 21446c0d46c4SAl Viro goto exit_mutex_unlock; 21456c0d46c4SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 21466c0d46c4SAl Viro dput(nd->path.dentry); 21476c0d46c4SAl Viro nd->path.dentry = dentry; 2148ca344a89SAl Viro goto common; 2149fb1cc555SAl Viro } 2150fb1cc555SAl Viro 2151fb1cc555SAl Viro /* 2152fb1cc555SAl Viro * It already exists. 2153fb1cc555SAl Viro */ 2154fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2155fb1cc555SAl Viro audit_inode(pathname, path->dentry); 2156fb1cc555SAl Viro 2157fb1cc555SAl Viro error = -EEXIST; 2158ca344a89SAl Viro if (open_flag & O_EXCL) 2159fb1cc555SAl Viro goto exit_dput; 2160fb1cc555SAl Viro 21619875cf80SDavid Howells error = follow_managed(path, nd->flags); 21629875cf80SDavid Howells if (error < 0) 2163fb1cc555SAl Viro goto exit_dput; 2164fb1cc555SAl Viro 2165fb1cc555SAl Viro error = -ENOENT; 2166fb1cc555SAl Viro if (!path->dentry->d_inode) 2167fb1cc555SAl Viro goto exit_dput; 21689e67f361SAl Viro 21699e67f361SAl Viro if (path->dentry->d_inode->i_op->follow_link) 2170fb1cc555SAl Viro return NULL; 2171fb1cc555SAl Viro 2172fb1cc555SAl Viro path_to_nameidata(path, nd); 217331e6b01fSNick Piggin nd->inode = path->dentry->d_inode; 2174fb1cc555SAl Viro error = -EISDIR; 217531e6b01fSNick Piggin if (S_ISDIR(nd->inode->i_mode)) 2176fb1cc555SAl Viro goto exit; 217767ee3ad2SAl Viro ok: 21786c0d46c4SAl Viro if (!S_ISREG(nd->inode->i_mode)) 21796c0d46c4SAl Viro will_truncate = 0; 21806c0d46c4SAl Viro 21810f9d1a10SAl Viro if (will_truncate) { 21820f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 21830f9d1a10SAl Viro if (error) 21840f9d1a10SAl Viro goto exit; 2185ca344a89SAl Viro want_write = 1; 21860f9d1a10SAl Viro } 2187ca344a89SAl Viro common: 2188bcda7652SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2189ca344a89SAl Viro if (error) 21900f9d1a10SAl Viro goto exit; 21910f9d1a10SAl Viro filp = nameidata_to_filp(nd); 21920f9d1a10SAl Viro if (!IS_ERR(filp)) { 21930f9d1a10SAl Viro error = ima_file_check(filp, op->acc_mode); 21940f9d1a10SAl Viro if (error) { 21950f9d1a10SAl Viro fput(filp); 21960f9d1a10SAl Viro filp = ERR_PTR(error); 21970f9d1a10SAl Viro } 21980f9d1a10SAl Viro } 21990f9d1a10SAl Viro if (!IS_ERR(filp)) { 22000f9d1a10SAl Viro if (will_truncate) { 22010f9d1a10SAl Viro error = handle_truncate(filp); 22020f9d1a10SAl Viro if (error) { 22030f9d1a10SAl Viro fput(filp); 22040f9d1a10SAl Viro filp = ERR_PTR(error); 22050f9d1a10SAl Viro } 22060f9d1a10SAl Viro } 22070f9d1a10SAl Viro } 2208ca344a89SAl Viro out: 2209ca344a89SAl Viro if (want_write) 22100f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 22110f9d1a10SAl Viro path_put(&nd->path); 2212fb1cc555SAl Viro return filp; 2213fb1cc555SAl Viro 2214fb1cc555SAl Viro exit_mutex_unlock: 2215fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2216fb1cc555SAl Viro exit_dput: 2217fb1cc555SAl Viro path_put_conditional(path, nd); 2218fb1cc555SAl Viro exit: 2219ca344a89SAl Viro filp = ERR_PTR(error); 2220ca344a89SAl Viro goto out; 2221fb1cc555SAl Viro } 2222fb1cc555SAl Viro 222313aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname, 222473d049a4SAl Viro struct nameidata *nd, const struct open_flags *op, int flags) 22251da177e4SLinus Torvalds { 2226fe2d35ffSAl Viro struct file *base = NULL; 22274a3fd211SDave Hansen struct file *filp; 22289850c056SAl Viro struct path path; 222913aab428SAl Viro int error; 223031e6b01fSNick Piggin 223131e6b01fSNick Piggin filp = get_empty_filp(); 223231e6b01fSNick Piggin if (!filp) 223331e6b01fSNick Piggin return ERR_PTR(-ENFILE); 223431e6b01fSNick Piggin 223547c805dcSAl Viro filp->f_flags = op->open_flag; 223673d049a4SAl Viro nd->intent.open.file = filp; 223773d049a4SAl Viro nd->intent.open.flags = open_to_namei_flags(op->open_flag); 223873d049a4SAl Viro nd->intent.open.create_mode = op->mode; 223931e6b01fSNick Piggin 224073d049a4SAl Viro error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base); 224131e6b01fSNick Piggin if (unlikely(error)) 224213aab428SAl Viro goto out_filp; 224331e6b01fSNick Piggin 2244fe2d35ffSAl Viro current->total_link_count = 0; 224573d049a4SAl Viro error = link_path_walk(pathname, nd); 224631e6b01fSNick Piggin if (unlikely(error)) 224731e6b01fSNick Piggin goto out_filp; 22481da177e4SLinus Torvalds 224973d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2250806b681cSAl Viro while (unlikely(!filp)) { /* trailing symlink */ 22517b9337aaSNick Piggin struct path link = path; 2252def4af30SAl Viro void *cookie; 2253574197e0SAl Viro if (!(nd->flags & LOOKUP_FOLLOW)) { 225473d049a4SAl Viro path_put_conditional(&path, nd); 225573d049a4SAl Viro path_put(&nd->path); 225640b39136SAl Viro filp = ERR_PTR(-ELOOP); 225740b39136SAl Viro break; 225840b39136SAl Viro } 225973d049a4SAl Viro nd->flags |= LOOKUP_PARENT; 226073d049a4SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 2261574197e0SAl Viro error = follow_link(&link, nd, &cookie); 2262c3e380b0SAl Viro if (unlikely(error)) 2263f1afe9efSAl Viro filp = ERR_PTR(error); 2264c3e380b0SAl Viro else 226573d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2266574197e0SAl Viro put_link(nd, &link, cookie); 2267806b681cSAl Viro } 226810fa8e62SAl Viro out: 226973d049a4SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) 227073d049a4SAl Viro path_put(&nd->root); 2271fe2d35ffSAl Viro if (base) 2272fe2d35ffSAl Viro fput(base); 227373d049a4SAl Viro release_open_intent(nd); 227410fa8e62SAl Viro return filp; 22751da177e4SLinus Torvalds 227631e6b01fSNick Piggin out_filp: 227710fa8e62SAl Viro filp = ERR_PTR(error); 227810fa8e62SAl Viro goto out; 2279de459215SKirill Korotaev } 22801da177e4SLinus Torvalds 228113aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname, 228213aab428SAl Viro const struct open_flags *op, int flags) 228313aab428SAl Viro { 228473d049a4SAl Viro struct nameidata nd; 228513aab428SAl Viro struct file *filp; 228613aab428SAl Viro 228773d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); 228813aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 228973d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags); 229013aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 229173d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); 229213aab428SAl Viro return filp; 229313aab428SAl Viro } 229413aab428SAl Viro 229573d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, 229673d049a4SAl Viro const char *name, const struct open_flags *op, int flags) 229773d049a4SAl Viro { 229873d049a4SAl Viro struct nameidata nd; 229973d049a4SAl Viro struct file *file; 230073d049a4SAl Viro 230173d049a4SAl Viro nd.root.mnt = mnt; 230273d049a4SAl Viro nd.root.dentry = dentry; 230373d049a4SAl Viro 230473d049a4SAl Viro flags |= LOOKUP_ROOT; 230573d049a4SAl Viro 2306bcda7652SAl Viro if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN) 230773d049a4SAl Viro return ERR_PTR(-ELOOP); 230873d049a4SAl Viro 230973d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU); 231073d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 231173d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags); 231273d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 231373d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL); 231473d049a4SAl Viro return file; 231573d049a4SAl Viro } 231673d049a4SAl Viro 2317ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir) 23181da177e4SLinus Torvalds { 2319c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 2320ed75e95dSAl Viro struct nameidata nd; 2321ed75e95dSAl Viro int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd); 2322ed75e95dSAl Viro if (error) 2323ed75e95dSAl Viro return ERR_PTR(error); 23241da177e4SLinus Torvalds 2325c663e5d8SChristoph Hellwig /* 2326c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 2327c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 2328c663e5d8SChristoph Hellwig */ 2329ed75e95dSAl Viro if (nd.last_type != LAST_NORM) 2330ed75e95dSAl Viro goto out; 2331ed75e95dSAl Viro nd.flags &= ~LOOKUP_PARENT; 2332ed75e95dSAl Viro nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL; 2333ed75e95dSAl Viro nd.intent.open.flags = O_EXCL; 2334c663e5d8SChristoph Hellwig 2335c663e5d8SChristoph Hellwig /* 2336c663e5d8SChristoph Hellwig * Do the final lookup. 2337c663e5d8SChristoph Hellwig */ 2338ed75e95dSAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 2339ed75e95dSAl Viro dentry = lookup_hash(&nd); 23401da177e4SLinus Torvalds if (IS_ERR(dentry)) 23411da177e4SLinus Torvalds goto fail; 2342c663e5d8SChristoph Hellwig 2343e9baf6e5SAl Viro if (dentry->d_inode) 2344e9baf6e5SAl Viro goto eexist; 2345c663e5d8SChristoph Hellwig /* 2346c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 2347c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 2348c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 2349c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 2350c663e5d8SChristoph Hellwig */ 2351ed75e95dSAl Viro if (unlikely(!is_dir && nd.last.name[nd.last.len])) { 23521da177e4SLinus Torvalds dput(dentry); 23531da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 2354ed75e95dSAl Viro goto fail; 2355e9baf6e5SAl Viro } 2356ed75e95dSAl Viro *path = nd.path; 2357e9baf6e5SAl Viro return dentry; 2358e9baf6e5SAl Viro eexist: 2359e9baf6e5SAl Viro dput(dentry); 2360e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 23611da177e4SLinus Torvalds fail: 2362dae6ad8fSAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 2363ed75e95dSAl Viro out: 2364dae6ad8fSAl Viro path_put(&nd.path); 2365ed75e95dSAl Viro return dentry; 2366dae6ad8fSAl Viro } 2367dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create); 2368dae6ad8fSAl Viro 2369dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir) 2370dae6ad8fSAl Viro { 2371dae6ad8fSAl Viro char *tmp = getname(pathname); 2372dae6ad8fSAl Viro struct dentry *res; 2373dae6ad8fSAl Viro if (IS_ERR(tmp)) 2374dae6ad8fSAl Viro return ERR_CAST(tmp); 2375dae6ad8fSAl Viro res = kern_path_create(dfd, tmp, path, is_dir); 2376dae6ad8fSAl Viro putname(tmp); 2377dae6ad8fSAl Viro return res; 2378dae6ad8fSAl Viro } 2379dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create); 2380dae6ad8fSAl Viro 23811da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 23821da177e4SLinus Torvalds { 2383a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 23841da177e4SLinus Torvalds 23851da177e4SLinus Torvalds if (error) 23861da177e4SLinus Torvalds return error; 23871da177e4SLinus Torvalds 2388e795b717SSerge E. Hallyn if ((S_ISCHR(mode) || S_ISBLK(mode)) && 2389e795b717SSerge E. Hallyn !ns_capable(inode_userns(dir), CAP_MKNOD)) 23901da177e4SLinus Torvalds return -EPERM; 23911da177e4SLinus Torvalds 2392acfa4380SAl Viro if (!dir->i_op->mknod) 23931da177e4SLinus Torvalds return -EPERM; 23941da177e4SLinus Torvalds 239508ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 239608ce5f16SSerge E. Hallyn if (error) 239708ce5f16SSerge E. Hallyn return error; 239808ce5f16SSerge E. Hallyn 23991da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 24001da177e4SLinus Torvalds if (error) 24011da177e4SLinus Torvalds return error; 24021da177e4SLinus Torvalds 24031da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 2404a74574aaSStephen Smalley if (!error) 2405f38aa942SAmy Griffis fsnotify_create(dir, dentry); 24061da177e4SLinus Torvalds return error; 24071da177e4SLinus Torvalds } 24081da177e4SLinus Torvalds 2409463c3197SDave Hansen static int may_mknod(mode_t mode) 2410463c3197SDave Hansen { 2411463c3197SDave Hansen switch (mode & S_IFMT) { 2412463c3197SDave Hansen case S_IFREG: 2413463c3197SDave Hansen case S_IFCHR: 2414463c3197SDave Hansen case S_IFBLK: 2415463c3197SDave Hansen case S_IFIFO: 2416463c3197SDave Hansen case S_IFSOCK: 2417463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2418463c3197SDave Hansen return 0; 2419463c3197SDave Hansen case S_IFDIR: 2420463c3197SDave Hansen return -EPERM; 2421463c3197SDave Hansen default: 2422463c3197SDave Hansen return -EINVAL; 2423463c3197SDave Hansen } 2424463c3197SDave Hansen } 2425463c3197SDave Hansen 24262e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode, 24272e4d0924SHeiko Carstens unsigned, dev) 24281da177e4SLinus Torvalds { 24291da177e4SLinus Torvalds struct dentry *dentry; 2430dae6ad8fSAl Viro struct path path; 2431dae6ad8fSAl Viro int error; 24321da177e4SLinus Torvalds 24331da177e4SLinus Torvalds if (S_ISDIR(mode)) 24341da177e4SLinus Torvalds return -EPERM; 24351da177e4SLinus Torvalds 2436dae6ad8fSAl Viro dentry = user_path_create(dfd, filename, &path, 0); 2437dae6ad8fSAl Viro if (IS_ERR(dentry)) 2438dae6ad8fSAl Viro return PTR_ERR(dentry); 24392ad94ae6SAl Viro 2440dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 2441ce3b0f8dSAl Viro mode &= ~current_umask(); 2442463c3197SDave Hansen error = may_mknod(mode); 2443463c3197SDave Hansen if (error) 2444463c3197SDave Hansen goto out_dput; 2445dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 2446463c3197SDave Hansen if (error) 2447463c3197SDave Hansen goto out_dput; 2448dae6ad8fSAl Viro error = security_path_mknod(&path, dentry, mode, dev); 2449be6d3e56SKentaro Takeda if (error) 2450be6d3e56SKentaro Takeda goto out_drop_write; 24511da177e4SLinus Torvalds switch (mode & S_IFMT) { 24521da177e4SLinus Torvalds case 0: case S_IFREG: 2453dae6ad8fSAl Viro error = vfs_create(path.dentry->d_inode,dentry,mode,NULL); 24541da177e4SLinus Torvalds break; 24551da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 2456dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode, 24571da177e4SLinus Torvalds new_decode_dev(dev)); 24581da177e4SLinus Torvalds break; 24591da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 2460dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); 24611da177e4SLinus Torvalds break; 24621da177e4SLinus Torvalds } 2463be6d3e56SKentaro Takeda out_drop_write: 2464dae6ad8fSAl Viro mnt_drop_write(path.mnt); 2465463c3197SDave Hansen out_dput: 24661da177e4SLinus Torvalds dput(dentry); 2467dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2468dae6ad8fSAl Viro path_put(&path); 24691da177e4SLinus Torvalds 24701da177e4SLinus Torvalds return error; 24711da177e4SLinus Torvalds } 24721da177e4SLinus Torvalds 24733480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev) 24745590ff0dSUlrich Drepper { 24755590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 24765590ff0dSUlrich Drepper } 24775590ff0dSUlrich Drepper 24781da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 24791da177e4SLinus Torvalds { 2480a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 24811da177e4SLinus Torvalds 24821da177e4SLinus Torvalds if (error) 24831da177e4SLinus Torvalds return error; 24841da177e4SLinus Torvalds 2485acfa4380SAl Viro if (!dir->i_op->mkdir) 24861da177e4SLinus Torvalds return -EPERM; 24871da177e4SLinus Torvalds 24881da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 24891da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 24901da177e4SLinus Torvalds if (error) 24911da177e4SLinus Torvalds return error; 24921da177e4SLinus Torvalds 24931da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2494a74574aaSStephen Smalley if (!error) 2495f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 24961da177e4SLinus Torvalds return error; 24971da177e4SLinus Torvalds } 24981da177e4SLinus Torvalds 24992e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode) 25001da177e4SLinus Torvalds { 25016902d925SDave Hansen struct dentry *dentry; 2502dae6ad8fSAl Viro struct path path; 2503dae6ad8fSAl Viro int error; 25041da177e4SLinus Torvalds 2505dae6ad8fSAl Viro dentry = user_path_create(dfd, pathname, &path, 1); 25066902d925SDave Hansen if (IS_ERR(dentry)) 2507dae6ad8fSAl Viro return PTR_ERR(dentry); 25086902d925SDave Hansen 2509dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 2510ce3b0f8dSAl Viro mode &= ~current_umask(); 2511dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 2512463c3197SDave Hansen if (error) 2513463c3197SDave Hansen goto out_dput; 2514dae6ad8fSAl Viro error = security_path_mkdir(&path, dentry, mode); 2515be6d3e56SKentaro Takeda if (error) 2516be6d3e56SKentaro Takeda goto out_drop_write; 2517dae6ad8fSAl Viro error = vfs_mkdir(path.dentry->d_inode, dentry, mode); 2518be6d3e56SKentaro Takeda out_drop_write: 2519dae6ad8fSAl Viro mnt_drop_write(path.mnt); 2520463c3197SDave Hansen out_dput: 25211da177e4SLinus Torvalds dput(dentry); 2522dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2523dae6ad8fSAl Viro path_put(&path); 25241da177e4SLinus Torvalds return error; 25251da177e4SLinus Torvalds } 25261da177e4SLinus Torvalds 25273cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode) 25285590ff0dSUlrich Drepper { 25295590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 25305590ff0dSUlrich Drepper } 25315590ff0dSUlrich Drepper 25321da177e4SLinus Torvalds /* 2533a71905f0SSage Weil * The dentry_unhash() helper will try to drop the dentry early: we 2534a71905f0SSage Weil * should have a usage count of 2 if we're the only user of this 2535a71905f0SSage Weil * dentry, and if that is true (possibly after pruning the dcache), 2536a71905f0SSage Weil * then we drop the dentry now. 25371da177e4SLinus Torvalds * 25381da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 25391da177e4SLinus Torvalds * do a 25401da177e4SLinus Torvalds * 25411da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 25421da177e4SLinus Torvalds * return -EBUSY; 25431da177e4SLinus Torvalds * 25441da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 25451da177e4SLinus Torvalds * that is still in use by something else.. 25461da177e4SLinus Torvalds */ 25471da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 25481da177e4SLinus Torvalds { 25491da177e4SLinus Torvalds shrink_dcache_parent(dentry); 25501da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 255164252c75SSage Weil if (dentry->d_count == 1) 25521da177e4SLinus Torvalds __d_drop(dentry); 25531da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 25541da177e4SLinus Torvalds } 25551da177e4SLinus Torvalds 25561da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 25571da177e4SLinus Torvalds { 25581da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 25591da177e4SLinus Torvalds 25601da177e4SLinus Torvalds if (error) 25611da177e4SLinus Torvalds return error; 25621da177e4SLinus Torvalds 2563acfa4380SAl Viro if (!dir->i_op->rmdir) 25641da177e4SLinus Torvalds return -EPERM; 25651da177e4SLinus Torvalds 25661b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 2567912dbc15SSage Weil 25681da177e4SLinus Torvalds error = -EBUSY; 2569912dbc15SSage Weil if (d_mountpoint(dentry)) 2570912dbc15SSage Weil goto out; 2571912dbc15SSage Weil 25721da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 2573912dbc15SSage Weil if (error) 2574912dbc15SSage Weil goto out; 2575912dbc15SSage Weil 25763cebde24SSage Weil shrink_dcache_parent(dentry); 25771da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 2578912dbc15SSage Weil if (error) 2579912dbc15SSage Weil goto out; 2580912dbc15SSage Weil 25811da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 2582d83c49f3SAl Viro dont_mount(dentry); 25831da177e4SLinus Torvalds 2584912dbc15SSage Weil out: 2585912dbc15SSage Weil mutex_unlock(&dentry->d_inode->i_mutex); 2586912dbc15SSage Weil if (!error) 2587912dbc15SSage Weil d_delete(dentry); 25881da177e4SLinus Torvalds return error; 25891da177e4SLinus Torvalds } 25901da177e4SLinus Torvalds 25915590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 25921da177e4SLinus Torvalds { 25931da177e4SLinus Torvalds int error = 0; 25941da177e4SLinus Torvalds char * name; 25951da177e4SLinus Torvalds struct dentry *dentry; 25961da177e4SLinus Torvalds struct nameidata nd; 25971da177e4SLinus Torvalds 25982ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 25991da177e4SLinus Torvalds if (error) 26002ad94ae6SAl Viro return error; 26011da177e4SLinus Torvalds 26021da177e4SLinus Torvalds switch(nd.last_type) { 26031da177e4SLinus Torvalds case LAST_DOTDOT: 26041da177e4SLinus Torvalds error = -ENOTEMPTY; 26051da177e4SLinus Torvalds goto exit1; 26061da177e4SLinus Torvalds case LAST_DOT: 26071da177e4SLinus Torvalds error = -EINVAL; 26081da177e4SLinus Torvalds goto exit1; 26091da177e4SLinus Torvalds case LAST_ROOT: 26101da177e4SLinus Torvalds error = -EBUSY; 26111da177e4SLinus Torvalds goto exit1; 26121da177e4SLinus Torvalds } 26130612d9fbSOGAWA Hirofumi 26140612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 26150612d9fbSOGAWA Hirofumi 26164ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 261749705b77SChristoph Hellwig dentry = lookup_hash(&nd); 26181da177e4SLinus Torvalds error = PTR_ERR(dentry); 26196902d925SDave Hansen if (IS_ERR(dentry)) 26206902d925SDave Hansen goto exit2; 2621e6bc45d6STheodore Ts'o if (!dentry->d_inode) { 2622e6bc45d6STheodore Ts'o error = -ENOENT; 2623e6bc45d6STheodore Ts'o goto exit3; 2624e6bc45d6STheodore Ts'o } 26250622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 26260622753bSDave Hansen if (error) 26270622753bSDave Hansen goto exit3; 2628be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 2629be6d3e56SKentaro Takeda if (error) 2630be6d3e56SKentaro Takeda goto exit4; 26314ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 2632be6d3e56SKentaro Takeda exit4: 26330622753bSDave Hansen mnt_drop_write(nd.path.mnt); 26340622753bSDave Hansen exit3: 26351da177e4SLinus Torvalds dput(dentry); 26366902d925SDave Hansen exit2: 26374ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 26381da177e4SLinus Torvalds exit1: 26391d957f9bSJan Blunck path_put(&nd.path); 26401da177e4SLinus Torvalds putname(name); 26411da177e4SLinus Torvalds return error; 26421da177e4SLinus Torvalds } 26431da177e4SLinus Torvalds 26443cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 26455590ff0dSUlrich Drepper { 26465590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 26475590ff0dSUlrich Drepper } 26485590ff0dSUlrich Drepper 26491da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 26501da177e4SLinus Torvalds { 26511da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 26521da177e4SLinus Torvalds 26531da177e4SLinus Torvalds if (error) 26541da177e4SLinus Torvalds return error; 26551da177e4SLinus Torvalds 2656acfa4380SAl Viro if (!dir->i_op->unlink) 26571da177e4SLinus Torvalds return -EPERM; 26581da177e4SLinus Torvalds 26591b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 26601da177e4SLinus Torvalds if (d_mountpoint(dentry)) 26611da177e4SLinus Torvalds error = -EBUSY; 26621da177e4SLinus Torvalds else { 26631da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 2664bec1052eSAl Viro if (!error) { 26651da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 2666bec1052eSAl Viro if (!error) 2667d83c49f3SAl Viro dont_mount(dentry); 2668bec1052eSAl Viro } 26691da177e4SLinus Torvalds } 26701b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 26711da177e4SLinus Torvalds 26721da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 26731da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 2674ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 26751da177e4SLinus Torvalds d_delete(dentry); 26761da177e4SLinus Torvalds } 26770eeca283SRobert Love 26781da177e4SLinus Torvalds return error; 26791da177e4SLinus Torvalds } 26801da177e4SLinus Torvalds 26811da177e4SLinus Torvalds /* 26821da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 26831b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 26841da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 26851da177e4SLinus Torvalds * while waiting on the I/O. 26861da177e4SLinus Torvalds */ 26875590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 26881da177e4SLinus Torvalds { 26892ad94ae6SAl Viro int error; 26901da177e4SLinus Torvalds char *name; 26911da177e4SLinus Torvalds struct dentry *dentry; 26921da177e4SLinus Torvalds struct nameidata nd; 26931da177e4SLinus Torvalds struct inode *inode = NULL; 26941da177e4SLinus Torvalds 26952ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 26961da177e4SLinus Torvalds if (error) 26972ad94ae6SAl Viro return error; 26982ad94ae6SAl Viro 26991da177e4SLinus Torvalds error = -EISDIR; 27001da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 27011da177e4SLinus Torvalds goto exit1; 27020612d9fbSOGAWA Hirofumi 27030612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 27040612d9fbSOGAWA Hirofumi 27054ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 270649705b77SChristoph Hellwig dentry = lookup_hash(&nd); 27071da177e4SLinus Torvalds error = PTR_ERR(dentry); 27081da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 27091da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 271050338b88STörök Edwin if (nd.last.name[nd.last.len]) 271150338b88STörök Edwin goto slashes; 27121da177e4SLinus Torvalds inode = dentry->d_inode; 271350338b88STörök Edwin if (!inode) 2714e6bc45d6STheodore Ts'o goto slashes; 27157de9c6eeSAl Viro ihold(inode); 27160622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 27170622753bSDave Hansen if (error) 27180622753bSDave Hansen goto exit2; 2719be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 2720be6d3e56SKentaro Takeda if (error) 2721be6d3e56SKentaro Takeda goto exit3; 27224ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 2723be6d3e56SKentaro Takeda exit3: 27240622753bSDave Hansen mnt_drop_write(nd.path.mnt); 27251da177e4SLinus Torvalds exit2: 27261da177e4SLinus Torvalds dput(dentry); 27271da177e4SLinus Torvalds } 27284ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 27291da177e4SLinus Torvalds if (inode) 27301da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 27311da177e4SLinus Torvalds exit1: 27321d957f9bSJan Blunck path_put(&nd.path); 27331da177e4SLinus Torvalds putname(name); 27341da177e4SLinus Torvalds return error; 27351da177e4SLinus Torvalds 27361da177e4SLinus Torvalds slashes: 27371da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 27381da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 27391da177e4SLinus Torvalds goto exit2; 27401da177e4SLinus Torvalds } 27411da177e4SLinus Torvalds 27422e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 27435590ff0dSUlrich Drepper { 27445590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 27455590ff0dSUlrich Drepper return -EINVAL; 27465590ff0dSUlrich Drepper 27475590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 27485590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 27495590ff0dSUlrich Drepper 27505590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 27515590ff0dSUlrich Drepper } 27525590ff0dSUlrich Drepper 27533480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 27545590ff0dSUlrich Drepper { 27555590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 27565590ff0dSUlrich Drepper } 27575590ff0dSUlrich Drepper 2758db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 27591da177e4SLinus Torvalds { 2760a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 27611da177e4SLinus Torvalds 27621da177e4SLinus Torvalds if (error) 27631da177e4SLinus Torvalds return error; 27641da177e4SLinus Torvalds 2765acfa4380SAl Viro if (!dir->i_op->symlink) 27661da177e4SLinus Torvalds return -EPERM; 27671da177e4SLinus Torvalds 27681da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 27691da177e4SLinus Torvalds if (error) 27701da177e4SLinus Torvalds return error; 27711da177e4SLinus Torvalds 27721da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 2773a74574aaSStephen Smalley if (!error) 2774f38aa942SAmy Griffis fsnotify_create(dir, dentry); 27751da177e4SLinus Torvalds return error; 27761da177e4SLinus Torvalds } 27771da177e4SLinus Torvalds 27782e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 27792e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 27801da177e4SLinus Torvalds { 27812ad94ae6SAl Viro int error; 27821da177e4SLinus Torvalds char *from; 27836902d925SDave Hansen struct dentry *dentry; 2784dae6ad8fSAl Viro struct path path; 27851da177e4SLinus Torvalds 27861da177e4SLinus Torvalds from = getname(oldname); 27871da177e4SLinus Torvalds if (IS_ERR(from)) 27881da177e4SLinus Torvalds return PTR_ERR(from); 27892ad94ae6SAl Viro 2790dae6ad8fSAl Viro dentry = user_path_create(newdfd, newname, &path, 0); 27911da177e4SLinus Torvalds error = PTR_ERR(dentry); 27926902d925SDave Hansen if (IS_ERR(dentry)) 2793dae6ad8fSAl Viro goto out_putname; 27946902d925SDave Hansen 2795dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 279675c3f29dSDave Hansen if (error) 279775c3f29dSDave Hansen goto out_dput; 2798dae6ad8fSAl Viro error = security_path_symlink(&path, dentry, from); 2799be6d3e56SKentaro Takeda if (error) 2800be6d3e56SKentaro Takeda goto out_drop_write; 2801dae6ad8fSAl Viro error = vfs_symlink(path.dentry->d_inode, dentry, from); 2802be6d3e56SKentaro Takeda out_drop_write: 2803dae6ad8fSAl Viro mnt_drop_write(path.mnt); 280475c3f29dSDave Hansen out_dput: 28051da177e4SLinus Torvalds dput(dentry); 2806dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2807dae6ad8fSAl Viro path_put(&path); 28086902d925SDave Hansen out_putname: 28091da177e4SLinus Torvalds putname(from); 28101da177e4SLinus Torvalds return error; 28111da177e4SLinus Torvalds } 28121da177e4SLinus Torvalds 28133480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 28145590ff0dSUlrich Drepper { 28155590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 28165590ff0dSUlrich Drepper } 28175590ff0dSUlrich Drepper 28181da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 28191da177e4SLinus Torvalds { 28201da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 28211da177e4SLinus Torvalds int error; 28221da177e4SLinus Torvalds 28231da177e4SLinus Torvalds if (!inode) 28241da177e4SLinus Torvalds return -ENOENT; 28251da177e4SLinus Torvalds 2826a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 28271da177e4SLinus Torvalds if (error) 28281da177e4SLinus Torvalds return error; 28291da177e4SLinus Torvalds 28301da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 28311da177e4SLinus Torvalds return -EXDEV; 28321da177e4SLinus Torvalds 28331da177e4SLinus Torvalds /* 28341da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 28351da177e4SLinus Torvalds */ 28361da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 28371da177e4SLinus Torvalds return -EPERM; 2838acfa4380SAl Viro if (!dir->i_op->link) 28391da177e4SLinus Torvalds return -EPERM; 28407e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 28411da177e4SLinus Torvalds return -EPERM; 28421da177e4SLinus Torvalds 28431da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 28441da177e4SLinus Torvalds if (error) 28451da177e4SLinus Torvalds return error; 28461da177e4SLinus Torvalds 28477e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 2848aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 2849aae8a97dSAneesh Kumar K.V if (inode->i_nlink == 0) 2850aae8a97dSAneesh Kumar K.V error = -ENOENT; 2851aae8a97dSAneesh Kumar K.V else 28521da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 28537e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 2854e31e14ecSStephen Smalley if (!error) 28557e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 28561da177e4SLinus Torvalds return error; 28571da177e4SLinus Torvalds } 28581da177e4SLinus Torvalds 28591da177e4SLinus Torvalds /* 28601da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 28611da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 28621da177e4SLinus Torvalds * newname. --KAB 28631da177e4SLinus Torvalds * 28641da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 28651da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 28661da177e4SLinus Torvalds * and other special files. --ADM 28671da177e4SLinus Torvalds */ 28682e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 28692e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 28701da177e4SLinus Torvalds { 28711da177e4SLinus Torvalds struct dentry *new_dentry; 2872dae6ad8fSAl Viro struct path old_path, new_path; 287311a7b371SAneesh Kumar K.V int how = 0; 28741da177e4SLinus Torvalds int error; 28751da177e4SLinus Torvalds 287611a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 2877c04030e1SUlrich Drepper return -EINVAL; 287811a7b371SAneesh Kumar K.V /* 287911a7b371SAneesh Kumar K.V * To use null names we require CAP_DAC_READ_SEARCH 288011a7b371SAneesh Kumar K.V * This ensures that not everyone will be able to create 288111a7b371SAneesh Kumar K.V * handlink using the passed filedescriptor. 288211a7b371SAneesh Kumar K.V */ 288311a7b371SAneesh Kumar K.V if (flags & AT_EMPTY_PATH) { 288411a7b371SAneesh Kumar K.V if (!capable(CAP_DAC_READ_SEARCH)) 288511a7b371SAneesh Kumar K.V return -ENOENT; 288611a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 288711a7b371SAneesh Kumar K.V } 2888c04030e1SUlrich Drepper 288911a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 289011a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 289111a7b371SAneesh Kumar K.V 289211a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 28931da177e4SLinus Torvalds if (error) 28942ad94ae6SAl Viro return error; 28952ad94ae6SAl Viro 2896dae6ad8fSAl Viro new_dentry = user_path_create(newdfd, newname, &new_path, 0); 28971da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 28986902d925SDave Hansen if (IS_ERR(new_dentry)) 2899dae6ad8fSAl Viro goto out; 2900dae6ad8fSAl Viro 2901dae6ad8fSAl Viro error = -EXDEV; 2902dae6ad8fSAl Viro if (old_path.mnt != new_path.mnt) 2903dae6ad8fSAl Viro goto out_dput; 2904dae6ad8fSAl Viro error = mnt_want_write(new_path.mnt); 290575c3f29dSDave Hansen if (error) 290675c3f29dSDave Hansen goto out_dput; 2907dae6ad8fSAl Viro error = security_path_link(old_path.dentry, &new_path, new_dentry); 2908be6d3e56SKentaro Takeda if (error) 2909be6d3e56SKentaro Takeda goto out_drop_write; 2910dae6ad8fSAl Viro error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry); 2911be6d3e56SKentaro Takeda out_drop_write: 2912dae6ad8fSAl Viro mnt_drop_write(new_path.mnt); 291375c3f29dSDave Hansen out_dput: 29141da177e4SLinus Torvalds dput(new_dentry); 2915dae6ad8fSAl Viro mutex_unlock(&new_path.dentry->d_inode->i_mutex); 2916dae6ad8fSAl Viro path_put(&new_path); 29171da177e4SLinus Torvalds out: 29182d8f3038SAl Viro path_put(&old_path); 29191da177e4SLinus Torvalds 29201da177e4SLinus Torvalds return error; 29211da177e4SLinus Torvalds } 29221da177e4SLinus Torvalds 29233480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 29245590ff0dSUlrich Drepper { 2925c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 29265590ff0dSUlrich Drepper } 29275590ff0dSUlrich Drepper 29281da177e4SLinus Torvalds /* 29291da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 29301da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 29311da177e4SLinus Torvalds * Problems: 29321da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 29331da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 29341da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 2935a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 29361da177e4SLinus Torvalds * story. 29371da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 29381b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 29391da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 29401da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 2941a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 29421da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 29431da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 29441da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 2945a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 29461da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 29471da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 29481da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 2949e4eaac06SSage Weil * d) conversion from fhandle to dentry may come in the wrong moment - when 29501b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 29511da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 2952c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 29531da177e4SLinus Torvalds * locking]. 29541da177e4SLinus Torvalds */ 295575c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 29561da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 29571da177e4SLinus Torvalds { 29581da177e4SLinus Torvalds int error = 0; 29599055cba7SSage Weil struct inode *target = new_dentry->d_inode; 29601da177e4SLinus Torvalds 29611da177e4SLinus Torvalds /* 29621da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 29631da177e4SLinus Torvalds * we'll need to flip '..'. 29641da177e4SLinus Torvalds */ 29651da177e4SLinus Torvalds if (new_dir != old_dir) { 2966f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 29671da177e4SLinus Torvalds if (error) 29681da177e4SLinus Torvalds return error; 29691da177e4SLinus Torvalds } 29701da177e4SLinus Torvalds 29711da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 29721da177e4SLinus Torvalds if (error) 29731da177e4SLinus Torvalds return error; 29741da177e4SLinus Torvalds 2975d83c49f3SAl Viro if (target) 29761b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 29779055cba7SSage Weil 29781da177e4SLinus Torvalds error = -EBUSY; 29799055cba7SSage Weil if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) 29809055cba7SSage Weil goto out; 29819055cba7SSage Weil 29823cebde24SSage Weil if (target) 29833cebde24SSage Weil shrink_dcache_parent(new_dentry); 29841da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 29859055cba7SSage Weil if (error) 29869055cba7SSage Weil goto out; 29879055cba7SSage Weil 29881da177e4SLinus Torvalds if (target) { 29891da177e4SLinus Torvalds target->i_flags |= S_DEAD; 2990d83c49f3SAl Viro dont_mount(new_dentry); 2991d83c49f3SAl Viro } 29929055cba7SSage Weil out: 29939055cba7SSage Weil if (target) 29941b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 2995e31e14ecSStephen Smalley if (!error) 2996349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 29971da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 29981da177e4SLinus Torvalds return error; 29991da177e4SLinus Torvalds } 30001da177e4SLinus Torvalds 300175c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 30021da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30031da177e4SLinus Torvalds { 300451892bbbSSage Weil struct inode *target = new_dentry->d_inode; 30051da177e4SLinus Torvalds int error; 30061da177e4SLinus Torvalds 30071da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 30081da177e4SLinus Torvalds if (error) 30091da177e4SLinus Torvalds return error; 30101da177e4SLinus Torvalds 30111da177e4SLinus Torvalds dget(new_dentry); 30121da177e4SLinus Torvalds if (target) 30131b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 301451892bbbSSage Weil 30151da177e4SLinus Torvalds error = -EBUSY; 301651892bbbSSage Weil if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 301751892bbbSSage Weil goto out; 301851892bbbSSage Weil 30191da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 302051892bbbSSage Weil if (error) 302151892bbbSSage Weil goto out; 302251892bbbSSage Weil 3023bec1052eSAl Viro if (target) 3024d83c49f3SAl Viro dont_mount(new_dentry); 3025349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 30261da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 302751892bbbSSage Weil out: 30281da177e4SLinus Torvalds if (target) 30291b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 30301da177e4SLinus Torvalds dput(new_dentry); 30311da177e4SLinus Torvalds return error; 30321da177e4SLinus Torvalds } 30331da177e4SLinus Torvalds 30341da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 30351da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30361da177e4SLinus Torvalds { 30371da177e4SLinus Torvalds int error; 30381da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 303959b0df21SEric Paris const unsigned char *old_name; 30401da177e4SLinus Torvalds 30411da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 30421da177e4SLinus Torvalds return 0; 30431da177e4SLinus Torvalds 30441da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 30451da177e4SLinus Torvalds if (error) 30461da177e4SLinus Torvalds return error; 30471da177e4SLinus Torvalds 30481da177e4SLinus Torvalds if (!new_dentry->d_inode) 3049a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 30501da177e4SLinus Torvalds else 30511da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 30521da177e4SLinus Torvalds if (error) 30531da177e4SLinus Torvalds return error; 30541da177e4SLinus Torvalds 3055acfa4380SAl Viro if (!old_dir->i_op->rename) 30561da177e4SLinus Torvalds return -EPERM; 30571da177e4SLinus Torvalds 30580eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 30590eeca283SRobert Love 30601da177e4SLinus Torvalds if (is_dir) 30611da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 30621da177e4SLinus Torvalds else 30631da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3064123df294SAl Viro if (!error) 3065123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 30665a190ae6SAl Viro new_dentry->d_inode, old_dentry); 30670eeca283SRobert Love fsnotify_oldname_free(old_name); 30680eeca283SRobert Love 30691da177e4SLinus Torvalds return error; 30701da177e4SLinus Torvalds } 30711da177e4SLinus Torvalds 30722e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 30732e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 30741da177e4SLinus Torvalds { 30751da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 30761da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 30771da177e4SLinus Torvalds struct dentry *trap; 30781da177e4SLinus Torvalds struct nameidata oldnd, newnd; 30792ad94ae6SAl Viro char *from; 30802ad94ae6SAl Viro char *to; 30812ad94ae6SAl Viro int error; 30821da177e4SLinus Torvalds 30832ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 30841da177e4SLinus Torvalds if (error) 30851da177e4SLinus Torvalds goto exit; 30861da177e4SLinus Torvalds 30872ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 30881da177e4SLinus Torvalds if (error) 30891da177e4SLinus Torvalds goto exit1; 30901da177e4SLinus Torvalds 30911da177e4SLinus Torvalds error = -EXDEV; 30924ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 30931da177e4SLinus Torvalds goto exit2; 30941da177e4SLinus Torvalds 30954ac91378SJan Blunck old_dir = oldnd.path.dentry; 30961da177e4SLinus Torvalds error = -EBUSY; 30971da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 30981da177e4SLinus Torvalds goto exit2; 30991da177e4SLinus Torvalds 31004ac91378SJan Blunck new_dir = newnd.path.dentry; 31011da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 31021da177e4SLinus Torvalds goto exit2; 31031da177e4SLinus Torvalds 31040612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 31050612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 31064e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 31070612d9fbSOGAWA Hirofumi 31081da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 31091da177e4SLinus Torvalds 311049705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 31111da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 31121da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 31131da177e4SLinus Torvalds goto exit3; 31141da177e4SLinus Torvalds /* source must exist */ 31151da177e4SLinus Torvalds error = -ENOENT; 31161da177e4SLinus Torvalds if (!old_dentry->d_inode) 31171da177e4SLinus Torvalds goto exit4; 31181da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 31191da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 31201da177e4SLinus Torvalds error = -ENOTDIR; 31211da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 31221da177e4SLinus Torvalds goto exit4; 31231da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 31241da177e4SLinus Torvalds goto exit4; 31251da177e4SLinus Torvalds } 31261da177e4SLinus Torvalds /* source should not be ancestor of target */ 31271da177e4SLinus Torvalds error = -EINVAL; 31281da177e4SLinus Torvalds if (old_dentry == trap) 31291da177e4SLinus Torvalds goto exit4; 313049705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 31311da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 31321da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 31331da177e4SLinus Torvalds goto exit4; 31341da177e4SLinus Torvalds /* target should not be an ancestor of source */ 31351da177e4SLinus Torvalds error = -ENOTEMPTY; 31361da177e4SLinus Torvalds if (new_dentry == trap) 31371da177e4SLinus Torvalds goto exit5; 31381da177e4SLinus Torvalds 31399079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 31409079b1ebSDave Hansen if (error) 31419079b1ebSDave Hansen goto exit5; 3142be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3143be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3144be6d3e56SKentaro Takeda if (error) 3145be6d3e56SKentaro Takeda goto exit6; 31461da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 31471da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 3148be6d3e56SKentaro Takeda exit6: 31499079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 31501da177e4SLinus Torvalds exit5: 31511da177e4SLinus Torvalds dput(new_dentry); 31521da177e4SLinus Torvalds exit4: 31531da177e4SLinus Torvalds dput(old_dentry); 31541da177e4SLinus Torvalds exit3: 31551da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 31561da177e4SLinus Torvalds exit2: 31571d957f9bSJan Blunck path_put(&newnd.path); 31582ad94ae6SAl Viro putname(to); 31591da177e4SLinus Torvalds exit1: 31601d957f9bSJan Blunck path_put(&oldnd.path); 31611da177e4SLinus Torvalds putname(from); 31622ad94ae6SAl Viro exit: 31631da177e4SLinus Torvalds return error; 31641da177e4SLinus Torvalds } 31651da177e4SLinus Torvalds 3166a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 31675590ff0dSUlrich Drepper { 31685590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 31695590ff0dSUlrich Drepper } 31705590ff0dSUlrich Drepper 31711da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 31721da177e4SLinus Torvalds { 31731da177e4SLinus Torvalds int len; 31741da177e4SLinus Torvalds 31751da177e4SLinus Torvalds len = PTR_ERR(link); 31761da177e4SLinus Torvalds if (IS_ERR(link)) 31771da177e4SLinus Torvalds goto out; 31781da177e4SLinus Torvalds 31791da177e4SLinus Torvalds len = strlen(link); 31801da177e4SLinus Torvalds if (len > (unsigned) buflen) 31811da177e4SLinus Torvalds len = buflen; 31821da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 31831da177e4SLinus Torvalds len = -EFAULT; 31841da177e4SLinus Torvalds out: 31851da177e4SLinus Torvalds return len; 31861da177e4SLinus Torvalds } 31871da177e4SLinus Torvalds 31881da177e4SLinus Torvalds /* 31891da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 31901da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 31911da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 31921da177e4SLinus Torvalds */ 31931da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 31941da177e4SLinus Torvalds { 31951da177e4SLinus Torvalds struct nameidata nd; 3196cc314eefSLinus Torvalds void *cookie; 3197694a1764SMarcin Slusarz int res; 3198cc314eefSLinus Torvalds 31991da177e4SLinus Torvalds nd.depth = 0; 3200cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3201694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3202694a1764SMarcin Slusarz return PTR_ERR(cookie); 3203694a1764SMarcin Slusarz 3204694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 32051da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3206cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3207694a1764SMarcin Slusarz return res; 32081da177e4SLinus Torvalds } 32091da177e4SLinus Torvalds 32101da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 32111da177e4SLinus Torvalds { 32121da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 32131da177e4SLinus Torvalds } 32141da177e4SLinus Torvalds 32151da177e4SLinus Torvalds /* get the link contents into pagecache */ 32161da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 32171da177e4SLinus Torvalds { 3218ebd09abbSDuane Griffin char *kaddr; 32191da177e4SLinus Torvalds struct page *page; 32201da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3221090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 32221da177e4SLinus Torvalds if (IS_ERR(page)) 32236fe6900eSNick Piggin return (char*)page; 32241da177e4SLinus Torvalds *ppage = page; 3225ebd09abbSDuane Griffin kaddr = kmap(page); 3226ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3227ebd09abbSDuane Griffin return kaddr; 32281da177e4SLinus Torvalds } 32291da177e4SLinus Torvalds 32301da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 32311da177e4SLinus Torvalds { 32321da177e4SLinus Torvalds struct page *page = NULL; 32331da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 32341da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 32351da177e4SLinus Torvalds if (page) { 32361da177e4SLinus Torvalds kunmap(page); 32371da177e4SLinus Torvalds page_cache_release(page); 32381da177e4SLinus Torvalds } 32391da177e4SLinus Torvalds return res; 32401da177e4SLinus Torvalds } 32411da177e4SLinus Torvalds 3242cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 32431da177e4SLinus Torvalds { 3244cc314eefSLinus Torvalds struct page *page = NULL; 32451da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 3246cc314eefSLinus Torvalds return page; 32471da177e4SLinus Torvalds } 32481da177e4SLinus Torvalds 3249cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 32501da177e4SLinus Torvalds { 3251cc314eefSLinus Torvalds struct page *page = cookie; 3252cc314eefSLinus Torvalds 3253cc314eefSLinus Torvalds if (page) { 32541da177e4SLinus Torvalds kunmap(page); 32551da177e4SLinus Torvalds page_cache_release(page); 32561da177e4SLinus Torvalds } 32571da177e4SLinus Torvalds } 32581da177e4SLinus Torvalds 325954566b2cSNick Piggin /* 326054566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 326154566b2cSNick Piggin */ 326254566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 32631da177e4SLinus Torvalds { 32641da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 32650adb25d2SKirill Korotaev struct page *page; 3266afddba49SNick Piggin void *fsdata; 3267beb497abSDmitriy Monakhov int err; 32681da177e4SLinus Torvalds char *kaddr; 326954566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 327054566b2cSNick Piggin if (nofs) 327154566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 32721da177e4SLinus Torvalds 32737e53cac4SNeilBrown retry: 3274afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 327554566b2cSNick Piggin flags, &page, &fsdata); 32761da177e4SLinus Torvalds if (err) 3277afddba49SNick Piggin goto fail; 3278afddba49SNick Piggin 32791da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 32801da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 32811da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 3282afddba49SNick Piggin 3283afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 3284afddba49SNick Piggin page, fsdata); 32851da177e4SLinus Torvalds if (err < 0) 32861da177e4SLinus Torvalds goto fail; 3287afddba49SNick Piggin if (err < len-1) 3288afddba49SNick Piggin goto retry; 3289afddba49SNick Piggin 32901da177e4SLinus Torvalds mark_inode_dirty(inode); 32911da177e4SLinus Torvalds return 0; 32921da177e4SLinus Torvalds fail: 32931da177e4SLinus Torvalds return err; 32941da177e4SLinus Torvalds } 32951da177e4SLinus Torvalds 32960adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 32970adb25d2SKirill Korotaev { 32980adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 329954566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 33000adb25d2SKirill Korotaev } 33010adb25d2SKirill Korotaev 330292e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 33031da177e4SLinus Torvalds .readlink = generic_readlink, 33041da177e4SLinus Torvalds .follow_link = page_follow_link_light, 33051da177e4SLinus Torvalds .put_link = page_put_link, 33061da177e4SLinus Torvalds }; 33071da177e4SLinus Torvalds 33082d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 3309cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one); 33101da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 33111da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 33121da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 33131da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 33141da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 33151da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 33161da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 33171da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 33181da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 33190adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 33201da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 33211da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 3322d1811465SAl Viro EXPORT_SYMBOL(kern_path); 332316f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 3324f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 33251da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 33261da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 33271da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 33281da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 33291da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 33301da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 33311da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 33321da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 33331da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 33341da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 33351da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 33361da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 33371da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 33381da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 3339