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 3113ddcd056SLinus Torvalds /* 3123ddcd056SLinus Torvalds * We _really_ want to just do "generic_permission()" without 3133ddcd056SLinus Torvalds * even looking at the inode->i_op values. So we keep a cache 3143ddcd056SLinus Torvalds * flag in inode->i_opflags, that says "this has not special 3153ddcd056SLinus Torvalds * permission function, use the fast case". 3163ddcd056SLinus Torvalds */ 3173ddcd056SLinus Torvalds static inline int do_inode_permission(struct inode *inode, int mask) 3183ddcd056SLinus Torvalds { 3193ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { 3203ddcd056SLinus Torvalds if (likely(inode->i_op->permission)) 3213ddcd056SLinus Torvalds return inode->i_op->permission(inode, mask); 3223ddcd056SLinus Torvalds 3233ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 3243ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 3253ddcd056SLinus Torvalds inode->i_opflags |= IOP_FASTPERM; 3263ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 3273ddcd056SLinus Torvalds } 3283ddcd056SLinus Torvalds return generic_permission(inode, mask); 3293ddcd056SLinus Torvalds } 3303ddcd056SLinus Torvalds 331cb23beb5SChristoph Hellwig /** 332cb23beb5SChristoph Hellwig * inode_permission - check for access rights to a given inode 333cb23beb5SChristoph Hellwig * @inode: inode to check permission on 334cb23beb5SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 335cb23beb5SChristoph Hellwig * 336cb23beb5SChristoph Hellwig * Used to check for read/write/execute permissions on an inode. 337cb23beb5SChristoph Hellwig * We use "fsuid" for this, letting us set arbitrary permissions 338cb23beb5SChristoph Hellwig * for filesystem access without changing the "normal" uids which 339cb23beb5SChristoph Hellwig * are used for other things. 340cb23beb5SChristoph Hellwig */ 341f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask) 3421da177e4SLinus Torvalds { 343e6305c43SAl Viro int retval; 3441da177e4SLinus Torvalds 3453ddcd056SLinus Torvalds if (unlikely(mask & MAY_WRITE)) { 34622590e41SMiklos Szeredi umode_t mode = inode->i_mode; 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds /* 3491da177e4SLinus Torvalds * Nobody gets write access to a read-only fs. 3501da177e4SLinus Torvalds */ 3511da177e4SLinus Torvalds if (IS_RDONLY(inode) && 3521da177e4SLinus Torvalds (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 3531da177e4SLinus Torvalds return -EROFS; 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds /* 3561da177e4SLinus Torvalds * Nobody gets write access to an immutable file. 3571da177e4SLinus Torvalds */ 3581da177e4SLinus Torvalds if (IS_IMMUTABLE(inode)) 3591da177e4SLinus Torvalds return -EACCES; 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds 3623ddcd056SLinus Torvalds retval = do_inode_permission(inode, mask); 3631da177e4SLinus Torvalds if (retval) 3641da177e4SLinus Torvalds return retval; 3651da177e4SLinus Torvalds 36608ce5f16SSerge E. Hallyn retval = devcgroup_inode_permission(inode, mask); 36708ce5f16SSerge E. Hallyn if (retval) 36808ce5f16SSerge E. Hallyn return retval; 36908ce5f16SSerge E. Hallyn 370d09ca739SEric Paris return security_inode_permission(inode, mask); 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds 373f4d6ff89SAl Viro /** 3745dd784d0SJan Blunck * path_get - get a reference to a path 3755dd784d0SJan Blunck * @path: path to get the reference to 3765dd784d0SJan Blunck * 3775dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 3785dd784d0SJan Blunck */ 3795dd784d0SJan Blunck void path_get(struct path *path) 3805dd784d0SJan Blunck { 3815dd784d0SJan Blunck mntget(path->mnt); 3825dd784d0SJan Blunck dget(path->dentry); 3835dd784d0SJan Blunck } 3845dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 3855dd784d0SJan Blunck 3865dd784d0SJan Blunck /** 3871d957f9bSJan Blunck * path_put - put a reference to a path 3881d957f9bSJan Blunck * @path: path to put the reference to 3891d957f9bSJan Blunck * 3901d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 3911d957f9bSJan Blunck */ 3921d957f9bSJan Blunck void path_put(struct path *path) 3931da177e4SLinus Torvalds { 3941d957f9bSJan Blunck dput(path->dentry); 3951d957f9bSJan Blunck mntput(path->mnt); 3961da177e4SLinus Torvalds } 3971d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 3981da177e4SLinus Torvalds 39919660af7SAl Viro /* 40031e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 40119660af7SAl Viro * Documentation/filesystems/path-lookup.txt). In situations when we can't 40219660af7SAl Viro * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab 40319660af7SAl Viro * normal reference counts on dentries and vfsmounts to transition to rcu-walk 40419660af7SAl Viro * mode. Refcounts are grabbed at the last known good point before rcu-walk 40519660af7SAl Viro * got stuck, so ref-walk may continue from there. If this is not successful 40619660af7SAl Viro * (eg. a seqcount has changed), then failure is returned and it's up to caller 40719660af7SAl Viro * to restart the path walk from the beginning in ref-walk mode. 40831e6b01fSNick Piggin */ 40931e6b01fSNick Piggin 41031e6b01fSNick Piggin /** 41119660af7SAl Viro * unlazy_walk - try to switch to ref-walk mode. 41219660af7SAl Viro * @nd: nameidata pathwalk data 41319660af7SAl Viro * @dentry: child of nd->path.dentry or NULL 41439191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 41531e6b01fSNick Piggin * 41619660af7SAl Viro * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry 41719660af7SAl Viro * for ref-walk mode. @dentry must be a path found by a do_lookup call on 41819660af7SAl Viro * @nd or NULL. Must be called from rcu-walk context. 41931e6b01fSNick Piggin */ 42019660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry) 42131e6b01fSNick Piggin { 42231e6b01fSNick Piggin struct fs_struct *fs = current->fs; 42331e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 4245b6ca027SAl Viro int want_root = 0; 42531e6b01fSNick Piggin 42631e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 4275b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 4285b6ca027SAl Viro want_root = 1; 42931e6b01fSNick Piggin spin_lock(&fs->lock); 43031e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 43131e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 43231e6b01fSNick Piggin goto err_root; 43331e6b01fSNick Piggin } 43431e6b01fSNick Piggin spin_lock(&parent->d_lock); 43519660af7SAl Viro if (!dentry) { 43619660af7SAl Viro if (!__d_rcu_to_refcount(parent, nd->seq)) 43719660af7SAl Viro goto err_parent; 43819660af7SAl Viro BUG_ON(nd->inode != parent->d_inode); 43919660af7SAl Viro } else { 44094c0d4ecSAl Viro if (dentry->d_parent != parent) 44194c0d4ecSAl Viro goto err_parent; 44231e6b01fSNick Piggin spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); 44331e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 44419660af7SAl Viro goto err_child; 44531e6b01fSNick Piggin /* 44619660af7SAl Viro * If the sequence check on the child dentry passed, then 44719660af7SAl Viro * the child has not been removed from its parent. This 44819660af7SAl Viro * means the parent dentry must be valid and able to take 44919660af7SAl Viro * a reference at this point. 45031e6b01fSNick Piggin */ 45131e6b01fSNick Piggin BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); 45231e6b01fSNick Piggin BUG_ON(!parent->d_count); 45331e6b01fSNick Piggin parent->d_count++; 45431e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 45519660af7SAl Viro } 45631e6b01fSNick Piggin spin_unlock(&parent->d_lock); 4575b6ca027SAl Viro if (want_root) { 45831e6b01fSNick Piggin path_get(&nd->root); 45931e6b01fSNick Piggin spin_unlock(&fs->lock); 46031e6b01fSNick Piggin } 46131e6b01fSNick Piggin mntget(nd->path.mnt); 46231e6b01fSNick Piggin 46331e6b01fSNick Piggin rcu_read_unlock(); 46431e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 46531e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 46631e6b01fSNick Piggin return 0; 46719660af7SAl Viro 46819660af7SAl Viro err_child: 46931e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 47019660af7SAl Viro err_parent: 47131e6b01fSNick Piggin spin_unlock(&parent->d_lock); 47231e6b01fSNick Piggin err_root: 4735b6ca027SAl Viro if (want_root) 47431e6b01fSNick Piggin spin_unlock(&fs->lock); 47531e6b01fSNick Piggin return -ECHILD; 47631e6b01fSNick Piggin } 47731e6b01fSNick Piggin 47831e6b01fSNick Piggin /** 479834f2a4aSTrond Myklebust * release_open_intent - free up open intent resources 480834f2a4aSTrond Myklebust * @nd: pointer to nameidata 481834f2a4aSTrond Myklebust */ 482834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd) 483834f2a4aSTrond Myklebust { 4842dab5974SLinus Torvalds struct file *file = nd->intent.open.file; 4852dab5974SLinus Torvalds 4862dab5974SLinus Torvalds if (file && !IS_ERR(file)) { 4872dab5974SLinus Torvalds if (file->f_path.dentry == NULL) 4882dab5974SLinus Torvalds put_filp(file); 489834f2a4aSTrond Myklebust else 4902dab5974SLinus Torvalds fput(file); 4912dab5974SLinus Torvalds } 492834f2a4aSTrond Myklebust } 493834f2a4aSTrond Myklebust 494f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd) 49534286d66SNick Piggin { 496f60aef7eSAl Viro return dentry->d_op->d_revalidate(dentry, nd); 49734286d66SNick Piggin } 49834286d66SNick Piggin 4999f1fafeeSAl Viro /** 5009f1fafeeSAl Viro * complete_walk - successful completion of path walk 5019f1fafeeSAl Viro * @nd: pointer nameidata 50239159de2SJeff Layton * 5039f1fafeeSAl Viro * If we had been in RCU mode, drop out of it and legitimize nd->path. 5049f1fafeeSAl Viro * Revalidate the final result, unless we'd already done that during 5059f1fafeeSAl Viro * the path walk or the filesystem doesn't ask for it. Return 0 on 5069f1fafeeSAl Viro * success, -error on failure. In case of failure caller does not 5079f1fafeeSAl Viro * need to drop nd->path. 50839159de2SJeff Layton */ 5099f1fafeeSAl Viro static int complete_walk(struct nameidata *nd) 51039159de2SJeff Layton { 51116c2cd71SAl Viro struct dentry *dentry = nd->path.dentry; 51239159de2SJeff Layton int status; 51339159de2SJeff Layton 5149f1fafeeSAl Viro if (nd->flags & LOOKUP_RCU) { 5159f1fafeeSAl Viro nd->flags &= ~LOOKUP_RCU; 5169f1fafeeSAl Viro if (!(nd->flags & LOOKUP_ROOT)) 5179f1fafeeSAl Viro nd->root.mnt = NULL; 5189f1fafeeSAl Viro spin_lock(&dentry->d_lock); 5199f1fafeeSAl Viro if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) { 5209f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 5219f1fafeeSAl Viro rcu_read_unlock(); 5229f1fafeeSAl Viro br_read_unlock(vfsmount_lock); 5239f1fafeeSAl Viro return -ECHILD; 5249f1fafeeSAl Viro } 5259f1fafeeSAl Viro BUG_ON(nd->inode != dentry->d_inode); 5269f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 5279f1fafeeSAl Viro mntget(nd->path.mnt); 5289f1fafeeSAl Viro rcu_read_unlock(); 5299f1fafeeSAl Viro br_read_unlock(vfsmount_lock); 5309f1fafeeSAl Viro } 5319f1fafeeSAl Viro 53216c2cd71SAl Viro if (likely(!(nd->flags & LOOKUP_JUMPED))) 53339159de2SJeff Layton return 0; 53439159de2SJeff Layton 53516c2cd71SAl Viro if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE))) 53616c2cd71SAl Viro return 0; 53716c2cd71SAl Viro 53816c2cd71SAl Viro if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))) 53916c2cd71SAl Viro return 0; 54016c2cd71SAl Viro 54116c2cd71SAl Viro /* Note: we do not d_invalidate() */ 54234286d66SNick Piggin status = d_revalidate(dentry, nd); 54339159de2SJeff Layton if (status > 0) 54439159de2SJeff Layton return 0; 54539159de2SJeff Layton 54616c2cd71SAl Viro if (!status) 54739159de2SJeff Layton status = -ESTALE; 54816c2cd71SAl Viro 5499f1fafeeSAl Viro path_put(&nd->path); 55039159de2SJeff Layton return status; 55139159de2SJeff Layton } 55239159de2SJeff Layton 5532a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 5542a737871SAl Viro { 555f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 556f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 5572a737871SAl Viro } 5582a737871SAl Viro 5596de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 5606de88d72SAl Viro 56131e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 56231e6b01fSNick Piggin { 56331e6b01fSNick Piggin if (!nd->root.mnt) { 56431e6b01fSNick Piggin struct fs_struct *fs = current->fs; 565c28cc364SNick Piggin unsigned seq; 566c28cc364SNick Piggin 567c28cc364SNick Piggin do { 568c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 56931e6b01fSNick Piggin nd->root = fs->root; 570c1530019STim Chen nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq); 571c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 57231e6b01fSNick Piggin } 57331e6b01fSNick Piggin } 57431e6b01fSNick Piggin 575f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 5761da177e4SLinus Torvalds { 57731e6b01fSNick Piggin int ret; 57831e6b01fSNick Piggin 5791da177e4SLinus Torvalds if (IS_ERR(link)) 5801da177e4SLinus Torvalds goto fail; 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds if (*link == '/') { 5832a737871SAl Viro set_root(nd); 5841d957f9bSJan Blunck path_put(&nd->path); 5852a737871SAl Viro nd->path = nd->root; 5862a737871SAl Viro path_get(&nd->root); 58716c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 5881da177e4SLinus Torvalds } 58931e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 590b4091d5fSChristoph Hellwig 59131e6b01fSNick Piggin ret = link_path_walk(link, nd); 59231e6b01fSNick Piggin return ret; 5931da177e4SLinus Torvalds fail: 5941d957f9bSJan Blunck path_put(&nd->path); 5951da177e4SLinus Torvalds return PTR_ERR(link); 5961da177e4SLinus Torvalds } 5971da177e4SLinus Torvalds 5981d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 599051d3812SIan Kent { 600051d3812SIan Kent dput(path->dentry); 6014ac91378SJan Blunck if (path->mnt != nd->path.mnt) 602051d3812SIan Kent mntput(path->mnt); 603051d3812SIan Kent } 604051d3812SIan Kent 6057b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path, 6067b9337aaSNick Piggin struct nameidata *nd) 607051d3812SIan Kent { 60831e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 6094ac91378SJan Blunck dput(nd->path.dentry); 61031e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 6114ac91378SJan Blunck mntput(nd->path.mnt); 6129a229683SHuang Shijie } 61331e6b01fSNick Piggin nd->path.mnt = path->mnt; 6144ac91378SJan Blunck nd->path.dentry = path->dentry; 615051d3812SIan Kent } 616051d3812SIan Kent 617574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie) 618574197e0SAl Viro { 619574197e0SAl Viro struct inode *inode = link->dentry->d_inode; 620574197e0SAl Viro if (!IS_ERR(cookie) && inode->i_op->put_link) 621574197e0SAl Viro inode->i_op->put_link(link->dentry, nd, cookie); 622574197e0SAl Viro path_put(link); 623574197e0SAl Viro } 624574197e0SAl Viro 625def4af30SAl Viro static __always_inline int 626574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p) 6271da177e4SLinus Torvalds { 6281da177e4SLinus Torvalds int error; 6297b9337aaSNick Piggin struct dentry *dentry = link->dentry; 6301da177e4SLinus Torvalds 631844a3917SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 632844a3917SAl Viro 6330e794589SAl Viro if (link->mnt == nd->path.mnt) 6340e794589SAl Viro mntget(link->mnt); 6350e794589SAl Viro 636574197e0SAl Viro if (unlikely(current->total_link_count >= 40)) { 637574197e0SAl Viro *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */ 638574197e0SAl Viro path_put(&nd->path); 639574197e0SAl Viro return -ELOOP; 640574197e0SAl Viro } 641574197e0SAl Viro cond_resched(); 642574197e0SAl Viro current->total_link_count++; 643574197e0SAl Viro 6447b9337aaSNick Piggin touch_atime(link->mnt, dentry); 6451da177e4SLinus Torvalds nd_set_link(nd, NULL); 646cd4e91d3SAl Viro 64736f3b4f6SAl Viro error = security_inode_follow_link(link->dentry, nd); 64836f3b4f6SAl Viro if (error) { 64936f3b4f6SAl Viro *p = ERR_PTR(error); /* no ->put_link(), please */ 65036f3b4f6SAl Viro path_put(&nd->path); 65136f3b4f6SAl Viro return error; 65236f3b4f6SAl Viro } 65336f3b4f6SAl Viro 65486acdca1SAl Viro nd->last_type = LAST_BIND; 655def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 656def4af30SAl Viro error = PTR_ERR(*p); 657def4af30SAl Viro if (!IS_ERR(*p)) { 6581da177e4SLinus Torvalds char *s = nd_get_link(nd); 659cc314eefSLinus Torvalds error = 0; 6601da177e4SLinus Torvalds if (s) 6611da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 662bcda7652SAl Viro else if (nd->last_type == LAST_BIND) { 66316c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 664b21041d0SAl Viro nd->inode = nd->path.dentry->d_inode; 665b21041d0SAl Viro if (nd->inode->i_op->follow_link) { 666bcda7652SAl Viro /* stepped on a _really_ weird one */ 667bcda7652SAl Viro path_put(&nd->path); 668bcda7652SAl Viro error = -ELOOP; 669bcda7652SAl Viro } 670bcda7652SAl Viro } 6711da177e4SLinus Torvalds } 6721da177e4SLinus Torvalds return error; 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds 67531e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 67631e6b01fSNick Piggin { 67731e6b01fSNick Piggin struct vfsmount *parent; 67831e6b01fSNick Piggin struct dentry *mountpoint; 67931e6b01fSNick Piggin 68031e6b01fSNick Piggin parent = path->mnt->mnt_parent; 68131e6b01fSNick Piggin if (parent == path->mnt) 68231e6b01fSNick Piggin return 0; 68331e6b01fSNick Piggin mountpoint = path->mnt->mnt_mountpoint; 68431e6b01fSNick Piggin path->dentry = mountpoint; 68531e6b01fSNick Piggin path->mnt = parent; 68631e6b01fSNick Piggin return 1; 68731e6b01fSNick Piggin } 68831e6b01fSNick Piggin 689bab77ebfSAl Viro int follow_up(struct path *path) 6901da177e4SLinus Torvalds { 6911da177e4SLinus Torvalds struct vfsmount *parent; 6921da177e4SLinus Torvalds struct dentry *mountpoint; 69399b7db7bSNick Piggin 69499b7db7bSNick Piggin br_read_lock(vfsmount_lock); 695bab77ebfSAl Viro parent = path->mnt->mnt_parent; 696bab77ebfSAl Viro if (parent == path->mnt) { 69799b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 6981da177e4SLinus Torvalds return 0; 6991da177e4SLinus Torvalds } 7001da177e4SLinus Torvalds mntget(parent); 701bab77ebfSAl Viro mountpoint = dget(path->mnt->mnt_mountpoint); 70299b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 703bab77ebfSAl Viro dput(path->dentry); 704bab77ebfSAl Viro path->dentry = mountpoint; 705bab77ebfSAl Viro mntput(path->mnt); 706bab77ebfSAl Viro path->mnt = parent; 7071da177e4SLinus Torvalds return 1; 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds 710b5c84bf6SNick Piggin /* 7119875cf80SDavid Howells * Perform an automount 7129875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 7139875cf80SDavid Howells * were called with. 7141da177e4SLinus Torvalds */ 7159875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags, 7169875cf80SDavid Howells bool *need_mntput) 71731e6b01fSNick Piggin { 7189875cf80SDavid Howells struct vfsmount *mnt; 719ea5b778aSDavid Howells int err; 7209875cf80SDavid Howells 7219875cf80SDavid Howells if (!path->dentry->d_op || !path->dentry->d_op->d_automount) 7229875cf80SDavid Howells return -EREMOTE; 7239875cf80SDavid Howells 7246f45b656SDavid Howells /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT 7256f45b656SDavid Howells * and this is the terminal part of the path. 7266f45b656SDavid Howells */ 72749084c3bSAl Viro if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_PARENT)) 7286f45b656SDavid Howells return -EISDIR; /* we actually want to stop here */ 7296f45b656SDavid Howells 7305a30d8a2SDavid Howells /* 7319875cf80SDavid Howells * We don't want to mount if someone's just doing a stat and they've 7329875cf80SDavid Howells * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and 7339875cf80SDavid Howells * appended a '/' to the name. 7349875cf80SDavid Howells */ 7355a30d8a2SDavid Howells if (!(flags & LOOKUP_FOLLOW)) { 7365a30d8a2SDavid Howells /* We do, however, want to mount if someone wants to open or 7375a30d8a2SDavid Howells * create a file of any type under the mountpoint, wants to 7385a30d8a2SDavid Howells * traverse through the mountpoint or wants to open the mounted 7395a30d8a2SDavid Howells * directory. 7405a30d8a2SDavid Howells * Also, autofs may mark negative dentries as being automount 7415a30d8a2SDavid Howells * points. These will need the attentions of the daemon to 7425a30d8a2SDavid Howells * instantiate them before they can be used. 7435a30d8a2SDavid Howells */ 7445a30d8a2SDavid Howells if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | 7455a30d8a2SDavid Howells LOOKUP_OPEN | LOOKUP_CREATE)) && 7465a30d8a2SDavid Howells path->dentry->d_inode) 7479875cf80SDavid Howells return -EISDIR; 7485a30d8a2SDavid Howells } 7499875cf80SDavid Howells current->total_link_count++; 7509875cf80SDavid Howells if (current->total_link_count >= 40) 7519875cf80SDavid Howells return -ELOOP; 7529875cf80SDavid Howells 7539875cf80SDavid Howells mnt = path->dentry->d_op->d_automount(path); 7549875cf80SDavid Howells if (IS_ERR(mnt)) { 7559875cf80SDavid Howells /* 7569875cf80SDavid Howells * The filesystem is allowed to return -EISDIR here to indicate 7579875cf80SDavid Howells * it doesn't want to automount. For instance, autofs would do 7589875cf80SDavid Howells * this so that its userspace daemon can mount on this dentry. 7599875cf80SDavid Howells * 7609875cf80SDavid Howells * However, we can only permit this if it's a terminal point in 7619875cf80SDavid Howells * the path being looked up; if it wasn't then the remainder of 7629875cf80SDavid Howells * the path is inaccessible and we should say so. 7639875cf80SDavid Howells */ 76449084c3bSAl Viro if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT)) 7659875cf80SDavid Howells return -EREMOTE; 7669875cf80SDavid Howells return PTR_ERR(mnt); 76731e6b01fSNick Piggin } 768ea5b778aSDavid Howells 7699875cf80SDavid Howells if (!mnt) /* mount collision */ 7709875cf80SDavid Howells return 0; 7719875cf80SDavid Howells 7728aef1884SAl Viro if (!*need_mntput) { 7738aef1884SAl Viro /* lock_mount() may release path->mnt on error */ 7748aef1884SAl Viro mntget(path->mnt); 7758aef1884SAl Viro *need_mntput = true; 7768aef1884SAl Viro } 77719a167afSAl Viro err = finish_automount(mnt, path); 778ea5b778aSDavid Howells 779ea5b778aSDavid Howells switch (err) { 780ea5b778aSDavid Howells case -EBUSY: 781ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 78219a167afSAl Viro return 0; 783ea5b778aSDavid Howells case 0: 7848aef1884SAl Viro path_put(path); 7859875cf80SDavid Howells path->mnt = mnt; 7869875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 7879875cf80SDavid Howells return 0; 78819a167afSAl Viro default: 78919a167afSAl Viro return err; 7909875cf80SDavid Howells } 79119a167afSAl Viro 792ea5b778aSDavid Howells } 7939875cf80SDavid Howells 7949875cf80SDavid Howells /* 7959875cf80SDavid Howells * Handle a dentry that is managed in some way. 796cc53ce53SDavid Howells * - Flagged for transit management (autofs) 7979875cf80SDavid Howells * - Flagged as mountpoint 7989875cf80SDavid Howells * - Flagged as automount point 7999875cf80SDavid Howells * 8009875cf80SDavid Howells * This may only be called in refwalk mode. 8019875cf80SDavid Howells * 8029875cf80SDavid Howells * Serialization is taken care of in namespace.c 8039875cf80SDavid Howells */ 8049875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 8059875cf80SDavid Howells { 8068aef1884SAl Viro struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */ 8079875cf80SDavid Howells unsigned managed; 8089875cf80SDavid Howells bool need_mntput = false; 8098aef1884SAl Viro int ret = 0; 8109875cf80SDavid Howells 8119875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 8129875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 8139875cf80SDavid Howells * the components of that value change under us */ 8149875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 8159875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 8169875cf80SDavid Howells unlikely(managed != 0)) { 817cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 818cc53ce53SDavid Howells * being held. */ 819cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 820cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 821cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 8221aed3e42SAl Viro ret = path->dentry->d_op->d_manage(path->dentry, false); 823cc53ce53SDavid Howells if (ret < 0) 8248aef1884SAl Viro break; 825cc53ce53SDavid Howells } 826cc53ce53SDavid Howells 8279875cf80SDavid Howells /* Transit to a mounted filesystem. */ 8289875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 8299875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 8309875cf80SDavid Howells if (mounted) { 8319875cf80SDavid Howells dput(path->dentry); 8329875cf80SDavid Howells if (need_mntput) 833463ffb2eSAl Viro mntput(path->mnt); 834463ffb2eSAl Viro path->mnt = mounted; 835463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 8369875cf80SDavid Howells need_mntput = true; 8379875cf80SDavid Howells continue; 838463ffb2eSAl Viro } 839463ffb2eSAl Viro 8409875cf80SDavid Howells /* Something is mounted on this dentry in another 8419875cf80SDavid Howells * namespace and/or whatever was mounted there in this 8429875cf80SDavid Howells * namespace got unmounted before we managed to get the 8439875cf80SDavid Howells * vfsmount_lock */ 8441da177e4SLinus Torvalds } 8459875cf80SDavid Howells 8469875cf80SDavid Howells /* Handle an automount point */ 8479875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 8489875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 8499875cf80SDavid Howells if (ret < 0) 8508aef1884SAl Viro break; 8519875cf80SDavid Howells continue; 8529875cf80SDavid Howells } 8539875cf80SDavid Howells 8549875cf80SDavid Howells /* We didn't change the current path point */ 8559875cf80SDavid Howells break; 8569875cf80SDavid Howells } 8578aef1884SAl Viro 8588aef1884SAl Viro if (need_mntput && path->mnt == mnt) 8598aef1884SAl Viro mntput(path->mnt); 8608aef1884SAl Viro if (ret == -EISDIR) 8618aef1884SAl Viro ret = 0; 8628aef1884SAl Viro return ret; 8631da177e4SLinus Torvalds } 8641da177e4SLinus Torvalds 865cc53ce53SDavid Howells int follow_down_one(struct path *path) 8661da177e4SLinus Torvalds { 8671da177e4SLinus Torvalds struct vfsmount *mounted; 8681da177e4SLinus Torvalds 8691c755af4SAl Viro mounted = lookup_mnt(path); 8701da177e4SLinus Torvalds if (mounted) { 8719393bd07SAl Viro dput(path->dentry); 8729393bd07SAl Viro mntput(path->mnt); 8739393bd07SAl Viro path->mnt = mounted; 8749393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 8751da177e4SLinus Torvalds return 1; 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds return 0; 8781da177e4SLinus Torvalds } 8791da177e4SLinus Torvalds 88062a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry) 88162a7375eSIan Kent { 88262a7375eSIan Kent return (dentry->d_flags & DCACHE_MANAGE_TRANSIT && 88362a7375eSIan Kent dentry->d_op->d_manage(dentry, true) < 0); 88462a7375eSIan Kent } 88562a7375eSIan Kent 8869875cf80SDavid Howells /* 887287548e4SAl Viro * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if 888287548e4SAl Viro * we meet a managed dentry that would need blocking. 8899875cf80SDavid Howells */ 8909875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 891287548e4SAl Viro struct inode **inode) 8929875cf80SDavid Howells { 89362a7375eSIan Kent for (;;) { 8949875cf80SDavid Howells struct vfsmount *mounted; 89562a7375eSIan Kent /* 89662a7375eSIan Kent * Don't forget we might have a non-mountpoint managed dentry 89762a7375eSIan Kent * that wants to block transit. 89862a7375eSIan Kent */ 899287548e4SAl Viro if (unlikely(managed_dentry_might_block(path->dentry))) 900ab90911fSDavid Howells return false; 90162a7375eSIan Kent 90262a7375eSIan Kent if (!d_mountpoint(path->dentry)) 90362a7375eSIan Kent break; 90462a7375eSIan Kent 9059875cf80SDavid Howells mounted = __lookup_mnt(path->mnt, path->dentry, 1); 9069875cf80SDavid Howells if (!mounted) 9079875cf80SDavid Howells break; 9089875cf80SDavid Howells path->mnt = mounted; 9099875cf80SDavid Howells path->dentry = mounted->mnt_root; 9109875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 91159430262SLinus Torvalds /* 91259430262SLinus Torvalds * Update the inode too. We don't need to re-check the 91359430262SLinus Torvalds * dentry sequence number here after this d_inode read, 91459430262SLinus Torvalds * because a mount-point is always pinned. 91559430262SLinus Torvalds */ 91659430262SLinus Torvalds *inode = path->dentry->d_inode; 9179875cf80SDavid Howells } 9189875cf80SDavid Howells return true; 9199875cf80SDavid Howells } 9209875cf80SDavid Howells 921dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd) 922287548e4SAl Viro { 923dea39376SAl Viro while (d_mountpoint(nd->path.dentry)) { 924287548e4SAl Viro struct vfsmount *mounted; 925dea39376SAl Viro mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1); 926287548e4SAl Viro if (!mounted) 927287548e4SAl Viro break; 928dea39376SAl Viro nd->path.mnt = mounted; 929dea39376SAl Viro nd->path.dentry = mounted->mnt_root; 930dea39376SAl Viro nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 931287548e4SAl Viro } 932287548e4SAl Viro } 933287548e4SAl Viro 93431e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 93531e6b01fSNick Piggin { 93631e6b01fSNick Piggin set_root_rcu(nd); 93731e6b01fSNick Piggin 93831e6b01fSNick Piggin while (1) { 93931e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 94031e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 94131e6b01fSNick Piggin break; 94231e6b01fSNick Piggin } 94331e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 94431e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 94531e6b01fSNick Piggin struct dentry *parent = old->d_parent; 94631e6b01fSNick Piggin unsigned seq; 94731e6b01fSNick Piggin 94831e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 94931e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 950ef7562d5SAl Viro goto failed; 95131e6b01fSNick Piggin nd->path.dentry = parent; 95231e6b01fSNick Piggin nd->seq = seq; 95331e6b01fSNick Piggin break; 95431e6b01fSNick Piggin } 95531e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 95631e6b01fSNick Piggin break; 95731e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 95831e6b01fSNick Piggin } 959dea39376SAl Viro follow_mount_rcu(nd); 960dea39376SAl Viro nd->inode = nd->path.dentry->d_inode; 96131e6b01fSNick Piggin return 0; 962ef7562d5SAl Viro 963ef7562d5SAl Viro failed: 964ef7562d5SAl Viro nd->flags &= ~LOOKUP_RCU; 9655b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 966ef7562d5SAl Viro nd->root.mnt = NULL; 967ef7562d5SAl Viro rcu_read_unlock(); 968ef7562d5SAl Viro br_read_unlock(vfsmount_lock); 969ef7562d5SAl Viro return -ECHILD; 97031e6b01fSNick Piggin } 97131e6b01fSNick Piggin 9729875cf80SDavid Howells /* 973cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 974cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 975cc53ce53SDavid Howells * caller is permitted to proceed or not. 976cc53ce53SDavid Howells */ 9777cc90cc3SAl Viro int follow_down(struct path *path) 978cc53ce53SDavid Howells { 979cc53ce53SDavid Howells unsigned managed; 980cc53ce53SDavid Howells int ret; 981cc53ce53SDavid Howells 982cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 983cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 984cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 985cc53ce53SDavid Howells * being held. 986cc53ce53SDavid Howells * 987cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 988cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 989cc53ce53SDavid Howells * other than its daemon the right to mount on its 990cc53ce53SDavid Howells * superstructure. 991cc53ce53SDavid Howells * 992cc53ce53SDavid Howells * The filesystem may sleep at this point. 993cc53ce53SDavid Howells */ 994cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 995cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 996cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 997ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 9981aed3e42SAl Viro path->dentry, false); 999cc53ce53SDavid Howells if (ret < 0) 1000cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 1001cc53ce53SDavid Howells } 1002cc53ce53SDavid Howells 1003cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 1004cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 1005cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 1006cc53ce53SDavid Howells if (!mounted) 1007cc53ce53SDavid Howells break; 1008cc53ce53SDavid Howells dput(path->dentry); 1009cc53ce53SDavid Howells mntput(path->mnt); 1010cc53ce53SDavid Howells path->mnt = mounted; 1011cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 1012cc53ce53SDavid Howells continue; 1013cc53ce53SDavid Howells } 1014cc53ce53SDavid Howells 1015cc53ce53SDavid Howells /* Don't handle automount points here */ 1016cc53ce53SDavid Howells break; 1017cc53ce53SDavid Howells } 1018cc53ce53SDavid Howells return 0; 1019cc53ce53SDavid Howells } 1020cc53ce53SDavid Howells 1021cc53ce53SDavid Howells /* 10229875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 10239875cf80SDavid Howells */ 10249875cf80SDavid Howells static void follow_mount(struct path *path) 10259875cf80SDavid Howells { 10269875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 10279875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 10289875cf80SDavid Howells if (!mounted) 10299875cf80SDavid Howells break; 10309875cf80SDavid Howells dput(path->dentry); 10319875cf80SDavid Howells mntput(path->mnt); 10329875cf80SDavid Howells path->mnt = mounted; 10339875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 10349875cf80SDavid Howells } 10359875cf80SDavid Howells } 10369875cf80SDavid Howells 103731e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 10381da177e4SLinus Torvalds { 10392a737871SAl Viro set_root(nd); 1040e518ddb7SAndreas Mohr 10411da177e4SLinus Torvalds while(1) { 10424ac91378SJan Blunck struct dentry *old = nd->path.dentry; 10431da177e4SLinus Torvalds 10442a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 10452a737871SAl Viro nd->path.mnt == nd->root.mnt) { 10461da177e4SLinus Torvalds break; 10471da177e4SLinus Torvalds } 10484ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 10493088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 10503088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 10511da177e4SLinus Torvalds dput(old); 10521da177e4SLinus Torvalds break; 10531da177e4SLinus Torvalds } 10543088dd70SAl Viro if (!follow_up(&nd->path)) 10551da177e4SLinus Torvalds break; 10561da177e4SLinus Torvalds } 105779ed0226SAl Viro follow_mount(&nd->path); 105831e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 10591da177e4SLinus Torvalds } 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds /* 1062baa03890SNick Piggin * Allocate a dentry with name and parent, and perform a parent 1063baa03890SNick Piggin * directory ->lookup on it. Returns the new dentry, or ERR_PTR 1064baa03890SNick Piggin * on error. parent->d_inode->i_mutex must be held. d_lookup must 1065baa03890SNick Piggin * have verified that no child exists while under i_mutex. 1066baa03890SNick Piggin */ 1067baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent, 1068baa03890SNick Piggin struct qstr *name, struct nameidata *nd) 1069baa03890SNick Piggin { 1070baa03890SNick Piggin struct inode *inode = parent->d_inode; 1071baa03890SNick Piggin struct dentry *dentry; 1072baa03890SNick Piggin struct dentry *old; 1073baa03890SNick Piggin 1074baa03890SNick Piggin /* Don't create child dentry for a dead directory. */ 1075baa03890SNick Piggin if (unlikely(IS_DEADDIR(inode))) 1076baa03890SNick Piggin return ERR_PTR(-ENOENT); 1077baa03890SNick Piggin 1078baa03890SNick Piggin dentry = d_alloc(parent, name); 1079baa03890SNick Piggin if (unlikely(!dentry)) 1080baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1081baa03890SNick Piggin 1082baa03890SNick Piggin old = inode->i_op->lookup(inode, dentry, nd); 1083baa03890SNick Piggin if (unlikely(old)) { 1084baa03890SNick Piggin dput(dentry); 1085baa03890SNick Piggin dentry = old; 1086baa03890SNick Piggin } 1087baa03890SNick Piggin return dentry; 1088baa03890SNick Piggin } 1089baa03890SNick Piggin 1090baa03890SNick Piggin /* 109144396f4bSJosef Bacik * We already have a dentry, but require a lookup to be performed on the parent 109244396f4bSJosef Bacik * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error. 109344396f4bSJosef Bacik * parent->d_inode->i_mutex must be held. d_lookup must have verified that no 109444396f4bSJosef Bacik * child exists while under i_mutex. 109544396f4bSJosef Bacik */ 109644396f4bSJosef Bacik static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry, 109744396f4bSJosef Bacik struct nameidata *nd) 109844396f4bSJosef Bacik { 109944396f4bSJosef Bacik struct inode *inode = parent->d_inode; 110044396f4bSJosef Bacik struct dentry *old; 110144396f4bSJosef Bacik 110244396f4bSJosef Bacik /* Don't create child dentry for a dead directory. */ 110344396f4bSJosef Bacik if (unlikely(IS_DEADDIR(inode))) 110444396f4bSJosef Bacik return ERR_PTR(-ENOENT); 110544396f4bSJosef Bacik 110644396f4bSJosef Bacik old = inode->i_op->lookup(inode, dentry, nd); 110744396f4bSJosef Bacik if (unlikely(old)) { 110844396f4bSJosef Bacik dput(dentry); 110944396f4bSJosef Bacik dentry = old; 111044396f4bSJosef Bacik } 111144396f4bSJosef Bacik return dentry; 111244396f4bSJosef Bacik } 111344396f4bSJosef Bacik 111444396f4bSJosef Bacik /* 11151da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 11161da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 11171da177e4SLinus Torvalds * It _is_ time-critical. 11181da177e4SLinus Torvalds */ 11191da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name, 112031e6b01fSNick Piggin struct path *path, struct inode **inode) 11211da177e4SLinus Torvalds { 11224ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 112331e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 11245a18fff2SAl Viro int need_reval = 1; 11255a18fff2SAl Viro int status = 1; 11269875cf80SDavid Howells int err; 11279875cf80SDavid Howells 11283cac260aSAl Viro /* 1129b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1130b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1131b04f784eSNick Piggin * do the non-racy lookup, below. 1132b04f784eSNick Piggin */ 113331e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 113431e6b01fSNick Piggin unsigned seq; 113531e6b01fSNick Piggin *inode = nd->inode; 113631e6b01fSNick Piggin dentry = __d_lookup_rcu(parent, name, &seq, inode); 11375a18fff2SAl Viro if (!dentry) 11385a18fff2SAl Viro goto unlazy; 11395a18fff2SAl Viro 114031e6b01fSNick Piggin /* Memory barrier in read_seqcount_begin of child is enough */ 114131e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 114231e6b01fSNick Piggin return -ECHILD; 114331e6b01fSNick Piggin nd->seq = seq; 11445a18fff2SAl Viro 114524643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 11465a18fff2SAl Viro status = d_revalidate(dentry, nd); 11475a18fff2SAl Viro if (unlikely(status <= 0)) { 11485a18fff2SAl Viro if (status != -ECHILD) 11495a18fff2SAl Viro need_reval = 0; 11505a18fff2SAl Viro goto unlazy; 11515a18fff2SAl Viro } 115224643087SAl Viro } 115344396f4bSJosef Bacik if (unlikely(d_need_lookup(dentry))) 115444396f4bSJosef Bacik goto unlazy; 115531e6b01fSNick Piggin path->mnt = mnt; 115631e6b01fSNick Piggin path->dentry = dentry; 1157d6e9bd25SAl Viro if (unlikely(!__follow_mount_rcu(nd, path, inode))) 1158d6e9bd25SAl Viro goto unlazy; 1159d6e9bd25SAl Viro if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 1160d6e9bd25SAl Viro goto unlazy; 11619875cf80SDavid Howells return 0; 11625a18fff2SAl Viro unlazy: 116319660af7SAl Viro if (unlazy_walk(nd, dentry)) 11645a18fff2SAl Viro return -ECHILD; 11655a18fff2SAl Viro } else { 116631e6b01fSNick Piggin dentry = __d_lookup(parent, name); 116724643087SAl Viro } 11685a18fff2SAl Viro 116944396f4bSJosef Bacik if (dentry && unlikely(d_need_lookup(dentry))) { 117044396f4bSJosef Bacik dput(dentry); 117144396f4bSJosef Bacik dentry = NULL; 117244396f4bSJosef Bacik } 11735a18fff2SAl Viro retry: 11745a18fff2SAl Viro if (unlikely(!dentry)) { 11755a18fff2SAl Viro struct inode *dir = parent->d_inode; 11765a18fff2SAl Viro BUG_ON(nd->inode != dir); 11775a18fff2SAl Viro 11785a18fff2SAl Viro mutex_lock(&dir->i_mutex); 11795a18fff2SAl Viro dentry = d_lookup(parent, name); 11805a18fff2SAl Viro if (likely(!dentry)) { 11815a18fff2SAl Viro dentry = d_alloc_and_lookup(parent, name, nd); 11825a18fff2SAl Viro if (IS_ERR(dentry)) { 11835a18fff2SAl Viro mutex_unlock(&dir->i_mutex); 11845a18fff2SAl Viro return PTR_ERR(dentry); 11855a18fff2SAl Viro } 11865a18fff2SAl Viro /* known good */ 11875a18fff2SAl Viro need_reval = 0; 11885a18fff2SAl Viro status = 1; 118944396f4bSJosef Bacik } else if (unlikely(d_need_lookup(dentry))) { 119044396f4bSJosef Bacik dentry = d_inode_lookup(parent, dentry, nd); 119144396f4bSJosef Bacik if (IS_ERR(dentry)) { 119244396f4bSJosef Bacik mutex_unlock(&dir->i_mutex); 119344396f4bSJosef Bacik return PTR_ERR(dentry); 119444396f4bSJosef Bacik } 119544396f4bSJosef Bacik /* known good */ 119644396f4bSJosef Bacik need_reval = 0; 119744396f4bSJosef Bacik status = 1; 11985a18fff2SAl Viro } 11995a18fff2SAl Viro mutex_unlock(&dir->i_mutex); 12005a18fff2SAl Viro } 12015a18fff2SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) 12025a18fff2SAl Viro status = d_revalidate(dentry, nd); 12035a18fff2SAl Viro if (unlikely(status <= 0)) { 12045a18fff2SAl Viro if (status < 0) { 12055a18fff2SAl Viro dput(dentry); 12065a18fff2SAl Viro return status; 12075a18fff2SAl Viro } 12085a18fff2SAl Viro if (!d_invalidate(dentry)) { 12095a18fff2SAl Viro dput(dentry); 12105a18fff2SAl Viro dentry = NULL; 12115a18fff2SAl Viro need_reval = 1; 12125a18fff2SAl Viro goto retry; 12135a18fff2SAl Viro } 12145a18fff2SAl Viro } 12155a18fff2SAl Viro 12161da177e4SLinus Torvalds path->mnt = mnt; 12171da177e4SLinus Torvalds path->dentry = dentry; 12189875cf80SDavid Howells err = follow_managed(path, nd->flags); 121989312214SIan Kent if (unlikely(err < 0)) { 122089312214SIan Kent path_put_conditional(path, nd); 12219875cf80SDavid Howells return err; 122289312214SIan Kent } 122331e6b01fSNick Piggin *inode = path->dentry->d_inode; 12241da177e4SLinus Torvalds return 0; 12251da177e4SLinus Torvalds } 12261da177e4SLinus Torvalds 122752094c8aSAl Viro static inline int may_lookup(struct nameidata *nd) 122852094c8aSAl Viro { 122952094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 12304ad5abb3SAl Viro int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); 123152094c8aSAl Viro if (err != -ECHILD) 123252094c8aSAl Viro return err; 123319660af7SAl Viro if (unlazy_walk(nd, NULL)) 123452094c8aSAl Viro return -ECHILD; 123552094c8aSAl Viro } 12364ad5abb3SAl Viro return inode_permission(nd->inode, MAY_EXEC); 123752094c8aSAl Viro } 123852094c8aSAl Viro 12399856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type) 12409856fa1bSAl Viro { 12419856fa1bSAl Viro if (type == LAST_DOTDOT) { 12429856fa1bSAl Viro if (nd->flags & LOOKUP_RCU) { 12439856fa1bSAl Viro if (follow_dotdot_rcu(nd)) 12449856fa1bSAl Viro return -ECHILD; 12459856fa1bSAl Viro } else 12469856fa1bSAl Viro follow_dotdot(nd); 12479856fa1bSAl Viro } 12489856fa1bSAl Viro return 0; 12499856fa1bSAl Viro } 12509856fa1bSAl Viro 1251951361f9SAl Viro static void terminate_walk(struct nameidata *nd) 1252951361f9SAl Viro { 1253951361f9SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1254951361f9SAl Viro path_put(&nd->path); 1255951361f9SAl Viro } else { 1256951361f9SAl Viro nd->flags &= ~LOOKUP_RCU; 12575b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1258951361f9SAl Viro nd->root.mnt = NULL; 1259951361f9SAl Viro rcu_read_unlock(); 1260951361f9SAl Viro br_read_unlock(vfsmount_lock); 1261951361f9SAl Viro } 1262951361f9SAl Viro } 1263951361f9SAl Viro 12643ddcd056SLinus Torvalds /* 12653ddcd056SLinus Torvalds * Do we need to follow links? We _really_ want to be able 12663ddcd056SLinus Torvalds * to do this check without having to look at inode->i_op, 12673ddcd056SLinus Torvalds * so we keep a cache of "no, this doesn't need follow_link" 12683ddcd056SLinus Torvalds * for the common case. 12693ddcd056SLinus Torvalds */ 12703ddcd056SLinus Torvalds static inline int do_follow_link(struct inode *inode, int follow) 12713ddcd056SLinus Torvalds { 12723ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) { 12733ddcd056SLinus Torvalds if (likely(inode->i_op->follow_link)) 12743ddcd056SLinus Torvalds return follow; 12753ddcd056SLinus Torvalds 12763ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 12773ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 12783ddcd056SLinus Torvalds inode->i_opflags |= IOP_NOFOLLOW; 12793ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 12803ddcd056SLinus Torvalds } 12813ddcd056SLinus Torvalds return 0; 12823ddcd056SLinus Torvalds } 12833ddcd056SLinus Torvalds 1284ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path, 1285ce57dfc1SAl Viro struct qstr *name, int type, int follow) 1286ce57dfc1SAl Viro { 1287ce57dfc1SAl Viro struct inode *inode; 1288ce57dfc1SAl Viro int err; 1289ce57dfc1SAl Viro /* 1290ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1291ce57dfc1SAl Viro * to be able to know about the current root directory and 1292ce57dfc1SAl Viro * parent relationships. 1293ce57dfc1SAl Viro */ 1294ce57dfc1SAl Viro if (unlikely(type != LAST_NORM)) 1295ce57dfc1SAl Viro return handle_dots(nd, type); 1296ce57dfc1SAl Viro err = do_lookup(nd, name, path, &inode); 1297ce57dfc1SAl Viro if (unlikely(err)) { 1298ce57dfc1SAl Viro terminate_walk(nd); 1299ce57dfc1SAl Viro return err; 1300ce57dfc1SAl Viro } 1301ce57dfc1SAl Viro if (!inode) { 1302ce57dfc1SAl Viro path_to_nameidata(path, nd); 1303ce57dfc1SAl Viro terminate_walk(nd); 1304ce57dfc1SAl Viro return -ENOENT; 1305ce57dfc1SAl Viro } 13063ddcd056SLinus Torvalds if (do_follow_link(inode, follow)) { 130719660af7SAl Viro if (nd->flags & LOOKUP_RCU) { 130819660af7SAl Viro if (unlikely(unlazy_walk(nd, path->dentry))) { 130919660af7SAl Viro terminate_walk(nd); 1310ce57dfc1SAl Viro return -ECHILD; 131119660af7SAl Viro } 131219660af7SAl Viro } 1313ce57dfc1SAl Viro BUG_ON(inode != path->dentry->d_inode); 1314ce57dfc1SAl Viro return 1; 1315ce57dfc1SAl Viro } 1316ce57dfc1SAl Viro path_to_nameidata(path, nd); 1317ce57dfc1SAl Viro nd->inode = inode; 1318ce57dfc1SAl Viro return 0; 1319ce57dfc1SAl Viro } 1320ce57dfc1SAl Viro 13211da177e4SLinus Torvalds /* 1322b356379aSAl Viro * This limits recursive symlink follows to 8, while 1323b356379aSAl Viro * limiting consecutive symlinks to 40. 1324b356379aSAl Viro * 1325b356379aSAl Viro * Without that kind of total limit, nasty chains of consecutive 1326b356379aSAl Viro * symlinks can cause almost arbitrarily long lookups. 1327b356379aSAl Viro */ 1328b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd) 1329b356379aSAl Viro { 1330b356379aSAl Viro int res; 1331b356379aSAl Viro 1332b356379aSAl Viro if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { 1333b356379aSAl Viro path_put_conditional(path, nd); 1334b356379aSAl Viro path_put(&nd->path); 1335b356379aSAl Viro return -ELOOP; 1336b356379aSAl Viro } 13371a4022f8SErez Zadok BUG_ON(nd->depth >= MAX_NESTED_LINKS); 1338b356379aSAl Viro 1339b356379aSAl Viro nd->depth++; 1340b356379aSAl Viro current->link_count++; 1341b356379aSAl Viro 1342b356379aSAl Viro do { 1343b356379aSAl Viro struct path link = *path; 1344b356379aSAl Viro void *cookie; 1345574197e0SAl Viro 1346574197e0SAl Viro res = follow_link(&link, nd, &cookie); 1347b356379aSAl Viro if (!res) 1348b356379aSAl Viro res = walk_component(nd, path, &nd->last, 1349b356379aSAl Viro nd->last_type, LOOKUP_FOLLOW); 1350574197e0SAl Viro put_link(nd, &link, cookie); 1351b356379aSAl Viro } while (res > 0); 1352b356379aSAl Viro 1353b356379aSAl Viro current->link_count--; 1354b356379aSAl Viro nd->depth--; 1355b356379aSAl Viro return res; 1356b356379aSAl Viro } 1357b356379aSAl Viro 1358b356379aSAl Viro /* 13593ddcd056SLinus Torvalds * We really don't want to look at inode->i_op->lookup 13603ddcd056SLinus Torvalds * when we don't have to. So we keep a cache bit in 13613ddcd056SLinus Torvalds * the inode ->i_opflags field that says "yes, we can 13623ddcd056SLinus Torvalds * do lookup on this inode". 13633ddcd056SLinus Torvalds */ 13643ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode) 13653ddcd056SLinus Torvalds { 13663ddcd056SLinus Torvalds if (likely(inode->i_opflags & IOP_LOOKUP)) 13673ddcd056SLinus Torvalds return 1; 13683ddcd056SLinus Torvalds if (likely(!inode->i_op->lookup)) 13693ddcd056SLinus Torvalds return 0; 13703ddcd056SLinus Torvalds 13713ddcd056SLinus Torvalds /* We do this once for the lifetime of the inode */ 13723ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 13733ddcd056SLinus Torvalds inode->i_opflags |= IOP_LOOKUP; 13743ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 13753ddcd056SLinus Torvalds return 1; 13763ddcd056SLinus Torvalds } 13773ddcd056SLinus Torvalds 13783ddcd056SLinus Torvalds /* 13791da177e4SLinus Torvalds * Name resolution. 1380ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1381ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 13821da177e4SLinus Torvalds * 1383ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1384ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 13851da177e4SLinus Torvalds */ 13866de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 13871da177e4SLinus Torvalds { 13881da177e4SLinus Torvalds struct path next; 13891da177e4SLinus Torvalds int err; 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds while (*name=='/') 13921da177e4SLinus Torvalds name++; 13931da177e4SLinus Torvalds if (!*name) 1394086e183aSAl Viro return 0; 13951da177e4SLinus Torvalds 13961da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 13971da177e4SLinus Torvalds for(;;) { 13981da177e4SLinus Torvalds unsigned long hash; 13991da177e4SLinus Torvalds struct qstr this; 14001da177e4SLinus Torvalds unsigned int c; 1401fe479a58SAl Viro int type; 14021da177e4SLinus Torvalds 140352094c8aSAl Viro err = may_lookup(nd); 14041da177e4SLinus Torvalds if (err) 14051da177e4SLinus Torvalds break; 14061da177e4SLinus Torvalds 14071da177e4SLinus Torvalds this.name = name; 14081da177e4SLinus Torvalds c = *(const unsigned char *)name; 14091da177e4SLinus Torvalds 14101da177e4SLinus Torvalds hash = init_name_hash(); 14111da177e4SLinus Torvalds do { 14121da177e4SLinus Torvalds name++; 14131da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 14141da177e4SLinus Torvalds c = *(const unsigned char *)name; 14151da177e4SLinus Torvalds } while (c && (c != '/')); 14161da177e4SLinus Torvalds this.len = name - (const char *) this.name; 14171da177e4SLinus Torvalds this.hash = end_name_hash(hash); 14181da177e4SLinus Torvalds 1419fe479a58SAl Viro type = LAST_NORM; 1420fe479a58SAl Viro if (this.name[0] == '.') switch (this.len) { 1421fe479a58SAl Viro case 2: 142216c2cd71SAl Viro if (this.name[1] == '.') { 1423fe479a58SAl Viro type = LAST_DOTDOT; 142416c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 142516c2cd71SAl Viro } 1426fe479a58SAl Viro break; 1427fe479a58SAl Viro case 1: 1428fe479a58SAl Viro type = LAST_DOT; 1429fe479a58SAl Viro } 14305a202bcdSAl Viro if (likely(type == LAST_NORM)) { 14315a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 143216c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 14335a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 14345a202bcdSAl Viro err = parent->d_op->d_hash(parent, nd->inode, 14355a202bcdSAl Viro &this); 14365a202bcdSAl Viro if (err < 0) 14375a202bcdSAl Viro break; 14385a202bcdSAl Viro } 14395a202bcdSAl Viro } 1440fe479a58SAl Viro 14411da177e4SLinus Torvalds /* remove trailing slashes? */ 14421da177e4SLinus Torvalds if (!c) 14431da177e4SLinus Torvalds goto last_component; 14441da177e4SLinus Torvalds while (*++name == '/'); 14451da177e4SLinus Torvalds if (!*name) 1446b356379aSAl Viro goto last_component; 14471da177e4SLinus Torvalds 1448ce57dfc1SAl Viro err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW); 1449ce57dfc1SAl Viro if (err < 0) 1450ce57dfc1SAl Viro return err; 1451fe479a58SAl Viro 1452ce57dfc1SAl Viro if (err) { 1453b356379aSAl Viro err = nested_symlink(&next, nd); 14541da177e4SLinus Torvalds if (err) 1455a7472babSAl Viro return err; 145631e6b01fSNick Piggin } 14573ddcd056SLinus Torvalds if (can_lookup(nd->inode)) 14581da177e4SLinus Torvalds continue; 14593ddcd056SLinus Torvalds err = -ENOTDIR; 14603ddcd056SLinus Torvalds break; 14611da177e4SLinus Torvalds /* here ends the main loop */ 14621da177e4SLinus Torvalds 14631da177e4SLinus Torvalds last_component: 1464ce57dfc1SAl Viro nd->last = this; 1465ce57dfc1SAl Viro nd->last_type = type; 1466ce57dfc1SAl Viro return 0; 1467ce57dfc1SAl Viro } 1468951361f9SAl Viro terminate_walk(nd); 14691da177e4SLinus Torvalds return err; 14701da177e4SLinus Torvalds } 14711da177e4SLinus Torvalds 147270e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, 147370e9b357SAl Viro struct nameidata *nd, struct file **fp) 147431e6b01fSNick Piggin { 147531e6b01fSNick Piggin int retval = 0; 147631e6b01fSNick Piggin int fput_needed; 147731e6b01fSNick Piggin struct file *file; 147831e6b01fSNick Piggin 147931e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 148016c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 148131e6b01fSNick Piggin nd->depth = 0; 14825b6ca027SAl Viro if (flags & LOOKUP_ROOT) { 14835b6ca027SAl Viro struct inode *inode = nd->root.dentry->d_inode; 148473d049a4SAl Viro if (*name) { 14855b6ca027SAl Viro if (!inode->i_op->lookup) 14865b6ca027SAl Viro return -ENOTDIR; 14875b6ca027SAl Viro retval = inode_permission(inode, MAY_EXEC); 14885b6ca027SAl Viro if (retval) 14895b6ca027SAl Viro return retval; 149073d049a4SAl Viro } 14915b6ca027SAl Viro nd->path = nd->root; 14925b6ca027SAl Viro nd->inode = inode; 14935b6ca027SAl Viro if (flags & LOOKUP_RCU) { 14945b6ca027SAl Viro br_read_lock(vfsmount_lock); 14955b6ca027SAl Viro rcu_read_lock(); 14965b6ca027SAl Viro nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 14975b6ca027SAl Viro } else { 14985b6ca027SAl Viro path_get(&nd->path); 14995b6ca027SAl Viro } 15005b6ca027SAl Viro return 0; 15015b6ca027SAl Viro } 15025b6ca027SAl Viro 150331e6b01fSNick Piggin nd->root.mnt = NULL; 150431e6b01fSNick Piggin 150531e6b01fSNick Piggin if (*name=='/') { 1506e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 150731e6b01fSNick Piggin br_read_lock(vfsmount_lock); 150831e6b01fSNick Piggin rcu_read_lock(); 1509e41f7d4eSAl Viro set_root_rcu(nd); 1510e41f7d4eSAl Viro } else { 1511e41f7d4eSAl Viro set_root(nd); 1512e41f7d4eSAl Viro path_get(&nd->root); 1513e41f7d4eSAl Viro } 151431e6b01fSNick Piggin nd->path = nd->root; 151531e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 1516e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 151731e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1518c28cc364SNick Piggin unsigned seq; 151931e6b01fSNick Piggin 152031e6b01fSNick Piggin br_read_lock(vfsmount_lock); 152131e6b01fSNick Piggin rcu_read_lock(); 152231e6b01fSNick Piggin 1523c28cc364SNick Piggin do { 1524c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 152531e6b01fSNick Piggin nd->path = fs->pwd; 1526c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1527c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1528e41f7d4eSAl Viro } else { 1529e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 1530e41f7d4eSAl Viro } 153131e6b01fSNick Piggin } else { 153231e6b01fSNick Piggin struct dentry *dentry; 153331e6b01fSNick Piggin 15341abf0c71SAl Viro file = fget_raw_light(dfd, &fput_needed); 153531e6b01fSNick Piggin retval = -EBADF; 153631e6b01fSNick Piggin if (!file) 153731e6b01fSNick Piggin goto out_fail; 153831e6b01fSNick Piggin 153931e6b01fSNick Piggin dentry = file->f_path.dentry; 154031e6b01fSNick Piggin 1541f52e0c11SAl Viro if (*name) { 154231e6b01fSNick Piggin retval = -ENOTDIR; 154331e6b01fSNick Piggin if (!S_ISDIR(dentry->d_inode->i_mode)) 154431e6b01fSNick Piggin goto fput_fail; 154531e6b01fSNick Piggin 15464ad5abb3SAl Viro retval = inode_permission(dentry->d_inode, MAY_EXEC); 154731e6b01fSNick Piggin if (retval) 154831e6b01fSNick Piggin goto fput_fail; 1549f52e0c11SAl Viro } 155031e6b01fSNick Piggin 155131e6b01fSNick Piggin nd->path = file->f_path; 1552e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 155331e6b01fSNick Piggin if (fput_needed) 155470e9b357SAl Viro *fp = file; 1555c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 155631e6b01fSNick Piggin br_read_lock(vfsmount_lock); 155731e6b01fSNick Piggin rcu_read_lock(); 15585590ff0dSUlrich Drepper } else { 15595dd784d0SJan Blunck path_get(&file->f_path); 15605590ff0dSUlrich Drepper fput_light(file, fput_needed); 15611da177e4SLinus Torvalds } 1562e41f7d4eSAl Viro } 1563e41f7d4eSAl Viro 156431e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 15659b4a9b14SAl Viro return 0; 15662dfdd266SJosef 'Jeff' Sipek 15679b4a9b14SAl Viro fput_fail: 15689b4a9b14SAl Viro fput_light(file, fput_needed); 15699b4a9b14SAl Viro out_fail: 15709b4a9b14SAl Viro return retval; 15719b4a9b14SAl Viro } 15729b4a9b14SAl Viro 1573bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path) 1574bd92d7feSAl Viro { 1575bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 1576bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 1577bd92d7feSAl Viro 1578bd92d7feSAl Viro nd->flags &= ~LOOKUP_PARENT; 1579bd92d7feSAl Viro return walk_component(nd, path, &nd->last, nd->last_type, 1580bd92d7feSAl Viro nd->flags & LOOKUP_FOLLOW); 1581bd92d7feSAl Viro } 1582bd92d7feSAl Viro 15839b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1584ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name, 15859b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 15869b4a9b14SAl Viro { 158770e9b357SAl Viro struct file *base = NULL; 1588bd92d7feSAl Viro struct path path; 1589bd92d7feSAl Viro int err; 159031e6b01fSNick Piggin 159131e6b01fSNick Piggin /* 159231e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 159331e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 159431e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 159531e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 159631e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 159731e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 159831e6b01fSNick Piggin * analogue, foo_rcu(). 159931e6b01fSNick Piggin * 160031e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 160131e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 160231e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 160331e6b01fSNick Piggin * be able to complete). 160431e6b01fSNick Piggin */ 1605bd92d7feSAl Viro err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); 1606ee0827cdSAl Viro 1607bd92d7feSAl Viro if (unlikely(err)) 1608bd92d7feSAl Viro return err; 1609ee0827cdSAl Viro 1610ee0827cdSAl Viro current->total_link_count = 0; 1611bd92d7feSAl Viro err = link_path_walk(name, nd); 1612bd92d7feSAl Viro 1613bd92d7feSAl Viro if (!err && !(flags & LOOKUP_PARENT)) { 1614bd92d7feSAl Viro err = lookup_last(nd, &path); 1615bd92d7feSAl Viro while (err > 0) { 1616bd92d7feSAl Viro void *cookie; 1617bd92d7feSAl Viro struct path link = path; 1618bd92d7feSAl Viro nd->flags |= LOOKUP_PARENT; 1619574197e0SAl Viro err = follow_link(&link, nd, &cookie); 1620bd92d7feSAl Viro if (!err) 1621bd92d7feSAl Viro err = lookup_last(nd, &path); 1622574197e0SAl Viro put_link(nd, &link, cookie); 1623bd92d7feSAl Viro } 1624bd92d7feSAl Viro } 1625ee0827cdSAl Viro 16269f1fafeeSAl Viro if (!err) 16279f1fafeeSAl Viro err = complete_walk(nd); 1628bd92d7feSAl Viro 1629bd92d7feSAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) { 1630bd92d7feSAl Viro if (!nd->inode->i_op->lookup) { 1631bd92d7feSAl Viro path_put(&nd->path); 1632bd23a539SAl Viro err = -ENOTDIR; 1633bd92d7feSAl Viro } 1634bd92d7feSAl Viro } 163516c2cd71SAl Viro 163670e9b357SAl Viro if (base) 163770e9b357SAl Viro fput(base); 1638ee0827cdSAl Viro 16395b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 164031e6b01fSNick Piggin path_put(&nd->root); 164131e6b01fSNick Piggin nd->root.mnt = NULL; 164231e6b01fSNick Piggin } 1643bd92d7feSAl Viro return err; 164431e6b01fSNick Piggin } 164531e6b01fSNick Piggin 1646ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name, 1647ee0827cdSAl Viro unsigned int flags, struct nameidata *nd) 1648ee0827cdSAl Viro { 1649ee0827cdSAl Viro int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd); 1650ee0827cdSAl Viro if (unlikely(retval == -ECHILD)) 1651ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags, nd); 1652ee0827cdSAl Viro if (unlikely(retval == -ESTALE)) 1653ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd); 1654ee0827cdSAl Viro 165531e6b01fSNick Piggin if (likely(!retval)) { 165631e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) { 165731e6b01fSNick Piggin if (nd->path.dentry && nd->inode) 165831e6b01fSNick Piggin audit_inode(name, nd->path.dentry); 165931e6b01fSNick Piggin } 166031e6b01fSNick Piggin } 1661170aa3d0SUlrich Drepper return retval; 16621da177e4SLinus Torvalds } 16631da177e4SLinus Torvalds 1664c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd) 16655590ff0dSUlrich Drepper { 1666c9c6cac0SAl Viro return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd); 16675590ff0dSUlrich Drepper } 16685590ff0dSUlrich Drepper 1669d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1670d1811465SAl Viro { 1671d1811465SAl Viro struct nameidata nd; 1672d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1673d1811465SAl Viro if (!res) 1674d1811465SAl Viro *path = nd.path; 1675d1811465SAl Viro return res; 1676d1811465SAl Viro } 1677d1811465SAl Viro 167816f18200SJosef 'Jeff' Sipek /** 167916f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 168016f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 168116f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 168216f18200SJosef 'Jeff' Sipek * @name: pointer to file name 168316f18200SJosef 'Jeff' Sipek * @flags: lookup flags 1684e0a01249SAl Viro * @path: pointer to struct path to fill 168516f18200SJosef 'Jeff' Sipek */ 168616f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 168716f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 1688e0a01249SAl Viro struct path *path) 168916f18200SJosef 'Jeff' Sipek { 1690e0a01249SAl Viro struct nameidata nd; 1691e0a01249SAl Viro int err; 1692e0a01249SAl Viro nd.root.dentry = dentry; 1693e0a01249SAl Viro nd.root.mnt = mnt; 1694e0a01249SAl Viro BUG_ON(flags & LOOKUP_PARENT); 16955b6ca027SAl Viro /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ 1696e0a01249SAl Viro err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd); 1697e0a01249SAl Viro if (!err) 1698e0a01249SAl Viro *path = nd.path; 1699e0a01249SAl Viro return err; 170016f18200SJosef 'Jeff' Sipek } 170116f18200SJosef 'Jeff' Sipek 1702eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name, 1703eead1911SChristoph Hellwig struct dentry *base, struct nameidata *nd) 17041da177e4SLinus Torvalds { 170581fca444SChristoph Hellwig struct inode *inode = base->d_inode; 17061da177e4SLinus Torvalds struct dentry *dentry; 17071da177e4SLinus Torvalds int err; 17081da177e4SLinus Torvalds 17094ad5abb3SAl Viro err = inode_permission(inode, MAY_EXEC); 171081fca444SChristoph Hellwig if (err) 171181fca444SChristoph Hellwig return ERR_PTR(err); 17121da177e4SLinus Torvalds 17131da177e4SLinus Torvalds /* 1714b04f784eSNick Piggin * Don't bother with __d_lookup: callers are for creat as 1715b04f784eSNick Piggin * well as unlink, so a lot of the time it would cost 1716b04f784eSNick Piggin * a double lookup. 17176e6b1bd1SAl Viro */ 17186e6b1bd1SAl Viro dentry = d_lookup(base, name); 17196e6b1bd1SAl Viro 172044396f4bSJosef Bacik if (dentry && d_need_lookup(dentry)) { 172144396f4bSJosef Bacik /* 172244396f4bSJosef Bacik * __lookup_hash is called with the parent dir's i_mutex already 172344396f4bSJosef Bacik * held, so we are good to go here. 172444396f4bSJosef Bacik */ 172544396f4bSJosef Bacik dentry = d_inode_lookup(base, dentry, nd); 172644396f4bSJosef Bacik if (IS_ERR(dentry)) 172744396f4bSJosef Bacik return dentry; 172844396f4bSJosef Bacik } 172944396f4bSJosef Bacik 1730d2d9e9fbSAl Viro if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) { 1731d2d9e9fbSAl Viro int status = d_revalidate(dentry, nd); 1732d2d9e9fbSAl Viro if (unlikely(status <= 0)) { 1733d2d9e9fbSAl Viro /* 1734d2d9e9fbSAl Viro * The dentry failed validation. 1735d2d9e9fbSAl Viro * If d_revalidate returned 0 attempt to invalidate 1736d2d9e9fbSAl Viro * the dentry otherwise d_revalidate is asking us 1737d2d9e9fbSAl Viro * to return a fail status. 1738d2d9e9fbSAl Viro */ 1739d2d9e9fbSAl Viro if (status < 0) { 1740d2d9e9fbSAl Viro dput(dentry); 1741d2d9e9fbSAl Viro return ERR_PTR(status); 1742d2d9e9fbSAl Viro } else if (!d_invalidate(dentry)) { 1743d2d9e9fbSAl Viro dput(dentry); 1744d2d9e9fbSAl Viro dentry = NULL; 1745d2d9e9fbSAl Viro } 1746d2d9e9fbSAl Viro } 1747d2d9e9fbSAl Viro } 17486e6b1bd1SAl Viro 17491da177e4SLinus Torvalds if (!dentry) 1750baa03890SNick Piggin dentry = d_alloc_and_lookup(base, name, nd); 17515a202bcdSAl Viro 17521da177e4SLinus Torvalds return dentry; 17531da177e4SLinus Torvalds } 17541da177e4SLinus Torvalds 1755057f6c01SJames Morris /* 1756057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1757057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1758057f6c01SJames Morris * SMP-safe. 1759057f6c01SJames Morris */ 1760a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 17611da177e4SLinus Torvalds { 17624ac91378SJan Blunck return __lookup_hash(&nd->last, nd->path.dentry, nd); 17631da177e4SLinus Torvalds } 17641da177e4SLinus Torvalds 1765eead1911SChristoph Hellwig /** 1766a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1767eead1911SChristoph Hellwig * @name: pathname component to lookup 1768eead1911SChristoph Hellwig * @base: base directory to lookup from 1769eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1770eead1911SChristoph Hellwig * 1771a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1772a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1773eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1774eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1775eead1911SChristoph Hellwig */ 1776057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1777057f6c01SJames Morris { 1778057f6c01SJames Morris struct qstr this; 17796a96ba54SAl Viro unsigned long hash; 17806a96ba54SAl Viro unsigned int c; 1781057f6c01SJames Morris 17822f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 17832f9092e1SDavid Woodhouse 17846a96ba54SAl Viro this.name = name; 17856a96ba54SAl Viro this.len = len; 17866a96ba54SAl Viro if (!len) 17876a96ba54SAl Viro return ERR_PTR(-EACCES); 17886a96ba54SAl Viro 17896a96ba54SAl Viro hash = init_name_hash(); 17906a96ba54SAl Viro while (len--) { 17916a96ba54SAl Viro c = *(const unsigned char *)name++; 17926a96ba54SAl Viro if (c == '/' || c == '\0') 17936a96ba54SAl Viro return ERR_PTR(-EACCES); 17946a96ba54SAl Viro hash = partial_name_hash(c, hash); 17956a96ba54SAl Viro } 17966a96ba54SAl Viro this.hash = end_name_hash(hash); 17975a202bcdSAl Viro /* 17985a202bcdSAl Viro * See if the low-level filesystem might want 17995a202bcdSAl Viro * to use its own hash.. 18005a202bcdSAl Viro */ 18015a202bcdSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 18025a202bcdSAl Viro int err = base->d_op->d_hash(base, base->d_inode, &this); 18035a202bcdSAl Viro if (err < 0) 18045a202bcdSAl Viro return ERR_PTR(err); 18055a202bcdSAl Viro } 1806eead1911SChristoph Hellwig 180749705b77SChristoph Hellwig return __lookup_hash(&this, base, NULL); 1808057f6c01SJames Morris } 1809057f6c01SJames Morris 18102d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags, 18112d8f3038SAl Viro struct path *path) 18121da177e4SLinus Torvalds { 18132d8f3038SAl Viro struct nameidata nd; 1814f52e0c11SAl Viro char *tmp = getname_flags(name, flags); 18151da177e4SLinus Torvalds int err = PTR_ERR(tmp); 18161da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 18172d8f3038SAl Viro 18182d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 18192d8f3038SAl Viro 18202d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 18211da177e4SLinus Torvalds putname(tmp); 18222d8f3038SAl Viro if (!err) 18232d8f3038SAl Viro *path = nd.path; 18241da177e4SLinus Torvalds } 18251da177e4SLinus Torvalds return err; 18261da177e4SLinus Torvalds } 18271da177e4SLinus Torvalds 18282ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 18292ad94ae6SAl Viro struct nameidata *nd, char **name) 18302ad94ae6SAl Viro { 18312ad94ae6SAl Viro char *s = getname(path); 18322ad94ae6SAl Viro int error; 18332ad94ae6SAl Viro 18342ad94ae6SAl Viro if (IS_ERR(s)) 18352ad94ae6SAl Viro return PTR_ERR(s); 18362ad94ae6SAl Viro 18372ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 18382ad94ae6SAl Viro if (error) 18392ad94ae6SAl Viro putname(s); 18402ad94ae6SAl Viro else 18412ad94ae6SAl Viro *name = s; 18422ad94ae6SAl Viro 18432ad94ae6SAl Viro return error; 18442ad94ae6SAl Viro } 18452ad94ae6SAl Viro 18461da177e4SLinus Torvalds /* 18471da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 18481da177e4SLinus Torvalds * minimal. 18491da177e4SLinus Torvalds */ 18501da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 18511da177e4SLinus Torvalds { 1852da9592edSDavid Howells uid_t fsuid = current_fsuid(); 1853da9592edSDavid Howells 18541da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 18551da177e4SLinus Torvalds return 0; 1856e795b717SSerge E. Hallyn if (current_user_ns() != inode_userns(inode)) 1857e795b717SSerge E. Hallyn goto other_userns; 1858da9592edSDavid Howells if (inode->i_uid == fsuid) 18591da177e4SLinus Torvalds return 0; 1860da9592edSDavid Howells if (dir->i_uid == fsuid) 18611da177e4SLinus Torvalds return 0; 1862e795b717SSerge E. Hallyn 1863e795b717SSerge E. Hallyn other_userns: 1864e795b717SSerge E. Hallyn return !ns_capable(inode_userns(inode), CAP_FOWNER); 18651da177e4SLinus Torvalds } 18661da177e4SLinus Torvalds 18671da177e4SLinus Torvalds /* 18681da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 18691da177e4SLinus Torvalds * whether the type of victim is right. 18701da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 18711da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 18721da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 18731da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 18741da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 18751da177e4SLinus Torvalds * a. be owner of dir, or 18761da177e4SLinus Torvalds * b. be owner of victim, or 18771da177e4SLinus Torvalds * c. have CAP_FOWNER capability 18781da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 18791da177e4SLinus Torvalds * links pointing to it. 18801da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 18811da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 18821da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 18831da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 18841da177e4SLinus Torvalds * nfs_async_unlink(). 18851da177e4SLinus Torvalds */ 1886858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 18871da177e4SLinus Torvalds { 18881da177e4SLinus Torvalds int error; 18891da177e4SLinus Torvalds 18901da177e4SLinus Torvalds if (!victim->d_inode) 18911da177e4SLinus Torvalds return -ENOENT; 18921da177e4SLinus Torvalds 18931da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 1894cccc6bbaSAl Viro audit_inode_child(victim, dir); 18951da177e4SLinus Torvalds 1896f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 18971da177e4SLinus Torvalds if (error) 18981da177e4SLinus Torvalds return error; 18991da177e4SLinus Torvalds if (IS_APPEND(dir)) 19001da177e4SLinus Torvalds return -EPERM; 19011da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 1902f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 19031da177e4SLinus Torvalds return -EPERM; 19041da177e4SLinus Torvalds if (isdir) { 19051da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 19061da177e4SLinus Torvalds return -ENOTDIR; 19071da177e4SLinus Torvalds if (IS_ROOT(victim)) 19081da177e4SLinus Torvalds return -EBUSY; 19091da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 19101da177e4SLinus Torvalds return -EISDIR; 19111da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 19121da177e4SLinus Torvalds return -ENOENT; 19131da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 19141da177e4SLinus Torvalds return -EBUSY; 19151da177e4SLinus Torvalds return 0; 19161da177e4SLinus Torvalds } 19171da177e4SLinus Torvalds 19181da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 19191da177e4SLinus Torvalds * dir. 19201da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 19211da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 19221da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 19231da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 19241da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 19251da177e4SLinus Torvalds */ 1926a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 19271da177e4SLinus Torvalds { 19281da177e4SLinus Torvalds if (child->d_inode) 19291da177e4SLinus Torvalds return -EEXIST; 19301da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 19311da177e4SLinus Torvalds return -ENOENT; 1932f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 19331da177e4SLinus Torvalds } 19341da177e4SLinus Torvalds 19351da177e4SLinus Torvalds /* 19361da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 19371da177e4SLinus Torvalds */ 19381da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 19391da177e4SLinus Torvalds { 19401da177e4SLinus Torvalds struct dentry *p; 19411da177e4SLinus Torvalds 19421da177e4SLinus Torvalds if (p1 == p2) { 1943f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 19441da177e4SLinus Torvalds return NULL; 19451da177e4SLinus Torvalds } 19461da177e4SLinus Torvalds 1947a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 19481da177e4SLinus Torvalds 1949e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 1950e2761a11SOGAWA Hirofumi if (p) { 1951f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 1952f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 19531da177e4SLinus Torvalds return p; 19541da177e4SLinus Torvalds } 19551da177e4SLinus Torvalds 1956e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 1957e2761a11SOGAWA Hirofumi if (p) { 1958f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1959f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 19601da177e4SLinus Torvalds return p; 19611da177e4SLinus Torvalds } 19621da177e4SLinus Torvalds 1963f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1964f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 19651da177e4SLinus Torvalds return NULL; 19661da177e4SLinus Torvalds } 19671da177e4SLinus Torvalds 19681da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 19691da177e4SLinus Torvalds { 19701b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 19711da177e4SLinus Torvalds if (p1 != p2) { 19721b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 1973a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 19741da177e4SLinus Torvalds } 19751da177e4SLinus Torvalds } 19761da177e4SLinus Torvalds 19771da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode, 19781da177e4SLinus Torvalds struct nameidata *nd) 19791da177e4SLinus Torvalds { 1980a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 19811da177e4SLinus Torvalds 19821da177e4SLinus Torvalds if (error) 19831da177e4SLinus Torvalds return error; 19841da177e4SLinus Torvalds 1985acfa4380SAl Viro if (!dir->i_op->create) 19861da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 19871da177e4SLinus Torvalds mode &= S_IALLUGO; 19881da177e4SLinus Torvalds mode |= S_IFREG; 19891da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 19901da177e4SLinus Torvalds if (error) 19911da177e4SLinus Torvalds return error; 19921da177e4SLinus Torvalds error = dir->i_op->create(dir, dentry, mode, nd); 1993a74574aaSStephen Smalley if (!error) 1994f38aa942SAmy Griffis fsnotify_create(dir, dentry); 19951da177e4SLinus Torvalds return error; 19961da177e4SLinus Torvalds } 19971da177e4SLinus Torvalds 199873d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag) 19991da177e4SLinus Torvalds { 20003fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 20011da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 20021da177e4SLinus Torvalds int error; 20031da177e4SLinus Torvalds 2004bcda7652SAl Viro /* O_PATH? */ 2005bcda7652SAl Viro if (!acc_mode) 2006bcda7652SAl Viro return 0; 2007bcda7652SAl Viro 20081da177e4SLinus Torvalds if (!inode) 20091da177e4SLinus Torvalds return -ENOENT; 20101da177e4SLinus Torvalds 2011c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2012c8fe8f30SChristoph Hellwig case S_IFLNK: 20131da177e4SLinus Torvalds return -ELOOP; 2014c8fe8f30SChristoph Hellwig case S_IFDIR: 2015c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 20161da177e4SLinus Torvalds return -EISDIR; 2017c8fe8f30SChristoph Hellwig break; 2018c8fe8f30SChristoph Hellwig case S_IFBLK: 2019c8fe8f30SChristoph Hellwig case S_IFCHR: 20203fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 20211da177e4SLinus Torvalds return -EACCES; 2022c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 2023c8fe8f30SChristoph Hellwig case S_IFIFO: 2024c8fe8f30SChristoph Hellwig case S_IFSOCK: 20251da177e4SLinus Torvalds flag &= ~O_TRUNC; 2026c8fe8f30SChristoph Hellwig break; 20274a3fd211SDave Hansen } 2028b41572e9SDave Hansen 20293fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 2030b41572e9SDave Hansen if (error) 2031b41572e9SDave Hansen return error; 20326146f0d5SMimi Zohar 20331da177e4SLinus Torvalds /* 20341da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 20351da177e4SLinus Torvalds */ 20361da177e4SLinus Torvalds if (IS_APPEND(inode)) { 20378737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 20387715b521SAl Viro return -EPERM; 20391da177e4SLinus Torvalds if (flag & O_TRUNC) 20407715b521SAl Viro return -EPERM; 20411da177e4SLinus Torvalds } 20421da177e4SLinus Torvalds 20431da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 20442e149670SSerge E. Hallyn if (flag & O_NOATIME && !inode_owner_or_capable(inode)) 20457715b521SAl Viro return -EPERM; 20461da177e4SLinus Torvalds 20471da177e4SLinus Torvalds /* 20481da177e4SLinus Torvalds * Ensure there are no outstanding leases on the file. 20491da177e4SLinus Torvalds */ 2050b65a9cfcSAl Viro return break_lease(inode, flag); 20517715b521SAl Viro } 20527715b521SAl Viro 2053e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 20547715b521SAl Viro { 2055e1181ee6SJeff Layton struct path *path = &filp->f_path; 20567715b521SAl Viro struct inode *inode = path->dentry->d_inode; 20577715b521SAl Viro int error = get_write_access(inode); 20581da177e4SLinus Torvalds if (error) 20597715b521SAl Viro return error; 20601da177e4SLinus Torvalds /* 20611da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 20621da177e4SLinus Torvalds */ 20631da177e4SLinus Torvalds error = locks_verify_locked(inode); 2064be6d3e56SKentaro Takeda if (!error) 2065ea0d3ab2STetsuo Handa error = security_path_truncate(path); 20661da177e4SLinus Torvalds if (!error) { 20677715b521SAl Viro error = do_truncate(path->dentry, 0, 2068d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 2069e1181ee6SJeff Layton filp); 20701da177e4SLinus Torvalds } 20711da177e4SLinus Torvalds put_write_access(inode); 2072acd0c935SMimi Zohar return error; 20731da177e4SLinus Torvalds } 20741da177e4SLinus Torvalds 2075d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2076d57999e1SDave Hansen { 20778a5e929dSAl Viro if ((flag & O_ACCMODE) == 3) 20788a5e929dSAl Viro flag--; 2079d57999e1SDave Hansen return flag; 2080d57999e1SDave Hansen } 2081d57999e1SDave Hansen 208231e6b01fSNick Piggin /* 2083fe2d35ffSAl Viro * Handle the last step of open() 208431e6b01fSNick Piggin */ 2085fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path, 2086c3e380b0SAl Viro const struct open_flags *op, const char *pathname) 2087fb1cc555SAl Viro { 2088a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 20896c0d46c4SAl Viro struct dentry *dentry; 2090ca344a89SAl Viro int open_flag = op->open_flag; 20916c0d46c4SAl Viro int will_truncate = open_flag & O_TRUNC; 2092ca344a89SAl Viro int want_write = 0; 2093bcda7652SAl Viro int acc_mode = op->acc_mode; 2094fb1cc555SAl Viro struct file *filp; 209516c2cd71SAl Viro int error; 2096fb1cc555SAl Viro 2097c3e380b0SAl Viro nd->flags &= ~LOOKUP_PARENT; 2098c3e380b0SAl Viro nd->flags |= op->intent; 2099c3e380b0SAl Viro 21001f36f774SAl Viro switch (nd->last_type) { 21011f36f774SAl Viro case LAST_DOTDOT: 2102176306f5SNeil Brown case LAST_DOT: 2103fe2d35ffSAl Viro error = handle_dots(nd, nd->last_type); 2104fe2d35ffSAl Viro if (error) 2105fe2d35ffSAl Viro return ERR_PTR(error); 21061f36f774SAl Viro /* fallthrough */ 21071f36f774SAl Viro case LAST_ROOT: 21089f1fafeeSAl Viro error = complete_walk(nd); 210916c2cd71SAl Viro if (error) 21109f1fafeeSAl Viro return ERR_PTR(error); 2111fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2112ca344a89SAl Viro if (open_flag & O_CREAT) { 211316c2cd71SAl Viro error = -EISDIR; 21141f36f774SAl Viro goto exit; 2115fe2d35ffSAl Viro } 2116fe2d35ffSAl Viro goto ok; 21171f36f774SAl Viro case LAST_BIND: 21189f1fafeeSAl Viro error = complete_walk(nd); 211916c2cd71SAl Viro if (error) 21209f1fafeeSAl Viro return ERR_PTR(error); 21211f36f774SAl Viro audit_inode(pathname, dir); 21221f36f774SAl Viro goto ok; 21231f36f774SAl Viro } 2124a2c36b45SAl Viro 2125ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 2126bcda7652SAl Viro int symlink_ok = 0; 2127fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 2128fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2129bcda7652SAl Viro if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) 2130bcda7652SAl Viro symlink_ok = 1; 2131fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 2132ce57dfc1SAl Viro error = walk_component(nd, path, &nd->last, LAST_NORM, 2133ce57dfc1SAl Viro !symlink_ok); 2134ce57dfc1SAl Viro if (error < 0) 2135fe2d35ffSAl Viro return ERR_PTR(error); 2136ce57dfc1SAl Viro if (error) /* symlink */ 2137fe2d35ffSAl Viro return NULL; 2138fe2d35ffSAl Viro /* sayonara */ 21399f1fafeeSAl Viro error = complete_walk(nd); 21409f1fafeeSAl Viro if (error) 2141fe2d35ffSAl Viro return ERR_PTR(-ECHILD); 2142fe2d35ffSAl Viro 2143fe2d35ffSAl Viro error = -ENOTDIR; 2144fe2d35ffSAl Viro if (nd->flags & LOOKUP_DIRECTORY) { 2145ce57dfc1SAl Viro if (!nd->inode->i_op->lookup) 2146fe2d35ffSAl Viro goto exit; 2147fe2d35ffSAl Viro } 2148fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2149fe2d35ffSAl Viro goto ok; 2150fe2d35ffSAl Viro } 2151fe2d35ffSAl Viro 2152fe2d35ffSAl Viro /* create side of things */ 21539f1fafeeSAl Viro error = complete_walk(nd); 21549f1fafeeSAl Viro if (error) 21559f1fafeeSAl Viro return ERR_PTR(error); 2156fe2d35ffSAl Viro 2157fe2d35ffSAl Viro audit_inode(pathname, dir); 215816c2cd71SAl Viro error = -EISDIR; 21591f36f774SAl Viro /* trailing slashes? */ 216031e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 21611f36f774SAl Viro goto exit; 21621f36f774SAl Viro 2163a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 2164a1e28038SAl Viro 21656c0d46c4SAl Viro dentry = lookup_hash(nd); 21666c0d46c4SAl Viro error = PTR_ERR(dentry); 21676c0d46c4SAl Viro if (IS_ERR(dentry)) { 2168fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2169fb1cc555SAl Viro goto exit; 2170fb1cc555SAl Viro } 2171fb1cc555SAl Viro 21726c0d46c4SAl Viro path->dentry = dentry; 21736c0d46c4SAl Viro path->mnt = nd->path.mnt; 21746c0d46c4SAl Viro 2175fb1cc555SAl Viro /* Negative dentry, just create the file */ 21766c0d46c4SAl Viro if (!dentry->d_inode) { 21776c0d46c4SAl Viro int mode = op->mode; 21786c0d46c4SAl Viro if (!IS_POSIXACL(dir->d_inode)) 21796c0d46c4SAl Viro mode &= ~current_umask(); 2180fb1cc555SAl Viro /* 2181fb1cc555SAl Viro * This write is needed to ensure that a 21826c0d46c4SAl Viro * rw->ro transition does not occur between 2183fb1cc555SAl Viro * the time when the file is created and when 2184fb1cc555SAl Viro * a permanent write count is taken through 2185fb1cc555SAl Viro * the 'struct file' in nameidata_to_filp(). 2186fb1cc555SAl Viro */ 2187fb1cc555SAl Viro error = mnt_want_write(nd->path.mnt); 2188fb1cc555SAl Viro if (error) 2189fb1cc555SAl Viro goto exit_mutex_unlock; 2190ca344a89SAl Viro want_write = 1; 21919b44f1b3SAl Viro /* Don't check for write permission, don't truncate */ 2192ca344a89SAl Viro open_flag &= ~O_TRUNC; 21936c0d46c4SAl Viro will_truncate = 0; 2194bcda7652SAl Viro acc_mode = MAY_OPEN; 21956c0d46c4SAl Viro error = security_path_mknod(&nd->path, dentry, mode, 0); 21966c0d46c4SAl Viro if (error) 21976c0d46c4SAl Viro goto exit_mutex_unlock; 21986c0d46c4SAl Viro error = vfs_create(dir->d_inode, dentry, mode, nd); 21996c0d46c4SAl Viro if (error) 22006c0d46c4SAl Viro goto exit_mutex_unlock; 22016c0d46c4SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 22026c0d46c4SAl Viro dput(nd->path.dentry); 22036c0d46c4SAl Viro nd->path.dentry = dentry; 2204ca344a89SAl Viro goto common; 2205fb1cc555SAl Viro } 2206fb1cc555SAl Viro 2207fb1cc555SAl Viro /* 2208fb1cc555SAl Viro * It already exists. 2209fb1cc555SAl Viro */ 2210fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2211fb1cc555SAl Viro audit_inode(pathname, path->dentry); 2212fb1cc555SAl Viro 2213fb1cc555SAl Viro error = -EEXIST; 2214ca344a89SAl Viro if (open_flag & O_EXCL) 2215fb1cc555SAl Viro goto exit_dput; 2216fb1cc555SAl Viro 22179875cf80SDavid Howells error = follow_managed(path, nd->flags); 22189875cf80SDavid Howells if (error < 0) 2219fb1cc555SAl Viro goto exit_dput; 2220fb1cc555SAl Viro 2221fb1cc555SAl Viro error = -ENOENT; 2222fb1cc555SAl Viro if (!path->dentry->d_inode) 2223fb1cc555SAl Viro goto exit_dput; 22249e67f361SAl Viro 22259e67f361SAl Viro if (path->dentry->d_inode->i_op->follow_link) 2226fb1cc555SAl Viro return NULL; 2227fb1cc555SAl Viro 2228fb1cc555SAl Viro path_to_nameidata(path, nd); 222931e6b01fSNick Piggin nd->inode = path->dentry->d_inode; 2230fb1cc555SAl Viro error = -EISDIR; 223131e6b01fSNick Piggin if (S_ISDIR(nd->inode->i_mode)) 2232fb1cc555SAl Viro goto exit; 223367ee3ad2SAl Viro ok: 22346c0d46c4SAl Viro if (!S_ISREG(nd->inode->i_mode)) 22356c0d46c4SAl Viro will_truncate = 0; 22366c0d46c4SAl Viro 22370f9d1a10SAl Viro if (will_truncate) { 22380f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 22390f9d1a10SAl Viro if (error) 22400f9d1a10SAl Viro goto exit; 2241ca344a89SAl Viro want_write = 1; 22420f9d1a10SAl Viro } 2243ca344a89SAl Viro common: 2244bcda7652SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2245ca344a89SAl Viro if (error) 22460f9d1a10SAl Viro goto exit; 22470f9d1a10SAl Viro filp = nameidata_to_filp(nd); 22480f9d1a10SAl Viro if (!IS_ERR(filp)) { 22490f9d1a10SAl Viro error = ima_file_check(filp, op->acc_mode); 22500f9d1a10SAl Viro if (error) { 22510f9d1a10SAl Viro fput(filp); 22520f9d1a10SAl Viro filp = ERR_PTR(error); 22530f9d1a10SAl Viro } 22540f9d1a10SAl Viro } 22550f9d1a10SAl Viro if (!IS_ERR(filp)) { 22560f9d1a10SAl Viro if (will_truncate) { 22570f9d1a10SAl Viro error = handle_truncate(filp); 22580f9d1a10SAl Viro if (error) { 22590f9d1a10SAl Viro fput(filp); 22600f9d1a10SAl Viro filp = ERR_PTR(error); 22610f9d1a10SAl Viro } 22620f9d1a10SAl Viro } 22630f9d1a10SAl Viro } 2264ca344a89SAl Viro out: 2265ca344a89SAl Viro if (want_write) 22660f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 22670f9d1a10SAl Viro path_put(&nd->path); 2268fb1cc555SAl Viro return filp; 2269fb1cc555SAl Viro 2270fb1cc555SAl Viro exit_mutex_unlock: 2271fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2272fb1cc555SAl Viro exit_dput: 2273fb1cc555SAl Viro path_put_conditional(path, nd); 2274fb1cc555SAl Viro exit: 2275ca344a89SAl Viro filp = ERR_PTR(error); 2276ca344a89SAl Viro goto out; 2277fb1cc555SAl Viro } 2278fb1cc555SAl Viro 227913aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname, 228073d049a4SAl Viro struct nameidata *nd, const struct open_flags *op, int flags) 22811da177e4SLinus Torvalds { 2282fe2d35ffSAl Viro struct file *base = NULL; 22834a3fd211SDave Hansen struct file *filp; 22849850c056SAl Viro struct path path; 228513aab428SAl Viro int error; 228631e6b01fSNick Piggin 228731e6b01fSNick Piggin filp = get_empty_filp(); 228831e6b01fSNick Piggin if (!filp) 228931e6b01fSNick Piggin return ERR_PTR(-ENFILE); 229031e6b01fSNick Piggin 229147c805dcSAl Viro filp->f_flags = op->open_flag; 229273d049a4SAl Viro nd->intent.open.file = filp; 229373d049a4SAl Viro nd->intent.open.flags = open_to_namei_flags(op->open_flag); 229473d049a4SAl Viro nd->intent.open.create_mode = op->mode; 229531e6b01fSNick Piggin 229673d049a4SAl Viro error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base); 229731e6b01fSNick Piggin if (unlikely(error)) 229813aab428SAl Viro goto out_filp; 229931e6b01fSNick Piggin 2300fe2d35ffSAl Viro current->total_link_count = 0; 230173d049a4SAl Viro error = link_path_walk(pathname, nd); 230231e6b01fSNick Piggin if (unlikely(error)) 230331e6b01fSNick Piggin goto out_filp; 23041da177e4SLinus Torvalds 230573d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2306806b681cSAl Viro while (unlikely(!filp)) { /* trailing symlink */ 23077b9337aaSNick Piggin struct path link = path; 2308def4af30SAl Viro void *cookie; 2309574197e0SAl Viro if (!(nd->flags & LOOKUP_FOLLOW)) { 231073d049a4SAl Viro path_put_conditional(&path, nd); 231173d049a4SAl Viro path_put(&nd->path); 231240b39136SAl Viro filp = ERR_PTR(-ELOOP); 231340b39136SAl Viro break; 231440b39136SAl Viro } 231573d049a4SAl Viro nd->flags |= LOOKUP_PARENT; 231673d049a4SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 2317574197e0SAl Viro error = follow_link(&link, nd, &cookie); 2318c3e380b0SAl Viro if (unlikely(error)) 2319f1afe9efSAl Viro filp = ERR_PTR(error); 2320c3e380b0SAl Viro else 232173d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2322574197e0SAl Viro put_link(nd, &link, cookie); 2323806b681cSAl Viro } 232410fa8e62SAl Viro out: 232573d049a4SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) 232673d049a4SAl Viro path_put(&nd->root); 2327fe2d35ffSAl Viro if (base) 2328fe2d35ffSAl Viro fput(base); 232973d049a4SAl Viro release_open_intent(nd); 233010fa8e62SAl Viro return filp; 23311da177e4SLinus Torvalds 233231e6b01fSNick Piggin out_filp: 233310fa8e62SAl Viro filp = ERR_PTR(error); 233410fa8e62SAl Viro goto out; 2335de459215SKirill Korotaev } 23361da177e4SLinus Torvalds 233713aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname, 233813aab428SAl Viro const struct open_flags *op, int flags) 233913aab428SAl Viro { 234073d049a4SAl Viro struct nameidata nd; 234113aab428SAl Viro struct file *filp; 234213aab428SAl Viro 234373d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); 234413aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 234573d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags); 234613aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 234773d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); 234813aab428SAl Viro return filp; 234913aab428SAl Viro } 235013aab428SAl Viro 235173d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, 235273d049a4SAl Viro const char *name, const struct open_flags *op, int flags) 235373d049a4SAl Viro { 235473d049a4SAl Viro struct nameidata nd; 235573d049a4SAl Viro struct file *file; 235673d049a4SAl Viro 235773d049a4SAl Viro nd.root.mnt = mnt; 235873d049a4SAl Viro nd.root.dentry = dentry; 235973d049a4SAl Viro 236073d049a4SAl Viro flags |= LOOKUP_ROOT; 236173d049a4SAl Viro 2362bcda7652SAl Viro if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN) 236373d049a4SAl Viro return ERR_PTR(-ELOOP); 236473d049a4SAl Viro 236573d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU); 236673d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 236773d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags); 236873d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 236973d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL); 237073d049a4SAl Viro return file; 237173d049a4SAl Viro } 237273d049a4SAl Viro 2373ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir) 23741da177e4SLinus Torvalds { 2375c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 2376ed75e95dSAl Viro struct nameidata nd; 2377ed75e95dSAl Viro int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd); 2378ed75e95dSAl Viro if (error) 2379ed75e95dSAl Viro return ERR_PTR(error); 23801da177e4SLinus Torvalds 2381c663e5d8SChristoph Hellwig /* 2382c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 2383c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 2384c663e5d8SChristoph Hellwig */ 2385ed75e95dSAl Viro if (nd.last_type != LAST_NORM) 2386ed75e95dSAl Viro goto out; 2387ed75e95dSAl Viro nd.flags &= ~LOOKUP_PARENT; 2388ed75e95dSAl Viro nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL; 2389ed75e95dSAl Viro nd.intent.open.flags = O_EXCL; 2390c663e5d8SChristoph Hellwig 2391c663e5d8SChristoph Hellwig /* 2392c663e5d8SChristoph Hellwig * Do the final lookup. 2393c663e5d8SChristoph Hellwig */ 2394ed75e95dSAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 2395ed75e95dSAl Viro dentry = lookup_hash(&nd); 23961da177e4SLinus Torvalds if (IS_ERR(dentry)) 23971da177e4SLinus Torvalds goto fail; 2398c663e5d8SChristoph Hellwig 2399e9baf6e5SAl Viro if (dentry->d_inode) 2400e9baf6e5SAl Viro goto eexist; 2401c663e5d8SChristoph Hellwig /* 2402c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 2403c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 2404c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 2405c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 2406c663e5d8SChristoph Hellwig */ 2407ed75e95dSAl Viro if (unlikely(!is_dir && nd.last.name[nd.last.len])) { 24081da177e4SLinus Torvalds dput(dentry); 24091da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 2410ed75e95dSAl Viro goto fail; 2411e9baf6e5SAl Viro } 2412ed75e95dSAl Viro *path = nd.path; 2413e9baf6e5SAl Viro return dentry; 2414e9baf6e5SAl Viro eexist: 2415e9baf6e5SAl Viro dput(dentry); 2416e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 24171da177e4SLinus Torvalds fail: 2418dae6ad8fSAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 2419ed75e95dSAl Viro out: 2420dae6ad8fSAl Viro path_put(&nd.path); 2421ed75e95dSAl Viro return dentry; 2422dae6ad8fSAl Viro } 2423dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create); 2424dae6ad8fSAl Viro 2425dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir) 2426dae6ad8fSAl Viro { 2427dae6ad8fSAl Viro char *tmp = getname(pathname); 2428dae6ad8fSAl Viro struct dentry *res; 2429dae6ad8fSAl Viro if (IS_ERR(tmp)) 2430dae6ad8fSAl Viro return ERR_CAST(tmp); 2431dae6ad8fSAl Viro res = kern_path_create(dfd, tmp, path, is_dir); 2432dae6ad8fSAl Viro putname(tmp); 2433dae6ad8fSAl Viro return res; 2434dae6ad8fSAl Viro } 2435dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create); 2436dae6ad8fSAl Viro 24371da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 24381da177e4SLinus Torvalds { 2439a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 24401da177e4SLinus Torvalds 24411da177e4SLinus Torvalds if (error) 24421da177e4SLinus Torvalds return error; 24431da177e4SLinus Torvalds 2444e795b717SSerge E. Hallyn if ((S_ISCHR(mode) || S_ISBLK(mode)) && 2445e795b717SSerge E. Hallyn !ns_capable(inode_userns(dir), CAP_MKNOD)) 24461da177e4SLinus Torvalds return -EPERM; 24471da177e4SLinus Torvalds 2448acfa4380SAl Viro if (!dir->i_op->mknod) 24491da177e4SLinus Torvalds return -EPERM; 24501da177e4SLinus Torvalds 245108ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 245208ce5f16SSerge E. Hallyn if (error) 245308ce5f16SSerge E. Hallyn return error; 245408ce5f16SSerge E. Hallyn 24551da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 24561da177e4SLinus Torvalds if (error) 24571da177e4SLinus Torvalds return error; 24581da177e4SLinus Torvalds 24591da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 2460a74574aaSStephen Smalley if (!error) 2461f38aa942SAmy Griffis fsnotify_create(dir, dentry); 24621da177e4SLinus Torvalds return error; 24631da177e4SLinus Torvalds } 24641da177e4SLinus Torvalds 2465463c3197SDave Hansen static int may_mknod(mode_t mode) 2466463c3197SDave Hansen { 2467463c3197SDave Hansen switch (mode & S_IFMT) { 2468463c3197SDave Hansen case S_IFREG: 2469463c3197SDave Hansen case S_IFCHR: 2470463c3197SDave Hansen case S_IFBLK: 2471463c3197SDave Hansen case S_IFIFO: 2472463c3197SDave Hansen case S_IFSOCK: 2473463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2474463c3197SDave Hansen return 0; 2475463c3197SDave Hansen case S_IFDIR: 2476463c3197SDave Hansen return -EPERM; 2477463c3197SDave Hansen default: 2478463c3197SDave Hansen return -EINVAL; 2479463c3197SDave Hansen } 2480463c3197SDave Hansen } 2481463c3197SDave Hansen 24822e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode, 24832e4d0924SHeiko Carstens unsigned, dev) 24841da177e4SLinus Torvalds { 24851da177e4SLinus Torvalds struct dentry *dentry; 2486dae6ad8fSAl Viro struct path path; 2487dae6ad8fSAl Viro int error; 24881da177e4SLinus Torvalds 24891da177e4SLinus Torvalds if (S_ISDIR(mode)) 24901da177e4SLinus Torvalds return -EPERM; 24911da177e4SLinus Torvalds 2492dae6ad8fSAl Viro dentry = user_path_create(dfd, filename, &path, 0); 2493dae6ad8fSAl Viro if (IS_ERR(dentry)) 2494dae6ad8fSAl Viro return PTR_ERR(dentry); 24952ad94ae6SAl Viro 2496dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 2497ce3b0f8dSAl Viro mode &= ~current_umask(); 2498463c3197SDave Hansen error = may_mknod(mode); 2499463c3197SDave Hansen if (error) 2500463c3197SDave Hansen goto out_dput; 2501dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 2502463c3197SDave Hansen if (error) 2503463c3197SDave Hansen goto out_dput; 2504dae6ad8fSAl Viro error = security_path_mknod(&path, dentry, mode, dev); 2505be6d3e56SKentaro Takeda if (error) 2506be6d3e56SKentaro Takeda goto out_drop_write; 25071da177e4SLinus Torvalds switch (mode & S_IFMT) { 25081da177e4SLinus Torvalds case 0: case S_IFREG: 2509dae6ad8fSAl Viro error = vfs_create(path.dentry->d_inode,dentry,mode,NULL); 25101da177e4SLinus Torvalds break; 25111da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 2512dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode, 25131da177e4SLinus Torvalds new_decode_dev(dev)); 25141da177e4SLinus Torvalds break; 25151da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 2516dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); 25171da177e4SLinus Torvalds break; 25181da177e4SLinus Torvalds } 2519be6d3e56SKentaro Takeda out_drop_write: 2520dae6ad8fSAl Viro mnt_drop_write(path.mnt); 2521463c3197SDave Hansen out_dput: 25221da177e4SLinus Torvalds dput(dentry); 2523dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2524dae6ad8fSAl Viro path_put(&path); 25251da177e4SLinus Torvalds 25261da177e4SLinus Torvalds return error; 25271da177e4SLinus Torvalds } 25281da177e4SLinus Torvalds 25293480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev) 25305590ff0dSUlrich Drepper { 25315590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 25325590ff0dSUlrich Drepper } 25335590ff0dSUlrich Drepper 25341da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 25351da177e4SLinus Torvalds { 2536a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 25371da177e4SLinus Torvalds 25381da177e4SLinus Torvalds if (error) 25391da177e4SLinus Torvalds return error; 25401da177e4SLinus Torvalds 2541acfa4380SAl Viro if (!dir->i_op->mkdir) 25421da177e4SLinus Torvalds return -EPERM; 25431da177e4SLinus Torvalds 25441da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 25451da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 25461da177e4SLinus Torvalds if (error) 25471da177e4SLinus Torvalds return error; 25481da177e4SLinus Torvalds 25491da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2550a74574aaSStephen Smalley if (!error) 2551f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 25521da177e4SLinus Torvalds return error; 25531da177e4SLinus Torvalds } 25541da177e4SLinus Torvalds 25552e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode) 25561da177e4SLinus Torvalds { 25576902d925SDave Hansen struct dentry *dentry; 2558dae6ad8fSAl Viro struct path path; 2559dae6ad8fSAl Viro int error; 25601da177e4SLinus Torvalds 2561dae6ad8fSAl Viro dentry = user_path_create(dfd, pathname, &path, 1); 25626902d925SDave Hansen if (IS_ERR(dentry)) 2563dae6ad8fSAl Viro return PTR_ERR(dentry); 25646902d925SDave Hansen 2565dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 2566ce3b0f8dSAl Viro mode &= ~current_umask(); 2567dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 2568463c3197SDave Hansen if (error) 2569463c3197SDave Hansen goto out_dput; 2570dae6ad8fSAl Viro error = security_path_mkdir(&path, dentry, mode); 2571be6d3e56SKentaro Takeda if (error) 2572be6d3e56SKentaro Takeda goto out_drop_write; 2573dae6ad8fSAl Viro error = vfs_mkdir(path.dentry->d_inode, dentry, mode); 2574be6d3e56SKentaro Takeda out_drop_write: 2575dae6ad8fSAl Viro mnt_drop_write(path.mnt); 2576463c3197SDave Hansen out_dput: 25771da177e4SLinus Torvalds dput(dentry); 2578dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2579dae6ad8fSAl Viro path_put(&path); 25801da177e4SLinus Torvalds return error; 25811da177e4SLinus Torvalds } 25821da177e4SLinus Torvalds 25833cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode) 25845590ff0dSUlrich Drepper { 25855590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 25865590ff0dSUlrich Drepper } 25875590ff0dSUlrich Drepper 25881da177e4SLinus Torvalds /* 2589a71905f0SSage Weil * The dentry_unhash() helper will try to drop the dentry early: we 2590a71905f0SSage Weil * should have a usage count of 2 if we're the only user of this 2591a71905f0SSage Weil * dentry, and if that is true (possibly after pruning the dcache), 2592a71905f0SSage Weil * then we drop the dentry now. 25931da177e4SLinus Torvalds * 25941da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 25951da177e4SLinus Torvalds * do a 25961da177e4SLinus Torvalds * 25971da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 25981da177e4SLinus Torvalds * return -EBUSY; 25991da177e4SLinus Torvalds * 26001da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 26011da177e4SLinus Torvalds * that is still in use by something else.. 26021da177e4SLinus Torvalds */ 26031da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 26041da177e4SLinus Torvalds { 26051da177e4SLinus Torvalds shrink_dcache_parent(dentry); 26061da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 260764252c75SSage Weil if (dentry->d_count == 1) 26081da177e4SLinus Torvalds __d_drop(dentry); 26091da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 26101da177e4SLinus Torvalds } 26111da177e4SLinus Torvalds 26121da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 26131da177e4SLinus Torvalds { 26141da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 26151da177e4SLinus Torvalds 26161da177e4SLinus Torvalds if (error) 26171da177e4SLinus Torvalds return error; 26181da177e4SLinus Torvalds 2619acfa4380SAl Viro if (!dir->i_op->rmdir) 26201da177e4SLinus Torvalds return -EPERM; 26211da177e4SLinus Torvalds 26221b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 2623912dbc15SSage Weil 26241da177e4SLinus Torvalds error = -EBUSY; 2625912dbc15SSage Weil if (d_mountpoint(dentry)) 2626912dbc15SSage Weil goto out; 2627912dbc15SSage Weil 26281da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 2629912dbc15SSage Weil if (error) 2630912dbc15SSage Weil goto out; 2631912dbc15SSage Weil 26323cebde24SSage Weil shrink_dcache_parent(dentry); 26331da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 2634912dbc15SSage Weil if (error) 2635912dbc15SSage Weil goto out; 2636912dbc15SSage Weil 26371da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 2638d83c49f3SAl Viro dont_mount(dentry); 26391da177e4SLinus Torvalds 2640912dbc15SSage Weil out: 2641912dbc15SSage Weil mutex_unlock(&dentry->d_inode->i_mutex); 2642912dbc15SSage Weil if (!error) 2643912dbc15SSage Weil d_delete(dentry); 26441da177e4SLinus Torvalds return error; 26451da177e4SLinus Torvalds } 26461da177e4SLinus Torvalds 26475590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 26481da177e4SLinus Torvalds { 26491da177e4SLinus Torvalds int error = 0; 26501da177e4SLinus Torvalds char * name; 26511da177e4SLinus Torvalds struct dentry *dentry; 26521da177e4SLinus Torvalds struct nameidata nd; 26531da177e4SLinus Torvalds 26542ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 26551da177e4SLinus Torvalds if (error) 26562ad94ae6SAl Viro return error; 26571da177e4SLinus Torvalds 26581da177e4SLinus Torvalds switch(nd.last_type) { 26591da177e4SLinus Torvalds case LAST_DOTDOT: 26601da177e4SLinus Torvalds error = -ENOTEMPTY; 26611da177e4SLinus Torvalds goto exit1; 26621da177e4SLinus Torvalds case LAST_DOT: 26631da177e4SLinus Torvalds error = -EINVAL; 26641da177e4SLinus Torvalds goto exit1; 26651da177e4SLinus Torvalds case LAST_ROOT: 26661da177e4SLinus Torvalds error = -EBUSY; 26671da177e4SLinus Torvalds goto exit1; 26681da177e4SLinus Torvalds } 26690612d9fbSOGAWA Hirofumi 26700612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 26710612d9fbSOGAWA Hirofumi 26724ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 267349705b77SChristoph Hellwig dentry = lookup_hash(&nd); 26741da177e4SLinus Torvalds error = PTR_ERR(dentry); 26756902d925SDave Hansen if (IS_ERR(dentry)) 26766902d925SDave Hansen goto exit2; 2677e6bc45d6STheodore Ts'o if (!dentry->d_inode) { 2678e6bc45d6STheodore Ts'o error = -ENOENT; 2679e6bc45d6STheodore Ts'o goto exit3; 2680e6bc45d6STheodore Ts'o } 26810622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 26820622753bSDave Hansen if (error) 26830622753bSDave Hansen goto exit3; 2684be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 2685be6d3e56SKentaro Takeda if (error) 2686be6d3e56SKentaro Takeda goto exit4; 26874ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 2688be6d3e56SKentaro Takeda exit4: 26890622753bSDave Hansen mnt_drop_write(nd.path.mnt); 26900622753bSDave Hansen exit3: 26911da177e4SLinus Torvalds dput(dentry); 26926902d925SDave Hansen exit2: 26934ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 26941da177e4SLinus Torvalds exit1: 26951d957f9bSJan Blunck path_put(&nd.path); 26961da177e4SLinus Torvalds putname(name); 26971da177e4SLinus Torvalds return error; 26981da177e4SLinus Torvalds } 26991da177e4SLinus Torvalds 27003cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 27015590ff0dSUlrich Drepper { 27025590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 27035590ff0dSUlrich Drepper } 27045590ff0dSUlrich Drepper 27051da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 27061da177e4SLinus Torvalds { 27071da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 27081da177e4SLinus Torvalds 27091da177e4SLinus Torvalds if (error) 27101da177e4SLinus Torvalds return error; 27111da177e4SLinus Torvalds 2712acfa4380SAl Viro if (!dir->i_op->unlink) 27131da177e4SLinus Torvalds return -EPERM; 27141da177e4SLinus Torvalds 27151b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 27161da177e4SLinus Torvalds if (d_mountpoint(dentry)) 27171da177e4SLinus Torvalds error = -EBUSY; 27181da177e4SLinus Torvalds else { 27191da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 2720bec1052eSAl Viro if (!error) { 27211da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 2722bec1052eSAl Viro if (!error) 2723d83c49f3SAl Viro dont_mount(dentry); 2724bec1052eSAl Viro } 27251da177e4SLinus Torvalds } 27261b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 27271da177e4SLinus Torvalds 27281da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 27291da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 2730ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 27311da177e4SLinus Torvalds d_delete(dentry); 27321da177e4SLinus Torvalds } 27330eeca283SRobert Love 27341da177e4SLinus Torvalds return error; 27351da177e4SLinus Torvalds } 27361da177e4SLinus Torvalds 27371da177e4SLinus Torvalds /* 27381da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 27391b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 27401da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 27411da177e4SLinus Torvalds * while waiting on the I/O. 27421da177e4SLinus Torvalds */ 27435590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 27441da177e4SLinus Torvalds { 27452ad94ae6SAl Viro int error; 27461da177e4SLinus Torvalds char *name; 27471da177e4SLinus Torvalds struct dentry *dentry; 27481da177e4SLinus Torvalds struct nameidata nd; 27491da177e4SLinus Torvalds struct inode *inode = NULL; 27501da177e4SLinus Torvalds 27512ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 27521da177e4SLinus Torvalds if (error) 27532ad94ae6SAl Viro return error; 27542ad94ae6SAl Viro 27551da177e4SLinus Torvalds error = -EISDIR; 27561da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 27571da177e4SLinus Torvalds goto exit1; 27580612d9fbSOGAWA Hirofumi 27590612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 27600612d9fbSOGAWA Hirofumi 27614ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 276249705b77SChristoph Hellwig dentry = lookup_hash(&nd); 27631da177e4SLinus Torvalds error = PTR_ERR(dentry); 27641da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 27651da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 276650338b88STörök Edwin if (nd.last.name[nd.last.len]) 276750338b88STörök Edwin goto slashes; 27681da177e4SLinus Torvalds inode = dentry->d_inode; 276950338b88STörök Edwin if (!inode) 2770e6bc45d6STheodore Ts'o goto slashes; 27717de9c6eeSAl Viro ihold(inode); 27720622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 27730622753bSDave Hansen if (error) 27740622753bSDave Hansen goto exit2; 2775be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 2776be6d3e56SKentaro Takeda if (error) 2777be6d3e56SKentaro Takeda goto exit3; 27784ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 2779be6d3e56SKentaro Takeda exit3: 27800622753bSDave Hansen mnt_drop_write(nd.path.mnt); 27811da177e4SLinus Torvalds exit2: 27821da177e4SLinus Torvalds dput(dentry); 27831da177e4SLinus Torvalds } 27844ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 27851da177e4SLinus Torvalds if (inode) 27861da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 27871da177e4SLinus Torvalds exit1: 27881d957f9bSJan Blunck path_put(&nd.path); 27891da177e4SLinus Torvalds putname(name); 27901da177e4SLinus Torvalds return error; 27911da177e4SLinus Torvalds 27921da177e4SLinus Torvalds slashes: 27931da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 27941da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 27951da177e4SLinus Torvalds goto exit2; 27961da177e4SLinus Torvalds } 27971da177e4SLinus Torvalds 27982e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 27995590ff0dSUlrich Drepper { 28005590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 28015590ff0dSUlrich Drepper return -EINVAL; 28025590ff0dSUlrich Drepper 28035590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 28045590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 28055590ff0dSUlrich Drepper 28065590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 28075590ff0dSUlrich Drepper } 28085590ff0dSUlrich Drepper 28093480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 28105590ff0dSUlrich Drepper { 28115590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 28125590ff0dSUlrich Drepper } 28135590ff0dSUlrich Drepper 2814db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 28151da177e4SLinus Torvalds { 2816a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 28171da177e4SLinus Torvalds 28181da177e4SLinus Torvalds if (error) 28191da177e4SLinus Torvalds return error; 28201da177e4SLinus Torvalds 2821acfa4380SAl Viro if (!dir->i_op->symlink) 28221da177e4SLinus Torvalds return -EPERM; 28231da177e4SLinus Torvalds 28241da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 28251da177e4SLinus Torvalds if (error) 28261da177e4SLinus Torvalds return error; 28271da177e4SLinus Torvalds 28281da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 2829a74574aaSStephen Smalley if (!error) 2830f38aa942SAmy Griffis fsnotify_create(dir, dentry); 28311da177e4SLinus Torvalds return error; 28321da177e4SLinus Torvalds } 28331da177e4SLinus Torvalds 28342e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 28352e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 28361da177e4SLinus Torvalds { 28372ad94ae6SAl Viro int error; 28381da177e4SLinus Torvalds char *from; 28396902d925SDave Hansen struct dentry *dentry; 2840dae6ad8fSAl Viro struct path path; 28411da177e4SLinus Torvalds 28421da177e4SLinus Torvalds from = getname(oldname); 28431da177e4SLinus Torvalds if (IS_ERR(from)) 28441da177e4SLinus Torvalds return PTR_ERR(from); 28452ad94ae6SAl Viro 2846dae6ad8fSAl Viro dentry = user_path_create(newdfd, newname, &path, 0); 28471da177e4SLinus Torvalds error = PTR_ERR(dentry); 28486902d925SDave Hansen if (IS_ERR(dentry)) 2849dae6ad8fSAl Viro goto out_putname; 28506902d925SDave Hansen 2851dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 285275c3f29dSDave Hansen if (error) 285375c3f29dSDave Hansen goto out_dput; 2854dae6ad8fSAl Viro error = security_path_symlink(&path, dentry, from); 2855be6d3e56SKentaro Takeda if (error) 2856be6d3e56SKentaro Takeda goto out_drop_write; 2857dae6ad8fSAl Viro error = vfs_symlink(path.dentry->d_inode, dentry, from); 2858be6d3e56SKentaro Takeda out_drop_write: 2859dae6ad8fSAl Viro mnt_drop_write(path.mnt); 286075c3f29dSDave Hansen out_dput: 28611da177e4SLinus Torvalds dput(dentry); 2862dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2863dae6ad8fSAl Viro path_put(&path); 28646902d925SDave Hansen out_putname: 28651da177e4SLinus Torvalds putname(from); 28661da177e4SLinus Torvalds return error; 28671da177e4SLinus Torvalds } 28681da177e4SLinus Torvalds 28693480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 28705590ff0dSUlrich Drepper { 28715590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 28725590ff0dSUlrich Drepper } 28735590ff0dSUlrich Drepper 28741da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 28751da177e4SLinus Torvalds { 28761da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 28771da177e4SLinus Torvalds int error; 28781da177e4SLinus Torvalds 28791da177e4SLinus Torvalds if (!inode) 28801da177e4SLinus Torvalds return -ENOENT; 28811da177e4SLinus Torvalds 2882a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 28831da177e4SLinus Torvalds if (error) 28841da177e4SLinus Torvalds return error; 28851da177e4SLinus Torvalds 28861da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 28871da177e4SLinus Torvalds return -EXDEV; 28881da177e4SLinus Torvalds 28891da177e4SLinus Torvalds /* 28901da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 28911da177e4SLinus Torvalds */ 28921da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 28931da177e4SLinus Torvalds return -EPERM; 2894acfa4380SAl Viro if (!dir->i_op->link) 28951da177e4SLinus Torvalds return -EPERM; 28967e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 28971da177e4SLinus Torvalds return -EPERM; 28981da177e4SLinus Torvalds 28991da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 29001da177e4SLinus Torvalds if (error) 29011da177e4SLinus Torvalds return error; 29021da177e4SLinus Torvalds 29037e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 2904aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 2905aae8a97dSAneesh Kumar K.V if (inode->i_nlink == 0) 2906aae8a97dSAneesh Kumar K.V error = -ENOENT; 2907aae8a97dSAneesh Kumar K.V else 29081da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 29097e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 2910e31e14ecSStephen Smalley if (!error) 29117e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 29121da177e4SLinus Torvalds return error; 29131da177e4SLinus Torvalds } 29141da177e4SLinus Torvalds 29151da177e4SLinus Torvalds /* 29161da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 29171da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 29181da177e4SLinus Torvalds * newname. --KAB 29191da177e4SLinus Torvalds * 29201da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 29211da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 29221da177e4SLinus Torvalds * and other special files. --ADM 29231da177e4SLinus Torvalds */ 29242e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 29252e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 29261da177e4SLinus Torvalds { 29271da177e4SLinus Torvalds struct dentry *new_dentry; 2928dae6ad8fSAl Viro struct path old_path, new_path; 292911a7b371SAneesh Kumar K.V int how = 0; 29301da177e4SLinus Torvalds int error; 29311da177e4SLinus Torvalds 293211a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 2933c04030e1SUlrich Drepper return -EINVAL; 293411a7b371SAneesh Kumar K.V /* 293511a7b371SAneesh Kumar K.V * To use null names we require CAP_DAC_READ_SEARCH 293611a7b371SAneesh Kumar K.V * This ensures that not everyone will be able to create 293711a7b371SAneesh Kumar K.V * handlink using the passed filedescriptor. 293811a7b371SAneesh Kumar K.V */ 293911a7b371SAneesh Kumar K.V if (flags & AT_EMPTY_PATH) { 294011a7b371SAneesh Kumar K.V if (!capable(CAP_DAC_READ_SEARCH)) 294111a7b371SAneesh Kumar K.V return -ENOENT; 294211a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 294311a7b371SAneesh Kumar K.V } 2944c04030e1SUlrich Drepper 294511a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 294611a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 294711a7b371SAneesh Kumar K.V 294811a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 29491da177e4SLinus Torvalds if (error) 29502ad94ae6SAl Viro return error; 29512ad94ae6SAl Viro 2952dae6ad8fSAl Viro new_dentry = user_path_create(newdfd, newname, &new_path, 0); 29531da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 29546902d925SDave Hansen if (IS_ERR(new_dentry)) 2955dae6ad8fSAl Viro goto out; 2956dae6ad8fSAl Viro 2957dae6ad8fSAl Viro error = -EXDEV; 2958dae6ad8fSAl Viro if (old_path.mnt != new_path.mnt) 2959dae6ad8fSAl Viro goto out_dput; 2960dae6ad8fSAl Viro error = mnt_want_write(new_path.mnt); 296175c3f29dSDave Hansen if (error) 296275c3f29dSDave Hansen goto out_dput; 2963dae6ad8fSAl Viro error = security_path_link(old_path.dentry, &new_path, new_dentry); 2964be6d3e56SKentaro Takeda if (error) 2965be6d3e56SKentaro Takeda goto out_drop_write; 2966dae6ad8fSAl Viro error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry); 2967be6d3e56SKentaro Takeda out_drop_write: 2968dae6ad8fSAl Viro mnt_drop_write(new_path.mnt); 296975c3f29dSDave Hansen out_dput: 29701da177e4SLinus Torvalds dput(new_dentry); 2971dae6ad8fSAl Viro mutex_unlock(&new_path.dentry->d_inode->i_mutex); 2972dae6ad8fSAl Viro path_put(&new_path); 29731da177e4SLinus Torvalds out: 29742d8f3038SAl Viro path_put(&old_path); 29751da177e4SLinus Torvalds 29761da177e4SLinus Torvalds return error; 29771da177e4SLinus Torvalds } 29781da177e4SLinus Torvalds 29793480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 29805590ff0dSUlrich Drepper { 2981c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 29825590ff0dSUlrich Drepper } 29835590ff0dSUlrich Drepper 29841da177e4SLinus Torvalds /* 29851da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 29861da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 29871da177e4SLinus Torvalds * Problems: 29881da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 29891da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 29901da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 2991a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 29921da177e4SLinus Torvalds * story. 29931da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 29941b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 29951da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 29961da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 2997a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 29981da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 29991da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 30001da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 3001a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 30021da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 30031da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 30041da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 3005e4eaac06SSage Weil * d) conversion from fhandle to dentry may come in the wrong moment - when 30061b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 30071da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 3008c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 30091da177e4SLinus Torvalds * locking]. 30101da177e4SLinus Torvalds */ 301175c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 30121da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30131da177e4SLinus Torvalds { 30141da177e4SLinus Torvalds int error = 0; 30159055cba7SSage Weil struct inode *target = new_dentry->d_inode; 30161da177e4SLinus Torvalds 30171da177e4SLinus Torvalds /* 30181da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 30191da177e4SLinus Torvalds * we'll need to flip '..'. 30201da177e4SLinus Torvalds */ 30211da177e4SLinus Torvalds if (new_dir != old_dir) { 3022f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 30231da177e4SLinus Torvalds if (error) 30241da177e4SLinus Torvalds return error; 30251da177e4SLinus Torvalds } 30261da177e4SLinus Torvalds 30271da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 30281da177e4SLinus Torvalds if (error) 30291da177e4SLinus Torvalds return error; 30301da177e4SLinus Torvalds 3031d83c49f3SAl Viro if (target) 30321b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 30339055cba7SSage Weil 30341da177e4SLinus Torvalds error = -EBUSY; 30359055cba7SSage Weil if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) 30369055cba7SSage Weil goto out; 30379055cba7SSage Weil 30383cebde24SSage Weil if (target) 30393cebde24SSage Weil shrink_dcache_parent(new_dentry); 30401da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 30419055cba7SSage Weil if (error) 30429055cba7SSage Weil goto out; 30439055cba7SSage Weil 30441da177e4SLinus Torvalds if (target) { 30451da177e4SLinus Torvalds target->i_flags |= S_DEAD; 3046d83c49f3SAl Viro dont_mount(new_dentry); 3047d83c49f3SAl Viro } 30489055cba7SSage Weil out: 30499055cba7SSage Weil if (target) 30501b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 3051e31e14ecSStephen Smalley if (!error) 3052349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 30531da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 30541da177e4SLinus Torvalds return error; 30551da177e4SLinus Torvalds } 30561da177e4SLinus Torvalds 305775c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 30581da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30591da177e4SLinus Torvalds { 306051892bbbSSage Weil struct inode *target = new_dentry->d_inode; 30611da177e4SLinus Torvalds int error; 30621da177e4SLinus Torvalds 30631da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 30641da177e4SLinus Torvalds if (error) 30651da177e4SLinus Torvalds return error; 30661da177e4SLinus Torvalds 30671da177e4SLinus Torvalds dget(new_dentry); 30681da177e4SLinus Torvalds if (target) 30691b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 307051892bbbSSage Weil 30711da177e4SLinus Torvalds error = -EBUSY; 307251892bbbSSage Weil if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 307351892bbbSSage Weil goto out; 307451892bbbSSage Weil 30751da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 307651892bbbSSage Weil if (error) 307751892bbbSSage Weil goto out; 307851892bbbSSage Weil 3079bec1052eSAl Viro if (target) 3080d83c49f3SAl Viro dont_mount(new_dentry); 3081349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 30821da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 308351892bbbSSage Weil out: 30841da177e4SLinus Torvalds if (target) 30851b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 30861da177e4SLinus Torvalds dput(new_dentry); 30871da177e4SLinus Torvalds return error; 30881da177e4SLinus Torvalds } 30891da177e4SLinus Torvalds 30901da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 30911da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30921da177e4SLinus Torvalds { 30931da177e4SLinus Torvalds int error; 30941da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 309559b0df21SEric Paris const unsigned char *old_name; 30961da177e4SLinus Torvalds 30971da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 30981da177e4SLinus Torvalds return 0; 30991da177e4SLinus Torvalds 31001da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 31011da177e4SLinus Torvalds if (error) 31021da177e4SLinus Torvalds return error; 31031da177e4SLinus Torvalds 31041da177e4SLinus Torvalds if (!new_dentry->d_inode) 3105a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 31061da177e4SLinus Torvalds else 31071da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 31081da177e4SLinus Torvalds if (error) 31091da177e4SLinus Torvalds return error; 31101da177e4SLinus Torvalds 3111acfa4380SAl Viro if (!old_dir->i_op->rename) 31121da177e4SLinus Torvalds return -EPERM; 31131da177e4SLinus Torvalds 31140eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 31150eeca283SRobert Love 31161da177e4SLinus Torvalds if (is_dir) 31171da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 31181da177e4SLinus Torvalds else 31191da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3120123df294SAl Viro if (!error) 3121123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 31225a190ae6SAl Viro new_dentry->d_inode, old_dentry); 31230eeca283SRobert Love fsnotify_oldname_free(old_name); 31240eeca283SRobert Love 31251da177e4SLinus Torvalds return error; 31261da177e4SLinus Torvalds } 31271da177e4SLinus Torvalds 31282e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 31292e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 31301da177e4SLinus Torvalds { 31311da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 31321da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 31331da177e4SLinus Torvalds struct dentry *trap; 31341da177e4SLinus Torvalds struct nameidata oldnd, newnd; 31352ad94ae6SAl Viro char *from; 31362ad94ae6SAl Viro char *to; 31372ad94ae6SAl Viro int error; 31381da177e4SLinus Torvalds 31392ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 31401da177e4SLinus Torvalds if (error) 31411da177e4SLinus Torvalds goto exit; 31421da177e4SLinus Torvalds 31432ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 31441da177e4SLinus Torvalds if (error) 31451da177e4SLinus Torvalds goto exit1; 31461da177e4SLinus Torvalds 31471da177e4SLinus Torvalds error = -EXDEV; 31484ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 31491da177e4SLinus Torvalds goto exit2; 31501da177e4SLinus Torvalds 31514ac91378SJan Blunck old_dir = oldnd.path.dentry; 31521da177e4SLinus Torvalds error = -EBUSY; 31531da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 31541da177e4SLinus Torvalds goto exit2; 31551da177e4SLinus Torvalds 31564ac91378SJan Blunck new_dir = newnd.path.dentry; 31571da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 31581da177e4SLinus Torvalds goto exit2; 31591da177e4SLinus Torvalds 31600612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 31610612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 31624e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 31630612d9fbSOGAWA Hirofumi 31641da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 31651da177e4SLinus Torvalds 316649705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 31671da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 31681da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 31691da177e4SLinus Torvalds goto exit3; 31701da177e4SLinus Torvalds /* source must exist */ 31711da177e4SLinus Torvalds error = -ENOENT; 31721da177e4SLinus Torvalds if (!old_dentry->d_inode) 31731da177e4SLinus Torvalds goto exit4; 31741da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 31751da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 31761da177e4SLinus Torvalds error = -ENOTDIR; 31771da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 31781da177e4SLinus Torvalds goto exit4; 31791da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 31801da177e4SLinus Torvalds goto exit4; 31811da177e4SLinus Torvalds } 31821da177e4SLinus Torvalds /* source should not be ancestor of target */ 31831da177e4SLinus Torvalds error = -EINVAL; 31841da177e4SLinus Torvalds if (old_dentry == trap) 31851da177e4SLinus Torvalds goto exit4; 318649705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 31871da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 31881da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 31891da177e4SLinus Torvalds goto exit4; 31901da177e4SLinus Torvalds /* target should not be an ancestor of source */ 31911da177e4SLinus Torvalds error = -ENOTEMPTY; 31921da177e4SLinus Torvalds if (new_dentry == trap) 31931da177e4SLinus Torvalds goto exit5; 31941da177e4SLinus Torvalds 31959079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 31969079b1ebSDave Hansen if (error) 31979079b1ebSDave Hansen goto exit5; 3198be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3199be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3200be6d3e56SKentaro Takeda if (error) 3201be6d3e56SKentaro Takeda goto exit6; 32021da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 32031da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 3204be6d3e56SKentaro Takeda exit6: 32059079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 32061da177e4SLinus Torvalds exit5: 32071da177e4SLinus Torvalds dput(new_dentry); 32081da177e4SLinus Torvalds exit4: 32091da177e4SLinus Torvalds dput(old_dentry); 32101da177e4SLinus Torvalds exit3: 32111da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 32121da177e4SLinus Torvalds exit2: 32131d957f9bSJan Blunck path_put(&newnd.path); 32142ad94ae6SAl Viro putname(to); 32151da177e4SLinus Torvalds exit1: 32161d957f9bSJan Blunck path_put(&oldnd.path); 32171da177e4SLinus Torvalds putname(from); 32182ad94ae6SAl Viro exit: 32191da177e4SLinus Torvalds return error; 32201da177e4SLinus Torvalds } 32211da177e4SLinus Torvalds 3222a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 32235590ff0dSUlrich Drepper { 32245590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 32255590ff0dSUlrich Drepper } 32265590ff0dSUlrich Drepper 32271da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 32281da177e4SLinus Torvalds { 32291da177e4SLinus Torvalds int len; 32301da177e4SLinus Torvalds 32311da177e4SLinus Torvalds len = PTR_ERR(link); 32321da177e4SLinus Torvalds if (IS_ERR(link)) 32331da177e4SLinus Torvalds goto out; 32341da177e4SLinus Torvalds 32351da177e4SLinus Torvalds len = strlen(link); 32361da177e4SLinus Torvalds if (len > (unsigned) buflen) 32371da177e4SLinus Torvalds len = buflen; 32381da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 32391da177e4SLinus Torvalds len = -EFAULT; 32401da177e4SLinus Torvalds out: 32411da177e4SLinus Torvalds return len; 32421da177e4SLinus Torvalds } 32431da177e4SLinus Torvalds 32441da177e4SLinus Torvalds /* 32451da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 32461da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 32471da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 32481da177e4SLinus Torvalds */ 32491da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 32501da177e4SLinus Torvalds { 32511da177e4SLinus Torvalds struct nameidata nd; 3252cc314eefSLinus Torvalds void *cookie; 3253694a1764SMarcin Slusarz int res; 3254cc314eefSLinus Torvalds 32551da177e4SLinus Torvalds nd.depth = 0; 3256cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3257694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3258694a1764SMarcin Slusarz return PTR_ERR(cookie); 3259694a1764SMarcin Slusarz 3260694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 32611da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3262cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3263694a1764SMarcin Slusarz return res; 32641da177e4SLinus Torvalds } 32651da177e4SLinus Torvalds 32661da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 32671da177e4SLinus Torvalds { 32681da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 32691da177e4SLinus Torvalds } 32701da177e4SLinus Torvalds 32711da177e4SLinus Torvalds /* get the link contents into pagecache */ 32721da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 32731da177e4SLinus Torvalds { 3274ebd09abbSDuane Griffin char *kaddr; 32751da177e4SLinus Torvalds struct page *page; 32761da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3277090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 32781da177e4SLinus Torvalds if (IS_ERR(page)) 32796fe6900eSNick Piggin return (char*)page; 32801da177e4SLinus Torvalds *ppage = page; 3281ebd09abbSDuane Griffin kaddr = kmap(page); 3282ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3283ebd09abbSDuane Griffin return kaddr; 32841da177e4SLinus Torvalds } 32851da177e4SLinus Torvalds 32861da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 32871da177e4SLinus Torvalds { 32881da177e4SLinus Torvalds struct page *page = NULL; 32891da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 32901da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 32911da177e4SLinus Torvalds if (page) { 32921da177e4SLinus Torvalds kunmap(page); 32931da177e4SLinus Torvalds page_cache_release(page); 32941da177e4SLinus Torvalds } 32951da177e4SLinus Torvalds return res; 32961da177e4SLinus Torvalds } 32971da177e4SLinus Torvalds 3298cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 32991da177e4SLinus Torvalds { 3300cc314eefSLinus Torvalds struct page *page = NULL; 33011da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 3302cc314eefSLinus Torvalds return page; 33031da177e4SLinus Torvalds } 33041da177e4SLinus Torvalds 3305cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 33061da177e4SLinus Torvalds { 3307cc314eefSLinus Torvalds struct page *page = cookie; 3308cc314eefSLinus Torvalds 3309cc314eefSLinus Torvalds if (page) { 33101da177e4SLinus Torvalds kunmap(page); 33111da177e4SLinus Torvalds page_cache_release(page); 33121da177e4SLinus Torvalds } 33131da177e4SLinus Torvalds } 33141da177e4SLinus Torvalds 331554566b2cSNick Piggin /* 331654566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 331754566b2cSNick Piggin */ 331854566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 33191da177e4SLinus Torvalds { 33201da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 33210adb25d2SKirill Korotaev struct page *page; 3322afddba49SNick Piggin void *fsdata; 3323beb497abSDmitriy Monakhov int err; 33241da177e4SLinus Torvalds char *kaddr; 332554566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 332654566b2cSNick Piggin if (nofs) 332754566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 33281da177e4SLinus Torvalds 33297e53cac4SNeilBrown retry: 3330afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 333154566b2cSNick Piggin flags, &page, &fsdata); 33321da177e4SLinus Torvalds if (err) 3333afddba49SNick Piggin goto fail; 3334afddba49SNick Piggin 33351da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 33361da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 33371da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 3338afddba49SNick Piggin 3339afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 3340afddba49SNick Piggin page, fsdata); 33411da177e4SLinus Torvalds if (err < 0) 33421da177e4SLinus Torvalds goto fail; 3343afddba49SNick Piggin if (err < len-1) 3344afddba49SNick Piggin goto retry; 3345afddba49SNick Piggin 33461da177e4SLinus Torvalds mark_inode_dirty(inode); 33471da177e4SLinus Torvalds return 0; 33481da177e4SLinus Torvalds fail: 33491da177e4SLinus Torvalds return err; 33501da177e4SLinus Torvalds } 33511da177e4SLinus Torvalds 33520adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 33530adb25d2SKirill Korotaev { 33540adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 335554566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 33560adb25d2SKirill Korotaev } 33570adb25d2SKirill Korotaev 335892e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 33591da177e4SLinus Torvalds .readlink = generic_readlink, 33601da177e4SLinus Torvalds .follow_link = page_follow_link_light, 33611da177e4SLinus Torvalds .put_link = page_put_link, 33621da177e4SLinus Torvalds }; 33631da177e4SLinus Torvalds 33642d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 3365cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one); 33661da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 33671da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 33681da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 33691da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 33701da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 33711da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 33721da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 33731da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 33741da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 33750adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 33761da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 33771da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 3378d1811465SAl Viro EXPORT_SYMBOL(kern_path); 337916f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 3380f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 33811da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 33821da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 33831da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 33841da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 33851da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 33861da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 33871da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 33881da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 33891da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 33901da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 33911da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 33921da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 33931da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 33941da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 3395